2 * Copyright (C) 2012 Altera Corporation
3 * Copyright (c) 2011 Picochip Ltd., Jamie Iles
5 * Modified from mach-picoxcell/time.c
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/dw_apb_timer.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
24 #include <asm/sched_clock.h>
26 static void timer_get_base_and_rate(struct device_node *np,
27 void __iomem **base, u32 *rate)
29 *base = of_iomap(np, 0);
32 panic("Unable to map regs for %s", np->name);
34 if (of_property_read_u32(np, "clock-freq", rate) &&
35 of_property_read_u32(np, "clock-frequency", rate))
36 panic("No clock-frequency property for %s", np->name);
39 static void add_clockevent(struct device_node *event_timer)
42 struct dw_apb_clock_event_device *ced;
45 irq = irq_of_parse_and_map(event_timer, 0);
47 panic("No IRQ for clock event timer");
49 timer_get_base_and_rate(event_timer, &iobase, &rate);
51 ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
54 panic("Unable to initialise clockevent device");
56 dw_apb_clockevent_register(ced);
59 static void __iomem *sched_io_base;
61 /* This is actually same as __apbt_read_clocksource(), but with
62 different interface */
63 static u32 read_sched_clock_sptimer(void)
65 return ~__raw_readl(sched_io_base + APBTMR_N_CURRENT_VALUE);
68 static void add_clocksource(struct device_node *source_timer)
71 struct dw_apb_clocksource *cs;
74 timer_get_base_and_rate(source_timer, &iobase, &rate);
76 cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
78 panic("Unable to initialise clocksource device");
80 dw_apb_clocksource_start(cs);
81 dw_apb_clocksource_register(cs);
83 sched_io_base = iobase;
84 setup_sched_clock(read_sched_clock_sptimer, 32, rate);
87 static const struct of_device_id osctimer_ids[] __initconst = {
88 { .compatible = "picochip,pc3x2-timer" },
89 { .compatible = "snps,dw-apb-timer-osc" },
90 { .compatible = "snps,dw-apb-timer-sp" },
95 You don't have to use dw_apb_timer for scheduler clock,
96 this should also work fine on arm:
98 twd_local_timer_of_register();
99 arch_timer_of_register();
100 arch_timer_sched_clock_init();
104 void __init dw_apb_timer_init(void)
106 struct device_node *event_timer, *source_timer;
108 event_timer = of_find_matching_node(NULL, osctimer_ids);
110 panic("No timer for clockevent");
111 add_clockevent(event_timer);
113 source_timer = of_find_matching_node(event_timer, osctimer_ids);
115 panic("No timer for clocksource");
116 add_clocksource(source_timer);
118 of_node_put(event_timer);
119 of_node_put(source_timer);