/** @file
Serial driver for standard UARTS on an ISA bus.
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "Serial.h"
//
// ISA Serial Driver Global Variables
//
0xa,
NULL,
};
NULL,
{ // SerialIo
},
{ // SerialMode
},
NULL,
NULL,
{ // UartDevicePath
{
{
(UINT8) (sizeof (UART_DEVICE_PATH)),
}
},
0,
},
NULL,
0, //BaseAddress
{
0,
0,
{ 0 }
},
{
0,
0,
{ 0 }
},
};
/**
Check the device path node whether it's the Flow Control node or not.
@param[in] FlowControl The device path node to be checked.
@retval TRUE It's the Flow Control node.
@retval FALSE It's not.
**/
)
{
return (BOOLEAN) (
);
}
/**
Check the device path node whether it contains Flow Control node or not.
@param[in] DevicePath The device path to be checked.
@retval TRUE It contains the Flow Control node.
@retval FALSE It doesn't.
**/
)
{
while (!IsDevicePathEnd (DevicePath)) {
return TRUE;
}
}
return FALSE;
}
/**
The user Entry Point for module IsaSerial. The user code starts with this function.
@param[in] ImageHandle The firmware allocated handle for the EFI image.
@param[in] SystemTable A pointer to the EFI System Table.
@retval EFI_SUCCESS The entry point is executed successfully.
@retval other Some error occurs when executing this entry point.
**/
)
{
//
// Install driver model protocol(s).
//
);
return Status;
}
/**
Check to see if this driver supports the given controller
@param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param Controller The handle of the controller to test.
@param RemainingDevicePath A pointer to the remaining portion of a device path.
@return EFI_SUCCESS This driver can support the given controller
**/
)
{
//
// Check RemainingDevicePath validation
//
if (RemainingDevicePath != NULL) {
//
// Check if RemainingDevicePath is the End of Device Path Node,
// if yes, go on checking other conditions
//
if (!IsDevicePathEnd (RemainingDevicePath)) {
//
// If RemainingDevicePath isn't the End of Device Path Node,
// check its validation
//
) {
goto Error;
}
goto Error;
}
goto Error;
}
goto Error;
}
goto Error;
}
goto Error;
}
if ((UartNode->DataBits >= 6) && (UartNode->DataBits <= 8) && (UartNode->StopBits == OneFiveStopBits)) {
goto Error;
}
if (IsUartFlowControlNode (FlowControlNode)) {
//
// If the second node is Flow Control Node,
// return error when it request other than hardware flow control.
//
goto Error;
}
}
}
}
//
// Open the IO Abstraction(s) needed to perform the supported test
//
);
if (Status == EFI_ALREADY_STARTED) {
//
// If RemainingDevicePath is NULL or is the End of Device Path Node
//
return EFI_SUCCESS;
}
//
// When the driver has produced device path with flow control node but RemainingDevicePath only contains UART node,
// return unsupported, and vice versa.
//
);
return Status;
}
(VOID **) &DevicePath,
);
}
}
break;
}
}
return Status;
}
return Status;
}
//
// Close the I/O Abstraction(s) used to perform the supported test
//
gBS->CloseProtocol (
);
//
// Open the EFI Device Path protocol needed to perform the supported test
//
(VOID **) &ParentDevicePath,
);
if (Status == EFI_ALREADY_STARTED) {
return EFI_SUCCESS;
}
return Status;
}
//
// Use the ISA I/O Protocol to see if Controller is standard ISA UART that
// can be managed by this driver.
//
goto Error;
}
//
// Close protocol, don't use device path protocol in the Support() function
//
gBS->CloseProtocol (
);
return Status;
}
/**
Start to management the controller passed in
@param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param Controller The handle of the controller to test.
@param RemainingDevicePath A pointer to the remaining portion of a device path.
@return EFI_SUCCESS Driver is started successfully
**/
)
{
SerialDevice = NULL;
//
// Get the Parent Device Path
//
(VOID **) &ParentDevicePath,
);
return Status;
}
//
// Report status code enable the serial
//
);
//
// Grab the IO abstraction we need to get any work done
//
);
goto Error;
}
if (Status == EFI_ALREADY_STARTED) {
//
// If RemainingDevicePath is NULL or is the End of Device Path Node
//
return EFI_SUCCESS;
}
//
// Make sure a child handle does not already exist. This driver can only
// produce one child per serial port.
//
);
return Status;
}
);
);
} else {
}
//
// Clear the bits that are not allowed to pass to SetControl
//
}
}
}
break;
}
}
return Status;
}
if (RemainingDevicePath != NULL) {
if (IsDevicePathEnd (RemainingDevicePath)) {
//
// If RemainingDevicePath is the End of Device Path Node,
// skip enumerate any device and return EFI_SUCESSS
//
return EFI_SUCCESS;
}
}
//
// Initialize the serial device instance
//
if (SerialDevice == NULL) {
goto Error;
}
FlowControl = NULL;
FlowControlMap = 0;
//
// Check if RemainingDevicePath is NULL,
// if yes, use the values from the gSerialDevTempate as no remaining device path was
// passed in.
//
if (RemainingDevicePath != NULL) {
//
// If RemainingDevicePath isn't NULL,
// match the configuration of the RemainingDevicePath. IsHandleSupported()
// already checked to make sure the RemainingDevicePath contains settings
// that we can support.
//
if (IsUartFlowControlNode (FlowControl)) {
} else {
FlowControl = NULL;
}
}
for (Index = 0; SerialDevice->IsaIo->ResourceList->ResourceItem[Index].Type != EfiIsaAcpiResourceEndOfList; Index++) {
SerialDevice->BaseAddress = (UINT16) SerialDevice->IsaIo->ResourceList->ResourceItem[Index].StartRange;
}
}
//
// Report status code the serial present
//
);
if (!IsaSerialPortPresent (SerialDevice)) {
);
goto Error;
}
//
// Build the device path by appending the UART node to the ParentDevicePath.
// The Uart setings are zero here, since SetAttribute() will update them to match
// the default setings.
//
);
//
// Only produce the Flow Control node when remaining device path has it
//
if (FlowControl != NULL) {
if (TempDevicePath != NULL) {
);
}
}
goto Error;
}
//
// Fill in Serial I/O Mode structure based on either the RemainingDevicePath or defaults.
//
//
// Issue a reset to initialize the COM port
//
);
goto Error;
}
//
// Install protocol interfaces for the serial device.
//
);
goto Error;
}
//
// Open For Child Device
//
);
gBS->CloseProtocol (
);
gBS->CloseProtocol (
);
if (SerialDevice != NULL) {
}
}
}
return Status;
}
/**
Disconnect this driver with the controller, uninstall related protocol instance
@param This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
@param Controller The handle of the controller to test.
@param NumberOfChildren Number of child device.
@param ChildHandleBuffer A pointer to the remaining portion of a device path.
@retval EFI_SUCCESS Operation successfully
@retval EFI_DEVICE_ERROR Cannot stop the driver successfully
**/
)
{
(VOID **) &DevicePath
);
//
// Report the status code disable the serial
//
);
//
// Complete all outstanding transactions to Controller.
// Don't allow any new transaction to Controller to be started.
//
if (NumberOfChildren == 0) {
//
// Close the bus driver
//
);
);
return Status;
}
);
);
);
gBS->OpenProtocol (
);
} else {
}
}
}
}
}
if (!AllChildrenStopped) {
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Detect whether specific FIFO is full or not.
@param Fifo A pointer to the Data Structure SERIAL_DEV_FIFO
@return whether specific FIFO is full or not
**/
)
{
return TRUE;
}
return FALSE;
}
/**
Detect whether specific FIFO is empty or not.
@param Fifo A pointer to the Data Structure SERIAL_DEV_FIFO
@return whether specific FIFO is empty or not
**/
)
{
return TRUE;
}
return FALSE;
}
/**
Add data to specific FIFO.
@param Fifo A pointer to the Data Structure SERIAL_DEV_FIFO
@param Data the data added to FIFO
@retval EFI_SUCCESS Add data to specific FIFO successfully
@retval EFI_OUT_OF_RESOURCE Failed to add data because FIFO is already full
**/
)
{
//
// if FIFO full can not add data
//
if (IsaSerialFifoFull (Fifo)) {
return EFI_OUT_OF_RESOURCES;
}
//
// FIFO is not full can add data
//
}
return EFI_SUCCESS;
}
/**
Remove data from specific FIFO.
@param Fifo A pointer to the Data Structure SERIAL_DEV_FIFO
@param Data the data removed from FIFO
@retval EFI_SUCCESS Remove data from specific FIFO successfully
@retval EFI_OUT_OF_RESOURCE Failed to remove data because FIFO is empty
**/
)
{
//
// if FIFO is empty, no data can remove
//
if (IsaSerialFifoEmpty (Fifo)) {
return EFI_OUT_OF_RESOURCES;
}
//
// FIFO is not empty, can remove data
//
}
return EFI_SUCCESS;
}
/**
Reads and writes all avaliable data.
@param SerialDevice The device to flush
@retval EFI_OUT_OF_RESOURCE Failed because software receive FIFO is full. Note, when
this happens, pending writes are not done.
**/
)
{
Data = 0;
//
// Begin the read or write
//
if (SerialDevice->SoftwareLoopbackEnable) {
do {
if (ReceiveFifoFull) {
return EFI_OUT_OF_RESOURCES;
}
}
} else {
//
// For full handshake flow control, tell the peer to send data
// if receive buffer is available.
//
if (SerialDevice->HardwareFlowControl &&
) {
}
do {
//
// Flush incomming data to prevent a an overrun during a long write
//
if (!ReceiveFifoFull) {
if (Lsr.Bits.FIFOe == 1 || Lsr.Bits.Oe == 1 || Lsr.Bits.Pe == 1 || Lsr.Bits.Fe == 1 || Lsr.Bits.Bi == 1) {
);
continue;
}
}
//
// For full handshake flow control, if receive buffer full
// tell the peer to stop sending data.
//
if (SerialDevice->HardwareFlowControl &&
) {
}
continue;
} else {
);
}
}
//
// Do the write
//
//
// Make sure the transmit data will not be missed
//
if (SerialDevice->HardwareFlowControl) {
//
// For half handshake flow control assert RTS before sending.
//
}
//
// Wait for CTS
//
TimeOut = 0;
while ((Msr.Bits.Dcd == 1) && ((Msr.Bits.Cts == 0) ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {
TimeOut++;
if (TimeOut > 5) {
break;
}
}
if ((Msr.Bits.Dcd == 0) || ((Msr.Bits.Cts == 1) ^ FeaturePcdGet(PcdIsaBusSerialUseHalfHandshake))) {
}
//
// For half handshake flow control, tell DCE we are done.
//
}
} else {
}
}
}
return EFI_SUCCESS;
}
//
// Interface Functions
//
/**
Reset serial device.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@retval EFI_SUCCESS Reset successfully
@retval EFI_DEVICE_ERROR Failed to reset
**/
)
{
//
// Report the status code reset the serial
//
);
//
// Make sure DLAB is 0.
//
//
// Turn off all interrupts
//
//
// Disable the FIFO.
//
//
// Turn off loopback and disable device interrupt.
//
//
// Clear the scratch pad register
//
//
// Go set the current attributes
//
This,
);
return EFI_DEVICE_ERROR;
}
//
// Go set the current control bits
//
Control = 0;
if (SerialDevice->HardwareFlowControl) {
}
if (SerialDevice->SoftwareLoopbackEnable) {
}
This,
);
return EFI_DEVICE_ERROR;
}
//
// for 16550A enable FIFO, 16550 disable FIFO
//
//
// Reset the software FIFO
//
//
// Device reset is complete
//
return EFI_SUCCESS;
}
/**
Set new attributes to a serial device.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@param BaudRate The baudrate of the serial device
@param ReceiveFifoDepth The depth of receive FIFO buffer
@param Timeout The request timeout for a single char
@param Parity The type of parity used in serial device
@param DataBits Number of databits used in serial device
@param StopBits Number of stopbits used in serial device
@retval EFI_SUCCESS The new attributes were set
@retval EFI_INVALID_PARAMETERS One or more attributes have an unsupported value
@retval EFI_UNSUPPORTED Data Bits can not set to 5 or 6
@retval EFI_DEVICE_ERROR The serial device is not functioning correctly (no return)
**/
)
{
//
// Check for default settings and fill in actual values.
//
if (BaudRate == 0) {
}
if (ReceiveFifoDepth == 0) {
}
if (Timeout == 0) {
}
if (Parity == DefaultParity) {
}
if (DataBits == 0) {
}
if (StopBits == DefaultStopBits) {
}
//
// 5 and 6 data bits can not be verified on a 16550A UART
// Return EFI_INVALID_PARAMETER if an attempt is made to use these settings.
//
return EFI_INVALID_PARAMETER;
}
//
// Make sure all parameters are valid
//
return EFI_INVALID_PARAMETER;
}
//
// 50,75,110,134,150,300,600,1200,1800,2000,2400,3600,4800,7200,9600,19200,
// 38400,57600,115200
//
if (BaudRate < 75) {
BaudRate = 50;
} else if (BaudRate < 110) {
BaudRate = 75;
} else if (BaudRate < 134) {
BaudRate = 110;
} else if (BaudRate < 150) {
BaudRate = 134;
} else if (BaudRate < 300) {
BaudRate = 150;
} else if (BaudRate < 600) {
BaudRate = 300;
} else if (BaudRate < 1200) {
BaudRate = 600;
} else if (BaudRate < 1800) {
BaudRate = 1200;
} else if (BaudRate < 2000) {
BaudRate = 1800;
} else if (BaudRate < 2400) {
BaudRate = 2000;
} else if (BaudRate < 3600) {
BaudRate = 2400;
} else if (BaudRate < 4800) {
BaudRate = 3600;
} else if (BaudRate < 7200) {
BaudRate = 4800;
} else if (BaudRate < 9600) {
BaudRate = 7200;
} else if (BaudRate < 19200) {
BaudRate = 9600;
} else if (BaudRate < 38400) {
BaudRate = 19200;
} else if (BaudRate < 57600) {
BaudRate = 38400;
} else if (BaudRate < 115200) {
BaudRate = 57600;
} else if (BaudRate <= SERIAL_PORT_MAX_BAUD_RATE) {
BaudRate = 115200;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
//
// for DataBits = 6,7,8, StopBits can not set OneFiveStopBits
//
return EFI_INVALID_PARAMETER;
}
//
// Compute divisor use to program the baud rate using a round determination
//
);
if (Remained != 0) {
Divisor += 1;
}
return EFI_INVALID_PARAMETER;
}
//
// Compute the actual baud rate that the serial port will be programmed for.
//
//
// Put serial port on Divisor Latch Mode
//
//
// Write the divisor to the serial port
//
//
// Put serial port back in normal mode and set remaining attributes.
//
switch (Parity) {
case NoParity:
break;
case EvenParity:
break;
case OddParity:
break;
case SpaceParity:
break;
case MarkParity:
break;
default:
break;
}
switch (StopBits) {
case OneStopBit:
break;
case OneFiveStopBits:
case TwoStopBits:
break;
default:
break;
}
//
// DataBits
//
//
// Set the Serial I/O mode
//
//
// See if Device Path Node has actually changed
//
) {
return EFI_SUCCESS;
}
//
// Update the device path
//
Uart = (UART_DEVICE_PATH *) (
);
);
}
return Status;
}
/**
Set Control Bits.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@param Control Control bits that can be settable
@retval EFI_SUCCESS New Control bits were set successfully
@retval EFI_UNSUPPORTED The Control bits wanted to set are not supported
**/
)
{
//
// The control bits that can be set are :
// EFI_SERIAL_DATA_TERMINAL_READY: 0x0001 // WO
// EFI_SERIAL_REQUEST_TO_SEND: 0x0002 // WO
// EFI_SERIAL_HARDWARE_LOOPBACK_ENABLE: 0x1000 // RW
// EFI_SERIAL_SOFTWARE_LOOPBACK_ENABLE: 0x2000 // RW
// EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE: 0x4000 // RW
//
//
// first determine the parameter is invalid
//
EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0) {
return EFI_UNSUPPORTED;
}
}
}
}
if ((Control & EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) == EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE) {
}
}
+ sizeof (UART_DEVICE_PATH)
);
if (IsUartFlowControlNode (FlowControl) &&
((ReadUnaligned32 (&FlowControl->FlowControlMap) == UART_FLOW_CONTROL_HARDWARE) ^ SerialDevice->HardwareFlowControl)) {
//
// Flow Control setting is changed, need to reinstall device path protocol
//
WriteUnaligned32 (&FlowControl->FlowControlMap, SerialDevice->HardwareFlowControl ? UART_FLOW_CONTROL_HARDWARE : 0);
);
}
}
return Status;
}
/**
Get ControlBits.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@param Control Control signals of the serial device
@retval EFI_SUCCESS Get Control signals successfully
**/
)
{
*Control = 0;
//
// Read the Modem Status Register
//
}
}
}
}
//
// Read the Modem Control Register
//
}
}
}
if (SerialDevice->HardwareFlowControl) {
}
//
// See if the Transmit FIFO is empty
//
}
//
// See if the Receive FIFO is empty.
//
}
if (SerialDevice->SoftwareLoopbackEnable) {
}
return EFI_SUCCESS;
}
/**
Write the specified number of bytes to serial device.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@param BufferSize On input the size of Buffer, on output the amount of
data actually written
@param Buffer The buffer of data to write
@retval EFI_SUCCESS The data were written successfully
@retval EFI_DEVICE_ERROR The device reported an error
@retval EFI_TIMEOUT The write operation was stopped due to timeout
**/
)
{
Elapsed = 0;
ActualWrite = 0;
if (*BufferSize == 0) {
return EFI_SUCCESS;
}
);
return EFI_DEVICE_ERROR;
}
//
// Compute the number of bits in a single character. This is a start bit,
// followed by the number of data bits, followed by the number of stop bits.
// The number of stop bits is specified by an enumeration that includes
// support for 1.5 stop bits. Treat 1.5 stop bits as 2 stop bits.
//
1 +
//
// Compute the timeout in microseconds to wait for a single byte to be
// transmitted. The Mode structure contans a Timeout field that is the
// maximum time to transmit or receive a character. However, many UARTs
// have a FIFO for transmits, so the time required to add one new character
// to the transmit FIFO may be the time required to flush a full FIFO. If
// the Timeout in the Mode structure is smaller than the time required to
// flush a full FIFO at the current baud rate, then use a timeout value that
// is required to flush a full transmit FIFO.
//
)
);
while (IsaSerialReceiveTransmit (SerialDevice) != EFI_SUCCESS || !IsaSerialFifoEmpty (&SerialDevice->Transmit)) {
//
// Unsuccessful write so check if timeout has expired, if not,
// stall for a bit, increment time elapsed, and try again
//
return EFI_TIMEOUT;
}
}
ActualWrite++;
//
// Successful write so reset timeout
//
Elapsed = 0;
}
return EFI_SUCCESS;
}
/**
Read the specified number of bytes from serial device.
@param This Pointer to EFI_SERIAL_IO_PROTOCOL
@param BufferSize On input the size of Buffer, on output the amount of
data returned in buffer
@param Buffer The buffer to return the data into
@retval EFI_SUCCESS The data were read successfully
@retval EFI_DEVICE_ERROR The device reported an error
@retval EFI_TIMEOUT The read operation was stopped due to timeout
**/
)
{
Elapsed = 0;
if (*BufferSize == 0) {
return EFI_SUCCESS;
}
return EFI_DEVICE_ERROR;
}
*BufferSize = 0;
);
return EFI_DEVICE_ERROR;
}
//
// Unsuccessful read so check if timeout has expired, if not,
// stall for a bit, increment time elapsed, and try again
// Need this time out to get conspliter to work.
//
*BufferSize = Index;
return EFI_TIMEOUT;
}
if (Status == EFI_DEVICE_ERROR) {
*BufferSize = Index;
return EFI_DEVICE_ERROR;
}
}
//
// Successful read so reset timeout
//
Elapsed = 0;
}
return EFI_SUCCESS;
}
/**
Use scratchpad register to test if this serial port is present.
@param SerialDevice Pointer to serial device structure
@return if this serial port is present
**/
)
{
//
// Save SCR reg
//
}
}
//
// Restore SCR
//
return Status;
}
/**
Use IsaIo protocol to read serial port.
@param IsaIo Pointer to EFI_ISA_IO_PROTOCOL instance
@param BaseAddress Serial port register group base address
@param Offset Offset in register group
@return Data read from serial port
**/
)
{
//
// Use IsaIo to access IO
//
1,
&Data
);
return Data;
}
/**
Use IsaIo protocol to write serial port.
@param IsaIo Pointer to EFI_ISA_IO_PROTOCOL instance
@param BaseAddress Serial port register group base address
@param Offset Offset in register group
@param Data data which is to be written to some serial port register
**/
)
{
//
// Use IsaIo to access IO
//
1,
&Data
);
}