1 /* $NetBSD: sa11x0_ost.c,v 1.11 2003/07/15 00:24:51 lukem Exp $ */
4 * Copyright (c) 1997 Mark Brinicombe.
5 * Copyright (c) 1997 Causality Limited.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <machine/bus.h>
52 #include <machine/resource.h>
53 #include <machine/intr.h>
55 #include <machine/cpu.h>
56 #include <machine/cpufunc.h>
57 #include <machine/frame.h>
59 #include <arm/sa11x0/sa11x0_reg.h>
60 #include <arm/sa11x0/sa11x0_var.h>
61 #include <arm/sa11x0/sa11x0_ostreg.h>
63 static int saost_probe(device_t);
64 static int saost_attach(device_t);
67 static int clockintr(void *);
69 static int statintr(void *);
74 static struct mtx clock_lock;
79 bus_addr_t sc_baseaddr;
80 bus_space_tag_t sc_iot;
81 bus_space_handle_t sc_ioh;
83 u_int32_t sc_clock_count;
84 u_int32_t sc_statclock_count;
85 u_int32_t sc_statclock_step;
88 static struct saost_softc *saost_sc = NULL;
90 #define TIMER_FREQUENCY 3686400 /* 3.6864MHz */
91 #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY/1000000)
97 static device_method_t saost_methods[] = {
98 DEVMETHOD(device_probe, saost_probe),
99 DEVMETHOD(device_attach, saost_attach),
103 static driver_t saost_driver = {
106 sizeof(struct saost_softc),
108 static devclass_t saost_devclass;
110 DRIVER_MODULE(saost, saip, saost_driver, saost_devclass, 0, 0);
112 saost_probe(device_t dev)
119 saost_attach(device_t dev)
121 struct saost_softc *sc = device_get_softc(dev);
122 struct sa11x0_softc *sa = device_get_softc(device_get_parent(dev));
125 sc->sc_iot = sa->sc_iot;
126 sc->sc_baseaddr = 0x90000000;
130 if(bus_space_map(sa->sc_iot, sc->sc_baseaddr, 8, 0,
132 panic("%s: Cannot map registers", device_get_name(dev));
134 /* disable all channel and clear interrupt status */
135 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
136 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
145 struct trapframe *frame = arg;
146 u_int32_t oscr, nextmatch, oldmatch;
150 mtx_lock_spin(&clock_lock);
152 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
155 /* schedule next clock intr */
156 oldmatch = saost_sc->sc_clock_count;
157 nextmatch = oldmatch + TIMER_FREQUENCY / hz;
159 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
162 if ((nextmatch > oldmatch &&
163 (oscr > nextmatch || oscr < oldmatch)) ||
164 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
166 * we couldn't set the matching register in time.
167 * just set it to some value so that next interrupt happens.
168 * XXX is it possible to compansate lost interrupts?
172 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
174 nextmatch = oscr + 10;
177 saost_sc->sc_clock_count = nextmatch;
178 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
180 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
182 mtx_unlock_spin(&clock_lock);
184 return (FILTER_HANDLED);
192 struct trapframe *frame = arg;
193 u_int32_t oscr, nextmatch, oldmatch;
196 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
199 /* schedule next clock intr */
200 oldmatch = saost_sc->sc_statclock_count;
201 nextmatch = oldmatch + saost_sc->sc_statclock_step;
203 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
205 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
208 if ((nextmatch > oldmatch &&
209 (oscr > nextmatch || oscr < oldmatch)) ||
210 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
212 * we couldn't set the matching register in time.
213 * just set it to some value so that next interrupt happens.
214 * XXX is it possible to compansate lost interrupts?
218 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
220 nextmatch = oscr + 10;
221 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
222 SAOST_MR1, nextmatch);
226 saost_sc->sc_statclock_count = nextmatch;
227 statclock(TRAPF_USERMODE(frame));
228 return (FILTER_HANDLED);
234 setstatclockrate(int hz)
238 saost_sc->sc_statclock_step = TIMER_FREQUENCY / hz;
239 count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
240 count += saost_sc->sc_statclock_step;
241 saost_sc->sc_statclock_count = count;
242 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
249 device_t dev = saost_sc->sc_dev;
254 mtx_init(&clock_lock, "SA1110 Clock locké", NULL, MTX_SPIN);
256 saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
257 struct resource *irq1, *irq2;
259 void *ih1/*, *ih2 */;
261 printf("clock: hz=%d stathz = %d\n", hz, stathz);
263 /* Use the channels 0 and 1 for hardclock and statclock, respectively */
264 saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
265 saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
267 irq1 = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0,
270 irq2 = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
272 bus_setup_intr(dev, irq1, INTR_TYPE_CLK, clockintr, NULL, NULL,
275 bus_setup_intr(dev, irq2, INTR_TYPE_CLK, statintr, NULL, NULL,
278 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
279 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
280 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
281 saost_sc->sc_clock_count);
283 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
286 /* Zero the counter value */
287 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
295 savedints = disable_interrupts(I32_bit);
297 counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
300 restore_interrupts(savedints);
308 u_int32_t tick, otick, delta;
311 csec = usecs / 10000;
312 usec = usecs % 10000;
314 usecs = (TIMER_FREQUENCY / 100) * csec
315 + (TIMER_FREQUENCY / 100) * usec / 10000;
318 /* clock isn't initialized yet */
319 for(; usecs > 0; usecs--)
320 for(j = 100; j > 0; j--)
326 mtx_lock_spin(&clock_lock);
331 for(j = 100; j > 0; j--)
334 delta = tick - otick;
342 mtx_unlock_spin(&clock_lock);
347 cpu_startprofclock(void)
349 printf("STARTPROFCLOCK\n");
353 cpu_stopprofclock(void)