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