Refactor COM port support, to get rid of global variables in library
This commit is contained in:
parent
91e8a86ee2
commit
906e09fd9f
@ -68,8 +68,9 @@
|
|||||||
/* PIC vector definitions */
|
/* PIC vector definitions */
|
||||||
#define PIC1_VECTOR_SPURIOUS 0x37
|
#define PIC1_VECTOR_SPURIOUS 0x37
|
||||||
|
|
||||||
/* Serial port I/O addresses */
|
/* Serial ports information */
|
||||||
#define COMPORT_ADDRESSES {0x000, 0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8}
|
#define COMPORT_ADDRESS {0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8}
|
||||||
|
#define COMPORT_COUNT 8
|
||||||
|
|
||||||
/* Initial stall factor */
|
/* Initial stall factor */
|
||||||
#define INITIAL_STALL_FACTOR 100
|
#define INITIAL_STALL_FACTOR 100
|
||||||
|
@ -23,7 +23,6 @@ HlComPortPutByte(IN PCPPORT Port,
|
|||||||
XTCDECL
|
XTCDECL
|
||||||
XTSTATUS
|
XTSTATUS
|
||||||
HlInitializeComPort(IN OUT PCPPORT Port,
|
HlInitializeComPort(IN OUT PCPPORT Port,
|
||||||
IN ULONG PortNumber,
|
|
||||||
IN PUCHAR PortAddress,
|
IN PUCHAR PortAddress,
|
||||||
IN ULONG BaudRate);
|
IN ULONG BaudRate);
|
||||||
|
|
||||||
|
@ -75,8 +75,9 @@
|
|||||||
/* PIC vector definitions */
|
/* PIC vector definitions */
|
||||||
#define PIC1_VECTOR_SPURIOUS 0x37
|
#define PIC1_VECTOR_SPURIOUS 0x37
|
||||||
|
|
||||||
/* Serial port I/O addresses */
|
/* Serial ports information */
|
||||||
#define COMPORT_ADDRESSES {0x000, 0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8}
|
#define COMPORT_ADDRESS {0x3F8, 0x2F8, 0x3E8, 0x2E8, 0x5F8, 0x4F8, 0x5E8, 0x4E8}
|
||||||
|
#define COMPORT_COUNT 8
|
||||||
|
|
||||||
/* Initial stall factor */
|
/* Initial stall factor */
|
||||||
#define INITIAL_STALL_FACTOR 100
|
#define INITIAL_STALL_FACTOR 100
|
||||||
|
@ -232,18 +232,35 @@ BlpInitializeSerialPort(IN ULONG PortNumber,
|
|||||||
EFI_STATUS EfiStatus;
|
EFI_STATUS EfiStatus;
|
||||||
XTSTATUS Status;
|
XTSTATUS Status;
|
||||||
|
|
||||||
/* Print debug message depending on port settings */
|
/* Check if custom COM port address supplied */
|
||||||
if(PortAddress)
|
if(!PortAddress)
|
||||||
{
|
{
|
||||||
BlConsolePrint(L"Initializing serial console at COM port address: 0x%lX\n", PortAddress);
|
/* We support only a pre-defined number of ports */
|
||||||
|
if(PortNumber > COMPORT_COUNT)
|
||||||
|
{
|
||||||
|
/* Fail if wrong/unsupported port used */
|
||||||
|
return STATUS_INVALID_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if serial port is set */
|
||||||
|
if(PortNumber == 0)
|
||||||
|
{
|
||||||
|
/* Use COM1 by default */
|
||||||
|
PortNumber = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set custom port address based on the port number and print debug message */
|
||||||
|
PortAddress = BlComPortList[PortNumber - 1];
|
||||||
|
BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BlConsolePrint(L"Initializing serial console at port COM%d\n", PortNumber);
|
/* Custom port address supplied, print debug message */
|
||||||
|
BlConsolePrint(L"Initializing serial console at COM port address: 0x%lX\n", PortAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize COM port */
|
/* Initialize COM port */
|
||||||
Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate);
|
Status = HlInitializeComPort(&BlpStatus.SerialPort, UlongToPtr(PortAddress), BaudRate);
|
||||||
|
|
||||||
/* Port not found under supplied address */
|
/* Port not found under supplied address */
|
||||||
if(Status == STATUS_NOT_FOUND && PortAddress)
|
if(Status == STATUS_NOT_FOUND && PortAddress)
|
||||||
@ -254,7 +271,7 @@ BlpInitializeSerialPort(IN ULONG PortNumber,
|
|||||||
{
|
{
|
||||||
/* Try to reinitialize COM port */
|
/* Try to reinitialize COM port */
|
||||||
BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n");
|
BlConsolePrint(L"Enabled I/O space access for all PCI(E) serial controllers found\n");
|
||||||
Status = HlInitializeComPort(&BlpStatus.SerialPort, PortNumber, UlongToPtr(PortAddress), BaudRate);
|
Status = HlInitializeComPort(&BlpStatus.SerialPort, UlongToPtr(PortAddress), BaudRate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,9 @@
|
|||||||
/* XT Boot Loader registered boot protocol list */
|
/* XT Boot Loader registered boot protocol list */
|
||||||
LIST_ENTRY BlpBootProtocols;
|
LIST_ENTRY BlpBootProtocols;
|
||||||
|
|
||||||
|
/* XT Boot Loader serial ports list */
|
||||||
|
ULONG BlComPortList[COMPORT_COUNT] = COMPORT_ADDRESS;
|
||||||
|
|
||||||
/* XT Boot Loader configuration list */
|
/* XT Boot Loader configuration list */
|
||||||
LIST_ENTRY BlpConfig;
|
LIST_ENTRY BlpConfig;
|
||||||
|
|
||||||
|
@ -15,6 +15,9 @@
|
|||||||
/* XT Boot Loader registered boot protocol list */
|
/* XT Boot Loader registered boot protocol list */
|
||||||
EXTERN LIST_ENTRY BlpBootProtocols;
|
EXTERN LIST_ENTRY BlpBootProtocols;
|
||||||
|
|
||||||
|
/* XT Boot Loader serial ports list */
|
||||||
|
EXTERN ULONG BlComPortList[COMPORT_COUNT];
|
||||||
|
|
||||||
/* XT Boot Loader configuration list */
|
/* XT Boot Loader configuration list */
|
||||||
EXTERN LIST_ENTRY BlpConfig;
|
EXTERN LIST_ENTRY BlpConfig;
|
||||||
|
|
||||||
|
@ -9,9 +9,6 @@
|
|||||||
#include <xtos.h>
|
#include <xtos.h>
|
||||||
|
|
||||||
|
|
||||||
/* COM port I/O addresses */
|
|
||||||
ULONG ComPortAddress[] = COMPORT_ADDRESSES;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This routine gets a byte from serial port.
|
* This routine gets a byte from serial port.
|
||||||
*
|
*
|
||||||
@ -212,37 +209,16 @@ HlComPortReadLsr(IN PCPPORT Port,
|
|||||||
XTCDECL
|
XTCDECL
|
||||||
XTSTATUS
|
XTSTATUS
|
||||||
HlInitializeComPort(IN OUT PCPPORT Port,
|
HlInitializeComPort(IN OUT PCPPORT Port,
|
||||||
IN ULONG PortNumber,
|
|
||||||
IN PUCHAR PortAddress,
|
IN PUCHAR PortAddress,
|
||||||
IN ULONG BaudRate)
|
IN ULONG BaudRate)
|
||||||
{
|
{
|
||||||
USHORT Flags = 0;
|
USHORT Flags;
|
||||||
UCHAR Byte = 0;
|
UCHAR Byte;
|
||||||
PUCHAR Address;
|
|
||||||
ULONG Mode;
|
ULONG Mode;
|
||||||
|
|
||||||
/* We support only a pre-defined number of ports */
|
/* Initialize variables */
|
||||||
if(PortNumber > ARRAY_SIZE(ComPortAddress))
|
Byte = 0;
|
||||||
{
|
Flags = 0;
|
||||||
/* Fail if wrong/unsupported port used */
|
|
||||||
return STATUS_INVALID_PARAMETER;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if serial port is set */
|
|
||||||
if(PortNumber == 0)
|
|
||||||
{
|
|
||||||
/* Check if port address supplied instead */
|
|
||||||
if(PortAddress)
|
|
||||||
{
|
|
||||||
/* Set custom port address */
|
|
||||||
ComPortAddress[PortNumber] = PtrToUlong(PortAddress);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Use COM1 by default */
|
|
||||||
PortNumber = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if baud rate is set */
|
/* Check if baud rate is set */
|
||||||
if(BaudRate == 0)
|
if(BaudRate == 0)
|
||||||
@ -252,11 +228,8 @@ HlInitializeComPort(IN OUT PCPPORT Port,
|
|||||||
Flags |= COMPORT_FLAG_DBR;
|
Flags |= COMPORT_FLAG_DBR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store COM pointer */
|
|
||||||
Address = UlongToPtr(ComPortAddress[PortNumber]);
|
|
||||||
|
|
||||||
/* Check whether this port is not already initialized */
|
/* Check whether this port is not already initialized */
|
||||||
if((Port->Address == Address) && (Port->Baud == BaudRate))
|
if((Port->Address == PortAddress) && (Port->Baud == BaudRate))
|
||||||
{
|
{
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -265,8 +238,8 @@ HlInitializeComPort(IN OUT PCPPORT Port,
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
/* Check whether the 16450/16550 scratch register exists */
|
/* Check whether the 16450/16550 scratch register exists */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_SR), Byte);
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_SR), Byte);
|
||||||
if(HlIoPortInByte(PtrToUshort(Address + (ULONG)COMPORT_REG_SR)) != Byte)
|
if(HlIoPortInByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_SR)) != Byte)
|
||||||
{
|
{
|
||||||
return STATUS_NOT_FOUND;
|
return STATUS_NOT_FOUND;
|
||||||
}
|
}
|
||||||
@ -274,40 +247,40 @@ HlInitializeComPort(IN OUT PCPPORT Port,
|
|||||||
while(++Byte != 0);
|
while(++Byte != 0);
|
||||||
|
|
||||||
/* Disable interrupts */
|
/* Disable interrupts */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LSR_DIS);
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR), COMPORT_LSR_DIS);
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_IER), COMPORT_LSR_DIS);
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_IER), COMPORT_LSR_DIS);
|
||||||
|
|
||||||
/* Enable Divisor Latch Access Bit (DLAB) */
|
/* Enable Divisor Latch Access Bit (DLAB) */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_DLAB);
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR), COMPORT_LCR_DLAB);
|
||||||
|
|
||||||
/* Set baud rate */
|
/* Set baud rate */
|
||||||
Mode = COMPORT_CLOCK_RATE / BaudRate;
|
Mode = COMPORT_CLOCK_RATE / BaudRate;
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLL), (UCHAR)(Mode & 0xFF));
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_DIV_DLL), (UCHAR)(Mode & 0xFF));
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_DIV_DLM), (UCHAR)((Mode >> 8) & 0xFF));
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_DIV_DLM), (UCHAR)((Mode >> 8) & 0xFF));
|
||||||
|
|
||||||
/* Set 8 data bits, 1 stop bits, no parity (8n1) */
|
/* Set 8 data bits, 1 stop bits, no parity (8n1) */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_LCR),
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_LCR),
|
||||||
COMPORT_LCR_8DATA | COMPORT_LCR_1STOP | COMPORT_LCR_PARN);
|
COMPORT_LCR_8DATA | COMPORT_LCR_1STOP | COMPORT_LCR_PARN);
|
||||||
|
|
||||||
/* Enable DTR, RTS and OUT2 */
|
/* Enable DTR, RTS and OUT2 */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR),
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_MCR),
|
||||||
COMPORT_MCR_DTR | COMPORT_MCR_RTS | COMPORT_MCR_OUT2);
|
COMPORT_MCR_DTR | COMPORT_MCR_RTS | COMPORT_MCR_OUT2);
|
||||||
|
|
||||||
/* Enable FIFO */
|
/* Enable FIFO */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_FCR),
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_FCR),
|
||||||
COMPORT_FCR_ENABLE | COMPORT_FCR_RCVR_RESET | COMPORT_FCR_TXMT_RESET);
|
COMPORT_FCR_ENABLE | COMPORT_FCR_RCVR_RESET | COMPORT_FCR_TXMT_RESET);
|
||||||
|
|
||||||
/* Mark port as fully initialized */
|
/* Mark port as fully initialized */
|
||||||
Flags |= COMPORT_FLAG_INIT;
|
Flags |= COMPORT_FLAG_INIT;
|
||||||
|
|
||||||
/* Make sure port works in Normal Operation Mode (NOM) */
|
/* Make sure port works in Normal Operation Mode (NOM) */
|
||||||
HlIoPortOutByte(PtrToUshort(Address + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM);
|
HlIoPortOutByte(PtrToUshort(PortAddress + (ULONG)COMPORT_REG_MCR), COMPORT_MCR_NOM);
|
||||||
|
|
||||||
/* Read junk data out of the Receive Buffer Register (RBR) */
|
/* Read junk data out of the Receive Buffer Register (RBR) */
|
||||||
HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_RBR));
|
HlIoPortInByte(PtrToUshort(Port->Address + (ULONG)COMPORT_REG_RBR));
|
||||||
|
|
||||||
/* Store port details */
|
/* Store port details */
|
||||||
Port->Address = Address;
|
Port->Address = PortAddress;
|
||||||
Port->Baud = BaudRate;
|
Port->Baud = BaudRate;
|
||||||
Port->Flags = Flags;
|
Port->Flags = Flags;
|
||||||
|
|
||||||
|
@ -56,7 +56,6 @@ HlGetRunLevel(VOID);
|
|||||||
XTCDECL
|
XTCDECL
|
||||||
XTSTATUS
|
XTSTATUS
|
||||||
HlInitializeComPort(IN OUT PCPPORT Port,
|
HlInitializeComPort(IN OUT PCPPORT Port,
|
||||||
IN ULONG PortNumber,
|
|
||||||
IN PUCHAR PortAddress,
|
IN PUCHAR PortAddress,
|
||||||
IN ULONG BaudRate);
|
IN ULONG BaudRate);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user