autoconf.c revision 1.53
1/*	$NetBSD: autoconf.c,v 1.53 1997/07/22 15:03:22 is Exp $	*/
2
3/*
4 * Copyright (c) 1994 Christian E. Hopps
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *      This product includes software developed by Christian E. Hopps.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/reboot.h>
35#include <sys/conf.h>
36#include <sys/buf.h>
37#include <sys/device.h>
38#include <sys/disklabel.h>
39#include <sys/disk.h>
40#include <machine/cpu.h>
41#include <amiga/amiga/cfdev.h>
42#include <amiga/amiga/device.h>
43#include <amiga/amiga/custom.h>
44
45void findroot __P((struct device **, int *));
46void mbattach __P((struct device *, struct device *, void *));
47int mbprint __P((void *, const char *));
48int mbmatch __P((struct device *, struct cfdata *, void *));
49
50int cold;	/* 1 if still booting */
51#include <sys/kernel.h>
52
53struct devnametobdevmaj amiga_nam2blk[] = {
54	{ "fd",		2 },
55	{ "sd",		4 },
56	{ "cd",		7 },
57#ifdef notyet
58	{ "md",		XXX },
59#endif
60	{ NULL,		0 },
61};
62u_long boot_partition;
63
64/*
65 * called at boot time, configure all devices on system
66 */
67void
68configure()
69{
70	int s;
71
72	/*
73	 * this is the real thing baby (i.e. not console init)
74	 */
75	amiga_realconfig = 1;
76#ifdef DRACO
77	if (is_draco()) {
78		*draco_intena &= ~DRIRQ_GLOBAL;
79	} else
80#endif
81	custom.intena = INTF_INTEN;
82	s = splhigh();
83
84	if (config_rootfound("mainbus", "mainbus") == NULL)
85		panic("no mainbus found");
86
87#ifdef DEBUG_KERNEL_START
88	printf("survived autoconf, going to enable interrupts\n");
89#endif
90
91#ifdef DRACO
92	if (is_draco()) {
93		*draco_intena |= DRIRQ_GLOBAL;
94		/* softints always enabled */
95	} else
96#endif
97	{
98		custom.intena = INTF_SETCLR | INTF_INTEN;
99
100		/* also enable hardware aided software interrupts */
101		custom.intena = INTF_SETCLR | INTF_SOFTINT;
102	}
103	splx(s);
104#ifdef DEBUG_KERNEL_START
105	printf("survived interrupt enable\n");
106#endif
107	cold = 0;
108}
109
110void
111cpu_rootconf()
112{
113	struct device *booted_device;
114	int booted_partition;
115
116	findroot(&booted_device, &booted_partition);
117#ifdef DEBUG_KERNEL_START
118	printf("survived findroot()\n");
119#endif
120	setroot(booted_device, booted_partition, amiga_nam2blk);
121#ifdef DEBUG_KERNEL_START
122	printf("survived setroot()\n");
123#endif
124}
125
126/*ARGSUSED*/
127int
128simple_devprint(auxp, pnp)
129	void *auxp;
130	const char *pnp;
131{
132	return(QUIET);
133}
134
135int
136matchname(fp, sp)
137	char *fp, *sp;
138{
139	int len;
140
141	len = strlen(fp);
142	if (strlen(sp) != len)
143		return(0);
144	if (bcmp(fp, sp, len) == 0)
145		return(1);
146	return(0);
147}
148
149/*
150 * use config_search to find appropriate device, then call that device
151 * directly with NULL device variable storage.  A device can then
152 * always tell the difference betwean the real and console init
153 * by checking for NULL.
154 */
155int
156amiga_config_found(pcfp, pdp, auxp, pfn)
157	struct cfdata *pcfp;
158	struct device *pdp;
159	void *auxp;
160	cfprint_t pfn;
161{
162	struct device temp;
163	struct cfdata *cf;
164
165	if (amiga_realconfig)
166		return(config_found(pdp, auxp, pfn) != NULL);
167
168	if (pdp == NULL)
169		pdp = &temp;
170
171	pdp->dv_cfdata = pcfp;
172	if ((cf = config_search((cfmatch_t)NULL, pdp, auxp)) != NULL) {
173		cf->cf_attach->ca_attach(pdp, NULL, auxp);
174		pdp->dv_cfdata = NULL;
175		return(1);
176	}
177	pdp->dv_cfdata = NULL;
178	return(0);
179}
180
181/*
182 * this function needs to get enough configured to do a console
183 * basically this means start attaching the grfxx's that support
184 * the console. Kinda hacky but it works.
185 */
186void
187config_console()
188{
189	struct cfdata *cf;
190
191	/*
192	 * we need mainbus' cfdata.
193	 */
194	cf = config_rootsearch(NULL, "mainbus", "mainbus");
195	if (cf == NULL) {
196		panic("no mainbus");
197	}
198	/*
199	 * delay clock calibration.
200	 */
201	amiga_config_found(cf, NULL, "clock", NULL);
202
203	/*
204	 * internal grf.
205	 */
206#ifdef DRACO
207	if (!(is_draco()))
208#endif
209		amiga_config_found(cf, NULL, "grfcc", NULL);
210	/*
211	 * zbus knows when its not for real and will
212	 * only configure the appropriate hardware
213	 */
214	amiga_config_found(cf, NULL, "zbus", NULL);
215}
216
217/*
218 * mainbus driver
219 */
220struct cfattach mainbus_ca = {
221	sizeof(struct device), mbmatch, mbattach
222};
223
224struct cfdriver mainbus_cd = {
225	NULL, "mainbus", DV_DULL, NULL, 0
226};
227
228int
229mbmatch(pdp, cfp, auxp)
230	struct device	*pdp;
231	struct cfdata	*cfp;
232	void		*auxp;
233{
234
235	if (cfp->cf_unit > 0)
236		return(0);
237	/*
238	 * We are always here
239	 */
240	return(1);
241}
242
243/*
244 * "find" all the things that should be there.
245 */
246void
247mbattach(pdp, dp, auxp)
248	struct device *pdp, *dp;
249	void *auxp;
250{
251	printf("\n");
252	config_found(dp, "clock", simple_devprint);
253	if (is_a3000() || is_a4000()) {
254		config_found(dp, "a34kbbc", simple_devprint);
255	} else if (
256#ifdef DRACO
257	    !is_draco() &&
258#endif
259	    !is_a1200()) {
260
261		config_found(dp, "a2kbbc", simple_devprint);
262	}
263#ifdef DRACO
264	if (is_draco()) {
265		config_found(dp, "drbbc", simple_devprint);
266		config_found(dp, "kbd", simple_devprint);
267		config_found(dp, "drsc", simple_devprint);
268		config_found(dp, "drcom", simple_devprint);
269		config_found(dp, "drcom", simple_devprint);
270		/*
271		 * XXX -- missing here:
272		 * SuperIO chip parallel, floppy
273		 * or maybe just make that into a pseudo
274		 * ISA bus.
275		 */
276	} else
277#endif
278	{
279		config_found(dp, "ser", simple_devprint);
280		config_found(dp, "par", simple_devprint);
281		config_found(dp, "kbd", simple_devprint);
282		config_found(dp, "ms", simple_devprint);
283		config_found(dp, "ms", simple_devprint);
284		config_found(dp, "grfcc", simple_devprint);
285		config_found(dp, "fdc", simple_devprint);
286	}
287	if (is_a4000() || is_a1200())
288		config_found(dp, "idesc", simple_devprint);
289	if (is_a4000())			/* Try to configure A4000T SCSI */
290		config_found(dp, "afsc", simple_devprint);
291	config_found(dp, "zbus", simple_devprint);
292	if (is_a3000())
293		config_found(dp, "ahsc", simple_devprint);
294}
295
296int
297mbprint(auxp, pnp)
298	void *auxp;
299	const char *pnp;
300{
301	if (pnp)
302		printf("%s at %s", (char *)auxp, pnp);
303	return(UNCONF);
304}
305
306/*
307 * The system will assign the "booted device" indicator (and thus
308 * rootdev if rootspec is wildcarded) to the first partition 'a'
309 * in preference of boot.  However, it does walk unit backwards
310 * to remain compatible with the old Amiga method of picking the
311 * last root found.
312 */
313#include <sys/fcntl.h>		/* XXXX and all that uses it */
314#include <sys/proc.h>		/* XXXX and all that uses it */
315
316#include "fd.h"
317#include "sd.h"
318#include "cd.h"
319
320#if NFD > 0
321extern  struct cfdriver fd_cd;
322#endif
323#if NSD > 0
324extern  struct cfdriver sd_cd;
325#endif
326#if NCD > 0
327extern  struct cfdriver cd_cd;
328#endif
329
330struct cfdriver *genericconf[] = {
331#if NFD > 0
332	&fd_cd,
333#endif
334#if NSD > 0
335	&sd_cd,
336#endif
337#if NCD > 0
338	&cd_cd,
339#endif
340	NULL,
341};
342
343void
344findroot(devpp, partp)
345	struct device **devpp;
346	int *partp;
347{
348	struct disk *dkp;
349	struct partition *pp;
350	struct device **devs;
351	int i, maj, unit;
352
353	/*
354	 * Default to "not found".
355	 */
356	*devpp = NULL;
357
358	/* always partition 'a' */
359	*partp = 0;
360
361#if NSD > 0
362	/*
363	 * If we have the boot partition offset (boot_partition), try
364	 * to locate the device corresponding to that partition.
365	 */
366	if (boot_partition != 0) {
367	 	struct bdevsw *bdp;
368
369		for (unit = 0; unit < sd_cd.cd_ndevs; ++unit) {
370			if (sd_cd.cd_devs[unit] == NULL)
371				continue;
372
373			/*
374			 * Find the disk corresponding to the current
375			 * device.
376			 */
377			devs = (struct device **)sd_cd.cd_devs;
378			if ((dkp = disk_find(devs[unit]->dv_xname)) == NULL)
379				continue;
380
381			if (dkp->dk_driver == NULL ||
382			    dkp->dk_driver->d_strategy == NULL)
383				continue;
384			for (bdp = bdevsw; bdp < (bdevsw + nblkdev); bdp++)
385				if (bdp->d_strategy ==
386				    dkp->dk_driver->d_strategy)
387					break;
388			if (bdp->d_open(MAKEDISKDEV(4, unit, 0),
389			    FREAD | FNONBLOCK, 0, curproc))
390				continue;
391			bdp->d_close(MAKEDISKDEV(4, unit, 0),
392			    FREAD | FNONBLOCK, 0, curproc);
393			/*
394			 * XXX - assumes booting only from 'a' partition
395			 */
396			pp = &dkp->dk_label->d_partitions[0];
397			if (pp->p_size == 0 || pp->p_fstype != FS_BSDFFS)
398				continue;
399			if (pp->p_offset == boot_partition) {
400				if (*devpp == NULL) {
401					*devpp = devs[unit];
402					*partp = 0;	/* XXX */
403				} else
404					printf("Ambiguous boot device\n");
405			}
406		}
407	}
408	if (*devpp != NULL)
409		return;		/* we found the boot device */
410#endif
411
412	for (i = 0; genericconf[i] != NULL; i++) {
413		for (unit = genericconf[i]->cd_ndevs - 1; unit >= 0; unit--) {
414			if (genericconf[i]->cd_devs[unit] == NULL)
415				continue;
416
417			/*
418			 * Find the disk structure corresponding to the
419			 * current device.
420			 */
421			devs = (struct device **)genericconf[i]->cd_devs;
422			if ((dkp = disk_find(devs[unit]->dv_xname)) == NULL)
423				continue;
424
425			if (dkp->dk_driver == NULL ||
426			    dkp->dk_driver->d_strategy == NULL)
427				continue;
428
429			for (maj = 0; maj < nblkdev; maj++)
430				if (bdevsw[maj].d_strategy ==
431				    dkp->dk_driver->d_strategy)
432					break;
433#ifdef DIAGNOSTIC
434			if (maj >= nblkdev)
435				panic("findroot: impossible");
436#endif
437
438			/* Open disk; forces read of disklabel. */
439			if ((*bdevsw[maj].d_open)(MAKEDISKDEV(maj,
440			    unit, 0), FREAD|FNONBLOCK, 0, &proc0))
441				continue;
442			(void)(*bdevsw[maj].d_close)(MAKEDISKDEV(maj,
443			    unit, 0), FREAD|FNONBLOCK, 0, &proc0);
444
445			pp = &dkp->dk_label->d_partitions[0];
446			if (pp->p_size != 0 && pp->p_fstype == FS_BSDFFS) {
447				*devpp = devs[unit];
448				*partp = 0;
449				return;
450			}
451		}
452	}
453}
454
455/*
456 * Try to determine, of this machine is an A3000, which has a builtin
457 * realtime clock and scsi controller, so that this hardware is only
458 * included as "configured" if this IS an A3000
459 */
460
461int a3000_flag = 1;		/* patchable */
462#ifdef A4000
463int a4000_flag = 1;		/* patchable - default to A4000 */
464#else
465int a4000_flag = 0;		/* patchable */
466#endif
467
468int
469is_a3000()
470{
471	/* this is a dirty kludge.. but how do you do this RIGHT ? :-) */
472	extern long boot_fphystart;
473	short sc;
474
475	if ((machineid >> 16) == 3000)
476		return (1);			/* It's an A3000 */
477	if (machineid >> 16)
478		return (0);			/* It's not an A3000 */
479	/* Machine type is unknown, so try to guess it */
480	/* where is fastram on the A4000 ?? */
481	/* if fastram is below 0x07000000, assume it's not an A3000 */
482	if (boot_fphystart < 0x07000000)
483		return(0);
484	/*
485	 * OK, fastram starts at or above 0x07000000, check specific
486	 * machines
487	 */
488	for (sc = 0; sc < ncfdev; sc++) {
489		switch (cfdev[sc].rom.manid) {
490		case 2026:		/* Progressive Peripherals, Inc */
491			switch (cfdev[sc].rom.prodid) {
492			case 0:		/* PPI Mercury - A3000 */
493			case 1:		/* PP&S A3000 '040 */
494				return(1);
495			case 150:	/* PPI Zeus - it's an A2000 */
496			case 105:	/* PP&S A2000 '040 */
497			case 187:	/* PP&S A500 '040 */
498				return(0);
499			}
500			break;
501
502		case 2112:			/* IVS */
503			switch (cfdev[sc].rom.prodid) {
504			case 242:
505				return(0);	/* A2000 accelerator? */
506			}
507			break;
508		}
509	}
510	return (a3000_flag);		/* XXX let flag tell now */
511}
512
513int
514is_a4000()
515{
516	if ((machineid >> 16) == 4000)
517		return (1);		/* It's an A4000 */
518	if ((machineid >> 16) == 1200)
519		return (0);		/* It's an A1200, so not A4000 */
520#ifdef DRACO
521	if (is_draco())
522		return (0);
523#endif
524	/* Do I need this any more? */
525	if ((custom.deniseid & 0xff) == 0xf8)
526		return (1);
527#ifdef DEBUG
528	if (a4000_flag)
529		printf("Denise ID = %04x\n", (unsigned short)custom.deniseid);
530#endif
531	if (machineid >> 16)
532		return (0);		/* It's not an A4000 */
533	return (a4000_flag);		/* Machine type not set */
534}
535
536int
537is_a1200()
538{
539	if ((machineid >> 16) == 1200)
540		return (1);		/* It's an A1200 */
541	return (0);			/* Machine type not set */
542}
543
544#ifdef DRACO
545int
546is_draco()
547{
548	if ((machineid >> 24) == 0x7D)
549		return ((machineid >> 16) & 0xFF);
550	return (0);
551}
552#endif
553