Home | History | Annotate | Line # | Download | only in pci
cac_pci.c revision 1.6
      1 /*	$NetBSD: cac_pci.c,v 1.6 2000/09/26 11:38:47 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 #include <sys/proc.h>
     49 #include <sys/buf.h>
     50 
     51 #include <machine/endian.h>
     52 #include <machine/bus.h>
     53 
     54 #include <dev/pci/pcidevs.h>
     55 #include <dev/pci/pcivar.h>
     56 
     57 #include <dev/ic/cacreg.h>
     58 #include <dev/ic/cacvar.h>
     59 
     60 #define	PCI_CBIO	0x10	/* Configuration base I/O address */
     61 #define	PCI_CBMA	0x14	/* Configuration base memory address */
     62 
     63 static void	cac_pci_attach(struct device *, struct device *, void *);
     64 static int	cac_pci_match(struct device *, struct cfdata *, void *);
     65 
     66 static struct	cac_ccb *cac_pci_l0_completed(struct cac_softc *);
     67 static int	cac_pci_l0_fifo_full(struct cac_softc *);
     68 static void	cac_pci_l0_intr_enable(struct cac_softc *, int);
     69 static int	cac_pci_l0_intr_pending(struct cac_softc *);
     70 static void	cac_pci_l0_submit(struct cac_softc *, struct cac_ccb *);
     71 
     72 struct cfattach cac_pci_ca = {
     73 	sizeof(struct cac_softc), cac_pci_match, cac_pci_attach
     74 };
     75 
     76 static struct cac_linkage cac_pci_l0 = {
     77 	cac_pci_l0_completed,
     78 	cac_pci_l0_fifo_full,
     79 	cac_pci_l0_intr_enable,
     80 	cac_pci_l0_intr_pending,
     81 	cac_pci_l0_submit
     82 };
     83 
     84 #define	CT_IOMAP	0x01	/* Use I/O port access */
     85 #define CT_STARTFW	0x02	/* Need to start controller firmware */
     86 
     87 struct cac_pci_type {
     88 	int	ct_subsysid;
     89 	int	ct_flags;
     90 	struct	cac_linkage *ct_linkage;
     91 	char	*ct_typestr;
     92 } static cac_pci_type[] = {
     93 	{ 0x3040110e,	CT_IOMAP,	&cac_l0,	"SMART-2/E" },
     94 	{ 0x40300e11,	0, 		&cac_l0,	"SMART-2/P" },
     95 	{ 0x40310e11,	0, 		&cac_l0, 	"SMART-2SL" },
     96 	{ 0x40320e11,	0, 		&cac_l0,	"Smart Array 3200" },
     97 	{ 0x40330e11,	0, 		&cac_l0,	"Smart Array 3100ES" },
     98 	{ 0x40340e11,	0, 		&cac_l0,	"Smart Array 221" },
     99 	{ 0x40400e11,	CT_STARTFW, 	&cac_pci_l0,	"Integrated Array" },
    100 	{ 0x40480e11,	CT_STARTFW,	&cac_pci_l0,	"RAID LC2" },
    101 	{ 0x40500e11,	0,	 	&cac_pci_l0,	"Smart Array 4200" },
    102 	{ 0x40510e11,	0, 		&cac_pci_l0,	"Smart Array 4200ES" },
    103 	{ 0x40580e11,	0,		&cac_pci_l0,	"Smart Array 431" },
    104 	{ 0,		0,		&cac_l0,	NULL },
    105 };
    106 
    107 static int
    108 cac_pci_match(struct device *parent, struct cfdata *match, void *aux)
    109 {
    110 	struct pci_attach_args *pa;
    111 
    112 	pa = (struct pci_attach_args *)aux;
    113 
    114 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_COMPAQ &&
    115 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_COMPAQ_SMART2P)
    116 		return (1);
    117 
    118 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_DEC &&
    119 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_DEC_CPQ42XX)
    120 		return (1);
    121 
    122 	return (0);
    123 }
    124 
    125 static void
    126 cac_pci_attach(struct device *parent, struct device *self, void *aux)
    127 {
    128 	struct pci_attach_args *pa;
    129 	struct cac_pci_type *ct;
    130 	struct cac_softc *sc;
    131 	pci_chipset_tag_t pc;
    132 	pci_intr_handle_t ih;
    133 	const char *intrstr;
    134 	pcireg_t csr, subsysid;
    135 	int flags;
    136 
    137 	sc = (struct cac_softc *)self;
    138 	pa = (struct pci_attach_args *)aux;
    139 	pc = pa->pa_pc;
    140 
    141 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    142 
    143 	for (ct = cac_pci_type; ct->ct_subsysid != 0; ct++)
    144 		if (subsysid == ct->ct_subsysid)
    145 			break;
    146 
    147 	if (((flags = ct->ct_flags) & CT_IOMAP) == 0)
    148 		if (pci_mapreg_map(pa, PCI_CBMA, PCI_MAPREG_TYPE_MEM, 0,
    149 		    &sc->sc_iot, &sc->sc_ioh, NULL, NULL))
    150 		    	flags |= CT_IOMAP;
    151 
    152 	if ((flags & CT_IOMAP) != 0)
    153 		if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    154 		    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    155 			printf("can't map i/o space\n");
    156 			return;
    157 		}
    158 
    159 	sc->sc_dmat = pa->pa_dmat;
    160 
    161 	/* Enable the device. */
    162 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    163 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    164 		       csr | PCI_COMMAND_MASTER_ENABLE);
    165 
    166 	/* Map and establish the interrupt. */
    167 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    168 	    pa->pa_intrline, &ih)) {
    169 		printf("can't map interrupt\n");
    170 		return;
    171 	}
    172 	intrstr = pci_intr_string(pc, ih);
    173 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, cac_intr, sc);
    174 	if (sc->sc_ih == NULL) {
    175 		printf("can't establish interrupt");
    176 		if (intrstr != NULL)
    177 			printf(" at %s", intrstr);
    178 		printf("\n");
    179 		return;
    180 	}
    181 
    182 	printf(": Compaq ");
    183 	if (ct->ct_typestr == NULL) {
    184 		printf("array controller\n%s: unknown subsystem ID: 0x%08x\n",
    185 		    sc->sc_dv.dv_xname, (u_int)subsysid);
    186 	} else
    187 		printf("%s\n", ct->ct_typestr);
    188 
    189 	/* Now attach to the bus-independent code. */
    190 	sc->sc_cl = ct->ct_linkage;
    191 	cac_init(sc, intrstr, (flags & CT_STARTFW) != 0);
    192 }
    193 
    194 static void
    195 cac_pci_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    196 {
    197 
    198 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
    199 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    200 	cac_outl(sc, CAC_42REG_CMD_FIFO, ccb->ccb_paddr);
    201 }
    202 
    203 static struct cac_ccb *
    204 cac_pci_l0_completed(struct cac_softc *sc)
    205 {
    206 	struct cac_ccb *ccb;
    207 	u_int32_t off;
    208 
    209 	if ((off = cac_inl(sc, CAC_42REG_DONE_FIFO)) == 0xffffffffU)
    210 		return (0);
    211 
    212 	cac_outl(sc, CAC_42REG_DONE_FIFO, 0);
    213 	off = (off & ~3) - sc->sc_ccbs_paddr;
    214 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
    215 
    216 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    217 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    218 
    219 	return (ccb);
    220 }
    221 
    222 static int
    223 cac_pci_l0_intr_pending(struct cac_softc *sc)
    224 {
    225 
    226 	return (cac_inl(sc, CAC_42REG_INTR_PENDING) &
    227 	    cac_inl(sc, CAC_42REG_STATUS));
    228 }
    229 
    230 static void
    231 cac_pci_l0_intr_enable(struct cac_softc *sc, int state)
    232 {
    233 
    234 	cac_outl(sc, CAC_42REG_INTR_MASK, (state ? 0 : 8));	/* XXX */
    235 }
    236 
    237 static int
    238 cac_pci_l0_fifo_full(struct cac_softc *sc)
    239 {
    240 
    241 	return (~cac_inl(sc, CAC_42REG_CMD_FIFO));
    242 }
    243