blob: 49e657c15067bf271c5404017c068b26bdb415ca [file] [log] [blame]
Anton Vorontsovd94f9442010-03-25 17:12:41 +03001/*
2 * Copyright 1999 - 2003 ARM Limited
3 * Copyright 2000 Deep Blue Solutions Ltd
4 * Copyright 2008 Cavium Networks
5 *
6 * This file 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
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/clockchips.h>
14#include <linux/io.h>
Rob Herring520f7bd2012-12-27 13:10:24 -060015#include <linux/irqchip/arm-gic.h>
Arnd Bergmann415f59142013-03-14 22:27:32 +010016#include <linux/of_platform.h>
17#include <linux/platform_device.h>
18#include <linux/usb/ehci_pdriver.h>
19#include <linux/usb/ohci_pdriver.h>
20#include <asm/mach/arch.h>
Anton Vorontsovd94f9442010-03-25 17:12:41 +030021#include <asm/mach/map.h>
22#include <asm/mach/time.h>
23#include <asm/mach/irq.h>
Anton Vorontsov93e85d82011-07-06 16:45:09 +040024#include <asm/hardware/cache-l2x0.h>
Arnd Bergmann3f9fb2a2013-03-13 13:15:25 +010025#include "cns3xxx.h"
Anton Vorontsovd94f9442010-03-25 17:12:41 +030026#include "core.h"
Arnd Bergmann415f59142013-03-14 22:27:32 +010027#include "pm.h"
Anton Vorontsovd94f9442010-03-25 17:12:41 +030028
29static struct map_desc cns3xxx_io_desc[] __initdata = {
30 {
31 .virtual = CNS3XXX_TC11MP_TWD_BASE_VIRT,
32 .pfn = __phys_to_pfn(CNS3XXX_TC11MP_TWD_BASE),
33 .length = SZ_4K,
34 .type = MT_DEVICE,
35 }, {
36 .virtual = CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT,
37 .pfn = __phys_to_pfn(CNS3XXX_TC11MP_GIC_CPU_BASE),
38 .length = SZ_4K,
39 .type = MT_DEVICE,
40 }, {
41 .virtual = CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT,
42 .pfn = __phys_to_pfn(CNS3XXX_TC11MP_GIC_DIST_BASE),
43 .length = SZ_4K,
44 .type = MT_DEVICE,
45 }, {
46 .virtual = CNS3XXX_TIMER1_2_3_BASE_VIRT,
47 .pfn = __phys_to_pfn(CNS3XXX_TIMER1_2_3_BASE),
48 .length = SZ_4K,
49 .type = MT_DEVICE,
50 }, {
51 .virtual = CNS3XXX_GPIOA_BASE_VIRT,
52 .pfn = __phys_to_pfn(CNS3XXX_GPIOA_BASE),
53 .length = SZ_4K,
54 .type = MT_DEVICE,
55 }, {
56 .virtual = CNS3XXX_GPIOB_BASE_VIRT,
57 .pfn = __phys_to_pfn(CNS3XXX_GPIOB_BASE),
58 .length = SZ_4K,
59 .type = MT_DEVICE,
60 }, {
61 .virtual = CNS3XXX_MISC_BASE_VIRT,
62 .pfn = __phys_to_pfn(CNS3XXX_MISC_BASE),
63 .length = SZ_4K,
64 .type = MT_DEVICE,
65 }, {
66 .virtual = CNS3XXX_PM_BASE_VIRT,
67 .pfn = __phys_to_pfn(CNS3XXX_PM_BASE),
68 .length = SZ_4K,
69 .type = MT_DEVICE,
70 },
71};
72
73void __init cns3xxx_map_io(void)
74{
75 iotable_init(cns3xxx_io_desc, ARRAY_SIZE(cns3xxx_io_desc));
76}
77
78/* used by entry-macro.S */
Anton Vorontsovd94f9442010-03-25 17:12:41 +030079void __init cns3xxx_init_irq(void)
80{
Rob Herringa2a47ca2012-03-09 17:16:40 -060081 gic_init(0, 29, IOMEM(CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT),
82 IOMEM(CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT));
Anton Vorontsovd94f9442010-03-25 17:12:41 +030083}
84
85void cns3xxx_power_off(void)
86{
Rob Herringa2a47ca2012-03-09 17:16:40 -060087 u32 __iomem *pm_base = IOMEM(CNS3XXX_PM_BASE_VIRT);
Anton Vorontsovd94f9442010-03-25 17:12:41 +030088 u32 clkctrl;
89
90 printk(KERN_INFO "powering system down...\n");
91
92 clkctrl = readl(pm_base + PM_SYS_CLK_CTRL_OFFSET);
93 clkctrl &= 0xfffff1ff;
94 clkctrl |= (0x5 << 9); /* Hibernate */
95 writel(clkctrl, pm_base + PM_SYS_CLK_CTRL_OFFSET);
96
97}
98
99/*
100 * Timer
101 */
102static void __iomem *cns3xxx_tmr1;
103
104static void cns3xxx_timer_set_mode(enum clock_event_mode mode,
105 struct clock_event_device *clk)
106{
107 unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
108 int pclk = cns3xxx_cpu_clock() / 8;
109 int reload;
110
111 switch (mode) {
112 case CLOCK_EVT_MODE_PERIODIC:
113 reload = pclk * 20 / (3 * HZ) * 0x25000;
114 writel(reload, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
115 ctrl |= (1 << 0) | (1 << 2) | (1 << 9);
116 break;
117 case CLOCK_EVT_MODE_ONESHOT:
118 /* period set, and timer enabled in 'next_event' hook */
119 ctrl |= (1 << 2) | (1 << 9);
120 break;
121 case CLOCK_EVT_MODE_UNUSED:
122 case CLOCK_EVT_MODE_SHUTDOWN:
123 default:
124 ctrl = 0;
125 }
126
127 writel(ctrl, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
128}
129
130static int cns3xxx_timer_set_next_event(unsigned long evt,
131 struct clock_event_device *unused)
132{
133 unsigned long ctrl = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
134
135 writel(evt, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
136 writel(ctrl | (1 << 0), cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
137
138 return 0;
139}
140
141static struct clock_event_device cns3xxx_tmr1_clockevent = {
142 .name = "cns3xxx timer1",
Anton Vorontsovd94f9442010-03-25 17:12:41 +0300143 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
144 .set_mode = cns3xxx_timer_set_mode,
145 .set_next_event = cns3xxx_timer_set_next_event,
146 .rating = 350,
147 .cpumask = cpu_all_mask,
148};
149
150static void __init cns3xxx_clockevents_init(unsigned int timer_irq)
151{
152 cns3xxx_tmr1_clockevent.irq = timer_irq;
Shawn Guo838a2ae2013-01-12 11:50:05 +0000153 clockevents_config_and_register(&cns3xxx_tmr1_clockevent,
154 (cns3xxx_cpu_clock() >> 3) * 1000000,
155 0xf, 0xffffffff);
Anton Vorontsovd94f9442010-03-25 17:12:41 +0300156}
157
158/*
159 * IRQ handler for the timer
160 */
161static irqreturn_t cns3xxx_timer_interrupt(int irq, void *dev_id)
162{
163 struct clock_event_device *evt = &cns3xxx_tmr1_clockevent;
164 u32 __iomem *stat = cns3xxx_tmr1 + TIMER1_2_INTERRUPT_STATUS_OFFSET;
165 u32 val;
166
167 /* Clear the interrupt */
168 val = readl(stat);
169 writel(val & ~(1 << 2), stat);
170
171 evt->event_handler(evt);
172
173 return IRQ_HANDLED;
174}
175
176static struct irqaction cns3xxx_timer_irq = {
177 .name = "timer",
178 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
179 .handler = cns3xxx_timer_interrupt,
180};
181
182/*
183 * Set up the clock source and clock events devices
184 */
185static void __init __cns3xxx_timer_init(unsigned int timer_irq)
186{
187 u32 val;
188 u32 irq_mask;
189
190 /*
191 * Initialise to a known state (all timers off)
192 */
193
194 /* disable timer1 and timer2 */
195 writel(0, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
196 /* stop free running timer3 */
197 writel(0, cns3xxx_tmr1 + TIMER_FREERUN_CONTROL_OFFSET);
198
199 /* timer1 */
200 writel(0x5C800, cns3xxx_tmr1 + TIMER1_COUNTER_OFFSET);
201 writel(0x5C800, cns3xxx_tmr1 + TIMER1_AUTO_RELOAD_OFFSET);
202
203 writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V1_OFFSET);
204 writel(0, cns3xxx_tmr1 + TIMER1_MATCH_V2_OFFSET);
205
206 /* mask irq, non-mask timer1 overflow */
207 irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
208 irq_mask &= ~(1 << 2);
209 irq_mask |= 0x03;
210 writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
211
212 /* down counter */
213 val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
214 val |= (1 << 9);
215 writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
216
217 /* timer2 */
218 writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V1_OFFSET);
219 writel(0, cns3xxx_tmr1 + TIMER2_MATCH_V2_OFFSET);
220
221 /* mask irq */
222 irq_mask = readl(cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
223 irq_mask |= ((1 << 3) | (1 << 4) | (1 << 5));
224 writel(irq_mask, cns3xxx_tmr1 + TIMER1_2_INTERRUPT_MASK_OFFSET);
225
226 /* down counter */
227 val = readl(cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
228 val |= (1 << 10);
229 writel(val, cns3xxx_tmr1 + TIMER1_2_CONTROL_OFFSET);
230
231 /* Make irqs happen for the system timer */
232 setup_irq(timer_irq, &cns3xxx_timer_irq);
233
234 cns3xxx_clockevents_init(timer_irq);
235}
236
Stephen Warren6bb27d72012-11-08 12:40:59 -0700237void __init cns3xxx_timer_init(void)
Anton Vorontsovd94f9442010-03-25 17:12:41 +0300238{
Rob Herringa2a47ca2012-03-09 17:16:40 -0600239 cns3xxx_tmr1 = IOMEM(CNS3XXX_TIMER1_2_3_BASE_VIRT);
Anton Vorontsovd94f9442010-03-25 17:12:41 +0300240
241 __cns3xxx_timer_init(IRQ_CNS3XXX_TIMER0);
242}
243
Anton Vorontsov93e85d82011-07-06 16:45:09 +0400244#ifdef CONFIG_CACHE_L2X0
245
246void __init cns3xxx_l2x0_init(void)
247{
248 void __iomem *base = ioremap(CNS3XXX_L2C_BASE, SZ_4K);
249 u32 val;
250
251 if (WARN_ON(!base))
252 return;
253
254 /*
255 * Tag RAM Control register
256 *
257 * bit[10:8] - 1 cycle of write accesses latency
258 * bit[6:4] - 1 cycle of read accesses latency
259 * bit[3:0] - 1 cycle of setup latency
260 *
261 * 1 cycle of latency for setup, read and write accesses
262 */
263 val = readl(base + L2X0_TAG_LATENCY_CTRL);
264 val &= 0xfffff888;
265 writel(val, base + L2X0_TAG_LATENCY_CTRL);
266
267 /*
268 * Data RAM Control register
269 *
270 * bit[10:8] - 1 cycles of write accesses latency
271 * bit[6:4] - 1 cycles of read accesses latency
272 * bit[3:0] - 1 cycle of setup latency
273 *
274 * 1 cycle of latency for setup, read and write accesses
275 */
276 val = readl(base + L2X0_DATA_LATENCY_CTRL);
277 val &= 0xfffff888;
278 writel(val, base + L2X0_DATA_LATENCY_CTRL);
279
280 /* 32 KiB, 8-way, parity disable */
281 l2x0_init(base, 0x00540000, 0xfe000fff);
282}
283
284#endif /* CONFIG_CACHE_L2X0 */
Arnd Bergmann415f59142013-03-14 22:27:32 +0100285
286static int csn3xxx_usb_power_on(struct platform_device *pdev)
287{
288 /*
289 * EHCI and OHCI share the same clock and power,
290 * resetting twice would cause the 1st controller been reset.
291 * Therefore only do power up at the first up device, and
292 * power down at the last down device.
293 *
294 * Set USB AHB INCR length to 16
295 */
296 if (atomic_inc_return(&usb_pwr_ref) == 1) {
297 cns3xxx_pwr_power_up(1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_PLL_USB);
298 cns3xxx_pwr_clk_en(1 << PM_CLK_GATE_REG_OFFSET_USB_HOST);
299 cns3xxx_pwr_soft_rst(1 << PM_SOFT_RST_REG_OFFST_USB_HOST);
300 __raw_writel((__raw_readl(MISC_CHIP_CONFIG_REG) | (0X2 << 24)),
301 MISC_CHIP_CONFIG_REG);
302 }
303
304 return 0;
305}
306
307static void csn3xxx_usb_power_off(struct platform_device *pdev)
308{
309 /*
310 * EHCI and OHCI share the same clock and power,
311 * resetting twice would cause the 1st controller been reset.
312 * Therefore only do power up at the first up device, and
313 * power down at the last down device.
314 */
315 if (atomic_dec_return(&usb_pwr_ref) == 0)
316 cns3xxx_pwr_clk_dis(1 << PM_CLK_GATE_REG_OFFSET_USB_HOST);
317}
318
319static struct usb_ehci_pdata cns3xxx_usb_ehci_pdata = {
320 .power_on = csn3xxx_usb_power_on,
321 .power_off = csn3xxx_usb_power_off,
322};
323
324static struct usb_ohci_pdata cns3xxx_usb_ohci_pdata = {
325 .num_ports = 1,
326 .power_on = csn3xxx_usb_power_on,
327 .power_off = csn3xxx_usb_power_off,
328};
329
330static struct of_dev_auxdata cns3xxx_auxdata[] __initconst = {
331 { "intel,usb-ehci", CNS3XXX_USB_BASE, "ehci-platform", &cns3xxx_usb_ehci_pdata },
332 { "intel,usb-ohci", CNS3XXX_USB_OHCI_BASE, "ohci-platform", &cns3xxx_usb_ohci_pdata },
333 { "cavium,cns3420-ahci", CNS3XXX_SATA2_BASE, "ahci", NULL },
334 { "cavium,cns3420-sdhci", CNS3XXX_SDIO_BASE, "ahci", NULL },
335 {},
336};
337
338static void __init cns3xxx_init(void)
339{
340 struct device_node *dn;
341
342 cns3xxx_l2x0_init();
343
344 dn = of_find_compatible_node(NULL, NULL, "cavium,cns3420-ahci");
345 if (of_device_is_available(dn)) {
346 u32 tmp;
347
348 tmp = __raw_readl(MISC_SATA_POWER_MODE);
349 tmp |= 0x1 << 16; /* Disable SATA PHY 0 from SLUMBER Mode */
350 tmp |= 0x1 << 17; /* Disable SATA PHY 1 from SLUMBER Mode */
351 __raw_writel(tmp, MISC_SATA_POWER_MODE);
352
353 /* Enable SATA PHY */
354 cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY0);
355 cns3xxx_pwr_power_up(0x1 << PM_PLL_HM_PD_CTRL_REG_OFFSET_SATA_PHY1);
356
357 /* Enable SATA Clock */
358 cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_SATA);
359
360 /* De-Asscer SATA Reset */
361 cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SATA));
362 }
363
364 dn = of_find_compatible_node(NULL, NULL, "cavium,cns3420-sdhci");
365 if (of_device_is_available(dn)) {
366 u32 __iomem *gpioa = IOMEM(CNS3XXX_MISC_BASE_VIRT + 0x0014);
367 u32 gpioa_pins = __raw_readl(gpioa);
368
369 /* MMC/SD pins share with GPIOA */
370 gpioa_pins |= 0x1fff0004;
371 __raw_writel(gpioa_pins, gpioa);
372
373 cns3xxx_pwr_clk_en(CNS3XXX_PWR_CLK_EN(SDIO));
374 cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SDIO));
375 }
376
377 pm_power_off = cns3xxx_power_off;
378
379 of_platform_populate(NULL, of_default_bus_match_table,
380 cns3xxx_auxdata, NULL);
381}
382
383static const char *cns3xxx_dt_compat[] __initdata = {
384 "cavium,cns3410",
385 "cavium,cns3420",
386 NULL,
387};
388
389DT_MACHINE_START(CNS3XXX_DT, "Cavium Networks CNS3xxx")
390 .dt_compat = cns3xxx_dt_compat,
391 .nr_irqs = NR_IRQS_CNS3XXX,
392 .map_io = cns3xxx_map_io,
393 .init_irq = cns3xxx_init_irq,
394 .init_time = cns3xxx_timer_init,
395 .init_machine = cns3xxx_init,
396 .restart = cns3xxx_restart,
397MACHINE_END