2 Serial I/O Port library functions with no library constructor/destructor
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "PlatformSerialPortLib.h"
12 UINT16 gComBase
= 0x3f8;
20 Initialize Serial Port
22 The Baud Rate Divisor registers are programmed and the LCR
23 is used to configure the communications format. Hard coded
24 UART config comes from globals in DebugSerialPlatform lib.
44 Data
= (UINT8
) (gData
- (UINT8
) 5);
47 // Calculate divisor for baud generator
49 Divisor
= 115200 / gBps
;
52 // Set communications format
54 OutputData
= (UINT8
) ((DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
55 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
58 // Configure baud rate
60 IoWrite8 (gComBase
+ BAUD_HIGH_OFFSET
, (UINT8
) (Divisor
>> 8));
61 IoWrite8 (gComBase
+ BAUD_LOW_OFFSET
, (UINT8
) (Divisor
& 0xff));
64 // Switch back to bank 0
66 OutputData
= (UINT8
) ((~DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
67 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
69 return RETURN_SUCCESS
;
73 Common function to initialize UART Serial device and USB Serial device.
82 SerialPortInitialize (
90 return RETURN_SUCCESS
;
94 Write data to serial device.
96 If the buffer is NULL, then return 0;
97 if NumberOfBytes is zero, then return 0.
99 @param Buffer Point of data buffer which need to be writed.
100 @param NumberOfBytes Number of output bytes which are cached in Buffer.
102 @retval 0 Write data failed.
103 @retval !0 Actual number of bytes writed to serial device.
110 IN UINTN NumberOfBytes
116 if (NULL
== Buffer
) {
120 Result
= NumberOfBytes
;
122 while (NumberOfBytes
--) {
124 // Wait for the serial port to be ready.
127 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
128 } while ((Data
& LSR_TXRDY
) == 0);
129 IoWrite8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
), *Buffer
++);
136 Common function to write data to UART Serial device and USB Serial device.
138 @param Buffer Point of data buffer which need to be writed.
139 @param NumberOfBytes Number of output bytes which are cached in Buffer.
146 IN UINTN NumberOfBytes
149 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
150 UARTDbgOut (Buffer
, NumberOfBytes
);
153 return RETURN_SUCCESS
;
157 Read data from serial device and save the datas in buffer.
159 If the buffer is NULL, then return 0;
160 if NumberOfBytes is zero, then return 0.
162 @param Buffer Point of data buffer which need to be writed.
163 @param NumberOfBytes Number of output bytes which are cached in Buffer.
165 @retval 0 Read data failed.
166 @retval !0 Actual number of bytes raed to serial device.
173 IN UINTN NumberOfBytes
179 if (NULL
== Buffer
) {
183 Result
= NumberOfBytes
;
185 while (NumberOfBytes
--) {
187 // Wait for the serial port to be ready.
190 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
191 } while ((Data
& LSR_RXDA
) == 0);
193 *Buffer
++ = IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
));
200 Common function to Read data from UART serial device, USB serial device and save the datas in buffer.
202 @param Buffer Point of data buffer which need to be writed.
203 @param NumberOfBytes Number of output bytes which are cached in Buffer.
210 IN UINTN NumberOfBytes
213 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
214 UARTDbgIn (Buffer
, NumberOfBytes
);
217 return RETURN_SUCCESS
;
222 Polls a serial device to see if there is any data waiting to be read.
224 Polls aserial device to see if there is any data waiting to be read.
225 If there is data waiting to be read from the serial device, then TRUE is returned.
226 If there is no data waiting to be read from the serial device, then FALSE is returned.
228 @retval TRUE Data is waiting to be read from the serial device.
229 @retval FALSE There is no data waiting to be read from the serial device.
241 // Read the serial port status.
243 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
245 return (BOOLEAN
) ((Data
& LSR_RXDA
) != 0);