1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
7 #include <asm/arch/imx-regs.h>
9 #include <asm/mach-imx/sys_proto.h>
14 #include <linux/arm-smccc.h>
15 #include <linux/compiler.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
21 /* Just to avoid build error */
22 #if IS_ENABLED(CONFIG_IMX8M)
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
28 __weak const struct rproc_att *imx_bootaux_get_hostmap(void)
33 static const struct rproc_att *get_host_mapping(unsigned long auxcore)
35 const struct rproc_att *mmap = imx_bootaux_get_hostmap();
37 while (mmap && mmap->size) {
38 if (mmap->da <= auxcore &&
39 mmap->da + mmap->size > auxcore)
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.
52 static u32 load_elf_image_m_core_phdr(unsigned long addr, u32 *stack)
54 Elf32_Ehdr *ehdr; /* ELF header structure pointer */
55 Elf32_Phdr *phdr; /* Program header structure pointer */
59 ehdr = (Elf32_Ehdr *)addr;
60 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
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);
67 if (phdr->p_type != PT_LOAD)
71 printf("Invalid aux core address: %08x\n",
76 dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa;
77 src = (void *)addr + phdr->p_offset;
79 debug("Loading phdr %i to 0x%p (%i bytes)\n",
80 i, dst, phdr->p_filesz);
83 memcpy(dst, src, phdr->p_filesz);
84 /* Stack in __isr_vector is the first section/word */
86 *stack = *(uint32_t *)src;
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));
100 int arch_auxiliary_core_up(u32 core_id, ulong addr)
108 * handling ELF64 binaries
109 * isn't supported yet.
111 if (valid_elf_image(addr)) {
112 pc = load_elf_image_m_core_phdr(addr, &stack);
114 return CMD_RET_FAILURE;
116 if (!IS_ENABLED(CONFIG_ARM64))
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).
124 stack = *(u32 *)addr;
125 pc = *(u32 *)(addr + 4);
128 printf("## Starting auxiliary core stack = 0x%08X, pc = 0x%08X...\n",
131 /* Set the stack and pc to MCU bootROM */
132 writel(stack, MCU_BOOTROM_BASE_ADDR);
133 writel(pc, MCU_BOOTROM_BASE_ADDR + 4);
138 if (IS_ENABLED(CONFIG_IMX8M)) {
139 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_START, 0, 0, 0, 0, 0, 0, NULL);
141 clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET,
142 SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK);
148 int arch_auxiliary_core_check_up(u32 core_id)
150 struct arm_smccc_res res;
153 if (IS_ENABLED(CONFIG_IMX8M)) {
154 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_STARTED, 0, 0, 0, 0, 0, 0, &res);
158 val = readl(SRC_BASE_ADDR + SRC_M4_REG_OFFSET);
160 if (val & SRC_M4C_NON_SCLR_RST_MASK)
161 return 0; /* assert in reset */
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.
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
174 * vector with getting the PC and SP from image and filling them to
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.
179 static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
187 return CMD_RET_USAGE;
190 core = simple_strtoul(argv[2], NULL, 10);
192 up = arch_auxiliary_core_check_up(core);
194 printf("## Auxiliary core is already up\n");
195 return CMD_RET_SUCCESS;
198 addr = hextoul(argv[1], NULL);
201 return CMD_RET_FAILURE;
203 ret = arch_auxiliary_core_up(core, addr);
205 return CMD_RET_FAILURE;
207 return CMD_RET_SUCCESS;
211 bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
212 "Start auxiliary core",
213 "<address> [<core>]\n"
214 " - start auxiliary core [<core>] (default 0),\n"
215 " at address <address>\n"