env: Move env_set() to env.h
[oweals/u-boot.git] / arch / arm / mach-meson / board-common.c
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 <env.h>
9 #include <linux/libfdt.h>
10 #include <linux/err.h>
11 #include <environment.h>
12 #include <asm/arch/mem.h>
13 #include <asm/arch/sm.h>
14 #include <asm/armv8/mmu.h>
15 #include <asm/unaligned.h>
16 #include <efi_loader.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 __weak int board_init(void)
21 {
22         return 0;
23 }
24
25 int dram_init(void)
26 {
27         const fdt64_t *val;
28         int offset;
29         int len;
30
31         offset = fdt_path_offset(gd->fdt_blob, "/memory");
32         if (offset < 0)
33                 return -EINVAL;
34
35         val = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
36         if (len < sizeof(*val) * 2)
37                 return -EINVAL;
38
39         /* Use unaligned access since cache is still disabled */
40         gd->ram_size = get_unaligned_be64(&val[1]);
41
42         return 0;
43 }
44
45 __weak int meson_ft_board_setup(void *blob, bd_t *bd)
46 {
47         return 0;
48 }
49
50 int ft_board_setup(void *blob, bd_t *bd)
51 {
52         meson_init_reserved_memory(blob);
53
54         return meson_ft_board_setup(blob, bd);
55 }
56
57 void meson_board_add_reserved_memory(void *fdt, u64 start, u64 size)
58 {
59         int ret;
60
61         ret = fdt_add_mem_rsv(fdt, start, size);
62         if (ret)
63                 printf("Could not reserve zone @ 0x%llx\n", start);
64
65         if (IS_ENABLED(CONFIG_EFI_LOADER)) {
66                 efi_add_memory_map(start,
67                                    ALIGN(size, EFI_PAGE_SIZE) >> EFI_PAGE_SHIFT,
68                                    EFI_RESERVED_MEMORY_TYPE, false);
69         }
70 }
71
72 int meson_generate_serial_ethaddr(void)
73 {
74         u8 mac_addr[ARP_HLEN];
75         char serial[SM_SERIAL_SIZE];
76         u32 sid;
77         u16 sid16;
78
79         if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) {
80                 sid = crc32(0, (unsigned char *)serial, SM_SERIAL_SIZE);
81                 sid16 = crc16_ccitt(0, (unsigned char *)serial, SM_SERIAL_SIZE);
82
83                 /* Ensure the NIC specific bytes of the mac are not all 0 */
84                 if ((sid & 0xffffff) == 0)
85                         sid |= 0x800000;
86
87                 /* Non OUI / registered MAC address */
88                 mac_addr[0] = ((sid16 >> 8) & 0xfc) | 0x02;
89                 mac_addr[1] = (sid16 >>  0) & 0xff;
90                 mac_addr[2] = (sid >> 24) & 0xff;
91                 mac_addr[3] = (sid >> 16) & 0xff;
92                 mac_addr[4] = (sid >>  8) & 0xff;
93                 mac_addr[5] = (sid >>  0) & 0xff;
94
95                 eth_env_set_enetaddr("ethaddr", mac_addr);
96         } else
97                 return -EINVAL;
98
99         return 0;
100 }
101
102 static void meson_set_boot_source(void)
103 {
104         const char *source;
105
106         switch (meson_get_boot_device()) {
107         case BOOT_DEVICE_EMMC:
108                 source = "emmc";
109                 break;
110
111         case BOOT_DEVICE_NAND:
112                 source = "nand";
113                 break;
114
115         case BOOT_DEVICE_SPI:
116                 source = "spi";
117                 break;
118
119         case BOOT_DEVICE_SD:
120                 source = "sd";
121                 break;
122
123         case BOOT_DEVICE_USB:
124                 source = "usb";
125                 break;
126
127         default:
128                 source = "unknown";
129         }
130
131         env_set("boot_source", source);
132 }
133
134 __weak int meson_board_late_init(void)
135 {
136         return 0;
137 }
138
139 int board_late_init(void)
140 {
141         meson_set_boot_source();
142
143         return meson_board_late_init();
144 }
145
146 void reset_cpu(ulong addr)
147 {
148         psci_system_reset();
149 }