1 /* $NetBSD: autoconf.c,v 1.74 2008/02/12 17:30:58 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass and Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Setup the system to run on the current machine. 41 * 42 * Configure() is called at boot time. Available devices are 43 * determined (from possibilities mentioned in ioconf.c), and 44 * the drivers are initialized. 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.74 2008/02/12 17:30:58 joerg Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/conf.h> 53 #include <sys/device.h> 54 55 #include "scsibus.h" 56 57 #if NSCSIBUS > 0 58 #include <dev/scsipi/scsi_all.h> 59 #include <dev/scsipi/scsipi_all.h> 60 #include <dev/scsipi/scsiconf.h> 61 #endif 62 63 #include <machine/autoconf.h> 64 #include <machine/mon.h> 65 66 #include <sun3/sun3/machdep.h> 67 68 69 /* Make sure the config is OK. */ 70 #if (defined(_SUN3_) + defined(_SUN3X_)) != 1 71 #error "Must have exactly one of: SUN3 and SUN3X options" 72 #endif 73 74 /* 75 * Do general device autoconfiguration, 76 * then choose root device (etc.) 77 * Called by sys/kern/subr_autoconf.c: configure() 78 */ 79 void 80 cpu_configure(void) 81 { 82 83 /* General device autoconfiguration. */ 84 if (config_rootfound("mainbus", NULL) == NULL) 85 panic("%s: mainbus not found", __func__); 86 87 /* 88 * Now that device autoconfiguration is finished, 89 * we can safely enable interrupts. 90 */ 91 printf("enabling interrupts\n"); 92 (void)spl0(); 93 } 94 95 /* 96 * bus_scan: 97 * This function is passed to config_search_ia() by the attach function 98 * for each of the "bus" drivers (obctl, obio, obmem, vme, ...). 99 * The purpose of this function is to copy the "locators" into our 100 * confargs structure, so child drivers may use the confargs both 101 * as match parameters and as temporary storage for the defaulted 102 * locator values determined in the child_match and preserved for 103 * the child_attach function. If the bus attach functions just 104 * used config_found, then we would not have an opportunity to 105 * setup the confargs for each child match and attach call. 106 */ 107 int 108 bus_scan(struct device *parent, struct cfdata *cf, const int *ldesc, void *aux) 109 { 110 struct confargs *ca = aux; 111 112 #ifdef DIAGNOSTIC 113 if (cf->cf_fstate == FSTATE_STAR) 114 panic("%s: FSTATE_STAR", __func__); 115 #endif 116 117 /* 118 * Copy the locators into our confargs. 119 * Our parent set ca->ca_bustype already. 120 */ 121 ca->ca_paddr = cf->cf_paddr; 122 ca->ca_intpri = cf->cf_intpri; 123 ca->ca_intvec = cf->cf_intvec; 124 125 /* 126 * Note that this allows the match function to save 127 * defaulted locators in the confargs that will be 128 * preserved for the related attach call. 129 * XXX - This is a hack... 130 */ 131 if (config_match(parent, cf, ca) > 0) { 132 config_attach(parent, cf, ca, bus_print); 133 } 134 return 0; 135 } 136 137 /* 138 * bus_print: 139 * Just print out the final (non-default) locators. 140 * The parent name is non-NULL when there was no match 141 * found by config_found(). 142 */ 143 int 144 bus_print(void *args, const char *name) 145 { 146 struct confargs *ca = args; 147 148 if (name) 149 aprint_normal("%s:", name); 150 151 if (ca->ca_paddr != -1) 152 aprint_normal(" addr 0x%lx", ca->ca_paddr); 153 if (ca->ca_intpri != -1) 154 aprint_normal(" ipl %d", ca->ca_intpri); 155 if (ca->ca_intvec != -1) 156 aprint_normal(" vect 0x%x", ca->ca_intvec); 157 158 return UNCONF; 159 } 160 161 /****************************************************************/ 162 163 /* This takes the args: name, ctlr, unit */ 164 typedef struct device * (*findfunc_t)(char *, int, int); 165 166 static struct device * net_find (char *, int, int); 167 #if NSCSIBUS > 0 168 static struct device * scsi_find(char *, int, int); 169 #endif 170 static struct device * xx_find (char *, int, int); 171 172 struct prom_n2f { 173 const char name[4]; 174 findfunc_t func; 175 }; 176 static struct prom_n2f prom_dev_table[] = { 177 { "ie", net_find }, 178 { "le", net_find }, 179 #if NSCSIBUS > 0 180 { "sd", scsi_find }, 181 #endif 182 { "xy", xx_find }, 183 { "xd", xx_find }, 184 { "", 0 }, 185 }; 186 187 /* 188 * Choose root and swap devices. 189 */ 190 void 191 cpu_rootconf(void) 192 { 193 struct bootparam *bp; 194 struct prom_n2f *nf; 195 struct device *boot_device; 196 int boot_partition; 197 const char *devname; 198 findfunc_t find; 199 char promname[4]; 200 char partname[4]; 201 202 /* PROM boot parameters. */ 203 bp = *romVectorPtr->bootParam; 204 205 /* 206 * Copy PROM boot device name (two letters) 207 * to a normal, null terminated string. 208 * (No terminating null in bp->devName) 209 */ 210 promname[0] = bp->devName[0]; 211 promname[1] = bp->devName[1]; 212 promname[2] = '\0'; 213 214 /* Default to "unknown" */ 215 boot_device = NULL; 216 boot_partition = 0; 217 devname = "<unknown>"; 218 partname[0] = '\0'; 219 find = NULL; 220 221 /* Do we know anything about the PROM boot device? */ 222 for (nf = prom_dev_table; nf->func; nf++) 223 if (!strcmp(nf->name, promname)) { 224 find = nf->func; 225 break; 226 } 227 if (find) 228 boot_device = (*find)(promname, bp->ctlrNum, bp->unitNum); 229 if (boot_device) { 230 devname = boot_device->dv_xname; 231 if (device_class(boot_device) == DV_DISK) { 232 boot_partition = bp->partNum & 7; 233 partname[0] = 'a' + boot_partition; 234 partname[1] = '\0'; 235 } 236 } 237 238 printf("boot device: %s%s\n", devname, partname); 239 setroot(boot_device, boot_partition); 240 } 241 242 /* 243 * Functions to find devices using PROM boot parameters. 244 */ 245 246 /* 247 * Network device: Just use controller number. 248 */ 249 static struct device * 250 net_find(char *name, int ctlr, int unit) 251 { 252 return device_find_by_driver_unit(name, ctlr); 253 } 254 255 #if NSCSIBUS > 0 256 /* 257 * SCSI device: The controller number corresponds to the 258 * scsibus number, and the unit number is (targ*8 + LUN). 259 */ 260 static struct device * 261 scsi_find(char *name, int ctlr, int unit) 262 { 263 struct device *scsibus; 264 struct scsibus_softc *sbsc; 265 struct scsipi_periph *periph; 266 int target, lun; 267 268 scsibus = device_find_by_driver_unit("scsibus", ctlr); 269 if (scsibus == NULL) 270 return NULL; 271 272 /* Compute SCSI target/LUN from PROM unit. */ 273 target = (unit >> 3) & 7; 274 lun = unit & 7; 275 276 /* Find the device at this target/LUN */ 277 sbsc = device_private(scsibus); 278 periph = scsipi_lookup_periph(sbsc->sc_channel, target, lun); 279 if (periph == NULL) 280 return NULL; 281 282 return periph->periph_dev; 283 } 284 #endif /* NSCSIBUS > 0 */ 285 286 /* 287 * Xylogics SMD disk: (xy, xd) 288 * Assume wired-in unit numbers for now... 289 */ 290 static struct device * 291 xx_find(char *name, int ctlr, int unit) 292 { 293 return device_find_by_driver_unit(name, ctlr * 2 + unit); 294 } 295 296 /* 297 * Misc. helpers used by driver match/attach functions. 298 */ 299 300 /* 301 * Read addr with size len (1,2,4) into val. 302 * If this generates a bus error, return -1 303 * 304 * Create a temporary mapping, 305 * Try the access using peek_* 306 * Clean up temp. mapping 307 */ 308 int 309 bus_peek(int bustype, int pa, int sz) 310 { 311 void *va; 312 int rv; 313 314 va = bus_tmapin(bustype, pa); 315 switch (sz) { 316 case 1: 317 rv = peek_byte(va); 318 break; 319 case 2: 320 rv = peek_word(va); 321 break; 322 case 4: 323 rv = peek_long(va); 324 break; 325 default: 326 printf(" %s: invalid size=%d\n", __func__, sz); 327 rv = -1; 328 } 329 bus_tmapout(va); 330 331 return rv; 332 } 333 334 /* from hp300: badbaddr() */ 335 int 336 peek_byte(void *addr) 337 { 338 label_t faultbuf; 339 int x; 340 341 nofault = &faultbuf; 342 if (setjmp(&faultbuf)) 343 x = -1; 344 else 345 x = *(volatile u_char *)addr; 346 347 nofault = NULL; 348 return x; 349 } 350 351 int 352 peek_word(void *addr) 353 { 354 label_t faultbuf; 355 int x; 356 357 nofault = &faultbuf; 358 if (setjmp(&faultbuf)) 359 x = -1; 360 else 361 x = *(volatile u_short *)addr; 362 363 nofault = NULL; 364 return x; 365 } 366 367 int 368 peek_long(void *addr) 369 { 370 label_t faultbuf; 371 int x; 372 373 nofault = &faultbuf; 374 if (setjmp(&faultbuf)) 375 x = -1; 376 else { 377 x = *(volatile int *)addr; 378 if (x == -1) { 379 printf("%s: uh-oh, actually read -1!\n", __func__); 380 x &= 0x7FFFffff; /* XXX */ 381 } 382 } 383 384 nofault = NULL; 385 return x; 386 } 387