From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from EUR01-VE1-obe.outbound.protection.outlook.com (EUR01-VE1-obe.outbound.protection.outlook.com [40.107.14.87]) by mx.groups.io with SMTP id smtpd.web10.9591.1586866492976266890 for ; Tue, 14 Apr 2020 05:14:53 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@nxp1.onmicrosoft.com header.s=selector2-nxp1-onmicrosoft-com header.b=jZdAPNkk; spf=pass (domain: oss.nxp.com, ip: 40.107.14.87, mailfrom: pankaj.bansal@oss.nxp.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UxCEYM5UuXBO58Sz+uplCAy3WfiLHZaMOjOUnq24/8YCmtF8YVVleuZlAymQXVqwpDEJLry6bB6o2aUfs4W2qTq+4uG4hDYdiAc5H/tDuqPVrft7CeBpYL/oYF9QATFuVZal+9PSpYrmEXWWuimODGHVqspQnTg58tEcIQdCMwdz0t2ILG9/+Q2XhYeFAY23PKvYLrAW6qjVd+PnbzdzYYceJhfoVtfpZhcOp6bbWYJV3KyyqsgqxQpskWDUEsHt5iDVSbLjh81yDKUJRK8cJy+MvAIFGXRTIFDCp/hwPB7ynU55IGTOnxCq8cp4OKmtJv8KI3LZTHuRylVowFCRpA== 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:X-MS-Exchange-SenderADCheck; bh=6TIP7kEZTrSpLrC0XOiSzajj5RC0paxRe99y/IGNVfM=; b=D/9zkMwSUrKdDl60nzbfnG4CfgAx09ps+52mTPY85KzwZTkfGucS/cOlYphK+1JeWnZsWBiW1R8Z6c/bZ+DWHk4rfwgO0fXBB/Fj9vIzUWIOR4IG95cY0zeGmvKPVjP94qZkVuQu1w2FnoYpnQZ7a7j2ZKpS+qMtW9Uk0I6eXoI2oQlFDZ+sK+/9UNXNZdW0Lwn1JQH8K6L9fKh2CaTEJBEHRFOTrMwP1w2GFxjPn5ekn1rp4L/wHW2aKZg7xbGJcY1eorFomyifFhMS3Hafzo0RKVNS3X/PZSNl5MLXK93KhBl8VdjjhplRJwoJ5ukjKxSDzlSWEG9Y6B5ALVlJAg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6TIP7kEZTrSpLrC0XOiSzajj5RC0paxRe99y/IGNVfM=; b=jZdAPNkkQLbw24/NhuVZm5bntwkwQqiSCCdchqIdi9AVkNTtTbxHCf1xlawYTKPNvFFsxe/Lau7QFUIEsLnkf9y8z9QfpMaxeZM5zarVel1abGflhpFpkb72frHLuyvFe+SncO2YRurv5pa+4c6rvjGkRTaMBarNDNnHTGGnE3o= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=pankaj.bansal@oss.nxp.com; Received: from VI1PR04MB5933.eurprd04.prod.outlook.com (2603:10a6:803:ec::16) by VI1PR04MB4461.eurprd04.prod.outlook.com (2603:10a6:803:70::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.28; Tue, 14 Apr 2020 12:14:50 +0000 Received: from VI1PR04MB5933.eurprd04.prod.outlook.com ([fe80::45c4:8846:5327:9513]) by VI1PR04MB5933.eurprd04.prod.outlook.com ([fe80::45c4:8846:5327:9513%7]) with mapi id 15.20.2900.028; Tue, 14 Apr 2020 12:14:50 +0000 From: "Pankaj Bansal" To: Leif Lindholm , Meenakshi Aggarwal , Michael D Kinney , devel@edk2.groups.io, Varun Sethi , Samer El-Haj-Mahmoud , Jon Nettleton , Ard Biesheuvel Subject: [PATCH edk2-platforms v3 02/24] Silicon/NXP: changes to use I2clib in i2cdxe Date: Wed, 15 Apr 2020 17:43:20 +0530 Message-ID: <20200415121342.9246-3-pankaj.bansal@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200415121342.9246-1-pankaj.bansal@oss.nxp.com> References: <20200415121342.9246-1-pankaj.bansal@oss.nxp.com> X-ClientProxiedBy: SG2PR01CA0092.apcprd01.prod.exchangelabs.com (2603:1096:3:15::18) To VI1PR04MB5933.eurprd04.prod.outlook.com (2603:10a6:803:ec::16) Return-Path: pankaj.bansal@oss.nxp.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from uefi-workstation.ap.freescale.net (92.120.0.69) by SG2PR01CA0092.apcprd01.prod.exchangelabs.com (2603:1096:3:15::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.15 via Frontend Transport; Tue, 14 Apr 2020 12:14:47 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [92.120.0.69] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: fac4f4b7-8d53-48ad-173e-08d7e06d6e1d X-MS-TrafficTypeDiagnostic: VI1PR04MB4461:|VI1PR04MB4461: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-Forefront-PRVS: 0373D94D15 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR04MB5933.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10009020)(4636009)(376002)(366004)(346002)(136003)(39850400004)(396003)(30864003)(1076003)(186003)(8936002)(16526019)(86362001)(110136005)(26005)(478600001)(81156014)(8676002)(6512007)(66946007)(316002)(44832011)(66476007)(66556008)(52116002)(5660300002)(6666004)(2906002)(6486002)(6506007)(956004)(19627235002)(2616005);DIR:OUT;SFP:1101; Received-SPF: None (protection.outlook.com: oss.nxp.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2lGA09wkqiCrY8r0uJny+oZBPn5O82KlPJZZ+npXA7h6EjEV8zuSa2ROoVELGFtoFy8lIP8p0zauFLRH8p7kOLr8dBLWxjssQfbdrk8A3xlvV+b8UYW9X2+pdJ/B+xBTsCEOg1sdvi5RENUUYNRu1Pd2RBrLJJlTHTQv6lg5wB625+Ur2PrJ1gOhJ0+AfDVG+BMFR/m3FHt/thDDcv3CrdKGuLSdic+GrSAxcPKCsNSB8NCQKWs5UWv+3uSXqyn9dmpMqvI+dzOxrd5pA/R0ayQK9ta1yskc8k00CpxsuhwEWzXHjurDpLfcGNyzrt1r147p5RaQG+FXsIMd2QMC9UzBXy+TMUQsoXks2RWyCCVS3HpKpCQ4Vd74lp6tmxl1XYB6Hf2iu0j6XF1Jot6+DRtOSo24E+SgjBrtot4MEHeUlR0vQUuk2eG1xq80/YyE X-MS-Exchange-AntiSpam-MessageData: QXRto0SAh9PRzEepLSrn6GSPovGP0Ee5nWKwiAubE7oX2zr7khIGOfKbp70ymZexHhBVYSi6CArKfGjmMKj2Vg2Jo75VhoaHj+M/zkKXdBTflQ5YKq/Cpmo5P59VHI07h6bGDPbguIVO5IjqxQH8GQ== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: fac4f4b7-8d53-48ad-173e-08d7e06d6e1d X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Apr 2020 12:14:50.3209 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: O7cKqBxmrtHO6EIDqlA4tpwZJ9QuxRe/I+xOATkHWTPfVKvYOBEATANuBhCoVuPIbp2gMHQrJYIM9JMkOGKWEQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB4461 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain From: Pankaj Bansal I2c lib contains the i2c controller functionality. this can be used in I2c DXE driver to communicate with i2c devices. Signed-off-by: Pankaj Bansal --- Notes: - moved I2cLib addition to NxpQoriqLs.dsc.inc Platform/NXP/NxpQoriqLs.dsc.inc | 4 +- Silicon/NXP/Drivers/I2cDxe/I2cDxe.inf | 9 +- Silicon/NXP/Drivers/I2cDxe/I2cDxe.h | 44 +- Silicon/NXP/Drivers/I2cDxe/I2cDxe.c | 526 +------------------- 4 files changed, 19 insertions(+), 564 deletions(-) diff --git a/Platform/NXP/NxpQoriqLs.dsc.inc b/Platform/NXP/NxpQoriqLs.dsc.= inc index fa5f30dd3909..25c0a41e5d4a 100644 --- a/Platform/NXP/NxpQoriqLs.dsc.inc +++ b/Platform/NXP/NxpQoriqLs.dsc.inc @@ -1,6 +1,6 @@ # @file # -# Copyright 2017-2019 NXP. +# Copyright 2017-2020 NXP # # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -94,6 +94,8 @@ NonDiscoverableDeviceRegistrationLib|MdeModulePkg/Library/NonDiscoverabl= eDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeRepor= tStatusCodeLib.inf =20 + I2cLib|Silicon/NXP/Library/I2cLib/I2cLib.inf + [LibraryClasses.common.SEC] PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf UefiDecompressLib|MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompres= sLib.inf diff --git a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.inf b/Silicon/NXP/Drivers/I2= cDxe/I2cDxe.inf index 0c0bf63bb2e2..84adb837c249 100644 --- a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.inf +++ b/Silicon/NXP/Drivers/I2cDxe/I2cDxe.inf @@ -3,7 +3,7 @@ # Component description file for I2c driver # # Copyright (c) 2015, Freescale Semiconductor, Inc. All rights reserved. -# Copyright 2017-2019 NXP +# Copyright 2017-2020 NXP # # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -27,6 +27,7 @@ ArmLib BaseMemoryLib DevicePathLib + I2cLib IoLib MemoryAllocationLib PcdLib @@ -48,11 +49,5 @@ gEdkiiNonDiscoverableDeviceProtocolGuid ## TO_START gEfiI2cMasterProtocolGuid ## BY_START =20 -[Pcd] - gNxpQoriqLsTokenSpaceGuid.PcdI2cSpeed - gNxpQoriqLsTokenSpaceGuid.PcdI2c0BaseAddr - gNxpQoriqLsTokenSpaceGuid.PcdI2cSize - gNxpQoriqLsTokenSpaceGuid.PcdNumI2cController - [Depex] TRUE diff --git a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.h b/Silicon/NXP/Drivers/I2cD= xe/I2cDxe.h index 02a29a5cf2b9..88316f313380 100644 --- a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.h +++ b/Silicon/NXP/Drivers/I2cDxe/I2cDxe.h @@ -1,7 +1,7 @@ /** I2cDxe.h Header defining the constant, base address amd function for I2C controll= er =20 - Copyright 2017-2019 NXP + Copyright 2017-2020 NXP =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -16,32 +16,6 @@ #include #include =20 -#define I2C_CR_IIEN (1 << 6) -#define I2C_CR_MSTA (1 << 5) -#define I2C_CR_MTX (1 << 4) -#define I2C_CR_TX_NO_AK (1 << 3) -#define I2C_CR_RSTA (1 << 2) - -#define I2C_SR_ICF (1 << 7) -#define I2C_SR_IBB (1 << 5) -#define I2C_SR_IAL (1 << 4) -#define I2C_SR_IIF (1 << 1) -#define I2C_SR_RX_NO_AK (1 << 0) - -#define I2C_CR_IEN (0 << 7) -#define I2C_CR_IDIS (1 << 7) -#define I2C_SR_IIF_CLEAR (1 << 1) - -#define BUS_IDLE (0 | (I2C_SR_IBB << 8)) -#define BUS_BUSY (I2C_SR_IBB | (I2C_SR_IBB << 8)) -#define IIF (I2C_SR_IIF | (I2C_SR_IIF << 8)) - -#define I2C_FLAG_WRITE 0x0 - -#define I2C_STATE_RETRIES 50000 - -#define RETRY_COUNT 3 - #define NXP_I2C_SIGNATURE SIGNATURE_32 ('N', 'I', '2', 'C') #define NXP_I2C_FROM_THIS(a) CR ((a), NXP_I2C_MASTER, \ I2cMaster, NXP_I2C_SIGNATURE) @@ -63,22 +37,6 @@ typedef struct { NON_DISCOVERABLE_DEVICE *Dev; } NXP_I2C_MASTER; =20 -/** - Record defining i2c registers -**/ -typedef struct { - UINT8 I2cAdr; - UINT8 I2cFdr; - UINT8 I2cCr; - UINT8 I2cSr; - UINT8 I2cDr; -} I2C_REGS; - -typedef struct { - UINT16 SCLDivider; - UINT16 BusClockRate; -} CLK_DIV; - extern UINT64 GetBusFrequency ( diff --git a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.c b/Silicon/NXP/Drivers/I2cD= xe/I2cDxe.c index 853c426fbca2..848e707c1673 100644 --- a/Silicon/NXP/Drivers/I2cDxe/I2cDxe.c +++ b/Silicon/NXP/Drivers/I2cDxe/I2cDxe.c @@ -1,7 +1,7 @@ /** I2cDxe.c I2c driver APIs for read, write, initialize, set speed and reset =20 - Copyright 2017-2019 NXP + Copyright 2017-2020 NXP =20 SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -25,444 +26,6 @@ STATIC CONST EFI_I2C_CONTROLLER_CAPABILITIES mI2cContro= llerCapabilities =3D { 0 }; =20 -STATIC CONST CLK_DIV mClkDiv[] =3D { - { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 }, - { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 }, - { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D }, - { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 }, - { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 }, - { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 }, - { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 }, - { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 }, - { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 }, - { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B }, - { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 }, - { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 }, - { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B }, - { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A }, - { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E } -}; - -/** - Calculate and return proper clock divider - - @param Rate desired clock rate - - @retval ClkDiv Index value used to get Bus Clock Rate - -**/ -STATIC -UINT8 -GetClkDivIndex ( - IN UINT32 Rate - ) -{ - UINTN ClkRate; - UINT32 Div; - UINT8 Index; - - Index =3D 0; - ClkRate =3D GetBusFrequency (); - - Div =3D (ClkRate + Rate - 1) / Rate; - - if (Div < mClkDiv[0].SCLDivider) { - return 0; - } - - do { - if (mClkDiv[Index].SCLDivider >=3D Div ) { - return Index; - } - Index++; - } while (Index < ARRAY_SIZE (mClkDiv)); - - return (ARRAY_SIZE (mClkDiv) - 1); -} - -/** - Function used to check if i2c is in mentioned state or not - - @param I2cRegs Pointer to I2C registers - @param State i2c state need to be checked - - @retval EFI_NOT_READY Arbitration was lost - @retval EFI_TIMEOUT Timeout occured - @retval CurrState Value of state register - -**/ -STATIC -EFI_STATUS -WaitForI2cState ( - IN I2C_REGS *I2cRegs, - IN UINT32 State - ) -{ - UINT8 CurrState; - UINT64 Count; - - for (Count =3D 0; Count < I2C_STATE_RETRIES; Count++) { - MemoryFence (); - CurrState =3D MmioRead8 ((UINTN)&I2cRegs->I2cSr); - if (CurrState & I2C_SR_IAL) { - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, CurrState | I2C_SR_IAL); - return EFI_NOT_READY; - } - - if ((CurrState & (State >> 8)) =3D=3D (UINT8)State) { - return CurrState; - } - } - - return EFI_TIMEOUT; -} - -/** - Function to transfer byte on i2c - - @param I2cRegs Pointer to i2c registers - @param Byte Byte to be transferred on i2c bus - - @retval EFI_NOT_READY Arbitration was lost - @retval EFI_TIMEOUT Timeout occured - @retval EFI_NOT_FOUND ACK was not recieved - @retval EFI_SUCCESS Data transfer was succesful - -**/ -STATIC -EFI_STATUS -TransferByte ( - IN I2C_REGS *I2cRegs, - IN UINT8 Byte - ) -{ - EFI_STATUS RetVal; - - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); - MmioWrite8 ((UINTN)&I2cRegs->I2cDr, Byte); - - RetVal =3D WaitForI2cState (I2cRegs, IIF); - if ((RetVal =3D=3D EFI_TIMEOUT) || (RetVal =3D=3D EFI_NOT_READY)) { - return RetVal; - } - - if (RetVal & I2C_SR_RX_NO_AK) { - return EFI_NOT_FOUND; - } - - return EFI_SUCCESS; -} - -/** - Function to stop transaction on i2c bus - - @param I2cRegs Pointer to i2c registers - - @retval EFI_NOT_READY Arbitration was lost - @retval EFI_TIMEOUT Timeout occured - @retval EFI_SUCCESS Stop operation was successful - -**/ -STATIC -EFI_STATUS -I2cStop ( - IN I2C_REGS *I2cRegs - ) -{ - EFI_STATUS RetVal; - UINT32 Temp; - - Temp =3D MmioRead8 ((UINTN)&I2cRegs->I2cCr); - - Temp &=3D ~(I2C_CR_MSTA | I2C_CR_MTX); - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - - RetVal =3D WaitForI2cState (I2cRegs, BUS_IDLE); - - if (RetVal < 0) { - return RetVal; - } else { - return EFI_SUCCESS; - } -} - -/** - Function to send start signal, Chip Address and - memory offset - - @param I2cRegs Pointer to i2c base registers - @param Chip Chip Address - @param Offset Slave memory's offset - @param AddressLength length of chip address - - @retval EFI_NOT_READY Arbitration lost - @retval EFI_TIMEOUT Failed to initialize data transfer in predefine= d time - @retval EFI_NOT_FOUND ACK was not recieved - @retval EFI_SUCCESS Read was successful - -**/ -STATIC -EFI_STATUS -InitTransfer ( - IN I2C_REGS *I2cRegs, - IN UINT8 Chip, - IN UINT32 Offset, - IN INT32 AddressLength - ) -{ - UINT32 Temp; - EFI_STATUS RetVal; - - // Enable I2C controller - if (MmioRead8 ((UINTN)&I2cRegs->I2cCr) & I2C_CR_IDIS) { - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, I2C_CR_IEN); - } - - if (MmioRead8 ((UINTN)&I2cRegs->I2cAdr) =3D=3D (Chip << 1)) { - MmioWrite8 ((UINTN)&I2cRegs->I2cAdr, (Chip << 1) ^ 2); - } - - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); - RetVal =3D WaitForI2cState (I2cRegs, BUS_IDLE); - if ((RetVal =3D=3D EFI_TIMEOUT) || (RetVal =3D=3D EFI_NOT_READY)) { - return RetVal; - } - - // Start I2C transaction - Temp =3D MmioRead8 ((UINTN)&I2cRegs->I2cCr); - // set to master mode - Temp |=3D I2C_CR_MSTA; - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - - RetVal =3D WaitForI2cState (I2cRegs, BUS_BUSY); - if ((RetVal =3D=3D EFI_TIMEOUT) || (RetVal =3D=3D EFI_NOT_READY)) { - return RetVal; - } - - Temp |=3D I2C_CR_MTX | I2C_CR_TX_NO_AK; - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - - // write slave Address - RetVal =3D TransferByte (I2cRegs, Chip << 1); - if (RetVal !=3D EFI_SUCCESS) { - return RetVal; - } - - if (AddressLength >=3D 0) { - while (AddressLength--) { - RetVal =3D TransferByte (I2cRegs, (Offset >> (AddressLength * 8)) & = 0xff); - if (RetVal !=3D EFI_SUCCESS) - return RetVal; - } - } - return EFI_SUCCESS; -} - -/** - Function to check if i2c bus is idle - - @param Base Pointer to base address of I2c controller - - @retval EFI_SUCCESS - -**/ -STATIC -INT32 -I2cBusIdle ( - IN VOID *Base - ) -{ - return EFI_SUCCESS; -} - -/** - Function to initiate data transfer on i2c bus - - @param I2cRegs Pointer to i2c base registers - @param Chip Chip Address - @param Offset Slave memory's offset - @param AddressLength length of chip address - - @retval EFI_NOT_READY Arbitration lost - @retval EFI_TIMEOUT Failed to initialize data transfer in predefine= d time - @retval EFI_NOT_FOUND ACK was not recieved - @retval EFI_SUCCESS Read was successful - -**/ -STATIC -EFI_STATUS -InitDataTransfer ( - IN I2C_REGS *I2cRegs, - IN UINT8 Chip, - IN UINT32 Offset, - IN INT32 AddressLength - ) -{ - EFI_STATUS RetVal; - INT32 Retry; - - for (Retry =3D 0; Retry < RETRY_COUNT; Retry++) { - RetVal =3D InitTransfer (I2cRegs, Chip, Offset, AddressLength); - if (RetVal =3D=3D EFI_SUCCESS) { - return EFI_SUCCESS; - } - - I2cStop (I2cRegs); - - if (EFI_NOT_FOUND =3D=3D RetVal) { - return RetVal; - } - - // Disable controller - if (RetVal !=3D EFI_NOT_READY) { - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, I2C_CR_IDIS); - } - - if (I2cBusIdle (I2cRegs) < 0) { - break; - } - } - return RetVal; -} - -/** - Function to read data using i2c bus - - @param BaseAddr I2c Controller Base Address - @param Chip Address of slave device from where data to be r= ead - @param Offset Offset of slave memory - @param AddressLength Address length of slave - @param Buffer A pointer to the destination buffer for the dat= a - @param Len Length of data to be read - - @retval EFI_NOT_READY Arbitration lost - @retval EFI_TIMEOUT Failed to initialize data transfer in predefine= d time - @retval EFI_NOT_FOUND ACK was not recieved - @retval EFI_SUCCESS Read was successful - -**/ -STATIC -EFI_STATUS -I2cDataRead ( - IN UINTN BaseAddr, - IN UINT8 Chip, - IN UINT32 Offset, - IN UINT32 AddressLength, - IN UINT8 *Buffer, - IN UINT32 Len - ) -{ - EFI_STATUS RetVal; - UINT32 Temp; - INT32 I; - I2C_REGS *I2cRegs; - - I2cRegs =3D (I2C_REGS *)(BaseAddr); - - RetVal =3D InitDataTransfer (I2cRegs, Chip, Offset, AddressLength); - if (RetVal !=3D EFI_SUCCESS) { - return RetVal; - } - - Temp =3D MmioRead8 ((UINTN)&I2cRegs->I2cCr); - Temp |=3D I2C_CR_RSTA; - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - - RetVal =3D TransferByte (I2cRegs, (Chip << 1) | 1); - if (RetVal !=3D EFI_SUCCESS) { - I2cStop (I2cRegs); - return RetVal; - } - - // setup bus to read data - Temp =3D MmioRead8 ((UINTN)&I2cRegs->I2cCr); - Temp &=3D ~(I2C_CR_MTX | I2C_CR_TX_NO_AK); - if (Len =3D=3D 1) { - Temp |=3D I2C_CR_TX_NO_AK; - } - - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); - - // Dummy Read to initiate recieve operation - MmioRead8 ((UINTN)&I2cRegs->I2cDr); - - for (I =3D 0; I < Len; I++) { - RetVal =3D WaitForI2cState (I2cRegs, IIF); - if ((RetVal =3D=3D EFI_TIMEOUT) || (RetVal =3D=3D EFI_NOT_READY)) { - I2cStop (I2cRegs); - return RetVal; - } - // - // It must generate STOP before read I2DR to prevent - // controller from generating another clock cycle - // - if (I =3D=3D (Len - 1)) { - I2cStop (I2cRegs); - } else if (I =3D=3D (Len - 2)) { - Temp =3D MmioRead8 ((UINTN)&I2cRegs->I2cCr); - Temp |=3D I2C_CR_TX_NO_AK; - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, Temp); - } - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); - Buffer[I] =3D MmioRead8 ((UINTN)&I2cRegs->I2cDr); - } - - I2cStop (I2cRegs); - - return EFI_SUCCESS; -} - -/** - Function to write data using i2c bus - - @param BaseAddr I2c Controller Base Address - @param Chip Address of slave device where data to be writte= n - @param Offset Offset of slave memory - @param AddressLength Address length of slave - @param Buffer A pointer to the source buffer for the data - @param Len Length of data to be write - - @retval EFI_NOT_READY Arbitration lost - @retval EFI_TIMEOUT Failed to initialize data transfer in predefine= d time - @retval EFI_NOT_FOUND ACK was not recieved - @retval EFI_SUCCESS Read was successful - -**/ -STATIC -EFI_STATUS -I2cDataWrite ( - IN UINTN BaseAddr, - IN UINT8 Chip, - IN UINT32 Offset, - IN INT32 AddressLength, - OUT UINT8 *Buffer, - IN INT32 Len - ) -{ - EFI_STATUS RetVal; - I2C_REGS *I2cRegs; - INT32 I; - - I2cRegs =3D (I2C_REGS *)BaseAddr; - - RetVal =3D InitDataTransfer (I2cRegs, Chip, Offset, AddressLength); - if (RetVal !=3D EFI_SUCCESS) { - return RetVal; - } - - // Write operation - for (I =3D 0; I < Len; I++) { - RetVal =3D TransferByte (I2cRegs, Buffer[I]); - if (RetVal !=3D EFI_SUCCESS) { - break; - } - } - - I2cStop (I2cRegs); - return RetVal; -} - /** Function to set i2c bus frequency =20 @@ -479,22 +42,17 @@ SetBusFrequency ( IN OUT UINTN *BusClockHertz ) { - I2C_REGS *I2cRegs; - UINT8 ClkId; - UINT8 SpeedId; + UINTN I2cBase; + UINT64 I2cClock; NXP_I2C_MASTER *I2c; =20 I2c =3D NXP_I2C_FROM_THIS (This); =20 - I2cRegs =3D (I2C_REGS *)(I2c->Dev->Resources[0].AddrRangeMin); + I2cBase =3D (UINTN)(I2c->Dev->Resources[0].AddrRangeMin); =20 - ClkId =3D GetClkDivIndex (*BusClockHertz); - SpeedId =3D mClkDiv[ClkId].BusClockRate; + I2cClock =3D GetBusFrequency (); =20 - // Store divider value - MmioWrite8 ((UINTN)&I2cRegs->I2cFdr, SpeedId); - - MemoryFence (); + I2cInitialize (I2cBase, I2cClock, *BusClockHertz); =20 return EFI_SUCCESS; } @@ -513,19 +71,6 @@ Reset ( IN CONST EFI_I2C_MASTER_PROTOCOL *This ) { - I2C_REGS *I2cRegs; - NXP_I2C_MASTER *I2c; - - I2c =3D NXP_I2C_FROM_THIS (This); - - I2cRegs =3D (I2C_REGS *)(I2c->Dev->Resources[0].AddrRangeMin); - - // Reset module - MmioWrite8 ((UINTN)&I2cRegs->I2cCr, I2C_CR_IDIS); - MmioWrite8 ((UINTN)&I2cRegs->I2cSr, 0); - - MemoryFence (); - return EFI_SUCCESS; } =20 @@ -540,62 +85,17 @@ StartRequest ( OUT EFI_STATUS *I2cStatus OPTIONAL ) { - NXP_I2C_MASTER *I2c; - UINT32 Count; - INT32 RetVal; - UINT32 Length; - UINT8 *Buffer; - UINT32 Flag; - UINT32 RegAddress; - UINT32 OffsetLength; - - RegAddress =3D 0; + NXP_I2C_MASTER *I2c; + UINTN I2cBase; + EFI_STATUS Status; =20 I2c =3D NXP_I2C_FROM_THIS (This); =20 - if (RequestPacket->OperationCount <=3D 0) { - DEBUG ((DEBUG_ERROR,"%a: Operation count is not valid %d\n", - __FUNCTION__, RequestPacket->OperationCount)); - return EFI_INVALID_PARAMETER; - } + I2cBase =3D (UINTN)(I2c->Dev->Resources[0].AddrRangeMin); =20 - OffsetLength =3D RequestPacket->Operation[0].LengthInBytes; - RegAddress =3D *RequestPacket->Operation[0].Buffer; + Status =3D I2cBusXfer (I2cBase, SlaveAddress, RequestPacket); =20 - for (Count =3D 1; Count < RequestPacket->OperationCount; Count++) { - Flag =3D RequestPacket->Operation[Count].Flags; - Length =3D RequestPacket->Operation[Count].LengthInBytes; - Buffer =3D RequestPacket->Operation[Count].Buffer; - - if (Length <=3D 0) { - DEBUG ((DEBUG_ERROR,"%a: Invalid length of buffer %d\n", - __FUNCTION__, Length)); - return EFI_INVALID_PARAMETER; - } - - if (Flag =3D=3D I2C_FLAG_READ) { - RetVal =3D I2cDataRead (I2c->Dev->Resources[0].AddrRangeMin, SlaveAd= dress, - RegAddress, OffsetLength, Buffer, Length); - if (RetVal !=3D EFI_SUCCESS) { - DEBUG ((DEBUG_ERROR,"%a: I2c read operation failed (error %d)\n", - __FUNCTION__, RetVal)); - return RetVal; - } - } else if (Flag =3D=3D I2C_FLAG_WRITE) { - RetVal =3D I2cDataWrite (I2c->Dev->Resources[0].AddrRangeMin, SlaveA= ddress, - RegAddress, OffsetLength, Buffer, Length); - if (RetVal !=3D EFI_SUCCESS) { - DEBUG ((DEBUG_ERROR,"%a: I2c write operation failed (error %d)\n", - __FUNCTION__, RetVal)); - return RetVal; - } - } else { - DEBUG ((DEBUG_ERROR,"%a: Invalid Flag %d\n", __FUNCTION__, Flag)); - return EFI_INVALID_PARAMETER; - } - } - - return EFI_SUCCESS; + return Status; } =20 EFI_STATUS --=20 2.17.1