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