sbus.c revision 1.17 1 1.17 pk /* $NetBSD: sbus.c,v 1.17 1997/06/01 22:10:39 pk Exp $ */
2 1.4 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1992, 1993
5 1.1 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * This software was developed by the Computer Systems Engineering group
8 1.1 deraadt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 deraadt * contributed to Berkeley.
10 1.1 deraadt *
11 1.1 deraadt * All advertising materials mentioning features or use of this software
12 1.1 deraadt * must display the following acknowledgement:
13 1.1 deraadt * This product includes software developed by the University of
14 1.1 deraadt * California, Lawrence Berkeley Laboratory.
15 1.1 deraadt *
16 1.1 deraadt * Redistribution and use in source and binary forms, with or without
17 1.1 deraadt * modification, are permitted provided that the following conditions
18 1.1 deraadt * are met:
19 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
20 1.1 deraadt * notice, this list of conditions and the following disclaimer.
21 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
23 1.1 deraadt * documentation and/or other materials provided with the distribution.
24 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
25 1.1 deraadt * must display the following acknowledgement:
26 1.1 deraadt * This product includes software developed by the University of
27 1.1 deraadt * California, Berkeley and its contributors.
28 1.1 deraadt * 4. Neither the name of the University nor the names of its contributors
29 1.1 deraadt * may be used to endorse or promote products derived from this software
30 1.1 deraadt * without specific prior written permission.
31 1.1 deraadt *
32 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 deraadt * SUCH DAMAGE.
43 1.1 deraadt *
44 1.1 deraadt * @(#)sbus.c 8.1 (Berkeley) 6/11/93
45 1.1 deraadt */
46 1.1 deraadt
47 1.1 deraadt /*
48 1.1 deraadt * Sbus stuff.
49 1.1 deraadt */
50 1.1 deraadt
51 1.1 deraadt #include <sys/param.h>
52 1.16 pk #include <sys/malloc.h>
53 1.7 christos #include <sys/systm.h>
54 1.1 deraadt #include <sys/device.h>
55 1.17 pk #include <vm/vm.h>
56 1.1 deraadt
57 1.1 deraadt #include <machine/autoconf.h>
58 1.1 deraadt
59 1.2 deraadt #include <sparc/dev/sbusreg.h>
60 1.2 deraadt #include <sparc/dev/sbusvar.h>
61 1.1 deraadt
62 1.11 cgd int sbus_print __P((void *, const char *));
63 1.8 thorpej void sbusreset __P((int));
64 1.8 thorpej
65 1.1 deraadt /* autoconfiguration driver */
66 1.1 deraadt void sbus_attach __P((struct device *, struct device *, void *));
67 1.14 pk int sbus_match __P((struct device *, struct cfdata *, void *));
68 1.8 thorpej
69 1.8 thorpej struct cfattach sbus_ca = {
70 1.8 thorpej sizeof(struct sbus_softc), sbus_match, sbus_attach
71 1.2 deraadt };
72 1.7 christos
73 1.8 thorpej struct cfdriver sbus_cd = {
74 1.8 thorpej NULL, "sbus", DV_DULL
75 1.8 thorpej };
76 1.1 deraadt
77 1.1 deraadt /*
78 1.1 deraadt * Print the location of some sbus-attached device (called just
79 1.1 deraadt * before attaching that device). If `sbus' is not NULL, the
80 1.1 deraadt * device was found but not configured; print the sbus as well.
81 1.1 deraadt * Return UNCONF (config_find ignores this if the device was configured).
82 1.1 deraadt */
83 1.1 deraadt int
84 1.1 deraadt sbus_print(args, sbus)
85 1.1 deraadt void *args;
86 1.11 cgd const char *sbus;
87 1.1 deraadt {
88 1.2 deraadt register struct confargs *ca = args;
89 1.1 deraadt
90 1.1 deraadt if (sbus)
91 1.13 christos printf("%s at %s", ca->ca_ra.ra_name, sbus);
92 1.13 christos printf(" slot %d offset 0x%x", ca->ca_slot, ca->ca_offset);
93 1.1 deraadt return (UNCONF);
94 1.1 deraadt }
95 1.1 deraadt
96 1.3 deraadt int
97 1.14 pk sbus_match(parent, cf, aux)
98 1.3 deraadt struct device *parent;
99 1.14 pk struct cfdata *cf;
100 1.14 pk void *aux;
101 1.3 deraadt {
102 1.3 deraadt register struct confargs *ca = aux;
103 1.3 deraadt register struct romaux *ra = &ca->ca_ra;
104 1.3 deraadt
105 1.9 pk if (CPU_ISSUN4)
106 1.3 deraadt return (0);
107 1.9 pk
108 1.3 deraadt return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
109 1.3 deraadt }
110 1.3 deraadt
111 1.1 deraadt /*
112 1.1 deraadt * Attach an Sbus.
113 1.1 deraadt */
114 1.1 deraadt void
115 1.1 deraadt sbus_attach(parent, self, aux)
116 1.1 deraadt struct device *parent;
117 1.1 deraadt struct device *self;
118 1.1 deraadt void *aux;
119 1.1 deraadt {
120 1.1 deraadt register struct sbus_softc *sc = (struct sbus_softc *)self;
121 1.2 deraadt struct confargs *ca = aux;
122 1.2 deraadt register struct romaux *ra = &ca->ca_ra;
123 1.9 pk register int node;
124 1.1 deraadt register char *name;
125 1.2 deraadt struct confargs oca;
126 1.16 pk int rlen;
127 1.1 deraadt
128 1.1 deraadt /*
129 1.1 deraadt * XXX there is only one Sbus, for now -- do not know how to
130 1.1 deraadt * address children on others
131 1.1 deraadt */
132 1.1 deraadt if (sc->sc_dev.dv_unit > 0) {
133 1.13 christos printf(" unsupported\n");
134 1.1 deraadt return;
135 1.1 deraadt }
136 1.1 deraadt
137 1.1 deraadt /*
138 1.1 deraadt * Record clock frequency for synchronous SCSI.
139 1.1 deraadt * IS THIS THE CORRECT DEFAULT??
140 1.1 deraadt */
141 1.1 deraadt node = ra->ra_node;
142 1.1 deraadt sc->sc_clockfreq = getpropint(node, "clock-frequency", 25*1000*1000);
143 1.13 christos printf(": clock = %s MHz\n", clockfreq(sc->sc_clockfreq));
144 1.10 abrown
145 1.10 abrown /*
146 1.10 abrown * Get the SBus burst transfer size if burst transfers are supported
147 1.10 abrown */
148 1.10 abrown sc->sc_burst = getpropint(node, "burst-sizes", 0);
149 1.1 deraadt
150 1.1 deraadt if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "sbus") == 0)
151 1.2 deraadt oca.ca_ra.ra_bp = ra->ra_bp + 1;
152 1.1 deraadt else
153 1.2 deraadt oca.ca_ra.ra_bp = NULL;
154 1.1 deraadt
155 1.16 pk rlen = getproplen(node, "ranges");
156 1.16 pk if (rlen > 0) {
157 1.16 pk sc->sc_nrange = rlen / sizeof(struct rom_range);
158 1.16 pk sc->sc_range =
159 1.16 pk (struct rom_range *)malloc(rlen, M_DEVBUF, M_NOWAIT);
160 1.16 pk if (sc->sc_range == 0)
161 1.16 pk panic("sbus: PROM ranges too large: %d", rlen);
162 1.16 pk (void)getprop(node, "ranges", sc->sc_range, rlen);
163 1.16 pk }
164 1.9 pk
165 1.1 deraadt /*
166 1.1 deraadt * Loop through ROM children, fixing any relative addresses
167 1.1 deraadt * and then configuring each device.
168 1.1 deraadt */
169 1.1 deraadt for (node = firstchild(node); node; node = nextsibling(node)) {
170 1.1 deraadt name = getpropstring(node, "name");
171 1.2 deraadt if (!romprop(&oca.ca_ra, name, node))
172 1.1 deraadt continue;
173 1.9 pk
174 1.9 pk sbus_translate(self, &oca);
175 1.9 pk oca.ca_bustype = BUS_SBUS;
176 1.9 pk (void) config_found(&sc->sc_dev, (void *)&oca, sbus_print);
177 1.9 pk }
178 1.9 pk }
179 1.9 pk
180 1.9 pk void
181 1.9 pk sbus_translate(dev, ca)
182 1.9 pk struct device *dev;
183 1.9 pk struct confargs *ca;
184 1.9 pk {
185 1.9 pk struct sbus_softc *sc = (struct sbus_softc *)dev;
186 1.9 pk register int base, slot;
187 1.9 pk register int i;
188 1.9 pk
189 1.9 pk if (sc->sc_nrange == 0) {
190 1.9 pk /* Old-style Sbus configuration */
191 1.9 pk base = (int)ca->ca_ra.ra_paddr;
192 1.1 deraadt if (SBUS_ABS(base)) {
193 1.9 pk ca->ca_slot = SBUS_ABS_TO_SLOT(base);
194 1.9 pk ca->ca_offset = SBUS_ABS_TO_OFFSET(base);
195 1.1 deraadt } else {
196 1.17 pk if (!CPU_ISSUN4C)
197 1.17 pk panic("relative sbus addressing not supported");
198 1.9 pk ca->ca_slot = slot = ca->ca_ra.ra_iospace;
199 1.9 pk ca->ca_offset = base;
200 1.17 pk ca->ca_ra.ra_paddr = (void *)SBUS_ADDR(slot, base);
201 1.17 pk ca->ca_ra.ra_iospace = PMAP_OBIO;
202 1.17 pk
203 1.6 pk /* Fix any remaining register banks */
204 1.9 pk for (i = 1; i < ca->ca_ra.ra_nreg; i++) {
205 1.9 pk base = (int)ca->ca_ra.ra_reg[i].rr_paddr;
206 1.9 pk ca->ca_ra.ra_reg[i].rr_paddr =
207 1.6 pk (void *)SBUS_ADDR(slot, base);
208 1.17 pk ca->ca_ra.ra_reg[i].rr_iospace = PMAP_OBIO;
209 1.6 pk }
210 1.1 deraadt }
211 1.9 pk
212 1.9 pk } else {
213 1.9 pk
214 1.9 pk ca->ca_slot = ca->ca_ra.ra_iospace;
215 1.9 pk ca->ca_offset = (int)ca->ca_ra.ra_paddr;
216 1.9 pk
217 1.9 pk /* Translate into parent address spaces */
218 1.9 pk for (i = 0; i < ca->ca_ra.ra_nreg; i++) {
219 1.9 pk int j, cspace = ca->ca_ra.ra_reg[i].rr_iospace;
220 1.9 pk
221 1.9 pk for (j = 0; j < sc->sc_nrange; j++) {
222 1.9 pk if (sc->sc_range[j].cspace == cspace) {
223 1.9 pk (int)ca->ca_ra.ra_reg[i].rr_paddr +=
224 1.9 pk sc->sc_range[j].poffset;
225 1.9 pk (int)ca->ca_ra.ra_reg[i].rr_iospace =
226 1.9 pk sc->sc_range[j].pspace;
227 1.9 pk break;
228 1.9 pk }
229 1.9 pk }
230 1.9 pk }
231 1.1 deraadt }
232 1.1 deraadt }
233 1.1 deraadt
234 1.1 deraadt /*
235 1.1 deraadt * Each attached device calls sbus_establish after it initializes
236 1.1 deraadt * its sbusdev portion.
237 1.1 deraadt */
238 1.1 deraadt void
239 1.1 deraadt sbus_establish(sd, dev)
240 1.1 deraadt register struct sbusdev *sd;
241 1.1 deraadt register struct device *dev;
242 1.1 deraadt {
243 1.9 pk register struct sbus_softc *sc;
244 1.9 pk register struct device *curdev;
245 1.9 pk
246 1.9 pk /*
247 1.9 pk * We have to look for the sbus by name, since it is not necessarily
248 1.9 pk * our immediate parent (i.e. sun4m /iommu/sbus/espdma/esp)
249 1.9 pk * We don't just use the device structure of the above-attached
250 1.9 pk * sbus, since we might (in the future) support multiple sbus's.
251 1.9 pk */
252 1.9 pk for (curdev = dev->dv_parent; ; curdev = curdev->dv_parent) {
253 1.9 pk if (!curdev || !curdev->dv_xname)
254 1.15 pk panic("sbus_establish: can't find sbus parent for %s",
255 1.15 pk sd->sd_dev->dv_xname
256 1.15 pk ? sd->sd_dev->dv_xname
257 1.15 pk : "<unknown>" );
258 1.9 pk
259 1.9 pk if (strncmp(curdev->dv_xname, "sbus", 4) == 0)
260 1.15 pk break;
261 1.9 pk }
262 1.9 pk sc = (struct sbus_softc *) curdev;
263 1.1 deraadt
264 1.1 deraadt sd->sd_dev = dev;
265 1.1 deraadt sd->sd_bchain = sc->sc_sbdev;
266 1.1 deraadt sc->sc_sbdev = sd;
267 1.1 deraadt }
268 1.1 deraadt
269 1.1 deraadt /*
270 1.1 deraadt * Reset the given sbus. (???)
271 1.1 deraadt */
272 1.1 deraadt void
273 1.1 deraadt sbusreset(sbus)
274 1.1 deraadt int sbus;
275 1.1 deraadt {
276 1.1 deraadt register struct sbusdev *sd;
277 1.8 thorpej struct sbus_softc *sc = sbus_cd.cd_devs[sbus];
278 1.1 deraadt struct device *dev;
279 1.1 deraadt
280 1.13 christos printf("reset %s:", sc->sc_dev.dv_xname);
281 1.1 deraadt for (sd = sc->sc_sbdev; sd != NULL; sd = sd->sd_bchain) {
282 1.1 deraadt if (sd->sd_reset) {
283 1.1 deraadt dev = sd->sd_dev;
284 1.1 deraadt (*sd->sd_reset)(dev);
285 1.13 christos printf(" %s", dev->dv_xname);
286 1.1 deraadt }
287 1.1 deraadt }
288 1.1 deraadt }
289