]>
Commit | Line | Data |
---|---|---|
f5752cb2 | 1 | /** @file\r |
78297266 | 2 | UART Serial Port library instance with empty functions.\r |
f5752cb2 | 3 | \r |
78297266 | 4 | Copyright (c) 2006 - 2008, Intel Corporation\r |
f5752cb2 | 5 | All rights reserved. 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 | |
f5752cb2 | 15 | #include <Base.h>\r |
16 | \r | |
f5752cb2 | 17 | #include <Library/SerialPortLib.h>\r |
78297266 | 18 | #include <Library/IoLib.h>\r |
f5752cb2 | 19 | \r |
20 | //---------------------------------------------\r | |
21 | // UART Register Offsets\r | |
22 | //---------------------------------------------\r | |
23 | #define BAUD_LOW_OFFSET 0x00\r | |
24 | #define BAUD_HIGH_OFFSET 0x01\r | |
25 | #define IER_OFFSET 0x01\r | |
26 | #define LCR_SHADOW_OFFSET 0x01\r | |
27 | #define FCR_SHADOW_OFFSET 0x02\r | |
28 | #define IR_CONTROL_OFFSET 0x02\r | |
29 | #define FCR_OFFSET 0x02\r | |
30 | #define EIR_OFFSET 0x02\r | |
31 | #define BSR_OFFSET 0x03\r | |
32 | #define LCR_OFFSET 0x03\r | |
33 | #define MCR_OFFSET 0x04\r | |
34 | #define LSR_OFFSET 0x05\r | |
35 | #define MSR_OFFSET 0x06\r | |
36 | \r | |
37 | //---------------------------------------------\r | |
38 | // UART Register Bit Defines\r | |
39 | //---------------------------------------------\r | |
40 | #define LSR_TXRDY 0x20\r | |
41 | #define LSR_RXDA 0x01\r | |
42 | #define DLAB 0x01\r | |
43 | \r | |
78297266 | 44 | //---------------------------------------------\r |
45 | // UART Settings\r | |
46 | //---------------------------------------------\r | |
47 | UINT16 gUartBase = 0x3F8;\r | |
48 | UINTN gBps = 115200;\r | |
49 | UINT8 gData = 8;\r | |
50 | UINT8 gStop = 1;\r | |
51 | UINT8 gParity = 0;\r | |
f5752cb2 | 52 | UINT8 gBreakSet = 0;\r |
f5752cb2 | 53 | \r |
78297266 | 54 | /**\r |
55 | Initialize the serial device hardware.\r | |
56 | \r | |
57 | If no initialization is required, then return RETURN_SUCCESS.\r | |
58 | If the serial device was successfuly initialized, then return RETURN_SUCCESS.\r | |
59 | If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r | |
60 | \r | |
61 | @retval RETURN_SUCCESS The serial device was initialized.\r | |
62 | @retval RETURN_DEVICE_ERROR The serail device could not be initialized.\r | |
f5752cb2 | 63 | \r |
64 | **/\r | |
65 | RETURN_STATUS\r | |
66 | EFIAPI\r | |
67 | SerialPortInitialize (\r | |
68 | VOID\r | |
69 | )\r | |
70 | {\r | |
78297266 | 71 | UINTN Divisor;\r |
72 | UINT8 OutputData;\r | |
73 | UINT8 Data;\r | |
f5752cb2 | 74 | \r |
78297266 | 75 | //\r |
f5752cb2 | 76 | // Map 5..8 to 0..3\r |
77 | //\r | |
78297266 | 78 | Data = (UINT8) (gData - (UINT8) 5);\r |
f5752cb2 | 79 | \r |
80 | //\r | |
81 | // Calculate divisor for baud generator\r | |
82 | //\r | |
78297266 | 83 | Divisor = 115200 / gBps;\r |
f5752cb2 | 84 | \r |
85 | //\r | |
86 | // Set communications format\r | |
87 | //\r | |
78297266 | 88 | OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r |
89 | IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r | |
f5752cb2 | 90 | \r |
91 | //\r | |
92 | // Configure baud rate\r | |
93 | //\r | |
78297266 | 94 | IoWrite8 ((UINTN) (gUartBase + BAUD_HIGH_OFFSET), (UINT8) (Divisor >> 8));\r |
95 | IoWrite8 ((UINTN) (gUartBase + BAUD_LOW_OFFSET), (UINT8) (Divisor & 0xff));\r | |
f5752cb2 | 96 | \r |
97 | //\r | |
98 | // Switch back to bank 0\r | |
99 | //\r | |
78297266 | 100 | OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r |
101 | IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r | |
f5752cb2 | 102 | \r |
103 | return RETURN_SUCCESS;\r | |
104 | }\r | |
105 | \r | |
106 | /**\r | |
78297266 | 107 | Write data from buffer to serial device. \r |
108 | \r | |
109 | Writes NumberOfBytes data bytes from Buffer to the serial device. \r | |
110 | The number of bytes actually written to the serial device is returned.\r | |
111 | If the return value is less than NumberOfBytes, then the write operation failed.\r | |
f5752cb2 | 112 | \r |
78297266 | 113 | If Buffer is NULL, then ASSERT(). \r |
f5752cb2 | 114 | \r |
78297266 | 115 | If NumberOfBytes is zero, then return 0.\r |
116 | \r | |
117 | @param Buffer Pointer to the data buffer to be written.\r | |
118 | @param NumberOfBytes Number of bytes to written to the serial device.\r | |
119 | \r | |
120 | @retval 0 NumberOfBytes is 0.\r | |
121 | @retval >0 The number of bytes written to the serial device. \r | |
122 | If this value is less than NumberOfBytes, then the read operation failed.\r | |
f5752cb2 | 123 | \r |
124 | **/\r | |
125 | UINTN\r | |
126 | EFIAPI\r | |
127 | SerialPortWrite (\r | |
128 | IN UINT8 *Buffer,\r | |
129 | IN UINTN NumberOfBytes\r | |
130 | )\r | |
131 | {\r | |
78297266 | 132 | UINTN Result;\r |
133 | UINT8 Data;\r | |
f5752cb2 | 134 | \r |
78297266 | 135 | if (Buffer == NULL) {\r |
f5752cb2 | 136 | return 0;\r |
137 | }\r | |
138 | \r | |
139 | Result = NumberOfBytes;\r | |
140 | \r | |
141 | while (NumberOfBytes--) {\r | |
78297266 | 142 | //\r |
143 | // Wait for the serail port to be ready.\r | |
144 | //\r | |
145 | do {\r | |
146 | Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r | |
147 | } while ((Data & LSR_TXRDY) == 0);\r | |
148 | IoWrite8 ((UINT16) gUartBase, *Buffer++);\r | |
f5752cb2 | 149 | }\r |
150 | \r | |
151 | return Result;\r | |
f5752cb2 | 152 | }\r |
153 | \r | |
154 | \r | |
155 | /**\r | |
78297266 | 156 | Reads data from a serial device into a buffer.\r |
f5752cb2 | 157 | \r |
78297266 | 158 | @param Buffer Pointer to the data buffer to store the data read from the serial device.\r |
159 | @param NumberOfBytes Number of bytes to read from the serial device.\r | |
f5752cb2 | 160 | \r |
78297266 | 161 | @retval 0 NumberOfBytes is 0.\r |
162 | @retval >0 The number of bytes read from the serial device. \r | |
163 | If this value is less than NumberOfBytes, then the read operation failed.\r | |
f5752cb2 | 164 | \r |
165 | **/\r | |
166 | UINTN\r | |
167 | EFIAPI\r | |
168 | SerialPortRead (\r | |
169 | OUT UINT8 *Buffer,\r | |
170 | IN UINTN NumberOfBytes\r | |
171 | )\r | |
172 | {\r | |
78297266 | 173 | UINTN Result;\r |
174 | UINT8 Data;\r | |
175 | \r | |
176 | if (NULL == Buffer) {\r | |
177 | return 0;\r | |
178 | }\r | |
179 | \r | |
180 | Result = NumberOfBytes;\r | |
181 | \r | |
182 | while (NumberOfBytes--) {\r | |
183 | //\r | |
184 | // Wait for the serail port to be ready.\r | |
185 | //\r | |
186 | do {\r | |
187 | Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r | |
188 | } while ((Data & LSR_RXDA) == 0);\r | |
189 | \r | |
190 | *Buffer++ = IoRead8 ((UINT16) gUartBase);\r | |
191 | }\r | |
192 | \r | |
193 | return Result;\r | |
194 | }\r | |
195 | \r | |
196 | /**\r | |
197 | Polls a serial device to see if there is any data waiting to be read.\r | |
198 | \r | |
199 | Polls aserial device to see if there is any data waiting to be read.\r | |
200 | If there is data waiting to be read from the serial device, then TRUE is returned.\r | |
201 | If there is no data waiting to be read from the serial device, then FALSE is returned.\r | |
202 | \r | |
203 | @retval TRUE Data is waiting to be read from the serial device.\r | |
204 | @retval FALSE There is no data waiting to be read from the serial device.\r | |
205 | \r | |
206 | **/\r | |
207 | BOOLEAN\r | |
208 | EFIAPI\r | |
209 | SerialPortPoll (\r | |
210 | VOID\r | |
211 | )\r | |
212 | {\r | |
213 | UINT8 Data;\r | |
214 | \r | |
215 | //\r | |
216 | // Read the serial port status.\r | |
217 | //\r | |
218 | Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r | |
219 | \r | |
220 | return (BOOLEAN) ((Data & LSR_RXDA) != 0);\r | |
f5752cb2 | 221 | }\r |
222 | \r |