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