Home | History | Annotate | Line # | Download | only in cardbus
ahc_cardbus.c revision 1.17
      1 /*	$NetBSD: ahc_cardbus.c,v 1.17 2005/05/12 06:21:01 augustss Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2005 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/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: ahc_cardbus.c,v 1.17 2005/05/12 06:21:01 augustss Exp $");
     49 
     50 #include "opt_ahc_cardbus.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/malloc.h>
     55 #include <sys/kernel.h>
     56 #include <sys/queue.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/bus.h>
     60 #include <machine/intr.h>
     61 
     62 #include <dev/scsipi/scsi_all.h>
     63 #include <dev/scsipi/scsipi_all.h>
     64 #include <dev/scsipi/scsiconf.h>
     65 
     66 #include <dev/pci/pcireg.h>
     67 
     68 #include <dev/cardbus/cardbusvar.h>
     69 #include <dev/pci/pcidevs.h>
     70 
     71 #include <dev/ic/aic7xxx_osm.h>
     72 #include <dev/ic/aic7xxx_inline.h>
     73 
     74 
     75 #ifndef	AHC_CARDBUS_DEFAULT_SCSI_ID
     76 #define	AHC_CARDBUS_DEFAULT_SCSI_ID	0x7
     77 #endif
     78 
     79 #define	AHC_CARDBUS_IOBA	0x10
     80 #define	AHC_CARDBUS_MMBA	0x14
     81 
     82 struct ahc_cardbus_softc {
     83 	struct ahc_softc sc_ahc;	/* real AHC */
     84 
     85 	/* CardBus-specific goo. */
     86 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
     87 	int	sc_intrline;		/* our interrupt line */
     88 	cardbustag_t sc_tag;
     89 
     90 	int	sc_cbenable;		/* what CardBus access type to enable */
     91 	int	sc_csr;			/* CSR bits */
     92 	bus_size_t sc_size;
     93 };
     94 
     95 int	ahc_cardbus_match(struct device *, struct cfdata *, void *);
     96 void	ahc_cardbus_attach(struct device *, struct device *, void *);
     97 int	ahc_cardbus_detach(struct device *, int);
     98 int	ahc_activate(struct device *self, enum devact act);
     99 
    100 CFATTACH_DECL(ahc_cardbus, sizeof(struct ahc_cardbus_softc),
    101     ahc_cardbus_match, ahc_cardbus_attach, ahc_cardbus_detach, ahc_activate);
    102 
    103 int
    104 ahc_cardbus_match(parent, match, aux)
    105 	struct device *parent;
    106 	struct cfdata *match;
    107 	void *aux;
    108 {
    109 	struct cardbus_attach_args *ca = aux;
    110 
    111 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_ADP &&
    112 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_ADP_APA1480)
    113 		return (1);
    114 
    115 	return (0);
    116 }
    117 
    118 void
    119 ahc_cardbus_attach(parent, self, aux)
    120 	struct device *parent, *self;
    121 	void *aux;
    122 {
    123 	struct cardbus_attach_args *ca = aux;
    124 	struct ahc_cardbus_softc *csc = (void *) self;
    125 	struct ahc_softc *ahc = &csc->sc_ahc;
    126 	cardbus_devfunc_t ct = ca->ca_ct;
    127 	cardbus_chipset_tag_t cc = ct->ct_cc;
    128 	cardbus_function_tag_t cf = ct->ct_cf;
    129 	bus_space_tag_t bst;
    130 	bus_space_handle_t bsh;
    131 	pcireg_t reg;
    132 	u_int sxfrctl1 = 0;
    133 	u_char sblkctl;
    134 
    135 
    136 	csc->sc_ct = ct;
    137 	csc->sc_tag = ca->ca_tag;
    138 	csc->sc_intrline = ca->ca_intrline;
    139 
    140 	printf(": Adaptec ADP-1480 SCSI\n");
    141 
    142 	/*
    143 	 * Map the device.
    144 	 */
    145 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    146 	if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
    147 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    148 	    &bst, &bsh, NULL, &csc->sc_size) == 0) {
    149 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
    150 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    151 	} else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
    152 	    PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, &csc->sc_size) == 0) {
    153 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
    154 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    155 	} else {
    156 		printf("%s: unable to map device registers\n",
    157 		    ahc_name(ahc));
    158 		return;
    159 	}
    160 
    161 	/* Make sure the right access type is on the CardBus bridge. */
    162 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
    163 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    164 
    165 	/* Enable the appropriate bits in the PCI CSR. */
    166 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    167 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    168 	reg |= csc->sc_csr;
    169 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    170 
    171 	/*
    172 	 * Make sure the latency timer is set to some reasonable
    173 	 * value.
    174 	 */
    175 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
    176 	if (PCI_LATTIMER(reg) < 0x20) {
    177 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    178 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    179 		cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
    180 	}
    181 
    182 	ahc_set_name(ahc, ahc->sc_dev.dv_xname);
    183 
    184 	ahc->parent_dmat = ca->ca_dmat;
    185 	ahc->tag = bst;
    186 	ahc->bsh = bsh;
    187 
    188 	/*
    189 	 * ADP-1480 is always an AIC-7860.
    190 	 */
    191 	ahc->chip = AHC_AIC7860 | AHC_PCI;
    192 	ahc->features = AHC_AIC7860_FE|AHC_REMOVABLE;
    193 	ahc->bugs |= AHC_TMODE_WIDEODD_BUG|AHC_CACHETHEN_BUG|AHC_PCI_MWI_BUG;
    194 	if (PCI_REVISION(ca->ca_class) >= 1)
    195 		ahc->bugs |= AHC_PCI_2_1_RETRY_BUG;
    196 
    197 	if (ahc_softc_init(ahc) != 0)
    198 		return;
    199 
    200 	/*
    201 	 * On all CardBus adapters, we allow SCB paging.
    202 	 */
    203 	ahc->flags = AHC_PAGESCBS;
    204 
    205 	ahc->channel = 'A';
    206 
    207 	ahc_intr_enable(ahc, FALSE);
    208 
    209 	ahc_reset(ahc);
    210 
    211 	/*
    212 	 * Establish the interrupt.
    213 	 */
    214 	ahc->ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
    215 	    ahc_intr, ahc);
    216 	if (ahc->ih == NULL) {
    217 		printf("%s: unable to establish interrupt at %d\n",
    218 		    ahc_name(ahc), ca->ca_intrline);
    219 		return;
    220 	}
    221 	printf("%s: interrupting at %d\n", ahc_name(ahc), ca->ca_intrline);
    222 
    223 	ahc->seep_config = malloc(sizeof(*ahc->seep_config),
    224 				  M_DEVBUF, M_NOWAIT);
    225 	if (ahc->seep_config == NULL)
    226 		return;
    227 
    228 	ahc_check_extport(ahc, &sxfrctl1);
    229 	/*
    230 	 * Take the LED out of diagnostic mode.
    231 	 */
    232 	sblkctl = ahc_inb(ahc, SBLKCTL);
    233 	ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
    234 
    235 	/*
    236 	 * I don't know where this is set in the SEEPROM or by the
    237 	 * BIOS, so we default to 100%.
    238 	 */
    239 	ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
    240 
    241 	if (ahc->flags & AHC_USEDEFAULTS) {
    242 		int our_id;
    243 		/*
    244 		 * Assume only one connector and always turn
    245 		 * on termination.
    246 		 */
    247 		our_id = AHC_CARDBUS_DEFAULT_SCSI_ID;
    248 		sxfrctl1 = STPWEN;
    249 		ahc_outb(ahc, SCSICONF, our_id | ENSPCHK | RESET_SCSI);
    250 		ahc->our_id = our_id;
    251 	}
    252 
    253 	printf("%s: aic7860", ahc_name(ahc));
    254 
    255 	/*
    256 	 * Record our termination setting for the
    257 	 * generic initialization routine.
    258 	 */
    259         if ((sxfrctl1 & STPWEN) != 0)
    260 		ahc->flags |= AHC_TERM_ENB_A;
    261 
    262 	if (ahc_init(ahc)) {
    263 		ahc_free(ahc);
    264 		return;
    265 	}
    266 
    267 	ahc_attach(ahc);
    268 }
    269 
    270 int
    271 ahc_cardbus_detach(self, flags)
    272 	struct device *self;
    273 	int flags;
    274 {
    275 	struct ahc_cardbus_softc *csc = (void*)self;
    276 	struct ahc_softc *ahc = &csc->sc_ahc;
    277 
    278 	int rv;
    279 
    280 	rv = ahc_detach((void *)ahc, flags);
    281 	if (rv)
    282 		return rv;
    283 
    284 	if (ahc->ih) {
    285 		cardbus_intr_disestablish(csc->sc_ct->ct_cc,
    286 					  csc->sc_ct->ct_cf, ahc->ih);
    287 		ahc->ih = 0;
    288 	}
    289 
    290 	if (csc->sc_cbenable) {
    291 		if (csc->sc_cbenable == CARDBUS_MEM_ENABLE)
    292 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_MMBA,
    293 				ahc->tag, ahc->bsh, csc->sc_size);
    294 		else if (csc->sc_cbenable == CARDBUS_IO_ENABLE)
    295 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_IOBA,
    296 				ahc->tag, ahc->bsh, csc->sc_size);
    297 	csc->sc_cbenable = 0;
    298 	}
    299 
    300 	return (0);
    301 }
    302 
    303 
    304 int
    305 ahc_activate(struct device *self, enum devact act)
    306 {
    307 	struct ahc_cardbus_softc *csc = (void*)self;
    308 	struct ahc_softc *ahc = &csc->sc_ahc;
    309 	int s, rv = 0;
    310 
    311 	s = splhigh();
    312 	switch (act) {
    313 	case DVACT_ACTIVATE:
    314 		rv = EOPNOTSUPP;
    315 		break;
    316 
    317 	case DVACT_DEACTIVATE:
    318 		if (ahc->sc_child != NULL)
    319 			rv = config_deactivate(ahc->sc_child);
    320 		break;
    321 	}
    322 	splx(s);
    323 
    324 	return (rv);
    325 }
    326