command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / arch / arm / mach-imx / imx_bootaux.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Freescale Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <asm/io.h>
8 #include <asm/mach-imx/sys_proto.h>
9 #include <command.h>
10 #include <elf.h>
11 #include <imx_sip.h>
12 #include <linux/compiler.h>
13 #include <cpu_func.h>
14
15 int arch_auxiliary_core_up(u32 core_id, ulong addr)
16 {
17         ulong stack, pc;
18
19         if (!addr)
20                 return -EINVAL;
21
22 #ifdef CONFIG_IMX8M
23         stack = *(u32 *)addr;
24         pc = *(u32 *)(addr + 4);
25 #else
26         /*
27          * handling ELF64 binaries
28          * isn't supported yet.
29          */
30         if (valid_elf_image(addr)) {
31                 stack = 0x0;
32                 pc = load_elf_image_phdr(addr);
33                 if (!pc)
34                         return CMD_RET_FAILURE;
35
36         } else {
37                 /*
38                  * Assume binary file with vector table at the beginning.
39                  * Cortex-M4 vector tables start with the stack pointer (SP)
40                  * and reset vector (initial PC).
41                  */
42                 stack = *(u32 *)addr;
43                 pc = *(u32 *)(addr + 4);
44         }
45 #endif
46         printf("## Starting auxiliary core stack = 0x%08lX, pc = 0x%08lX...\n",
47                stack, pc);
48
49         /* Set the stack and pc to M4 bootROM */
50         writel(stack, M4_BOOTROM_BASE_ADDR);
51         writel(pc, M4_BOOTROM_BASE_ADDR + 4);
52
53         flush_dcache_all();
54
55         /* Enable M4 */
56 #ifdef CONFIG_IMX8M
57         call_imx_sip(IMX_SIP_SRC, IMX_SIP_SRC_M4_START, 0, 0, 0);
58 #else
59         clrsetbits_le32(SRC_BASE_ADDR + SRC_M4_REG_OFFSET,
60                         SRC_M4C_NON_SCLR_RST_MASK, SRC_M4_ENABLE_MASK);
61 #endif
62
63         return 0;
64 }
65
66 int arch_auxiliary_core_check_up(u32 core_id)
67 {
68 #ifdef CONFIG_IMX8M
69         return call_imx_sip(IMX_SIP_SRC, IMX_SIP_SRC_M4_STARTED, 0, 0, 0);
70 #else
71         unsigned int val;
72
73         val = readl(SRC_BASE_ADDR + SRC_M4_REG_OFFSET);
74
75         if (val & SRC_M4C_NON_SCLR_RST_MASK)
76                 return 0;  /* assert in reset */
77
78         return 1;
79 #endif
80 }
81
82 /*
83  * To i.MX6SX and i.MX7D, the image supported by bootaux needs
84  * the reset vector at the head for the image, with SP and PC
85  * as the first two words.
86  *
87  * Per the cortex-M reference manual, the reset vector of M4 needs
88  * to exist at 0x0 (TCMUL). The PC and SP are the first two addresses
89  * of that vector.  So to boot M4, the A core must build the M4's reset
90  * vector with getting the PC and SP from image and filling them to
91  * TCMUL. When M4 is kicked, it will load the PC and SP by itself.
92  * The TCMUL is mapped to (M4_BOOTROM_BASE_ADDR) at A core side for
93  * accessing the M4 TCMUL.
94  */
95 static int do_bootaux(struct cmd_tbl *cmdtp, int flag, int argc,
96                       char *const argv[])
97 {
98         ulong addr;
99         int ret, up;
100
101         if (argc < 2)
102                 return CMD_RET_USAGE;
103
104         up = arch_auxiliary_core_check_up(0);
105         if (up) {
106                 printf("## Auxiliary core is already up\n");
107                 return CMD_RET_SUCCESS;
108         }
109
110         addr = simple_strtoul(argv[1], NULL, 16);
111
112         if (!addr)
113                 return CMD_RET_FAILURE;
114
115         ret = arch_auxiliary_core_up(0, addr);
116         if (ret)
117                 return CMD_RET_FAILURE;
118
119         return CMD_RET_SUCCESS;
120 }
121
122 U_BOOT_CMD(
123         bootaux, CONFIG_SYS_MAXARGS, 1, do_bootaux,
124         "Start auxiliary core",
125         ""
126 );