blob: 2891878973e5258da1d075c9b3283df6aadd3831 [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
Lukas Funkec6f90582022-10-28 14:15:47 +020045#define ZYNQMP_RPU0_USE_MASK BIT(1)
46#define ZYNQMP_RPU1_USE_MASK BIT(2)
47
Michal Simek58f865f2015-04-15 13:36:40 +020048int is_core_valid(unsigned int core)
49{
50 if (core < ZYNQMP_MAX_CORES)
51 return 1;
52
53 return 0;
54}
55
Michal Simek1669e182018-06-13 08:56:31 +020056int cpu_reset(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +020057{
58 puts("Feature is not implemented.\n");
59 return 0;
60}
61
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060062static void set_r5_halt_mode(u32 nr, u8 halt, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +020063{
64 u32 tmp;
65
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060066 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0) {
67 tmp = readl(&rpu_base->rpu0_cfg);
68 if (halt == HALT)
69 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
70 else
71 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
72 writel(tmp, &rpu_base->rpu0_cfg);
73 }
Michal Simek58f865f2015-04-15 13:36:40 +020074
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -060075 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1) {
Michal Simek58f865f2015-04-15 13:36:40 +020076 tmp = readl(&rpu_base->rpu1_cfg);
77 if (halt == HALT)
78 tmp &= ~ZYNQMP_RPU_CFG_CPU_HALT_MASK;
79 else
80 tmp |= ZYNQMP_RPU_CFG_CPU_HALT_MASK;
81 writel(tmp, &rpu_base->rpu1_cfg);
82 }
83}
84
85static void set_r5_tcm_mode(u8 mode)
86{
87 u32 tmp;
88
89 tmp = readl(&rpu_base->rpu_glbl_ctrl);
90 if (mode == LOCK) {
91 tmp &= ~ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK;
92 tmp |= ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK |
93 ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK;
94 } else {
95 tmp |= ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK;
96 tmp &= ~(ZYNQMP_RPU_GLBL_CTRL_TCM_COMB_MASK |
97 ZYNQMP_RPU_GLBL_CTRL_SLCLAMP_MASK);
98 }
99
100 writel(tmp, &rpu_base->rpu_glbl_ctrl);
101}
102
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600103static void set_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200104{
105 u32 tmp;
106
107 tmp = readl(&crlapb_base->rst_lpd_top);
Neal Fragerd929bbf2022-05-04 09:12:26 +0200108 if (mode == LOCK) {
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600109 tmp |= (ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
Neal Fragerd929bbf2022-05-04 09:12:26 +0200110 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK |
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600111 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Neal Fragerd929bbf2022-05-04 09:12:26 +0200112 } else {
113 if (nr == ZYNQMP_CORE_RPU0) {
114 tmp |= ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK;
115 if (tmp & ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK)
116 tmp |= ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK;
117 } else {
118 tmp |= ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK;
119 if (tmp & ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK)
120 tmp |= ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK;
121 }
122 }
Michal Simek58f865f2015-04-15 13:36:40 +0200123
124 writel(tmp, &crlapb_base->rst_lpd_top);
125}
126
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600127static void release_r5_reset(u32 nr, u8 mode)
Michal Simek58f865f2015-04-15 13:36:40 +0200128{
129 u32 tmp;
130
131 tmp = readl(&crlapb_base->rst_lpd_top);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600132 if (mode == LOCK || nr == ZYNQMP_CORE_RPU0)
133 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
134 ZYNQMP_CRLAPB_RST_LPD_R50_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200135
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600136 if (mode == LOCK || nr == ZYNQMP_CORE_RPU1)
137 tmp &= ~(ZYNQMP_CRLAPB_RST_LPD_AMBA_RST_MASK |
138 ZYNQMP_CRLAPB_RST_LPD_R51_RST_MASK);
Michal Simek58f865f2015-04-15 13:36:40 +0200139
140 writel(tmp, &crlapb_base->rst_lpd_top);
141}
142
143static void enable_clock_r5(void)
144{
145 u32 tmp;
146
147 tmp = readl(&crlapb_base->cpu_r5_ctrl);
148 tmp |= ZYNQMP_CRLAPB_CPU_R5_CTRL_CLKACT_MASK;
149 writel(tmp, &crlapb_base->cpu_r5_ctrl);
150
151 /* Give some delay for clock
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400152 * to propagate */
Michal Simek58f865f2015-04-15 13:36:40 +0200153 udelay(0x500);
154}
155
Neal Fragerd929bbf2022-05-04 09:12:26 +0200156static int check_r5_mode(void)
157{
158 u32 tmp;
159
160 tmp = readl(&rpu_base->rpu_glbl_ctrl);
161 if (tmp & ZYNQMP_RPU_GLBL_CTRL_SPLIT_LOCK_MASK)
162 return SPLIT;
163
164 return LOCK;
165}
166
Michal Simek1669e182018-06-13 08:56:31 +0200167int cpu_disable(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200168{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530169 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200170 u32 val = readl(&crfapb_base->rst_fpd_apu);
171 val |= 1 << nr;
172 writel(val, &crfapb_base->rst_fpd_apu);
173 } else {
Neal Fragerd929bbf2022-05-04 09:12:26 +0200174 set_r5_reset(nr, check_r5_mode());
Michal Simek58f865f2015-04-15 13:36:40 +0200175 }
176
177 return 0;
178}
179
Michal Simek1669e182018-06-13 08:56:31 +0200180int cpu_status(u32 nr)
Michal Simek58f865f2015-04-15 13:36:40 +0200181{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530182 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200183 u32 addr_low = readl(((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
184 u32 addr_high = readl(((u8 *)&apu_base->rvbar_addr0_h) +
185 nr * 8);
186 u32 val = readl(&crfapb_base->rst_fpd_apu);
187 val &= 1 << nr;
188 printf("APU CPU%d %s - starting address HI: %x, LOW: %x\n",
189 nr, val ? "OFF" : "ON" , addr_high, addr_low);
190 } else {
191 u32 val = readl(&crlapb_base->rst_lpd_top);
192 val &= 1 << (nr - 4);
193 printf("RPU CPU%d %s\n", nr - 4, val ? "OFF" : "ON");
194 }
195
196 return 0;
197}
198
199static void set_r5_start(u8 high)
200{
201 u32 tmp;
202
203 tmp = readl(&rpu_base->rpu0_cfg);
204 if (high)
205 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
206 else
207 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
208 writel(tmp, &rpu_base->rpu0_cfg);
209
210 tmp = readl(&rpu_base->rpu1_cfg);
211 if (high)
212 tmp |= ZYNQMP_RPU_CFG_HIVEC_MASK;
213 else
214 tmp &= ~ZYNQMP_RPU_CFG_HIVEC_MASK;
215 writel(tmp, &rpu_base->rpu1_cfg);
216}
217
Michal Simekf5005ce2015-05-22 13:28:23 +0200218static void write_tcm_boot_trampoline(u32 boot_addr)
219{
220 if (boot_addr) {
221 /*
222 * Boot trampoline is simple ASM code below.
223 *
224 * b over;
225 * label:
226 * .word 0
227 * over: ldr r0, =label
228 * ldr r1, [r0]
229 * bx r1
230 */
231 debug("Write boot trampoline for %x\n", boot_addr);
232 writel(0xea000000, ZYNQMP_TCM_START_ADDRESS);
233 writel(boot_addr, ZYNQMP_TCM_START_ADDRESS + 0x4);
234 writel(0xe59f0004, ZYNQMP_TCM_START_ADDRESS + 0x8);
235 writel(0xe5901000, ZYNQMP_TCM_START_ADDRESS + 0xc);
236 writel(0xe12fff11, ZYNQMP_TCM_START_ADDRESS + 0x10);
237 writel(0x00000004, ZYNQMP_TCM_START_ADDRESS + 0x14); // address for
238 }
239}
240
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530241void initialize_tcm(bool mode)
242{
243 if (!mode) {
244 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600245 set_r5_halt_mode(ZYNQMP_CORE_RPU0, HALT, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530246 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600247 release_r5_reset(ZYNQMP_CORE_RPU0, LOCK);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530248 } else {
249 set_r5_tcm_mode(SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600250 set_r5_halt_mode(ZYNQMP_CORE_RPU1, HALT, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530251 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600252 release_r5_reset(ZYNQMP_CORE_RPU1, SPLIT);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530253 }
Lukas Funkec6f90582022-10-28 14:15:47 +0200254}
255
256static void mark_r5_used(u32 nr, u8 mode)
257{
258 u32 mask = 0;
259
260 if (mode == LOCK) {
261 mask = ZYNQMP_RPU0_USE_MASK | ZYNQMP_RPU1_USE_MASK;
262 } else {
263 switch (nr) {
264 case ZYNQMP_CORE_RPU0:
265 mask = ZYNQMP_RPU0_USE_MASK;
266 break;
267 case ZYNQMP_CORE_RPU1:
268 mask = ZYNQMP_RPU1_USE_MASK;
269 break;
270 default:
271 return;
272 }
273 }
274 zynqmp_mmio_write((ulong)&pmu_base->gen_storage4, mask, mask);
Siva Durga Prasad Paladugu5e2a9072017-07-13 19:01:09 +0530275}
276
Simon Glassed38aef2020-05-10 11:40:03 -0600277int cpu_release(u32 nr, int argc, char *const argv[])
Michal Simek58f865f2015-04-15 13:36:40 +0200278{
Venkatesh Yadav Abbarapuae8bc3d2022-10-04 11:04:54 +0530279 if (nr <= ZYNQMP_CORE_APU3) {
Michal Simek58f865f2015-04-15 13:36:40 +0200280 u64 boot_addr = simple_strtoull(argv[0], NULL, 16);
281 /* HIGH */
282 writel((u32)(boot_addr >> 32),
283 ((u8 *)&apu_base->rvbar_addr0_h) + nr * 8);
284 /* LOW */
285 writel((u32)(boot_addr & ZYNQMP_BOOTADDR_HIGH_MASK),
286 ((u8 *)&apu_base->rvbar_addr0_l) + nr * 8);
287
288 u32 val = readl(&crfapb_base->rst_fpd_apu);
289 val &= ~(1 << nr);
290 writel(val, &crfapb_base->rst_fpd_apu);
291 } else {
292 if (argc != 2) {
293 printf("Invalid number of arguments to release.\n");
294 printf("<addr> <mode>-Start addr lockstep or split\n");
295 return 1;
296 }
297
Simon Glass3ff49ec2021-07-24 09:03:29 -0600298 u32 boot_addr = hextoul(argv[0], NULL);
Michal Simekf5005ce2015-05-22 13:28:23 +0200299 u32 boot_addr_uniq = 0;
Michal Simek58f865f2015-04-15 13:36:40 +0200300 if (!(boot_addr == ZYNQMP_R5_LOVEC_ADDR ||
301 boot_addr == ZYNQMP_R5_HIVEC_ADDR)) {
Michal Simekf5005ce2015-05-22 13:28:23 +0200302 printf("Using TCM jump trampoline for address 0x%x\n",
303 boot_addr);
304 /* Save boot address for later usage */
305 boot_addr_uniq = boot_addr;
306 /*
307 * R5 needs to start from LOVEC at TCM
308 * OCM will be probably occupied by ATF
309 */
310 boot_addr = ZYNQMP_R5_LOVEC_ADDR;
Michal Simek58f865f2015-04-15 13:36:40 +0200311 }
312
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530313 /*
314 * Since we don't know where the user may have loaded the image
315 * for an R5 we have to flush all the data cache to ensure
316 * the R5 sees it.
317 */
318 flush_dcache_all();
319
Michal Simek58f865f2015-04-15 13:36:40 +0200320 if (!strncmp(argv[1], "lockstep", 8)) {
321 printf("R5 lockstep mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600322 set_r5_reset(nr, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200323 set_r5_tcm_mode(LOCK);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600324 set_r5_halt_mode(nr, HALT, LOCK);
Michal Simek08adc902015-05-22 13:26:33 +0200325 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200326 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600327 release_r5_reset(nr, LOCK);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530328 dcache_disable();
Michal Simekf5005ce2015-05-22 13:28:23 +0200329 write_tcm_boot_trampoline(boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530330 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600331 set_r5_halt_mode(nr, RELEASE, LOCK);
Lukas Funkec6f90582022-10-28 14:15:47 +0200332 mark_r5_used(nr, LOCK);
Michal Simek58f865f2015-04-15 13:36:40 +0200333 } else if (!strncmp(argv[1], "split", 5)) {
334 printf("R5 split mode\n");
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600335 set_r5_reset(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200336 set_r5_tcm_mode(SPLIT);
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600337 set_r5_halt_mode(nr, HALT, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530338 set_r5_start(boot_addr);
Michal Simek58f865f2015-04-15 13:36:40 +0200339 enable_clock_r5();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600340 release_r5_reset(nr, SPLIT);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530341 dcache_disable();
Michal Simekf5005ce2015-05-22 13:28:23 +0200342 write_tcm_boot_trampoline(boot_addr_uniq);
Siva Durga Prasad Paladugue0a1f1e2017-08-01 16:24:52 +0530343 dcache_enable();
Ashok Reddy Soma86d212e2021-04-15 05:12:15 -0600344 set_r5_halt_mode(nr, RELEASE, SPLIT);
Lukas Funkec6f90582022-10-28 14:15:47 +0200345 mark_r5_used(nr, SPLIT);
Michal Simek58f865f2015-04-15 13:36:40 +0200346 } else {
347 printf("Unsupported mode\n");
348 return 1;
349 }
350 }
351
352 return 0;
353}