blob: 7d062ab326740ff775cd30e8920acad34198ece6 [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>
Uwe Kleine-Königac11a1d2013-11-14 10:49:19 +010034#include <mach/hardware.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010035
Andrew Victor963151f2006-06-19 15:23:41 +010036static unsigned long last_crtr;
David Brownell5e802df2007-07-31 01:41:26 +010037static u32 irqmask;
38static struct clock_event_device clkevt;
Andrew Victor963151f2006-06-19 15:23:41 +010039
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080040#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
41
SAN People73a59c12006-01-09 17:05:41 +000042/*
David Brownell5e802df2007-07-31 01:41:26 +010043 * The ST_CRTR is updated asynchronously to the master clock ... but
44 * the updates as seen by the CPU don't seem to be strictly monotonic.
45 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000046 */
David Brownell5e802df2007-07-31 01:41:26 +010047static inline unsigned long read_CRTR(void)
48{
SAN People73a59c12006-01-09 17:05:41 +000049 unsigned long x1, x2;
50
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010051 x1 = at91_st_read(AT91_ST_CRTR);
SAN People73a59c12006-01-09 17:05:41 +000052 do {
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010053 x2 = at91_st_read(AT91_ST_CRTR);
David Brownell5e802df2007-07-31 01:41:26 +010054 if (x1 == x2)
55 break;
56 x1 = x2;
57 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000058 return x1;
59}
60
61/*
SAN People73a59c12006-01-09 17:05:41 +000062 * IRQ handler for the timer.
63 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070064static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000065{
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010066 u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
SAN People73a59c12006-01-09 17:05:41 +000067
Uwe Kleine-König501d7032009-09-21 09:30:09 +020068 /*
69 * irqs should be disabled here, but as the irq is shared they are only
70 * guaranteed to be off if the timer irq is registered first.
71 */
72 WARN_ON_ONCE(!irqs_disabled());
73
David Brownell5e802df2007-07-31 01:41:26 +010074 /* simulate "oneshot" timer with alarm */
75 if (sr & AT91_ST_ALMS) {
76 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000077 return IRQ_HANDLED;
78 }
David Brownell5e802df2007-07-31 01:41:26 +010079
80 /* periodic mode should handle delayed ticks */
81 if (sr & AT91_ST_PITS) {
82 u32 crtr = read_CRTR();
83
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080084 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
85 last_crtr += RM9200_TIMER_LATCH;
David Brownell5e802df2007-07-31 01:41:26 +010086 clkevt.event_handler(&clkevt);
87 }
88 return IRQ_HANDLED;
89 }
90
91 /* this irq is shared ... */
92 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000093}
94
95static struct irqaction at91rm9200_timer_irq = {
96 .name = "at91_tick",
Michael Opdenacker9ceb3892013-09-04 06:54:39 +020097 .flags = IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
Joachim Eastwood454c46d2012-10-28 18:31:07 +000098 .handler = at91rm9200_timer_interrupt,
99 .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
SAN People73a59c12006-01-09 17:05:41 +0000100};
101
Magnus Damm8e196082009-04-21 12:24:00 -0700102static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +0100103{
David Brownell5e802df2007-07-31 01:41:26 +0100104 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +0100105}
106
David Brownell5e802df2007-07-31 01:41:26 +0100107static struct clocksource clk32k = {
108 .name = "32k_counter",
109 .rating = 150,
110 .read = read_clk32k,
111 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100112 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
113};
114
115static void
116clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
117{
118 /* Disable and flush pending timer interrupts */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100119 at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100120 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100121
122 last_crtr = read_CRTR();
123 switch (mode) {
124 case CLOCK_EVT_MODE_PERIODIC:
125 /* PIT for periodic irqs; fixed rate of 1/HZ */
126 irqmask = AT91_ST_PITS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100127 at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
David Brownell5e802df2007-07-31 01:41:26 +0100128 break;
129 case CLOCK_EVT_MODE_ONESHOT:
130 /* ALM for oneshot irqs, set by next_event()
131 * before 32 seconds have passed
132 */
133 irqmask = AT91_ST_ALMS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100134 at91_st_write(AT91_ST_RTAR, last_crtr);
David Brownell5e802df2007-07-31 01:41:26 +0100135 break;
136 case CLOCK_EVT_MODE_SHUTDOWN:
137 case CLOCK_EVT_MODE_UNUSED:
138 case CLOCK_EVT_MODE_RESUME:
139 irqmask = 0;
140 break;
141 }
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100142 at91_st_write(AT91_ST_IER, irqmask);
David Brownell5e802df2007-07-31 01:41:26 +0100143}
144
145static int
146clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
147{
David Brownell5e802df2007-07-31 01:41:26 +0100148 u32 alm;
149 int status = 0;
150
151 BUG_ON(delta < 2);
152
David Brownell5e802df2007-07-31 01:41:26 +0100153 /* The alarm IRQ uses absolute time (now+delta), not the relative
154 * time (delta) in our calling convention. Like all clockevents
155 * using such "match" hardware, we have a race to defend against.
156 *
157 * Our defense here is to have set up the clockevent device so the
158 * delta is at least two. That way we never end up writing RTAR
159 * with the value then held in CRTR ... which would mean the match
160 * wouldn't trigger until 32 seconds later, after CRTR wraps.
161 */
162 alm = read_CRTR();
163
164 /* Cancel any pending alarm; flush any pending IRQ */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100165 at91_st_write(AT91_ST_RTAR, alm);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100166 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100167
168 /* Schedule alarm by writing RTAR. */
169 alm += delta;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100170 at91_st_write(AT91_ST_RTAR, alm);
David Brownell5e802df2007-07-31 01:41:26 +0100171
David Brownell5e802df2007-07-31 01:41:26 +0100172 return status;
173}
174
175static struct clock_event_device clkevt = {
176 .name = "at91_tick",
177 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
David Brownell5e802df2007-07-31 01:41:26 +0100178 .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
Uwe Kleine-König444d2d32015-02-18 21:19:56 +0100186static const struct of_device_id at91rm9200_st_timer_ids[] = {
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000187 { .compatible = "atmel,at91rm9200-st" },
188 { /* sentinel */ }
189};
190
191static int __init of_at91rm9200_st_init(void)
192{
193 struct device_node *np;
194 int ret;
195
196 np = of_find_matching_node(NULL, at91rm9200_st_timer_ids);
197 if (!np)
198 goto err;
199
200 at91_st_base = of_iomap(np, 0);
201 if (!at91_st_base)
202 goto node_err;
203
204 /* Get the interrupts property */
205 ret = irq_of_parse_and_map(np, 0);
206 if (!ret)
207 goto ioremap_err;
208 at91rm9200_timer_irq.irq = ret;
209
210 of_node_put(np);
211
212 return 0;
213
214ioremap_err:
215 iounmap(at91_st_base);
216node_err:
217 of_node_put(np);
218err:
219 return -EINVAL;
220}
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100221
SAN People73a59c12006-01-09 17:05:41 +0000222/*
David Brownell5e802df2007-07-31 01:41:26 +0100223 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000224 */
Alexandre Bellonibbfc97e2015-03-12 13:07:30 +0100225static void __init atmel_st_timer_init(struct device_node *node)
SAN People73a59c12006-01-09 17:05:41 +0000226{
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000227 /* For device tree enabled device: initialize here */
228 of_at91rm9200_st_init();
229
David Brownell5e802df2007-07-31 01:41:26 +0100230 /* Disable all timer interrupts, and clear any pending ones */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100231 at91_st_write(AT91_ST_IDR,
David Brownell5e802df2007-07-31 01:41:26 +0100232 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100233 at91_st_read(AT91_ST_SR);
SAN People73a59c12006-01-09 17:05:41 +0000234
Andrew Victor2a6f9902006-06-19 15:26:50 +0100235 /* Make IRQs happen for the system timer */
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000236 setup_irq(at91rm9200_timer_irq.irq, &at91rm9200_timer_irq);
SAN People73a59c12006-01-09 17:05:41 +0000237
David Brownell5e802df2007-07-31 01:41:26 +0100238 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
239 * directly for the clocksource and all clockevents, after adjusting
240 * its prescaler from the 1 Hz default.
241 */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100242 at91_st_write(AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000243
David Brownell5e802df2007-07-31 01:41:26 +0100244 /* Setup timer clockevent, with minimum of two ticks (important!!) */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030245 clkevt.cpumask = cpumask_of(0);
Uwe Kleine-König1c283532013-10-08 16:38:53 +0200246 clockevents_config_and_register(&clkevt, AT91_SLOW_CLOCK,
247 2, AT91_ST_ALMV);
SAN People73a59c12006-01-09 17:05:41 +0000248
David Brownell5e802df2007-07-31 01:41:26 +0100249 /* register clocksource */
Russell King132b1632010-12-13 13:14:55 +0000250 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100251}
Alexandre Bellonibbfc97e2015-03-12 13:07:30 +0100252CLOCKSOURCE_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st",
253 atmel_st_timer_init);