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