rambo.c revision 1.3.2.2 1 1.3.2.2 bouyer /* $NetBSD: rambo.c,v 1.3.2.2 2000/11/20 20:14:11 bouyer Exp $ */
2 1.3.2.2 bouyer
3 1.3.2.2 bouyer /*
4 1.3.2.2 bouyer * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.3.2.2 bouyer * All rights reserved.
6 1.3.2.2 bouyer *
7 1.3.2.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 bouyer * by Wayne Knowles
9 1.3.2.2 bouyer *
10 1.3.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.3.2.2 bouyer * are met:
13 1.3.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.3.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 bouyer * must display the following acknowledgement:
20 1.3.2.2 bouyer * This product includes software developed by the NetBSD
21 1.3.2.2 bouyer * Foundation, Inc. and its contributors.
22 1.3.2.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 bouyer * contributors may be used to endorse or promote products derived
24 1.3.2.2 bouyer * from this software without specific prior written permission.
25 1.3.2.2 bouyer *
26 1.3.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 bouyer */
38 1.3.2.2 bouyer
39 1.3.2.2 bouyer #include <sys/param.h>
40 1.3.2.2 bouyer #include <sys/kernel.h>
41 1.3.2.2 bouyer #include <sys/device.h>
42 1.3.2.2 bouyer #include <sys/systm.h>
43 1.3.2.2 bouyer
44 1.3.2.2 bouyer #include <machine/cpu.h>
45 1.3.2.2 bouyer #include <machine/mainboard.h>
46 1.3.2.2 bouyer #include <machine/autoconf.h>
47 1.3.2.2 bouyer #include <machine/sysconf.h>
48 1.3.2.2 bouyer #include <machine/bus.h>
49 1.3.2.2 bouyer
50 1.3.2.2 bouyer #include <mipsco/obio/rambo.h>
51 1.3.2.2 bouyer
52 1.3.2.2 bouyer /*
53 1.3.2.2 bouyer * Timer & Interrupt manipulation routines for the Rambo Custom ASIC
54 1.3.2.2 bouyer */
55 1.3.2.2 bouyer
56 1.3.2.2 bouyer static int rambo_match __P((struct device *, struct cfdata *, void *));
57 1.3.2.2 bouyer static void rambo_attach __P((struct device *, struct device *, void *));
58 1.3.2.2 bouyer static unsigned rambo_clkread __P((void));
59 1.3.2.2 bouyer void rambo_clkintr __P((struct clockframe *));
60 1.3.2.2 bouyer
61 1.3.2.2 bouyer struct rambo_softc {
62 1.3.2.2 bouyer struct device dev;
63 1.3.2.2 bouyer struct evcnt sc_intrcnt;
64 1.3.2.2 bouyer bus_space_tag_t sc_bst;
65 1.3.2.2 bouyer bus_space_handle_t sc_bsh;
66 1.3.2.2 bouyer u_int32_t sc_tclast;
67 1.3.2.2 bouyer u_int32_t sc_hzticks;
68 1.3.2.2 bouyer };
69 1.3.2.2 bouyer
70 1.3.2.2 bouyer static struct rambo_softc *rambo;
71 1.3.2.2 bouyer
72 1.3.2.2 bouyer struct cfattach rambo_ca = {
73 1.3.2.2 bouyer sizeof(struct rambo_softc), rambo_match, rambo_attach
74 1.3.2.2 bouyer };
75 1.3.2.2 bouyer
76 1.3.2.2 bouyer static int
77 1.3.2.2 bouyer rambo_match(parent, cf, aux)
78 1.3.2.2 bouyer struct device *parent;
79 1.3.2.2 bouyer struct cfdata *cf;
80 1.3.2.2 bouyer void *aux;
81 1.3.2.2 bouyer {
82 1.3.2.2 bouyer return 1;
83 1.3.2.2 bouyer }
84 1.3.2.2 bouyer
85 1.3.2.2 bouyer static void
86 1.3.2.2 bouyer rambo_attach(parent, self, aux)
87 1.3.2.2 bouyer struct device *parent, *self;
88 1.3.2.2 bouyer void *aux;
89 1.3.2.2 bouyer {
90 1.3.2.2 bouyer struct confargs *ca = aux;
91 1.3.2.2 bouyer struct rambo_softc *sc = (void *)self;
92 1.3.2.2 bouyer
93 1.3.2.2 bouyer sc->sc_bst = ca->ca_bustag;
94 1.3.2.2 bouyer
95 1.3.2.2 bouyer if (bus_space_map(ca->ca_bustag, ca->ca_addr, 256,
96 1.3.2.2 bouyer BUS_SPACE_MAP_LINEAR,
97 1.3.2.2 bouyer &sc->sc_bsh) != 0) {
98 1.3.2.2 bouyer printf(": cannot map registers\n");
99 1.3.2.2 bouyer return;
100 1.3.2.2 bouyer }
101 1.3.2.2 bouyer evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
102 1.3.2.2 bouyer "timer", "intr");
103 1.3.2.2 bouyer
104 1.3.2.2 bouyer /* Setup RAMBO Timer to generate timer interrupts */
105 1.3.2.2 bouyer sc->sc_hzticks = HZ_TO_TICKS(hz);
106 1.3.2.2 bouyer
107 1.3.2.2 bouyer sc->sc_tclast = 0;
108 1.3.2.2 bouyer bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TCOUNT, 0);
109 1.3.2.2 bouyer bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TBREAK, sc->sc_hzticks);
110 1.3.2.2 bouyer
111 1.3.2.2 bouyer bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_CTLREG,
112 1.3.2.2 bouyer RB_PARITY_EN | RB_BUZZOFF | RB_CLR_IOERR);
113 1.3.2.2 bouyer
114 1.3.2.2 bouyer printf(": parity enabled\n");
115 1.3.2.2 bouyer rambo = sc;
116 1.3.2.2 bouyer platform.clkread = rambo_clkread;
117 1.3.2.2 bouyer }
118 1.3.2.2 bouyer
119 1.3.2.2 bouyer void
120 1.3.2.2 bouyer rambo_clkintr(cf)
121 1.3.2.2 bouyer struct clockframe *cf;
122 1.3.2.2 bouyer {
123 1.3.2.2 bouyer register u_int32_t tbreak, tcount;
124 1.3.2.2 bouyer register int delta;
125 1.3.2.2 bouyer
126 1.3.2.2 bouyer rambo->sc_intrcnt.ev_count++;
127 1.3.2.2 bouyer tbreak = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK);
128 1.3.2.2 bouyer tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT);
129 1.3.2.2 bouyer delta = tcount - tbreak;
130 1.3.2.2 bouyer
131 1.3.2.2 bouyer if (delta > (rambo->sc_hzticks>>1)) {
132 1.3.2.2 bouyer /*
133 1.3.2.2 bouyer * Either tcount may overtake the updated tbreak value
134 1.3.2.2 bouyer * or we have missed several interrupt's
135 1.3.2.2 bouyer */
136 1.3.2.2 bouyer int cycles = 10 * hz;
137 1.3.2.2 bouyer while (cycles && tbreak < tcount) {
138 1.3.2.2 bouyer hardclock(cf);
139 1.3.2.2 bouyer rambo->sc_tclast = tbreak;
140 1.3.2.2 bouyer tbreak += rambo->sc_hzticks;
141 1.3.2.2 bouyer cycles--;
142 1.3.2.2 bouyer }
143 1.3.2.2 bouyer if (cycles == 0) { /* catchup failed - assume we are in sync */
144 1.3.2.2 bouyer tcount = bus_space_read_4(rambo->sc_bst,
145 1.3.2.2 bouyer rambo->sc_bsh, RB_TCOUNT);
146 1.3.2.2 bouyer rambo->sc_tclast = tbreak = tcount;
147 1.3.2.2 bouyer }
148 1.3.2.2 bouyer } else {
149 1.3.2.2 bouyer hardclock(cf);
150 1.3.2.2 bouyer rambo->sc_tclast = tbreak;
151 1.3.2.2 bouyer }
152 1.3.2.2 bouyer
153 1.3.2.2 bouyer tbreak += rambo->sc_hzticks;
154 1.3.2.2 bouyer
155 1.3.2.2 bouyer bus_space_write_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK, tbreak);
156 1.3.2.2 bouyer }
157 1.3.2.2 bouyer
158 1.3.2.2 bouyer /*
159 1.3.2.2 bouyer * Calculate the number of microseconds since the last clock tick
160 1.3.2.2 bouyer */
161 1.3.2.2 bouyer static unsigned
162 1.3.2.2 bouyer rambo_clkread()
163 1.3.2.2 bouyer {
164 1.3.2.2 bouyer register u_int32_t tcount;
165 1.3.2.2 bouyer
166 1.3.2.2 bouyer tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT);
167 1.3.2.2 bouyer return TICKS_TO_USECS(tcount - rambo->sc_tclast);
168 1.3.2.2 bouyer }
169