]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/Library/FdtPL011SerialPortLib/EarlyFdtPL011SerialPortLib.c
ArmVirtPkg/FdtPL011SerialPortLib: honor DT node 'status' property
[mirror_edk2.git] / ArmVirtPkg / Library / FdtPL011SerialPortLib / EarlyFdtPL011SerialPortLib.c
1 /** @file
2 Serial I/O Port library functions with base address discovered from FDT
3
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>
8
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
13
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.
16
17 **/
18
19 #include <Base.h>
20
21 #include <Library/PcdLib.h>
22 #include <Library/SerialPortLib.h>
23 #include <libfdt.h>
24
25 #include <Drivers/PL011Uart.h>
26
27 RETURN_STATUS
28 EFIAPI
29 SerialPortInitialize (
30 VOID
31 )
32 {
33 //
34 // This SerialPortInitialize() function is completely empty, for a number of
35 // reasons:
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.
46 //
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.
50 //
51 return RETURN_SUCCESS;
52 }
53
54 STATIC
55 UINT64
56 SerialPortGetBaseAddress (
57 VOID
58 )
59 {
60 UINT64 BaudRate;
61 UINT32 ReceiveFifoDepth;
62 EFI_PARITY_TYPE Parity;
63 UINT8 DataBits;
64 EFI_STOP_BITS_TYPE StopBits;
65 VOID *DeviceTreeBase;
66 INT32 Node, Prev;
67 INT32 Len;
68 CONST CHAR8 *Compatible;
69 CONST CHAR8 *NodeStatus;
70 CONST CHAR8 *CompatibleItem;
71 CONST UINT64 *RegProperty;
72 UINTN UartBase;
73 RETURN_STATUS Status;
74
75 DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
76
77 if ((DeviceTreeBase == NULL) || (fdt_check_header (DeviceTreeBase) != 0)) {
78 return 0;
79 }
80
81 //
82 // Enumerate all FDT nodes looking for a PL011 and capture its base address
83 //
84 for (Prev = 0;; Prev = Node) {
85 Node = fdt_next_node (DeviceTreeBase, Prev, NULL);
86 if (Node < 0) {
87 break;
88 }
89
90 Compatible = fdt_getprop (DeviceTreeBase, Node, "compatible", &Len);
91 if (Compatible == NULL) {
92 continue;
93 }
94
95 //
96 // Iterate over the NULL-separated items in the compatible string
97 //
98 for (CompatibleItem = Compatible; CompatibleItem < Compatible + Len;
99 CompatibleItem += 1 + AsciiStrLen (CompatibleItem)) {
100
101 if (AsciiStrCmp (CompatibleItem, "arm,pl011") == 0) {
102 NodeStatus = fdt_getprop (DeviceTreeBase, Node, "status", &Len);
103 if (NodeStatus != NULL && AsciiStrCmp (NodeStatus, "okay") != 0) {
104 continue;
105 }
106
107 RegProperty = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);
108 if (Len != 16) {
109 return 0;
110 }
111 UartBase = (UINTN)fdt64_to_cpu (ReadUnaligned64 (RegProperty));
112
113 BaudRate = (UINTN)FixedPcdGet64 (PcdUartDefaultBaudRate);
114 ReceiveFifoDepth = 0; // Use the default value for Fifo depth
115 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
116 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);
117 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);
118
119 Status = PL011UartInitializePort (
120 UartBase,
121 FixedPcdGet32 (PL011UartClkInHz),
122 &BaudRate,
123 &ReceiveFifoDepth,
124 &Parity,
125 &DataBits,
126 &StopBits
127 );
128 if (!EFI_ERROR (Status)) {
129 return UartBase;
130 }
131 }
132 }
133 }
134 return 0;
135 }
136
137 /**
138 Write data to serial device.
139
140 @param Buffer Point of data buffer which need to be written.
141 @param NumberOfBytes Number of output bytes which are cached in Buffer.
142
143 @retval 0 Write data failed.
144 @retval !0 Actual number of bytes written to serial device.
145
146 **/
147 UINTN
148 EFIAPI
149 SerialPortWrite (
150 IN UINT8 *Buffer,
151 IN UINTN NumberOfBytes
152 )
153 {
154 UINT64 SerialRegisterBase;
155
156 SerialRegisterBase = SerialPortGetBaseAddress ();
157 if (SerialRegisterBase != 0) {
158 return PL011UartWrite ((UINTN)SerialRegisterBase, Buffer, NumberOfBytes);
159 }
160 return 0;
161 }
162
163 /**
164 Read data from serial device and save the data in buffer.
165
166 @param Buffer Point of data buffer which need to be written.
167 @param NumberOfBytes Size of Buffer[].
168
169 @retval 0 Read data failed.
170 @retval !0 Actual number of bytes read from serial device.
171
172 **/
173 UINTN
174 EFIAPI
175 SerialPortRead (
176 OUT UINT8 *Buffer,
177 IN UINTN NumberOfBytes
178 )
179 {
180 return 0;
181 }
182
183 /**
184 Check to see if any data is available to be read from the debug device.
185
186 @retval TRUE At least one byte of data is available to be read
187 @retval FALSE No data is available to be read
188
189 **/
190 BOOLEAN
191 EFIAPI
192 SerialPortPoll (
193 VOID
194 )
195 {
196 return FALSE;
197 }
198
199 /**
200 Sets the control bits on a serial device.
201
202 @param[in] Control Sets the bits of Control that are settable.
203
204 @retval RETURN_SUCCESS The new control bits were set on the serial device.
205 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
206 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
207
208 **/
209 RETURN_STATUS
210 EFIAPI
211 SerialPortSetControl (
212 IN UINT32 Control
213 )
214 {
215 return RETURN_UNSUPPORTED;
216 }
217
218 /**
219 Retrieve the status of the control bits on a serial device.
220
221 @param[out] Control A pointer to return the current control signals from the serial device.
222
223 @retval RETURN_SUCCESS The control bits were read from the serial device.
224 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
225 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
226
227 **/
228 RETURN_STATUS
229 EFIAPI
230 SerialPortGetControl (
231 OUT UINT32 *Control
232 )
233 {
234 return RETURN_UNSUPPORTED;
235 }
236
237 /**
238 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
239 data bits, and stop bits on a serial device.
240
241 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
242 device's default interface speed.
243 On output, the value actually set.
244 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
245 serial interface. A ReceiveFifoDepth value of 0 will use
246 the device's default FIFO depth.
247 On output, the value actually set.
248 @param Timeout The requested time out for a single character in microseconds.
249 This timeout applies to both the transmit and receive side of the
250 interface. A Timeout value of 0 will use the device's default time
251 out value.
252 On output, the value actually set.
253 @param Parity The type of parity to use on this serial device. A Parity value of
254 DefaultParity will use the device's default parity value.
255 On output, the value actually set.
256 @param DataBits The number of data bits to use on the serial device. A DataBits
257 vaule of 0 will use the device's default data bit setting.
258 On output, the value actually set.
259 @param StopBits The number of stop bits to use on this serial device. A StopBits
260 value of DefaultStopBits will use the device's default number of
261 stop bits.
262 On output, the value actually set.
263
264 @retval RETURN_SUCCESS The new attributes were set on the serial device.
265 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
266 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
267 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
268
269 **/
270 RETURN_STATUS
271 EFIAPI
272 SerialPortSetAttributes (
273 IN OUT UINT64 *BaudRate,
274 IN OUT UINT32 *ReceiveFifoDepth,
275 IN OUT UINT32 *Timeout,
276 IN OUT EFI_PARITY_TYPE *Parity,
277 IN OUT UINT8 *DataBits,
278 IN OUT EFI_STOP_BITS_TYPE *StopBits
279 )
280 {
281 return RETURN_UNSUPPORTED;
282 }
283