Home | History | Annotate | Line # | Download | only in eisa
ahc_eisa.c revision 1.3
      1 /*
      2  * Product specific probe and attach routines for:
      3  * 	27/284X and aic7770 motherboard SCSI controllers
      4  *
      5  * Copyright (c) 1994, 1995, 1996 Justin T. Gibbs.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice immediately at the beginning of the file, without modification,
     13  *    this list of conditions, and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  *	$Id: ahc_eisa.c,v 1.3 1996/05/16 06:39:34 mycroft Exp $
     33  */
     34 
     35 #if defined(__FreeBSD__)
     36 #include <eisa.h>
     37 #endif
     38 #if NEISA > 0 || defined(__NetBSD__)
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #if defined(__FreeBSD__)
     43 #include <sys/devconf.h>
     44 #endif
     45 #include <sys/kernel.h>
     46 
     47 #if defined(__NetBSD__)
     48 #include <sys/device.h>
     49 #include <machine/bus.h>
     50 #include <machine/intr.h>
     51 #endif /* defined(__NetBSD__) */
     52 
     53 #include <scsi/scsi_all.h>
     54 #include <scsi/scsiconf.h>
     55 
     56 #if defined(__FreeBSD__)
     57 
     58 #include <machine/clock.h>
     59 
     60 #include <i386/eisa/eisaconf.h>
     61 #include <i386/scsi/aic7xxx.h>
     62 #include <dev/aic7xxx/aic7xxx_reg.h>
     63 
     64 #define EISA_DEVICE_ID_ADAPTEC_AIC7770	0x04907770
     65 #define EISA_DEVICE_ID_ADAPTEC_274x	0x04907771
     66 #define EISA_DEVICE_ID_ADAPTEC_284xB	0x04907756 /* BIOS enabled */
     67 #define EISA_DEVICE_ID_ADAPTEC_284x	0x04907757 /* BIOS disabled*/
     68 
     69 #elif defined(__NetBSD__)
     70 
     71 #include <dev/eisa/eisareg.h>
     72 #include <dev/eisa/eisavar.h>
     73 #include <dev/eisa/eisadevs.h>
     74 
     75 #include <dev/ic/aic7xxxreg.h>
     76 #include <dev/ic/aic7xxxvar.h>
     77 
     78 #endif /* defined(__NetBSD__) */
     79 
     80 #define AHC_EISA_SLOT_OFFSET	0xc00
     81 #define AHC_EISA_IOSIZE		0x100
     82 #define INTDEF			0x5cul	/* Interrupt Definition Register */
     83 
     84 #if defined(__FreeBSD__)
     85 
     86 static int	aic7770probe __P((void));
     87 static int	aic7770_attach __P((struct eisa_device *e_dev));
     88 
     89 static struct eisa_driver ahc_eisa_driver = {
     90 					"ahc",
     91 					aic7770probe,
     92 					aic7770_attach,
     93 					/*shutdown*/NULL,
     94 					&ahc_unit
     95 				      };
     96 
     97 DATA_SET (eisadriver_set, ahc_eisa_driver);
     98 
     99 static struct kern_devconf kdc_aic7770 = {
    100 	0, 0, 0,                /* filled in by dev_attach */
    101 	"ahc", 0, { MDDT_EISA, 0, "bio" },
    102 	eisa_generic_externalize, 0, 0, EISA_EXTERNALLEN,
    103 	&kdc_eisa0,		/* parent */
    104 	0,			/* parentdata */
    105 	DC_UNCONFIGURED,	/* always start out here */
    106 	NULL,
    107 	DC_CLS_MISC		/* host adapters aren't special */
    108 };
    109 
    110 
    111 static char	*aic7770_match __P((eisa_id_t type));
    112 
    113 static  char*
    114 aic7770_match(type)
    115 	eisa_id_t type;
    116 {
    117 	switch(type) {
    118 		case EISA_DEVICE_ID_ADAPTEC_AIC7770:
    119 			return ("Adaptec aic7770 SCSI host adapter");
    120 			break;
    121 		case EISA_DEVICE_ID_ADAPTEC_274x:
    122 			return ("Adaptec 274X SCSI host adapter");
    123 			break;
    124 		case EISA_DEVICE_ID_ADAPTEC_284xB:
    125 		case EISA_DEVICE_ID_ADAPTEC_284x:
    126 			return ("Adaptec 284X SCSI host adapter");
    127 			break;
    128 		default:
    129 			break;
    130 	}
    131 	return (NULL);
    132 }
    133 
    134 static int
    135 aic7770probe(void)
    136 {
    137 	u_long iobase;
    138 	char intdef;
    139 	u_long irq;
    140 	struct eisa_device *e_dev = NULL;
    141 	int count;
    142 
    143 	count = 0;
    144 	while ((e_dev = eisa_match_dev(e_dev, aic7770_match))) {
    145 		iobase = (e_dev->ioconf.slot * EISA_SLOT_SIZE)
    146 			 + AHC_EISA_SLOT_OFFSET;
    147 		ahc_reset(iobase);
    148 
    149 		eisa_add_iospace(e_dev, iobase, AHC_EISA_IOSIZE, RESVADDR_NONE);
    150 		intdef = inb(INTDEF + iobase);
    151 		switch (intdef & 0xf) {
    152 			case 9:
    153 				irq = 9;
    154 				break;
    155 			case 10:
    156 				irq = 10;
    157 				break;
    158 			case 11:
    159 				irq = 11;
    160 				break;
    161 			case 12:
    162 				irq = 12;
    163 				break;
    164 			case 14:
    165 				irq = 14;
    166 				break;
    167 			case 15:
    168 				irq = 15;
    169 				break;
    170 			default:
    171 				printf("aic7770 at slot %d: illegal "
    172 				       "irq setting %d\n", e_dev->ioconf.slot,
    173 					intdef);
    174 				continue;
    175 		}
    176 		eisa_add_intr(e_dev, irq);
    177 		eisa_registerdev(e_dev, &ahc_eisa_driver, &kdc_aic7770);
    178 		if(e_dev->id == EISA_DEVICE_ID_ADAPTEC_284xB
    179 		   || e_dev->id == EISA_DEVICE_ID_ADAPTEC_284x) {
    180 			/* Our real parent is the isa bus.  Say so. */
    181 			e_dev->kdc->kdc_parent = &kdc_isa0;
    182 		}
    183 		count++;
    184 	}
    185 	return count;
    186 }
    187 
    188 #elif defined(__NetBSD__)
    189 
    190 #define bootverbose	1
    191 
    192 int	ahc_eisa_match __P((struct device *, void *, void *));
    193 void	ahc_eisa_attach __P((struct device *, struct device *, void *));
    194 
    195 
    196 struct cfattach ahc_eisa_ca = {
    197 	sizeof(struct ahc_data), ahc_eisa_match, ahc_eisa_attach
    198 };
    199 
    200 /*
    201  * Return irq setting of the board, otherwise -1.
    202  */
    203 int
    204 ahc_eisa_irq(bc, ioh)
    205 	bus_chipset_tag_t bc;
    206 	bus_io_handle_t ioh;
    207 {
    208 	int irq;
    209 	u_char intdef;
    210 
    211 	ahc_reset("ahc_eisa", bc, ioh);
    212 	intdef = bus_io_read_1(bc, ioh, INTDEF);
    213 	switch (irq = (intdef & 0xf)) {
    214 	case 9:
    215 	case 10:
    216 	case 11:
    217 	case 12:
    218 	case 14:
    219 	case 15:
    220 		break;
    221 	default:
    222 		printf("ahc_eisa_irq: illegal irq setting %d\n", intdef);
    223 		return -1;
    224 	}
    225 
    226 	/* Note that we are going and return (to probe) */
    227 	return irq;
    228 }
    229 
    230 /*
    231  * Check the slots looking for a board we recognise
    232  * If we find one, note it's address (slot) and call
    233  * the actual probe routine to check it out.
    234  */
    235 int
    236 ahc_eisa_match(parent, match, aux)
    237 	struct device *parent;
    238 	void *match, *aux;
    239 {
    240 	struct eisa_attach_args *ea = aux;
    241 	bus_chipset_tag_t bc = ea->ea_bc;
    242 	bus_io_handle_t ioh;
    243 	int irq;
    244 
    245 	/* must match one of our known ID strings */
    246 	if (strcmp(ea->ea_idstring, "ADP7770") &&
    247 	    strcmp(ea->ea_idstring, "ADP7771") &&
    248 	    strcmp(ea->ea_idstring, "ADP7756") && /* XXX - not EISA, but VL */
    249 	    strcmp(ea->ea_idstring, "ADP7757"))	  /* XXX - not EISA, but VL */
    250 		return (0);
    251 
    252 	if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + AHC_EISA_SLOT_OFFSET,
    253 	    AHC_EISA_IOSIZE, &ioh))
    254 		return (0);
    255 
    256 	irq = ahc_eisa_irq(bc, ioh);
    257 
    258 	bus_io_unmap(bc, ioh, AHC_EISA_IOSIZE);
    259 
    260 	return (irq >= 0);
    261 }
    262 
    263 #endif /* defined(__NetBSD__) */
    264 
    265 #if defined(__FreeBSD__)
    266 static int
    267 aic7770_attach(e_dev)
    268 	struct eisa_device *e_dev;
    269 #elif defined(__NetBSD__)
    270 void
    271 ahc_eisa_attach(parent, self, aux)
    272 	struct device *parent, *self;
    273 	void *aux;
    274 #endif
    275 {
    276 	ahc_type type;
    277 
    278 #if defined(__FreeBSD__)
    279 	struct ahc_data *ahc;
    280 	resvaddr_t *iospace;
    281 	int unit = e_dev->unit;
    282 	int irq = ffs(e_dev->ioconf.irq) - 1;
    283 
    284 	iospace = e_dev->ioconf.ioaddrs.lh_first;
    285 
    286 	if(!iospace)
    287 		return -1;
    288 
    289 	switch(e_dev->id) {
    290 		case EISA_DEVICE_ID_ADAPTEC_AIC7770:
    291 			type = AHC_AIC7770;
    292 			break;
    293 		case EISA_DEVICE_ID_ADAPTEC_274x:
    294 			type = AHC_274;
    295 			break;
    296 		case EISA_DEVICE_ID_ADAPTEC_284xB:
    297 		case EISA_DEVICE_ID_ADAPTEC_284x:
    298 			type = AHC_284;
    299 			break;
    300 		default:
    301 			printf("aic7770_attach: Unknown device type!\n");
    302 			return -1;
    303 			break;
    304 	}
    305 
    306 	if(!(ahc = ahc_alloc(unit, iospace->addr, type, AHC_FNONE)))
    307 		return -1;
    308 
    309 	eisa_reg_start(e_dev);
    310 	if(eisa_reg_iospace(e_dev, iospace)) {
    311 		ahc_free(ahc);
    312 		return -1;
    313 	}
    314 
    315 	/*
    316 	 * The IRQMS bit enables level sensitive interrupts. Only allow
    317 	 * IRQ sharing if it's set.
    318 	 */
    319 	if(eisa_reg_intr(e_dev, irq, ahc_intr, (void *)ahc, &bio_imask,
    320 			 /*shared ==*/ahc->pause & IRQMS)) {
    321 		ahc_free(ahc);
    322 		return -1;
    323 	}
    324 	eisa_reg_end(e_dev);
    325 
    326 #elif defined(__NetBSD__)
    327 
    328 	struct ahc_data *ahc = (void *)self;
    329 	struct eisa_attach_args *ea = aux;
    330 	bus_chipset_tag_t bc = ea->ea_bc;
    331 	bus_io_handle_t ioh;
    332 	int irq;
    333 	eisa_chipset_tag_t ec = ea->ea_ec;
    334 	eisa_intr_handle_t ih;
    335 	const char *model, *intrstr;
    336 
    337 	if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + AHC_EISA_SLOT_OFFSET,
    338 		       AHC_EISA_IOSIZE, &ioh))
    339 		panic("ahc_eisa_attach: could not map I/O addresses");
    340 	if ((irq = ahc_eisa_irq(bc, ioh)) < 0)
    341 		panic("ahc_eisa_attach: ahc_eisa_irq failed!");
    342 
    343 	if (strcmp(ea->ea_idstring, "ADP7770") == 0) {
    344 		model = EISA_PRODUCT_ADP7770;
    345 		type = AHC_AIC7770;
    346 	} else if (strcmp(ea->ea_idstring, "ADP7771") == 0) {
    347 		model = EISA_PRODUCT_ADP7771;
    348 		type = AHC_274;
    349 	} else if (strcmp(ea->ea_idstring, "ADP7756") == 0) {
    350 		model = EISA_PRODUCT_ADP7756;
    351 		type = AHC_284;
    352 	} else if (strcmp(ea->ea_idstring, "ADP7757") == 0) {
    353 		model = EISA_PRODUCT_ADP7757;
    354 		type = AHC_284;
    355 	} else {
    356 		panic("ahc_eisa_attach: Unknown device type %s\n",
    357 		      ea->ea_idstring);
    358 	}
    359 	printf(": %s\n", model);
    360 
    361 	ahc_construct(ahc, bc, ioh, type, AHC_FNONE);
    362 	if (eisa_intr_map(ec, irq, &ih)) {
    363 		printf("%s: couldn't map interrupt (%d)\n",
    364 		       ahc->sc_dev.dv_xname, irq);
    365 		return;
    366 	}
    367 #endif /* defined(__NetBSD__) */
    368 
    369 	/*
    370 	 * Tell the user what type of interrupts we're using.
    371 	 * usefull for debugging irq problems
    372 	 */
    373 	if(bootverbose) {
    374 		printf(
    375 		       "%s: Using %s Interrupts\n",
    376 		       ahc_name(ahc),
    377 		       ahc->pause & IRQMS ?
    378 				"Level Sensitive" : "Edge Triggered");
    379 	}
    380 
    381 	/*
    382 	 * Now that we know we own the resources we need, do the
    383 	 * card initialization.
    384 	 *
    385 	 * First, the aic7770 card specific setup.
    386 	 */
    387 	switch( ahc->type ) {
    388 	    case AHC_AIC7770:
    389 	    case AHC_274:
    390 	    {
    391 		u_char biosctrl = AHC_INB(ahc, HA_274_BIOSCTRL);
    392 
    393 		/* Get the primary channel information */
    394 		ahc->flags |= (biosctrl & CHANNEL_B_PRIMARY);
    395 
    396 		if((biosctrl & BIOSMODE) == BIOSDISABLED)
    397 			ahc->flags |= AHC_USEDEFAULTS;
    398 		break;
    399 	    }
    400 	    case AHC_284:
    401 	    {
    402 		/* XXX
    403 		 * All values are automagically intialized at
    404 		 * POST for these cards, so we can always rely
    405 		 * on the Scratch Ram values.  However, we should
    406 		 * read the SEEPROM here (Dan has the code to do
    407 		 * it) so we can say what kind of translation the
    408 		 * BIOS is using.  Printing out the geometry could
    409 		 * save a lot of users the grief of failed installs.
    410 		 */
    411 		break;
    412 	    }
    413 	    default:
    414 		break;
    415 	}
    416 
    417 	/*
    418 	 * See if we have a Rev E or higher aic7770. Anything below a
    419 	 * Rev E will have a R/O autoflush disable configuration bit.
    420 	 * It's still not clear exactly what is differenent about the Rev E.
    421 	 * We think it allows 8 bit entries in the QOUTFIFO to support
    422 	 * "paging" SCBs so you can have more than 4 commands active at
    423 	 * once.
    424 	 */
    425 	{
    426 		char *id_string;
    427 		u_char sblkctl;
    428 		u_char sblkctl_orig;
    429 
    430 		sblkctl_orig = AHC_INB(ahc, SBLKCTL);
    431 		sblkctl = sblkctl_orig ^ AUTOFLUSHDIS;
    432 		AHC_OUTB(ahc, SBLKCTL, sblkctl);
    433 		sblkctl = AHC_INB(ahc, SBLKCTL);
    434 		if(sblkctl != sblkctl_orig)
    435 		{
    436 			id_string = "aic7770 >= Rev E, ";
    437 			/*
    438 			 * Ensure autoflush is enabled
    439 			 */
    440 			sblkctl &= ~AUTOFLUSHDIS;
    441 			AHC_OUTB(ahc, SBLKCTL, sblkctl);
    442 
    443 			/* Allow paging on this adapter */
    444 			ahc->flags |= AHC_PAGESCBS;
    445 		}
    446 		else
    447 			id_string = "aic7770 <= Rev C, ";
    448 
    449 		printf("%s: %s", ahc_name(ahc), id_string);
    450 	}
    451 
    452 	/* Setup the FIFO threshold and the bus off time */
    453 	{
    454 		u_char hostconf = AHC_INB(ahc, HOSTCONF);
    455 		AHC_OUTB(ahc, BUSSPD, hostconf & DFTHRSH);
    456 		AHC_OUTB(ahc, BUSTIME, (hostconf << 2) & BOFF);
    457 	}
    458 
    459 	/*
    460 	 * Generic aic7xxx initialization.
    461 	 */
    462 	if(ahc_init(ahc)){
    463 #if defined(__FreeBSD__)
    464 		ahc_free(ahc);
    465 		/*
    466 		 * The board's IRQ line is not yet enabled so it's safe
    467 		 * to release the irq.
    468 		 */
    469 		eisa_release_intr(e_dev, irq, ahc_intr);
    470 		return -1;
    471 #elif defined(__NetBSD__)
    472 		ahc_free(ahc);
    473 		return;
    474 #endif
    475 	}
    476 
    477 	/*
    478 	 * Enable the board's BUS drivers
    479 	 */
    480 	AHC_OUTB(ahc, BCTL, ENABLE);
    481 
    482 #if defined(__FreeBSD__)
    483 	/*
    484 	 * Enable our interrupt handler.
    485 	 */
    486 	if(eisa_enable_intr(e_dev, irq)) {
    487 		ahc_free(ahc);
    488 		eisa_release_intr(e_dev, irq, ahc_intr);
    489 		return -1;
    490 	}
    491 
    492 	e_dev->kdc->kdc_state = DC_BUSY; /* host adapters always busy */
    493 #elif defined(__NetBSD__)
    494 	intrstr = eisa_intr_string(ec, ih);
    495 	/*
    496 	 * The IRQMS bit enables level sensitive interrupts only allow
    497 	 * IRQ sharing if its set.
    498 	 */
    499 	ahc->sc_ih = eisa_intr_establish(ec, ih,
    500 	    ahc->pause & IRQMS ? IST_LEVEL : IST_EDGE, IPL_BIO, ahc_intr, ahc
    501 #ifdef __OpenBSD__
    502 	    , ahc->sc_dev.dv_xname
    503 #endif
    504 	    );
    505 	if (ahc->sc_ih == NULL) {
    506 		printf("%s: couldn't establish interrupt",
    507 		       ahc->sc_dev.dv_xname);
    508 		if (intrstr != NULL)
    509 			printf(" at %s", intrstr);
    510 		printf("\n");
    511 		ahc_free(ahc);
    512 		return;
    513 	}
    514 	if (intrstr != NULL)
    515 		printf("%s: interrupting at %s\n", ahc->sc_dev.dv_xname,
    516 		       intrstr);
    517 #endif /* defined(__NetBSD__) */
    518 
    519 	/* Attach sub-devices - always succeeds */
    520 	ahc_attach(ahc);
    521 
    522 #if defined(__FreeBSD__)
    523 	return 0;
    524 #endif
    525 }
    526 
    527 #endif /* NEISA > 0 */
    528