Merge tag 'drivers-3.15' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[pandora-kernel.git] / arch / arm / mach-integrator / integrator_ap.c
1 /*
2  *  linux/arch/arm/mach-integrator/integrator_ap.c
3  *
4  *  Copyright (C) 2000-2003 Deep Blue Solutions Ltd
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 as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/string.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/amba/bus.h>
29 #include <linux/amba/kmi.h>
30 #include <linux/clocksource.h>
31 #include <linux/clockchips.h>
32 #include <linux/interrupt.h>
33 #include <linux/io.h>
34 #include <linux/irqchip/versatile-fpga.h>
35 #include <linux/mtd/physmap.h>
36 #include <linux/clk.h>
37 #include <linux/platform_data/clk-integrator.h>
38 #include <linux/of_irq.h>
39 #include <linux/of_address.h>
40 #include <linux/of_platform.h>
41 #include <linux/stat.h>
42 #include <linux/sys_soc.h>
43 #include <linux/termios.h>
44 #include <linux/sched_clock.h>
45 #include <linux/clk-provider.h>
46
47 #include <mach/hardware.h>
48 #include <mach/platform.h>
49 #include <asm/hardware/arm_timer.h>
50 #include <asm/setup.h>
51 #include <asm/param.h>          /* HZ */
52 #include <asm/mach-types.h>
53
54 #include <mach/lm.h>
55
56 #include <asm/mach/arch.h>
57 #include <asm/mach/irq.h>
58 #include <asm/mach/map.h>
59 #include <asm/mach/time.h>
60
61 #include "cm.h"
62 #include "common.h"
63 #include "pci_v3.h"
64
65 /* Base address to the AP system controller */
66 void __iomem *ap_syscon_base;
67 /* Base address to the external bus interface */
68 static void __iomem *ebi_base;
69
70
71 /*
72  * All IO addresses are mapped onto VA 0xFFFx.xxxx, where x.xxxx
73  * is the (PA >> 12).
74  *
75  * Setup a VA for the Integrator interrupt controller (for header #0,
76  * just for now).
77  */
78 #define VA_IC_BASE      __io_address(INTEGRATOR_IC_BASE)
79
80 /*
81  * Logical      Physical
82  * ef000000                     Cache flush
83  * f1100000     11000000        System controller registers
84  * f1300000     13000000        Counter/Timer
85  * f1400000     14000000        Interrupt controller
86  * f1600000     16000000        UART 0
87  * f1700000     17000000        UART 1
88  * f1a00000     1a000000        Debug LEDs
89  * f1b00000     1b000000        GPIO
90  */
91
92 static struct map_desc ap_io_desc[] __initdata __maybe_unused = {
93         {
94                 .virtual        = IO_ADDRESS(INTEGRATOR_CT_BASE),
95                 .pfn            = __phys_to_pfn(INTEGRATOR_CT_BASE),
96                 .length         = SZ_4K,
97                 .type           = MT_DEVICE
98         }, {
99                 .virtual        = IO_ADDRESS(INTEGRATOR_IC_BASE),
100                 .pfn            = __phys_to_pfn(INTEGRATOR_IC_BASE),
101                 .length         = SZ_4K,
102                 .type           = MT_DEVICE
103         }, {
104                 .virtual        = IO_ADDRESS(INTEGRATOR_UART0_BASE),
105                 .pfn            = __phys_to_pfn(INTEGRATOR_UART0_BASE),
106                 .length         = SZ_4K,
107                 .type           = MT_DEVICE
108         }, {
109                 .virtual        = IO_ADDRESS(INTEGRATOR_DBG_BASE),
110                 .pfn            = __phys_to_pfn(INTEGRATOR_DBG_BASE),
111                 .length         = SZ_4K,
112                 .type           = MT_DEVICE
113         }, {
114                 .virtual        = IO_ADDRESS(INTEGRATOR_AP_GPIO_BASE),
115                 .pfn            = __phys_to_pfn(INTEGRATOR_AP_GPIO_BASE),
116                 .length         = SZ_4K,
117                 .type           = MT_DEVICE
118         }
119 };
120
121 static void __init ap_map_io(void)
122 {
123         iotable_init(ap_io_desc, ARRAY_SIZE(ap_io_desc));
124         pci_v3_early_init();
125 }
126
127 #ifdef CONFIG_PM
128 static unsigned long ic_irq_enable;
129
130 static int irq_suspend(void)
131 {
132         ic_irq_enable = readl(VA_IC_BASE + IRQ_ENABLE);
133         return 0;
134 }
135
136 static void irq_resume(void)
137 {
138         /* disable all irq sources */
139         cm_clear_irqs();
140         writel(-1, VA_IC_BASE + IRQ_ENABLE_CLEAR);
141         writel(-1, VA_IC_BASE + FIQ_ENABLE_CLEAR);
142
143         writel(ic_irq_enable, VA_IC_BASE + IRQ_ENABLE_SET);
144 }
145 #else
146 #define irq_suspend NULL
147 #define irq_resume NULL
148 #endif
149
150 static struct syscore_ops irq_syscore_ops = {
151         .suspend        = irq_suspend,
152         .resume         = irq_resume,
153 };
154
155 static int __init irq_syscore_init(void)
156 {
157         register_syscore_ops(&irq_syscore_ops);
158
159         return 0;
160 }
161
162 device_initcall(irq_syscore_init);
163
164 /*
165  * Flash handling.
166  */
167 static int ap_flash_init(struct platform_device *dev)
168 {
169         u32 tmp;
170
171         writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
172                ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
173
174         tmp = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET) |
175                 INTEGRATOR_EBI_WRITE_ENABLE;
176         writel(tmp, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
177
178         if (!(readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET)
179               & INTEGRATOR_EBI_WRITE_ENABLE)) {
180                 writel(0xa05f, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
181                 writel(tmp, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
182                 writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
183         }
184         return 0;
185 }
186
187 static void ap_flash_exit(struct platform_device *dev)
188 {
189         u32 tmp;
190
191         writel(INTEGRATOR_SC_CTRL_nFLVPPEN | INTEGRATOR_SC_CTRL_nFLWP,
192                ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
193
194         tmp = readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET) &
195                 ~INTEGRATOR_EBI_WRITE_ENABLE;
196         writel(tmp, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
197
198         if (readl(ebi_base + INTEGRATOR_EBI_CSR1_OFFSET) &
199             INTEGRATOR_EBI_WRITE_ENABLE) {
200                 writel(0xa05f, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
201                 writel(tmp, ebi_base + INTEGRATOR_EBI_CSR1_OFFSET);
202                 writel(0, ebi_base + INTEGRATOR_EBI_LOCK_OFFSET);
203         }
204 }
205
206 static void ap_flash_set_vpp(struct platform_device *pdev, int on)
207 {
208         if (on)
209                 writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
210                        ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
211         else
212                 writel(INTEGRATOR_SC_CTRL_nFLVPPEN,
213                        ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
214 }
215
216 static struct physmap_flash_data ap_flash_data = {
217         .width          = 4,
218         .init           = ap_flash_init,
219         .exit           = ap_flash_exit,
220         .set_vpp        = ap_flash_set_vpp,
221 };
222
223 /*
224  * For the PL010 found in the Integrator/AP some of the UART control is
225  * implemented in the system controller and accessed using a callback
226  * from the driver.
227  */
228 static void integrator_uart_set_mctrl(struct amba_device *dev,
229                                 void __iomem *base, unsigned int mctrl)
230 {
231         unsigned int ctrls = 0, ctrlc = 0, rts_mask, dtr_mask;
232         u32 phybase = dev->res.start;
233
234         if (phybase == INTEGRATOR_UART0_BASE) {
235                 /* UART0 */
236                 rts_mask = 1 << 4;
237                 dtr_mask = 1 << 5;
238         } else {
239                 /* UART1 */
240                 rts_mask = 1 << 6;
241                 dtr_mask = 1 << 7;
242         }
243
244         if (mctrl & TIOCM_RTS)
245                 ctrlc |= rts_mask;
246         else
247                 ctrls |= rts_mask;
248
249         if (mctrl & TIOCM_DTR)
250                 ctrlc |= dtr_mask;
251         else
252                 ctrls |= dtr_mask;
253
254         __raw_writel(ctrls, ap_syscon_base + INTEGRATOR_SC_CTRLS_OFFSET);
255         __raw_writel(ctrlc, ap_syscon_base + INTEGRATOR_SC_CTRLC_OFFSET);
256 }
257
258 struct amba_pl010_data ap_uart_data = {
259         .set_mctrl = integrator_uart_set_mctrl,
260 };
261
262 /*
263  * Where is the timer (VA)?
264  */
265 #define TIMER0_VA_BASE __io_address(INTEGRATOR_TIMER0_BASE)
266 #define TIMER1_VA_BASE __io_address(INTEGRATOR_TIMER1_BASE)
267 #define TIMER2_VA_BASE __io_address(INTEGRATOR_TIMER2_BASE)
268
269 static unsigned long timer_reload;
270
271 static u64 notrace integrator_read_sched_clock(void)
272 {
273         return -readl((void __iomem *) TIMER2_VA_BASE + TIMER_VALUE);
274 }
275
276 static void integrator_clocksource_init(unsigned long inrate,
277                                         void __iomem *base)
278 {
279         u32 ctrl = TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC;
280         unsigned long rate = inrate;
281
282         if (rate >= 1500000) {
283                 rate /= 16;
284                 ctrl |= TIMER_CTRL_DIV16;
285         }
286
287         writel(0xffff, base + TIMER_LOAD);
288         writel(ctrl, base + TIMER_CTRL);
289
290         clocksource_mmio_init(base + TIMER_VALUE, "timer2",
291                         rate, 200, 16, clocksource_mmio_readl_down);
292         sched_clock_register(integrator_read_sched_clock, 16, rate);
293 }
294
295 static void __iomem * clkevt_base;
296
297 /*
298  * IRQ handler for the timer
299  */
300 static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
301 {
302         struct clock_event_device *evt = dev_id;
303
304         /* clear the interrupt */
305         writel(1, clkevt_base + TIMER_INTCLR);
306
307         evt->event_handler(evt);
308
309         return IRQ_HANDLED;
310 }
311
312 static void clkevt_set_mode(enum clock_event_mode mode, struct clock_event_device *evt)
313 {
314         u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
315
316         /* Disable timer */
317         writel(ctrl, clkevt_base + TIMER_CTRL);
318
319         switch (mode) {
320         case CLOCK_EVT_MODE_PERIODIC:
321                 /* Enable the timer and start the periodic tick */
322                 writel(timer_reload, clkevt_base + TIMER_LOAD);
323                 ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
324                 writel(ctrl, clkevt_base + TIMER_CTRL);
325                 break;
326         case CLOCK_EVT_MODE_ONESHOT:
327                 /* Leave the timer disabled, .set_next_event will enable it */
328                 ctrl &= ~TIMER_CTRL_PERIODIC;
329                 writel(ctrl, clkevt_base + TIMER_CTRL);
330                 break;
331         case CLOCK_EVT_MODE_UNUSED:
332         case CLOCK_EVT_MODE_SHUTDOWN:
333         case CLOCK_EVT_MODE_RESUME:
334         default:
335                 /* Just leave in disabled state */
336                 break;
337         }
338
339 }
340
341 static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
342 {
343         unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
344
345         writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
346         writel(next, clkevt_base + TIMER_LOAD);
347         writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
348
349         return 0;
350 }
351
352 static struct clock_event_device integrator_clockevent = {
353         .name           = "timer1",
354         .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
355         .set_mode       = clkevt_set_mode,
356         .set_next_event = clkevt_set_next_event,
357         .rating         = 300,
358 };
359
360 static struct irqaction integrator_timer_irq = {
361         .name           = "timer",
362         .flags          = IRQF_TIMER | IRQF_IRQPOLL,
363         .handler        = integrator_timer_interrupt,
364         .dev_id         = &integrator_clockevent,
365 };
366
367 static void integrator_clockevent_init(unsigned long inrate,
368                                 void __iomem *base, int irq)
369 {
370         unsigned long rate = inrate;
371         unsigned int ctrl = 0;
372
373         clkevt_base = base;
374         /* Calculate and program a divisor */
375         if (rate > 0x100000 * HZ) {
376                 rate /= 256;
377                 ctrl |= TIMER_CTRL_DIV256;
378         } else if (rate > 0x10000 * HZ) {
379                 rate /= 16;
380                 ctrl |= TIMER_CTRL_DIV16;
381         }
382         timer_reload = rate / HZ;
383         writel(ctrl, clkevt_base + TIMER_CTRL);
384
385         setup_irq(irq, &integrator_timer_irq);
386         clockevents_config_and_register(&integrator_clockevent,
387                                         rate,
388                                         1,
389                                         0xffffU);
390 }
391
392 void __init ap_init_early(void)
393 {
394 }
395
396 static void __init ap_of_timer_init(void)
397 {
398         struct device_node *node;
399         const char *path;
400         void __iomem *base;
401         int err;
402         int irq;
403         struct clk *clk;
404         unsigned long rate;
405
406         of_clk_init(NULL);
407
408         err = of_property_read_string(of_aliases,
409                                 "arm,timer-primary", &path);
410         if (WARN_ON(err))
411                 return;
412         node = of_find_node_by_path(path);
413         base = of_iomap(node, 0);
414         if (WARN_ON(!base))
415                 return;
416
417         clk = of_clk_get(node, 0);
418         BUG_ON(IS_ERR(clk));
419         clk_prepare_enable(clk);
420         rate = clk_get_rate(clk);
421
422         writel(0, base + TIMER_CTRL);
423         integrator_clocksource_init(rate, base);
424
425         err = of_property_read_string(of_aliases,
426                                 "arm,timer-secondary", &path);
427         if (WARN_ON(err))
428                 return;
429         node = of_find_node_by_path(path);
430         base = of_iomap(node, 0);
431         if (WARN_ON(!base))
432                 return;
433         irq = irq_of_parse_and_map(node, 0);
434
435         clk = of_clk_get(node, 0);
436         BUG_ON(IS_ERR(clk));
437         clk_prepare_enable(clk);
438         rate = clk_get_rate(clk);
439
440         writel(0, base + TIMER_CTRL);
441         integrator_clockevent_init(rate, base, irq);
442 }
443
444 static const struct of_device_id fpga_irq_of_match[] __initconst = {
445         { .compatible = "arm,versatile-fpga-irq", .data = fpga_irq_of_init, },
446         { /* Sentinel */ }
447 };
448
449 static void __init ap_init_irq_of(void)
450 {
451         cm_init();
452         of_irq_init(fpga_irq_of_match);
453 }
454
455 /* For the Device Tree, add in the UART callbacks as AUXDATA */
456 static struct of_dev_auxdata ap_auxdata_lookup[] __initdata = {
457         OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_RTC_BASE,
458                 "rtc", NULL),
459         OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_UART0_BASE,
460                 "uart0", &ap_uart_data),
461         OF_DEV_AUXDATA("arm,primecell", INTEGRATOR_UART1_BASE,
462                 "uart1", &ap_uart_data),
463         OF_DEV_AUXDATA("arm,primecell", KMI0_BASE,
464                 "kmi0", NULL),
465         OF_DEV_AUXDATA("arm,primecell", KMI1_BASE,
466                 "kmi1", NULL),
467         OF_DEV_AUXDATA("cfi-flash", INTEGRATOR_FLASH_BASE,
468                 "physmap-flash", &ap_flash_data),
469         { /* sentinel */ },
470 };
471
472 static const struct of_device_id ap_syscon_match[] = {
473         { .compatible = "arm,integrator-ap-syscon"},
474         { },
475 };
476
477 static const struct of_device_id ebi_match[] = {
478         { .compatible = "arm,external-bus-interface"},
479         { },
480 };
481
482 static void __init ap_init_of(void)
483 {
484         unsigned long sc_dec;
485         struct device_node *root;
486         struct device_node *syscon;
487         struct device_node *ebi;
488         struct device *parent;
489         struct soc_device *soc_dev;
490         struct soc_device_attribute *soc_dev_attr;
491         u32 ap_sc_id;
492         int err;
493         int i;
494
495         /* Here we create an SoC device for the root node */
496         root = of_find_node_by_path("/");
497         if (!root)
498                 return;
499
500         syscon = of_find_matching_node(root, ap_syscon_match);
501         if (!syscon)
502                 return;
503         ebi = of_find_matching_node(root, ebi_match);
504         if (!ebi)
505                 return;
506
507         ap_syscon_base = of_iomap(syscon, 0);
508         if (!ap_syscon_base)
509                 return;
510         ebi_base = of_iomap(ebi, 0);
511         if (!ebi_base)
512                 return;
513
514         ap_sc_id = readl(ap_syscon_base);
515
516         soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
517         if (!soc_dev_attr)
518                 return;
519
520         err = of_property_read_string(root, "compatible",
521                                       &soc_dev_attr->soc_id);
522         if (err)
523                 return;
524         err = of_property_read_string(root, "model", &soc_dev_attr->machine);
525         if (err)
526                 return;
527         soc_dev_attr->family = "Integrator";
528         soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c",
529                                            'A' + (ap_sc_id & 0x0f));
530
531         soc_dev = soc_device_register(soc_dev_attr);
532         if (IS_ERR(soc_dev)) {
533                 kfree(soc_dev_attr->revision);
534                 kfree(soc_dev_attr);
535                 return;
536         }
537
538         parent = soc_device_to_device(soc_dev);
539         integrator_init_sysfs(parent, ap_sc_id);
540
541         of_platform_populate(root, of_default_bus_match_table,
542                         ap_auxdata_lookup, parent);
543
544         sc_dec = readl(ap_syscon_base + INTEGRATOR_SC_DEC_OFFSET);
545         for (i = 0; i < 4; i++) {
546                 struct lm_device *lmdev;
547
548                 if ((sc_dec & (16 << i)) == 0)
549                         continue;
550
551                 lmdev = kzalloc(sizeof(struct lm_device), GFP_KERNEL);
552                 if (!lmdev)
553                         continue;
554
555                 lmdev->resource.start = 0xc0000000 + 0x10000000 * i;
556                 lmdev->resource.end = lmdev->resource.start + 0x0fffffff;
557                 lmdev->resource.flags = IORESOURCE_MEM;
558                 lmdev->irq = irq_of_parse_and_map(syscon, i);
559                 lmdev->id = i;
560
561                 lm_device_register(lmdev);
562         }
563 }
564
565 static const char * ap_dt_board_compat[] = {
566         "arm,integrator-ap",
567         NULL,
568 };
569
570 DT_MACHINE_START(INTEGRATOR_AP_DT, "ARM Integrator/AP (Device Tree)")
571         .reserve        = integrator_reserve,
572         .map_io         = ap_map_io,
573         .init_early     = ap_init_early,
574         .init_irq       = ap_init_irq_of,
575         .handle_irq     = fpga_handle_irq,
576         .init_time      = ap_of_timer_init,
577         .init_machine   = ap_init_of,
578         .restart        = integrator_restart,
579         .dt_compat      = ap_dt_board_compat,
580 MACHINE_END