2 16550 UART Serial Port library functions
4 Copyright (c) 2006 - 2010, 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_DCD BIT7
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 Initialize the serial device hardware.
94 If no initialization is required, then return RETURN_SUCCESS.
95 If the serial device was successfuly initialized, then return RETURN_SUCCESS.
96 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
98 @retval RETURN_SUCCESS The serial device was initialized.
99 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
104 SerialPortInitialize (
108 RETURN_STATUS Status
;
113 // Perform platform specific initialization required to enable use of the 16550 device
114 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.
116 Status
= PlatformHookSerialPortInitialize ();
117 if (RETURN_ERROR (Status
)) {
122 // See if the serial port is already initialized
125 if ((SerialPortReadRegister (R_UART_FCR
) & (B_UART_FCR_FIFOE
| B_UART_FCR_FIFO64
)) !=
126 (PcdGet8 (PcdSerialFifoControl
) & (B_UART_FCR_FIFOE
| B_UART_FCR_FIFO64
)) ) {
129 if ((SerialPortReadRegister (R_UART_LCR
) & 0x3F) != (PcdGet8 (PcdSerialLineControl
) & 0x3F)) {
132 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(SerialPortReadRegister (R_UART_LCR
) | B_UART_LCR_DLAB
));
133 Divisor
= SerialPortReadRegister (R_UART_BAUD_HIGH
) << 8;
134 Divisor
|= SerialPortReadRegister (R_UART_BAUD_LOW
);
135 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(SerialPortReadRegister (R_UART_LCR
) & ~B_UART_LCR_DLAB
));
136 if (Divisor
!= 115200 / PcdGet32 (PcdSerialBaudRate
)) {
140 return RETURN_SUCCESS
;
144 // Configure baud rate
146 Divisor
= 115200 / PcdGet32 (PcdSerialBaudRate
);
147 SerialPortWriteRegister (R_UART_LCR
, B_UART_LCR_DLAB
);
148 SerialPortWriteRegister (R_UART_BAUD_HIGH
, (UINT8
) (Divisor
>> 8));
149 SerialPortWriteRegister (R_UART_BAUD_LOW
, (UINT8
) (Divisor
& 0xff));
152 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
153 // Strip reserved bits from PcdSerialLineControl
155 SerialPortWriteRegister (R_UART_LCR
, (UINT8
)(PcdGet8 (PcdSerialLineControl
) & 0x3F));
158 // Enable and reset FIFOs
159 // Strip reserved bits from PcdSerialFifoControl
161 SerialPortWriteRegister (R_UART_FCR
, (UINT8
)(PcdGet8 (PcdSerialFifoControl
) & 0x17));
164 // Put Modem Control Register(MCR) into its reset state of 0x00.
166 SerialPortWriteRegister (R_UART_MCR
, 0x00);
168 return RETURN_SUCCESS
;
172 Write data from buffer to serial device.
174 Writes NumberOfBytes data bytes from Buffer to the serial device.
175 The number of bytes actually written to the serial device is returned.
176 If the return value is less than NumberOfBytes, then the write operation failed.
178 If Buffer is NULL, then ASSERT().
180 If NumberOfBytes is zero, then return 0.
182 @param Buffer Pointer to the data buffer to be written.
183 @param NumberOfBytes Number of bytes to written to the serial device.
185 @retval 0 NumberOfBytes is 0.
186 @retval >0 The number of bytes written to the serial device.
187 If this value is less than NumberOfBytes, then the read operation failed.
194 IN UINTN NumberOfBytes
201 if (Buffer
== NULL
) {
206 // Compute the maximum size of the Tx FIFO
209 if ((PcdGet8 (PcdSerialFifoControl
) & B_UART_FCR_FIFOE
) != 0) {
210 if ((PcdGet8 (PcdSerialFifoControl
) & B_UART_FCR_FIFO64
) == 0) {
217 Result
= NumberOfBytes
;
218 while (NumberOfBytes
!= 0) {
220 // Wait for the serial port to be ready, to make sure both the transmit FIFO
221 // and shift register empty.
223 while ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_TEMT
) == 0);
226 // Fill then entire Tx FIFO
228 for (Index
= 0; Index
< FifoSize
&& NumberOfBytes
!= 0; Index
++, NumberOfBytes
--, Buffer
++) {
229 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
231 // Wait for notification from peer to send data
233 while ((SerialPortReadRegister (R_UART_MSR
) & (B_UART_MSR_CTS
| B_UART_MSR_DCD
)) == B_UART_MSR_DCD
);
237 // Write byte to the transmit buffer.
239 SerialPortWriteRegister (R_UART_TXBUF
, *Buffer
);
246 Reads data from a serial device into a buffer.
248 @param Buffer Pointer to the data buffer to store the data read from the serial device.
249 @param NumberOfBytes Number of bytes to read from the serial device.
251 @retval 0 NumberOfBytes is 0.
252 @retval >0 The number of bytes read from the serial device.
253 If this value is less than NumberOfBytes, then the read operation failed.
260 IN UINTN NumberOfBytes
266 if (NULL
== Buffer
) {
270 Mcr
= (UINT8
)(SerialPortReadRegister (R_UART_MCR
) & ~B_UART_MCR_RTS
);
272 for (Result
= 0; NumberOfBytes
-- != 0; Result
++, Buffer
++) {
274 // Wait for the serial port to have some data.
276 while ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_RXRDY
) == 0) {
277 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
279 // Set RTS to let the peer send some data
281 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(Mcr
| B_UART_MCR_RTS
));
284 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
286 // Clear RTS to prevent peer from sending data
288 SerialPortWriteRegister (R_UART_MCR
, Mcr
);
292 // Read byte from the receive buffer.
294 *Buffer
= SerialPortReadRegister (R_UART_RXBUF
);
301 Polls a serial device to see if there is any data waiting to be read.
303 Polls aserial device to see if there is any data waiting to be read.
304 If there is data waiting to be read from the serial device, then TRUE is returned.
305 If there is no data waiting to be read from the serial device, then FALSE is returned.
307 @retval TRUE Data is waiting to be read from the serial device.
308 @retval FALSE There is no data waiting to be read from the serial device.
318 // Read the serial port status
320 if ((SerialPortReadRegister (R_UART_LSR
) & B_UART_LSR_RXRDY
) != 0) {
321 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
323 // Clear RTS to prevent peer from sending data
325 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(SerialPortReadRegister (R_UART_MCR
) & ~B_UART_MCR_RTS
));
330 if (PcdGetBool (PcdSerialUseHardwareFlowControl
)) {
332 // Set RTS to let the peer send some data
334 SerialPortWriteRegister (R_UART_MCR
, (UINT8
)(SerialPortReadRegister (R_UART_MCR
) | B_UART_MCR_RTS
));