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