1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/clockchips.h>
26
27#include <asm/mach/time.h>
28
29#include <mach/at91_st.h>
30
31static unsigned long last_crtr;
32static u32 irqmask;
33static struct clock_event_device clkevt;
34
35
36
37
38
39
40static inline unsigned long read_CRTR(void)
41{
42 unsigned long x1, x2;
43
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;
52}
53
54
55
56
57static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
58{
59 u32 sr = at91_sys_read(AT91_ST_SR) & irqmask;
60
61
62 if (sr & AT91_ST_ALMS) {
63 clkevt.event_handler(&clkevt);
64 return IRQ_HANDLED;
65 }
66
67
68 if (sr & AT91_ST_PITS) {
69 u32 crtr = read_CRTR();
70
71 while (((crtr - last_crtr) & AT91_ST_CRTV) >= LATCH) {
72 last_crtr += LATCH;
73 clkevt.event_handler(&clkevt);
74 }
75 return IRQ_HANDLED;
76 }
77
78
79 return IRQ_NONE;
80}
81
82static struct irqaction at91rm9200_timer_irq = {
83 .name = "at91_tick",
84 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
85 .handler = at91rm9200_timer_interrupt
86};
87
88static cycle_t read_clk32k(void)
89{
90 return read_CRTR();
91}
92
93static 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,
100};
101
102static void
103clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
104{
105
106 at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
107 (void) at91_sys_read(AT91_ST_SR);
108
109 last_crtr = read_CRTR();
110 switch (mode) {
111 case CLOCK_EVT_MODE_PERIODIC:
112
113 irqmask = AT91_ST_PITS;
114 at91_sys_write(AT91_ST_PIMR, LATCH);
115 break;
116 case CLOCK_EVT_MODE_ONESHOT:
117
118
119
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;
128 }
129 at91_sys_write(AT91_ST_IER, irqmask);
130}
131
132static int
133clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
134{
135 unsigned long flags;
136 u32 alm;
137 int status = 0;
138
139 BUG_ON(delta < 2);
140
141
142 raw_local_irq_save(flags);
143
144
145
146
147
148
149
150
151
152
153 alm = read_CRTR();
154
155
156 at91_sys_write(AT91_ST_RTAR, alm);
157 (void) at91_sys_read(AT91_ST_SR);
158
159
160 alm += delta;
161 at91_sys_write(AT91_ST_RTAR, alm);
162
163 raw_local_irq_restore(flags);
164 return status;
165}
166
167static struct clock_event_device clkevt = {
168 .name = "at91_tick",
169 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
170 .shift = 32,
171 .rating = 150,
172 .cpumask = CPU_MASK_CPU0,
173 .set_next_event = clkevt32k_next_event,
174 .set_mode = clkevt32k_mode,
175};
176
177
178
179
180void __init at91rm9200_timer_init(void)
181{
182
183 at91_sys_write(AT91_ST_IDR,
184 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
185 (void) at91_sys_read(AT91_ST_SR);
186
187
188 setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
189
190
191
192
193
194 at91_sys_write(AT91_ST_RTMR, 1);
195
196
197 clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
198 clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
199 clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
200 clkevt.cpumask = cpumask_of_cpu(0);
201 clockevents_register_device(&clkevt);
202
203
204 clk32k.mult = clocksource_hz2mult(AT91_SLOW_CLOCK, clk32k.shift);
205 clocksource_register(&clk32k);
206}
207
208struct sys_timer at91rm9200_timer = {
209 .init = at91rm9200_timer_init,
210};
211
212