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 629C9AC0F85 for ; Thu, 16 May 2024 15:24:36 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=5lgi3OYuiC9wng/dY5dXiEC1BAbtOxaj9QAGFWyeeiY=; c=relaxed/simple; d=groups.io; h=Received-SPF:Authentication-Results-Original:Message-ID:Date:User-Agent:Subject:To:Cc:References:From:In-Reply-To:MIME-Version:NoDisclaimer: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-Type:Content-Language; s=20240206; t=1715873074; v=1; b=gWIWa+vS8teNbtFCrfIq34CamCZOeQAs1bS1R2Tn3lWqCRci43W6UOU7UZm7V07xX9BykL4k mbeW2kLB+cVzopenob8WivJtqfhgYjEMcoAIXdftGvOP69VJoqr1BLrvsnO9pkhYHECOz9qrn+E J/UgTYXhA60RQFETAbLYbnpjEIiwpnP6MeJeXNHxVxxIaaTV1wgpHNkj71eXNDFyG0+EsalQ03S scAYipGWJARRO1dULWLNIUK+SbRc+2Ap723gsX9kET9hmZljBHf0NqKTtmxCAtAMqfRo4n9vAil VilqS7YM0q4dkRSfEgNRwJtOCgC3SqpGItmJrauBJNcMw== X-Received: by 127.0.0.2 with SMTP id fEThYY7687511xrbXZEHO8PU; Thu, 16 May 2024 08:24:34 -0700 X-Received: from EUR04-HE1-obe.outbound.protection.outlook.com (EUR04-HE1-obe.outbound.protection.outlook.com [40.107.7.44]) by mx.groups.io with SMTP id smtpd.web10.16896.1715873073040708647 for ; Thu, 16 May 2024 08:24:33 -0700 X-Received: from DU6P191CA0041.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:53f::21) by PR3PR08MB5641.eurprd08.prod.outlook.com (2603:10a6:102:91::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.28; Thu, 16 May 2024 15:24:26 +0000 X-Received: from DB3PEPF0000885A.eurprd02.prod.outlook.com (2603:10a6:10:53f:cafe::2f) by DU6P191CA0041.outlook.office365.com (2603:10a6:10:53f::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.28 via Frontend Transport; Thu, 16 May 2024 15:24:25 +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 DB3PEPF0000885A.mail.protection.outlook.com (10.167.242.5) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.7587.21 via Frontend Transport; Thu, 16 May 2024 15:24:25 +0000 X-Received: ("Tessian outbound b7675f20d34d:v315"); Thu, 16 May 2024 15:24:25 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 96aeff996c24dd11 X-CR-MTA-TID: 64aa7808 X-Received: from 3484c42ba7a6.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 1011A86E-6E4E-441D-BE4E-482A8ED84EC3.1; Thu, 16 May 2024 15:24:18 +0000 X-Received: from EUR04-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 3484c42ba7a6.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 16 May 2024 15:24:18 +0000 Authentication-Results-Original: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=arm.com; X-Received: from AS8PR08MB6806.eurprd08.prod.outlook.com (2603:10a6:20b:39b::12) by AS8PR08MB9071.eurprd08.prod.outlook.com (2603:10a6:20b:5c1::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7587.27; Thu, 16 May 2024 15:24:15 +0000 X-Received: from AS8PR08MB6806.eurprd08.prod.outlook.com ([fe80::1e13:dc65:224e:219c]) by AS8PR08MB6806.eurprd08.prod.outlook.com ([fe80::1e13:dc65:224e:219c%5]) with mapi id 15.20.7587.028; Thu, 16 May 2024 15:24:15 +0000 Message-ID: <1674c97a-a13e-4ae6-ab79-e174b010140d@arm.com> Date: Thu, 16 May 2024 16:24:11 +0100 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [PATCH RESEND edk2-platforms][PATCH V2 12/14] Platform/ARM: Add CadenceQspiNorFlashDeviceLib for NorFlashDxe To: Sahil Kaushal , devel@edk2.groups.io Cc: Ard Biesheuvel , Leif Lindholm , sahil , "nd@arm.com" References: <20240423055638.1271531-1-Sahil.Kaushal@arm.com> <20240423055638.1271531-13-Sahil.Kaushal@arm.com> From: "Sami Mujawar" In-Reply-To: <20240423055638.1271531-13-Sahil.Kaushal@arm.com> X-ClientProxiedBy: LO3P265CA0017.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:bb::22) To AS8PR08MB6806.eurprd08.prod.outlook.com (2603:10a6:20b:39b::12) MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: AS8PR08MB6806:EE_|AS8PR08MB9071:EE_|DB3PEPF0000885A:EE_|PR3PR08MB5641:EE_ X-MS-Office365-Filtering-Correlation-Id: 67642880-ff3b-486e-c68b-08dc75bc4555 x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0;ARA:13230031|366007|1800799015|376005; X-Microsoft-Antispam-Message-Info-Original: =?us-ascii?Q?Bn2OdQa6semPBDoAAsZzQs9gNKHakmPPE8OcEf17WqY83u2t8opwwY9FFsO8?= =?us-ascii?Q?BRKw3IgXF7+dNkvxLy2NCKEt0Y6QLfdyOB4lau2e3woepuJeTqbnz9r0baW2?= =?us-ascii?Q?0raJp0EAthsc3Bk1JPVxdx3qUMPLNbyDBym5mJGrTlOhGuxgwGssX0YtV8t+?= =?us-ascii?Q?WsgLbs35M8JngKHOBwhugB3E8m8Qxxn6bXc0xtD7ySV280aGY7XgdBAnhqLS?= =?us-ascii?Q?1H1sb1O1rzH3byk+yg+sWfhXBcvTICeO42+/m87XRn/rULKdi9d98kddBIiT?= =?us-ascii?Q?lNDY1GKG+W/4gP3SVSSHLF9M+qPSWp5Amg189CdabNu0hFDRTKC4Ka+dVWlu?= =?us-ascii?Q?qLC0v2FfVTBQ8ZCY+HxOYpKOp0n29rmzUwMNiNVa4/aQs/jye/r0BOcFMcyA?= =?us-ascii?Q?Abqif/Y0DN6yVxaDf2+MvDUhr2vgkRCeeM2lrkhtDbOt9AX49G84Jhroa4CO?= =?us-ascii?Q?KNjpUAo8gJamBE8RMVtb7gHwY+stZNIOSyRohuG5D7N5KJ36iOfQ5Y4Buxjb?= =?us-ascii?Q?rg6kfSc8tXcpgk/efDRRfF7u6ziDNqqSur+Qg5ideP2jpM8ZBxduYzoHNXDw?= =?us-ascii?Q?F7nh0nZ/8evjk+U5uHLZw2rpKKFtMhENb98jJy/SCsqb/V3IH620bE0BO2o4?= =?us-ascii?Q?Sk19OModLSD2xBEtXmuwwlXQCdm+YjOzmT++0VQfv27K11ZQ5Q0a2Ruk6+Ih?= =?us-ascii?Q?SjVkbZ5YriSHsHRmm8v9EUSJrTHn5HmTzI5WYFyRJ3u3EfU6A9WswYK4n4qy?= =?us-ascii?Q?K3UZqnmPPJuALkSvg6vh9O0qesSI5sLJ2fdvu7ocYgyFYet9LK1etgo59rDm?= =?us-ascii?Q?Tz0PVy59F23naipbs/58zsHBxrmfqXCMMd5GGz/m5nFYsjAc/73pPA+vj19/?= =?us-ascii?Q?bDrhbLegJMQUu8VXCgbs84uhBhVaTDchthUHr8EOfOxKJTSr5WkQKcH5TDML?= =?us-ascii?Q?8n1XbmjOzB5AISEZOhoFmXvx7w8TJUDgD92o1QTV5l25wgK8sTL41gWpGG0Y?= =?us-ascii?Q?WwoBXmGtDeGudvIHdt+dkqv/hdcHDip6xik3lZLTZsLC3CFmTrxIlFf4qx/z?= =?us-ascii?Q?6fTncC/hKKaLmPEdjX/IllUgYhPGmzM0j6MGhGX5CHj1+lSztV1AnZY88fwm?= =?us-ascii?Q?Sr4MXE7an0IcZgT44Sh5feAUFCWPbVY3Wtw2KkpSGGWR+BCqC6j1KSfwtFHb?= =?us-ascii?Q?D8xPgljJQgCu3YdCOPP7oMQK9kydrU4K4jgcBnohbdUW84Nx77mI1wf8Cfpq?= =?us-ascii?Q?dp5pByzRw2W5u7VJkHcLGA7YzdUgKpDdksdtZsFNHw=3D=3D?= X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AS8PR08MB6806.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366007)(1800799015)(376005);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB9071 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: DB3PEPF0000885A.eurprd02.prod.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: e3dea92b-4340-411c-57ed-08dc75bc3d48 X-Microsoft-Antispam-Message-Info: =?utf-8?B?dFZxWlVCOEhrZ0RWZ0EySmkyYjg3NHBpcEhJeG9RZG5RREtpSjdTdUFld1Mv?= =?utf-8?B?aklpTXIxZkZybjJyQWY2VzVValRSdCt6REJQZk1HU2xkQ0tHeS9QcFZuNjQy?= =?utf-8?B?d0E5TUxFbVArdzdLcEJYRVdFdVNSTWNJWVpFcnZyUUQybTJxT0NQUlN2RXVj?= =?utf-8?B?MzVmYTdnUTNjcW1wd2o1TmVBU25wT0VIQ1p1TkptSi9hdGhKSEZ5VVZISjhp?= =?utf-8?B?WHJJa3kzNmlJc3JGY2RjelNLZnF2ajFLdXNZSTlmT2NabXJsUUpkakFaMlRZ?= =?utf-8?B?bForNnRXV0tJRElVOGtUZU5lK3BKNVlxanFEdmFKY1A0Z0pZMk5yYWFwUTVo?= =?utf-8?B?NGhMUGlTcU5jbGcyUXRWdEZBeStwc1ZuVTgxazgyUll3dWR3enBhRS9abi9i?= =?utf-8?B?b0NCc3NZN05FM3Nhc0ZGMUpXRy82QTRmY0pOZjIrK0ZRVGhjbkNqakk1SVdB?= =?utf-8?B?Ym9kMnQ2U3UzSWEvaFozd1J1K0hyUWhjVWpMenUrZmRFRkliRHRHV1ZMZXdR?= =?utf-8?B?azRSMk8yRkFmSDdBZSt1YVVNdWdpOXVnemlBQ01jZ0FXTVR3eUVUTjRkLzYv?= =?utf-8?B?SXRUdjNrSjQwMmpBeXlTZ1IvTnZCUERuQ1ZqeVhYa3V6bVVpZUNpdWtaMFZK?= =?utf-8?B?UGF0aElNUXZaRGtjZDA4R2ZMZTRRYW5ZeTI0dWRjZXkyQmtOaHJZV3VZN2R2?= =?utf-8?B?RjBiMDY4VEpQWjhHZnpaako3Q1c3ZTRYbDYzN0tFUHRxN1MxQWtNbG5oQ1lF?= =?utf-8?B?Uktmei9sL2dCMWFHUHNkejBVY2l2bFoxVXp4OU9xNzhIQzZ3UXZVeURTUE1n?= =?utf-8?B?d2dVRVM4SjJXZkI3R1oxNWd1bGVzbEhIQVIyYTh0THBicm9QMjArTDBMK2N4?= =?utf-8?B?MFlsRXlObjg1dUJzRnM2L0NjUU50b2hZM0FmOWtxRVg1Q2VxbysyMzd6cEZv?= =?utf-8?B?VnQxN3NwM3ZTYTZnZWYxaUZzVUlsb3M3MUpVaGFXeEhpdVk4WC9yaDJKVEY1?= =?utf-8?B?L2VSL1YwaWlSMWszMDdSR3Z4RnpCQWlkeDJJZnpJY3AzZEM3OG5yOFZQcUVS?= =?utf-8?B?TXFtcC9jL1o0bGFRR1Rxc1hyV0dXZC9oWXpNcEV4dVcxY1JZWkxkTTdHWjVj?= =?utf-8?B?NnZ0KzZNTzFoTW1BWGNRTkJKaS9BelFXZmZGNjg2cksrbEkyaGVDSG8vbm1s?= =?utf-8?B?TjF2Qm9namZoOUJlTk1xY1lzTVBTK1VDalBUK1RQYW9VQ2ZJRG5jdmJjMURY?= =?utf-8?B?QnBWU2tOL2pwS0grNE55RTJxSUh4QmdIdGNicXA0aWswVHozU2lKRXdCM2pR?= =?utf-8?B?NEpjVmJMTzkrSEFacWY1V1RUQ2F2UERZR1V6bjJBeW5NSkQyT3doZllpY2F2?= =?utf-8?B?RWErcUpLblpXNFdRcitVREhINm5XeFhJNHh4dENWUUlIZWwxbS9CRnJyYytS?= =?utf-8?B?OEc2YjYxVnp4eHdyT2tzZWVRN244MGg3UnpyL3lKRjhFWWxBUlpEL0E1OHZy?= =?utf-8?B?eGpoaVMxSWhFYm53dEFGZEpaYllmcGJ3RmVxcHhwVkY0ZjUvdVRvTEtIQnE5?= =?utf-8?B?TU5yKzlpT3RxcHAwaHZkS21DL0lxaVd3UjhudVJhbWlLSjRJWER6RVYwbGli?= =?utf-8?B?WCs4L1A0cEJ3OE5lNndGQzZhTkE3T29Mcktxei9kNG01RlZrYjcwM2ZIWERV?= =?utf-8?B?YXlhcExKaEpxTjI3T2ZjREtCVENwN2NZeU96blZGT1U1a3cxSWJycllGR0g3?= =?utf-8?B?cHNVc2VWUmtnRWRjUUJEWHZqd3kwdk51Vk0zc1ZSM2l2YUlPeDcza0Vvd2h5?= =?utf-8?B?aGdrS2s5bGdZVFBvS01uVVFCZUFMRExsUzEvWEk2akNsVzErenhseHRrU0R4?= =?utf-8?Q?Wf2XzY1Gw4Ya/?= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 May 2024 15:24:25.7162 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 67642880-ff3b-486e-c68b-08dc75bc4555 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: DB3PEPF0000885A.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3PR08MB5641 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: Thu, 16 May 2024 08:24:34 -0700 Resent-From: sami.mujawar@arm.com Reply-To: devel@edk2.groups.io,sami.mujawar@arm.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: eBkj0LFcdaGvnCPqXZU3oAaIx7686176AA= Content-Type: multipart/alternative; boundary="------------3dlkpuZ01mujS0NeMitBQ96Z" Content-Language: en-GB X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=gWIWa+vS; 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 --------------3dlkpuZ01mujS0NeMitBQ96Z Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Sahil, Thank you for this patch. Please find my response inline marked [SAMI]. With that fixed, Reviewed-by: Sami Mujawar Regards, Sami Mujawar On 23/04/2024 06:56 am, Sahil Kaushal wrote: > 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/CadenceQspiNorFlashDeviceLib.inf | 32 + > Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.h | 44 + > Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c | 1011 ++++++++++++++++++++ > 3 files changed, 1087 insertions(+) > > diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf > new file mode 100644 > index 000000000000..506876b62285 > --- /dev/null > +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.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 = 0x00010005 > > + BASE_NAME = CadenceQspiNorFlashDeviceLib > > + FILE_GUID = ed172366-066b-4998-9b5e-ca7f385a170b > > + MODULE_TYPE = DXE_RUNTIME_DRIVER > > + VERSION_STRING = 1.0 > > + LIBRARY_CLASS = 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/CadenceQspiNorFlashDeviceLib.h b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.h > new file mode 100644 > index 000000000000..d43d27fe5eb4 > --- /dev/null > +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.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_QSPI_FLASH_CMD_CTRL_REG_READBYTE_BIT_POS) > > +#define CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_ADDR_BYTES(x) ((x - 1) << CDNS_QSPI_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 timeout > > + > > +#endif /* CADENCE_QSPI_NOR_FLASH_DEVICE_LIB_H_ */ > > diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c > new file mode 100644 > index 000000000000..8a416e07f15e > --- /dev/null > +++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.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 Register. > > + > > + @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_OFFSET, > > + Val > > + ); > > + // Execute the command > > + MmioWrite32 ( > > + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSET, > > + Val | CDNS_QSPI_FLASH_CMD_CTRL_REG_EXECUTE > > + ); > > + > > + // Wait until command has been executed > > + while ( > > + (MmioRead32 ( > > + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSET > > + ) & CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT) == > > + 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 = 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 = (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 = > > + 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; [SAMI] CdnsQspiExecuteCommand() always wiats in a loop until the CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT bit is set and returns success. Should CdnsQspiExecuteCommand() implement a timeout? Can you check, please? Otherwise these checks do not really make sense. > > + } > > + > > + SRegDone = > > + (MmioRead8 ( > > + Instance->HostRegisterBaseAddress + > > + CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET > > + ) > > + & SPINOR_SR_WIP > > + ) == 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 = EFI_SUCCESS; > > + > > + if (!NorFlashBlockIsLocked (Instance, BlockAddress)) { > > + Status = 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 = (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 region. > > + > > + @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 = 0x0; > > + > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashEraseSingleBlock(BlockAddress=0x%08x)\n", > > + BlockAddress > > + )); > > + > > + if (EFI_ERROR (NorFlashEnableWrite (Instance))) { > > + return EFI_DEVICE_ERROR; > > + } > > + > > + EraseOffset = BlockAddress - Instance->DeviceBaseAddress; > > + > > + MmioWrite32 ( > > + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_ADDR_REG_OFFSET, > > + EraseOffset > > + ); > > + > > + DevConfigVal = (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 region. > > + @param[in] BlockAddress Block address within the variable store region. > > + > > + @retval EFI_SUCCESS The erase and unlock successfully completed. > > +**/ > > +EFI_STATUS > > +NorFlashUnlockAndEraseSingleBlock ( > > + IN NOR_FLASH_INSTANCE *Instance, > > + IN UINTN BlockAddress > > + ) > > +{ > > + EFI_STATUS Status; > > + UINTN Index; > > + EFI_TPL OriginalTPL; > > + > > + NorFlashLock (&OriginalTPL); > > + > > + Index = 0; > > + do { > > + // Unlock the block if we have to > > + Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress); > > + if (EFI_ERROR (Status)) { > > + break; > > + } > > + > > + Status = NorFlashEraseSingleBlock (Instance, BlockAddress); > > + if (EFI_ERROR (Status)) { > > + break; > > + } > > + > > + Index++; > > + } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED)); > > + > > + if (Index == NOR_FLASH_ERASE_RETRY) { > > + DEBUG (( > > + DEBUG_ERROR, > > + "EraseSingleBlock(BlockAddress=0x%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 region. > > + @param[in] WordAddress The address in NOR flash to write given word. > > + @param[in] WriteData The data to write into NOR flash location. > > + > > + @retval EFI_SUCCESS The write is completed. > > + @retval EFI_DEVICE_ERROR The device reported an error. [SAMI] any other error code? > > +**/ > > +EFI_STATUS > > +NorFlashWriteSingleWord ( > > + IN NOR_FLASH_INSTANCE *Instance, > > + IN UINTN WordAddress, > > + IN UINT32 WriteData > > + ) > > +{ > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashWriteSingleWord(WordAddress=0x%08x, WriteData=0x%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 region. > > + @param[in] Lba The logical block address in NOR flash. > > + @param[in] DataBuffer The data to write into NOR flash location. > > + @param[in] BlockSizeInWords The number of bytes to write. > > + > > + @retval EFI_SUCCESS The write is completed. [SAMI] any other error code? I tihink EFI_DEVICE_ERROR can be returned. Can you check the other functions in the file as well, please? > > +**/ > > +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 = EFI_SUCCESS; > > + > > + // Get the physical address of the block > > + BlockAddress = GET_NOR_BLOCK_ADDRESS ( > > + Instance->RegionBaseAddress, > > + Lba, > > + BlockSizeInWords * 4 > > + ); > > + > > + // Start writing from the first address at the start of the block > > + WordAddress = BlockAddress; > > + > > + NorFlashLock (&OriginalTPL); > > + > > + Status = 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; [SAMI] According to the edk2 coding guidelines words with all capital letters are intended for macros. Change EXIT => exit_handler.  I know the P30 device lib has this issue as well. But atleast we should not repeat this again here. [/SAMI] > > + } > > + > > + for (WordIndex = 0; > > + WordIndex < BlockSizeInWords; > > + WordIndex++, DataBuffer++, WordAddress += 4) > > + { > > + Status = 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 = %r.\n", > > + WordAddress, > > + Status > > + )); > > + } > > + > > + return Status; > > +} > > + > > +/** > > + Write a full block. > > + > > + @param[in] Instance NOR flash Instance of variable store 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 buffer 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; [SAMI] The 'p' prefix does not comply with the edk2 coding standard. Can you check, please? > > + EFI_STATUS Status; > > + EFI_LBA CurrentBlock; > > + UINT32 BlockSizeInWords; > > + UINT32 NumBlocks; > > + UINT32 BlockCount; > > + > > + Status = EFI_SUCCESS; > > + // The buffer must be valid > > + if (Buffer == NULL) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + // We must have some bytes to read > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashWriteBlocks: BufferSizeInBytes=0x%x\n", > > + BufferSizeInBytes > > + )); > > + if (BufferSizeInBytes == 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 =0x%x\n", > > + Instance->Media.BlockSize > > + )); > > + if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) { > > + return EFI_BAD_BUFFER_SIZE; > > + } > > + > > + // All blocks must be within the device > > + NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize; > > + > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashWriteBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%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)) == 0); [SAMI] Is this an alignment requirement for the Buffer? what happens in release build? > > + > > + BlockSizeInWords = 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 [SAMI] *ReadBuffer => *WriteBuffer ? > > + pWriteBuffer = (UINT32 *)Buffer; > > + > > + CurrentBlock = Lba; > > + for (BlockCount = 0; > > + BlockCount < NumBlocks; > > + BlockCount++, CurrentBlock++, pWriteBuffer += BlockSizeInWords) > > + { > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashWriteBlocks: Writing block #%d\n", > > + (UINTN)CurrentBlock > > + )); > > + > > + Status = NorFlashWriteFullBlock ( > > + Instance, > > + CurrentBlock, > > + pWriteBuffer, > > + BlockSizeInWords > > + ); > > + > > + if (EFI_ERROR (Status)) { > > + break; > > + } > > + } > > + > > + DEBUG ((DEBUG_INFO, "NorFlashWriteBlocks: Exit Status = %r.\n", Status)); > > + return Status; > > +} > > + > > +/** > > + Read a full block. > > + > > + @param[in] Instance NOR flash Instance of variable store region. > > + @param[in] Lba The starting logical block index to 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=0x%xB BlockSize=0x%xB LastBlock=%ld, " > > + "Lba=%ld.\n", > > + BufferSizeInBytes, > > + Instance->Media.BlockSize, > > + Instance->Media.LastBlock, > > + Lba > > + )); > > + > > + // The buffer must be valid > > + if (Buffer == NULL) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + // Return if we do not have any byte to read > > + if (BufferSizeInBytes == 0) { > > + return EFI_SUCCESS; > > + } > > + > > + // The size of the buffer must be a multiple of the block size > > + if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) { > > + return EFI_BAD_BUFFER_SIZE; > > + } > > + > > + NumBlocks = ((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 = 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 store region. > > + @param[in] Lba The starting logical block index to 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 == NULL) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + // Return if we do not have any byte to read > > + if (BufferSizeInBytes == 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 = 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 store 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 == NULL) { > > + DEBUG (( > > + DEBUG_ERROR, > > + "NorFlashWriteSingleBlock: ERROR - Buffer is invalid\n" > > + )); > > + return EFI_OUT_OF_RESOURCES; > > + } > > + > > + PrevBlockAddress = 0; > > + [SAMI] Validate that NumBytes is not NULL. > > + DEBUG (( > > + DEBUG_INFO, > > + "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, " > > + "*NumBytes=0x%x, Buffer @ 0x%08x)\n", > > + Lba, > > + Offset, > > + *NumBytes, > > + Buffer > > + )); > > + > > + // Localise the block size to avoid de-referencing pointers all the time > > + BlockSize = 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 >= BlockSize) || > > + (*NumBytes > BlockSize) || > > + ((Offset + *NumBytes) > BlockSize)) > > + { > > + DEBUG (( > > + DEBUG_ERROR, > > + "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: " > > + "(Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", > > + Offset, > > + *NumBytes, > > + BlockSize > > + )); > > + return EFI_BAD_BUFFER_SIZE; > > + } > > + > > + // We must have some bytes to write > > + if (*NumBytes == 0) { > > + DEBUG (( > > + DEBUG_ERROR, > > + "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: " > > + "(Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n", > > + Offset, > > + *NumBytes, > > + BlockSize > > + )); > > + return EFI_BAD_BUFFER_SIZE; > > + } > > + > > + // Pick 128bytes as a good start for word operations as opposed to erasing the > > + // block and writing the data regardless if an erase is really needed. > > + // It looks like most individual NV variable writes are smaller than 128bytes. > > + if (*NumBytes <= 128) { > > + // Check to see if we need to erase before programming the data into NOR. > > + // 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 = FALSE; > > + BytesToWrite = *NumBytes; > > + CurOffset = Offset; > > + > > + while (BytesToWrite > 0) { > > + // Read full word from NOR, splice as required. A word is the smallest > > + // unit we can write. > > + Status = 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 = (CurOffset & ~(0x3)) + > > + GET_NOR_BLOCK_ADDRESS ( > > + Instance->RegionBaseAddress, > > + Lba, > > + BlockSize > > + ); > > + > > + // The word of data that is to be written. > > + TmpBuf = ReadUnaligned32 ( > > + (UINT32 *)(Buffer + (*NumBytes - BytesToWrite)) > > + ); > > + > > + // First do word aligned chunks. > > + if ((CurOffset & 0x3) == 0) { > > + if (BytesToWrite >= 4) { > > + // Is the destination still in 'erased' state? > > + if (~Tmp != 0) { > > + // Check to see if we are only changing bits to zero. > > + if ((Tmp ^ TmpBuf) & TmpBuf) { > > + DoErase = TRUE; > > + break; > > + } > > + } > > + > > + // Write this word to NOR > > + WordToWrite = TmpBuf; > > + CurOffset += sizeof (TmpBuf); > > + BytesToWrite -= sizeof (TmpBuf); > > + } else { > > + // BytesToWrite < 4. Do small writes and left-overs > > + Mask = ~((~0) << (BytesToWrite * 8)); > > + // Mask out the bytes we want. > > + TmpBuf &= Mask; > > + // Is the destination still in 'erased' state? > > + if ((Tmp & Mask) != Mask) { > > + // Check to see if we are only changing bits to zero. > > + if ((Tmp ^ TmpBuf) & TmpBuf) { > > + DoErase = TRUE; > > + break; > > + } > > + } > > + > > + // Merge old and new data. Write merged word to NOR > > + WordToWrite = (Tmp & ~Mask) | TmpBuf; > > + CurOffset += BytesToWrite; > > + BytesToWrite = 0; > > + } > > + } else { > > + // Do multiple words, but starting unaligned. > > + if (BytesToWrite > (4 - (CurOffset & 0x3))) { > > + Mask = ((~0) << ((CurOffset & 0x3) * 8)); > > + // Mask out the bytes we want. > > + TmpBuf &= Mask; > > + // Is the destination still in 'erased' state? > > + if ((Tmp & Mask) != Mask) { > > + // Check to see if we are only changing bits to zero. > > + if ((Tmp ^ TmpBuf) & TmpBuf) { > > + DoErase = TRUE; > > + break; > > + } > > + } > > + > > + // Merge old and new data. Write merged word to NOR > > + WordToWrite = (Tmp & ~Mask) | TmpBuf; > > + BytesToWrite -= (4 - (CurOffset & 0x3)); > > + CurOffset += (4 - (CurOffset & 0x3)); > > + } else { > > + // Unaligned and fits in one word. > > + Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8); > > + // Mask out the bytes we want. > > + TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask; > > + // Is the destination still in 'erased' state? > > + if ((Tmp & Mask) != Mask) { > > + // Check to see if we are only changing bits to zero. > > + if ((Tmp ^ TmpBuf) & TmpBuf) { > > + DoErase = TRUE; > > + break; > > + } > > + } > > + > > + // Merge old and new data. Write merged word to NOR > > + WordToWrite = (Tmp & ~Mask) | TmpBuf; > > + CurOffset += BytesToWrite; > > + BytesToWrite = 0; > > + } > > + } > > + > > + BlockAddress = GET_NOR_BLOCK_ADDRESS ( > > + Instance->RegionBaseAddress, > > + Lba, > > + BlockSize > > + ); > > + if (BlockAddress != PrevBlockAddress) { > > + Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress); > > + if (EFI_ERROR (Status)) { > > + return EFI_DEVICE_ERROR; > > + } > > + > > + PrevBlockAddress = BlockAddress; > > + } > > + > > + Status = 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 == NULL) { > > + DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n")); > > + return EFI_DEVICE_ERROR; > > + } > > + > > + // Read NOR Flash data into shadow buffer > > + Status = 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 = 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 store region. > > + @param[out] JedecId JEDEC ID of NOR flash device. > > + Maximum length of JedecId can be upto 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 == NULL) || (JedecId == NULL)) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + val = (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 = MmioRead32 ( > > + Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET > > + ); > > + > > + // Manu.ID field > > + JedecId[0] = (UINT8)val; > > + // Type field > > + JedecId[1] = (UINT8)(val >> 8); > > + // Capacity field > > + JedecId[2] = (UINT8)(val >> 16); > > + > > + DEBUG (( > > + DEBUG_INFO, > > + "Nor flash detected, Jedec ID, Manu.Id=%x Type=%x Capacity=%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; > > +} > -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118968): https://edk2.groups.io/g/devel/message/118968 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] -=-=-=-=-=-=-=-=-=-=-=- --------------3dlkpuZ01mujS0NeMitBQ96Z Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi Sahil,

Thank you for this patch.

Please find my response inline marked [SAMI].

With that fixed,

Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>

Regards,

Sami Mujawar

On 23/04/2024 06:56 am, Sahil Kaushal wrote:
From: sahil <sahil@arm.com>

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 <sahil@arm.com>
---
 Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf |   32 +
 Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.h   |   44 +
 Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c   | 1011 ++++++++++++++++++++
 3 files changed, 1087 insertions(+)

diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf
new file mode 100644
index 000000000000..506876b62285
--- /dev/null
+++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.inf
@@ -0,0 +1,32 @@
+#/** @file

+#

+#  Component description file for CadenceQspiNorFlashDeviceLib Library

+#

+#  Copyright (c) 2024, Arm Limited. All rights reserved.<BR>

+#

+#  SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#**/

+

+[Defines]

+  INF_VERSION                = 0x00010005

+  BASE_NAME                  = CadenceQspiNorFlashDeviceLib

+  FILE_GUID                  = ed172366-066b-4998-9b5e-ca7f385a170b

+  MODULE_TYPE                = DXE_RUNTIME_DRIVER

+  VERSION_STRING             = 1.0

+  LIBRARY_CLASS              = 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/CadenceQspiNorFlashDeviceLib.h b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.h
new file mode 100644
index 000000000000..d43d27fe5eb4
--- /dev/null
+++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.h
@@ -0,0 +1,44 @@
+/** @file

+

+  Copyright (c) 2024, ARM Limited. All rights reserved.<BR>

+

+  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_QSPI_FLASH_CMD_CTRL_REG_READBYTE_BIT_POS)

+#define CDNS_QSPI_FLASH_CMD_CTRL_REG_NUM_ADDR_BYTES(x)  ((x - 1) << CDNS_QSPI_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 timeout

+

+#endif /* CADENCE_QSPI_NOR_FLASH_DEVICE_LIB_H_ */

diff --git a/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c
new file mode 100644
index 000000000000..8a416e07f15e
--- /dev/null
+++ b/Platform/ARM/Library/CadenceQspiNorFlashDeviceLib/CadenceQspiNorFlashDeviceLib.c
@@ -0,0 +1,1011 @@
+/** @file

+

+  Copyright (c) 2024 ARM Limited. All rights reserved.<BR>

+

+  SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <Library/BaseMemoryLib.h>

+#include <Library/DebugLib.h>

+#include <Library/IoLib.h>

+#include <Library/MemoryAllocationLib.h>

+#include <Library/NorFlashDeviceLib.h>

+#include <Library/TimerLib.h>

+#include <Library/UefiLib.h>

+

+#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 Register.

+

+  @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_OFFSET,

+    Val

+    );

+  // Execute the command

+  MmioWrite32 (

+    Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSET,

+    Val | CDNS_QSPI_FLASH_CMD_CTRL_REG_EXECUTE

+    );

+

+  // Wait until command has been executed

+  while (

+         (MmioRead32 (

+            Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_CTRL_REG_OFFSET

+            ) & CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT) ==

+         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 = 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 = (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 =

+    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;
[SAMI] CdnsQspiExecuteCommand() always wiats in a loop until the CDNS_QSPI_FLASH_CMD_CTRL_REG_STATUS_BIT bit is set and returns success. Should CdnsQspiExecuteCommand() implement a timeout? Can you check, please? Otherwise these checks do not really make sense.

+    }

+

+    SRegDone =

+      (MmioRead8 (

+         Instance->HostRegisterBaseAddress +

+         CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET

+         )

+       & SPINOR_SR_WIP

+      ) == 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 = EFI_SUCCESS;

+

+  if (!NorFlashBlockIsLocked (Instance, BlockAddress)) {

+    Status = 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 = (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 region.

+

+  @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 = 0x0;

+

+  DEBUG ((

+    DEBUG_INFO,

+    "NorFlashEraseSingleBlock(BlockAddress=0x%08x)\n",

+    BlockAddress

+    ));

+

+  if (EFI_ERROR (NorFlashEnableWrite (Instance))) {

+    return EFI_DEVICE_ERROR;

+  }

+

+  EraseOffset = BlockAddress - Instance->DeviceBaseAddress;

+

+  MmioWrite32 (

+    Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_ADDR_REG_OFFSET,

+    EraseOffset

+    );

+

+  DevConfigVal = (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 region.

+  @param[in]     BlockAddress   Block address within the variable store region.

+

+  @retval        EFI_SUCCESS    The erase and unlock successfully completed.

+**/

+EFI_STATUS

+NorFlashUnlockAndEraseSingleBlock (

+  IN NOR_FLASH_INSTANCE  *Instance,

+  IN UINTN               BlockAddress

+  )

+{

+  EFI_STATUS  Status;

+  UINTN       Index;

+  EFI_TPL     OriginalTPL;

+

+  NorFlashLock (&OriginalTPL);

+

+  Index = 0;

+  do {

+    // Unlock the block if we have to

+    Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);

+    if (EFI_ERROR (Status)) {

+      break;

+    }

+

+    Status = NorFlashEraseSingleBlock (Instance, BlockAddress);

+    if (EFI_ERROR (Status)) {

+      break;

+    }

+

+    Index++;

+  } while ((Index < NOR_FLASH_ERASE_RETRY) && (Status == EFI_WRITE_PROTECTED));

+

+  if (Index == NOR_FLASH_ERASE_RETRY) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "EraseSingleBlock(BlockAddress=0x%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 region.

+  @param[in]    WordAddress       The address in NOR flash to write given word.

+  @param[in]    WriteData         The data to write into NOR flash location.

+

+  @retval       EFI_SUCCESS       The write is completed.

+  @retval       EFI_DEVICE_ERROR  The device reported an error.
[SAMI] any other error code?

+**/

+EFI_STATUS

+NorFlashWriteSingleWord (

+  IN NOR_FLASH_INSTANCE  *Instance,

+  IN UINTN               WordAddress,

+  IN UINT32              WriteData

+  )

+{

+  DEBUG ((

+    DEBUG_INFO,

+    "NorFlashWriteSingleWord(WordAddress=0x%08x, WriteData=0x%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 region.

+  @param[in]    Lba                The logical block address in NOR flash.

+  @param[in]    DataBuffer         The data to write into NOR flash location.

+  @param[in]    BlockSizeInWords   The number of bytes to write.

+

+  @retval       EFI_SUCCESS        The write is completed.
[SAMI] any other error code? I tihink EFI_DEVICE_ERROR can be returned. Can you check the other functions in the file as well, please?

+**/

+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 = EFI_SUCCESS;

+

+  // Get the physical address of the block

+  BlockAddress = GET_NOR_BLOCK_ADDRESS (

+                   Instance->RegionBaseAddress,

+                   Lba,

+                   BlockSizeInWords * 4

+                   );

+

+  // Start writing from the first address at the start of the block

+  WordAddress = BlockAddress;

+

+  NorFlashLock (&OriginalTPL);

+

+  Status = 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;

[SAMI] According to the edk2 coding guidelines words with all capital letters are intended for macros. Change EXIT => exit_handler.

 I know the P30 device lib has this issue as well. But atleast we should not repeat this again here.

[/SAMI]


+  }

+

+  for (WordIndex = 0;

+       WordIndex < BlockSizeInWords;

+       WordIndex++, DataBuffer++, WordAddress += 4)

+  {

+    Status = 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 = %r.\n",

+      WordAddress,

+      Status

+      ));

+  }

+

+  return Status;

+}

+

+/**

+  Write a full  block.

+

+  @param[in]    Instance               NOR flash Instance of variable store 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 buffer 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;
[SAMI] The 'p' prefix does not comply with the edk2 coding standard. Can you check, please?

+  EFI_STATUS  Status;

+  EFI_LBA     CurrentBlock;

+  UINT32      BlockSizeInWords;

+  UINT32      NumBlocks;

+  UINT32      BlockCount;

+

+  Status = EFI_SUCCESS;

+  // The buffer must be valid

+  if (Buffer == NULL) {

+    return EFI_INVALID_PARAMETER;

+  }

+

+  // We must have some bytes to read

+  DEBUG ((

+    DEBUG_INFO,

+    "NorFlashWriteBlocks: BufferSizeInBytes=0x%x\n",

+    BufferSizeInBytes

+    ));

+  if (BufferSizeInBytes == 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 =0x%x\n",

+    Instance->Media.BlockSize

+    ));

+  if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  // All blocks must be within the device

+  NumBlocks = ((UINT32)BufferSizeInBytes) / Instance->Media.BlockSize;

+

+  DEBUG ((

+    DEBUG_INFO,

+    "NorFlashWriteBlocks: NumBlocks=%d, LastBlock=%ld, Lba=%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)) == 0);
[SAMI] Is this an alignment requirement for the Buffer? what happens in release build?

+

+  BlockSizeInWords = 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
[SAMI] *ReadBuffer => *WriteBuffer ?

+  pWriteBuffer = (UINT32 *)Buffer;

+

+  CurrentBlock = Lba;

+  for (BlockCount = 0;

+       BlockCount < NumBlocks;

+       BlockCount++, CurrentBlock++, pWriteBuffer += BlockSizeInWords)

+  {

+    DEBUG ((

+      DEBUG_INFO,

+      "NorFlashWriteBlocks: Writing block #%d\n",

+      (UINTN)CurrentBlock

+      ));

+

+    Status = NorFlashWriteFullBlock (

+               Instance,

+               CurrentBlock,

+               pWriteBuffer,

+               BlockSizeInWords

+               );

+

+    if (EFI_ERROR (Status)) {

+      break;

+    }

+  }

+

+  DEBUG ((DEBUG_INFO, "NorFlashWriteBlocks: Exit Status = %r.\n", Status));

+  return Status;

+}

+

+/**

+  Read a full  block.

+

+  @param[in]     Instance               NOR flash Instance of variable store region.

+  @param[in]     Lba                    The starting logical block index to 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=0x%xB BlockSize=0x%xB LastBlock=%ld, "

+    "Lba=%ld.\n",

+    BufferSizeInBytes,

+    Instance->Media.BlockSize,

+    Instance->Media.LastBlock,

+    Lba

+    ));

+

+  // The buffer must be valid

+  if (Buffer == NULL) {

+    return EFI_INVALID_PARAMETER;

+  }

+

+  // Return if we do not have any byte to read

+  if (BufferSizeInBytes == 0) {

+    return EFI_SUCCESS;

+  }

+

+  // The size of the buffer must be a multiple of the block size

+  if ((BufferSizeInBytes % Instance->Media.BlockSize) != 0) {

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  NumBlocks = ((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 = 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 store region.

+  @param[in]     Lba                    The starting logical block index to 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 == NULL) {

+    return EFI_INVALID_PARAMETER;

+  }

+

+  // Return if we do not have any byte to read

+  if (BufferSizeInBytes == 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 = 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 store 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 == NULL) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "NorFlashWriteSingleBlock: ERROR - Buffer is invalid\n"

+      ));

+    return EFI_OUT_OF_RESOURCES;

+  }

+

+  PrevBlockAddress = 0;

+
[SAMI] Validate that NumBytes is not NULL.

+  DEBUG ((

+    DEBUG_INFO,

+    "NorFlashWriteSingleBlock(Parameters: Lba=%ld, Offset=0x%x, "

+    "*NumBytes=0x%x, Buffer @ 0x%08x)\n",

+    Lba,

+    Offset,

+    *NumBytes,

+    Buffer

+    ));

+

+  // Localise the block size to avoid de-referencing pointers all the time

+  BlockSize = 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               >= BlockSize) ||

+      (*NumBytes            >  BlockSize) ||

+      ((Offset + *NumBytes) >  BlockSize))

+  {

+    DEBUG ((

+      DEBUG_ERROR,

+      "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: "

+      "(Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n",

+      Offset,

+      *NumBytes,

+      BlockSize

+      ));

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  // We must have some bytes to write

+  if (*NumBytes == 0) {

+    DEBUG ((

+      DEBUG_ERROR,

+      "NorFlashWriteSingleBlock: ERROR - EFI_BAD_BUFFER_SIZE: "

+      "(Offset=0x%x + NumBytes=0x%x) > BlockSize=0x%x\n",

+      Offset,

+      *NumBytes,

+      BlockSize

+      ));

+    return EFI_BAD_BUFFER_SIZE;

+  }

+

+  // Pick 128bytes as a good start for word operations as opposed to erasing the

+  // block and writing the data regardless if an erase is really needed.

+  // It looks like most individual NV variable writes are smaller than 128bytes.

+  if (*NumBytes <= 128) {

+    // Check to see if we need to erase before programming the data into NOR.

+    // 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      = FALSE;

+    BytesToWrite = *NumBytes;

+    CurOffset    = Offset;

+

+    while (BytesToWrite > 0) {

+      // Read full word from NOR, splice as required. A word is the smallest

+      // unit we can write.

+      Status = 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 = (CurOffset & ~(0x3)) +

+                 GET_NOR_BLOCK_ADDRESS (

+                   Instance->RegionBaseAddress,

+                   Lba,

+                   BlockSize

+                   );

+

+      // The word of data that is to be written.

+      TmpBuf = ReadUnaligned32 (

+                 (UINT32 *)(Buffer + (*NumBytes - BytesToWrite))

+                 );

+

+      // First do word aligned chunks.

+      if ((CurOffset & 0x3) == 0) {

+        if (BytesToWrite >= 4) {

+          // Is the destination still in 'erased' state?

+          if (~Tmp != 0) {

+            // Check to see if we are only changing bits to zero.

+            if ((Tmp ^ TmpBuf) & TmpBuf) {

+              DoErase = TRUE;

+              break;

+            }

+          }

+

+          // Write this word to NOR

+          WordToWrite   = TmpBuf;

+          CurOffset    += sizeof (TmpBuf);

+          BytesToWrite -= sizeof (TmpBuf);

+        } else {

+          // BytesToWrite < 4. Do small writes and left-overs

+          Mask = ~((~0) << (BytesToWrite * 8));

+          // Mask out the bytes we want.

+          TmpBuf &= Mask;

+          // Is the destination still in 'erased' state?

+          if ((Tmp & Mask) != Mask) {

+            // Check to see if we are only changing bits to zero.

+            if ((Tmp ^ TmpBuf) & TmpBuf) {

+              DoErase = TRUE;

+              break;

+            }

+          }

+

+          // Merge old and new data. Write merged word to NOR

+          WordToWrite  = (Tmp & ~Mask) | TmpBuf;

+          CurOffset   += BytesToWrite;

+          BytesToWrite = 0;

+        }

+      } else {

+        // Do multiple words, but starting unaligned.

+        if (BytesToWrite > (4 - (CurOffset & 0x3))) {

+          Mask = ((~0) << ((CurOffset & 0x3) * 8));

+          // Mask out the bytes we want.

+          TmpBuf &= Mask;

+          // Is the destination still in 'erased' state?

+          if ((Tmp & Mask) != Mask) {

+            // Check to see if we are only changing bits to zero.

+            if ((Tmp ^ TmpBuf) & TmpBuf) {

+              DoErase = TRUE;

+              break;

+            }

+          }

+

+          // Merge old and new data. Write merged word to NOR

+          WordToWrite   = (Tmp & ~Mask) | TmpBuf;

+          BytesToWrite -= (4 - (CurOffset & 0x3));

+          CurOffset    += (4 - (CurOffset & 0x3));

+        } else {

+          // Unaligned and fits in one word.

+          Mask = (~((~0) << (BytesToWrite * 8))) << ((CurOffset & 0x3) * 8);

+          // Mask out the bytes we want.

+          TmpBuf = (TmpBuf << ((CurOffset & 0x3) * 8)) & Mask;

+          // Is the destination still in 'erased' state?

+          if ((Tmp & Mask) != Mask) {

+            // Check to see if we are only changing bits to zero.

+            if ((Tmp ^ TmpBuf) & TmpBuf) {

+              DoErase = TRUE;

+              break;

+            }

+          }

+

+          // Merge old and new data. Write merged word to NOR

+          WordToWrite  = (Tmp & ~Mask) | TmpBuf;

+          CurOffset   += BytesToWrite;

+          BytesToWrite = 0;

+        }

+      }

+

+      BlockAddress = GET_NOR_BLOCK_ADDRESS (

+                       Instance->RegionBaseAddress,

+                       Lba,

+                       BlockSize

+                       );

+      if (BlockAddress != PrevBlockAddress) {

+        Status = NorFlashUnlockSingleBlockIfNecessary (Instance, BlockAddress);

+        if (EFI_ERROR (Status)) {

+          return EFI_DEVICE_ERROR;

+        }

+

+        PrevBlockAddress = BlockAddress;

+      }

+

+      Status = 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 == NULL) {

+    DEBUG ((DEBUG_ERROR, "FvbWrite: ERROR - Buffer not ready\n"));

+    return EFI_DEVICE_ERROR;

+  }

+

+  // Read NOR Flash data into shadow buffer

+  Status = 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 = 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 store region.

+  @param[out]    JedecId                JEDEC ID of NOR flash device.

+                                        Maximum length of JedecId can be upto 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 == NULL) || (JedecId == NULL)) {

+    return EFI_INVALID_PARAMETER;

+  }

+

+  val = (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 = MmioRead32 (

+          Instance->HostRegisterBaseAddress + CDNS_QSPI_FLASH_CMD_READ_DATA_REG_OFFSET

+          );

+

+  // Manu.ID field

+  JedecId[0] = (UINT8)val;

+  // Type field

+  JedecId[1] = (UINT8)(val >> 8);

+  // Capacity field

+  JedecId[2] = (UINT8)(val >> 16);

+

+  DEBUG ((

+    DEBUG_INFO,

+    "Nor flash detected, Jedec ID, Manu.Id=%x Type=%x Capacity=%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;

+}

_._,_._,_

Groups.io Links:

You receive all messages sent to this group.

View/Reply Online (#118968) | | Mute This Topic | New Topic
Your Subscription | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--------------3dlkpuZ01mujS0NeMitBQ96Z--