]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
2fc6031ab74d1eb0e38e08c03343fa7c4fa8dfe8
[mirror_edk2.git] / MdeModulePkg / Library / BaseSerialPortLib16550 / BaseSerialPortLib16550.c
1 /** @file
2 16550 UART Serial Port library functions
3
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
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_DSR BIT5
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 write.
73 @param Value The value to write to the 16550 register specified by Offset.
74
75 @return The value written to the 16550 register.
76
77 **/
78 UINT8
79 SerialPortWriteRegister (
80 UINTN Offset,
81 UINT8 Value
82 )
83 {
84 if (PcdGetBool (PcdSerialUseMmio)) {
85 return MmioWrite8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);
86 } else {
87 return IoWrite8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);
88 }
89 }
90
91 /**
92 Return whether the hardware flow control signal allows writing.
93
94 @retval TRUE The serial port is writable.
95 @retval FALSE The serial port is not writable.
96 **/
97 BOOLEAN
98 SerialPortWritable (
99 VOID
100 )
101 {
102 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
103 if (PcdGetBool (PcdSerialDetectCable)) {
104 //
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
108 //
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
115 //
116 return (BOOLEAN) ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) == (B_UART_MSR_DSR | B_UART_MSR_CTS));
117 } else {
118 //
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
122 //
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
129 //
130 return (BOOLEAN) ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) != (B_UART_MSR_DSR));
131 }
132 }
133
134 return TRUE;
135 }
136
137 /**
138 Initialize the serial device hardware.
139
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.
143
144 @retval RETURN_SUCCESS The serial device was initialized.
145 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
146
147 **/
148 RETURN_STATUS
149 EFIAPI
150 SerialPortInitialize (
151 VOID
152 )
153 {
154 RETURN_STATUS Status;
155 UINTN Divisor;
156 BOOLEAN Initialized;
157
158 //
159 // Perform platform specific initialization required to enable use of the 16550 device
160 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.
161 //
162 Status = PlatformHookSerialPortInitialize ();
163 if (RETURN_ERROR (Status)) {
164 return Status;
165 }
166
167 //
168 // See if the serial port is already initialized
169 //
170 Initialized = TRUE;
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)) ) {
173 Initialized = FALSE;
174 }
175 if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {
176 Initialized = FALSE;
177 }
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)) {
183 Initialized = FALSE;
184 }
185 if (Initialized) {
186 return RETURN_SUCCESS;
187 }
188
189 //
190 // Configure baud rate
191 //
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));
196
197 //
198 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
199 // Strip reserved bits from PcdSerialLineControl
200 //
201 SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F));
202
203 //
204 // Enable and reset FIFOs
205 // Strip reserved bits from PcdSerialFifoControl
206 //
207 SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & 0x27));
208
209 //
210 // Put Modem Control Register(MCR) into its reset state of 0x00.
211 //
212 SerialPortWriteRegister (R_UART_MCR, 0x00);
213
214 return RETURN_SUCCESS;
215 }
216
217 /**
218 Write data from buffer to serial device.
219
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.
223
224 If Buffer is NULL, then ASSERT().
225
226 If NumberOfBytes is zero, then return 0.
227
228 @param Buffer Pointer to the data buffer to be written.
229 @param NumberOfBytes Number of bytes to written to the serial device.
230
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.
234
235 **/
236 UINTN
237 EFIAPI
238 SerialPortWrite (
239 IN UINT8 *Buffer,
240 IN UINTN NumberOfBytes
241 )
242 {
243 UINTN Result;
244 UINTN Index;
245 UINTN FifoSize;
246
247 if (Buffer == NULL) {
248 return 0;
249 }
250
251 if (NumberOfBytes == 0) {
252 //
253 // Flush the hardware
254 //
255
256 //
257 // Wait for both the transmit FIFO and shift register empty.
258 //
259 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);
260
261 //
262 // Wait for the hardware flow control signal
263 //
264 while (!SerialPortWritable ());
265 return 0;
266 }
267
268 //
269 // Compute the maximum size of the Tx FIFO
270 //
271 FifoSize = 1;
272 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
273 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
274 FifoSize = 16;
275 } else {
276 FifoSize = 64;
277 }
278 }
279
280 Result = NumberOfBytes;
281 while (NumberOfBytes != 0) {
282 //
283 // Wait for the serial port to be ready, to make sure both the transmit FIFO
284 // and shift register empty.
285 //
286 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);
287
288 //
289 // Fill then entire Tx FIFO
290 //
291 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {
292 //
293 // Wait for the hardware flow control signal
294 //
295 while (!SerialPortWritable ());
296
297 //
298 // Write byte to the transmit buffer.
299 //
300 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);
301 }
302 }
303 return Result;
304 }
305
306 /**
307 Reads data from a serial device into a buffer.
308
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.
311
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.
315
316 **/
317 UINTN
318 EFIAPI
319 SerialPortRead (
320 OUT UINT8 *Buffer,
321 IN UINTN NumberOfBytes
322 )
323 {
324 UINTN Result;
325 UINT8 Mcr;
326
327 if (NULL == Buffer) {
328 return 0;
329 }
330
331 Mcr = (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS);
332
333 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {
334 //
335 // Wait for the serial port to have some data.
336 //
337 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0) {
338 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
339 //
340 // Set RTS to let the peer send some data
341 //
342 SerialPortWriteRegister (R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS));
343 }
344 }
345 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
346 //
347 // Clear RTS to prevent peer from sending data
348 //
349 SerialPortWriteRegister (R_UART_MCR, Mcr);
350 }
351
352 //
353 // Read byte from the receive buffer.
354 //
355 *Buffer = SerialPortReadRegister (R_UART_RXBUF);
356 }
357
358 return Result;
359 }
360
361 /**
362 Polls a serial device to see if there is any data waiting to be read.
363
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.
367
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.
370
371 **/
372 BOOLEAN
373 EFIAPI
374 SerialPortPoll (
375 VOID
376 )
377 {
378 //
379 // Read the serial port status
380 //
381 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {
382 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
383 //
384 // Clear RTS to prevent peer from sending data
385 //
386 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS));
387 }
388 return TRUE;
389 }
390
391 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
392 //
393 // Set RTS to let the peer send some data
394 //
395 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) | B_UART_MCR_RTS));
396 }
397
398 return FALSE;
399 }