From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id A2777941312 for ; Wed, 24 Apr 2024 09:55:36 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=pvTh31NZ0W/DJfCb5HHAFsp4jhrBqCFYuO0pNsleiU8=; c=relaxed/simple; d=groups.io; h=Received-SPF:From:To:CC:Subject:Thread-Topic:Thread-Index:Date:Message-ID:References:In-Reply-To:Accept-Language:msip_labels:Authentication-Results-Original:nodisclaimer:MIME-Version:Original-Authentication-Results:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20240206; t=1713952535; v=1; b=J53Ib89THzVDoqQfSusOzWpTJ7bqbbxQ93Jy5OM95TXOG0jr335RbgJGaJovhgyFgSkzDsOD P8L5rxemiKQzl+XwZLxZvGpjCmTNFhyavTMFMAZJw3C4/OQamXvbYCLRQ/lSqmo96ZihqJyO6iw Mt0aHvYOTVDl5wVpQBxvfPjFPXTbjrQ/cKuD5acjufCmYZXdHJzoVBNC42q8qdF0zEB2z5tPnoE FqNWnAmRGR/PNv/7ZWzWMx13JGfgH+HSMIhzShsdbAcv0/8Q0jEn+WZRgskKSLWaa+4U8ii/j91 IHw5jEbBfte+qjBnlgdgfZ19jJmXjNy5uj3c3hTFRzIsg== X-Received: by 127.0.0.2 with SMTP id UBDVYY7687511x2ozHMeoqMZ; Wed, 24 Apr 2024 02:55:35 -0700 X-Received: from EUR04-DB3-obe.outbound.protection.outlook.com (EUR04-DB3-obe.outbound.protection.outlook.com [40.107.6.87]) by mx.groups.io with SMTP id smtpd.web11.12549.1713952533282721611 for ; Wed, 24 Apr 2024 02:55:34 -0700 X-Received: from AS4PR09CA0001.eurprd09.prod.outlook.com (2603:10a6:20b:5e0::7) by VI1PR08MB10217.eurprd08.prod.outlook.com (2603:10a6:800:1bc::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Wed, 24 Apr 2024 09:55:23 +0000 X-Received: from AM4PEPF00027A66.eurprd04.prod.outlook.com (2603:10a6:20b:5e0:cafe::cf) by AS4PR09CA0001.outlook.office365.com (2603:10a6:20b:5e0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.36 via Frontend Transport; Wed, 24 Apr 2024 09:55:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=arm.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C X-Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM4PEPF00027A66.mail.protection.outlook.com (10.167.16.91) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.7519.19 via Frontend Transport; Wed, 24 Apr 2024 09:55:22 +0000 X-Received: ("Tessian outbound caed45120527:v313"); Wed, 24 Apr 2024 09:55:22 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: dcc95f0e8863cafa X-CR-MTA-TID: 64aa7808 X-Received: from 22832a6a1dfe.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 7E58501D-D82D-4991-B403-27077B2DE182.1; Wed, 24 Apr 2024 09:55:15 +0000 X-Received: from EUR05-AM6-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 22832a6a1dfe.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 24 Apr 2024 09:55:15 +0000 X-Received: from AM0PR08MB4289.eurprd08.prod.outlook.com (2603:10a6:208:148::12) by PAWPR08MB11088.eurprd08.prod.outlook.com (2603:10a6:102:468::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Wed, 24 Apr 2024 09:55:13 +0000 X-Received: from AM0PR08MB4289.eurprd08.prod.outlook.com ([fe80::4a5d:48:bf49:a524]) by AM0PR08MB4289.eurprd08.prod.outlook.com ([fe80::4a5d:48:bf49:a524%7]) with mapi id 15.20.7472.044; Wed, 24 Apr 2024 09:55:13 +0000 From: "levi.yun" To: "devel@edk2.groups.io" , Sahil Kaushal CC: Ard Biesheuvel , =?koi8-r?Q?Leif_Lindholm_=9A?= , Sami Mujawar Subject: Re: [edk2-devel] [PATCH RESEND edk2-platforms][PATCH V2 12/14] Platform/ARM: Add CadenceQspiNorFlashDeviceLib for NorFlashDxe Thread-Topic: [edk2-devel] [PATCH RESEND edk2-platforms][PATCH V2 12/14] Platform/ARM: Add CadenceQspiNorFlashDeviceLib for NorFlashDxe Thread-Index: AQHalZBhcE1Uyn4dBE+AYxCJMJeoybF3L0U1 Date: Wed, 24 Apr 2024 09:55:13 +0000 Message-ID: References: <20240423055638.1271531-1-Sahil.Kaushal@arm.com> <20240423055638.1271531-13-Sahil.Kaushal@arm.com> In-Reply-To: <20240423055638.1271531-13-Sahil.Kaushal@arm.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; x-ms-traffictypediagnostic: AM0PR08MB4289:EE_|PAWPR08MB11088:EE_|AM4PEPF00027A66:EE_|VI1PR08MB10217:EE_ X-MS-Office365-Filtering-Correlation-Id: 40aed5c6-6739-4268-0853-08dc6444a8ad x-checkrecipientrouted: true nodisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: =?koi8-r?Q?GqFiGZJGvTduEuFrUwQtBfYZ1Eo34lAyf21OGf7BEdAu+LdABsAxdMAU/DJKLJ?= =?koi8-r?Q?nbPDuzohhz3piSlbqEMpmJ14hFfQbFRJ+wKqEXG9s75DaXWGDY3K8KVRYC9J+3?= =?koi8-r?Q?UmFVdZxPsn216VACuYyZCA0xy7hl9+AayO+hGTYBqB6H5o7XlvTduxbboLIZxG?= =?koi8-r?Q?H7IpjzNZ8vpag3ADH1pHCzPghW+l9F4eHBpFsNAQmKjf5DDpClqnJvqAAvYevS?= =?koi8-r?Q?0I/zHXXfraWK/wd28WwBfiYte9PwaCeMIartaGId6ADMu3TZcETBk8ee3pQH6h?= =?koi8-r?Q?jU5xgvhRKQLHCqfzQwR+egEcx8Nw6kfR/45EoB8TZ2V4xt/XJ1MA6hloIRa4ib?= =?koi8-r?Q?XXwYXjkplVIHMVJIrwSVS89T+nYbol+hBNz6saUg3qUiA+C1Uh0b4VhCKNKJ0x?= =?koi8-r?Q?XhhX37gPn7V4VQC1kNYUr7AxjzgPdkB99tRo8ptgrTMRvmsJ8lt4uYCF72lH3x?= =?koi8-r?Q?EpnOw3oHIyTq31N1JudODhTG4cNeYAoPyUwqlVTHwZWIiTjJUF5fBFW3wnZ0Lt?= =?koi8-r?Q?c7Zb3FgXLiPJuF9T9SvFMgmKHwJbrhRWB2XN8JlZD6G3aVU/4d+Gu33u0a9XE5?= =?koi8-r?Q?cZ37NalA5cP6UAzGKTtbQtZtXPJtj+Z6AdBpuupUxj0PYaathr569ep6QxtBpC?= =?koi8-r?Q?tP5HHz6TULP2Yo85hUBp5ssh/8rRTXzGgHZs2NHVPdh+Ry1hU2/N5tTVlYILLW?= =?koi8-r?Q?9DZCX9xl4J1i2ZuNUEP65vuh/Bqw3ipuveZTkaqdd1PL0Pduox6QHiph+txwum?= =?koi8-r?Q?NtaL+pKwMWnOkZZr4XdyCM1cVg5DvErppids8V8Y4gfsEmLo7wpQtF2Dlbsa8H?= =?koi8-r?Q?/SxEmaGtNEFsV6v2i/RWfTGsmYhGtY95FHlrcCC4iVQxZqRps0sGUsEvRrAHVy?= =?koi8-r?Q?+8uNc2HIx9yKhNzTA8SeiBihq19Dywy/c5GDMEVA5SarEH8/PtXzqlSy9+w/lz?= =?koi8-r?Q?0o3eziM/UtFVfyBNoZK2vyvxKyxKPfShXrC+W14xz2QyjoHJpuy+30KGJQzVfj?= =?koi8-r?Q?ZeWnVdkNDZDTYLVZ1ZR5lmIDS9hWeiOcM3+lwccF18wafpgP5TNFw06veyJesp?= =?koi8-r?Q?TZLS8yUYYTG4c4vrqH67R+MFn88SG3O3IIUb7iiO9NC3n/M+LHln2vowwrjGgU?= =?koi8-r?Q?0UQfksfkACu4Xd4Gjl63XEdseIfNy9h0HgNRdPEk6lVpfuo6u1THvkwiUtvxle?= =?koi8-r?Q?mvsUtoGvLOBlU2XleRkP3WtZOV/iO128y3aB1i5lDKn4p3Up/BjSL0e9CsFisW?= =?koi8-r?Q?sctRMXcK8X0yhCUxYyCGLMdbkGLF8I/1UXSxnXQ04aHn4Z5FRmyfNZChO+d8f8?= =?koi8-r?Q?Cw=3D=3D?= X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM0PR08MB4289.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(1800799015)(376005)(38070700009);DIR:OUT;SFP:1101; MIME-Version: 1.0 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAWPR08MB11088 Original-Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM4PEPF00027A66.eurprd04.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 2f91d397-4ac2-40ca-b76b-08dc6444a2f0 X-Microsoft-Antispam-Message-Info: =?koi8-r?Q?FW/f+xjcpqrFJNmT9kZaF2jerSjsk1Iy9wGtmAYPs/KWZJAt+jXXG68JvGzYUv?= =?koi8-r?Q?cV8vmF1eQ/PPDAe5pLa1MVdohSBqKDfepJhpuv/eKMf+GtIkN4C+ppql7KCNZ5?= =?koi8-r?Q?bZuO8tcKzY/G7C94+p63ciVzNMzwRPr0Xxdk+vcIeDiRlKYTVIKEo4FNH+FVFD?= =?koi8-r?Q?7lz42fA0gouzKliASGsLAOaqBjPPorgBd4BVU90GiiyKZjH6lWqXrcygm23kUR?= =?koi8-r?Q?vBDidNtLuQe+2/oMi4+3QHULojUYzGflmniZ0uKCKD67BBJElgVlQxsb92TRyu?= =?koi8-r?Q?IwuCdlOORImZuO9LAeF7SrJo7FGBa9NdgDftvFuQlQYZxM80VxNKV91ki80EEU?= =?koi8-r?Q?ZblMf9AZqtzl5dN+cjT02nEJjuYCy+rn9dYtk2ZEnhF5a9nYjUpJ2+u/45EyJP?= =?koi8-r?Q?mIzRcwrTmCUeCLMA9GsBVbeNpSBi15aOeVjqjYCD0KXFKImrMc9qbV6/gPoAxk?= =?koi8-r?Q?u6H7D6eaVVQ0bVOZlRpQ+H5B14PGvAVMvb4ycm2Fjv8Re3gtWbJSy0q1DDEUk4?= =?koi8-r?Q?TvMGwE7ZRgRTn9DA3708vtV3YZJAHYhNt0aRpgIb9GUXE3JmpDuRDIje1cvHUn?= =?koi8-r?Q?+UcjSat2hLXTOO2T+plra4OkaifXCW4gcFt2UVmUsre0bg9RXroU5cWP+XMKEI?= =?koi8-r?Q?ZtS6xI0zSzUTY+Tn5GPpzI7Z8uDcn8Sd8gLzScv4h0OeM8VeXw3PsXuxxTWMrB?= =?koi8-r?Q?tckcxUGmfbVPdEWFbaJFMKqY2qUh4q0toBffiVQbbs0qDpDKQTPNjtO/jrOUzQ?= =?koi8-r?Q?/LMdHGicSK999Qys7yMhilEDUPvbTKtgT6BT7XcCDvLHXhxN7zt4e/GRvHt+MU?= =?koi8-r?Q?T/kqnIY5hTNTH93IX6cgqM/OusVn6bbjC7yh5barucZy7r/ND94GlBhlBu+wih?= =?koi8-r?Q?gTck+OtorJ1uSDkMb2hKNW13MG4rrfWhLyM4PfAa5mpuHDtxlaA74n1ldOKXoB?= =?koi8-r?Q?8sUN3qQOMydGmgl9TNGXTtEAhiuqpnvBg5u4oO8TAvO03aqveCngCIc3k0RXEH?= =?koi8-r?Q?v9FcwPy0a+KmHx+P5eRWpi2bk1iqcV7rbTLxl8+KJ1GPIiYlxsTqrbuKHmyECX?= =?koi8-r?Q?wbTywYHO1KNVSmI9UP4sLtrFMfDo0p6cQn0gETavA/47Av/uo3NGjPPZwSo3oO?= =?koi8-r?Q?wJBKaGecSV433Uo35GHN52EuI0NtkGRjnderF39GzFEHTW7gzwPqXMzfwdkJ/c?= =?koi8-r?Q?ufRUgCiFt7IPT7S3u05LoN0MXP1ij7rqHDXJ4Lt/5XJpz8ikZvewfyOyppX4/H?= =?koi8-r?Q?Rh2u4mMnLSzRLCZmjnMIB9mx6NMvMHT958wK0cVkTCnRD0IR6UuFqDl9GUfBTA?= =?koi8-r?Q?yVcdI7Zn0GMt9x8sck4dXLYNVC4rxIChtKFLjVtQ2Qon?= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2024 09:55:22.9608 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 40aed5c6-6739-4268-0853-08dc6444a8ad X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM4PEPF00027A66.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB10217 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Wed, 24 Apr 2024 02:55:34 -0700 Resent-From: yeoreum.yun@arm.com Reply-To: devel@edk2.groups.io,yeoreum.yun@arm.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: QonIaidRPIPs7MoSV8e32l2Bx7686176AA= Content-Language: en-GB Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=J53Ib89T; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=arm.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io > +EFI_STATUS > +NorFlashWriteSingleBlock ( > + IN NOR_FLASH_INSTANCE *Instance, > + IN EFI_LBA Lba, > + IN UINTN Offset, > + IN OUT UINTN *NumBytes, > + IN UINT8 *Buffer > + ) > +{ > + EFI_STATUS Status; > + UINT32 Tmp; > + UINT32 TmpBuf; > + UINT32 WordToWrite; > + UINT32 Mask; > + BOOLEAN DoErase; > + UINTN BytesToWrite; > + UINTN CurOffset; > + UINTN WordAddr; > + UINTN BlockSize; > + UINTN BlockAddress; > + UINTN PrevBlockAddress; > + > + if (Buffer =3D=3D NULL) { > + DEBUG (( > + DEBUG_ERROR, > + "NorFlashWriteSingleBlock: ERROR - Buffer is invalid\n" > + )); > + return EFI_OUT_OF_RESOURCES; > + } Is it much better to return EFI_INVALID_PARAMETER? Also, I think NumBytes should be checked too. right? ________________________________________ From: devel@edk2.groups.io on behalf of Sahil Kausha= l via groups.io Sent: 23 April 2024 06:56 To: devel@edk2.groups.io Cc: Ard Biesheuvel; Leif Lindholm ; Sami Mujawar; Sahil Kaushal Subject: [edk2-devel] [PATCH RESEND edk2-platforms][PATCH V2 12/14] Platfor= m/ARM: Add CadenceQspiNorFlashDeviceLib for NorFlashDxe From: sahil In N1Sdp platform, the SoC is connected to IOFPGA which has a Cadence Quad SPI (QSPI) controller. This QSPI controller manages the flash chip device via QSPI bus. This patch adds CadenceQspiNorFlashDeviceLib which is used to manage and access the above configuration. Signed-off-by: sahil --- Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDevic= eLib.inf | 32 + Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDevic= eLib.h | 44 + Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDevic= eLib.c | 1011 ++++++++++++++++++++ 3 files changed, 1087 insertions(+) diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiN= orFlashDeviceLib.inf b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/Ca= denceQspiNorFlashDeviceLib.inf new file mode 100644 index 000000000000..506876b62285 --- /dev/null +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlash= DeviceLib.inf @@ -0,0 +1,32 @@ +#/** @file +# +# Component description file for CadenceQspiNorFlashDeviceLib Library +# +# Copyright (c) 2024, Arm Limited. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D CadenceQspiNorFlashDeviceLib + FILE_GUID =3D ed172366-066b-4998-9b5e-ca7f385a170b + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D NorFlashDeviceLib + +[Sources.common] + CadenceQspiNorFlashDeviceLib.c + CadenceQspiNorFlashDeviceLib.h + +[Packages] + MdePkg/MdePkg.dec + Platform/ARM/ARM.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + IoLib + TimerLib diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiN= orFlashDeviceLib.h b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/Cade= nceQspiNorFlashDeviceLib.h new file mode 100644 index 000000000000..d43d27fe5eb4 --- /dev/null +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlash= DeviceLib.h @@ -0,0 +1,44 @@ +/** @file + + Copyright (c) 2024, ARM Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef CADENCE_QSPI_NOR_FLASH_DEVICE_LIB_H_ +#define CADENCE_QSPI_NOR_FLASH_DEVICE_LIB_H_ + +#define NOR_FLASH_ERASE_RETRY 10 + +// QSPI Controller defines +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSET 0x90 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_EXECUTE 0x01 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_ADDR_ENABLE 0x01 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_ADDR_BIT_POS 19 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_ADDR_BYTE_BIT_POS 16 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT 0x02 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_OPCODE_BIT_POS 24 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_READ_ENABLE 0x01 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_READ_BYTE_3B 0x02 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_READEN_BIT_POS 23 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_READBYTE_BIT_POS 20 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_DUMMY_8C 0x8 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_DUMMY_BIT_POS 7 +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_DATA_BYTES(x) ((x - 1) << CDNS_Q= SPI_FLASH_CMD_CTRL_REG_READBYTE_BIT_POS) +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_ADDR_BYTES(x) ((x - 1) << CDNS_Q= SPI_FLASH_CMD_CTRL_REG_ADDR_BYTE_BIT_POS) + +#define CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET 0xA0 + +#define CDNS_QSPI_FLASH_CMD_ADDR_REG_OFFSET 0x94 + +#define SPINOR_SR_WIP BIT0 // Write in progress + +#define SPINOR_OP_WREN 0x06 // Write enable +#define SPINOR_OP_BE_4K 0x20 // Erase 4KiB block +#define SPINOR_OP_RDID 0x9f // Read JEDEC ID +#define SPINOR_OP_RDSR 0x05 // Read status register + +#define SPINOR_SR_WIP_POLL_TIMEOUT_MS 1000u // Status Register read timeo= ut + +#endif /* CADENCE_QSPI_NOR_FLASH_DEVICE_LIB_H_ */ diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiN= orFlashDeviceLib.c b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/Cade= nceQspiNorFlashDeviceLib.c new file mode 100644 index 000000000000..8a416e07f15e --- /dev/null +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlash= DeviceLib.c @@ -0,0 +1,1011 @@ +/** @file + + Copyright (c) 2024 ARM Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include + +#include "CadenceQspiNorFlashDeviceLib.h" + +/** + Execute Flash cmd ctrl and Read Status. + + @param[in] Instance NOR flash Instance. + @param[in] Val Value to be written to Flash cmd ctrl R= egister. + + @retval EFI_SUCCESS Request is executed successfully. + +**/ +STATIC +EFI_STATUS +CdnsQspiExecuteCommand ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINT32 Val + ) +{ + // Set the command + MmioWrite32 ( + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSE= T, + Val + ); + // Execute the command + MmioWrite32 ( + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSE= T, + Val | CDNS_QSPI_FLASH_CMD_CTRL_REG_EXECUTE + ); + + // Wait until command has been executed + while ( + (MmioRead32 ( + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_R= EG_OFFSET + ) & CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT) =3D=3D + CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT + ) + { + continue; + } + + return EFI_SUCCESS; +} + +/** + Converts milliseconds into number of ticks of the performance counter. + + @param[in] Milliseconds Milliseconds to convert into ticks. + + @retval Milliseconds expressed as number of ticks. + +**/ +STATIC +UINT64 +MilliSecondsToTicks ( + IN UINTN Milliseconds + ) +{ + CONST UINT64 NanoSecondsPerTick =3D GetTimeInNanoSecond (1); + + return (Milliseconds * 1000000) / NanoSecondsPerTick; +} + +/** + Poll Status register for NOR flash erase/write completion. + + @param[in] Instance NOR flash Instance. + + @retval EFI_SUCCESS Request is executed successfully. + @retval EFI_TIMEOUT Operation timed out. + @retval EFI_DEVICE_ERROR Controller operartion failed. + +**/ +STATIC +EFI_STATUS +NorFlashPollStatusRegister ( + IN NOR_FLASH_INSTANCE *Instance + ) +{ + BOOLEAN SRegDone; + UINT32 val; + + val =3D (SPINOR_OP_RDSR << CDNS_QSPI_FLASH_CMD_CTRL_REG_OPCODE_BIT_POS) = | + (CDNS_QSPI_FLASH_CMD_CTRL_REG_READ_ENABLE << + CDNS_QSPI_FLASH_CMD_CTRL_REG_READEN_BIT_POS) | + CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_DATA_BYTES (1) | + (CDNS_QSPI_FLASH_CMD_CTRL_REG_DUMMY_8C << + CDNS_QSPI_FLASH_CMD_CTRL_REG_DUMMY_BIT_POS); + + CONST UINT64 TickOut =3D + GetPerformanceCounter () + + MilliSecondsToTicks (SPINOR_SR_WIP_POLL_TIMEOUT_MS); + + do { + if (GetPerformanceCounter () > TickOut) { + DEBUG (( + DEBUG_ERROR, + "NorFlashPollStatusRegister: Timeout waiting for erase/write.\n" + )); + return EFI_TIMEOUT; + } + + if (EFI_ERROR (CdnsQspiExecuteCommand (Instance, val))) { + return EFI_DEVICE_ERROR; + } + + SRegDone =3D + (MmioRead8 ( + Instance->HostRegisterBaseAddress + + CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET + ) + & SPINOR_SR_WIP + ) =3D=3D 0; + } while (!SRegDone); + + return EFI_SUCCESS; +} + +/** + Check whether NOR flash opertions are Locked. + + @param[in] Instance NOR flash Instance. + @param[in] BlockAddress BlockAddress in NOR flash device. + + @retval FALSE If NOR flash is not locked. +**/ +STATIC +BOOLEAN +NorFlashBlockIsLocked ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN BlockAddress + ) +{ + return FALSE; +} + +/** + Unlock NOR flash operations on given block. + + @param[in] Instance NOR flash instance. + @param[in] BlockAddress BlockAddress in NOR flash device. + + @retval EFI_SUCCESS NOR flash operations is unlocked. +**/ +EFI_STATUS +NorFlashUnlockSingleBlock ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN BlockAddress + ) +{ + return EFI_SUCCESS; +} + +/** + Unlock NOR flash operations if it is necessary. + + @param[in] Instance NOR flash instance. + @param[in] BlockAddress BlockAddress in NOR flash device. + + @retval EFI_SUCCESS Request is executed successfully. +**/ +EFI_STATUS +NorFlashUnlockSingleBlockIfNecessary ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN BlockAddress + ) +{ + EFI_STATUS Status; + + Status =3D EFI_SUCCESS; + + if (!NorFlashBlockIsLocked (Instance, BlockAddress)) { + Status =3D NorFlashUnlockSingleBlock (Instance, BlockAddress); + } + + return Status; +} + +/** + Enable write to NOR flash device. + + @param[in] Instance NOR flash instance. + + @retval EFI_SUCCESS Request is executed successfully. + @retval EFI_DEVICE_ERROR The device reported an error. +**/ +STATIC +EFI_STATUS +NorFlashEnableWrite ( + IN NOR_FLASH_INSTANCE *Instance + ) +{ + UINT32 val; + + DEBUG ((DEBUG_INFO, "NorFlashEnableWrite()\n")); + val =3D (SPINOR_OP_WREN << CDNS_QSPI_FLASH_CMD_CTRL_REG_OPCODE_BIT_POS); + if (EFI_ERROR (CdnsQspiExecuteCommand (Instance, val))) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + The following function presumes that the block has already been unlocked= . + + @param[in] Instance NOR flash instance. + @param[in] BlockAddress Block address within the variable regi= on. + + @retval EFI_SUCCESS Request is executed successfully. + @retval EFI_DEVICE_ERROR The device reported an error. + **/ +EFI_STATUS +NorFlashEraseSingleBlock ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN BlockAddress + ) +{ + UINT32 DevConfigVal; + UINT32 EraseOffset; + + EraseOffset =3D 0x0; + + DEBUG (( + DEBUG_INFO, + "NorFlashEraseSingleBlock(BlockAddress=3D0x%08x)\n", + BlockAddress + )); + + if (EFI_ERROR (NorFlashEnableWrite (Instance))) { + return EFI_DEVICE_ERROR; + } + + EraseOffset =3D BlockAddress - Instance->DeviceBaseAddress; + + MmioWrite32 ( + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_ADDR_REG_OFFSE= T, + EraseOffset + ); + + DevConfigVal =3D (SPINOR_OP_BE_4K << CDNS_QSPI_FLASH_CMD_CTRL_REG_OPCODE= _BIT_POS) | + (CDNS_QSPI_FLASH_CMD_CTRL_REG_ADDR_ENABLE << + CDNS_QSPI_FLASH_CMD_CTRL_REG_ADDR_BIT_POS) | + CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_ADDR_BYTES (3); + + if (EFI_ERROR (CdnsQspiExecuteCommand (Instance, DevConfigVal))) { + return EFI_DEVICE_ERROR; + } + + if (EFI_ERROR (NorFlashPollStatusRegister (Instance))) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + This function unlock and erase an entire NOR Flash block. + + @param[in] Instance NOR flash Instance of variable store regio= n. + @param[in] BlockAddress Block address within the variable store re= gion. + + @retval EFI_SUCCESS The erase and unlock successfully complete= d. +**/ +EFI_STATUS +NorFlashUnlockAndEraseSingleBlock ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN BlockAddress + ) +{ + EFI_STATUS Status; + UINTN Index; + EFI_TPL OriginalTPL; + + NorFlashLock (&OriginalTPL); + + Index =3D 0; + do { + // Unlock the block if we have to + Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddres= s); + if (EFI_ERROR (Status)) { + break; + } + + Status =3D NorFlashEraseSingleBlock (Instance, BlockAddress); + if (EFI_ERROR (Status)) { + break; + } + + Index++; + } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status =3D=3D EFI_WRITE_PRO= TECTED)); + + if (Index =3D=3D NOR_FLASH_ERASE_RETRY) { + DEBUG (( + DEBUG_ERROR, + "EraseSingleBlock(BlockAddress=3D0x%08x: Block Locked Error " + "(try to erase %d times)\n", + BlockAddress, + Index + )); + } + + NorFlashUnlock (OriginalTPL); + + return Status; +} + +/** + Write a single word to given location. + + @param[in] Instance NOR flash Instance of variable store reg= ion. + @param[in] WordAddress The address in NOR flash to write given = word. + @param[in] WriteData The data to write into NOR flash locatio= n. + + @retval EFI_SUCCESS The write is completed. + @retval EFI_DEVICE_ERROR The device reported an error. +**/ +EFI_STATUS +NorFlashWriteSingleWord ( + IN NOR_FLASH_INSTANCE *Instance, + IN UINTN WordAddress, + IN UINT32 WriteData + ) +{ + DEBUG (( + DEBUG_INFO, + "NorFlashWriteSingleWord(WordAddress=3D0x%08x, WriteData=3D0x%08x)\n", + WordAddress, + WriteData + )); + + if (EFI_ERROR (NorFlashEnableWrite (Instance))) { + return EFI_DEVICE_ERROR; + } + + MmioWrite32 (WordAddress, WriteData); + if (EFI_ERROR (NorFlashPollStatusRegister (Instance))) { + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Write a full block to given location. + + @param[in] Instance NOR flash Instance of variable store re= gion. + @param[in] Lba The logical block address in NOR flash. + @param[in] DataBuffer The data to write into NOR flash locati= on. + @param[in] BlockSizeInWords The number of bytes to write. + + @retval EFI_SUCCESS The write is completed. +**/ +EFI_STATUS +NorFlashWriteFullBlock ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINT32 *DataBuffer, + IN UINT32 BlockSizeInWords + ) +{ + EFI_STATUS Status; + UINTN WordAddress; + UINT32 WordIndex; + UINTN BlockAddress; + EFI_TPL OriginalTPL; + + Status =3D EFI_SUCCESS; + + // Get the physical address of the block + BlockAddress =3D GET_NOR_BLOCK_ADDRESS ( + Instance->RegionBaseAddress, + Lba, + BlockSizeInWords * 4 + ); + + // Start writing from the first address at the start of the block + WordAddress =3D BlockAddress; + + NorFlashLock (&OriginalTPL); + + Status =3D NorFlashUnlockAndEraseSingleBlock (Instance, BlockAddress); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "WriteSingleBlock: ERROR - Failed to Unlock and Erase the single " + "block at 0x%X\n", + BlockAddress + )); + goto EXIT; + } + + for (WordIndex =3D 0; + WordIndex < BlockSizeInWords; + WordIndex++, DataBuffer++, WordAddress +=3D 4) + { + Status =3D NorFlashWriteSingleWord (Instance, WordAddress, *DataBuffer= ); + if (EFI_ERROR (Status)) { + goto EXIT; + } + } + +EXIT: + NorFlashUnlock (OriginalTPL); + + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "NOR FLASH Programming [WriteSingleBlock] failed at address 0x%08x. = " + "Exit Status =3D %r.\n", + WordAddress, + Status + )); + } + + return Status; +} + +/** + Write a full block. + + @param[in] Instance NOR flash Instance of variable stor= e region. + @param[in] Lba The starting logical block index. + @param[in] BufferSizeInBytes The number of bytes to read. + @param[in] Buffer The pointer to a caller-allocated b= uffer that + contains the source for the write. + + @retval EFI_SUCCESS The write is completed. + @retval EFI_INVALID_PARAMETER Invalid parameters passed. + @retval EFI_BAD_BUFFER_SIZE Invalid buffer size passed. +**/ +EFI_STATUS +NorFlashWriteBlocks ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN BufferSizeInBytes, + IN VOID *Buffer + ) +{ + UINT32 *pWriteBuffer; + EFI_STATUS Status; + EFI_LBA CurrentBlock; + UINT32 BlockSizeInWords; + UINT32 NumBlocks; + UINT32 BlockCount; + + Status =3D EFI_SUCCESS; + // The buffer must be valid + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // We must have some bytes to read + DEBUG (( + DEBUG_INFO, + "NorFlashWriteBlocks: BufferSizeInBytes=3D0x%x\n", + BufferSizeInBytes + )); + if (BufferSizeInBytes =3D=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + // The size of the buffer must be a multiple of the block size + DEBUG (( + DEBUG_INFO, + "NorFlashWriteBlocks: BlockSize in bytes =3D0x%x\n", + Instance->Media.BlockSize + )); + if ((BufferSizeInBytes % Instance->Media.BlockSize) !=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + // All blocks must be within the device + NumBlocks =3D ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize; + + DEBUG (( + DEBUG_INFO, + "NorFlashWriteBlocks: NumBlocks=3D%d, LastBlock=3D%ld, Lba=3D%ld.\n", + NumBlocks, + Instance->Media.LastBlock, + Lba + )); + + if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) { + DEBUG (( + DEBUG_ERROR, + "NorFlashWriteBlocks: ERROR - Write will exceed last block.\n" + )); + return EFI_INVALID_PARAMETER; + } + + ASSERT (((UINTN)Buffer % sizeof (UINT32)) =3D=3D 0); + + BlockSizeInWords =3D Instance->Media.BlockSize / 4; + + // Because the target *Buffer is a pointer to VOID, we must put + // all the data into a pointer to a proper data type, so use *ReadBuffer + pWriteBuffer =3D (UINT32 *)Buffer; + + CurrentBlock =3D Lba; + for (BlockCount =3D 0; + BlockCount < NumBlocks; + BlockCount++, CurrentBlock++, pWriteBuffer +=3D BlockSizeInWords) + { + DEBUG (( + DEBUG_INFO, + "NorFlashWriteBlocks: Writing block #%d\n", + (UINTN)CurrentBlock + )); + + Status =3D NorFlashWriteFullBlock ( + Instance, + CurrentBlock, + pWriteBuffer, + BlockSizeInWords + ); + + if (EFI_ERROR (Status)) { + break; + } + } + + DEBUG ((DEBUG_INFO, "NorFlashWriteBlocks: Exit Status =3D %r.\n", Status= )); + return Status; +} + +/** + Read a full block. + + @param[in] Instance NOR flash Instance of variable sto= re region. + @param[in] Lba The starting logical block index t= o read from. + @param[in] BufferSizeInBytes The number of bytes to read. + @param[out] Buffer The pointer to a caller-allocated = buffer that + should be copied with read data. + + @retval EFI_SUCCESS The read is completed. + @retval EFI_INVALID_PARAMETER Invalid parameters passed. + @retval EFI_BAD_BUFFER_SIZE Invalid buffer size passed. +**/ +EFI_STATUS +NorFlashReadBlocks ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN BufferSizeInBytes, + OUT VOID *Buffer + ) +{ + UINT32 NumBlocks; + UINTN StartAddress; + + DEBUG (( + DEBUG_INFO, + "NorFlashReadBlocks: BufferSize=3D0x%xB BlockSize=3D0x%xB LastBlock=3D= %ld, " + "Lba=3D%ld.\n", + BufferSizeInBytes, + Instance->Media.BlockSize, + Instance->Media.LastBlock, + Lba + )); + + // The buffer must be valid + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // Return if we do not have any byte to read + if (BufferSizeInBytes =3D=3D 0) { + return EFI_SUCCESS; + } + + // The size of the buffer must be a multiple of the block size + if ((BufferSizeInBytes % Instance->Media.BlockSize) !=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + NumBlocks =3D ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize; + + if ((Lba + NumBlocks) > (Instance->Media.LastBlock + 1)) { + DEBUG (( + DEBUG_ERROR, + "NorFlashReadBlocks: ERROR - Read will exceed last block\n" + )); + return EFI_INVALID_PARAMETER; + } + + // Get the address to start reading from + StartAddress =3D GET_NOR_BLOCK_ADDRESS ( + Instance->RegionBaseAddress, + Lba, + Instance->Media.BlockSize + ); + + // Readout the data + CopyMem (Buffer, (UINTN *)StartAddress, BufferSizeInBytes); + + return EFI_SUCCESS; +} + +/** + Read from nor flash. + + @param[in] Instance NOR flash Instance of variable sto= re region. + @param[in] Lba The starting logical block index t= o read from. + @param[in] Offset Offset into the block at which to = begin reading. + @param[in] BufferSizeInBytes The number of bytes to read. + @param[out] Buffer The pointer to a caller-allocated = buffer that + should copied with read data. + + @retval EFI_SUCCESS The read is completed. + @retval EFI_INVALID_PARAMETER Invalid parameters passed. +**/ +EFI_STATUS +NorFlashRead ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN BufferSizeInBytes, + OUT VOID *Buffer + ) +{ + UINTN StartAddress; + + // The buffer must be valid + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // Return if we do not have any byte to read + if (BufferSizeInBytes =3D=3D 0) { + return EFI_SUCCESS; + } + + if (((Lba * Instance->Media.BlockSize) + Offset + BufferSizeInBytes) > + Instance->Size) + { + DEBUG (( + DEBUG_ERROR, + "NorFlashRead: ERROR - Read will exceed device size.\n" + )); + return EFI_INVALID_PARAMETER; + } + + // Get the address to start reading from + StartAddress =3D GET_NOR_BLOCK_ADDRESS ( + Instance->RegionBaseAddress, + Lba, + Instance->Media.BlockSize + ); + + // Readout the data + CopyMem (Buffer, (UINTN *)(StartAddress + Offset), BufferSizeInBytes); + + return EFI_SUCCESS; +} + +/** + Write a full or portion of a block. + + @param[in] Instance NOR flash Instance of variable st= ore region. + @param[in] Lba The starting logical block index = to write to. + @param[in] Offset Offset into the block at which to= begin writing. + @param[in, out] NumBytes The total size of the buffer. + @param[in] Buffer The pointer to a caller-allocated= buffer that + contains the source for the write= . + + @retval EFI_SUCCESS The write is completed. + @retval EFI_OUT_OF_RESOURCES Invalid Buffer passed. + @retval EFI_BAD_BUFFER_SIZE Buffer size not enough. + @retval EFI_DEVICE_ERROR The device reported an error. +**/ +EFI_STATUS +NorFlashWriteSingleBlock ( + IN NOR_FLASH_INSTANCE *Instance, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + EFI_STATUS Status; + UINT32 Tmp; + UINT32 TmpBuf; + UINT32 WordToWrite; + UINT32 Mask; + BOOLEAN DoErase; + UINTN BytesToWrite; + UINTN CurOffset; + UINTN WordAddr; + UINTN BlockSize; + UINTN BlockAddress; + UINTN PrevBlockAddress; + + if (Buffer =3D=3D NULL) { + DEBUG (( + DEBUG_ERROR, + "NorFlashWriteSingleBlock: ERROR - Buffer is invalid\n" + )); + return EFI_OUT_OF_RESOURCES; + } + + PrevBlockAddress =3D 0; + + DEBUG (( + DEBUG_INFO, + "NorFlashWriteSingleBlock(Parameters: Lba=3D%ld, Offset=3D0x%x, " + "*NumBytes=3D0x%x, Buffer @ 0x%08x)\n", + Lba, + Offset, + *NumBytes, + Buffer + )); + + // Localise the block size to avoid de-referencing pointers all the time + BlockSize =3D Instance->Media.BlockSize; + + // The write must not span block boundaries. + // We need to check each variable individually because adding two large + // values together overflows. + if ((Offset >=3D BlockSize) || + (*NumBytes > BlockSize) || + ((Offset + *NumBytes) > BlockSize)) + { + DEBUG (( + DEBUG_ERROR, + "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: " + "(Offset=3D0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", + Offset, + *NumBytes, + BlockSize + )); + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to write + if (*NumBytes =3D=3D 0) { + DEBUG (( + DEBUG_ERROR, + "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: " + "(Offset=3D0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", + Offset, + *NumBytes, + BlockSize + )); + return EFI_BAD_BUFFER_SIZE; + } + + // Pick 128bytes as a good start for word operations as opposed to erasi= ng the + // block and writing the data regardless if an erase is really needed. + // It looks like most individual NV variable writes are smaller than 128= bytes. + if (*NumBytes <=3D 128) { + // Check to see if we need to erase before programming the data into N= OR. + // If the destination bits are only changing from 1s to 0s we can just= write. + // After a block is erased all bits in the block is set to 1. + // If any byte requires us to erase we just give up and rewrite all of= it. + DoErase =3D FALSE; + BytesToWrite =3D *NumBytes; + CurOffset =3D Offset; + + while (BytesToWrite > 0) { + // Read full word from NOR, splice as required. A word is the smalle= st + // unit we can write. + Status =3D NorFlashRead ( + Instance, + Lba, + CurOffset & ~(0x3), + sizeof (Tmp), + &Tmp + ); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + // Physical address of word in NOR to write. + WordAddr =3D (CurOffset & ~(0x3)) + + GET_NOR_BLOCK_ADDRESS ( + Instance->RegionBaseAddress, + Lba, + BlockSize + ); + + // The word of data that is to be written. + TmpBuf =3D ReadUnaligned32 ( + (UINT32 *)(Buffer + (*NumBytes - BytesToWrite)) + ); + + // First do word aligned chunks. + if ((CurOffset & 0x3) =3D=3D 0) { + if (BytesToWrite >=3D 4) { + // Is the destination still in 'erased' state? + if (~Tmp !=3D 0) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Write this word to NOR + WordToWrite =3D TmpBuf; + CurOffset +=3D sizeof (TmpBuf); + BytesToWrite -=3D sizeof (TmpBuf); + } else { + // BytesToWrite < 4. Do small writes and left-overs + Mask =3D ~((~0) << (BytesToWrite * 8)); + // Mask out the bytes we want. + TmpBuf &=3D Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + CurOffset +=3D BytesToWrite; + BytesToWrite =3D 0; + } + } else { + // Do multiple words, but starting unaligned. + if (BytesToWrite > (4 - (CurOffset & 0x3))) { + Mask =3D ((~0) << ((CurOffset & 0x3) * 8)); + // Mask out the bytes we want. + TmpBuf &=3D Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + BytesToWrite -=3D (4 - (CurOffset & 0x3)); + CurOffset +=3D (4 - (CurOffset & 0x3)); + } else { + // Unaligned and fits in one word. + Mask =3D (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) *= 8); + // Mask out the bytes we want. + TmpBuf =3D (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask; + // Is the destination still in 'erased' state? + if ((Tmp & Mask) !=3D Mask) { + // Check to see if we are only changing bits to zero. + if ((Tmp ^ TmpBuf) & TmpBuf) { + DoErase =3D TRUE; + break; + } + } + + // Merge old and new data. Write merged word to NOR + WordToWrite =3D (Tmp & ~Mask) | TmpBuf; + CurOffset +=3D BytesToWrite; + BytesToWrite =3D 0; + } + } + + BlockAddress =3D GET_NOR_BLOCK_ADDRESS ( + Instance->RegionBaseAddress, + Lba, + BlockSize + ); + if (BlockAddress !=3D PrevBlockAddress) { + Status =3D NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAd= dress); + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + + PrevBlockAddress =3D BlockAddress; + } + + Status =3D NorFlashWriteSingleWord (Instance, WordAddr, WordToWrite)= ; + if (EFI_ERROR (Status)) { + return EFI_DEVICE_ERROR; + } + } + + // Exit if we got here and could write all the data. Otherwise do the + // Erase-Write cycle. + if (!DoErase) { + return EFI_SUCCESS; + } + } + + // Check we did get some memory. Buffer is BlockSize. + if (Instance->ShadowBuffer =3D=3D NULL) { + DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n")); + return EFI_DEVICE_ERROR; + } + + // Read NOR Flash data into shadow buffer + Status =3D NorFlashReadBlocks ( + Instance, + Lba, + BlockSize, + Instance->ShadowBuffer + ); + if (EFI_ERROR (Status)) { + // Return one of the pre-approved error statuses + return EFI_DEVICE_ERROR; + } + + // Put the data at the appropriate location inside the buffer area + CopyMem ( + (VOID *)((UINTN)Instance->ShadowBuffer + Offset), + Buffer, + *NumBytes + ); + + // Write the modified buffer back to the NorFlash + Status =3D NorFlashWriteBlocks ( + Instance, + Lba, + BlockSize, + Instance->ShadowBuffer + ); + if (EFI_ERROR (Status)) { + // Return one of the pre-approved error statuses + return EFI_DEVICE_ERROR; + } + + return EFI_SUCCESS; +} + +/** + Read JEDEC ID of NOR flash device. + + @param[in] Instance NOR flash Instance of variable sto= re region. + @param[out] JedecId JEDEC ID of NOR flash device. + Maximum length of JedecId can be u= pto 6 bytes + @retval EFI_SUCCESS The write is completed. + @retval EFI_UNSUPPORTED JEDEC ID retrievel not implemented= . + @retval EFI_DEVICE_ERROR Failed to fetch JEDEC ID. + @retval EFI_INVALID_PARAMETER Invalid parameters passed. +**/ +EFI_STATUS +NorFlashReadID ( + IN NOR_FLASH_INSTANCE *Instance, + OUT UINT8 *JedecId + ) +{ + UINT32 val; + + if ((Instance =3D=3D NULL) || (JedecId =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + val =3D (SPINOR_OP_RDID << + CDNS_QSPI_FLASH_CMD_CTRL_REG_OPCODE_BIT_POS) | + (CDNS_QSPI_FLASH_CMD_CTRL_REG_READ_ENABLE << + CDNS_QSPI_FLASH_CMD_CTRL_REG_READEN_BIT_POS) | + CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_DATA_BYTES (3); + + if (EFI_ERROR (CdnsQspiExecuteCommand (Instance, val))) { + return EFI_DEVICE_ERROR; + } + + val =3D MmioRead32 ( + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_READ_DAT= A_REG_OFFSET + ); + + // Manu.ID field + JedecId[0] =3D (UINT8)val; + // Type field + JedecId[1] =3D (UINT8)(val >> 8); + // Capacity field + JedecId[2] =3D (UINT8)(val >> 16); + + DEBUG (( + DEBUG_INFO, + "Nor flash detected, Jedec ID, Manu.Id=3D%x Type=3D%x Capacity=3D%x \n= ", + JedecId[0], + JedecId[1], + JedecId[2] + )); + + return EFI_SUCCESS; +} + +/** + Nor Flash Reset + + @param[in] Instance NOR flash instance. + + @retval EFI_SUCCESS Return success on every call. +**/ +EFI_STATUS +NorFlashReset ( + IN NOR_FLASH_INSTANCE *Instance + ) +{ + return EFI_SUCCESS; +} -- 2.25.1 -=3D-=3D-=3D-=3D-=3D-=3D Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118162): https://edk2.groups.io/g/devel/message/118162 Mute This Topic: https://groups.io/mt/105690947/7717249 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [yeoreum.yun@arm.com] -=3D-=3D-=3D-=3D-=3D-=3D IMPORTANT NOTICE: The contents of this email and any attachments are confid= ential and may also be privileged. If you are not the intended recipient, p= lease notify the sender immediately and do not disclose the contents to any= other person, use it for any purpose, or store or copy the information in = any medium. Thank you. -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118196): https://edk2.groups.io/g/devel/message/118196 Mute This Topic: https://groups.io/mt/105690947/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-