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