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