]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/Library/FdtPL011SerialPortLib/FdtPL011SerialPortLib.c
ArmVirtPkg/ArmVirtPsciResetSystemLib: Add a new API ResetSystem
[mirror_edk2.git] / ArmVirtPkg / Library / FdtPL011SerialPortLib / FdtPL011SerialPortLib.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) 2014, Red Hat, Inc.<BR>
8 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
9
10 SPDX-License-Identifier: BSD-2-Clause-Patent
11
12 **/
13
14 #include <Base.h>
15
16 #include <Library/PcdLib.h>
17 #include <Library/PL011UartLib.h>
18 #include <Library/SerialPortLib.h>
19 #include <Pi/PiBootMode.h>
20 #include <Uefi/UefiBaseType.h>
21 #include <Uefi/UefiMultiPhase.h>
22 #include <Pi/PiHob.h>
23 #include <Library/HobLib.h>
24 #include <Guid/EarlyPL011BaseAddress.h>
25
26 STATIC UINTN mSerialBaseAddress;
27
28 RETURN_STATUS
29 EFIAPI
30 SerialPortInitialize (
31 VOID
32 )
33 {
34 return RETURN_SUCCESS;
35 }
36
37 /**
38
39 Program hardware of Serial port
40
41 @return RETURN_NOT_FOUND if no PL011 base address could be found
42 Otherwise, result of PL011UartInitializePort () is returned
43
44 **/
45 RETURN_STATUS
46 EFIAPI
47 FdtPL011SerialPortLibInitialize (
48 VOID
49 )
50 {
51 VOID *Hob;
52 CONST UINT64 *UartBase;
53 UINT64 BaudRate;
54 UINT32 ReceiveFifoDepth;
55 EFI_PARITY_TYPE Parity;
56 UINT8 DataBits;
57 EFI_STOP_BITS_TYPE StopBits;
58
59 Hob = GetFirstGuidHob (&gEarlyPL011BaseAddressGuid);
60 if (Hob == NULL || GET_GUID_HOB_DATA_SIZE (Hob) != sizeof *UartBase) {
61 return RETURN_NOT_FOUND;
62 }
63 UartBase = GET_GUID_HOB_DATA (Hob);
64
65 mSerialBaseAddress = (UINTN)*UartBase;
66 if (mSerialBaseAddress == 0) {
67 return RETURN_NOT_FOUND;
68 }
69
70 BaudRate = (UINTN)PcdGet64 (PcdUartDefaultBaudRate);
71 ReceiveFifoDepth = 0; // Use the default value for Fifo depth
72 Parity = (EFI_PARITY_TYPE)PcdGet8 (PcdUartDefaultParity);
73 DataBits = PcdGet8 (PcdUartDefaultDataBits);
74 StopBits = (EFI_STOP_BITS_TYPE) PcdGet8 (PcdUartDefaultStopBits);
75
76 return PL011UartInitializePort (
77 mSerialBaseAddress,
78 FixedPcdGet32 (PL011UartClkInHz),
79 &BaudRate,
80 &ReceiveFifoDepth,
81 &Parity,
82 &DataBits,
83 &StopBits
84 );
85 }
86
87 /**
88 Write data to serial device.
89
90 @param Buffer Point of data buffer which need to be written.
91 @param NumberOfBytes Number of output bytes which are cached in Buffer.
92
93 @retval 0 Write data failed.
94 @retval !0 Actual number of bytes written to serial device.
95
96 **/
97 UINTN
98 EFIAPI
99 SerialPortWrite (
100 IN UINT8 *Buffer,
101 IN UINTN NumberOfBytes
102 )
103 {
104 if (mSerialBaseAddress != 0) {
105 return PL011UartWrite (mSerialBaseAddress, Buffer, NumberOfBytes);
106 }
107 return 0;
108 }
109
110 /**
111 Read data from serial device and save the data in buffer.
112
113 @param Buffer Point of data buffer which need to be written.
114 @param NumberOfBytes Number of output bytes which are cached in Buffer.
115
116 @retval 0 Read data failed.
117 @retval !0 Actual number of bytes read from serial device.
118
119 **/
120 UINTN
121 EFIAPI
122 SerialPortRead (
123 OUT UINT8 *Buffer,
124 IN UINTN NumberOfBytes
125 )
126 {
127 if (mSerialBaseAddress != 0) {
128 return PL011UartRead (mSerialBaseAddress, Buffer, NumberOfBytes);
129 }
130 return 0;
131 }
132
133 /**
134 Check to see if any data is available to be read from the debug device.
135
136 @retval TRUE At least one byte of data is available to be read
137 @retval FALSE No data is available to be read
138
139 **/
140 BOOLEAN
141 EFIAPI
142 SerialPortPoll (
143 VOID
144 )
145 {
146 if (mSerialBaseAddress != 0) {
147 return PL011UartPoll (mSerialBaseAddress);
148 }
149 return FALSE;
150 }
151
152 /**
153 Sets the baud rate, receive FIFO depth, transmit/receive time out, parity,
154 data bits, and stop bits on a serial device.
155
156 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
157 device's default interface speed.
158 On output, the value actually set.
159 @param ReceiveFifoDepth The requested depth of the FIFO on the receive side of the
160 serial interface. A ReceiveFifoDepth value of 0 will use
161 the device's default FIFO depth.
162 On output, the value actually set.
163 @param Timeout The requested time out for a single character in microseconds.
164 This timeout applies to both the transmit and receive side of the
165 interface. A Timeout value of 0 will use the device's default time
166 out value.
167 On output, the value actually set.
168 @param Parity The type of parity to use on this serial device. A Parity value of
169 DefaultParity will use the device's default parity value.
170 On output, the value actually set.
171 @param DataBits The number of data bits to use on the serial device. A DataBits
172 value of 0 will use the device's default data bit setting.
173 On output, the value actually set.
174 @param StopBits The number of stop bits to use on this serial device. A StopBits
175 value of DefaultStopBits will use the device's default number of
176 stop bits.
177 On output, the value actually set.
178
179 @retval RETURN_SUCCESS The new attributes were set on the serial device.
180 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
181 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
182 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
183
184 **/
185 RETURN_STATUS
186 EFIAPI
187 SerialPortSetAttributes (
188 IN OUT UINT64 *BaudRate,
189 IN OUT UINT32 *ReceiveFifoDepth,
190 IN OUT UINT32 *Timeout,
191 IN OUT EFI_PARITY_TYPE *Parity,
192 IN OUT UINT8 *DataBits,
193 IN OUT EFI_STOP_BITS_TYPE *StopBits
194 )
195 {
196 RETURN_STATUS Status;
197
198 if (mSerialBaseAddress == 0) {
199 Status = RETURN_UNSUPPORTED;
200 } else {
201 Status = PL011UartInitializePort (
202 mSerialBaseAddress,
203 FixedPcdGet32 (PL011UartClkInHz),
204 BaudRate,
205 ReceiveFifoDepth,
206 Parity,
207 DataBits,
208 StopBits
209 );
210 }
211
212 return Status;
213 }
214
215 /**
216 Sets the control bits on a serial device.
217
218 @param Control Sets the bits of Control that are settable.
219
220 @retval RETURN_SUCCESS The new control bits were set on the serial device.
221 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
222 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
223
224 **/
225 RETURN_STATUS
226 EFIAPI
227 SerialPortSetControl (
228 IN UINT32 Control
229 )
230 {
231 RETURN_STATUS Status;
232
233 if (mSerialBaseAddress == 0) {
234 Status = RETURN_UNSUPPORTED;
235 } else {
236 Status = PL011UartSetControl (mSerialBaseAddress, Control);
237 }
238
239 return Status;
240 }
241
242 /**
243 Retrieve the status of the control bits on a serial device.
244
245 @param Control A pointer to return the current control signals from the serial device.
246
247 @retval RETURN_SUCCESS The control bits were read from the serial device.
248 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
249 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
250
251 **/
252 RETURN_STATUS
253 EFIAPI
254 SerialPortGetControl (
255 OUT UINT32 *Control
256 )
257 {
258 RETURN_STATUS Status;
259
260 if (mSerialBaseAddress == 0) {
261 Status = RETURN_UNSUPPORTED;
262 } else {
263 Status = PL011UartGetControl (mSerialBaseAddress, Control);
264 }
265
266 return Status;
267 }
268