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