2 * Copyright (c) 2011 Picochip Ltd., Jamie Iles
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * All enquiries to support@picochip.com
10 #include <linux/dw_apb_timer.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/sched.h>
16 #include <asm/mach/time.h>
17 #include <asm/sched_clock.h>
21 static void timer_get_base_and_rate(struct device_node *np,
22 void __iomem **base, u32 *rate)
24 *base = of_iomap(np, 0);
27 panic("Unable to map regs for %s", np->name);
29 if (of_property_read_u32(np, "clock-freq", rate))
30 panic("No clock-freq property for %s", np->name);
33 static void picoxcell_add_clockevent(struct device_node *event_timer)
36 struct dw_apb_clock_event_device *ced;
39 irq = irq_of_parse_and_map(event_timer, 0);
41 panic("No IRQ for clock event timer");
43 timer_get_base_and_rate(event_timer, &iobase, &rate);
45 ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
48 panic("Unable to initialise clockevent device");
50 dw_apb_clockevent_register(ced);
53 static void picoxcell_add_clocksource(struct device_node *source_timer)
56 struct dw_apb_clocksource *cs;
59 timer_get_base_and_rate(source_timer, &iobase, &rate);
61 cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
63 panic("Unable to initialise clocksource device");
65 dw_apb_clocksource_start(cs);
66 dw_apb_clocksource_register(cs);
69 static DEFINE_CLOCK_DATA(cd);
70 static void __iomem *sched_io_base;
72 unsigned long long notrace sched_clock(void)
74 cycle_t cyc = sched_io_base ? __raw_readl(sched_io_base) : 0;
76 return cyc_to_sched_clock(&cd, cyc, (u32)~0);
79 static void notrace picoxcell_update_sched_clock(void)
81 cycle_t cyc = sched_io_base ? __raw_readl(sched_io_base) : 0;
83 update_sched_clock(&cd, cyc, (u32)~0);
86 static const struct of_device_id picoxcell_rtc_ids[] __initconst = {
87 { .compatible = "picochip,pc3x2-rtc" },
91 static void picoxcell_init_sched_clock(void)
93 struct device_node *sched_timer;
96 sched_timer = of_find_matching_node(NULL, picoxcell_rtc_ids);
98 panic("No RTC for sched clock to use");
100 timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
101 of_node_put(sched_timer);
103 init_sched_clock(&cd, picoxcell_update_sched_clock, 32, rate);
106 static const struct of_device_id picoxcell_timer_ids[] __initconst = {
107 { .compatible = "picochip,pc3x2-timer" },
111 static void __init picoxcell_timer_init(void)
113 struct device_node *event_timer, *source_timer;
115 event_timer = of_find_matching_node(NULL, picoxcell_timer_ids);
117 panic("No timer for clockevent");
118 picoxcell_add_clockevent(event_timer);
120 source_timer = of_find_matching_node(event_timer, picoxcell_timer_ids);
122 panic("No timer for clocksource");
123 picoxcell_add_clocksource(source_timer);
125 of_node_put(source_timer);
127 picoxcell_init_sched_clock();
130 struct sys_timer picoxcell_timer = {
131 .init = picoxcell_timer_init,