gemini_timer.c revision 1.2.4.2 1 1.2.4.2 mjf /* $NetBSD: gemini_timer.c,v 1.2.4.2 2009/01/17 13:27:53 mjf Exp $ */
2 1.2.4.2 mjf
3 1.2.4.2 mjf /* adapted from:
4 1.2.4.2 mjf * NetBSD: omap2_geminitmr.c,v 1.1 2008/08/27 11:03:10 matt Exp
5 1.2.4.2 mjf */
6 1.2.4.2 mjf
7 1.2.4.2 mjf /*
8 1.2.4.2 mjf * GEMINI Timers
9 1.2.4.2 mjf */
10 1.2.4.2 mjf
11 1.2.4.2 mjf /*
12 1.2.4.2 mjf * Based on i80321_timer.c and arch/arm/sa11x0/sa11x0_ost.c
13 1.2.4.2 mjf *
14 1.2.4.2 mjf * Copyright (c) 1997 Mark Brinicombe.
15 1.2.4.2 mjf * Copyright (c) 1997 Causality Limited.
16 1.2.4.2 mjf * All rights reserved.
17 1.2.4.2 mjf *
18 1.2.4.2 mjf * This code is derived from software contributed to The NetBSD Foundation
19 1.2.4.2 mjf * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
20 1.2.4.2 mjf *
21 1.2.4.2 mjf * Redistribution and use in source and binary forms, with or without
22 1.2.4.2 mjf * modification, are permitted provided that the following conditions
23 1.2.4.2 mjf * are met:
24 1.2.4.2 mjf * 1. Redistributions of source code must retain the above copyright
25 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer.
26 1.2.4.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
27 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer in the
28 1.2.4.2 mjf * documentation and/or other materials provided with the distribution.
29 1.2.4.2 mjf * 3. All advertising materials mentioning features or use of this software
30 1.2.4.2 mjf * must display the following acknowledgement:
31 1.2.4.2 mjf * This product includes software developed by the NetBSD
32 1.2.4.2 mjf * Foundation, Inc. and its contributors.
33 1.2.4.2 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
34 1.2.4.2 mjf * contributors may be used to endorse or promote products derived
35 1.2.4.2 mjf * from this software without specific prior written permission.
36 1.2.4.2 mjf *
37 1.2.4.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
38 1.2.4.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
39 1.2.4.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 1.2.4.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
41 1.2.4.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 1.2.4.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 1.2.4.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 1.2.4.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 1.2.4.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 1.2.4.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 1.2.4.2 mjf * POSSIBILITY OF SUCH DAMAGE.
48 1.2.4.2 mjf *
49 1.2.4.2 mjf * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
50 1.2.4.2 mjf * All rights reserved.
51 1.2.4.2 mjf *
52 1.2.4.2 mjf * Written by Jason R. Thorpe for Wasabi Systems, Inc.
53 1.2.4.2 mjf *
54 1.2.4.2 mjf * Redistribution and use in source and binary forms, with or without
55 1.2.4.2 mjf * modification, are permitted provided that the following conditions
56 1.2.4.2 mjf * are met:
57 1.2.4.2 mjf * 1. Redistributions of source code must retain the above copyright
58 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer.
59 1.2.4.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
60 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer in the
61 1.2.4.2 mjf * documentation and/or other materials provided with the distribution.
62 1.2.4.2 mjf * 3. All advertising materials mentioning features or use of this software
63 1.2.4.2 mjf * must display the following acknowledgement:
64 1.2.4.2 mjf * This product includes software developed for the NetBSD Project by
65 1.2.4.2 mjf * Wasabi Systems, Inc.
66 1.2.4.2 mjf * 4. The name of Wasabi Systems, Inc. may not be used to endorse
67 1.2.4.2 mjf * or promote products derived from this software without specific prior
68 1.2.4.2 mjf * written permission.
69 1.2.4.2 mjf *
70 1.2.4.2 mjf * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
71 1.2.4.2 mjf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
72 1.2.4.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
73 1.2.4.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
74 1.2.4.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
75 1.2.4.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
76 1.2.4.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 1.2.4.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
78 1.2.4.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
79 1.2.4.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
80 1.2.4.2 mjf * POSSIBILITY OF SUCH DAMAGE.
81 1.2.4.2 mjf */
82 1.2.4.2 mjf
83 1.2.4.2 mjf #include <sys/cdefs.h>
84 1.2.4.2 mjf __KERNEL_RCSID(0, "$NetBSD: gemini_timer.c,v 1.2.4.2 2009/01/17 13:27:53 mjf Exp $");
85 1.2.4.2 mjf
86 1.2.4.2 mjf #include "opt_gemini.h"
87 1.2.4.2 mjf #include "opt_cpuoptions.h"
88 1.2.4.2 mjf
89 1.2.4.2 mjf #include <sys/types.h>
90 1.2.4.2 mjf #include <sys/param.h>
91 1.2.4.2 mjf #include <sys/systm.h>
92 1.2.4.2 mjf #include <sys/kernel.h>
93 1.2.4.2 mjf #include <sys/time.h>
94 1.2.4.2 mjf #include <sys/timetc.h>
95 1.2.4.2 mjf #include <sys/device.h>
96 1.2.4.2 mjf
97 1.2.4.2 mjf #include <dev/clock_subr.h>
98 1.2.4.2 mjf
99 1.2.4.2 mjf #include <machine/bus.h>
100 1.2.4.2 mjf #include <machine/intr.h>
101 1.2.4.2 mjf
102 1.2.4.2 mjf #include <arm/cpufunc.h>
103 1.2.4.2 mjf #include <arm/pic/picvar.h>
104 1.2.4.2 mjf
105 1.2.4.2 mjf #include <arm/gemini/gemini_reg.h>
106 1.2.4.2 mjf #include <arm/gemini/gemini_timervar.h>
107 1.2.4.2 mjf #include <arm/gemini/gemini_timervar.h>
108 1.2.4.2 mjf
109 1.2.4.2 mjf
110 1.2.4.2 mjf static const uint32_t counts_per_usec = (GEMINI_TIMER_CLOCK_FREQ / 1000000);
111 1.2.4.2 mjf static uint32_t counts_per_hz = ~0;
112 1.2.4.2 mjf
113 1.2.4.2 mjf struct geminitmr_softc *clock_sc;
114 1.2.4.2 mjf struct geminitmr_softc *stat_sc;
115 1.2.4.2 mjf struct geminitmr_softc *ref_sc;
116 1.2.4.2 mjf static uint32_t gemini_get_timecount(struct timecounter *);
117 1.2.4.2 mjf static void timer_init(geminitmr_softc_t *, int, boolean_t, boolean_t);
118 1.2.4.2 mjf static void timer_factors(geminitmr_softc_t *, int, boolean_t);
119 1.2.4.2 mjf
120 1.2.4.2 mjf #ifdef GEMINI_TIMER_DEBUG
121 1.2.4.2 mjf static void tfprint(uint, timer_factors_t *);
122 1.2.4.2 mjf #endif
123 1.2.4.2 mjf
124 1.2.4.2 mjf static struct timecounter gemini_timecounter = {
125 1.2.4.2 mjf .tc_get_timecount = gemini_get_timecount,
126 1.2.4.2 mjf .tc_counter_mask = 0xffffffff,
127 1.2.4.2 mjf .tc_frequency = GEMINI_TIMER_CLOCK_FREQ,
128 1.2.4.2 mjf .tc_name = "gpt",
129 1.2.4.2 mjf .tc_quality = 100,
130 1.2.4.2 mjf .tc_priv = NULL
131 1.2.4.2 mjf };
132 1.2.4.2 mjf
133 1.2.4.2 mjf static inline void
134 1.2.4.2 mjf _timer_intr_dis(struct geminitmr_softc *sc)
135 1.2.4.2 mjf {
136 1.2.4.2 mjf uint32_t r;
137 1.2.4.2 mjf
138 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRMASK);
139 1.2.4.2 mjf r |= GEMINI_TIMERn_INTRMASK(sc->sc_timerno);
140 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRMASK, r);
141 1.2.4.2 mjf }
142 1.2.4.2 mjf
143 1.2.4.2 mjf static inline void
144 1.2.4.2 mjf _timer_intr_enb(struct geminitmr_softc *sc)
145 1.2.4.2 mjf {
146 1.2.4.2 mjf uint32_t r;
147 1.2.4.2 mjf
148 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRMASK);
149 1.2.4.2 mjf r &= ~TIMER_INTRMASK_TMnMATCH1(sc->sc_timerno);
150 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRMASK, r);
151 1.2.4.2 mjf }
152 1.2.4.2 mjf
153 1.2.4.2 mjf static inline void
154 1.2.4.2 mjf _timer_intr_clr(struct geminitmr_softc *sc)
155 1.2.4.2 mjf {
156 1.2.4.2 mjf uint32_t r;
157 1.2.4.2 mjf int psw;
158 1.2.4.2 mjf
159 1.2.4.2 mjf psw = disable_interrupts(I32_bit);
160 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRSTATE);
161 1.2.4.2 mjf r &= ~GEMINI_TIMERn_INTRMASK(sc->sc_timerno);
162 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_INTRSTATE, r);
163 1.2.4.2 mjf restore_interrupts(psw);
164 1.2.4.2 mjf }
165 1.2.4.2 mjf
166 1.2.4.2 mjf static inline uint32_t
167 1.2.4.2 mjf _timer_read(struct geminitmr_softc *sc)
168 1.2.4.2 mjf {
169 1.2.4.2 mjf uint32_t r;
170 1.2.4.2 mjf
171 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
172 1.2.4.2 mjf GEMINI_TIMERn_COUNTER(sc->sc_timerno));
173 1.2.4.2 mjf
174 1.2.4.2 mjf return r;
175 1.2.4.2 mjf }
176 1.2.4.2 mjf
177 1.2.4.2 mjf static inline void
178 1.2.4.2 mjf _timer_stop(struct geminitmr_softc *sc)
179 1.2.4.2 mjf {
180 1.2.4.2 mjf uint32_t r;
181 1.2.4.2 mjf
182 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_TMCR);
183 1.2.4.2 mjf r &= ~GEMINI_TIMER_TMnCR_MASK(sc->sc_timerno);
184 1.2.4.2 mjf }
185 1.2.4.2 mjf
186 1.2.4.2 mjf /*
187 1.2.4.2 mjf * note:
188 1.2.4.2 mjf * This function assumes the timer is enabled.
189 1.2.4.2 mjf * If the timer is disabled, GEMINI_TIMERn_COUNTER(n) will hold the value.
190 1.2.4.2 mjf */
191 1.2.4.2 mjf static inline void
192 1.2.4.2 mjf _timer_reload(struct geminitmr_softc *sc, uint32_t val)
193 1.2.4.2 mjf {
194 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh,
195 1.2.4.2 mjf GEMINI_TIMERn_COUNTER(sc->sc_timerno), val);
196 1.2.4.2 mjf }
197 1.2.4.2 mjf
198 1.2.4.2 mjf static inline void
199 1.2.4.2 mjf _timer_start(struct geminitmr_softc *sc)
200 1.2.4.2 mjf {
201 1.2.4.2 mjf uint32_t r;
202 1.2.4.2 mjf uint n = sc->sc_timerno;
203 1.2.4.2 mjf timer_factors_t *tfp = &sc->sc_tf;
204 1.2.4.2 mjf
205 1.2.4.2 mjf /* set Counter, TmLoad, Match1, Match2 */
206 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh,
207 1.2.4.2 mjf GEMINI_TIMERn_COUNTER(n), tfp->tf_counter);
208 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh,
209 1.2.4.2 mjf GEMINI_TIMERn_LOAD(n), tfp->tf_reload);
210 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh,
211 1.2.4.2 mjf GEMINI_TIMERn_MATCH1(n), tfp->tf_match1);
212 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh,
213 1.2.4.2 mjf GEMINI_TIMERn_MATCH2(n), tfp->tf_match2);
214 1.2.4.2 mjf
215 1.2.4.2 mjf /* set TmCR */
216 1.2.4.2 mjf r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_TMCR);
217 1.2.4.2 mjf r &= ~GEMINI_TIMER_TMnCR_MASK(n);
218 1.2.4.2 mjf r |= tfp->tf_tmcr & GEMINI_TIMER_TMnCR_MASK(n);
219 1.2.4.2 mjf bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_TIMER_TMCR, r);
220 1.2.4.2 mjf
221 1.2.4.2 mjf }
222 1.2.4.2 mjf
223 1.2.4.2 mjf static uint32_t
224 1.2.4.2 mjf gemini_get_timecount(struct timecounter *tc)
225 1.2.4.2 mjf {
226 1.2.4.2 mjf uint32_t r;
227 1.2.4.2 mjf
228 1.2.4.2 mjf r = _timer_read(ref_sc);
229 1.2.4.2 mjf
230 1.2.4.2 mjf return -r;
231 1.2.4.2 mjf }
232 1.2.4.2 mjf
233 1.2.4.2 mjf int
234 1.2.4.2 mjf clockintr(void *frame)
235 1.2.4.2 mjf {
236 1.2.4.2 mjf struct geminitmr_softc *sc = clock_sc;;
237 1.2.4.2 mjf
238 1.2.4.2 mjf _timer_intr_clr(sc);
239 1.2.4.2 mjf _timer_reload(sc, sc->sc_tf.tf_counter);
240 1.2.4.2 mjf hardclock(frame);
241 1.2.4.2 mjf if (clock_sc == stat_sc)
242 1.2.4.2 mjf statclock(frame);
243 1.2.4.2 mjf return 1;
244 1.2.4.2 mjf }
245 1.2.4.2 mjf
246 1.2.4.2 mjf int
247 1.2.4.2 mjf statintr(void *frame)
248 1.2.4.2 mjf {
249 1.2.4.2 mjf struct geminitmr_softc *sc = stat_sc;;
250 1.2.4.2 mjf
251 1.2.4.2 mjf _timer_intr_clr(sc);
252 1.2.4.2 mjf _timer_reload(sc, sc->sc_tf.tf_counter);
253 1.2.4.2 mjf statclock(frame);
254 1.2.4.2 mjf return 1;
255 1.2.4.2 mjf }
256 1.2.4.2 mjf
257 1.2.4.2 mjf static void
258 1.2.4.2 mjf timer_init(geminitmr_softc_t *sc, int schz, boolean_t autoload, boolean_t intr)
259 1.2.4.2 mjf {
260 1.2.4.2 mjf int psw;
261 1.2.4.2 mjf
262 1.2.4.2 mjf psw = disable_interrupts(I32_bit);
263 1.2.4.2 mjf timer_factors(sc, schz, autoload);
264 1.2.4.2 mjf _timer_stop(sc);
265 1.2.4.2 mjf _timer_intr_dis(sc);
266 1.2.4.2 mjf _timer_intr_clr(sc);
267 1.2.4.2 mjf if (intr)
268 1.2.4.2 mjf _timer_intr_enb(sc);
269 1.2.4.2 mjf _timer_start(sc);
270 1.2.4.2 mjf psw = disable_interrupts(I32_bit);
271 1.2.4.2 mjf }
272 1.2.4.2 mjf
273 1.2.4.2 mjf void
274 1.2.4.2 mjf gemini_microtime_init()
275 1.2.4.2 mjf {
276 1.2.4.2 mjf if (ref_sc == NULL)
277 1.2.4.2 mjf panic("microtime reference timer was not configured.");
278 1.2.4.2 mjf timer_init(ref_sc, 0, TRUE, FALSE);
279 1.2.4.2 mjf }
280 1.2.4.2 mjf
281 1.2.4.2 mjf void
282 1.2.4.2 mjf setstatclockrate(int schz)
283 1.2.4.2 mjf {
284 1.2.4.2 mjf if (stat_sc == NULL)
285 1.2.4.2 mjf panic("Statistics timer was not configured.");
286 1.2.4.2 mjf if (stat_sc != clock_sc)
287 1.2.4.2 mjf timer_init(stat_sc, schz, FALSE, TRUE);
288 1.2.4.2 mjf }
289 1.2.4.2 mjf
290 1.2.4.2 mjf /*
291 1.2.4.2 mjf * clock_sc and stat_sc starts here
292 1.2.4.2 mjf * ref_sc is initialized already by obiotimer_attach
293 1.2.4.2 mjf */
294 1.2.4.2 mjf void
295 1.2.4.2 mjf cpu_initclocks(void)
296 1.2.4.2 mjf {
297 1.2.4.2 mjf if (clock_sc == NULL)
298 1.2.4.2 mjf panic("Clock timer was not configured.");
299 1.2.4.2 mjf if (stat_sc == NULL)
300 1.2.4.2 mjf panic("Statistics timer was not configured.");
301 1.2.4.2 mjf if (ref_sc == NULL)
302 1.2.4.2 mjf panic("Microtime reference timer was not configured.");
303 1.2.4.2 mjf
304 1.2.4.2 mjf /*
305 1.2.4.2 mjf * We already have the timers running, but not generating interrupts.
306 1.2.4.2 mjf * In addition, we've set stathz and profhz.
307 1.2.4.2 mjf */
308 1.2.4.2 mjf printf("clock: hz=%d stathz=%d\n", hz, stathz);
309 1.2.4.2 mjf
310 1.2.4.2 mjf /*
311 1.2.4.2 mjf * The "cookie" parameter must be zero to pass the interrupt frame
312 1.2.4.2 mjf * through to hardclock() and statclock().
313 1.2.4.2 mjf */
314 1.2.4.2 mjf intr_establish(clock_sc->sc_intr, IPL_CLOCK, IST_LEVEL_HIGH,
315 1.2.4.2 mjf clockintr, 0);
316 1.2.4.2 mjf
317 1.2.4.2 mjf if (clock_sc != stat_sc)
318 1.2.4.2 mjf intr_establish(stat_sc->sc_intr, IPL_HIGH, IST_LEVEL_HIGH,
319 1.2.4.2 mjf statintr, 0);
320 1.2.4.2 mjf
321 1.2.4.2 mjf timer_init(clock_sc, hz, FALSE, TRUE);
322 1.2.4.2 mjf if (clock_sc != stat_sc)
323 1.2.4.2 mjf timer_init(stat_sc, stathz, FALSE, TRUE);
324 1.2.4.2 mjf
325 1.2.4.2 mjf tc_init(&gemini_timecounter);
326 1.2.4.2 mjf }
327 1.2.4.2 mjf
328 1.2.4.2 mjf void
329 1.2.4.2 mjf delay(u_int n)
330 1.2.4.2 mjf {
331 1.2.4.2 mjf struct geminitmr_softc *sc = ref_sc;
332 1.2.4.2 mjf uint32_t cur, last, delta, usecs;
333 1.2.4.2 mjf
334 1.2.4.2 mjf if (sc == NULL)
335 1.2.4.2 mjf panic("The timer must be initialized sooner.");
336 1.2.4.2 mjf
337 1.2.4.2 mjf /*
338 1.2.4.2 mjf * This works by polling the timer and counting the
339 1.2.4.2 mjf * number of microseconds that go by.
340 1.2.4.2 mjf */
341 1.2.4.2 mjf last = _timer_read(sc);
342 1.2.4.2 mjf
343 1.2.4.2 mjf delta = usecs = 0;
344 1.2.4.2 mjf
345 1.2.4.2 mjf while (n > usecs) {
346 1.2.4.2 mjf cur = _timer_read(sc);
347 1.2.4.2 mjf
348 1.2.4.2 mjf /* Check to see if the timer has wrapped around. */
349 1.2.4.2 mjf if (last < cur)
350 1.2.4.2 mjf delta += (last + (counts_per_hz - cur));
351 1.2.4.2 mjf else
352 1.2.4.2 mjf delta += (last - cur);
353 1.2.4.2 mjf
354 1.2.4.2 mjf last = cur;
355 1.2.4.2 mjf
356 1.2.4.2 mjf if (delta >= counts_per_usec) {
357 1.2.4.2 mjf usecs += delta / counts_per_usec;
358 1.2.4.2 mjf delta %= counts_per_usec;
359 1.2.4.2 mjf }
360 1.2.4.2 mjf }
361 1.2.4.2 mjf }
362 1.2.4.2 mjf
363 1.2.4.2 mjf static void
364 1.2.4.2 mjf timer_factors(
365 1.2.4.2 mjf geminitmr_softc_t *sc,
366 1.2.4.2 mjf int ints_per_sec,
367 1.2.4.2 mjf boolean_t autoload)
368 1.2.4.2 mjf {
369 1.2.4.2 mjf timer_factors_t *tfp = &sc->sc_tf;
370 1.2.4.2 mjf uint n = sc->sc_timerno;
371 1.2.4.2 mjf const uint32_t us_per_sec = 1000000;
372 1.2.4.2 mjf
373 1.2.4.2 mjf /*
374 1.2.4.2 mjf * UPDOWN=0 (Down)
375 1.2.4.2 mjf * OFENABLE=0 (no Irpt on overflow)
376 1.2.4.2 mjf * CLOCK=0 (PCLK)
377 1.2.4.2 mjf * ENABLE=1
378 1.2.4.2 mjf */
379 1.2.4.2 mjf tfp->tf_tmcr = TIMER_TMCR_TMnENABLE(n);
380 1.2.4.2 mjf
381 1.2.4.2 mjf if (ints_per_sec == 0) {
382 1.2.4.2 mjf tfp->tf_counter = ~0U;
383 1.2.4.2 mjf } else {
384 1.2.4.2 mjf uint32_t count_freq;
385 1.2.4.2 mjf
386 1.2.4.2 mjf count_freq = GEMINI_TIMER_CLOCK_FREQ;
387 1.2.4.2 mjf count_freq /= ints_per_sec;
388 1.2.4.2 mjf tfp->tf_counter = count_freq;
389 1.2.4.2 mjf }
390 1.2.4.2 mjf tfp->tf_counts_per_usec = GEMINI_TIMER_CLOCK_FREQ / us_per_sec;
391 1.2.4.2 mjf
392 1.2.4.2 mjf if (autoload)
393 1.2.4.2 mjf tfp->tf_reload = tfp->tf_counter; /* auto-reload */
394 1.2.4.2 mjf else
395 1.2.4.2 mjf tfp->tf_reload = 0; /* no-auto_reload */
396 1.2.4.2 mjf
397 1.2.4.2 mjf tfp->tf_match1 = 0;
398 1.2.4.2 mjf tfp->tf_match2 = 0;
399 1.2.4.2 mjf
400 1.2.4.2 mjf #ifdef GEMINI_TIMER_DEBUG
401 1.2.4.2 mjf tfprint(sc->sc_timerno, tfp);
402 1.2.4.2 mjf Debugger();
403 1.2.4.2 mjf #endif
404 1.2.4.2 mjf }
405 1.2.4.2 mjf
406 1.2.4.2 mjf #ifdef GEMINI_TIMER_DEBUG
407 1.2.4.2 mjf void
408 1.2.4.2 mjf tfprint(uint n, timer_factors_t *tfp)
409 1.2.4.2 mjf {
410 1.2.4.2 mjf printf("%s: timer# %d\n", __FUNCTION__, n);
411 1.2.4.2 mjf printf("\ttf_counts_per_usec: %#x\n", tfp->tf_counts_per_usec);
412 1.2.4.2 mjf printf("\ttf_tmcr: %#x\n", tfp->tf_tmcr);
413 1.2.4.2 mjf printf("\ttf_counter: %#x\n", tfp->tf_counter);
414 1.2.4.2 mjf printf("\ttf_reload: %#x\n", tfp->tf_reload);
415 1.2.4.2 mjf printf("\ttf_match1: %#x\n", tfp->tf_match1);
416 1.2.4.2 mjf printf("\ttf_match2: %#x\n", tfp->tf_match2);
417 1.2.4.2 mjf }
418 1.2.4.2 mjf #endif
419