sa11x0_ost.c revision 1.20 1 /* $NetBSD: sa11x0_ost.c,v 1.20 2006/09/24 15:40:14 peter Exp $ */
2
3 /*
4 * Copyright (c) 1997 Mark Brinicombe.
5 * Copyright (c) 1997 Causality Limited.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: sa11x0_ost.c,v 1.20 2006/09/24 15:40:14 peter Exp $");
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/time.h>
48 #include <sys/timetc.h>
49 #include <sys/device.h>
50
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53
54 #include <arm/cpufunc.h>
55
56 #include <arm/sa11x0/sa11x0_reg.h>
57 #include <arm/sa11x0/sa11x0_var.h>
58 #include <arm/sa11x0/sa11x0_ostreg.h>
59
60 static int saost_match(struct device *, struct cfdata *, void *);
61 static void saost_attach(struct device *, struct device *, void *);
62
63 #ifdef __HAVE_TIMECOUNTER
64 static void saost_tc_init(void);
65 #endif /* __HAVE_TIMECOUNTER */
66
67 static uint32_t gettick(void);
68 static int clockintr(void *);
69 static int statintr(void *);
70
71 struct saost_softc {
72 struct device sc_dev;
73
74 bus_space_tag_t sc_iot;
75 bus_space_handle_t sc_ioh;
76
77 uint32_t sc_clock_count;
78 uint32_t sc_statclock_count;
79 uint32_t sc_statclock_step;
80 };
81
82 static struct saost_softc *saost_sc = NULL;
83
84 #define TIMER_FREQUENCY 3686400 /* 3.6864MHz */
85
86 #ifndef STATHZ
87 #define STATHZ 64
88 #endif
89
90 CFATTACH_DECL(saost, sizeof(struct saost_softc),
91 saost_match, saost_attach, NULL, NULL);
92
93 static int
94 saost_match(struct device *parent, struct cfdata *match, void *aux)
95 {
96
97 return 1;
98 }
99
100 static void
101 saost_attach(struct device *parent, struct device *self, void *aux)
102 {
103 struct saost_softc *sc = (struct saost_softc *)self;
104 struct sa11x0_attach_args *sa = aux;
105
106 printf("\n");
107
108 sc->sc_iot = sa->sa_iot;
109
110 saost_sc = sc;
111
112 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
113 &sc->sc_ioh))
114 panic("%s: Cannot map registers", self->dv_xname);
115
116 /* disable all channel and clear interrupt status */
117 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 0);
118 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
119
120 printf("%s: SA-11x0 OS Timer\n", sc->sc_dev.dv_xname);
121 }
122
123 static int
124 clockintr(void *arg)
125 {
126 struct saost_softc *sc = saost_sc;
127 struct clockframe *frame = arg;
128 uint32_t oscr, nextmatch, oldmatch;
129 int s;
130
131 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 1);
132
133 /* schedule next clock intr */
134 oldmatch = sc->sc_clock_count;
135 nextmatch = oldmatch + TIMER_FREQUENCY / hz;
136
137 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
138 oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
139
140 if ((nextmatch > oldmatch &&
141 (oscr > nextmatch || oscr < oldmatch)) ||
142 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
143 /*
144 * we couldn't set the matching register in time.
145 * just set it to some value so that next interrupt happens.
146 * XXX is it possible to compensate lost interrupts?
147 */
148
149 s = splhigh();
150 oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
151 nextmatch = oscr + 10;
152 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0, nextmatch);
153 splx(s);
154 }
155
156 sc->sc_clock_count = nextmatch;
157 hardclock(frame);
158
159 return 1;
160 }
161
162 static int
163 statintr(void *arg)
164 {
165 struct saost_softc *sc = saost_sc;
166 struct clockframe *frame = arg;
167 uint32_t oscr, nextmatch, oldmatch;
168 int s;
169
170 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 2);
171
172 /* schedule next clock intr */
173 oldmatch = sc->sc_statclock_count;
174 nextmatch = oldmatch + sc->sc_statclock_step;
175
176 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
177 oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
178
179 if ((nextmatch > oldmatch &&
180 (oscr > nextmatch || oscr < oldmatch)) ||
181 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
182 /*
183 * we couldn't set the matching register in time.
184 * just set it to some value so that next interrupt happens.
185 * XXX is it possible to compensate lost interrupts?
186 */
187
188 s = splhigh();
189 oscr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
190 nextmatch = oscr + 10;
191 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, nextmatch);
192 splx(s);
193 }
194
195 sc->sc_statclock_count = nextmatch;
196 statclock(frame);
197
198 return 1;
199 }
200
201 void
202 setstatclockrate(int schz)
203 {
204 struct saost_softc *sc = saost_sc;
205 uint32_t count;
206
207 sc->sc_statclock_step = TIMER_FREQUENCY / schz;
208 count = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
209 count += sc->sc_statclock_step;
210 sc->sc_statclock_count = count;
211 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1, count);
212 }
213
214 void
215 cpu_initclocks(void)
216 {
217 struct saost_softc *sc = saost_sc;
218
219 stathz = STATHZ;
220 profhz = stathz;
221 sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
222
223 printf("clock: hz=%d stathz=%d\n", hz, stathz);
224
225 /* Use the channels 0 and 1 for hardclock and statclock, respectively */
226 sc->sc_clock_count = TIMER_FREQUENCY / hz;
227 sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
228
229 sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
230 sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
231
232 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_SR, 0xf);
233 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_IR, 3);
234 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR0,
235 sc->sc_clock_count);
236 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_MR1,
237 sc->sc_statclock_count);
238
239 /* Zero the counter value */
240 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SAOST_CR, 0);
241
242 #ifdef __HAVE_TIMECOUNTER
243 saost_tc_init();
244 #endif /* __HAVE_TIMECOUNTER */
245 }
246
247 #ifdef __HAVE_TIMECOUNTER
248 static u_int
249 saost_tc_get_timecount(struct timecounter *tc)
250 {
251 return (u_int)gettick();
252 }
253
254 static void
255 saost_tc_init(void)
256 {
257 static struct timecounter saost_tc = {
258 .tc_get_timecount = saost_tc_get_timecount,
259 .tc_frequency = TIMER_FREQUENCY,
260 .tc_counter_mask = ~0,
261 .tc_name = "saost_count",
262 .tc_quality = 100,
263 };
264
265 tc_init(&saost_tc);
266 }
267 #endif /* __HAVE_TIMECOUNTER */
268
269 static uint32_t
270 gettick(void)
271 {
272 struct saost_softc *sc = saost_sc;
273 uint32_t counter;
274 u_int saved_ints;
275
276 saved_ints = disable_interrupts(I32_bit);
277 counter = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
278 restore_interrupts(saved_ints);
279
280 return counter;
281 }
282
283 #ifndef __HAVE_TIMECOUNTER
284 void
285 microtime(struct timeval *tvp)
286 {
287 struct saost_softc *sc = saost_sc;
288 int s, tm, deltatm;
289 static struct timeval lasttime;
290
291 if (sc == NULL) {
292 tvp->tv_sec = 0;
293 tvp->tv_usec = 0;
294 return;
295 }
296
297 s = splhigh();
298 tm = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SAOST_CR);
299
300 deltatm = sc->sc_clock_count - tm;
301
302 *tvp = time;
303 tvp->tv_usec++; /* XXX */
304 while (tvp->tv_usec >= 1000000) {
305 tvp->tv_sec++;
306 tvp->tv_usec -= 1000000;
307 }
308
309 if (tvp->tv_sec == lasttime.tv_sec &&
310 tvp->tv_usec <= lasttime.tv_usec &&
311 (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
312 {
313 tvp->tv_sec++;
314 tvp->tv_usec -= 1000000;
315 }
316 lasttime = *tvp;
317 splx(s);
318 }
319 #endif /* !__HAVE_TIMECOUNTER */
320
321 void
322 delay(u_int usecs)
323 {
324 uint32_t xtick, otick, delta;
325 int j, csec, usec;
326
327 csec = usecs / 10000;
328 usec = usecs % 10000;
329
330 usecs = (TIMER_FREQUENCY / 100) * csec
331 + (TIMER_FREQUENCY / 100) * usec / 10000;
332
333 if (saost_sc == NULL) {
334 /* clock isn't initialized yet */
335 for (; usecs > 0; usecs--)
336 for (j = 100; j > 0; j--)
337 continue;
338 return;
339 }
340
341 otick = gettick();
342
343 while (1) {
344 for (j = 100; j > 0; j--)
345 continue;
346 xtick = gettick();
347 delta = xtick - otick;
348 if (delta > usecs)
349 break;
350 usecs -= delta;
351 otick = xtick;
352 }
353 }
354
355 #ifndef __HAVE_GENERIC_TODR
356 void
357 resettodr(void)
358 {
359 }
360
361 void
362 inittodr(time_t base)
363 {
364 time.tv_sec = base;
365 time.tv_usec = 0;
366 }
367 #endif /* !__HAVE_GENERIC_TODR */
368