Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Freescale Semiconductor, Inc. |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
Peng Fan | bac0845 | 2018-01-10 13:20:33 +0800 | [diff] [blame] | 9 | #include <asm/mach-imx/sys_proto.h> |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 10 | #include <command.h> |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 11 | #include <elf.h> |
Peng Fan | bac0845 | 2018-01-10 13:20:33 +0800 | [diff] [blame] | 12 | #include <imx_sip.h> |
Peng Fan | d2d9338 | 2020-05-11 15:15:21 +0800 | [diff] [blame] | 13 | #include <linux/arm-smccc.h> |
Tom Rini | 2f21887 | 2018-01-03 08:52:39 -0500 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Igor Opaniuk | bfc68a4 | 2019-11-28 15:56:20 +0200 | [diff] [blame] | 15 | #include <cpu_func.h> |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 16 | |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 17 | #ifndef CONFIG_IMX8 |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 18 | /* Just to avoid build error */ |
Marek Vasut | 4a4d410 | 2022-12-13 05:46:06 +0100 | [diff] [blame] | 19 | #if IS_ENABLED(CONFIG_IMX8M) |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 20 | #define SRC_M4C_NON_SCLR_RST_MASK BIT(0) |
| 21 | #define SRC_M4_ENABLE_MASK BIT(0) |
| 22 | #define SRC_M4_REG_OFFSET 0 |
| 23 | #endif |
| 24 | |
Marek Vasut | ddc5935 | 2022-12-13 05:46:07 +0100 | [diff] [blame] | 25 | __weak const struct rproc_att *imx_bootaux_get_hostmap(void) |
| 26 | { |
| 27 | return NULL; |
| 28 | } |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 29 | |
| 30 | static const struct rproc_att *get_host_mapping(unsigned long auxcore) |
| 31 | { |
Marek Vasut | ddc5935 | 2022-12-13 05:46:07 +0100 | [diff] [blame] | 32 | const struct rproc_att *mmap = imx_bootaux_get_hostmap(); |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 33 | |
| 34 | while (mmap && mmap->size) { |
| 35 | if (mmap->da <= auxcore && |
| 36 | mmap->da + mmap->size > auxcore) |
| 37 | return mmap; |
| 38 | mmap++; |
| 39 | } |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * A very simple elf loader for the auxilary core, assumes the image |
| 46 | * is valid, returns the entry point address. |
| 47 | * Translates load addresses in the elf file to the U-Boot address space. |
| 48 | */ |
Ye Li | 8743350 | 2023-06-15 18:09:19 +0800 | [diff] [blame] | 49 | static u32 load_elf_image_m_core_phdr(unsigned long addr, u32 *stack) |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 50 | { |
| 51 | Elf32_Ehdr *ehdr; /* ELF header structure pointer */ |
| 52 | Elf32_Phdr *phdr; /* Program header structure pointer */ |
Peng Fan | a4cafd9 | 2022-04-29 16:03:13 +0800 | [diff] [blame] | 53 | int num = 0; |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 54 | int i; |
| 55 | |
| 56 | ehdr = (Elf32_Ehdr *)addr; |
| 57 | phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff); |
| 58 | |
| 59 | /* Load each program header */ |
| 60 | for (i = 0; i < ehdr->e_phnum; ++i, ++phdr) { |
| 61 | const struct rproc_att *mmap = get_host_mapping(phdr->p_paddr); |
| 62 | void *dst, *src; |
| 63 | |
| 64 | if (phdr->p_type != PT_LOAD) |
| 65 | continue; |
| 66 | |
| 67 | if (!mmap) { |
Peng Fan | 53d2714 | 2022-04-29 16:03:12 +0800 | [diff] [blame] | 68 | printf("Invalid aux core address: %08x\n", |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 69 | phdr->p_paddr); |
| 70 | return 0; |
| 71 | } |
| 72 | |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 73 | dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa; |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 74 | src = (void *)addr + phdr->p_offset; |
| 75 | |
| 76 | debug("Loading phdr %i to 0x%p (%i bytes)\n", |
| 77 | i, dst, phdr->p_filesz); |
| 78 | |
Peng Fan | a4cafd9 | 2022-04-29 16:03:13 +0800 | [diff] [blame] | 79 | if (phdr->p_filesz) { |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 80 | memcpy(dst, src, phdr->p_filesz); |
Peng Fan | a4cafd9 | 2022-04-29 16:03:13 +0800 | [diff] [blame] | 81 | /* Stack in __isr_vector is the first section/word */ |
| 82 | if (!num) |
| 83 | *stack = *(uint32_t *)src; |
| 84 | num++; |
| 85 | } |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 86 | if (phdr->p_filesz != phdr->p_memsz) |
| 87 | memset(dst + phdr->p_filesz, 0x00, |
| 88 | phdr->p_memsz - phdr->p_filesz); |
| 89 | flush_cache((unsigned long)dst & |
| 90 | ~(CONFIG_SYS_CACHELINE_SIZE - 1), |
| 91 | ALIGN(phdr->p_filesz, CONFIG_SYS_CACHELINE_SIZE)); |
| 92 | } |
| 93 | |
| 94 | return ehdr->e_entry; |
| 95 | } |
Max Krummenacher | 5a4f65c | 2021-03-11 18:18:07 +0100 | [diff] [blame] | 96 | |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 97 | int arch_auxiliary_core_up(u32 core_id, ulong addr) |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 98 | { |
Ye Li | 8743350 | 2023-06-15 18:09:19 +0800 | [diff] [blame] | 99 | u32 stack, pc; |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 100 | |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 101 | if (!addr) |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 102 | return -EINVAL; |
| 103 | |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 104 | /* |
| 105 | * handling ELF64 binaries |
| 106 | * isn't supported yet. |
| 107 | */ |
| 108 | if (valid_elf_image(addr)) { |
Peng Fan | a4cafd9 | 2022-04-29 16:03:13 +0800 | [diff] [blame] | 109 | pc = load_elf_image_m_core_phdr(addr, &stack); |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 110 | if (!pc) |
| 111 | return CMD_RET_FAILURE; |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 112 | |
Marek Vasut | 4a4d410 | 2022-12-13 05:46:06 +0100 | [diff] [blame] | 113 | if (!IS_ENABLED(CONFIG_ARM64)) |
Peng Fan | a4cafd9 | 2022-04-29 16:03:13 +0800 | [diff] [blame] | 114 | stack = 0x0; |
Igor Opaniuk | b65af98 | 2019-12-30 13:56:44 +0200 | [diff] [blame] | 115 | } else { |
| 116 | /* |
| 117 | * Assume binary file with vector table at the beginning. |
| 118 | * Cortex-M4 vector tables start with the stack pointer (SP) |
| 119 | * and reset vector (initial PC). |
| 120 | */ |
| 121 | stack = *(u32 *)addr; |
| 122 | pc = *(u32 *)(addr + 4); |
| 123 | } |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 124 | |
Ye Li | 8743350 | 2023-06-15 18:09:19 +0800 | [diff] [blame] | 125 | printf("## Starting auxiliary core stack = 0x%08X, pc = 0x%08X...\n", |
Igor Opaniuk | ebbee91 | 2019-11-28 15:56:19 +0200 | [diff] [blame] | 126 | stack, pc); |
| 127 | |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 128 | /* Set the stack and pc to MCU bootROM */ |
| 129 | writel(stack, MCU_BOOTROM_BASE_ADDR); |
| 130 | writel(pc, MCU_BOOTROM_BASE_ADDR + 4); |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 131 | |
Igor Opaniuk | bfc68a4 | 2019-11-28 15:56:20 +0200 | [diff] [blame] | 132 | flush_dcache_all(); |
| 133 | |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 134 | /* Enable MCU */ |
Marek Vasut | 4a4d410 | 2022-12-13 05:46:06 +0100 | [diff] [blame] | 135 | if (IS_ENABLED(CONFIG_IMX8M)) { |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 136 | arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_START, 0, 0, 0, 0, 0, 0, NULL); |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 137 | } else { |
| 138 | clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET, |
| 139 | SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK); |
| 140 | } |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 141 | |
| 142 | return 0; |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 145 | int arch_auxiliary_core_check_up(u32 core_id) |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 146 | { |
Peng Fan | d2d9338 | 2020-05-11 15:15:21 +0800 | [diff] [blame] | 147 | struct arm_smccc_res res; |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 148 | unsigned int val; |
| 149 | |
Marek Vasut | 4a4d410 | 2022-12-13 05:46:06 +0100 | [diff] [blame] | 150 | if (IS_ENABLED(CONFIG_IMX8M)) { |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 151 | arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_STARTED, 0, 0, 0, 0, 0, 0, &res); |
Peng Fan | 250724b | 2022-04-29 16:03:11 +0800 | [diff] [blame] | 152 | return res.a0; |
| 153 | } |
| 154 | |
Peng Fan | d7e46ca | 2018-01-10 13:20:32 +0800 | [diff] [blame] | 155 | val = readl(SRC_BASE_ADDR + SRC_M4_REG_OFFSET); |
| 156 | |
| 157 | if (val & SRC_M4C_NON_SCLR_RST_MASK) |
| 158 | return 0; /* assert in reset */ |
| 159 | |
| 160 | return 1; |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 161 | } |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 162 | #endif |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 163 | /* |
| 164 | * To i.MX6SX and i.MX7D, the image supported by bootaux needs |
| 165 | * the reset vector at the head for the image, with SP and PC |
| 166 | * as the first two words. |
| 167 | * |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 168 | * Per the cortex-M reference manual, the reset vector of M4/M7 needs |
| 169 | * to exist at 0x0 (TCMUL/IDTCM). The PC and SP are the first two addresses |
| 170 | * of that vector. So to boot M4/M7, the A core must build the M4/M7's reset |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 171 | * vector with getting the PC and SP from image and filling them to |
Peng Fan | e9015f3 | 2023-06-15 18:09:18 +0800 | [diff] [blame] | 172 | * TCMUL/IDTCM. When M4/M7 is kicked, it will load the PC and SP by itself. |
| 173 | * The TCMUL/IDTCM is mapped to (MCU_BOOTROM_BASE_ADDR) at A core side for |
| 174 | * accessing the M4/M7 TCMUL/IDTCM. |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 175 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 176 | static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc, |
| 177 | char *const argv[]) |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 178 | { |
| 179 | ulong addr; |
| 180 | int ret, up; |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 181 | u32 core = 0; |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 182 | |
| 183 | if (argc < 2) |
| 184 | return CMD_RET_USAGE; |
| 185 | |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 186 | if (argc > 2) |
| 187 | core = simple_strtoul(argv[2], NULL, 10); |
| 188 | |
| 189 | up = arch_auxiliary_core_check_up(core); |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 190 | if (up) { |
| 191 | printf("## Auxiliary core is already up\n"); |
| 192 | return CMD_RET_SUCCESS; |
| 193 | } |
| 194 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 195 | addr = hextoul(argv[1], NULL); |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 196 | |
Igor Opaniuk | ebbee91 | 2019-11-28 15:56:19 +0200 | [diff] [blame] | 197 | if (!addr) |
| 198 | return CMD_RET_FAILURE; |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 199 | |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 200 | ret = arch_auxiliary_core_up(core, addr); |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 201 | if (ret) |
| 202 | return CMD_RET_FAILURE; |
| 203 | |
| 204 | return CMD_RET_SUCCESS; |
| 205 | } |
| 206 | |
| 207 | U_BOOT_CMD( |
| 208 | bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux, |
| 209 | "Start auxiliary core", |
Ye Li | c07ac74 | 2023-06-15 18:09:20 +0800 | [diff] [blame] | 210 | "<address> [<core>]\n" |
| 211 | " - start auxiliary core [<core>] (default 0),\n" |
| 212 | " at address <address>\n" |
Peng Fan | fcabb6d | 2016-01-28 16:55:04 +0800 | [diff] [blame] | 213 | ); |