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