]>
git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c
2 UART Serial Port library functions
4 Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>
5 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.
16 #include <Library/IoLib.h>
17 #include <Library/SerialPortLib.h>
19 //---------------------------------------------
20 // UART Register Offsets
21 //---------------------------------------------
22 #define BAUD_LOW_OFFSET 0x00
23 #define BAUD_HIGH_OFFSET 0x01
24 #define IER_OFFSET 0x01
25 #define LCR_SHADOW_OFFSET 0x01
26 #define FCR_SHADOW_OFFSET 0x02
27 #define IR_CONTROL_OFFSET 0x02
28 #define FCR_OFFSET 0x02
29 #define EIR_OFFSET 0x02
30 #define BSR_OFFSET 0x03
31 #define LCR_OFFSET 0x03
32 #define MCR_OFFSET 0x04
33 #define LSR_OFFSET 0x05
34 #define MSR_OFFSET 0x06
36 //---------------------------------------------
37 // UART Register Bit Defines
38 //---------------------------------------------
39 #define LSR_TXRDY 0x20
43 //---------------------------------------------
45 //---------------------------------------------
46 UINT16 gUartBase
= 0x3F8;
54 Initialize the serial device hardware.
56 If no initialization is required, then return RETURN_SUCCESS.
57 If the serial device was successfuly initialized, then return RETURN_SUCCESS.
58 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
60 @retval RETURN_SUCCESS The serial device was initialized.
61 @retval RETURN_DEVICE_ERROR The serail device could not be initialized.
66 SerialPortInitialize (
77 Data
= (UINT8
) (gData
- (UINT8
) 5);
80 // Calculate divisor for baud generator
82 Divisor
= 115200 / gBps
;
85 // Set communications format
87 OutputData
= (UINT8
) ((DLAB
<< 7) | (gBreakSet
<< 6) | (gParity
<< 3) | (gStop
<< 2) | Data
);
88 IoWrite8 ((UINTN
) (gUartBase
+ LCR_OFFSET
), OutputData
);
91 // Configure baud rate
93 IoWrite8 ((UINTN
) (gUartBase
+ BAUD_HIGH_OFFSET
), (UINT8
) (Divisor
>> 8));
94 IoWrite8 ((UINTN
) (gUartBase
+ BAUD_LOW_OFFSET
), (UINT8
) (Divisor
& 0xff));
97 // Switch back to bank 0
99 OutputData
= (UINT8
) ((~DLAB
<< 7) | (gBreakSet
<< 6) | (gParity
<< 3) | (gStop
<< 2) | Data
);
100 IoWrite8 ((UINTN
) (gUartBase
+ LCR_OFFSET
), OutputData
);
102 return RETURN_SUCCESS
;
106 Write data from buffer to serial device.
108 Writes NumberOfBytes data bytes from Buffer to the serial device.
109 The number of bytes actually written to the serial device is returned.
110 If the return value is less than NumberOfBytes, then the write operation failed.
112 If Buffer is NULL, then ASSERT().
114 If NumberOfBytes is zero, then return 0.
116 @param Buffer Pointer to the data buffer to be written.
117 @param NumberOfBytes Number of bytes to written to the serial device.
119 @retval 0 NumberOfBytes is 0.
120 @retval >0 The number of bytes written to the serial device.
121 If this value is less than NumberOfBytes, then the read operation failed.
128 IN UINTN NumberOfBytes
134 if (Buffer
== NULL
) {
138 Result
= NumberOfBytes
;
140 while (NumberOfBytes
--) {
142 // Wait for the serail port to be ready.
145 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
146 } while ((Data
& LSR_TXRDY
) == 0);
147 IoWrite8 ((UINT16
) gUartBase
, *Buffer
++);
155 Reads data from a serial device into a buffer.
157 @param Buffer Pointer to the data buffer to store the data read from the serial device.
158 @param NumberOfBytes Number of bytes to read from the serial device.
160 @retval 0 NumberOfBytes is 0.
161 @retval >0 The number of bytes read from the serial device.
162 If this value is less than NumberOfBytes, then the read operation failed.
169 IN UINTN NumberOfBytes
175 if (NULL
== Buffer
) {
179 Result
= NumberOfBytes
;
181 while (NumberOfBytes
--) {
183 // Wait for the serail port to be ready.
186 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
187 } while ((Data
& LSR_RXDA
) == 0);
189 *Buffer
++ = IoRead8 ((UINT16
) gUartBase
);
196 Polls a serial device to see if there is any data waiting to be read.
198 Polls aserial device to see if there is any data waiting to be read.
199 If there is data waiting to be read from the serial device, then TRUE is returned.
200 If there is no data waiting to be read from the serial device, then FALSE is returned.
202 @retval TRUE Data is waiting to be read from the serial device.
203 @retval FALSE There is no data waiting to be read from the serial device.
215 // Read the serial port status.
217 Data
= IoRead8 ((UINT16
) gUartBase
+ LSR_OFFSET
);
219 return (BOOLEAN
) ((Data
& LSR_RXDA
) != 0);