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