1 | /** @file
|
---|
2 | Serial Port Library backed by SBI console.
|
---|
3 |
|
---|
4 | Meant for PrePi and DXE environments (where globals are allowed). See
|
---|
5 | BaseSerialPortLibRiscVSbiLib.c for a reduced variant appropriate for
|
---|
6 | SEC and PEI (XIP) environments.
|
---|
7 |
|
---|
8 | Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
|
---|
9 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
10 |
|
---|
11 | **/
|
---|
12 |
|
---|
13 | #include <Base.h>
|
---|
14 | #include <Library/SerialPortLib.h>
|
---|
15 | #include <Library/BaseRiscVSbiLib.h>
|
---|
16 | #include "Common.h"
|
---|
17 |
|
---|
18 | STATIC BOOLEAN mHaveDbcn = FALSE;
|
---|
19 | STATIC BOOLEAN mHaveLegacyPutchar = FALSE;
|
---|
20 | STATIC BOOLEAN mHaveLegacyGetchar = FALSE;
|
---|
21 | STATIC INT64 mLastGetChar = -1;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | Return whether the legacy console getchar extension is implemented.
|
---|
25 |
|
---|
26 | @retval TRUE Extension is implemented.
|
---|
27 | @retval FALSE Extension is not implemented.
|
---|
28 |
|
---|
29 | **/
|
---|
30 | STATIC
|
---|
31 | BOOLEAN
|
---|
32 | SbiImplementsLegacyGetchar (
|
---|
33 | VOID
|
---|
34 | )
|
---|
35 | {
|
---|
36 | SBI_RET Ret;
|
---|
37 |
|
---|
38 | Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
|
---|
39 | if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
|
---|
40 | (Ret.Value != 0))
|
---|
41 | {
|
---|
42 | return TRUE;
|
---|
43 | }
|
---|
44 |
|
---|
45 | return FALSE;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | Initialize the serial device hardware.
|
---|
50 |
|
---|
51 | If no initialization is required, then return RETURN_SUCCESS.
|
---|
52 | If the serial device was successfully initialized, then return RETURN_SUCCESS.
|
---|
53 | If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
|
---|
54 |
|
---|
55 | @retval RETURN_SUCCESS The serial device was initialized.
|
---|
56 | @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
|
---|
57 |
|
---|
58 | **/
|
---|
59 | RETURN_STATUS
|
---|
60 | EFIAPI
|
---|
61 | SerialPortInitialize (
|
---|
62 | VOID
|
---|
63 | )
|
---|
64 | {
|
---|
65 | if (SbiImplementsDbcn ()) {
|
---|
66 | mHaveDbcn = TRUE;
|
---|
67 | return RETURN_SUCCESS;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (SbiImplementsLegacyPutchar ()) {
|
---|
71 | mHaveLegacyPutchar = TRUE;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (SbiImplementsLegacyGetchar ()) {
|
---|
75 | mHaveLegacyGetchar = TRUE;
|
---|
76 | }
|
---|
77 |
|
---|
78 | return (mHaveLegacyGetchar && mHaveLegacyPutchar) ?
|
---|
79 | RETURN_SUCCESS :
|
---|
80 | RETURN_DEVICE_ERROR;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | Write data from buffer to serial device.
|
---|
85 |
|
---|
86 | Writes NumberOfBytes data bytes from Buffer to the serial device.
|
---|
87 | The number of bytes actually written to the serial device is returned.
|
---|
88 | If the return value is less than NumberOfBytes, then the write operation failed.
|
---|
89 | If NumberOfBytes is zero, then return 0.
|
---|
90 |
|
---|
91 | @param Buffer The pointer to the data buffer to be written.
|
---|
92 | @param NumberOfBytes The number of bytes to written to the serial device.
|
---|
93 |
|
---|
94 | @retval 0 NumberOfBytes is 0.
|
---|
95 | @retval >0 The number of bytes written to the serial device.
|
---|
96 | If this value is less than NumberOfBytes, then the write operation failed.
|
---|
97 |
|
---|
98 | **/
|
---|
99 | UINTN
|
---|
100 | EFIAPI
|
---|
101 | SerialPortWrite (
|
---|
102 | IN UINT8 *Buffer,
|
---|
103 | IN UINTN NumberOfBytes
|
---|
104 | )
|
---|
105 | {
|
---|
106 | if (NumberOfBytes == 0) {
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (mHaveDbcn) {
|
---|
111 | return SbiDbcnWrite (Buffer, NumberOfBytes);
|
---|
112 | } else if (mHaveLegacyPutchar) {
|
---|
113 | return SbiLegacyPutchar (Buffer, NumberOfBytes);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Neither DBCN or legacy extension were present.
|
---|
118 | */
|
---|
119 | return 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | Read data from serial device and save the datas in buffer.
|
---|
124 |
|
---|
125 | Reads NumberOfBytes data bytes from a serial device into the buffer
|
---|
126 | specified by Buffer. The number of bytes actually read is returned.
|
---|
127 | If the return value is less than NumberOfBytes, then the rest operation failed.
|
---|
128 | If NumberOfBytes is zero, then return 0.
|
---|
129 |
|
---|
130 | @param Buffer The pointer to the data buffer to store the data read from the serial device.
|
---|
131 | @param NumberOfBytes The number of bytes which will be read.
|
---|
132 |
|
---|
133 | @retval 0 Read data failed; No data is to be read.
|
---|
134 | @retval >0 The actual number of bytes read from serial device.
|
---|
135 |
|
---|
136 | **/
|
---|
137 | UINTN
|
---|
138 | EFIAPI
|
---|
139 | SerialPortRead (
|
---|
140 | OUT UINT8 *Buffer,
|
---|
141 | IN UINTN NumberOfBytes
|
---|
142 | )
|
---|
143 | {
|
---|
144 | UINTN Index;
|
---|
145 |
|
---|
146 | Index = 0;
|
---|
147 | while ((Index < NumberOfBytes) && SerialPortPoll ()) {
|
---|
148 | Buffer[Index++] = (UINT8)mLastGetChar;
|
---|
149 | mLastGetChar = -1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | return Index;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | Polls a serial device to see if there is any data waiting to be read.
|
---|
157 |
|
---|
158 | Polls a serial device to see if there is any data waiting to be read.
|
---|
159 | If there is data waiting to be read from the serial device, then TRUE is returned.
|
---|
160 | If there is no data waiting to be read from the serial device, then FALSE is returned.
|
---|
161 |
|
---|
162 | @retval TRUE Data is waiting to be read from the serial device.
|
---|
163 | @retval FALSE There is no data waiting to be read from the serial device.
|
---|
164 |
|
---|
165 | **/
|
---|
166 | BOOLEAN
|
---|
167 | EFIAPI
|
---|
168 | SerialPortPoll (
|
---|
169 | VOID
|
---|
170 | )
|
---|
171 | {
|
---|
172 | /*
|
---|
173 | * Careful. OpenSBI with HTIF console will return -1 followed by -2
|
---|
174 | * if there is no character received. So just check for values >= 0.
|
---|
175 | */
|
---|
176 |
|
---|
177 | if (mLastGetChar >= 0) {
|
---|
178 | return TRUE;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (mHaveDbcn) {
|
---|
182 | UINT8 Buffer;
|
---|
183 | SBI_RET Ret;
|
---|
184 |
|
---|
185 | Ret = SbiCall (
|
---|
186 | SBI_EXT_DBCN,
|
---|
187 | SBI_EXT_DBCN_READ,
|
---|
188 | 3,
|
---|
189 | 1,
|
---|
190 | ((UINTN)&Buffer),
|
---|
191 | 0
|
---|
192 | );
|
---|
193 | if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
|
---|
194 | (Ret.Value == 1))
|
---|
195 | {
|
---|
196 | mLastGetChar = Buffer;
|
---|
197 | }
|
---|
198 | } else if (mHaveLegacyGetchar) {
|
---|
199 | mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0).Error;
|
---|
200 | }
|
---|
201 |
|
---|
202 | return mLastGetChar >= 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | Sets the control bits on a serial device.
|
---|
207 |
|
---|
208 | @param Control Sets the bits of Control that are settable.
|
---|
209 |
|
---|
210 | @retval RETURN_SUCCESS The new control bits were set on the serial device.
|
---|
211 | @retval RETURN_UNSUPPORTED The serial device does not support this operation.
|
---|
212 | @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
|
---|
213 |
|
---|
214 | **/
|
---|
215 | RETURN_STATUS
|
---|
216 | EFIAPI
|
---|
217 | SerialPortSetControl (
|
---|
218 | IN UINT32 Control
|
---|
219 | )
|
---|
220 | {
|
---|
221 | return RETURN_SUCCESS;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | Retrieve the status of the control bits on a serial device.
|
---|
226 |
|
---|
227 | @param Control A pointer to return the current control signals from the serial device.
|
---|
228 |
|
---|
229 | @retval RETURN_SUCCESS The control bits were read from the serial device.
|
---|
230 | @retval RETURN_UNSUPPORTED The serial device does not support this operation.
|
---|
231 | @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
|
---|
232 |
|
---|
233 | **/
|
---|
234 | RETURN_STATUS
|
---|
235 | EFIAPI
|
---|
236 | SerialPortGetControl (
|
---|
237 | OUT UINT32 *Control
|
---|
238 | )
|
---|
239 | {
|
---|
240 | *Control = 0;
|
---|
241 | return RETURN_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
|
---|
246 | data bits, and stop bits on a serial device.
|
---|
247 |
|
---|
248 | @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
|
---|
249 | device's default interface speed.
|
---|
250 | On output, the value actually set.
|
---|
251 | @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
|
---|
252 | serial interface. A ReceiveFifoDepth value of 0 will use
|
---|
253 | the device's default FIFO depth.
|
---|
254 | On output, the value actually set.
|
---|
255 | @param Timeout The requested time out for a single character in microseconds.
|
---|
256 | This timeout applies to both the transmit and receive side of the
|
---|
257 | interface. A Timeout value of 0 will use the device's default time
|
---|
258 | out value.
|
---|
259 | On output, the value actually set.
|
---|
260 | @param Parity The type of parity to use on this serial device. A Parity value of
|
---|
261 | DefaultParity will use the device's default parity value.
|
---|
262 | On output, the value actually set.
|
---|
263 | @param DataBits The number of data bits to use on the serial device. A DataBits
|
---|
264 | vaule of 0 will use the device's default data bit setting.
|
---|
265 | On output, the value actually set.
|
---|
266 | @param StopBits The number of stop bits to use on this serial device. A StopBits
|
---|
267 | value of DefaultStopBits will use the device's default number of
|
---|
268 | stop bits.
|
---|
269 | On output, the value actually set.
|
---|
270 |
|
---|
271 | @retval RETURN_SUCCESS The new attributes were set on the serial device.
|
---|
272 | @retval RETURN_UNSUPPORTED The serial device does not support this operation.
|
---|
273 | @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
|
---|
274 | @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
|
---|
275 |
|
---|
276 | **/
|
---|
277 | RETURN_STATUS
|
---|
278 | EFIAPI
|
---|
279 | SerialPortSetAttributes (
|
---|
280 | IN OUT UINT64 *BaudRate,
|
---|
281 | IN OUT UINT32 *ReceiveFifoDepth,
|
---|
282 | IN OUT UINT32 *Timeout,
|
---|
283 | IN OUT EFI_PARITY_TYPE *Parity,
|
---|
284 | IN OUT UINT8 *DataBits,
|
---|
285 | IN OUT EFI_STOP_BITS_TYPE *StopBits
|
---|
286 | )
|
---|
287 | {
|
---|
288 | return RETURN_SUCCESS;
|
---|
289 | }
|
---|