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