]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
1) Add PcdSerialDetectCable to MdeModulePkg to enable/disable cable detection if...
[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 #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 Initialize the serial device hardware.
93
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.
97
98 @retval RETURN_SUCCESS The serial device was initialized.
99 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
100
101 **/
102 RETURN_STATUS
103 EFIAPI
104 SerialPortInitialize (
105 VOID
106 )
107 {
108 RETURN_STATUS Status;
109 UINTN Divisor;
110 BOOLEAN Initialized;
111
112 //
113 // Perform platform specific initialization required to enable use of the 16550 device
114 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.
115 //
116 Status = PlatformHookSerialPortInitialize ();
117 if (RETURN_ERROR (Status)) {
118 return Status;
119 }
120
121 //
122 // See if the serial port is already initialized
123 //
124 Initialized = TRUE;
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)) ) {
127 Initialized = FALSE;
128 }
129 if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {
130 Initialized = FALSE;
131 }
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)) {
137 Initialized = FALSE;
138 }
139 if (Initialized) {
140 return RETURN_SUCCESS;
141 }
142
143 //
144 // Configure baud rate
145 //
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));
150
151 //
152 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.
153 // Strip reserved bits from PcdSerialLineControl
154 //
155 SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F));
156
157 //
158 // Enable and reset FIFOs
159 // Strip reserved bits from PcdSerialFifoControl
160 //
161 SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & 0x17));
162
163 //
164 // Put Modem Control Register(MCR) into its reset state of 0x00.
165 //
166 SerialPortWriteRegister (R_UART_MCR, 0x00);
167
168 return RETURN_SUCCESS;
169 }
170
171 /**
172 Write data from buffer to serial device.
173
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.
177
178 If Buffer is NULL, then ASSERT().
179
180 If NumberOfBytes is zero, then return 0.
181
182 @param Buffer Pointer to the data buffer to be written.
183 @param NumberOfBytes Number of bytes to written to the serial device.
184
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.
188
189 **/
190 UINTN
191 EFIAPI
192 SerialPortWrite (
193 IN UINT8 *Buffer,
194 IN UINTN NumberOfBytes
195 )
196 {
197 UINTN Result;
198 UINTN Index;
199 UINTN FifoSize;
200
201 if (Buffer == NULL) {
202 return 0;
203 }
204
205 //
206 // Compute the maximum size of the Tx FIFO
207 //
208 FifoSize = 1;
209 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
210 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
211 FifoSize = 16;
212 } else {
213 FifoSize = 64;
214 }
215 }
216
217 Result = NumberOfBytes;
218 while (NumberOfBytes != 0) {
219 //
220 // Wait for the serial port to be ready, to make sure both the transmit FIFO
221 // and shift register empty.
222 //
223 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);
224
225 //
226 // Fill then entire Tx FIFO
227 //
228 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {
229 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
230 if (PcdGetBool (PcdSerialDetectCable)) {
231 //
232 // Wait for both DSR and CTS to be set
233 // DSR is set if a cable is connected.
234 // CTS is set if it is ok to transmit data
235 //
236 // DSR CTS Description Action
237 // === === ======================================== ========
238 // 0 0 No cable connected. Wait
239 // 0 1 No cable connected. Wait
240 // 1 0 Cable connected, but not clear to send. Wait
241 // 1 1 Cable connected, and clear to send. Transmit
242 //
243 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) != (B_UART_MSR_DSR | B_UART_MSR_CTS));
244 } else {
245 //
246 // Wait for both DSR and CTS to be set OR for DSR to be clear.
247 // DSR is set if a cable is connected.
248 // CTS is set if it is ok to transmit data
249 //
250 // DSR CTS Description Action
251 // === === ======================================== ========
252 // 0 0 No cable connected. Transmit
253 // 0 1 No cable connected. Transmit
254 // 1 0 Cable connected, but not clear to send. Wait
255 // 1 1 Cable connected, and clar to send. Transmit
256 //
257 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) == (B_UART_MSR_DSR));
258 }
259 }
260
261 //
262 // Write byte to the transmit buffer.
263 //
264 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);
265 }
266 }
267 return Result;
268 }
269
270 /**
271 Reads data from a serial device into a buffer.
272
273 @param Buffer Pointer to the data buffer to store the data read from the serial device.
274 @param NumberOfBytes Number of bytes to read from the serial device.
275
276 @retval 0 NumberOfBytes is 0.
277 @retval >0 The number of bytes read from the serial device.
278 If this value is less than NumberOfBytes, then the read operation failed.
279
280 **/
281 UINTN
282 EFIAPI
283 SerialPortRead (
284 OUT UINT8 *Buffer,
285 IN UINTN NumberOfBytes
286 )
287 {
288 UINTN Result;
289 UINT8 Mcr;
290
291 if (NULL == Buffer) {
292 return 0;
293 }
294
295 Mcr = (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS);
296
297 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {
298 //
299 // Wait for the serial port to have some data.
300 //
301 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0) {
302 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
303 //
304 // Set RTS to let the peer send some data
305 //
306 SerialPortWriteRegister (R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS));
307 }
308 }
309 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
310 //
311 // Clear RTS to prevent peer from sending data
312 //
313 SerialPortWriteRegister (R_UART_MCR, Mcr);
314 }
315
316 //
317 // Read byte from the receive buffer.
318 //
319 *Buffer = SerialPortReadRegister (R_UART_RXBUF);
320 }
321
322 return Result;
323 }
324
325 /**
326 Polls a serial device to see if there is any data waiting to be read.
327
328 Polls aserial device to see if there is any data waiting to be read.
329 If there is data waiting to be read from the serial device, then TRUE is returned.
330 If there is no data waiting to be read from the serial device, then FALSE is returned.
331
332 @retval TRUE Data is waiting to be read from the serial device.
333 @retval FALSE There is no data waiting to be read from the serial device.
334
335 **/
336 BOOLEAN
337 EFIAPI
338 SerialPortPoll (
339 VOID
340 )
341 {
342 //
343 // Read the serial port status
344 //
345 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {
346 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
347 //
348 // Clear RTS to prevent peer from sending data
349 //
350 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS));
351 }
352 return TRUE;
353 }
354
355 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {
356 //
357 // Set RTS to let the peer send some data
358 //
359 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) | B_UART_MCR_RTS));
360 }
361
362 return FALSE;
363 }