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>
23 #include <linux/sched_clock.h>
25 static void timer_get_base_and_rate(struct device_node *np,
26 void __iomem **base, u32 *rate)
28 *base = of_iomap(np, 0);
31 panic("Unable to map regs for %s", np->name);
33 if (of_property_read_u32(np, "clock-freq", rate) &&
34 of_property_read_u32(np, "clock-frequency", rate))
35 panic("No clock-frequency property for %s", np->name);
38 static void add_clockevent(struct device_node *event_timer)
41 struct dw_apb_clock_event_device *ced;
44 irq = irq_of_parse_and_map(event_timer, 0);
46 panic("No IRQ for clock event timer");
48 timer_get_base_and_rate(event_timer, &iobase, &rate);
50 ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
53 panic("Unable to initialise clockevent device");
55 dw_apb_clockevent_register(ced);
58 static void add_clocksource(struct device_node *source_timer)
61 struct dw_apb_clocksource *cs;
64 timer_get_base_and_rate(source_timer, &iobase, &rate);
66 cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
68 panic("Unable to initialise clocksource device");
70 dw_apb_clocksource_start(cs);
71 dw_apb_clocksource_register(cs);
74 static void __iomem *sched_io_base;
76 static u32 read_sched_clock(void)
78 return __raw_readl(sched_io_base);
81 static const struct of_device_id sptimer_ids[] __initconst = {
82 { .compatible = "picochip,pc3x2-rtc" },
83 { .compatible = "snps,dw-apb-timer-sp" },
87 static void init_sched_clock(void)
89 struct device_node *sched_timer;
92 sched_timer = of_find_matching_node(NULL, sptimer_ids);
94 panic("No RTC for sched clock to use");
96 timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
97 of_node_put(sched_timer);
99 setup_sched_clock(read_sched_clock, 32, rate);
102 static const struct of_device_id osctimer_ids[] __initconst = {
103 { .compatible = "picochip,pc3x2-timer" },
104 { .compatible = "snps,dw-apb-timer-osc" },
108 void __init dw_apb_timer_init(void)
110 struct device_node *event_timer, *source_timer;
112 event_timer = of_find_matching_node(NULL, osctimer_ids);
114 panic("No timer for clockevent");
115 add_clockevent(event_timer);
117 source_timer = of_find_matching_node(event_timer, osctimer_ids);
119 panic("No timer for clocksource");
120 add_clocksource(source_timer);
122 of_node_put(source_timer);