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