2 16550 UART Serial Port library functions
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/SerialPortLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/PlatformHookLib.h>
22 // 16550 UART register offsets and bitfields
24 #define R_UART_RXBUF 0
25 #define R_UART_TXBUF 0
26 #define R_UART_BAUD_LOW 0
27 #define R_UART_BAUD_HIGH 1
29 #define B_UART_FCR_FIFOE BIT0
30 #define B_UART_FCR_FIFO64 BIT5
32 #define B_UART_LCR_DLAB BIT7
34 #define B_UART_MCR_RTS BIT1
36 #define B_UART_LSR_RXRDY BIT0
37 #define B_UART_LSR_TXRDY BIT5
38 #define B_UART_LSR_TEMT BIT6
40 #define B_UART_MSR_CTS BIT4
41 #define B_UART_MSR_DSR BIT5
44 Read an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is read from
45 MMIO space. If PcdSerialUseMmio is FALSE, then the value is read from I/O space. The
46 parameter Offset is added to the base address of the 16550 registers that is specified
47 by PcdSerialRegisterBase.
49 @param Offset The offset of the 16550 register to read.
51 @return The value read from the 16550 register.
55 SerialPortReadRegister (
59 if (PcdGetBool (PcdSerialUseMmio
)) {
60 return MmioRead8 ((UINTN
)PcdGet64 (PcdSerialRegisterBase
) + Offset
);
62 return IoRead8 ((UINT16
)PcdGet64 (PcdSerialRegisterBase
) + Offset
);
67 Write an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is written to
68 MMIO space. If PcdSerialUseMmio is FALSE, then the value is written to I/O space. The
69 parameter Offset is added to the base address of the 16550 registers that is specified
70 by PcdSerialRegisterBase.
72 @param Offset The offset of the 16550 register to write.
73 @param Value The value to write to the 16550 register specified by Offset.
75 @return The value written to the 16550 register.
79 SerialPortWriteRegister (
84 if (PcdGetBool (PcdSerialUseMmio
)) {
85 return MmioWrite8 ((UINTN
)PcdGet64 (PcdSerialRegisterBase
) + Offset
, Value
);
87 return IoWrite8 ((UINT16
)PcdGet64 (PcdSerialRegisterBase
) + Offset
, Value
);
92 Return whether the hardware flow control signal allows writing.
94 @retval TRUE The serial port is writable.
95 @retval FALSE The serial port is not writable.
102 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
103 if (PcdGetBool (PcdSerialDetectCable
)) {
105 // Wait for both DSR and CTS to be set
106 // DSR is set if a cable is connected.
107 // CTS is set if it is ok to transmit data
109 // DSR CTS Description Action
110 // === === ======================================== ========
111 // 0 0 No cable connected. Wait
112 // 0 1 No cable connected. Wait
113 // 1 0 Cable connected, but not clear to send. Wait
114 // 1 1 Cable connected, and clear to send. Transmit
116 return (BOOLEAN
) ((SerialPortReadRegister (R_UART_MSR
) & (B_UART_MSR_DSR
| B_UART_MSR_CTS
)) == (B_UART_MSR_DSR
| B_UART_MSR_CTS
));
119 // Wait for both DSR and CTS to be set OR for DSR to be clear.
120 // DSR is set if a cable is connected.
121 // CTS is set if it is ok to transmit data
123 // DSR CTS Description Action
124 // === === ======================================== ========
125 // 0 0 No cable connected. Transmit
126 // 0 1 No cable connected. Transmit
127 // 1 0 Cable connected, but not clear to send. Wait
128 // 1 1 Cable connected, and clar to send. Transmit
130 return (BOOLEAN
) ((SerialPortReadRegister (R_UART_MSR
) & (B_UART_MSR_DSR
| B_UART_MSR_CTS
)) != (B_UART_MSR_DSR
));
138 Initialize the serial device hardware.
140 If no initialization is required, then return RETURN_SUCCESS.
141 If the serial device was successfully initialized, then return RETURN_SUCCESS.
142 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
144 @retval RETURN_SUCCESS The serial device was initialized.
145 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
150 SerialPortInitialize (
154 RETURN_STATUS Status
;
159 // Perform platform specific initialization required to enable use of the 16550 device
160 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.
162 Status
= PlatformHookSerialPortInitialize ();
163 if (RETURN_ERROR (Status
)) {
168 // See if the serial port is already initialized
171 if ((SerialPortReadRegister (R_UART_FCR
) & (B_UART_FCR_FIFOE
| B_UART_FCR_FIFO64
)) !=
172 (PcdGet8 (PcdSerialFifoControl
) & (B_UART_FCR_FIFOE
| B_UART_FCR_FIFO64
)) ) {
175 if ((SerialPortReadRegister (R_UART_LCR
) & 0x3F) != (PcdGet8 (PcdSerialLineControl
) & 0x3F)) {
178 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(SerialPortReadRegister (R_UART_LCR
) | B_UART_LCR_DLAB
));
179 Divisor
= SerialPortReadRegister (R_UART_BAUD_HIGH
) << 8;
180 Divisor
|= (UINTN
)SerialPortReadRegister (R_UART_BAUD_LOW
);
181 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(SerialPortReadRegister (R_UART_LCR
) & ~B_UART_LCR_DLAB
));
182 if (Divisor
!= 115200 / PcdGet32 (PcdSerialBaudRate
)) {
186 return RETURN_SUCCESS
;
190 // Configure baud rate
192 Divisor
= 115200 / PcdGet32 (PcdSerialBaudRate
);
193 SerialPortWriteRegister (R_UART_LCR
, B_UART_LCR_DLAB
);
194 SerialPortWriteRegister (R_UART_BAUD_HIGH
, (UINT8
) (Divisor
>> 8));
195 SerialPortWriteRegister (R_UART_BAUD_LOW
, (UINT8
) (Divisor
& 0xff));
198 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
199 // Strip reserved bits from PcdSerialLineControl
201 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(PcdGet8 (PcdSerialLineControl
) & 0x3F));
204 // Enable and reset FIFOs
205 // Strip reserved bits from PcdSerialFifoControl
207 SerialPortWriteRegister (R_UART_FCR
, (UINT8
)(PcdGet8 (PcdSerialFifoControl
) & 0x27));
210 // Put Modem Control Register(MCR) into its reset state of 0x00.
212 SerialPortWriteRegister (R_UART_MCR
, 0x00);
214 return RETURN_SUCCESS
;
218 Write data from buffer to serial device.
220 Writes NumberOfBytes data bytes from Buffer to the serial device.
221 The number of bytes actually written to the serial device is returned.
222 If the return value is less than NumberOfBytes, then the write operation failed.
224 If Buffer is NULL, then ASSERT().
226 If NumberOfBytes is zero, then return 0.
228 @param Buffer Pointer to the data buffer to be written.
229 @param NumberOfBytes Number of bytes to written to the serial device.
231 @retval 0 NumberOfBytes is 0.
232 @retval >0 The number of bytes written to the serial device.
233 If this value is less than NumberOfBytes, then the read operation failed.
240 IN UINTN NumberOfBytes
247 if (Buffer
== NULL
) {
251 if (NumberOfBytes
== 0) {
253 // Flush the hardware
257 // Wait for both the transmit FIFO and shift register empty.
259 while ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_TEMT
) == 0);
262 // Wait for the hardware flow control signal
264 while (!SerialPortWritable ());
269 // Compute the maximum size of the Tx FIFO
272 if ((PcdGet8 (PcdSerialFifoControl
) & B_UART_FCR_FIFOE
) != 0) {
273 if ((PcdGet8 (PcdSerialFifoControl
) & B_UART_FCR_FIFO64
) == 0) {
280 Result
= NumberOfBytes
;
281 while (NumberOfBytes
!= 0) {
283 // Wait for the serial port to be ready, to make sure both the transmit FIFO
284 // and shift register empty.
286 while ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_TEMT
) == 0);
289 // Fill then entire Tx FIFO
291 for (Index
= 0; Index
< FifoSize
&& NumberOfBytes
!= 0; Index
++, NumberOfBytes
--, Buffer
++) {
293 // Wait for the hardware flow control signal
295 while (!SerialPortWritable ());
298 // Write byte to the transmit buffer.
300 SerialPortWriteRegister (R_UART_TXBUF
, *Buffer
);
307 Reads data from a serial device into a buffer.
309 @param Buffer Pointer to the data buffer to store the data read from the serial device.
310 @param NumberOfBytes Number of bytes to read from the serial device.
312 @retval 0 NumberOfBytes is 0.
313 @retval >0 The number of bytes read from the serial device.
314 If this value is less than NumberOfBytes, then the read operation failed.
321 IN UINTN NumberOfBytes
327 if (NULL
== Buffer
) {
331 Mcr
= (UINT8
)(SerialPortReadRegister (R_UART_MCR
) & ~B_UART_MCR_RTS
);
333 for (Result
= 0; NumberOfBytes
-- != 0; Result
++, Buffer
++) {
335 // Wait for the serial port to have some data.
337 while ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_RXRDY
) == 0) {
338 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
340 // Set RTS to let the peer send some data
342 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(Mcr
| B_UART_MCR_RTS
));
345 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
347 // Clear RTS to prevent peer from sending data
349 SerialPortWriteRegister (R_UART_MCR
, Mcr
);
353 // Read byte from the receive buffer.
355 *Buffer
= SerialPortReadRegister (R_UART_RXBUF
);
362 Polls a serial device to see if there is any data waiting to be read.
364 Polls aserial device to see if there is any data waiting to be read.
365 If there is data waiting to be read from the serial device, then TRUE is returned.
366 If there is no data waiting to be read from the serial device, then FALSE is returned.
368 @retval TRUE Data is waiting to be read from the serial device.
369 @retval FALSE There is no data waiting to be read from the serial device.
379 // Read the serial port status
381 if ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_RXRDY
) != 0) {
382 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
384 // Clear RTS to prevent peer from sending data
386 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(SerialPortReadRegister (R_UART_MCR
) & ~B_UART_MCR_RTS
));
391 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
393 // Set RTS to let the peer send some data
395 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(SerialPortReadRegister (R_UART_MCR
) | B_UART_MCR_RTS
));