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