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