2 * Copyright 1999 - 2003 ARM Limited
3 * Copyright 2000 Deep Blue Solutions Ltd
4 * Copyright 2008 Cavium Networks
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.
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/clockchips.h>
15 #include <asm/mach/map.h>
16 #include <asm/mach/time.h>
17 #include <asm/mach/irq.h>
18 #include <asm/hardware/gic.h>
19 #include <mach/cns3xxx.h>
22 static struct map_desc cns3xxx_io_desc
[] __initdata
= {
24 .virtual = CNS3XXX_TC11MP_TWD_BASE_VIRT
,
25 .pfn
= __phys_to_pfn(CNS3XXX_TC11MP_TWD_BASE
),
29 .virtual = CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT
,
30 .pfn
= __phys_to_pfn(CNS3XXX_TC11MP_GIC_CPU_BASE
),
34 .virtual = CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT
,
35 .pfn
= __phys_to_pfn(CNS3XXX_TC11MP_GIC_DIST_BASE
),
39 .virtual = CNS3XXX_TIMER1_2_3_BASE_VIRT
,
40 .pfn
= __phys_to_pfn(CNS3XXX_TIMER1_2_3_BASE
),
44 .virtual = CNS3XXX_GPIOA_BASE_VIRT
,
45 .pfn
= __phys_to_pfn(CNS3XXX_GPIOA_BASE
),
49 .virtual = CNS3XXX_GPIOB_BASE_VIRT
,
50 .pfn
= __phys_to_pfn(CNS3XXX_GPIOB_BASE
),
54 .virtual = CNS3XXX_MISC_BASE_VIRT
,
55 .pfn
= __phys_to_pfn(CNS3XXX_MISC_BASE
),
59 .virtual = CNS3XXX_PM_BASE_VIRT
,
60 .pfn
= __phys_to_pfn(CNS3XXX_PM_BASE
),
66 void __init
cns3xxx_map_io(void)
68 iotable_init(cns3xxx_io_desc
, ARRAY_SIZE(cns3xxx_io_desc
));
71 /* used by entry-macro.S */
72 void __iomem
*gic_cpu_base_addr
;
74 void __init
cns3xxx_init_irq(void)
76 gic_cpu_base_addr
= __io(CNS3XXX_TC11MP_GIC_CPU_BASE_VIRT
);
77 gic_dist_init(0, __io(CNS3XXX_TC11MP_GIC_DIST_BASE_VIRT
), 29);
78 gic_cpu_init(0, gic_cpu_base_addr
);
81 void cns3xxx_power_off(void)
83 u32 __iomem
*pm_base
= __io(CNS3XXX_PM_BASE_VIRT
);
86 printk(KERN_INFO
"powering system down...\n");
88 clkctrl
= readl(pm_base
+ PM_SYS_CLK_CTRL_OFFSET
);
89 clkctrl
&= 0xfffff1ff;
90 clkctrl
|= (0x5 << 9); /* Hibernate */
91 writel(clkctrl
, pm_base
+ PM_SYS_CLK_CTRL_OFFSET
);
98 static void __iomem
*cns3xxx_tmr1
;
100 static void cns3xxx_timer_set_mode(enum clock_event_mode mode
,
101 struct clock_event_device
*clk
)
103 unsigned long ctrl
= readl(cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
104 int pclk
= cns3xxx_cpu_clock() / 8;
108 case CLOCK_EVT_MODE_PERIODIC
:
109 reload
= pclk
* 20 / (3 * HZ
) * 0x25000;
110 writel(reload
, cns3xxx_tmr1
+ TIMER1_AUTO_RELOAD_OFFSET
);
111 ctrl
|= (1 << 0) | (1 << 2) | (1 << 9);
113 case CLOCK_EVT_MODE_ONESHOT
:
114 /* period set, and timer enabled in 'next_event' hook */
115 ctrl
|= (1 << 2) | (1 << 9);
117 case CLOCK_EVT_MODE_UNUSED
:
118 case CLOCK_EVT_MODE_SHUTDOWN
:
123 writel(ctrl
, cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
126 static int cns3xxx_timer_set_next_event(unsigned long evt
,
127 struct clock_event_device
*unused
)
129 unsigned long ctrl
= readl(cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
131 writel(evt
, cns3xxx_tmr1
+ TIMER1_AUTO_RELOAD_OFFSET
);
132 writel(ctrl
| (1 << 0), cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
137 static struct clock_event_device cns3xxx_tmr1_clockevent
= {
138 .name
= "cns3xxx timer1",
140 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
141 .set_mode
= cns3xxx_timer_set_mode
,
142 .set_next_event
= cns3xxx_timer_set_next_event
,
144 .cpumask
= cpu_all_mask
,
147 static void __init
cns3xxx_clockevents_init(unsigned int timer_irq
)
149 cns3xxx_tmr1_clockevent
.irq
= timer_irq
;
150 cns3xxx_tmr1_clockevent
.mult
=
151 div_sc((cns3xxx_cpu_clock() >> 3) * 1000000, NSEC_PER_SEC
,
152 cns3xxx_tmr1_clockevent
.shift
);
153 cns3xxx_tmr1_clockevent
.max_delta_ns
=
154 clockevent_delta2ns(0xffffffff, &cns3xxx_tmr1_clockevent
);
155 cns3xxx_tmr1_clockevent
.min_delta_ns
=
156 clockevent_delta2ns(0xf, &cns3xxx_tmr1_clockevent
);
158 clockevents_register_device(&cns3xxx_tmr1_clockevent
);
162 * IRQ handler for the timer
164 static irqreturn_t
cns3xxx_timer_interrupt(int irq
, void *dev_id
)
166 struct clock_event_device
*evt
= &cns3xxx_tmr1_clockevent
;
167 u32 __iomem
*stat
= cns3xxx_tmr1
+ TIMER1_2_INTERRUPT_STATUS_OFFSET
;
170 /* Clear the interrupt */
172 writel(val
& ~(1 << 2), stat
);
174 evt
->event_handler(evt
);
179 static struct irqaction cns3xxx_timer_irq
= {
181 .flags
= IRQF_DISABLED
| IRQF_TIMER
| IRQF_IRQPOLL
,
182 .handler
= cns3xxx_timer_interrupt
,
186 * Set up the clock source and clock events devices
188 static void __init
__cns3xxx_timer_init(unsigned int timer_irq
)
194 * Initialise to a known state (all timers off)
197 /* disable timer1 and timer2 */
198 writel(0, cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
199 /* stop free running timer3 */
200 writel(0, cns3xxx_tmr1
+ TIMER_FREERUN_CONTROL_OFFSET
);
203 writel(0x5C800, cns3xxx_tmr1
+ TIMER1_COUNTER_OFFSET
);
204 writel(0x5C800, cns3xxx_tmr1
+ TIMER1_AUTO_RELOAD_OFFSET
);
206 writel(0, cns3xxx_tmr1
+ TIMER1_MATCH_V1_OFFSET
);
207 writel(0, cns3xxx_tmr1
+ TIMER1_MATCH_V2_OFFSET
);
209 /* mask irq, non-mask timer1 overflow */
210 irq_mask
= readl(cns3xxx_tmr1
+ TIMER1_2_INTERRUPT_MASK_OFFSET
);
211 irq_mask
&= ~(1 << 2);
213 writel(irq_mask
, cns3xxx_tmr1
+ TIMER1_2_INTERRUPT_MASK_OFFSET
);
216 val
= readl(cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
218 writel(val
, cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
221 writel(0, cns3xxx_tmr1
+ TIMER2_MATCH_V1_OFFSET
);
222 writel(0, cns3xxx_tmr1
+ TIMER2_MATCH_V2_OFFSET
);
225 irq_mask
= readl(cns3xxx_tmr1
+ TIMER1_2_INTERRUPT_MASK_OFFSET
);
226 irq_mask
|= ((1 << 3) | (1 << 4) | (1 << 5));
227 writel(irq_mask
, cns3xxx_tmr1
+ TIMER1_2_INTERRUPT_MASK_OFFSET
);
230 val
= readl(cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
232 writel(val
, cns3xxx_tmr1
+ TIMER1_2_CONTROL_OFFSET
);
234 /* Make irqs happen for the system timer */
235 setup_irq(timer_irq
, &cns3xxx_timer_irq
);
237 cns3xxx_clockevents_init(timer_irq
);
240 static void __init
cns3xxx_timer_init(void)
242 cns3xxx_tmr1
= __io(CNS3XXX_TIMER1_2_3_BASE_VIRT
);
244 __cns3xxx_timer_init(IRQ_CNS3XXX_TIMER0
);
247 struct sys_timer cns3xxx_timer
= {
248 .init
= cns3xxx_timer_init
,