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