blob: f7b14ca38d94263bebf7bf8dc5855921e6b6c1c4 [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
6#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.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>
Peng Fand2d93382020-05-11 15:15:21 +080013#include <linux/arm-smccc.h>
Tom Rini2f218872018-01-03 08:52:39 -050014#include <linux/compiler.h>
Igor Opaniukbfc68a42019-11-28 15:56:20 +020015#include <cpu_func.h>
Peng Fanfcabb6d2016-01-28 16:55:04 +080016
Ye Lic07ac742023-06-15 18:09:20 +080017#ifndef CONFIG_IMX8
Peng Fan250724b2022-04-29 16:03:11 +080018/* Just to avoid build error */
Marek Vasut4a4d4102022-12-13 05:46:06 +010019#if IS_ENABLED(CONFIG_IMX8M)
Peng Fan250724b2022-04-29 16:03:11 +080020#define SRC_M4C_NON_SCLR_RST_MASK BIT(0)
21#define SRC_M4_ENABLE_MASK BIT(0)
22#define SRC_M4_REG_OFFSET 0
23#endif
24
Marek Vasutddc59352022-12-13 05:46:07 +010025__weak const struct rproc_att *imx_bootaux_get_hostmap(void)
26{
27 return NULL;
28}
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010029
30static const struct rproc_att *get_host_mapping(unsigned long auxcore)
31{
Marek Vasutddc59352022-12-13 05:46:07 +010032 const struct rproc_att *mmap = imx_bootaux_get_hostmap();
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010033
34 while (mmap && mmap->size) {
35 if (mmap->da <= auxcore &&
36 mmap->da + mmap->size > auxcore)
37 return mmap;
38 mmap++;
39 }
40
41 return NULL;
42}
43
44/*
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.
48 */
Ye Li87433502023-06-15 18:09:19 +080049static u32 load_elf_image_m_core_phdr(unsigned long addr, u32 *stack)
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010050{
51 Elf32_Ehdr *ehdr; /* ELF header structure pointer */
52 Elf32_Phdr *phdr; /* Program header structure pointer */
Peng Fana4cafd92022-04-29 16:03:13 +080053 int num = 0;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010054 int i;
55
56 ehdr = (Elf32_Ehdr *)addr;
57 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
58
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);
62 void *dst, *src;
63
64 if (phdr->p_type != PT_LOAD)
65 continue;
66
67 if (!mmap) {
Peng Fan53d27142022-04-29 16:03:12 +080068 printf("Invalid aux core address: %08x\n",
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010069 phdr->p_paddr);
70 return 0;
71 }
72
Peng Fan250724b2022-04-29 16:03:11 +080073 dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010074 src = (void *)addr + phdr->p_offset;
75
76 debug("Loading phdr %i to 0x%p (%i bytes)\n",
77 i, dst, phdr->p_filesz);
78
Peng Fana4cafd92022-04-29 16:03:13 +080079 if (phdr->p_filesz) {
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010080 memcpy(dst, src, phdr->p_filesz);
Peng Fana4cafd92022-04-29 16:03:13 +080081 /* Stack in __isr_vector is the first section/word */
82 if (!num)
83 *stack = *(uint32_t *)src;
84 num++;
85 }
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010086 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));
92 }
93
94 return ehdr->e_entry;
95}
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010096
Igor Opaniukb65af982019-12-30 13:56:44 +020097int arch_auxiliary_core_up(u32 core_id, ulong addr)
Peng Fanfcabb6d2016-01-28 16:55:04 +080098{
Ye Li87433502023-06-15 18:09:19 +080099 u32 stack, pc;
Peng Fand7e46ca2018-01-10 13:20:32 +0800100
Igor Opaniukb65af982019-12-30 13:56:44 +0200101 if (!addr)
Peng Fand7e46ca2018-01-10 13:20:32 +0800102 return -EINVAL;
103
Igor Opaniukb65af982019-12-30 13:56:44 +0200104 /*
105 * handling ELF64 binaries
106 * isn't supported yet.
107 */
108 if (valid_elf_image(addr)) {
Peng Fana4cafd92022-04-29 16:03:13 +0800109 pc = load_elf_image_m_core_phdr(addr, &stack);
Igor Opaniukb65af982019-12-30 13:56:44 +0200110 if (!pc)
111 return CMD_RET_FAILURE;
Peng Fand7e46ca2018-01-10 13:20:32 +0800112
Marek Vasut4a4d4102022-12-13 05:46:06 +0100113 if (!IS_ENABLED(CONFIG_ARM64))
Peng Fana4cafd92022-04-29 16:03:13 +0800114 stack = 0x0;
Igor Opaniukb65af982019-12-30 13:56:44 +0200115 } else {
116 /*
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).
120 */
121 stack = *(u32 *)addr;
122 pc = *(u32 *)(addr + 4);
123 }
Peng Fan250724b2022-04-29 16:03:11 +0800124
Ye Li87433502023-06-15 18:09:19 +0800125 printf("## Starting auxiliary core stack = 0x%08X, pc = 0x%08X...\n",
Igor Opaniukebbee912019-11-28 15:56:19 +0200126 stack, pc);
127
Peng Fane9015f32023-06-15 18:09:18 +0800128 /* Set the stack and pc to MCU bootROM */
129 writel(stack, MCU_BOOTROM_BASE_ADDR);
130 writel(pc, MCU_BOOTROM_BASE_ADDR + 4);
Peng Fand7e46ca2018-01-10 13:20:32 +0800131
Igor Opaniukbfc68a42019-11-28 15:56:20 +0200132 flush_dcache_all();
133
Peng Fane9015f32023-06-15 18:09:18 +0800134 /* Enable MCU */
Marek Vasut4a4d4102022-12-13 05:46:06 +0100135 if (IS_ENABLED(CONFIG_IMX8M)) {
Peng Fane9015f32023-06-15 18:09:18 +0800136 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 +0800137 } else {
138 clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET,
139 SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK);
140 }
Peng Fand7e46ca2018-01-10 13:20:32 +0800141
142 return 0;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800143}
144
Peng Fand7e46ca2018-01-10 13:20:32 +0800145int arch_auxiliary_core_check_up(u32 core_id)
Peng Fanfcabb6d2016-01-28 16:55:04 +0800146{
Peng Fand2d93382020-05-11 15:15:21 +0800147 struct arm_smccc_res res;
Peng Fand7e46ca2018-01-10 13:20:32 +0800148 unsigned int val;
149
Marek Vasut4a4d4102022-12-13 05:46:06 +0100150 if (IS_ENABLED(CONFIG_IMX8M)) {
Peng Fane9015f32023-06-15 18:09:18 +0800151 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 +0800152 return res.a0;
153 }
154
Peng Fand7e46ca2018-01-10 13:20:32 +0800155 val = readl(SRC_BASE_ADDR + SRC_M4_REG_OFFSET);
156
157 if (val & SRC_M4C_NON_SCLR_RST_MASK)
158 return 0; /* assert in reset */
159
160 return 1;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800161}
Ye Lic07ac742023-06-15 18:09:20 +0800162#endif
Peng Fanfcabb6d2016-01-28 16:55:04 +0800163/*
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.
167 *
Peng Fane9015f32023-06-15 18:09:18 +0800168 * 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
Peng Fanfcabb6d2016-01-28 16:55:04 +0800171 * vector with getting the PC and SP from image and filling them to
Peng Fane9015f32023-06-15 18:09:18 +0800172 * 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.
Peng Fanfcabb6d2016-01-28 16:55:04 +0800175 */
Simon Glassed38aef2020-05-10 11:40:03 -0600176static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
177 char *const argv[])
Peng Fanfcabb6d2016-01-28 16:55:04 +0800178{
179 ulong addr;
180 int ret, up;
Ye Lic07ac742023-06-15 18:09:20 +0800181 u32 core = 0;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800182
183 if (argc < 2)
184 return CMD_RET_USAGE;
185
Ye Lic07ac742023-06-15 18:09:20 +0800186 if (argc > 2)
187 core = simple_strtoul(argv[2], NULL, 10);
188
189 up = arch_auxiliary_core_check_up(core);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800190 if (up) {
191 printf("## Auxiliary core is already up\n");
192 return CMD_RET_SUCCESS;
193 }
194
Simon Glass3ff49ec2021-07-24 09:03:29 -0600195 addr = hextoul(argv[1], NULL);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800196
Igor Opaniukebbee912019-11-28 15:56:19 +0200197 if (!addr)
198 return CMD_RET_FAILURE;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800199
Ye Lic07ac742023-06-15 18:09:20 +0800200 ret = arch_auxiliary_core_up(core, addr);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800201 if (ret)
202 return CMD_RET_FAILURE;
203
204 return CMD_RET_SUCCESS;
205}
206
207U_BOOT_CMD(
208 bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
209 "Start auxiliary core",
Ye Lic07ac742023-06-15 18:09:20 +0800210 "<address> [<core>]\n"
211 " - start auxiliary core [<core>] (default 0),\n"
212 " at address <address>\n"
Peng Fanfcabb6d2016-01-28 16:55:04 +0800213);