2 Serial I/O Port library functions with no library constructor/destructor
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
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.
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.
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.
22 Module Name: SerialPortLib.c
26 #include "PlatformSerialPortLib.h"
28 UINT16 gComBase
= 0x3f8;
36 Initialize Serial Port
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.
60 Data
= (UINT8
) (gData
- (UINT8
) 5);
63 // Calculate divisor for baud generator
65 Divisor
= 115200 / gBps
;
68 // Set communications format
70 OutputData
= (UINT8
) ((DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
71 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
74 // Configure baud rate
76 IoWrite8 (gComBase
+ BAUD_HIGH_OFFSET
, (UINT8
) (Divisor
>> 8));
77 IoWrite8 (gComBase
+ BAUD_LOW_OFFSET
, (UINT8
) (Divisor
& 0xff));
80 // Switch back to bank 0
82 OutputData
= (UINT8
) ((~DLAB
<< 7) | ((gBreakSet
<< 6) | ((gParity
<< 3) | ((gStop
<< 2) | Data
))));
83 IoWrite8 (gComBase
+ LCR_OFFSET
, OutputData
);
85 return RETURN_SUCCESS
;
89 Common function to initialize UART Serial device and USB Serial device.
98 SerialPortInitialize (
106 return RETURN_SUCCESS
;
110 Write data to serial device.
112 If the buffer is NULL, then return 0;
113 if NumberOfBytes is zero, then return 0.
115 @param Buffer Point of data buffer which need to be writed.
116 @param NumberOfBytes Number of output bytes which are cached in Buffer.
118 @retval 0 Write data failed.
119 @retval !0 Actual number of bytes writed to serial device.
126 IN UINTN NumberOfBytes
132 if (NULL
== Buffer
) {
136 Result
= NumberOfBytes
;
138 while (NumberOfBytes
--) {
140 // Wait for the serial port to be ready.
143 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
144 } while ((Data
& LSR_TXRDY
) == 0);
145 IoWrite8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
), *Buffer
++);
152 Common function to write data to UART Serial device and USB Serial device.
154 @param Buffer Point of data buffer which need to be writed.
155 @param NumberOfBytes Number of output bytes which are cached in Buffer.
162 IN UINTN NumberOfBytes
165 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
166 UARTDbgOut (Buffer
, NumberOfBytes
);
169 return RETURN_SUCCESS
;
173 Read data from serial device and save the datas in buffer.
175 If the buffer is NULL, then return 0;
176 if NumberOfBytes is zero, then return 0.
178 @param Buffer Point of data buffer which need to be writed.
179 @param NumberOfBytes Number of output bytes which are cached in Buffer.
181 @retval 0 Read data failed.
182 @retval !0 Actual number of bytes raed to serial device.
189 IN UINTN NumberOfBytes
195 if (NULL
== Buffer
) {
199 Result
= NumberOfBytes
;
201 while (NumberOfBytes
--) {
203 // Wait for the serial port to be ready.
206 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
207 } while ((Data
& LSR_RXDA
) == 0);
209 *Buffer
++ = IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
));
216 Common function to Read data from UART serial device, USB serial device and save the datas in buffer.
218 @param Buffer Point of data buffer which need to be writed.
219 @param NumberOfBytes Number of output bytes which are cached in Buffer.
226 IN UINTN NumberOfBytes
229 if (FeaturePcdGet (PcdStatusCodeUseIsaSerial
)) {
230 UARTDbgIn (Buffer
, NumberOfBytes
);
233 return RETURN_SUCCESS
;
238 Polls a serial device to see if there is any data waiting to be read.
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.
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.
257 // Read the serial port status.
259 Data
= IoRead8 ((UINT16
) PcdGet64 (PcdSerialRegisterBase
) + LSR_OFFSET
);
261 return (BOOLEAN
) ((Data
& LSR_RXDA
) != 0);