]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
Cache the state whether the ROM image contains EFI Option ROM when loading the ROM...
[mirror_edk2.git] / MdeModulePkg / Library / BaseSerialPortLib16550 / BaseSerialPortLib16550.c
CommitLineData
467d15ae 1/** @file\r
2 16550 UART Serial Port library functions\r
3\r
db662a64 4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
467d15ae 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16#include <Library/SerialPortLib.h>\r
17#include <Library/PcdLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/PlatformHookLib.h>\r
20\r
21//\r
22// 16550 UART register offsets and bitfields\r
23//\r
24#define R_UART_RXBUF 0\r
25#define R_UART_TXBUF 0\r
26#define R_UART_BAUD_LOW 0\r
27#define R_UART_BAUD_HIGH 1\r
28#define R_UART_FCR 2\r
29#define B_UART_FCR_FIFOE BIT0\r
30#define B_UART_FCR_FIFO64 BIT5\r
31#define R_UART_LCR 3\r
32#define B_UART_LCR_DLAB BIT7\r
33#define R_UART_MCR 4\r
34#define B_UART_MCR_RTS BIT1\r
35#define R_UART_LSR 5\r
36#define B_UART_LSR_RXRDY BIT0\r
37#define B_UART_LSR_TXRDY BIT5\r
38#define B_UART_LSR_TEMT BIT6\r
39#define R_UART_MSR 6\r
40#define B_UART_MSR_CTS BIT4\r
784ce127 41#define B_UART_MSR_DSR BIT5\r
467d15ae 42\r
43/**\r
44 Read an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is read from \r
45 MMIO space. If PcdSerialUseMmio is FALSE, then the value is read from I/O space. The\r
46 parameter Offset is added to the base address of the 16550 registers that is specified \r
47 by PcdSerialRegisterBase. \r
48 \r
49 @param Offset The offset of the 16550 register to read.\r
50\r
51 @return The value read from the 16550 register.\r
52\r
53**/\r
54UINT8\r
55SerialPortReadRegister (\r
56 UINTN Offset\r
57 )\r
58{\r
59 if (PcdGetBool (PcdSerialUseMmio)) {\r
60 return MmioRead8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset);\r
61 } else {\r
62 return IoRead8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset);\r
63 }\r
64}\r
65\r
66/**\r
67 Write an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is written to\r
68 MMIO space. If PcdSerialUseMmio is FALSE, then the value is written to I/O space. The\r
69 parameter Offset is added to the base address of the 16550 registers that is specified \r
70 by PcdSerialRegisterBase. \r
71 \r
e25fb2c0 72 @param Offset The offset of the 16550 register to write.\r
73 @param Value The value to write to the 16550 register specified by Offset.\r
467d15ae 74\r
75 @return The value written to the 16550 register.\r
76\r
77**/\r
78UINT8\r
79SerialPortWriteRegister (\r
80 UINTN Offset,\r
81 UINT8 Value\r
82 )\r
83{\r
84 if (PcdGetBool (PcdSerialUseMmio)) {\r
85 return MmioWrite8 ((UINTN)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);\r
86 } else {\r
87 return IoWrite8 ((UINT16)PcdGet64 (PcdSerialRegisterBase) + Offset, Value);\r
88 }\r
89}\r
90\r
91/**\r
92 Initialize the serial device hardware.\r
93 \r
94 If no initialization is required, then return RETURN_SUCCESS.\r
95 If the serial device was successfuly initialized, then return RETURN_SUCCESS.\r
96 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
97 \r
98 @retval RETURN_SUCCESS The serial device was initialized.\r
99 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.\r
100\r
101**/\r
102RETURN_STATUS\r
103EFIAPI\r
104SerialPortInitialize (\r
105 VOID\r
106 )\r
107{\r
108 RETURN_STATUS Status;\r
109 UINTN Divisor;\r
110 BOOLEAN Initialized;\r
111\r
112 //\r
113 // Perform platform specific initialization required to enable use of the 16550 device\r
114 // at the location specified by PcdSerialUseMmio and PcdSerialRegisterBase.\r
115 //\r
116 Status = PlatformHookSerialPortInitialize ();\r
117 if (RETURN_ERROR (Status)) {\r
118 return Status;\r
119 }\r
120\r
121 //\r
122 // See if the serial port is already initialized\r
123 //\r
124 Initialized = TRUE;\r
125 if ((SerialPortReadRegister (R_UART_FCR) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)) !=\r
126 (PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)) ) {\r
127 Initialized = FALSE;\r
128 }\r
129 if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {\r
130 Initialized = FALSE;\r
131 }\r
f2ad9497 132 SerialPortWriteRegister (R_UART_LCR, (UINT8)(SerialPortReadRegister (R_UART_LCR) | B_UART_LCR_DLAB));\r
133 Divisor = SerialPortReadRegister (R_UART_BAUD_HIGH) << 8;\r
134 Divisor |= SerialPortReadRegister (R_UART_BAUD_LOW);\r
135 SerialPortWriteRegister (R_UART_LCR, (UINT8)(SerialPortReadRegister (R_UART_LCR) & ~B_UART_LCR_DLAB));\r
467d15ae 136 if (Divisor != 115200 / PcdGet32 (PcdSerialBaudRate)) {\r
137 Initialized = FALSE;\r
138 }\r
139 if (Initialized) {\r
140 return RETURN_SUCCESS;\r
141 }\r
142 \r
143 //\r
144 // Configure baud rate\r
145 //\r
146 Divisor = 115200 / PcdGet32 (PcdSerialBaudRate);\r
147 SerialPortWriteRegister (R_UART_LCR, B_UART_LCR_DLAB);\r
148 SerialPortWriteRegister (R_UART_BAUD_HIGH, (UINT8) (Divisor >> 8));\r
149 SerialPortWriteRegister (R_UART_BAUD_LOW, (UINT8) (Divisor & 0xff));\r
150\r
151 //\r
152 // Clear DLAB and configure Data Bits, Parity, and Stop Bits.\r
153 // Strip reserved bits from PcdSerialLineControl\r
154 //\r
f2ad9497 155 SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F));\r
467d15ae 156\r
157 //\r
158 // Enable and reset FIFOs\r
159 // Strip reserved bits from PcdSerialFifoControl\r
160 //\r
acaa2726 161 SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & 0x27));\r
467d15ae 162\r
163 //\r
164 // Put Modem Control Register(MCR) into its reset state of 0x00.\r
165 // \r
166 SerialPortWriteRegister (R_UART_MCR, 0x00);\r
167 \r
168 return RETURN_SUCCESS;\r
169}\r
170\r
171/**\r
172 Write data from buffer to serial device. \r
173 \r
174 Writes NumberOfBytes data bytes from Buffer to the serial device. \r
175 The number of bytes actually written to the serial device is returned.\r
176 If the return value is less than NumberOfBytes, then the write operation failed.\r
177\r
178 If Buffer is NULL, then ASSERT(). \r
179\r
180 If NumberOfBytes is zero, then return 0.\r
181\r
182 @param Buffer Pointer to the data buffer to be written.\r
183 @param NumberOfBytes Number of bytes to written to the serial device.\r
184\r
185 @retval 0 NumberOfBytes is 0.\r
186 @retval >0 The number of bytes written to the serial device. \r
187 If this value is less than NumberOfBytes, then the read operation failed.\r
188\r
189**/\r
190UINTN\r
191EFIAPI\r
192SerialPortWrite (\r
193 IN UINT8 *Buffer,\r
194 IN UINTN NumberOfBytes\r
195)\r
196{\r
db662a64 197 UINTN Result;\r
198 UINTN Index;\r
199 UINTN FifoSize;\r
467d15ae 200\r
201 if (Buffer == NULL) {\r
202 return 0;\r
203 }\r
204\r
205 //\r
206 // Compute the maximum size of the Tx FIFO\r
207 //\r
208 FifoSize = 1;\r
209 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {\r
210 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {\r
211 FifoSize = 16;\r
212 } else {\r
213 FifoSize = 64;\r
214 }\r
215 }\r
db662a64 216\r
467d15ae 217 Result = NumberOfBytes;\r
218 while (NumberOfBytes != 0) {\r
219 //\r
220 // Wait for the serial port to be ready, to make sure both the transmit FIFO\r
221 // and shift register empty.\r
222 //\r
223 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);\r
224\r
225 //\r
226 // Fill then entire Tx FIFO\r
227 //\r
228 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {\r
229 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
784ce127 230 if (PcdGetBool (PcdSerialDetectCable)) {\r
231 //\r
232 // Wait for both DSR and CTS to be set\r
233 // DSR is set if a cable is connected.\r
234 // CTS is set if it is ok to transmit data\r
235 //\r
236 // DSR CTS Description Action\r
237 // === === ======================================== ========\r
238 // 0 0 No cable connected. Wait\r
239 // 0 1 No cable connected. Wait\r
240 // 1 0 Cable connected, but not clear to send. Wait\r
241 // 1 1 Cable connected, and clear to send. Transmit\r
242 //\r
243 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) != (B_UART_MSR_DSR | B_UART_MSR_CTS));\r
244 } else {\r
245 //\r
246 // Wait for both DSR and CTS to be set OR for DSR to be clear. \r
247 // DSR is set if a cable is connected.\r
248 // CTS is set if it is ok to transmit data\r
249 //\r
250 // DSR CTS Description Action\r
251 // === === ======================================== ========\r
252 // 0 0 No cable connected. Transmit\r
253 // 0 1 No cable connected. Transmit\r
254 // 1 0 Cable connected, but not clear to send. Wait\r
255 // 1 1 Cable connected, and clar to send. Transmit\r
256 //\r
257 while ((SerialPortReadRegister (R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) == (B_UART_MSR_DSR));\r
258 }\r
467d15ae 259 }\r
260 \r
261 //\r
262 // Write byte to the transmit buffer.\r
263 //\r
264 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);\r
265 }\r
266 }\r
267 return Result;\r
268}\r
269\r
270/**\r
271 Reads data from a serial device into a buffer.\r
272\r
273 @param Buffer Pointer to the data buffer to store the data read from the serial device.\r
274 @param NumberOfBytes Number of bytes to read from the serial device.\r
275\r
276 @retval 0 NumberOfBytes is 0.\r
277 @retval >0 The number of bytes read from the serial device. \r
278 If this value is less than NumberOfBytes, then the read operation failed.\r
279\r
280**/\r
281UINTN\r
282EFIAPI\r
283SerialPortRead (\r
284 OUT UINT8 *Buffer,\r
285 IN UINTN NumberOfBytes\r
286)\r
287{\r
288 UINTN Result;\r
289 UINT8 Mcr;\r
290\r
291 if (NULL == Buffer) {\r
292 return 0;\r
293 }\r
294\r
f2ad9497 295 Mcr = (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS);\r
467d15ae 296 \r
297 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {\r
298 //\r
299 // Wait for the serial port to have some data.\r
300 //\r
301 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0) {\r
302 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
303 //\r
304 // Set RTS to let the peer send some data\r
305 //\r
f2ad9497 306 SerialPortWriteRegister (R_UART_MCR, (UINT8)(Mcr | B_UART_MCR_RTS));\r
467d15ae 307 }\r
308 }\r
309 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
310 //\r
311 // Clear RTS to prevent peer from sending data\r
312 //\r
313 SerialPortWriteRegister (R_UART_MCR, Mcr);\r
314 }\r
315 \r
316 //\r
317 // Read byte from the receive buffer.\r
318 //\r
319 *Buffer = SerialPortReadRegister (R_UART_RXBUF);\r
320 }\r
321 \r
322 return Result;\r
323}\r
324\r
325/**\r
326 Polls a serial device to see if there is any data waiting to be read.\r
327\r
328 Polls aserial device to see if there is any data waiting to be read.\r
329 If there is data waiting to be read from the serial device, then TRUE is returned.\r
330 If there is no data waiting to be read from the serial device, then FALSE is returned.\r
331\r
332 @retval TRUE Data is waiting to be read from the serial device.\r
333 @retval FALSE There is no data waiting to be read from the serial device.\r
334\r
335**/\r
336BOOLEAN\r
337EFIAPI\r
338SerialPortPoll (\r
339 VOID\r
340 )\r
341{\r
342 //\r
343 // Read the serial port status\r
344 //\r
345 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {\r
346 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
347 //\r
348 // Clear RTS to prevent peer from sending data\r
349 //\r
f2ad9497 350 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) & ~B_UART_MCR_RTS));\r
467d15ae 351 }\r
352 return TRUE;\r
353 } \r
354 \r
355 if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
356 //\r
357 // Set RTS to let the peer send some data\r
358 //\r
f2ad9497 359 SerialPortWriteRegister (R_UART_MCR, (UINT8)(SerialPortReadRegister (R_UART_MCR) | B_UART_MCR_RTS));\r
467d15ae 360 }\r
361 \r
362 return FALSE;\r
363}\r