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