Home | History | Annotate | Line # | Download | only in pci
cac_pci.c revision 1.10
      1 /*	$NetBSD: cac_pci.c,v 1.10 2001/01/10 16:48:04 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      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 /*
     40  * PCI front-end for cac(4) driver.
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/queue.h>
     48 
     49 #include <machine/endian.h>
     50 #include <machine/bus.h>
     51 
     52 #include <dev/pci/pcidevs.h>
     53 #include <dev/pci/pcivar.h>
     54 
     55 #include <dev/ic/cacreg.h>
     56 #include <dev/ic/cacvar.h>
     57 
     58 static void	cac_pci_attach(struct device *, struct device *, void *);
     59 static struct	cac_pci_type *cac_pci_findtype(struct pci_attach_args *);
     60 static int	cac_pci_match(struct device *, struct cfdata *, void *);
     61 
     62 static struct	cac_ccb *cac_pci_l0_completed(struct cac_softc *);
     63 static int	cac_pci_l0_fifo_full(struct cac_softc *);
     64 static void	cac_pci_l0_intr_enable(struct cac_softc *, int);
     65 static int	cac_pci_l0_intr_pending(struct cac_softc *);
     66 static void	cac_pci_l0_submit(struct cac_softc *, struct cac_ccb *);
     67 
     68 struct cfattach cac_pci_ca = {
     69 	sizeof(struct cac_softc), cac_pci_match, cac_pci_attach
     70 };
     71 
     72 static struct cac_linkage cac_pci_l0 = {
     73 	cac_pci_l0_completed,
     74 	cac_pci_l0_fifo_full,
     75 	cac_pci_l0_intr_enable,
     76 	cac_pci_l0_intr_pending,
     77 	cac_pci_l0_submit
     78 };
     79 
     80 #define CT_STARTFW	0x01	/* Need to start controller firmware */
     81 
     82 struct cac_pci_type {
     83 	int	ct_subsysid;
     84 	int	ct_flags;
     85 	struct	cac_linkage *ct_linkage;
     86 	char	*ct_typestr;
     87 } static cac_pci_type[] = {
     88 	{ 0x40300e11,	0, 		&cac_l0,	"SMART-2/P" },
     89 	{ 0x40310e11,	0, 		&cac_l0, 	"SMART-2SL" },
     90 	{ 0x40320e11,	0, 		&cac_l0,	"Smart Array 3200" },
     91 	{ 0x40330e11,	0, 		&cac_l0,	"Smart Array 3100ES" },
     92 	{ 0x40340e11,	0, 		&cac_l0,	"Smart Array 221" },
     93 	{ 0x40400e11,	CT_STARTFW, 	&cac_pci_l0,	"Integrated Array" },
     94 	{ 0x40480e11,	CT_STARTFW,	&cac_pci_l0,	"RAID LC2" },
     95 	{ 0x40500e11,	0,	 	&cac_pci_l0,	"Smart Array 4200" },
     96 	{ 0x40510e11,	0, 		&cac_pci_l0,	"Smart Array 4200ES" },
     97 	{ 0x40580e11,	0,		&cac_pci_l0,	"Smart Array 431" },
     98 };
     99 
    100 struct cac_pci_product {
    101 	u_short	cp_vendor;
    102 	u_short	cp_product;
    103 } static cac_pci_product[] = {
    104 	{ PCI_VENDOR_COMPAQ,	PCI_PRODUCT_COMPAQ_SMART2P },
    105 	{ PCI_VENDOR_DEC,	PCI_PRODUCT_DEC_CPQ42XX },
    106 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1510 },
    107 };
    108 
    109 static struct cac_pci_type *
    110 cac_pci_findtype(struct pci_attach_args *pa)
    111 {
    112 	struct cac_pci_type *ct;
    113 	struct cac_pci_product *cp;
    114 	pcireg_t subsysid;
    115 	int i;
    116 
    117 	cp = cac_pci_product;
    118 	i = 0;
    119 	while (i < sizeof(cac_pci_product) / sizeof(cac_pci_product[0])) {
    120 		if (PCI_VENDOR(pa->pa_id) == cp->cp_vendor &&
    121 		    PCI_PRODUCT(pa->pa_id) == cp->cp_product)
    122 		    	break;
    123 		cp++;
    124 		i++;
    125 	}
    126 	if (i == sizeof(cac_pci_product) / sizeof(cac_pci_product[0]))
    127 		return (NULL);
    128 
    129 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    130 	ct = cac_pci_type;
    131 	i = 0;
    132 	while (i < sizeof(cac_pci_type) / sizeof(cac_pci_type[0])) {
    133 		if (subsysid == ct->ct_subsysid)
    134 			break;
    135 		ct++;
    136 		i++;
    137 	}
    138 	if (i == sizeof(cac_pci_type) / sizeof(cac_pci_type[0]))
    139 		return (NULL);
    140 
    141 	return (ct);
    142 }
    143 
    144 static int
    145 cac_pci_match(struct device *parent, struct cfdata *match, void *aux)
    146 {
    147 
    148 	return (cac_pci_findtype(aux) != NULL);
    149 }
    150 
    151 static void
    152 cac_pci_attach(struct device *parent, struct device *self, void *aux)
    153 {
    154 	struct pci_attach_args *pa;
    155 	struct cac_pci_type *ct;
    156 	struct cac_softc *sc;
    157 	pci_chipset_tag_t pc;
    158 	pci_intr_handle_t ih;
    159 	const char *intrstr;
    160 	pcireg_t reg;
    161 	int memr, ior, i;
    162 
    163 	sc = (struct cac_softc *)self;
    164 	pa = (struct pci_attach_args *)aux;
    165 	pc = pa->pa_pc;
    166 	ct = cac_pci_findtype(pa);
    167 
    168 	/*
    169 	 * Map the PCI register window.
    170 	 */
    171 	memr = -1;
    172 	ior = -1;
    173 
    174 	for (i = 0x10; i <= 0x14; i += 4) {
    175 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, i);
    176 
    177 		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
    178 			if (ior == -1 && PCI_MAPREG_IO_SIZE(reg) != 0)
    179 				ior = i;
    180 		} else {
    181 			if (memr == -1 && PCI_MAPREG_MEM_SIZE(reg) != 0)
    182 				memr = i;
    183 		}
    184 	}
    185 
    186 	if (memr != -1) {
    187 		if (pci_mapreg_map(pa, memr, PCI_MAPREG_TYPE_MEM, 0,
    188 		    &sc->sc_iot, &sc->sc_ioh, NULL, NULL))
    189 			memr = -1;
    190 		else
    191 			ior = -1;
    192 	}
    193 	if (ior != -1)
    194 		if (pci_mapreg_map(pa, ior, PCI_MAPREG_TYPE_IO, 0,
    195 		    &sc->sc_iot, &sc->sc_ioh, NULL, NULL))
    196 		    	ior = -1;
    197 	if (memr == -1 && ior == -1) {
    198 		printf("%s: can't map i/o or memory space\n", self->dv_xname);
    199 		return;
    200 	}
    201 
    202 	sc->sc_dmat = pa->pa_dmat;
    203 
    204 	/* Enable the device. */
    205 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    206 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    207 		       reg | PCI_COMMAND_MASTER_ENABLE);
    208 
    209 	/* Map and establish the interrupt. */
    210 	if (pci_intr_map(pa, &ih)) {
    211 		printf("can't map interrupt\n");
    212 		return;
    213 	}
    214 	intrstr = pci_intr_string(pc, ih);
    215 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, cac_intr, sc);
    216 	if (sc->sc_ih == NULL) {
    217 		printf("can't establish interrupt");
    218 		if (intrstr != NULL)
    219 			printf(" at %s", intrstr);
    220 		printf("\n");
    221 		return;
    222 	}
    223 
    224 	printf(": Compaq %s\n", ct->ct_typestr);
    225 
    226 	/* Now attach to the bus-independent code. */
    227 	sc->sc_cl = ct->ct_linkage;
    228 	cac_init(sc, intrstr, (ct->ct_flags & CT_STARTFW) != 0);
    229 }
    230 
    231 static void
    232 cac_pci_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    233 {
    234 
    235 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
    236 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    237 	cac_outl(sc, CAC_42REG_CMD_FIFO, ccb->ccb_paddr);
    238 }
    239 
    240 static struct cac_ccb *
    241 cac_pci_l0_completed(struct cac_softc *sc)
    242 {
    243 	struct cac_ccb *ccb;
    244 	u_int32_t off;
    245 
    246 	if ((off = cac_inl(sc, CAC_42REG_DONE_FIFO)) == 0xffffffffU)
    247 		return (0);
    248 
    249 	cac_outl(sc, CAC_42REG_DONE_FIFO, 0);
    250 	off = (off & ~3) - sc->sc_ccbs_paddr;
    251 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
    252 
    253 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    254 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    255 
    256 	return (ccb);
    257 }
    258 
    259 static int
    260 cac_pci_l0_intr_pending(struct cac_softc *sc)
    261 {
    262 
    263 	return ((cac_inl(sc, CAC_42REG_STATUS) & CAC_42_EXTINT) != 0);
    264 }
    265 
    266 static void
    267 cac_pci_l0_intr_enable(struct cac_softc *sc, int state)
    268 {
    269 
    270 	cac_outl(sc, CAC_42REG_INTR_MASK, (state ? 0 : 8));	/* XXX */
    271 }
    272 
    273 static int
    274 cac_pci_l0_fifo_full(struct cac_softc *sc)
    275 {
    276 
    277 	return (cac_inl(sc, CAC_42REG_CMD_FIFO) != 0);
    278 }
    279