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