blob: af13b8559b613134b99ca04a0aca61caea37e560 [file] [log] [blame]
Dinh Nguyencfda5902012-07-11 15:13:16 -05001/*
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
Dinh Nguyencfda5902012-07-11 15:13:16 -050024#include <asm/sched_clock.h>
25
26static void timer_get_base_and_rate(struct device_node *np,
27 void __iomem **base, u32 *rate)
28{
29 *base = of_iomap(np, 0);
30
31 if (!*base)
32 panic("Unable to map regs for %s", np->name);
33
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);
37}
38
39static void add_clockevent(struct device_node *event_timer)
40{
41 void __iomem *iobase;
42 struct dw_apb_clock_event_device *ced;
43 u32 irq, rate;
44
45 irq = irq_of_parse_and_map(event_timer, 0);
Baruch Siach1a33bd22013-05-29 10:11:17 +020046 if (irq == 0)
Dinh Nguyencfda5902012-07-11 15:13:16 -050047 panic("No IRQ for clock event timer");
48
49 timer_get_base_and_rate(event_timer, &iobase, &rate);
50
51 ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
52 rate);
53 if (!ced)
54 panic("Unable to initialise clockevent device");
55
56 dw_apb_clockevent_register(ced);
57}
58
Pavel Machek55a68c22013-05-07 22:11:26 +020059static void __iomem *sched_io_base;
60
61/* This is actually same as __apbt_read_clocksource(), but with
62 different interface */
63static u32 read_sched_clock_sptimer(void)
64{
65 return ~__raw_readl(sched_io_base + APBTMR_N_CURRENT_VALUE);
66}
67
Dinh Nguyencfda5902012-07-11 15:13:16 -050068static void add_clocksource(struct device_node *source_timer)
69{
70 void __iomem *iobase;
71 struct dw_apb_clocksource *cs;
72 u32 rate;
73
74 timer_get_base_and_rate(source_timer, &iobase, &rate);
75
76 cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
77 if (!cs)
78 panic("Unable to initialise clocksource device");
79
80 dw_apb_clocksource_start(cs);
81 dw_apb_clocksource_register(cs);
Dinh Nguyencfda5902012-07-11 15:13:16 -050082
Pavel Machek55a68c22013-05-07 22:11:26 +020083 sched_io_base = iobase;
84 setup_sched_clock(read_sched_clock_sptimer, 32, rate);
Dinh Nguyencfda5902012-07-11 15:13:16 -050085}
86
87static const struct of_device_id osctimer_ids[] __initconst = {
88 { .compatible = "picochip,pc3x2-timer" },
89 { .compatible = "snps,dw-apb-timer-osc" },
Pavel Machek55a68c22013-05-07 22:11:26 +020090 { .compatible = "snps,dw-apb-timer-sp" },
91 { /* Sentinel */ },
Dinh Nguyencfda5902012-07-11 15:13:16 -050092};
93
Pavel Machek55a68c22013-05-07 22:11:26 +020094/*
95 You don't have to use dw_apb_timer for scheduler clock,
96 this should also work fine on arm:
97
98 twd_local_timer_of_register();
99 arch_timer_of_register();
100 arch_timer_sched_clock_init();
101*/
102
103
Stephen Warren6bb27d72012-11-08 12:40:59 -0700104void __init dw_apb_timer_init(void)
Dinh Nguyencfda5902012-07-11 15:13:16 -0500105{
106 struct device_node *event_timer, *source_timer;
107
108 event_timer = of_find_matching_node(NULL, osctimer_ids);
109 if (!event_timer)
110 panic("No timer for clockevent");
111 add_clockevent(event_timer);
112
113 source_timer = of_find_matching_node(event_timer, osctimer_ids);
114 if (!source_timer)
115 panic("No timer for clocksource");
116 add_clocksource(source_timer);
117
Pavel Machek55a68c22013-05-07 22:11:26 +0200118 of_node_put(event_timer);
Dinh Nguyencfda5902012-07-11 15:13:16 -0500119 of_node_put(source_timer);
Dinh Nguyencfda5902012-07-11 15:13:16 -0500120}