Home | History | Annotate | Line # | Download | only in pci
siop_pci_common.c revision 1.13
      1 /*	$NetBSD: siop_pci_common.c,v 1.13 2002/04/23 10:38:38 bouyer 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: siop_pci_common.c,v 1.13 2002/04/23 10:38:38 bouyer Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/buf.h>
     42 #include <sys/kernel.h>
     43 
     44 #include <machine/endian.h>
     45 
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcidevs.h>
     49 
     50 #include <dev/scsipi/scsipi_all.h>
     51 #include <dev/scsipi/scsipiconf.h>
     52 
     53 #include <dev/ic/siopreg.h>
     54 #include <dev/ic/siopvar_common.h>
     55 #include <dev/pci/siop_pci_common.h>
     56 
     57 /* List (array, really :) of chips we know how to handle */
     58 const struct siop_product_desc siop_products[] = {
     59 	{ PCI_PRODUCT_SYMBIOS_810,
     60 	0x00,
     61 	"Symbios Logic 53c810 (fast scsi)",
     62 	SF_PCI_RL | SF_CHIP_LS,
     63 	4, 8, 3, 250, 0
     64 	},
     65 	{ PCI_PRODUCT_SYMBIOS_810,
     66 	0x10,
     67 	"Symbios Logic 53c810a (fast scsi)",
     68 	SF_PCI_RL | SF_PCI_BOF | SF_CHIP_PF | SF_CHIP_LS,
     69 	4, 8, 3, 250, 0
     70 	},
     71 	{ PCI_PRODUCT_SYMBIOS_815,
     72 	0x00,
     73 	"Symbios Logic 53c815 (fast scsi)",
     74 	SF_PCI_RL | SF_PCI_BOF,
     75 	4, 8, 3, 250, 0
     76 	},
     77 	{ PCI_PRODUCT_SYMBIOS_820,
     78 	0x00,
     79 	"Symbios Logic 53c820 (fast wide scsi)",
     80 	SF_PCI_RL | SF_CHIP_LS | SF_BUS_WIDE,
     81 	4, 8, 3, 250, 0
     82 	},
     83 	{ PCI_PRODUCT_SYMBIOS_825,
     84 	0x00,
     85 	"Symbios Logic 53c825 (fast wide scsi)",
     86 	SF_PCI_RL | SF_PCI_BOF | SF_BUS_WIDE,
     87 	4, 8, 3, 250, 0
     88 	},
     89 	{ PCI_PRODUCT_SYMBIOS_825,
     90 	0x10,
     91 	"Symbios Logic 53c825a (fast wide scsi)",
     92 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
     93 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_LS | SF_CHIP_10REGS |
     94 	SF_BUS_WIDE,
     95 	7, 8, 3, 250, 4096
     96 	},
     97 	{ PCI_PRODUCT_SYMBIOS_860,
     98 	0x00,
     99 	"Symbios Logic 53c860 (ultra scsi)",
    100 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    101 	SF_CHIP_PF | SF_CHIP_LS |
    102 	SF_BUS_ULTRA,
    103 	4, 8, 5, 125, 0
    104 	},
    105 	{ PCI_PRODUCT_SYMBIOS_875,
    106 	0x00,
    107 	"Symbios Logic 53c875 (ultra-wide scsi)",
    108 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    109 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_LS | SF_CHIP_10REGS |
    110 	SF_BUS_ULTRA | SF_BUS_WIDE,
    111 	7, 16, 5, 125, 4096
    112 	},
    113 	{ PCI_PRODUCT_SYMBIOS_875,
    114 	0x02,
    115 	"Symbios Logic 53c875 (ultra-wide scsi)",
    116 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    117 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    118 	SF_CHIP_LS | SF_CHIP_10REGS |
    119 	SF_BUS_ULTRA | SF_BUS_WIDE,
    120 	7, 16, 5, 125, 4096
    121 	},
    122 	{ PCI_PRODUCT_SYMBIOS_875J,
    123 	0x00,
    124 	"Symbios Logic 53c875j (ultra-wide scsi)",
    125 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    126 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    127 	SF_CHIP_LS | SF_CHIP_10REGS |
    128 	SF_BUS_ULTRA | SF_BUS_WIDE,
    129 	7, 16, 5, 125, 4096
    130 	},
    131 	{ PCI_PRODUCT_SYMBIOS_885,
    132 	0x00,
    133 	"Symbios Logic 53c885 (ultra-wide scsi)",
    134 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    135 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_DBLR |
    136 	SF_CHIP_LS | SF_CHIP_10REGS |
    137 	SF_BUS_ULTRA | SF_BUS_WIDE,
    138 	7, 16, 5, 125, 4096
    139 	},
    140 	{ PCI_PRODUCT_SYMBIOS_895,
    141 	0x00,
    142 	"Symbios Logic 53c895 (ultra2-wide scsi)",
    143 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    144 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    145 	SF_CHIP_LS | SF_CHIP_10REGS |
    146 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    147 	7, 31, 7, 62, 4096
    148 	},
    149 	{ PCI_PRODUCT_SYMBIOS_896,
    150 	0x00,
    151 	"Symbios Logic 53c896 (ultra2-wide scsi)",
    152 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    153 	SF_CHIP_LEDC | SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    154 	SF_CHIP_LS | SF_CHIP_10REGS |
    155 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    156 	7, 31, 7, 62, 8192
    157 	},
    158 	{ PCI_PRODUCT_SYMBIOS_895A,
    159 	0x00,
    160 	"Symbios Logic 53c895a (ultra2-wide scsi)",
    161 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    162 	SF_CHIP_LEDC | SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    163 	SF_CHIP_LS | SF_CHIP_10REGS |
    164 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    165 	7, 31, 7, 62, 8192
    166 	},
    167 	{ PCI_PRODUCT_SYMBIOS_1010,
    168 	0x00,
    169 	"Symbios Logic 53c1010-33 (ultra2-wide scsi)",
    170 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    171 	SF_CHIP_LEDC | SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM |
    172 	SF_CHIP_LS | SF_CHIP_10REGS | SF_CHIP_DFBC | SF_CHIP_DBLR |
    173 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    174 	7, 31, 7, 62, 8192
    175 	},
    176 	{ PCI_PRODUCT_SYMBIOS_1010_2,
    177 	0x00,
    178 	"Symbios Logic 53c1010-66 (ultra2-wide scsi)",
    179 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    180 	SF_CHIP_LEDC | SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM |
    181 	SF_CHIP_LS | SF_CHIP_10REGS | SF_CHIP_DFBC | SF_CHIP_DBLR |
    182 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    183 	7, 31, 7, 62, 8192
    184 	},
    185 	{ PCI_PRODUCT_SYMBIOS_1510D,
    186 	0x00,
    187 	"Symbios Logic 53c1510d (ultra2-wide scsi)",
    188 	SF_PCI_RL | SF_PCI_CLS | SF_PCI_WRI | SF_PCI_RM |
    189 	SF_CHIP_FIFO | SF_CHIP_PF | SF_CHIP_RAM | SF_CHIP_QUAD |
    190 	SF_CHIP_LS | SF_CHIP_10REGS |
    191 	SF_BUS_ULTRA2 | SF_BUS_WIDE,
    192 	7, 31, 7, 62, 4096
    193 	},
    194 	{ 0,
    195 	0x00,
    196 	NULL,
    197 	0x00,
    198 	0, 0, 0, 0, 0
    199 	},
    200 };
    201 
    202 const struct siop_product_desc *
    203 siop_lookup_product(id, rev)
    204 	u_int32_t id;
    205 	int rev;
    206 {
    207 	const struct siop_product_desc *pp;
    208 	const struct siop_product_desc *rp = NULL;
    209 
    210 	if (PCI_VENDOR(id) != PCI_VENDOR_SYMBIOS)
    211 		return NULL;
    212 
    213 	for (pp = siop_products; pp->name != NULL; pp++) {
    214 		if (PCI_PRODUCT(id) == pp->product && pp->revision <= rev)
    215 			if (rp == NULL || pp->revision > rp->revision)
    216 				rp = pp;
    217 	}
    218 	return rp;
    219 }
    220 
    221 int
    222 siop_pci_attach_common(pci_sc, siop_sc, pa, intr)
    223 	struct siop_pci_common_softc *pci_sc;
    224 	struct siop_common_softc *siop_sc;
    225 	struct pci_attach_args *pa;
    226 	int (*intr) __P((void*));
    227 
    228 {
    229 	pci_chipset_tag_t pc = pa->pa_pc;
    230 	pcitag_t tag = pa->pa_tag;
    231 	const char *intrstr;
    232 	pci_intr_handle_t intrhandle;
    233 	bus_space_tag_t iot, memt;
    234 	bus_space_handle_t ioh, memh;
    235 	pcireg_t memtype;
    236 	int memh_valid, ioh_valid;
    237 	bus_addr_t ioaddr, memaddr;
    238 
    239 	pci_sc->sc_pp =
    240 	    siop_lookup_product(pa->pa_id, PCI_REVISION(pa->pa_class));
    241 	if (pci_sc->sc_pp == NULL) {
    242 		printf("sym: broken match/attach!!\n");
    243 		return 0;
    244 	}
    245 	/* copy interesting infos about the chip */
    246 	siop_sc->features = pci_sc->sc_pp->features;
    247 #ifdef SIOP_SYMLED    /* XXX Should be a devprop! */
    248 	siop_sc->features |= SF_CHIP_LED0;
    249 #endif
    250 	siop_sc->maxburst = pci_sc->sc_pp->maxburst;
    251 	siop_sc->maxoff = pci_sc->sc_pp->maxoff;
    252 	siop_sc->clock_div = pci_sc->sc_pp->clock_div;
    253 	siop_sc->clock_period = pci_sc->sc_pp->clock_period;
    254 	siop_sc->ram_size = pci_sc->sc_pp->ram_size;
    255 
    256 	siop_sc->sc_reset = siop_pci_reset;
    257 	printf(": %s\n", pci_sc->sc_pp->name);
    258 	pci_sc->sc_pc = pc;
    259 	pci_sc->sc_tag = tag;
    260 	siop_sc->sc_dmat = pa->pa_dmat;
    261 
    262 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x14);
    263 	switch (memtype) {
    264 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    265 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    266 		memh_valid = (pci_mapreg_map(pa, 0x14, memtype, 0,
    267 		    &memt, &memh, &memaddr, NULL) == 0);
    268 		break;
    269 	default:
    270 		memh_valid = 0;
    271 	}
    272 
    273 	ioh_valid = (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0,
    274 	    &iot, &ioh, &ioaddr, NULL) == 0);
    275 
    276 	if (memh_valid) {
    277 		siop_sc->sc_rt = memt;
    278 		siop_sc->sc_rh = memh;
    279 		siop_sc->sc_raddr = memaddr;
    280 	} else if (ioh_valid) {
    281 		siop_sc->sc_rt = iot;
    282 		siop_sc->sc_rh = ioh;
    283 		siop_sc->sc_raddr = ioaddr;
    284 	} else {
    285 		printf("%s: unable to map device registers\n",
    286 		    siop_sc->sc_dev.dv_xname);
    287 		return 0;
    288 	}
    289 
    290 	if (siop_sc->features & SF_CHIP_RAM) {
    291 		int bar;
    292 		switch (memtype) {
    293 		case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    294 			bar = 0x18;
    295 			break;
    296 		case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    297 			bar = 0x1c;
    298 			break;
    299 		}
    300 		if (pci_mapreg_map(pa, bar, memtype, 0,
    301                     &siop_sc->sc_ramt, &siop_sc->sc_ramh,
    302 		    &siop_sc->sc_scriptaddr, NULL) == 0) {
    303 			printf("%s: using on-board RAM\n",
    304 			    siop_sc->sc_dev.dv_xname);
    305 		} else {
    306 			printf("%s: can't map on-board RAM\n",
    307 			    siop_sc->sc_dev.dv_xname);
    308 			siop_sc->features &= ~SF_CHIP_RAM;
    309 		}
    310 	}
    311 
    312 	if (pci_intr_map(pa, &intrhandle) != 0) {
    313 		printf("%s: couldn't map interrupt\n",
    314 		    siop_sc->sc_dev.dv_xname);
    315 		return 0;
    316 	}
    317 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    318 	pci_sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
    319 	    intr, siop_sc);
    320 	if (pci_sc->sc_ih != NULL) {
    321 		printf("%s: interrupting at %s\n",
    322 		    siop_sc->sc_dev.dv_xname,
    323 		    intrstr ? intrstr : "unknown interrupt");
    324 	} else {
    325 		printf("%s: couldn't establish interrupt",
    326 		    siop_sc->sc_dev.dv_xname);
    327 		if (intrstr != NULL)
    328 			printf(" at %s", intrstr);
    329 		printf("\n");
    330 		return 0;
    331 	}
    332 	return 1;
    333 }
    334 
    335 void
    336 siop_pci_reset(sc)
    337 	struct siop_common_softc *sc;
    338 {
    339 	int dmode;
    340 
    341 	dmode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE);
    342 	if (sc->features & SF_PCI_RL)
    343 		dmode |= DMODE_ERL;
    344 	if (sc->features & SF_PCI_RM)
    345 		dmode |= DMODE_ERMP;
    346 	if (sc->features & SF_PCI_BOF)
    347 		dmode |= DMODE_BOF;
    348 	if (sc->features & SF_PCI_CLS)
    349 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
    350 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL) |
    351 		    DCNTL_CLSE);
    352 	if (sc->features & SF_PCI_WRI)
    353 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
    354 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
    355 		    CTEST3_WRIE);
    356 	if (sc->maxburst) {
    357 		int ctest5 = bus_space_read_1(sc->sc_rt, sc->sc_rh,
    358 		    SIOP_CTEST5);
    359 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
    360 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) &
    361 		    ~CTEST4_BDIS);
    362 		dmode &= ~DMODE_BL_MASK;
    363 		dmode |= ((sc->maxburst - 1) << DMODE_BL_SHIFT) & DMODE_BL_MASK;
    364 		ctest5 &= ~CTEST5_BBCK;
    365 		ctest5 |= (sc->maxburst - 1) & CTEST5_BBCK;
    366 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5, ctest5);
    367 	} else {
    368 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
    369 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) |
    370 		    CTEST4_BDIS);
    371 	}
    372 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE, dmode);
    373 }
    374