autoconf.c revision 1.87
1/*	$NetBSD: autoconf.c,v 1.87 2003/01/01 00:28:56 thorpej 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
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.87 2003/01/01 00:28:56 thorpej Exp $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/reboot.h>
39#include <sys/conf.h>
40#include <sys/buf.h>
41#include <sys/device.h>
42#include <sys/disklabel.h>
43#include <sys/disk.h>
44#include <machine/cpu.h>
45#include <amiga/amiga/cfdev.h>
46#include <amiga/amiga/device.h>
47#include <amiga/amiga/custom.h>
48
49static void findroot(void);
50void mbattach(struct device *, struct device *, void *);
51int mbprint(void *, const char *);
52int mbmatch(struct device *, struct cfdata *, void *);
53
54#include <sys/kernel.h>
55
56u_long boot_partition;
57struct device *booted_device;
58int booted_partition;
59
60int amiga_realconfig;
61
62/*
63 * called at boot time, configure all devices on system
64 */
65void
66cpu_configure()
67{
68	int s;
69#ifdef DEBUG_KERNEL_START
70	int i;
71#endif
72
73	/*
74	 * this is the real thing baby (i.e. not console init)
75	 */
76	amiga_realconfig = 1;
77#ifdef DRACO
78	if (is_draco()) {
79		*draco_intena &= ~DRIRQ_GLOBAL;
80	} else
81#endif
82	custom.intena = INTF_INTEN;
83	s = splhigh();
84
85	if (config_rootfound("mainbus", "mainbus") == NULL)
86		panic("no mainbus found");
87
88#ifdef DEBUG_KERNEL_START
89	printf("survived autoconf, going to enable interrupts\n");
90#endif
91
92#ifdef DRACO
93	if (is_draco()) {
94		*draco_intena |= DRIRQ_GLOBAL;
95		/* softints always enabled */
96	} else
97#endif
98	{
99		custom.intena = INTF_SETCLR | INTF_INTEN;
100
101		/* also enable hardware aided software interrupts */
102		custom.intena = INTF_SETCLR | INTF_SOFTINT;
103	}
104#ifdef DEBUG_KERNEL_START
105	for (i=splhigh(); i>=s ;i-=0x100) {
106		splx(i);
107		printf("%d...", (i>>8) & 7);
108	}
109	printf("survived interrupt enable\n");
110#else
111	splx(s);
112#endif
113#ifdef DEBUG_KERNEL_START
114	printf("survived configure...\n");
115#endif
116}
117
118void
119cpu_rootconf()
120{
121	findroot();
122#ifdef DEBUG_KERNEL_START
123	printf("survived findroot()\n");
124#endif
125	setroot(booted_device, booted_partition);
126#ifdef DEBUG_KERNEL_START
127	printf("survived setroot()\n");
128#endif
129}
130
131/*ARGSUSED*/
132int
133simple_devprint(auxp, pnp)
134	void *auxp;
135	const char *pnp;
136{
137	return(QUIET);
138}
139
140int
141matchname(fp, sp)
142	char *fp, *sp;
143{
144	int len;
145
146	len = strlen(fp);
147	if (strlen(sp) != len)
148		return(0);
149	if (bcmp(fp, sp, len) == 0)
150		return(1);
151	return(0);
152}
153
154/*
155 * use config_search to find appropriate device, then call that device
156 * directly with NULL device variable storage.  A device can then
157 * always tell the difference betwean the real and console init
158 * by checking for NULL.
159 */
160int
161amiga_config_found(pcfp, pdp, auxp, pfn)
162	struct cfdata *pcfp;
163	struct device *pdp;
164	void *auxp;
165	cfprint_t pfn;
166{
167	struct device temp;
168	struct cfdata *cf;
169	const struct cfattach *ca;
170
171	if (amiga_realconfig)
172		return(config_found(pdp, auxp, pfn) != NULL);
173
174	if (pdp == NULL)
175		pdp = &temp;
176
177	pdp->dv_cfdata = pcfp;
178	if ((cf = config_search((cfmatch_t)NULL, pdp, auxp)) != NULL) {
179		ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
180		if (ca != NULL) {
181			(*ca->ca_attach)(pdp, NULL, auxp);
182			pdp->dv_cfdata = NULL;
183			return(1);
184		}
185	}
186	pdp->dv_cfdata = NULL;
187	return(0);
188}
189
190/*
191 * this function needs to get enough configured to do a console
192 * basically this means start attaching the grfxx's that support
193 * the console. Kinda hacky but it works.
194 */
195void
196config_console()
197{
198	struct cfdata *cf;
199
200	config_init();
201
202	/*
203	 * we need mainbus' cfdata.
204	 */
205	cf = config_rootsearch(NULL, "mainbus", "mainbus");
206	if (cf == NULL) {
207		panic("no mainbus");
208	}
209	/*
210	 * delay clock calibration.
211	 */
212	amiga_config_found(cf, NULL, "clock", NULL);
213
214	/*
215	 * internal grf.
216	 */
217#ifdef DRACO
218	if (!(is_draco()))
219#endif
220		amiga_config_found(cf, NULL, "grfcc", NULL);
221
222	/*
223	 * zbus knows when its not for real and will
224	 * only configure the appropriate hardware
225	 */
226	amiga_config_found(cf, NULL, "zbus", NULL);
227}
228
229/*
230 * mainbus driver
231 */
232CFATTACH_DECL(mainbus, sizeof(struct device),
233    mbmatch, mbattach, NULL, NULL);
234
235int
236mbmatch(pdp, cfp, auxp)
237	struct device	*pdp;
238	struct cfdata	*cfp;
239	void		*auxp;
240{
241#if 0	/*
242	 * XXX is this right? but we need to be found twice
243	 * (early console init hack)
244	 */
245	static int mainbus_matched = 0;
246
247	/* Allow only one instance. */
248	if (mainbus_matched)
249		return (0);
250
251	mainbus_matched = 1;
252#endif
253	return (1);
254}
255
256/*
257 * "find" all the things that should be there.
258 */
259void
260mbattach(pdp, dp, auxp)
261	struct device *pdp, *dp;
262	void *auxp;
263{
264	printf("\n");
265	config_found(dp, "clock", simple_devprint);
266	if (is_a3000() || is_a4000()) {
267		config_found(dp, "a34kbbc", simple_devprint);
268	} else
269#ifdef DRACO
270	if (!is_draco())
271#endif
272	{
273		config_found(dp, "a2kbbc", simple_devprint);
274	}
275#ifdef DRACO
276	if (is_draco()) {
277		config_found(dp, "drbbc", simple_devprint);
278		config_found(dp, "kbd", simple_devprint);
279		config_found(dp, "drsc", simple_devprint);
280		config_found(dp, "drsupio", simple_devprint);
281	} else
282#endif
283	{
284		config_found(dp, "ser", simple_devprint);
285		config_found(dp, "par", simple_devprint);
286		config_found(dp, "kbd", simple_devprint);
287		config_found(dp, "ms", simple_devprint);
288		config_found(dp, "grfcc", simple_devprint);
289		config_found(dp, "amidisplaycc", simple_devprint);
290		config_found(dp, "fdc", simple_devprint);
291	}
292	if (is_a4000() || is_a1200()) {
293		config_found(dp, "wdc", simple_devprint);
294		config_found(dp, "idesc", simple_devprint);
295	}
296	if (is_a4000())			/* Try to configure A4000T SCSI */
297		config_found(dp, "afsc", simple_devprint);
298	if (is_a3000())
299		config_found(dp, "ahsc", simple_devprint);
300	if (/*is_a600() || */is_a1200())
301		config_found(dp, "pccard", simple_devprint);
302#ifdef DRACO
303	if (!is_draco())
304#endif
305		config_found(dp, "aucc", simple_devprint);
306
307	config_found(dp, "zbus", simple_devprint);
308}
309
310int
311mbprint(auxp, pnp)
312	void *auxp;
313	const char *pnp;
314{
315	if (pnp)
316		aprint_normal("%s at %s", (char *)auxp, pnp);
317	return(UNCONF);
318}
319
320/*
321 * The system will assign the "booted device" indicator (and thus
322 * rootdev if rootspec is wildcarded) to the first partition 'a'
323 * in preference of boot.  However, it does walk unit backwards
324 * to remain compatible with the old Amiga method of picking the
325 * last root found.
326 */
327#include <sys/fcntl.h>		/* XXXX and all that uses it */
328#include <sys/proc.h>		/* XXXX and all that uses it */
329
330#include "fd.h"
331#include "sd.h"
332#include "cd.h"
333#include "wd.h"
334
335#if NFD > 0
336extern  struct cfdriver fd_cd;
337extern	const struct bdevsw fd_bdevsw;
338#endif
339#if NSD > 0
340extern  struct cfdriver sd_cd;
341extern	const struct bdevsw sd_bdevsw;
342#endif
343#if NCD > 0
344extern  struct cfdriver cd_cd;
345extern	const struct bdevsw cd_bdevsw;
346#endif
347#if NWD > 0
348extern  struct cfdriver wd_cd;
349extern	const struct bdevsw wd_bdevsw;
350#endif
351
352struct cfdriver *genericconf[] = {
353#if NFD > 0
354	&fd_cd,
355#endif
356#if NSD > 0
357	&sd_cd,
358#endif
359#if NWD > 0
360	&wd_cd,
361#endif
362#if NCD > 0
363	&cd_cd,
364#endif
365	NULL,
366};
367
368void
369findroot(void)
370{
371	struct disk *dkp;
372	struct partition *pp;
373	struct device **devs;
374	int i, maj, unit;
375	const struct bdevsw *bdp;
376
377#if NSD > 0
378	/*
379	 * If we have the boot partition offset (boot_partition), try
380	 * to locate the device corresponding to that partition.
381	 */
382#ifdef DEBUG_KERNEL_START
383	printf("Boot partition offset is %ld\n", boot_partition);
384#endif
385	if (boot_partition != 0) {
386		int i;
387
388		for (unit = 0; unit < sd_cd.cd_ndevs; ++unit) {
389#ifdef DEBUG_KERNEL_START
390			printf("probing for sd%d\n", unit);
391#endif
392			if (sd_cd.cd_devs[unit] == NULL)
393				continue;
394
395			/*
396			 * Find the disk corresponding to the current
397			 * device.
398			 */
399			devs = (struct device **)sd_cd.cd_devs;
400			if ((dkp = disk_find(devs[unit]->dv_xname)) == NULL)
401				continue;
402
403			if (dkp->dk_driver == NULL ||
404			    dkp->dk_driver->d_strategy == NULL)
405				continue;
406			bdp = &sd_bdevsw;
407			maj = bdevsw_lookup_major(bdp);
408			if ((*bdp->d_open)(MAKEDISKDEV(maj, unit, RAW_PART),
409			    FREAD | FNONBLOCK, 0, curproc))
410				continue;
411			(*bdp->d_close)(MAKEDISKDEV(maj, unit, RAW_PART),
412			    FREAD | FNONBLOCK, 0, curproc);
413			pp = &dkp->dk_label->d_partitions[0];
414			for (i = 0; i < dkp->dk_label->d_npartitions;
415			    i++, pp++) {
416#ifdef DEBUG_KERNEL_START
417				printf("sd%d%c type %d offset %d size %d\n",
418					unit, i+'a', pp->p_fstype,
419					pp->p_offset, pp->p_size);
420#endif
421				if (pp->p_size == 0 ||
422				    (pp->p_fstype != FS_BSDFFS &&
423				    pp->p_fstype != FS_SWAP))
424					continue;
425				if (pp->p_offset == boot_partition) {
426					if (booted_device == NULL) {
427						booted_device = devs[unit];
428						booted_partition = i;
429					} else
430						printf("Ambiguous boot device\n");
431				}
432			}
433		}
434	}
435	if (booted_device != NULL)
436		return;		/* we found the boot device */
437#endif
438
439	for (i = 0; genericconf[i] != NULL; i++) {
440		for (unit = genericconf[i]->cd_ndevs - 1; unit >= 0; unit--) {
441			if (genericconf[i]->cd_devs[unit] == NULL)
442				continue;
443
444			/*
445			 * Find the disk structure corresponding to the
446			 * current device.
447			 */
448			devs = (struct device **)genericconf[i]->cd_devs;
449			if ((dkp = disk_find(devs[unit]->dv_xname)) == NULL)
450				continue;
451
452			if (dkp->dk_driver == NULL ||
453			    dkp->dk_driver->d_strategy == NULL)
454				continue;
455
456#if NFD > 0
457			if (fd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
458				bdp = &fd_bdevsw;
459#endif
460#if NSD > 0
461			if (sd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
462				bdp = &sd_bdevsw;
463#endif
464#if NWD > 0
465			if (wd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
466				bdp = &wd_bdevsw;
467#endif
468#if NCD > 0
469			if (cd_bdevsw.d_strategy == dkp->dk_driver->d_strategy)
470				bdp = &cd_bdevsw;
471#endif
472#ifdef DIAGNOSTIC
473			if (bdp == NULL)
474				panic("findroot: impossible");
475#endif
476			maj = bdevsw_lookup_major(bdp);
477
478			/* Open disk; forces read of disklabel. */
479			if ((*bdp->d_open)(MAKEDISKDEV(maj,
480			    unit, 0), FREAD|FNONBLOCK, 0, &proc0))
481				continue;
482			(void)(*bdp->d_close)(MAKEDISKDEV(maj,
483			    unit, 0), FREAD|FNONBLOCK, 0, &proc0);
484
485			pp = &dkp->dk_label->d_partitions[0];
486			if (pp->p_size != 0 && pp->p_fstype == FS_BSDFFS) {
487				booted_device = devs[unit];
488				booted_partition = 0;
489				return;
490			}
491		}
492	}
493}
494
495/*
496 * Try to determine, of this machine is an A3000, which has a builtin
497 * realtime clock and scsi controller, so that this hardware is only
498 * included as "configured" if this IS an A3000
499 */
500
501int a3000_flag = 1;		/* patchable */
502#ifdef A4000
503int a4000_flag = 1;		/* patchable - default to A4000 */
504#else
505int a4000_flag = 0;		/* patchable */
506#endif
507
508int
509is_a3000()
510{
511	/* this is a dirty kludge.. but how do you do this RIGHT ? :-) */
512	extern long boot_fphystart;
513	short sc;
514
515	if ((machineid >> 16) == 3000)
516		return (1);			/* It's an A3000 */
517	if (machineid >> 16)
518		return (0);			/* It's not an A3000 */
519	/* Machine type is unknown, so try to guess it */
520	/* where is fastram on the A4000 ?? */
521	/* if fastram is below 0x07000000, assume it's not an A3000 */
522	if (boot_fphystart < 0x07000000)
523		return(0);
524	/*
525	 * OK, fastram starts at or above 0x07000000, check specific
526	 * machines
527	 */
528	for (sc = 0; sc < ncfdev; sc++) {
529		switch (cfdev[sc].rom.manid) {
530		case 2026:		/* Progressive Peripherals, Inc */
531			switch (cfdev[sc].rom.prodid) {
532			case 0:		/* PPI Mercury - A3000 */
533			case 1:		/* PP&S A3000 '040 */
534				return(1);
535			case 150:	/* PPI Zeus - it's an A2000 */
536			case 105:	/* PP&S A2000 '040 */
537			case 187:	/* PP&S A500 '040 */
538				return(0);
539			}
540			break;
541
542		case 2112:			/* IVS */
543			switch (cfdev[sc].rom.prodid) {
544			case 242:
545				return(0);	/* A2000 accelerator? */
546			}
547			break;
548		}
549	}
550	return (a3000_flag);		/* XXX let flag tell now */
551}
552
553int
554is_a4000()
555{
556	if ((machineid >> 16) == 4000)
557		return (1);		/* It's an A4000 */
558	if ((machineid >> 16) == 1200)
559		return (0);		/* It's an A1200, so not A4000 */
560#ifdef DRACO
561	if (is_draco())
562		return (0);
563#endif
564	/* Do I need this any more? */
565	if ((custom.deniseid & 0xff) == 0xf8)
566		return (1);
567#ifdef DEBUG
568	if (a4000_flag)
569		printf("Denise ID = %04x\n", (unsigned short)custom.deniseid);
570#endif
571	if (machineid >> 16)
572		return (0);		/* It's not an A4000 */
573	return (a4000_flag);		/* Machine type not set */
574}
575
576int
577is_a1200()
578{
579	if ((machineid >> 16) == 1200)
580		return (1);		/* It's an A1200 */
581	return (0);			/* Machine type not set */
582}
583