ad0a7ef84660598533379fb50a3869bedcf46da0
[sfrench/cifs-2.6.git] / arch / arc / plat-axs10x / axs10x.c
1 /*
2  * AXS101/AXS103 Software Development Platform
3  *
4  * Copyright (C) 2013-15 Synopsys, Inc. (www.synopsys.com)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/of_platform.h>
18 #include <asm/clk.h>
19 #include <asm/io.h>
20 #include <asm/mach_desc.h>
21 #include <asm/mcip.h>
22
23 #define AXS_MB_CGU              0xE0010000
24 #define AXS_MB_CREG             0xE0011000
25
26 #define CREG_MB_IRQ_MUX         (AXS_MB_CREG + 0x214)
27 #define CREG_MB_SW_RESET        (AXS_MB_CREG + 0x220)
28 #define CREG_MB_VER             (AXS_MB_CREG + 0x230)
29 #define CREG_MB_CONFIG          (AXS_MB_CREG + 0x234)
30
31 #define AXC001_CREG             0xF0001000
32 #define AXC001_GPIO_INTC        0xF0003000
33
34 static void __init axs10x_enable_gpio_intc_wire(void)
35 {
36         /*
37          * Peripherals on CPU Card and Mother Board are wired to cpu intc via
38          * intermediate DW APB GPIO blocks (mainly for debouncing)
39          *
40          *         ---------------------
41          *        |  snps,arc700-intc |
42          *        ---------------------
43          *          | #7          | #15
44          * -------------------   -------------------
45          * | snps,dw-apb-gpio |  | snps,dw-apb-gpio |
46          * -------------------   -------------------
47          *        |                         |
48          *        |                 [ Debug UART on cpu card ]
49          *        |
50          * ------------------------
51          * | snps,dw-apb-intc (MB)|
52          * ------------------------
53          *  |      |       |      |
54          * [eth] [uart]        [... other perip on Main Board]
55          *
56          * Current implementation of "irq-dw-apb-ictl" driver doesn't work well
57          * with stacked INTCs. In particular problem happens if its master INTC
58          * not yet instantiated. See discussion here -
59          * https://lkml.org/lkml/2015/3/4/755
60          *
61          * So setup the first gpio block as a passive pass thru and hide it from
62          * DT hardware topology - connect MB intc directly to cpu intc
63          * The GPIO "wire" needs to be init nevertheless (here)
64          *
65          * One side adv is that peripheral interrupt handling avoids one nested
66          * intc ISR hop
67          */
68 #define GPIO_INTEN              (AXC001_GPIO_INTC + 0x30)
69 #define GPIO_INTMASK            (AXC001_GPIO_INTC + 0x34)
70 #define GPIO_INTTYPE_LEVEL      (AXC001_GPIO_INTC + 0x38)
71 #define GPIO_INT_POLARITY       (AXC001_GPIO_INTC + 0x3c)
72 #define MB_TO_GPIO_IRQ          12
73
74         iowrite32(~(1 << MB_TO_GPIO_IRQ), (void __iomem *) GPIO_INTMASK);
75         iowrite32(0, (void __iomem *) GPIO_INTTYPE_LEVEL);
76         iowrite32(~0, (void __iomem *) GPIO_INT_POLARITY);
77         iowrite32(1 << MB_TO_GPIO_IRQ, (void __iomem *) GPIO_INTEN);
78 }
79
80 static inline void __init
81 write_cgu_reg(uint32_t value, void __iomem *reg, void __iomem *lock_reg)
82 {
83         unsigned int loops = 128 * 1024, ctr;
84
85         iowrite32(value, reg);
86
87         ctr = loops;
88         while (((ioread32(lock_reg) & 1) == 1) && ctr--) /* wait for unlock */
89                 cpu_relax();
90
91         ctr = loops;
92         while (((ioread32(lock_reg) & 1) == 0) && ctr--) /* wait for re-lock */
93                 cpu_relax();
94 }
95
96 static void __init axs10x_print_board_ver(unsigned int creg, const char *str)
97 {
98         union ver {
99                 struct {
100 #ifdef CONFIG_CPU_BIG_ENDIAN
101                         unsigned int pad:11, y:12, m:4, d:5;
102 #else
103                         unsigned int d:5, m:4, y:12, pad:11;
104 #endif
105                 };
106                 unsigned int val;
107         } board;
108
109         board.val = ioread32((void __iomem *)creg);
110         pr_info("AXS: %s FPGA Date: %u-%u-%u\n", str, board.d, board.m,
111                 board.y);
112 }
113
114 static void __init axs10x_early_init(void)
115 {
116         int mb_rev;
117         char mb[32];
118
119         /* Determine motherboard version */
120         if (ioread32((void __iomem *) CREG_MB_CONFIG) & (1 << 28))
121                 mb_rev = 3;     /* HT-3 (rev3.0) */
122         else
123                 mb_rev = 2;     /* HT-2 (rev2.0) */
124
125         axs10x_enable_gpio_intc_wire();
126
127         scnprintf(mb, 32, "MainBoard v%d", mb_rev);
128         axs10x_print_board_ver(CREG_MB_VER, mb);
129 }
130
131 #ifdef CONFIG_AXS101
132
133 #define CREG_CPU_ADDR_770       (AXC001_CREG + 0x20)
134 #define CREG_CPU_ADDR_TUNN      (AXC001_CREG + 0x60)
135 #define CREG_CPU_ADDR_770_UPD   (AXC001_CREG + 0x34)
136 #define CREG_CPU_ADDR_TUNN_UPD  (AXC001_CREG + 0x74)
137
138 #define CREG_CPU_ARC770_IRQ_MUX (AXC001_CREG + 0x114)
139 #define CREG_CPU_GPIO_UART_MUX  (AXC001_CREG + 0x120)
140
141 /*
142  * Set up System Memory Map for ARC cpu / peripherals controllers
143  *
144  * Each AXI master has a 4GB memory map specified as 16 apertures of 256MB, each
145  * of which maps to a corresponding 256MB aperture in Target slave memory map.
146  *
147  * e.g. ARC cpu AXI Master's aperture 8 (0x8000_0000) is mapped to aperture 0
148  * (0x0000_0000) of DDR Port 0 (slave #1)
149  *
150  * Access from cpu to MB controllers such as GMAC is setup using AXI Tunnel:
151  * which has master/slaves on both ends.
152  * e.g. aperture 14 (0xE000_0000) of ARC cpu is mapped to aperture 14
153  * (0xE000_0000) of CPU Card AXI Tunnel slave (slave #3) which is mapped to
154  * MB AXI Tunnel Master, which also has a mem map setup
155  *
156  * In the reverse direction, MB AXI Masters (e.g. GMAC) mem map is setup
157  * to map to MB AXI Tunnel slave which connects to CPU Card AXI Tunnel Master
158  */
159 struct aperture {
160         unsigned int slave_sel:4, slave_off:4, pad:24;
161 };
162
163 /* CPU Card target slaves */
164 #define AXC001_SLV_NONE                 0
165 #define AXC001_SLV_DDR_PORT0            1
166 #define AXC001_SLV_SRAM                 2
167 #define AXC001_SLV_AXI_TUNNEL           3
168 #define AXC001_SLV_AXI2APB              6
169 #define AXC001_SLV_DDR_PORT1            7
170
171 /* MB AXI Target slaves */
172 #define AXS_MB_SLV_NONE                 0
173 #define AXS_MB_SLV_AXI_TUNNEL_CPU       1
174 #define AXS_MB_SLV_AXI_TUNNEL_HAPS      2
175 #define AXS_MB_SLV_SRAM                 3
176 #define AXS_MB_SLV_CONTROL              4
177
178 /* MB AXI masters */
179 #define AXS_MB_MST_TUNNEL_CPU           0
180 #define AXS_MB_MST_USB_OHCI             10
181
182 /*
183  * memmap for ARC core on CPU Card
184  */
185 static const struct aperture axc001_memmap[16] = {
186         {AXC001_SLV_AXI_TUNNEL,         0x0},
187         {AXC001_SLV_AXI_TUNNEL,         0x1},
188         {AXC001_SLV_SRAM,               0x0}, /* 0x2000_0000: Local SRAM */
189         {AXC001_SLV_NONE,               0x0},
190         {AXC001_SLV_NONE,               0x0},
191         {AXC001_SLV_NONE,               0x0},
192         {AXC001_SLV_NONE,               0x0},
193         {AXC001_SLV_NONE,               0x0},
194         {AXC001_SLV_DDR_PORT0,          0x0}, /* 0x8000_0000: DDR   0..256M */
195         {AXC001_SLV_DDR_PORT0,          0x1}, /* 0x9000_0000: DDR 256..512M */
196         {AXC001_SLV_DDR_PORT0,          0x2},
197         {AXC001_SLV_DDR_PORT0,          0x3},
198         {AXC001_SLV_NONE,               0x0},
199         {AXC001_SLV_AXI_TUNNEL,         0xD},
200         {AXC001_SLV_AXI_TUNNEL,         0xE}, /* MB: CREG, CGU... */
201         {AXC001_SLV_AXI2APB,            0x0}, /* CPU Card local CREG, CGU... */
202 };
203
204 /*
205  * memmap for CPU Card AXI Tunnel Master (for access by MB controllers)
206  * GMAC (MB) -> MB AXI Tunnel slave -> CPU Card AXI Tunnel Master -> DDR
207  */
208 static const struct aperture axc001_axi_tunnel_memmap[16] = {
209         {AXC001_SLV_AXI_TUNNEL,         0x0},
210         {AXC001_SLV_AXI_TUNNEL,         0x1},
211         {AXC001_SLV_SRAM,               0x0},
212         {AXC001_SLV_NONE,               0x0},
213         {AXC001_SLV_NONE,               0x0},
214         {AXC001_SLV_NONE,               0x0},
215         {AXC001_SLV_NONE,               0x0},
216         {AXC001_SLV_NONE,               0x0},
217         {AXC001_SLV_DDR_PORT1,          0x0},
218         {AXC001_SLV_DDR_PORT1,          0x1},
219         {AXC001_SLV_DDR_PORT1,          0x2},
220         {AXC001_SLV_DDR_PORT1,          0x3},
221         {AXC001_SLV_NONE,               0x0},
222         {AXC001_SLV_AXI_TUNNEL,         0xD},
223         {AXC001_SLV_AXI_TUNNEL,         0xE},
224         {AXC001_SLV_AXI2APB,            0x0},
225 };
226
227 /*
228  * memmap for MB AXI Masters
229  * Same mem map for all perip controllers as well as MB AXI Tunnel Master
230  */
231 static const struct aperture axs_mb_memmap[16] = {
232         {AXS_MB_SLV_SRAM,               0x0},
233         {AXS_MB_SLV_SRAM,               0x0},
234         {AXS_MB_SLV_NONE,               0x0},
235         {AXS_MB_SLV_NONE,               0x0},
236         {AXS_MB_SLV_NONE,               0x0},
237         {AXS_MB_SLV_NONE,               0x0},
238         {AXS_MB_SLV_NONE,               0x0},
239         {AXS_MB_SLV_NONE,               0x0},
240         {AXS_MB_SLV_AXI_TUNNEL_CPU,     0x8},   /* DDR on CPU Card */
241         {AXS_MB_SLV_AXI_TUNNEL_CPU,     0x9},   /* DDR on CPU Card */
242         {AXS_MB_SLV_AXI_TUNNEL_CPU,     0xA},
243         {AXS_MB_SLV_AXI_TUNNEL_CPU,     0xB},
244         {AXS_MB_SLV_NONE,               0x0},
245         {AXS_MB_SLV_AXI_TUNNEL_HAPS,    0xD},
246         {AXS_MB_SLV_CONTROL,            0x0},   /* MB Local CREG, CGU... */
247         {AXS_MB_SLV_AXI_TUNNEL_CPU,     0xF},
248 };
249
250 static noinline void __init
251 axs101_set_memmap(void __iomem *base, const struct aperture map[16])
252 {
253         unsigned int slave_select, slave_offset;
254         int i;
255
256         slave_select = slave_offset = 0;
257         for (i = 0; i < 8; i++) {
258                 slave_select |= map[i].slave_sel << (i << 2);
259                 slave_offset |= map[i].slave_off << (i << 2);
260         }
261
262         iowrite32(slave_select, base + 0x0);    /* SLV0 */
263         iowrite32(slave_offset, base + 0x8);    /* OFFSET0 */
264
265         slave_select = slave_offset = 0;
266         for (i = 0; i < 8; i++) {
267                 slave_select |= map[i+8].slave_sel << (i << 2);
268                 slave_offset |= map[i+8].slave_off << (i << 2);
269         }
270
271         iowrite32(slave_select, base + 0x4);    /* SLV1 */
272         iowrite32(slave_offset, base + 0xC);    /* OFFSET1 */
273 }
274
275 static void __init axs101_early_init(void)
276 {
277         int i;
278
279         /* ARC 770D memory view */
280         axs101_set_memmap((void __iomem *) CREG_CPU_ADDR_770, axc001_memmap);
281         iowrite32(1, (void __iomem *) CREG_CPU_ADDR_770_UPD);
282
283         /* AXI tunnel memory map (incoming traffic from MB into CPU Card */
284         axs101_set_memmap((void __iomem *) CREG_CPU_ADDR_TUNN,
285                               axc001_axi_tunnel_memmap);
286         iowrite32(1, (void __iomem *) CREG_CPU_ADDR_TUNN_UPD);
287
288         /* MB peripherals memory map */
289         for (i = AXS_MB_MST_TUNNEL_CPU; i <= AXS_MB_MST_USB_OHCI; i++)
290                 axs101_set_memmap((void __iomem *) AXS_MB_CREG + (i << 4),
291                                       axs_mb_memmap);
292
293         iowrite32(0x3ff, (void __iomem *) AXS_MB_CREG + 0x100); /* Update */
294
295         /* GPIO pins 18 and 19 are used as UART rx and tx, respectively. */
296         iowrite32(0x01, (void __iomem *) CREG_CPU_GPIO_UART_MUX);
297
298         /* Set up the MB interrupt system: mux interrupts to GPIO7) */
299         iowrite32(0x01, (void __iomem *) CREG_MB_IRQ_MUX);
300
301         /* reset ethernet and ULPI interfaces */
302         iowrite32(0x18, (void __iomem *) CREG_MB_SW_RESET);
303
304         /* map GPIO 14:10 to ARC 9:5 (IRQ mux change for MB v2 onwards) */
305         iowrite32(0x52, (void __iomem *) CREG_CPU_ARC770_IRQ_MUX);
306
307         axs10x_early_init();
308 }
309
310 #endif  /* CONFIG_AXS101 */
311
312 #ifdef CONFIG_AXS103
313
314 #define AXC003_CGU      0xF0000000
315 #define AXC003_CREG     0xF0001000
316 #define AXC003_MST_AXI_TUNNEL   0
317 #define AXC003_MST_HS38         1
318
319 #define CREG_CPU_AXI_M0_IRQ_MUX (AXC003_CREG + 0x440)
320 #define CREG_CPU_GPIO_UART_MUX  (AXC003_CREG + 0x480)
321 #define CREG_CPU_TUN_IO_CTRL    (AXC003_CREG + 0x494)
322
323
324 union pll_reg {
325         struct {
326 #ifdef CONFIG_CPU_BIG_ENDIAN
327                 unsigned int pad:17, noupd:1, bypass:1, edge:1, high:6, low:6;
328 #else
329                 unsigned int low:6, high:6, edge:1, bypass:1, noupd:1, pad:17;
330 #endif
331         };
332         unsigned int val;
333 };
334
335 static unsigned int __init axs103_get_freq(void)
336 {
337         union pll_reg idiv, fbdiv, odiv;
338         unsigned int f = 33333333;
339
340         idiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 0);
341         fbdiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 4);
342         odiv.val = ioread32((void __iomem *)AXC003_CGU + 0x80 + 8);
343
344         if (idiv.bypass != 1)
345                 f = f / (idiv.low + idiv.high);
346
347         if (fbdiv.bypass != 1)
348                 f = f * (fbdiv.low + fbdiv.high);
349
350         if (odiv.bypass != 1)
351                 f = f / (odiv.low + odiv.high);
352
353         f = (f + 500000) / 1000000; /* Rounding */
354         return f;
355 }
356
357 static inline unsigned int __init encode_div(unsigned int id, int upd)
358 {
359         union pll_reg div;
360
361         div.val = 0;
362
363         div.noupd = !upd;
364         div.bypass = id == 1 ? 1 : 0;
365         div.edge = (id%2 == 0) ? 0 : 1;  /* 0 = rising */
366         div.low = (id%2 == 0) ? id >> 1 : (id >> 1)+1;
367         div.high = id >> 1;
368
369         return div.val;
370 }
371
372 noinline static void __init
373 axs103_set_freq(unsigned int id, unsigned int fd, unsigned int od)
374 {
375         write_cgu_reg(encode_div(id, 0),
376                       (void __iomem *)AXC003_CGU + 0x80 + 0,
377                       (void __iomem *)AXC003_CGU + 0x110);
378
379         write_cgu_reg(encode_div(fd, 0),
380                       (void __iomem *)AXC003_CGU + 0x80 + 4,
381                       (void __iomem *)AXC003_CGU + 0x110);
382
383         write_cgu_reg(encode_div(od, 1),
384                       (void __iomem *)AXC003_CGU + 0x80 + 8,
385                       (void __iomem *)AXC003_CGU + 0x110);
386 }
387
388 static void __init axs103_early_init(void)
389 {
390         switch (arc_get_core_freq()/1000000) {
391         case 33:
392                 axs103_set_freq(1, 1, 1);
393                 break;
394         case 50:
395                 axs103_set_freq(1, 30, 20);
396                 break;
397         case 75:
398                 axs103_set_freq(2, 45, 10);
399                 break;
400         case 90:
401                 axs103_set_freq(2, 54, 10);
402                 break;
403         case 100:
404                 axs103_set_freq(1, 30, 10);
405                 break;
406         case 125:
407                 axs103_set_freq(2, 45,  6);
408                 break;
409         default:
410                 /*
411                  * In this case, core_frequency derived from
412                  * DT "clock-frequency" might not match with board value.
413                  * Hence update it to match the board value.
414                  */
415                 arc_set_core_freq(axs103_get_freq() * 1000000);
416                 break;
417         }
418
419         pr_info("Freq is %dMHz\n", axs103_get_freq());
420
421         /* Memory maps already config in pre-bootloader */
422
423         /* set GPIO mux to UART */
424         iowrite32(0x01, (void __iomem *) CREG_CPU_GPIO_UART_MUX);
425
426         iowrite32((0x00100000U | 0x000C0000U | 0x00003322U),
427                   (void __iomem *) CREG_CPU_TUN_IO_CTRL);
428
429         /* Set up the AXS_MB interrupt system.*/
430         iowrite32(12, (void __iomem *) (CREG_CPU_AXI_M0_IRQ_MUX
431                                          + (AXC003_MST_HS38 << 2)));
432
433         /* connect ICTL - Main Board with GPIO line */
434         iowrite32(0x01, (void __iomem *) CREG_MB_IRQ_MUX);
435
436         axs10x_print_board_ver(AXC003_CREG + 4088, "AXC003 CPU Card");
437
438         axs10x_early_init();
439
440 #ifdef CONFIG_ARC_MCIP
441         /* No Hardware init, but filling the smp ops callbacks */
442         mcip_init_early_smp();
443 #endif
444 }
445 #endif
446
447 #ifdef CONFIG_AXS101
448
449 static const char *axs101_compat[] __initconst = {
450         "snps,axs101",
451         NULL,
452 };
453
454 MACHINE_START(AXS101, "axs101")
455         .dt_compat      = axs101_compat,
456         .init_early     = axs101_early_init,
457 MACHINE_END
458
459 #endif  /* CONFIG_AXS101 */
460
461 #ifdef CONFIG_AXS103
462
463 static const char *axs103_compat[] __initconst = {
464         "snps,axs103",
465         NULL,
466 };
467
468 MACHINE_START(AXS103, "axs103")
469         .dt_compat      = axs103_compat,
470         .init_early     = axs103_early_init,
471 #ifdef CONFIG_ARC_MCIP
472         .init_smp       = mcip_init_smp,
473 #endif
474 MACHINE_END
475
476 #endif  /* CONFIG_AXS103 */