From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=104.47.36.82; helo=nam02-sn1-obe.outbound.protection.outlook.com; envelope-from=meenakshi.aggarwal@nxp.com; receiver=edk2-devel@lists.01.org Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on0082.outbound.protection.outlook.com [104.47.36.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B13CD2034D825 for ; Tue, 7 Nov 2017 00:50:38 -0800 (PST) Received: from MWHPR03CA0056.namprd03.prod.outlook.com (10.174.173.173) by BN3PR03MB2353.namprd03.prod.outlook.com (10.166.74.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.197.13; Tue, 7 Nov 2017 08:54:36 +0000 Received: from BL2FFO11FD047.protection.gbl (2a01:111:f400:7c09::180) by MWHPR03CA0056.outlook.office365.com (2603:10b6:301:3b::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.197.13 via Frontend Transport; Tue, 7 Nov 2017 08:54:35 +0000 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=fail action=none header.from=nxp.com; Received-SPF: Fail (protection.outlook.com: domain of nxp.com does not designate 192.88.168.50 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.168.50; helo=tx30smr01.am.freescale.net; Received: from tx30smr01.am.freescale.net (192.88.168.50) by BL2FFO11FD047.mail.protection.outlook.com (10.173.161.209) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.178.5 via Frontend Transport; Tue, 7 Nov 2017 08:54:35 +0000 Received: from uefi-OptiPlex-790.ap.freescale.net (uefi-OptiPlex-790.ap.freescale.net [10.232.132.78]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id vA78s4Dd009319; Tue, 7 Nov 2017 01:54:32 -0700 From: Meenakshi Aggarwal To: , , , Date: Tue, 7 Nov 2017 20:12:12 +0530 Message-ID: <1510065736-9394-7-git-send-email-meenakshi.aggarwal@nxp.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1510065736-9394-1-git-send-email-meenakshi.aggarwal@nxp.com> References: <1510065736-9394-1-git-send-email-meenakshi.aggarwal@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131545184758548163; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(336005)(7966004)(346002)(376002)(39380400002)(39860400002)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(85426001)(33646002)(106466001)(104016004)(305945005)(356003)(2906002)(50986999)(76176999)(105606002)(551934003)(53376002)(966005)(4326008)(36756003)(53936002)(5660300001)(47776003)(498600001)(8656006)(189998001)(16799955002)(15188155005)(2950100002)(6306002)(6666003)(5003940100001)(2201001)(16586007)(110136005)(68736007)(86362001)(575784001)(50466002)(54906003)(48376002)(77096006)(8676002)(97736004)(50226002)(316002)(81156014)(81166006)(8936002)(19627235001); DIR:OUT; SFP:1101; SCL:1; SRVR:BN3PR03MB2353; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD047; 1:lHiLOC24hH48vuTaWZaAjCw8uTfE9PQC6cThldZly9Agif7fOeLFnezN8vyi12N5ORc3EjHIfsIgMmaAH2DdJ/VltWELfiAnj/THh1lIvYoEpHtpMk5KlZ/cEbb+YrF5 MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: c5643cae-5b99-47bb-e564-08d525bd2be6 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(4534020)(4628075)(201703131517081)(2017052603199); SRVR:BN3PR03MB2353; X-Microsoft-Exchange-Diagnostics: 1; BN3PR03MB2353; 3:dDGTq51nlLSNYfh7Y6q+PqOBcBCWZhdslZuM6Vlehsbi2f+ltV3FVHFQcjG338Wwn32GI1MdEbPbOPlmM+fZPovnuVBSe25vGqd6MPddlOk6ci+kTQppb8KnhGMdcyU0y+ReDBrCgx6uqNK0BLZ/RFzOmXlUbXj4MwrkfYObTcSQVlh/JwkIBG4tXwbZFQHK+KtRrh0gPdWu4xCoKxqJmgP3REcgdLWxKkyEYc4LCXeN+Z9xf/XAp5tD1h4Tn6fKaztb9CTXuo5YnR3svz/yaVmyjlDw3gmvbQzC+S6u2Fu1bckiLzPSvNCH0bPVWJY3HlMIZWQzP4p2G13L3YkaF/b/VthyIpVsno2M0sq43D0=; 25:2TWx2oIRBjXfOZZlIzzQOrAlq/D75jF9dC0cPNrokSQIjZdIo1IXdM8K6Q5VbCNV0WyoiB5HFJ5FYl+y/3iFkMuWgJ//pkQgOiWvjn1xeV5gaMWZf3Nu5jxoDVJdP8shq31aOryJeESA/Kqtt+yTqv0q78qY+gfLgJIioaJ8C7YQ5ljaxtpkkLG+9GKkKNFyKNENNyKss/1Gs1w3+D8nlk4dDYKsgjBzfGBgtPQRY+ZCBAmQGvgqmm2UWQIPHvtWmGC4oMpAGVRaO908Rw9CC5Y4Ayc0baJpsNqLeb31HuFg80H5QXoI//6Eb+uNG99+yvW3nqT+0NSHQ75afXkr5g== X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN3PR03MB2353: X-Microsoft-Exchange-Diagnostics: 1; BN3PR03MB2353; 31:xxYTBJzI9Hn4jCeqCGsO0P7rmsgSgNR+jcmod/jljBBzCcgnWlgo2KcrLAPeOoX1WSe5RqCRO3REE3MZXxwlrYTNMrLyjLgssr9yyM3I/CjzfCDkXTxm2XsXNxmDbQlfBJSI0rEXOnmqVWWn6KGiLNHc/XKziLpsDvyNNcXR8xssiMDcez2v2QLYdUTAAS78ge32p+M2YouM6gWCWQ1xpsJ4ljcC4bpd6ZiyLxZPyO0=; 4:7QfDfel0NM23M9ltzsmXlaNfXsahngQv2riR6gmLBT6gOgBIGPioVTWGkXGZVCeRWsTTzxL7xDWSGe+vV2Mc8hH/nPo9BynaYzTmaGP9crP3HJR9Gf+XKs0s3ucbJ1pcb2TshvSU79/VAO8IGwyZfNwkhLwcrzvULmoQY+PSZdRDj2Ax5P8tY+Z/+l1QCVP+NXOISOXS7WJiOhWdu0UIrjjswWTdJSTAoCm63Jmz/ogsN+Ytvnu7MeTOhn/0cWc6iFO7sTH2oTy+iSidxuf7GIGO0m/DgO1/ANs+4MXfMKATnS4B5a2vJanyo5+iW62q X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6095135)(2401047)(8121501046)(5005006)(10201501046)(3002001)(3231021)(93006095)(93001095)(100000703101)(100105400095)(6055026)(6096035)(20161123563025)(20161123556025)(20161123565025)(20161123559100)(201703131430075)(201703131433075)(201703131441075)(201703131448075)(201703161259150)(20161123561025)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:BN3PR03MB2353; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(400006)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:BN3PR03MB2353; X-Forefront-PRVS: 0484063412 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BN3PR03MB2353; 23:Cwvqkr3V0mop+1ePlx9kc853biCzvQN/gQ5rDEmsZ?= =?us-ascii?Q?nPE/mLMTQeDOQ6921OQJ60BEAQXh+TBXCQOThSDs7T8oqi1KJz1bEdfdkihU?= =?us-ascii?Q?KKEjdnEnNm13oGvdai72YbHcg+/vn4empnt/Xef50JdfvrF9HVbQqAt8RybF?= =?us-ascii?Q?v5QHBdjb+W7n6L/V5VBvQBVvbhCJfoDDgxumRtIRf3cz2DV7KSUEtcsZb017?= =?us-ascii?Q?sDYZnMKqlbXszup3Sti5Q+NOW5uFCYg5y9Zr8GS+ctHHKgsK9ksU0BHC2Z2c?= =?us-ascii?Q?roO13NsIkn7JyQnQSy93NYWo9g+J+LCaY06EX5Gwr/nP7MLh9XwpBVkavYho?= =?us-ascii?Q?lkUG96URrV3IiIauTPYfWT9W0qRv/G6L7IMdQkrL3ALO9qpJI0N35H85HMzd?= =?us-ascii?Q?juTxNUZGdL21dWfn1khideqLHlCCjFi/uKBPLFevm0nrgSBnplAamtBgau1e?= =?us-ascii?Q?EkDG1cXjOVp/oE59SAXNogRMwIj0uCKwS0AJkjJs0EseS4bfMdQCsd9jUIUV?= =?us-ascii?Q?ERj3sIEe59BguVb//uqKapADo0e2MNN9x2MJyoTktLjTmwWtSCdkyTOIpnha?= =?us-ascii?Q?36HeAxuyXODGvoTsev1bM2n7RmeDEWcLlAsq0RWlNkGKloAz/XQHBStBqbZ9?= =?us-ascii?Q?g77JAdztTgh/4vBdhw1w8APmVKEVJeORhwf2p12uxTRZuM4RuJ9F5aCzAl+H?= =?us-ascii?Q?wt+JaMn1DYLUKOoe1iXbBxqzeXveREytq/izUH0bkhAEQA0gMHqAsRw5o4Es?= =?us-ascii?Q?5UDBKIvNXp7rFcwiDc6kjy3i1dzl0o6TUKdBREdFJ8MJTDDu9JjVC2+fUFE+?= =?us-ascii?Q?xYYSHHyh86VC7NPh9k+seyX0sSuSsFLhLammn7M6t2jmFmCmyrnoeabkEahW?= =?us-ascii?Q?oslyY4IUlRTxByWRsdZwaRTfHONlHwpqD8aiCE21woJdzv3OnamUttOYj37I?= =?us-ascii?Q?c6oLGGnbS+tmHlo2AXblY0cktJk7Rqr/DytO0G5EC2OE9s28OUCWlIlD8cdB?= =?us-ascii?Q?Evh3gyslS/AfmQrQlvAuoa9dxmDeRogxbVaTZkUS1vf8Vrb/yP213k1wIPqL?= =?us-ascii?Q?qwI/+rBGwUyDchxckKQ2+1Mhg//2chYowrudtURE4tQwDwEd/lN+7+EHmjU0?= =?us-ascii?Q?6HgMQIfbaOd9fFDj/NQjBxq4zlMwZrtDE7shgndSpNLgUZO8S+39g0y47Ntj?= =?us-ascii?Q?tlIAkgpv/N75hynN1RDFDXYIs5CNimY1QnNXCLx6U7EPZD/g+V59vzjGI/bd?= =?us-ascii?Q?YqDlW9GW0kS5XujSIWieRCmJkCDnp1BMvib9xH13XP1JlaAOpx2AUZ6dy6Zj?= =?us-ascii?Q?gl1w4EexE9jUElPWf7tVjrGW8SL1hegqu1kNa0ba51x6Ss/pncOdG4onFImb?= =?us-ascii?Q?s/L14YefQZe7WRlDy4rKzi18QA=3D?= X-Microsoft-Exchange-Diagnostics: 1; BN3PR03MB2353; 6:cQfEbMNUr0vPV2QfxphrAFJBTXefDvPxkj2yj86uNp+8Hk6BPo6G2nb1bqzF+e/yfcJeo8xrB2/Dvf4EZJO6UGhraibDa2CxnmagRNWyGZqvTXM5H9hWv5hmkuxIoBks0cDDsPqaqWIfQJt6jweqYWwME3rZqQET9netug8lcEZseN0skiTGdghOEGMEy/xGGkKVyn/edt2ACEVTQR9/dE7s+7+bBjnMBdyluhhKkuACD6F22rs6qZp6/cg/X2tzMt9QrGBQYZaFDnkg8ATXgZbFEeQnhBzM0dRB9zXIDgLtyrJCwOtW8VyMjpbxgMYI/MNzZtdHEUW0Tu+nlNybPNfBqGQumEhlGnrFfe/pXqI=; 5:IClPYbLRDPWB3aq2Di+UYxSFOiA7OrrY2bEOBd8Ur1yEqVmCGZPPO5dPQwfUUmD0kwLUb0MZXyawNbzVujgaxUyVHKzeZlobLBCpQW2948H6XGQHri4D7tJF4UWSv69kUOwa7mtuWiM9RnvApuNSfpnJLGVsfJxNCZ+9ZIjDaMM=; 24:jEB5wKr0pmfNNEUe41OEfkEW1XFHhU+c9t/ITghBo4a/olBLM7OJRjEDWta7+RbEsYKNclL+L2tFUddCBNDzCgIMIwxKf+y88N9/cUrxjQ4=; 7:1zzewo7bhI7u7uTKtQmQjZeA8A7aF6UHVzKxBoR7TDenRfwONSBZg/MX/8Z/64vSgacP/7zmZh4OXCTURzpMDsprL+8MNc7pGYkXTqRdtHaz1RIKUO5S9IPfCAUqMAb0vGbRtgI0oI6NG6q3AhtlLwrBiXXEZU9k0/e0qlvX4gq8048mtSF84guUAQsLBDgqvxwjJIRaqbcVJrvuRHHOiqVdwHYp/0CMcw7UxiF7twmPXt85sLFFJY54jSu2RUFy SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Nov 2017 08:54:35.6676 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c5643cae-5b99-47bb-e564-08d525bd2be6 X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR03MB2353 Subject: [PATCH 06/10] Platform/NXP: Add support for I2c operations library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Nov 2017 08:50:39 -0000 Content-Type: text/plain I2C bus initialization and I2c read/write APIs added. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Meenakshi Aggarwal --- Platform/NXP/Include/Library/I2c.h | 125 ++++++++ Platform/NXP/Library/I2cLib/I2cLib.c | 549 +++++++++++++++++++++++++++++++++ Platform/NXP/Library/I2cLib/I2cLib.h | 109 +++++++ Platform/NXP/Library/I2cLib/I2cLib.inf | 43 +++ 4 files changed, 826 insertions(+) create mode 100644 Platform/NXP/Include/Library/I2c.h create mode 100644 Platform/NXP/Library/I2cLib/I2cLib.c create mode 100644 Platform/NXP/Library/I2cLib/I2cLib.h create mode 100644 Platform/NXP/Library/I2cLib/I2cLib.inf diff --git a/Platform/NXP/Include/Library/I2c.h b/Platform/NXP/Include/Library/I2c.h new file mode 100644 index 0000000..c7195ab --- /dev/null +++ b/Platform/NXP/Include/Library/I2c.h @@ -0,0 +1,125 @@ +/** I2c.h + Header defining the constant, base address amd function for I2C controller + + Copyright 2017 NXP + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef __I2C_H___ +#define __I2C_H__ + +#include + +#define I2C1 0 +#define I2C2 1 +#define I2C3 2 +#define I2C4 3 + +/// +/// Define the I2C flags +/// +#define I2C_READ_FLAG 0x1 +#define I2C_WRITE_FLAG 0x2 + +/** + Function to initialize i2c bus + + @param I2cBus I2c Controller number + @param Speed Value to be set + + @retval EFI_SUCCESS +**/ +EFI_STATUS +EFIAPI +I2cBusInit ( + IN UINT32 I2cBus, + IN UINT32 Speed + ); + +/** + Function to read data usin i2c + + @param I2cBus I2c Controller number + @param Chip Address of slave device from where data to be read + @param Offset Offset of slave memory + @param Alen Address length of slave + @param Buffer A pointer to the destination buffer for the data + @param Len Length of data to be read + + @retval EFI_NOT_READY Arbitration lost + @retval EFI_TIMEOUT Failed to initialize data transfer in predefined time + @retval EFI_NOT_FOUND ACK was not recieved + @retval EFI_SUCCESS Read was successful + +**/ +EFI_STATUS +I2cDataRead ( + IN UINT32 I2cBus, + IN UINT8 Chip, + IN UINT32 Offset, + IN UINT32 Alen, + OUT UINT8 *Buffer, + IN UINT32 Len + ); + +/** + Function to write data using i2c bus + + @param I2cBus I2c Controller number + @param Chip Address of slave device where data to be written + @param Offset Offset of slave memory + @param Alen 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 predefined time + @retval EFI_NOT_FOUND ACK was not recieved + @retval EFI_SUCCESS Read was successful + +**/ +EFI_STATUS +I2cDataWrite ( + IN UINT32 I2cBus, + IN UINT8 Chip, + IN UINT32 Offset, + IN INT32 Alen, + OUT UINT8 *Buffer, + IN INT32 Len + ); + +/** + Function to reset I2c + @param I2cBus I2c Controller number + + @return VOID +**/ +VOID +I2cReset ( + UINT32 I2cBus + ); + +/** + Function to Probe I2c slave device + @param I2cBus I2c Controller number + + @retval EFI_INVALID_PARAMETER Invalid I2c Controller number + @retval EFI_SUCCESS I2c device probed successfully + +**/ +EFI_STATUS +EFIAPI +I2cProbeDevices ( + IN INT16 I2cBus, + IN UINT8 Chip + ); + +#endif diff --git a/Platform/NXP/Library/I2cLib/I2cLib.c b/Platform/NXP/Library/I2cLib/I2cLib.c new file mode 100644 index 0000000..574b899 --- /dev/null +++ b/Platform/NXP/Library/I2cLib/I2cLib.c @@ -0,0 +1,549 @@ +/** I2cLib.c + I2c Library containing functions for read, write, initialize, set speed etc + + Copyright 2017 NXP + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ +#include +#include +#include +#include +#include + +#include "I2cLib.h" + +EFI_PHYSICAL_ADDRESS I2cAddrArr[FixedPcdGet32(PcdNumI2cController)] = { + FixedPcdGet64(PcdI2c0BaseAddr), + FixedPcdGet64(PcdI2c1BaseAddr), + FixedPcdGet64(PcdI2c2BaseAddr), + FixedPcdGet64(PcdI2c3BaseAddr) +}; + +UINT16 ClkDiv[60][2] = { + { 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 set proper clock divider + + @param Rate : clock rate + + @retval ClkDiv : Value used to get frequency divider value + +**/ +UINT8 +GetClk( + IN UINT32 Rate + ) +{ + UINTN ClkRate; + UINT32 Div; + UINT8 ClkDivx; + + ClkRate = CalculateI2cClockRate(); + + Div = (ClkRate + Rate - 1) / Rate; + + if (Div < ClkDiv[0][0]) { + ClkDivx = 0; + } else if (Div > ClkDiv[ARRAY_SIZE(ClkDiv) - 1][0]){ + ClkDivx = ARRAY_SIZE(ClkDiv) - 1; + } else { + for (ClkDivx = 0; ClkDiv[ClkDivx][0] < Div; ClkDivx++); + } + + return ClkDivx; +} + +/** + Function to reset I2c + @param I2cBus : I2c Controller number + + @return VOID +**/ + +VOID +I2cReset ( + IN UINT32 I2cBus + ) +{ + I2C_REGS *I2cRegs; + + I2cRegs = (I2C_REGS *)I2cAddrArr[I2cBus]; + + /** Reset module */ + MmioWrite8((UINTN)&I2cRegs->I2cCr, I2C_CR_IDIS); + MmioWrite8((UINTN)&I2cRegs->I2cSr, 0); +} + +/** + Function to set I2c bus speed + + @param I2cBus : I2c Controller number + @param Speed : Value to be set + + @retval EFI_SUCCESS +**/ +EFI_STATUS +EFIAPI +I2cSetBusSpeed ( + IN UINT32 I2cBus, + IN UINT32 Speed + ) +{ + I2C_REGS *I2cRegs; + UINT8 ClkId; + UINT8 SpeedId; + + I2cRegs = (I2C_REGS *)I2cAddrArr[I2cBus]; + + ASSERT(I2cRegs); + + ClkId = GetClk(Speed); + SpeedId = ClkDiv[ClkId][1]; + + /** Store divider value */ + MmioWrite8((UINTN)&I2cRegs->I2cFdr, SpeedId); + I2cReset(I2cBus); + + return EFI_SUCCESS; +} + +/** + 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 + +**/ +EFI_STATUS +WaitForI2cState ( + IN I2C_REGS *I2cRegs, + IN UINT32 State + ) +{ + UINT8 CurrState; + UINT64 Cnt; + + for (Cnt = 0; Cnt < 50; Cnt++) { + CurrState = 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)) == (UINT8)State) { + return CurrState; + } + + MicroSecondDelay(300); + } + + 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 + +**/ +EFI_STATUS +TransferByte ( + IN I2C_REGS *I2cRegs, + IN UINT8 Byte + ) +{ + EFI_STATUS Ret; + + MmioWrite8((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); + MmioWrite8((UINTN)&I2cRegs->I2cDr, Byte); + + Ret = WaitForI2cState(I2cRegs, IIF); + if ((Ret == EFI_TIMEOUT) || (Ret == EFI_NOT_READY)) { + return Ret; + } + + if (Ret & 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 + +**/ +EFI_STATUS +I2cStop ( + IN I2C_REGS *I2cRegs + ) +{ + INT32 Ret; + UINT32 Temp; + + Temp = MmioRead8((UINTN)&I2cRegs->I2cCr); + + Temp &= ~(I2C_CR_MSTA | I2C_CR_MTX); + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + + Ret = WaitForI2cState(I2cRegs, BUS_IDLE); + + if (Ret < 0) { + return Ret; + } 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 Alen : length of chip address + + @retval EFI_NOT_READY Arbitration lost + @retval EFI_TIMEOUT Failed to initialize data transfer in predefined time + @retval EFI_NOT_FOUND ACK was not recieved + @retval EFI_SUCCESS Read was successful + +**/ +EFI_STATUS +InitTransfer ( + IN I2C_REGS *I2cRegs, + IN UINT8 Chip, + IN UINT32 Offset, + IN INT32 Alen + ) +{ + UINT32 Temp; + EFI_STATUS Ret; + + /** Enable I2C controller */ + if (MmioRead8((UINTN)&I2cRegs->I2cCr) & I2C_CR_IDIS) { + MmioWrite8((UINTN)&I2cRegs->I2cCr, I2C_CR_IEN); + } + + if (MmioRead8((UINTN)&I2cRegs->I2cAdr) == (Chip << 1)) { + MmioWrite8((UINTN)&I2cRegs->I2cAdr, (Chip << 1) ^ 2); + } + + MmioWrite8((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); + Ret = WaitForI2cState(I2cRegs, BUS_IDLE); + if ((Ret == EFI_TIMEOUT) || (Ret == EFI_NOT_READY)) { + return Ret; + } + + /** Start I2C transaction */ + Temp = MmioRead8((UINTN)&I2cRegs->I2cCr); + /** set to master mode */ + Temp |= I2C_CR_MSTA; + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + + Ret = WaitForI2cState(I2cRegs, BUS_BUSY); + if ((Ret == EFI_TIMEOUT) || (Ret == EFI_NOT_READY)) { + return Ret; + } + + Temp |= I2C_CR_MTX | I2C_CR_TX_NO_AK; + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + + /** write slave Address */ + Ret = TransferByte(I2cRegs, Chip << 1); + if (Ret != EFI_SUCCESS) { + return Ret; + } + + if (Alen >= 0) { + while (Alen--) { + Ret = TransferByte(I2cRegs, (Offset >> (Alen * 8)) & 0xff); + if (Ret != EFI_SUCCESS) + return Ret; + } + } + return EFI_SUCCESS; +} + +/** + Function to check if i2c bud is idle + + @param Base : Pointer to base address of I2c controller + + @retval EFI_SUCCESS + +**/ +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 Alen : length of chip address + + @retval EFI_NOT_READY : Arbitration lost + @retval EFI_TIMEOUT : Failed to initialize data transfer in predefined time + @retval EFI_NOT_FOUND : ACK was not recieved + @retval EFI_SUCCESS : Read was successful + +**/ +EFI_STATUS +InitDataTransfer ( + IN I2C_REGS *I2cRegs, + IN UINT8 Chip, + IN UINT32 Offset, + IN INT32 Alen + ) +{ + EFI_STATUS Ret; + INT32 Retry; + + for (Retry = 0; Retry < 3; Retry++) { + Ret = InitTransfer(I2cRegs, Chip, Offset, Alen); + if (Ret == EFI_SUCCESS) { + return EFI_SUCCESS; + } + + I2cStop(I2cRegs); + + if (EFI_NOT_FOUND == Ret) { + return Ret; + } + + /** Disable controller */ + if (Ret != EFI_NOT_READY) { + MmioWrite8((UINTN)&I2cRegs->I2cCr, I2C_CR_IDIS); + } + + if (I2cBusIdle(I2cRegs) < 0) { + break; + } + } + return Ret; +} + +/** + Function to read data using i2c bus + + @param I2cBus : I2c Controller number + @param Chip : Address of slave device from where data to be read + @param Offset : Offset of slave memory + @param Alen : Address length of slave + @param Buffer : A pointer to the destination buffer for the data + @param Len : Length of data to be read + + @retval EFI_NOT_READY : Arbitration lost + @retval EFI_TIMEOUT : Failed to initialize data transfer in predefined time + @retval EFI_NOT_FOUND : ACK was not recieved + @retval EFI_SUCCESS : Read was successful + +**/ +EFI_STATUS +I2cDataRead ( + IN UINT32 I2cBus, + IN UINT8 Chip, + IN UINT32 Offset, + IN UINT32 Alen, + IN UINT8 *Buffer, + IN UINT32 Len + ) +{ + EFI_STATUS Ret; + UINT32 Temp; + INT32 I; + I2C_REGS *I2cRegs; + + I2cRegs = (I2C_REGS *)I2cAddrArr[I2cBus]; + + Ret = InitDataTransfer(I2cRegs, Chip, Offset, Alen); + if (Ret != EFI_SUCCESS) { + return Ret; + } + + Temp = MmioRead8((UINTN)&I2cRegs->I2cCr); + Temp |= I2C_CR_RSTA; + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + + Ret = TransferByte(I2cRegs, (Chip << 1) | 1); + if (Ret != EFI_SUCCESS) { + I2cStop(I2cRegs); + return Ret; + } + + /** setup bus to read data */ + Temp = MmioRead8((UINTN)&I2cRegs->I2cCr); + Temp &= ~(I2C_CR_MTX | I2C_CR_TX_NO_AK); + if (Len == 1) { + Temp |= I2C_CR_TX_NO_AK; + } + + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + MmioWrite8((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); + + /** read data */ + /** Dummy Read to initiate recieve operation */ + MmioRead8((UINTN)&I2cRegs->I2cDr); + + for (I = 0; I < Len; I++) { + Ret = WaitForI2cState(I2cRegs, IIF); + if ((Ret == EFI_TIMEOUT) || (Ret == EFI_NOT_READY)) { + I2cStop(I2cRegs); + return Ret; + } + /** + It must generate STOP before read I2DR to prevent + controller from generating another clock cycle + **/ + if (I == (Len - 1)) { + I2cStop(I2cRegs); + } else if (I == (Len - 2)) { + Temp = MmioRead8((UINTN)&I2cRegs->I2cCr); + Temp |= I2C_CR_TX_NO_AK; + MmioWrite8((UINTN)&I2cRegs->I2cCr, Temp); + } + MmioWrite8((UINTN)&I2cRegs->I2cSr, I2C_SR_IIF_CLEAR); + Buffer[I] = MmioRead8((UINTN)&I2cRegs->I2cDr); + } + + I2cStop(I2cRegs); + + return EFI_SUCCESS; +} + +/** + Function to write data using i2c bus + + @param I2cBus : I2c Controller number + @param Chip : Address of slave device where data to be written + @param Offset : Offset of slave memory + @param Alen : 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 predefined time + @retval EFI_NOT_FOUND : ACK was not recieved + @retval EFI_SUCCESS : Read was successful + +**/ +EFI_STATUS +I2cDataWrite ( + IN UINT32 I2cBus, + IN UINT8 Chip, + IN UINT32 Offset, + IN INT32 Alen, + OUT UINT8 *Buffer, + IN INT32 Len + ) +{ + EFI_STATUS Ret; + I2C_REGS *I2cRegs; + INT32 I; + + I2cRegs = (I2C_REGS *)I2cAddrArr[I2cBus]; + + Ret = InitDataTransfer(I2cRegs, Chip, Offset, Alen); + if (Ret != EFI_SUCCESS) { + return Ret; + } + + /** write data */ + /** Dummy write to initiate write operation */ + for (I = 0; I < Len; I++) { + Ret = TransferByte(I2cRegs, Buffer[I]); + if (Ret != EFI_SUCCESS) { + break; + } + } + + I2cStop(I2cRegs); + return Ret; +} + +/** + Function to Probe i2c bus + + @param I2c : I2c Controller number + + @retval EFI_INVALID_PARAMETER : Input parametr I2c was invalid + @retval EFI_SUCCESS : I2c was initialized successfully + +**/ +EFI_STATUS +EFIAPI +I2cProbeDevices ( + IN INT16 I2c, + IN UINT8 ChipAdd + ) +{ + if(I2c >= PcdGet32(PcdNumI2cController) || I2cAddrArr[I2c] == 0x0) { + return EFI_INVALID_PARAMETER; + } + + return I2cDataWrite(I2c, ChipAdd, 0, 0, NULL, 0); +} + +/** + Function to initialize i2c bus + + @param I2cBus : I2c Controller number + @param Speed : value to be set +**/ +EFI_STATUS +EFIAPI +I2cBusInit ( + IN UINT32 I2cBus, + IN UINT32 Speed + ) +{ + return I2cSetBusSpeed(I2cBus, Speed); +} diff --git a/Platform/NXP/Library/I2cLib/I2cLib.h b/Platform/NXP/Library/I2cLib/I2cLib.h new file mode 100644 index 0000000..61076f5 --- /dev/null +++ b/Platform/NXP/Library/I2cLib/I2cLib.h @@ -0,0 +1,109 @@ +/** I2cLib.h + Header defining the constant, base address amd function for I2C controller + + Copyright 2017 NXP + + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD License + which accompanies this distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef __I2C_H___ +#define __I2C_H__ + +#include + +#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)) + +/** + Record defining i2c registers +**/ +typedef struct { + UINT8 I2cAdr; + UINT8 I2cFdr; + UINT8 I2cCr; + UINT8 I2cSr; + UINT8 I2cDr; +} I2C_REGS ; + +/** + Function to set I2c bus speed + + @param I2cBus I2c Controller number + @param Speed Value to be set + + @retval EFI_SUCCESS +**/ +EFI_STATUS +EFIAPI +I2cSetBusSpeed ( + IN UINT32 I2cBus, + IN UINT32 Speed + ); + +/** + 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 + +**/ +EFI_STATUS +I2cStop ( + IN I2C_REGS *I2cRegs + ); + +/** + 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 Alen length of chip address + + @retval EFI_NOT_READY Arbitration lost + @retval EFI_TIMEOUT Failed to initialize data transfer in predefined time + @retval EFI_NOT_FOUND ACK was not recieved + @retval EFI_SUCCESS Read was successful + +**/ +EFI_STATUS +I2cBusInitTransfer ( + IN I2C_REGS *I2cRegs, + IN UINT8 Chip, + IN UINT32 Offset, + IN INT32 Alen + ); + +UINT32 +CalculateI2cClockRate ( + VOID + ); + +#endif diff --git a/Platform/NXP/Library/I2cLib/I2cLib.inf b/Platform/NXP/Library/I2cLib/I2cLib.inf new file mode 100644 index 0000000..427c3b8 --- /dev/null +++ b/Platform/NXP/Library/I2cLib/I2cLib.inf @@ -0,0 +1,43 @@ +# I2cLib.inf +# +# Component description file for I2cLib module +# +# Copyright 2017 NXP +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = I2cLib + FILE_GUID = 8ecefc8f-a2c4-4091-b81f-20f7aeb0567f + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = I2cLib + +[Sources.common] + I2cLib.c + +[LibraryClasses] + BaseLib + IoLib + SocLib + TimerLib + +[Packages] + MdePkg/MdePkg.dec + edk2-platforms/Platform/NXP/NxpQoriqLs.dec + +[Pcd] + gNxpQoriqLsTokenSpaceGuid.PcdI2c0BaseAddr + gNxpQoriqLsTokenSpaceGuid.PcdI2c1BaseAddr + gNxpQoriqLsTokenSpaceGuid.PcdI2c2BaseAddr + gNxpQoriqLsTokenSpaceGuid.PcdI2c3BaseAddr + gNxpQoriqLsTokenSpaceGuid.PcdNumI2cController -- 1.9.1