1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
9 #include <asm/mach-imx/sys_proto.h>
13 #include <linux/arm-smccc.h>
14 #include <linux/compiler.h>
18 /* Just to avoid build error */
19 #if IS_ENABLED(CONFIG_IMX8M)
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
25 __weak const struct rproc_att *imx_bootaux_get_hostmap(void)
30 static const struct rproc_att *get_host_mapping(unsigned long auxcore)
32 const struct rproc_att *mmap = imx_bootaux_get_hostmap();
34 while (mmap && mmap->size) {
35 if (mmap->da <= auxcore &&
36 mmap->da + mmap->size > auxcore)
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.
49 static u32 load_elf_image_m_core_phdr(unsigned long addr, u32 *stack)
51 Elf32_Ehdr *ehdr; /* ELF header structure pointer */
52 Elf32_Phdr *phdr; /* Program header structure pointer */
56 ehdr = (Elf32_Ehdr *)addr;
57 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
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);
64 if (phdr->p_type != PT_LOAD)
68 printf("Invalid aux core address: %08x\n",
73 dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa;
74 src = (void *)addr + phdr->p_offset;
76 debug("Loading phdr %i to 0x%p (%i bytes)\n",
77 i, dst, phdr->p_filesz);
80 memcpy(dst, src, phdr->p_filesz);
81 /* Stack in __isr_vector is the first section/word */
83 *stack = *(uint32_t *)src;
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));
97 int arch_auxiliary_core_up(u32 core_id, ulong addr)
105 * handling ELF64 binaries
106 * isn't supported yet.
108 if (valid_elf_image(addr)) {
109 pc = load_elf_image_m_core_phdr(addr, &stack);
111 return CMD_RET_FAILURE;
113 if (!IS_ENABLED(CONFIG_ARM64))
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).
121 stack = *(u32 *)addr;
122 pc = *(u32 *)(addr + 4);
125 printf("## Starting auxiliary core stack = 0x%08X, pc = 0x%08X...\n",
128 /* Set the stack and pc to MCU bootROM */
129 writel(stack, MCU_BOOTROM_BASE_ADDR);
130 writel(pc, MCU_BOOTROM_BASE_ADDR + 4);
135 if (IS_ENABLED(CONFIG_IMX8M)) {
136 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_START, 0, 0, 0, 0, 0, 0, NULL);
138 clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET,
139 SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK);
145 int arch_auxiliary_core_check_up(u32 core_id)
147 struct arm_smccc_res res;
150 if (IS_ENABLED(CONFIG_IMX8M)) {
151 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_STARTED, 0, 0, 0, 0, 0, 0, &res);
155 val = readl(SRC_BASE_ADDR + SRC_M4_REG_OFFSET);
157 if (val & SRC_M4C_NON_SCLR_RST_MASK)
158 return 0; /* assert in reset */
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.
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
171 * vector with getting the PC and SP from image and filling them to
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.
176 static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
184 return CMD_RET_USAGE;
187 core = simple_strtoul(argv[2], NULL, 10);
189 up = arch_auxiliary_core_check_up(core);
191 printf("## Auxiliary core is already up\n");
192 return CMD_RET_SUCCESS;
195 addr = hextoul(argv[1], NULL);
198 return CMD_RET_FAILURE;
200 ret = arch_auxiliary_core_up(core, addr);
202 return CMD_RET_FAILURE;
204 return CMD_RET_SUCCESS;
208 bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
209 "Start auxiliary core",
210 "<address> [<core>]\n"
211 " - start auxiliary core [<core>] (default 0),\n"
212 " at address <address>\n"