2 Serial driver that layers on top of a Serial Port Library instance.
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/SerialPortLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PcdLib.h>
23 #include <Protocol/SerialIo.h>
24 #include <Protocol/DevicePath.h>
27 VENDOR_DEVICE_PATH Guid
;
28 UART_DEVICE_PATH Uart
;
29 EFI_DEVICE_PATH_PROTOCOL End
;
33 Reset the serial device.
35 @param This Protocol instance pointer.
37 @retval EFI_SUCCESS The device was reset.
38 @retval EFI_DEVICE_ERROR The serial device could not be reset.
44 IN EFI_SERIAL_IO_PROTOCOL
*This
48 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
49 data bits, and stop bits on a serial device.
51 @param This Protocol instance pointer.
52 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
53 device's default interface speed.
54 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
55 serial interface. A ReceiveFifoDepth value of 0 will use
56 the device's default FIFO depth.
57 @param Timeout The requested time out for a single character in microseconds.
58 This timeout applies to both the transmit and receive side of the
59 interface. A Timeout value of 0 will use the device's default time
61 @param Parity The type of parity to use on this serial device. A Parity value of
62 DefaultParity will use the device's default parity value.
63 @param DataBits The number of data bits to use on the serial device. A DataBits
64 value of 0 will use the device's default data bit setting.
65 @param StopBits The number of stop bits to use on this serial device. A StopBits
66 value of DefaultStopBits will use the device's default number of
69 @retval EFI_SUCCESS The device was reset.
70 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.
71 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
77 IN EFI_SERIAL_IO_PROTOCOL
*This
,
79 IN UINT32 ReceiveFifoDepth
,
81 IN EFI_PARITY_TYPE Parity
,
83 IN EFI_STOP_BITS_TYPE StopBits
87 Set the control bits on a serial device
89 @param This Protocol instance pointer.
90 @param Control Set the bits of Control that are settable.
92 @retval EFI_SUCCESS The new control bits were set on the serial device.
93 @retval EFI_UNSUPPORTED The serial device does not support this operation.
94 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
100 IN EFI_SERIAL_IO_PROTOCOL
*This
,
105 Retrieves the status of the control bits on a serial device
107 @param This Protocol instance pointer.
108 @param Control A pointer to return the current Control signals from the serial device.
110 @retval EFI_SUCCESS The control bits were read from the serial device.
111 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
117 IN EFI_SERIAL_IO_PROTOCOL
*This
,
122 Writes data to a serial device.
124 @param This Protocol instance pointer.
125 @param BufferSize On input, the size of the Buffer. On output, the amount of
126 data actually written.
127 @param Buffer The buffer of data to write
129 @retval EFI_SUCCESS The data was written.
130 @retval EFI_DEVICE_ERROR The device reported an error.
131 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
137 IN EFI_SERIAL_IO_PROTOCOL
*This
,
138 IN OUT UINTN
*BufferSize
,
143 Reads data from a serial device.
145 @param This Protocol instance pointer.
146 @param BufferSize On input, the size of the Buffer. On output, the amount of
147 data returned in Buffer.
148 @param Buffer The buffer to return the data into.
150 @retval EFI_SUCCESS The data was read.
151 @retval EFI_DEVICE_ERROR The device reported an error.
152 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
158 IN EFI_SERIAL_IO_PROTOCOL
*This
,
159 IN OUT UINTN
*BufferSize
,
163 EFI_HANDLE mSerialHandle
= NULL
;
165 SERIAL_DEVICE_PATH mSerialDevicePath
= {
167 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
), 0} },
168 EFI_CALLER_ID_GUID
// Use the driver's GUID
171 { MESSAGING_DEVICE_PATH
, MSG_UART_DP
, { sizeof (UART_DEVICE_PATH
), 0} },
178 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 } }
182 // Template used to initialize the Serial IO protocols.
184 EFI_SERIAL_IO_MODE mSerialIoMode
= {
186 // value field set in SerialDxeInitialize()?
187 //--------- ------------------- -----------------------------
189 1000 * 1000, // Timeout
191 1, // ReceiveFifoDepth
197 EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate
= {
198 SERIAL_IO_INTERFACE_REVISION
,
209 Reset the serial device.
211 @param This Protocol instance pointer.
213 @retval EFI_SUCCESS The device was reset.
214 @retval EFI_DEVICE_ERROR The serial device could not be reset.
220 IN EFI_SERIAL_IO_PROTOCOL
*This
225 Status
= SerialPortInitialize ();
226 if (EFI_ERROR (Status
)) {
231 // Go set the current attributes
233 Status
= This
->SetAttributes (
235 This
->Mode
->BaudRate
,
236 This
->Mode
->ReceiveFifoDepth
,
238 (EFI_PARITY_TYPE
) This
->Mode
->Parity
,
239 (UINT8
) This
->Mode
->DataBits
,
240 (EFI_STOP_BITS_TYPE
) This
->Mode
->StopBits
247 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
248 data bits, and stop bits on a serial device.
250 @param This Protocol instance pointer.
251 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
252 device's default interface speed.
253 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
254 serial interface. A ReceiveFifoDepth value of 0 will use
255 the device's default FIFO depth.
256 @param Timeout The requested time out for a single character in microseconds.
257 This timeout applies to both the transmit and receive side of the
258 interface. A Timeout value of 0 will use the device's default time
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 @param DataBits The number of data bits to use on the serial device. A DataBits
263 value of 0 will use the device's default data bit setting.
264 @param StopBits The number of stop bits to use on this serial device. A StopBits
265 value of DefaultStopBits will use the device's default number of
268 @retval EFI_SUCCESS The device was reset.
269 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.
270 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
275 SerialSetAttributes (
276 IN EFI_SERIAL_IO_PROTOCOL
*This
,
278 IN UINT32 ReceiveFifoDepth
,
280 IN EFI_PARITY_TYPE Parity
,
282 IN EFI_STOP_BITS_TYPE StopBits
287 UINT64 OriginalBaudRate
;
288 UINT32 OriginalReceiveFifoDepth
;
289 UINT32 OriginalTimeout
;
290 EFI_PARITY_TYPE OriginalParity
;
291 UINT8 OriginalDataBits
;
292 EFI_STOP_BITS_TYPE OriginalStopBits
;
295 // Preserve the original input values in case
296 // SerialPortSetAttributes() updates the input/output
297 // parameters even on error.
299 OriginalBaudRate
= BaudRate
;
300 OriginalReceiveFifoDepth
= ReceiveFifoDepth
;
301 OriginalTimeout
= Timeout
;
302 OriginalParity
= Parity
;
303 OriginalDataBits
= DataBits
;
304 OriginalStopBits
= StopBits
;
305 Status
= SerialPortSetAttributes (&BaudRate
, &ReceiveFifoDepth
, &Timeout
, &Parity
, &DataBits
, &StopBits
);
306 if (EFI_ERROR (Status
)) {
308 // If it is just to set Timeout value and unsupported is returned,
309 // do not return error.
311 if ((Status
== EFI_UNSUPPORTED
) &&
312 (This
->Mode
->Timeout
!= OriginalTimeout
) &&
313 (This
->Mode
->ReceiveFifoDepth
== OriginalReceiveFifoDepth
) &&
314 (This
->Mode
->BaudRate
== OriginalBaudRate
) &&
315 (This
->Mode
->DataBits
== (UINT32
) OriginalDataBits
) &&
316 (This
->Mode
->Parity
== (UINT32
) OriginalParity
) &&
317 (This
->Mode
->StopBits
== (UINT32
) OriginalStopBits
)) {
319 // Restore to the original input values.
321 BaudRate
= OriginalBaudRate
;
322 ReceiveFifoDepth
= OriginalReceiveFifoDepth
;
323 Timeout
= OriginalTimeout
;
324 Parity
= OriginalParity
;
325 DataBits
= OriginalDataBits
;
326 StopBits
= OriginalStopBits
;
327 Status
= EFI_SUCCESS
;
328 } else if (Status
== EFI_INVALID_PARAMETER
|| Status
== EFI_UNSUPPORTED
) {
329 return EFI_INVALID_PARAMETER
;
331 return EFI_DEVICE_ERROR
;
336 // Set the Serial I/O mode and update the device path
339 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
342 // Set the Serial I/O mode
344 This
->Mode
->ReceiveFifoDepth
= ReceiveFifoDepth
;
345 This
->Mode
->Timeout
= Timeout
;
346 This
->Mode
->BaudRate
= BaudRate
;
347 This
->Mode
->DataBits
= (UINT32
) DataBits
;
348 This
->Mode
->Parity
= (UINT32
) Parity
;
349 This
->Mode
->StopBits
= (UINT32
) StopBits
;
352 // Check if the device path has actually changed
354 if (mSerialDevicePath
.Uart
.BaudRate
== BaudRate
&&
355 mSerialDevicePath
.Uart
.DataBits
== DataBits
&&
356 mSerialDevicePath
.Uart
.Parity
== (UINT8
) Parity
&&
357 mSerialDevicePath
.Uart
.StopBits
== (UINT8
) StopBits
359 gBS
->RestoreTPL (Tpl
);
364 // Update the device path
366 mSerialDevicePath
.Uart
.BaudRate
= BaudRate
;
367 mSerialDevicePath
.Uart
.DataBits
= DataBits
;
368 mSerialDevicePath
.Uart
.Parity
= (UINT8
) Parity
;
369 mSerialDevicePath
.Uart
.StopBits
= (UINT8
) StopBits
;
371 Status
= gBS
->ReinstallProtocolInterface (
373 &gEfiDevicePathProtocolGuid
,
378 gBS
->RestoreTPL (Tpl
);
384 Set the control bits on a serial device
386 @param This Protocol instance pointer.
387 @param Control Set the bits of Control that are settable.
389 @retval EFI_SUCCESS The new control bits were set on the serial device.
390 @retval EFI_UNSUPPORTED The serial device does not support this operation.
391 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
397 IN EFI_SERIAL_IO_PROTOCOL
*This
,
401 return SerialPortSetControl (Control
);
405 Retrieves the status of the control bits on a serial device
407 @param This Protocol instance pointer.
408 @param Control A pointer to return the current Control signals from the serial device.
410 @retval EFI_SUCCESS The control bits were read from the serial device.
411 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
417 IN EFI_SERIAL_IO_PROTOCOL
*This
,
421 return SerialPortGetControl (Control
);
425 Writes data to a serial device.
427 @param This Protocol instance pointer.
428 @param BufferSize On input, the size of the Buffer. On output, the amount of
429 data actually written.
430 @param Buffer The buffer of data to write
432 @retval EFI_SUCCESS The data was written.
433 @retval EFI_DEVICE_ERROR The device reported an error.
434 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
440 IN EFI_SERIAL_IO_PROTOCOL
*This
,
441 IN OUT UINTN
*BufferSize
,
447 Count
= SerialPortWrite (Buffer
, *BufferSize
);
449 if (Count
!= *BufferSize
) {
458 Reads data from a serial device.
460 @param This Protocol instance pointer.
461 @param BufferSize On input, the size of the Buffer. On output, the amount of
462 data returned in Buffer.
463 @param Buffer The buffer to return the data into.
465 @retval EFI_SUCCESS The data was read.
466 @retval EFI_DEVICE_ERROR The device reported an error.
467 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
473 IN EFI_SERIAL_IO_PROTOCOL
*This
,
474 IN OUT UINTN
*BufferSize
,
483 while (Count
< *BufferSize
) {
485 while (TimeOut
< mSerialIoMode
.Timeout
) {
486 if (SerialPortPoll ()) {
492 if (TimeOut
>= mSerialIoMode
.Timeout
) {
495 SerialPortRead (Buffer
, 1);
497 Buffer
= (VOID
*) ((UINT8
*) Buffer
+ 1);
500 if (Count
!= *BufferSize
) {
509 Initialization for the Serial Io Protocol.
511 @param[in] ImageHandle The firmware allocated handle for the EFI image.
512 @param[in] SystemTable A pointer to the EFI System Table.
514 @retval EFI_SUCCESS The entry point is executed successfully.
515 @retval other Some error occurs when executing this entry point.
520 SerialDxeInitialize (
521 IN EFI_HANDLE ImageHandle
,
522 IN EFI_SYSTEM_TABLE
*SystemTable
527 mSerialIoMode
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
528 mSerialIoMode
.DataBits
= (UINT32
) PcdGet8 (PcdUartDefaultDataBits
);
529 mSerialIoMode
.Parity
= (UINT32
) PcdGet8 (PcdUartDefaultParity
);
530 mSerialIoMode
.StopBits
= (UINT32
) PcdGet8 (PcdUartDefaultStopBits
);
531 mSerialIoMode
.ReceiveFifoDepth
= PcdGet16 (PcdUartDefaultReceiveFifoDepth
);
532 mSerialDevicePath
.Uart
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
533 mSerialDevicePath
.Uart
.DataBits
= PcdGet8 (PcdUartDefaultDataBits
);
534 mSerialDevicePath
.Uart
.Parity
= PcdGet8 (PcdUartDefaultParity
);
535 mSerialDevicePath
.Uart
.StopBits
= PcdGet8 (PcdUartDefaultStopBits
);
538 // Issue a reset to initialize the Serial Port
540 Status
= mSerialIoTemplate
.Reset (&mSerialIoTemplate
);
541 if (EFI_ERROR (Status
)) {
546 // Make a new handle with Serial IO protocol and its device path on it.
548 Status
= gBS
->InstallMultipleProtocolInterfaces (
550 &gEfiSerialIoProtocolGuid
, &mSerialIoTemplate
,
551 &gEfiDevicePathProtocolGuid
, &mSerialDevicePath
,
554 ASSERT_EFI_ERROR (Status
);