2 Serial I/O Port library functions with base address discovered from FDT
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
7 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/PcdLib.h>
22 #include <Library/SerialPortLib.h>
25 #include <Drivers/PL011Uart.h>
29 SerialPortInitialize (
34 // This SerialPortInitialize() function is completely empty, for a number of
36 // - if we are executing from flash, it is hard to keep state (i.e., store the
37 // discovered base address in a global), and the most robust way to deal
38 // with this is to discover the base address at every Write ();
39 // - calls to the Write() function in this module may be issued before this
40 // initialization function is called: this is not a problem when the base
41 // address of the UART is hardcoded, and only the baud rate may be wrong,
42 // but if we don't know the base address yet, we may be poking into memory
43 // that does not tolerate being poked into;
44 // - SEC and PEI phases produce debug output only, so with debug disabled, no
45 // initialization (or device tree parsing) is performed at all.
47 // Note that this means that on *every* Write () call, the device tree will be
48 // parsed and the UART re-initialized. However, this is a small price to pay
49 // for having serial debug output on a UART with no fixed base address.
51 return RETURN_SUCCESS
;
56 SerialPortGetBaseAddress (
61 UINT32 ReceiveFifoDepth
;
62 EFI_PARITY_TYPE Parity
;
64 EFI_STOP_BITS_TYPE StopBits
;
68 CONST CHAR8
*Compatible
;
69 CONST CHAR8
*CompatibleItem
;
70 CONST UINT64
*RegProperty
;
74 DeviceTreeBase
= (VOID
*)(UINTN
)FixedPcdGet64 (PcdDeviceTreeInitialBaseAddress
);
76 if ((DeviceTreeBase
== NULL
) || (fdt_check_header (DeviceTreeBase
) != 0)) {
81 // Enumerate all FDT nodes looking for a PL011 and capture its base address
83 for (Prev
= 0;; Prev
= Node
) {
84 Node
= fdt_next_node (DeviceTreeBase
, Prev
, NULL
);
89 Compatible
= fdt_getprop (DeviceTreeBase
, Node
, "compatible", &Len
);
90 if (Compatible
== NULL
) {
95 // Iterate over the NULL-separated items in the compatible string
97 for (CompatibleItem
= Compatible
; CompatibleItem
< Compatible
+ Len
;
98 CompatibleItem
+= 1 + AsciiStrLen (CompatibleItem
)) {
100 if (AsciiStrCmp (CompatibleItem
, "arm,pl011") == 0) {
101 RegProperty
= fdt_getprop (DeviceTreeBase
, Node
, "reg", &Len
);
105 UartBase
= (UINTN
)fdt64_to_cpu (ReadUnaligned64 (RegProperty
));
107 BaudRate
= (UINTN
)FixedPcdGet64 (PcdUartDefaultBaudRate
);
108 ReceiveFifoDepth
= 0; // Use the default value for Fifo depth
109 Parity
= (EFI_PARITY_TYPE
)FixedPcdGet8 (PcdUartDefaultParity
);
110 DataBits
= FixedPcdGet8 (PcdUartDefaultDataBits
);
111 StopBits
= (EFI_STOP_BITS_TYPE
) FixedPcdGet8 (PcdUartDefaultStopBits
);
113 Status
= PL011UartInitializePort (
115 &BaudRate
, &ReceiveFifoDepth
, &Parity
, &DataBits
, &StopBits
);
116 if (!EFI_ERROR (Status
)) {
126 Write data to serial device.
128 @param Buffer Point of data buffer which need to be written.
129 @param NumberOfBytes Number of output bytes which are cached in Buffer.
131 @retval 0 Write data failed.
132 @retval !0 Actual number of bytes written to serial device.
139 IN UINTN NumberOfBytes
142 UINT64 SerialRegisterBase
;
144 SerialRegisterBase
= SerialPortGetBaseAddress ();
145 if (SerialRegisterBase
!= 0) {
146 return PL011UartWrite ((UINTN
)SerialRegisterBase
, Buffer
, NumberOfBytes
);
152 Read data from serial device and save the data in buffer.
154 @param Buffer Point of data buffer which need to be written.
155 @param NumberOfBytes Size of Buffer[].
157 @retval 0 Read data failed.
158 @retval !0 Actual number of bytes read from serial device.
165 IN UINTN NumberOfBytes
172 Check to see if any data is available to be read from the debug device.
174 @retval TRUE At least one byte of data is available to be read
175 @retval FALSE No data is available to be read
188 Sets the control bits on a serial device.
190 @param[in] Control Sets the bits of Control that are settable.
192 @retval RETURN_SUCCESS The new control bits were set on the serial device.
193 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
194 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
199 SerialPortSetControl (
203 return RETURN_UNSUPPORTED
;
207 Retrieve the status of the control bits on a serial device.
209 @param[out] Control A pointer to return the current control signals from the serial device.
211 @retval RETURN_SUCCESS The control bits were read from the serial device.
212 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
213 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
218 SerialPortGetControl (
222 return RETURN_UNSUPPORTED
;
226 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
227 data bits, and stop bits on a serial device.
229 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
230 device's default interface speed.
231 On output, the value actually set.
232 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
233 serial interface. A ReceiveFifoDepth value of 0 will use
234 the device's default FIFO depth.
235 On output, the value actually set.
236 @param Timeout The requested time out for a single character in microseconds.
237 This timeout applies to both the transmit and receive side of the
238 interface. A Timeout value of 0 will use the device's default time
240 On output, the value actually set.
241 @param Parity The type of parity to use on this serial device. A Parity value of
242 DefaultParity will use the device's default parity value.
243 On output, the value actually set.
244 @param DataBits The number of data bits to use on the serial device. A DataBits
245 vaule of 0 will use the device's default data bit setting.
246 On output, the value actually set.
247 @param StopBits The number of stop bits to use on this serial device. A StopBits
248 value of DefaultStopBits will use the device's default number of
250 On output, the value actually set.
252 @retval RETURN_SUCCESS The new attributes were set on the serial device.
253 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
254 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
255 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
260 SerialPortSetAttributes (
261 IN OUT UINT64
*BaudRate
,
262 IN OUT UINT32
*ReceiveFifoDepth
,
263 IN OUT UINT32
*Timeout
,
264 IN OUT EFI_PARITY_TYPE
*Parity
,
265 IN OUT UINT8
*DataBits
,
266 IN OUT EFI_STOP_BITS_TYPE
*StopBits
269 return RETURN_UNSUPPORTED
;