blob: 8115bf40f1a9fde84232beb82e6b330b548162ee [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
Peng Fan250724b2022-04-29 16:03:11 +080017/* 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 Krummenacher5a4f65c2021-03-11 18:18:07 +010024const __weak struct rproc_att hostmap[] = { };
25
26static 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 Fana4cafd92022-04-29 16:03:13 +080045static unsigned long load_elf_image_m_core_phdr(unsigned long addr, ulong *stack)
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010046{
47 Elf32_Ehdr *ehdr; /* ELF header structure pointer */
48 Elf32_Phdr *phdr; /* Program header structure pointer */
Peng Fana4cafd92022-04-29 16:03:13 +080049 int num = 0;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010050 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 Fan53d27142022-04-29 16:03:12 +080064 printf("Invalid aux core address: %08x\n",
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010065 phdr->p_paddr);
66 return 0;
67 }
68
Peng Fan250724b2022-04-29 16:03:11 +080069 dst = (void *)(ulong)(phdr->p_paddr - mmap->da) + mmap->sa;
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010070 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 Fana4cafd92022-04-29 16:03:13 +080075 if (phdr->p_filesz) {
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010076 memcpy(dst, src, phdr->p_filesz);
Peng Fana4cafd92022-04-29 16:03:13 +080077 /* Stack in __isr_vector is the first section/word */
78 if (!num)
79 *stack = *(uint32_t *)src;
80 num++;
81 }
Max Krummenacher5a4f65c2021-03-11 18:18:07 +010082 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 Krummenacher5a4f65c2021-03-11 18:18:07 +010092
Igor Opaniukb65af982019-12-30 13:56:44 +020093int arch_auxiliary_core_up(u32 core_id, ulong addr)
Peng Fanfcabb6d2016-01-28 16:55:04 +080094{
Peng Fand7e46ca2018-01-10 13:20:32 +080095 ulong stack, pc;
96
Igor Opaniukb65af982019-12-30 13:56:44 +020097 if (!addr)
Peng Fand7e46ca2018-01-10 13:20:32 +080098 return -EINVAL;
99
Igor Opaniukb65af982019-12-30 13:56:44 +0200100 /*
101 * handling ELF64 binaries
102 * isn't supported yet.
103 */
104 if (valid_elf_image(addr)) {
Peng Fana4cafd92022-04-29 16:03:13 +0800105 pc = load_elf_image_m_core_phdr(addr, &stack);
Igor Opaniukb65af982019-12-30 13:56:44 +0200106 if (!pc)
107 return CMD_RET_FAILURE;
Peng Fand7e46ca2018-01-10 13:20:32 +0800108
Peng Fana4cafd92022-04-29 16:03:13 +0800109 if (!CONFIG_IS_ENABLED(ARM64))
110 stack = 0x0;
Igor Opaniukb65af982019-12-30 13:56:44 +0200111 } 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 Fan250724b2022-04-29 16:03:11 +0800120
Igor Opaniukebbee912019-11-28 15:56:19 +0200121 printf("## Starting auxiliary core stack = 0x%08lX, pc = 0x%08lX...\n",
122 stack, pc);
123
Peng Fand7e46ca2018-01-10 13:20:32 +0800124 /* 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 Opaniukbfc68a42019-11-28 15:56:20 +0200128 flush_dcache_all();
129
Peng Fand7e46ca2018-01-10 13:20:32 +0800130 /* Enable M4 */
Peng Fan250724b2022-04-29 16:03:11 +0800131 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 Fand7e46ca2018-01-10 13:20:32 +0800137
138 return 0;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800139}
140
Peng Fand7e46ca2018-01-10 13:20:32 +0800141int arch_auxiliary_core_check_up(u32 core_id)
Peng Fanfcabb6d2016-01-28 16:55:04 +0800142{
Peng Fand2d93382020-05-11 15:15:21 +0800143 struct arm_smccc_res res;
Peng Fand7e46ca2018-01-10 13:20:32 +0800144 unsigned int val;
145
Peng Fan250724b2022-04-29 16:03:11 +0800146 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 Fand7e46ca2018-01-10 13:20:32 +0800151 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 Fanfcabb6d2016-01-28 16:55:04 +0800157}
158
Peng Fanfcabb6d2016-01-28 16:55:04 +0800159/*
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 Glassed38aef2020-05-10 11:40:03 -0600172static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
173 char *const argv[])
Peng Fanfcabb6d2016-01-28 16:55:04 +0800174{
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 Glass3ff49ec2021-07-24 09:03:29 -0600187 addr = hextoul(argv[1], NULL);
Peng Fanfcabb6d2016-01-28 16:55:04 +0800188
Igor Opaniukebbee912019-11-28 15:56:19 +0200189 if (!addr)
190 return CMD_RET_FAILURE;
Peng Fanfcabb6d2016-01-28 16:55:04 +0800191
192 ret = arch_auxiliary_core_up(0, addr);
193 if (ret)
194 return CMD_RET_FAILURE;
195
196 return CMD_RET_SUCCESS;
197}
198
199U_BOOT_CMD(
200 bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
201 "Start auxiliary core",
202 ""
203);