]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c
PcAtChipsetPkg: Refine casting expression result to bigger size
[mirror_edk2.git] / PcAtChipsetPkg / Library / SerialIoLib / SerialPortLib.c
index f0cdc4982f3ab72312b17b0b2b5ef19988f27ac1..95e0db70ad6915872a1b2c8050ad5044b5dc06e6 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
-  UART Serial Port library instance with empty functions.\r
+  UART Serial Port library functions\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 2017, 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
@@ -13,9 +13,8 @@
 **/\r
 \r
 #include <Base.h>\r
-\r
-#include <Library/SerialPortLib.h>\r
 #include <Library/IoLib.h>\r
+#include <Library/SerialPortLib.h>\r
 \r
 //---------------------------------------------\r
 // UART Register Offsets\r
 #define LSR_TXRDY               0x20\r
 #define LSR_RXDA                0x01\r
 #define DLAB                    0x01\r
+#define MCR_DTRC                0x01\r
+#define MCR_RTS                 0x02\r
+#define MSR_CTS                 0x10\r
+#define MSR_DSR                 0x20\r
+#define MSR_RI                  0x40\r
+#define MSR_DCD                 0x80\r
 \r
 //---------------------------------------------\r
 // UART Settings\r
@@ -55,7 +60,7 @@ UINT8   gBreakSet = 0;
   Initialize the serial device hardware.\r
   \r
   If no initialization is required, then return RETURN_SUCCESS.\r
-  If the serial device was successfuly initialized, 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
   @retval RETURN_SUCCESS        The serial device was initialized.\r
@@ -86,19 +91,19 @@ SerialPortInitialize (
   // Set communications format\r
   //\r
   OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
-  IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
+  IoWrite8 (gUartBase + LCR_OFFSET, OutputData);\r
 \r
   //\r
   // Configure baud rate\r
   //\r
-  IoWrite8 ((UINTN) (gUartBase + BAUD_HIGH_OFFSET), (UINT8) (Divisor >> 8));\r
-  IoWrite8 ((UINTN) (gUartBase + BAUD_LOW_OFFSET), (UINT8) (Divisor & 0xff));\r
+  IoWrite8 (gUartBase + BAUD_HIGH_OFFSET, (UINT8) (Divisor >> 8));\r
+  IoWrite8 (gUartBase + BAUD_LOW_OFFSET, (UINT8) (Divisor & 0xff));\r
 \r
   //\r
   // Switch back to bank 0\r
   //\r
   OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
-  IoWrite8 ((UINTN) (gUartBase + LCR_OFFSET), OutputData);\r
+  IoWrite8 (gUartBase + LCR_OFFSET, OutputData);\r
 \r
   return RETURN_SUCCESS;\r
 }\r
@@ -119,7 +124,7 @@ SerialPortInitialize (
 \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
+                           If this value is less than NumberOfBytes, then the write operation failed.\r
 \r
 **/\r
 UINTN\r
@@ -138,7 +143,7 @@ SerialPortWrite (
 \r
   Result = NumberOfBytes;\r
 \r
-  while (NumberOfBytes--) {\r
+  while ((NumberOfBytes--) != 0) {\r
     //\r
     // Wait for the serail port to be ready.\r
     //\r
@@ -179,7 +184,7 @@ SerialPortRead (
 \r
   Result = NumberOfBytes;\r
 \r
-  while (NumberOfBytes--) {\r
+  while ((NumberOfBytes--) != 0) {\r
     //\r
     // Wait for the serail port to be ready.\r
     //\r
@@ -220,3 +225,265 @@ SerialPortPoll (
   return (BOOLEAN) ((Data & LSR_RXDA) != 0);\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
+  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))) != 0) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Read the Modem Control Register.\r
+  //\r
+  Mcr = IoRead8 ((UINT16) gUartBase + MCR_OFFSET);\r
+  Mcr &= (~(MCR_DTRC | MCR_RTS));\r
+\r
+  if ((Control & EFI_SERIAL_DATA_TERMINAL_READY) == EFI_SERIAL_DATA_TERMINAL_READY) {\r
+    Mcr |= MCR_DTRC;\r
+  }\r
+\r
+  if ((Control & EFI_SERIAL_REQUEST_TO_SEND) == EFI_SERIAL_REQUEST_TO_SEND) {\r
+    Mcr |= MCR_RTS;\r
+  }\r
+\r
+  //\r
+  // Write the Modem Control Register.\r
+  //\r
+  IoWrite8 ((UINT16) gUartBase + MCR_OFFSET, 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
+  UINT8 Msr;\r
+  UINT8 Mcr;\r
+  UINT8 Lsr;\r
+\r
+  *Control = 0;\r
+\r
+  //\r
+  // Read the Modem Status Register.\r
+  //\r
+  Msr = IoRead8 ((UINT16) gUartBase + MSR_OFFSET);\r
+\r
+  if ((Msr & MSR_CTS) == MSR_CTS) {\r
+    *Control |= EFI_SERIAL_CLEAR_TO_SEND;\r
+  }\r
+\r
+  if ((Msr & MSR_DSR) == MSR_DSR) {\r
+    *Control |= EFI_SERIAL_DATA_SET_READY;\r
+  }\r
+\r
+  if ((Msr & MSR_RI) == MSR_RI) {\r
+    *Control |= EFI_SERIAL_RING_INDICATE;\r
+  }\r
+\r
+  if ((Msr & MSR_DCD) == MSR_DCD) {\r
+    *Control |= EFI_SERIAL_CARRIER_DETECT;\r
+  }\r
+\r
+  //\r
+  // Read the Modem Control Register.\r
+  //\r
+  Mcr = IoRead8 ((UINT16) gUartBase + MCR_OFFSET);\r
+\r
+  if ((Mcr & MCR_DTRC) == MCR_DTRC) {\r
+    *Control |= EFI_SERIAL_DATA_TERMINAL_READY;\r
+  }\r
+\r
+  if ((Mcr & MCR_RTS) == MCR_RTS) {\r
+    *Control |= EFI_SERIAL_REQUEST_TO_SEND;\r
+  }\r
+\r
+  //\r
+  // Read the Line Status Register.\r
+  //\r
+  Lsr = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+\r
+  if ((Lsr & LSR_TXRDY) == LSR_TXRDY) {\r
+    *Control |= EFI_SERIAL_OUTPUT_BUFFER_EMPTY;\r
+  }\r
+\r
+  if ((Lsr & LSR_RXDA) == 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 Divisor;\r
+  UINT8 OutputData;\r
+  UINT8 LcrData;\r
+  UINT8 LcrParity;\r
+  UINT8 LcrStop;\r
+\r
+  //\r
+  // Check for default settings and fill in actual values.\r
+  //\r
+  if (*BaudRate == 0) {\r
+    *BaudRate = gBps;\r
+  }\r
+\r
+  if (*DataBits == 0) {\r
+    *DataBits = gData;\r
+  }\r
+\r
+  if (*Parity == DefaultParity) {\r
+    *Parity = NoParity;\r
+  }\r
+\r
+  if (*StopBits == DefaultStopBits) {\r
+    *StopBits = OneStopBit;\r
+  }\r
+\r
+  if ((*DataBits < 5) || (*DataBits > 8)) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Map 5..8 to 0..3\r
+  //\r
+  LcrData = (UINT8) (*DataBits - (UINT8) 5);\r
+\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
+  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
+  // Calculate divisor for baud generator\r
+  //\r
+  Divisor = 115200 / (UINTN) *BaudRate;\r
+\r
+  //\r
+  // Set communications format\r
+  //\r
+  OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (LcrParity << 3) | (LcrStop << 2) | LcrData);\r
+  IoWrite8 (gUartBase + LCR_OFFSET, OutputData);\r
+\r
+  //\r
+  // Configure baud rate\r
+  //\r
+  IoWrite8 (gUartBase + BAUD_HIGH_OFFSET, (UINT8) (Divisor >> 8));\r
+  IoWrite8 (gUartBase + BAUD_LOW_OFFSET, (UINT8) (Divisor & 0xff));\r
+\r
+  //\r
+  // Switch back to bank 0\r
+  //\r
+  OutputData = (UINT8) ((~DLAB << 7) | (gBreakSet << 6) | (LcrParity << 3) | (LcrStop << 2) | LcrData);\r
+  IoWrite8 (gUartBase + LCR_OFFSET, OutputData);\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r