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 - 2012, 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>
25 Initialise the serial port to the specified settings.
26 All unspecified settings will be set to the default values.
28 @return Always return EFI_SUCCESS or EFI_INVALID_PARAMETER.
33 PL011UartInitializePort (
36 IN UINT32 ReceiveFifoDepth
,
38 IN EFI_PARITY_TYPE Parity
,
40 IN EFI_STOP_BITS_TYPE StopBits
46 // The BaudRate must be passed
48 return RETURN_INVALID_PARAMETER
;
53 // The PL011 supports a buffer of either 1 or 32 chars. Therefore we can accept
54 // 1 char buffer as the minimum fifo size. Because everything can be rounded down,
55 // there is no maximum fifo size.
56 if (ReceiveFifoDepth
== 0) {
57 LineControl
|= PL011_UARTLCR_H_FEN
;
58 } else if (ReceiveFifoDepth
< 32) {
59 // Nothing else to do. 1 byte fifo is default.
60 } else if (ReceiveFifoDepth
>= 32) {
61 LineControl
|= PL011_UARTLCR_H_FEN
;
70 // Nothing to do. Parity is disabled by default.
73 LineControl
|= (PL011_UARTLCR_H_PEN
| PL011_UARTLCR_H_EPS
);
76 LineControl
|= PL011_UARTLCR_H_PEN
;
79 LineControl
|= (PL011_UARTLCR_H_PEN
| PL011_UARTLCR_H_SPS
| PL011_UARTLCR_H_EPS
);
82 LineControl
|= (PL011_UARTLCR_H_PEN
| PL011_UARTLCR_H_SPS
);
85 return RETURN_INVALID_PARAMETER
;
94 LineControl
|= PL011_UARTLCR_H_WLEN_8
;
97 LineControl
|= PL011_UARTLCR_H_WLEN_7
;
100 LineControl
|= PL011_UARTLCR_H_WLEN_6
;
103 LineControl
|= PL011_UARTLCR_H_WLEN_5
;
106 return RETURN_INVALID_PARAMETER
;
113 case DefaultStopBits
:
115 // Nothing to do. One stop bit is enabled by default.
118 LineControl
|= PL011_UARTLCR_H_STP2
;
120 case OneFiveStopBits
:
121 // Only 1 or 2 stops bits are supported
123 return RETURN_INVALID_PARAMETER
;
126 // Don't send the LineControl value to the PL011 yet,
127 // wait until after the Baud Rate setting.
128 // This ensures we do not mess up the UART settings halfway through
129 // in the rare case when there is an error with the Baud Rate.
134 if (PcdGet32(PL011UartInteger
) != 0) {
135 // Integer and Factional part must be different of 0
136 ASSERT(PcdGet32(PL011UartFractional
) != 0);
138 MmioWrite32 (UartBase
+ UARTIBRD
, PcdGet32(PL011UartInteger
));
139 MmioWrite32 (UartBase
+ UARTFBRD
, PcdGet32(PL011UartFractional
));
141 Divisor
= (PcdGet32 (PL011UartClkInHz
) * 4) / BaudRate
;
142 MmioWrite32 (UartBase
+ UARTIBRD
, Divisor
>> 6);
143 MmioWrite32 (UartBase
+ UARTFBRD
, Divisor
& 0x3F);
146 // No parity, 1 stop, no fifo, 8 data bits
147 MmioWrite32 (UartBase
+ UARTLCR_H
, LineControl
);
149 // Clear any pending errors
150 MmioWrite32 (UartBase
+ UARTECR
, 0);
152 // Enable tx, rx, and uart overall
153 MmioWrite32 (UartBase
+ UARTCR
, PL011_UARTCR_RXE
| PL011_UARTCR_TXE
| PL011_UARTCR_UARTEN
);
155 return RETURN_SUCCESS
;
159 Set the serial device control bits.
161 @param UartBase The base address of the PL011 UART.
162 @param Control Control bits which are to be set on the serial device.
164 @retval EFI_SUCCESS The new control bits were set on the serial device.
165 @retval EFI_UNSUPPORTED The serial device does not support this operation.
166 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
171 PL011UartSetControl (
177 UINT32 ValidControlBits
;
179 ValidControlBits
= ( EFI_SERIAL_REQUEST_TO_SEND
180 | EFI_SERIAL_DATA_TERMINAL_READY
181 // | EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE // Not implemented yet.
182 // | EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE // Not implemented yet.
183 | EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE
186 if (Control
& (~ValidControlBits
)) {
187 return EFI_UNSUPPORTED
;
190 Bits
= MmioRead32 (UartBase
+ UARTCR
);
192 if (Control
& EFI_SERIAL_REQUEST_TO_SEND
) {
193 Bits
|= PL011_UARTCR_RTS
;
196 if (Control
& EFI_SERIAL_DATA_TERMINAL_READY
) {
197 Bits
|= PL011_UARTCR_DTR
;
200 if (Control
& EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE
) {
201 Bits
|= PL011_UARTCR_LBE
;
204 if (Control
& EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE
) {
205 Bits
|= (PL011_UARTCR_CTSEN
& PL011_UARTCR_RTSEN
);
208 MmioWrite32 (UartBase
+ UARTCR
, Bits
);
210 return RETURN_SUCCESS
;
214 Get the serial device control bits.
216 @param UartBase The base address of the PL011 UART.
217 @param Control Control signals read from the serial device.
219 @retval EFI_SUCCESS The control bits were read from the serial device.
220 @retval EFI_DEVICE_ERROR The serial device is not functioning correctly.
225 PL011UartGetControl (
231 UINT32 ControlRegister
;
234 FlagRegister
= MmioRead32 (UartBase
+ UARTFR
);
235 ControlRegister
= MmioRead32 (UartBase
+ UARTCR
);
239 if ((FlagRegister
& PL011_UARTFR_CTS
) == PL011_UARTFR_CTS
) {
240 *Control
|= EFI_SERIAL_CLEAR_TO_SEND
;
243 if ((FlagRegister
& PL011_UARTFR_DSR
) == PL011_UARTFR_DSR
) {
244 *Control
|= EFI_SERIAL_DATA_SET_READY
;
247 if ((FlagRegister
& PL011_UARTFR_RI
) == PL011_UARTFR_RI
) {
248 *Control
|= EFI_SERIAL_RING_INDICATE
;
251 if ((FlagRegister
& PL011_UARTFR_DCD
) == PL011_UARTFR_DCD
) {
252 *Control
|= EFI_SERIAL_CARRIER_DETECT
;
255 if ((ControlRegister
& PL011_UARTCR_RTS
) == PL011_UARTCR_RTS
) {
256 *Control
|= EFI_SERIAL_REQUEST_TO_SEND
;
259 if ((ControlRegister
& PL011_UARTCR_DTR
) == PL011_UARTCR_DTR
) {
260 *Control
|= EFI_SERIAL_DATA_TERMINAL_READY
;
263 if ((FlagRegister
& PL011_UARTFR_RXFE
) == PL011_UARTFR_RXFE
) {
264 *Control
|= EFI_SERIAL_INPUT_BUFFER_EMPTY
;
267 if ((FlagRegister
& PL011_UARTFR_TXFE
) == PL011_UARTFR_TXFE
) {
268 *Control
|= EFI_SERIAL_OUTPUT_BUFFER_EMPTY
;
271 if ((ControlRegister
& (PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
)) == (PL011_UARTCR_CTSEN
| PL011_UARTCR_RTSEN
)) {
272 *Control
|= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE
;
276 // ToDo: Implement EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE
277 if ((ControlRegister
& PL011_UARTCR_LBE
) == PL011_UARTCR_LBE
) {
278 *Control
|= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE
;
281 // ToDo: Implement EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE
282 if (SoftwareLoopbackEnable
) {
283 *Control
|= EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE
;
287 return RETURN_SUCCESS
;
291 Write data to serial device.
293 @param Buffer Point of data buffer which need to be written.
294 @param NumberOfBytes Number of output bytes which are cached in Buffer.
296 @retval 0 Write data failed.
297 @retval !0 Actual number of bytes written to serial device.
305 IN UINTN NumberOfBytes
310 for (Count
= 0; Count
< NumberOfBytes
; Count
++, Buffer
++) {
311 while ((MmioRead32 (UartBase
+ UARTFR
) & UART_TX_EMPTY_FLAG_MASK
) == 0);
312 MmioWrite8 (UartBase
+ UARTDR
, *Buffer
);
315 return NumberOfBytes
;
319 Read data from serial device and save the data in buffer.
321 @param Buffer Point of data buffer which need to be written.
322 @param NumberOfBytes Number of output bytes which are cached in Buffer.
324 @retval 0 Read data failed.
325 @retval !0 Actual number of bytes read from serial device.
333 IN UINTN NumberOfBytes
338 for (Count
= 0; Count
< NumberOfBytes
; Count
++, Buffer
++) {
339 while ((MmioRead32 (UartBase
+ UARTFR
) & UART_RX_EMPTY_FLAG_MASK
) != 0);
340 *Buffer
= MmioRead8 (UartBase
+ UARTDR
);
343 return NumberOfBytes
;
347 Check to see if any data is available to be read from the debug device.
349 @retval EFI_SUCCESS At least one byte of data is available to be read
350 @retval EFI_NOT_READY No data is available to be read
351 @retval EFI_DEVICE_ERROR The serial device is not functioning properly
360 return ((MmioRead32 (UartBase
+ UARTFR
) & UART_RX_EMPTY_FLAG_MASK
) == 0);