]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/FdtPL011SerialPortLib/EarlyFdtPL011SerialPortLib.c
ArmVirtPkg: switch to new PL011UartLib implementation
[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
1195b857 22#include <Library/PL011UartLib.h>\r
f1f0ba19 23#include <Library/SerialPortLib.h>\r
f1f0ba19
LE
24#include <libfdt.h>\r
25\r
f1f0ba19
LE
26RETURN_STATUS\r
27EFIAPI\r
28SerialPortInitialize (\r
29 VOID\r
30 )\r
31{\r
32 //\r
33 // This SerialPortInitialize() function is completely empty, for a number of\r
34 // reasons:\r
35 // - if we are executing from flash, it is hard to keep state (i.e., store the\r
36 // discovered base address in a global), and the most robust way to deal\r
37 // with this is to discover the base address at every Write ();\r
38 // - calls to the Write() function in this module may be issued before this\r
39 // initialization function is called: this is not a problem when the base\r
40 // address of the UART is hardcoded, and only the baud rate may be wrong,\r
41 // but if we don't know the base address yet, we may be poking into memory\r
42 // that does not tolerate being poked into;\r
43 // - SEC and PEI phases produce debug output only, so with debug disabled, no\r
44 // initialization (or device tree parsing) is performed at all.\r
45 //\r
46 // Note that this means that on *every* Write () call, the device tree will be\r
47 // parsed and the UART re-initialized. However, this is a small price to pay\r
48 // for having serial debug output on a UART with no fixed base address.\r
49 //\r
50 return RETURN_SUCCESS;\r
51}\r
52\r
53STATIC\r
54UINT64\r
55SerialPortGetBaseAddress (\r
56 VOID\r
57 )\r
58{\r
59 UINT64 BaudRate;\r
60 UINT32 ReceiveFifoDepth;\r
61 EFI_PARITY_TYPE Parity;\r
62 UINT8 DataBits;\r
63 EFI_STOP_BITS_TYPE StopBits;\r
64 VOID *DeviceTreeBase;\r
65 INT32 Node, Prev;\r
66 INT32 Len;\r
67 CONST CHAR8 *Compatible;\r
b1f3e48e 68 CONST CHAR8 *NodeStatus;\r
f1f0ba19
LE
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
b1f3e48e
AB
101 NodeStatus = fdt_getprop (DeviceTreeBase, Node, "status", &Len);\r
102 if (NodeStatus != NULL && AsciiStrCmp (NodeStatus, "okay") != 0) {\r
103 continue;\r
104 }\r
105\r
f1f0ba19
LE
106 RegProperty = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);\r
107 if (Len != 16) {\r
108 return 0;\r
109 }\r
110 UartBase = (UINTN)fdt64_to_cpu (ReadUnaligned64 (RegProperty));\r
111\r
112 BaudRate = (UINTN)FixedPcdGet64 (PcdUartDefaultBaudRate);\r
113 ReceiveFifoDepth = 0; // Use the default value for Fifo depth\r
114 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);\r
115 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);\r
116 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);\r
117\r
118 Status = PL011UartInitializePort (\r
119 UartBase,\r
090916d8
EL
120 FixedPcdGet32 (PL011UartClkInHz),\r
121 &BaudRate,\r
122 &ReceiveFifoDepth,\r
123 &Parity,\r
124 &DataBits,\r
125 &StopBits\r
126 );\r
f1f0ba19
LE
127 if (!EFI_ERROR (Status)) {\r
128 return UartBase;\r
129 }\r
130 }\r
131 }\r
132 }\r
133 return 0;\r
134}\r
135\r
136/**\r
137 Write data to serial device.\r
138\r
139 @param Buffer Point of data buffer which need to be written.\r
140 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
141\r
142 @retval 0 Write data failed.\r
143 @retval !0 Actual number of bytes written to serial device.\r
144\r
145**/\r
146UINTN\r
147EFIAPI\r
148SerialPortWrite (\r
149 IN UINT8 *Buffer,\r
150 IN UINTN NumberOfBytes\r
151 )\r
152{\r
153 UINT64 SerialRegisterBase;\r
154\r
155 SerialRegisterBase = SerialPortGetBaseAddress ();\r
156 if (SerialRegisterBase != 0) {\r
157 return PL011UartWrite ((UINTN)SerialRegisterBase, Buffer, NumberOfBytes);\r
158 }\r
159 return 0;\r
160}\r
161\r
162/**\r
163 Read data from serial device and save the data in buffer.\r
164\r
165 @param Buffer Point of data buffer which need to be written.\r
166 @param NumberOfBytes Size of Buffer[].\r
167\r
168 @retval 0 Read data failed.\r
169 @retval !0 Actual number of bytes read from serial device.\r
170\r
171**/\r
172UINTN\r
173EFIAPI\r
174SerialPortRead (\r
175 OUT UINT8 *Buffer,\r
176 IN UINTN NumberOfBytes\r
177)\r
178{\r
179 return 0;\r
180}\r
181\r
182/**\r
183 Check to see if any data is available to be read from the debug device.\r
184\r
185 @retval TRUE At least one byte of data is available to be read\r
186 @retval FALSE No data is available to be read\r
187\r
188**/\r
189BOOLEAN\r
190EFIAPI\r
191SerialPortPoll (\r
192 VOID\r
193 )\r
194{\r
195 return FALSE;\r
196}\r
ad7f6bc2
SZ
197\r
198/**\r
199 Sets the control bits on a serial device.\r
200\r
201 @param[in] Control Sets the bits of Control that are settable.\r
202\r
203 @retval RETURN_SUCCESS The new control bits were set on the serial device.\r
204 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
205 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
206\r
207**/\r
208RETURN_STATUS\r
209EFIAPI\r
210SerialPortSetControl (\r
211 IN UINT32 Control\r
212 )\r
213{\r
214 return RETURN_UNSUPPORTED;\r
215}\r
216\r
217/**\r
218 Retrieve the status of the control bits on a serial device.\r
219\r
220 @param[out] Control A pointer to return the current control signals from the serial device.\r
221\r
222 @retval RETURN_SUCCESS The control bits were read from the serial device.\r
223 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
224 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
225\r
226**/\r
227RETURN_STATUS\r
228EFIAPI\r
229SerialPortGetControl (\r
230 OUT UINT32 *Control\r
231 )\r
232{\r
233 return RETURN_UNSUPPORTED;\r
234}\r
235\r
236/**\r
237 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,\r
238 data bits, and stop bits on a serial device.\r
239\r
240 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the\r
241 device's default interface speed.\r
242 On output, the value actually set.\r
243 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the\r
244 serial interface. A ReceiveFifoDepth value of 0 will use\r
245 the device's default FIFO depth.\r
246 On output, the value actually set.\r
247 @param Timeout The requested time out for a single character in microseconds.\r
248 This timeout applies to both the transmit and receive side of the\r
249 interface. A Timeout value of 0 will use the device's default time\r
250 out value.\r
251 On output, the value actually set.\r
252 @param Parity The type of parity to use on this serial device. A Parity value of\r
253 DefaultParity will use the device's default parity value.\r
254 On output, the value actually set.\r
255 @param DataBits The number of data bits to use on the serial device. A DataBits\r
256 vaule of 0 will use the device's default data bit setting.\r
257 On output, the value actually set.\r
258 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
259 value of DefaultStopBits will use the device's default number of\r
260 stop bits.\r
261 On output, the value actually set.\r
262\r
263 @retval RETURN_SUCCESS The new attributes were set on the serial device.\r
264 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
265 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.\r
266 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
267\r
268**/\r
269RETURN_STATUS\r
270EFIAPI\r
271SerialPortSetAttributes (\r
272 IN OUT UINT64 *BaudRate,\r
273 IN OUT UINT32 *ReceiveFifoDepth,\r
274 IN OUT UINT32 *Timeout,\r
275 IN OUT EFI_PARITY_TYPE *Parity,\r
276 IN OUT UINT8 *DataBits,\r
277 IN OUT EFI_STOP_BITS_TYPE *StopBits\r
278 )\r
279{\r
280 return RETURN_UNSUPPORTED;\r
281}\r
282\r