]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Library/PL011SerialPortLib/PL011SerialPortLib.c
ArmPlatformPkg: reorganize PL011 code
[mirror_edk2.git] / ArmPlatformPkg / Library / PL011SerialPortLib / PL011SerialPortLib.c
1 /** @file
2 Serial I/O Port library functions with no library constructor/destructor
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2012 - 2016, ARM Ltd. All rights reserved.<BR>
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
7
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include <Base.h>
19
20 #include <Library/IoLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/PL011UartLib.h>
23 #include <Library/SerialPortLib.h>
24
25 /** Initialise the serial device hardware with default settings.
26
27 @retval RETURN_SUCCESS The serial device was initialised.
28 @retval RETURN_INVALID_PARAMETER One or more of the default settings
29 has an unsupported value.
30 **/
31 RETURN_STATUS
32 EFIAPI
33 SerialPortInitialize (
34 VOID
35 )
36 {
37 UINT64 BaudRate;
38 UINT32 ReceiveFifoDepth;
39 EFI_PARITY_TYPE Parity;
40 UINT8 DataBits;
41 EFI_STOP_BITS_TYPE StopBits;
42
43 BaudRate = FixedPcdGet64 (PcdUartDefaultBaudRate);
44 ReceiveFifoDepth = 0; // Use default FIFO depth
45 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);
46 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);
47 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);
48
49 return PL011UartInitializePort (
50 (UINTN)FixedPcdGet64 (PcdSerialRegisterBase),
51 FixedPcdGet32 (PL011UartClkInHz),
52 &BaudRate,
53 &ReceiveFifoDepth,
54 &Parity,
55 &DataBits,
56 &StopBits
57 );
58 }
59
60 /**
61 Write data to serial device.
62
63 @param Buffer Point of data buffer which need to be written.
64 @param NumberOfBytes Number of output bytes which are cached in Buffer.
65
66 @retval 0 Write data failed.
67 @retval !0 Actual number of bytes written to serial device.
68
69 **/
70 UINTN
71 EFIAPI
72 SerialPortWrite (
73 IN UINT8 *Buffer,
74 IN UINTN NumberOfBytes
75 )
76 {
77 return PL011UartWrite ((UINTN)FixedPcdGet64 (PcdSerialRegisterBase), Buffer, NumberOfBytes);
78 }
79
80 /**
81 Read data from serial device and save the data in buffer.
82
83 @param Buffer Point of data buffer which need to be written.
84 @param NumberOfBytes Number of output bytes which are cached in Buffer.
85
86 @retval 0 Read data failed.
87 @retval !0 Actual number of bytes read from serial device.
88
89 **/
90 UINTN
91 EFIAPI
92 SerialPortRead (
93 OUT UINT8 *Buffer,
94 IN UINTN NumberOfBytes
95 )
96 {
97 return PL011UartRead ((UINTN)FixedPcdGet64 (PcdSerialRegisterBase), Buffer, NumberOfBytes);
98 }
99
100 /**
101 Check to see if any data is available to be read from the debug device.
102
103 @retval TRUE At least one byte of data is available to be read
104 @retval FALSE No data is available to be read
105
106 **/
107 BOOLEAN
108 EFIAPI
109 SerialPortPoll (
110 VOID
111 )
112 {
113 return PL011UartPoll ((UINTN)FixedPcdGet64 (PcdSerialRegisterBase));
114 }
115 /**
116 Set new attributes to PL011.
117
118 @param BaudRate The baud rate of the serial device. If the
119 baud rate is not supported, the speed will
120 be reduced down to the nearest supported one
121 and the variable's value will be updated
122 accordingly.
123 @param ReceiveFifoDepth The number of characters the device will
124 buffer on input. If the specified value is
125 not supported, the variable's value will
126 be reduced down to the nearest supported one.
127 @param Timeout If applicable, the number of microseconds the
128 device will wait before timing out a Read or
129 a Write operation.
130 @param Parity If applicable, this is the EFI_PARITY_TYPE
131 that is computed or checked as each character
132 is transmitted or received. If the device
133 does not support parity, the value is the
134 default parity value.
135 @param DataBits The number of data bits in each character
136 @param StopBits If applicable, the EFI_STOP_BITS_TYPE number
137 of stop bits per character. If the device
138 does not support stop bits, the value is the
139 default stop bit value.
140
141 @retval EFI_SUCCESS All attributes were set correctly.
142 @retval EFI_INVALID_PARAMETERS One or more attributes has an unsupported
143 value.
144
145 **/
146 RETURN_STATUS
147 EFIAPI
148 SerialPortSetAttributes (
149 IN OUT UINT64 *BaudRate,
150 IN OUT UINT32 *ReceiveFifoDepth,
151 IN OUT UINT32 *Timeout,
152 IN OUT EFI_PARITY_TYPE *Parity,
153 IN OUT UINT8 *DataBits,
154 IN OUT EFI_STOP_BITS_TYPE *StopBits
155 )
156 {
157 return PL011UartInitializePort (
158 (UINTN)FixedPcdGet64 (PcdSerialRegisterBase),
159 FixedPcdGet32 (PL011UartClkInHz),
160 BaudRate,
161 ReceiveFifoDepth,
162 Parity,
163 DataBits,
164 StopBits
165 );
166 }
167
168 /**
169
170 Assert or deassert the control signals on a serial port.
171 The following control signals are set according their bit settings :
172 . Request to Send
173 . Data Terminal Ready
174
175 @param[in] Control The following bits are taken into account :
176 . EFI_SERIAL_REQUEST_TO_SEND : assert/deassert the
177 "Request To Send" control signal if this bit is
178 equal to one/zero.
179 . EFI_SERIAL_DATA_TERMINAL_READY : assert/deassert
180 the "Data Terminal Ready" control signal if this
181 bit is equal to one/zero.
182 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : enable/disable
183 the hardware loopback if this bit is equal to
184 one/zero.
185 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : not supported.
186 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : enable/
187 disable the hardware flow control based on CTS (Clear
188 To Send) and RTS (Ready To Send) control signals.
189
190 @retval RETURN_SUCCESS The new control bits were set on the device.
191 @retval RETURN_UNSUPPORTED The device does not support this operation.
192
193 **/
194 RETURN_STATUS
195 EFIAPI
196 SerialPortSetControl (
197 IN UINT32 Control
198 )
199 {
200 return PL011UartSetControl ((UINTN)FixedPcdGet64 (PcdSerialRegisterBase), Control);
201 }
202
203 /**
204
205 Retrieve the status of the control bits on a serial device.
206
207 @param[out] Control Status of the control bits on a serial device :
208
209 . EFI_SERIAL_DATA_CLEAR_TO_SEND,
210 EFI_SERIAL_DATA_SET_READY,
211 EFI_SERIAL_RING_INDICATE,
212 EFI_SERIAL_CARRIER_DETECT,
213 EFI_SERIAL_REQUEST_TO_SEND,
214 EFI_SERIAL_DATA_TERMINAL_READY
215 are all related to the DTE (Data Terminal Equipment)
216 and DCE (Data Communication Equipment) modes of
217 operation of the serial device.
218 . EFI_SERIAL_INPUT_BUFFER_EMPTY : equal to one if the
219 receive buffer is empty, 0 otherwise.
220 . EFI_SERIAL_OUTPUT_BUFFER_EMPTY : equal to one if the
221 transmit buffer is empty, 0 otherwise.
222 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : equal to one if
223 the hardware loopback is enabled (the output feeds
224 the receive buffer), 0 otherwise.
225 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : equal to one
226 if a loopback is accomplished by software, else 0.
227 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : equal to
228 one if the hardware flow control based on CTS (Clear
229 To Send) and RTS (Ready To Send) control signals is
230 enabled, 0 otherwise.
231
232 @retval RETURN_SUCCESS The control bits were read from the device.
233
234 **/
235 RETURN_STATUS
236 EFIAPI
237 SerialPortGetControl (
238 OUT UINT32 *Control
239 )
240 {
241 return PL011UartGetControl ((UINTN)FixedPcdGet64 (PcdSerialRegisterBase), Control);
242 }