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