Merge tag 'regmap-v3.10-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6.git] / drivers / clocksource / bcm2835_timer.c
blob766611d299455efa3699fe49d5aa9a42812bd226
1 /*
2 * Copyright 2012 Simon Arlott
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/bitops.h>
20 #include <linux/clockchips.h>
21 #include <linux/clocksource.h>
22 #include <linux/interrupt.h>
23 #include <linux/irqreturn.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_platform.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
32 #include <asm/sched_clock.h>
33 #include <asm/irq.h>
35 #define REG_CONTROL 0x00
36 #define REG_COUNTER_LO 0x04
37 #define REG_COUNTER_HI 0x08
38 #define REG_COMPARE(n) (0x0c + (n) * 4)
39 #define MAX_TIMER 3
40 #define DEFAULT_TIMER 3
42 struct bcm2835_timer {
43 void __iomem *control;
44 void __iomem *compare;
45 int match_mask;
46 struct clock_event_device evt;
47 struct irqaction act;
50 static void __iomem *system_clock __read_mostly;
52 static u32 notrace bcm2835_sched_read(void)
54 return readl_relaxed(system_clock);
57 static void bcm2835_time_set_mode(enum clock_event_mode mode,
58 struct clock_event_device *evt_dev)
60 switch (mode) {
61 case CLOCK_EVT_MODE_ONESHOT:
62 case CLOCK_EVT_MODE_UNUSED:
63 case CLOCK_EVT_MODE_SHUTDOWN:
64 case CLOCK_EVT_MODE_RESUME:
65 break;
66 default:
67 WARN(1, "%s: unhandled event mode %d\n", __func__, mode);
68 break;
72 static int bcm2835_time_set_next_event(unsigned long event,
73 struct clock_event_device *evt_dev)
75 struct bcm2835_timer *timer = container_of(evt_dev,
76 struct bcm2835_timer, evt);
77 writel_relaxed(readl_relaxed(system_clock) + event,
78 timer->compare);
79 return 0;
82 static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id)
84 struct bcm2835_timer *timer = dev_id;
85 void (*event_handler)(struct clock_event_device *);
86 if (readl_relaxed(timer->control) & timer->match_mask) {
87 writel_relaxed(timer->match_mask, timer->control);
89 event_handler = ACCESS_ONCE(timer->evt.event_handler);
90 if (event_handler)
91 event_handler(&timer->evt);
92 return IRQ_HANDLED;
93 } else {
94 return IRQ_NONE;
98 static void __init bcm2835_timer_init(struct device_node *node)
100 void __iomem *base;
101 u32 freq;
102 int irq;
103 struct bcm2835_timer *timer;
105 base = of_iomap(node, 0);
106 if (!base)
107 panic("Can't remap registers");
109 if (of_property_read_u32(node, "clock-frequency", &freq))
110 panic("Can't read clock-frequency");
112 system_clock = base + REG_COUNTER_LO;
113 setup_sched_clock(bcm2835_sched_read, 32, freq);
115 clocksource_mmio_init(base + REG_COUNTER_LO, node->name,
116 freq, 300, 32, clocksource_mmio_readl_up);
118 irq = irq_of_parse_and_map(node, DEFAULT_TIMER);
119 if (irq <= 0)
120 panic("Can't parse IRQ");
122 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
123 if (!timer)
124 panic("Can't allocate timer struct\n");
126 timer->control = base + REG_CONTROL;
127 timer->compare = base + REG_COMPARE(DEFAULT_TIMER);
128 timer->match_mask = BIT(DEFAULT_TIMER);
129 timer->evt.name = node->name;
130 timer->evt.rating = 300;
131 timer->evt.features = CLOCK_EVT_FEAT_ONESHOT;
132 timer->evt.set_mode = bcm2835_time_set_mode;
133 timer->evt.set_next_event = bcm2835_time_set_next_event;
134 timer->evt.cpumask = cpumask_of(0);
135 timer->act.name = node->name;
136 timer->act.flags = IRQF_TIMER | IRQF_SHARED;
137 timer->act.dev_id = timer;
138 timer->act.handler = bcm2835_time_interrupt;
140 if (setup_irq(irq, &timer->act))
141 panic("Can't set up timer IRQ\n");
143 clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff);
145 pr_info("bcm2835: system timer (irq = %d)\n", irq);
147 CLOCKSOURCE_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer",
148 bcm2835_timer_init);