Home | History | Annotate | Line # | Download | only in pci
njs_pci.c revision 1.1
      1  1.1  itohy /*	$NetBSD: njs_pci.c,v 1.1 2004/08/26 14:13:46 itohy 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  itohy  *    must display the following acknowledgement:
     20  1.1  itohy  *	This product includes software developed by the NetBSD
     21  1.1  itohy  *	Foundation, Inc. and its contributors.
     22  1.1  itohy  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  itohy  *    contributors may be used to endorse or promote products derived
     24  1.1  itohy  *    from this software without specific prior written permission.
     25  1.1  itohy  *
     26  1.1  itohy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  itohy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  itohy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  itohy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  itohy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  itohy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  itohy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  itohy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  itohy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  itohy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  itohy  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  itohy  */
     38  1.1  itohy 
     39  1.1  itohy #include <sys/cdefs.h>
     40  1.1  itohy __KERNEL_RCSID(0, "$NetBSD: njs_pci.c,v 1.1 2004/08/26 14:13:46 itohy Exp $");
     41  1.1  itohy 
     42  1.1  itohy #include <sys/param.h>
     43  1.1  itohy #include <sys/systm.h>
     44  1.1  itohy #include <sys/kernel.h>
     45  1.1  itohy #include <sys/device.h>
     46  1.1  itohy 
     47  1.1  itohy #include <machine/bus.h>
     48  1.1  itohy #include <machine/intr.h>
     49  1.1  itohy 
     50  1.1  itohy #include <dev/scsipi/scsi_all.h>
     51  1.1  itohy #include <dev/scsipi/scsipi_all.h>
     52  1.1  itohy #include <dev/scsipi/scsiconf.h>
     53  1.1  itohy 
     54  1.1  itohy #include <dev/pci/pcivar.h>
     55  1.1  itohy #include <dev/pci/pcidevs.h>
     56  1.1  itohy 
     57  1.1  itohy #include <dev/ic/ninjascsi32reg.h>
     58  1.1  itohy #include <dev/ic/ninjascsi32var.h>
     59  1.1  itohy 
     60  1.1  itohy #define NJSC32_PCI_BASEADDR_IO		PCI_MAPREG_START
     61  1.1  itohy #define NJSC32_PCI_BASEADDR_MEM		(PCI_MAPREG_START + 4)
     62  1.1  itohy 
     63  1.1  itohy struct njsc32_pci_softc {
     64  1.1  itohy 	struct njsc32_softc	sc_njsc32;
     65  1.1  itohy 
     66  1.1  itohy 	pci_chipset_tag_t	sc_pc;
     67  1.1  itohy 
     68  1.1  itohy 	bus_space_handle_t	sc_regmaph;
     69  1.1  itohy 	bus_size_t		sc_regmap_size;
     70  1.1  itohy };
     71  1.1  itohy 
     72  1.1  itohy int	njs_pci_match __P((struct device *, struct cfdata *, void *));
     73  1.1  itohy void	njs_pci_attach __P((struct device *, struct device *, void *));
     74  1.1  itohy int	njs_pci_detach __P((struct device *, int));
     75  1.1  itohy 
     76  1.1  itohy CFATTACH_DECL(njs_pci, sizeof(struct njsc32_pci_softc),
     77  1.1  itohy     njs_pci_match, njs_pci_attach, njs_pci_detach, NULL);
     78  1.1  itohy 
     79  1.1  itohy const struct njsc32_pci_product {
     80  1.1  itohy 	pci_vendor_id_t		p_vendor;
     81  1.1  itohy 	pci_product_id_t	p_product;
     82  1.1  itohy 	njsc32_model_t		p_model;
     83  1.1  itohy 	int			p_clk;		/* one of NJSC32_CLK_* */
     84  1.1  itohy } njsc32_pci_products[] = {
     85  1.1  itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32UDE_IODATA,
     86  1.1  itohy 	  NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE,	NJSC32_CLK_40M },
     87  1.1  itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32UDE_LOGITEC,
     88  1.1  itohy 	  NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE,	NJSC32_CLK_40M },
     89  1.1  itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32UDE_LOGITEC2,
     90  1.1  itohy 	  NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE,	NJSC32_CLK_40M },
     91  1.1  itohy 	{ PCI_VENDOR_WORKBIT,	PCI_PRODUCT_WORKBIT_NJSC32UDE_BUFFALO,
     92  1.1  itohy 	  NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE,	NJSC32_CLK_40M },
     93  1.1  itohy 
     94  1.1  itohy 	{ 0,				0,
     95  1.1  itohy 	  NJSC32_MODEL_INVALID,		0 },
     96  1.1  itohy };
     97  1.1  itohy 
     98  1.1  itohy const struct njsc32_pci_product * njs_pci_lookup
     99  1.1  itohy     __P((const struct pci_attach_args *));
    100  1.1  itohy 
    101  1.1  itohy const struct njsc32_pci_product *
    102  1.1  itohy njs_pci_lookup(pa)
    103  1.1  itohy 	const struct pci_attach_args *pa;
    104  1.1  itohy {
    105  1.1  itohy 	const struct njsc32_pci_product *p;
    106  1.1  itohy 
    107  1.1  itohy 	for (p = njsc32_pci_products;
    108  1.1  itohy 	    p->p_model != NJSC32_MODEL_INVALID; p++) {
    109  1.1  itohy 		if (PCI_VENDOR(pa->pa_id) == p->p_vendor &&
    110  1.1  itohy 		    PCI_PRODUCT(pa->pa_id) == p->p_product)
    111  1.1  itohy 			return p;
    112  1.1  itohy 	}
    113  1.1  itohy 
    114  1.1  itohy 	return NULL;
    115  1.1  itohy }
    116  1.1  itohy 
    117  1.1  itohy int
    118  1.1  itohy njs_pci_match(parent, match, aux)
    119  1.1  itohy 	struct device *parent;
    120  1.1  itohy 	struct cfdata *match;
    121  1.1  itohy 	void *aux;
    122  1.1  itohy {
    123  1.1  itohy 	struct pci_attach_args *pa = aux;
    124  1.1  itohy 
    125  1.1  itohy 	if (njs_pci_lookup(pa))
    126  1.1  itohy 		return 1;
    127  1.1  itohy 
    128  1.1  itohy 	return 0;
    129  1.1  itohy }
    130  1.1  itohy 
    131  1.1  itohy void
    132  1.1  itohy njs_pci_attach(parent, self, aux)
    133  1.1  itohy 	struct device *parent, *self;
    134  1.1  itohy 	void *aux;
    135  1.1  itohy {
    136  1.1  itohy 	struct pci_attach_args *pa = aux;
    137  1.1  itohy 	struct njsc32_pci_softc *psc = (void *) self;
    138  1.1  itohy 	struct njsc32_softc *sc = &psc->sc_njsc32;
    139  1.1  itohy 	const struct njsc32_pci_product *prod;
    140  1.1  itohy 	pci_intr_handle_t ih;
    141  1.1  itohy 	pci_chipset_tag_t pc = pa->pa_pc;
    142  1.1  itohy 	pcireg_t reg;
    143  1.1  itohy 	const char *str_intr, *str_at;
    144  1.1  itohy 
    145  1.1  itohy 	aprint_naive(": SCSI controller\n");
    146  1.1  itohy 	if ((prod = njs_pci_lookup(pa)) == NULL)
    147  1.1  itohy 		panic("njs_pci_attach");
    148  1.1  itohy 
    149  1.1  itohy 	aprint_normal(": Workbit NinjaSCSI-32 SCSI adapter\n");
    150  1.1  itohy 	sc->sc_model = prod->p_model;
    151  1.1  itohy 	sc->sc_clk = prod->p_clk;
    152  1.1  itohy 
    153  1.1  itohy 	psc->sc_pc = pc;
    154  1.1  itohy 
    155  1.1  itohy 	/* enable device and DMA */
    156  1.1  itohy 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    157  1.1  itohy 	reg |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
    158  1.1  itohy 	    PCI_COMMAND_MASTER_ENABLE;
    159  1.1  itohy 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    160  1.1  itohy 
    161  1.1  itohy 	/*
    162  1.1  itohy 	 * Map registers.
    163  1.1  itohy 	 * Try memory map first, and then try I/O.
    164  1.1  itohy 	 */
    165  1.1  itohy 	if (pci_mapreg_map(pa, NJSC32_PCI_BASEADDR_MEM,
    166  1.1  itohy 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    167  1.1  itohy 	    &sc->sc_regt, &psc->sc_regmaph, NULL, &psc->sc_regmap_size) == 0) {
    168  1.1  itohy 		if (bus_space_subregion(sc->sc_regt, psc->sc_regmaph,
    169  1.1  itohy 		    NJSC32_MEMOFFSET_REG, NJSC32_REGSIZE, &sc->sc_regh) != 0) {
    170  1.1  itohy 			/* failed -- undo map and try I/O */
    171  1.1  itohy 			bus_space_unmap(sc->sc_regt, psc->sc_regmaph,
    172  1.1  itohy 			    psc->sc_regmap_size);
    173  1.1  itohy 			goto try_io;
    174  1.1  itohy 		}
    175  1.1  itohy #ifdef NJSC32_DEBUG
    176  1.1  itohy 		printf("%s: memory space mapped\n", sc->sc_dev.dv_xname);
    177  1.1  itohy #endif
    178  1.1  itohy 		sc->sc_flags = NJSC32_MEM_MAPPED;
    179  1.1  itohy 	} else {
    180  1.1  itohy 	try_io:
    181  1.1  itohy 		if (pci_mapreg_map(pa, NJSC32_PCI_BASEADDR_IO,
    182  1.1  itohy 		    PCI_MAPREG_TYPE_IO, 0, &sc->sc_regt, &sc->sc_regh,
    183  1.1  itohy 		    NULL, &psc->sc_regmap_size) == 0) {
    184  1.1  itohy #ifdef NJSC32_DEBUG
    185  1.1  itohy 			printf("%s: io space mapped\n", sc->sc_dev.dv_xname);
    186  1.1  itohy #endif
    187  1.1  itohy 			sc->sc_flags = NJSC32_IO_MAPPED;
    188  1.1  itohy 		} else {
    189  1.1  itohy 			aprint_error("%s: unable to map device registers\n",
    190  1.1  itohy 			    sc->sc_dev.dv_xname);
    191  1.1  itohy 			return;
    192  1.1  itohy 		}
    193  1.1  itohy 	}
    194  1.1  itohy 
    195  1.1  itohy 	sc->sc_dmat = pa->pa_dmat;
    196  1.1  itohy 
    197  1.1  itohy 	/* map interrupt */
    198  1.1  itohy 	if (pci_intr_map(pa, &ih)) {
    199  1.1  itohy 		aprint_error("%s: couldn't map interrupt\n",
    200  1.1  itohy 		    sc->sc_dev.dv_xname);
    201  1.1  itohy 		return;
    202  1.1  itohy 	}
    203  1.1  itohy 
    204  1.1  itohy 	str_intr = pci_intr_string(pa->pa_pc, ih);
    205  1.1  itohy 	str_at = " at ";
    206  1.1  itohy 	if (str_intr == NULL)
    207  1.1  itohy 		str_at = str_intr = "";
    208  1.1  itohy 
    209  1.1  itohy 	/* setup interrupt handler */
    210  1.1  itohy 	if ((sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, njsc32_intr, sc))
    211  1.1  itohy 	    == NULL) {
    212  1.1  itohy 		printf("%s: unable to establish interrupt%s%s\n",
    213  1.1  itohy 		    sc->sc_dev.dv_xname, str_at, str_intr);
    214  1.1  itohy 		return;
    215  1.1  itohy 	}
    216  1.1  itohy 	printf("%s: interrupting%s%s\n",
    217  1.1  itohy 		sc->sc_dev.dv_xname, str_at, str_intr);
    218  1.1  itohy 
    219  1.1  itohy 	/* attach */
    220  1.1  itohy 	njsc32_attach(sc);
    221  1.1  itohy }
    222  1.1  itohy 
    223  1.1  itohy int
    224  1.1  itohy njs_pci_detach(self, flags)
    225  1.1  itohy 	struct device *self;
    226  1.1  itohy 	int flags;
    227  1.1  itohy {
    228  1.1  itohy 	struct njsc32_pci_softc *psc = (void *) self;
    229  1.1  itohy 	struct njsc32_softc *sc = &psc->sc_njsc32;
    230  1.1  itohy 	int rv;
    231  1.1  itohy 
    232  1.1  itohy 	rv = njsc32_detach(sc, flags);
    233  1.1  itohy 	if (rv)
    234  1.1  itohy 		return rv;
    235  1.1  itohy 
    236  1.1  itohy 	if (sc->sc_ih)
    237  1.1  itohy 		pci_intr_disestablish(psc->sc_pc, sc->sc_ih);
    238  1.1  itohy 
    239  1.1  itohy 	if (sc->sc_flags & NJSC32_IO_MAPPED)
    240  1.1  itohy 		bus_space_unmap(sc->sc_regt, sc->sc_regh, psc->sc_regmap_size);
    241  1.1  itohy 	if (sc->sc_flags & NJSC32_MEM_MAPPED)
    242  1.1  itohy 		bus_space_unmap(sc->sc_regt, psc->sc_regmaph,
    243  1.1  itohy 		    psc->sc_regmap_size);
    244  1.1  itohy 
    245  1.1  itohy 	return 0;
    246  1.1  itohy }
    247