25#define PCI_BRIDGE_32_BIT_IO_SPACE 0x01
32#define R_UART_BAUD_LOW 0
33#define R_UART_BAUD_HIGH 1
36#define B_UART_FCR_FIFOE BIT0
37#define B_UART_FCR_FIFO64 BIT5
39#define B_UART_LCR_DLAB BIT7
41#define B_UART_MCR_DTRC BIT0
42#define B_UART_MCR_RTS BIT1
44#define B_UART_LSR_RXRDY BIT0
45#define B_UART_LSR_TXRDY BIT5
46#define B_UART_LSR_TEMT BIT6
48#define B_UART_MSR_CTS BIT4
49#define B_UART_MSR_DSR BIT5
50#define B_UART_MSR_RI BIT6
51#define B_UART_MSR_DCD BIT7
59 UINT16 PowerManagementStatusAndControlRegister;
82 if (
PcdGet8 (PcdSerialRegisterAccessWidth) == 32) {
114 if (
PcdGet8 (PcdSerialRegisterAccessWidth) == 32) {
115 return (UINT8)
MmioWrite32 (Base + Offset *
PcdGet32 (PcdSerialRegisterStride), (UINT8)Value);
120 return IoWrite8 (Base + Offset *
PcdGet32 (PcdSerialRegisterStride), Value);
146 CurrentValue =
PciRead16 (PciAddress) & Mask;
147 if (CurrentValue != 0) {
178 CurrentValue =
PciRead32 (PciAddress) & Mask;
179 if (CurrentValue != 0) {
202 UINTN SubordinateBusNumber;
204 UINT32 ParentIoLimit;
205 UINT16 ParentMemoryBase;
206 UINT16 ParentMemoryLimit;
211 UINTN SerialRegisterBase;
213 UINT32 RegisterBaseMask;
224 if (DeviceInfo->Device == 0xff) {
231 ParentMemoryBase = 0 >> 16;
232 ParentMemoryLimit = 0xfff00000 >> 16;
233 ParentIoBase = 0 >> 12;
234 ParentIoLimit = 0xf000 >> 12;
240 for (BusNumber = 0; (DeviceInfo + 1)->Device != 0xff; DeviceInfo++) {
244 PciLibAddress =
PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);
249 BusNumber =
PciRead8 (PciLibAddress + PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);
250 SubordinateBusNumber =
PciRead8 (PciLibAddress + PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET);
251 if ((BusNumber == 0) || (BusNumber > SubordinateBusNumber)) {
265 if (MemoryLimit < MemoryBase) {
272 if ((MemoryBase < ParentMemoryBase) || (MemoryBase > ParentMemoryLimit) || (MemoryLimit > ParentMemoryLimit)) {
276 ParentMemoryBase = MemoryBase;
277 ParentMemoryLimit = MemoryLimit;
280 if ((IoLimit & PCI_BRIDGE_32_BIT_IO_SPACE) == 0) {
281 IoLimit = IoLimit >> 4;
287 if ((IoBase & PCI_BRIDGE_32_BIT_IO_SPACE) == 0) {
288 IoBase = IoBase >> 4;
296 if (IoLimit < IoBase) {
303 if ((IoBase < ParentIoBase) || (IoBase > ParentIoLimit) || (IoLimit > ParentIoLimit)) {
307 ParentIoBase = IoBase;
308 ParentIoLimit = IoLimit;
315 PciLibAddress =
PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);
320 RegisterBaseMask = 0xFFFFFFF0;
321 for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) {
322 SerialRegisterBase =
PciRead32 (PciLibAddress + PCI_BASE_ADDRESSREG_OFFSET + BarIndex * 4);
323 if (
PcdGetBool (PcdSerialUseMmio) && ((SerialRegisterBase & BIT0) == 0)) {
327 RegisterBaseMask = 0xFFFFFFF0;
331 if ((!
PcdGetBool (PcdSerialUseMmio)) && ((SerialRegisterBase & BIT0) != 0)) {
335 RegisterBaseMask = 0xFFFFFFF8;
343 if (BarIndex == PCI_MAX_BAR) {
351 PciLibAddress + PCI_BASE_ADDRESSREG_OFFSET + BarIndex * 4,
352 (UINT32)
PcdGet64 (PcdSerialRegisterBase),
360 if ((((SerialRegisterBase >> 16) & 0xfff0) < ParentMemoryBase) || (((SerialRegisterBase >> 16) & 0xfff0) > ParentMemoryLimit)) {
364 if (((SerialRegisterBase >> 12) < ParentIoBase) || ((SerialRegisterBase >> 12) > ParentIoLimit)) {
373 PciLibAddress + PCI_COMMAND_OFFSET,
380 if (DeviceInfo->PowerManagementStatusAndControlRegister != 0x00) {
381 if ((
PciRead16 (PciLibAddress + DeviceInfo->PowerManagementStatusAndControlRegister) & (BIT0 | BIT1)) != 0x00) {
382 PciAnd16 (PciLibAddress + DeviceInfo->PowerManagementStatusAndControlRegister, (UINT16) ~(BIT0 | BIT1));
399 for (BusNumber = 0; (DeviceInfo + 1)->Device != 0xff; DeviceInfo++) {
403 PciLibAddress =
PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);
409 PciLibAddress + PCI_COMMAND_OFFSET,
416 if (DeviceInfo->PowerManagementStatusAndControlRegister != 0x00) {
417 if ((
PciRead16 (PciLibAddress + DeviceInfo->PowerManagementStatusAndControlRegister) & (BIT0 | BIT1)) != 0x00) {
418 PciAnd16 (PciLibAddress + DeviceInfo->PowerManagementStatusAndControlRegister, (UINT16) ~(BIT0 | BIT1));
422 BusNumber =
PciRead8 (PciLibAddress + PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);
425 return SerialRegisterBase;
438 UINTN SerialRegisterBase
441 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
455 return (BOOLEAN)((
SerialPortReadRegister (SerialRegisterBase, R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) == (B_UART_MSR_DSR | B_UART_MSR_CTS));
469 return (BOOLEAN)((
SerialPortReadRegister (SerialRegisterBase, R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) != (B_UART_MSR_DSR));
493 RETURN_STATUS Status;
494 UINTN SerialRegisterBase;
496 UINT32 CurrentDivisor;
512 Divisor =
PcdGet32 (PcdSerialClockRate) / (
PcdGet32 (PcdSerialBaudRate) * 16);
521 if (SerialRegisterBase == 0) {
537 if (CurrentDivisor != Divisor) {
549 while ((
SerialPortReadRegister (SerialRegisterBase, R_UART_LSR) & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) != (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {
611 UINTN SerialRegisterBase;
616 if (Buffer ==
NULL) {
621 if (SerialRegisterBase == 0) {
625 if (NumberOfBytes == 0) {
633 while ((
SerialPortReadRegister (SerialRegisterBase, R_UART_LSR) & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) != (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {
649 if ((
PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFOE) != 0) {
650 if ((
PcdGet8 (PcdSerialFifoControl) & B_UART_FCR_FIFO64) == 0) {
653 FifoSize =
PcdGet32 (PcdSerialExtendedTxFifoSize);
657 Result = NumberOfBytes;
658 while (NumberOfBytes != 0) {
663 while ((
SerialPortReadRegister (SerialRegisterBase, R_UART_LSR) & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) != (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {
669 for (Index = 0; Index < FifoSize && NumberOfBytes != 0; Index++, NumberOfBytes--, Buffer++) {
704 UINTN SerialRegisterBase;
708 if (
NULL == Buffer) {
713 if (SerialRegisterBase == 0) {
719 for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {
724 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
732 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
765 UINTN SerialRegisterBase;
768 if (SerialRegisterBase == 0) {
776 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
786 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
812 UINTN SerialRegisterBase;
818 if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINAL_READY |
819 EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0)
825 if (SerialRegisterBase == 0) {
833 Mcr &= (~(B_UART_MCR_DTRC | B_UART_MCR_RTS));
835 if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) == EFI_SERIAL_DATA_TERMINAL_READY) {
836 Mcr |= B_UART_MCR_DTRC;
839 if ((Control & EFI_SERIAL_REQUEST_TO_SEND) == EFI_SERIAL_REQUEST_TO_SEND) {
840 Mcr |= B_UART_MCR_RTS;
867 UINTN SerialRegisterBase;
873 if (SerialRegisterBase == 0) {
884 if ((Msr & B_UART_MSR_CTS) == B_UART_MSR_CTS) {
885 *Control |= EFI_SERIAL_CLEAR_TO_SEND;
888 if ((Msr & B_UART_MSR_DSR) == B_UART_MSR_DSR) {
889 *Control |= EFI_SERIAL_DATA_SET_READY;
892 if ((Msr & B_UART_MSR_RI) == B_UART_MSR_RI) {
893 *Control |= EFI_SERIAL_RING_INDICATE;
896 if ((Msr & B_UART_MSR_DCD) == B_UART_MSR_DCD) {
897 *Control |= EFI_SERIAL_CARRIER_DETECT;
905 if ((Mcr & B_UART_MCR_DTRC) == B_UART_MCR_DTRC) {
906 *Control |= EFI_SERIAL_DATA_TERMINAL_READY;
909 if ((Mcr & B_UART_MCR_RTS) == B_UART_MCR_RTS) {
910 *Control |= EFI_SERIAL_REQUEST_TO_SEND;
913 if (
PcdGetBool (PcdSerialUseHardwareFlowControl)) {
914 *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;
922 if ((Lsr & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) == (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {
923 *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;
926 if ((Lsr & B_UART_LSR_RXRDY) == 0) {
927 *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;
969 IN OUT UINT64 *BaudRate,
970 IN OUT UINT32 *ReceiveFifoDepth,
971 IN OUT UINT32 *Timeout,
973 IN OUT UINT8 *DataBits,
977 UINTN SerialRegisterBase;
978 UINT32 SerialBaudRate;
986 if (SerialRegisterBase == 0) {
993 if (*BaudRate == 0) {
994 *BaudRate =
PcdGet32 (PcdSerialBaudRate);
997 SerialBaudRate = (UINT32)*BaudRate;
999 if (*DataBits == 0) {
1000 LcrData = (UINT8)(
PcdGet8 (PcdSerialLineControl) & 0x3);
1001 *DataBits = LcrData + 5;
1003 if ((*DataBits < 5) || (*DataBits > 8)) {
1010 LcrData = (UINT8)(*DataBits - (UINT8)5);
1013 if (*Parity == DefaultParity) {
1014 LcrParity = (UINT8)((
PcdGet8 (PcdSerialLineControl) >> 3) & 0x7);
1015 switch (LcrParity) {
1021 *Parity = EvenParity;
1025 *Parity = OddParity;
1029 *Parity = SpaceParity;
1033 *Parity = MarkParity;
1066 if (*StopBits == DefaultStopBits) {
1067 LcrStop = (UINT8)((
PcdGet8 (PcdSerialLineControl) >> 2) & 0x1);
1070 *StopBits = OneStopBit;
1074 if (*DataBits == 5) {
1075 *StopBits = OneFiveStopBits;
1077 *StopBits = TwoStopBits;
1086 switch (*StopBits) {
1091 case OneFiveStopBits:
1105 Divisor =
PcdGet32 (PcdSerialClockRate) / (SerialBaudRate * 16);
1106 if ((
PcdGet32 (PcdSerialClockRate) % (SerialBaudRate * 16)) >= SerialBaudRate * 8) {
1121 Lcr = (UINT8)((LcrParity << 3) | (LcrStop << 2) | LcrData);
RETURN_STATUS EFIAPI PlatformHookSerialPortInitialize(VOID)
UINT32 SerialPortLibUpdatePciRegister32(UINTN PciAddress, UINT32 Value, UINT32 Mask)
BOOLEAN EFIAPI SerialPortPoll(VOID)
RETURN_STATUS EFIAPI SerialPortSetAttributes(IN OUT UINT64 *BaudRate, IN OUT UINT32 *ReceiveFifoDepth, IN OUT UINT32 *Timeout, IN OUT EFI_PARITY_TYPE *Parity, IN OUT UINT8 *DataBits, IN OUT EFI_STOP_BITS_TYPE *StopBits)
UINT8 SerialPortWriteRegister(UINTN Base, UINTN Offset, UINT8 Value)
UINT8 SerialPortReadRegister(UINTN Base, UINTN Offset)
UINTN EFIAPI SerialPortRead(OUT UINT8 *Buffer, IN UINTN NumberOfBytes)
RETURN_STATUS EFIAPI SerialPortInitialize(VOID)
RETURN_STATUS EFIAPI SerialPortSetControl(IN UINT32 Control)
RETURN_STATUS EFIAPI SerialPortGetControl(OUT UINT32 *Control)
UINTN GetSerialRegisterBase(VOID)
UINT16 SerialPortLibUpdatePciRegister16(UINTN PciAddress, UINT16 Value, UINT16 Mask)
EFI_STATUS EFIAPI BaseSerialPortLib16550(VOID)
UINTN EFIAPI SerialPortWrite(IN UINT8 *Buffer, IN UINTN NumberOfBytes)
BOOLEAN SerialPortWritable(UINTN SerialRegisterBase)
UINT8 EFIAPI IoWrite8(IN UINTN Port, IN UINT8 Value)
UINT8 EFIAPI MmioRead8(IN UINTN Address)
UINT8 EFIAPI MmioWrite8(IN UINTN Address, IN UINT8 Value)
UINT8 EFIAPI IoRead8(IN UINTN Port)
UINT32 EFIAPI MmioRead32(IN UINTN Address)
UINT32 EFIAPI MmioWrite32(IN UINTN Address, IN UINT32 Value)
#define RETURN_ERROR(StatusCode)
#define RETURN_DEVICE_ERROR
#define RETURN_UNSUPPORTED
#define OFFSET_OF(TYPE, Field)
#define RETURN_INVALID_PARAMETER
UINT32 EFIAPI PciRead32(IN UINTN Address)
UINT8 EFIAPI PciRead8(IN UINTN Address)
#define PCI_LIB_ADDRESS(Bus, Device, Function, Register)
UINT16 EFIAPI PciAnd16(IN UINTN Address, IN UINT16 AndData)
UINT32 EFIAPI PciWrite32(IN UINTN Address, IN UINT32 Value)
UINT16 EFIAPI PciOr16(IN UINTN Address, IN UINT16 OrData)
UINT16 EFIAPI PciWrite16(IN UINTN Address, IN UINT16 Value)
UINT16 EFIAPI PciRead16(IN UINTN Address)
#define PcdGet64(TokenName)
#define PcdGet8(TokenName)
#define PcdGet32(TokenName)
#define PcdGetBool(TokenName)
#define PcdGetPtr(TokenName)
#define EFI_PCI_COMMAND_MEMORY_SPACE
0x0002
#define EFI_PCI_COMMAND_IO_SPACE
0x0001