2 Serial I/O Port library functions with no library constructor/destructor
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/PcdLib.h>
21 #include <Drivers/PL011Uart.h>
23 #define FRACTION_PART_SIZE_IN_BITS 6
24 #define FRACTION_PART_MASK ((1 << FRACTION_PART_SIZE_IN_BITS) - 1)
27 // EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE is the only
28 // control bit that is not supported.
30 STATIC CONST UINT32 mInvalidControlBits
= EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE
;
34 Initialise the serial port to the specified settings.
35 All unspecified settings will be set to the default values.
37 @param UartBase The base address of the serial device.
38 @param UartClkInHz The clock in Hz for the serial device.
39 Ignored if the PCD PL011UartInteger is not 0
40 @param BaudRate The baud rate of the serial device. If the
41 baud rate is not supported, the speed will be
42 reduced to the nearest supported one and the
43 variable's value will be updated accordingly.
44 @param ReceiveFifoDepth The number of characters the device will
45 buffer on input. Value of 0 will use the
46 device's default FIFO depth.
47 @param Parity If applicable, this is the EFI_PARITY_TYPE
48 that is computed or checked as each character
49 is transmitted or received. If the device
50 does not support parity, the value is the
52 @param DataBits The number of data bits in each character.
53 @param StopBits If applicable, the EFI_STOP_BITS_TYPE number
54 of stop bits per character.
55 If the device does not support stop bits, the
56 value is the default stop bit value.
58 @retval RETURN_SUCCESS All attributes were set correctly on the
60 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an
66 PL011UartInitializePort (
68 IN UINT32 UartClkInHz
,
69 IN OUT UINT64
*BaudRate
,
70 IN OUT UINT32
*ReceiveFifoDepth
,
71 IN OUT EFI_PARITY_TYPE
*Parity
,
72 IN OUT UINT8
*DataBits
,
73 IN OUT EFI_STOP_BITS_TYPE
*StopBits
81 // The PL011 supports a buffer of 1, 16 or 32 chars. Therefore we can accept
82 // 1 char buffer as the minimum FIFO size. Because everything can be rounded
83 // down, there is no maximum FIFO size.
84 if ((*ReceiveFifoDepth
== 0) || (*ReceiveFifoDepth
>= 32)) {
86 LineControl
= PL011_UARTLCR_H_FEN
;
87 if (PL011_UARTPID2_VER (MmioRead32 (UartBase
+ UARTPID2
)) > PL011_VER_R1P4
)
88 *ReceiveFifoDepth
= 32;
90 *ReceiveFifoDepth
= 16;
94 // Nothing else to do. 1 byte FIFO is default.
95 *ReceiveFifoDepth
= 1;
105 // Nothing to do. Parity is disabled by default.
108 LineControl
|= (PL011_UARTLCR_H_PEN
| PL011_UARTLCR_H_EPS
);
111 LineControl
|= PL011_UARTLCR_H_PEN
;
114 LineControl
|= ( PL011_UARTLCR_H_PEN \
115 | PL011_UARTLCR_H_SPS \
116 | PL011_UARTLCR_H_EPS
);
119 LineControl
|= (PL011_UARTLCR_H_PEN
| PL011_UARTLCR_H_SPS
);
122 return RETURN_INVALID_PARAMETER
;
132 LineControl
|= PL011_UARTLCR_H_WLEN_8
;
135 LineControl
|= PL011_UARTLCR_H_WLEN_7
;
138 LineControl
|= PL011_UARTLCR_H_WLEN_6
;
141 LineControl
|= PL011_UARTLCR_H_WLEN_5
;
144 return RETURN_INVALID_PARAMETER
;
151 case DefaultStopBits
:
152 *StopBits
= OneStopBit
;
154 // Nothing to do. One stop bit is enabled by default.
157 LineControl
|= PL011_UARTLCR_H_STP2
;
159 case OneFiveStopBits
:
160 // Only 1 or 2 stop bits are supported
162 return RETURN_INVALID_PARAMETER
;
165 // Don't send the LineControl value to the PL011 yet,
166 // wait until after the Baud Rate setting.
167 // This ensures we do not mess up the UART settings halfway through
168 // in the rare case when there is an error with the Baud Rate.
174 // If PL011 Integer value has been defined then always ignore the BAUD rate
175 if (FixedPcdGet32 (PL011UartInteger
) != 0) {
176 Integer
= FixedPcdGet32 (PL011UartInteger
);
177 Fractional
= FixedPcdGet32 (PL011UartFractional
);
179 // If BAUD rate is zero then replace it with the system default value
180 if (*BaudRate
== 0) {
181 *BaudRate
= FixedPcdGet32 (PcdSerialBaudRate
);
182 if (*BaudRate
== 0) {
183 return RETURN_INVALID_PARAMETER
;
186 if (0 == UartClkInHz
) {
187 return RETURN_INVALID_PARAMETER
;
190 Divisor
= (UartClkInHz
* 4) / *BaudRate
;
191 Integer
= Divisor
>> FRACTION_PART_SIZE_IN_BITS
;
192 Fractional
= Divisor
& FRACTION_PART_MASK
;
194 // Set Baud Rate Registers
195 MmioWrite32 (UartBase
+ UARTIBRD
, Integer
);
196 MmioWrite32 (UartBase
+ UARTFBRD
, Fractional
);
198 // No parity, 1 stop, no fifo, 8 data bits
199 MmioWrite32 (UartBase
+ UARTLCR_H
, LineControl
);
201 // Clear any pending errors
202 MmioWrite32 (UartBase
+ UARTECR
, 0);
204 // Enable Tx, Rx, and UART overall
205 MmioWrite32 (UartBase
+ UARTCR
,
206 PL011_UARTCR_RXE
| PL011_UARTCR_TXE
| PL011_UARTCR_UARTEN
);
208 return RETURN_SUCCESS
;
213 Assert or deassert the control signals on a serial port.
214 The following control signals are set according their bit settings :
216 . Data Terminal Ready
218 @param[in] UartBase UART registers base address
219 @param[in] Control The following bits are taken into account :
220 . EFI_SERIAL_REQUEST_TO_SEND : assert/deassert the
221 "Request To Send" control signal if this bit is
223 . EFI_SERIAL_DATA_TERMINAL_READY : assert/deassert
224 the "Data Terminal Ready" control signal if this
225 bit is equal to one/zero.
226 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : enable/disable
227 the hardware loopback if this bit is equal to
229 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : not supported.
230 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : enable/
231 disable the hardware flow control based on CTS (Clear
232 To Send) and RTS (Ready To Send) control signals.
234 @retval RETURN_SUCCESS The new control bits were set on the device.
235 @retval RETURN_UNSUPPORTED The device does not support this operation.
240 PL011UartSetControl (
247 if (Control
& (mInvalidControlBits
)) {
248 return RETURN_UNSUPPORTED
;
251 Bits
= MmioRead32 (UartBase
+ UARTCR
);
253 if (Control
& EFI_SERIAL_REQUEST_TO_SEND
) {
254 Bits
|= PL011_UARTCR_RTS
;
256 Bits
&= ~PL011_UARTCR_RTS
;
259 if (Control
& EFI_SERIAL_DATA_TERMINAL_READY
) {
260 Bits
|= PL011_UARTCR_DTR
;
262 Bits
&= ~PL011_UARTCR_DTR
;
265 if (Control
& EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE
) {
266 Bits
|= PL011_UARTCR_LBE
;
268 Bits
&= ~PL011_UARTCR_LBE
;
271 if (Control
& EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE
) {
272 Bits
|= (PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
);
274 Bits
&= ~(PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
);
277 MmioWrite32 (UartBase
+ UARTCR
, Bits
);
279 return RETURN_SUCCESS
;
284 Retrieve the status of the control bits on a serial device.
286 @param[in] UartBase UART registers base address
287 @param[out] Control Status of the control bits on a serial device :
289 . EFI_SERIAL_DATA_CLEAR_TO_SEND,
290 EFI_SERIAL_DATA_SET_READY,
291 EFI_SERIAL_RING_INDICATE,
292 EFI_SERIAL_CARRIER_DETECT,
293 EFI_SERIAL_REQUEST_TO_SEND,
294 EFI_SERIAL_DATA_TERMINAL_READY
295 are all related to the DTE (Data Terminal Equipment)
296 and DCE (Data Communication Equipment) modes of
297 operation of the serial device.
298 . EFI_SERIAL_INPUT_BUFFER_EMPTY : equal to one if the
299 receive buffer is empty, 0 otherwise.
300 . EFI_SERIAL_OUTPUT_BUFFER_EMPTY : equal to one if the
301 transmit buffer is empty, 0 otherwise.
302 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : equal to one if
303 the hardware loopback is enabled (the ouput feeds the
304 receive buffer), 0 otherwise.
305 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : equal to one if
306 a loopback is accomplished by software, 0 otherwise.
307 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : equal to
308 one if the hardware flow control based on CTS (Clear
309 To Send) and RTS (Ready To Send) control signals is
310 enabled, 0 otherwise.
312 @retval RETURN_SUCCESS The control bits were read from the serial device.
317 PL011UartGetControl (
323 UINT32 ControlRegister
;
326 FlagRegister
= MmioRead32 (UartBase
+ UARTFR
);
327 ControlRegister
= MmioRead32 (UartBase
+ UARTCR
);
331 if ((FlagRegister
& PL011_UARTFR_CTS
) == PL011_UARTFR_CTS
) {
332 *Control
|= EFI_SERIAL_CLEAR_TO_SEND
;
335 if ((FlagRegister
& PL011_UARTFR_DSR
) == PL011_UARTFR_DSR
) {
336 *Control
|= EFI_SERIAL_DATA_SET_READY
;
339 if ((FlagRegister
& PL011_UARTFR_RI
) == PL011_UARTFR_RI
) {
340 *Control
|= EFI_SERIAL_RING_INDICATE
;
343 if ((FlagRegister
& PL011_UARTFR_DCD
) == PL011_UARTFR_DCD
) {
344 *Control
|= EFI_SERIAL_CARRIER_DETECT
;
347 if ((ControlRegister
& PL011_UARTCR_RTS
) == PL011_UARTCR_RTS
) {
348 *Control
|= EFI_SERIAL_REQUEST_TO_SEND
;
351 if ((ControlRegister
& PL011_UARTCR_DTR
) == PL011_UARTCR_DTR
) {
352 *Control
|= EFI_SERIAL_DATA_TERMINAL_READY
;
355 if ((FlagRegister
& PL011_UARTFR_RXFE
) == PL011_UARTFR_RXFE
) {
356 *Control
|= EFI_SERIAL_INPUT_BUFFER_EMPTY
;
359 if ((FlagRegister
& PL011_UARTFR_TXFE
) == PL011_UARTFR_TXFE
) {
360 *Control
|= EFI_SERIAL_OUTPUT_BUFFER_EMPTY
;
363 if ((ControlRegister
& (PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
))
364 == (PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
)) {
365 *Control
|= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE
;
368 if ((ControlRegister
& PL011_UARTCR_LBE
) == PL011_UARTCR_LBE
) {
369 *Control
|= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE
;
372 return RETURN_SUCCESS
;
376 Write data to serial device.
378 @param Buffer Point of data buffer which need to be written.
379 @param NumberOfBytes Number of output bytes which are cached in Buffer.
381 @retval 0 Write data failed.
382 @retval !0 Actual number of bytes written to serial device.
390 IN UINTN NumberOfBytes
393 UINT8
* CONST Final
= &Buffer
[NumberOfBytes
];
395 while (Buffer
< Final
) {
396 // Wait until UART able to accept another char
397 while ((MmioRead32 (UartBase
+ UARTFR
) & UART_TX_FULL_FLAG_MASK
));
399 MmioWrite8 (UartBase
+ UARTDR
, *Buffer
++);
402 return NumberOfBytes
;
406 Read data from serial device and save the data in buffer.
408 @param Buffer Point of data buffer which need to be written.
409 @param NumberOfBytes Number of output bytes which are cached in Buffer.
411 @retval 0 Read data failed.
412 @retval !0 Actual number of bytes read from serial device.
420 IN UINTN NumberOfBytes
425 for (Count
= 0; Count
< NumberOfBytes
; Count
++, Buffer
++) {
426 while ((MmioRead32 (UartBase
+ UARTFR
) & UART_RX_EMPTY_FLAG_MASK
) != 0);
427 *Buffer
= MmioRead8 (UartBase
+ UARTDR
);
430 return NumberOfBytes
;
434 Check to see if any data is available to be read from the debug device.
436 @retval EFI_SUCCESS At least one byte of data is available to be read
437 @retval EFI_NOT_READY No data is available to be read
438 @retval EFI_DEVICE_ERROR The serial device is not functioning properly
447 return ((MmioRead32 (UartBase
+ UARTFR
) & UART_RX_EMPTY_FLAG_MASK
) == 0);