1 /* $NetBSD: ahc_isa.c,v 1.38 2010/02/28 15:52:16 snj Exp $ */ 2 3 /* 4 * Product specific probe and attach routines for: 5 * AHA-284X VL-bus SCSI controllers 6 * 7 * Copyright (c) 1994, 1995, 1996, 1997, 1998 Justin T. Gibbs. 8 * All rights reserved. 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 immediately at the beginning of the file, without modification, 15 * this list of conditions, and the following disclaimer. 16 * 2. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: src/sys/dev/aic7xxx/ahc_eisa.c,v 1.15 2000/01/29 14:22:19 peter Exp $ 32 */ 33 34 /*- 35 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 36 * All rights reserved. 37 * 38 * This code is derived from software contributed to The NetBSD Foundation 39 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 40 * NASA Ames Research Center. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 52 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 54 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 61 * POSSIBILITY OF SUCH DAMAGE. 62 */ 63 64 /* 65 * Copyright (c) 1995, 1996 Christopher G. Demetriou 66 * All rights reserved. 67 * 68 * Redistribution and use in source and binary forms, with or without 69 * modification, are permitted provided that the following conditions 70 * are met: 71 * 1. Redistributions of source code must retain the above copyright 72 * notice, this list of conditions and the following disclaimer. 73 * 2. Redistributions in binary form must reproduce the above copyright 74 * notice, this list of conditions and the following disclaimer in the 75 * documentation and/or other materials provided with the distribution. 76 * 3. All advertising materials mentioning features or use of this software 77 * must display the following acknowledgement: 78 * This product includes software developed by Christopher G. Demetriou 79 * for the NetBSD Project. 80 * 4. The name of the author may not be used to endorse or promote products 81 * derived from this software without specific prior written permission 82 * 83 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 84 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 85 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 86 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 87 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 88 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 89 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 90 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 91 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 92 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 93 */ 94 95 /* 96 * This front-end driver is really sort of a hack. The AHA-284X likes 97 * to masquerade as an EISA device. However, on VLbus machines with 98 * no EISA signature in the BIOS, the EISA bus will never be scanned. 99 * This is intended to catch the 284X controllers on those systems 100 * by looking in "EISA i/o space" for 284X controllers. 101 * 102 * This relies heavily on i/o port accounting. We also just use the 103 * EISA macros for everything ... it's a real waste to redefine them. 104 * 105 * Note: there isn't any #ifdef for FreeBSD in this file, since the 106 * FreeBSD EISA driver handles all cases of the 284X. 107 * 108 * -- Jason R. Thorpe <thorpej@NetBSD.org> 109 * July 12, 1996 110 */ 111 112 #include <sys/cdefs.h> 113 __KERNEL_RCSID(0, "$NetBSD: ahc_isa.c,v 1.38 2010/02/28 15:52:16 snj Exp $"); 114 115 #include <sys/param.h> 116 #include <sys/systm.h> 117 #include <sys/kernel.h> 118 #include <sys/device.h> 119 #include <sys/queue.h> 120 #include <sys/malloc.h> 121 #include <sys/reboot.h> 122 123 #include <machine/bus.h> 124 #include <machine/intr.h> 125 126 #include <dev/scsipi/scsi_all.h> 127 #include <dev/scsipi/scsipi_all.h> 128 #include <dev/scsipi/scsiconf.h> 129 130 #include <dev/isa/isavar.h> 131 132 #include <dev/eisa/eisareg.h> 133 #include <dev/eisa/eisavar.h> 134 #include <dev/eisa/eisadevs.h> 135 136 #include <dev/ic/aic7xxx_osm.h> 137 #include <dev/ic/aic7xxx_inline.h> 138 #include <dev/ic/aic77xxreg.h> 139 #include <dev/ic/aic77xxvar.h> 140 #include <dev/ic/smc93cx6var.h> 141 142 /* IO port address setting range as EISA slot number */ 143 #define AHC_ISA_MIN_SLOT 0x1 /* from iobase = 0x1c00 */ 144 #define AHC_ISA_MAX_SLOT 0xe /* to iobase = 0xec00 */ 145 146 #define AHC_ISA_SLOT_OFFSET AHC_EISA_SLOT_OFFSET 147 #define AHC_ISA_IOSIZE AHC_EISA_IOSIZE 148 149 /* 150 * I/O port offsets 151 */ 152 #define AHC_ISA_VID (EISA_SLOTOFF_VID - AHC_ISA_SLOT_OFFSET) 153 #define AHC_ISA_PID (EISA_SLOTOFF_PID - AHC_ISA_SLOT_OFFSET) 154 #define AHC_ISA_PRIMING AHC_ISA_VID /* enable vendor/product ID */ 155 156 /* 157 * AHC_ISA_PRIMING register values (write) 158 */ 159 #define AHC_ISA_PRIMING_VID(index) (AHC_ISA_VID + (index)) 160 #define AHC_ISA_PRIMING_PID(index) (AHC_ISA_PID + (index)) 161 162 int ahc_isa_idstring(bus_space_tag_t, bus_space_handle_t, char *); 163 int ahc_isa_match(struct isa_attach_args *, bus_addr_t); 164 165 int ahc_isa_probe(device_t, cfdata_t, void *); 166 void ahc_isa_attach(device_t, device_t, void *); 167 void aha2840_load_seeprom(struct ahc_softc *ahc); 168 static int verify_seeprom_cksum(struct seeprom_config *sc); 169 170 CFATTACH_DECL_NEW(ahc_isa, sizeof(struct ahc_softc), 171 ahc_isa_probe, ahc_isa_attach, NULL, NULL); 172 173 /* 174 * This keeps track of which slots are to be checked next if the 175 * iobase locator is a wildcard. A simple static variable isn't enough, 176 * since it's conceivable that a system might have more than one ISA 177 * bus. 178 * 179 * The "bus" member is the unit number of the parent ISA bus, e.g. "0" 180 * for "isa0". 181 */ 182 struct ahc_isa_slot { 183 LIST_ENTRY(ahc_isa_slot) link; 184 int bus; 185 int slot; 186 }; 187 static LIST_HEAD(, ahc_isa_slot) ahc_isa_all_slots; 188 static int ahc_isa_slot_initialized; 189 190 int 191 ahc_isa_idstring(bus_space_tag_t iot, bus_space_handle_t ioh, char *idstring) 192 { 193 uint8_t vid[EISA_NVIDREGS], pid[EISA_NPIDREGS]; 194 int i; 195 196 /* Get the vendor ID bytes */ 197 for (i = 0; i < EISA_NVIDREGS; i++) { 198 bus_space_write_1(iot, ioh, AHC_ISA_PRIMING, 199 AHC_ISA_PRIMING_VID(i)); 200 vid[i] = bus_space_read_1(iot, ioh, AHC_ISA_VID + i); 201 } 202 203 /* Check for device existence */ 204 if (EISA_VENDID_NODEV(vid)) { 205 #if 0 206 aprint_error("ahc_isa_idstring: no device at 0x%lx\n", 207 ioh); /* XXX knows about ioh guts */ 208 aprint_error("\t(0x%x, 0x%x)\n", vid[0], vid[1]); 209 #endif 210 return (0); 211 } 212 213 /* And check that the firmware didn't biff something badly */ 214 if (EISA_VENDID_IDDELAY(vid)) { 215 aprint_error("ahc_isa_idstring: BIOS biffed it at 0x%lx\n", 216 ioh); /* XXX knows about ioh guts */ 217 return (0); 218 } 219 220 /* Get the product ID bytes */ 221 for (i = 0; i < EISA_NPIDREGS; i++) { 222 bus_space_write_1(iot, ioh, AHC_ISA_PRIMING, 223 AHC_ISA_PRIMING_PID(i)); 224 pid[i] = bus_space_read_1(iot, ioh, AHC_ISA_PID + i); 225 } 226 227 /* Create the ID string from the vendor and product IDs */ 228 idstring[0] = EISA_VENDID_0(vid); 229 idstring[1] = EISA_VENDID_1(vid); 230 idstring[2] = EISA_VENDID_2(vid); 231 idstring[3] = EISA_PRODID_0(pid); 232 idstring[4] = EISA_PRODID_1(pid); 233 idstring[5] = EISA_PRODID_2(pid); 234 idstring[6] = EISA_PRODID_3(pid); 235 idstring[7] = '\0'; /* sanity */ 236 237 return (1); 238 } 239 240 int 241 ahc_isa_match(struct isa_attach_args *ia, bus_addr_t iobase) 242 { 243 bus_space_tag_t iot = ia->ia_iot; 244 bus_space_handle_t ioh; 245 int irq; 246 char idstring[EISA_IDSTRINGLEN]; 247 248 /* 249 * Get a mapping for the while slot-specific address 250 * space. If we can't, assume nothing's there, but 251 * warn about it. 252 */ 253 if (bus_space_map(iot, iobase, AHC_ISA_IOSIZE, 0, &ioh)) { 254 #if 0 255 /* 256 * Don't print anything out here, since this could 257 * be common on machines configured to look for 258 * ahc_eisa and ahc_isa. 259 */ 260 aprint_error("ahc_isa_match: can't map I/O space for 0x%x\n", 261 iobase); 262 #endif 263 return (0); 264 } 265 266 if (!ahc_isa_idstring(iot, ioh, idstring)) 267 irq = -1; /* cannot get the ID string */ 268 else if (strcmp(idstring, "ADP7756") && 269 strcmp(idstring, "ADP7757")) 270 irq = -1; /* unknown ID strings */ 271 else 272 irq = ahc_aic77xx_irq(iot, ioh); 273 274 bus_space_unmap(iot, ioh, AHC_ISA_IOSIZE); 275 276 if (irq < 0) 277 return (0); 278 279 if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ && 280 ia->ia_irq[0].ir_irq != irq) { 281 aprint_error("ahc_isa_match: irq mismatch (kernel %d, card %d)\n", 282 ia->ia_irq[0].ir_irq, irq); 283 return (0); 284 } 285 286 /* We have a match */ 287 ia->ia_nio = 1; 288 ia->ia_io[0].ir_addr = iobase; 289 ia->ia_io[0].ir_size = AHC_ISA_IOSIZE; 290 291 ia->ia_nirq = 1; 292 ia->ia_irq[0].ir_irq = irq; 293 294 ia->ia_ndrq = 0; 295 ia->ia_niomem = 0; 296 297 return (1); 298 } 299 300 /* 301 * Check the slots looking for a board we recognise 302 * If we find one, note it's address (slot) and call 303 * the actual probe routine to check it out. 304 */ 305 int 306 ahc_isa_probe(device_t parent, cfdata_t match, void *aux) 307 { 308 struct isa_attach_args *ia = aux; 309 struct ahc_isa_slot *as; 310 311 if (ahc_isa_slot_initialized == 0) { 312 LIST_INIT(&ahc_isa_all_slots); 313 ahc_isa_slot_initialized = 1; 314 } 315 316 if (ia->ia_nio < 1) 317 return (0); 318 if (ia->ia_nirq < 1) 319 return (0); 320 321 if (ISA_DIRECT_CONFIG(ia)) 322 return (0); 323 324 if (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) 325 return (ahc_isa_match(ia, ia->ia_io[0].ir_addr)); 326 327 /* 328 * Find this bus's state. If we don't yet have a slot 329 * marker, allocate and initialize one. 330 */ 331 for (as = ahc_isa_all_slots.lh_first; as != NULL; 332 as = as->link.le_next) 333 if (as->bus == device_unit(parent)) 334 goto found_slot_marker; 335 336 /* 337 * Don't have one, so make one. 338 */ 339 as = (struct ahc_isa_slot *) 340 malloc(sizeof(struct ahc_isa_slot), M_DEVBUF, M_NOWAIT); 341 if (as == NULL) 342 panic("ahc_isa_probe: can't allocate slot marker"); 343 344 as->bus = device_unit(parent); 345 as->slot = AHC_ISA_MIN_SLOT; 346 LIST_INSERT_HEAD(&ahc_isa_all_slots, as, link); 347 348 found_slot_marker: 349 350 for (; as->slot <= AHC_ISA_MAX_SLOT; as->slot++) { 351 if (ahc_isa_match(ia, EISA_SLOT_ADDR(as->slot) + 352 AHC_ISA_SLOT_OFFSET)) { 353 as->slot++; /* next slot to search */ 354 return (1); 355 } 356 } 357 358 /* No matching cards were found. */ 359 return (0); 360 } 361 362 void 363 ahc_isa_attach(device_t parent, device_t self, void *aux) 364 { 365 struct ahc_softc *ahc = device_private(self); 366 struct isa_attach_args *ia = aux; 367 bus_space_tag_t iot = ia->ia_iot; 368 bus_space_handle_t ioh; 369 int irq, intrtype; 370 const char *intrtypestr; 371 char idstring[EISA_IDSTRINGLEN]; 372 u_char intdef; 373 374 ahc->sc_dev = self; 375 aprint_naive(": SCSI controller\n"); 376 377 if (bus_space_map(iot, ia->ia_io[0].ir_addr, ia->ia_io[0].ir_size, 378 0, &ioh)) { 379 aprint_error(": can't map i/o space\n"); 380 return; 381 } 382 if (!ahc_isa_idstring(iot, ioh, idstring)) { 383 aprint_error(": can't read ID string\n"); 384 goto free_io; 385 } 386 if ((irq = ahc_aic77xx_irq(iot, ioh)) < 0) { 387 aprint_error(": ahc_aic77xx_irq failed\n"); 388 goto free_io; 389 } 390 391 if (strcmp(idstring, "ADP7756") == 0) { 392 aprint_normal(": %s\n", EISA_PRODUCT_ADP7756); 393 } else if (strcmp(idstring, "ADP7757") == 0) { 394 aprint_normal(": %s\n", EISA_PRODUCT_ADP7757); 395 } else { 396 aprint_error(": unknown device type %s\n", idstring); 397 goto free_io; 398 } 399 400 /* 401 * Tell the bus-DMA interface that we can do 32bit DMA 402 * NOTE: this variable is first referenced in ahc_init(). 403 */ 404 ahc->sc_dmaflags = ISABUS_DMA_32BIT; 405 406 ahc_set_name(ahc, device_xname(ahc->sc_dev)); 407 ahc->parent_dmat = ia->ia_dmat; 408 ahc->channel = 'A'; 409 ahc->chip = AHC_AIC7770|AHC_VL; 410 ahc->features = AHC_AIC7770_FE; 411 ahc->bugs |= AHC_TMODE_WIDEODD_BUG; 412 ahc->flags |= AHC_PAGESCBS; 413 ahc->tag = iot; 414 ahc->bsh = ioh; 415 416 if (ahc_softc_init(ahc) != 0) 417 goto free_io; 418 419 ahc_intr_enable(ahc, false); 420 421 if (ahc_reset(ahc) != 0) 422 goto free_io; 423 424 intdef = bus_space_read_1(iot, ioh, INTDEF); 425 426 if (intdef & EDGE_TRIG) { 427 intrtype = IST_EDGE; 428 intrtypestr = "edge triggered"; 429 } else { 430 intrtype = IST_LEVEL; 431 intrtypestr = "level sensitive"; 432 } 433 ahc->ih = isa_intr_establish(ia->ia_ic, irq, 434 intrtype, IPL_BIO, ahc_intr, ahc); 435 if (ahc->ih == NULL) { 436 aprint_error_dev(ahc->sc_dev, "couldn't establish %s interrupt\n", 437 intrtypestr); 438 goto free_io; 439 } 440 441 /* 442 * Tell the user what type of interrupts we're using. 443 * useful for debugging irq problems 444 */ 445 if (bootverbose) { 446 aprint_verbose_dev(ahc->sc_dev, "Using %s interrupts\n", 447 intrtypestr); 448 } 449 450 /* 451 * Now that we know we own the resources we need, do the 452 * card initialization. 453 */ 454 aha2840_load_seeprom(ahc); 455 456 /* Attach sub-devices */ 457 if (ahc_aic77xx_attach(ahc) == 0) 458 return; /* succeed */ 459 460 /* failed */ 461 isa_intr_disestablish(ia->ia_ic, ahc->ih); 462 free_io: 463 bus_space_unmap(iot, ioh, ia->ia_io[0].ir_size); 464 } 465 466 /* 467 * Read the 284x SEEPROM. 468 */ 469 void 470 aha2840_load_seeprom(struct ahc_softc *ahc) 471 { 472 struct seeprom_descriptor sd; 473 struct seeprom_config sc; 474 uint8_t scsi_conf; 475 int have_seeprom; 476 477 sd.sd_tag = ahc->tag; 478 sd.sd_bsh = ahc->bsh; 479 sd.sd_control_offset = SEECTL_2840; 480 sd.sd_status_offset = STATUS_2840; 481 sd.sd_dataout_offset = STATUS_2840; 482 sd.sd_chip = C46; 483 sd.sd_MS = 0; 484 sd.sd_RDY = EEPROM_TF; 485 sd.sd_CS = CS_2840; 486 sd.sd_CK = CK_2840; 487 sd.sd_DO = DO_2840; 488 sd.sd_DI = DI_2840; 489 490 if (bootverbose) 491 aprint_verbose("%s: Reading SEEPROM...", ahc_name(ahc)); 492 have_seeprom = read_seeprom(&sd, (uint16_t *)&sc, 493 /*start_addr*/0, sizeof(sc)/2); 494 495 if (have_seeprom) { 496 if (verify_seeprom_cksum(&sc) == 0) { 497 if(bootverbose) 498 aprint_verbose ("checksum error\n"); 499 have_seeprom = 0; 500 } else if (bootverbose) { 501 aprint_verbose("done.\n"); 502 } 503 } 504 505 if (!have_seeprom) { 506 if (bootverbose) 507 aprint_verbose("%s: No SEEPROM available\n", 508 ahc_name(ahc)); 509 ahc->flags |= AHC_USEDEFAULTS; 510 } else { 511 /* 512 * Put the data we've collected down into SRAM 513 * where ahc_init will find it. 514 */ 515 int i; 516 int max_targ = (ahc->features & AHC_WIDE) != 0 ? 16 : 8; 517 uint16_t discenable; 518 519 discenable = 0; 520 for (i = 0; i < max_targ; i++){ 521 uint8_t target_settings; 522 target_settings = (sc.device_flags[i] & CFXFER) << 4; 523 if (sc.device_flags[i] & CFSYNCH) 524 target_settings |= SOFS; 525 if (sc.device_flags[i] & CFWIDEB) 526 target_settings |= WIDEXFER; 527 if (sc.device_flags[i] & CFDISC) 528 discenable |= (0x01 << i); 529 ahc_outb(ahc, TARG_SCSIRATE + i, target_settings); 530 } 531 ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff)); 532 ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff)); 533 534 ahc->our_id = sc.brtime_id & CFSCSIID; 535 536 scsi_conf = (ahc->our_id & 0x7); 537 if (sc.adapter_control & CFSPARITY) 538 scsi_conf |= ENSPCHK; 539 if (sc.adapter_control & CFRESETB) 540 scsi_conf |= RESET_SCSI; 541 542 if (sc.bios_control & CF284XEXTEND) 543 ahc->flags |= AHC_EXTENDED_TRANS_A; 544 /* Set SCSICONF info */ 545 ahc_outb(ahc, SCSICONF, scsi_conf); 546 547 if (sc.adapter_control & CF284XSTERM) 548 ahc->flags |= AHC_TERM_ENB_A; 549 } 550 } 551 552 static int 553 verify_seeprom_cksum(struct seeprom_config *sc) 554 { 555 int i; 556 int maxaddr; 557 uint32_t checksum; 558 uint16_t *scarray; 559 560 maxaddr = (sizeof(*sc)/2) - 1; 561 checksum = 0; 562 scarray = (uint16_t *)sc; 563 564 for (i = 0; i < maxaddr; i++) 565 checksum = checksum + scarray[i]; 566 if (checksum == 0 567 || (checksum & 0xFFFF) != sc->checksum) { 568 return (0); 569 } else { 570 return(1); 571 } 572 } 573