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
244 // The serial device may not support some of the attributes. To prevent
245 // later failure, always return EFI_SUCCESS when SetAttributes is returning
246 // EFI_INVALID_PARAMETER.
248 if (Status
== EFI_INVALID_PARAMETER
) {
256 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
257 data bits, and stop bits on a serial device.
259 @param This Protocol instance pointer.
260 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the the
261 device's default interface speed.
262 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
263 serial interface. A ReceiveFifoDepth value of 0 will use
264 the device's default FIFO depth.
265 @param Timeout The requested time out for a single character in microseconds.
266 This timeout applies to both the transmit and receive side of the
267 interface. A Timeout value of 0 will use the device's default time
269 @param Parity The type of parity to use on this serial device. A Parity value of
270 DefaultParity will use the device's default parity value.
271 @param DataBits The number of data bits to use on the serial device. A DataBits
272 value of 0 will use the device's default data bit setting.
273 @param StopBits The number of stop bits to use on this serial device. A StopBits
274 value of DefaultStopBits will use the device's default number of
277 @retval EFI_SUCCESS The device was reset.
278 @retval EFI_INVALID_PARAMETER One or more attributes has an unsupported value.
279 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
284 SerialSetAttributes (
285 IN EFI_SERIAL_IO_PROTOCOL
*This
,
287 IN UINT32 ReceiveFifoDepth
,
289 IN EFI_PARITY_TYPE Parity
,
291 IN EFI_STOP_BITS_TYPE StopBits
296 UINT64 OriginalBaudRate
;
297 UINT32 OriginalReceiveFifoDepth
;
298 UINT32 OriginalTimeout
;
299 EFI_PARITY_TYPE OriginalParity
;
300 UINT8 OriginalDataBits
;
301 EFI_STOP_BITS_TYPE OriginalStopBits
;
304 // Preserve the original input values in case
305 // SerialPortSetAttributes() updates the input/output
306 // parameters even on error.
308 OriginalBaudRate
= BaudRate
;
309 OriginalReceiveFifoDepth
= ReceiveFifoDepth
;
310 OriginalTimeout
= Timeout
;
311 OriginalParity
= Parity
;
312 OriginalDataBits
= DataBits
;
313 OriginalStopBits
= StopBits
;
314 Status
= SerialPortSetAttributes (&BaudRate
, &ReceiveFifoDepth
, &Timeout
, &Parity
, &DataBits
, &StopBits
);
315 if (EFI_ERROR (Status
)) {
317 // If it is just to set Timeout value and unsupported is returned,
318 // do not return error.
320 if ((Status
== EFI_UNSUPPORTED
) &&
321 (This
->Mode
->Timeout
!= OriginalTimeout
) &&
322 (This
->Mode
->ReceiveFifoDepth
== OriginalReceiveFifoDepth
) &&
323 (This
->Mode
->BaudRate
== OriginalBaudRate
) &&
324 (This
->Mode
->DataBits
== (UINT32
) OriginalDataBits
) &&
325 (This
->Mode
->Parity
== (UINT32
) OriginalParity
) &&
326 (This
->Mode
->StopBits
== (UINT32
) OriginalStopBits
)) {
328 // Restore to the original input values.
330 BaudRate
= OriginalBaudRate
;
331 ReceiveFifoDepth
= OriginalReceiveFifoDepth
;
332 Timeout
= OriginalTimeout
;
333 Parity
= OriginalParity
;
334 DataBits
= OriginalDataBits
;
335 StopBits
= OriginalStopBits
;
336 Status
= EFI_SUCCESS
;
337 } else if (Status
== EFI_INVALID_PARAMETER
|| Status
== EFI_UNSUPPORTED
) {
338 return EFI_INVALID_PARAMETER
;
340 return EFI_DEVICE_ERROR
;
345 // Set the Serial I/O mode and update the device path
348 Tpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
351 // Set the Serial I/O mode
353 This
->Mode
->ReceiveFifoDepth
= ReceiveFifoDepth
;
354 This
->Mode
->Timeout
= Timeout
;
355 This
->Mode
->BaudRate
= BaudRate
;
356 This
->Mode
->DataBits
= (UINT32
) DataBits
;
357 This
->Mode
->Parity
= (UINT32
) Parity
;
358 This
->Mode
->StopBits
= (UINT32
) StopBits
;
361 // Check if the device path has actually changed
363 if (mSerialDevicePath
.Uart
.BaudRate
== BaudRate
&&
364 mSerialDevicePath
.Uart
.DataBits
== DataBits
&&
365 mSerialDevicePath
.Uart
.Parity
== (UINT8
) Parity
&&
366 mSerialDevicePath
.Uart
.StopBits
== (UINT8
) StopBits
368 gBS
->RestoreTPL (Tpl
);
373 // Update the device path
375 mSerialDevicePath
.Uart
.BaudRate
= BaudRate
;
376 mSerialDevicePath
.Uart
.DataBits
= DataBits
;
377 mSerialDevicePath
.Uart
.Parity
= (UINT8
) Parity
;
378 mSerialDevicePath
.Uart
.StopBits
= (UINT8
) StopBits
;
380 Status
= gBS
->ReinstallProtocolInterface (
382 &gEfiDevicePathProtocolGuid
,
387 gBS
->RestoreTPL (Tpl
);
393 Set the control bits on a serial device
395 @param This Protocol instance pointer.
396 @param Control Set the bits of Control that are settable.
398 @retval EFI_SUCCESS The new control bits were set on the serial device.
399 @retval EFI_UNSUPPORTED The serial device does not support this operation.
400 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
406 IN EFI_SERIAL_IO_PROTOCOL
*This
,
410 return SerialPortSetControl (Control
);
414 Retrieves the status of the control bits on a serial device
416 @param This Protocol instance pointer.
417 @param Control A pointer to return the current Control signals from the serial device.
419 @retval EFI_SUCCESS The control bits were read from the serial device.
420 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
426 IN EFI_SERIAL_IO_PROTOCOL
*This
,
430 return SerialPortGetControl (Control
);
434 Writes data to a serial device.
436 @param This Protocol instance pointer.
437 @param BufferSize On input, the size of the Buffer. On output, the amount of
438 data actually written.
439 @param Buffer The buffer of data to write
441 @retval EFI_SUCCESS The data was written.
442 @retval EFI_DEVICE_ERROR The device reported an error.
443 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
449 IN EFI_SERIAL_IO_PROTOCOL
*This
,
450 IN OUT UINTN
*BufferSize
,
456 Count
= SerialPortWrite (Buffer
, *BufferSize
);
458 if (Count
!= *BufferSize
) {
467 Reads data from a serial device.
469 @param This Protocol instance pointer.
470 @param BufferSize On input, the size of the Buffer. On output, the amount of
471 data returned in Buffer.
472 @param Buffer The buffer to return the data into.
474 @retval EFI_SUCCESS The data was read.
475 @retval EFI_DEVICE_ERROR The device reported an error.
476 @retval EFI_TIMEOUT The data write was stopped due to a timeout.
482 IN EFI_SERIAL_IO_PROTOCOL
*This
,
483 IN OUT UINTN
*BufferSize
,
492 while (Count
< *BufferSize
) {
494 while (TimeOut
< mSerialIoMode
.Timeout
) {
495 if (SerialPortPoll ()) {
501 if (TimeOut
>= mSerialIoMode
.Timeout
) {
504 SerialPortRead (Buffer
, 1);
506 Buffer
= (VOID
*) ((UINT8
*) Buffer
+ 1);
509 if (Count
!= *BufferSize
) {
518 Initialization for the Serial Io Protocol.
520 @param[in] ImageHandle The firmware allocated handle for the EFI image.
521 @param[in] SystemTable A pointer to the EFI System Table.
523 @retval EFI_SUCCESS The entry point is executed successfully.
524 @retval other Some error occurs when executing this entry point.
529 SerialDxeInitialize (
530 IN EFI_HANDLE ImageHandle
,
531 IN EFI_SYSTEM_TABLE
*SystemTable
536 mSerialIoMode
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
537 mSerialIoMode
.DataBits
= (UINT32
) PcdGet8 (PcdUartDefaultDataBits
);
538 mSerialIoMode
.Parity
= (UINT32
) PcdGet8 (PcdUartDefaultParity
);
539 mSerialIoMode
.StopBits
= (UINT32
) PcdGet8 (PcdUartDefaultStopBits
);
540 mSerialIoMode
.ReceiveFifoDepth
= PcdGet16 (PcdUartDefaultReceiveFifoDepth
);
541 mSerialDevicePath
.Uart
.BaudRate
= PcdGet64 (PcdUartDefaultBaudRate
);
542 mSerialDevicePath
.Uart
.DataBits
= PcdGet8 (PcdUartDefaultDataBits
);
543 mSerialDevicePath
.Uart
.Parity
= PcdGet8 (PcdUartDefaultParity
);
544 mSerialDevicePath
.Uart
.StopBits
= PcdGet8 (PcdUartDefaultStopBits
);
547 // Issue a reset to initialize the Serial Port
549 Status
= mSerialIoTemplate
.Reset (&mSerialIoTemplate
);
550 if (EFI_ERROR (Status
)) {
555 // Make a new handle with Serial IO protocol and its device path on it.
557 Status
= gBS
->InstallMultipleProtocolInterfaces (
559 &gEfiSerialIoProtocolGuid
, &mSerialIoTemplate
,
560 &gEfiDevicePathProtocolGuid
, &mSerialDevicePath
,
563 ASSERT_EFI_ERROR (Status
);