]>
Commit | Line | Data |
---|---|---|
2ec099da | 1 | /** @file |
2 | Serial I/O Port library functions with no library constructor/destructor | |
3 | ||
4 | Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR> | |
5 | \r | |
9dc8036d | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent |
2ec099da | 7 | |
8 | **/ | |
9 | ||
10 | #include "PlatformSerialPortLib.h" | |
11 | ||
12 | UINT16 gComBase = 0x3f8; | |
13 | UINTN gBps = 115200; | |
14 | UINT8 gData = 8; | |
15 | UINT8 gStop = 1; | |
16 | UINT8 gParity = 0; | |
17 | UINT8 gBreakSet = 0; | |
18 | ||
19 | /** | |
20 | Initialize Serial Port | |
21 | ||
22 | The Baud Rate Divisor registers are programmed and the LCR | |
23 | is used to configure the communications format. Hard coded | |
24 | UART config comes from globals in DebugSerialPlatform lib. | |
25 | ||
26 | @param None | |
27 | ||
28 | @retval None | |
29 | ||
30 | **/ | |
31 | RETURN_STATUS | |
32 | EFIAPI | |
33 | UARTInitialize ( | |
34 | VOID | |
35 | ) | |
36 | { | |
37 | UINTN Divisor; | |
38 | UINT8 OutputData; | |
39 | UINT8 Data; | |
40 | ||
41 | // | |
42 | // Map 5..8 to 0..3 | |
43 | // | |
44 | Data = (UINT8) (gData - (UINT8) 5); | |
45 | ||
46 | // | |
47 | // Calculate divisor for baud generator | |
48 | // | |
49 | Divisor = 115200 / gBps; | |
50 | ||
51 | // | |
52 | // Set communications format | |
53 | // | |
54 | OutputData = (UINT8) ((DLAB << 7) | ((gBreakSet << 6) | ((gParity << 3) | ((gStop << 2) | Data)))); | |
55 | IoWrite8 (gComBase + LCR_OFFSET, OutputData); | |
56 | ||
57 | // | |
58 | // Configure baud rate | |
59 | // | |
60 | IoWrite8 (gComBase + BAUD_HIGH_OFFSET, (UINT8) (Divisor >> 8)); | |
61 | IoWrite8 (gComBase + BAUD_LOW_OFFSET, (UINT8) (Divisor & 0xff)); | |
62 | ||
63 | // | |
64 | // Switch back to bank 0 | |
65 | // | |
66 | OutputData = (UINT8) ((~DLAB << 7) | ((gBreakSet << 6) | ((gParity << 3) | ((gStop << 2) | Data)))); | |
67 | IoWrite8 (gComBase + LCR_OFFSET, OutputData); | |
68 | ||
69 | return RETURN_SUCCESS; | |
70 | } | |
71 | ||
72 | /** | |
73 | Common function to initialize UART Serial device and USB Serial device. | |
74 | ||
75 | @param None | |
76 | ||
77 | @retval None | |
78 | ||
79 | **/ | |
80 | RETURN_STATUS | |
81 | EFIAPI | |
82 | SerialPortInitialize ( | |
83 | VOID | |
84 | ) | |
85 | { | |
86 | ||
87 | UARTInitialize (); | |
88 | ||
89 | ||
90 | return RETURN_SUCCESS; | |
91 | } | |
92 | ||
93 | /** | |
94 | Write data to serial device. | |
95 | ||
96 | If the buffer is NULL, then return 0; | |
97 | if NumberOfBytes is zero, then return 0. | |
98 | ||
99 | @param Buffer Point of data buffer which need to be writed. | |
100 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
101 | ||
102 | @retval 0 Write data failed. | |
103 | @retval !0 Actual number of bytes writed to serial device. | |
104 | ||
105 | **/ | |
106 | UINTN | |
107 | EFIAPI | |
108 | UARTDbgOut ( | |
109 | IN UINT8 *Buffer, | |
110 | IN UINTN NumberOfBytes | |
111 | ) | |
112 | { | |
113 | UINTN Result; | |
114 | UINT8 Data; | |
115 | ||
116 | if (NULL == Buffer) { | |
117 | return 0; | |
118 | } | |
119 | ||
120 | Result = NumberOfBytes; | |
121 | ||
122 | while (NumberOfBytes--) { | |
123 | // | |
124 | // Wait for the serial port to be ready. | |
125 | // | |
126 | do { | |
127 | Data = IoRead8 ((UINT16) PcdGet64 (PcdSerialRegisterBase) + LSR_OFFSET); | |
128 | } while ((Data & LSR_TXRDY) == 0); | |
129 | IoWrite8 ((UINT16) PcdGet64 (PcdSerialRegisterBase), *Buffer++); | |
130 | } | |
131 | ||
132 | return Result; | |
133 | } | |
134 | ||
135 | /** | |
136 | Common function to write data to UART Serial device and USB Serial device. | |
137 | ||
138 | @param Buffer Point of data buffer which need to be writed. | |
139 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
140 | ||
141 | **/ | |
142 | UINTN | |
143 | EFIAPI | |
144 | SerialPortWrite ( | |
145 | IN UINT8 *Buffer, | |
146 | IN UINTN NumberOfBytes | |
147 | ) | |
148 | { | |
149 | if (FeaturePcdGet (PcdStatusCodeUseIsaSerial)) { | |
150 | UARTDbgOut (Buffer, NumberOfBytes); | |
151 | } | |
152 | ||
153 | return RETURN_SUCCESS; | |
154 | } | |
155 | ||
156 | /** | |
157 | Read data from serial device and save the datas in buffer. | |
158 | ||
159 | If the buffer is NULL, then return 0; | |
160 | if NumberOfBytes is zero, then return 0. | |
161 | ||
162 | @param Buffer Point of data buffer which need to be writed. | |
163 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
164 | ||
165 | @retval 0 Read data failed. | |
166 | @retval !0 Actual number of bytes raed to serial device. | |
167 | ||
168 | **/ | |
169 | UINTN | |
170 | EFIAPI | |
171 | UARTDbgIn ( | |
172 | OUT UINT8 *Buffer, | |
173 | IN UINTN NumberOfBytes | |
174 | ) | |
175 | { | |
176 | UINTN Result; | |
177 | UINT8 Data; | |
178 | ||
179 | if (NULL == Buffer) { | |
180 | return 0; | |
181 | } | |
182 | ||
183 | Result = NumberOfBytes; | |
184 | ||
185 | while (NumberOfBytes--) { | |
186 | // | |
187 | // Wait for the serial port to be ready. | |
188 | // | |
189 | do { | |
190 | Data = IoRead8 ((UINT16) PcdGet64 (PcdSerialRegisterBase) + LSR_OFFSET); | |
191 | } while ((Data & LSR_RXDA) == 0); | |
192 | ||
193 | *Buffer++ = IoRead8 ((UINT16) PcdGet64 (PcdSerialRegisterBase)); | |
194 | } | |
195 | ||
196 | return Result; | |
197 | } | |
198 | ||
199 | /** | |
200 | Common function to Read data from UART serial device, USB serial device and save the datas in buffer. | |
201 | ||
202 | @param Buffer Point of data buffer which need to be writed. | |
203 | @param NumberOfBytes Number of output bytes which are cached in Buffer. | |
204 | ||
205 | **/ | |
206 | UINTN | |
207 | EFIAPI | |
208 | SerialPortRead ( | |
209 | OUT UINT8 *Buffer, | |
210 | IN UINTN NumberOfBytes | |
211 | ) | |
212 | { | |
213 | if (FeaturePcdGet (PcdStatusCodeUseIsaSerial)) { | |
214 | UARTDbgIn (Buffer, NumberOfBytes); | |
215 | } | |
216 | ||
217 | return RETURN_SUCCESS; | |
218 | } | |
219 | ||
220 | ||
221 | /** | |
222 | Polls a serial device to see if there is any data waiting to be read. | |
223 | ||
224 | Polls aserial device to see if there is any data waiting to be read. | |
225 | If there is data waiting to be read from the serial device, then TRUE is returned. | |
226 | If there is no data waiting to be read from the serial device, then FALSE is returned. | |
227 | ||
228 | @retval TRUE Data is waiting to be read from the serial device. | |
229 | @retval FALSE There is no data waiting to be read from the serial device. | |
230 | ||
231 | **/ | |
232 | BOOLEAN | |
233 | EFIAPI | |
234 | SerialPortPoll ( | |
235 | VOID | |
236 | ) | |
237 | { | |
238 | UINT8 Data; | |
239 | ||
240 | // | |
241 | // Read the serial port status. | |
242 | // | |
243 | Data = IoRead8 ((UINT16) PcdGet64 (PcdSerialRegisterBase) + LSR_OFFSET); | |
244 | ||
245 | return (BOOLEAN) ((Data & LSR_RXDA) != 0); | |
246 | } |