]> Pileus Git - ~andy/linux/blob - drivers/clocksource/dw_apb_timer_of.c
a97b4065dacfb17d0434ea52f9941b842389fbad
[~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 __iomem *sched_io_base;
59
60 /* This is actually same as __apbt_read_clocksource(), but with
61    different interface */
62 static u32 read_sched_clock_sptimer(void)
63 {
64         return ~__raw_readl(sched_io_base + APBTMR_N_CURRENT_VALUE);
65 }
66
67 static void add_clocksource(struct device_node *source_timer)
68 {
69         void __iomem *iobase;
70         struct dw_apb_clocksource *cs;
71         u32 rate;
72
73         timer_get_base_and_rate(source_timer, &iobase, &rate);
74
75         cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
76         if (!cs)
77                 panic("Unable to initialise clocksource device");
78
79         dw_apb_clocksource_start(cs);
80         dw_apb_clocksource_register(cs);
81
82         sched_io_base = iobase;
83         setup_sched_clock(read_sched_clock_sptimer, 32, rate);
84 }
85
86 static const struct of_device_id osctimer_ids[] __initconst = {
87         { .compatible = "picochip,pc3x2-timer" },
88         { .compatible = "snps,dw-apb-timer-osc" },
89         { .compatible = "snps,dw-apb-timer-sp" },
90         {  /* Sentinel */ },
91 };
92
93 /*
94    You don't have to use dw_apb_timer for scheduler clock,
95    this should also work fine on arm:
96
97   twd_local_timer_of_register();
98   arch_timer_of_register();
99   arch_timer_sched_clock_init();
100 */
101
102
103 void __init dw_apb_timer_init(void)
104 {
105         struct device_node *event_timer, *source_timer;
106
107         event_timer = of_find_matching_node(NULL, osctimer_ids);
108         if (!event_timer)
109                 panic("No timer for clockevent");
110         add_clockevent(event_timer);
111
112         source_timer = of_find_matching_node(event_timer, osctimer_ids);
113         if (!source_timer)
114                 panic("No timer for clocksource");
115         add_clocksource(source_timer);
116
117         of_node_put(event_timer);
118         of_node_put(source_timer);
119 }