Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | afb0215 | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 8 | #include <fastboot.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <net.h> |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 11 | #include <asm/arch/boot.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 13 | #include <asm/cache.h> |
Simon Glass | 6b9f010 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 14 | #include <asm/ptrace.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <asm/arch/mem.h> |
| 18 | #include <asm/arch/sm.h> |
| 19 | #include <asm/armv8/mmu.h> |
| 20 | #include <asm/unaligned.h> |
| 21 | #include <efi_loader.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 22 | #include <u-boot/crc.h> |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 23 | |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 24 | #if CONFIG_IS_ENABLED(FASTBOOT) |
| 25 | #include <asm/psci.h> |
| 26 | #include <fastboot.h> |
| 27 | #endif |
| 28 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Jerome Brunet | 5c6fa8e | 2018-10-24 14:57:54 +0200 | [diff] [blame] | 31 | __weak int board_init(void) |
| 32 | { |
| 33 | return 0; |
| 34 | } |
| 35 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 36 | int dram_init(void) |
| 37 | { |
| 38 | const fdt64_t *val; |
| 39 | int offset; |
| 40 | int len; |
| 41 | |
| 42 | offset = fdt_path_offset(gd->fdt_blob, "/memory"); |
| 43 | if (offset < 0) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | val = fdt_getprop(gd->fdt_blob, offset, "reg", &len); |
| 47 | if (len < sizeof(*val) * 2) |
| 48 | return -EINVAL; |
| 49 | |
| 50 | /* Use unaligned access since cache is still disabled */ |
| 51 | gd->ram_size = get_unaligned_be64(&val[1]); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 56 | __weak int meson_ft_board_setup(void *blob, struct bd_info *bd) |
Jerome Brunet | 5c6fa8e | 2018-10-24 14:57:54 +0200 | [diff] [blame] | 57 | { |
| 58 | return 0; |
| 59 | } |
| 60 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 61 | int ft_board_setup(void *blob, struct bd_info *bd) |
Jerome Brunet | 5c6fa8e | 2018-10-24 14:57:54 +0200 | [diff] [blame] | 62 | { |
| 63 | meson_init_reserved_memory(blob); |
| 64 | |
| 65 | return meson_ft_board_setup(blob, bd); |
| 66 | } |
| 67 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 68 | void meson_board_add_reserved_memory(void *fdt, u64 start, u64 size) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | ret = fdt_add_mem_rsv(fdt, start, size); |
| 73 | if (ret) |
| 74 | printf("Could not reserve zone @ 0x%llx\n", start); |
| 75 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 76 | if (IS_ENABLED(CONFIG_EFI_LOADER)) |
| 77 | efi_add_memory_map(start, size, EFI_RESERVED_MEMORY_TYPE); |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Neil Armstrong | b4acf5a | 2019-06-12 11:49:07 +0200 | [diff] [blame] | 80 | int meson_generate_serial_ethaddr(void) |
| 81 | { |
| 82 | u8 mac_addr[ARP_HLEN]; |
| 83 | char serial[SM_SERIAL_SIZE]; |
| 84 | u32 sid; |
| 85 | u16 sid16; |
| 86 | |
| 87 | if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) { |
| 88 | sid = crc32(0, (unsigned char *)serial, SM_SERIAL_SIZE); |
| 89 | sid16 = crc16_ccitt(0, (unsigned char *)serial, SM_SERIAL_SIZE); |
| 90 | |
| 91 | /* Ensure the NIC specific bytes of the mac are not all 0 */ |
| 92 | if ((sid & 0xffffff) == 0) |
| 93 | sid |= 0x800000; |
| 94 | |
| 95 | /* Non OUI / registered MAC address */ |
| 96 | mac_addr[0] = ((sid16 >> 8) & 0xfc) | 0x02; |
| 97 | mac_addr[1] = (sid16 >> 0) & 0xff; |
| 98 | mac_addr[2] = (sid >> 24) & 0xff; |
| 99 | mac_addr[3] = (sid >> 16) & 0xff; |
| 100 | mac_addr[4] = (sid >> 8) & 0xff; |
| 101 | mac_addr[5] = (sid >> 0) & 0xff; |
| 102 | |
| 103 | eth_env_set_enetaddr("ethaddr", mac_addr); |
| 104 | } else |
| 105 | return -EINVAL; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Neil Armstrong | 2fbfcbb | 2018-07-27 14:10:00 +0200 | [diff] [blame] | 110 | static void meson_set_boot_source(void) |
| 111 | { |
| 112 | const char *source; |
| 113 | |
| 114 | switch (meson_get_boot_device()) { |
| 115 | case BOOT_DEVICE_EMMC: |
| 116 | source = "emmc"; |
| 117 | break; |
| 118 | |
| 119 | case BOOT_DEVICE_NAND: |
| 120 | source = "nand"; |
| 121 | break; |
| 122 | |
| 123 | case BOOT_DEVICE_SPI: |
| 124 | source = "spi"; |
| 125 | break; |
| 126 | |
| 127 | case BOOT_DEVICE_SD: |
| 128 | source = "sd"; |
| 129 | break; |
| 130 | |
| 131 | case BOOT_DEVICE_USB: |
| 132 | source = "usb"; |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | source = "unknown"; |
| 137 | } |
| 138 | |
| 139 | env_set("boot_source", source); |
| 140 | } |
| 141 | |
| 142 | __weak int meson_board_late_init(void) |
| 143 | { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | int board_late_init(void) |
| 148 | { |
| 149 | meson_set_boot_source(); |
| 150 | |
| 151 | return meson_board_late_init(); |
| 152 | } |
| 153 | |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 154 | #if CONFIG_IS_ENABLED(FASTBOOT) |
| 155 | static unsigned int reboot_reason = REBOOT_REASON_NORMAL; |
| 156 | |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 157 | int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 158 | { |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 159 | if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER) |
| 160 | return -ENOTSUPP; |
| 161 | |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 162 | reboot_reason = REBOOT_REASON_BOOTLOADER; |
| 163 | |
| 164 | printf("Using reboot reason: 0x%x\n", reboot_reason); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 169 | void reset_cpu(ulong addr) |
| 170 | { |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 171 | struct pt_regs regs; |
| 172 | |
| 173 | regs.regs[0] = ARM_PSCI_0_2_FN_SYSTEM_RESET; |
| 174 | regs.regs[1] = reboot_reason; |
| 175 | |
| 176 | printf("Rebooting with reason: 0x%lx\n", regs.regs[1]); |
| 177 | |
| 178 | smc_call(®s); |
| 179 | |
| 180 | while (1) |
| 181 | ; |
| 182 | } |
| 183 | #else |
| 184 | void reset_cpu(ulong addr) |
| 185 | { |
Jerome Brunet | f897c4b | 2018-10-05 17:00:37 +0200 | [diff] [blame] | 186 | psci_system_reset(); |
| 187 | } |
Neil Armstrong | 1a5ed9c | 2019-05-22 13:30:25 +0200 | [diff] [blame] | 188 | #endif |