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