Home | History | Annotate | Line # | Download | only in cardbus
ahc_cardbus.c revision 1.2
      1 /*	$NetBSD: ahc_cardbus.c,v 1.2 2000/03/15 02:03:51 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * CardBus front-end for the Adaptec AIC-7xxx family of SCSI controllers.
     42  *
     43  * TODO:
     44  *	- power management
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/malloc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/queue.h>
     52 #include <sys/device.h>
     53 
     54 #include <machine/bus.h>
     55 #include <machine/intr.h>
     56 
     57 #include <dev/scsipi/scsi_all.h>
     58 #include <dev/scsipi/scsipi_all.h>
     59 #include <dev/scsipi/scsiconf.h>
     60 
     61 #include <dev/pci/pcireg.h>
     62 
     63 #include <dev/cardbus/cardbusvar.h>
     64 #include <dev/cardbus/cardbusdevs.h>
     65 
     66 #include <dev/ic/aic7xxxvar.h>
     67 
     68 #define	AHC_CARDBUS_IOBA	0x10
     69 #define	AHC_CARDBUS_MMBA	0x14
     70 
     71 struct ahc_cardbus_softc {
     72 	struct ahc_softc sc_ahc;	/* real AHC */
     73 
     74 	/* CardBus-specific goo. */
     75 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
     76 	int	sc_intrline;		/* our interrupt line */
     77 	cardbustag_t sc_tag;
     78 
     79 	int	sc_cbenable;		/* what CardBus access type to enable */
     80 	int	sc_csr;			/* CSR bits */
     81 };
     82 
     83 int	ahc_cardbus_match __P((struct device *, struct cfdata *, void *));
     84 void	ahc_cardbus_attach __P((struct device *, struct device *, void *));
     85 
     86 struct cfattach ahc_cardbus_ca = {
     87 	sizeof(struct ahc_softc), ahc_cardbus_match, ahc_cardbus_attach,
     88 };
     89 
     90 int
     91 ahc_cardbus_match(parent, match, aux)
     92 	struct device *parent;
     93 	struct cfdata *match;
     94 	void *aux;
     95 {
     96 	struct cardbus_attach_args *ca = aux;
     97 
     98 	if (CARDBUS_VENDOR(ca->ca_id) == CARDBUS_VENDOR_ADP &&
     99 	    CARDBUS_PRODUCT(ca->ca_id) == CARDBUS_PRODUCT_ADP_1480)
    100 		return (1);
    101 
    102 	return (0);
    103 }
    104 
    105 void
    106 ahc_cardbus_attach(parent, self, aux)
    107 	struct device *parent, *self;
    108 	void *aux;
    109 {
    110 	struct cardbus_attach_args *ca = aux;
    111 	struct ahc_cardbus_softc *csc = (void *) self;
    112 	struct ahc_softc *ahc = &csc->sc_ahc;
    113 	cardbus_devfunc_t ct = ca->ca_ct;
    114 	cardbus_chipset_tag_t cc = ct->ct_cc;
    115 	cardbus_function_tag_t cf = ct->ct_cf;
    116 	bus_space_tag_t bst;
    117 	bus_space_handle_t bsh;
    118 	pcireg_t reg;
    119 	u_int sxfrctl1 = 0;
    120 	ahc_chip ahc_t = AHC_NONE;
    121 	ahc_feature ahc_fe = AHC_FENONE;
    122 	ahc_flag ahc_f = AHC_FNONE;
    123 
    124 
    125 	csc->sc_ct = ct;
    126 	csc->sc_tag = ca->ca_tag;
    127 	csc->sc_intrline = ca->ca_intrline;
    128 
    129 	printf(": Adaptec ADP-1480 SCSI\n");
    130 
    131 	/*
    132 	 * Map the device.
    133 	 */
    134 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    135 	if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
    136 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    137 	    &bst, &bsh, NULL, NULL) == 0) {
    138 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
    139 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    140 	} else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
    141 	    PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, NULL) == 0) {
    142 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
    143 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    144 	} else {
    145 		printf("%s: unable to map device registers\n",
    146 		    ahc_name(ahc));
    147 		return;
    148 	}
    149 
    150 	/* Make sure the right access type is on the CardBus bridge. */
    151 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
    152 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    153 
    154 	/* Enable the appropriate bits in the PCI CSR. */
    155 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    156 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    157 	reg |= csc->sc_csr;
    158 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    159 
    160 	/*
    161 	 * Make sure the latency timer is set to some reasonable
    162 	 * value.
    163 	 */
    164 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
    165 	if (PCI_LATTIMER(reg) < 0x20) {
    166 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    167 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    168 		cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
    169 	}
    170 
    171 	/*
    172 	 * ADP-1480 is always an AIC-7860.
    173 	 */
    174 	ahc_t = AHC_AIC7860;
    175 
    176 	/*
    177 	 * On all CardBus adapters, we allow SCB paging.
    178 	 */
    179 	ahc_f = AHC_PAGESCBS;
    180 
    181 	ahc_fe = AHC_AIC7860_FE;
    182 	ahc_t = AHC_AIC7860;
    183 
    184 	if (ahc_alloc(ahc, bsh, bst, ca->ca_dmat, ahc_t, ahc_fe, ahc_f) < 0) {
    185 		printf("%s: unable to initialize softc\n", ahc_name(ahc));
    186 		return;
    187 	}
    188 
    189 	ahc->channel = 'A';
    190 
    191 	ahc_reset(ahc->sc_dev.dv_xname);
    192 
    193 	/*
    194 	 * Establish the interrupt.
    195 	 */
    196 	ahc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
    197 	    ahc_intr, ahc);
    198 	if (ahc->sc_ih == NULL) {
    199 		printf("%s: unable to establish interrupt at %d\n",
    200 		    ahc_name(ahc), ca->ca_intrline);
    201 		return;
    202 	}
    203 	printf("%s: interrupting at %d\n", ahc_name(ahc), ca->ca_intrline);
    204 
    205 	/*
    206 	 * Do chip-specific initialization.
    207 	 */
    208 	{
    209 		u_char sblkctl;
    210 		const char *id_string;
    211 
    212 		switch (ahc->type) {
    213 		case AHC_AIC7860:
    214 			id_string = "aic7860 ";
    215 			check_export(ahc, &sxfrctl1);
    216 			break;
    217 
    218 		default:
    219 			printf("%s: unknown controller type\n", ahc_name(ahc));
    220 			ahc_free(ahc);
    221 			return;
    222 		}
    223 
    224 		/*
    225 		 * Take the LED out of diagnostic mode.
    226 		 */
    227 		sblkctl = ahc_inb(ahc, SBLKCTL);
    228 		ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
    229 
    230 		/*
    231 		 * I don't know where this is set in the SEEPROM or by the
    232 		 * BIOS, so we default to 100%.
    233 		 */
    234 		ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
    235 
    236 		if (ahc->flags & AHC_USEDEFAULTS) {
    237 			/*
    238 			 * We can't "use defaults", as we have no way
    239 			 * of knowing what default settings hould be.
    240 			 */
    241 			printf("%s: CardBus device requires an SEEPROM\n",
    242 			    ahc_name(ahc));
    243 			return;
    244 		}
    245 
    246 		printf("%s: %s", ahc_name(ahc), id_string);
    247 	}
    248 
    249 	/*
    250 	 * XXX does this card have external SRAM? - fvdl
    251 	 */
    252 
    253 	/*
    254 	 * Record our termination setting for the
    255 	 * generic initialization routine.
    256 	 */
    257         if ((sxfrctl1 & STPWEN) != 0)
    258 		ahc->flags |= AHC_TERM_ENB_A;
    259 
    260 	if (ahc_init(ahc)) {
    261 		ahc_free(ahc);
    262 		return;
    263 	}
    264 
    265 	ahc_attach(ahc);
    266 }
    267