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