]> Pileus Git - ~andy/linux/blob - drivers/clocksource/dw_apb_timer_of.c
Revert "dw_apb_timer_of.c: Remove parts that were picoxcell-specific"
[~andy/linux] / drivers / clocksource / dw_apb_timer_of.c
1 /*
2  * Copyright (C) 2012 Altera Corporation
3  * Copyright (c) 2011 Picochip Ltd., Jamie Iles
4  *
5  * Modified from mach-picoxcell/time.c
6  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include <linux/dw_apb_timer.h>
20 #include <linux/of.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/sched_clock.h>
24
25 static void timer_get_base_and_rate(struct device_node *np,
26                                     void __iomem **base, u32 *rate)
27 {
28         *base = of_iomap(np, 0);
29
30         if (!*base)
31                 panic("Unable to map regs for %s", np->name);
32
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);
36 }
37
38 static void add_clockevent(struct device_node *event_timer)
39 {
40         void __iomem *iobase;
41         struct dw_apb_clock_event_device *ced;
42         u32 irq, rate;
43
44         irq = irq_of_parse_and_map(event_timer, 0);
45         if (irq == 0)
46                 panic("No IRQ for clock event timer");
47
48         timer_get_base_and_rate(event_timer, &iobase, &rate);
49
50         ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
51                                      rate);
52         if (!ced)
53                 panic("Unable to initialise clockevent device");
54
55         dw_apb_clockevent_register(ced);
56 }
57
58 static void add_clocksource(struct device_node *source_timer)
59 {
60         void __iomem *iobase;
61         struct dw_apb_clocksource *cs;
62         u32 rate;
63
64         timer_get_base_and_rate(source_timer, &iobase, &rate);
65
66         cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
67         if (!cs)
68                 panic("Unable to initialise clocksource device");
69
70         dw_apb_clocksource_start(cs);
71         dw_apb_clocksource_register(cs);
72 }
73
74 static void __iomem *sched_io_base;
75
76 static u32 read_sched_clock(void)
77 {
78         return __raw_readl(sched_io_base);
79 }
80
81 static const struct of_device_id sptimer_ids[] __initconst = {
82         { .compatible = "picochip,pc3x2-rtc" },
83         { .compatible = "snps,dw-apb-timer-sp" },
84         { /* Sentinel */ },
85 };
86
87 static void init_sched_clock(void)
88 {
89         struct device_node *sched_timer;
90         u32 rate;
91
92         sched_timer = of_find_matching_node(NULL, sptimer_ids);
93         if (!sched_timer)
94                 panic("No RTC for sched clock to use");
95
96         timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
97         of_node_put(sched_timer);
98
99         setup_sched_clock(read_sched_clock, 32, rate);
100 }
101
102 static const struct of_device_id osctimer_ids[] __initconst = {
103         { .compatible = "picochip,pc3x2-timer" },
104         { .compatible = "snps,dw-apb-timer-osc" },
105         {},
106 };
107
108 void __init dw_apb_timer_init(void)
109 {
110         struct device_node *event_timer, *source_timer;
111
112         event_timer = of_find_matching_node(NULL, osctimer_ids);
113         if (!event_timer)
114                 panic("No timer for clockevent");
115         add_clockevent(event_timer);
116
117         source_timer = of_find_matching_node(event_timer, osctimer_ids);
118         if (!source_timer)
119                 panic("No timer for clocksource");
120         add_clocksource(source_timer);
121
122         of_node_put(source_timer);
123
124         init_sched_clock();
125 }