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