3 * Copyright (c) 2011-2014, ARM Limited. All rights reserved.
4 * Copyright (c) 2014, Linaro Limited. All rights reserved.
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
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.
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/HobLib.h>
21 #include <Library/PcdLib.h>
24 #include <Guid/EarlyPL011BaseAddress.h>
25 #include <Guid/FdtHob.h>
40 CONST CHAR8
*Compatible
;
41 CONST CHAR8
*CompItem
;
42 CONST CHAR8
*NodeStatus
;
45 CONST UINT64
*RegProp
;
49 Base
= (VOID
*)(UINTN
)PcdGet64 (PcdDeviceTreeInitialBaseAddress
);
50 ASSERT (Base
!= NULL
);
51 ASSERT (fdt_check_header (Base
) == 0);
53 FdtSize
= fdt_totalsize (Base
) + PcdGet32 (PcdDeviceTreeAllocationPadding
);
54 FdtPages
= EFI_SIZE_TO_PAGES (FdtSize
);
55 NewBase
= AllocatePages (FdtPages
);
56 ASSERT (NewBase
!= NULL
);
57 fdt_open_into (Base
, NewBase
, EFI_PAGES_TO_SIZE (FdtPages
));
59 FdtHobData
= BuildGuidHob (&gFdtHobGuid
, sizeof *FdtHobData
);
60 ASSERT (FdtHobData
!= NULL
);
61 *FdtHobData
= (UINTN
)NewBase
;
63 UartHobData
= BuildGuidHob (&gEarlyPL011BaseAddressGuid
, sizeof *UartHobData
);
64 ASSERT (UartHobData
!= NULL
);
68 // Look for a UART node
70 for (Prev
= 0;; Prev
= Node
) {
71 Node
= fdt_next_node (Base
, Prev
, NULL
);
77 // Check for UART node
79 Compatible
= fdt_getprop (Base
, Node
, "compatible", &Len
);
82 // Iterate over the NULL-separated items in the compatible string
84 for (CompItem
= Compatible
; CompItem
!= NULL
&& CompItem
< Compatible
+ Len
;
85 CompItem
+= 1 + AsciiStrLen (CompItem
)) {
87 if (AsciiStrCmp (CompItem
, "arm,pl011") == 0) {
88 NodeStatus
= fdt_getprop (Base
, Node
, "status", &StatusLen
);
89 if (NodeStatus
!= NULL
&& AsciiStrCmp (NodeStatus
, "okay") != 0) {
93 RegProp
= fdt_getprop (Base
, Node
, "reg", &Len
);
96 UartBase
= fdt64_to_cpu (ReadUnaligned64 (RegProp
));
98 DEBUG ((EFI_D_INFO
, "%a: PL011 UART @ 0x%lx\n", __FUNCTION__
, UartBase
));
100 *UartHobData
= UartBase
;
106 BuildFvHob (PcdGet64 (PcdFvBaseAddress
), PcdGet32 (PcdFvSize
));