]>
git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c
2 UART Serial Port library instance with empty functions.
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
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.
17 #include <Library/SerialPortLib.h>
18 #include <Library/IoLib.h>
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
37 //---------------------------------------------
38 // UART Register Bit Defines
39 //---------------------------------------------
40 #define LSR_TXRDY 0x20
44 //---------------------------------------------
46 //---------------------------------------------
47 UINT16 gUartBase
= 0x3F8;
55 Initialize the serial device hardware.
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.
61 @retval RETURN_SUCCESS The serial device was initialized.
62 @retval RETURN_DEVICE_ERROR The serail device could not be initialized.
67 SerialPortInitialize (
78 Data
= (UINT8
) (gData
- (UINT8
) 5);
81 // Calculate divisor for baud generator
83 Divisor
= 115200 / gBps
;
86 // Set communications format
88 OutputData
= (UINT8
) ((DLAB
<< 7) | (gBreakSet
<< 6) | (gParity
<< 3) | (gStop
<< 2) | Data
);
89 IoWrite8 ((UINTN
) (gUartBase
+ LCR_OFFSET
), OutputData
);
92 // Configure baud rate
94 IoWrite8 ((UINTN
) (gUartBase
+ BAUD_HIGH_OFFSET
), (UINT8
) (Divisor
>> 8));
95 IoWrite8 ((UINTN
) (gUartBase
+ BAUD_LOW_OFFSET
), (UINT8
) (Divisor
& 0xff));
98 // Switch back to bank 0
100 OutputData
= (UINT8
) ((~DLAB
<< 7) | (gBreakSet
<< 6) | (gParity
<< 3) | (gStop
<< 2) | Data
);
101 IoWrite8 ((UINTN
) (gUartBase
+ LCR_OFFSET
), OutputData
);
103 return RETURN_SUCCESS
;
107 Write data from buffer to serial device.
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.
113 If Buffer is NULL, then ASSERT().
115 If NumberOfBytes is zero, then return 0.
117 @param Buffer Pointer to the data buffer to be written.
118 @param NumberOfBytes Number of bytes to written to the serial device.
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.
129 IN UINTN NumberOfBytes
135 if (Buffer
== NULL
) {
139 Result
= NumberOfBytes
;
141 while (NumberOfBytes
--) {
143 // Wait for the serail port to be ready.
146 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
147 } while ((Data
& LSR_TXRDY
) == 0);
148 IoWrite8 ((UINT16
) gUartBase
, *Buffer
++);
156 Reads data from a serial device into a buffer.
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.
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.
170 IN UINTN NumberOfBytes
176 if (NULL
== Buffer
) {
180 Result
= NumberOfBytes
;
182 while (NumberOfBytes
--) {
184 // Wait for the serail port to be ready.
187 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
188 } while ((Data
& LSR_RXDA
) == 0);
190 *Buffer
++ = IoRead8 ((UINT16
) gUartBase
);
197 Polls a serial device to see if there is any data waiting to be read.
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.
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.
216 // Read the serial port status.
218 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
220 return (BOOLEAN
) ((Data
& LSR_RXDA
) != 0);