Home | History | Annotate | Line # | Download | only in cardbus
njs_cardbus.c revision 1.11
      1  1.11    cegger /*	$NetBSD: njs_cardbus.c,v 1.11 2009/05/12 14:17:31 cegger Exp $	*/
      2   1.1     itohy 
      3   1.1     itohy /*-
      4   1.1     itohy  * Copyright (c) 2004 The NetBSD Foundation, Inc.
      5   1.1     itohy  * All rights reserved.
      6   1.1     itohy  *
      7   1.1     itohy  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     itohy  * by ITOH Yasufumi.
      9   1.1     itohy  *
     10   1.1     itohy  * Redistribution and use in source and binary forms, with or without
     11   1.1     itohy  * modification, are permitted provided that the following conditions
     12   1.1     itohy  * are met:
     13   1.1     itohy  * 1. Redistributions of source code must retain the above copyright
     14   1.1     itohy  *    notice, this list of conditions and the following disclaimer.
     15   1.1     itohy  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     itohy  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     itohy  *    documentation and/or other materials provided with the distribution.
     18   1.1     itohy  *
     19   1.1     itohy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1     itohy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1     itohy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1     itohy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1     itohy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1     itohy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1     itohy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1     itohy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1     itohy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1     itohy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1     itohy  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1     itohy  */
     31   1.1     itohy 
     32   1.1     itohy #include <sys/cdefs.h>
     33  1.11    cegger __KERNEL_RCSID(0, "$NetBSD: njs_cardbus.c,v 1.11 2009/05/12 14:17:31 cegger Exp $");
     34   1.1     itohy 
     35   1.1     itohy #include <sys/param.h>
     36   1.1     itohy #include <sys/systm.h>
     37   1.1     itohy #include <sys/kernel.h>
     38   1.1     itohy #include <sys/device.h>
     39   1.1     itohy 
     40   1.6        ad #include <sys/bus.h>
     41   1.6        ad #include <sys/intr.h>
     42   1.1     itohy 
     43   1.1     itohy #include <dev/scsipi/scsi_all.h>
     44   1.1     itohy #include <dev/scsipi/scsipi_all.h>
     45   1.1     itohy #include <dev/scsipi/scsiconf.h>
     46   1.1     itohy 
     47   1.1     itohy #include <dev/cardbus/cardbusvar.h>
     48   1.1     itohy #include <dev/pci/pcidevs.h>
     49   1.1     itohy 
     50   1.1     itohy #include <dev/ic/ninjascsi32reg.h>
     51   1.1     itohy #include <dev/ic/ninjascsi32var.h>
     52   1.1     itohy 
     53   1.1     itohy #define NJSC32_CARDBUS_BASEADDR_IO	CARDBUS_BASE0_REG
     54   1.1     itohy #define NJSC32_CARDBUS_BASEADDR_MEM	CARDBUS_BASE1_REG
     55   1.1     itohy 
     56   1.1     itohy struct njsc32_cardbus_softc {
     57   1.1     itohy 	struct njsc32_softc	sc_njsc32;
     58   1.1     itohy 
     59   1.1     itohy 	/* CardBus-specific goo */
     60   1.1     itohy 	cardbus_devfunc_t	sc_ct;		/* our CardBus devfuncs */
     61   1.9  drochner 	cardbus_intr_line_t	sc_intrline;	/* our interrupt line */
     62   1.1     itohy 	cardbustag_t		sc_tag;
     63   1.1     itohy 
     64   1.1     itohy 	bus_space_handle_t	sc_regmaph;
     65   1.1     itohy 	bus_size_t		sc_regmap_size;
     66   1.1     itohy };
     67   1.1     itohy 
     68  1.10     joerg static int	njs_cardbus_match(device_t, cfdata_t, void *);
     69  1.10     joerg static void	njs_cardbus_attach(device_t, device_t, void *);
     70  1.10     joerg static int	njs_cardbus_detach(device_t, int);
     71   1.1     itohy 
     72  1.10     joerg CFATTACH_DECL_NEW(njs_cardbus, sizeof(struct njsc32_cardbus_softc),
     73   1.1     itohy     njs_cardbus_match, njs_cardbus_attach, njs_cardbus_detach, NULL);
     74   1.1     itohy 
     75   1.2   thorpej static const struct njsc32_cardbus_product {
     76   1.1     itohy 	cardbus_vendor_id_t	p_vendor;
     77   1.1     itohy 	cardbus_product_id_t	p_product;
     78   1.1     itohy 	njsc32_model_t		p_model;
     79   1.1     itohy 	int			p_clk;		/* one of NJSC32_CLK_* */
     80   1.1     itohy } njsc32_cardbus_products[] = {
     81   1.1     itohy 	{ PCI_VENDOR_IODATA,	PCI_PRODUCT_IODATA_CBSCII,
     82   1.1     itohy 	  NJSC32_MODEL_32BI,	NJSC32_CLK_40M },
     83   1.1     itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32BI,
     84   1.1     itohy 	  NJSC32_MODEL_32BI,	NJSC32_CLK_40M },
     85   1.1     itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32UDE,
     86   1.1     itohy 	  NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE,	NJSC32_CLK_40M },
     87   1.1     itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32BI_KME,
     88   1.1     itohy 	  NJSC32_MODEL_32BI,	NJSC32_CLK_40M },
     89   1.1     itohy 
     90   1.1     itohy 	{ 0,				0,
     91   1.1     itohy 	  NJSC32_MODEL_INVALID,		0 },
     92   1.1     itohy };
     93   1.1     itohy 
     94   1.2   thorpej static const struct njsc32_cardbus_product *
     95   1.2   thorpej njs_cardbus_lookup(const struct cardbus_attach_args *ca)
     96   1.1     itohy {
     97   1.1     itohy 	const struct njsc32_cardbus_product *p;
     98   1.1     itohy 
     99   1.1     itohy 	for (p = njsc32_cardbus_products;
    100   1.1     itohy 	    p->p_model != NJSC32_MODEL_INVALID; p++) {
    101   1.1     itohy 		if (CARDBUS_VENDOR(ca->ca_id) == p->p_vendor &&
    102   1.1     itohy 		    CARDBUS_PRODUCT(ca->ca_id) == p->p_product)
    103   1.1     itohy 			return p;
    104   1.1     itohy 	}
    105   1.1     itohy 
    106   1.1     itohy 	return NULL;
    107   1.1     itohy }
    108   1.1     itohy 
    109   1.2   thorpej static int
    110  1.10     joerg njs_cardbus_match(device_t parent, cfdata_t match, void *aux)
    111   1.1     itohy {
    112   1.1     itohy 	struct cardbus_attach_args *ca = aux;
    113   1.1     itohy 
    114   1.1     itohy 	if (njs_cardbus_lookup(ca))
    115   1.1     itohy 		return 1;
    116   1.1     itohy 
    117   1.1     itohy 	return 0;
    118   1.1     itohy }
    119   1.1     itohy 
    120   1.2   thorpej static void
    121  1.10     joerg njs_cardbus_attach(device_t parent, device_t self, void *aux)
    122   1.1     itohy {
    123   1.1     itohy 	struct cardbus_attach_args *ca = aux;
    124  1.10     joerg 	struct njsc32_cardbus_softc *csc = device_private(self);
    125   1.1     itohy 	struct njsc32_softc *sc = &csc->sc_njsc32;
    126   1.1     itohy 	const struct njsc32_cardbus_product *prod;
    127   1.1     itohy 	cardbus_devfunc_t ct = ca->ca_ct;
    128   1.1     itohy 	cardbus_chipset_tag_t cc = ct->ct_cc;
    129   1.1     itohy 	cardbus_function_tag_t cf = ct->ct_cf;
    130   1.1     itohy 	pcireg_t reg;
    131   1.1     itohy 	int csr;
    132   1.1     itohy 	u_int8_t latency = 0x20;
    133   1.1     itohy 
    134   1.1     itohy 	if ((prod = njs_cardbus_lookup(ca)) == NULL)
    135   1.1     itohy 		panic("njs_cardbus_attach");
    136   1.1     itohy 
    137   1.1     itohy 	printf(": Workbit NinjaSCSI-32 SCSI adapter\n");
    138  1.10     joerg 	sc->sc_dev = self;
    139   1.1     itohy 	sc->sc_model = prod->p_model;
    140   1.1     itohy 	sc->sc_clk = prod->p_clk;
    141   1.1     itohy 
    142   1.1     itohy 	csc->sc_ct = ct;
    143   1.1     itohy 	csc->sc_tag = ca->ca_tag;
    144   1.1     itohy 	csc->sc_intrline = ca->ca_intrline;
    145   1.1     itohy 
    146   1.1     itohy 	/*
    147   1.1     itohy 	 * Map the device.
    148   1.1     itohy 	 */
    149   1.1     itohy 	csr = PCI_COMMAND_MASTER_ENABLE;
    150   1.1     itohy 
    151   1.1     itohy 	/*
    152   1.1     itohy 	 * Map registers.
    153   1.1     itohy 	 * Try memory map first, and then try I/O.
    154   1.1     itohy 	 */
    155   1.1     itohy 	if (Cardbus_mapreg_map(csc->sc_ct, NJSC32_CARDBUS_BASEADDR_MEM,
    156   1.1     itohy 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    157   1.1     itohy 	    &sc->sc_regt, &csc->sc_regmaph, NULL, &csc->sc_regmap_size) == 0) {
    158   1.1     itohy 		if (bus_space_subregion(sc->sc_regt, csc->sc_regmaph,
    159   1.1     itohy 		    NJSC32_MEMOFFSET_REG, NJSC32_REGSIZE, &sc->sc_regh) != 0) {
    160   1.1     itohy 			/* failed -- undo map and try I/O */
    161   1.1     itohy 			Cardbus_mapreg_unmap(csc->sc_ct,
    162   1.1     itohy 			    NJSC32_CARDBUS_BASEADDR_MEM,
    163   1.1     itohy 			    sc->sc_regt, csc->sc_regmaph, csc->sc_regmap_size);
    164   1.1     itohy 			goto try_io;
    165   1.1     itohy 		}
    166   1.1     itohy #ifdef NJSC32_DEBUG
    167  1.10     joerg 		printf("%s: memory space mapped\n", device_xname(self));
    168   1.1     itohy #endif
    169   1.1     itohy 		csr |= PCI_COMMAND_MEM_ENABLE;
    170   1.1     itohy 		sc->sc_flags = NJSC32_MEM_MAPPED;
    171   1.1     itohy 		(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    172   1.1     itohy 	} else {
    173   1.1     itohy 	try_io:
    174   1.1     itohy 		if (Cardbus_mapreg_map(csc->sc_ct, NJSC32_CARDBUS_BASEADDR_IO,
    175   1.1     itohy 		    PCI_MAPREG_TYPE_IO, 0, &sc->sc_regt, &sc->sc_regh,
    176   1.1     itohy 		    NULL, &csc->sc_regmap_size) == 0) {
    177   1.1     itohy #ifdef NJSC32_DEBUG
    178  1.10     joerg 			printf("%s: io space mapped\n", device_xname(self));
    179   1.1     itohy #endif
    180   1.1     itohy 			csr |= PCI_COMMAND_IO_ENABLE;
    181   1.1     itohy 			sc->sc_flags = NJSC32_IO_MAPPED;
    182   1.1     itohy 			(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
    183   1.1     itohy 		} else {
    184  1.10     joerg 			aprint_error_dev(self, "unable to map device registers\n");
    185   1.1     itohy 			return;
    186   1.1     itohy 		}
    187   1.1     itohy 	}
    188   1.1     itohy 
    189   1.1     itohy 	/* Make sure the right access type is on the CardBus bridge. */
    190   1.1     itohy 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    191   1.1     itohy 
    192   1.1     itohy 	/* Enable the appropriate bits in the PCI CSR. */
    193   1.1     itohy 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    194   1.1     itohy 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    195   1.1     itohy 	reg |= csr;
    196   1.1     itohy 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    197   1.1     itohy 
    198   1.1     itohy 	/*
    199   1.1     itohy 	 * Make sure the latency timer is set to some reasonable
    200   1.1     itohy 	 * value.
    201   1.1     itohy 	 */
    202   1.1     itohy 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
    203   1.1     itohy 	if (CARDBUS_LATTIMER(reg) < latency) {
    204   1.1     itohy 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    205   1.1     itohy 		reg |= (latency << CARDBUS_LATTIMER_SHIFT);
    206   1.1     itohy 		cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, reg);
    207   1.1     itohy 	}
    208   1.1     itohy 
    209   1.1     itohy 	sc->sc_dmat = ca->ca_dmat;
    210   1.1     itohy 
    211   1.1     itohy 	/*
    212   1.1     itohy 	 * Establish the interrupt.
    213   1.1     itohy 	 */
    214   1.1     itohy 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
    215   1.1     itohy 	    njsc32_intr, sc);
    216   1.1     itohy 	if (sc->sc_ih == NULL) {
    217  1.10     joerg 		aprint_error_dev(self,
    218   1.9  drochner 				 "unable to establish interrupt\n");
    219   1.1     itohy 		return;
    220   1.1     itohy 	}
    221   1.1     itohy 
    222   1.1     itohy 	/* CardBus device cannot supply termination power. */
    223   1.1     itohy 	sc->sc_flags |= NJSC32_CANNOT_SUPPLY_TERMPWR;
    224   1.1     itohy 
    225   1.1     itohy 	/* attach */
    226   1.1     itohy 	njsc32_attach(sc);
    227   1.1     itohy }
    228   1.1     itohy 
    229   1.2   thorpej static int
    230  1.11    cegger njs_cardbus_detach(device_t self, int flags)
    231   1.1     itohy {
    232  1.10     joerg 	struct njsc32_cardbus_softc *csc = device_private(self);
    233   1.1     itohy 	struct njsc32_softc *sc = &csc->sc_njsc32;
    234   1.1     itohy 	int rv;
    235   1.1     itohy 
    236   1.1     itohy 	rv = njsc32_detach(sc, flags);
    237   1.1     itohy 	if (rv)
    238   1.1     itohy 		return rv;
    239   1.1     itohy 
    240   1.1     itohy 	if (sc->sc_ih)
    241   1.1     itohy 		cardbus_intr_disestablish(csc->sc_ct->ct_cc,
    242   1.1     itohy 		    csc->sc_ct->ct_cf, sc->sc_ih);
    243   1.1     itohy 
    244   1.1     itohy 	if (sc->sc_flags & NJSC32_IO_MAPPED)
    245   1.1     itohy 		Cardbus_mapreg_unmap(csc->sc_ct, NJSC32_CARDBUS_BASEADDR_IO,
    246   1.1     itohy 		    sc->sc_regt, sc->sc_regh, csc->sc_regmap_size);
    247   1.1     itohy 	if (sc->sc_flags & NJSC32_MEM_MAPPED)
    248   1.1     itohy 		Cardbus_mapreg_unmap(csc->sc_ct, NJSC32_CARDBUS_BASEADDR_MEM,
    249   1.1     itohy 		    sc->sc_regt, csc->sc_regmaph, csc->sc_regmap_size);
    250   1.1     itohy 
    251   1.1     itohy 	return 0;
    252   1.1     itohy }
    253