EmulatorPkg: Use SerialDxe in MdeModulePkg instead of EmbeddedPkg
[mirror_edk2.git] / CorebootPayloadPkg / Library / SerialPortLib / SerialPortLib.c
CommitLineData
9c228fb0
MM
1/** @file\r
2 SerialPortLib instance for UART device upon coreboot\r
3\r
4 Copyright (c) 2014, 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 <Uefi/UefiBaseType.h>\r
16#include <Library/SerialPortLib.h>\r
17#include <Library/PcdLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/CbParseLib.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_DSR BIT5\r
42\r
43UINT32 mSerialRegBase = 0;\r
44UINT32 mSerialRegAccessType = 0;\r
45\r
46/**\r
47 Read an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is read from \r
48 MMIO space. If PcdSerialUseMmio is FALSE, then the value is read from I/O space. The\r
49 parameter Offset is added to the base address of the 16550 registers that is specified \r
50 by PcdSerialRegisterBase. \r
51 \r
52 @param Offset The offset of the 16550 register to read.\r
53\r
54 @return The value read from the 16550 register.\r
55\r
56**/\r
57UINT8\r
58SerialPortReadRegister (\r
59 UINTN Offset\r
60 )\r
61{\r
62 if (mSerialRegAccessType == 2) { //MMIO\r
63 return MmioRead8 (mSerialRegBase + Offset);\r
64 } else { //IO\r
65 return IoRead8 ((UINT16)mSerialRegBase + Offset);\r
66 }\r
67}\r
68\r
69/**\r
70 Write an 8-bit 16550 register. If PcdSerialUseMmio is TRUE, then the value is written to\r
71 MMIO space. If PcdSerialUseMmio is FALSE, then the value is written to I/O space. The\r
72 parameter Offset is added to the base address of the 16550 registers that is specified \r
73 by PcdSerialRegisterBase. \r
74 \r
75 @param Offset The offset of the 16550 register to write.\r
76 @param Value The value to write to the 16550 register specified by Offset.\r
77\r
78 @return The value written to the 16550 register.\r
79\r
80**/\r
81UINT8\r
82SerialPortWriteRegister (\r
83 UINTN Offset,\r
84 UINT8 Value\r
85 )\r
86{\r
87 if (mSerialRegAccessType == 2) { //MMIO\r
88 return MmioWrite8 (mSerialRegBase + Offset, Value);\r
89 } else {// IO\r
90 return IoWrite8 ((UINT16)mSerialRegBase + Offset, Value);\r
91 }\r
92}\r
93\r
94/**\r
95 Initialize the serial device hardware.\r
96 \r
97 If no initialization is required, then return RETURN_SUCCESS.\r
98 If the serial device was successfully initialized, then return RETURN_SUCCESS.\r
99 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
100 \r
101 @retval RETURN_SUCCESS The serial device was initialized.\r
102 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.\r
103\r
104**/\r
105RETURN_STATUS\r
106EFIAPI\r
107SerialPortInitialize (\r
108 VOID\r
109 )\r
110{\r
111 RETURN_STATUS Status;\r
112 UINTN Divisor;\r
113 BOOLEAN Initialized;\r
114 \r
115 Status = CbParseSerialInfo (&mSerialRegBase, &mSerialRegAccessType, NULL);\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\r
129 if ((SerialPortReadRegister (R_UART_LCR) & 0x3F) != (PcdGet8 (PcdSerialLineControl) & 0x3F)) {\r
130 Initialized = FALSE;\r
131 }\r
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
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
155 SerialPortWriteRegister (R_UART_LCR, (UINT8)(PcdGet8 (PcdSerialLineControl) & 0x3F));\r
156\r
157 //\r
158 // Enable and reset FIFOs\r
159 // Strip reserved bits from PcdSerialFifoControl\r
160 //\r
161 SerialPortWriteRegister (R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & 0x27));\r
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
197 UINTN Result;\r
198 UINTN Index;\r
199 UINTN FifoSize;\r
200\r
201 if (Buffer == NULL) {\r
202 return 0;\r
203 }\r
204\r
205 if (NumberOfBytes == 0) {\r
206 //\r
207 // Flush the hardware\r
208 //\r
209\r
210 //\r
211 // Wait for both the transmit FIFO and shift register empty.\r
212 //\r
213 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);\r
214\r
215 return 0;\r
216 }\r
217\r
218 //\r
219 // Compute the maximum size of the Tx FIFO\r
220 //\r
221 FifoSize = 1;\r
222 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {\r
223 if ((PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {\r
224 FifoSize = 16;\r
225 } else {\r
226 FifoSize = 64;\r
227 }\r
228 }\r
229\r
230 Result = NumberOfBytes;\r
231 while (NumberOfBytes != 0) {\r
232 //\r
233 // Wait for the serial port to be ready, to make sure both the transmit FIFO\r
234 // and shift register empty.\r
235 //\r
236 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_TEMT) == 0);\r
237\r
238 //\r
239 // Fill then entire Tx FIFO\r
240 //\r
241 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {\r
242\r
243 //\r
244 // Write byte to the transmit buffer.\r
245 //\r
246 SerialPortWriteRegister (R_UART_TXBUF, *Buffer);\r
247 }\r
248 }\r
249 return Result;\r
250}\r
251\r
252/**\r
253 Reads data from a serial device into a buffer.\r
254\r
255 @param Buffer Pointer to the data buffer to store the data read from the serial device.\r
256 @param NumberOfBytes Number of bytes to read from the serial device.\r
257\r
258 @retval 0 NumberOfBytes is 0.\r
259 @retval >0 The number of bytes read from the serial device. \r
260 If this value is less than NumberOfBytes, then the read operation failed.\r
261\r
262**/\r
263UINTN\r
264EFIAPI\r
265SerialPortRead (\r
266 OUT UINT8 *Buffer,\r
267 IN UINTN NumberOfBytes\r
268)\r
269{\r
270 UINTN Result;\r
271\r
272 if (NULL == Buffer) {\r
273 return 0;\r
274 }\r
275 \r
276 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {\r
277 //\r
278 // Wait for the serial port to have some data.\r
279 //\r
280 while ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) == 0);\r
281 \r
282 //\r
283 // Read byte from the receive buffer.\r
284 //\r
285 *Buffer = SerialPortReadRegister (R_UART_RXBUF);\r
286 }\r
287 \r
288 return Result;\r
289}\r
290\r
291/**\r
292 Polls a serial device to see if there is any data waiting to be read.\r
293\r
294 Polls aserial device to see if there is any data waiting to be read.\r
295 If there is data waiting to be read from the serial device, then TRUE is returned.\r
296 If there is no data waiting to be read from the serial device, then FALSE is returned.\r
297\r
298 @retval TRUE Data is waiting to be read from the serial device.\r
299 @retval FALSE There is no data waiting to be read from the serial device.\r
300\r
301**/\r
302BOOLEAN\r
303EFIAPI\r
304SerialPortPoll (\r
305 VOID\r
306 )\r
307{\r
308 //\r
309 // Read the serial port status\r
310 //\r
311 if ((SerialPortReadRegister (R_UART_LSR) & B_UART_LSR_RXRDY) != 0) {\r
312 return TRUE;\r
313 } \r
314 \r
315 return FALSE;\r
316}\r