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