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