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