blob: 674ef2519d6bf3e47277943208e3264f5fcde513 [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>
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010027#include <linux/mfd/syscon.h>
28#include <linux/mfd/syscon/atmel-st.h>
Joachim Eastwood454c46d2012-10-28 18:31:07 +000029#include <linux/of_irq.h>
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010030#include <linux/regmap.h>
SAN People73a59c12006-01-09 17:05:41 +000031
SAN People73a59c12006-01-09 17:05:41 +000032#include <asm/mach/time.h>
33
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;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010039static struct regmap *regmap_st;
Andrew Victor963151f2006-06-19 15:23:41 +010040
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080041#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
42
SAN People73a59c12006-01-09 17:05:41 +000043/*
David Brownell5e802df2007-07-31 01:41:26 +010044 * The ST_CRTR is updated asynchronously to the master clock ... but
45 * the updates as seen by the CPU don't seem to be strictly monotonic.
46 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000047 */
David Brownell5e802df2007-07-31 01:41:26 +010048static inline unsigned long read_CRTR(void)
49{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010050 unsigned int x1, x2;
SAN People73a59c12006-01-09 17:05:41 +000051
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010052 regmap_read(regmap_st, AT91_ST_CRTR, &x1);
SAN People73a59c12006-01-09 17:05:41 +000053 do {
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010054 regmap_read(regmap_st, AT91_ST_CRTR, &x2);
David Brownell5e802df2007-07-31 01:41:26 +010055 if (x1 == x2)
56 break;
57 x1 = x2;
58 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000059 return x1;
60}
61
62/*
SAN People73a59c12006-01-09 17:05:41 +000063 * IRQ handler for the timer.
64 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070065static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000066{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010067 u32 sr;
68
69 regmap_read(regmap_st, AT91_ST_SR, &sr);
70 sr &= irqmask;
SAN People73a59c12006-01-09 17:05:41 +000071
Uwe Kleine-König501d7032009-09-21 09:30:09 +020072 /*
73 * irqs should be disabled here, but as the irq is shared they are only
74 * guaranteed to be off if the timer irq is registered first.
75 */
76 WARN_ON_ONCE(!irqs_disabled());
77
David Brownell5e802df2007-07-31 01:41:26 +010078 /* simulate "oneshot" timer with alarm */
79 if (sr & AT91_ST_ALMS) {
80 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000081 return IRQ_HANDLED;
82 }
David Brownell5e802df2007-07-31 01:41:26 +010083
84 /* periodic mode should handle delayed ticks */
85 if (sr & AT91_ST_PITS) {
86 u32 crtr = read_CRTR();
87
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080088 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
89 last_crtr += RM9200_TIMER_LATCH;
David Brownell5e802df2007-07-31 01:41:26 +010090 clkevt.event_handler(&clkevt);
91 }
92 return IRQ_HANDLED;
93 }
94
95 /* this irq is shared ... */
96 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000097}
98
99static struct irqaction at91rm9200_timer_irq = {
100 .name = "at91_tick",
Michael Opdenacker9ceb3892013-09-04 06:54:39 +0200101 .flags = IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000102 .handler = at91rm9200_timer_interrupt,
103 .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
SAN People73a59c12006-01-09 17:05:41 +0000104};
105
Magnus Damm8e196082009-04-21 12:24:00 -0700106static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +0100107{
David Brownell5e802df2007-07-31 01:41:26 +0100108 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +0100109}
110
David Brownell5e802df2007-07-31 01:41:26 +0100111static struct clocksource clk32k = {
112 .name = "32k_counter",
113 .rating = 150,
114 .read = read_clk32k,
115 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100116 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
117};
118
119static void
120clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
121{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100122 unsigned int val;
123
David Brownell5e802df2007-07-31 01:41:26 +0100124 /* Disable and flush pending timer interrupts */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100125 regmap_write(regmap_st, AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
126 regmap_read(regmap_st, AT91_ST_SR, &val);
David Brownell5e802df2007-07-31 01:41:26 +0100127
128 last_crtr = read_CRTR();
129 switch (mode) {
130 case CLOCK_EVT_MODE_PERIODIC:
131 /* PIT for periodic irqs; fixed rate of 1/HZ */
132 irqmask = AT91_ST_PITS;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100133 regmap_write(regmap_st, AT91_ST_PIMR, RM9200_TIMER_LATCH);
David Brownell5e802df2007-07-31 01:41:26 +0100134 break;
135 case CLOCK_EVT_MODE_ONESHOT:
136 /* ALM for oneshot irqs, set by next_event()
137 * before 32 seconds have passed
138 */
139 irqmask = AT91_ST_ALMS;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100140 regmap_write(regmap_st, AT91_ST_RTAR, last_crtr);
David Brownell5e802df2007-07-31 01:41:26 +0100141 break;
142 case CLOCK_EVT_MODE_SHUTDOWN:
143 case CLOCK_EVT_MODE_UNUSED:
144 case CLOCK_EVT_MODE_RESUME:
145 irqmask = 0;
146 break;
147 }
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100148 regmap_write(regmap_st, AT91_ST_IER, irqmask);
David Brownell5e802df2007-07-31 01:41:26 +0100149}
150
151static int
152clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
153{
David Brownell5e802df2007-07-31 01:41:26 +0100154 u32 alm;
155 int status = 0;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100156 unsigned int val;
David Brownell5e802df2007-07-31 01:41:26 +0100157
158 BUG_ON(delta < 2);
159
David Brownell5e802df2007-07-31 01:41:26 +0100160 /* The alarm IRQ uses absolute time (now+delta), not the relative
161 * time (delta) in our calling convention. Like all clockevents
162 * using such "match" hardware, we have a race to defend against.
163 *
164 * Our defense here is to have set up the clockevent device so the
165 * delta is at least two. That way we never end up writing RTAR
166 * with the value then held in CRTR ... which would mean the match
167 * wouldn't trigger until 32 seconds later, after CRTR wraps.
168 */
169 alm = read_CRTR();
170
171 /* Cancel any pending alarm; flush any pending IRQ */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100172 regmap_write(regmap_st, AT91_ST_RTAR, alm);
173 regmap_read(regmap_st, AT91_ST_SR, &val);
David Brownell5e802df2007-07-31 01:41:26 +0100174
175 /* Schedule alarm by writing RTAR. */
176 alm += delta;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100177 regmap_write(regmap_st, AT91_ST_RTAR, alm);
David Brownell5e802df2007-07-31 01:41:26 +0100178
David Brownell5e802df2007-07-31 01:41:26 +0100179 return status;
180}
181
182static struct clock_event_device clkevt = {
183 .name = "at91_tick",
184 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
David Brownell5e802df2007-07-31 01:41:26 +0100185 .rating = 150,
David Brownell5e802df2007-07-31 01:41:26 +0100186 .set_next_event = clkevt32k_next_event,
187 .set_mode = clkevt32k_mode,
188};
189
SAN People73a59c12006-01-09 17:05:41 +0000190/*
David Brownell5e802df2007-07-31 01:41:26 +0100191 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000192 */
Alexandre Bellonibbfc97e2015-03-12 13:07:30 +0100193static void __init atmel_st_timer_init(struct device_node *node)
SAN People73a59c12006-01-09 17:05:41 +0000194{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100195 unsigned int val;
196
197 regmap_st = syscon_node_to_regmap(node);
198 if (IS_ERR(regmap_st))
199 panic(pr_fmt("Unable to get regmap\n"));
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000200
David Brownell5e802df2007-07-31 01:41:26 +0100201 /* Disable all timer interrupts, and clear any pending ones */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100202 regmap_write(regmap_st, AT91_ST_IDR,
David Brownell5e802df2007-07-31 01:41:26 +0100203 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100204 regmap_read(regmap_st, AT91_ST_SR, &val);
205
206 /* Get the interrupts property */
207 at91rm9200_timer_irq.irq = irq_of_parse_and_map(node, 0);
208 if (!at91rm9200_timer_irq.irq)
209 panic(pr_fmt("Unable to get IRQ from DT\n"));
SAN People73a59c12006-01-09 17:05:41 +0000210
Andrew Victor2a6f9902006-06-19 15:26:50 +0100211 /* Make IRQs happen for the system timer */
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000212 setup_irq(at91rm9200_timer_irq.irq, &at91rm9200_timer_irq);
SAN People73a59c12006-01-09 17:05:41 +0000213
David Brownell5e802df2007-07-31 01:41:26 +0100214 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
215 * directly for the clocksource and all clockevents, after adjusting
216 * its prescaler from the 1 Hz default.
217 */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100218 regmap_write(regmap_st, AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000219
David Brownell5e802df2007-07-31 01:41:26 +0100220 /* Setup timer clockevent, with minimum of two ticks (important!!) */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030221 clkevt.cpumask = cpumask_of(0);
Uwe Kleine-König1c283532013-10-08 16:38:53 +0200222 clockevents_config_and_register(&clkevt, AT91_SLOW_CLOCK,
223 2, AT91_ST_ALMV);
SAN People73a59c12006-01-09 17:05:41 +0000224
David Brownell5e802df2007-07-31 01:41:26 +0100225 /* register clocksource */
Russell King132b1632010-12-13 13:14:55 +0000226 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100227}
Alexandre Bellonibbfc97e2015-03-12 13:07:30 +0100228CLOCKSOURCE_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st",
229 atmel_st_timer_init);