]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
ArmPlatformPkg: reorganize PL011 code
[mirror_edk2.git] / ArmPlatformPkg / Library / PL011UartLib / PL011UartLib.c
CommitLineData
12156134
AB
1/** @file\r
2 Serial I/O Port library functions with no library constructor/destructor\r
3\r
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.<BR>\r
6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <Uefi.h>\r
18\r
19#include <Library/DebugLib.h>\r
20#include <Library/IoLib.h>\r
21#include <Library/PcdLib.h>\r
22\r
23#include <Protocol/SerialIo.h>\r
24\r
25#include "PL011Uart.h"\r
26\r
27#define FRACTION_PART_SIZE_IN_BITS 6\r
28#define FRACTION_PART_MASK ((1 << FRACTION_PART_SIZE_IN_BITS) - 1)\r
29\r
30//\r
31// EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE is the only\r
32// control bit that is not supported.\r
33//\r
34STATIC CONST UINT32 mInvalidControlBits = EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE;\r
35\r
36/**\r
37\r
38 Initialise the serial port to the specified settings.\r
39 The serial port is re-configured only if the specified settings\r
40 are different from the current settings.\r
41 All unspecified settings will be set to the default values.\r
42\r
43 @param UartBase The base address of the serial device.\r
44 @param UartClkInHz The clock in Hz for the serial device.\r
45 Ignored if the PCD PL011UartInteger is not 0\r
46 @param BaudRate The baud rate of the serial device. If the\r
47 baud rate is not supported, the speed will be\r
48 reduced to the nearest supported one and the\r
49 variable's value will be updated accordingly.\r
50 @param ReceiveFifoDepth The number of characters the device will\r
51 buffer on input. Value of 0 will use the\r
52 device's default FIFO depth.\r
53 @param Parity If applicable, this is the EFI_PARITY_TYPE\r
54 that is computed or checked as each character\r
55 is transmitted or received. If the device\r
56 does not support parity, the value is the\r
57 default parity value.\r
58 @param DataBits The number of data bits in each character.\r
59 @param StopBits If applicable, the EFI_STOP_BITS_TYPE number\r
60 of stop bits per character.\r
61 If the device does not support stop bits, the\r
62 value is the default stop bit value.\r
63\r
64 @retval RETURN_SUCCESS All attributes were set correctly on the\r
65 serial device.\r
66 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an\r
67 unsupported value.\r
68\r
69**/\r
70RETURN_STATUS\r
71EFIAPI\r
72PL011UartInitializePort (\r
73 IN UINTN UartBase,\r
74 IN UINT32 UartClkInHz,\r
75 IN OUT UINT64 *BaudRate,\r
76 IN OUT UINT32 *ReceiveFifoDepth,\r
77 IN OUT EFI_PARITY_TYPE *Parity,\r
78 IN OUT UINT8 *DataBits,\r
79 IN OUT EFI_STOP_BITS_TYPE *StopBits\r
80 )\r
81{\r
82 UINT32 LineControl;\r
83 UINT32 Divisor;\r
84 UINT32 Integer;\r
85 UINT32 Fractional;\r
86 UINT32 HardwareFifoDepth;\r
87\r
88 HardwareFifoDepth = (PL011_UARTPID2_VER (MmioRead32 (UartBase + UARTPID2)) \\r
89 > PL011_VER_R1P4) \\r
90 ? 32 : 16 ;\r
91 // The PL011 supports a buffer of 1, 16 or 32 chars. Therefore we can accept\r
92 // 1 char buffer as the minimum FIFO size. Because everything can be rounded\r
93 // down, there is no maximum FIFO size.\r
94 if ((*ReceiveFifoDepth == 0) || (*ReceiveFifoDepth >= HardwareFifoDepth)) {\r
95 // Enable FIFO\r
96 LineControl = PL011_UARTLCR_H_FEN;\r
97 *ReceiveFifoDepth = HardwareFifoDepth;\r
98 } else {\r
99 // Disable FIFO\r
100 LineControl = 0;\r
101 // Nothing else to do. 1 byte FIFO is default.\r
102 *ReceiveFifoDepth = 1;\r
103 }\r
104\r
105 //\r
106 // Parity\r
107 //\r
108 switch (*Parity) {\r
109 case DefaultParity:\r
110 *Parity = NoParity;\r
111 case NoParity:\r
112 // Nothing to do. Parity is disabled by default.\r
113 break;\r
114 case EvenParity:\r
115 LineControl |= (PL011_UARTLCR_H_PEN | PL011_UARTLCR_H_EPS);\r
116 break;\r
117 case OddParity:\r
118 LineControl |= PL011_UARTLCR_H_PEN;\r
119 break;\r
120 case MarkParity:\r
121 LineControl |= ( PL011_UARTLCR_H_PEN \\r
122 | PL011_UARTLCR_H_SPS \\r
123 | PL011_UARTLCR_H_EPS);\r
124 break;\r
125 case SpaceParity:\r
126 LineControl |= (PL011_UARTLCR_H_PEN | PL011_UARTLCR_H_SPS);\r
127 break;\r
128 default:\r
129 return RETURN_INVALID_PARAMETER;\r
130 }\r
131\r
132 //\r
133 // Data Bits\r
134 //\r
135 switch (*DataBits) {\r
136 case 0:\r
137 *DataBits = 8;\r
138 case 8:\r
139 LineControl |= PL011_UARTLCR_H_WLEN_8;\r
140 break;\r
141 case 7:\r
142 LineControl |= PL011_UARTLCR_H_WLEN_7;\r
143 break;\r
144 case 6:\r
145 LineControl |= PL011_UARTLCR_H_WLEN_6;\r
146 break;\r
147 case 5:\r
148 LineControl |= PL011_UARTLCR_H_WLEN_5;\r
149 break;\r
150 default:\r
151 return RETURN_INVALID_PARAMETER;\r
152 }\r
153\r
154 //\r
155 // Stop Bits\r
156 //\r
157 switch (*StopBits) {\r
158 case DefaultStopBits:\r
159 *StopBits = OneStopBit;\r
160 case OneStopBit:\r
161 // Nothing to do. One stop bit is enabled by default.\r
162 break;\r
163 case TwoStopBits:\r
164 LineControl |= PL011_UARTLCR_H_STP2;\r
165 break;\r
166 case OneFiveStopBits:\r
167 // Only 1 or 2 stop bits are supported\r
168 default:\r
169 return RETURN_INVALID_PARAMETER;\r
170 }\r
171\r
172 // Don't send the LineControl value to the PL011 yet,\r
173 // wait until after the Baud Rate setting.\r
174 // This ensures we do not mess up the UART settings halfway through\r
175 // in the rare case when there is an error with the Baud Rate.\r
176\r
177 //\r
178 // Baud Rate\r
179 //\r
180\r
181 // If PL011 Integer value has been defined then always ignore the BAUD rate\r
182 if (FixedPcdGet32 (PL011UartInteger) != 0) {\r
183 Integer = FixedPcdGet32 (PL011UartInteger);\r
184 Fractional = FixedPcdGet32 (PL011UartFractional);\r
185 } else {\r
186 // If BAUD rate is zero then replace it with the system default value\r
187 if (*BaudRate == 0) {\r
188 *BaudRate = FixedPcdGet32 (PcdSerialBaudRate);\r
189 if (*BaudRate == 0) {\r
190 return RETURN_INVALID_PARAMETER;\r
191 }\r
192 }\r
193 if (0 == UartClkInHz) {\r
194 return RETURN_INVALID_PARAMETER;\r
195 }\r
196\r
197 Divisor = (UartClkInHz * 4) / *BaudRate;\r
198 Integer = Divisor >> FRACTION_PART_SIZE_IN_BITS;\r
199 Fractional = Divisor & FRACTION_PART_MASK;\r
200 }\r
201\r
202 //\r
203 // If PL011 is already initialized, check the current settings\r
204 // and re-initialize only if the settings are different.\r
205 //\r
206 if (((MmioRead32 (UartBase + UARTCR) & PL011_UARTCR_UARTEN) != 0) &&\r
207 (MmioRead32 (UartBase + UARTLCR_H) == LineControl) &&\r
208 (MmioRead32 (UartBase + UARTIBRD) == Integer) &&\r
209 (MmioRead32 (UartBase + UARTFBRD) == Fractional)) {\r
210 // Nothing to do - already initialized with correct attributes\r
211 return RETURN_SUCCESS;\r
212 }\r
213\r
214 // Wait for the end of transmission\r
215 while ((MmioRead32 (UartBase + UARTFR) & PL011_UARTFR_TXFE) == 0);\r
216\r
217 // Disable UART: "The UARTLCR_H, UARTIBRD, and UARTFBRD registers must not be changed\r
218 // when the UART is enabled"\r
219 MmioWrite32 (UartBase + UARTCR, 0);\r
220\r
221 // Set Baud Rate Registers\r
222 MmioWrite32 (UartBase + UARTIBRD, Integer);\r
223 MmioWrite32 (UartBase + UARTFBRD, Fractional);\r
224\r
225 // No parity, 1 stop, no fifo, 8 data bits\r
226 MmioWrite32 (UartBase + UARTLCR_H, LineControl);\r
227\r
228 // Clear any pending errors\r
229 MmioWrite32 (UartBase + UARTECR, 0);\r
230\r
231 // Enable Tx, Rx, and UART overall\r
232 MmioWrite32 (UartBase + UARTCR,\r
233 PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN);\r
234\r
235 return RETURN_SUCCESS;\r
236}\r
237\r
238/**\r
239\r
240 Assert or deassert the control signals on a serial port.\r
241 The following control signals are set according their bit settings :\r
242 . Request to Send\r
243 . Data Terminal Ready\r
244\r
245 @param[in] UartBase UART registers base address\r
246 @param[in] Control The following bits are taken into account :\r
247 . EFI_SERIAL_REQUEST_TO_SEND : assert/deassert the\r
248 "Request To Send" control signal if this bit is\r
249 equal to one/zero.\r
250 . EFI_SERIAL_DATA_TERMINAL_READY : assert/deassert\r
251 the "Data Terminal Ready" control signal if this\r
252 bit is equal to one/zero.\r
253 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : enable/disable\r
254 the hardware loopback if this bit is equal to\r
255 one/zero.\r
256 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : not supported.\r
257 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : enable/\r
258 disable the hardware flow control based on CTS (Clear\r
259 To Send) and RTS (Ready To Send) control signals.\r
260\r
261 @retval RETURN_SUCCESS The new control bits were set on the device.\r
262 @retval RETURN_UNSUPPORTED The device does not support this operation.\r
263\r
264**/\r
265RETURN_STATUS\r
266EFIAPI\r
267PL011UartSetControl (\r
268 IN UINTN UartBase,\r
269 IN UINT32 Control\r
270 )\r
271{\r
272 UINT32 Bits;\r
273\r
274 if (Control & (mInvalidControlBits)) {\r
275 return RETURN_UNSUPPORTED;\r
276 }\r
277\r
278 Bits = MmioRead32 (UartBase + UARTCR);\r
279\r
280 if (Control & EFI_SERIAL_REQUEST_TO_SEND) {\r
281 Bits |= PL011_UARTCR_RTS;\r
282 } else {\r
283 Bits &= ~PL011_UARTCR_RTS;\r
284 }\r
285\r
286 if (Control & EFI_SERIAL_DATA_TERMINAL_READY) {\r
287 Bits |= PL011_UARTCR_DTR;\r
288 } else {\r
289 Bits &= ~PL011_UARTCR_DTR;\r
290 }\r
291\r
292 if (Control & EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE) {\r
293 Bits |= PL011_UARTCR_LBE;\r
294 } else {\r
295 Bits &= ~PL011_UARTCR_LBE;\r
296 }\r
297\r
298 if (Control & EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) {\r
299 Bits |= (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN);\r
300 } else {\r
301 Bits &= ~(PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN);\r
302 }\r
303\r
304 MmioWrite32 (UartBase + UARTCR, Bits);\r
305\r
306 return RETURN_SUCCESS;\r
307}\r
308\r
309/**\r
310\r
311 Retrieve the status of the control bits on a serial device.\r
312\r
313 @param[in] UartBase UART registers base address\r
314 @param[out] Control Status of the control bits on a serial device :\r
315\r
316 . EFI_SERIAL_DATA_CLEAR_TO_SEND,\r
317 EFI_SERIAL_DATA_SET_READY,\r
318 EFI_SERIAL_RING_INDICATE,\r
319 EFI_SERIAL_CARRIER_DETECT,\r
320 EFI_SERIAL_REQUEST_TO_SEND,\r
321 EFI_SERIAL_DATA_TERMINAL_READY\r
322 are all related to the DTE (Data Terminal Equipment)\r
323 and DCE (Data Communication Equipment) modes of\r
324 operation of the serial device.\r
325 . EFI_SERIAL_INPUT_BUFFER_EMPTY : equal to one if the\r
326 receive buffer is empty, 0 otherwise.\r
327 . EFI_SERIAL_OUTPUT_BUFFER_EMPTY : equal to one if the\r
328 transmit buffer is empty, 0 otherwise.\r
329 . EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE : equal to one if\r
330 the hardware loopback is enabled (the ouput feeds the\r
331 receive buffer), 0 otherwise.\r
332 . EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE : equal to one if\r
333 a loopback is accomplished by software, 0 otherwise.\r
334 . EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE : equal to\r
335 one if the hardware flow control based on CTS (Clear\r
336 To Send) and RTS (Ready To Send) control signals is\r
337 enabled, 0 otherwise.\r
338\r
339 @retval RETURN_SUCCESS The control bits were read from the serial device.\r
340\r
341**/\r
342RETURN_STATUS\r
343EFIAPI\r
344PL011UartGetControl (\r
345 IN UINTN UartBase,\r
346 OUT UINT32 *Control\r
347 )\r
348{\r
349 UINT32 FlagRegister;\r
350 UINT32 ControlRegister;\r
351\r
352\r
353 FlagRegister = MmioRead32 (UartBase + UARTFR);\r
354 ControlRegister = MmioRead32 (UartBase + UARTCR);\r
355\r
356 *Control = 0;\r
357\r
358 if ((FlagRegister & PL011_UARTFR_CTS) == PL011_UARTFR_CTS) {\r
359 *Control |= EFI_SERIAL_CLEAR_TO_SEND;\r
360 }\r
361\r
362 if ((FlagRegister & PL011_UARTFR_DSR) == PL011_UARTFR_DSR) {\r
363 *Control |= EFI_SERIAL_DATA_SET_READY;\r
364 }\r
365\r
366 if ((FlagRegister & PL011_UARTFR_RI) == PL011_UARTFR_RI) {\r
367 *Control |= EFI_SERIAL_RING_INDICATE;\r
368 }\r
369\r
370 if ((FlagRegister & PL011_UARTFR_DCD) == PL011_UARTFR_DCD) {\r
371 *Control |= EFI_SERIAL_CARRIER_DETECT;\r
372 }\r
373\r
374 if ((ControlRegister & PL011_UARTCR_RTS) == PL011_UARTCR_RTS) {\r
375 *Control |= EFI_SERIAL_REQUEST_TO_SEND;\r
376 }\r
377\r
378 if ((ControlRegister & PL011_UARTCR_DTR) == PL011_UARTCR_DTR) {\r
379 *Control |= EFI_SERIAL_DATA_TERMINAL_READY;\r
380 }\r
381\r
382 if ((FlagRegister & PL011_UARTFR_RXFE) == PL011_UARTFR_RXFE) {\r
383 *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;\r
384 }\r
385\r
386 if ((FlagRegister & PL011_UARTFR_TXFE) == PL011_UARTFR_TXFE) {\r
387 *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;\r
388 }\r
389\r
390 if ((ControlRegister & (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN))\r
391 == (PL011_UARTCR_CTSEN | PL011_UARTCR_RTSEN)) {\r
392 *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;\r
393 }\r
394\r
395 if ((ControlRegister & PL011_UARTCR_LBE) == PL011_UARTCR_LBE) {\r
396 *Control |= EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE;\r
397 }\r
398\r
399 return RETURN_SUCCESS;\r
400}\r
401\r
402/**\r
403 Write data to serial device.\r
404\r
405 @param Buffer Point of data buffer which need to be written.\r
406 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
407\r
408 @retval 0 Write data failed.\r
409 @retval !0 Actual number of bytes written to serial device.\r
410\r
411**/\r
412UINTN\r
413EFIAPI\r
414PL011UartWrite (\r
415 IN UINTN UartBase,\r
416 IN UINT8 *Buffer,\r
417 IN UINTN NumberOfBytes\r
418 )\r
419{\r
420 UINT8* CONST Final = &Buffer[NumberOfBytes];\r
421\r
422 while (Buffer < Final) {\r
423 // Wait until UART able to accept another char\r
424 while ((MmioRead32 (UartBase + UARTFR) & UART_TX_FULL_FLAG_MASK));\r
425\r
426 MmioWrite8 (UartBase + UARTDR, *Buffer++);\r
427 }\r
428\r
429 return NumberOfBytes;\r
430}\r
431\r
432/**\r
433 Read data from serial device and save the data in buffer.\r
434\r
435 @param Buffer Point of data buffer which need to be written.\r
436 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
437\r
438 @retval 0 Read data failed.\r
439 @retval !0 Actual number of bytes read from serial device.\r
440\r
441**/\r
442UINTN\r
443EFIAPI\r
444PL011UartRead (\r
445 IN UINTN UartBase,\r
446 OUT UINT8 *Buffer,\r
447 IN UINTN NumberOfBytes\r
448 )\r
449{\r
450 UINTN Count;\r
451\r
452 for (Count = 0; Count < NumberOfBytes; Count++, Buffer++) {\r
453 while ((MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) != 0);\r
454 *Buffer = MmioRead8 (UartBase + UARTDR);\r
455 }\r
456\r
457 return NumberOfBytes;\r
458}\r
459\r
460/**\r
461 Check to see if any data is available to be read from the debug device.\r
462\r
463 @retval TRUE At least one byte of data is available to be read\r
464 @retval FALSE No data is available to be read\r
465\r
466**/\r
467BOOLEAN\r
468EFIAPI\r
469PL011UartPoll (\r
470 IN UINTN UartBase\r
471 )\r
472{\r
473 return ((MmioRead32 (UartBase + UARTFR) & UART_RX_EMPTY_FLAG_MASK) == 0);\r
474}\r