Home | History | Annotate | Line # | Download | only in cardbus
njata_cardbus.c revision 1.1
      1 /*	$Id: njata_cardbus.c,v 1.1 2006/09/07 14:22:07 itohy Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 ITOH Yasufumi <itohy (at) NetBSD.org>.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS''
     17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     26  * THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: njata_cardbus.c,v 1.1 2006/09/07 14:22:07 itohy Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 
     37 #include <machine/bus.h>
     38 #include <machine/intr.h>
     39 
     40 #include <dev/cardbus/cardbusvar.h>
     41 #include <dev/pci/pcidevs.h>
     42 
     43 #include <dev/ata/atavar.h>
     44 #include <dev/ic/wdcreg.h>
     45 #include <dev/ic/wdcvar.h>
     46 
     47 #include <dev/ic/ninjaata32reg.h>
     48 #include <dev/ic/ninjaata32var.h>
     49 
     50 #define NJATA32_CARDBUS_BASEADDR_IO	CARDBUS_BASE0_REG
     51 #define NJATA32_CARDBUS_BASEADDR_MEM	CARDBUS_BASE1_REG
     52 
     53 struct njata32_cardbus_softc {
     54 	struct njata32_softc	sc_njata32;
     55 
     56 	/* CardBus-specific goo */
     57 	cardbus_devfunc_t	sc_ct;		/* our CardBus devfuncs */
     58 	int			sc_intrline;	/* our interrupt line */
     59 	cardbustag_t		sc_tag;
     60 
     61 	bus_space_handle_t	sc_regmaph;
     62 	bus_size_t		sc_regmap_size;
     63 };
     64 
     65 static const struct njata32_cardbus_product *njata_cardbus_lookup
     66 		    (const struct cardbus_attach_args *);
     67 static int	njata_cardbus_match(struct device *, struct cfdata *,
     68 		    void *);
     69 static void	njata_cardbus_attach(struct device *, struct device *,
     70 		    void *);
     71 static int	njata_cardbus_detach(struct device *, int);
     72 
     73 CFATTACH_DECL(njata_cardbus, sizeof(struct njata32_cardbus_softc),
     74     njata_cardbus_match, njata_cardbus_attach, njata_cardbus_detach, NULL);
     75 
     76 static const struct njata32_cardbus_product {
     77 	cardbus_vendor_id_t	p_vendor;
     78 	cardbus_product_id_t	p_product;
     79 	uint8_t			p_flags;
     80 #define NJATA32_FL_IOMAP_ONLY	1	/* registers are only in the I/O map */
     81 } njata32_cardbus_products[] = {
     82 	{ PCI_VENDOR_IODATA,	PCI_PRODUCT_IODATA_CBIDE2,
     83 	  0 },
     84 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJATA32BI,
     85 	  0 },
     86 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJATA32BI_KME,
     87 	  0 },
     88 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NPATA32_CF32A,
     89 	  NJATA32_FL_IOMAP_ONLY },
     90 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NPATA32_KME,
     91 	  NJATA32_FL_IOMAP_ONLY },
     92 
     93 	{ PCI_VENDOR_INVALID,	0,
     94 	  0 }
     95 };
     96 
     97 static const struct njata32_cardbus_product *
     98 njata_cardbus_lookup(ca)
     99 	const struct cardbus_attach_args *ca;
    100 {
    101 	const struct njata32_cardbus_product *p;
    102 
    103 	for (p = njata32_cardbus_products;
    104 	    p->p_vendor != PCI_VENDOR_INVALID; p++) {
    105 		if (CARDBUS_VENDOR(ca->ca_id) == p->p_vendor &&
    106 		    CARDBUS_PRODUCT(ca->ca_id) == p->p_product)
    107 			return p;
    108 	}
    109 
    110 	return NULL;
    111 }
    112 
    113 static int
    114 njata_cardbus_match(parent, match, aux)
    115 	struct device *parent;
    116 	struct cfdata *match;
    117 	void *aux;
    118 {
    119 	struct cardbus_attach_args *ca = aux;
    120 
    121 	if (njata_cardbus_lookup(ca))
    122 		return 1;
    123 
    124 	return 0;
    125 }
    126 
    127 static void
    128 njata_cardbus_attach(parent, self, aux)
    129 	struct device *parent, *self;
    130 	void *aux;
    131 {
    132 	struct cardbus_attach_args *ca = aux;
    133 	struct njata32_cardbus_softc *csc = (void *)self;
    134 	struct njata32_softc *sc = &csc->sc_njata32;
    135 	const struct njata32_cardbus_product *prod;
    136 	cardbus_devfunc_t ct = ca->ca_ct;
    137 	cardbus_chipset_tag_t cc = ct->ct_cc;
    138 	cardbus_function_tag_t cf = ct->ct_cf;
    139 	pcireg_t reg;
    140 	int csr;
    141 	uint8_t latency = 0x20;
    142 
    143 	if ((prod = njata_cardbus_lookup(ca)) == NULL)
    144 		panic("njata_cardbus_attach");
    145 
    146 	printf(": Workbit NinjaATA-32 IDE controller\n");
    147 
    148 	csc->sc_ct = ct;
    149 	csc->sc_tag = ca->ca_tag;
    150 	csc->sc_intrline = ca->ca_intrline;
    151 
    152 	/*
    153 	 * Map the device.
    154 	 */
    155 	csr = PCI_COMMAND_MASTER_ENABLE;
    156 
    157 	/*
    158 	 * Map registers.
    159 	 * Try memory map first, and then try I/O.
    160 	 */
    161 	if ((prod->p_flags & NJATA32_FL_IOMAP_ONLY) == 0 &&
    162 	    Cardbus_mapreg_map(csc->sc_ct, NJATA32_CARDBUS_BASEADDR_MEM,
    163 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    164 	    &NJATA32_REGT(sc), &csc->sc_regmaph, NULL, &csc->sc_regmap_size)
    165 	    == 0) {
    166 		if (bus_space_subregion(NJATA32_REGT(sc), csc->sc_regmaph,
    167 		    NJATA32_MEMOFFSET_REG, NJATA32_REGSIZE, &NJATA32_REGH(sc))
    168 		    != 0) {
    169 			/* failed -- undo map and try I/O */
    170 			Cardbus_mapreg_unmap(csc->sc_ct,
    171 			    NJATA32_CARDBUS_BASEADDR_MEM, NJATA32_REGT(sc),
    172 			    csc->sc_regmaph, csc->sc_regmap_size);
    173 			goto try_io;
    174 		}
    175 #ifdef NJATA32_DEBUG
    176 		printf("%s: memory space mapped, size %u\n",
    177 		    NJATA32NAME(sc), (unsigned)csc->sc_regmap_size);
    178 #endif
    179 		csr |= PCI_COMMAND_MEM_ENABLE;
    180 		sc->sc_flags = NJATA32_MEM_MAPPED;
    181 		(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    182 	} else {
    183 	try_io:
    184 		if (Cardbus_mapreg_map(csc->sc_ct, NJATA32_CARDBUS_BASEADDR_IO,
    185 		    PCI_MAPREG_TYPE_IO, 0, &NJATA32_REGT(sc),
    186 		    &NJATA32_REGH(sc), NULL, &csc->sc_regmap_size) == 0) {
    187 #ifdef NJATA32_DEBUG
    188 			printf("%s: io space mapped, size %u\n",
    189 			    NJATA32NAME(sc), (unsigned)csc->sc_regmap_size);
    190 #endif
    191 			csr |= PCI_COMMAND_IO_ENABLE;
    192 			sc->sc_flags = NJATA32_IO_MAPPED;
    193 			(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
    194 		} else {
    195 			printf("%s: unable to map device registers\n",
    196 			    NJATA32NAME(sc));
    197 			return;
    198 		}
    199 	}
    200 
    201 	/* Make sure the right access type is on the CardBus bridge. */
    202 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    203 
    204 	/* Enable the appropriate bits in the PCI CSR. */
    205 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    206 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    207 	reg |= csr;
    208 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    209 
    210 	/*
    211 	 * Make sure the latency timer is set to some reasonable
    212 	 * value.
    213 	 */
    214 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG);
    215 	if (CARDBUS_LATTIMER(reg) < latency) {
    216 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    217 		reg |= (latency << CARDBUS_LATTIMER_SHIFT);
    218 		cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_BHLC_REG, reg);
    219 	}
    220 
    221 	sc->sc_dmat = ca->ca_dmat;
    222 
    223 	/*
    224 	 * Establish the interrupt.
    225 	 */
    226 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
    227 	    njata32_intr, sc);
    228 	if (sc->sc_ih == NULL) {
    229 		printf("%s: unable to establish interrupt at %d\n",
    230 		    NJATA32NAME(sc), ca->ca_intrline);
    231 		return;
    232 	}
    233 	printf("%s: interrupting at %d\n", NJATA32NAME(sc), ca->ca_intrline);
    234 
    235 	/* attach */
    236 	njata32_attach(sc);
    237 }
    238 
    239 static int
    240 njata_cardbus_detach(self, flags)
    241 	struct device *self;
    242 	int flags;
    243 {
    244 	struct njata32_cardbus_softc *csc = (void *) self;
    245 	struct njata32_softc *sc = &csc->sc_njata32;
    246 	int rv;
    247 
    248 	rv = njata32_detach(sc, flags);
    249 	if (rv)
    250 		return rv;
    251 
    252 	if (sc->sc_ih)
    253 		cardbus_intr_disestablish(csc->sc_ct->ct_cc,
    254 		    csc->sc_ct->ct_cf, sc->sc_ih);
    255 
    256 	if (sc->sc_flags & NJATA32_IO_MAPPED)
    257 		Cardbus_mapreg_unmap(csc->sc_ct, NJATA32_CARDBUS_BASEADDR_IO,
    258 		    NJATA32_REGT(sc), NJATA32_REGH(sc), csc->sc_regmap_size);
    259 	if (sc->sc_flags & NJATA32_MEM_MAPPED)
    260 		Cardbus_mapreg_unmap(csc->sc_ct, NJATA32_CARDBUS_BASEADDR_MEM,
    261 		    NJATA32_REGT(sc), csc->sc_regmaph, csc->sc_regmap_size);
    262 
    263 	return 0;
    264 }
    265