]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
04d8ca194934ead73b66e684dd539f97237ab0c9
[mirror_edk2.git] / MdeModulePkg / Library / BaseSerialPortLib16550 / BaseSerialPortLib16550.c
1 /** @file
2 16550 UART Serial Port library functions
3
4 Copyright (c) 2006 - 2011, 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
42 /**
43 Read an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is read from
44 MMIO space. If PcdSerialUseMmio is FALSE, then the value is read from I/O space. The
45 parameter Offset is added to the base address of the 16550 registers that is specified
46 by PcdSerialRegisterBase.
47
48 @param Offset The offset of the 16550 register to read.
49
50 @return The value read from the 16550 register.
51
52 **/
53 UINT8
54 SerialPortReadRegister (
55 UINTN Offset
56 )
57 {
58 if (PcdGetBool (PcdSerialUseMmio)) {
59 return MmioRead8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset);
60 } else {
61 return IoRead8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset);
62 }
63 }
64
65 /**
66 Write an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is written to
67 MMIO space. If PcdSerialUseMmio is FALSE, then the value is written to I/O space. The
68 parameter Offset is added to the base address of the 16550 registers that is specified
69 by PcdSerialRegisterBase.
70
71 @param Offset The offset of the 16550 register to write.
72 @param Value The value to write to the 16550 register specified by Offset.
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, (UINT8)(SerialPortReadRegister (R_UART_LCR) | B_UART_LCR_DLAB));
132 Divisor = SerialPortReadRegister (R_UART_BAUD_HIGH) << 8;
133 Divisor |= SerialPortReadRegister (R_UART_BAUD_LOW);
134 SerialPortWriteRegister (R_UART_LCR, (UINT8)(SerialPortReadRegister (R_UART_LCR) & ~B_UART_LCR_DLAB));
135 if (Divisor != 115200 / PcdGet32 (PcdSerialBaudRate)) {
136 Initialized = FALSE;
137 }
138 if (Initialized) {
139 return RETURN_SUCCESS;
140 }
141
142 //
143 // Configure baud rate
144 //
145 Divisor = 115200 / PcdGet32 (PcdSerialBaudRate);
146 SerialPortWriteRegister (R_UART_LCR, B_UART_LCR_DLAB);
147 SerialPortWriteRegister (R_UART_BAUD_HIGH, (UINT8) (Divisor >> 8));
148 SerialPortWriteRegister (R_UART_BAUD_LOW, (UINT8) (Divisor & 0xff));
149
150 //
151 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
152 // Strip reserved bits from PcdSerialLineControl
153 //
154 SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F));
155
156 //
157 // Enable and reset FIFOs
158 // Strip reserved bits from PcdSerialFifoControl
159 //
160 SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & 0x17));
161
162 //
163 // Put Modem Control Register(MCR) into its reset state of 0x00.
164 //
165 SerialPortWriteRegister (R_UART_MCR, 0x00);
166
167 return RETURN_SUCCESS;
168 }
169
170 /**
171 Write data from buffer to serial device.
172
173 Writes NumberOfBytes data bytes from Buffer to the serial device.
174 The number of bytes actually written to the serial device is returned.
175 If the return value is less than NumberOfBytes, then the write operation failed.
176
177 If Buffer is NULL, then ASSERT().
178
179 If NumberOfBytes is zero, then return 0.
180
181 @param Buffer Pointer to the data buffer to be written.
182 @param NumberOfBytes Number of bytes to written to the serial device.
183
184 @retval 0 NumberOfBytes is 0.
185 @retval >0 The number of bytes written to the serial device.
186 If this value is less than NumberOfBytes, then the read operation failed.
187
188 **/
189 UINTN
190 EFIAPI
191 SerialPortWrite (
192 IN UINT8 *Buffer,
193 IN UINTN NumberOfBytes
194 )
195 {
196 UINTN Result;
197 UINTN Index;
198 UINTN FifoSize;
199
200 if (Buffer == NULL) {
201 return 0;
202 }
203
204 //
205 // Compute the maximum size of the Tx FIFO
206 //
207 FifoSize = 1;
208 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
209 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
210 FifoSize = 16;
211 } else {
212 FifoSize = 64;
213 }
214 }
215
216 Result = NumberOfBytes;
217 while (NumberOfBytes != 0) {
218 //
219 // Wait for the serial port to be ready, to make sure both the transmit FIFO
220 // and shift register empty.
221 //
222 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);
223
224 //
225 // Fill then entire Tx FIFO
226 //
227 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {
228 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
229 //
230 // Wait for notification from peer to send data
231 //
232 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_CTS)) == 0);
233 }
234
235 //
236 // Write byte to the transmit buffer.
237 //
238 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);
239 }
240 }
241 return Result;
242 }
243
244 /**
245 Reads data from a serial device into a buffer.
246
247 @param Buffer Pointer to the data buffer to store the data read from the serial device.
248 @param NumberOfBytes Number of bytes to read from the serial device.
249
250 @retval 0 NumberOfBytes is 0.
251 @retval >0 The number of bytes read from the serial device.
252 If this value is less than NumberOfBytes, then the read operation failed.
253
254 **/
255 UINTN
256 EFIAPI
257 SerialPortRead (
258 OUT UINT8 *Buffer,
259 IN UINTN NumberOfBytes
260 )
261 {
262 UINTN Result;
263 UINT8 Mcr;
264
265 if (NULL == Buffer) {
266 return 0;
267 }
268
269 Mcr = (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS);
270
271 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {
272 //
273 // Wait for the serial port to have some data.
274 //
275 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0) {
276 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
277 //
278 // Set RTS to let the peer send some data
279 //
280 SerialPortWriteRegister (R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS));
281 }
282 }
283 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
284 //
285 // Clear RTS to prevent peer from sending data
286 //
287 SerialPortWriteRegister (R_UART_MCR, Mcr);
288 }
289
290 //
291 // Read byte from the receive buffer.
292 //
293 *Buffer = SerialPortReadRegister (R_UART_RXBUF);
294 }
295
296 return Result;
297 }
298
299 /**
300 Polls a serial device to see if there is any data waiting to be read.
301
302 Polls aserial device to see if there is any data waiting to be read.
303 If there is data waiting to be read from the serial device, then TRUE is returned.
304 If there is no data waiting to be read from the serial device, then FALSE is returned.
305
306 @retval TRUE Data is waiting to be read from the serial device.
307 @retval FALSE There is no data waiting to be read from the serial device.
308
309 **/
310 BOOLEAN
311 EFIAPI
312 SerialPortPoll (
313 VOID
314 )
315 {
316 //
317 // Read the serial port status
318 //
319 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {
320 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
321 //
322 // Clear RTS to prevent peer from sending data
323 //
324 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS));
325 }
326 return TRUE;
327 }
328
329 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
330 //
331 // Set RTS to let the peer send some data
332 //
333 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) | B_UART_MCR_RTS));
334 }
335
336 return FALSE;
337 }