xref: /netbsd-src/sys/arch/sun3/sun3/autoconf.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: autoconf.c,v 1.55 2001/04/25 17:53:25 bouyer Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass and Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Setup the system to run on the current machine.
41  *
42  * Configure() is called at boot time.  Available devices are
43  * determined (from possibilities mentioned in ioconf.c), and
44  * the drivers are initialized.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/conf.h>
50 #include <sys/device.h>
51 
52 #include <dev/scsipi/scsi_all.h>
53 #include <dev/scsipi/scsipi_all.h>
54 #include <dev/scsipi/scsiconf.h>
55 
56 #include <machine/autoconf.h>
57 #include <machine/mon.h>
58 
59 #include <sun3/sun3/machdep.h>
60 
61 /* Make sure the config is OK. */
62 #if (defined(_SUN3_) + defined(_SUN3X_)) != 1
63 #error "Must have exactly one of: SUN3 and SUN3X options"
64 #endif
65 
66 /*
67  * Do general device autoconfiguration,
68  * then choose root device (etc.)
69  * Called by machdep.c: cpu_startup()
70  */
71 void
72 cpu_configure()
73 {
74 
75 	/* General device autoconfiguration. */
76 	if (config_rootfound("mainbus", NULL) == NULL)
77 		panic("configure: mainbus not found");
78 
79 	/*
80 	 * Now that device autoconfiguration is finished,
81 	 * we can safely enable interrupts.
82 	 */
83 	printf("enabling interrupts\n");
84 	(void)spl0();
85 }
86 
87 /*
88  * bus_scan:
89  * This function is passed to config_search() by the attach function
90  * for each of the "bus" drivers (obctl, obio, obmem, vme, ...).
91  * The purpose of this function is to copy the "locators" into our
92  * confargs structure, so child drivers may use the confargs both
93  * as match parameters and as temporary storage for the defaulted
94  * locator values determined in the child_match and preserved for
95  * the child_attach function.  If the bus attach functions just
96  * used config_found, then we would not have an opportunity to
97  * setup the confargs for each child match and attach call.
98  */
99 int bus_scan(parent, cf, aux)
100 	struct device *parent;
101 	struct cfdata *cf;
102 	void *aux;
103 {
104 	struct confargs *ca = aux;
105 	cfmatch_t mf;
106 
107 #ifdef	DIAGNOSTIC
108 	if (cf->cf_fstate == FSTATE_STAR)
109 		panic("bus_scan: FSTATE_STAR");
110 #endif
111 
112 	/*
113 	 * Copy the locators into our confargs.
114 	 * Our parent set ca->ca_bustype already.
115 	 */
116 	ca->ca_paddr  = cf->cf_paddr;
117 	ca->ca_intpri = cf->cf_intpri;
118 	ca->ca_intvec = cf->cf_intvec;
119 
120 	/*
121 	 * Note that this allows the match function to save
122 	 * defaulted locators in the confargs that will be
123 	 * preserved for the related attach call.
124 	 * XXX - This is a hack...
125 	 */
126 	mf = cf->cf_attach->ca_match;
127 	if ((*mf)(parent, cf, ca) > 0) {
128 		config_attach(parent, cf, ca, bus_print);
129 	}
130 	return (0);
131 }
132 
133 /*
134  * bus_print:
135  * Just print out the final (non-default) locators.
136  * The parent name is non-NULL when there was no match
137  * found by config_found().
138  */
139 int
140 bus_print(args, name)
141 	void *args;
142 	const char *name;
143 {
144 	struct confargs *ca = args;
145 
146 	if (name)
147 		printf("%s:", name);
148 
149 	if (ca->ca_paddr != -1)
150 		printf(" addr 0x%x", ca->ca_paddr);
151 	if (ca->ca_intpri != -1)
152 		printf(" ipl %d", ca->ca_intpri);
153 	if (ca->ca_intvec != -1)
154 		printf(" vect 0x%x", ca->ca_intvec);
155 
156 	return(UNCONF);
157 }
158 
159 /****************************************************************/
160 
161 /* This takes the args: name, ctlr, unit */
162 typedef struct device * (*findfunc_t) __P((char *, int, int));
163 
164 static struct device * find_dev_byname __P((char *));
165 static struct device * net_find  __P((char *, int, int));
166 static struct device * scsi_find __P((char *, int, int));
167 static struct device * xx_find   __P((char *, int, int));
168 
169 struct prom_n2f {
170 	const char name[4];
171 	findfunc_t func;
172 };
173 static struct prom_n2f prom_dev_table[] = {
174 	{ "ie",		net_find },
175 	{ "le",		net_find },
176 	{ "sd",		scsi_find },
177 	{ "xy",		xx_find },
178 	{ "xd",		xx_find },
179 	{ "",		0 },
180 };
181 
182 /*
183  * Choose root and swap devices.
184  */
185 void
186 cpu_rootconf()
187 {
188 	struct bootparam *bp;
189 	struct prom_n2f *nf;
190 	struct device *boot_device;
191 	int boot_partition;
192 	char *devname;
193 	findfunc_t find;
194 	char promname[4];
195 	char partname[4];
196 
197 	/* PROM boot parameters. */
198 	bp = *romVectorPtr->bootParam;
199 
200 	/*
201 	 * Copy PROM boot device name (two letters)
202 	 * to a normal, null terminated string.
203 	 * (No terminating null in bp->devName)
204 	 */
205 	promname[0] = bp->devName[0];
206 	promname[1] = bp->devName[1];
207 	promname[2] = '\0';
208 
209 	/* Default to "unknown" */
210 	boot_device = NULL;
211 	boot_partition = 0;
212 	devname = "<unknown>";
213 	partname[0] = '\0';
214 	find = NULL;
215 
216 	/* Do we know anything about the PROM boot device? */
217 	for (nf = prom_dev_table; nf->func; nf++)
218 		if (!strcmp(nf->name, promname)) {
219 			find = nf->func;
220 			break;
221 		}
222 	if (find)
223 		boot_device = (*find)(promname, bp->ctlrNum, bp->unitNum);
224 	if (boot_device) {
225 		devname = boot_device->dv_xname;
226 		if (boot_device->dv_class == DV_DISK) {
227 			boot_partition = bp->partNum & 7;
228 			partname[0] = 'a' + boot_partition;
229 			partname[1] = '\0';
230 		}
231 	}
232 
233 	printf("boot device: %s%s\n", devname, partname);
234 	setroot(boot_device, boot_partition);
235 }
236 
237 /*
238  * Functions to find devices using PROM boot parameters.
239  */
240 
241 /*
242  * Network device:  Just use controller number.
243  */
244 static struct device *
245 net_find(name, ctlr, unit)
246 	char *name;
247 	int ctlr, unit;
248 {
249 	char tname[16];
250 
251 	sprintf(tname, "%s%d", name, ctlr);
252 	return (find_dev_byname(tname));
253 }
254 
255 /*
256  * SCSI device:  The controller number corresponds to the
257  * scsibus number, and the unit number is (targ*8 + LUN).
258  */
259 static struct device *
260 scsi_find(name, ctlr, unit)
261 	char *name;
262 	int ctlr, unit;
263 {
264 	struct device *scsibus;
265 	struct scsibus_softc *sbsc;
266 	struct scsipi_periph *periph;
267 	int target, lun;
268 	char tname[16];
269 
270 	sprintf(tname, "scsibus%d", ctlr);
271 	scsibus = find_dev_byname(tname);
272 	if (scsibus == NULL)
273 		return (NULL);
274 
275 	/* Compute SCSI target/LUN from PROM unit. */
276 	target = (unit >> 3) & 7;
277 	lun = unit & 7;
278 
279 	/* Find the device at this target/LUN */
280 	sbsc = (struct scsibus_softc *)scsibus;
281 	periph = sbsc->sc_channel->chan_periphs[target][lun];
282 	if (periph == NULL)
283 		return (NULL);
284 
285 	return (periph->periph_dev);
286 }
287 
288 /*
289  * Xylogics SMD disk: (xy, xd)
290  * Assume wired-in unit numbers for now...
291  */
292 static struct device *
293 xx_find(name, ctlr, unit)
294 	char *name;
295 	int ctlr, unit;
296 {
297 	int diskunit;
298 	char tname[16];
299 
300 	diskunit = (ctlr * 2) + unit;
301 	sprintf(tname, "%s%d", name, diskunit);
302 	return (find_dev_byname(tname));
303 }
304 
305 /*
306  * Given a device name, find its struct device
307  * XXX - Move this to some common file?
308  */
309 static struct device *
310 find_dev_byname(name)
311 	char *name;
312 {
313 	struct device *dv;
314 
315 	for (dv = alldevs.tqh_first; dv != NULL;
316 	    dv = dv->dv_list.tqe_next) {
317 		if (!strcmp(dv->dv_xname, name)) {
318 			return(dv);
319 		}
320 	}
321 	return (NULL);
322 }
323 
324 /*
325  * Misc. helpers used by driver match/attach functions.
326  */
327 
328 /*
329  * Read addr with size len (1,2,4) into val.
330  * If this generates a bus error, return -1
331  *
332  *	Create a temporary mapping,
333  *	Try the access using peek_*
334  *	Clean up temp. mapping
335  */
336 int
337 bus_peek(bustype, pa, sz)
338 	int bustype, pa, sz;
339 {
340 	caddr_t va;
341 	int rv;
342 
343 	va = bus_tmapin(bustype, pa);
344 	switch (sz) {
345 	case 1:
346 		rv = peek_byte(va);
347 		break;
348 	case 2:
349 		rv = peek_word(va);
350 		break;
351 	case 4:
352 		rv = peek_long(va);
353 		break;
354 	default:
355 		printf(" bus_peek: invalid size=%d\n", sz);
356 		rv = -1;
357 	}
358 	bus_tmapout(va);
359 
360 	return (rv);
361 }
362 
363 /* from hp300: badbaddr() */
364 int
365 peek_byte(addr)
366 	register caddr_t addr;
367 {
368 	label_t 	faultbuf;
369 	register int x;
370 
371 	nofault = &faultbuf;
372 	if (setjmp(&faultbuf))
373 		x = -1;
374 	else
375 		x = *(volatile u_char *)addr;
376 
377 	nofault = NULL;
378 	return(x);
379 }
380 
381 int
382 peek_word(addr)
383 	register caddr_t addr;
384 {
385 	label_t		faultbuf;
386 	register int x;
387 
388 	nofault = &faultbuf;
389 	if (setjmp(&faultbuf))
390 		x = -1;
391 	else
392 		x = *(volatile u_short *)addr;
393 
394 	nofault = NULL;
395 	return(x);
396 }
397 
398 int
399 peek_long(addr)
400 	register caddr_t addr;
401 {
402 	label_t		faultbuf;
403 	register int x;
404 
405 	nofault = &faultbuf;
406 	if (setjmp(&faultbuf))
407 		x = -1;
408 	else {
409 		x = *(volatile int *)addr;
410 		if (x == -1) {
411 			printf("peek_long: uh-oh, actually read -1!\n");
412 			x &= 0x7FFFffff; /* XXX */
413 		}
414 	}
415 
416 	nofault = NULL;
417 	return(x);
418 }
419