blob: b06c867e57f662852353cbdc56387737819ff7e6 [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
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +020035#define ZYNQMP_R5_0_TCM_START_ADDR 0xFFE00000
36#define ZYNQMP_R5_1_TCM_START_ADDR 0xFFE90000
Michal Simek58f865f2015-04-15 13:36:40 +020037#define ZYNQMP_TCM_BOTH_SIZE 0x40000
38
39#define ZYNQMP_CORE_APU0 0
40#define ZYNQMP_CORE_APU3 3
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060041#define ZYNQMP_CORE_RPU0 4
42#define ZYNQMP_CORE_RPU1 5
Michal Simek58f865f2015-04-15 13:36:40 +020043
44#define ZYNQMP_MAX_CORES 6
45
Lukas Funkec6f90582022-10-28 14:15:47 +020046#define ZYNQMP_RPU0_USE_MASK BIT(1)
47#define ZYNQMP_RPU1_USE_MASK BIT(2)
48
Michal Simek58f865f2015-04-15 13:36:40 +020049int is_core_valid(unsigned int core)
50{
51 if (core < ZYNQMP_MAX_CORES)
52 return 1;
53
54 return 0;
55}
56
Michal Simek1669e182018-06-13 08:56:31 +020057int cpu_reset(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +020058{
59 puts("Feature is not implemented.\n");
60 return 0;
61}
62
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060063static void set_r5_halt_mode(u32 nr, u8 halt, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +020064{
65 u32 tmp;
66
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060067 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0) {
68 tmp = readl(&rpu_base->rpu0_cfg);
69 if (halt == HALT)
70 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
71 else
72 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
73 writel(tmp, &rpu_base->rpu0_cfg);
74 }
Michal Simek58f865f2015-04-15 13:36:40 +020075
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060076 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1) {
Michal Simek58f865f2015-04-15 13:36:40 +020077 tmp = readl(&rpu_base->rpu1_cfg);
78 if (halt == HALT)
79 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
80 else
81 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
82 writel(tmp, &rpu_base->rpu1_cfg);
83 }
84}
85
86static void set_r5_tcm_mode(u8 mode)
87{
88 u32 tmp;
89
90 tmp = readl(&rpu_base->rpu_glbl_ctrl);
91 if (mode == LOCK) {
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 } else {
96 tmp |= ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK;
97 tmp &= ~(ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK |
98 ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK);
99 }
100
101 writel(tmp, &rpu_base->rpu_glbl_ctrl);
102}
103
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600104static void set_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200105{
106 u32 tmp;
107
108 tmp = readl(&crlapb_base->rst_lpd_top);
Neal Fragerd929bbf2022-05-04 09:12:26 +0200109 if (mode == LOCK) {
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600110 tmp |= (ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
Neal Fragerd929bbf2022-05-04 09:12:26 +0200111 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK |
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600112 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Neal Fragerd929bbf2022-05-04 09:12:26 +0200113 } else {
114 if (nr == ZYNQMP_CORE_RPU0) {
115 tmp |= ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK;
116 if (tmp & ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK)
117 tmp |= ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK;
118 } else {
119 tmp |= ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK;
120 if (tmp & ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK)
121 tmp |= ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK;
122 }
123 }
Michal Simek58f865f2015-04-15 13:36:40 +0200124
125 writel(tmp, &crlapb_base->rst_lpd_top);
126}
127
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600128static void release_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200129{
130 u32 tmp;
131
132 tmp = readl(&crlapb_base->rst_lpd_top);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600133 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0)
134 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
135 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200136
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600137 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1)
138 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
139 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200140
141 writel(tmp, &crlapb_base->rst_lpd_top);
142}
143
144static void enable_clock_r5(void)
145{
146 u32 tmp;
147
148 tmp = readl(&crlapb_base->cpu_r5_ctrl);
149 tmp |= ZYNQMP_CRLAPB_CPU_R5_CTRL_CLKACT_MASK;
150 writel(tmp, &crlapb_base->cpu_r5_ctrl);
151
152 /* Give some delay for clock
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400153 * to propagate */
Michal Simek58f865f2015-04-15 13:36:40 +0200154 udelay(0x500);
155}
156
Neal Fragerd929bbf2022-05-04 09:12:26 +0200157static int check_r5_mode(void)
158{
159 u32 tmp;
160
161 tmp = readl(&rpu_base->rpu_glbl_ctrl);
162 if (tmp & ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK)
163 return SPLIT;
164
165 return LOCK;
166}
167
Michal Simek1669e182018-06-13 08:56:31 +0200168int cpu_disable(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200169{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530170 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200171 u32 val = readl(&crfapb_base->rst_fpd_apu);
172 val |= 1 << nr;
173 writel(val, &crfapb_base->rst_fpd_apu);
174 } else {
Neal Fragerd929bbf2022-05-04 09:12:26 +0200175 set_r5_reset(nr, check_r5_mode());
Michal Simek58f865f2015-04-15 13:36:40 +0200176 }
177
178 return 0;
179}
180
Michal Simek1669e182018-06-13 08:56:31 +0200181int cpu_status(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200182{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530183 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200184 u32 addr_low = readl(((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
185 u32 addr_high = readl(((u8 *)&apu_base->rvbar_addr0_h) +
186 nr * 8);
187 u32 val = readl(&crfapb_base->rst_fpd_apu);
188 val &= 1 << nr;
189 printf("APU CPU%d %s - starting address HI: %x, LOW: %x\n",
190 nr, val ? "OFF" : "ON" , addr_high, addr_low);
191 } else {
192 u32 val = readl(&crlapb_base->rst_lpd_top);
193 val &= 1 << (nr - 4);
194 printf("RPU CPU%d %s\n", nr - 4, val ? "OFF" : "ON");
195 }
196
197 return 0;
198}
199
200static void set_r5_start(u8 high)
201{
202 u32 tmp;
203
204 tmp = readl(&rpu_base->rpu0_cfg);
205 if (high)
206 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
207 else
208 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
209 writel(tmp, &rpu_base->rpu0_cfg);
210
211 tmp = readl(&rpu_base->rpu1_cfg);
212 if (high)
213 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
214 else
215 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
216 writel(tmp, &rpu_base->rpu1_cfg);
217}
218
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +0200219static void write_tcm_boot_trampoline(u32 nr, u32 boot_addr)
Michal Simekf5005ce2015-05-22 13:28:23 +0200220{
221 if (boot_addr) {
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +0200222 u64 tcm_start_addr = ZYNQMP_R5_0_TCM_START_ADDR;
223
224 if (nr == ZYNQMP_CORE_RPU1)
225 tcm_start_addr = ZYNQMP_R5_1_TCM_START_ADDR;
226
Michal Simekf5005ce2015-05-22 13:28:23 +0200227 /*
228 * Boot trampoline is simple ASM code below.
229 *
230 * b over;
231 * label:
232 * .word 0
233 * over: ldr r0, =label
234 * ldr r1, [r0]
235 * bx r1
236 */
237 debug("Write boot trampoline for %x\n", boot_addr);
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +0200238 writel(0xea000000, tcm_start_addr);
239 writel(boot_addr, tcm_start_addr + 0x4);
240 writel(0xe59f0004, tcm_start_addr + 0x8);
241 writel(0xe5901000, tcm_start_addr + 0xc);
242 writel(0xe12fff11, tcm_start_addr + 0x10);
243 writel(0x00000004, tcm_start_addr + 0x14);
Michal Simekf5005ce2015-05-22 13:28:23 +0200244 }
245}
246
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530247void initialize_tcm(bool mode)
248{
249 if (!mode) {
250 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600251 set_r5_halt_mode(ZYNQMP_CORE_RPU0, HALT, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530252 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600253 release_r5_reset(ZYNQMP_CORE_RPU0, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530254 } else {
255 set_r5_tcm_mode(SPLIT);
Neal Frager7aba2552023-03-23 08:25:06 +0000256 set_r5_halt_mode(ZYNQMP_CORE_RPU0, HALT, SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600257 set_r5_halt_mode(ZYNQMP_CORE_RPU1, HALT, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530258 enable_clock_r5();
Neal Frager7aba2552023-03-23 08:25:06 +0000259 release_r5_reset(ZYNQMP_CORE_RPU0, SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600260 release_r5_reset(ZYNQMP_CORE_RPU1, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530261 }
Lukas Funkec6f90582022-10-28 14:15:47 +0200262}
263
264static void mark_r5_used(u32 nr, u8 mode)
265{
266 u32 mask = 0;
267
268 if (mode == LOCK) {
269 mask = ZYNQMP_RPU0_USE_MASK | ZYNQMP_RPU1_USE_MASK;
270 } else {
271 switch (nr) {
272 case ZYNQMP_CORE_RPU0:
273 mask = ZYNQMP_RPU0_USE_MASK;
274 break;
275 case ZYNQMP_CORE_RPU1:
276 mask = ZYNQMP_RPU1_USE_MASK;
277 break;
278 default:
279 return;
280 }
281 }
282 zynqmp_mmio_write((ulong)&pmu_base->gen_storage4, mask, mask);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530283}
284
Simon Glassed38aef2020-05-10 11:40:03 -0600285int cpu_release(u32 nr, int argc, char *const argv[])
Michal Simek58f865f2015-04-15 13:36:40 +0200286{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530287 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200288 u64 boot_addr = simple_strtoull(argv[0], NULL, 16);
289 /* HIGH */
290 writel((u32)(boot_addr >> 32),
291 ((u8 *)&apu_base->rvbar_addr0_h) + nr * 8);
292 /* LOW */
293 writel((u32)(boot_addr & ZYNQMP_BOOTADDR_HIGH_MASK),
294 ((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
295
296 u32 val = readl(&crfapb_base->rst_fpd_apu);
297 val &= ~(1 << nr);
298 writel(val, &crfapb_base->rst_fpd_apu);
299 } else {
300 if (argc != 2) {
301 printf("Invalid number of arguments to release.\n");
302 printf("<addr> <mode>-Start addr lockstep or split\n");
303 return 1;
304 }
305
Simon Glass3ff49ec2021-07-24 09:03:29 -0600306 u32 boot_addr = hextoul(argv[0], NULL);
Michal Simekf5005ce2015-05-22 13:28:23 +0200307 u32 boot_addr_uniq = 0;
Michal Simek58f865f2015-04-15 13:36:40 +0200308 if (!(boot_addr == ZYNQMP_R5_LOVEC_ADDR ||
309 boot_addr == ZYNQMP_R5_HIVEC_ADDR)) {
Michal Simekf5005ce2015-05-22 13:28:23 +0200310 printf("Using TCM jump trampoline for address 0x%x\n",
311 boot_addr);
312 /* Save boot address for later usage */
313 boot_addr_uniq = boot_addr;
314 /*
315 * R5 needs to start from LOVEC at TCM
316 * OCM will be probably occupied by ATF
317 */
318 boot_addr = ZYNQMP_R5_LOVEC_ADDR;
Michal Simek58f865f2015-04-15 13:36:40 +0200319 }
320
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530321 /*
322 * Since we don't know where the user may have loaded the image
323 * for an R5 we have to flush all the data cache to ensure
324 * the R5 sees it.
325 */
326 flush_dcache_all();
327
Michal Simek58f865f2015-04-15 13:36:40 +0200328 if (!strncmp(argv[1], "lockstep", 8)) {
Venkatesh Yadav Abbarapu5824c172023-06-08 08:51:52 +0530329 if (nr != ZYNQMP_CORE_RPU0) {
330 printf("Lockstep mode should run on ZYNQMP_CORE_RPU0\n");
331 return 1;
332 }
Michal Simek58f865f2015-04-15 13:36:40 +0200333 printf("R5 lockstep mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600334 set_r5_reset(nr, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200335 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600336 set_r5_halt_mode(nr, HALT, LOCK);
Michal Simek08adc902015-05-22 13:26:33 +0200337 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200338 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600339 release_r5_reset(nr, LOCK);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530340 dcache_disable();
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +0200341 write_tcm_boot_trampoline(nr, boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530342 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600343 set_r5_halt_mode(nr, RELEASE, LOCK);
Lukas Funkec6f90582022-10-28 14:15:47 +0200344 mark_r5_used(nr, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200345 } else if (!strncmp(argv[1], "split", 5)) {
346 printf("R5 split mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600347 set_r5_reset(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200348 set_r5_tcm_mode(SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600349 set_r5_halt_mode(nr, HALT, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530350 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200351 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600352 release_r5_reset(nr, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530353 dcache_disable();
Ashok Reddy Somab43d3cf2023-04-05 15:06:45 +0200354 write_tcm_boot_trampoline(nr, boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530355 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600356 set_r5_halt_mode(nr, RELEASE, SPLIT);
Lukas Funkec6f90582022-10-28 14:15:47 +0200357 mark_r5_used(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200358 } else {
359 printf("Unsupported mode\n");
360 return 1;
361 }
362 }
363
364 return 0;
365}