]> git.proxmox.com Git - mirror_edk2.git/blame - ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
ArmVirtPkg/NorFlashQemuLib: discover NOR flash banks dynamically
[mirror_edk2.git] / ArmVirtPkg / Library / NorFlashQemuLib / NorFlashQemuLib.c
CommitLineData
86d96aef
AB
1/** @file\r
2\r
72e514c9 3 Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.<BR>\r
86d96aef
AB
4\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13 **/\r
14\r
72e514c9
AB
15#include <Library/BaseLib.h>\r
16#include <Library/DebugLib.h>\r
86d96aef 17#include <Library/NorFlashPlatformLib.h>\r
72e514c9
AB
18#include <Library/UefiBootServicesTableLib.h>\r
19\r
20#include <Protocol/FdtClient.h>\r
86d96aef 21\r
f311e5d8 22#define QEMU_NOR_BLOCK_SIZE SIZE_256KB\r
72e514c9
AB
23\r
24#define MAX_FLASH_BANKS 4\r
f311e5d8 25\r
86d96aef
AB
26EFI_STATUS\r
27NorFlashPlatformInitialization (\r
28 VOID\r
29 )\r
30{\r
31 return EFI_SUCCESS;\r
32}\r
33\r
72e514c9 34NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_BANKS];\r
86d96aef
AB
35\r
36EFI_STATUS\r
37NorFlashPlatformGetDevices (\r
38 OUT NOR_FLASH_DESCRIPTION **NorFlashDescriptions,\r
39 OUT UINT32 *Count\r
40 )\r
41{\r
72e514c9
AB
42 FDT_CLIENT_PROTOCOL *FdtClient;\r
43 INT32 Node;\r
44 EFI_STATUS Status;\r
45 EFI_STATUS FindNodeStatus;\r
46 CONST UINT32 *Reg;\r
47 UINT32 PropSize;\r
48 UINT32 Num;\r
49 UINT64 Base;\r
50 UINT64 Size;\r
51\r
52 Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,\r
53 (VOID **)&FdtClient);\r
54 ASSERT_EFI_ERROR (Status);\r
55\r
56 Num = 0;\r
57 for (FindNodeStatus = FdtClient->FindCompatibleNode (FdtClient,\r
58 "cfi-flash", &Node);\r
59 !EFI_ERROR (FindNodeStatus) && Num < MAX_FLASH_BANKS;\r
60 FindNodeStatus = FdtClient->FindNextCompatibleNode (FdtClient,\r
61 "cfi-flash", Node, &Node)) {\r
62\r
63 Status = FdtClient->GetNodeProperty (FdtClient, Node, "reg",\r
64 (CONST VOID **)&Reg, &PropSize);\r
65 if (EFI_ERROR (Status)) {\r
66 DEBUG ((DEBUG_ERROR, "%a: GetNodeProperty () failed (Status == %r)\n",\r
67 __FUNCTION__, Status));\r
68 continue;\r
69 }\r
70\r
71 ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);\r
72\r
73 while (PropSize >= (4 * sizeof (UINT32)) && Num < MAX_FLASH_BANKS) {\r
74 Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));\r
75 Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));\r
76 Reg += 4;\r
77\r
78 mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;\r
79 mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;\r
80 mNorFlashDevices[Num].Size = (UINTN)Size;\r
81 mNorFlashDevices[Num].BlockSize = QEMU_NOR_BLOCK_SIZE;\r
82 Num++;\r
83\r
84 PropSize -= 4 * sizeof (UINT32);\r
85 }\r
86 }\r
87\r
86d96aef 88 *NorFlashDescriptions = mNorFlashDevices;\r
72e514c9
AB
89 *Count = Num;\r
90\r
86d96aef
AB
91 return EFI_SUCCESS;\r
92}\r