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