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/clock.h>
49 #include <sys/module.h>
51 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #include <machine/intr.h>
56 #include <machine/cpu.h>
57 #include <machine/cpufunc.h>
58 #include <machine/frame.h>
60 #include <arm/sa11x0/sa11x0_reg.h>
61 #include <arm/sa11x0/sa11x0_var.h>
62 #include <arm/sa11x0/sa11x0_ostreg.h>
64 static int saost_probe(device_t);
65 static int saost_attach(device_t);
68 static int clockintr(void *);
70 static int statintr(void *);
75 static struct mtx clock_lock;
80 bus_addr_t sc_baseaddr;
81 bus_space_tag_t sc_iot;
82 bus_space_handle_t sc_ioh;
84 u_int32_t sc_clock_count;
85 u_int32_t sc_statclock_count;
86 u_int32_t sc_statclock_step;
89 static struct saost_softc *saost_sc = NULL;
91 #define TIMER_FREQUENCY 3686400 /* 3.6864MHz */
92 #define TICKS_PER_MICROSECOND (TIMER_FREQUENCY/1000000)
98 static device_method_t saost_methods[] = {
99 DEVMETHOD(device_probe, saost_probe),
100 DEVMETHOD(device_attach, saost_attach),
104 static driver_t saost_driver = {
107 sizeof(struct saost_softc),
109 static devclass_t saost_devclass;
111 DRIVER_MODULE(saost, saip, saost_driver, saost_devclass, 0, 0);
113 saost_probe(device_t dev)
120 saost_attach(device_t dev)
122 struct saost_softc *sc = device_get_softc(dev);
123 struct sa11x0_softc *sa = device_get_softc(device_get_parent(dev));
126 sc->sc_iot = sa->sc_iot;
127 sc->sc_baseaddr = 0x90000000;
131 if(bus_space_map(sa->sc_iot, sc->sc_baseaddr, 8, 0,
133 panic("%s: Cannot map registers", device_get_name(dev));
135 /* disable all channel and clear interrupt status */
136 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
137 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
146 struct trapframe *frame = arg;
147 u_int32_t oscr, nextmatch, oldmatch;
151 mtx_lock_spin(&clock_lock);
153 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
156 /* schedule next clock intr */
157 oldmatch = saost_sc->sc_clock_count;
158 nextmatch = oldmatch + TIMER_FREQUENCY / hz;
160 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
163 if ((nextmatch > oldmatch &&
164 (oscr > nextmatch || oscr < oldmatch)) ||
165 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
167 * we couldn't set the matching register in time.
168 * just set it to some value so that next interrupt happens.
169 * XXX is it possible to compansate lost interrupts?
173 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
175 nextmatch = oscr + 10;
178 saost_sc->sc_clock_count = nextmatch;
179 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
181 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
183 mtx_unlock_spin(&clock_lock);
185 return (FILTER_HANDLED);
193 struct trapframe *frame = arg;
194 u_int32_t oscr, nextmatch, oldmatch;
197 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
200 /* schedule next clock intr */
201 oldmatch = saost_sc->sc_statclock_count;
202 nextmatch = oldmatch + saost_sc->sc_statclock_step;
204 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
206 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
209 if ((nextmatch > oldmatch &&
210 (oscr > nextmatch || oscr < oldmatch)) ||
211 (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
213 * we couldn't set the matching register in time.
214 * just set it to some value so that next interrupt happens.
215 * XXX is it possible to compansate lost interrupts?
219 oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
221 nextmatch = oscr + 10;
222 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
223 SAOST_MR1, nextmatch);
227 saost_sc->sc_statclock_count = nextmatch;
228 statclock(TRAPF_USERMODE(frame));
229 return (FILTER_HANDLED);
235 setstatclockrate(int hz)
239 saost_sc->sc_statclock_step = TIMER_FREQUENCY / hz;
240 count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
241 count += saost_sc->sc_statclock_step;
242 saost_sc->sc_statclock_count = count;
243 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
250 device_t dev = saost_sc->sc_dev;
255 mtx_init(&clock_lock, "SA1110 Clock locké", NULL, MTX_SPIN);
257 saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
258 struct resource *irq1, *irq2;
260 void *ih1/*, *ih2 */;
262 printf("clock: hz=%d stathz = %d\n", hz, stathz);
264 /* Use the channels 0 and 1 for hardclock and statclock, respectively */
265 saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
266 saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
268 irq1 = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0,
271 irq2 = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
273 bus_setup_intr(dev, irq1, INTR_TYPE_CLK, clockintr, NULL, NULL,
276 bus_setup_intr(dev, irq2, INTR_TYPE_CLK, statintr, NULL, NULL,
279 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
280 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
281 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
282 saost_sc->sc_clock_count);
284 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
287 /* Zero the counter value */
288 bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
296 savedints = disable_interrupts(I32_bit);
298 counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
301 restore_interrupts(savedints);
309 u_int32_t tick, otick, delta;
312 csec = usecs / 10000;
313 usec = usecs % 10000;
315 usecs = (TIMER_FREQUENCY / 100) * csec
316 + (TIMER_FREQUENCY / 100) * usec / 10000;
319 /* clock isn't initialized yet */
320 for(; usecs > 0; usecs--)
321 for(j = 100; j > 0; j--)
327 mtx_lock_spin(&clock_lock);
332 for(j = 100; j > 0; j--)
335 delta = tick - otick;
343 mtx_unlock_spin(&clock_lock);
348 cpu_startprofclock(void)
350 printf("STARTPROFCLOCK\n");
354 cpu_stopprofclock(void)