xref: /netbsd-src/sys/arch/zaurus/zaurus/autoconf.c (revision e5fbc36ada28f9b9a5836ecffaf4a06aa1ebb687)
1*e5fbc36aSthorpej /*	$NetBSD: autoconf.c,v 1.17 2023/12/20 15:34:46 thorpej Exp $	*/
29dd043ccSober 
39dd043ccSober /*-
49dd043ccSober  * Copyright (c) 2002 The NetBSD Foundation, Inc.
59dd043ccSober  * All rights reserved.
69dd043ccSober  *
79dd043ccSober  * Redistribution and use in source and binary forms, with or without
89dd043ccSober  * modification, are permitted provided that the following conditions
99dd043ccSober  * are met:
109dd043ccSober  * 1. Redistributions of source code must retain the above copyright
119dd043ccSober  *    notice, this list of conditions and the following disclaimer.
129dd043ccSober  * 2. Redistributions in binary form must reproduce the above copyright
139dd043ccSober  *    notice, this list of conditions and the following disclaimer in the
149dd043ccSober  *    documentation and/or other materials provided with the distribution.
159dd043ccSober  *
169dd043ccSober  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
179dd043ccSober  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
189dd043ccSober  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199dd043ccSober  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
209dd043ccSober  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
219dd043ccSober  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
229dd043ccSober  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
239dd043ccSober  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
249dd043ccSober  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
259dd043ccSober  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
269dd043ccSober  * POSSIBILITY OF SUCH DAMAGE.
279dd043ccSober  */
289dd043ccSober 
299dd043ccSober #include <sys/cdefs.h>
30*e5fbc36aSthorpej __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.17 2023/12/20 15:34:46 thorpej Exp $");
31f19ed1a8Speter 
32f19ed1a8Speter #include "opt_md.h"
339dd043ccSober 
349dd043ccSober #include <sys/param.h>
359dd043ccSober #include <sys/systm.h>
369dd043ccSober #include <sys/device.h>
37af901e68Snonaka #include <sys/disklabel.h>
389dd043ccSober #include <sys/conf.h>
39af901e68Snonaka #include <sys/vnode.h>
40af901e68Snonaka #include <sys/fcntl.h>
41af901e68Snonaka #include <sys/proc.h>
42af901e68Snonaka #include <sys/disk.h>
43af901e68Snonaka #include <sys/kauth.h>
44f19ed1a8Speter 
45bc98febdStsutsui #include <dev/i2c/i2cvar.h>
46bc98febdStsutsui 
47f19ed1a8Speter #include <machine/intr.h>
48f19ed1a8Speter #include <machine/bootconfig.h>
49af901e68Snonaka #include <machine/bootinfo.h>
501de7f198Snonaka #include <machine/config_hook.h>
51f19ed1a8Speter 
52b504b9ffSdyoung static int is_valid_disk(device_t dv);
53b504b9ffSdyoung static int match_bootdisk(device_t dv, struct btinfo_bootdisk *bid);
54af901e68Snonaka static void findroot(void);
559dd043ccSober 
569dd043ccSober void
cpu_configure(void)579dd043ccSober cpu_configure(void)
589dd043ccSober {
599dd043ccSober 
609dd043ccSober 	splhigh();
61f19ed1a8Speter 	splserial();
62f19ed1a8Speter 
631de7f198Snonaka 	config_hook_init();
641de7f198Snonaka 
659dd043ccSober 	if (config_rootfound("mainbus", NULL) == NULL)
669dd043ccSober 		panic("no mainbus found");
679dd043ccSober 
689dd043ccSober 	/* Configuration is finished, turn on interrupts. */
699dd043ccSober 	spl0();
709dd043ccSober }
719dd043ccSober 
72af901e68Snonaka static int
is_valid_disk(device_t dv)73b504b9ffSdyoung is_valid_disk(device_t dv)
74af901e68Snonaka {
75af901e68Snonaka 
76af901e68Snonaka 	if (device_class(dv) != DV_DISK)
77af901e68Snonaka 		return 0;
78af901e68Snonaka 
79af901e68Snonaka 	return (device_is_a(dv, "dk") ||
80af901e68Snonaka 		device_is_a(dv, "sd") ||
81af901e68Snonaka 		device_is_a(dv, "wd") ||
82af901e68Snonaka 		device_is_a(dv, "ld"));
83af901e68Snonaka }
84af901e68Snonaka 
85af901e68Snonaka /*
86af901e68Snonaka  * Helper function for findroot():
87af901e68Snonaka  * Return non-zero if disk device matches bootinfo.
88af901e68Snonaka  */
89af901e68Snonaka static int
match_bootdisk(device_t dv,struct btinfo_bootdisk * bid)90b504b9ffSdyoung match_bootdisk(device_t dv, struct btinfo_bootdisk *bid)
91af901e68Snonaka {
92af901e68Snonaka 	struct vnode *tmpvn;
93af901e68Snonaka 	int error;
94af901e68Snonaka 	struct disklabel label;
95af901e68Snonaka 	int found = 0;
96af901e68Snonaka 
97af901e68Snonaka 	if (device_is_a(dv, "dk"))
98af901e68Snonaka 		return 0;
99af901e68Snonaka 
100af901e68Snonaka 	/*
101af901e68Snonaka 	 * A disklabel is required here.  The boot loader doesn't refuse
102af901e68Snonaka 	 * to boot from a disk without a label, but this is normally not
103af901e68Snonaka 	 * wanted.
104af901e68Snonaka 	 */
105af901e68Snonaka 	if (bid->labelsector == -1)
106af901e68Snonaka 		return 0;
107af901e68Snonaka 
108af901e68Snonaka 	if ((tmpvn = opendisk(dv)) == NULL)
109af901e68Snonaka 		return 0;
110af901e68Snonaka 
111afc8fd54Shannken 	VOP_UNLOCK(tmpvn);
112af901e68Snonaka 	error = VOP_IOCTL(tmpvn, DIOCGDINFO, &label, FREAD, NOCRED);
113afc8fd54Shannken 	vn_lock(tmpvn, LK_EXCLUSIVE | LK_RETRY);
114af901e68Snonaka 	if (error) {
115af901e68Snonaka 		/*
116af901e68Snonaka 		 * XXX Can't happen -- open() would have errored out
117af901e68Snonaka 		 * or faked one up.
118af901e68Snonaka 		 */
119af901e68Snonaka 		printf("match_bootdisk: can't get label for dev %s (%d)\n",
120af901e68Snonaka 		    device_xname(dv), error);
121af901e68Snonaka 		goto closeout;
122af901e68Snonaka 	}
123af901e68Snonaka 
124af901e68Snonaka 	/* Compare with our data. */
125af901e68Snonaka 	if (label.d_type == bid->label.type &&
126af901e68Snonaka 	    label.d_checksum == bid->label.checksum &&
127af901e68Snonaka 	    strncmp(label.d_packname, bid->label.packname, 16) == 0)
128af901e68Snonaka 		found = 1;
129af901e68Snonaka 
130af901e68Snonaka  closeout:
131af901e68Snonaka 	VOP_CLOSE(tmpvn, FREAD, NOCRED);
132af901e68Snonaka 	vput(tmpvn);
133af901e68Snonaka 	return found;
134af901e68Snonaka }
135af901e68Snonaka 
136af901e68Snonaka static void
findroot(void)137af901e68Snonaka findroot(void)
138af901e68Snonaka {
13915d23a2fSnonaka 	struct btinfo_rootdevice *biv;
140af901e68Snonaka 	struct btinfo_bootdisk *bid;
141af901e68Snonaka 	device_t dv;
142b504b9ffSdyoung 	deviter_t di;
143af901e68Snonaka 
144af901e68Snonaka 	if (booted_device)
145af901e68Snonaka 		return;
146af901e68Snonaka 
14715d23a2fSnonaka 	if ((biv = lookup_bootinfo(BTINFO_ROOTDEVICE)) != NULL) {
148b504b9ffSdyoung 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
149b504b9ffSdyoung 		     dv != NULL;
150b504b9ffSdyoung 		     dv = deviter_next(&di)) {
15115d23a2fSnonaka 			cfdata_t cd;
15215d23a2fSnonaka 			size_t len;
15315d23a2fSnonaka 
15415d23a2fSnonaka 			if (device_class(dv) != DV_DISK)
15515d23a2fSnonaka 				continue;
15615d23a2fSnonaka 
15715d23a2fSnonaka 			cd = device_cfdata(dv);
15815d23a2fSnonaka 			len = strlen(cd->cf_name);
15915d23a2fSnonaka 
16015d23a2fSnonaka 			if (strncmp(cd->cf_name, biv->devname, len) == 0 &&
16115d23a2fSnonaka 			    biv->devname[len] - '0' == cd->cf_unit) {
1626e129ceaSmlelstv 				booted_device = dv;
1636e129ceaSmlelstv 				booted_partition = biv->devname[len + 1] - 'a';
164b504b9ffSdyoung 				break;
165b504b9ffSdyoung 			}
166b504b9ffSdyoung 		}
167b504b9ffSdyoung 		deviter_release(&di);
168b504b9ffSdyoung 		if (dv != NULL)
16915d23a2fSnonaka 			return;
17015d23a2fSnonaka 	}
17115d23a2fSnonaka 
172af901e68Snonaka 	if ((bid = lookup_bootinfo(BTINFO_BOOTDISK)) != NULL) {
173af901e68Snonaka 		/*
174af901e68Snonaka 		 * Scan all disk devices for ones that match the passed data.
175af901e68Snonaka 		 * Don't break if one is found, to get possible multiple
176af901e68Snonaka 		 * matches - for problem tracking.  Use the first match anyway
177af901e68Snonaka 		 * because lower device numbers are more likely to be the
178af901e68Snonaka 		 * boot device.
179af901e68Snonaka 		 */
180b504b9ffSdyoung 		for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
181b504b9ffSdyoung 		     dv != NULL;
182b504b9ffSdyoung 		     dv = deviter_next(&di)) {
183af901e68Snonaka 			if (device_class(dv) != DV_DISK)
184af901e68Snonaka 				continue;
185af901e68Snonaka 
186af901e68Snonaka 			if (is_valid_disk(dv)) {
1879d698592Snonaka 				if (match_bootdisk(dv, bid))
188af901e68Snonaka 					goto bootdisk_found;
189af901e68Snonaka 			}
190af901e68Snonaka 			continue;
191af901e68Snonaka 
192af901e68Snonaka  bootdisk_found:
193af901e68Snonaka 			if (booted_device) {
194af901e68Snonaka 				printf("WARNING: double match for boot "
195af901e68Snonaka 				    "device (%s, %s)\n",
196af901e68Snonaka 				    device_xname(booted_device),
197af901e68Snonaka 				    device_xname(dv));
198af901e68Snonaka 				continue;
199af901e68Snonaka 			}
2006e129ceaSmlelstv 			booted_device = dv;
2016e129ceaSmlelstv 			booted_partition = bid->partition;
202af901e68Snonaka 		}
203b504b9ffSdyoung 		deviter_release(&di);
204af901e68Snonaka 
205af901e68Snonaka 		if (booted_device)
206af901e68Snonaka 			return;
207af901e68Snonaka 	}
208af901e68Snonaka }
209af901e68Snonaka 
210af901e68Snonaka void
cpu_rootconf(void)211af901e68Snonaka cpu_rootconf(void)
212af901e68Snonaka {
213af901e68Snonaka 
214af901e68Snonaka 	findroot();
215af901e68Snonaka 
216af901e68Snonaka 	aprint_normal("boot device: %s\n",
217af901e68Snonaka 	    booted_device ? device_xname(booted_device) : "<unknown>");
2188ce44338Smlelstv 	rootconf();
219af901e68Snonaka }
220af901e68Snonaka 
2219dd043ccSober void
device_register(device_t dev,void * aux)222b504b9ffSdyoung device_register(device_t dev, void *aux)
2239dd043ccSober {
2249dd043ccSober 
225bc98febdStsutsui 	/*
226bc98febdStsutsui 	 * I2C bus conntected to pxaiic(4) for zaudio(4) devices has
227bc98febdStsutsui 	 * limited capabilities.
228bc98febdStsutsui 	 */
229bc98febdStsutsui 	if (device_is_a(dev, "iic") &&
2303aa5a3aeSriastradh 	    device_is_a(device_parent(dev), "ziic")) {
23131510453Schristos 		(void)prop_dictionary_set_string_nocopy(device_properties(dev),
232bc98febdStsutsui 		    I2C_PROP_INDIRECT_PROBE_STRATEGY, I2C_PROBE_STRATEGY_NONE);
233bc98febdStsutsui 	}
2349dd043ccSober }
235