]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/FdtPL011SerialPortLib/EarlyFdtPL011SerialPortLib.c
ArmVirtPkg/FdtPL011SerialPortLib: honor DT node 'status' property
[mirror_edk2.git] / ArmVirtPkg / Library / FdtPL011SerialPortLib / EarlyFdtPL011SerialPortLib.c
CommitLineData
f1f0ba19
LE
1/** @file\r
2 Serial I/O Port library functions with base address discovered from FDT\r
3\r
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>\r
ad7f6bc2 7 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
f1f0ba19
LE
8\r
9 This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include <Base.h>\r
20\r
21#include <Library/PcdLib.h>\r
22#include <Library/SerialPortLib.h>\r
f1f0ba19
LE
23#include <libfdt.h>\r
24\r
25#include <Drivers/PL011Uart.h>\r
26\r
27RETURN_STATUS\r
28EFIAPI\r
29SerialPortInitialize (\r
30 VOID\r
31 )\r
32{\r
33 //\r
34 // This SerialPortInitialize() function is completely empty, for a number of\r
35 // reasons:\r
36 // - if we are executing from flash, it is hard to keep state (i.e., store the\r
37 // discovered base address in a global), and the most robust way to deal\r
38 // with this is to discover the base address at every Write ();\r
39 // - calls to the Write() function in this module may be issued before this\r
40 // initialization function is called: this is not a problem when the base\r
41 // address of the UART is hardcoded, and only the baud rate may be wrong,\r
42 // but if we don't know the base address yet, we may be poking into memory\r
43 // that does not tolerate being poked into;\r
44 // - SEC and PEI phases produce debug output only, so with debug disabled, no\r
45 // initialization (or device tree parsing) is performed at all.\r
46 //\r
47 // Note that this means that on *every* Write () call, the device tree will be\r
48 // parsed and the UART re-initialized. However, this is a small price to pay\r
49 // for having serial debug output on a UART with no fixed base address.\r
50 //\r
51 return RETURN_SUCCESS;\r
52}\r
53\r
54STATIC\r
55UINT64\r
56SerialPortGetBaseAddress (\r
57 VOID\r
58 )\r
59{\r
60 UINT64 BaudRate;\r
61 UINT32 ReceiveFifoDepth;\r
62 EFI_PARITY_TYPE Parity;\r
63 UINT8 DataBits;\r
64 EFI_STOP_BITS_TYPE StopBits;\r
65 VOID *DeviceTreeBase;\r
66 INT32 Node, Prev;\r
67 INT32 Len;\r
68 CONST CHAR8 *Compatible;\r
b1f3e48e 69 CONST CHAR8 *NodeStatus;\r
f1f0ba19
LE
70 CONST CHAR8 *CompatibleItem;\r
71 CONST UINT64 *RegProperty;\r
72 UINTN UartBase;\r
73 RETURN_STATUS Status;\r
74\r
ff05707e 75 DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);\r
f1f0ba19
LE
76\r
77 if ((DeviceTreeBase == NULL) || (fdt_check_header (DeviceTreeBase) != 0)) {\r
78 return 0;\r
79 }\r
80\r
81 //\r
82 // Enumerate all FDT nodes looking for a PL011 and capture its base address\r
83 //\r
84 for (Prev = 0;; Prev = Node) {\r
85 Node = fdt_next_node (DeviceTreeBase, Prev, NULL);\r
86 if (Node < 0) {\r
87 break;\r
88 }\r
89\r
90 Compatible = fdt_getprop (DeviceTreeBase, Node, "compatible", &Len);\r
91 if (Compatible == NULL) {\r
92 continue;\r
93 }\r
94\r
95 //\r
96 // Iterate over the NULL-separated items in the compatible string\r
97 //\r
98 for (CompatibleItem = Compatible; CompatibleItem < Compatible + Len;\r
99 CompatibleItem += 1 + AsciiStrLen (CompatibleItem)) {\r
100\r
101 if (AsciiStrCmp (CompatibleItem, "arm,pl011") == 0) {\r
b1f3e48e
AB
102 NodeStatus = fdt_getprop (DeviceTreeBase, Node, "status", &Len);\r
103 if (NodeStatus != NULL && AsciiStrCmp (NodeStatus, "okay") != 0) {\r
104 continue;\r
105 }\r
106\r
f1f0ba19
LE
107 RegProperty = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);\r
108 if (Len != 16) {\r
109 return 0;\r
110 }\r
111 UartBase = (UINTN)fdt64_to_cpu (ReadUnaligned64 (RegProperty));\r
112\r
113 BaudRate = (UINTN)FixedPcdGet64 (PcdUartDefaultBaudRate);\r
114 ReceiveFifoDepth = 0; // Use the default value for Fifo depth\r
115 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);\r
116 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);\r
117 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);\r
118\r
119 Status = PL011UartInitializePort (\r
120 UartBase,\r
090916d8
EL
121 FixedPcdGet32 (PL011UartClkInHz),\r
122 &BaudRate,\r
123 &ReceiveFifoDepth,\r
124 &Parity,\r
125 &DataBits,\r
126 &StopBits\r
127 );\r
f1f0ba19
LE
128 if (!EFI_ERROR (Status)) {\r
129 return UartBase;\r
130 }\r
131 }\r
132 }\r
133 }\r
134 return 0;\r
135}\r
136\r
137/**\r
138 Write data to serial device.\r
139\r
140 @param Buffer Point of data buffer which need to be written.\r
141 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
142\r
143 @retval 0 Write data failed.\r
144 @retval !0 Actual number of bytes written to serial device.\r
145\r
146**/\r
147UINTN\r
148EFIAPI\r
149SerialPortWrite (\r
150 IN UINT8 *Buffer,\r
151 IN UINTN NumberOfBytes\r
152 )\r
153{\r
154 UINT64 SerialRegisterBase;\r
155\r
156 SerialRegisterBase = SerialPortGetBaseAddress ();\r
157 if (SerialRegisterBase != 0) {\r
158 return PL011UartWrite ((UINTN)SerialRegisterBase, Buffer, NumberOfBytes);\r
159 }\r
160 return 0;\r
161}\r
162\r
163/**\r
164 Read data from serial device and save the data in buffer.\r
165\r
166 @param Buffer Point of data buffer which need to be written.\r
167 @param NumberOfBytes Size of Buffer[].\r
168\r
169 @retval 0 Read data failed.\r
170 @retval !0 Actual number of bytes read from serial device.\r
171\r
172**/\r
173UINTN\r
174EFIAPI\r
175SerialPortRead (\r
176 OUT UINT8 *Buffer,\r
177 IN UINTN NumberOfBytes\r
178)\r
179{\r
180 return 0;\r
181}\r
182\r
183/**\r
184 Check to see if any data is available to be read from the debug device.\r
185\r
186 @retval TRUE At least one byte of data is available to be read\r
187 @retval FALSE No data is available to be read\r
188\r
189**/\r
190BOOLEAN\r
191EFIAPI\r
192SerialPortPoll (\r
193 VOID\r
194 )\r
195{\r
196 return FALSE;\r
197}\r
ad7f6bc2
SZ
198\r
199/**\r
200 Sets the control bits on a serial device.\r
201\r
202 @param[in] Control Sets the bits of Control that are settable.\r
203\r
204 @retval RETURN_SUCCESS The new control bits were set on the serial device.\r
205 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
206 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
207\r
208**/\r
209RETURN_STATUS\r
210EFIAPI\r
211SerialPortSetControl (\r
212 IN UINT32 Control\r
213 )\r
214{\r
215 return RETURN_UNSUPPORTED;\r
216}\r
217\r
218/**\r
219 Retrieve the status of the control bits on a serial device.\r
220\r
221 @param[out] Control A pointer to return the current control signals from the serial device.\r
222\r
223 @retval RETURN_SUCCESS The control bits were read from the serial device.\r
224 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
225 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
226\r
227**/\r
228RETURN_STATUS\r
229EFIAPI\r
230SerialPortGetControl (\r
231 OUT UINT32 *Control\r
232 )\r
233{\r
234 return RETURN_UNSUPPORTED;\r
235}\r
236\r
237/**\r
238 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,\r
239 data bits, and stop bits on a serial device.\r
240\r
241 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the\r
242 device's default interface speed.\r
243 On output, the value actually set.\r
244 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the\r
245 serial interface. A ReceiveFifoDepth value of 0 will use\r
246 the device's default FIFO depth.\r
247 On output, the value actually set.\r
248 @param Timeout The requested time out for a single character in microseconds.\r
249 This timeout applies to both the transmit and receive side of the\r
250 interface. A Timeout value of 0 will use the device's default time\r
251 out value.\r
252 On output, the value actually set.\r
253 @param Parity The type of parity to use on this serial device. A Parity value of\r
254 DefaultParity will use the device's default parity value.\r
255 On output, the value actually set.\r
256 @param DataBits The number of data bits to use on the serial device. A DataBits\r
257 vaule of 0 will use the device's default data bit setting.\r
258 On output, the value actually set.\r
259 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
260 value of DefaultStopBits will use the device's default number of\r
261 stop bits.\r
262 On output, the value actually set.\r
263\r
264 @retval RETURN_SUCCESS The new attributes were set on the serial device.\r
265 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
266 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.\r
267 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
268\r
269**/\r
270RETURN_STATUS\r
271EFIAPI\r
272SerialPortSetAttributes (\r
273 IN OUT UINT64 *BaudRate,\r
274 IN OUT UINT32 *ReceiveFifoDepth,\r
275 IN OUT UINT32 *Timeout,\r
276 IN OUT EFI_PARITY_TYPE *Parity,\r
277 IN OUT UINT8 *DataBits,\r
278 IN OUT EFI_STOP_BITS_TYPE *StopBits\r
279 )\r
280{\r
281 return RETURN_UNSUPPORTED;\r
282}\r
283\r