1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
5 */
6
7 #include <common.h>
8 #include <config.h>
9 #include <cpu_func.h>
10 #include <env.h>
11 #include <init.h>
12 #include <irq_func.h>
13 #include <linux/printk.h>
14 #include <linux/kernel.h>
15 #include <linux/io.h>
16 #include <asm/arcregs.h>
17 #include <fdt_support.h>
18 #include <dwmmc.h>
19 #include <malloc.h>
20 #include <usb.h>
21
22 #include "clk-lib.h"
23 #include "env-lib.h"
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 #define ALL_CPU_MASK GENMASK(NR_CPUS - 1, 0)
28 #define MASTER_CPU_ID 0
29 #define APERTURE_SHIFT 28
30 #define NO_CCM 0x10
31 #define SLAVE_CPU_READY 0x12345678
32 #define BOOTSTAGE_1 1 /* after SP, FP setup, before HW init */
33 #define BOOTSTAGE_2 2 /* after HW init, before self halt */
34 #define BOOTSTAGE_3 3 /* after self halt */
35 #define BOOTSTAGE_4 4 /* before app launch */
36 #define BOOTSTAGE_5 5 /* after app launch, unreachable */
37
38 #define RESET_VECTOR_ADDR 0x0
39
40 #define CREG_BASE (ARC_PERIPHERAL_BASE + 0x1000)
41 #define CREG_CPU_START (CREG_BASE + 0x400)
42 #define CREG_CPU_START_MASK 0xF
43
44 #define SDIO_BASE (ARC_PERIPHERAL_BASE + 0xA000)
45 #define SDIO_UHS_REG_EXT (SDIO_BASE + 0x108)
46 #define SDIO_UHS_REG_EXT_DIV_2 (2 << 30)
47
48 /* Uncached access macros */
49 #define arc_read_uncached_32(ptr) \
50 ({ \
51 unsigned int __ret; \
52 __asm__ __volatile__( \
53 " ld.di %0, [%1] \n" \
54 : "=r"(__ret) \
55 : "r"(ptr)); \
56 __ret; \
57 })
58
59 #define arc_write_uncached_32(ptr, data)\
60 ({ \
61 __asm__ __volatile__( \
62 " st.di %0, [%1] \n" \
63 : \
64 : "r"(data), "r"(ptr)); \
65 })
66
67 struct hsdk_env_core_ctl {
68 u32_env entry[NR_CPUS];
69 u32_env iccm[NR_CPUS];
70 u32_env dccm[NR_CPUS];
71 };
72
73 struct hsdk_env_common_ctl {
74 bool halt_on_boot;
75 u32_env core_mask;
76 u32_env cpu_freq;
77 u32_env axi_freq;
78 u32_env tun_freq;
79 u32_env nvlim;
80 u32_env icache;
81 u32_env dcache;
82 };
83
84 /*
85 * Uncached cross-cpu structure. All CPUs must access to this structure fields
86 * only with arc_read_uncached_32() / arc_write_uncached_32() accessors (which
87 * implement ld.di / st.di instructions). Simultaneous cached and uncached
88 * access to this area will lead to data loss.
89 * We flush all data caches in board_early_init_r() as we don't want to have
90 * any dirty line in L1d$ or SL$ in this area.
91 */
92 struct hsdk_cross_cpu {
93 /* slave CPU ready flag */
94 u32 ready_flag;
95 /* address of the area, which can be used for stack by slave CPU */
96 u32 stack_ptr;
97 /* slave CPU status - bootstage number */
98 s32 status[NR_CPUS];
99
100 /*
101 * Slave CPU data - it is copy of corresponding fields in
102 * hsdk_env_core_ctl and hsdk_env_common_ctl structures which are
103 * required for slave CPUs initialization.
104 * This fields can be populated by copying from hsdk_env_core_ctl
105 * and hsdk_env_common_ctl structures with sync_cross_cpu_data()
106 * function.
107 */
108 u32 entry[NR_CPUS];
109 u32 iccm[NR_CPUS];
110 u32 dccm[NR_CPUS];
111
112 u32 core_mask;
113 u32 icache;
114 u32 dcache;
115
116 u8 cache_padding[ARCH_DMA_MINALIGN];
117 } __aligned(ARCH_DMA_MINALIGN);
118
119 /* Place for slave CPUs temporary stack */
120 static u32 slave_stack[256 * NR_CPUS] __aligned(ARCH_DMA_MINALIGN);
121
122 static struct hsdk_env_common_ctl env_common = {};
123 static struct hsdk_env_core_ctl env_core = {};
124 static struct hsdk_cross_cpu cross_cpu_data;
125
126 static const struct env_map_common env_map_common[] = {
127 { "core_mask", ENV_HEX, true, 0x1, 0xF, &env_common.core_mask },
128 { "non_volatile_limit", ENV_HEX, true, 0, 0xF, &env_common.nvlim },
129 { "icache_ena", ENV_HEX, true, 0, 1, &env_common.icache },
130 { "dcache_ena", ENV_HEX, true, 0, 1, &env_common.dcache },
131 {}
132 };
133
134 static const struct env_map_common env_map_clock[] = {
135 { "cpu_freq", ENV_DEC, false, 100, 1000, &env_common.cpu_freq },
136 { "axi_freq", ENV_DEC, false, 200, 800, &env_common.axi_freq },
137 { "tun_freq", ENV_DEC, false, 0, 150, &env_common.tun_freq },
138 {}
139 };
140
141 static const struct env_map_percpu env_map_core[] = {
142 { "core_iccm", ENV_HEX, true, {NO_CCM, 0, NO_CCM, 0}, {NO_CCM, 0xF, NO_CCM, 0xF}, &env_core.iccm },
143 { "core_dccm", ENV_HEX, true, {NO_CCM, 0, NO_CCM, 0}, {NO_CCM, 0xF, NO_CCM, 0xF}, &env_core.dccm },
144 {}
145 };
146
147 static const struct env_map_common env_map_mask[] = {
148 { "core_mask", ENV_HEX, false, 0x1, 0xF, &env_common.core_mask },
149 {}
150 };
151
152 static const struct env_map_percpu env_map_go[] = {
153 { "core_entry", ENV_HEX, true, {0, 0, 0, 0}, {U32_MAX, U32_MAX, U32_MAX, U32_MAX}, &env_core.entry },
154 {}
155 };
156
sync_cross_cpu_data(void)157 static void sync_cross_cpu_data(void)
158 {
159 u32 value;
160
161 for (u32 i = 0; i < NR_CPUS; i++) {
162 value = env_core.entry[i].val;
163 arc_write_uncached_32(&cross_cpu_data.entry[i], value);
164 }
165
166 for (u32 i = 0; i < NR_CPUS; i++) {
167 value = env_core.iccm[i].val;
168 arc_write_uncached_32(&cross_cpu_data.iccm[i], value);
169 }
170
171 for (u32 i = 0; i < NR_CPUS; i++) {
172 value = env_core.dccm[i].val;
173 arc_write_uncached_32(&cross_cpu_data.dccm[i], value);
174 }
175
176 value = env_common.core_mask.val;
177 arc_write_uncached_32(&cross_cpu_data.core_mask, value);
178
179 value = env_common.icache.val;
180 arc_write_uncached_32(&cross_cpu_data.icache, value);
181
182 value = env_common.dcache.val;
183 arc_write_uncached_32(&cross_cpu_data.dcache, value);
184 }
185
186 /* Can be used only on master CPU */
is_cpu_used(u32 cpu_id)187 static bool is_cpu_used(u32 cpu_id)
188 {
189 return !!(env_common.core_mask.val & BIT(cpu_id));
190 }
191
192 /* TODO: add ICCM BCR and DCCM BCR runtime check */
init_slave_cpu_func(u32 core)193 static void init_slave_cpu_func(u32 core)
194 {
195 u32 val;
196
197 /* Remap ICCM to another memory region if it exists */
198 val = arc_read_uncached_32(&cross_cpu_data.iccm[core]);
199 if (val != NO_CCM)
200 write_aux_reg(ARC_AUX_ICCM_BASE, val << APERTURE_SHIFT);
201
202 /* Remap DCCM to another memory region if it exists */
203 val = arc_read_uncached_32(&cross_cpu_data.dccm[core]);
204 if (val != NO_CCM)
205 write_aux_reg(ARC_AUX_DCCM_BASE, val << APERTURE_SHIFT);
206
207 if (arc_read_uncached_32(&cross_cpu_data.icache))
208 icache_enable();
209 else
210 icache_disable();
211
212 if (arc_read_uncached_32(&cross_cpu_data.dcache))
213 dcache_enable();
214 else
215 dcache_disable();
216 }
217
init_cluster_nvlim(void)218 static void init_cluster_nvlim(void)
219 {
220 u32 val = env_common.nvlim.val << APERTURE_SHIFT;
221
222 flush_dcache_all();
223 write_aux_reg(ARC_AUX_NON_VOLATILE_LIMIT, val);
224 write_aux_reg(AUX_AUX_CACHE_LIMIT, val);
225 flush_n_invalidate_dcache_all();
226 }
227
init_master_icache(void)228 static void init_master_icache(void)
229 {
230 if (icache_status()) {
231 /* I$ is enabled - we need to disable it */
232 if (!env_common.icache.val)
233 icache_disable();
234 } else {
235 /* I$ is disabled - we need to enable it */
236 if (env_common.icache.val) {
237 icache_enable();
238
239 /* invalidate I$ right after enable */
240 invalidate_icache_all();
241 }
242 }
243 }
244
init_master_dcache(void)245 static void init_master_dcache(void)
246 {
247 if (dcache_status()) {
248 /* D$ is enabled - we need to disable it */
249 if (!env_common.dcache.val)
250 dcache_disable();
251 } else {
252 /* D$ is disabled - we need to enable it */
253 if (env_common.dcache.val)
254 dcache_enable();
255
256 /* TODO: probably we need ti invalidate D$ right after enable */
257 }
258 }
259
cleanup_before_go(void)260 static int cleanup_before_go(void)
261 {
262 disable_interrupts();
263 sync_n_cleanup_cache_all();
264
265 return 0;
266 }
267
slave_cpu_set_boot_addr(u32 addr)268 void slave_cpu_set_boot_addr(u32 addr)
269 {
270 /* All cores have reset vector pointing to 0 */
271 writel(addr, (void __iomem *)RESET_VECTOR_ADDR);
272
273 /* Make sure other cores see written value in memory */
274 sync_n_cleanup_cache_all();
275 }
276
halt_this_cpu(void)277 static inline void halt_this_cpu(void)
278 {
279 __builtin_arc_flag(1);
280 }
281
smp_kick_cpu_x(u32 cpu_id)282 static void smp_kick_cpu_x(u32 cpu_id)
283 {
284 int cmd = readl((void __iomem *)CREG_CPU_START);
285
286 if (cpu_id > NR_CPUS)
287 return;
288
289 cmd &= ~CREG_CPU_START_MASK;
290 cmd |= (1 << cpu_id);
291 writel(cmd, (void __iomem *)CREG_CPU_START);
292 }
293
prepare_cpu_ctart_reg(void)294 static u32 prepare_cpu_ctart_reg(void)
295 {
296 int cmd = readl((void __iomem *)CREG_CPU_START);
297
298 cmd &= ~CREG_CPU_START_MASK;
299
300 return cmd | env_common.core_mask.val;
301 }
302
303 /* slave CPU entry for configuration */
hsdk_core_init_f(void)304 __attribute__((naked, noreturn, flatten)) noinline void hsdk_core_init_f(void)
305 {
306 __asm__ __volatile__(
307 "ld.di r8, [%0]\n"
308 "mov %%sp, r8\n"
309 "mov %%fp, %%sp\n"
310 : /* no output */
311 : "r" (&cross_cpu_data.stack_ptr));
312
313 invalidate_icache_all();
314
315 arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_1);
316 init_slave_cpu_func(CPU_ID_GET());
317
318 arc_write_uncached_32(&cross_cpu_data.ready_flag, SLAVE_CPU_READY);
319 arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_2);
320
321 /* Halt the processor until the master kick us again */
322 halt_this_cpu();
323
324 /*
325 * 3 NOPs after FLAG 1 instruction are no longer required for ARCv2
326 * cores but we leave them for gebug purposes.
327 */
328 __builtin_arc_nop();
329 __builtin_arc_nop();
330 __builtin_arc_nop();
331
332 arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_3);
333
334 /* get the updated entry - invalidate i$ */
335 invalidate_icache_all();
336
337 arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_4);
338
339 /* Run our program */
340 ((void (*)(void))(arc_read_uncached_32(&cross_cpu_data.entry[CPU_ID_GET()])))();
341
342 /* This bootstage is unreachable as we don't return from app we launch */
343 arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_5);
344
345 /* Something went terribly wrong */
346 while (true)
347 halt_this_cpu();
348 }
349
clear_cross_cpu_data(void)350 static void clear_cross_cpu_data(void)
351 {
352 arc_write_uncached_32(&cross_cpu_data.ready_flag, 0);
353 arc_write_uncached_32(&cross_cpu_data.stack_ptr, 0);
354
355 for (u32 i = 0; i < NR_CPUS; i++)
356 arc_write_uncached_32(&cross_cpu_data.status[i], 0);
357 }
358
do_init_slave_cpu(u32 cpu_id)359 static noinline void do_init_slave_cpu(u32 cpu_id)
360 {
361 /* attempts number for check clave CPU ready_flag */
362 u32 attempts = 100;
363 u32 stack_ptr = (u32)(slave_stack + (64 * cpu_id));
364
365 if (cpu_id >= NR_CPUS)
366 return;
367
368 arc_write_uncached_32(&cross_cpu_data.ready_flag, 0);
369
370 /* Use global unique place for each slave cpu stack */
371 arc_write_uncached_32(&cross_cpu_data.stack_ptr, stack_ptr);
372
373 debug("CPU %u: stack pool base: %p\n", cpu_id, slave_stack);
374 debug("CPU %u: current slave stack base: %x\n", cpu_id, stack_ptr);
375 slave_cpu_set_boot_addr((u32)hsdk_core_init_f);
376
377 smp_kick_cpu_x(cpu_id);
378
379 debug("CPU %u: cross-cpu flag: %x [before timeout]\n", cpu_id,
380 arc_read_uncached_32(&cross_cpu_data.ready_flag));
381
382 while (!arc_read_uncached_32(&cross_cpu_data.ready_flag) && attempts--)
383 mdelay(10);
384
385 /* Just to be sure that slave cpu is halted after it set ready_flag */
386 mdelay(20);
387
388 /*
389 * Only print error here if we reach timeout as there is no option to
390 * halt slave cpu (or check that slave cpu is halted)
391 */
392 if (!attempts)
393 pr_err("CPU %u is not responding after init!\n", cpu_id);
394
395 /* Check current stage of slave cpu */
396 if (arc_read_uncached_32(&cross_cpu_data.status[cpu_id]) != BOOTSTAGE_2)
397 pr_err("CPU %u status is unexpected: %d\n", cpu_id,
398 arc_read_uncached_32(&cross_cpu_data.status[cpu_id]));
399
400 debug("CPU %u: cross-cpu flag: %x [after timeout]\n", cpu_id,
401 arc_read_uncached_32(&cross_cpu_data.ready_flag));
402 debug("CPU %u: status: %d [after timeout]\n", cpu_id,
403 arc_read_uncached_32(&cross_cpu_data.status[cpu_id]));
404 }
405
do_init_slave_cpus(void)406 static void do_init_slave_cpus(void)
407 {
408 clear_cross_cpu_data();
409 sync_cross_cpu_data();
410
411 debug("cross_cpu_data location: %#x\n", (u32)&cross_cpu_data);
412
413 for (u32 i = MASTER_CPU_ID + 1; i < NR_CPUS; i++)
414 if (is_cpu_used(i))
415 do_init_slave_cpu(i);
416 }
417
do_init_master_cpu(void)418 static void do_init_master_cpu(void)
419 {
420 /*
421 * Setup master caches even if master isn't used as we want to use
422 * same cache configuration on all running CPUs
423 */
424 init_master_icache();
425 init_master_dcache();
426 }
427
428 enum hsdk_axi_masters {
429 M_HS_CORE = 0,
430 M_HS_RTT,
431 M_AXI_TUN,
432 M_HDMI_VIDEO,
433 M_HDMI_AUDIO,
434 M_USB_HOST,
435 M_ETHERNET,
436 M_SDIO,
437 M_GPU,
438 M_DMAC_0,
439 M_DMAC_1,
440 M_DVFS
441 };
442
443 #define UPDATE_VAL 1
444
445 /*
446 * m master AXI_M_m_SLV0 AXI_M_m_SLV1 AXI_M_m_OFFSET0 AXI_M_m_OFFSET1
447 * 0 HS (CBU) 0x11111111 0x63111111 0xFEDCBA98 0x0E543210
448 * 1 HS (RTT) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
449 * 2 AXI Tunnel 0x88888888 0x88888888 0xFEDCBA98 0x76543210
450 * 3 HDMI-VIDEO 0x77777777 0x77777777 0xFEDCBA98 0x76543210
451 * 4 HDMI-ADUIO 0x77777777 0x77777777 0xFEDCBA98 0x76543210
452 * 5 USB-HOST 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
453 * 6 ETHERNET 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
454 * 7 SDIO 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
455 * 8 GPU 0x77777777 0x77777777 0xFEDCBA98 0x76543210
456 * 9 DMAC (port #1) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
457 * 10 DMAC (port #2) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
458 * 11 DVFS 0x00000000 0x60000000 0x00000000 0x00000000
459 *
460 * Please read ARC HS Development IC Specification, section 17.2 for more
461 * information about apertures configuration.
462 * NOTE: we intentionally modify default settings in U-boot. Default settings
463 * are specified in "Table 111 CREG Address Decoder register reset values".
464 */
465
466 #define CREG_AXI_M_SLV0(m) ((void __iomem *)(CREG_BASE + 0x020 * (m)))
467 #define CREG_AXI_M_SLV1(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x004))
468 #define CREG_AXI_M_OFT0(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x008))
469 #define CREG_AXI_M_OFT1(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x00C))
470 #define CREG_AXI_M_UPDT(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x014))
471
472 #define CREG_AXI_M_HS_CORE_BOOT ((void __iomem *)(CREG_BASE + 0x010))
473
474 #define CREG_PAE ((void __iomem *)(CREG_BASE + 0x180))
475 #define CREG_PAE_UPDT ((void __iomem *)(CREG_BASE + 0x194))
476
init_memory_bridge(void)477 void init_memory_bridge(void)
478 {
479 u32 reg;
480
481 /*
482 * M_HS_CORE has one unic register - BOOT.
483 * We need to clean boot mirror (BOOT[1:0]) bits in them.
484 */
485 reg = readl(CREG_AXI_M_HS_CORE_BOOT) & (~0x3);
486 writel(reg, CREG_AXI_M_HS_CORE_BOOT);
487 writel(0x11111111, CREG_AXI_M_SLV0(M_HS_CORE));
488 writel(0x63111111, CREG_AXI_M_SLV1(M_HS_CORE));
489 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HS_CORE));
490 writel(0x0E543210, CREG_AXI_M_OFT1(M_HS_CORE));
491 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HS_CORE));
492
493 writel(0x77777777, CREG_AXI_M_SLV0(M_HS_RTT));
494 writel(0x77777777, CREG_AXI_M_SLV1(M_HS_RTT));
495 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HS_RTT));
496 writel(0x76543210, CREG_AXI_M_OFT1(M_HS_RTT));
497 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HS_RTT));
498
499 writel(0x88888888, CREG_AXI_M_SLV0(M_AXI_TUN));
500 writel(0x88888888, CREG_AXI_M_SLV1(M_AXI_TUN));
501 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_AXI_TUN));
502 writel(0x76543210, CREG_AXI_M_OFT1(M_AXI_TUN));
503 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_AXI_TUN));
504
505 writel(0x77777777, CREG_AXI_M_SLV0(M_HDMI_VIDEO));
506 writel(0x77777777, CREG_AXI_M_SLV1(M_HDMI_VIDEO));
507 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HDMI_VIDEO));
508 writel(0x76543210, CREG_AXI_M_OFT1(M_HDMI_VIDEO));
509 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HDMI_VIDEO));
510
511 writel(0x77777777, CREG_AXI_M_SLV0(M_HDMI_AUDIO));
512 writel(0x77777777, CREG_AXI_M_SLV1(M_HDMI_AUDIO));
513 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HDMI_AUDIO));
514 writel(0x76543210, CREG_AXI_M_OFT1(M_HDMI_AUDIO));
515 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HDMI_AUDIO));
516
517 writel(0x77777777, CREG_AXI_M_SLV0(M_USB_HOST));
518 writel(0x77999999, CREG_AXI_M_SLV1(M_USB_HOST));
519 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_USB_HOST));
520 writel(0x76DCBA98, CREG_AXI_M_OFT1(M_USB_HOST));
521 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_USB_HOST));
522
523 writel(0x77777777, CREG_AXI_M_SLV0(M_ETHERNET));
524 writel(0x77999999, CREG_AXI_M_SLV1(M_ETHERNET));
525 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_ETHERNET));
526 writel(0x76DCBA98, CREG_AXI_M_OFT1(M_ETHERNET));
527 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_ETHERNET));
528
529 writel(0x77777777, CREG_AXI_M_SLV0(M_SDIO));
530 writel(0x77999999, CREG_AXI_M_SLV1(M_SDIO));
531 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_SDIO));
532 writel(0x76DCBA98, CREG_AXI_M_OFT1(M_SDIO));
533 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_SDIO));
534
535 writel(0x77777777, CREG_AXI_M_SLV0(M_GPU));
536 writel(0x77777777, CREG_AXI_M_SLV1(M_GPU));
537 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_GPU));
538 writel(0x76543210, CREG_AXI_M_OFT1(M_GPU));
539 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_GPU));
540
541 writel(0x77777777, CREG_AXI_M_SLV0(M_DMAC_0));
542 writel(0x77777777, CREG_AXI_M_SLV1(M_DMAC_0));
543 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_DMAC_0));
544 writel(0x76543210, CREG_AXI_M_OFT1(M_DMAC_0));
545 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DMAC_0));
546
547 writel(0x77777777, CREG_AXI_M_SLV0(M_DMAC_1));
548 writel(0x77777777, CREG_AXI_M_SLV1(M_DMAC_1));
549 writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_DMAC_1));
550 writel(0x76543210, CREG_AXI_M_OFT1(M_DMAC_1));
551 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DMAC_1));
552
553 writel(0x00000000, CREG_AXI_M_SLV0(M_DVFS));
554 writel(0x60000000, CREG_AXI_M_SLV1(M_DVFS));
555 writel(0x00000000, CREG_AXI_M_OFT0(M_DVFS));
556 writel(0x00000000, CREG_AXI_M_OFT1(M_DVFS));
557 writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DVFS));
558
559 writel(0x00000000, CREG_PAE);
560 writel(UPDATE_VAL, CREG_PAE_UPDT);
561 }
562
setup_clocks(void)563 static void setup_clocks(void)
564 {
565 ulong rate;
566
567 /* Setup CPU clock */
568 if (env_common.cpu_freq.set) {
569 rate = env_common.cpu_freq.val;
570 soc_clk_ctl("cpu-clk", &rate, CLK_ON | CLK_SET | CLK_MHZ);
571 }
572
573 /* Setup TUN clock */
574 if (env_common.tun_freq.set) {
575 rate = env_common.tun_freq.val;
576 if (rate)
577 soc_clk_ctl("tun-clk", &rate, CLK_ON | CLK_SET | CLK_MHZ);
578 else
579 soc_clk_ctl("tun-clk", NULL, CLK_OFF);
580 }
581
582 if (env_common.axi_freq.set) {
583 rate = env_common.axi_freq.val;
584 soc_clk_ctl("axi-clk", &rate, CLK_SET | CLK_ON | CLK_MHZ);
585 }
586 }
587
do_init_cluster(void)588 static void do_init_cluster(void)
589 {
590 /*
591 * A multi-core ARC HS configuration always includes only one
592 * ARC_AUX_NON_VOLATILE_LIMIT register, which is shared by all the
593 * cores.
594 */
595 init_cluster_nvlim();
596 }
597
check_master_cpu_id(void)598 static int check_master_cpu_id(void)
599 {
600 if (CPU_ID_GET() == MASTER_CPU_ID)
601 return 0;
602
603 pr_err("u-boot runs on non-master cpu with id: %lu\n", CPU_ID_GET());
604
605 return -ENOENT;
606 }
607
prepare_cpus(void)608 static noinline int prepare_cpus(void)
609 {
610 int ret;
611
612 ret = check_master_cpu_id();
613 if (ret)
614 return ret;
615
616 ret = envs_process_and_validate(env_map_common, env_map_core, is_cpu_used);
617 if (ret)
618 return ret;
619
620 printf("CPU start mask is %#x\n", env_common.core_mask.val);
621
622 do_init_slave_cpus();
623 do_init_master_cpu();
624 do_init_cluster();
625
626 return 0;
627 }
628
hsdk_go_run(u32 cpu_start_reg)629 static int hsdk_go_run(u32 cpu_start_reg)
630 {
631 /* Cleanup caches, disable interrupts */
632 cleanup_before_go();
633
634 if (env_common.halt_on_boot)
635 halt_this_cpu();
636
637 /*
638 * 3 NOPs after FLAG 1 instruction are no longer required for ARCv2
639 * cores but we leave them for gebug purposes.
640 */
641 __builtin_arc_nop();
642 __builtin_arc_nop();
643 __builtin_arc_nop();
644
645 /* Kick chosen slave CPUs */
646 writel(cpu_start_reg, (void __iomem *)CREG_CPU_START);
647
648 if (is_cpu_used(MASTER_CPU_ID))
649 ((void (*)(void))(env_core.entry[MASTER_CPU_ID].val))();
650 else
651 halt_this_cpu();
652
653 pr_err("u-boot still runs on cpu [%ld]\n", CPU_ID_GET());
654
655 /*
656 * We will never return after executing our program if master cpu used
657 * otherwise halt master cpu manually.
658 */
659 while (true)
660 halt_this_cpu();
661
662 return 0;
663 }
664
board_prep_linux(bootm_headers_t * images)665 int board_prep_linux(bootm_headers_t *images)
666 {
667 int ret, ofst;
668 char mask[15];
669
670 ret = envs_read_validate_common(env_map_mask);
671 if (ret)
672 return ret;
673
674 /* Rollback to default values */
675 if (!env_common.core_mask.set) {
676 env_common.core_mask.val = ALL_CPU_MASK;
677 env_common.core_mask.set = true;
678 }
679
680 printf("CPU start mask is %#x\n", env_common.core_mask.val);
681
682 if (!is_cpu_used(MASTER_CPU_ID))
683 pr_err("ERR: try to launch linux with CPU[0] disabled! It doesn't work for ARC.\n");
684
685 /*
686 * If we want to launch linux on all CPUs we don't need to patch
687 * linux DTB as it is default configuration
688 */
689 if (env_common.core_mask.val == ALL_CPU_MASK)
690 return 0;
691
692 if (!IMAGE_ENABLE_OF_LIBFDT || !images->ft_len) {
693 pr_err("WARN: core_mask setup will work properly only with external DTB!\n");
694 return 0;
695 }
696
697 /* patch '/possible-cpus' property according to cpu mask */
698 ofst = fdt_path_offset(images->ft_addr, "/");
699 sprintf(mask, "%s%s%s%s",
700 is_cpu_used(0) ? "0," : "",
701 is_cpu_used(1) ? "1," : "",
702 is_cpu_used(2) ? "2," : "",
703 is_cpu_used(3) ? "3," : "");
704 ret = fdt_setprop_string(images->ft_addr, ofst, "possible-cpus", mask);
705 /*
706 * If we failed to patch '/possible-cpus' property we don't need break
707 * linux loading process: kernel will handle it but linux will print
708 * warning like "Timeout: CPU1 FAILED to comeup !!!".
709 * So warn here about error, but return 0 like no error had occurred.
710 */
711 if (ret)
712 pr_err("WARN: failed to patch '/possible-cpus' property, ret=%d\n",
713 ret);
714
715 return 0;
716 }
717
board_jump_and_run(ulong entry,int zero,int arch,uint params)718 void board_jump_and_run(ulong entry, int zero, int arch, uint params)
719 {
720 void (*kernel_entry)(int zero, int arch, uint params);
721 u32 cpu_start_reg;
722
723 kernel_entry = (void (*)(int, int, uint))entry;
724
725 /* Prepare CREG_CPU_START for kicking chosen CPUs */
726 cpu_start_reg = prepare_cpu_ctart_reg();
727
728 /* In case of run without hsdk_init */
729 slave_cpu_set_boot_addr(entry);
730
731 /* In case of run with hsdk_init */
732 for (u32 i = 0; i < NR_CPUS; i++) {
733 env_core.entry[i].val = entry;
734 env_core.entry[i].set = true;
735 }
736 /* sync cross_cpu struct as we updated core-entry variables */
737 sync_cross_cpu_data();
738
739 /* Kick chosen slave CPUs */
740 writel(cpu_start_reg, (void __iomem *)CREG_CPU_START);
741
742 if (is_cpu_used(0))
743 kernel_entry(zero, arch, params);
744 }
745
hsdk_go_prepare_and_run(void)746 static int hsdk_go_prepare_and_run(void)
747 {
748 /* Prepare CREG_CPU_START for kicking chosen CPUs */
749 u32 reg = prepare_cpu_ctart_reg();
750
751 if (env_common.halt_on_boot)
752 printf("CPU will halt before application start, start application with debugger.\n");
753
754 return hsdk_go_run(reg);
755 }
756
do_hsdk_go(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])757 static int do_hsdk_go(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
758 {
759 int ret;
760
761 /*
762 * Check for 'halt' parameter. 'halt' = enter halt-mode just before
763 * starting the application; can be used for debug.
764 */
765 if (argc > 1) {
766 env_common.halt_on_boot = !strcmp(argv[1], "halt");
767 if (!env_common.halt_on_boot) {
768 pr_err("Unrecognised parameter: \'%s\'\n", argv[1]);
769 return CMD_RET_FAILURE;
770 }
771 }
772
773 ret = check_master_cpu_id();
774 if (ret)
775 return ret;
776
777 ret = envs_process_and_validate(env_map_mask, env_map_go, is_cpu_used);
778 if (ret)
779 return ret;
780
781 /* sync cross_cpu struct as we updated core-entry variables */
782 sync_cross_cpu_data();
783
784 ret = hsdk_go_prepare_and_run();
785
786 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
787 }
788
789 U_BOOT_CMD(
790 hsdk_go, 3, 0, do_hsdk_go,
791 "Synopsys HSDK specific command",
792 " - Boot stand-alone application on HSDK\n"
793 "hsdk_go halt - Boot stand-alone application on HSDK, halt CPU just before application run\n"
794 );
795
do_hsdk_init(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])796 static int do_hsdk_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
797 {
798 static bool done = false;
799 int ret;
800
801 /* hsdk_init can be run only once */
802 if (done) {
803 printf("HSDK HW is already initialized! Please reset the board if you want to change the configuration.\n");
804 return CMD_RET_FAILURE;
805 }
806
807 ret = prepare_cpus();
808 if (!ret)
809 done = true;
810
811 return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
812 }
813
814 U_BOOT_CMD(
815 hsdk_init, 1, 0, do_hsdk_init,
816 "Synopsys HSDK specific command",
817 "- Init HSDK HW\n"
818 );
819
do_hsdk_clock_set(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])820 static int do_hsdk_clock_set(cmd_tbl_t *cmdtp, int flag, int argc,
821 char *const argv[])
822 {
823 int ret = 0;
824
825 /* Strip off leading subcommand argument */
826 argc--;
827 argv++;
828
829 envs_cleanup_common(env_map_clock);
830
831 if (!argc) {
832 printf("Set clocks to values specified in environment\n");
833 ret = envs_read_common(env_map_clock);
834 } else {
835 printf("Set clocks to values specified in args\n");
836 ret = args_envs_enumerate(env_map_clock, 2, argc, argv);
837 }
838
839 if (ret)
840 return CMD_RET_FAILURE;
841
842 ret = envs_validate_common(env_map_clock);
843 if (ret)
844 return CMD_RET_FAILURE;
845
846 /* Setup clock tree HW */
847 setup_clocks();
848
849 return CMD_RET_SUCCESS;
850 }
851
do_hsdk_clock_get(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])852 static int do_hsdk_clock_get(cmd_tbl_t *cmdtp, int flag, int argc,
853 char *const argv[])
854 {
855 ulong rate;
856
857 if (soc_clk_ctl("cpu-clk", &rate, CLK_GET | CLK_MHZ))
858 return CMD_RET_FAILURE;
859
860 if (env_set_ulong("cpu_freq", rate))
861 return CMD_RET_FAILURE;
862
863 if (soc_clk_ctl("tun-clk", &rate, CLK_GET | CLK_MHZ))
864 return CMD_RET_FAILURE;
865
866 if (env_set_ulong("tun_freq", rate))
867 return CMD_RET_FAILURE;
868
869 if (soc_clk_ctl("axi-clk", &rate, CLK_GET | CLK_MHZ))
870 return CMD_RET_FAILURE;
871
872 if (env_set_ulong("axi_freq", rate))
873 return CMD_RET_FAILURE;
874
875 printf("Clock values are saved to environment\n");
876
877 return CMD_RET_SUCCESS;
878 }
879
do_hsdk_clock_print(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])880 static int do_hsdk_clock_print(cmd_tbl_t *cmdtp, int flag, int argc,
881 char *const argv[])
882 {
883 /* Main clocks */
884 soc_clk_ctl("cpu-clk", NULL, CLK_PRINT | CLK_MHZ);
885 soc_clk_ctl("tun-clk", NULL, CLK_PRINT | CLK_MHZ);
886 soc_clk_ctl("axi-clk", NULL, CLK_PRINT | CLK_MHZ);
887 soc_clk_ctl("ddr-clk", NULL, CLK_PRINT | CLK_MHZ);
888
889 return CMD_RET_SUCCESS;
890 }
891
do_hsdk_clock_print_all(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])892 static int do_hsdk_clock_print_all(cmd_tbl_t *cmdtp, int flag, int argc,
893 char *const argv[])
894 {
895 /*
896 * NOTE: as of today we don't use some peripherals like HDMI / EBI
897 * so we don't want to print their clocks ("hdmi-sys-clk", "hdmi-pll",
898 * "hdmi-clk", "ebi-clk"). Nevertheless their clock subsystems is fully
899 * functional and we can print their clocks if it is required
900 */
901
902 /* CPU clock domain */
903 soc_clk_ctl("cpu-pll", NULL, CLK_PRINT | CLK_MHZ);
904 soc_clk_ctl("cpu-clk", NULL, CLK_PRINT | CLK_MHZ);
905 printf("\n");
906
907 /* SYS clock domain */
908 soc_clk_ctl("sys-pll", NULL, CLK_PRINT | CLK_MHZ);
909 soc_clk_ctl("apb-clk", NULL, CLK_PRINT | CLK_MHZ);
910 soc_clk_ctl("axi-clk", NULL, CLK_PRINT | CLK_MHZ);
911 soc_clk_ctl("eth-clk", NULL, CLK_PRINT | CLK_MHZ);
912 soc_clk_ctl("usb-clk", NULL, CLK_PRINT | CLK_MHZ);
913 soc_clk_ctl("sdio-clk", NULL, CLK_PRINT | CLK_MHZ);
914 /* soc_clk_ctl("hdmi-sys-clk", NULL, CLK_PRINT | CLK_MHZ); */
915 soc_clk_ctl("gfx-core-clk", NULL, CLK_PRINT | CLK_MHZ);
916 soc_clk_ctl("gfx-dma-clk", NULL, CLK_PRINT | CLK_MHZ);
917 soc_clk_ctl("gfx-cfg-clk", NULL, CLK_PRINT | CLK_MHZ);
918 soc_clk_ctl("dmac-core-clk", NULL, CLK_PRINT | CLK_MHZ);
919 soc_clk_ctl("dmac-cfg-clk", NULL, CLK_PRINT | CLK_MHZ);
920 soc_clk_ctl("sdio-ref-clk", NULL, CLK_PRINT | CLK_MHZ);
921 soc_clk_ctl("spi-clk", NULL, CLK_PRINT | CLK_MHZ);
922 soc_clk_ctl("i2c-clk", NULL, CLK_PRINT | CLK_MHZ);
923 /* soc_clk_ctl("ebi-clk", NULL, CLK_PRINT | CLK_MHZ); */
924 soc_clk_ctl("uart-clk", NULL, CLK_PRINT | CLK_MHZ);
925 printf("\n");
926
927 /* DDR clock domain */
928 soc_clk_ctl("ddr-clk", NULL, CLK_PRINT | CLK_MHZ);
929 printf("\n");
930
931 /* HDMI clock domain */
932 /* soc_clk_ctl("hdmi-pll", NULL, CLK_PRINT | CLK_MHZ); */
933 /* soc_clk_ctl("hdmi-clk", NULL, CLK_PRINT | CLK_MHZ); */
934 /* printf("\n"); */
935
936 /* TUN clock domain */
937 soc_clk_ctl("tun-pll", NULL, CLK_PRINT | CLK_MHZ);
938 soc_clk_ctl("tun-clk", NULL, CLK_PRINT | CLK_MHZ);
939 soc_clk_ctl("rom-clk", NULL, CLK_PRINT | CLK_MHZ);
940 soc_clk_ctl("pwm-clk", NULL, CLK_PRINT | CLK_MHZ);
941 printf("\n");
942
943 return CMD_RET_SUCCESS;
944 }
945
946 cmd_tbl_t cmd_hsdk_clock[] = {
947 U_BOOT_CMD_MKENT(set, 3, 0, do_hsdk_clock_set, "", ""),
948 U_BOOT_CMD_MKENT(get, 3, 0, do_hsdk_clock_get, "", ""),
949 U_BOOT_CMD_MKENT(print, 4, 0, do_hsdk_clock_print, "", ""),
950 U_BOOT_CMD_MKENT(print_all, 4, 0, do_hsdk_clock_print_all, "", ""),
951 };
952
do_hsdk_clock(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])953 static int do_hsdk_clock(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
954 {
955 cmd_tbl_t *c;
956
957 if (argc < 2)
958 return CMD_RET_USAGE;
959
960 /* Strip off leading 'hsdk_clock' command argument */
961 argc--;
962 argv++;
963
964 c = find_cmd_tbl(argv[0], cmd_hsdk_clock, ARRAY_SIZE(cmd_hsdk_clock));
965 if (!c)
966 return CMD_RET_USAGE;
967
968 return c->cmd(cmdtp, flag, argc, argv);
969 }
970
971 U_BOOT_CMD(
972 hsdk_clock, CONFIG_SYS_MAXARGS, 0, do_hsdk_clock,
973 "Synopsys HSDK specific clock command",
974 "set - Set clock to values specified in environment / command line arguments\n"
975 "hsdk_clock get - Save clock values to environment\n"
976 "hsdk_clock print - Print main clock values to console\n"
977 "hsdk_clock print_all - Print all clock values to console\n"
978 );
979
980 /* init calls */
board_early_init_f(void)981 int board_early_init_f(void)
982 {
983 /*
984 * Setup AXI apertures unconditionally as we want to have DDR
985 * in 0x00000000 region when we are kicking slave cpus.
986 */
987 init_memory_bridge();
988
989 /*
990 * Switch SDIO external ciu clock divider from default div-by-8 to
991 * minimum possible div-by-2.
992 */
993 writel(SDIO_UHS_REG_EXT_DIV_2, (void __iomem *)SDIO_UHS_REG_EXT);
994
995 return 0;
996 }
997
board_early_init_r(void)998 int board_early_init_r(void)
999 {
1000 /*
1001 * TODO: Init USB here to be able read environment from USB MSD.
1002 * It can be done with usb_init() call. We can't do it right now
1003 * due to brocken USB IP SW reset and lack of USB IP HW reset in
1004 * linux kernel (if we init USB here we will break USB in linux)
1005 */
1006
1007 /*
1008 * Flush all d$ as we want to use uncached area with st.di / ld.di
1009 * instructions and we don't want to have any dirty line in L1d$ or SL$
1010 * in this area. It is enough to flush all d$ once here as we access to
1011 * uncached area with regular st (non .di) instruction only when we copy
1012 * data during u-boot relocation.
1013 */
1014 flush_dcache_all();
1015
1016 printf("Relocation Offset is: %08lx\n", gd->reloc_off);
1017
1018 return 0;
1019 }
1020
board_late_init(void)1021 int board_late_init(void)
1022 {
1023 /*
1024 * Populate environment with clock frequency values -
1025 * run hsdk_clock get callback without uboot command run.
1026 */
1027 do_hsdk_clock_get(NULL, 0, 0, NULL);
1028
1029 return 0;
1030 }
1031
checkboard(void)1032 int checkboard(void)
1033 {
1034 puts("Board: Synopsys ARC HS Development Kit\n");
1035 return 0;
1036 };
1037