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