blob: 26374fdc33eca986c2cf7a661a1225ef0bf06f30 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fanfcabb6d2016-01-28 16:55:04 +08002/*
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
Peng Fanfcabb6d2016-01-28 16:55:04 +08004 */
5
Simon Glass0f2af882020-05-10 11:40:05 -06006#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <asm/arch/imx-regs.h>
Peng Fand7e46ca2018-01-10 13:20:32 +08008#include <asm/io.h>
Peng Fanbac08452018-01-10 13:20:33 +08009#include <asm/mach-imx/sys_proto.h>
Peng Fanfcabb6d2016-01-28 16:55:04 +080010#include <command.h>
Igor Opaniukb65af982019-12-30 13:56:44 +020011#include <elf.h>
Peng Fanbac08452018-01-10 13:20:33 +080012#include <imx_sip.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <vsprintf.h>
Peng Fand2d93382020-05-11 15:15:21 +080014#include <linux/arm-smccc.h>
Tom Rini2f218872018-01-03 08:52:39 -050015#include <linux/compiler.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060016#include <linux/errno.h>
17#include <linux/string.h>
Igor Opaniukbfc68a42019-11-28 15:56:20 +020018#include <cpu_func.h>
Peng Fanfcabb6d2016-01-28 16:55:04 +080019
Ye Lic07ac742023-06-15 18:09:20 +080020#ifndef CONFIG_IMX8
Peng Fan250724b2022-04-29 16:03:11 +080021/* Just to avoid build error */
Marek Vasut4a4d4102022-12-13 05:46:06 +010022#if IS_ENABLED(CONFIG_IMX8M)
Peng Fan250724b2022-04-29 16:03:11 +080023#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 Vasutddc59352022-12-13 05:46:07 +010028__weak const struct rproc_att *imx_bootaux_get_hostmap(void)
29{
30 return NULL;
31}
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010032
33static const struct rproc_att *get_host_mapping(unsigned long auxcore)
34{
Marek Vasutddc59352022-12-13 05:46:07 +010035 const struct rproc_att *mmap = imx_bootaux_get_hostmap();
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010036
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 Li87433502023-06-15 18:09:19 +080052static u32 load_elf_image_m_core_phdr(unsigned long addr, u32 *stack)
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010053{
54 Elf32_Ehdr *ehdr; /* ELF header structure pointer */
55 Elf32_Phdr *phdr; /* Program header structure pointer */
Peng Fana4cafd92022-04-29 16:03:13 +080056 int num = 0;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010057 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 Fan53d27142022-04-29 16:03:12 +080071 printf("Invalid aux core address: %08x\n",
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010072 phdr->p_paddr);
73 return 0;
74 }
75
Peng Fan250724b2022-04-29 16:03:11 +080076 dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010077 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 Fana4cafd92022-04-29 16:03:13 +080082 if (phdr->p_filesz) {
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010083 memcpy(dst, src, phdr->p_filesz);
Peng Fana4cafd92022-04-29 16:03:13 +080084 /* Stack in __isr_vector is the first section/word */
85 if (!num)
86 *stack = *(uint32_t *)src;
87 num++;
88 }
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010089 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 Krummenacher5a4f65c2021-03-11 18:18:07 +010099
Igor Opaniukb65af982019-12-30 13:56:44 +0200100int arch_auxiliary_core_up(u32 core_id, ulong addr)
Peng Fanfcabb6d2016-01-28 16:55:04 +0800101{
Ye Li87433502023-06-15 18:09:19 +0800102 u32 stack, pc;
Peng Fand7e46ca2018-01-10 13:20:32 +0800103
Igor Opaniukb65af982019-12-30 13:56:44 +0200104 if (!addr)
Peng Fand7e46ca2018-01-10 13:20:32 +0800105 return -EINVAL;
106
Igor Opaniukb65af982019-12-30 13:56:44 +0200107 /*
108 * handling ELF64 binaries
109 * isn't supported yet.
110 */
111 if (valid_elf_image(addr)) {
Peng Fana4cafd92022-04-29 16:03:13 +0800112 pc = load_elf_image_m_core_phdr(addr, &stack);
Igor Opaniukb65af982019-12-30 13:56:44 +0200113 if (!pc)
114 return CMD_RET_FAILURE;
Peng Fand7e46ca2018-01-10 13:20:32 +0800115
Marek Vasut4a4d4102022-12-13 05:46:06 +0100116 if (!IS_ENABLED(CONFIG_ARM64))
Peng Fana4cafd92022-04-29 16:03:13 +0800117 stack = 0x0;
Igor Opaniukb65af982019-12-30 13:56:44 +0200118 } 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 Fan250724b2022-04-29 16:03:11 +0800127
Ye Li87433502023-06-15 18:09:19 +0800128 printf("## Starting auxiliary core stack = 0x%08X, pc = 0x%08X...\n",
Igor Opaniukebbee912019-11-28 15:56:19 +0200129 stack, pc);
130
Peng Fane9015f32023-06-15 18:09:18 +0800131 /* Set the stack and pc to MCU bootROM */
132 writel(stack, MCU_BOOTROM_BASE_ADDR);
133 writel(pc, MCU_BOOTROM_BASE_ADDR + 4);
Peng Fand7e46ca2018-01-10 13:20:32 +0800134
Igor Opaniukbfc68a42019-11-28 15:56:20 +0200135 flush_dcache_all();
136
Peng Fane9015f32023-06-15 18:09:18 +0800137 /* Enable MCU */
Marek Vasut4a4d4102022-12-13 05:46:06 +0100138 if (IS_ENABLED(CONFIG_IMX8M)) {
Peng Fane9015f32023-06-15 18:09:18 +0800139 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_START, 0, 0, 0, 0, 0, 0, NULL);
Peng Fan250724b2022-04-29 16:03:11 +0800140 } else {
141 clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET,
142 SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK);
143 }
Peng Fand7e46ca2018-01-10 13:20:32 +0800144
145 return 0;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800146}
147
Peng Fand7e46ca2018-01-10 13:20:32 +0800148int arch_auxiliary_core_check_up(u32 core_id)
Peng Fanfcabb6d2016-01-28 16:55:04 +0800149{
Peng Fand2d93382020-05-11 15:15:21 +0800150 struct arm_smccc_res res;
Peng Fand7e46ca2018-01-10 13:20:32 +0800151 unsigned int val;
152
Marek Vasut4a4d4102022-12-13 05:46:06 +0100153 if (IS_ENABLED(CONFIG_IMX8M)) {
Peng Fane9015f32023-06-15 18:09:18 +0800154 arm_smccc_smc(IMX_SIP_SRC, IMX_SIP_SRC_MCU_STARTED, 0, 0, 0, 0, 0, 0, &res);
Peng Fan250724b2022-04-29 16:03:11 +0800155 return res.a0;
156 }
157
Peng Fand7e46ca2018-01-10 13:20:32 +0800158 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 Fanfcabb6d2016-01-28 16:55:04 +0800164}
Ye Lic07ac742023-06-15 18:09:20 +0800165#endif
Peng Fanfcabb6d2016-01-28 16:55:04 +0800166/*
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 Fane9015f32023-06-15 18:09:18 +0800171 * 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 Fanfcabb6d2016-01-28 16:55:04 +0800174 * vector with getting the PC and SP from image and filling them to
Peng Fane9015f32023-06-15 18:09:18 +0800175 * 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 Fanfcabb6d2016-01-28 16:55:04 +0800178 */
Simon Glassed38aef2020-05-10 11:40:03 -0600179static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
180 char *const argv[])
Peng Fanfcabb6d2016-01-28 16:55:04 +0800181{
182 ulong addr;
183 int ret, up;
Ye Lic07ac742023-06-15 18:09:20 +0800184 u32 core = 0;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800185
186 if (argc < 2)
187 return CMD_RET_USAGE;
188
Ye Lic07ac742023-06-15 18:09:20 +0800189 if (argc > 2)
190 core = simple_strtoul(argv[2], NULL, 10);
191
192 up = arch_auxiliary_core_check_up(core);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800193 if (up) {
194 printf("## Auxiliary core is already up\n");
195 return CMD_RET_SUCCESS;
196 }
197
Simon Glass3ff49ec2021-07-24 09:03:29 -0600198 addr = hextoul(argv[1], NULL);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800199
Igor Opaniukebbee912019-11-28 15:56:19 +0200200 if (!addr)
201 return CMD_RET_FAILURE;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800202
Ye Lic07ac742023-06-15 18:09:20 +0800203 ret = arch_auxiliary_core_up(core, addr);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800204 if (ret)
205 return CMD_RET_FAILURE;
206
207 return CMD_RET_SUCCESS;
208}
209
210U_BOOT_CMD(
211 bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
212 "Start auxiliary core",
Ye Lic07ac742023-06-15 18:09:20 +0800213 "<address> [<core>]\n"
214 " - start auxiliary core [<core>] (default 0),\n"
215 " at address <address>\n"
Peng Fanfcabb6d2016-01-28 16:55:04 +0800216);