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