From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=104.47.40.65; helo=nam03-co1-obe.outbound.protection.outlook.com; envelope-from=vabhav.sharma@nxp.com; receiver=edk2-devel@lists.01.org Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0065.outbound.protection.outlook.com [104.47.40.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0D9B422183C83 for ; Fri, 1 Dec 2017 08:07:30 -0800 (PST) Received: from BN3PR03CA0094.namprd03.prod.outlook.com (10.174.66.12) by CY4PR03MB2694.namprd03.prod.outlook.com (10.173.43.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.282.5; Fri, 1 Dec 2017 16:11:55 +0000 Received: from BN1AFFO11OLC003.protection.gbl (2a01:111:f400:7c10::104) by BN3PR03CA0094.outlook.office365.com (2603:10b6:400:4::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.282.5 via Frontend Transport; Fri, 1 Dec 2017 16:11:55 +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 BN1AFFO11OLC003.mail.protection.outlook.com (10.58.53.74) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.239.4 via Frontend Transport; Fri, 1 Dec 2017 16:11:55 +0000 Received: from uefi-OptiPlex-790.ap.freescale.net ([10.232.132.56]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id vB1GBiuj003880; Fri, 1 Dec 2017 09:11:51 -0700 From: Vabhav To: , , , Date: Fri, 1 Dec 2017 09:40:04 +0530 Message-ID: <1512101406-11567-3-git-send-email-vabhav.sharma@nxp.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1512101406-11567-1-git-send-email-vabhav.sharma@nxp.com> References: <1512101406-11567-1-git-send-email-vabhav.sharma@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131566183156370833; (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)(39380400002)(376002)(346002)(39860400002)(2980300002)(1109001)(1110001)(339900001)(189002)(199003)(86362001)(575784001)(33646002)(16586007)(316002)(16799955002)(106466001)(5660300001)(97736004)(15188155005)(36756003)(498600001)(77096006)(105606002)(966005)(2950100002)(6666003)(8936002)(8656006)(47776003)(53936002)(53946003)(6306002)(50226002)(356003)(85426001)(8676002)(81166006)(2906002)(53376002)(68736007)(81156014)(48376002)(2201001)(189998001)(50466002)(110136005)(54906003)(4326008)(51416003)(305945005)(104016004)(76176011)(579004)(19627235001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR03MB2694; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BN1AFFO11OLC003; 1:9xmMciuGiOmZt/6FuUdITTBvsOnaowlH+O2oi1e7Yc7mieHLP7MxX5OQtvJ7LESBHReSvkr2ZvFfib1uPA59XeNsagcAuDWrrXjCKCO391JRHCXV1KDzSh/F4BBWXi9P MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 022e013b-d645-4787-ec33-08d538d63df1 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(4534020)(4628075)(201703131517081)(5600026)(4604075)(2017052603286); SRVR:CY4PR03MB2694; X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2694; 3:Oj1tYjoLCtMaamRn85/4UECs+CVHPkjTKAtbX2Y+c1p+f9xPineq6r4zMceQclUHRV5Uru3/g/7HmlOGMADzl6gcWRuJn4HtJ1ESAuVt2Ua0DJQAABfmlxTIHtU9I0JXasrBjO69EdHMo4imzgClfdk2j8/AYsO6eT8+WEgJRhv/XlNCFeUUW/50Uq44mou3z35AEDrFPrsVMoz4Rq3Fj3gfRVax2qoaliKqQI+q2Ycb4rDgnJE3N3Gqm+L8xbxE5LoWrP79RlLxl7eOTl8+muSjxBGQW04/9IVRUHyY1jj6Z7mKjVhLJCt3MBvzboJsLqHtU619hMXXoJ9yQDV3sbu40mC0rTcCLs93ang99KQ=; 25:UrT8KKmEJTebJiSBRCXf/F6A7mJH45yuh3wiYftYQow6i3CIfQSLvv5tkOg05Q2DFUsXkapI45Mac/QUrnN6uHvLFqR94z8zOVYB92065j9liBgkRLsG5giEG6hhvikVx5OSV+dDLKCCL4l7lX3h2jLF9JaMx+mu3jjsWootIjSAXme/1uJbOmnjBHYIycovIVYS/ycFeQF80ProQzMfcj9343RIgRWTdESc4JRFGZnyeecFy+fQ3MXAmATCaFEGNQqhDWqZpEPcGaaYTZev3gjv1mh4q5r9v4wMVRjm9SGvjX0lg2bY1g0I6QtxGkDNV5EKs0G96vJQojE6dBDFvw== X-MS-TrafficTypeDiagnostic: CY4PR03MB2694: X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2694; 31:GdNGWVqy2+zawDkLIjmbK5ZRC9FKPjfJ4tS0ls23wKcFsae/6mZklQv7B5BmoyNKfreknaP2rZJ1GM/+2LgzdP1GF/3GD4UlxHnJP+SnAC7aP3JFkAjra3cWyiLN+/zg8I00Yn0oHMKEWiPZNt999snbfi20KPmaxpG5frzFqkYfX7x7G+ofXwOuj0IpWyoE+5RXCCpS1OtjRq10Rld2B4AfVGmWbQMiVa/87emhfN4=; 4:wCueWVxQUZU9fxSuo7mvf18/LotIjdaN3GhgY6C3Q5UozgkuAedB0Khy6q0hfqnnfhG9DsAS7aYI8Iaj5zKZqi6ykZG5UI9Xcjnn5Pi8FjExyQJHrwOqy+CrlpRE40MixssKhNBVa40UoygPum6Lbj/+37Uh1uOKGeYe7NAF9osT1Xk6pEGw8xjUQBl9IppLbB9DmLq1xa7XpmN2Q/Jg45K54iy7/rXtHbS7yUopLXzNXshg8jIqQAuN8tD+wGPHqFXKOffOTyh020FSa6Ji7QtSGBItkz64pJBwMy0Ibd6GAGaak7ne2sAZpD0p2UDV X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6095135)(2401047)(8121501046)(5005006)(3231022)(3002001)(10201501046)(93006095)(93001095)(6055026)(6096035)(20161123565025)(20161123561025)(20161123563025)(20161123556025)(20161123559100)(201703131430075)(201703131433075)(201703131448075)(201703161259150)(201703151042153)(201708071742011); SRVR:CY4PR03MB2694; BCL:0; PCL:0; RULEID:(100000803101)(100110400095)(400006); SRVR:CY4PR03MB2694; X-Forefront-PRVS: 05087F0C24 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY4PR03MB2694; 23:73hDU84LwH0w6OfaFWbQkSQjx5r7Pv3s/3hl7l26R?= =?us-ascii?Q?sQBYoSs7Ga7qtSxJFfchTMXkfMjpqg6BhPbzlEbqvzRoCtZ1n4MVOyhUgvrx?= =?us-ascii?Q?DlhgsaiFHQw795MGTjes/LeHDVxl9nCEZ8fBzsM0ZsNNuNJC7fVa/GKHHL+i?= =?us-ascii?Q?2H7nI6dviw29WhhHxunWDgIElfJe50exH/OSr4Lr3uZ1BZGcK7BK6ArvZmLO?= =?us-ascii?Q?JvJ9uAiTawsuu0IDhCtzVv+4V42qPNY9ueu2RJ/Wq4KlR6DNDjgpdHQ61nQz?= =?us-ascii?Q?4DUVoxKMHhWRS8txgv9EyqDE2bqYosJgcvQj5perpiIBBtB1GvSYyjRLUWET?= =?us-ascii?Q?T5A9ldqX9rFrfQBOHWdAv4P2Fte4VzftNI/aHmho28oo+Owgrx6EopvPZAdA?= =?us-ascii?Q?J4BtrY5K4g2k5BqHvx18CUKZpr6Fpums2xWWhjqsWVsSREl2SKd9fEozsn2P?= =?us-ascii?Q?VNm0GTMULCO3zycAQoZge+eyNvr7qvsqLJ6DgYI1SUw0m1aMCn4yAcZ6iKCp?= =?us-ascii?Q?ikYJqPlLkC4xcIe1pLzmK3GDKjbz4KFajzvstRvhxwJIbr9ccwFDLze4ASto?= =?us-ascii?Q?8XIWPKXZiLo4AANOJWNiXt8IBYjeh2biSzdgmVJzvaObwGoHDgYN+XWqXgLp?= =?us-ascii?Q?gO0oxKobsisU1O8pnZiXtIkAEhFe8BnewrPx8wBOl0aAWSdQfqA3AZ3pbqMc?= =?us-ascii?Q?nQ7wEt+eK0lRMj5ORgmY3rc3HoFAjWuS38bvFJTPc3cJeI/j0yjBhVt8lsUj?= =?us-ascii?Q?MxCUN4evYyfmmtshcZly5Fhn1ImhAL0Ru0tqOEio9PhdT8vTHGKe3/bxS5k5?= =?us-ascii?Q?jBjpaZWpiqS70HLb8o/f6bEzUYV9hMYOTBjn8CiVhSUFrUxG5HjEo7ywcrQw?= =?us-ascii?Q?izipKP9QK0XjHVNOGmxXyn9X6GJM0XEw8Q4wFWcv4zFs6G4RQ4rpRAH7MZbh?= =?us-ascii?Q?eSe5qtlyajnfbUNRcy8Yny+IqBb3VVbtu+j9uJyp8o1MF1ujd475kWTvTfiA?= =?us-ascii?Q?VtmGpHE9ue1R7N2V+BsKnvxyh985qe7pM+IXCwiF99Y4m2HrxvPezOs401Td?= =?us-ascii?Q?91U0vzbYavWU/u8df3TocdF5sN7TNk4hGBha8MKZ5FdYwK8vf3mdO26PHjwS?= =?us-ascii?Q?I/PaNCO36qYumfoXv9Nu1a7P6YGjo45O9Pz1WsOeV6WEhLxvii0oXqSWFHeW?= =?us-ascii?Q?HKcXLIvDSx6FIhWCWRFOlgPC2hCKneLrcTIH06cvU4aRbWRIx/iNBqYYfB3q?= =?us-ascii?Q?tK/ClvK9DCG+McdqEWPQZNYFiuK5uySDHLSyUuHT/dVh2kja0McWsk5WIGds?= =?us-ascii?Q?WXQTeVEEkwiQAacz8dQ25ggYxgbm5ObSQ9I462/U5O+?= X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2694; 6:LpZBX6xpmj+3HQGTgI8BhEwcLBJdIZaMmLaCG/w27vFG6kqIaxTnWWqohVYOD93P4CKmukS1HBxoD8ALJhQp08JoUiwh/1aPMLaKEZjf+DGWU9XR+9IWSUz1uXq3rx8y9yxSyI2hzelx5heZEoiB74u8JO6GPMPQw2xfCj9tUKW/aGvBOIUiBUoQ4GXMsVHHSoBEK13/p581Xv7vcd28H9HgnHvrsEU0CfcNdQrPiIrV5/ZjaV2jrhU+X858lWWsCyR67mtBya11IyxeEV1jWD+x9BhkGKIJ5LSUvduWfm3PkoPBpXPgzqwYXG+x8xg8S+6W57zIIbtmTO/HnTKnS1sBlJjmwMDKqp8iG63rzDA=; 5:m5QYkI0BrbvlncRjl0EivQ44Qc7FT8a2DlfGr/LLBRwqEV5HeZDaR5nFs7GMuJHhopLoZ2wslwwZJ7SRv/5clMIcswfFDcb3YLH9sJkO3v05cNcvm1wdFGQr/Fhu8MZrzRSzLv9E63O1bKXqFcl171KtwhHL41RVaQB3z19kWPc=; 24:3jTkQD+tl7XdpsuyjCx8sLJ2qKpHRlrD+//AIo9vk+MVq2nWOPBhnSftz+rJoBOm5tCiBBIaSLQ64sG5eR2NEyEAsL3D5gBc+we4L2g8ZSk=; 7:8RVr7A+6jqlEzoCZ6Z6K2kQJZXHqiWabOUT2op45ELdyzfWG0KkXT+HDFtA+1doqrm6GPqfANCjqBdxDltin+CxGYH5MO3rHBMjwJSFSsKqEaC3/PJ9cOtyYViwh6PPLLlDqf+9D/ANfZbjU0s+546lDwUN82hp8A3rD8esbZ2O4VESuvvr2FWvghWkM7nmzswdXcfmma3te4SPPrpB0Ov2TH21ps7hls6NlV+F/41IxJCh5LlbW3RzS0Q1gm3gU SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Dec 2017 16:11:55.3718 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 022e013b-d645-4787-ec33-08d538d63df1 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: CY4PR03MB2694 Subject: [PATCH 2/4] Platform/NXP : Add Support for NOR flash 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: Fri, 01 Dec 2017 16:07:31 -0000 Content-Type: text/plain This patch adds support for NOR flash library(NorFlashLib) to provide functions which will be used by NOR flash Driver. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Vabhav --- Platform/NXP/Include/Library/NorFlashLib.h | 67 +++ Platform/NXP/Library/NorFlashLib/CfiCommand.h | 99 ++++ Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.c | 184 ++++++ Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.h | 54 ++ Platform/NXP/Library/NorFlashLib/IfcNorFlashLib.c | 52 ++ Platform/NXP/Library/NorFlashLib/NorFlashLib.c | 658 ++++++++++++++++++++++ Platform/NXP/Library/NorFlashLib/NorFlashLib.inf | 42 ++ 7 files changed, 1156 insertions(+) create mode 100644 Platform/NXP/Include/Library/NorFlashLib.h create mode 100644 Platform/NXP/Library/NorFlashLib/CfiCommand.h create mode 100644 Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.c create mode 100644 Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.h create mode 100644 Platform/NXP/Library/NorFlashLib/IfcNorFlashLib.c create mode 100644 Platform/NXP/Library/NorFlashLib/NorFlashLib.c create mode 100644 Platform/NXP/Library/NorFlashLib/NorFlashLib.inf diff --git a/Platform/NXP/Include/Library/NorFlashLib.h b/Platform/NXP/Include/Library/NorFlashLib.h new file mode 100644 index 0000000..8eb0f82 --- /dev/null +++ b/Platform/NXP/Include/Library/NorFlashLib.h @@ -0,0 +1,67 @@ +/** @file + + Copyright (c) 2011-2012, ARM Ltd. All rights reserved. + Copyright (c) 2016, Freescale Semiconductor. All rights reserved. + 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 _NOR_FLASH_LIB_H_ +#define _NOR_FLASH_LIB_H_ + +#include + +typedef struct { + UINTN DeviceBaseAddress; // Start address of the Device Base Address (DBA) + UINTN RegionBaseAddress; // Start address of one single region + UINTN Size; + UINTN BlockSize; + UINTN MultiByteWordCount; // Maximum Word count that can be written to Nor Flash in multi byte write + UINTN WordWriteTimeOut; // single byte/word timeout usec + UINTN BufferWriteTimeOut; // buffer write timeout usec + UINTN BlockEraseTimeOut; // block erase timeout usec + UINTN ChipEraseTimeOut; // chip erase timeout usec +} NorFlashDescription; + +EFI_STATUS +NorFlashPlatformGetDevices ( + OUT NorFlashDescription **NorFlashDevices, + OUT UINT32 *Count + ); + +EFI_STATUS +NorFlashPlatformFlashGetAttributes ( + OUT NorFlashDescription *NorFlashDevices, + IN UINT32 Count + ); + +typedef struct { + UINT8 ChipSelect; + IFC_REGS* IfcRegs; +} NorFlashInfo; + +VOID +GetIfcNorFlashInfo ( + IN NorFlashInfo *NorFlashInfo + ); + +VOID +GetIfcNorFlashTimings ( + IN IfcTimings * NorIfcTimings + ); + +typedef UINT16 FLASH_DATA; + +FLASH_DATA IfcNorFlashData (FLASH_DATA Val); + +VOID IfcNorWrite32 (VOID *a, UINT32 v); + +#endif /* _NOR_FLASH_LIB_H_ */ diff --git a/Platform/NXP/Library/NorFlashLib/CfiCommand.h b/Platform/NXP/Library/NorFlashLib/CfiCommand.h new file mode 100644 index 0000000..5e755be --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/CfiCommand.h @@ -0,0 +1,99 @@ +/** @CfiCommand.h + + 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 __CFI_COMMAND_H__ +#define __CFI_COMMAND_H__ + +// CFI Data "QRY" +#define CFI_QRY_Q 0x51 +#define CFI_QRY_R 0x52 +#define CFI_QRY_Y 0x59 +#define CFI_QRY 0x515259 + +#define ENTER_CFI_QUERY_MODE_ADDR 0x0055 +#define ENTER_CFI_QUERY_MODE_CMD 0x0098 + +#define CFI_QUERY_UNIQUE_QRY_STRING 0x10 + +// Offsets for CFI queries +#define CFI_QUERY_TYP_TIMEOUT_WORD_WRITE 0x1F +#define CFI_QUERY_TYP_TIMEOUT_MAX_BUFFER_WRITE 0x20 +#define CFI_QUERY_TYP_TIMEOUT_BLOCK_ERASE 0x21 +#define CFI_QUERY_TYP_TIMEOUT_CHIP_ERASE 0x22 +#define CFI_QUERY_MAX_TIMEOUT_WORD_WRITE 0x23 +#define CFI_QUERY_MAX_TIMEOUT_MAX_BUFFER_WRITE 0x24 +#define CFI_QUERY_MAX_TIMEOUT_BLOCK_ERASE 0x25 +#define CFI_QUERY_MAX_TIMEOUT_CHIP_ERASE 0x26 +#define CFI_QUERY_DEVICE_SIZE 0x27 +#define CFI_QUERY_MAX_NUM_BYTES_WRITE 0x2A +#define CFI_QUERY_BLOCK_SIZE 0x2F + +// Unlock Address +#define CMD_UNLOCK_1_ADDR 0x555 +#define CMD_UNLOCK_2_ADDR 0x2AA + +// RESET Command +#define CMD_RESET_FIRST 0xAA +#define CMD_RESET_SECOND 0x55 +#define CMD_RESET 0xF0 + +// READ Command + +// Manufacturer ID +#define CMD_READ_M_ID_FIRST 0xAA +#define CMD_READ_M_ID_SECOND 0x55 +#define CMD_READ_M_ID_THIRD 0x90 +#define CMD_READ_M_ID_FOURTH 0x01 + +// Device ID +#define CMD_READ_D_ID_FIRST 0xAA +#define CMD_READ_D_ID_SECOND 0x55 +#define CMD_READ_D_ID_THIRD 0x90 +#define CMD_READ_D_ID_FOURTH 0x7E +#define CMD_READ_D_ID_FIFTH 0x13 +#define CMD_READ_D_ID_SIXTH 0x00 + +// WRITE Commands + +// PROGRAM Commands +#define CMD_PROGRAM_FIRST 0xAA +#define CMD_PROGRAM_SECOND 0x55 +#define CMD_PROGRAM_THIRD 0xA0 + +// Write Buffer Commands +#define CMD_WRITE_TO_BUFFER_FIRST 0xAA +#define CMD_WRITE_TO_BUFFER_SECOND 0x55 +#define CMD_WRITE_TO_BUFFER_THIRD 0x25 +#define CMD_WRITE_TO_BUFFER_CONFIRM 0x29 + +// ERASE Commands + +// UNLOCK COMMANDS FOR ERASE +#define CMD_ERASE_FIRST 0xAA +#define CMD_ERASE_SECOND 0x55 +#define CMD_ERASE_THIRD 0x80 +#define CMD_ERASE_FOURTH 0xAA +#define CMD_ERASE_FIFTH 0x55 + +// Chip Erase Commands +#define CMD_CHIP_ERASE_SIXTH 0x10 + +// Sector Erase Commands +#define CMD_SECTOR_ERASE_SIXTH 0x30 + +// SUSPEND Commands +#define CMD_PROGRAM_OR_ERASE_SUSPEND 0xB0 +#define CMD_PROGRAM_OR_ERASE_RESUME 0x30 + +#endif /* __CFI_FLASH_H__ */ diff --git a/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.c b/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.c new file mode 100644 index 0000000..2683cae --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.c @@ -0,0 +1,184 @@ +/** @CfiNorFlashLib.c + + Copyright (c) 2015, Freescale Semiconductor, Inc. All rights reserved. + + 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 "CfiCommand.h" +#include "CfiNorFlashLib.h" + +VOID +FlashWrite_Data ( + FLASH_DATA Val, + UINTN Addr + ) +{ + *(volatile FLASH_DATA *)(Addr) = (Val); +} + +VOID +FlashWrite ( + FLASH_DATA Val, + UINTN Addr + ) +{ + FLASH_DATA ShiftVal; + ShiftVal = IfcNorFlashData (Val); + + *(volatile FLASH_DATA *)(Addr) = (ShiftVal); +} + +FLASH_DATA +FlashReadData ( + UINTN Addr + ) +{ + FLASH_DATA Val; + Val = *(volatile FLASH_DATA *)(Addr); + + return (Val); +} + +FLASH_DATA +FlashRead ( + UINTN Addr + ) +{ + FLASH_DATA Val; + FLASH_DATA ShiftVal; + + Val = *(volatile FLASH_DATA *)(Addr); + ShiftVal = IfcNorFlashData (Val); + + return (ShiftVal); +} + +STATIC +VOID +NorFlashReadCfiData ( + IN UINTN DeviceBaseAddress, + IN UINTN CFI_Offset, + IN UINT32 NumberOfShorts, + OUT VOID *Data + ) +{ + UINT32 Count; + FLASH_DATA *TmpData = (FLASH_DATA *)Data; + + for (Count = 0; Count < NumberOfShorts; Count++, TmpData++) { + *TmpData = FLASH_READ ((UINTN)((FLASH_DATA*)DeviceBaseAddress + CFI_Offset)); + CFI_Offset++; + } +} + +/* + Currently we support only CFI flash devices; Bail-out otherwise +*/ +EFI_STATUS +CfiNorFlashFlashGetAttributes ( + OUT NorFlashDescription *NorFlashDevices, + IN UINT32 Index + ) +{ + UINT32 Count; + FLASH_DATA QryData[3] = {0}; + FLASH_DATA BlockSize[2] = {0}; + UINTN DeviceBaseAddress = 0; + FLASH_DATA MaxNumBytes[2] = {0}; + FLASH_DATA Size = 0; + FLASH_DATA HighByteMask = 0xFF; // Masks High byte in a UIN16 word + FLASH_DATA HighByteShift = 8; // Bitshifts needed to make a byte High Byte in a UIN16 word + FLASH_DATA Temp1 = 0; + FLASH_DATA Temp2 = 0; + + for (Count = 0; Count < Index; Count++) { + + NorFlashDevices[Count].DeviceBaseAddress = DeviceBaseAddress = PcdGet64 (PcdFlashDeviceBase64); + + // Reset flash first + NorFlashPlatformReset (DeviceBaseAddress); + + // Enter the CFI Query Mode + SEND_NOR_COMMAND (DeviceBaseAddress, ENTER_CFI_QUERY_MODE_ADDR, + ENTER_CFI_QUERY_MODE_CMD); + + ArmDataSynchronizationBarrier (); + + // Query the unique QRY + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_UNIQUE_QRY_STRING, + 3, + &QryData); + if (QryData[0] != (FLASH_DATA)CFI_QRY_Q || QryData[1] != + (FLASH_DATA)CFI_QRY_R || QryData[2] != (FLASH_DATA)CFI_QRY_Y ) { + DEBUG ((DEBUG_ERROR, "Not a CFI flash (QRY not recvd): " + "Got = 0x%04x, 0x%04x, 0x%04x\n", + QryData[0], QryData[1], QryData[2])); + return EFI_DEVICE_ERROR; + } + + NorFlashReadCfiData (DeviceBaseAddress, CFI_QUERY_DEVICE_SIZE, + 1, &Size); + // Refer CFI Specification + NorFlashDevices[Count].Size = 1 << Size; + + NorFlashReadCfiData (DeviceBaseAddress, CFI_QUERY_BLOCK_SIZE, + 2, &BlockSize); + // Refer CFI Specification + NorFlashDevices[Count].BlockSize = 256 * ((FLASH_DATA) ((BlockSize[1] << + HighByteShift) | (BlockSize[0] & HighByteMask))); + + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_MAX_NUM_BYTES_WRITE, 2, &MaxNumBytes); + // Refer CFI Specification + /* from CFI query we get the Max. number of BYTE in multi-byte write = 2^N. + But our Flash Library is able to read/write in WORD size (2 bytes) which + is why we need to CONVERT MAX BYTES TO MAX WORDS by diving it by + width of word size */ + NorFlashDevices[Count].MultiByteWordCount =\ + (1 << ((FLASH_DATA)((MaxNumBytes[1] << HighByteShift) | + (MaxNumBytes[0] & HighByteMask))))/sizeof(FLASH_DATA); + + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_TYP_TIMEOUT_WORD_WRITE, 1, &Temp1); + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_MAX_TIMEOUT_WORD_WRITE, 1, &Temp2); + NorFlashDevices[Count].WordWriteTimeOut = (1U << Temp1) * (1U << Temp2); + + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_TYP_TIMEOUT_MAX_BUFFER_WRITE, 1, &Temp1); + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_MAX_TIMEOUT_MAX_BUFFER_WRITE, 1, &Temp2); + NorFlashDevices[Count].BufferWriteTimeOut = (1U << Temp1) * (1U << Temp2); + + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_TYP_TIMEOUT_BLOCK_ERASE, 1, &Temp1); + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_MAX_TIMEOUT_BLOCK_ERASE, 1, &Temp2); + NorFlashDevices[Count].BlockEraseTimeOut = + (1U << Temp1) * (1U << Temp2) * 1000; + + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_TYP_TIMEOUT_CHIP_ERASE, 1, &Temp1); + NorFlashReadCfiData (DeviceBaseAddress, + CFI_QUERY_MAX_TIMEOUT_CHIP_ERASE, 1, &Temp2); + NorFlashDevices[Count].ChipEraseTimeOut = + (1U << Temp1) * (1U << Temp2) * 1000; + + // Put device back into Read Array mode (via Reset) + NorFlashPlatformReset (DeviceBaseAddress); + } + + return EFI_SUCCESS; +} diff --git a/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.h b/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.h new file mode 100644 index 0000000..7e798dc --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/CfiNorFlashLib.h @@ -0,0 +1,54 @@ +/** @CfiNorFlashLib.h + + Copyright (c) 2015, Freescale Semiconductor, Inc. All rights reserved. + + 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 __CFI_NOR_FLASH_LIB_H__ +#define __CFI_NOR_FLASH_LIB_H__ + +#include +#include + +/* + * Values for the width of the port + */ +#define FLASH_CFI_8BIT 0x01 +#define FLASH_CFI_16BIT 0x02 +#define FLASH_CFI_32BIT 0x04 +#define FLASH_CFI_64BIT 0x08 + +#define NOR_FLASH_ERASE_RETRY 10 + +#define CREATE_BYTE_OFFSET(OffsetAddr) ((sizeof(FLASH_DATA)) * (OffsetAddr)) +#define CREATE_NOR_ADDRESS(BaseAddr,OffsetAddr) ((BaseAddr) + (OffsetAddr)) +#define FLASH_READ(Addr) FlashRead((Addr)) +#define FLASH_WRITE(Addr, Val) FlashWrite((Val), (Addr)) +#define FLASH_READ_DATA(Addr) FlashReadData((Addr)) +#define FLASH_WRITE_DATA(Addr, Val) FlashWrite_Data((Val), (Addr)) + +#define SEND_NOR_COMMAND(BaseAddr,Offset,Cmd) FLASH_WRITE(CREATE_NOR_ADDRESS(BaseAddr,CREATE_BYTE_OFFSET(Offset)), (Cmd)) + +#define GET_NOR_BLOCK_ADDRESS(BaseAddr,Lba,LbaSize) ( BaseAddr + (UINTN)((Lba) * LbaSize) ) + +VOID FlashWrite (FLASH_DATA Val, UINTN Addr); +FLASH_DATA FlashRead (UINTN Addr); + +VOID FlashWrite_Data (FLASH_DATA Val, UINTN Addr); +FLASH_DATA FlashReadData (UINTN Addr); + +EFI_STATUS +CfiNorFlashFlashGetAttributes ( + OUT NorFlashDescription *NorFlashDevices, + IN UINT32 Index + ); + +#endif /* __CFI_NOR_FLASH_LIB_H__ */ diff --git a/Platform/NXP/Library/NorFlashLib/IfcNorFlashLib.c b/Platform/NXP/Library/NorFlashLib/IfcNorFlashLib.c new file mode 100644 index 0000000..f87ebc3 --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/IfcNorFlashLib.c @@ -0,0 +1,52 @@ +/** @IfcNorLib.c + + Copyright (c) 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 + +/* + Initialise Integrated flash controller(IFC) NOR flash +*/ +VOID +IfcNorInit ( + VOID + ) +{ + UINT32 NorCs; + NorFlashInfo NorFlashInfo; + IfcTimings NorIfcTimings; + + GetIfcNorFlashInfo (&NorFlashInfo); + NorCs = NorFlashInfo.ChipSelect; + + // Get Nor Flash Timings + GetIfcNorFlashTimings (&NorIfcTimings); + + // Tune IFC Nor Flash Timings + IfcNorWrite32 (&NorFlashInfo.IfcRegs->CsprCs[NorCs].CsprExt, + NorIfcTimings.CsprExt); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->FtimCs[NorCs].Ftim[IFC_FTIM0], + NorIfcTimings.Ftim[0]); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->FtimCs[NorCs].Ftim[IFC_FTIM1], + NorIfcTimings.Ftim[1]); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->FtimCs[NorCs].Ftim[IFC_FTIM2], + NorIfcTimings.Ftim[2]); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->FtimCs[NorCs].Ftim[IFC_FTIM3], + NorIfcTimings.Ftim[3]); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->CsprCs[NorCs].Cspr, + NorIfcTimings.Cspr); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->AmaskCs[NorCs].Amask, + NorIfcTimings.Amask); + IfcNorWrite32 (&NorFlashInfo.IfcRegs->CsorCs[NorCs].Csor, + NorIfcTimings.Csor); +} diff --git a/Platform/NXP/Library/NorFlashLib/NorFlashLib.c b/Platform/NXP/Library/NorFlashLib/NorFlashLib.c new file mode 100644 index 0000000..e5e469f --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/NorFlashLib.c @@ -0,0 +1,658 @@ +/** @NorFlashLib.c + + Based on NorFlash implementation available in NorFlashDxe.c + + Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved. + Copyright (c) 2015, Freescale Semiconductor, Inc. All rights reserved. + + 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 "CfiCommand.h" +#include "CfiNorFlashLib.h" + +#define GET_BLOCK_OFFSET(Lba) ((Instance->RegionBaseAddress)-(Instance->DeviceBaseAddress)+((UINTN)((Lba) * Instance->Media.BlockSize))) + +NorFlashDescription mNorFlashDevices[NOR_FLASH_DEVICE_COUNT]; + +STATIC VOID +UnlockEraseAddress ( + IN UINTN DeviceBaseAddress + ) +{ // Issue the Unlock cmds + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_ERASE_FIRST); + + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_2_ADDR, + CMD_ERASE_SECOND); + + // Issue a setup command + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_ERASE_THIRD); + + // Issue the Unlock cmds + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_ERASE_FOURTH); + + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_2_ADDR, + CMD_ERASE_FIFTH); + + return; +} + +STATIC +UINT64 +ConvertMicroSecondsToTicks( + IN UINTN MicroSeconds +) +{ + UINT64 TimerTicks64; + + TimerTicks64 = 0; + + // Calculate counter ticks that represent requested delay: + // = MicroSeconds x TICKS_PER_MICRO_SEC + // = MicroSeconds x Timer Frequency(in Hz) x 10^-6 + // GetPerformanceCounterProperties = Get Arm Timer Frequency in Hz + TimerTicks64 = DivU64x32 ( + MultU64x64 ( + MicroSeconds, + GetPerformanceCounterProperties (NULL, NULL) + ), + 1000000U + ); + return TimerTicks64; +} + +/** + * The following function erases a NOR flash sector. + **/ +EFI_STATUS +NorFlashPlatformEraseSector ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN SectorAddress + ) +{ + FLASH_DATA EraseStatus1; + FLASH_DATA EraseStatus2; + UINT64 Timeout; + UINT64 SystemCounterVal; + + EraseStatus1 = 0; + EraseStatus2 = 0; + Timeout = 0; + + Timeout = ConvertMicroSecondsToTicks ( + mNorFlashDevices[Instance->Media.MediaId].BlockEraseTimeOut); + // Request a sector erase by writing two unlock cycles, followed by a + // setup command and two additional unlock cycles + + UnlockEraseAddress (Instance->DeviceBaseAddress); + + // Now send the address of the sector to be erased + SEND_NOR_COMMAND (SectorAddress, 0, CMD_SECTOR_ERASE_SIXTH); + + // Wait for erase to complete + // Read Sector start address twice to detect bit toggle and to + // determine ERASE DONE (all bits are 1) + // Get the maximum timer ticks needed to complete the operation + // Check if operation is complete or not in continous loop? + // if complete, exit from loop + // if not check the ticks that have been passed from the begining of loop + // if Maximum Ticks allocated for operation has passed exit from loop + + SystemCounterVal = GetPerformanceCounter (); + Timeout += SystemCounterVal; + while (SystemCounterVal < Timeout) { + if ((EraseStatus1 = FLASH_READ(SectorAddress)) == + (EraseStatus2 = FLASH_READ(SectorAddress))) { + if (0xFFFF == FLASH_READ(SectorAddress)) { + break; + } + } + SystemCounterVal = GetPerformanceCounter (); + } + + if(SystemCounterVal >= Timeout) { + DEBUG((DEBUG_ERROR, "%a :Failed to Erase @ SectorAddress 0x%p, Timeout\n", + __FUNCTION__, SectorAddress)); + return EFI_DEVICE_ERROR; + } else { + return EFI_SUCCESS; + } +} + +EFI_STATUS NorFlashPlatformWriteWord +( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN WordOffset, + IN FLASH_DATA Word +) +{ + UINT64 Timeout; + UINTN TargetAddress; + UINT64 SystemCounterVal; + FLASH_DATA Read1; + FLASH_DATA Read2; + + Timeout = 0; + + Timeout = ConvertMicroSecondsToTicks( + mNorFlashDevices[Instance->Media.MediaId].WordWriteTimeOut); + + TargetAddress = CREATE_NOR_ADDRESS(Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET(WordOffset)); + + // Issue the Unlock cmds + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_PROGRAM_FIRST); + + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, CMD_UNLOCK_2_ADDR, + CMD_PROGRAM_SECOND); + + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_PROGRAM_THIRD); + + FLASH_WRITE_DATA (TargetAddress, Word); + + // Wait for Write to Complete + // Read the last written address twice to detect bit toggle and + // to determine if date is wriiten successfully or not ? + // Get the maximum timer ticks needed to complete the operation + // Check if operation is complete or not in continous loop? + // if complete, exit from loop + // if not check the ticks that have been passed from the begining of loop + // if Maximum Ticks allocated for operation has passed, then exit from loop + + SystemCounterVal = GetPerformanceCounter (); + Timeout += SystemCounterVal; + while (SystemCounterVal < Timeout) { + if ((Read1 = FLASH_READ_DATA(TargetAddress)) == + (Read2 = FLASH_READ_DATA(TargetAddress))) { + if (Word == FLASH_READ_DATA(TargetAddress)) { + break; + } + } + SystemCounterVal = GetPerformanceCounter (); + } + + if(SystemCounterVal >= Timeout) { + DEBUG((DEBUG_ERROR, "%a: Failed to Write @ TargetAddress 0x%p, Timeout\n", + __FUNCTION__, TargetAddress)); + return EFI_DEVICE_ERROR; + } else { + return EFI_SUCCESS; + } +} + +EFI_STATUS NorFlashPlatformWritePageBuffer +( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN PageBufferOffset, + IN UINTN NumWords, + IN FLASH_DATA *Buffer +) +{ + UINT64 Timeout; + UINTN LastWrittenAddress; + FLASH_DATA LastWritenData; + UINTN current_offset; + UINTN end_offset; + UINTN TargetAddress; + UINT64 SystemCounterVal; + FLASH_DATA Read1; + FLASH_DATA Read2; + + // Initialize variables + Timeout = 0; + LastWrittenAddress = 0; + LastWritenData = 0; + current_offset = PageBufferOffset; + end_offset = PageBufferOffset + NumWords - 1; + Timeout = ConvertMicroSecondsToTicks ( + mNorFlashDevices[Instance->Media.MediaId].BufferWriteTimeOut); + TargetAddress = CREATE_NOR_ADDRESS (Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET (current_offset)); + + // don't try with a count of zero + if (!NumWords) { + return EFI_SUCCESS; + } + else if (NumWords == 1) { + return NorFlashPlatformWriteWord (Instance, PageBufferOffset, *Buffer); + } + + // Issue the Unlock cmds + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_WRITE_TO_BUFFER_FIRST); + + SEND_NOR_COMMAND (Instance->DeviceBaseAddress, CMD_UNLOCK_2_ADDR, + CMD_WRITE_TO_BUFFER_SECOND); + + // Write the buffer load + SEND_NOR_COMMAND (TargetAddress, 0, CMD_WRITE_TO_BUFFER_THIRD); + + // Write # of locations to program + SEND_NOR_COMMAND (TargetAddress, 0, (NumWords - 1)); + + // Load Data into Buffer + while (current_offset <= end_offset) { + LastWrittenAddress = CREATE_NOR_ADDRESS (Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET (current_offset++)); + LastWritenData = *Buffer++; + + // Write Data + FLASH_WRITE_DATA (LastWrittenAddress,LastWritenData); + } + + // Issue the Buffered Program Confirm command + SEND_NOR_COMMAND (TargetAddress, 0, CMD_WRITE_TO_BUFFER_CONFIRM); + + /* Wait for Write to Complete + Read the last written address twice to detect bit toggle and + to determine if date is wriiten successfully or not ? + Get the maximum timer ticks needed to complete the operation + Check if operation is complete or not in continous loop? + if complete, exit from loop + if not check the ticks that have been passed from the begining of loop + if Maximum Ticks allocated for operation has passed, then exit from loop **/ + SystemCounterVal = GetPerformanceCounter(); + Timeout += SystemCounterVal; + while (SystemCounterVal < Timeout) { + if ((Read1 = FLASH_READ_DATA (LastWrittenAddress)) == + (Read2 = FLASH_READ_DATA (LastWrittenAddress))) { + if (LastWritenData == FLASH_READ_DATA (LastWrittenAddress)) { + break; + } + } + SystemCounterVal = GetPerformanceCounter (); + } + + if (SystemCounterVal >= Timeout) { + DEBUG((DEBUG_ERROR, "%a: Failed to Write @LastWrittenAddress 0x%p, Timeout\n", + __FUNCTION__, LastWrittenAddress)); + return EFI_DEVICE_ERROR; + } else { + return EFI_SUCCESS; + } +} + +EFI_STATUS NorFlashPlatformWriteWordAlignedAddressBuffer +( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN Offset, + IN UINTN NumWords, + IN FLASH_DATA *Buffer + ) +{ + UINTN MultiByteWordCount; + UINTN Mask; + UINTN IntWords; + EFI_STATUS Status; + + MultiByteWordCount = mNorFlashDevices[Instance->Media.MediaId].MultiByteWordCount; + Mask = MultiByteWordCount - 1; + IntWords = NumWords; + Status = EFI_SUCCESS; + + if (Offset & Mask) + { + // program only as much as necessary, so pick the lower of the two numbers + if (NumWords < (MultiByteWordCount - (Offset & Mask))) { + IntWords = NumWords; + } + else { + IntWords = MultiByteWordCount - (Offset & Mask); + } + + // program the first few to get write buffer aligned + Status = NorFlashPlatformWritePageBuffer (Instance, Offset, IntWords, Buffer); + if (EFI_ERROR (Status)) { + return Status; + } + + Offset += IntWords; // adjust pointers and counter + NumWords -= IntWords; + Buffer += IntWords; + + if (NumWords == 0) { + return Status; + } + } + + while (NumWords >= MultiByteWordCount) // while big chunks to do + { + Status = NorFlashPlatformWritePageBuffer (Instance, Offset, + MultiByteWordCount, Buffer); + if (EFI_ERROR (Status)) { + return (Status); + } + + Offset += MultiByteWordCount; // adjust pointers and counter + NumWords -= MultiByteWordCount; + Buffer += MultiByteWordCount; + } + if (NumWords == 0) { + return (Status); + } + + Status = NorFlashPlatformWritePageBuffer (Instance, Offset, NumWords, Buffer); + return (Status); +} + +/* + * Writes data to the NOR Flash using the Buffered Programming method. + * + * Write Buffer Programming allows the system to write a maximum of 32 bytes + * in one programming operation. Therefore this function will only handle + * buffers up to 32 bytes. + * To deal with larger buffers, call this function again. + */ +EFI_STATUS +NorFlashPlatformWriteBuffer ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + FLASH_DATA *pSrcBuffer; + UINTN TargetOffsetinBytes; + UINTN WordsToWrite; + UINTN Mask; + UINTN BufferSizeInBytes; + UINTN IntBytes; + UINT8 *CopyFrom, *CopyTo; + FLASH_DATA TempWrite = 0; + + pSrcBuffer = (FLASH_DATA *)Buffer; + TargetOffsetinBytes = 0; + WordsToWrite = 0; + Mask = sizeof(FLASH_DATA) - 1; + BufferSizeInBytes = *NumBytes; + IntBytes = BufferSizeInBytes; // Intermediate Bytes needed to copy for alignment + TempWrite = 0; + + DEBUG ((DEBUG_BLKIO, "%a(Parameters: Lba=%ld, Offset=0x%x, " + "*NumBytes=0x%x, Buffer @ 0x%08x)\n", + __FUNCTION__, Lba, Offset, *NumBytes, Buffer)); + + TargetOffsetinBytes = GET_BLOCK_OFFSET (Lba) + (UINTN)(Offset); + + if (TargetOffsetinBytes & Mask) { + // Write only as much as necessary, so pick the lower of the two numbers + // and call it Intermediate bytes to write to make alignment proper + if (BufferSizeInBytes < (sizeof(FLASH_DATA) - (TargetOffsetinBytes & Mask))) { + IntBytes = BufferSizeInBytes; + } + else { + IntBytes = sizeof(FLASH_DATA) - (TargetOffsetinBytes & Mask); + } + + // Read the first few to get Read buffer aligned + NorFlashPlatformRead (Instance, Lba, (TargetOffsetinBytes & ~Mask) - + GET_BLOCK_OFFSET (Lba), sizeof(TempWrite), (UINT8*)&TempWrite); + + CopyTo = (UINT8*)&TempWrite; + CopyTo += (TargetOffsetinBytes & Mask); + CopyFrom = (UINT8*)Buffer; + + InternalMemCopyMem (CopyTo, CopyFrom, IntBytes); + + Status = NorFlashPlatformWriteWordAlignedAddressBuffer (Instance, + (UINTN)((TargetOffsetinBytes & ~Mask)/sizeof(FLASH_DATA)), + 1, &TempWrite); + if (EFI_ERROR (Status)) { + DEBUG((DEBUG_ERROR, "%a : Failed to Write @TargetOffset 0x%x (0x%x)\n", + __FUNCTION__, TargetOffsetinBytes, Status)); + goto EXIT; + } + + TargetOffsetinBytes += IntBytes; /* adjust pointers and counter */ + BufferSizeInBytes -= IntBytes; + Buffer += IntBytes; + + if (BufferSizeInBytes == 0) { + goto EXIT; + } + } + + // Write the bytes to CFI width aligned address. + // Note we can Write number of bytes=CFI width in one operation + WordsToWrite = BufferSizeInBytes/sizeof(FLASH_DATA); + pSrcBuffer = (FLASH_DATA*)Buffer; + + Status = NorFlashPlatformWriteWordAlignedAddressBuffer ( + Instance, + (UINTN)(TargetOffsetinBytes/sizeof(FLASH_DATA)), + WordsToWrite, + pSrcBuffer); + if (EFI_ERROR(Status)) { + DEBUG((DEBUG_ERROR, "%a : Failed to Write @ TargetOffset 0x%x (0x%x)\n", + __FUNCTION__, TargetOffsetinBytes, Status)); + goto EXIT; + } + + BufferSizeInBytes -= (WordsToWrite*sizeof(FLASH_DATA)); + Buffer += (WordsToWrite*sizeof(FLASH_DATA)); + TargetOffsetinBytes += (WordsToWrite*sizeof(FLASH_DATA)); + + if (BufferSizeInBytes == 0) { + goto EXIT; + } + + // Now Write bytes that are remaining and are less than CFI width. + // Read the first few to get Read buffer aligned + NorFlashPlatformRead (Instance, + Lba, + TargetOffsetinBytes - GET_BLOCK_OFFSET (Lba), + sizeof(TempWrite), + (UINT8*)&TempWrite); + + CopyFrom = (UINT8*)Buffer; + CopyTo = (UINT8*)&TempWrite; + + InternalMemCopyMem (CopyTo, CopyFrom, BufferSizeInBytes); + + Status = NorFlashPlatformWriteWordAlignedAddressBuffer (Instance, + (UINTN)(TargetOffsetinBytes/sizeof(FLASH_DATA)), + 1, + &TempWrite); + if (EFI_ERROR(Status)) { + DEBUG((DEBUG_ERROR, "%a: Failed to Write @TargetOffset 0x%x Status=%d\n", + __FUNCTION__, TargetOffsetinBytes, Status)); + goto EXIT; + } + +EXIT: + // Put device back into Read Array mode (via Reset) + NorFlashPlatformReset (Instance->DeviceBaseAddress); + return (Status); +} + +EFI_STATUS +NorFlashPlatformRead ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN BufferSizeInBytes, + OUT UINT8 *Buffer + ) +{ + UINTN IntBytes; + UINTN Mask; + FLASH_DATA TempRead; + UINT8 *CopyFrom, *CopyTo; + UINTN TargetOffsetinBytes; + FLASH_DATA *pReadData; + UINTN BlockSize; + + IntBytes = BufferSizeInBytes; // Intermediate Bytes needed to copy for alignment + Mask = sizeof(FLASH_DATA) - 1; + TempRead = 0; + TargetOffsetinBytes = (UINTN)(GET_BLOCK_OFFSET (Lba) + Offset); + BlockSize = Instance->Media.BlockSize; + + DEBUG ((DEBUG_BLKIO, "%a(Parameters: Lba=%ld, Offset=0x%x," + " BufferSizeInBytes=0x%x, Buffer @ 0x%p)\n", + __FUNCTION__, Lba, Offset, BufferSizeInBytes, Buffer)); + + // The buffer must be valid + if (Buffer == NULL) { + return EFI_INVALID_PARAMETER; + } + + // Return if we have not any byte to read + if (BufferSizeInBytes == 0) { + return EFI_SUCCESS; + } + + if (((Lba * BlockSize) + BufferSizeInBytes) > Instance->Size) { + DEBUG ((DEBUG_ERROR, "%a : Read will exceed device size.\n", __FUNCTION__)); + return EFI_INVALID_PARAMETER; + } + + // Put device back into Read Array mode (via Reset) + NorFlashPlatformReset (Instance->DeviceBaseAddress); + + // First Read bytes to make buffer aligned to CFI width + if (TargetOffsetinBytes & Mask) { + // Read only as much as necessary, so pick the lower of the two numbers + if (BufferSizeInBytes < (sizeof(FLASH_DATA) - (TargetOffsetinBytes & Mask))) { + IntBytes = BufferSizeInBytes; + } + else { + IntBytes = sizeof(FLASH_DATA) - (TargetOffsetinBytes & Mask); + } + + // Read the first few to get Read buffer aligned + TempRead = FLASH_READ_DATA (CREATE_NOR_ADDRESS ( + Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET ((TargetOffsetinBytes & ~Mask)/sizeof(FLASH_DATA)))); + + CopyFrom = (UINT8*)&TempRead; + CopyFrom += (TargetOffsetinBytes & Mask); + CopyTo = (UINT8*)Buffer; + + InternalMemCopyMem (CopyTo, CopyFrom, IntBytes); + + TargetOffsetinBytes += IntBytes; // adjust pointers and counter + BufferSizeInBytes -= IntBytes; + Buffer += IntBytes; + if (BufferSizeInBytes == 0) { + return EFI_SUCCESS; + } + } + + pReadData = (FLASH_DATA*)Buffer; + + // Readout the bytes from CFI width aligned address. + // Note we can read number of bytes=CFI width in one operation + while (BufferSizeInBytes >= sizeof(FLASH_DATA)) { + *pReadData = FLASH_READ_DATA (CREATE_NOR_ADDRESS ( + Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET (TargetOffsetinBytes/sizeof(FLASH_DATA)))); + pReadData += 1; + BufferSizeInBytes -= sizeof(FLASH_DATA); + TargetOffsetinBytes += sizeof(FLASH_DATA); + } + if (BufferSizeInBytes == 0) { + return EFI_SUCCESS; + } + + // Now read bytes that are remaining and are less than CFI width. + CopyTo = (UINT8*)pReadData; + // Read the first few to get Read buffer aligned + TempRead = FLASH_READ_DATA (CREATE_NOR_ADDRESS ( + Instance->DeviceBaseAddress, + CREATE_BYTE_OFFSET (TargetOffsetinBytes/sizeof(FLASH_DATA)))); + CopyFrom = (UINT8*)&TempRead; + + InternalMemCopyMem (CopyTo, CopyFrom, BufferSizeInBytes); + + return EFI_SUCCESS; +} + +EFI_STATUS +NorFlashPlatformReset ( + IN UINTN DeviceBaseAddress + ) +{ + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_1_ADDR, + CMD_RESET_FIRST); + + SEND_NOR_COMMAND (DeviceBaseAddress, CMD_UNLOCK_2_ADDR, + CMD_RESET_SECOND); + + SEND_NOR_COMMAND (DeviceBaseAddress, 0, CMD_RESET); + + return EFI_SUCCESS; +} + +EFI_STATUS +NorFlashPlatformGetDevices ( + OUT NorFlashDescription **NorFlashDevices, + OUT UINT32 *Count + ) +{ + if ((NorFlashDevices == NULL) || (Count == NULL)) { + return EFI_INVALID_PARAMETER; + } + + // Get the number of NOR flash devices supported + *NorFlashDevices = mNorFlashDevices; + *Count = NOR_FLASH_DEVICE_COUNT; + + return EFI_SUCCESS; +} + +EFI_STATUS +NorFlashPlatformFlashGetAttributes ( + OUT NorFlashDescription *NorFlashDevices, + IN UINT32 Count + ) +{ + EFI_STATUS Status; + UINT32 Index; + + if ((NorFlashDevices == NULL) || (Count == 0)) { + return EFI_INVALID_PARAMETER; + } + + // Check the attributes of the NOR flash slave we are connected to. + // Currently we support only CFI flash devices. Bail-out otherwise. + Status = CfiNorFlashFlashGetAttributes (NorFlashDevices, Count); + if (EFI_ERROR (Status)) { + return Status; + } + + // Limit the Size of Nor Flash that can be programmed + for (Index = 0; Index < Count; Index++) { + NorFlashDevices[Index].RegionBaseAddress = PcdGet64 (PcdFlashReservedRegionBase64); + NorFlashDevices[Index].Size -= (NorFlashDevices[Index].RegionBaseAddress - + NorFlashDevices[Index].DeviceBaseAddress); + if((NorFlashDevices[Index].RegionBaseAddress - NorFlashDevices[Index].DeviceBaseAddress) % + NorFlashDevices[Index].BlockSize) { + DEBUG ((DEBUG_ERROR, "%a : Reserved Region(0x%p) doesn't start " + "from block boundry(0x%08x)\n", __FUNCTION__, + (UINTN)NorFlashDevices[Index].RegionBaseAddress, + (UINT32)NorFlashDevices[Index].BlockSize)); + return EFI_DEVICE_ERROR; + } + } + return Status; +} diff --git a/Platform/NXP/Library/NorFlashLib/NorFlashLib.inf b/Platform/NXP/Library/NorFlashLib/NorFlashLib.inf new file mode 100644 index 0000000..14ca0ab --- /dev/null +++ b/Platform/NXP/Library/NorFlashLib/NorFlashLib.inf @@ -0,0 +1,42 @@ +#/** @NorFlashLib.inf +# +# Component description file for NorFlashLib 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 = 0x0001001A + BASE_NAME = NorFlashLib + FILE_GUID = f3176a49-dde1-450d-a909-8580c03b9ba8 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = NorFlashLib + +[Sources.common] + NorFlashLib.c + IfcNorFlashLib.c + CfiNorFlashLib.c + +[LibraryClasses] + ArmLib + IfcFlashLib + TimerLib + +[Packages] + ArmPkg/ArmPkg.dec + MdePkg/MdePkg.dec + Platform/NXP/NxpQoriqLs.dec + +[Pcd.common] + gNxpQoriqLsTokenSpaceGuid.PcdFlashDeviceBase64 + gNxpQoriqLsTokenSpaceGuid.PcdFlashReservedRegionBase64 -- 1.9.1