/** @file\r
Helper routines with common PEI / DXE implementation.\r
\r
-Copyright (c) 2013-2015 Intel Corporation.\r
+Copyright (c) 2013-2016 Intel Corporation.\r
\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
**/\r
\r
#include "CommonHeader.h"\r
+#include <Library/I2cLib.h>\r
\r
CHAR16 *mPlatTypeNameTable[] = { EFI_PLATFORM_TYPE_NAME_TABLE_DEFINITION };\r
UINTN mPlatTypeNameTableLen = ((sizeof(mPlatTypeNameTable)) / sizeof (CHAR16 *));\r
// Routines exported by this component.\r
//\r
\r
-/**\r
- Read 8bit character from debug stream.\r
-\r
- Block until character is read.\r
-\r
- @return 8bit character read from debug stream.\r
-\r
-**/\r
-CHAR8\r
-EFIAPI\r
-PlatformDebugPortGetChar8 (\r
- VOID\r
- )\r
-{\r
- CHAR8 Got;\r
-\r
- do {\r
- if (SerialPortPoll ()) {\r
- if (SerialPortRead ((UINT8 *) &Got, 1) == 1) {\r
- break;\r
- }\r
- }\r
- } while (TRUE);\r
-\r
- return Got;\r
-}\r
-\r
/**\r
Clear SPI Protect registers.\r
\r
GpioNumMask = (1 << GpioNum);\r
return ((RegValue & GpioNumMask) != 0);\r
}\r
+\r
+\r
+BOOLEAN\r
+Pcal9555GetPortRegBit (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum,\r
+ IN CONST UINT8 RegBase\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN ReadLength;\r
+ UINTN WriteLength;\r
+ UINT8 Data[2];\r
+ EFI_I2C_DEVICE_ADDRESS I2cDeviceAddr;\r
+ EFI_I2C_ADDR_MODE I2cAddrMode;\r
+ UINT8 *RegValuePtr;\r
+ UINT8 GpioNumMask;\r
+ UINT8 SubAddr;\r
+\r
+ I2cDeviceAddr.I2CDeviceAddress = (UINTN)Pcal9555SlaveAddr;\r
+ I2cAddrMode = EfiI2CSevenBitAddrMode;\r
+\r
+ if (GpioNum < 8) {\r
+ SubAddr = RegBase;\r
+ GpioNumMask = (UINT8)(1 << GpioNum);\r
+ } else {\r
+ SubAddr = RegBase + 1;\r
+ GpioNumMask = (UINT8)(1 << (GpioNum - 8));\r
+ }\r
+\r
+ //\r
+ // Output port value always at 2nd byte in Data variable.\r
+ //\r
+ RegValuePtr = &Data[1];\r
+\r
+ //\r
+ // On read entry sub address at 2nd byte, on read exit output\r
+ // port value in 2nd byte.\r
+ //\r
+ Data[1] = SubAddr;\r
+ WriteLength = 1;\r
+ ReadLength = 1;\r
+ Status = I2cReadMultipleByte (\r
+ I2cDeviceAddr,\r
+ I2cAddrMode,\r
+ &WriteLength,\r
+ &ReadLength,\r
+ &Data[1]\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ //\r
+ // Adjust output port bit given callers request.\r
+ //\r
+ return ((*RegValuePtr & GpioNumMask) != 0);\r
+}\r
+\r
+VOID\r
+Pcal9555SetPortRegBit (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum,\r
+ IN CONST UINT8 RegBase,\r
+ IN CONST BOOLEAN LogicOne\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN ReadLength;\r
+ UINTN WriteLength;\r
+ UINT8 Data[2];\r
+ EFI_I2C_DEVICE_ADDRESS I2cDeviceAddr;\r
+ EFI_I2C_ADDR_MODE I2cAddrMode;\r
+ UINT8 *RegValuePtr;\r
+ UINT8 GpioNumMask;\r
+ UINT8 SubAddr;\r
+\r
+ I2cDeviceAddr.I2CDeviceAddress = (UINTN)Pcal9555SlaveAddr;\r
+ I2cAddrMode = EfiI2CSevenBitAddrMode;\r
+\r
+ if (GpioNum < 8) {\r
+ SubAddr = RegBase;\r
+ GpioNumMask = (UINT8)(1 << GpioNum);\r
+ } else {\r
+ SubAddr = RegBase + 1;\r
+ GpioNumMask = (UINT8)(1 << (GpioNum - 8));\r
+ }\r
+\r
+ //\r
+ // Output port value always at 2nd byte in Data variable.\r
+ //\r
+ RegValuePtr = &Data[1];\r
+\r
+ //\r
+ // On read entry sub address at 2nd byte, on read exit output\r
+ // port value in 2nd byte.\r
+ //\r
+ Data[1] = SubAddr;\r
+ WriteLength = 1;\r
+ ReadLength = 1;\r
+ Status = I2cReadMultipleByte (\r
+ I2cDeviceAddr,\r
+ I2cAddrMode,\r
+ &WriteLength,\r
+ &ReadLength,\r
+ &Data[1]\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ //\r
+ // Adjust output port bit given callers request.\r
+ //\r
+ if (LogicOne) {\r
+ *RegValuePtr = *RegValuePtr | GpioNumMask;\r
+ } else {\r
+ *RegValuePtr = *RegValuePtr & ~(GpioNumMask);\r
+ }\r
+\r
+ //\r
+ // Update register. Sub address at 1st byte, value at 2nd byte.\r
+ //\r
+ WriteLength = 2;\r
+ Data[0] = SubAddr;\r
+ Status = I2cWriteMultipleByte (\r
+ I2cDeviceAddr,\r
+ I2cAddrMode,\r
+ &WriteLength,\r
+ Data\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+Set the direction of Pcal9555 IO Expander GPIO pin.\r
+\r
+@param Pcal9555SlaveAddr I2c Slave address of Pcal9555 Io Expander.\r
+@param GpioNum Gpio direction to configure - values 0-7 for Port0\r
+and 8-15 for Port1.\r
+@param CfgAsInput If TRUE set pin direction as input else set as output.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformPcal9555GpioSetDir (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum,\r
+ IN CONST BOOLEAN CfgAsInput\r
+ )\r
+{\r
+ Pcal9555SetPortRegBit (\r
+ Pcal9555SlaveAddr,\r
+ GpioNum,\r
+ PCAL9555_REG_CFG_PORT0,\r
+ CfgAsInput\r
+ );\r
+}\r
+\r
+/**\r
+Set the level of Pcal9555 IO Expander GPIO high or low.\r
+\r
+@param Pcal9555SlaveAddr I2c Slave address of Pcal9555 Io Expander.\r
+@param GpioNum Gpio to change values 0-7 for Port0 and 8-15\r
+for Port1.\r
+@param HighLevel If TRUE set pin high else set pin low.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformPcal9555GpioSetLevel (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum,\r
+ IN CONST BOOLEAN HighLevel\r
+ )\r
+{\r
+ Pcal9555SetPortRegBit (\r
+ Pcal9555SlaveAddr,\r
+ GpioNum,\r
+ PCAL9555_REG_OUT_PORT0,\r
+ HighLevel\r
+ );\r
+}\r
+\r
+/**\r
+\r
+Enable pull-up/pull-down resistors of Pcal9555 GPIOs.\r
+\r
+@param Pcal9555SlaveAddr I2c Slave address of Pcal9555 Io Expander.\r
+@param GpioNum Gpio to change values 0-7 for Port0 and 8-15\r
+for Port1.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformPcal9555GpioEnablePull (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum\r
+ )\r
+{\r
+ Pcal9555SetPortRegBit (\r
+ Pcal9555SlaveAddr,\r
+ GpioNum,\r
+ PCAL9555_REG_PULL_EN_PORT0,\r
+ TRUE\r
+ );\r
+}\r
+\r
+/**\r
+\r
+Disable pull-up/pull-down resistors of Pcal9555 GPIOs.\r
+\r
+@param Pcal9555SlaveAddr I2c Slave address of Pcal9555 Io Expander.\r
+@param GpioNum Gpio to change values 0-7 for Port0 and 8-15\r
+for Port1.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+PlatformPcal9555GpioDisablePull (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum\r
+ )\r
+{\r
+ Pcal9555SetPortRegBit (\r
+ Pcal9555SlaveAddr,\r
+ GpioNum,\r
+ PCAL9555_REG_PULL_EN_PORT0,\r
+ FALSE\r
+ );\r
+}\r
+\r
+/**\r
+\r
+Get state of Pcal9555 GPIOs.\r
+\r
+@param Pcal9555SlaveAddr I2c Slave address of Pcal9555 Io Expander.\r
+@param GpioNum Gpio to change values 0-7 for Port0 and 8-15\r
+for Port1.\r
+\r
+@retval TRUE GPIO pin is high\r
+@retval FALSE GPIO pin is low\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+PlatformPcal9555GpioGetState (\r
+ IN CONST UINT32 Pcal9555SlaveAddr,\r
+ IN CONST UINT32 GpioNum\r
+ )\r
+{\r
+ return Pcal9555GetPortRegBit (Pcal9555SlaveAddr, GpioNum, PCAL9555_REG_IN_PORT0);\r
+}\r
+\r
+\r