blob: a028cdf8f9749d8ad4da1aef23386da5850330c4 [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>
SAN People73a59c12006-01-09 17:05:41 +000026
SAN People73a59c12006-01-09 17:05:41 +000027#include <asm/mach/time.h>
28
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/at91_st.h>
Andrew Victor55d8bae2006-11-30 17:16:43 +010030
Andrew Victor963151f2006-06-19 15:23:41 +010031static unsigned long last_crtr;
David Brownell5e802df2007-07-31 01:41:26 +010032static u32 irqmask;
33static struct clock_event_device clkevt;
Andrew Victor963151f2006-06-19 15:23:41 +010034
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080035#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
36
SAN People73a59c12006-01-09 17:05:41 +000037/*
David Brownell5e802df2007-07-31 01:41:26 +010038 * The ST_CRTR is updated asynchronously to the master clock ... but
39 * the updates as seen by the CPU don't seem to be strictly monotonic.
40 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000041 */
David Brownell5e802df2007-07-31 01:41:26 +010042static inline unsigned long read_CRTR(void)
43{
SAN People73a59c12006-01-09 17:05:41 +000044 unsigned long x1, x2;
45
David Brownell5e802df2007-07-31 01:41:26 +010046 x1 = at91_sys_read(AT91_ST_CRTR);
SAN People73a59c12006-01-09 17:05:41 +000047 do {
SAN People73a59c12006-01-09 17:05:41 +000048 x2 = at91_sys_read(AT91_ST_CRTR);
David Brownell5e802df2007-07-31 01:41:26 +010049 if (x1 == x2)
50 break;
51 x1 = x2;
52 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000053 return x1;
54}
55
56/*
SAN People73a59c12006-01-09 17:05:41 +000057 * IRQ handler for the timer.
58 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070059static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000060{
David Brownell5e802df2007-07-31 01:41:26 +010061 u32 sr = at91_sys_read(AT91_ST_SR) & irqmask;
SAN People73a59c12006-01-09 17:05:41 +000062
Uwe Kleine-König501d7032009-09-21 09:30:09 +020063 /*
64 * irqs should be disabled here, but as the irq is shared they are only
65 * guaranteed to be off if the timer irq is registered first.
66 */
67 WARN_ON_ONCE(!irqs_disabled());
68
David Brownell5e802df2007-07-31 01:41:26 +010069 /* simulate "oneshot" timer with alarm */
70 if (sr & AT91_ST_ALMS) {
71 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000072 return IRQ_HANDLED;
73 }
David Brownell5e802df2007-07-31 01:41:26 +010074
75 /* periodic mode should handle delayed ticks */
76 if (sr & AT91_ST_PITS) {
77 u32 crtr = read_CRTR();
78
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080079 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
80 last_crtr += RM9200_TIMER_LATCH;
David Brownell5e802df2007-07-31 01:41:26 +010081 clkevt.event_handler(&clkevt);
82 }
83 return IRQ_HANDLED;
84 }
85
86 /* this irq is shared ... */
87 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000088}
89
90static struct irqaction at91rm9200_timer_irq = {
91 .name = "at91_tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -070092 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
SAN People73a59c12006-01-09 17:05:41 +000093 .handler = at91rm9200_timer_interrupt
94};
95
Magnus Damm8e196082009-04-21 12:24:00 -070096static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +010097{
David Brownell5e802df2007-07-31 01:41:26 +010098 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +010099}
100
David Brownell5e802df2007-07-31 01:41:26 +0100101static struct clocksource clk32k = {
102 .name = "32k_counter",
103 .rating = 150,
104 .read = read_clk32k,
105 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100106 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
107};
108
109static void
110clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
111{
112 /* Disable and flush pending timer interrupts */
113 at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
114 (void) at91_sys_read(AT91_ST_SR);
115
116 last_crtr = read_CRTR();
117 switch (mode) {
118 case CLOCK_EVT_MODE_PERIODIC:
119 /* PIT for periodic irqs; fixed rate of 1/HZ */
120 irqmask = AT91_ST_PITS;
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +0800121 at91_sys_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
David Brownell5e802df2007-07-31 01:41:26 +0100122 break;
123 case CLOCK_EVT_MODE_ONESHOT:
124 /* ALM for oneshot irqs, set by next_event()
125 * before 32 seconds have passed
126 */
127 irqmask = AT91_ST_ALMS;
128 at91_sys_write(AT91_ST_RTAR, last_crtr);
129 break;
130 case CLOCK_EVT_MODE_SHUTDOWN:
131 case CLOCK_EVT_MODE_UNUSED:
132 case CLOCK_EVT_MODE_RESUME:
133 irqmask = 0;
134 break;
135 }
136 at91_sys_write(AT91_ST_IER, irqmask);
137}
138
139static int
140clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
141{
David Brownell5e802df2007-07-31 01:41:26 +0100142 u32 alm;
143 int status = 0;
144
145 BUG_ON(delta < 2);
146
David Brownell5e802df2007-07-31 01:41:26 +0100147 /* The alarm IRQ uses absolute time (now+delta), not the relative
148 * time (delta) in our calling convention. Like all clockevents
149 * using such "match" hardware, we have a race to defend against.
150 *
151 * Our defense here is to have set up the clockevent device so the
152 * delta is at least two. That way we never end up writing RTAR
153 * with the value then held in CRTR ... which would mean the match
154 * wouldn't trigger until 32 seconds later, after CRTR wraps.
155 */
156 alm = read_CRTR();
157
158 /* Cancel any pending alarm; flush any pending IRQ */
159 at91_sys_write(AT91_ST_RTAR, alm);
160 (void) at91_sys_read(AT91_ST_SR);
161
162 /* Schedule alarm by writing RTAR. */
163 alm += delta;
164 at91_sys_write(AT91_ST_RTAR, alm);
165
David Brownell5e802df2007-07-31 01:41:26 +0100166 return status;
167}
168
169static struct clock_event_device clkevt = {
170 .name = "at91_tick",
171 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
172 .shift = 32,
173 .rating = 150,
David Brownell5e802df2007-07-31 01:41:26 +0100174 .set_next_event = clkevt32k_next_event,
175 .set_mode = clkevt32k_mode,
176};
177
SAN People73a59c12006-01-09 17:05:41 +0000178/*
David Brownell5e802df2007-07-31 01:41:26 +0100179 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000180 */
181void __init at91rm9200_timer_init(void)
182{
David Brownell5e802df2007-07-31 01:41:26 +0100183 /* Disable all timer interrupts, and clear any pending ones */
184 at91_sys_write(AT91_ST_IDR,
185 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
186 (void) at91_sys_read(AT91_ST_SR);
SAN People73a59c12006-01-09 17:05:41 +0000187
Andrew Victor2a6f9902006-06-19 15:26:50 +0100188 /* Make IRQs happen for the system timer */
SAN People73a59c12006-01-09 17:05:41 +0000189 setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
190
David Brownell5e802df2007-07-31 01:41:26 +0100191 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
192 * directly for the clocksource and all clockevents, after adjusting
193 * its prescaler from the 1 Hz default.
194 */
195 at91_sys_write(AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000196
David Brownell5e802df2007-07-31 01:41:26 +0100197 /* Setup timer clockevent, with minimum of two ticks (important!!) */
198 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
199 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
200 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030201 clkevt.cpumask = cpumask_of(0);
David Brownell5e802df2007-07-31 01:41:26 +0100202 clockevents_register_device(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +0000203
David Brownell5e802df2007-07-31 01:41:26 +0100204 /* register clocksource */
Russell King132b1632010-12-13 13:14:55 +0000205 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100206}
Andrew Victor2a6f9902006-06-19 15:26:50 +0100207
SAN People73a59c12006-01-09 17:05:41 +0000208struct sys_timer at91rm9200_timer = {
209 .init = at91rm9200_timer_init,
SAN People73a59c12006-01-09 17:05:41 +0000210};
Andrew Victor2a6f9902006-06-19 15:26:50 +0100211