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