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