From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (NAM04-MW2-obe.outbound.protection.outlook.com [40.107.101.91]) by mx.groups.io with SMTP id smtpd.web08.10070.1631721571896610643 for ; Wed, 15 Sep 2021 08:59:32 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@os.amperecomputing.com header.s=selector2 header.b=WNV3f6up; spf=pass (domain: os.amperecomputing.com, ip: 40.107.101.91, mailfrom: nhi@os.amperecomputing.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YlN4g/JPgWQUmXPuWcpdFoLBbd0bzLPDpQOQ3orgcYKRoc0UM6zoH262mw3939+sljPCoPihnX3r6fDakqOBEk7eYKEdmRUrX/ACn6iIxjobffb37w9W3topEh04r2qazjTcfRCZQ4Nc1qW7wDdpcGR50zZ3ZZzrNfPvGLsp5yYdRCIUD9wPPjaoosPdRAxjsYhF5tlcSZo1ouR1RhZkJtjg4pFT31HK+yp5SZJ022qRs6HwbXBsy6xKb19YXB27EyOE8rhuxAk7nBYSfNhIG4StnCG+SpEHkJWJr9EuuPnH8UhoTATpON9hzMO24a6cXGUQQIw3XtamifN3V7i4mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=ICGt2OYNsFf3Basb/jdLpq6+L0Vu2fw8thwwum+6cik=; b=TK6mQL6/PRTAaUsWgCzQtj+pubeVnk8MElCBkgd1XsJFoUzyV7MCo8ac8JDuEKfwReq2G/T1omMTjt+IVQ0VKRemVaFjnc32oBfChLPZRKGy064Xs6rz72Na6NG3C+ICaCn9zPUUDVHktTL7B5VXO+ClS2HAIQkdidxBflH/cUKAPEUxvvFWGJPmq1H1TsAhmGrvLUnGV6jefcsHfS7Nr2y2kc2Bt9b7cdmaRwrAFfG1Un29oBSsMi+mAYiBUbsGiy3V8WCLUY3L2a293vBnPZtGDCHh415GJpj5F6vwHaDI2ipmCcIOtjcYD42Fahoi2z1lm3fmCEiWER+YeaMXQA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ICGt2OYNsFf3Basb/jdLpq6+L0Vu2fw8thwwum+6cik=; b=WNV3f6upY9SsJb+LGOAGUSi3DQn8Wwrbd9jMWnbR3whNzCdzpvmGtRvzbk94d1v5LRl9Iz03q5ra4Nsln0KxKv4mibiSd+L8bxPpB/tqrTFMbA+WdrsV4+0jYERroIKf8rkGhvBebZRaap0l+ffgQmcW1Sif0Acd+jswR6SlXrQ= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) by DM6PR01MB5513.prod.exchangelabs.com (2603:10b6:5:151::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.17; Wed, 15 Sep 2021 15:59:29 +0000 Received: from DM6PR01MB5849.prod.exchangelabs.com ([fe80::8eb:704f:2ba7:9bc3]) by DM6PR01MB5849.prod.exchangelabs.com ([fe80::8eb:704f:2ba7:9bc3%4]) with mapi id 15.20.4523.014; Wed, 15 Sep 2021 15:59:29 +0000 From: "Nhi Pham" To: devel@edk2.groups.io CC: patches@amperecomputing.com, nhi@os.amperecomputing.com, vunguyen@os.amperecomputing.com, Thang Nguyen , Chuong Tran , Phong Vo , Leif Lindholm , Michael D Kinney , Ard Biesheuvel , Nate DeSimone Subject: [PATCH v3 05/28] AmpereAltraPkg: Add DwI2cLib library Date: Wed, 15 Sep 2021 22:55:04 +0700 Message-ID: <20210915155527.8176-6-nhi@os.amperecomputing.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210915155527.8176-1-nhi@os.amperecomputing.com> References: <20210915155527.8176-1-nhi@os.amperecomputing.com> X-ClientProxiedBy: HK2PR0302CA0012.apcprd03.prod.outlook.com (2603:1096:202::22) To DM6PR01MB5849.prod.exchangelabs.com (2603:10b6:5:205::20) Return-Path: nhi@os.amperecomputing.com MIME-Version: 1.0 Received: from sw004.amperecomputing.com (118.69.219.201) by HK2PR0302CA0012.apcprd03.prod.outlook.com (2603:1096:202::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.5 via Frontend Transport; Wed, 15 Sep 2021 15:59:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 77650048-256a-4b38-61ac-08d97861ccc0 X-MS-TrafficTypeDiagnostic: DM6PR01MB5513: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HWcHGgfi9e4JbHEsOGV3cEpxG77TZ0hlwujdqofFERl/rkP8PEtQYa28QBK+wDCsFYq3JojWGvfUkV/toamQYlkwp8DTn3C7RY8ANgtTOwuxuRS9ZDqcVBjSY+8SWavHR9d2+lrVGKHyM1PvZtdd+y3c11FjRlcLo+BMHu2RCaXFuiyRocaGu/NA0UOzG19A703IuWLccaUt0KMN13IVuJ7TIMvDIBgxX0SJ3OP4yz5dFzirB9xZc5N0BWZPtw095v2RWYcrx+Q9k1ylz5a/KyxszQjPZnKcsXhWpzPigkcfBxeZPiGsSyBGbqGRURG4x33lSa2KjjWOVrtsqmAhy0BYN6/SqQ4iPWwqLQf1wbitBSWfx0/N+7+sSuLg9jUk4B4NEevAaYl2+wvTz4zkGUrMaAaNAAl4CoDXivUs8yVd0umtVOh/8GZWpQYwvrlEdJ6ejXu66S1mQNViKVb+gNFjsYkL/qX7ercytgYEKhFBGD6iRM2fmsU74gzQXfh/HL6c4Hud7hqTvHlyTLfvAzU0fYjdHTDDBQy7os1FAyLIqywz+hI+IEMzeiGYclaPKwywGg+GpBPPjb/mt5+DOZ15AokjuEmXwTjOaezmwSf+LJ3uZi7CGyhNV+JcGtwqEGzEvqn73CoaP0SJ+Nhf8HdkERPnW1gpzDfwUvGkW17yQqwXACKF8x6zPX9ucDQlruprPOhxcw4uYXvfN+/ljA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR01MB5849.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(366004)(396003)(39850400004)(346002)(316002)(30864003)(1076003)(5660300002)(83380400001)(54906003)(6506007)(6666004)(4326008)(8676002)(186003)(956004)(6512007)(86362001)(38100700002)(19627235002)(6486002)(38350700002)(478600001)(66946007)(66476007)(66556008)(2616005)(26005)(6916009)(8936002)(52116002)(2906002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?NrDTe7ml1VQ8jnfTaUXdkFN6M1v8C91w3bosvOnbFAGpg9aw207OXseHuURB?= =?us-ascii?Q?FpUkc/oZdj061FmRLIpxPVvr9IdsZQputimcVpVGAi5wXXr/14t+4yKJq4He?= =?us-ascii?Q?utr8Uz8qpQ5qZxOlwBcZSIRpKpDxcpml+VmQ/zb5H2IpK7+d7jX/Y299g9ca?= =?us-ascii?Q?2TowQZlLGrM8I2DjtdLR5Ih7KIgTGfHazGEbodrnHhAdhLqDYDfrCC5EMylQ?= =?us-ascii?Q?H6B+WwprNhaWJs4WBNtAnwB1pG3d5HJGTVh75718hZxAv6hjM9h59kCRwsZS?= =?us-ascii?Q?/6FsO1nAmUjNr8lX1xu+Xs41PQWJJpZHdbHXfPa5kOcJf/yUt3/a/Rl2X2GS?= =?us-ascii?Q?kX+KoUvFIXsdSKZMieP0WUG3V9qbQuWNRnp7ftm8n2fA+wsFWKPy6Gy8ik11?= =?us-ascii?Q?xZMBidJP1kfgcdnHR4vcFmNEmpVZWjMHcXtu/Wint7xfMWQrzCpGJeqncxTF?= =?us-ascii?Q?ItZkWMel9AWtpzicAfkb59Yd1tkpLqUT4HFGxDp5AyhgHdYnj+OEG44EhwUv?= =?us-ascii?Q?Ij/fmcEBU9Dd0TPuorIu0vTrEuFu1VFVS/zN/HWsd2ORPnJT8L2K8fx4pOhl?= =?us-ascii?Q?f1SHMQ8z/mEYn8kqJfUUP2GehS/DZw0at68F63kpBl92fQgSp/p9Yx7nINpI?= =?us-ascii?Q?LRDyFKNbCBdxo3ShVxjl2KRXK0bimqY09Z4rzC4jaoyRCnGGhGSX/EBUaYVt?= =?us-ascii?Q?Phb9ffLQohqcE23fh324y5/rW95XwXnrTXjzbWE8rDQPMMIdTmIPbXc73RDO?= =?us-ascii?Q?Gp2aA6AXZmDYLDQPBecAgWphqLgXM8mxhcggCdPHGa3JLH9SBsn9Z6RcpYUs?= =?us-ascii?Q?FM7oR1Dx3R1Q4UNicLNqi7YR1ZNSjzlGWhlVxqcLcNolV32RMjQcLyiXHx0D?= =?us-ascii?Q?erKnmqHYFJcL9efpyHmOHAiwuUEnnfomJa9OTnI5LBDhFLLskHGNReurtYmP?= =?us-ascii?Q?H/jRJpkRGH9D9vqHFtxFPhDx+OJc08fJXamwZZw/rCU49YFGZTT0VvXbdziK?= =?us-ascii?Q?KDbUdtb/wIaKrDppy4gBFxwa/oCCrCVkcp4QiwKFst4TIciq02aU28VJIIsY?= =?us-ascii?Q?/kSti3pikIozGR2Jprt7f0dFuRHsdlofpNstOY0JIrAW8MBXTkmgEnnSUUVy?= =?us-ascii?Q?+hqF0jra6vAYivalmIYx9xMFwFWgmHWpYwNg/jbC4lL0bPUY++gXV3fcOOTX?= =?us-ascii?Q?qwykMXBg5DdKwGeM9q4GKA2OK6pW7Yh47E5Gjh4HW6ca8ak9xWX6wGJpwujk?= =?us-ascii?Q?wJF56W3SxcsBhqLchiaAQhA/maFzKPRtq9TsHrY5N+sfGSpl9ShQKa7m/PpD?= =?us-ascii?Q?BbdHdbl4j/TycAEHjel8lCYx?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 77650048-256a-4b38-61ac-08d97861ccc0 X-MS-Exchange-CrossTenant-AuthSource: DM6PR01MB5849.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Sep 2021 15:59:29.6064 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4BsxZou/Ea/+wjbdOxG65TCWaQnLEtYH0Q46JfXRZaUONwcHoQB0GjieGtyENsbj3ZRqp7JcogvHUhHJJ2EhPdGW258LJ/79DXmkInPDjQE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR01MB5513 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Vu Nguyen The DwI2cLib library provides basic functions to control the I2C controller on Ampere Altra processor. Cc: Thang Nguyen Cc: Chuong Tran Cc: Phong Vo Cc: Leif Lindholm Cc: Michael D Kinney Cc: Ard Biesheuvel Cc: Nate DeSimone Signed-off-by: Vu Nguyen Reviewed-by: Leif Lindholm --- Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec | 3 + Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.inf | 38 + Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h | 100 +++ Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c | 883 ++++++++= ++++++++++++ 4 files changed, 1024 insertions(+) diff --git a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec b/Silicon/Amp= ere/AmpereAltraPkg/AmpereAltraPkg.dec index 73097afaf841..8be6a329bb26 100644 --- a/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec +++ b/Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec @@ -28,6 +28,9 @@ [LibraryClasses] ## @libraryclass Defines a set of methods to communicate with SCP. SystemFirmwareInterfaceLib|Silicon/Ampere/AmpereAltraPkg/Include/Library= /SystemFirmwareInterfaceLib.h =20 + ## @libraryclass Defines a set of methods to read/write to I2C devices= . + I2cLib|Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h + ## @libraryclass Defines a set of methods to communicate with secure p= arition over MM interface. MmCommunicationLib|Silicon/Ampere/AmpereAltraPkg/Include/Library/MmCommu= nicationLib.h =20 diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.inf b/= Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.inf new file mode 100644 index 000000000000..091f5f9b310c --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.inf @@ -0,0 +1,38 @@ +## @file +# Component description for DwI2cLib library for the Designware I2C contro= ller. +# +# Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D DwI2cLib + FILE_GUID =3D 222609E2-C181-11E6-A4A6-CEC0C932CE01 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D I2cLib + CONSTRUCTOR =3D I2cLibConstructor + +[Sources] + DwI2cLib.c + +[Packages] + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + MdePkg/MdePkg.dec + Silicon/Ampere/AmpereAltraPkg/AmpereAltraPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + HobLib + IoLib + TimerLib + +[Guids] + gEfiEventVirtualAddressChangeGuid + gPlatformHobGuid diff --git a/Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h b/Silic= on/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h new file mode 100644 index 000000000000..f13794171029 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Include/Library/I2cLib.h @@ -0,0 +1,100 @@ +/** @file + Library implementation for the Designware I2C controller. + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef I2C_LIB_H_ +#define I2C_LIB_H_ + +#include + +/** + Write to I2C bus. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in,out] Buf Buffer that holds data to write. + @param[in,out] WriteLength Pointer to length of buffer. + + @return EFI_SUCCESS Write successfully. + @return EFI_INVALID_PARAMETER A parameter is invalid. + @return EFI_UNSUPPORTED The bus is not supported. + @return EFI_NOT_READY The device/bus is not ready. + @return EFI_TIMEOUT Timeout why transferring data. + +**/ +EFI_STATUS +EFIAPI +I2cWrite ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN OUT UINT8 *Buf, + IN OUT UINT32 *WriteLength + ); + +/** + Read data from I2C bus. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in] BufCmd Buffer where to send the command. + @param[in] CmdLength Pointer to length of BufCmd. + @param[in,out] Buf Buffer where to put the read data to. + @param[in,out] ReadLength Pointer to length of buffer. + + @return EFI_SUCCESS Read successfully. + @return EFI_INVALID_PARAMETER A parameter is invalid. + @return EFI_UNSUPPORTED The bus is not supported. + @return EFI_NOT_READY The device/bus is not ready. + @return EFI_TIMEOUT Timeout why transferring data. + @return EFI_CRC_ERROR There are errors on receiving data. + +**/ +EFI_STATUS +EFIAPI +I2cRead ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN UINT8 *BufCmd, + IN UINT32 CmdLength, + IN OUT UINT8 *Buf, + IN OUT UINT32 *ReadLength + ); + +/** + Setup new transaction with I2C slave device. + + @param[in] Bus I2C bus Id. + @param[in] BusSpeed I2C bus speed in Hz. + + @retval EFI_SUCCESS Success. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + +**/ +EFI_STATUS +EFIAPI +I2cProbe ( + IN UINT32 Bus, + IN UINTN BusSpeed + ); + +/** + Setup a bus that to be used in runtime service. + + @param[in] Bus I2C bus Id. + + @retval EFI_SUCCESS Success. + @retval Otherwise Error code. + +**/ +EFI_STATUS +EFIAPI +I2cSetupRuntime ( + IN UINT32 Bus + ); + +#endif /* I2C_LIB_H_ */ diff --git a/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c b/Si= licon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c new file mode 100644 index 000000000000..ca89863aec69 --- /dev/null +++ b/Silicon/Ampere/AmpereAltraPkg/Library/DwI2cLib/DwI2cLib.c @@ -0,0 +1,883 @@ +/** @file + + Copyright (c) 2020 - 2021, Ampere Computing LLC. All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define I2cSync() { asm volatile ("dmb ish" : : : "memory"); } + +// +// Runtime needs to be 64K alignment +// +#define RUNTIME_ADDRESS_MASK (~(SIZE_64KB - 1)) +#define RUNTIME_ADDRESS_LENGTH SIZE_64KB + +// +// Private I2C bus data +// +typedef struct { + UINTN Base; + UINT32 BusSpeed; + UINT32 RxFifo; + UINT32 TxFifo; + UINT32 PollingTime; + UINT32 Enabled; +} DW_I2C_CONTEXT_T; + +// +// I2C SCL counter macros +// +typedef enum { + I2cSpeedModeStandard =3D 0, + I2cSpeedModeFast, +} I2C_SPEED_MODE; + +#define DW_I2C_MAXIMUM_SPEED_HZ 400000 + +typedef enum { + I2cSclSpkLen =3D 0, + I2cSclHcnt, + I2cSclLcnt, +} I2C_SCL_PARAM; + +STATIC UINT32 I2cSclParam[][3] =3D { + /* SPK_LEN, HCNT, LCNT */ + [I2cSpeedModeStandard] =3D { 10, 0x3E2, 0x47D }, // SS (Standard Speed= ) + [I2cSpeedModeFast] =3D { 10, 0xA4, 0x13F }, // FS (Fast Speed) +}; + +STATIC BOOLEAN mI2cRuntimeEnableArray[MAX_PLATFORM_I2C_BUS_NUM] = =3D {FALSE}; +STATIC UINTN mI2cBaseArray[MAX_PLATFORM_I2C_BUS_NUM] =3D {PLATF= ORM_I2C_REGISTER_BASE}; +STATIC DW_I2C_CONTEXT_T mI2cBusList[MAX_PLATFORM_I2C_BUS_NUM]; +STATIC UINTN mI2cClock =3D 0; +STATIC EFI_EVENT mVirtualAddressChangeEvent =3D NULL; + +// +// Registers +// +#define DW_IC_CON 0x0 +#define DW_IC_CON_MASTER BIT0 +#define DW_IC_CON_SPEED_STD BIT1 +#define DW_IC_CON_SPEED_FAST BIT2 +#define DW_IC_CON_10BITADDR_MASTER BIT4 +#define DW_IC_CON_RESTART_EN BIT5 +#define DW_IC_CON_SLAVE_DISABLE BIT6 +#define DW_IC_TAR 0x4 +#define DW_IC_TAR_10BITS BIT12 +#define DW_IC_SAR 0x8 +#define DW_IC_DATA_CMD 0x10 +#define DW_IC_DATA_CMD_RESTART BIT10 +#define DW_IC_DATA_CMD_STOP BIT9 +#define DW_IC_DATA_CMD_CMD BIT8 +#define DW_IC_DATA_CMD_DAT_MASK 0xFF +#define DW_IC_SS_SCL_HCNT 0x14 +#define DW_IC_SS_SCL_LCNT 0x18 +#define DW_IC_FS_SCL_HCNT 0x1c +#define DW_IC_FS_SCL_LCNT 0x20 +#define DW_IC_HS_SCL_HCNT 0x24 +#define DW_IC_HS_SCL_LCNT 0x28 +#define DW_IC_INTR_STAT 0x2c +#define DW_IC_INTR_MASK 0x30 +#define DW_IC_INTR_RX_UNDER BIT0 +#define DW_IC_INTR_RX_OVER BIT1 +#define DW_IC_INTR_RX_FULL BIT2 +#define DW_IC_INTR_TX_EMPTY BIT4 +#define DW_IC_INTR_TX_ABRT BIT6 +#define DW_IC_INTR_ACTIVITY BIT8 +#define DW_IC_INTR_STOP_DET BIT9 +#define DW_IC_INTR_START_DET BIT10 +#define DW_IC_ERR_CONDITION \ + (DW_IC_INTR_RX_UNDER | DW_IC_INTR_RX_OVER | DW_IC_INTR_TX_= ABRT) +#define DW_IC_RAW_INTR_STAT 0x34 +#define DW_IC_CLR_INTR 0x40 +#define DW_IC_CLR_RX_UNDER 0x44 +#define DW_IC_CLR_RX_OVER 0x48 +#define DW_IC_CLR_TX_ABRT 0x54 +#define DW_IC_CLR_ACTIVITY 0x5c +#define DW_IC_CLR_STOP_DET 0x60 +#define DW_IC_CLR_START_DET 0x64 +#define DW_IC_ENABLE 0x6c +#define DW_IC_STATUS 0x70 +#define DW_IC_STATUS_ACTIVITY BIT0 +#define DW_IC_STATUS_TFE BIT2 +#define DW_IC_STATUS_RFNE BIT3 +#define DW_IC_STATUS_MST_ACTIVITY BIT5 +#define DW_IC_TXFLR 0x74 +#define DW_IC_RXFLR 0x78 +#define DW_IC_SDA_HOLD 0x7c +#define DW_IC_TX_ABRT_SOURCE 0x80 +#define DW_IC_ENABLE_STATUS 0x9c +#define DW_IC_COMP_PARAM_1 0xf4 +#define DW_IC_COMP_PARAM_1_RX_BUFFER_DEPTH(x) \ + ((((x) >> 8) & 0xFF) + 1) +#define DW_IC_COMP_PARAM_1_TX_BUFFER_DEPTH(x) \ + ((((x) >> 16) & 0xFF) + 1) +#define DW_IC_COMP_TYPE 0xfc +#define SB_DW_IC_CON 0xa8 +#define SB_DW_IC_SCL_TMO_CNT 0xac +#define SB_DW_IC_RX_PEC 0xb0 +#define SB_DW_IC_ACK 0xb4 +#define SB_DW_IC_FLG 0xb8 +#define SB_DW_IC_FLG_CLR 0xbc +#define SB_DW_IC_INTR_STAT 0xc0 +#define SB_DW_IC_INTR_STAT_MASK 0xc4 +#define SB_DW_IC_DEBUG_SEL 0xec +#define SB_DW_IC_ACK_DEBUG 0xf0 +#define DW_IC_FS_SPKLEN 0xa0 +#define DW_IC_HS_SPKLEN 0xa4 + +// +// Timeout interval +// +// The interval is equal to the 10 times the signaling period +// for the highest I2C transfer speed used in the system. +// +#define DW_POLL_INTERVAL_US(x) (10 * (1000000 / (x))) + +// +// Maximum timeout count +// +#define DW_MAX_TRANSFER_POLL_COUNT 100000 // Maximum timeout: 10s +#define DW_MAX_STATUS_POLL_COUNT 100 + +#define DW_POLL_MST_ACTIVITY_INTERVAL_US 1000 // 1ms +#define DW_MAX_MST_ACTIVITY_POLL_COUNT 20 + +/** + Initialize I2C Bus + **/ +VOID +I2cHWInit ( + UINT32 Bus + ) +{ + UINT32 Param; + + mI2cBusList[Bus].Base =3D mI2cBaseArray[Bus]; + + Param =3D MmioRead32 (mI2cBusList[Bus].Base + DW_IC_COMP_PARAM_1); + + mI2cBusList[Bus].PollingTime =3D DW_POLL_INTERVAL_US (mI2cBusList[Bus].B= usSpeed); + mI2cBusList[Bus].RxFifo =3D DW_IC_COMP_PARAM_1_RX_BUFFER_DEPTH (Param); + mI2cBusList[Bus].TxFifo =3D DW_IC_COMP_PARAM_1_TX_BUFFER_DEPTH (Param); + mI2cBusList[Bus].Enabled =3D 0; + + DEBUG ((DEBUG_VERBOSE, "%a: Bus %d, Rx_Buffer %d, Tx_Buffer %d\n", + __FUNCTION__, + Bus, + mI2cBusList[Bus].RxFifo, + mI2cBusList[Bus].TxFifo + )); +} + +/** + Enable or disable I2C Bus + */ +VOID +I2cEnable ( + UINT32 Bus, + UINT32 Enable + ) +{ + UINT32 I2cStatusCnt; + UINTN Base; + + Base =3D mI2cBusList[Bus].Base; + I2cStatusCnt =3D DW_MAX_STATUS_POLL_COUNT; + mI2cBusList[Bus].Enabled =3D Enable; + + MmioWrite32 (Base + DW_IC_ENABLE, Enable); + + do { + if ((MmioRead32 (Base + DW_IC_ENABLE_STATUS) & 0x01) =3D=3D Enable) { + break; + } + MicroSecondDelay (mI2cBusList[Bus].PollingTime); + } while (I2cStatusCnt-- !=3D 0); + + if (I2cStatusCnt =3D=3D 0) { + DEBUG ((DEBUG_ERROR, "%a: Enable/disable timeout\n", __FUNCTION__)); + } + + if ((Enable =3D=3D 0) || (I2cStatusCnt =3D=3D 0)) { + /* Unset the target adddress */ + MmioWrite32 (Base + DW_IC_TAR, 0); + mI2cBusList[Bus].Enabled =3D 0; + } +} + +/** + Setup Slave address + **/ +VOID +I2cSetSlaveAddr ( + UINT32 Bus, + UINT32 SlaveAddr + ) +{ + UINTN Base; + UINT32 OldEnableStatus; + + Base =3D mI2cBusList[Bus].Base; + OldEnableStatus =3D mI2cBusList[Bus].Enabled; + + I2cEnable (Bus, 0); + MmioWrite32 (Base + DW_IC_TAR, SlaveAddr); + if (OldEnableStatus !=3D 0) { + I2cEnable (Bus, 1); + } +} + +/** + Check for errors on I2C Bus + **/ +UINT32 +I2cCheckErrors ( + UINT32 Bus + ) +{ + UINTN Base; + UINT32 ErrorStatus; + + Base =3D mI2cBusList[Bus].Base; + + ErrorStatus =3D MmioRead32 (Base + DW_IC_RAW_INTR_STAT) & DW_IC_ERR_COND= ITION; + + if ((ErrorStatus & DW_IC_INTR_RX_UNDER) !=3D 0) { + DEBUG ((DEBUG_ERROR, "%a: RX_UNDER error on i2c bus %d error status %0= 8x\n", + __FUNCTION__, + Bus, + ErrorStatus + )); + MmioRead32 (Base + DW_IC_CLR_RX_UNDER); + } + + if ((ErrorStatus & DW_IC_INTR_RX_OVER) !=3D 0) { + DEBUG ((DEBUG_ERROR, "%a: RX_OVER error on i2c bus %d error status %08= x\n", + __FUNCTION__, + Bus, + ErrorStatus + )); + MmioRead32 (Base + DW_IC_CLR_RX_OVER); + } + + if ((ErrorStatus & DW_IC_INTR_TX_ABRT) !=3D 0) { + DEBUG ((DEBUG_VERBOSE, "%a: TX_ABORT at source %08x\n", + __FUNCTION__, + MmioRead32 (Base + DW_IC_TX_ABRT_SOURCE) + )); + MmioRead32 (Base + DW_IC_CLR_TX_ABRT); + } + + return ErrorStatus; +} + +/** + Waiting for bus to not be busy + **/ +BOOLEAN +I2cWaitBusNotBusy ( + UINT32 Bus + ) +{ + UINTN Base; + UINTN PollCount; + + Base =3D mI2cBusList[Bus].Base; + PollCount =3D DW_MAX_MST_ACTIVITY_POLL_COUNT; + + while ((MmioRead32 (Base + DW_IC_STATUS) & DW_IC_STATUS_MST_ACTIVITY) != =3D 0) { + if (PollCount =3D=3D 0) { + DEBUG ((DEBUG_VERBOSE, "%a: Timeout while waiting for bus ready\n", = __FUNCTION__)); + return FALSE; + } + PollCount--; + /* + * A delay isn't absolutely necessary. + * But to ensure that we don't hammer the bus constantly, + * delay for DW_POLL_MST_ACTIVITY_INTERVAL_US as with other implementa= tion. + */ + MicroSecondDelay (DW_POLL_MST_ACTIVITY_INTERVAL_US); + } + + return TRUE; +} + +/** + Waiting for TX FIFO buffer available + **/ +EFI_STATUS +I2cWaitTxData ( + UINT32 Bus + ) +{ + UINTN Base; + UINTN PollCount; + + Base =3D mI2cBusList[Bus].Base; + PollCount =3D 0; + + while (MmioRead32 (Base + DW_IC_TXFLR) =3D=3D mI2cBusList[Bus].TxFifo) { + if (PollCount++ >=3D DW_MAX_TRANSFER_POLL_COUNT) { + DEBUG ((DEBUG_ERROR, "%a: Timeout waiting for TX buffer available\n"= , __FUNCTION__)); + return EFI_TIMEOUT; + } + MicroSecondDelay (mI2cBusList[Bus].PollingTime); + } + + return EFI_SUCCESS; +} + +/** + Waiting for RX FIFO buffer available + **/ +EFI_STATUS +I2cWaitRxData ( + UINT32 Bus + ) +{ + UINTN Base; + UINTN PollCount; + + Base =3D mI2cBusList[Bus].Base; + PollCount =3D 0; + + while ((MmioRead32 (Base + DW_IC_STATUS) & DW_IC_STATUS_RFNE) =3D=3D 0) = { + if (PollCount++ >=3D DW_MAX_TRANSFER_POLL_COUNT) { + DEBUG ((DEBUG_ERROR, "%a: Timeout waiting for RX buffer available\n"= , __FUNCTION__)); + return EFI_TIMEOUT; + } + + if ((I2cCheckErrors (Bus) & DW_IC_INTR_TX_ABRT) !=3D 0) { + return EFI_ABORTED; + } + + MicroSecondDelay (mI2cBusList[Bus].PollingTime); + } + + return EFI_SUCCESS; +} + +/** + Initialize the Designware I2C SCL Counts + + This functions configures SCL clock Count for Standard Speed (SS) and Fas= t Speed (FS) mode. + **/ +VOID +I2cSclInit ( + UINT32 Bus, + UINT32 I2cClkFreq, + UINT32 I2cSpeed + ) +{ + UINT16 IcCon; + UINTN Base; + UINT32 I2cSpeedKhz; + + Base =3D mI2cBusList[Bus].Base; + I2cSpeedKhz =3D I2cSpeed / 1000; + + DEBUG ((DEBUG_VERBOSE, "%a: Bus %d I2cClkFreq %d I2cSpeed %d\n", + __FUNCTION__, + Bus, + I2cClkFreq, + I2cSpeed + )); + + IcCon =3D DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE | DW_IC_CON_RESTART= _EN; + + if (I2cSpeedKhz <=3D 100) { + IcCon |=3D DW_IC_CON_SPEED_STD; + // Standard speed mode + MmioWrite32 (Base + DW_IC_FS_SPKLEN, I2cSclParam[I2cSpeedModeStandard]= [I2cSclSpkLen]); + MmioWrite32 (Base + DW_IC_SS_SCL_HCNT, I2cSclParam[I2cSpeedModeStandar= d][I2cSclHcnt]); + MmioWrite32 (Base + DW_IC_SS_SCL_LCNT, I2cSclParam[I2cSpeedModeStandar= d][I2cSclLcnt]); + } else if (I2cSpeedKhz > 100 && I2cSpeedKhz <=3D 400) { + IcCon |=3D DW_IC_CON_SPEED_FAST; + // Fast speed mode + MmioWrite32 (Base + DW_IC_FS_SPKLEN, I2cSclParam[I2cSpeedModeFast][I2c= SclSpkLen]); + MmioWrite32 (Base + DW_IC_FS_SCL_HCNT, I2cSclParam[I2cSpeedModeFast][I= 2cSclHcnt]); + MmioWrite32 (Base + DW_IC_FS_SCL_LCNT, I2cSclParam[I2cSpeedModeFast][I= 2cSclLcnt]); + } + MmioWrite32 (Base + DW_IC_CON, IcCon); +} + +/** + Initialize the designware i2c master hardware + **/ +EFI_STATUS +I2cInit ( + UINT32 Bus, + UINTN BusSpeed + ) +{ + UINTN Base; + + ASSERT (mI2cClock !=3D 0); + + mI2cBusList[Bus].BusSpeed =3D BusSpeed; + I2cHWInit (Bus); + + Base =3D mI2cBusList[Bus].Base; + + /* Disable the adapter and interrupt */ + I2cEnable (Bus, 0); + MmioWrite32 (Base + DW_IC_INTR_MASK, 0); + + /* Set standard and fast speed divider for high/low periods */ + I2cSclInit (Bus, mI2cClock, BusSpeed); + MmioWrite32 (Base + DW_IC_SDA_HOLD, 0x4b); + + return EFI_SUCCESS; +} + +/** + Wait the transaction finished + **/ +EFI_STATUS +I2cFinish ( + UINT32 Bus + ) +{ + UINTN Base; + UINTN PollCount; + + Base =3D mI2cBusList[Bus].Base; + PollCount =3D 0; + + /* Wait for TX FIFO empty */ + do { + if ((MmioRead32 (Base + DW_IC_STATUS) & DW_IC_STATUS_TFE) !=3D 0) { + break; + } + MicroSecondDelay (mI2cBusList[Bus].PollingTime); + } while (PollCount++ < DW_MAX_TRANSFER_POLL_COUNT); + + if (PollCount >=3D DW_MAX_TRANSFER_POLL_COUNT) { + DEBUG ((DEBUG_ERROR, "%a: Timeout waiting for TX FIFO empty\n", __FUNC= TION__)); + return EFI_TIMEOUT; + } + + /* Wait for STOP signal detected on the bus */ + PollCount =3D 0; + do { + if ((MmioRead32 (Base + DW_IC_RAW_INTR_STAT) & DW_IC_INTR_STOP_DET) != =3D 0) { + MmioRead32 (Base + DW_IC_CLR_STOP_DET); + return EFI_SUCCESS; + } + MicroSecondDelay (mI2cBusList[Bus].PollingTime); + } while (PollCount++ < DW_MAX_TRANSFER_POLL_COUNT); + + DEBUG ((DEBUG_ERROR, "%a: Timeout waiting for transaction finished\n", _= _FUNCTION__)); + return EFI_TIMEOUT; +} + +EFI_STATUS +InternalI2cWrite ( + UINT32 Bus, + UINT8 *Buf, + UINT32 *Length + ) +{ + EFI_STATUS Status; + UINTN WriteCount; + UINTN Base; + + Status =3D EFI_SUCCESS; + Base =3D mI2cBusList[Bus].Base; + + DEBUG ((DEBUG_VERBOSE, "%a: Write Bus %d Buf %p Length %d\n", + __FUNCTION__, + Bus, + Buf, + *Length + )); + I2cEnable (Bus, 1); + + WriteCount =3D 0; + while ((*Length - WriteCount) !=3D 0) { + Status =3D I2cWaitTxData (Bus); + if (EFI_ERROR (Status)) { + MmioWrite32 (Base + DW_IC_DATA_CMD, DW_IC_DATA_CMD_STOP); + I2cSync (); + goto Exit; + } + + if (WriteCount =3D=3D *Length - 1) { + MmioWrite32 ( + Base + DW_IC_DATA_CMD, + (Buf[WriteCount] & DW_IC_DATA_CMD_DAT_MASK) | DW_IC_DATA_CMD_STOP + ); + } else { + MmioWrite32 ( + Base + DW_IC_DATA_CMD, + Buf[WriteCount] & DW_IC_DATA_CMD_DAT_MASK + ); + } + I2cSync (); + WriteCount++; + } + +Exit: + *Length =3D WriteCount; + I2cFinish (Bus); + I2cWaitBusNotBusy (Bus); + I2cEnable (Bus, 0); + + return Status; +} + +EFI_STATUS +InternalI2cRead ( + UINT32 Bus, + UINT8 *BufCmd, + UINT32 CmdLength, + UINT8 *Buf, + UINT32 *Length + ) +{ + EFI_STATUS Status; + UINTN Base; + UINT32 CmdSend; + UINT32 TxLimit, RxLimit; + UINTN Idx; + UINTN Count; + UINTN ReadCount; + UINTN WriteCount; + + Status =3D EFI_SUCCESS; + Base =3D mI2cBusList[Bus].Base; + Count =3D 0; + ReadCount =3D 0; + + DEBUG ((DEBUG_VERBOSE, "%a: Read Bus %d Buf %p Length:%d\n", + __FUNCTION__, + Bus, + Buf, + *Length + )); + + I2cEnable (Bus, 1); + + /* Write command data */ + WriteCount =3D 0; + while (CmdLength !=3D 0) { + TxLimit =3D mI2cBusList[Bus].TxFifo - MmioRead32 (Base + DW_IC_TXFLR); + Count =3D CmdLength > TxLimit ? TxLimit : CmdLength; + + for (Idx =3D 0; Idx < Count; Idx++ ) { + CmdSend =3D BufCmd[WriteCount++] & DW_IC_DATA_CMD_DAT_MASK; + MmioWrite32 (Base + DW_IC_DATA_CMD, CmdSend); + I2cSync (); + + if (I2cCheckErrors (Bus) !=3D 0) { + Status =3D EFI_CRC_ERROR; + goto Exit; + } + CmdLength--; + } + + Status =3D I2cWaitTxData (Bus); + if (EFI_ERROR (Status)) { + MmioWrite32 (Base + DW_IC_DATA_CMD, DW_IC_DATA_CMD_STOP); + I2cSync (); + goto Exit; + } + } + + WriteCount =3D 0; + while ((*Length - ReadCount) !=3D 0) { + TxLimit =3D mI2cBusList[Bus].TxFifo - MmioRead32 (Base + DW_IC_TXFLR); + RxLimit =3D mI2cBusList[Bus].RxFifo - MmioRead32 (Base + DW_IC_RXFLR); + Count =3D *Length - ReadCount; + Count =3D Count > RxLimit ? RxLimit : Count; + Count =3D Count > TxLimit ? TxLimit : Count; + + for (Idx =3D 0; Idx < Count; Idx++ ) { + CmdSend =3D DW_IC_DATA_CMD_CMD; + if (WriteCount =3D=3D *Length - 1) { + CmdSend |=3D DW_IC_DATA_CMD_STOP; + } + MmioWrite32 (Base + DW_IC_DATA_CMD, CmdSend); + I2cSync (); + WriteCount++; + + if (I2cCheckErrors (Bus) !=3D 0) { + DEBUG ((DEBUG_VERBOSE, + "%a: Sending reading command remaining length %d CRC error\n", + __FUNCTION__, + *Length + )); + Status =3D EFI_CRC_ERROR; + goto Exit; + } + } + + for (Idx =3D 0; Idx < Count; Idx++ ) { + Status =3D I2cWaitRxData (Bus); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_VERBOSE, + "%a: Reading remaining length %d failed to wait data\n", + __FUNCTION__, + *Length + )); + + if (Status !=3D EFI_ABORTED) { + MmioWrite32 (Base + DW_IC_DATA_CMD, DW_IC_DATA_CMD_STOP); + I2cSync (); + } + + goto Exit; + } + + Buf[ReadCount++] =3D MmioRead32 (Base + DW_IC_DATA_CMD) & DW_IC_DATA= _CMD_DAT_MASK; + I2cSync (); + + if (I2cCheckErrors (Bus) !=3D 0) { + DEBUG ((DEBUG_VERBOSE, "%a: Reading remaining length %d CRC error\= n", + __FUNCTION__, + *Length + )); + Status =3D EFI_CRC_ERROR; + goto Exit; + } + } + } + +Exit: + *Length =3D ReadCount; + I2cFinish (Bus); + I2cWaitBusNotBusy (Bus); + I2cEnable (Bus, 0); + + return Status; +} + +/** + Write to I2C bus. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in,out] Buf Buffer that holds data to write. + @param[in,out] WriteLength Pointer to length of buffer. + + @return EFI_SUCCESS Write successfully. + @return EFI_INVALID_PARAMETER A parameter is invalid. + @return EFI_UNSUPPORTED The bus is not supported. + @return EFI_NOT_READY The device/bus is not ready. + @return EFI_TIMEOUT Timeout why transferring data. + +**/ +EFI_STATUS +EFIAPI +I2cWrite ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN OUT UINT8 *Buf, + IN OUT UINT32 *WriteLength + ) +{ + if (Bus >=3D MAX_PLATFORM_I2C_BUS_NUM + || Buf =3D=3D NULL + || WriteLength =3D=3D NULL) + { + return EFI_INVALID_PARAMETER; + } + + I2cSetSlaveAddr (Bus, SlaveAddr); + + return InternalI2cWrite (Bus, Buf, WriteLength); +} + +/** + Read data from I2C bus. + + @param[in] Bus I2C bus Id. + @param[in] SlaveAddr The address of slave device on the bus. + @param[in] BufCmd Buffer where to send the command. + @param[in] CmdLength Pointer to length of BufCmd. + @param[in,out] Buf Buffer where to put the read data to. + @param[in,out] ReadLength Pointer to length of buffer. + + @return EFI_SUCCESS Read successfully. + @return EFI_INVALID_PARAMETER A parameter is invalid. + @return EFI_UNSUPPORTED The bus is not supported. + @return EFI_NOT_READY The device/bus is not ready. + @return EFI_TIMEOUT Timeout why transferring data. + @return EFI_CRC_ERROR There are errors on receiving data. + +**/ +EFI_STATUS +EFIAPI +I2cRead ( + IN UINT32 Bus, + IN UINT32 SlaveAddr, + IN UINT8 *BufCmd, + IN UINT32 CmdLength, + IN OUT UINT8 *Buf, + IN OUT UINT32 *ReadLength + ) +{ + if (Bus >=3D MAX_PLATFORM_I2C_BUS_NUM + || Buf =3D=3D NULL + || ReadLength =3D=3D NULL) + { + return EFI_INVALID_PARAMETER; + } + + I2cSetSlaveAddr (Bus, SlaveAddr); + + return InternalI2cRead (Bus, BufCmd, CmdLength, Buf, ReadLength); +} + +/** + Setup new transaction with I2C slave device. + + @param[in] Bus I2C bus Id. + @param[in] BusSpeed I2C bus speed in Hz. + + @retval EFI_SUCCESS Success. + @retval EFI_INVALID_PARAMETER A parameter is invalid. + +**/ +EFI_STATUS +EFIAPI +I2cProbe ( + IN UINT32 Bus, + IN UINTN BusSpeed + ) +{ + if (Bus >=3D MAX_PLATFORM_I2C_BUS_NUM + || BusSpeed > DW_I2C_MAXIMUM_SPEED_HZ) + { + return EFI_INVALID_PARAMETER; + } + + return I2cInit (Bus, BusSpeed); +} + +/** + * Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE. + * + * This is a notification function registered on EVT_SIGNAL_VIRTUAL_ADDRES= S_CHANGE event. + * It convers pointer to new virtual address. + * + * @param Event Event whose notification function is being invoked= . + * @param Context Pointer to the notification function's context. + */ +VOID +EFIAPI +I2cVirtualAddressChangeEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + UINTN Count; + + EfiConvertPointer (0x0, (VOID **)&mI2cBusList); + EfiConvertPointer (0x0, (VOID **)&mI2cBaseArray); + EfiConvertPointer (0x0, (VOID **)&mI2cClock); + for (Count =3D 0; Count < MAX_PLATFORM_I2C_BUS_NUM; Count++) { + if (!mI2cRuntimeEnableArray[Count]) { + continue; + } + EfiConvertPointer (0x0, (VOID **)&mI2cBaseArray[Count]); + EfiConvertPointer (0x0, (VOID **)&mI2cBusList[Count].Base); + } +} + +/** + Setup a bus that to be used in runtime service. + + @param[in] Bus I2C bus Id. + + @retval EFI_SUCCESS Success. + @retval Otherwise Error code. + +**/ +EFI_STATUS +EFIAPI +I2cSetupRuntime ( + IN UINT32 Bus + ) +{ + EFI_STATUS Status; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR Descriptor; + + if (Bus >=3D MAX_PLATFORM_I2C_BUS_NUM) { + return EFI_INVALID_PARAMETER; + } + + if (mVirtualAddressChangeEvent =3D=3D NULL) { + /* + * Register for the virtual address change event + */ + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + I2cVirtualAddressChangeEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mVirtualAddressChangeEvent + ); + ASSERT_EFI_ERROR (Status); + } + + Status =3D gDS->GetMemorySpaceDescriptor ( + mI2cBaseArray[Bus] & RUNTIME_ADDRESS_MASK, + &Descriptor + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D gDS->SetMemorySpaceAttributes ( + mI2cBaseArray[Bus] & RUNTIME_ADDRESS_MASK, + RUNTIME_ADDRESS_LENGTH, + Descriptor.Attributes | EFI_MEMORY_RUNTIME + ); + if (EFI_ERROR (Status)) { + return Status; + } + + mI2cRuntimeEnableArray[Bus] =3D TRUE; + + return Status; +} + +EFI_STATUS +EFIAPI +I2cLibConstructor ( + VOID + ) +{ + VOID *Hob; + PLATFORM_INFO_HOB *PlatformHob; + + /* Get I2C Clock from the Platform HOB */ + Hob =3D GetFirstGuidHob (&gPlatformHobGuid); + if (Hob =3D=3D NULL) { + return EFI_NOT_FOUND; + } + PlatformHob =3D (PLATFORM_INFO_HOB *)GET_GUID_HOB_DATA (Hob); + mI2cClock =3D PlatformHob->AhbClk; + ASSERT (mI2cClock !=3D 0); + + return EFI_SUCCESS; +} --=20 2.17.1