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