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