Home | History | Annotate | Line # | Download | only in pci
siop_pci_common.c revision 1.6
      1 /*	$NetBSD: siop_pci_common.c,v 1.6 2001/01/10 15:50:20 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Manuel Bouyer
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /* SYM53c8xx PCI-SCSI I/O Processors driver: PCI front-end */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/malloc.h>
     38 #include <sys/buf.h>
     39 #include <sys/kernel.h>
     40 
     41 #include <machine/endian.h>
     42 
     43 #include <dev/pci/pcireg.h>
     44 #include <dev/pci/pcivar.h>
     45 #include <dev/pci/pcidevs.h>
     46 
     47 #include <dev/scsipi/scsipi_all.h>
     48 #include <dev/scsipi/scsipiconf.h>
     49 
     50 #include <dev/ic/siopreg.h>
     51 #include <dev/ic/siopvar.h>
     52 #include <dev/pci/siop_pci_common.h>
     53 
     54 /* List (array, really :) of chips we know how to handle */
     55 const struct siop_product_desc siop_products[] = {
     56 	{ PCI_PRODUCT_SYMBIOS_810,
     57 	0x00,
     58 	"Symbios Logic 53c810 (fast scsi)",
     59 	SF_PCI_RL | SF_CHIP_LS,
     60 	4, 8, 3, 250, 0
     61 	},
     62 	{ PCI_PRODUCT_SYMBIOS_810,
     63 	0x10,
     64 	"Symbios Logic 53c810a (fast scsi)",
     65 	SF_PCI_RL | SF_PCI_BOF | SF_CHIP_PF | SF_CHIP_LS,
     66 	4, 8, 3, 250, 0
     67 	},
     68 	{ PCI_PRODUCT_SYMBIOS_815,
     69 	0x00,
     70 	"Symbios Logic 53c815 (fast scsi)",
     71 	SF_PCI_RL | SF_PCI_BOF,
     72 	4, 8, 3, 250, 0
     73 	},
     74 	{ PCI_PRODUCT_SYMBIOS_820,
     75 	0x00,
     76 	"Symbios Logic 53c820 (fast wide scsi)",
     77 	SF_PCI_RL | SF_CHIP_LS | SF_BUS_WIDE,
     78 	4, 8, 3, 250, 0
     79 	},
     80 	{ PCI_PRODUCT_SYMBIOS_825,
     81 	0x00,
     82 	"Symbios Logic 53c825 (fast wide scsi)",
     83 	SF_PCI_RL | SF_PCI_BOF | SF_BUS_WIDE,
     84 	4, 8, 3, 250, 0
     85 	},
     86 	{ PCI_PRODUCT_SYMBIOS_825,
     87 	0x10,
     88 	"Symbios Logic 53c825a (fast wide scsi)",
     89 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
     90 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_LS | SF_CHIP_10REGS |
     91 	SF_BUS_WIDE,
     92 	7, 8, 3, 250, 4096
     93 	},
     94 	{ PCI_PRODUCT_SYMBIOS_860,
     95 	0x00,
     96 	"Symbios Logic 53c860 (ultra scsi)",
     97 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
     98 	SF_CHIP_PF | SF_CHIP_LS |
     99 	SF_BUS_ULTRA,
    100 	4, 8, 5, 125, 0
    101 	},
    102 	{ PCI_PRODUCT_SYMBIOS_875,
    103 	0x00,
    104 	"Symbios Logic 53c875 (ultra-wide scsi)",
    105 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    106 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_LS | SF_CHIP_10REGS |
    107 	SF_BUS_ULTRA | SF_BUS_WIDE,
    108 	7, 16, 5, 125, 4096
    109 	},
    110 	{ PCI_PRODUCT_SYMBIOS_875,
    111 	0x02,
    112 	"Symbios Logic 53c875 (ultra-wide scsi)",
    113 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    114 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    115 	SF_CHIP_LS | SF_CHIP_10REGS |
    116 	SF_BUS_ULTRA | SF_BUS_WIDE,
    117 	7, 16, 5, 125, 4096
    118 	},
    119 	{ PCI_PRODUCT_SYMBIOS_875J,
    120 	0x00,
    121 	"Symbios Logic 53c875j (ultra-wide scsi)",
    122 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    123 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    124 	SF_CHIP_LS | SF_CHIP_10REGS |
    125 	SF_BUS_ULTRA | SF_BUS_WIDE,
    126 	7, 16, 5, 125, 4096
    127 	},
    128 	{ PCI_PRODUCT_SYMBIOS_885,
    129 	0x00,
    130 	"Symbios Logic 53c885 (ultra-wide scsi)",
    131 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    132 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    133 	SF_CHIP_LS | SF_CHIP_10REGS |
    134 	SF_BUS_ULTRA | SF_BUS_WIDE,
    135 	7, 16, 5, 125, 4096
    136 	},
    137 	{ PCI_PRODUCT_SYMBIOS_895,
    138 	0x00,
    139 	"Symbios Logic 53c895 (ultra2-wide scsi)",
    140 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    141 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    142 	SF_CHIP_LS | SF_CHIP_10REGS |
    143 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    144 	7, 31, 7, 62, 4096
    145 	},
    146 	{ PCI_PRODUCT_SYMBIOS_896,
    147 	0x00,
    148 	"Symbios Logic 53c896 (ultra2-wide scsi)",
    149 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    150 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    151 	SF_CHIP_LS | SF_CHIP_10REGS |
    152 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    153 	7, 31, 7, 62, 8192
    154 	},
    155 	{ PCI_PRODUCT_SYMBIOS_895A,
    156 	0x00,
    157 	"Symbios Logic 53c895a (ultra2-wide scsi)",
    158 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    159 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    160 	SF_CHIP_LS | SF_CHIP_10REGS |
    161 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    162 	7, 31, 7, 62, 8192
    163 	},
    164 	{ PCI_PRODUCT_SYMBIOS_1510D,
    165 	0x00,
    166 	"Symbios Logic 53c1510d (ultra2-wide scsi)",
    167 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    168 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    169 	SF_CHIP_LS | SF_CHIP_10REGS |
    170 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    171 	7, 31, 7, 62, 4096
    172 	},
    173 	{ 0,
    174 	0x00,
    175 	NULL,
    176 	0x00,
    177 	0, 0, 0, 0, 0
    178 	},
    179 };
    180 
    181 const struct siop_product_desc *
    182 siop_lookup_product(id, rev)
    183 	u_int32_t id;
    184 	int rev;
    185 {
    186 	const struct siop_product_desc *pp;
    187 	const struct siop_product_desc *rp = NULL;
    188 
    189 	if (PCI_VENDOR(id) != PCI_VENDOR_SYMBIOS)
    190 		return NULL;
    191 
    192 	for (pp = siop_products; pp->name != NULL; pp++) {
    193 		if (PCI_PRODUCT(id) == pp->product && pp->revision <= rev)
    194 			if (rp == NULL || pp->revision > rp->revision)
    195 				rp = pp;
    196 	}
    197 	return rp;
    198 }
    199 
    200 int
    201 siop_pci_attach_common(sc, pa)
    202 	struct siop_pci_softc *sc;
    203 	struct pci_attach_args *pa;
    204 {
    205 	pci_chipset_tag_t pc = pa->pa_pc;
    206 	pcitag_t tag = pa->pa_tag;
    207 	const char *intrstr;
    208 	pci_intr_handle_t intrhandle;
    209 	bus_space_tag_t iot, memt;
    210 	bus_space_handle_t ioh, memh;
    211 	pcireg_t memtype;
    212 	int memh_valid, ioh_valid;
    213 	bus_addr_t ioaddr, memaddr;
    214 
    215 	sc->sc_pp = siop_lookup_product(pa->pa_id, PCI_REVISION(pa->pa_class));
    216 	if (sc->sc_pp == NULL) {
    217 		printf("sym: broken match/attach!!\n");
    218 		return 0;
    219 	}
    220 	/* copy interesting infos about the chip */
    221 	sc->siop.features = sc->sc_pp->features;
    222 	sc->siop.maxburst = sc->sc_pp->maxburst;
    223 	sc->siop.maxoff = sc->sc_pp->maxoff;
    224 	sc->siop.clock_div = sc->sc_pp->clock_div;
    225 	sc->siop.clock_period = sc->sc_pp->clock_period;
    226 	sc->siop.ram_size = sc->sc_pp->ram_size;
    227 
    228 	sc->siop.sc_reset = siop_pci_reset;
    229 	printf(": %s\n", sc->sc_pp->name);
    230 	sc->sc_pc = pc;
    231 	sc->sc_tag = tag;
    232 	sc->siop.sc_dmat = pa->pa_dmat;
    233 
    234 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x14);
    235 	switch (memtype) {
    236 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    237 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    238 		memh_valid = (pci_mapreg_map(pa, 0x14, memtype, 0,
    239 		    &memt, &memh, &memaddr, NULL) == 0);
    240 		break;
    241 	default:
    242 		memh_valid = 0;
    243 	}
    244 
    245 	ioh_valid = (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0,
    246 	    &iot, &ioh, &ioaddr, NULL) == 0);
    247 
    248 	if (memh_valid) {
    249 		sc->siop.sc_rt = memt;
    250 		sc->siop.sc_rh = memh;
    251 		sc->siop.sc_raddr = memaddr;
    252 	} else if (ioh_valid) {
    253 		sc->siop.sc_rt = iot;
    254 		sc->siop.sc_rh = ioh;
    255 		sc->siop.sc_raddr = ioaddr;
    256 	} else {
    257 		printf("%s: unable to map device registers\n",
    258 		    sc->siop.sc_dev.dv_xname);
    259 		return 0;
    260 	}
    261 
    262 	if (sc->siop.features & SF_CHIP_RAM) {
    263 		int bar;
    264 		switch (memtype) {
    265 		case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    266 			bar = 0x18;
    267 			break;
    268 		case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    269 			bar = 0x1c;
    270 			break;
    271 		}
    272 		if (pci_mapreg_map(pa, bar, memtype, 0,
    273                     &sc->siop.sc_ramt, &sc->siop.sc_ramh,
    274 		    &sc->siop.sc_scriptaddr, NULL) == 0) {
    275 			printf("%s: using on-board RAM\n",
    276 			    sc->siop.sc_dev.dv_xname);
    277 		} else {
    278 			printf("%s: can't map on-board RAM\n",
    279 			    sc->siop.sc_dev.dv_xname);
    280 			sc->siop.features &= ~SF_CHIP_RAM;
    281 		}
    282 	}
    283 
    284 	if (pci_intr_map(pa, &intrhandle) != 0) {
    285 		printf("%s: couldn't map interrupt\n",
    286 		    sc->siop.sc_dev.dv_xname);
    287 		return 0;
    288 	}
    289 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    290 	sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
    291 	    siop_intr, &sc->siop);
    292 	if (sc->sc_ih != NULL) {
    293 		printf("%s: interrupting at %s\n",
    294 		    sc->siop.sc_dev.dv_xname,
    295 		    intrstr ? intrstr : "unknown interrupt");
    296 	} else {
    297 		printf("%s: couldn't establish interrupt",
    298 		    sc->siop.sc_dev.dv_xname);
    299 		if (intrstr != NULL)
    300 			printf(" at %s", intrstr);
    301 		printf("\n");
    302 		return 0;
    303 	}
    304 	return 1;
    305 }
    306 
    307 void
    308 siop_pci_reset(sc)
    309 	struct siop_softc *sc;
    310 {
    311 	int dmode;
    312 
    313 	dmode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE);
    314 	if (sc->features & SF_PCI_RL)
    315 		dmode |= DMODE_ERL;
    316 	if (sc->features & SF_PCI_RM)
    317 		dmode |= DMODE_ERMP;
    318 	if (sc->features & SF_PCI_BOF)
    319 		dmode |= DMODE_BOF;
    320 	if (sc->features & SF_PCI_CLS)
    321 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
    322 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL) |
    323 		    DCNTL_CLSE);
    324 	if (sc->features & SF_PCI_WRI)
    325 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    326 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
    327 		    CTEST3_WRIE);
    328 	if (sc->maxburst) {
    329 		int ctest5 = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    330 		    SIOP_CTEST5);
    331 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
    332 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) &
    333 		    ~CTEST4_BDIS);
    334 		dmode &= ~DMODE_BL_MASK;
    335 		dmode |= ((sc->maxburst - 1) << DMODE_BL_SHIFT) & DMODE_BL_MASK;
    336 		ctest5 &= ~CTEST5_BBCK;
    337 		ctest5 |= (sc->maxburst - 1) & CTEST5_BBCK;
    338 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5, ctest5);
    339 	} else {
    340 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
    341 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) |
    342 		    CTEST4_BDIS);
    343 	}
    344 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE, dmode);
    345 }
    346