sa11x0_ost.c revision 1.16 1 /* $NetBSD: sa11x0_ost.c,v 1.16 2006/03/04 17:24:13 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.16 2006/03/04 17:24:13 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/device.h>
49
50 #include <machine/bus.h>
51 #include <machine/intr.h>
52
53 #include <arm/cpufunc.h>
54
55 #include <arm/arm32/katelib.h>
56
57 #include <arm/sa11x0/sa11x0_reg.h>
58 #include <arm/sa11x0/sa11x0_var.h>
59 #include <arm/sa11x0/sa11x0_ostreg.h>
60
61 static int saost_match(struct device *, struct cfdata *, void *);
62 static void saost_attach(struct device *, struct device *, void *);
63
64 int gettick(void);
65 static int clockintr(void *);
66 static int statintr(void *);
67 void rtcinit(void);
68
69 struct saost_softc {
70 struct device sc_dev;
71 bus_addr_t sc_baseaddr;
72 bus_space_tag_t sc_iot;
73 bus_space_handle_t sc_ioh;
74
75 uint32_t sc_clock_count;
76 uint32_t sc_statclock_count;
77 uint32_t sc_statclock_step;
78 };
79
80 static struct saost_softc *saost_sc = NULL;
81
82 #define TIMER_FREQUENCY 3686400 /* 3.6864MHz */
83 #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY/1000000)
84
85 #ifndef STATHZ
86 #define STATHZ 64
87 #endif
88
89 CFATTACH_DECL(saost, sizeof(struct saost_softc),
90 saost_match, saost_attach, NULL, NULL);
91
92 static int
93 saost_match(struct device *parent, struct cfdata *match, void *aux)
94 {
95 return (1);
96 }
97
98 void
99 saost_attach(struct device *parent, struct device *self, void *aux)
100 {
101 struct saost_softc *sc = (struct saost_softc*)self;
102 struct sa11x0_attach_args *sa = aux;
103
104 printf("\n");
105
106 sc->sc_iot = sa->sa_iot;
107 sc->sc_baseaddr = sa->sa_addr;
108
109 saost_sc = sc;
110
111 if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
112 &sc->sc_ioh))
113 panic("%s: Cannot map registers", self->dv_xname);
114
115 /* disable all channel and clear interrupt status */
116 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
117 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
118
119 printf("%s: SA-11x0 OS Timer\n", sc->sc_dev.dv_xname);
120 }
121
122 static int
123 clockintr(void *arg)
124 {
125 struct clockframe *frame = arg;
126 uint32_t oscr, nextmatch, oldmatch;
127 int s;
128
129 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
130 SAOST_SR, 1);
131
132 /* schedule next clock intr */
133 oldmatch = saost_sc->sc_clock_count;
134 nextmatch = oldmatch + TIMER_FREQUENCY / hz;
135
136 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
137 nextmatch);
138 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
139 SAOST_CR);
140
141 if ((nextmatch > oldmatch &&
142 (oscr > nextmatch || oscr < oldmatch)) ||
143 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
144 /*
145 * we couldn't set the matching register in time.
146 * just set it to some value so that next interrupt happens.
147 * XXX is it possible to compansate lost interrupts?
148 */
149
150 s = splhigh();
151 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
152 SAOST_CR);
153 nextmatch = oscr + 10;
154 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
155 SAOST_MR0, nextmatch);
156 splx(s);
157 }
158
159 saost_sc->sc_clock_count = nextmatch;
160 hardclock(frame);
161
162 return(1);
163 }
164
165 static int
166 statintr(void *arg)
167 {
168 struct clockframe *frame = arg;
169 uint32_t oscr, nextmatch, oldmatch;
170 int s;
171
172 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
173 SAOST_SR, 2);
174
175 /* schedule next clock intr */
176 oldmatch = saost_sc->sc_statclock_count;
177 nextmatch = oldmatch + saost_sc->sc_statclock_step;
178
179 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
180 nextmatch);
181 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
182 SAOST_CR);
183
184 if ((nextmatch > oldmatch &&
185 (oscr > nextmatch || oscr < oldmatch)) ||
186 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
187 /*
188 * we couldn't set the matching register in time.
189 * just set it to some value so that next interrupt happens.
190 * XXX is it possible to compansate lost interrupts?
191 */
192
193 s = splhigh();
194 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
195 SAOST_CR);
196 nextmatch = oscr + 10;
197 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
198 SAOST_MR1, nextmatch);
199 splx(s);
200 }
201
202 saost_sc->sc_statclock_count = nextmatch;
203 statclock(frame);
204
205 return(1);
206 }
207
208
209 void
210 setstatclockrate(int schz)
211 {
212 uint32_t count;
213
214 saost_sc->sc_statclock_step = TIMER_FREQUENCY / schz;
215 count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
216 count += saost_sc->sc_statclock_step;
217 saost_sc->sc_statclock_count = count;
218 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
219 SAOST_MR1, count);
220 }
221
222 void
223 cpu_initclocks(void)
224 {
225 stathz = STATHZ;
226 profhz = stathz;
227 saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
228
229 #ifdef OST_DEBUG
230 printf("clock: hz=%d stathz = %d\n", hz, stathz);
231 #endif
232
233 /* Use the channels 0 and 1 for hardclock and statclock, respectively */
234 saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
235 saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
236
237 sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
238 sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
239
240 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
241 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
242 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
243 saost_sc->sc_clock_count);
244 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
245 saost_sc->sc_statclock_count);
246
247 /* Zero the counter value */
248 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
249 }
250
251 int
252 gettick(void)
253 {
254 int counter;
255 u_int savedints;
256 savedints = disable_interrupts(I32_bit);
257
258 counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
259 SAOST_CR);
260
261 restore_interrupts(savedints);
262 return counter;
263 }
264
265 void
266 microtime(struct timeval *tvp)
267 {
268 int s, tm, deltatm;
269 static struct timeval lasttime;
270
271 if(saost_sc == NULL) {
272 tvp->tv_sec = 0;
273 tvp->tv_usec = 0;
274 return;
275 }
276
277 s = splhigh();
278 tm = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
279 SAOST_CR);
280
281 deltatm = saost_sc->sc_clock_count - tm;
282
283 #ifdef OST_DEBUG
284 printf("deltatm = %d\n",deltatm);
285 #endif
286
287 *tvp = time;
288 tvp->tv_usec++; /* XXX */
289 while (tvp->tv_usec >= 1000000) {
290 tvp->tv_sec++;
291 tvp->tv_usec -= 1000000;
292 }
293
294 if (tvp->tv_sec == lasttime.tv_sec &&
295 tvp->tv_usec <= lasttime.tv_usec &&
296 (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
297 {
298 tvp->tv_sec++;
299 tvp->tv_usec -= 1000000;
300 }
301 lasttime = *tvp;
302 splx(s);
303 }
304
305 void
306 delay(u_int usecs)
307 {
308 uint32_t xtick, otick, delta;
309 int j, csec, usec;
310
311 csec = usecs / 10000;
312 usec = usecs % 10000;
313
314 usecs = (TIMER_FREQUENCY / 100) * csec
315 + (TIMER_FREQUENCY / 100) * usec / 10000;
316
317 if (! saost_sc) {
318 /* clock isn't initialized yet */
319 for(; usecs > 0; usecs--)
320 for(j = 100; j > 0; j--)
321 ;
322 return;
323 }
324
325 otick = gettick();
326
327 while (1) {
328 for(j = 100; j > 0; j--)
329 ;
330 xtick = gettick();
331 delta = xtick - otick;
332 if (delta > usecs)
333 break;
334 usecs -= delta;
335 otick = xtick;
336 }
337 }
338
339 void
340 resettodr(void)
341 {
342 }
343
344 void
345 inittodr(time_t base)
346 {
347 time.tv_sec = base;
348 time.tv_usec = 0;
349 }
350