blob: aaa443b48c91f121e2f698792fb94bb4d6e2afa6 [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>
SAN People73a59c12006-01-09 17:05:41 +000027
SAN People73a59c12006-01-09 17:05:41 +000028#include <asm/mach/time.h>
29
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/at91_st.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010031
Andrew Victor963151f2006-06-19 15:23:41 +010032static unsigned long last_crtr;
David Brownell5e802df2007-07-31 01:41:26 +010033static u32 irqmask;
34static struct clock_event_device clkevt;
Andrew Victor963151f2006-06-19 15:23:41 +010035
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080036#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
37
SAN People73a59c12006-01-09 17:05:41 +000038/*
David Brownell5e802df2007-07-31 01:41:26 +010039 * The ST_CRTR is updated asynchronously to the master clock ... but
40 * the updates as seen by the CPU don't seem to be strictly monotonic.
41 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000042 */
David Brownell5e802df2007-07-31 01:41:26 +010043static inline unsigned long read_CRTR(void)
44{
SAN People73a59c12006-01-09 17:05:41 +000045 unsigned long x1, x2;
46
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010047 x1 = at91_st_read(AT91_ST_CRTR);
SAN People73a59c12006-01-09 17:05:41 +000048 do {
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010049 x2 = at91_st_read(AT91_ST_CRTR);
David Brownell5e802df2007-07-31 01:41:26 +010050 if (x1 == x2)
51 break;
52 x1 = x2;
53 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000054 return x1;
55}
56
57/*
SAN People73a59c12006-01-09 17:05:41 +000058 * IRQ handler for the timer.
59 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070060static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000061{
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +010062 u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
SAN People73a59c12006-01-09 17:05:41 +000063
Uwe Kleine-König501d7032009-09-21 09:30:09 +020064 /*
65 * irqs should be disabled here, but as the irq is shared they are only
66 * guaranteed to be off if the timer irq is registered first.
67 */
68 WARN_ON_ONCE(!irqs_disabled());
69
David Brownell5e802df2007-07-31 01:41:26 +010070 /* simulate "oneshot" timer with alarm */
71 if (sr & AT91_ST_ALMS) {
72 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000073 return IRQ_HANDLED;
74 }
David Brownell5e802df2007-07-31 01:41:26 +010075
76 /* periodic mode should handle delayed ticks */
77 if (sr & AT91_ST_PITS) {
78 u32 crtr = read_CRTR();
79
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080080 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
81 last_crtr += RM9200_TIMER_LATCH;
David Brownell5e802df2007-07-31 01:41:26 +010082 clkevt.event_handler(&clkevt);
83 }
84 return IRQ_HANDLED;
85 }
86
87 /* this irq is shared ... */
88 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000089}
90
91static struct irqaction at91rm9200_timer_irq = {
92 .name = "at91_tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -070093 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
SAN People73a59c12006-01-09 17:05:41 +000094 .handler = at91rm9200_timer_interrupt
95};
96
Magnus Damm8e196082009-04-21 12:24:00 -070097static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +010098{
David Brownell5e802df2007-07-31 01:41:26 +010099 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +0100100}
101
David Brownell5e802df2007-07-31 01:41:26 +0100102static struct clocksource clk32k = {
103 .name = "32k_counter",
104 .rating = 150,
105 .read = read_clk32k,
106 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100107 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
108};
109
110static void
111clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
112{
113 /* Disable and flush pending timer interrupts */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100114 at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100115 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100116
117 last_crtr = read_CRTR();
118 switch (mode) {
119 case CLOCK_EVT_MODE_PERIODIC:
120 /* PIT for periodic irqs; fixed rate of 1/HZ */
121 irqmask = AT91_ST_PITS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100122 at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
David Brownell5e802df2007-07-31 01:41:26 +0100123 break;
124 case CLOCK_EVT_MODE_ONESHOT:
125 /* ALM for oneshot irqs, set by next_event()
126 * before 32 seconds have passed
127 */
128 irqmask = AT91_ST_ALMS;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100129 at91_st_write(AT91_ST_RTAR, last_crtr);
David Brownell5e802df2007-07-31 01:41:26 +0100130 break;
131 case CLOCK_EVT_MODE_SHUTDOWN:
132 case CLOCK_EVT_MODE_UNUSED:
133 case CLOCK_EVT_MODE_RESUME:
134 irqmask = 0;
135 break;
136 }
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100137 at91_st_write(AT91_ST_IER, irqmask);
David Brownell5e802df2007-07-31 01:41:26 +0100138}
139
140static int
141clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
142{
David Brownell5e802df2007-07-31 01:41:26 +0100143 u32 alm;
144 int status = 0;
145
146 BUG_ON(delta < 2);
147
David Brownell5e802df2007-07-31 01:41:26 +0100148 /* The alarm IRQ uses absolute time (now+delta), not the relative
149 * time (delta) in our calling convention. Like all clockevents
150 * using such "match" hardware, we have a race to defend against.
151 *
152 * Our defense here is to have set up the clockevent device so the
153 * delta is at least two. That way we never end up writing RTAR
154 * with the value then held in CRTR ... which would mean the match
155 * wouldn't trigger until 32 seconds later, after CRTR wraps.
156 */
157 alm = read_CRTR();
158
159 /* Cancel any pending alarm; flush any pending IRQ */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100160 at91_st_write(AT91_ST_RTAR, alm);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100161 at91_st_read(AT91_ST_SR);
David Brownell5e802df2007-07-31 01:41:26 +0100162
163 /* Schedule alarm by writing RTAR. */
164 alm += delta;
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100165 at91_st_write(AT91_ST_RTAR, alm);
David Brownell5e802df2007-07-31 01:41:26 +0100166
David Brownell5e802df2007-07-31 01:41:26 +0100167 return status;
168}
169
170static struct clock_event_device clkevt = {
171 .name = "at91_tick",
172 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
173 .shift = 32,
174 .rating = 150,
David Brownell5e802df2007-07-31 01:41:26 +0100175 .set_next_event = clkevt32k_next_event,
176 .set_mode = clkevt32k_mode,
177};
178
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100179void __iomem *at91_st_base;
Joachim Eastwood9fce85c2012-04-04 19:15:15 +0200180EXPORT_SYMBOL_GPL(at91_st_base);
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100181
182void __init at91rm9200_ioremap_st(u32 addr)
183{
184 at91_st_base = ioremap(addr, 256);
185 if (!at91_st_base)
186 panic("Impossible to ioremap ST\n");
187}
188
SAN People73a59c12006-01-09 17:05:41 +0000189/*
David Brownell5e802df2007-07-31 01:41:26 +0100190 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000191 */
192void __init at91rm9200_timer_init(void)
193{
David Brownell5e802df2007-07-31 01:41:26 +0100194 /* Disable all timer interrupts, and clear any pending ones */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100195 at91_st_write(AT91_ST_IDR,
David Brownell5e802df2007-07-31 01:41:26 +0100196 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
Nicolas Ferre9e1c0b22012-02-20 11:13:13 +0100197 at91_st_read(AT91_ST_SR);
SAN People73a59c12006-01-09 17:05:41 +0000198
Andrew Victor2a6f9902006-06-19 15:26:50 +0100199 /* Make IRQs happen for the system timer */
Ludovic Desroches85ebea12012-08-14 11:19:21 +0200200 setup_irq(NR_IRQS_LEGACY + AT91_ID_SYS, &at91rm9200_timer_irq);
SAN People73a59c12006-01-09 17:05:41 +0000201
David Brownell5e802df2007-07-31 01:41:26 +0100202 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
203 * directly for the clocksource and all clockevents, after adjusting
204 * its prescaler from the 1 Hz default.
205 */
Jean-Christophe PLAGNIOL-VILLARD5e9cf5e2012-02-20 11:07:39 +0100206 at91_st_write(AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000207
David Brownell5e802df2007-07-31 01:41:26 +0100208 /* Setup timer clockevent, with minimum of two ticks (important!!) */
209 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
210 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
211 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030212 clkevt.cpumask = cpumask_of(0);
David Brownell5e802df2007-07-31 01:41:26 +0100213 clockevents_register_device(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +0000214
David Brownell5e802df2007-07-31 01:41:26 +0100215 /* register clocksource */
Russell King132b1632010-12-13 13:14:55 +0000216 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100217}
Andrew Victor2a6f9902006-06-19 15:26:50 +0100218
SAN People73a59c12006-01-09 17:05:41 +0000219struct sys_timer at91rm9200_timer = {
220 .init = at91rm9200_timer_init,
SAN People73a59c12006-01-09 17:05:41 +0000221};
Andrew Victor2a6f9902006-06-19 15:26:50 +0100222