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