blob: 704520e7a3c9eb797c2d76e3cca665c216157597 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek58f865f2015-04-15 13:36:40 +02002/*
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
Michal Simek58f865f2015-04-15 13:36:40 +02005 */
6
7#include <common.h>
Simon Glass970b61e2019-11-14 12:57:09 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Michal Simek58f865f2015-04-15 13:36:40 +020010#include <asm/arch/hardware.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Michal Simek58f865f2015-04-15 13:36:40 +020014
15#define LOCK 0
16#define SPLIT 1
17
18#define HALT 0
19#define RELEASE 1
20
21#define ZYNQMP_BOOTADDR_HIGH_MASK 0xFFFFFFFF
22#define ZYNQMP_R5_HIVEC_ADDR 0xFFFF0000
23#define ZYNQMP_R5_LOVEC_ADDR 0x0
24#define ZYNQMP_RPU_CFG_CPU_HALT_MASK 0x01
25#define ZYNQMP_RPU_CFG_HIVEC_MASK 0x04
26#define ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK 0x08
27#define ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK 0x40
28#define ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK 0x10
29
30#define ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK 0x04
31#define ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK 0x01
32#define ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK 0x02
33#define ZYNQMP_CRLAPB_CPU_R5_CTRL_CLKACT_MASK 0x1000000
34
35#define ZYNQMP_TCM_START_ADDRESS 0xFFE00000
36#define ZYNQMP_TCM_BOTH_SIZE 0x40000
37
38#define ZYNQMP_CORE_APU0 0
39#define ZYNQMP_CORE_APU3 3
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060040#define ZYNQMP_CORE_RPU0 4
41#define ZYNQMP_CORE_RPU1 5
Michal Simek58f865f2015-04-15 13:36:40 +020042
43#define ZYNQMP_MAX_CORES 6
44
45int is_core_valid(unsigned int core)
46{
47 if (core < ZYNQMP_MAX_CORES)
48 return 1;
49
50 return 0;
51}
52
Michal Simek1669e182018-06-13 08:56:31 +020053int cpu_reset(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +020054{
55 puts("Feature is not implemented.\n");
56 return 0;
57}
58
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060059static void set_r5_halt_mode(u32 nr, u8 halt, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +020060{
61 u32 tmp;
62
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060063 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0) {
64 tmp = readl(&rpu_base->rpu0_cfg);
65 if (halt == HALT)
66 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
67 else
68 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
69 writel(tmp, &rpu_base->rpu0_cfg);
70 }
Michal Simek58f865f2015-04-15 13:36:40 +020071
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060072 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1) {
Michal Simek58f865f2015-04-15 13:36:40 +020073 tmp = readl(&rpu_base->rpu1_cfg);
74 if (halt == HALT)
75 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
76 else
77 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
78 writel(tmp, &rpu_base->rpu1_cfg);
79 }
80}
81
82static void set_r5_tcm_mode(u8 mode)
83{
84 u32 tmp;
85
86 tmp = readl(&rpu_base->rpu_glbl_ctrl);
87 if (mode == LOCK) {
88 tmp &= ~ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK;
89 tmp |= ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK |
90 ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK;
91 } else {
92 tmp |= ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK;
93 tmp &= ~(ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK |
94 ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK);
95 }
96
97 writel(tmp, &rpu_base->rpu_glbl_ctrl);
98}
99
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600100static void set_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200101{
102 u32 tmp;
103
104 tmp = readl(&crlapb_base->rst_lpd_top);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600105 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0)
106 tmp |= (ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
107 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200108
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600109 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1)
110 tmp |= (ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
111 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200112
113 writel(tmp, &crlapb_base->rst_lpd_top);
114}
115
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600116static void release_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200117{
118 u32 tmp;
119
120 tmp = readl(&crlapb_base->rst_lpd_top);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600121 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0)
122 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
123 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200124
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600125 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1)
126 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
127 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200128
129 writel(tmp, &crlapb_base->rst_lpd_top);
130}
131
132static void enable_clock_r5(void)
133{
134 u32 tmp;
135
136 tmp = readl(&crlapb_base->cpu_r5_ctrl);
137 tmp |= ZYNQMP_CRLAPB_CPU_R5_CTRL_CLKACT_MASK;
138 writel(tmp, &crlapb_base->cpu_r5_ctrl);
139
140 /* Give some delay for clock
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400141 * to propagate */
Michal Simek58f865f2015-04-15 13:36:40 +0200142 udelay(0x500);
143}
144
Michal Simek1669e182018-06-13 08:56:31 +0200145int cpu_disable(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200146{
147 if (nr >= ZYNQMP_CORE_APU0 && nr <= ZYNQMP_CORE_APU3) {
148 u32 val = readl(&crfapb_base->rst_fpd_apu);
149 val |= 1 << nr;
150 writel(val, &crfapb_base->rst_fpd_apu);
151 } else {
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600152 set_r5_reset(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200153 }
154
155 return 0;
156}
157
Michal Simek1669e182018-06-13 08:56:31 +0200158int cpu_status(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200159{
160 if (nr >= ZYNQMP_CORE_APU0 && nr <= ZYNQMP_CORE_APU3) {
161 u32 addr_low = readl(((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
162 u32 addr_high = readl(((u8 *)&apu_base->rvbar_addr0_h) +
163 nr * 8);
164 u32 val = readl(&crfapb_base->rst_fpd_apu);
165 val &= 1 << nr;
166 printf("APU CPU%d %s - starting address HI: %x, LOW: %x\n",
167 nr, val ? "OFF" : "ON" , addr_high, addr_low);
168 } else {
169 u32 val = readl(&crlapb_base->rst_lpd_top);
170 val &= 1 << (nr - 4);
171 printf("RPU CPU%d %s\n", nr - 4, val ? "OFF" : "ON");
172 }
173
174 return 0;
175}
176
177static void set_r5_start(u8 high)
178{
179 u32 tmp;
180
181 tmp = readl(&rpu_base->rpu0_cfg);
182 if (high)
183 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
184 else
185 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
186 writel(tmp, &rpu_base->rpu0_cfg);
187
188 tmp = readl(&rpu_base->rpu1_cfg);
189 if (high)
190 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
191 else
192 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
193 writel(tmp, &rpu_base->rpu1_cfg);
194}
195
Michal Simekf5005ce2015-05-22 13:28:23 +0200196static void write_tcm_boot_trampoline(u32 boot_addr)
197{
198 if (boot_addr) {
199 /*
200 * Boot trampoline is simple ASM code below.
201 *
202 * b over;
203 * label:
204 * .word 0
205 * over: ldr r0, =label
206 * ldr r1, [r0]
207 * bx r1
208 */
209 debug("Write boot trampoline for %x\n", boot_addr);
210 writel(0xea000000, ZYNQMP_TCM_START_ADDRESS);
211 writel(boot_addr, ZYNQMP_TCM_START_ADDRESS + 0x4);
212 writel(0xe59f0004, ZYNQMP_TCM_START_ADDRESS + 0x8);
213 writel(0xe5901000, ZYNQMP_TCM_START_ADDRESS + 0xc);
214 writel(0xe12fff11, ZYNQMP_TCM_START_ADDRESS + 0x10);
215 writel(0x00000004, ZYNQMP_TCM_START_ADDRESS + 0x14); // address for
216 }
217}
218
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530219void initialize_tcm(bool mode)
220{
221 if (!mode) {
222 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600223 set_r5_halt_mode(ZYNQMP_CORE_RPU0, HALT, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530224 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600225 release_r5_reset(ZYNQMP_CORE_RPU0, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530226 } else {
227 set_r5_tcm_mode(SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600228 set_r5_halt_mode(ZYNQMP_CORE_RPU1, HALT, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530229 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600230 release_r5_reset(ZYNQMP_CORE_RPU1, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530231 }
232}
233
Simon Glassed38aef2020-05-10 11:40:03 -0600234int cpu_release(u32 nr, int argc, char *const argv[])
Michal Simek58f865f2015-04-15 13:36:40 +0200235{
236 if (nr >= ZYNQMP_CORE_APU0 && nr <= ZYNQMP_CORE_APU3) {
237 u64 boot_addr = simple_strtoull(argv[0], NULL, 16);
238 /* HIGH */
239 writel((u32)(boot_addr >> 32),
240 ((u8 *)&apu_base->rvbar_addr0_h) + nr * 8);
241 /* LOW */
242 writel((u32)(boot_addr & ZYNQMP_BOOTADDR_HIGH_MASK),
243 ((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
244
245 u32 val = readl(&crfapb_base->rst_fpd_apu);
246 val &= ~(1 << nr);
247 writel(val, &crfapb_base->rst_fpd_apu);
248 } else {
249 if (argc != 2) {
250 printf("Invalid number of arguments to release.\n");
251 printf("<addr> <mode>-Start addr lockstep or split\n");
252 return 1;
253 }
254
Simon Glass3ff49ec2021-07-24 09:03:29 -0600255 u32 boot_addr = hextoul(argv[0], NULL);
Michal Simekf5005ce2015-05-22 13:28:23 +0200256 u32 boot_addr_uniq = 0;
Michal Simek58f865f2015-04-15 13:36:40 +0200257 if (!(boot_addr == ZYNQMP_R5_LOVEC_ADDR ||
258 boot_addr == ZYNQMP_R5_HIVEC_ADDR)) {
Michal Simekf5005ce2015-05-22 13:28:23 +0200259 printf("Using TCM jump trampoline for address 0x%x\n",
260 boot_addr);
261 /* Save boot address for later usage */
262 boot_addr_uniq = boot_addr;
263 /*
264 * R5 needs to start from LOVEC at TCM
265 * OCM will be probably occupied by ATF
266 */
267 boot_addr = ZYNQMP_R5_LOVEC_ADDR;
Michal Simek58f865f2015-04-15 13:36:40 +0200268 }
269
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530270 /*
271 * Since we don't know where the user may have loaded the image
272 * for an R5 we have to flush all the data cache to ensure
273 * the R5 sees it.
274 */
275 flush_dcache_all();
276
Michal Simek58f865f2015-04-15 13:36:40 +0200277 if (!strncmp(argv[1], "lockstep", 8)) {
278 printf("R5 lockstep mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600279 set_r5_reset(nr, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200280 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600281 set_r5_halt_mode(nr, HALT, LOCK);
Michal Simek08adc902015-05-22 13:26:33 +0200282 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200283 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600284 release_r5_reset(nr, LOCK);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530285 dcache_disable();
Michal Simekf5005ce2015-05-22 13:28:23 +0200286 write_tcm_boot_trampoline(boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530287 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600288 set_r5_halt_mode(nr, RELEASE, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200289 } else if (!strncmp(argv[1], "split", 5)) {
290 printf("R5 split mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600291 set_r5_reset(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200292 set_r5_tcm_mode(SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600293 set_r5_halt_mode(nr, HALT, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530294 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200295 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600296 release_r5_reset(nr, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530297 dcache_disable();
Michal Simekf5005ce2015-05-22 13:28:23 +0200298 write_tcm_boot_trampoline(boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530299 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600300 set_r5_halt_mode(nr, RELEASE, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200301 } else {
302 printf("Unsupported mode\n");
303 return 1;
304 }
305 }
306
307 return 0;
308}