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