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