]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPlatformPkg/ArmVirtualizationPkg/Library/FdtPL011SerialPortLib/EarlyFdtPL011SerialPortLib.c
MdeModulePkg/UfsPciHcDxe: Fix EBC build error
[mirror_edk2.git] / ArmPlatformPkg / ArmVirtualizationPkg / Library / FdtPL011SerialPortLib / EarlyFdtPL011SerialPortLib.c
CommitLineData
f1f0ba19
LE
1/** @file\r
2 Serial I/O Port library functions with base address discovered from FDT\r
3\r
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
5 Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
6 Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <Base.h>\r
19\r
20#include <Library/PcdLib.h>\r
21#include <Library/SerialPortLib.h>\r
22#include <Library/SerialPortExtLib.h>\r
23#include <libfdt.h>\r
24\r
25#include <Drivers/PL011Uart.h>\r
26\r
27RETURN_STATUS\r
28EFIAPI\r
29SerialPortInitialize (\r
30 VOID\r
31 )\r
32{\r
33 //\r
34 // This SerialPortInitialize() function is completely empty, for a number of\r
35 // reasons:\r
36 // - if we are executing from flash, it is hard to keep state (i.e., store the\r
37 // discovered base address in a global), and the most robust way to deal\r
38 // with this is to discover the base address at every Write ();\r
39 // - calls to the Write() function in this module may be issued before this\r
40 // initialization function is called: this is not a problem when the base\r
41 // address of the UART is hardcoded, and only the baud rate may be wrong,\r
42 // but if we don't know the base address yet, we may be poking into memory\r
43 // that does not tolerate being poked into;\r
44 // - SEC and PEI phases produce debug output only, so with debug disabled, no\r
45 // initialization (or device tree parsing) is performed at all.\r
46 //\r
47 // Note that this means that on *every* Write () call, the device tree will be\r
48 // parsed and the UART re-initialized. However, this is a small price to pay\r
49 // for having serial debug output on a UART with no fixed base address.\r
50 //\r
51 return RETURN_SUCCESS;\r
52}\r
53\r
54STATIC\r
55UINT64\r
56SerialPortGetBaseAddress (\r
57 VOID\r
58 )\r
59{\r
60 UINT64 BaudRate;\r
61 UINT32 ReceiveFifoDepth;\r
62 EFI_PARITY_TYPE Parity;\r
63 UINT8 DataBits;\r
64 EFI_STOP_BITS_TYPE StopBits;\r
65 VOID *DeviceTreeBase;\r
66 INT32 Node, Prev;\r
67 INT32 Len;\r
68 CONST CHAR8 *Compatible;\r
69 CONST CHAR8 *CompatibleItem;\r
70 CONST UINT64 *RegProperty;\r
71 UINTN UartBase;\r
72 RETURN_STATUS Status;\r
73\r
74 DeviceTreeBase = (VOID *)(UINTN)FixedPcdGet64 (PcdDeviceTreeInitialBaseAddress);\r
75\r
76 if ((DeviceTreeBase == NULL) || (fdt_check_header (DeviceTreeBase) != 0)) {\r
77 return 0;\r
78 }\r
79\r
80 //\r
81 // Enumerate all FDT nodes looking for a PL011 and capture its base address\r
82 //\r
83 for (Prev = 0;; Prev = Node) {\r
84 Node = fdt_next_node (DeviceTreeBase, Prev, NULL);\r
85 if (Node < 0) {\r
86 break;\r
87 }\r
88\r
89 Compatible = fdt_getprop (DeviceTreeBase, Node, "compatible", &Len);\r
90 if (Compatible == NULL) {\r
91 continue;\r
92 }\r
93\r
94 //\r
95 // Iterate over the NULL-separated items in the compatible string\r
96 //\r
97 for (CompatibleItem = Compatible; CompatibleItem < Compatible + Len;\r
98 CompatibleItem += 1 + AsciiStrLen (CompatibleItem)) {\r
99\r
100 if (AsciiStrCmp (CompatibleItem, "arm,pl011") == 0) {\r
101 RegProperty = fdt_getprop (DeviceTreeBase, Node, "reg", &Len);\r
102 if (Len != 16) {\r
103 return 0;\r
104 }\r
105 UartBase = (UINTN)fdt64_to_cpu (ReadUnaligned64 (RegProperty));\r
106\r
107 BaudRate = (UINTN)FixedPcdGet64 (PcdUartDefaultBaudRate);\r
108 ReceiveFifoDepth = 0; // Use the default value for Fifo depth\r
109 Parity = (EFI_PARITY_TYPE)FixedPcdGet8 (PcdUartDefaultParity);\r
110 DataBits = FixedPcdGet8 (PcdUartDefaultDataBits);\r
111 StopBits = (EFI_STOP_BITS_TYPE) FixedPcdGet8 (PcdUartDefaultStopBits);\r
112\r
113 Status = PL011UartInitializePort (\r
114 UartBase,\r
115 &BaudRate, &ReceiveFifoDepth, &Parity, &DataBits, &StopBits);\r
116 if (!EFI_ERROR (Status)) {\r
117 return UartBase;\r
118 }\r
119 }\r
120 }\r
121 }\r
122 return 0;\r
123}\r
124\r
125/**\r
126 Write data to serial device.\r
127\r
128 @param Buffer Point of data buffer which need to be written.\r
129 @param NumberOfBytes Number of output bytes which are cached in Buffer.\r
130\r
131 @retval 0 Write data failed.\r
132 @retval !0 Actual number of bytes written to serial device.\r
133\r
134**/\r
135UINTN\r
136EFIAPI\r
137SerialPortWrite (\r
138 IN UINT8 *Buffer,\r
139 IN UINTN NumberOfBytes\r
140 )\r
141{\r
142 UINT64 SerialRegisterBase;\r
143\r
144 SerialRegisterBase = SerialPortGetBaseAddress ();\r
145 if (SerialRegisterBase != 0) {\r
146 return PL011UartWrite ((UINTN)SerialRegisterBase, Buffer, NumberOfBytes);\r
147 }\r
148 return 0;\r
149}\r
150\r
151/**\r
152 Read data from serial device and save the data in buffer.\r
153\r
154 @param Buffer Point of data buffer which need to be written.\r
155 @param NumberOfBytes Size of Buffer[].\r
156\r
157 @retval 0 Read data failed.\r
158 @retval !0 Actual number of bytes read from serial device.\r
159\r
160**/\r
161UINTN\r
162EFIAPI\r
163SerialPortRead (\r
164 OUT UINT8 *Buffer,\r
165 IN UINTN NumberOfBytes\r
166)\r
167{\r
168 return 0;\r
169}\r
170\r
171/**\r
172 Check to see if any data is available to be read from the debug device.\r
173\r
174 @retval TRUE At least one byte of data is available to be read\r
175 @retval FALSE No data is available to be read\r
176\r
177**/\r
178BOOLEAN\r
179EFIAPI\r
180SerialPortPoll (\r
181 VOID\r
182 )\r
183{\r
184 return FALSE;\r
185}\r