]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
Add generic SerialPortLib instance for 16550 UARTs configured through PCDs. Depends...
[mirror_edk2.git] / MdeModulePkg / Library / BaseSerialPortLib16550 / BaseSerialPortLib16550.c
1 /** @file
2 16550 UART Serial Port library functions
3
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
9
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.
12
13 **/
14
15 #include <Base.h>
16 #include <Library/SerialPortLib.h>
17 #include <Library/PcdLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/PlatformHookLib.h>
20
21 //
22 // 16550 UART register offsets and bitfields
23 //
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
28 #define R_UART_FCR 2
29 #define B_UART_FCR_FIFOE BIT0
30 #define B_UART_FCR_FIFO64 BIT5
31 #define R_UART_LCR 3
32 #define B_UART_LCR_DLAB BIT7
33 #define R_UART_MCR 4
34 #define B_UART_MCR_RTS BIT1
35 #define R_UART_LSR 5
36 #define B_UART_LSR_RXRDY BIT0
37 #define B_UART_LSR_TXRDY BIT5
38 #define B_UART_LSR_TEMT BIT6
39 #define R_UART_MSR 6
40 #define B_UART_MSR_CTS BIT4
41 #define B_UART_MSR_DCD BIT7
42
43 /**
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.
48
49 @param Offset The offset of the 16550 register to read.
50
51 @return The value read from the 16550 register.
52
53 **/
54 UINT8
55 SerialPortReadRegister (
56 UINTN Offset
57 )
58 {
59 if (PcdGetBool (PcdSerialUseMmio)) {
60 return MmioRead8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset);
61 } else {
62 return IoRead8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset);
63 }
64 }
65
66 /**
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.
71
72 @param Offset The offset of the 16550 register to read.
73
74 @return The value written to the 16550 register.
75
76 **/
77 UINT8
78 SerialPortWriteRegister (
79 UINTN Offset,
80 UINT8 Value
81 )
82 {
83 if (PcdGetBool (PcdSerialUseMmio)) {
84 return MmioWrite8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);
85 } else {
86 return IoWrite8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);
87 }
88 }
89
90 /**
91 Initialize the serial device hardware.
92
93 If no initialization is required, then return RETURN_SUCCESS.
94 If the serial device was successfuly initialized, then return RETURN_SUCCESS.
95 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
96
97 @retval RETURN_SUCCESS The serial device was initialized.
98 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
99
100 **/
101 RETURN_STATUS
102 EFIAPI
103 SerialPortInitialize (
104 VOID
105 )
106 {
107 RETURN_STATUS Status;
108 UINTN Divisor;
109 BOOLEAN Initialized;
110
111 //
112 // Perform platform specific initialization required to enable use of the 16550 device
113 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.
114 //
115 Status = PlatformHookSerialPortInitialize ();
116 if (RETURN_ERROR (Status)) {
117 return Status;
118 }
119
120 //
121 // See if the serial port is already initialized
122 //
123 Initialized = TRUE;
124 if ((SerialPortReadRegister (R_UART_FCR) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)) !=
125 (PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)) ) {
126 Initialized = FALSE;
127 }
128 if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {
129 Initialized = FALSE;
130 }
131 SerialPortWriteRegister (R_UART_LCR, SerialPortReadRegister (R_UART_LCR) | B_UART_LCR_DLAB);
132 Divisor = (SerialPortReadRegister (R_UART_BAUD_HIGH) << 8) | SerialPortReadRegister (R_UART_BAUD_LOW);
133 SerialPortWriteRegister (R_UART_LCR, SerialPortReadRegister (R_UART_LCR) & ~B_UART_LCR_DLAB);
134 if (Divisor != 115200 / PcdGet32 (PcdSerialBaudRate)) {
135 Initialized = FALSE;
136 }
137 if (Initialized) {
138 return RETURN_SUCCESS;
139 }
140
141 //
142 // Configure baud rate
143 //
144 Divisor = 115200 / PcdGet32 (PcdSerialBaudRate);
145 SerialPortWriteRegister (R_UART_LCR, B_UART_LCR_DLAB);
146 SerialPortWriteRegister (R_UART_BAUD_HIGH, (UINT8) (Divisor >> 8));
147 SerialPortWriteRegister (R_UART_BAUD_LOW, (UINT8) (Divisor & 0xff));
148
149 //
150 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
151 // Strip reserved bits from PcdSerialLineControl
152 //
153 SerialPortWriteRegister (R_UART_LCR, PcdGet8 (PcdSerialLineControl) & 0x3F);
154
155 //
156 // Enable and reset FIFOs
157 // Strip reserved bits from PcdSerialFifoControl
158 //
159 SerialPortWriteRegister (R_UART_FCR, PcdGet8 (PcdSerialFifoControl) & 0x17);
160
161 //
162 // Put Modem Control Register(MCR) into its reset state of 0x00.
163 //
164 SerialPortWriteRegister (R_UART_MCR, 0x00);
165
166 return RETURN_SUCCESS;
167 }
168
169 /**
170 Write data from buffer to serial device.
171
172 Writes NumberOfBytes data bytes from Buffer to the serial device.
173 The number of bytes actually written to the serial device is returned.
174 If the return value is less than NumberOfBytes, then the write operation failed.
175
176 If Buffer is NULL, then ASSERT().
177
178 If NumberOfBytes is zero, then return 0.
179
180 @param Buffer Pointer to the data buffer to be written.
181 @param NumberOfBytes Number of bytes to written to the serial device.
182
183 @retval 0 NumberOfBytes is 0.
184 @retval >0 The number of bytes written to the serial device.
185 If this value is less than NumberOfBytes, then the read operation failed.
186
187 **/
188 UINTN
189 EFIAPI
190 SerialPortWrite (
191 IN UINT8 *Buffer,
192 IN UINTN NumberOfBytes
193 )
194 {
195 UINTN Result;
196 UINTN Index;
197 UINTN FifoSize;
198
199 if (Buffer == NULL) {
200 return 0;
201 }
202
203 //
204 // Compute the maximum size of the Tx FIFO
205 //
206 FifoSize = 1;
207 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
208 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
209 FifoSize = 16;
210 } else {
211 FifoSize = 64;
212 }
213 }
214
215 Result = NumberOfBytes;
216 while (NumberOfBytes != 0) {
217 //
218 // Wait for the serial port to be ready, to make sure both the transmit FIFO
219 // and shift register empty.
220 //
221 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);
222
223 //
224 // Fill then entire Tx FIFO
225 //
226 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {
227 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
228 //
229 // Wait for notification from peer to send data
230 //
231 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_CTS | B_UART_MSR_DCD)) == B_UART_MSR_DCD);
232 }
233
234 //
235 // Write byte to the transmit buffer.
236 //
237 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);
238 }
239 }
240 return Result;
241 }
242
243 /**
244 Reads data from a serial device into a buffer.
245
246 @param Buffer Pointer to the data buffer to store the data read from the serial device.
247 @param NumberOfBytes Number of bytes to read from the serial device.
248
249 @retval 0 NumberOfBytes is 0.
250 @retval >0 The number of bytes read from the serial device.
251 If this value is less than NumberOfBytes, then the read operation failed.
252
253 **/
254 UINTN
255 EFIAPI
256 SerialPortRead (
257 OUT UINT8 *Buffer,
258 IN UINTN NumberOfBytes
259 )
260 {
261 UINTN Result;
262 UINT8 Mcr;
263
264 if (NULL == Buffer) {
265 return 0;
266 }
267
268 Mcr = SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS;
269
270 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {
271 //
272 // Wait for the serial port to have some data.
273 //
274 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0) {
275 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
276 //
277 // Set RTS to let the peer send some data
278 //
279 SerialPortWriteRegister (R_UART_MCR, Mcr | B_UART_MCR_RTS);
280 }
281 }
282 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
283 //
284 // Clear RTS to prevent peer from sending data
285 //
286 SerialPortWriteRegister (R_UART_MCR, Mcr);
287 }
288
289 //
290 // Read byte from the receive buffer.
291 //
292 *Buffer = SerialPortReadRegister (R_UART_RXBUF);
293 }
294
295 return Result;
296 }
297
298 /**
299 Polls a serial device to see if there is any data waiting to be read.
300
301 Polls aserial device to see if there is any data waiting to be read.
302 If there is data waiting to be read from the serial device, then TRUE is returned.
303 If there is no data waiting to be read from the serial device, then FALSE is returned.
304
305 @retval TRUE Data is waiting to be read from the serial device.
306 @retval FALSE There is no data waiting to be read from the serial device.
307
308 **/
309 BOOLEAN
310 EFIAPI
311 SerialPortPoll (
312 VOID
313 )
314 {
315 //
316 // Read the serial port status
317 //
318 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {
319 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
320 //
321 // Clear RTS to prevent peer from sending data
322 //
323 SerialPortWriteRegister (R_UART_MCR, SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS);
324 }
325 return TRUE;
326 }
327
328 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
329 //
330 // Set RTS to let the peer send some data
331 //
332 SerialPortWriteRegister (R_UART_MCR, SerialPortReadRegister (R_UART_MCR) | B_UART_MCR_RTS);
333 }
334
335 return FALSE;
336 }