blob: 0f21ec8e5375c8ece10776fd903ce79a4efcfaf9 [file] [log] [blame]
Jerome Brunet33e33782018-10-05 17:00:37 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 */
5
6#include <common.h>
Simon Glass9a3b4ce2019-12-28 10:45:01 -07007#include <cpu_func.h>
Simon Glass52559322019-11-14 12:57:46 -07008#include <init.h>
Simon Glass90526e92020-05-10 11:39:56 -06009#include <net.h>
Neil Armstrongd96a7822018-07-27 14:10:00 +020010#include <asm/arch/boot.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass90526e92020-05-10 11:39:56 -060012#include <asm/cache.h>
Simon Glass25a58182020-05-10 11:40:06 -060013#include <asm/ptrace.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020014#include <linux/libfdt.h>
15#include <linux/err.h>
16#include <asm/arch/mem.h>
17#include <asm/arch/sm.h>
18#include <asm/armv8/mmu.h>
19#include <asm/unaligned.h>
20#include <efi_loader.h>
Simon Glass3db71102019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Jerome Brunet33e33782018-10-05 17:00:37 +020022
Neil Armstrong9a34ded2019-05-22 13:30:25 +020023#if CONFIG_IS_ENABLED(FASTBOOT)
24#include <asm/psci.h>
25#include <fastboot.h>
26#endif
27
Jerome Brunet33e33782018-10-05 17:00:37 +020028DECLARE_GLOBAL_DATA_PTR;
29
Jerome Brunetb890acc2018-10-24 14:57:54 +020030__weak int board_init(void)
31{
32 return 0;
33}
34
Jerome Brunet33e33782018-10-05 17:00:37 +020035int dram_init(void)
36{
37 const fdt64_t *val;
38 int offset;
39 int len;
40
41 offset = fdt_path_offset(gd->fdt_blob, "/memory");
42 if (offset < 0)
43 return -EINVAL;
44
45 val = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
46 if (len < sizeof(*val) * 2)
47 return -EINVAL;
48
49 /* Use unaligned access since cache is still disabled */
50 gd->ram_size = get_unaligned_be64(&val[1]);
51
52 return 0;
53}
54
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090055__weak int meson_ft_board_setup(void *blob, struct bd_info *bd)
Jerome Brunetb890acc2018-10-24 14:57:54 +020056{
57 return 0;
58}
59
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +090060int ft_board_setup(void *blob, struct bd_info *bd)
Jerome Brunetb890acc2018-10-24 14:57:54 +020061{
62 meson_init_reserved_memory(blob);
63
64 return meson_ft_board_setup(blob, bd);
65}
66
Jerome Brunet33e33782018-10-05 17:00:37 +020067void meson_board_add_reserved_memory(void *fdt, u64 start, u64 size)
68{
69 int ret;
70
71 ret = fdt_add_mem_rsv(fdt, start, size);
72 if (ret)
73 printf("Could not reserve zone @ 0x%llx\n", start);
74
Michael Walle714497e2020-05-17 12:29:19 +020075 if (IS_ENABLED(CONFIG_EFI_LOADER))
76 efi_add_memory_map(start, size, EFI_RESERVED_MEMORY_TYPE);
Jerome Brunet33e33782018-10-05 17:00:37 +020077}
78
Neil Armstrongdad258f2019-06-12 11:49:07 +020079int meson_generate_serial_ethaddr(void)
80{
81 u8 mac_addr[ARP_HLEN];
82 char serial[SM_SERIAL_SIZE];
83 u32 sid;
84 u16 sid16;
85
86 if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) {
87 sid = crc32(0, (unsigned char *)serial, SM_SERIAL_SIZE);
88 sid16 = crc16_ccitt(0, (unsigned char *)serial, SM_SERIAL_SIZE);
89
90 /* Ensure the NIC specific bytes of the mac are not all 0 */
91 if ((sid & 0xffffff) == 0)
92 sid |= 0x800000;
93
94 /* Non OUI / registered MAC address */
95 mac_addr[0] = ((sid16 >> 8) & 0xfc) | 0x02;
96 mac_addr[1] = (sid16 >> 0) & 0xff;
97 mac_addr[2] = (sid >> 24) & 0xff;
98 mac_addr[3] = (sid >> 16) & 0xff;
99 mac_addr[4] = (sid >> 8) & 0xff;
100 mac_addr[5] = (sid >> 0) & 0xff;
101
102 eth_env_set_enetaddr("ethaddr", mac_addr);
103 } else
104 return -EINVAL;
105
106 return 0;
107}
108
Neil Armstrongd96a7822018-07-27 14:10:00 +0200109static void meson_set_boot_source(void)
110{
111 const char *source;
112
113 switch (meson_get_boot_device()) {
114 case BOOT_DEVICE_EMMC:
115 source = "emmc";
116 break;
117
118 case BOOT_DEVICE_NAND:
119 source = "nand";
120 break;
121
122 case BOOT_DEVICE_SPI:
123 source = "spi";
124 break;
125
126 case BOOT_DEVICE_SD:
127 source = "sd";
128 break;
129
130 case BOOT_DEVICE_USB:
131 source = "usb";
132 break;
133
134 default:
135 source = "unknown";
136 }
137
138 env_set("boot_source", source);
139}
140
141__weak int meson_board_late_init(void)
142{
143 return 0;
144}
145
146int board_late_init(void)
147{
148 meson_set_boot_source();
149
150 return meson_board_late_init();
151}
152
Neil Armstrong9a34ded2019-05-22 13:30:25 +0200153#if CONFIG_IS_ENABLED(FASTBOOT)
154static unsigned int reboot_reason = REBOOT_REASON_NORMAL;
155
156int fastboot_set_reboot_flag()
157{
158 reboot_reason = REBOOT_REASON_BOOTLOADER;
159
160 printf("Using reboot reason: 0x%x\n", reboot_reason);
161
162 return 0;
163}
164
165void reset_cpu(ulong addr)
166{
167 struct pt_regs regs;
168
169 regs.regs[0] = ARM_PSCI_0_2_FN_SYSTEM_RESET;
170 regs.regs[1] = reboot_reason;
171
172 printf("Rebooting with reason: 0x%lx\n", regs.regs[1]);
173
174 smc_call(&regs);
175
176 while (1)
177 ;
178}
179#else
Jerome Brunet33e33782018-10-05 17:00:37 +0200180void reset_cpu(ulong addr)
181{
182 psci_system_reset();
183}
Neil Armstrong9a34ded2019-05-22 13:30:25 +0200184#endif