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_DEVICE_ERROR The serial device could not be reset.
76 IN EFI_SERIAL_IO_PROTOCOL
*This
,
78 IN UINT32 ReceiveFifoDepth
,
80 IN EFI_PARITY_TYPE Parity
,
82 IN EFI_STOP_BITS_TYPE StopBits
86 Set the control bits on a serial device
88 @param This Protocol instance pointer.
89 @param Control Set the bits of Control that are settable.
91 @retval EFI_SUCCESS The new control bits were set on the serial device.
92 @retval EFI_UNSUPPORTED The serial device does not support this operation.
93 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
99 IN EFI_SERIAL_IO_PROTOCOL
*This
,
104 Retrieves the status of the control bits on a serial device
106 @param This Protocol instance pointer.
107 @param Control A pointer to return the current Control signals from the serial device.
109 @retval EFI_SUCCESS The control bits were read from the serial device.
110 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
116 IN EFI_SERIAL_IO_PROTOCOL
*This
,
121 Writes data to a serial device.
123 @param This Protocol instance pointer.
124 @param BufferSize On input, the size of the Buffer. On output, the amount of
125 data actually written.
126 @param Buffer The buffer of data to write
128 @retval EFI_SUCCESS The data was written.
129 @retval EFI_DEVICE_ERROR The device reported an error.
130 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
136 IN EFI_SERIAL_IO_PROTOCOL
*This
,
137 IN OUT UINTN
*BufferSize
,
142 Reads data from a serial device.
144 @param This Protocol instance pointer.
145 @param BufferSize On input, the size of the Buffer. On output, the amount of
146 data returned in Buffer.
147 @param Buffer The buffer to return the data into.
149 @retval EFI_SUCCESS The data was read.
150 @retval EFI_DEVICE_ERROR The device reported an error.
151 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
157 IN EFI_SERIAL_IO_PROTOCOL
*This
,
158 IN OUT UINTN
*BufferSize
,
162 EFI_HANDLE mSerialHandle
= NULL
;
164 SERIAL_DEVICE_PATH mSerialDevicePath
= {
166 { HARDWARE_DEVICE_PATH
, HW_VENDOR_DP
, { sizeof (VENDOR_DEVICE_PATH
), 0} },
167 EFI_CALLER_ID_GUID
// Use the driver's GUID
170 { MESSAGING_DEVICE_PATH
, MSG_UART_DP
, { sizeof (UART_DEVICE_PATH
), 0} },
177 { END_DEVICE_PATH_TYPE
, END_ENTIRE_DEVICE_PATH_SUBTYPE
, { sizeof (EFI_DEVICE_PATH_PROTOCOL
), 0 } }
181 // Template used to initialize the Serial IO protocols.
183 EFI_SERIAL_IO_MODE mSerialIoMode
= {
185 // value field set in SerialDxeInitialize()?
186 //--------- ------------------- -----------------------------
188 1000 * 1000, // Timeout
190 1, // ReceiveFifoDepth
196 EFI_SERIAL_IO_PROTOCOL mSerialIoTemplate
= {
197 SERIAL_IO_INTERFACE_REVISION
,
208 Reset the serial device.
210 @param This Protocol instance pointer.
212 @retval EFI_SUCCESS The device was reset.
213 @retval EFI_DEVICE_ERROR The serial device could not be reset.
219 IN EFI_SERIAL_IO_PROTOCOL
*This
225 Status
= SerialPortInitialize ();
226 if (EFI_ERROR (Status
)) {
231 // Set the Serial I/O mode and update the device path
234 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
237 // Set the Serial I/O mode
239 This
->Mode
->ReceiveFifoDepth
= PcdGet16 (PcdUartDefaultReceiveFifoDepth
);
240 This
->Mode
->Timeout
= 1000 * 1000;
241 This
->Mode
->BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
242 This
->Mode
->DataBits
= (UINT32
) PcdGet8 (PcdUartDefaultDataBits
);
243 This
->Mode
->Parity
= (UINT32
) PcdGet8 (PcdUartDefaultParity
);
244 This
->Mode
->StopBits
= (UINT32
) PcdGet8 (PcdUartDefaultStopBits
);
247 // Check if the device path has actually changed
249 if (mSerialDevicePath
.Uart
.BaudRate
== This
->Mode
->BaudRate
&&
250 mSerialDevicePath
.Uart
.DataBits
== (UINT8
) This
->Mode
->DataBits
&&
251 mSerialDevicePath
.Uart
.Parity
== (UINT8
) This
->Mode
->Parity
&&
252 mSerialDevicePath
.Uart
.StopBits
== (UINT8
) This
->Mode
->StopBits
254 gBS
->RestoreTPL (Tpl
);
259 // Update the device path
261 mSerialDevicePath
.Uart
.BaudRate
= This
->Mode
->BaudRate
;
262 mSerialDevicePath
.Uart
.DataBits
= (UINT8
) This
->Mode
->DataBits
;
263 mSerialDevicePath
.Uart
.Parity
= (UINT8
) This
->Mode
->Parity
;
264 mSerialDevicePath
.Uart
.StopBits
= (UINT8
) This
->Mode
->StopBits
;
266 Status
= gBS
->ReinstallProtocolInterface (
268 &gEfiDevicePathProtocolGuid
,
273 gBS
->RestoreTPL (Tpl
);
279 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
280 data bits, and stop bits on a serial device.
282 @param This Protocol instance pointer.
283 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
284 device's default interface speed.
285 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
286 serial interface. A ReceiveFifoDepth value of 0 will use
287 the device's default FIFO depth.
288 @param Timeout The requested time out for a single character in microseconds.
289 This timeout applies to both the transmit and receive side of the
290 interface. A Timeout value of 0 will use the device's default time
292 @param Parity The type of parity to use on this serial device. A Parity value of
293 DefaultParity will use the device's default parity value.
294 @param DataBits The number of data bits to use on the serial device. A DataBits
295 value of 0 will use the device's default data bit setting.
296 @param StopBits The number of stop bits to use on this serial device. A StopBits
297 value of DefaultStopBits will use the device's default number of
300 @retval EFI_SUCCESS The device was reset.
301 @retval EFI_DEVICE_ERROR The serial device could not be reset.
306 SerialSetAttributes (
307 IN EFI_SERIAL_IO_PROTOCOL
*This
,
309 IN UINT32 ReceiveFifoDepth
,
311 IN EFI_PARITY_TYPE Parity
,
313 IN EFI_STOP_BITS_TYPE StopBits
319 Status
= SerialPortSetAttributes (&BaudRate
, &ReceiveFifoDepth
, &Timeout
, &Parity
, &DataBits
, &StopBits
);
320 if (EFI_ERROR (Status
)) {
325 // Set the Serial I/O mode and update the device path
328 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
331 // Set the Serial I/O mode
333 This
->Mode
->ReceiveFifoDepth
= ReceiveFifoDepth
;
334 This
->Mode
->Timeout
= Timeout
;
335 This
->Mode
->BaudRate
= BaudRate
;
336 This
->Mode
->DataBits
= (UINT32
) DataBits
;
337 This
->Mode
->Parity
= (UINT32
) Parity
;
338 This
->Mode
->StopBits
= (UINT32
) StopBits
;
341 // Check if the device path has actually changed
343 if (mSerialDevicePath
.Uart
.BaudRate
== BaudRate
&&
344 mSerialDevicePath
.Uart
.DataBits
== DataBits
&&
345 mSerialDevicePath
.Uart
.Parity
== (UINT8
) Parity
&&
346 mSerialDevicePath
.Uart
.StopBits
== (UINT8
) StopBits
348 gBS
->RestoreTPL (Tpl
);
353 // Update the device path
355 mSerialDevicePath
.Uart
.BaudRate
= BaudRate
;
356 mSerialDevicePath
.Uart
.DataBits
= DataBits
;
357 mSerialDevicePath
.Uart
.Parity
= (UINT8
) Parity
;
358 mSerialDevicePath
.Uart
.StopBits
= (UINT8
) StopBits
;
360 Status
= gBS
->ReinstallProtocolInterface (
362 &gEfiDevicePathProtocolGuid
,
367 gBS
->RestoreTPL (Tpl
);
373 Set the control bits on a serial device
375 @param This Protocol instance pointer.
376 @param Control Set the bits of Control that are settable.
378 @retval EFI_SUCCESS The new control bits were set on the serial device.
379 @retval EFI_UNSUPPORTED The serial device does not support this operation.
380 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
386 IN EFI_SERIAL_IO_PROTOCOL
*This
,
390 return SerialPortSetControl (Control
);
394 Retrieves the status of the control bits on a serial device
396 @param This Protocol instance pointer.
397 @param Control A pointer to return the current Control signals from the serial device.
399 @retval EFI_SUCCESS The control bits were read from the serial device.
400 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
406 IN EFI_SERIAL_IO_PROTOCOL
*This
,
410 return SerialPortGetControl (Control
);
414 Writes data to a serial device.
416 @param This Protocol instance pointer.
417 @param BufferSize On input, the size of the Buffer. On output, the amount of
418 data actually written.
419 @param Buffer The buffer of data to write
421 @retval EFI_SUCCESS The data was written.
422 @retval EFI_DEVICE_ERROR The device reported an error.
423 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
429 IN EFI_SERIAL_IO_PROTOCOL
*This
,
430 IN OUT UINTN
*BufferSize
,
436 Count
= SerialPortWrite (Buffer
, *BufferSize
);
438 if (Count
!= *BufferSize
) {
447 Reads data from a serial device.
449 @param This Protocol instance pointer.
450 @param BufferSize On input, the size of the Buffer. On output, the amount of
451 data returned in Buffer.
452 @param Buffer The buffer to return the data into.
454 @retval EFI_SUCCESS The data was read.
455 @retval EFI_DEVICE_ERROR The device reported an error.
456 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
462 IN EFI_SERIAL_IO_PROTOCOL
*This
,
463 IN OUT UINTN
*BufferSize
,
471 if (SerialPortPoll ()) {
472 Count
= SerialPortRead (Buffer
, *BufferSize
);
475 if (Count
!= *BufferSize
) {
484 Initialization for the Serial Io Protocol.
486 @param[in] ImageHandle The firmware allocated handle for the EFI image.
487 @param[in] SystemTable A pointer to the EFI System Table.
489 @retval EFI_SUCCESS The entry point is executed successfully.
490 @retval other Some error occurs when executing this entry point.
495 SerialDxeInitialize (
496 IN EFI_HANDLE ImageHandle
,
497 IN EFI_SYSTEM_TABLE
*SystemTable
502 Status
= SerialPortInitialize ();
503 if (EFI_ERROR (Status
)) {
507 mSerialIoMode
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
508 mSerialIoMode
.DataBits
= (UINT32
) PcdGet8 (PcdUartDefaultDataBits
);
509 mSerialIoMode
.Parity
= (UINT32
) PcdGet8 (PcdUartDefaultParity
);
510 mSerialIoMode
.StopBits
= (UINT32
) PcdGet8 (PcdUartDefaultStopBits
);
511 mSerialIoMode
.ReceiveFifoDepth
= PcdGet16 (PcdUartDefaultReceiveFifoDepth
);
512 mSerialDevicePath
.Uart
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
513 mSerialDevicePath
.Uart
.DataBits
= PcdGet8 (PcdUartDefaultDataBits
);
514 mSerialDevicePath
.Uart
.Parity
= PcdGet8 (PcdUartDefaultParity
);
515 mSerialDevicePath
.Uart
.StopBits
= PcdGet8 (PcdUartDefaultStopBits
);
518 // Make a new handle with Serial IO protocol and its device path on it.
520 Status
= gBS
->InstallMultipleProtocolInterfaces (
522 &gEfiSerialIoProtocolGuid
, &mSerialIoTemplate
,
523 &gEfiDevicePathProtocolGuid
, &mSerialDevicePath
,
526 ASSERT_EFI_ERROR (Status
);