1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4  */
5 
6 #include <common.h>
7 #include <asm/arch/boot.h>
8 #include <linux/libfdt.h>
9 #include <linux/err.h>
10 #include <asm/arch/mem.h>
11 #include <asm/arch/sm.h>
12 #include <asm/armv8/mmu.h>
13 #include <asm/unaligned.h>
14 #include <efi_loader.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 __weak int board_init(void)
19 {
20 	return 0;
21 }
22 
23 int dram_init(void)
24 {
25 	const fdt64_t *val;
26 	int offset;
27 	int len;
28 
29 	offset = fdt_path_offset(gd->fdt_blob, "/memory");
30 	if (offset < 0)
31 		return -EINVAL;
32 
33 	val = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
34 	if (len < sizeof(*val) * 2)
35 		return -EINVAL;
36 
37 	/* Use unaligned access since cache is still disabled */
38 	gd->ram_size = get_unaligned_be64(&val[1]);
39 
40 	return 0;
41 }
42 
43 __weak int meson_ft_board_setup(void *blob, bd_t *bd)
44 {
45 	return 0;
46 }
47 
48 int ft_board_setup(void *blob, bd_t *bd)
49 {
50 	meson_init_reserved_memory(blob);
51 
52 	return meson_ft_board_setup(blob, bd);
53 }
54 
55 void meson_board_add_reserved_memory(void *fdt, u64 start, u64 size)
56 {
57 	int ret;
58 
59 	ret = fdt_add_mem_rsv(fdt, start, size);
60 	if (ret)
61 		printf("Could not reserve zone @ 0x%llx\n", start);
62 
63 	if (IS_ENABLED(CONFIG_EFI_LOADER)) {
64 		efi_add_memory_map(start,
65 				   ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT,
66 				   EFI_RESERVED_MEMORY_TYPE, false);
67 	}
68 }
69 
70 static void meson_set_boot_source(void)
71 {
72 	const char *source;
73 
74 	switch (meson_get_boot_device()) {
75 	case BOOT_DEVICE_EMMC:
76 		source = "emmc";
77 		break;
78 
79 	case BOOT_DEVICE_NAND:
80 		source = "nand";
81 		break;
82 
83 	case BOOT_DEVICE_SPI:
84 		source = "spi";
85 		break;
86 
87 	case BOOT_DEVICE_SD:
88 		source = "sd";
89 		break;
90 
91 	case BOOT_DEVICE_USB:
92 		source = "usb";
93 		break;
94 
95 	default:
96 		source = "unknown";
97 	}
98 
99 	env_set("boot_source", source);
100 }
101 
102 __weak int meson_board_late_init(void)
103 {
104 	return 0;
105 }
106 
107 int board_late_init(void)
108 {
109 	meson_set_boot_source();
110 
111 	return meson_board_late_init();
112 }
113 
114 void reset_cpu(ulong addr)
115 {
116 	psci_system_reset();
117 }
118