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