1*cf10107dSdyoung /* $NetBSD: obio_timer.c,v 1.5 2011/07/01 19:32:28 dyoung Exp $ */
2f5d7ce3dSmatt
3f5d7ce3dSmatt /* adapted from:
4f5d7ce3dSmatt * NetBSD: obio_mputmr.c,v 1.3 2008/08/27 11:03:10 matt Exp
5f5d7ce3dSmatt */
6f5d7ce3dSmatt
7f5d7ce3dSmatt /*
8f5d7ce3dSmatt * Based on omap_mputmr.c
9f5d7ce3dSmatt * Based on i80321_timer.c and arch/arm/sa11x0/sa11x0_ost.c
10f5d7ce3dSmatt *
11f5d7ce3dSmatt * Copyright (c) 1997 Mark Brinicombe.
12f5d7ce3dSmatt * Copyright (c) 1997 Causality Limited.
13f5d7ce3dSmatt * All rights reserved.
14f5d7ce3dSmatt *
15f5d7ce3dSmatt * This code is derived from software contributed to The NetBSD Foundation
16f5d7ce3dSmatt * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
17f5d7ce3dSmatt *
18f5d7ce3dSmatt * Redistribution and use in source and binary forms, with or without
19f5d7ce3dSmatt * modification, are permitted provided that the following conditions
20f5d7ce3dSmatt * are met:
21f5d7ce3dSmatt * 1. Redistributions of source code must retain the above copyright
22f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer.
23f5d7ce3dSmatt * 2. Redistributions in binary form must reproduce the above copyright
24f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer in the
25f5d7ce3dSmatt * documentation and/or other materials provided with the distribution.
26f5d7ce3dSmatt * 3. All advertising materials mentioning features or use of this software
27f5d7ce3dSmatt * must display the following acknowledgement:
28f5d7ce3dSmatt * This product includes software developed by the NetBSD
29f5d7ce3dSmatt * Foundation, Inc. and its contributors.
30f5d7ce3dSmatt * 4. Neither the name of The NetBSD Foundation nor the names of its
31f5d7ce3dSmatt * contributors may be used to endorse or promote products derived
32f5d7ce3dSmatt * from this software without specific prior written permission.
33f5d7ce3dSmatt *
34f5d7ce3dSmatt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35f5d7ce3dSmatt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36f5d7ce3dSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37f5d7ce3dSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38f5d7ce3dSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39f5d7ce3dSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40f5d7ce3dSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41f5d7ce3dSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42f5d7ce3dSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43f5d7ce3dSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44f5d7ce3dSmatt * POSSIBILITY OF SUCH DAMAGE.
45f5d7ce3dSmatt *
46f5d7ce3dSmatt * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
47f5d7ce3dSmatt * All rights reserved.
48f5d7ce3dSmatt *
49f5d7ce3dSmatt * Written by Jason R. Thorpe for Wasabi Systems, Inc.
50f5d7ce3dSmatt *
51f5d7ce3dSmatt * Redistribution and use in source and binary forms, with or without
52f5d7ce3dSmatt * modification, are permitted provided that the following conditions
53f5d7ce3dSmatt * are met:
54f5d7ce3dSmatt * 1. Redistributions of source code must retain the above copyright
55f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer.
56f5d7ce3dSmatt * 2. Redistributions in binary form must reproduce the above copyright
57f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer in the
58f5d7ce3dSmatt * documentation and/or other materials provided with the distribution.
59f5d7ce3dSmatt * 3. All advertising materials mentioning features or use of this software
60f5d7ce3dSmatt * must display the following acknowledgement:
61f5d7ce3dSmatt * This product includes software developed for the NetBSD Project by
62f5d7ce3dSmatt * Wasabi Systems, Inc.
63f5d7ce3dSmatt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
64f5d7ce3dSmatt * or promote products derived from this software without specific prior
65f5d7ce3dSmatt * written permission.
66f5d7ce3dSmatt *
67f5d7ce3dSmatt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
68f5d7ce3dSmatt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
69f5d7ce3dSmatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
70f5d7ce3dSmatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
71f5d7ce3dSmatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
72f5d7ce3dSmatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
73f5d7ce3dSmatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
74f5d7ce3dSmatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
75f5d7ce3dSmatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76f5d7ce3dSmatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
77f5d7ce3dSmatt * POSSIBILITY OF SUCH DAMAGE.
78f5d7ce3dSmatt *
79f5d7ce3dSmatt * Copyright (c) 2007 Microsoft
80f5d7ce3dSmatt * All rights reserved.
81f5d7ce3dSmatt *
82f5d7ce3dSmatt * Redistribution and use in source and binary forms, with or without
83f5d7ce3dSmatt * modification, are permitted provided that the following conditions
84f5d7ce3dSmatt * are met:
85f5d7ce3dSmatt * 1. Redistributions of source code must retain the above copyright
86f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer.
87f5d7ce3dSmatt * 2. Redistributions in binary form must reproduce the above copyright
88f5d7ce3dSmatt * notice, this list of conditions and the following disclaimer in the
89f5d7ce3dSmatt * documentation and/or other materials provided with the distribution.
90f5d7ce3dSmatt * 3. All advertising materials mentioning features or use of this software
91f5d7ce3dSmatt * must display the following acknowledgement:
92f5d7ce3dSmatt * This product includes software developed by Microsoft
93f5d7ce3dSmatt *
94f5d7ce3dSmatt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
95f5d7ce3dSmatt * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
96f5d7ce3dSmatt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
97f5d7ce3dSmatt * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
98f5d7ce3dSmatt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
99f5d7ce3dSmatt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
100f5d7ce3dSmatt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101f5d7ce3dSmatt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102f5d7ce3dSmatt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103f5d7ce3dSmatt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104f5d7ce3dSmatt * SUCH DAMAGE.
105f5d7ce3dSmatt */
106f5d7ce3dSmatt
107f5d7ce3dSmatt #include <sys/cdefs.h>
108*cf10107dSdyoung __KERNEL_RCSID(0, "$NetBSD: obio_timer.c,v 1.5 2011/07/01 19:32:28 dyoung Exp $");
109f5d7ce3dSmatt
110f5d7ce3dSmatt #include "opt_cpuoptions.h"
111f5d7ce3dSmatt #include "opt_gemini.h"
112f5d7ce3dSmatt #include "locators.h"
113f5d7ce3dSmatt
114f5d7ce3dSmatt #include <sys/types.h>
115f5d7ce3dSmatt #include <sys/param.h>
116f5d7ce3dSmatt #include <sys/systm.h>
117f5d7ce3dSmatt #include <sys/kernel.h>
118f5d7ce3dSmatt #include <sys/time.h>
119f5d7ce3dSmatt #include <sys/device.h>
120f5d7ce3dSmatt
121f5d7ce3dSmatt #include <dev/clock_subr.h>
122f5d7ce3dSmatt
123*cf10107dSdyoung #include <sys/bus.h>
124f5d7ce3dSmatt #include <machine/intr.h>
125f5d7ce3dSmatt
126f5d7ce3dSmatt #include <arm/gemini/gemini_reg.h>
127f5d7ce3dSmatt #include <arm/gemini/gemini_obiovar.h>
128f5d7ce3dSmatt #include <arm/gemini/gemini_timervar.h>
129f5d7ce3dSmatt
1303100fdacScliff #if STATHZ != HZ
1313100fdacScliff # error system clock HZ and stat clock STATHZ must be same
1323100fdacScliff #endif
1333100fdacScliff
134f5d7ce3dSmatt
135f5d7ce3dSmatt #ifndef GEMINI_TIMER_CLOCK_FREQ
13601b65de4Scliff # error Specify the timer frequency in Hz with option GEMINI_TIMER_CLOCK_FREQ
137f5d7ce3dSmatt #endif
138f5d7ce3dSmatt
139f5d7ce3dSmatt static int obiotimer_match(device_t, struct cfdata *, void *);
140f5d7ce3dSmatt static void obiotimer_attach(device_t, device_t, void *);
141f5d7ce3dSmatt
142f5d7ce3dSmatt struct geminitmr_softc xsc;
143f5d7ce3dSmatt
144f5d7ce3dSmatt
145f5d7ce3dSmatt
146f5d7ce3dSmatt typedef struct {
147f5d7ce3dSmatt uint timerno;
148f5d7ce3dSmatt bus_addr_t addr;
149f5d7ce3dSmatt uint intr;
150f5d7ce3dSmatt } obiotimer_instance_t;
151f5d7ce3dSmatt
152f5d7ce3dSmatt /* XXX
153f5d7ce3dSmatt * this table can be used to match the GP Timers
154f5d7ce3dSmatt * until we use config(8) locators to distinguish between
155f5d7ce3dSmatt * gemini "sub-timers".
156f5d7ce3dSmatt */
157f5d7ce3dSmatt #define GPT_ENTRY(n, i) { \
158f5d7ce3dSmatt .timerno = (n), \
159f5d7ce3dSmatt .addr = GEMINI_TIMER_BASE, \
160f5d7ce3dSmatt .intr = i, \
161f5d7ce3dSmatt }
162f5d7ce3dSmatt static const obiotimer_instance_t obiotimer_instance_tab[] = {
163f5d7ce3dSmatt GPT_ENTRY(1, 14),
164f5d7ce3dSmatt GPT_ENTRY(2, 15),
165f5d7ce3dSmatt GPT_ENTRY(3, 16),
166f5d7ce3dSmatt };
167f5d7ce3dSmatt #undef GPT_ENTRY
168f5d7ce3dSmatt #define GPTIMER_INSTANCE_CNT __arraycount(obiotimer_instance_tab)
169f5d7ce3dSmatt
170f5d7ce3dSmatt static const obiotimer_instance_t *
171f5d7ce3dSmatt obiotimer_lookup(struct obio_attach_args *);
172f5d7ce3dSmatt static void obiotimer_enable(struct geminitmr_softc *,
173f5d7ce3dSmatt struct obio_attach_args *,
174f5d7ce3dSmatt const obiotimer_instance_t *);
175f5d7ce3dSmatt
176f5d7ce3dSmatt static int obiotimer_match(device_t, struct cfdata *, void *);
177f5d7ce3dSmatt static void obiotimer_attach(device_t, device_t, void *);
178f5d7ce3dSmatt
179f5d7ce3dSmatt
180f5d7ce3dSmatt CFATTACH_DECL_NEW(obiotimer, sizeof(struct geminitmr_softc),
181f5d7ce3dSmatt obiotimer_match, obiotimer_attach, NULL, NULL);
182f5d7ce3dSmatt
183f5d7ce3dSmatt
184f5d7ce3dSmatt static int
obiotimer_match(device_t parent,struct cfdata * match,void * aux)185f5d7ce3dSmatt obiotimer_match(device_t parent, struct cfdata *match, void *aux)
186f5d7ce3dSmatt {
187f5d7ce3dSmatt struct obio_attach_args *obio = aux;
188f5d7ce3dSmatt
1893100fdacScliff if ((obio->obio_addr == OBIOCF_ADDR_DEFAULT)
190f5d7ce3dSmatt || (obio->obio_intr == OBIOCF_INTR_DEFAULT))
191f5d7ce3dSmatt panic("geminitmr must have addr and intr specified in config.");
192f5d7ce3dSmatt
193f5d7ce3dSmatt if (obiotimer_lookup(obio) == NULL)
194f5d7ce3dSmatt return 0;
195f5d7ce3dSmatt
196f5d7ce3dSmatt return 1;
197f5d7ce3dSmatt }
198f5d7ce3dSmatt
199f5d7ce3dSmatt void
obiotimer_attach(device_t parent,device_t self,void * aux)200f5d7ce3dSmatt obiotimer_attach(device_t parent, device_t self, void *aux)
201f5d7ce3dSmatt {
202f5d7ce3dSmatt struct geminitmr_softc *sc = device_private(self);
203f5d7ce3dSmatt struct obio_attach_args *obio = aux;
204f5d7ce3dSmatt const obiotimer_instance_t *ip;
20554c2458eScliff #ifndef GEMINI_SLAVE
206f5d7ce3dSmatt static int once=1;
20754c2458eScliff #endif
208f5d7ce3dSmatt
209f5d7ce3dSmatt ip = obiotimer_lookup(obio);
210f5d7ce3dSmatt if (ip == NULL)
211f5d7ce3dSmatt panic("%s: bad lookup", device_xname(self));
212f5d7ce3dSmatt /* should not fail since we already matched */
213f5d7ce3dSmatt
214f5d7ce3dSmatt sc->sc_timerno = ip->timerno;
215f5d7ce3dSmatt sc->sc_iot = obio->obio_iot;
216f5d7ce3dSmatt sc->sc_intr = obio->obio_intr;
217f5d7ce3dSmatt sc->sc_addr = obio->obio_addr;
2183100fdacScliff sc->sc_size = (obio->obio_size == OBIOCF_SIZE_DEFAULT)
219f5d7ce3dSmatt ? (GEMINI_TIMER_INTRMASK + 4)
220f5d7ce3dSmatt : obio->obio_size;
221f5d7ce3dSmatt
222f5d7ce3dSmatt if (bus_space_map(sc->sc_iot, sc->sc_addr, sc->sc_size, 0, &sc->sc_ioh))
223f5d7ce3dSmatt panic("%s: Cannot map registers", device_xname(self));
224f5d7ce3dSmatt
225f5d7ce3dSmatt obiotimer_enable(sc, obio, obiotimer_lookup(obio));
226f5d7ce3dSmatt aprint_normal("\n");
227f5d7ce3dSmatt aprint_naive("\n");
228f5d7ce3dSmatt
22954c2458eScliff #ifndef GEMINI_SLAVE
230f5d7ce3dSmatt if (once) {
231f5d7ce3dSmatt once = 0;
232f5d7ce3dSmatt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
233f5d7ce3dSmatt GEMINI_TIMER_TMCR, 0);
234f5d7ce3dSmatt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
235f5d7ce3dSmatt GEMINI_TIMER_INTRMASK, (uint32_t)~TIMER_INTRMASK_Resv);
236f5d7ce3dSmatt bus_space_write_4(sc->sc_iot, sc->sc_ioh,
237f5d7ce3dSmatt GEMINI_TIMER_INTRSTATE, 0);
238f5d7ce3dSmatt }
23954c2458eScliff #endif
240f5d7ce3dSmatt
241f5d7ce3dSmatt switch (sc->sc_timerno) {
242f5d7ce3dSmatt case 1:
2433100fdacScliff #ifndef GEMINI_SLAVE
244f5d7ce3dSmatt /*
2453100fdacScliff * timer #1 is the combined system clock and stat clock
24601b65de4Scliff * for the Master or Single Gemini CPU
247f5d7ce3dSmatt * it gets started later
248f5d7ce3dSmatt */
2493100fdacScliff profhz = stathz = hz;
2503100fdacScliff stat_sc = clock_sc = sc;
2513100fdacScliff #endif
252f5d7ce3dSmatt break;
253f5d7ce3dSmatt case 2:
2543100fdacScliff #ifdef GEMINI_SLAVE
255f5d7ce3dSmatt /*
2563100fdacScliff * timer #2 is the combined system clock and stat clock
2573100fdacScliff * for the Slave Gemini CPU
258f5d7ce3dSmatt * it gets started later
259f5d7ce3dSmatt */
2603100fdacScliff profhz = stathz = hz;
2613100fdacScliff stat_sc = clock_sc = sc;
2623100fdacScliff #endif
263f5d7ce3dSmatt break;
264f5d7ce3dSmatt case 3:
265f5d7ce3dSmatt /*
26601b65de4Scliff * Timer #3 is used for microtime reference clock and delay()
26701b65de4Scliff * autoloading, non-interrupting, just wraps around
268f5d7ce3dSmatt * we start it now to make delay() available
269f5d7ce3dSmatt */
270f5d7ce3dSmatt ref_sc = sc;
27154c2458eScliff #ifndef GEMINI_SLAVE
272f5d7ce3dSmatt gemini_microtime_init();
27354c2458eScliff #endif
274f5d7ce3dSmatt break;
275f5d7ce3dSmatt default:
276f5d7ce3dSmatt panic("bad gemini timer number %d\n", sc->sc_timerno);
277f5d7ce3dSmatt break;
278f5d7ce3dSmatt }
279f5d7ce3dSmatt }
280f5d7ce3dSmatt
281f5d7ce3dSmatt static const obiotimer_instance_t *
obiotimer_lookup(struct obio_attach_args * obio)282f5d7ce3dSmatt obiotimer_lookup(struct obio_attach_args *obio)
283f5d7ce3dSmatt {
284f5d7ce3dSmatt const obiotimer_instance_t *ip;
285f5d7ce3dSmatt uint i;
286f5d7ce3dSmatt
287f5d7ce3dSmatt for (i = 0, ip = obiotimer_instance_tab;
288f5d7ce3dSmatt i < GPTIMER_INSTANCE_CNT; i++, ip++) {
289f5d7ce3dSmatt if (ip->addr == obio->obio_addr && ip->intr == obio->obio_intr)
290f5d7ce3dSmatt return ip;
291f5d7ce3dSmatt }
292f5d7ce3dSmatt
293f5d7ce3dSmatt return NULL;
294f5d7ce3dSmatt }
295f5d7ce3dSmatt
296f5d7ce3dSmatt void
obiotimer_enable(struct geminitmr_softc * sc,struct obio_attach_args * obio,const obiotimer_instance_t * ip)297f5d7ce3dSmatt obiotimer_enable(
298f5d7ce3dSmatt struct geminitmr_softc *sc,
299f5d7ce3dSmatt struct obio_attach_args *obio,
300f5d7ce3dSmatt const obiotimer_instance_t *ip)
301f5d7ce3dSmatt {
302f5d7ce3dSmatt /* nothing to do */
303f5d7ce3dSmatt }
304