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