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