linux/usb/audio.h: split header
[firewire-audio.git] / arch / arm / mach-at91 / at91rm9200_time.c
blob309f3511aa203a3c1c895815bad2bbd9b2d5583a
1 /*
2 * linux/arch/arm/mach-at91/at91rm9200_time.c
4 * Copyright (C) 2003 SAN People
5 * Copyright (C) 2003 ATMEL
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.
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.
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
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/irq.h>
25 #include <linux/clockchips.h>
27 #include <asm/mach/time.h>
29 #include <mach/at91_st.h>
31 static unsigned long last_crtr;
32 static u32 irqmask;
33 static struct clock_event_device clkevt;
36 * The ST_CRTR is updated asynchronously to the master clock ... but
37 * the updates as seen by the CPU don't seem to be strictly monotonic.
38 * Waiting until we read the same value twice avoids glitching.
40 static inline unsigned long read_CRTR(void)
42 unsigned long x1, x2;
44 x1 = at91_sys_read(AT91_ST_CRTR);
45 do {
46 x2 = at91_sys_read(AT91_ST_CRTR);
47 if (x1 == x2)
48 break;
49 x1 = x2;
50 } while (1);
51 return x1;
55 * IRQ handler for the timer.
57 static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
59 u32 sr = at91_sys_read(AT91_ST_SR) & irqmask;
61 /* simulate "oneshot" timer with alarm */
62 if (sr & AT91_ST_ALMS) {
63 clkevt.event_handler(&clkevt);
64 return IRQ_HANDLED;
67 /* periodic mode should handle delayed ticks */
68 if (sr & AT91_ST_PITS) {
69 u32 crtr = read_CRTR();
71 while (((crtr - last_crtr) & AT91_ST_CRTV) >= LATCH) {
72 last_crtr += LATCH;
73 clkevt.event_handler(&clkevt);
75 return IRQ_HANDLED;
78 /* this irq is shared ... */
79 return IRQ_NONE;
82 static struct irqaction at91rm9200_timer_irq = {
83 .name = "at91_tick",
84 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
85 .handler = at91rm9200_timer_interrupt
88 static cycle_t read_clk32k(struct clocksource *cs)
90 return read_CRTR();
93 static struct clocksource clk32k = {
94 .name = "32k_counter",
95 .rating = 150,
96 .read = read_clk32k,
97 .mask = CLOCKSOURCE_MASK(20),
98 .shift = 10,
99 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
102 static void
103 clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
105 /* Disable and flush pending timer interrupts */
106 at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
107 (void) at91_sys_read(AT91_ST_SR);
109 last_crtr = read_CRTR();
110 switch (mode) {
111 case CLOCK_EVT_MODE_PERIODIC:
112 /* PIT for periodic irqs; fixed rate of 1/HZ */
113 irqmask = AT91_ST_PITS;
114 at91_sys_write(AT91_ST_PIMR, LATCH);
115 break;
116 case CLOCK_EVT_MODE_ONESHOT:
117 /* ALM for oneshot irqs, set by next_event()
118 * before 32 seconds have passed
120 irqmask = AT91_ST_ALMS;
121 at91_sys_write(AT91_ST_RTAR, last_crtr);
122 break;
123 case CLOCK_EVT_MODE_SHUTDOWN:
124 case CLOCK_EVT_MODE_UNUSED:
125 case CLOCK_EVT_MODE_RESUME:
126 irqmask = 0;
127 break;
129 at91_sys_write(AT91_ST_IER, irqmask);
132 static int
133 clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
135 unsigned long flags;
136 u32 alm;
137 int status = 0;
139 BUG_ON(delta < 2);
141 /* Use "raw" primitives so we behave correctly on RT kernels. */
142 raw_local_irq_save(flags);
145 * According to Thomas Gleixner irqs are already disabled here. Simply
146 * removing raw_local_irq_save above (and the matching
147 * raw_local_irq_restore) was not accepted. See
148 * http://thread.gmane.org/gmane.linux.ports.arm.kernel/41174
149 * So for now (2008-11-20) just warn once if irqs were not disabled ...
151 WARN_ON_ONCE(!raw_irqs_disabled_flags(flags));
153 /* 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.
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.
162 alm = read_CRTR();
164 /* Cancel any pending alarm; flush any pending IRQ */
165 at91_sys_write(AT91_ST_RTAR, alm);
166 (void) at91_sys_read(AT91_ST_SR);
168 /* Schedule alarm by writing RTAR. */
169 alm += delta;
170 at91_sys_write(AT91_ST_RTAR, alm);
172 raw_local_irq_restore(flags);
173 return status;
176 static struct clock_event_device clkevt = {
177 .name = "at91_tick",
178 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
179 .shift = 32,
180 .rating = 150,
181 .set_next_event = clkevt32k_next_event,
182 .set_mode = clkevt32k_mode,
186 * ST (system timer) module supports both clockevents and clocksource.
188 void __init at91rm9200_timer_init(void)
190 /* Disable all timer interrupts, and clear any pending ones */
191 at91_sys_write(AT91_ST_IDR,
192 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
193 (void) at91_sys_read(AT91_ST_SR);
195 /* Make IRQs happen for the system timer */
196 setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
198 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
199 * directly for the clocksource and all clockevents, after adjusting
200 * its prescaler from the 1 Hz default.
202 at91_sys_write(AT91_ST_RTMR, 1);
204 /* Setup timer clockevent, with minimum of two ticks (important!!) */
205 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
206 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
207 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
208 clkevt.cpumask = cpumask_of(0);
209 clockevents_register_device(&clkevt);
211 /* register clocksource */
212 clk32k.mult = clocksource_hz2mult(AT91_SLOW_CLOCK, clk32k.shift);
213 clocksource_register(&clk32k);
216 struct sys_timer at91rm9200_timer = {
217 .init = at91rm9200_timer_init,