blob: cafe98836c8a7979703874a729ce22d6ae3eec86 [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * linux/arch/arm/mach-at91/at91rm9200_time.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
4 * Copyright (C) 2003 SAN People
5 * Copyright (C) 2003 ATMEL
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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
David Brownell5e802df2007-07-31 01:41:26 +010022#include <linux/kernel.h>
SAN People73a59c12006-01-09 17:05:41 +000023#include <linux/interrupt.h>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010024#include <linux/irq.h>
David Brownell5e802df2007-07-31 01:41:26 +010025#include <linux/clockchips.h>
Joachim Eastwood9fce85c2012-04-04 19:15:15 +020026#include <linux/export.h>
Joachim Eastwood454c46d2012-10-28 18:31:07 +000027#include <linux/of.h>
28#include <linux/of_address.h>
29#include <linux/of_irq.h>
SAN People73a59c12006-01-09 17:05:41 +000030
SAN People73a59c12006-01-09 17:05:41 +000031#include <asm/mach/time.h>
32
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/at91_st.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010034
Andrew Victor963151f2006-06-19 15:23:41 +010035static unsigned long last_crtr;
David Brownell5e802df2007-07-31 01:41:26 +010036static u32 irqmask;
37static struct clock_event_device clkevt;
Andrew Victor963151f2006-06-19 15:23:41 +010038
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080039#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
40
SAN People73a59c12006-01-09 17:05:41 +000041/*
David Brownell5e802df2007-07-31 01:41:26 +010042 * The ST_CRTR is updated asynchronously to the master clock ... but
43 * the updates as seen by the CPU don't seem to be strictly monotonic.
44 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000045 */
David Brownell5e802df2007-07-31 01:41:26 +010046static inline unsigned long read_CRTR(void)
47{
SAN People73a59c12006-01-09 17:05:41 +000048 unsigned long x1, x2;
49
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010050 x1 = at91_st_read(AT91_ST_CRTR);
SAN People73a59c12006-01-09 17:05:41 +000051 do {
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010052 x2 = at91_st_read(AT91_ST_CRTR);
David Brownell5e802df2007-07-31 01:41:26 +010053 if (x1 == x2)
54 break;
55 x1 = x2;
56 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000057 return x1;
58}
59
60/*
SAN People73a59c12006-01-09 17:05:41 +000061 * IRQ handler for the timer.
62 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070063static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000064{
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010065 u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
SAN People73a59c12006-01-09 17:05:41 +000066
Uwe Kleine-König501d7032009-09-21 09:30:09 +020067 /*
68 * irqs should be disabled here, but as the irq is shared they are only
69 * guaranteed to be off if the timer irq is registered first.
70 */
71 WARN_ON_ONCE(!irqs_disabled());
72
David Brownell5e802df2007-07-31 01:41:26 +010073 /* simulate "oneshot" timer with alarm */
74 if (sr & AT91_ST_ALMS) {
75 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000076 return IRQ_HANDLED;
77 }
David Brownell5e802df2007-07-31 01:41:26 +010078
79 /* periodic mode should handle delayed ticks */
80 if (sr & AT91_ST_PITS) {
81 u32 crtr = read_CRTR();
82
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080083 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
84 last_crtr += RM9200_TIMER_LATCH;
David Brownell5e802df2007-07-31 01:41:26 +010085 clkevt.event_handler(&clkevt);
86 }
87 return IRQ_HANDLED;
88 }
89
90 /* this irq is shared ... */
91 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000092}
93
94static struct irqaction at91rm9200_timer_irq = {
95 .name = "at91_tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -070096 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Joachim Eastwood454c46d2012-10-28 18:31:07 +000097 .handler = at91rm9200_timer_interrupt,
98 .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
SAN People73a59c12006-01-09 17:05:41 +000099};
100
Magnus Damm8e196082009-04-21 12:24:00 -0700101static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +0100102{
David Brownell5e802df2007-07-31 01:41:26 +0100103 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +0100104}
105
David Brownell5e802df2007-07-31 01:41:26 +0100106static struct clocksource clk32k = {
107 .name = "32k_counter",
108 .rating = 150,
109 .read = read_clk32k,
110 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100111 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
112};
113
114static void
115clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
116{
117 /* Disable and flush pending timer interrupts */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100118 at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100119 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100120
121 last_crtr = read_CRTR();
122 switch (mode) {
123 case CLOCK_EVT_MODE_PERIODIC:
124 /* PIT for periodic irqs; fixed rate of 1/HZ */
125 irqmask = AT91_ST_PITS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100126 at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
David Brownell5e802df2007-07-31 01:41:26 +0100127 break;
128 case CLOCK_EVT_MODE_ONESHOT:
129 /* ALM for oneshot irqs, set by next_event()
130 * before 32 seconds have passed
131 */
132 irqmask = AT91_ST_ALMS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100133 at91_st_write(AT91_ST_RTAR, last_crtr);
David Brownell5e802df2007-07-31 01:41:26 +0100134 break;
135 case CLOCK_EVT_MODE_SHUTDOWN:
136 case CLOCK_EVT_MODE_UNUSED:
137 case CLOCK_EVT_MODE_RESUME:
138 irqmask = 0;
139 break;
140 }
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100141 at91_st_write(AT91_ST_IER, irqmask);
David Brownell5e802df2007-07-31 01:41:26 +0100142}
143
144static int
145clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
146{
David Brownell5e802df2007-07-31 01:41:26 +0100147 u32 alm;
148 int status = 0;
149
150 BUG_ON(delta < 2);
151
David Brownell5e802df2007-07-31 01:41:26 +0100152 /* The alarm IRQ uses absolute time (now+delta), not the relative
153 * time (delta) in our calling convention. Like all clockevents
154 * using such "match" hardware, we have a race to defend against.
155 *
156 * Our defense here is to have set up the clockevent device so the
157 * delta is at least two. That way we never end up writing RTAR
158 * with the value then held in CRTR ... which would mean the match
159 * wouldn't trigger until 32 seconds later, after CRTR wraps.
160 */
161 alm = read_CRTR();
162
163 /* Cancel any pending alarm; flush any pending IRQ */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100164 at91_st_write(AT91_ST_RTAR, alm);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100165 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100166
167 /* Schedule alarm by writing RTAR. */
168 alm += delta;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100169 at91_st_write(AT91_ST_RTAR, alm);
David Brownell5e802df2007-07-31 01:41:26 +0100170
David Brownell5e802df2007-07-31 01:41:26 +0100171 return status;
172}
173
174static struct clock_event_device clkevt = {
175 .name = "at91_tick",
176 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
177 .shift = 32,
178 .rating = 150,
David Brownell5e802df2007-07-31 01:41:26 +0100179 .set_next_event = clkevt32k_next_event,
180 .set_mode = clkevt32k_mode,
181};
182
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100183void __iomem *at91_st_base;
Joachim Eastwood9fce85c2012-04-04 19:15:15 +0200184EXPORT_SYMBOL_GPL(at91_st_base);
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100185
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000186#ifdef CONFIG_OF
187static struct of_device_id at91rm9200_st_timer_ids[] = {
188 { .compatible = "atmel,at91rm9200-st" },
189 { /* sentinel */ }
190};
191
192static int __init of_at91rm9200_st_init(void)
193{
194 struct device_node *np;
195 int ret;
196
197 np = of_find_matching_node(NULL, at91rm9200_st_timer_ids);
198 if (!np)
199 goto err;
200
201 at91_st_base = of_iomap(np, 0);
202 if (!at91_st_base)
203 goto node_err;
204
205 /* Get the interrupts property */
206 ret = irq_of_parse_and_map(np, 0);
207 if (!ret)
208 goto ioremap_err;
209 at91rm9200_timer_irq.irq = ret;
210
211 of_node_put(np);
212
213 return 0;
214
215ioremap_err:
216 iounmap(at91_st_base);
217node_err:
218 of_node_put(np);
219err:
220 return -EINVAL;
221}
222#else
223static int __init of_at91rm9200_st_init(void)
224{
225 return -EINVAL;
226}
227#endif
228
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100229void __init at91rm9200_ioremap_st(u32 addr)
230{
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000231#ifdef CONFIG_OF
232 struct device_node *np;
233
234 np = of_find_matching_node(NULL, at91rm9200_st_timer_ids);
235 if (np) {
236 of_node_put(np);
237 return;
238 }
239#endif
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100240 at91_st_base = ioremap(addr, 256);
241 if (!at91_st_base)
242 panic("Impossible to ioremap ST\n");
243}
244
SAN People73a59c12006-01-09 17:05:41 +0000245/*
David Brownell5e802df2007-07-31 01:41:26 +0100246 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000247 */
248void __init at91rm9200_timer_init(void)
249{
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000250 /* For device tree enabled device: initialize here */
251 of_at91rm9200_st_init();
252
David Brownell5e802df2007-07-31 01:41:26 +0100253 /* Disable all timer interrupts, and clear any pending ones */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100254 at91_st_write(AT91_ST_IDR,
David Brownell5e802df2007-07-31 01:41:26 +0100255 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100256 at91_st_read(AT91_ST_SR);
SAN People73a59c12006-01-09 17:05:41 +0000257
Andrew Victor2a6f9902006-06-19 15:26:50 +0100258 /* Make IRQs happen for the system timer */
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000259 setup_irq(at91rm9200_timer_irq.irq, &at91rm9200_timer_irq);
SAN People73a59c12006-01-09 17:05:41 +0000260
David Brownell5e802df2007-07-31 01:41:26 +0100261 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
262 * directly for the clocksource and all clockevents, after adjusting
263 * its prescaler from the 1 Hz default.
264 */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100265 at91_st_write(AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000266
David Brownell5e802df2007-07-31 01:41:26 +0100267 /* Setup timer clockevent, with minimum of two ticks (important!!) */
268 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
269 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
270 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030271 clkevt.cpumask = cpumask_of(0);
David Brownell5e802df2007-07-31 01:41:26 +0100272 clockevents_register_device(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +0000273
David Brownell5e802df2007-07-31 01:41:26 +0100274 /* register clocksource */
Russell King132b1632010-12-13 13:14:55 +0000275 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100276}
Andrew Victor2a6f9902006-06-19 15:26:50 +0100277
SAN People73a59c12006-01-09 17:05:41 +0000278struct sys_timer at91rm9200_timer = {
279 .init = at91rm9200_timer_init,
SAN People73a59c12006-01-09 17:05:41 +0000280};
Andrew Victor2a6f9902006-06-19 15:26:50 +0100281