Linux 3.4-rc3
[deliverable/linux.git] / arch / arm / mach-at91 / at91rm9200_time.c
CommitLineData
73a59c1c 1/*
9d041268 2 * linux/arch/arm/mach-at91/at91rm9200_time.c
73a59c1c
SP
3 *
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
5e802dfa 22#include <linux/kernel.h>
73a59c1c 23#include <linux/interrupt.h>
07d265dd 24#include <linux/irq.h>
5e802dfa 25#include <linux/clockchips.h>
73a59c1c 26
73a59c1c
SP
27#include <asm/mach/time.h>
28
a09e64fb 29#include <mach/at91_st.h>
55d8baee 30
963151f2 31static unsigned long last_crtr;
5e802dfa
DB
32static u32 irqmask;
33static struct clock_event_device clkevt;
963151f2 34
2f5893cf
JCPV
35#define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
36
73a59c1c 37/*
5e802dfa
DB
38 * 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.
73a59c1c 41 */
5e802dfa
DB
42static inline unsigned long read_CRTR(void)
43{
73a59c1c
SP
44 unsigned long x1, x2;
45
5e9cf5e1 46 x1 = at91_st_read(AT91_ST_CRTR);
73a59c1c 47 do {
5e9cf5e1 48 x2 = at91_st_read(AT91_ST_CRTR);
5e802dfa
DB
49 if (x1 == x2)
50 break;
51 x1 = x2;
52 } while (1);
73a59c1c
SP
53 return x1;
54}
55
73a59c1c
SP
56/*
57 * IRQ handler for the timer.
58 */
0cd61b68 59static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
73a59c1c 60{
5e9cf5e1 61 u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
73a59c1c 62
501d7038
UKK
63 /*
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
5e802dfa
DB
69 /* simulate "oneshot" timer with alarm */
70 if (sr & AT91_ST_ALMS) {
71 clkevt.event_handler(&clkevt);
72 return IRQ_HANDLED;
73 }
73a59c1c 74
5e802dfa
DB
75 /* periodic mode should handle delayed ticks */
76 if (sr & AT91_ST_PITS) {
77 u32 crtr = read_CRTR();
73a59c1c 78
2f5893cf
JCPV
79 while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
80 last_crtr += RM9200_TIMER_LATCH;
5e802dfa
DB
81 clkevt.event_handler(&clkevt);
82 }
73a59c1c
SP
83 return IRQ_HANDLED;
84 }
5e802dfa
DB
85
86 /* this irq is shared ... */
87 return IRQ_NONE;
73a59c1c
SP
88}
89
90static struct irqaction at91rm9200_timer_irq = {
91 .name = "at91_tick",
b30fabad 92 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
73a59c1c
SP
93 .handler = at91rm9200_timer_interrupt
94};
95
8e19608e 96static cycle_t read_clk32k(struct clocksource *cs)
2a6f9902 97{
5e802dfa
DB
98 return read_CRTR();
99}
2a6f9902 100
5e802dfa
DB
101static struct clocksource clk32k = {
102 .name = "32k_counter",
103 .rating = 150,
104 .read = read_clk32k,
105 .mask = CLOCKSOURCE_MASK(20),
5e802dfa
DB
106 .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 */
5e9cf5e1 113 at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
9e1c0b2e 114 at91_st_read(AT91_ST_SR);
2a6f9902 115
5e802dfa
DB
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;
5e9cf5e1 121 at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
5e802dfa
DB
122 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;
5e9cf5e1 128 at91_st_write(AT91_ST_RTAR, last_crtr);
5e802dfa
DB
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 }
5e9cf5e1 136 at91_st_write(AT91_ST_IER, irqmask);
5e802dfa 137}
2a6f9902 138
5e802dfa
DB
139static int
140clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
141{
5e802dfa
DB
142 u32 alm;
143 int status = 0;
144
145 BUG_ON(delta < 2);
146
5e802dfa
DB
147 /* 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 */
5e9cf5e1 159 at91_st_write(AT91_ST_RTAR, alm);
9e1c0b2e 160 at91_st_read(AT91_ST_SR);
d100f259 161
5e802dfa
DB
162 /* Schedule alarm by writing RTAR. */
163 alm += delta;
5e9cf5e1 164 at91_st_write(AT91_ST_RTAR, alm);
5e802dfa 165
5e802dfa 166 return status;
2a6f9902
AV
167}
168
5e802dfa
DB
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,
5e802dfa
DB
174 .set_next_event = clkevt32k_next_event,
175 .set_mode = clkevt32k_mode,
176};
177
5e9cf5e1
JCPV
178void __iomem *at91_st_base;
179
180void __init at91rm9200_ioremap_st(u32 addr)
181{
182 at91_st_base = ioremap(addr, 256);
183 if (!at91_st_base)
184 panic("Impossible to ioremap ST\n");
185}
186
73a59c1c 187/*
5e802dfa 188 * ST (system timer) module supports both clockevents and clocksource.
73a59c1c
SP
189 */
190void __init at91rm9200_timer_init(void)
191{
5e802dfa 192 /* Disable all timer interrupts, and clear any pending ones */
5e9cf5e1 193 at91_st_write(AT91_ST_IDR,
5e802dfa 194 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
9e1c0b2e 195 at91_st_read(AT91_ST_SR);
73a59c1c 196
2a6f9902 197 /* Make IRQs happen for the system timer */
73a59c1c
SP
198 setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
199
5e802dfa
DB
200 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
201 * directly for the clocksource and all clockevents, after adjusting
202 * its prescaler from the 1 Hz default.
203 */
5e9cf5e1 204 at91_st_write(AT91_ST_RTMR, 1);
73a59c1c 205
5e802dfa
DB
206 /* Setup timer clockevent, with minimum of two ticks (important!!) */
207 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
208 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
209 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
320ab2b0 210 clkevt.cpumask = cpumask_of(0);
5e802dfa 211 clockevents_register_device(&clkevt);
2a6f9902 212
5e802dfa 213 /* register clocksource */
132b1632 214 clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
73a59c1c
SP
215}
216
217struct sys_timer at91rm9200_timer = {
218 .init = at91rm9200_timer_init,
73a59c1c 219};
2a6f9902 220
This page took 0.507235 seconds and 5 git commands to generate.