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