2 * Marvell Orion SoC timer handling.
4 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
10 * Timer 0 is used as free-running clocksource, while timer 1 is
11 * used as clock_event_device.
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/clk.h>
17 #include <linux/clockchips.h>
18 #include <linux/delay.h>
19 #include <linux/interrupt.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/spinlock.h>
23 #include <linux/sched_clock.h>
25 #define TIMER_CTRL 0x00
26 #define TIMER0_EN BIT(0)
27 #define TIMER0_RELOAD_EN BIT(1)
28 #define TIMER1_EN BIT(2)
29 #define TIMER1_RELOAD_EN BIT(3)
30 #define TIMER0_RELOAD 0x10
31 #define TIMER0_VAL 0x14
32 #define TIMER1_RELOAD 0x18
33 #define TIMER1_VAL 0x1c
35 #define ORION_ONESHOT_MIN 1
36 #define ORION_ONESHOT_MAX 0xfffffffe
38 static void __iomem *timer_base;
40 static unsigned long notrace orion_read_timer(void)
42 return ~readl(timer_base + TIMER0_VAL);
45 static struct delay_timer orion_delay_timer = {
46 .read_current_timer = orion_read_timer,
49 static void orion_delay_timer_init(unsigned long rate)
51 orion_delay_timer.freq = rate;
52 register_current_timer_delay(&orion_delay_timer);
56 * Free-running clocksource handling.
58 static u64 notrace orion_read_sched_clock(void)
60 return ~readl(timer_base + TIMER0_VAL);
64 * Clockevent handling.
66 static u32 ticks_per_jiffy;
68 static int orion_clkevt_next_event(unsigned long delta,
69 struct clock_event_device *dev)
71 /* setup and enable one-shot timer */
72 writel(delta, timer_base + TIMER1_VAL);
73 atomic_io_modify(timer_base + TIMER_CTRL,
74 TIMER1_RELOAD_EN | TIMER1_EN, TIMER1_EN);
79 static int orion_clkevt_shutdown(struct clock_event_device *dev)
82 atomic_io_modify(timer_base + TIMER_CTRL,
83 TIMER1_RELOAD_EN | TIMER1_EN, 0);
87 static int orion_clkevt_set_periodic(struct clock_event_device *dev)
89 /* setup and enable periodic timer at 1/HZ intervals */
90 writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD);
91 writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL);
92 atomic_io_modify(timer_base + TIMER_CTRL,
93 TIMER1_RELOAD_EN | TIMER1_EN,
94 TIMER1_RELOAD_EN | TIMER1_EN);
98 static struct clock_event_device orion_clkevt = {
99 .name = "orion_event",
100 .features = CLOCK_EVT_FEAT_ONESHOT |
101 CLOCK_EVT_FEAT_PERIODIC,
104 .set_next_event = orion_clkevt_next_event,
105 .set_state_shutdown = orion_clkevt_shutdown,
106 .set_state_periodic = orion_clkevt_set_periodic,
107 .set_state_oneshot = orion_clkevt_shutdown,
108 .tick_resume = orion_clkevt_shutdown,
111 static irqreturn_t orion_clkevt_irq_handler(int irq, void *dev_id)
113 orion_clkevt.event_handler(&orion_clkevt);
117 static int __init orion_timer_init(struct device_node *np)
123 /* timer registers are shared with watchdog timer */
124 timer_base = of_iomap(np, 0);
126 pr_err("%pOFn: unable to map resource\n", np);
130 clk = of_clk_get(np, 0);
132 pr_err("%pOFn: unable to get clk\n", np);
136 ret = clk_prepare_enable(clk);
138 pr_err("Failed to prepare clock\n");
142 /* we are only interested in timer1 irq */
143 irq = irq_of_parse_and_map(np, 1);
145 pr_err("%pOFn: unable to parse timer1 irq\n", np);
149 rate = clk_get_rate(clk);
151 /* setup timer0 as free-running clocksource */
152 writel(~0, timer_base + TIMER0_VAL);
153 writel(~0, timer_base + TIMER0_RELOAD);
154 atomic_io_modify(timer_base + TIMER_CTRL,
155 TIMER0_RELOAD_EN | TIMER0_EN,
156 TIMER0_RELOAD_EN | TIMER0_EN);
158 ret = clocksource_mmio_init(timer_base + TIMER0_VAL,
159 "orion_clocksource", rate, 300, 32,
160 clocksource_mmio_readl_down);
162 pr_err("Failed to initialize mmio timer\n");
166 sched_clock_register(orion_read_sched_clock, 32, rate);
168 /* setup timer1 as clockevent timer */
169 ret = request_irq(irq, orion_clkevt_irq_handler, IRQF_TIMER,
170 "orion_event", NULL);
172 pr_err("%pOFn: unable to setup irq\n", np);
176 ticks_per_jiffy = (clk_get_rate(clk) + HZ/2) / HZ;
177 orion_clkevt.cpumask = cpumask_of(0);
178 orion_clkevt.irq = irq;
179 clockevents_config_and_register(&orion_clkevt, rate,
180 ORION_ONESHOT_MIN, ORION_ONESHOT_MAX);
183 orion_delay_timer_init(rate);
187 TIMER_OF_DECLARE(orion_timer, "marvell,orion-timer", orion_timer_init);