1 /* $NetBSD: autoconf.c,v 1.66 2005/12/11 12:19:27 christos 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.66 2005/12/11 12:19:27 christos 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 machdep.c: cpu_startup() 78 */ 79 void 80 cpu_configure(void) 81 { 82 83 /* General device autoconfiguration. */ 84 if (config_rootfound("mainbus", NULL) == NULL) 85 panic("configure: mainbus not found"); 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, 109 const int *ldesc, void *aux) 110 { 111 struct confargs *ca = aux; 112 113 #ifdef DIAGNOSTIC 114 if (cf->cf_fstate == FSTATE_STAR) 115 panic("bus_scan: FSTATE_STAR"); 116 #endif 117 118 /* 119 * Copy the locators into our confargs. 120 * Our parent set ca->ca_bustype already. 121 */ 122 ca->ca_paddr = cf->cf_paddr; 123 ca->ca_intpri = cf->cf_intpri; 124 ca->ca_intvec = cf->cf_intvec; 125 126 /* 127 * Note that this allows the match function to save 128 * defaulted locators in the confargs that will be 129 * preserved for the related attach call. 130 * XXX - This is a hack... 131 */ 132 if (config_match(parent, cf, ca) > 0) { 133 config_attach(parent, cf, ca, bus_print); 134 } 135 return (0); 136 } 137 138 /* 139 * bus_print: 140 * Just print out the final (non-default) locators. 141 * The parent name is non-NULL when there was no match 142 * found by config_found(). 143 */ 144 int 145 bus_print(void *args, const char *name) 146 { 147 struct confargs *ca = args; 148 149 if (name) 150 aprint_normal("%s:", name); 151 152 if (ca->ca_paddr != -1) 153 aprint_normal(" addr 0x%x", ca->ca_paddr); 154 if (ca->ca_intpri != -1) 155 aprint_normal(" ipl %d", ca->ca_intpri); 156 if (ca->ca_intvec != -1) 157 aprint_normal(" vect 0x%x", ca->ca_intvec); 158 159 return(UNCONF); 160 } 161 162 /****************************************************************/ 163 164 /* This takes the args: name, ctlr, unit */ 165 typedef struct device * (*findfunc_t)(char *, int, int); 166 167 static struct device * find_dev_byname(char *); 168 static struct device * net_find (char *, int, int); 169 #if NSCSIBUS > 0 170 static struct device * scsi_find(char *, int, int); 171 #endif 172 static struct device * xx_find (char *, int, int); 173 174 struct prom_n2f { 175 const char name[4]; 176 findfunc_t func; 177 }; 178 static struct prom_n2f prom_dev_table[] = { 179 { "ie", net_find }, 180 { "le", net_find }, 181 #if NSCSIBUS > 0 182 { "sd", scsi_find }, 183 #endif 184 { "xy", xx_find }, 185 { "xd", xx_find }, 186 { "", 0 }, 187 }; 188 189 /* 190 * Choose root and swap devices. 191 */ 192 void 193 cpu_rootconf(void) 194 { 195 struct bootparam *bp; 196 struct prom_n2f *nf; 197 struct device *boot_device; 198 int boot_partition; 199 const char *devname; 200 findfunc_t find; 201 char promname[4]; 202 char partname[4]; 203 204 /* PROM boot parameters. */ 205 bp = *romVectorPtr->bootParam; 206 207 /* 208 * Copy PROM boot device name (two letters) 209 * to a normal, null terminated string. 210 * (No terminating null in bp->devName) 211 */ 212 promname[0] = bp->devName[0]; 213 promname[1] = bp->devName[1]; 214 promname[2] = '\0'; 215 216 /* Default to "unknown" */ 217 boot_device = NULL; 218 boot_partition = 0; 219 devname = "<unknown>"; 220 partname[0] = '\0'; 221 find = NULL; 222 223 /* Do we know anything about the PROM boot device? */ 224 for (nf = prom_dev_table; nf->func; nf++) 225 if (!strcmp(nf->name, promname)) { 226 find = nf->func; 227 break; 228 } 229 if (find) 230 boot_device = (*find)(promname, bp->ctlrNum, bp->unitNum); 231 if (boot_device) { 232 devname = boot_device->dv_xname; 233 if (boot_device->dv_class == DV_DISK) { 234 boot_partition = bp->partNum & 7; 235 partname[0] = 'a' + boot_partition; 236 partname[1] = '\0'; 237 } 238 } 239 240 printf("boot device: %s%s\n", devname, partname); 241 setroot(boot_device, boot_partition); 242 } 243 244 /* 245 * Functions to find devices using PROM boot parameters. 246 */ 247 248 /* 249 * Network device: Just use controller number. 250 */ 251 static struct device * 252 net_find(char *name, int ctlr, int unit) 253 { 254 char tname[16]; 255 256 sprintf(tname, "%s%d", name, ctlr); 257 return (find_dev_byname(tname)); 258 } 259 260 #if NSCSIBUS > 0 261 /* 262 * SCSI device: The controller number corresponds to the 263 * scsibus number, and the unit number is (targ*8 + LUN). 264 */ 265 static struct device * 266 scsi_find(char *name, int ctlr, int unit) 267 { 268 struct device *scsibus; 269 struct scsibus_softc *sbsc; 270 struct scsipi_periph *periph; 271 int target, lun; 272 char tname[16]; 273 274 sprintf(tname, "scsibus%d", ctlr); 275 scsibus = find_dev_byname(tname); 276 if (scsibus == NULL) 277 return (NULL); 278 279 /* Compute SCSI target/LUN from PROM unit. */ 280 target = (unit >> 3) & 7; 281 lun = unit & 7; 282 283 /* Find the device at this target/LUN */ 284 sbsc = (struct scsibus_softc *)scsibus; 285 periph = scsipi_lookup_periph(sbsc->sc_channel, target, lun); 286 if (periph == NULL) 287 return (NULL); 288 289 return (periph->periph_dev); 290 } 291 #endif /* NSCSIBUS > 0 */ 292 293 /* 294 * Xylogics SMD disk: (xy, xd) 295 * Assume wired-in unit numbers for now... 296 */ 297 static struct device * 298 xx_find(char *name, int ctlr, int unit) 299 { 300 int diskunit; 301 char tname[16]; 302 303 diskunit = (ctlr * 2) + unit; 304 sprintf(tname, "%s%d", name, diskunit); 305 return (find_dev_byname(tname)); 306 } 307 308 /* 309 * Given a device name, find its struct device 310 * XXX - Move this to some common file? 311 */ 312 static struct device * 313 find_dev_byname(char *name) 314 { 315 struct device *dv; 316 317 for (dv = alldevs.tqh_first; dv != NULL; 318 dv = dv->dv_list.tqe_next) { 319 if (!strcmp(dv->dv_xname, name)) { 320 return(dv); 321 } 322 } 323 return (NULL); 324 } 325 326 /* 327 * Misc. helpers used by driver match/attach functions. 328 */ 329 330 /* 331 * Read addr with size len (1,2,4) into val. 332 * If this generates a bus error, return -1 333 * 334 * Create a temporary mapping, 335 * Try the access using peek_* 336 * Clean up temp. mapping 337 */ 338 int 339 bus_peek(int bustype, int pa, int sz) 340 { 341 caddr_t va; 342 int rv; 343 344 va = bus_tmapin(bustype, pa); 345 switch (sz) { 346 case 1: 347 rv = peek_byte(va); 348 break; 349 case 2: 350 rv = peek_word(va); 351 break; 352 case 4: 353 rv = peek_long(va); 354 break; 355 default: 356 printf(" bus_peek: invalid size=%d\n", sz); 357 rv = -1; 358 } 359 bus_tmapout(va); 360 361 return (rv); 362 } 363 364 /* from hp300: badbaddr() */ 365 int 366 peek_byte(caddr_t addr) 367 { 368 label_t faultbuf; 369 int x; 370 371 nofault = &faultbuf; 372 if (setjmp(&faultbuf)) 373 x = -1; 374 else 375 x = *(volatile u_char *)addr; 376 377 nofault = NULL; 378 return(x); 379 } 380 381 int 382 peek_word(caddr_t addr) 383 { 384 label_t faultbuf; 385 int x; 386 387 nofault = &faultbuf; 388 if (setjmp(&faultbuf)) 389 x = -1; 390 else 391 x = *(volatile u_short *)addr; 392 393 nofault = NULL; 394 return(x); 395 } 396 397 int 398 peek_long(caddr_t addr) 399 { 400 label_t faultbuf; 401 int x; 402 403 nofault = &faultbuf; 404 if (setjmp(&faultbuf)) 405 x = -1; 406 else { 407 x = *(volatile int *)addr; 408 if (x == -1) { 409 printf("peek_long: uh-oh, actually read -1!\n"); 410 x &= 0x7FFFffff; /* XXX */ 411 } 412 } 413 414 nofault = NULL; 415 return(x); 416 } 417