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