Home | History | Annotate | Line # | Download | only in cardbus
ahc_cardbus.c revision 1.35.12.1
      1  1.35.12.1  jdolecek /*	$NetBSD: ahc_cardbus.c,v 1.35.12.1 2017/12/03 11:37:00 jdolecek 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  *
     20        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21        1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31        1.1   thorpej  */
     32        1.1   thorpej 
     33        1.1   thorpej /*
     34        1.1   thorpej  * CardBus front-end for the Adaptec AIC-7xxx family of SCSI controllers.
     35        1.1   thorpej  *
     36        1.1   thorpej  * TODO:
     37        1.1   thorpej  *	- power management
     38        1.1   thorpej  */
     39        1.5     lukem 
     40        1.5     lukem #include <sys/cdefs.h>
     41  1.35.12.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: ahc_cardbus.c,v 1.35.12.1 2017/12/03 11:37:00 jdolecek Exp $");
     42       1.17  augustss 
     43       1.17  augustss #include "opt_ahc_cardbus.h"
     44        1.1   thorpej 
     45        1.1   thorpej #include <sys/param.h>
     46        1.1   thorpej #include <sys/systm.h>
     47        1.1   thorpej #include <sys/malloc.h>
     48        1.1   thorpej #include <sys/kernel.h>
     49        1.1   thorpej #include <sys/queue.h>
     50        1.1   thorpej #include <sys/device.h>
     51        1.1   thorpej 
     52       1.22        ad #include <sys/bus.h>
     53       1.22        ad #include <sys/intr.h>
     54        1.1   thorpej 
     55       1.16     perry #include <dev/scsipi/scsi_all.h>
     56        1.1   thorpej #include <dev/scsipi/scsipi_all.h>
     57        1.1   thorpej #include <dev/scsipi/scsiconf.h>
     58        1.1   thorpej 
     59        1.1   thorpej #include <dev/pci/pcireg.h>
     60        1.1   thorpej 
     61        1.1   thorpej #include <dev/cardbus/cardbusvar.h>
     62       1.14   mycroft #include <dev/pci/pcidevs.h>
     63        1.1   thorpej 
     64       1.10      fvdl #include <dev/ic/aic7xxx_osm.h>
     65       1.10      fvdl #include <dev/ic/aic7xxx_inline.h>
     66        1.1   thorpej 
     67        1.3     enami 
     68       1.17  augustss #ifndef	AHC_CARDBUS_DEFAULT_SCSI_ID
     69       1.17  augustss #define	AHC_CARDBUS_DEFAULT_SCSI_ID	0x7
     70       1.17  augustss #endif
     71       1.17  augustss 
     72        1.1   thorpej #define	AHC_CARDBUS_IOBA	0x10
     73        1.1   thorpej #define	AHC_CARDBUS_MMBA	0x14
     74        1.1   thorpej 
     75        1.1   thorpej struct ahc_cardbus_softc {
     76        1.2      fvdl 	struct ahc_softc sc_ahc;	/* real AHC */
     77        1.1   thorpej 
     78        1.1   thorpej 	/* CardBus-specific goo. */
     79        1.1   thorpej 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
     80       1.30    dyoung 	pcitag_t sc_tag;
     81        1.1   thorpej 
     82       1.31    dyoung 	int	sc_bar;
     83       1.32    dyoung 	pcireg_t	sc_csr;
     84        1.6    ichiro 	bus_size_t sc_size;
     85        1.1   thorpej };
     86        1.1   thorpej 
     87       1.27    cegger int	ahc_cardbus_match(device_t, cfdata_t, void *);
     88       1.27    cegger void	ahc_cardbus_attach(device_t, device_t, void *);
     89       1.27    cegger int	ahc_cardbus_detach(device_t, int);
     90        1.1   thorpej 
     91       1.27    cegger CFATTACH_DECL_NEW(ahc_cardbus, sizeof(struct ahc_cardbus_softc),
     92       1.29    dyoung     ahc_cardbus_match, ahc_cardbus_attach, ahc_cardbus_detach, NULL);
     93        1.1   thorpej 
     94        1.1   thorpej int
     95       1.27    cegger ahc_cardbus_match(device_t parent, cfdata_t match, void *aux)
     96        1.1   thorpej {
     97        1.1   thorpej 	struct cardbus_attach_args *ca = aux;
     98        1.1   thorpej 
     99       1.33    dyoung 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_ADP &&
    100       1.33    dyoung 	    PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_ADP_APA1480)
    101        1.1   thorpej 		return (1);
    102        1.1   thorpej 
    103        1.1   thorpej 	return (0);
    104        1.1   thorpej }
    105        1.1   thorpej 
    106        1.1   thorpej void
    107       1.27    cegger ahc_cardbus_attach(device_t parent, device_t self, void *aux)
    108        1.1   thorpej {
    109        1.1   thorpej 	struct cardbus_attach_args *ca = aux;
    110       1.19   thorpej 	struct ahc_cardbus_softc *csc = device_private(self);
    111        1.2      fvdl 	struct ahc_softc *ahc = &csc->sc_ahc;
    112        1.1   thorpej 	cardbus_devfunc_t ct = ca->ca_ct;
    113        1.1   thorpej 	bus_space_tag_t bst;
    114        1.1   thorpej 	bus_space_handle_t bsh;
    115        1.1   thorpej 	pcireg_t reg;
    116        1.2      fvdl 	u_int sxfrctl1 = 0;
    117       1.10      fvdl 	u_char sblkctl;
    118        1.2      fvdl 
    119        1.1   thorpej 
    120       1.27    cegger 	ahc->sc_dev = self;
    121        1.1   thorpej 	csc->sc_ct = ct;
    122        1.1   thorpej 	csc->sc_tag = ca->ca_tag;
    123        1.1   thorpej 
    124        1.1   thorpej 	printf(": Adaptec ADP-1480 SCSI\n");
    125        1.1   thorpej 
    126        1.1   thorpej 	/*
    127        1.1   thorpej 	 * Map the device.
    128        1.1   thorpej 	 */
    129        1.1   thorpej 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    130        1.1   thorpej 	if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
    131        1.1   thorpej 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    132        1.6    ichiro 	    &bst, &bsh, NULL, &csc->sc_size) == 0) {
    133       1.31    dyoung 		csc->sc_bar = AHC_CARDBUS_MMBA;
    134        1.1   thorpej 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    135        1.1   thorpej 	} else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
    136        1.6    ichiro 	    PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, &csc->sc_size) == 0) {
    137       1.31    dyoung 		csc->sc_bar = AHC_CARDBUS_IOBA;
    138        1.1   thorpej 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    139        1.1   thorpej 	} else {
    140       1.31    dyoung 		csc->sc_bar = 0;
    141  1.35.12.1  jdolecek 		aprint_error("%s: unable to map device registers\n",
    142        1.1   thorpej 		    ahc_name(ahc));
    143        1.1   thorpej 		return;
    144        1.1   thorpej 	}
    145        1.1   thorpej 
    146        1.1   thorpej 	/* Enable the appropriate bits in the PCI CSR. */
    147       1.34    dyoung 	reg = Cardbus_conf_read(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    148        1.1   thorpej 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    149        1.1   thorpej 	reg |= csc->sc_csr;
    150       1.34    dyoung 	Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    151        1.1   thorpej 
    152        1.1   thorpej 	/*
    153        1.1   thorpej 	 * Make sure the latency timer is set to some reasonable
    154        1.1   thorpej 	 * value.
    155        1.1   thorpej 	 */
    156       1.34    dyoung 	reg = Cardbus_conf_read(ct, ca->ca_tag, PCI_BHLC_REG);
    157        1.1   thorpej 	if (PCI_LATTIMER(reg) < 0x20) {
    158        1.1   thorpej 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    159        1.1   thorpej 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    160       1.34    dyoung 		Cardbus_conf_write(ct, ca->ca_tag, PCI_BHLC_REG, reg);
    161        1.1   thorpej 	}
    162        1.1   thorpej 
    163       1.27    cegger 	ahc_set_name(ahc, device_xname(ahc->sc_dev));
    164       1.12      fvdl 
    165       1.12      fvdl 	ahc->parent_dmat = ca->ca_dmat;
    166        1.6    ichiro 	ahc->tag = bst;
    167        1.6    ichiro 	ahc->bsh = bsh;
    168        1.6    ichiro 
    169        1.1   thorpej 	/*
    170        1.1   thorpej 	 * ADP-1480 is always an AIC-7860.
    171        1.1   thorpej 	 */
    172       1.10      fvdl 	ahc->chip = AHC_AIC7860 | AHC_PCI;
    173       1.10      fvdl 	ahc->features = AHC_AIC7860_FE|AHC_REMOVABLE;
    174       1.10      fvdl 	ahc->bugs |= AHC_TMODE_WIDEODD_BUG|AHC_CACHETHEN_BUG|AHC_PCI_MWI_BUG;
    175       1.10      fvdl 	if (PCI_REVISION(ca->ca_class) >= 1)
    176       1.10      fvdl 		ahc->bugs |= AHC_PCI_2_1_RETRY_BUG;
    177       1.11      fvdl 
    178       1.11      fvdl 	if (ahc_softc_init(ahc) != 0)
    179       1.11      fvdl 		return;
    180        1.1   thorpej 
    181        1.1   thorpej 	/*
    182        1.1   thorpej 	 * On all CardBus adapters, we allow SCB paging.
    183        1.1   thorpej 	 */
    184       1.10      fvdl 	ahc->flags = AHC_PAGESCBS;
    185        1.2      fvdl 
    186       1.10      fvdl 	ahc->channel = 'A';
    187        1.2      fvdl 
    188       1.10      fvdl 	ahc_intr_enable(ahc, FALSE);
    189        1.1   thorpej 
    190        1.3     enami 	ahc_reset(ahc);
    191        1.1   thorpej 
    192        1.1   thorpej 	/*
    193        1.1   thorpej 	 * Establish the interrupt.
    194        1.1   thorpej 	 */
    195       1.35  drochner 	ahc->ih = Cardbus_intr_establish(ct, IPL_BIO, ahc_intr, ahc);
    196        1.3     enami 	if (ahc->ih == NULL) {
    197  1.35.12.1  jdolecek 		aprint_error("%s: unable to establish interrupt\n",
    198       1.25  drochner 		    ahc_name(ahc));
    199        1.1   thorpej 		return;
    200        1.1   thorpej 	}
    201        1.1   thorpej 
    202       1.13  jdolecek 	ahc->seep_config = malloc(sizeof(*ahc->seep_config),
    203       1.13  jdolecek 				  M_DEVBUF, M_NOWAIT);
    204       1.13  jdolecek 	if (ahc->seep_config == NULL)
    205       1.13  jdolecek 		return;
    206       1.13  jdolecek 
    207       1.10      fvdl 	ahc_check_extport(ahc, &sxfrctl1);
    208        1.1   thorpej 	/*
    209       1.10      fvdl 	 * Take the LED out of diagnostic mode.
    210        1.1   thorpej 	 */
    211       1.10      fvdl 	sblkctl = ahc_inb(ahc, SBLKCTL);
    212       1.10      fvdl 	ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
    213        1.1   thorpej 
    214       1.10      fvdl 	/*
    215       1.10      fvdl 	 * I don't know where this is set in the SEEPROM or by the
    216       1.10      fvdl 	 * BIOS, so we default to 100%.
    217       1.10      fvdl 	 */
    218       1.10      fvdl 	ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
    219        1.1   thorpej 
    220       1.10      fvdl 	if (ahc->flags & AHC_USEDEFAULTS) {
    221       1.17  augustss 		int our_id;
    222        1.1   thorpej 		/*
    223       1.17  augustss 		 * Assume only one connector and always turn
    224       1.17  augustss 		 * on termination.
    225        1.1   thorpej 		 */
    226       1.17  augustss 		our_id = AHC_CARDBUS_DEFAULT_SCSI_ID;
    227       1.17  augustss 		sxfrctl1 = STPWEN;
    228       1.17  augustss 		ahc_outb(ahc, SCSICONF, our_id | ENSPCHK | RESET_SCSI);
    229       1.17  augustss 		ahc->our_id = our_id;
    230        1.1   thorpej 	}
    231        1.2      fvdl 
    232       1.10      fvdl 	printf("%s: aic7860", ahc_name(ahc));
    233        1.2      fvdl 
    234        1.2      fvdl 	/*
    235        1.2      fvdl 	 * Record our termination setting for the
    236        1.2      fvdl 	 * generic initialization routine.
    237        1.2      fvdl 	 */
    238        1.2      fvdl         if ((sxfrctl1 & STPWEN) != 0)
    239        1.2      fvdl 		ahc->flags |= AHC_TERM_ENB_A;
    240        1.1   thorpej 
    241        1.1   thorpej 	if (ahc_init(ahc)) {
    242        1.1   thorpej 		ahc_free(ahc);
    243        1.1   thorpej 		return;
    244        1.1   thorpej 	}
    245        1.1   thorpej 
    246        1.1   thorpej 	ahc_attach(ahc);
    247        1.6    ichiro }
    248        1.6    ichiro 
    249        1.6    ichiro int
    250       1.27    cegger ahc_cardbus_detach(device_t self, int flags)
    251        1.6    ichiro {
    252       1.19   thorpej 	struct ahc_cardbus_softc *csc = device_private(self);
    253        1.6    ichiro 	struct ahc_softc *ahc = &csc->sc_ahc;
    254        1.6    ichiro 
    255        1.6    ichiro 	int rv;
    256        1.6    ichiro 
    257       1.28   tsutsui 	rv = ahc_detach(ahc, flags);
    258        1.6    ichiro 	if (rv)
    259        1.6    ichiro 		return rv;
    260        1.6    ichiro 
    261        1.6    ichiro 	if (ahc->ih) {
    262       1.34    dyoung 		Cardbus_intr_disestablish(csc->sc_ct, ahc->ih);
    263        1.6    ichiro 		ahc->ih = 0;
    264        1.6    ichiro 	}
    265        1.6    ichiro 
    266       1.31    dyoung 	if (csc->sc_bar != 0) {
    267       1.31    dyoung 		Cardbus_mapreg_unmap(csc->sc_ct, csc->sc_bar,
    268       1.31    dyoung 			ahc->tag, ahc->bsh, csc->sc_size);
    269       1.31    dyoung 		csc->sc_bar = 0;
    270        1.6    ichiro 	}
    271        1.6    ichiro 
    272        1.6    ichiro 	return (0);
    273       1.10      fvdl }
    274