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