]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformPeiLib/PlatformPeiLib.c
ArmVirtualizationPkg: move early UART discovery to PlatformPeim
[mirror_edk2.git] / ArmPlatformPkg / ArmVirtualizationPkg / Library / PlatformPeiLib / PlatformPeiLib.c
1 /** @file
2 *
3 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
4 * Copyright (c) 2014, Linaro Limited. All rights reserved.
5 *
6 * This program and the accompanying materials
7 * are licensed and made available under the terms and conditions of the BSD License
8 * which accompanies this distribution. The full text of the license may be found at
9 * http://opensource.org/licenses/bsd-license.php
10 *
11 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 *
14 **/
15
16 #include <PiPei.h>
17
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/PcdLib.h>
22 #include <libfdt.h>
23
24 #include <Guid/EarlyPL011BaseAddress.h>
25
26 EFI_STATUS
27 EFIAPI
28 PlatformPeim (
29 VOID
30 )
31 {
32 VOID *Base;
33 VOID *NewBase;
34 UINTN FdtSize;
35 UINT64 *UartHobData;
36 INT32 Node, Prev;
37 CONST CHAR8 *Compatible;
38 CONST CHAR8 *CompItem;
39 INT32 Len;
40 CONST UINT64 *RegProp;
41 UINT64 UartBase;
42
43
44 Base = (VOID*)(UINTN)FixedPcdGet64 (PcdDeviceTreeInitialBaseAddress);
45 ASSERT (fdt_check_header (Base) == 0);
46
47 FdtSize = fdt_totalsize (Base);
48 NewBase = AllocatePages (EFI_SIZE_TO_PAGES (FdtSize));
49 ASSERT (NewBase != NULL);
50
51 CopyMem (NewBase, Base, FdtSize);
52 PcdSet64 (PcdDeviceTreeBaseAddress, (UINT64)(UINTN)NewBase);
53
54 UartHobData = BuildGuidHob (&gEarlyPL011BaseAddressGuid, sizeof *UartHobData);
55 ASSERT (UartHobData != NULL);
56 *UartHobData = 0;
57
58 //
59 // Look for a UART node
60 //
61 for (Prev = 0;; Prev = Node) {
62 Node = fdt_next_node (Base, Prev, NULL);
63 if (Node < 0) {
64 break;
65 }
66
67 //
68 // Check for UART node
69 //
70 Compatible = fdt_getprop (Base, Node, "compatible", &Len);
71
72 //
73 // Iterate over the NULL-separated items in the compatible string
74 //
75 for (CompItem = Compatible; CompItem != NULL && CompItem < Compatible + Len;
76 CompItem += 1 + AsciiStrLen (CompItem)) {
77
78 if (AsciiStrCmp (CompItem, "arm,pl011") == 0) {
79 RegProp = fdt_getprop (Base, Node, "reg", &Len);
80 ASSERT (Len == 16);
81
82 UartBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
83
84 DEBUG ((EFI_D_INFO, "%a: PL011 UART @ 0x%lx\n", __FUNCTION__, UartBase));
85
86 *UartHobData = UartBase;
87 break;
88 }
89 }
90 }
91
92 BuildFvHob (PcdGet64 (PcdFvBaseAddress), PcdGet32 (PcdFvSize));
93
94 return EFI_SUCCESS;
95 }