at91st.c revision 1.1.20.2 1 1.1.20.2 yamt /*$NetBSD: at91st.c,v 1.1.20.2 2010/03/11 15:02:05 yamt Exp $*/
2 1.1.20.1 yamt
3 1.1.20.1 yamt /*
4 1.1.20.1 yamt * AT91RM9200 clock functions
5 1.1.20.1 yamt * Copyright (c) 2007, Embedtronics Oy
6 1.1.20.1 yamt * All rights reserved.
7 1.1.20.1 yamt *
8 1.1.20.1 yamt * Based on vx115_clk.c,
9 1.1.20.1 yamt * Copyright (c) 2006, Jon Sevy <jsevy (at) cs.drexel.edu>
10 1.1.20.1 yamt *
11 1.1.20.1 yamt * Based on epclk.c
12 1.1.20.1 yamt * Copyright (c) 2004 Jesse Off
13 1.1.20.1 yamt * All rights reserved.
14 1.1.20.1 yamt *
15 1.1.20.1 yamt * Redistribution and use in source and binary forms, with or without
16 1.1.20.1 yamt * modification, are permitted provided that the following conditions
17 1.1.20.1 yamt * are met:
18 1.1.20.1 yamt * 1. Redistributions of source code must retain the above copyright
19 1.1.20.1 yamt * notice, this list of conditions and the following disclaimer.
20 1.1.20.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
21 1.1.20.1 yamt * notice, this list of conditions and the following disclaimer in the
22 1.1.20.1 yamt * documentation and/or other materials provided with the distribution.
23 1.1.20.1 yamt *
24 1.1.20.1 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.1.20.1 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.1.20.1 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.1.20.1 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.1.20.1 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1.20.1 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1.20.1 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.1.20.1 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.1.20.1 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.1.20.1 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.1.20.1 yamt * POSSIBILITY OF SUCH DAMAGE.
35 1.1.20.1 yamt */
36 1.1.20.1 yamt
37 1.1.20.1 yamt /*
38 1.1.20.1 yamt * Driver for the AT91RM9200 clock tick.
39 1.1.20.1 yamt * We use Timer 1 for the system clock
40 1.1.20.1 yamt */
41 1.1.20.1 yamt
42 1.1.20.1 yamt #include <sys/cdefs.h>
43 1.1.20.2 yamt __KERNEL_RCSID(0, "$NetBSD: at91st.c,v 1.1.20.2 2010/03/11 15:02:05 yamt Exp $");
44 1.1.20.1 yamt
45 1.1.20.1 yamt #include <sys/types.h>
46 1.1.20.1 yamt #include <sys/param.h>
47 1.1.20.1 yamt #include <sys/systm.h>
48 1.1.20.1 yamt #include <sys/kernel.h>
49 1.1.20.1 yamt #include <sys/time.h>
50 1.1.20.1 yamt #include <sys/device.h>
51 1.1.20.1 yamt
52 1.1.20.1 yamt #include <dev/clock_subr.h>
53 1.1.20.1 yamt
54 1.1.20.1 yamt #include <machine/bus.h>
55 1.1.20.1 yamt #include <machine/intr.h>
56 1.1.20.1 yamt
57 1.1.20.1 yamt #include <arm/cpufunc.h>
58 1.1.20.1 yamt #include <arm/at91/at91reg.h>
59 1.1.20.1 yamt #include <arm/at91/at91var.h>
60 1.1.20.1 yamt #include <arm/at91/at91streg.h>
61 1.1.20.1 yamt
62 1.1.20.1 yamt #include <opt_hz.h> /* for HZ */
63 1.1.20.1 yamt
64 1.1.20.1 yamt
65 1.1.20.1 yamt //#define DEBUG_CLK
66 1.1.20.1 yamt #ifdef DEBUG_CLK
67 1.1.20.1 yamt #define DPRINTF(fmt...) printf(fmt)
68 1.1.20.1 yamt #else
69 1.1.20.1 yamt #define DPRINTF(fmt...)
70 1.1.20.1 yamt #endif
71 1.1.20.1 yamt
72 1.1.20.1 yamt
73 1.1.20.1 yamt static int at91st_match(device_t, cfdata_t, void *);
74 1.1.20.1 yamt static void at91st_attach(device_t, device_t, void *);
75 1.1.20.1 yamt
76 1.1.20.1 yamt void rtcinit(void);
77 1.1.20.1 yamt
78 1.1.20.1 yamt /* callback functions for intr_functions */
79 1.1.20.1 yamt static int at91st_intr(void* arg);
80 1.1.20.1 yamt
81 1.1.20.1 yamt struct at91st_softc {
82 1.1.20.1 yamt struct device sc_dev;
83 1.1.20.1 yamt bus_space_tag_t sc_iot;
84 1.1.20.1 yamt bus_space_handle_t sc_ioh;
85 1.1.20.1 yamt int sc_pid;
86 1.1.20.1 yamt int sc_initialized;
87 1.1.20.1 yamt };
88 1.1.20.1 yamt
89 1.1.20.1 yamt static struct at91st_softc *at91st_sc = NULL;
90 1.1.20.1 yamt static struct timeval lasttv;
91 1.1.20.1 yamt
92 1.1.20.1 yamt
93 1.1.20.1 yamt
94 1.1.20.1 yamt /* Match value for clock timer; running at 32.768kHz, want HZ ticks per second */
95 1.1.20.1 yamt /* BTW, we use HZ == 64 or HZ == 128 so have a nice divisor */
96 1.1.20.1 yamt /* NOTE: don't change there without visiting the functions below which */
97 1.1.20.1 yamt /* convert between timer counts and microseconds */
98 1.1.20.1 yamt #define AT91ST_DIVIDER (AT91_SCLK / HZ)
99 1.1.20.1 yamt #define USEC_PER_TICK (1000000 / (AT91_SCLK / AT91ST_DIVIDER))
100 1.1.20.1 yamt
101 1.1.20.1 yamt #if 0
102 1.1.20.1 yamt static uint32_t at91st_count_to_usec(uint32_t count)
103 1.1.20.1 yamt {
104 1.1.20.1 yamt uint32_t result;
105 1.1.20.1 yamt
106 1.1.20.1 yamt /* convert specified number of ticks to usec, and round up */
107 1.1.20.1 yamt /* note that with 16 kHz tick rate, maximum count will be */
108 1.1.20.1 yamt /* 256 (for HZ = 64), so we won't have overflow issues */
109 1.1.20.1 yamt result = (1000000 * count) / AT91_SCLK;
110 1.1.20.1 yamt
111 1.1.20.1 yamt if ((result * AT91_SCLK) != (count * 1000000))
112 1.1.20.1 yamt {
113 1.1.20.1 yamt /* round up */
114 1.1.20.1 yamt result += 1;
115 1.1.20.1 yamt }
116 1.1.20.1 yamt
117 1.1.20.1 yamt return result;
118 1.1.20.1 yamt }
119 1.1.20.1 yamt
120 1.1.20.1 yamt /* This may only be called when overflow is avoided; typically, */
121 1.1.20.1 yamt /* it will be used when usec < USEC_PER_TICK */
122 1.1.20.1 yamt static uint32_t usec_to_timer_count(uint32_t usec)
123 1.1.20.1 yamt {
124 1.1.20.1 yamt uint32_t result;
125 1.1.20.1 yamt
126 1.1.20.1 yamt /* convert specified number of usec to timer ticks, and round up */
127 1.1.20.1 yamt result = (AT91_SCLK * usec) / 1000000;
128 1.1.20.1 yamt
129 1.1.20.1 yamt if ((result * 1000000) != (usec * AT91_SCLK))
130 1.1.20.1 yamt {
131 1.1.20.1 yamt /* round up */
132 1.1.20.1 yamt result += 1;
133 1.1.20.1 yamt }
134 1.1.20.1 yamt
135 1.1.20.1 yamt return result;
136 1.1.20.1 yamt
137 1.1.20.1 yamt }
138 1.1.20.1 yamt #endif
139 1.1.20.1 yamt
140 1.1.20.1 yamt /* macros to simplify writing to the timer controller */
141 1.1.20.1 yamt #define READ_ST(offset) STREG(offset)
142 1.1.20.1 yamt //bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset)
143 1.1.20.1 yamt #define WRITE_ST(offset, value) do { \
144 1.1.20.1 yamt STREG(offset) = (value); \
145 1.1.20.1 yamt } while (/*CONSTCOND*/0)
146 1.1.20.1 yamt //bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value)
147 1.1.20.1 yamt
148 1.1.20.1 yamt
149 1.1.20.1 yamt
150 1.1.20.1 yamt CFATTACH_DECL(at91st, sizeof(struct at91st_softc), at91st_match, at91st_attach, NULL, NULL);
151 1.1.20.1 yamt
152 1.1.20.1 yamt
153 1.1.20.1 yamt
154 1.1.20.1 yamt static int
155 1.1.20.1 yamt at91st_match(device_t parent, cfdata_t match, void *aux)
156 1.1.20.1 yamt {
157 1.1.20.1 yamt if (strcmp(match->cf_name, "at91st") == 0)
158 1.1.20.1 yamt return 2;
159 1.1.20.1 yamt return 0;
160 1.1.20.1 yamt }
161 1.1.20.1 yamt
162 1.1.20.1 yamt static void
163 1.1.20.1 yamt at91st_attach(device_t parent, device_t self, void *aux)
164 1.1.20.1 yamt {
165 1.1.20.1 yamt struct at91st_softc *sc = (struct at91st_softc*) self;
166 1.1.20.1 yamt struct at91bus_attach_args *sa = (struct at91bus_attach_args*) aux;
167 1.1.20.1 yamt
168 1.1.20.1 yamt printf("\n");
169 1.1.20.1 yamt
170 1.1.20.1 yamt sc->sc_iot = sa->sa_iot;
171 1.1.20.1 yamt sc->sc_pid = sa->sa_pid;
172 1.1.20.1 yamt
173 1.1.20.1 yamt #if 0
174 1.1.20.1 yamt DPRINTF("-> bus_space_map()\n");
175 1.1.20.1 yamt
176 1.1.20.1 yamt /* map bus space and get handle */
177 1.1.20.1 yamt if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh) != 0)
178 1.1.20.1 yamt panic("%s: Cannot map registers", self->dv_xname);
179 1.1.20.1 yamt #endif
180 1.1.20.1 yamt
181 1.1.20.1 yamt if (at91st_sc == NULL)
182 1.1.20.1 yamt at91st_sc = sc;
183 1.1.20.1 yamt
184 1.1.20.1 yamt at91_peripheral_clock(sc->sc_pid, 1);
185 1.1.20.1 yamt
186 1.1.20.1 yamt WRITE_ST(ST_IDR, -1); /* make sure interrupts are disabled */
187 1.1.20.1 yamt
188 1.1.20.1 yamt /* set up and enable interval timer 1 as kernel timer, */
189 1.1.20.1 yamt /* using 32kHz clock source */
190 1.1.20.1 yamt WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
191 1.1.20.1 yamt WRITE_ST(ST_RTMR, 1);
192 1.1.20.1 yamt
193 1.1.20.1 yamt sc->sc_initialized = 1;
194 1.1.20.1 yamt
195 1.1.20.1 yamt DPRINTF("%s: done\n", __FUNCTION__);
196 1.1.20.1 yamt
197 1.1.20.1 yamt }
198 1.1.20.1 yamt
199 1.1.20.1 yamt /*
200 1.1.20.1 yamt * at91st_intr:
201 1.1.20.1 yamt *
202 1.1.20.1 yamt *Handle the hardclock interrupt.
203 1.1.20.1 yamt */
204 1.1.20.1 yamt static int
205 1.1.20.1 yamt at91st_intr(void *arg)
206 1.1.20.1 yamt {
207 1.1.20.1 yamt // struct at91st_softc *sc = at91st_sc;
208 1.1.20.1 yamt
209 1.1.20.1 yamt /* make sure it's the kernel timer that generated the interrupt */
210 1.1.20.1 yamt /* need to do this since the interrupt line is shared by the */
211 1.1.20.1 yamt /* other interval and PWM timers */
212 1.1.20.1 yamt if (READ_ST(ST_SR) & ST_SR_PITS)
213 1.1.20.1 yamt {
214 1.1.20.1 yamt /* call the kernel timer handler */
215 1.1.20.1 yamt hardclock((struct clockframe*) arg);
216 1.1.20.1 yamt #if 0
217 1.1.20.1 yamt if (hardclock_ticks % (HZ * 10) == 0)
218 1.1.20.1 yamt printf("time %i sec\n", hardclock_ticks/HZ);
219 1.1.20.1 yamt #endif
220 1.1.20.1 yamt return 1;
221 1.1.20.1 yamt }
222 1.1.20.1 yamt else
223 1.1.20.1 yamt {
224 1.1.20.1 yamt /* it's one of the other timers; just pass it on */
225 1.1.20.1 yamt return 0;
226 1.1.20.1 yamt }
227 1.1.20.1 yamt
228 1.1.20.1 yamt }
229 1.1.20.1 yamt
230 1.1.20.1 yamt /*
231 1.1.20.1 yamt * setstatclockrate:
232 1.1.20.1 yamt *
233 1.1.20.1 yamt *Set the rate of the statistics clock.
234 1.1.20.1 yamt *
235 1.1.20.1 yamt *We assume that hz is either stathz or profhz, and that neither
236 1.1.20.1 yamt *will change after being set by cpu_initclocks(). We could
237 1.1.20.1 yamt *recalculate the intervals here, but that would be a pain.
238 1.1.20.1 yamt */
239 1.1.20.1 yamt void
240 1.1.20.1 yamt setstatclockrate(int hzz)
241 1.1.20.1 yamt {
242 1.1.20.1 yamt /* use hardclock */
243 1.1.20.1 yamt (void)hzz;
244 1.1.20.1 yamt }
245 1.1.20.1 yamt
246 1.1.20.1 yamt /*
247 1.1.20.1 yamt * cpu_initclocks:
248 1.1.20.1 yamt *
249 1.1.20.1 yamt *Initialize the clock and get it going.
250 1.1.20.1 yamt */
251 1.1.20.1 yamt static void udelay(unsigned int usec);
252 1.1.20.1 yamt
253 1.1.20.1 yamt void
254 1.1.20.1 yamt cpu_initclocks(void)
255 1.1.20.1 yamt {
256 1.1.20.1 yamt struct at91st_softc *sc = at91st_sc;
257 1.1.20.1 yamt
258 1.1.20.1 yamt if (!sc || !sc->sc_initialized)
259 1.1.20.1 yamt panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc);
260 1.1.20.1 yamt
261 1.1.20.1 yamt stathz = profhz = 0;
262 1.1.20.1 yamt
263 1.1.20.1 yamt /* set up and enable interval timer 1 as kernel timer, */
264 1.1.20.1 yamt /* using 32kHz clock source */
265 1.1.20.1 yamt WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
266 1.1.20.1 yamt
267 1.1.20.1 yamt /* register interrupt handler */
268 1.1.20.1 yamt at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91st_intr, NULL);
269 1.1.20.1 yamt
270 1.1.20.1 yamt /* enable interrupts from timer */
271 1.1.20.1 yamt WRITE_ST(ST_IER, ST_SR_PITS);
272 1.1.20.1 yamt }
273 1.1.20.1 yamt
274 1.1.20.1 yamt
275 1.1.20.1 yamt
276 1.1.20.1 yamt
277 1.1.20.1 yamt /*
278 1.1.20.1 yamt * microtime:
279 1.1.20.1 yamt *
280 1.1.20.1 yamt *Fill in the specified timeval struct with the current time
281 1.1.20.1 yamt *accurate to the microsecond.
282 1.1.20.1 yamt */
283 1.1.20.1 yamt void
284 1.1.20.1 yamt microtime(register struct timeval *tvp)
285 1.1.20.1 yamt {
286 1.1.20.1 yamt // struct at91st_softc *sc = at91st_sc;
287 1.1.20.1 yamt u_int oldirqstate;
288 1.1.20.1 yamt u_int current_count;
289 1.1.20.1 yamt
290 1.1.20.1 yamt #ifdef DEBUG
291 1.1.20.1 yamt if (at91st_sc == NULL) {
292 1.1.20.1 yamt printf("microtime: called before initialize at91st\n");
293 1.1.20.1 yamt tvp->tv_sec = 0;
294 1.1.20.1 yamt tvp->tv_usec = 0;
295 1.1.20.1 yamt return;
296 1.1.20.1 yamt }
297 1.1.20.1 yamt #endif
298 1.1.20.1 yamt
299 1.1.20.1 yamt oldirqstate = disable_interrupts(I32_bit);
300 1.1.20.1 yamt
301 1.1.20.1 yamt /* get current timer count */
302 1.1.20.1 yamt current_count = READ_ST(ST_CRTR);
303 1.1.20.1 yamt
304 1.1.20.1 yamt /* Fill in the timeval struct. */
305 1.1.20.1 yamt *tvp = time;
306 1.1.20.1 yamt
307 1.1.20.1 yamt #if 0
308 1.1.20.1 yamt /* Refine the usec field using current timer count */
309 1.1.20.1 yamt tvp->tv_usec += at91st_count_to_usec(AT91ST_DIVIDER - current_count);
310 1.1.20.1 yamt
311 1.1.20.1 yamt /* Make sure microseconds doesn't overflow. */
312 1.1.20.1 yamt while (__predict_false(tvp->tv_usec >= 1000000))
313 1.1.20.1 yamt {
314 1.1.20.1 yamt tvp->tv_usec -= 1000000;
315 1.1.20.1 yamt tvp->tv_sec++;
316 1.1.20.1 yamt }
317 1.1.20.1 yamt #endif
318 1.1.20.1 yamt
319 1.1.20.1 yamt /* Make sure the time has advanced. */
320 1.1.20.1 yamt if (__predict_false(tvp->tv_sec == lasttv.tv_sec && tvp->tv_usec <= lasttv.tv_usec))
321 1.1.20.1 yamt {
322 1.1.20.1 yamt tvp->tv_usec = lasttv.tv_usec + 1;
323 1.1.20.1 yamt if (tvp->tv_usec >= 1000000)
324 1.1.20.1 yamt {
325 1.1.20.1 yamt tvp->tv_usec -= 1000000;
326 1.1.20.1 yamt tvp->tv_sec++;
327 1.1.20.1 yamt }
328 1.1.20.1 yamt }
329 1.1.20.1 yamt
330 1.1.20.1 yamt lasttv = *tvp;
331 1.1.20.1 yamt
332 1.1.20.1 yamt restore_interrupts(oldirqstate);
333 1.1.20.1 yamt }
334 1.1.20.1 yamt
335 1.1.20.1 yamt
336 1.1.20.1 yamt #if 0
337 1.1.20.1 yamt extern int hardclock_ticks;
338 1.1.20.1 yamt static void tdelay(unsigned int ticks)
339 1.1.20.1 yamt {
340 1.1.20.1 yamt u_int32_t start, end, current;
341 1.1.20.1 yamt
342 1.1.20.1 yamt current = hardclock_ticks;
343 1.1.20.1 yamt start = current;
344 1.1.20.1 yamt end = start + ticks;
345 1.1.20.1 yamt
346 1.1.20.1 yamt /* just loop for the specified number of ticks */
347 1.1.20.1 yamt while (current < end)
348 1.1.20.1 yamt current = hardclock_ticks;
349 1.1.20.1 yamt }
350 1.1.20.1 yamt #endif
351 1.1.20.1 yamt
352 1.1.20.1 yamt static void udelay(unsigned int usec)
353 1.1.20.1 yamt {
354 1.1.20.1 yamt // struct at91st_softc *sc = at91st_sc;
355 1.1.20.1 yamt u_int32_t crtv, t, diff;
356 1.1.20.1 yamt
357 1.1.20.1 yamt usec = (usec * 1000 + AT91_SCLK - 1) / AT91_SCLK + 1;
358 1.1.20.1 yamt
359 1.1.20.1 yamt for (crtv = READ_ST(ST_CRTR);;) {
360 1.1.20.1 yamt while (crtv == (t = READ_ST(ST_CRTR))) ;
361 1.1.20.1 yamt diff = (t - crtv) & ST_CRTR_CRTV;
362 1.1.20.1 yamt if (diff >= usec) {
363 1.1.20.1 yamt break;
364 1.1.20.1 yamt }
365 1.1.20.1 yamt crtv = t;
366 1.1.20.1 yamt usec -= diff;
367 1.1.20.1 yamt }
368 1.1.20.1 yamt }
369 1.1.20.1 yamt
370 1.1.20.1 yamt
371 1.1.20.1 yamt
372 1.1.20.1 yamt /*
373 1.1.20.1 yamt * delay:
374 1.1.20.1 yamt *
375 1.1.20.1 yamt *Delay for at least N microseconds. Note that due to our coarse clock,
376 1.1.20.1 yamt * our resolution is 61 us. But we round up so we'll wait at least as
377 1.1.20.1 yamt * long as requested.
378 1.1.20.1 yamt */
379 1.1.20.1 yamt void
380 1.1.20.1 yamt delay(unsigned int usec)
381 1.1.20.1 yamt {
382 1.1.20.1 yamt
383 1.1.20.1 yamt #ifdef DEBUG
384 1.1.20.1 yamt if (at91st_sc == NULL) {
385 1.1.20.1 yamt printf("delay: called before start at91st\n");
386 1.1.20.1 yamt return;
387 1.1.20.1 yamt }
388 1.1.20.1 yamt #endif
389 1.1.20.1 yamt
390 1.1.20.1 yamt if (usec >= USEC_PER_TICK)
391 1.1.20.1 yamt {
392 1.1.20.1 yamt /* have more than 1 tick; just do in ticks */
393 1.1.20.1 yamt unsigned int ticks = usec / USEC_PER_TICK;
394 1.1.20.1 yamt if (ticks*USEC_PER_TICK != usec)
395 1.1.20.1 yamt ticks += 1;
396 1.1.20.1 yamt while (ticks-- > 0) {
397 1.1.20.1 yamt udelay(USEC_PER_TICK);
398 1.1.20.1 yamt }
399 1.1.20.1 yamt }
400 1.1.20.1 yamt else
401 1.1.20.1 yamt {
402 1.1.20.1 yamt /* less than 1 tick; can do as usec */
403 1.1.20.1 yamt udelay(usec);
404 1.1.20.1 yamt }
405 1.1.20.1 yamt
406 1.1.20.1 yamt }
407 1.1.20.1 yamt
408