]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPortLib16550.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Library / BaseSerialPortLib16550 / BaseSerialPortLib16550.c
index 190740c0bb7363652525d8db1e05aac1e2e5d187..34df34d9c60b0aa287ec31f7ff2646841f2fe747 100644 (file)
@@ -1,14 +1,11 @@
 /** @file\r
   16550 UART Serial Port library functions\r
 \r
-  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
+  (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2018, AMD Incorporated. All rights reserved.<BR>\r
 \r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 //\r
 // 16550 UART register offsets and bitfields\r
 //\r
-#define R_UART_RXBUF          0\r
-#define R_UART_TXBUF          0\r
-#define R_UART_BAUD_LOW       0\r
-#define R_UART_BAUD_HIGH      1\r
+#define R_UART_RXBUF          0   // LCR_DLAB = 0\r
+#define R_UART_TXBUF          0   // LCR_DLAB = 0\r
+#define R_UART_BAUD_LOW       0   // LCR_DLAB = 1\r
+#define R_UART_BAUD_HIGH      1   // LCR_DLAB = 1\r
+#define R_UART_IER            1   // LCR_DLAB = 0\r
 #define R_UART_FCR            2\r
 #define   B_UART_FCR_FIFOE    BIT0\r
 #define   B_UART_FCR_FIFO64   BIT5\r
 #define R_UART_LCR            3\r
 #define   B_UART_LCR_DLAB     BIT7\r
 #define R_UART_MCR            4\r
+#define   B_UART_MCR_DTRC     BIT0\r
 #define   B_UART_MCR_RTS      BIT1\r
 #define R_UART_LSR            5\r
 #define   B_UART_LSR_RXRDY    BIT0\r
@@ -47,6 +46,8 @@
 #define R_UART_MSR            6\r
 #define   B_UART_MSR_CTS      BIT4\r
 #define   B_UART_MSR_DSR      BIT5\r
+#define   B_UART_MSR_RI       BIT6\r
+#define   B_UART_MSR_DCD      BIT7\r
 \r
 //\r
 // 4-byte structure for each PCI node in PcdSerialPciDeviceInfo\r
@@ -58,11 +59,12 @@ typedef struct {
 } PCI_UART_DEVICE_INFO;\r
 \r
 /**\r
-  Read an 8-bit 16550 register.  If PcdSerialUseMmio is TRUE, then the value is read from \r
+  Read an 8-bit 16550 register.  If PcdSerialUseMmio is TRUE, then the value is read from\r
   MMIO space.  If PcdSerialUseMmio is FALSE, then the value is read from I/O space.  The\r
-  parameter Offset is added to the base address of the 16550 registers that is specified \r
-  by PcdSerialRegisterBase. \r
-  \r
+  parameter Offset is added to the base address of the 16550 registers that is specified\r
+  by PcdSerialRegisterBase.\r
+\r
+  @param  Base    The base address register of UART device.\r
   @param  Offset  The offset of the 16550 register to read.\r
 \r
   @return The value read from the 16550 register.\r
@@ -75,18 +77,19 @@ SerialPortReadRegister (
   )\r
 {\r
   if (PcdGetBool (PcdSerialUseMmio)) {\r
-    return MmioRead8 (Base + Offset);\r
+    return MmioRead8 (Base + Offset * PcdGet32 (PcdSerialRegisterStride));\r
   } else {\r
-    return IoRead8 (Base + Offset);\r
+    return IoRead8 (Base + Offset * PcdGet32 (PcdSerialRegisterStride));\r
   }\r
 }\r
 \r
 /**\r
   Write an 8-bit 16550 register.  If PcdSerialUseMmio is TRUE, then the value is written to\r
   MMIO space.  If PcdSerialUseMmio is FALSE, then the value is written to I/O space.  The\r
-  parameter Offset is added to the base address of the 16550 registers that is specified \r
-  by PcdSerialRegisterBase. \r
-  \r
+  parameter Offset is added to the base address of the 16550 registers that is specified\r
+  by PcdSerialRegisterBase.\r
+\r
+  @param  Base    The base address register of UART device.\r
   @param  Offset  The offset of the 16550 register to write.\r
   @param  Value   The value to write to the 16550 register specified by Offset.\r
 \r
@@ -101,18 +104,18 @@ SerialPortWriteRegister (
   )\r
 {\r
   if (PcdGetBool (PcdSerialUseMmio)) {\r
-    return MmioWrite8 (Base + Offset, Value);\r
+    return MmioWrite8 (Base + Offset * PcdGet32 (PcdSerialRegisterStride), Value);\r
   } else {\r
-    return IoWrite8 (Base + Offset, Value);\r
+    return IoWrite8 (Base + Offset * PcdGet32 (PcdSerialRegisterStride), Value);\r
   }\r
 }\r
 \r
 /**\r
-  Update the value of an 16-bit PCI configuration register in a PCI device.  If the  \r
-  PCI Configuration register specified by PciAddress is already programmed with a \r
-  non-zero value, then return the current value.  Otherwise update the PCI configuration \r
+  Update the value of an 16-bit PCI configuration register in a PCI device.  If the\r
+  PCI Configuration register specified by PciAddress is already programmed with a\r
+  non-zero value, then return the current value.  Otherwise update the PCI configuration\r
   register specified by PciAddress with the value specified by Value and return the\r
-  value programmed into the PCI configuration register.  All values must be masked \r
+  value programmed into the PCI configuration register.  All values must be masked\r
   using the bitmask specified by Mask.\r
 \r
   @param  PciAddress  PCI Library address of the PCI Configuration register to update.\r
@@ -128,7 +131,7 @@ SerialPortLibUpdatePciRegister16 (
   )\r
 {\r
   UINT16  CurrentValue;\r
-  \r
+\r
   CurrentValue = PciRead16 (PciAddress) & Mask;\r
   if (CurrentValue != 0) {\r
     return CurrentValue;\r
@@ -137,11 +140,11 @@ SerialPortLibUpdatePciRegister16 (
 }\r
 \r
 /**\r
-  Update the value of an 32-bit PCI configuration register in a PCI device.  If the  \r
-  PCI Configuration register specified by PciAddress is already programmed with a \r
-  non-zero value, then return the current value.  Otherwise update the PCI configuration \r
+  Update the value of an 32-bit PCI configuration register in a PCI device.  If the\r
+  PCI Configuration register specified by PciAddress is already programmed with a\r
+  non-zero value, then return the current value.  Otherwise update the PCI configuration\r
   register specified by PciAddress with the value specified by Value and return the\r
-  value programmed into the PCI configuration register.  All values must be masked \r
+  value programmed into the PCI configuration register.  All values must be masked\r
   using the bitmask specified by Mask.\r
 \r
   @param  PciAddress  PCI Library address of the PCI Configuration register to update.\r
@@ -159,7 +162,7 @@ SerialPortLibUpdatePciRegister32 (
   )\r
 {\r
   UINT32  CurrentValue;\r
-  \r
+\r
   CurrentValue = PciRead32 (PciAddress) & Mask;\r
   if (CurrentValue != 0) {\r
     return CurrentValue;\r
@@ -168,12 +171,12 @@ SerialPortLibUpdatePciRegister32 (
 }\r
 \r
 /**\r
-  Retrieve the I/O or MMIO base address register for the PCI UART device. \r
-  \r
-  This function assumes Root Bus Numer is Zero, and enables I/O and MMIO in PCI UART \r
-  Device if they are not already enabled. \r
-  \r
-  @return  The base address register of the PCI UART device.\r
+  Retrieve the I/O or MMIO base address register for the PCI UART device.\r
+\r
+  This function assumes Root Bus Numer is Zero, and enables I/O and MMIO in PCI UART\r
+  Device if they are not already enabled.\r
+\r
+  @return  The base address register of the UART device.\r
 \r
 **/\r
 UINTN\r
@@ -182,7 +185,6 @@ GetSerialRegisterBase (
   )\r
 {\r
   UINTN                 PciLibAddress;\r
-  UINTN                 PrimaryBusNumber;\r
   UINTN                 BusNumber;\r
   UINTN                 SubordinateBusNumber;\r
   UINT32                ParentIoBase;\r
@@ -202,10 +204,10 @@ GetSerialRegisterBase (
   // Get PCI Device Info\r
   //\r
   DeviceInfo = (PCI_UART_DEVICE_INFO *) PcdGetPtr (PcdSerialPciDeviceInfo);\r
-  \r
+\r
   //\r
   // If PCI Device Info is empty, then assume fixed address UART and return PcdSerialRegisterBase\r
-  //  \r
+  //\r
   if (DeviceInfo->Device == 0xff) {\r
     return (UINTN)PcdGet64 (PcdSerialRegisterBase);\r
   }\r
@@ -217,21 +219,20 @@ GetSerialRegisterBase (
   ParentMemoryLimit = 0xfff00000 >> 16;\r
   ParentIoBase      = 0 >> 12;\r
   ParentIoLimit     = 0xf000 >> 12;\r
-  \r
+\r
   //\r
   // Enable I/O and MMIO in PCI Bridge\r
-  // Assume Root Bus Numer is Zero. \r
+  // Assume Root Bus Numer is Zero.\r
   //\r
   for (BusNumber = 0; (DeviceInfo + 1)->Device != 0xff; DeviceInfo++) {\r
     //\r
     // Compute PCI Lib Address to PCI to PCI Bridge\r
     //\r
     PciLibAddress = PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);\r
-    \r
+\r
     //\r
     // Retrieve and verify the bus numbers in the PCI to PCI Bridge\r
     //\r
-    PrimaryBusNumber     = PciRead8 (PciLibAddress + PCI_BRIDGE_PRIMARY_BUS_REGISTER_OFFSET);\r
     BusNumber            = PciRead8 (PciLibAddress + PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);\r
     SubordinateBusNumber = PciRead8 (PciLibAddress + PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET);\r
     if (BusNumber == 0 || BusNumber > SubordinateBusNumber) {\r
@@ -242,8 +243,8 @@ GetSerialRegisterBase (
     // Retrieve and verify the I/O or MMIO decode window in the PCI to PCI Bridge\r
     //\r
     if (PcdGetBool (PcdSerialUseMmio)) {\r
-      MemoryLimit = PciRead16 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, MemoryLimit)) & 0xfff0;\r
-      MemoryBase  = PciRead16 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, MemoryBase))  & 0xfff0;\r
+      MemoryLimit = PciRead16 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.MemoryLimit)) & 0xfff0;\r
+      MemoryBase  = PciRead16 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.MemoryBase))  & 0xfff0;\r
 \r
       //\r
       // If PCI Bridge MMIO window is disabled, then return 0\r
@@ -251,39 +252,39 @@ GetSerialRegisterBase (
       if (MemoryLimit < MemoryBase) {\r
         return 0;\r
       }\r
-      \r
+\r
       //\r
       // If PCI Bridge MMIO window is not in the address range decoded by the parent PCI Bridge, then return 0\r
-      //  \r
+      //\r
       if (MemoryBase < ParentMemoryBase || MemoryBase > ParentMemoryLimit || MemoryLimit > ParentMemoryLimit) {\r
         return 0;\r
       }\r
       ParentMemoryBase  = MemoryBase;\r
       ParentMemoryLimit = MemoryLimit;\r
     } else {\r
-      IoLimit = PciRead8 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, IoLimit));\r
+      IoLimit = PciRead8 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.IoLimit));\r
       if ((IoLimit & PCI_BRIDGE_32_BIT_IO_SPACE ) == 0) {\r
         IoLimit = IoLimit >> 4;\r
       } else {\r
-        IoLimit = (PciRead16 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, IoLimitUpper16)) << 4) | (IoLimit >> 4);\r
+        IoLimit = (PciRead16 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.IoLimitUpper16)) << 4) | (IoLimit >> 4);\r
       }\r
-      IoBase = PciRead8 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, IoBase));\r
+      IoBase = PciRead8 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.IoBase));\r
       if ((IoBase & PCI_BRIDGE_32_BIT_IO_SPACE ) == 0) {\r
         IoBase = IoBase >> 4;\r
       } else {\r
-        IoBase = (PciRead16 (PciLibAddress + OFFSET_OF (PCI_BRIDGE_CONTROL_REGISTER, IoBaseUpper16)) << 4) | (IoBase >> 4);\r
+        IoBase = (PciRead16 (PciLibAddress + OFFSET_OF (PCI_TYPE01, Bridge.IoBaseUpper16)) << 4) | (IoBase >> 4);\r
       }\r
-      \r
+\r
       //\r
       // If PCI Bridge I/O window is disabled, then return 0\r
       //\r
       if (IoLimit < IoBase) {\r
         return 0;\r
       }\r
-      \r
+\r
       //\r
       // If PCI Bridge I/O window is not in the address range decoded by the parent PCI Bridge, then return 0\r
-      //  \r
+      //\r
       if (IoBase < ParentIoBase || IoBase > ParentIoLimit || IoLimit > ParentIoLimit) {\r
         return 0;\r
       }\r
@@ -296,7 +297,7 @@ GetSerialRegisterBase (
   // Compute PCI Lib Address to PCI UART\r
   //\r
   PciLibAddress = PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);\r
-  \r
+\r
   //\r
   // Find the first IO or MMIO BAR\r
   //\r
@@ -329,16 +330,16 @@ GetSerialRegisterBase (
 \r
   //\r
   // Program UART BAR\r
-  //  \r
+  //\r
   SerialRegisterBase = SerialPortLibUpdatePciRegister32 (\r
                          PciLibAddress + PCI_BASE_ADDRESSREG_OFFSET + BarIndex * 4,\r
-                         (UINT32)PcdGet64 (PcdSerialRegisterBase), \r
+                         (UINT32)PcdGet64 (PcdSerialRegisterBase),\r
                          RegisterBaseMask\r
                          );\r
 \r
   //\r
   // Verify that the UART BAR is in the address range decoded by the parent PCI Bridge\r
-  //  \r
+  //\r
   if (PcdGetBool (PcdSerialUseMmio)) {\r
     if (((SerialRegisterBase >> 16) & 0xfff0) < ParentMemoryBase || ((SerialRegisterBase >> 16) & 0xfff0) > ParentMemoryLimit) {\r
       return 0;\r
@@ -348,7 +349,7 @@ GetSerialRegisterBase (
       return 0;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Enable I/O and MMIO in PCI UART Device if they are not already enabled\r
   //\r
@@ -369,7 +370,7 @@ GetSerialRegisterBase (
       SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)));\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Get PCI Device Info\r
   //\r
@@ -377,22 +378,22 @@ GetSerialRegisterBase (
 \r
   //\r
   // Enable I/O or MMIO in PCI Bridge\r
-  // Assume Root Bus Numer is Zero. \r
+  // Assume Root Bus Numer is Zero.\r
   //\r
   for (BusNumber = 0; (DeviceInfo + 1)->Device != 0xff; DeviceInfo++) {\r
     //\r
     // Compute PCI Lib Address to PCI to PCI Bridge\r
     //\r
     PciLibAddress = PCI_LIB_ADDRESS (BusNumber, DeviceInfo->Device, DeviceInfo->Function, 0);\r
-    \r
+\r
     //\r
     // Enable the I/O or MMIO decode windows in the PCI to PCI Bridge\r
     //\r
     PciOr16 (\r
-      PciLibAddress + PCI_COMMAND_OFFSET, \r
+      PciLibAddress + PCI_COMMAND_OFFSET,\r
       PcdGetBool (PcdSerialUseMmio) ? EFI_PCI_COMMAND_MEMORY_SPACE : EFI_PCI_COMMAND_IO_SPACE\r
       );\r
-      \r
+\r
     //\r
     // Force D0 state if a Power Management and Status Register is specified\r
     //\r
@@ -401,16 +402,18 @@ GetSerialRegisterBase (
         PciAnd16 (PciLibAddress + DeviceInfo->PowerManagementStatusAndControlRegister, (UINT16)~(BIT0 | BIT1));\r
       }\r
     }\r
-      \r
+\r
     BusNumber = PciRead8 (PciLibAddress + PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET);\r
   }\r
-  \r
+\r
   return SerialRegisterBase;\r
 }\r
 \r
 /**\r
   Return whether the hardware flow control signal allows writing.\r
 \r
+  @param  SerialRegisterBase The base address register of UART device.\r
+\r
   @retval TRUE  The serial port is writable.\r
   @retval FALSE The serial port is not writable.\r
 **/\r
@@ -436,7 +439,7 @@ SerialPortWritable (
       return (BOOLEAN) ((SerialPortReadRegister (SerialRegisterBase, R_UART_MSR) & (B_UART_MSR_DSR | B_UART_MSR_CTS)) == (B_UART_MSR_DSR | B_UART_MSR_CTS));\r
     } else {\r
       //\r
-      // Wait for both DSR and CTS to be set OR for DSR to be clear.  \r
+      // Wait for both DSR and CTS to be set OR for DSR to be clear.\r
       //   DSR is set if a cable is connected.\r
       //   CTS is set if it is ok to transmit data\r
       //\r
@@ -456,11 +459,11 @@ SerialPortWritable (
 \r
 /**\r
   Initialize the serial device hardware.\r
-  \r
+\r
   If no initialization is required, then return RETURN_SUCCESS.\r
   If the serial device was successfully initialized, then return RETURN_SUCCESS.\r
   If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
-  \r
+\r
   @retval RETURN_SUCCESS        The serial device was initialized.\r
   @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.\r
 \r
@@ -474,7 +477,7 @@ SerialPortInitialize (
   RETURN_STATUS  Status;\r
   UINTN          SerialRegisterBase;\r
   UINT32         Divisor;\r
-  UINT32         CurrentDivisor;  \r
+  UINT32         CurrentDivisor;\r
   BOOLEAN        Initialized;\r
 \r
   //\r
@@ -526,7 +529,7 @@ SerialPortInitialize (
   // Verify that both the transmit FIFO and the shift register are empty.\r
   //\r
   while ((SerialPortReadRegister (SerialRegisterBase, R_UART_LSR) & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) != (B_UART_LSR_TEMT | B_UART_LSR_TXRDY));\r
-  \r
+\r
   //\r
   // Configure baud rate\r
   //\r
@@ -547,22 +550,27 @@ SerialPortInitialize (
   SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, 0x00);\r
   SerialPortWriteRegister (SerialRegisterBase, R_UART_FCR, (UINT8)(PcdGet8 (PcdSerialFifoControl) & (B_UART_FCR_FIFOE | B_UART_FCR_FIFO64)));\r
 \r
+  //\r
+  // Set FIFO Polled Mode by clearing IER after setting FCR\r
+  //\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_IER, 0x00);\r
+\r
   //\r
   // Put Modem Control Register(MCR) into its reset state of 0x00.\r
-  //  \r
+  //\r
   SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, 0x00);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
 \r
 /**\r
-  Write data from buffer to serial device. \r
+  Write data from buffer to serial device.\r
 \r
-  Writes NumberOfBytes data bytes from Buffer to the serial device.  \r
+  Writes NumberOfBytes data bytes from Buffer to the serial device.\r
   The number of bytes actually written to the serial device is returned.\r
   If the return value is less than NumberOfBytes, then the write operation failed.\r
 \r
-  If Buffer is NULL, then ASSERT(). \r
+  If Buffer is NULL, then ASSERT().\r
 \r
   If NumberOfBytes is zero, then return 0.\r
 \r
@@ -570,8 +578,8 @@ SerialPortInitialize (
   @param  NumberOfBytes    Number of bytes to written to the serial device.\r
 \r
   @retval 0                NumberOfBytes is 0.\r
-  @retval >0               The number of bytes written to the serial device.  \r
-                           If this value is less than NumberOfBytes, then the read operation failed.\r
+  @retval >0               The number of bytes written to the serial device.\r
+                           If this value is less than NumberOfBytes, then the write operation failed.\r
 \r
 **/\r
 UINTN\r
@@ -594,7 +602,7 @@ SerialPortWrite (
   if (SerialRegisterBase ==0) {\r
     return 0;\r
   }\r
-  \r
+\r
   if (NumberOfBytes == 0) {\r
     //\r
     // Flush the hardware\r
@@ -657,7 +665,7 @@ SerialPortWrite (
   @param  NumberOfBytes    Number of bytes to read from the serial device.\r
 \r
   @retval 0                NumberOfBytes is 0.\r
-  @retval >0               The number of bytes read from the serial device.  \r
+  @retval >0               The number of bytes read from the serial device.\r
                            If this value is less than NumberOfBytes, then the read operation failed.\r
 \r
 **/\r
@@ -682,7 +690,7 @@ SerialPortRead (
   }\r
 \r
   Mcr = (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_MCR) & ~B_UART_MCR_RTS);\r
-  \r
+\r
   for (Result = 0; NumberOfBytes-- != 0; Result++, Buffer++) {\r
     //\r
     // Wait for the serial port to have some data.\r
@@ -701,13 +709,13 @@ SerialPortRead (
       //\r
       SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, Mcr);\r
     }\r
-    \r
+\r
     //\r
     // Read byte from the receive buffer.\r
     //\r
     *Buffer = SerialPortReadRegister (SerialRegisterBase, R_UART_RXBUF);\r
   }\r
-  \r
+\r
   return Result;\r
 }\r
 \r
@@ -730,7 +738,7 @@ SerialPortPoll (
   )\r
 {\r
   UINTN  SerialRegisterBase;\r
-  \r
+\r
   SerialRegisterBase = GetSerialRegisterBase ();\r
   if (SerialRegisterBase ==0) {\r
     return FALSE;\r
@@ -747,14 +755,342 @@ SerialPortPoll (
       SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_MCR) & ~B_UART_MCR_RTS));\r
     }\r
     return TRUE;\r
-  }    \r
-  \r
+  }\r
+\r
   if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
     //\r
     // Set RTS to let the peer send some data\r
     //\r
     SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, (UINT8)(SerialPortReadRegister (SerialRegisterBase, R_UART_MCR) | B_UART_MCR_RTS));\r
   }\r
-  \r
+\r
   return FALSE;\r
 }\r
+\r
+/**\r
+  Sets the control bits on a serial device.\r
+\r
+  @param Control                Sets the bits of Control that are settable.\r
+\r
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.\r
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.\r
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortSetControl (\r
+  IN UINT32 Control\r
+  )\r
+{\r
+  UINTN SerialRegisterBase;\r
+  UINT8 Mcr;\r
+\r
+  //\r
+  // First determine the parameter is invalid.\r
+  //\r
+  if ((Control & (~(EFI_SERIAL_REQUEST_TO_SEND | EFI_SERIAL_DATA_TERMINAL_READY |\r
+                    EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE))) != 0) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  SerialRegisterBase = GetSerialRegisterBase ();\r
+  if (SerialRegisterBase ==0) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Read the Modem Control Register.\r
+  //\r
+  Mcr = SerialPortReadRegister (SerialRegisterBase, R_UART_MCR);\r
+  Mcr &= (~(B_UART_MCR_DTRC | B_UART_MCR_RTS));\r
+\r
+  if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) == EFI_SERIAL_DATA_TERMINAL_READY) {\r
+    Mcr |= B_UART_MCR_DTRC;\r
+  }\r
+\r
+  if ((Control & EFI_SERIAL_REQUEST_TO_SEND) == EFI_SERIAL_REQUEST_TO_SEND) {\r
+    Mcr |= B_UART_MCR_RTS;\r
+  }\r
+\r
+  //\r
+  // Write the Modem Control Register.\r
+  //\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_MCR, Mcr);\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Retrieve the status of the control bits on a serial device.\r
+\r
+  @param Control                A pointer to return the current control signals from the serial device.\r
+\r
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.\r
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.\r
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortGetControl (\r
+  OUT UINT32 *Control\r
+  )\r
+{\r
+  UINTN SerialRegisterBase;\r
+  UINT8 Msr;\r
+  UINT8 Mcr;\r
+  UINT8 Lsr;\r
+\r
+  SerialRegisterBase = GetSerialRegisterBase ();\r
+  if (SerialRegisterBase ==0) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  *Control = 0;\r
+\r
+  //\r
+  // Read the Modem Status Register.\r
+  //\r
+  Msr = SerialPortReadRegister (SerialRegisterBase, R_UART_MSR);\r
+\r
+  if ((Msr & B_UART_MSR_CTS) == B_UART_MSR_CTS) {\r
+    *Control |= EFI_SERIAL_CLEAR_TO_SEND;\r
+  }\r
+\r
+  if ((Msr & B_UART_MSR_DSR) == B_UART_MSR_DSR) {\r
+    *Control |= EFI_SERIAL_DATA_SET_READY;\r
+  }\r
+\r
+  if ((Msr & B_UART_MSR_RI) == B_UART_MSR_RI) {\r
+    *Control |= EFI_SERIAL_RING_INDICATE;\r
+  }\r
+\r
+  if ((Msr & B_UART_MSR_DCD) == B_UART_MSR_DCD) {\r
+    *Control |= EFI_SERIAL_CARRIER_DETECT;\r
+  }\r
+\r
+  //\r
+  // Read the Modem Control Register.\r
+  //\r
+  Mcr = SerialPortReadRegister (SerialRegisterBase, R_UART_MCR);\r
+\r
+  if ((Mcr & B_UART_MCR_DTRC) == B_UART_MCR_DTRC) {\r
+    *Control |= EFI_SERIAL_DATA_TERMINAL_READY;\r
+  }\r
+\r
+  if ((Mcr & B_UART_MCR_RTS) == B_UART_MCR_RTS) {\r
+    *Control |= EFI_SERIAL_REQUEST_TO_SEND;\r
+  }\r
+\r
+  if (PcdGetBool (PcdSerialUseHardwareFlowControl)) {\r
+    *Control |= EFI_SERIAL_HARDWARE_FLOW_CONTROL_ENABLE;\r
+  }\r
+\r
+  //\r
+  // Read the Line Status Register.\r
+  //\r
+  Lsr = SerialPortReadRegister (SerialRegisterBase, R_UART_LSR);\r
+\r
+  if ((Lsr & (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) == (B_UART_LSR_TEMT | B_UART_LSR_TXRDY)) {\r
+    *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;\r
+  }\r
+\r
+  if ((Lsr & B_UART_LSR_RXRDY) == 0) {\r
+    *Control |= EFI_SERIAL_INPUT_BUFFER_EMPTY;\r
+  }\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,\r
+  data bits, and stop bits on a serial device.\r
+\r
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the\r
+                            device's default interface speed.\r
+                            On output, the value actually set.\r
+  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the\r
+                            serial interface. A ReceiveFifoDepth value of 0 will use\r
+                            the device's default FIFO depth.\r
+                            On output, the value actually set.\r
+  @param Timeout            The requested time out for a single character in microseconds.\r
+                            This timeout applies to both the transmit and receive side of the\r
+                            interface. A Timeout value of 0 will use the device's default time\r
+                            out value.\r
+                            On output, the value actually set.\r
+  @param Parity             The type of parity to use on this serial device. A Parity value of\r
+                            DefaultParity will use the device's default parity value.\r
+                            On output, the value actually set.\r
+  @param DataBits           The number of data bits to use on the serial device. A DataBits\r
+                            vaule of 0 will use the device's default data bit setting.\r
+                            On output, the value actually set.\r
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits\r
+                            value of DefaultStopBits will use the device's default number of\r
+                            stop bits.\r
+                            On output, the value actually set.\r
+\r
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.\r
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.\r
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.\r
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortSetAttributes (\r
+  IN OUT UINT64             *BaudRate,\r
+  IN OUT UINT32             *ReceiveFifoDepth,\r
+  IN OUT UINT32             *Timeout,\r
+  IN OUT EFI_PARITY_TYPE    *Parity,\r
+  IN OUT UINT8              *DataBits,\r
+  IN OUT EFI_STOP_BITS_TYPE *StopBits\r
+  )\r
+{\r
+  UINTN     SerialRegisterBase;\r
+  UINT32    SerialBaudRate;\r
+  UINTN     Divisor;\r
+  UINT8     Lcr;\r
+  UINT8     LcrData;\r
+  UINT8     LcrParity;\r
+  UINT8     LcrStop;\r
+\r
+  SerialRegisterBase = GetSerialRegisterBase ();\r
+  if (SerialRegisterBase ==0) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Check for default settings and fill in actual values.\r
+  //\r
+  if (*BaudRate == 0) {\r
+    *BaudRate = PcdGet32 (PcdSerialBaudRate);\r
+  }\r
+  SerialBaudRate = (UINT32) *BaudRate;\r
+\r
+  if (*DataBits == 0) {\r
+    LcrData = (UINT8) (PcdGet8 (PcdSerialLineControl) & 0x3);\r
+    *DataBits = LcrData + 5;\r
+  } else {\r
+    if ((*DataBits < 5) || (*DataBits > 8)) {\r
+      return RETURN_INVALID_PARAMETER;\r
+    }\r
+    //\r
+    // Map 5..8 to 0..3\r
+    //\r
+    LcrData = (UINT8) (*DataBits - (UINT8) 5);\r
+  }\r
+\r
+  if (*Parity == DefaultParity) {\r
+    LcrParity = (UINT8) ((PcdGet8 (PcdSerialLineControl) >> 3) & 0x7);\r
+    switch (LcrParity) {\r
+      case 0:\r
+        *Parity = NoParity;\r
+        break;\r
+\r
+      case 3:\r
+        *Parity = EvenParity;\r
+        break;\r
+\r
+      case 1:\r
+        *Parity = OddParity;\r
+        break;\r
+\r
+      case 7:\r
+        *Parity = SpaceParity;\r
+        break;\r
+\r
+      case 5:\r
+        *Parity = MarkParity;\r
+        break;\r
+\r
+      default:\r
+        break;\r
+    }\r
+  } else {\r
+    switch (*Parity) {\r
+      case NoParity:\r
+        LcrParity = 0;\r
+        break;\r
+\r
+      case EvenParity:\r
+        LcrParity = 3;\r
+        break;\r
+\r
+      case OddParity:\r
+        LcrParity = 1;\r
+        break;\r
+\r
+      case SpaceParity:\r
+        LcrParity = 7;\r
+        break;\r
+\r
+      case MarkParity:\r
+        LcrParity = 5;\r
+        break;\r
+\r
+      default:\r
+        return RETURN_INVALID_PARAMETER;\r
+    }\r
+  }\r
+\r
+  if (*StopBits == DefaultStopBits) {\r
+    LcrStop = (UINT8) ((PcdGet8 (PcdSerialLineControl) >> 2) & 0x1);\r
+    switch (LcrStop) {\r
+      case 0:\r
+        *StopBits = OneStopBit;\r
+        break;\r
+\r
+      case 1:\r
+        if (*DataBits == 5) {\r
+          *StopBits = OneFiveStopBits;\r
+        } else {\r
+          *StopBits = TwoStopBits;\r
+        }\r
+        break;\r
+\r
+      default:\r
+        break;\r
+    }\r
+  } else {\r
+    switch (*StopBits) {\r
+      case OneStopBit:\r
+        LcrStop = 0;\r
+        break;\r
+\r
+      case OneFiveStopBits:\r
+      case TwoStopBits:\r
+        LcrStop = 1;\r
+        break;\r
+\r
+      default:\r
+        return RETURN_INVALID_PARAMETER;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Calculate divisor for baud generator\r
+  //    Ref_Clk_Rate / Baud_Rate / 16\r
+  //\r
+  Divisor = PcdGet32 (PcdSerialClockRate) / (SerialBaudRate * 16);\r
+  if ((PcdGet32 (PcdSerialClockRate) % (SerialBaudRate * 16)) >= SerialBaudRate * 8) {\r
+    Divisor++;\r
+  }\r
+\r
+  //\r
+  // Configure baud rate\r
+  //\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, B_UART_LCR_DLAB);\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_HIGH, (UINT8) (Divisor >> 8));\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_BAUD_LOW, (UINT8) (Divisor & 0xff));\r
+\r
+  //\r
+  // Clear DLAB and configure Data Bits, Parity, and Stop Bits.\r
+  // Strip reserved bits from line control value\r
+  //\r
+  Lcr = (UINT8) ((LcrParity << 3) | (LcrStop << 2) | LcrData);\r
+  SerialPortWriteRegister (SerialRegisterBase, R_UART_LCR, (UINT8) (Lcr & 0x3F));\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r