]> git.proxmox.com Git - mirror_edk2.git/blobdiff - PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c
DuetPkg/Library/DuetSerialIoLib => PcAtChipsetPkg/Library/SerialIoLib
[mirror_edk2.git] / PcAtChipsetPkg / Library / SerialIoLib / SerialPortLib.c
diff --git a/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c b/PcAtChipsetPkg/Library/SerialIoLib/SerialPortLib.c
new file mode 100644 (file)
index 0000000..f0cdc49
--- /dev/null
@@ -0,0 +1,222 @@
+/** @file\r
+  UART Serial Port library instance with empty functions.\r
+\r
+  Copyright (c) 2006 - 2008, Intel Corporation\r
+  All rights reserved. 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
+\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
+\r
+**/\r
+\r
+#include <Base.h>\r
+\r
+#include <Library/SerialPortLib.h>\r
+#include <Library/IoLib.h>\r
+\r
+//---------------------------------------------\r
+// UART Register Offsets\r
+//---------------------------------------------\r
+#define BAUD_LOW_OFFSET         0x00\r
+#define BAUD_HIGH_OFFSET        0x01\r
+#define IER_OFFSET              0x01\r
+#define LCR_SHADOW_OFFSET       0x01\r
+#define FCR_SHADOW_OFFSET       0x02\r
+#define IR_CONTROL_OFFSET       0x02\r
+#define FCR_OFFSET              0x02\r
+#define EIR_OFFSET              0x02\r
+#define BSR_OFFSET              0x03\r
+#define LCR_OFFSET              0x03\r
+#define MCR_OFFSET              0x04\r
+#define LSR_OFFSET              0x05\r
+#define MSR_OFFSET              0x06\r
+\r
+//---------------------------------------------\r
+// UART Register Bit Defines\r
+//---------------------------------------------\r
+#define LSR_TXRDY               0x20\r
+#define LSR_RXDA                0x01\r
+#define DLAB                    0x01\r
+\r
+//---------------------------------------------\r
+// UART Settings\r
+//---------------------------------------------\r
+UINT16  gUartBase = 0x3F8;\r
+UINTN   gBps      = 115200;\r
+UINT8   gData     = 8;\r
+UINT8   gStop     = 1;\r
+UINT8   gParity   = 0;\r
+UINT8   gBreakSet = 0;\r
+\r
+/**\r
+  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 could not be initialized, then return RETURN_DEVICE_ERROR.\r
+  \r
+  @retval RETURN_SUCCESS        The serial device was initialized.\r
+  @retval RETURN_DEVICE_ERROR   The serail device could not be initialized.\r
+\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SerialPortInitialize (\r
+  VOID\r
+  )\r
+{\r
+  UINTN  Divisor;\r
+  UINT8  OutputData;\r
+  UINT8  Data;\r
+\r
+  //\r
+  // Map 5..8 to 0..3\r
+  //\r
+  Data = (UINT8) (gData - (UINT8) 5);\r
+\r
+  //\r
+  // Calculate divisor for baud generator\r
+  //\r
+  Divisor = 115200 / gBps;\r
+  \r
+  //\r
+  // Set communications format\r
+  //\r
+  OutputData = (UINT8) ((DLAB << 7) | (gBreakSet << 6) | (gParity << 3) | (gStop << 2) | Data);\r
+  IoWrite8 ((UINTN) (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
+\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
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+/**\r
+  Write data from buffer to serial device. \r
\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
+\r
+  If NumberOfBytes is zero, then return 0.\r
+\r
+  @param  Buffer           Pointer to the data buffer to be written.\r
+  @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
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+SerialPortWrite (\r
+  IN UINT8     *Buffer,\r
+  IN UINTN     NumberOfBytes\r
+)\r
+{\r
+  UINTN  Result;\r
+  UINT8  Data;\r
+\r
+  if (Buffer == NULL) {\r
+    return 0;\r
+  }\r
+\r
+  Result = NumberOfBytes;\r
+\r
+  while (NumberOfBytes--) {\r
+    //\r
+    // Wait for the serail port to be ready.\r
+    //\r
+    do {\r
+      Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+    } while ((Data & LSR_TXRDY) == 0);\r
+    IoWrite8 ((UINT16) gUartBase, *Buffer++);\r
+  }\r
+\r
+  return Result;\r
+}\r
+\r
+\r
+/**\r
+  Reads data from a serial device into a buffer.\r
+\r
+  @param  Buffer           Pointer to the data buffer to store the data read from the serial device.\r
+  @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
+                           If this value is less than NumberOfBytes, then the read operation failed.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+SerialPortRead (\r
+  OUT UINT8     *Buffer,\r
+  IN  UINTN     NumberOfBytes\r
+)\r
+{\r
+  UINTN  Result;\r
+  UINT8  Data;\r
+\r
+  if (NULL == Buffer) {\r
+    return 0;\r
+  }\r
+\r
+  Result = NumberOfBytes;\r
+\r
+  while (NumberOfBytes--) {\r
+    //\r
+    // Wait for the serail port to be ready.\r
+    //\r
+    do {\r
+      Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+    } while ((Data & LSR_RXDA) == 0);\r
+\r
+    *Buffer++ = IoRead8 ((UINT16) gUartBase);\r
+  }\r
+\r
+  return Result;\r
+}\r
+\r
+/**\r
+  Polls a serial device to see if there is any data waiting to be read.\r
+\r
+  Polls aserial device to see if there is any data waiting to be read.\r
+  If there is data waiting to be read from the serial device, then TRUE is returned.\r
+  If there is no data waiting to be read from the serial device, then FALSE is returned.\r
+\r
+  @retval TRUE             Data is waiting to be read from the serial device.\r
+  @retval FALSE            There is no data waiting to be read from the serial device.\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+SerialPortPoll (\r
+  VOID\r
+  )\r
+{\r
+  UINT8  Data;\r
+\r
+  //\r
+  // Read the serial port status.\r
+  //\r
+  Data = IoRead8 ((UINT16) gUartBase + LSR_OFFSET);\r
+\r
+  return (BOOLEAN) ((Data & LSR_RXDA) != 0);\r
+}\r
+\r