summaryrefslogtreecommitdiffstats
path: root/MdePkg/Library/BaseSerialPortLibRiscVSbiLib/Common.c
blob: 4926ac675b62df21543c2c3b93e92fd2307829de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/** @file
  Serial Port Library backed by SBI console.

  Common functionality shared by PrePiDxeSerialPortLibRiscVSbi and
  PrePiDxeSerialPortLibRiscVSbiRam implementations.

  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "Common.h"

/**
  Return whether the DBCN extension is implemented.

  @retval TRUE                  Extension is implemented.
  @retval FALSE                 Extension is not implemented.

**/
BOOLEAN
SbiImplementsDbcn (
  VOID
  )
{
  SBI_RET  Ret;

  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_DBCN);
  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
      (Ret.Value != 0))
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Return whether the legacy console putchar extension is implemented.

  @retval TRUE                  Extension is implemented.
  @retval FALSE                 Extension is not implemented.

**/
BOOLEAN
SbiImplementsLegacyPutchar (
  VOID
  )
{
  SBI_RET  Ret;

  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
      (Ret.Value != 0))
  {
    return TRUE;
  }

  return FALSE;
}

/**
  Write data from buffer to console via SBI legacy putchar extension.

  The number of bytes actually written to the SBI console is returned.
  If the return value is less than NumberOfBytes, then the write operation failed.

  @param  Buffer           The pointer to the data buffer to be written.
  @param  NumberOfBytes    The number of bytes to written to the serial device.

  @retval >=0              The number of bytes written to the serial device.
                           If this value is less than NumberOfBytes, then the
                           write operation failed.

**/
UINTN
SbiLegacyPutchar (
  IN  UINT8  *Buffer,
  IN  UINTN  NumberOfBytes
  )
{
  SBI_RET  Ret;
  UINTN    Index;

  for (Index = 0; Index < NumberOfBytes; Index++) {
    Ret =  SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
    if ((INT64)Ret.Error < 0) {
      break;
    }
  }

  return Index;
}

/**
  Write data from buffer to console via SBI DBCN.

  The number of bytes actually written to the SBI console is returned.
  If the return value is less than NumberOfBytes, then the write operation failed.

  @param  Buffer           The pointer to the data buffer to be written.
  @param  NumberOfBytes    The number of bytes to written to the serial device.

  @retval >=0              The number of bytes written to the serial device.
                           If this value is less than NumberOfBytes, then the
                           write operation failed.

**/
UINTN
SbiDbcnWrite (
  IN  UINT8  *Buffer,
  IN  UINTN  NumberOfBytes
  )
{
  SBI_RET  Ret;

  Ret = SbiCall (
          SBI_EXT_DBCN,
          SBI_EXT_DBCN_WRITE,
          3,
          NumberOfBytes,
          ((UINTN)Buffer),
          0
          );

  /*
   * May do partial writes. Don't bother decoding
   * Ret.Error as we're only interested in number of
   * bytes written to console.
   */
  return Ret.Value;
}