xref: /netbsd/sys/arch/sun3/sun3/autoconf.c (revision c4a72b64)
1 /*	$NetBSD: autoconf.c,v 1.59 2002/09/27 03:18:07 thorpej 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 "scsibus.h"
53 
54 #if NSCSIBUS > 0
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58 #endif
59 
60 #include <machine/autoconf.h>
61 #include <machine/mon.h>
62 
63 #include <sun3/sun3/machdep.h>
64 
65 
66 /* Make sure the config is OK. */
67 #if (defined(_SUN3_) + defined(_SUN3X_)) != 1
68 #error "Must have exactly one of: SUN3 and SUN3X options"
69 #endif
70 
71 /*
72  * Do general device autoconfiguration,
73  * then choose root device (etc.)
74  * Called by machdep.c: cpu_startup()
75  */
76 void
77 cpu_configure()
78 {
79 
80 	/* General device autoconfiguration. */
81 	if (config_rootfound("mainbus", NULL) == NULL)
82 		panic("configure: mainbus not found");
83 
84 	/*
85 	 * Now that device autoconfiguration is finished,
86 	 * we can safely enable interrupts.
87 	 */
88 	printf("enabling interrupts\n");
89 	(void)spl0();
90 }
91 
92 /*
93  * bus_scan:
94  * This function is passed to config_search() by the attach function
95  * for each of the "bus" drivers (obctl, obio, obmem, vme, ...).
96  * The purpose of this function is to copy the "locators" into our
97  * confargs structure, so child drivers may use the confargs both
98  * as match parameters and as temporary storage for the defaulted
99  * locator values determined in the child_match and preserved for
100  * the child_attach function.  If the bus attach functions just
101  * used config_found, then we would not have an opportunity to
102  * setup the confargs for each child match and attach call.
103  */
104 int bus_scan(parent, cf, aux)
105 	struct device *parent;
106 	struct cfdata *cf;
107 	void *aux;
108 {
109 	struct confargs *ca = aux;
110 
111 #ifdef	DIAGNOSTIC
112 	if (cf->cf_fstate == FSTATE_STAR)
113 		panic("bus_scan: FSTATE_STAR");
114 #endif
115 
116 	/*
117 	 * Copy the locators into our confargs.
118 	 * Our parent set ca->ca_bustype already.
119 	 */
120 	ca->ca_paddr  = cf->cf_paddr;
121 	ca->ca_intpri = cf->cf_intpri;
122 	ca->ca_intvec = cf->cf_intvec;
123 
124 	/*
125 	 * Note that this allows the match function to save
126 	 * defaulted locators in the confargs that will be
127 	 * preserved for the related attach call.
128 	 * XXX - This is a hack...
129 	 */
130 	if (config_match(parent, cf, ca) > 0) {
131 		config_attach(parent, cf, ca, bus_print);
132 	}
133 	return (0);
134 }
135 
136 /*
137  * bus_print:
138  * Just print out the final (non-default) locators.
139  * The parent name is non-NULL when there was no match
140  * found by config_found().
141  */
142 int
143 bus_print(args, name)
144 	void *args;
145 	const char *name;
146 {
147 	struct confargs *ca = args;
148 
149 	if (name)
150 		printf("%s:", name);
151 
152 	if (ca->ca_paddr != -1)
153 		printf(" addr 0x%x", ca->ca_paddr);
154 	if (ca->ca_intpri != -1)
155 		printf(" ipl %d", ca->ca_intpri);
156 	if (ca->ca_intvec != -1)
157 		printf(" vect 0x%x", ca->ca_intvec);
158 
159 	return(UNCONF);
160 }
161 
162 /****************************************************************/
163 
164 /* This takes the args: name, ctlr, unit */
165 typedef struct device * (*findfunc_t) __P((char *, int, int));
166 
167 static struct device * find_dev_byname __P((char *));
168 static struct device * net_find  __P((char *, int, int));
169 #if NSCSIBUS > 0
170 static struct device * scsi_find __P((char *, int, int));
171 #endif
172 static struct device * xx_find   __P((char *, int, int));
173 
174 struct prom_n2f {
175 	const char name[4];
176 	findfunc_t func;
177 };
178 static struct prom_n2f prom_dev_table[] = {
179 	{ "ie",		net_find },
180 	{ "le",		net_find },
181 #if NSCSIBUS > 0
182 	{ "sd",		scsi_find },
183 #endif
184 	{ "xy",		xx_find },
185 	{ "xd",		xx_find },
186 	{ "",		0 },
187 };
188 
189 /*
190  * Choose root and swap devices.
191  */
192 void
193 cpu_rootconf()
194 {
195 	struct bootparam *bp;
196 	struct prom_n2f *nf;
197 	struct device *boot_device;
198 	int boot_partition;
199 	char *devname;
200 	findfunc_t find;
201 	char promname[4];
202 	char partname[4];
203 
204 	/* PROM boot parameters. */
205 	bp = *romVectorPtr->bootParam;
206 
207 	/*
208 	 * Copy PROM boot device name (two letters)
209 	 * to a normal, null terminated string.
210 	 * (No terminating null in bp->devName)
211 	 */
212 	promname[0] = bp->devName[0];
213 	promname[1] = bp->devName[1];
214 	promname[2] = '\0';
215 
216 	/* Default to "unknown" */
217 	boot_device = NULL;
218 	boot_partition = 0;
219 	devname = "<unknown>";
220 	partname[0] = '\0';
221 	find = NULL;
222 
223 	/* Do we know anything about the PROM boot device? */
224 	for (nf = prom_dev_table; nf->func; nf++)
225 		if (!strcmp(nf->name, promname)) {
226 			find = nf->func;
227 			break;
228 		}
229 	if (find)
230 		boot_device = (*find)(promname, bp->ctlrNum, bp->unitNum);
231 	if (boot_device) {
232 		devname = boot_device->dv_xname;
233 		if (boot_device->dv_class == DV_DISK) {
234 			boot_partition = bp->partNum & 7;
235 			partname[0] = 'a' + boot_partition;
236 			partname[1] = '\0';
237 		}
238 	}
239 
240 	printf("boot device: %s%s\n", devname, partname);
241 	setroot(boot_device, boot_partition);
242 }
243 
244 /*
245  * Functions to find devices using PROM boot parameters.
246  */
247 
248 /*
249  * Network device:  Just use controller number.
250  */
251 static struct device *
252 net_find(name, ctlr, unit)
253 	char *name;
254 	int ctlr, unit;
255 {
256 	char tname[16];
257 
258 	sprintf(tname, "%s%d", name, ctlr);
259 	return (find_dev_byname(tname));
260 }
261 
262 #if NSCSIBUS > 0
263 /*
264  * SCSI device:  The controller number corresponds to the
265  * scsibus number, and the unit number is (targ*8 + LUN).
266  */
267 static struct device *
268 scsi_find(name, ctlr, unit)
269 	char *name;
270 	int ctlr, unit;
271 {
272 	struct device *scsibus;
273 	struct scsibus_softc *sbsc;
274 	struct scsipi_periph *periph;
275 	int target, lun;
276 	char tname[16];
277 
278 	sprintf(tname, "scsibus%d", ctlr);
279 	scsibus = find_dev_byname(tname);
280 	if (scsibus == NULL)
281 		return (NULL);
282 
283 	/* Compute SCSI target/LUN from PROM unit. */
284 	target = (unit >> 3) & 7;
285 	lun = unit & 7;
286 
287 	/* Find the device at this target/LUN */
288 	sbsc = (struct scsibus_softc *)scsibus;
289 	periph = scsipi_lookup_periph(sbsc->sc_channel, target, lun);
290 	if (periph == NULL)
291 		return (NULL);
292 
293 	return (periph->periph_dev);
294 }
295 #endif	/* NSCSIBUS > 0 */
296 
297 /*
298  * Xylogics SMD disk: (xy, xd)
299  * Assume wired-in unit numbers for now...
300  */
301 static struct device *
302 xx_find(name, ctlr, unit)
303 	char *name;
304 	int ctlr, unit;
305 {
306 	int diskunit;
307 	char tname[16];
308 
309 	diskunit = (ctlr * 2) + unit;
310 	sprintf(tname, "%s%d", name, diskunit);
311 	return (find_dev_byname(tname));
312 }
313 
314 /*
315  * Given a device name, find its struct device
316  * XXX - Move this to some common file?
317  */
318 static struct device *
319 find_dev_byname(name)
320 	char *name;
321 {
322 	struct device *dv;
323 
324 	for (dv = alldevs.tqh_first; dv != NULL;
325 	    dv = dv->dv_list.tqe_next) {
326 		if (!strcmp(dv->dv_xname, name)) {
327 			return(dv);
328 		}
329 	}
330 	return (NULL);
331 }
332 
333 /*
334  * Misc. helpers used by driver match/attach functions.
335  */
336 
337 /*
338  * Read addr with size len (1,2,4) into val.
339  * If this generates a bus error, return -1
340  *
341  *	Create a temporary mapping,
342  *	Try the access using peek_*
343  *	Clean up temp. mapping
344  */
345 int
346 bus_peek(bustype, pa, sz)
347 	int bustype, pa, sz;
348 {
349 	caddr_t va;
350 	int rv;
351 
352 	va = bus_tmapin(bustype, pa);
353 	switch (sz) {
354 	case 1:
355 		rv = peek_byte(va);
356 		break;
357 	case 2:
358 		rv = peek_word(va);
359 		break;
360 	case 4:
361 		rv = peek_long(va);
362 		break;
363 	default:
364 		printf(" bus_peek: invalid size=%d\n", sz);
365 		rv = -1;
366 	}
367 	bus_tmapout(va);
368 
369 	return (rv);
370 }
371 
372 /* from hp300: badbaddr() */
373 int
374 peek_byte(addr)
375 	caddr_t addr;
376 {
377 	label_t faultbuf;
378 	int x;
379 
380 	nofault = &faultbuf;
381 	if (setjmp(&faultbuf))
382 		x = -1;
383 	else
384 		x = *(volatile u_char *)addr;
385 
386 	nofault = NULL;
387 	return(x);
388 }
389 
390 int
391 peek_word(addr)
392 	caddr_t addr;
393 {
394 	label_t faultbuf;
395 	int x;
396 
397 	nofault = &faultbuf;
398 	if (setjmp(&faultbuf))
399 		x = -1;
400 	else
401 		x = *(volatile u_short *)addr;
402 
403 	nofault = NULL;
404 	return(x);
405 }
406 
407 int
408 peek_long(addr)
409 	caddr_t addr;
410 {
411 	label_t faultbuf;
412 	int x;
413 
414 	nofault = &faultbuf;
415 	if (setjmp(&faultbuf))
416 		x = -1;
417 	else {
418 		x = *(volatile int *)addr;
419 		if (x == -1) {
420 			printf("peek_long: uh-oh, actually read -1!\n");
421 			x &= 0x7FFFffff; /* XXX */
422 		}
423 	}
424 
425 	nofault = NULL;
426 	return(x);
427 }
428