xref: /netbsd-src/sys/dev/pci/siop_pci_common.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: siop_pci_common.c,v 1.29 2009/02/13 21:12:14 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.29 2009/02/13 21:12:14 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 static 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(u_int32_t id, int rev)
218 {
219 	const struct siop_product_desc *pp;
220 	const struct siop_product_desc *rp = NULL;
221 
222 	if (PCI_VENDOR(id) != PCI_VENDOR_SYMBIOS)
223 		return NULL;
224 
225 	for (pp = siop_products; pp->name != NULL; pp++) {
226 		if (PCI_PRODUCT(id) == pp->product && pp->revision <= rev)
227 			if (rp == NULL || pp->revision > rp->revision)
228 				rp = pp;
229 	}
230 	return rp;
231 }
232 
233 int
234 siop_pci_attach_common(struct siop_pci_common_softc *pci_sc,
235     struct siop_common_softc *siop_sc, struct pci_attach_args *pa,
236     int (*intr)(void *))
237 {
238 	pci_chipset_tag_t pc = pa->pa_pc;
239 	pcitag_t tag = pa->pa_tag;
240 	const char *intrstr;
241 	pci_intr_handle_t intrhandle;
242 	bus_space_tag_t iot, memt;
243 	bus_space_handle_t ioh, memh;
244 	pcireg_t memtype;
245 	prop_dictionary_t dict;
246 	int memh_valid, ioh_valid;
247 	bus_addr_t ioaddr, memaddr;
248 	bool use_pciclock;
249 
250 	aprint_naive(": SCSI controller\n");
251 
252 	pci_sc->sc_pp =
253 	    siop_lookup_product(pa->pa_id, PCI_REVISION(pa->pa_class));
254 	if (pci_sc->sc_pp == NULL) {
255 		aprint_error("sym: broken match/attach!!\n");
256 		return 0;
257 	}
258 	/* copy interesting infos about the chip */
259 	siop_sc->features = pci_sc->sc_pp->features;
260 #ifdef SIOP_SYMLED    /* XXX Should be a devprop! */
261 	siop_sc->features |= SF_CHIP_LED0;
262 #endif
263 	dict = device_properties(&siop_sc->sc_dev);
264 	if (prop_dictionary_get_bool(dict, "use_pciclock", &use_pciclock))
265 		if (use_pciclock)
266 			siop_sc->features |= SF_CHIP_USEPCIC;
267 	siop_sc->maxburst = pci_sc->sc_pp->maxburst;
268 	siop_sc->maxoff = pci_sc->sc_pp->maxoff;
269 	siop_sc->clock_div = pci_sc->sc_pp->clock_div;
270 	siop_sc->clock_period = pci_sc->sc_pp->clock_period;
271 	siop_sc->ram_size = pci_sc->sc_pp->ram_size;
272 
273 	siop_sc->sc_reset = siop_pci_reset;
274 	aprint_normal(": %s\n", pci_sc->sc_pp->name);
275 	pci_sc->sc_pc = pc;
276 	pci_sc->sc_tag = tag;
277 	siop_sc->sc_dmat = pa->pa_dmat;
278 
279 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x14);
280 	switch (memtype) {
281 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
282 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
283 		memh_valid = (pci_mapreg_map(pa, 0x14, memtype, 0,
284 		    &memt, &memh, &memaddr, NULL) == 0);
285 		break;
286 	default:
287 		memh_valid = 0;
288 	}
289 
290 	ioh_valid = (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0,
291 	    &iot, &ioh, &ioaddr, NULL) == 0);
292 
293 	if (memh_valid) {
294 		siop_sc->sc_rt = memt;
295 		siop_sc->sc_rh = memh;
296 		siop_sc->sc_raddr = memaddr;
297 	} else if (ioh_valid) {
298 		siop_sc->sc_rt = iot;
299 		siop_sc->sc_rh = ioh;
300 		siop_sc->sc_raddr = ioaddr;
301 	} else {
302 		aprint_error_dev(&siop_sc->sc_dev, "unable to map device registers\n");
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_dev(&siop_sc->sc_dev, "invalid memory type %d\n",
317 			    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_dev(&siop_sc->sc_dev, "using on-board RAM\n");
324 		} else {
325 			aprint_error_dev(&siop_sc->sc_dev, "can't map on-board RAM\n");
326 			siop_sc->features &= ~SF_CHIP_RAM;
327 		}
328 	}
329 
330 	if (pci_intr_map(pa, &intrhandle) != 0) {
331 		aprint_error_dev(&siop_sc->sc_dev, "couldn't map interrupt\n");
332 		return 0;
333 	}
334 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
335 	pci_sc->sc_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_BIO,
336 	    intr, siop_sc);
337 	if (pci_sc->sc_ih != NULL) {
338 		aprint_normal_dev(&siop_sc->sc_dev, "interrupting at %s\n",
339 		    intrstr ? intrstr : "unknown interrupt");
340 	} else {
341 		aprint_error_dev(&siop_sc->sc_dev, "couldn't establish interrupt");
342 		if (intrstr != NULL)
343 			aprint_normal(" at %s", intrstr);
344 		aprint_normal("\n");
345 		return 0;
346 	}
347 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
348 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
349 	    PCI_COMMAND_MASTER_ENABLE);
350 	return 1;
351 }
352 
353 void
354 siop_pci_reset(struct siop_common_softc *sc)
355 {
356 	int dmode;
357 
358 	dmode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE);
359 	if (sc->features & SF_PCI_RL)
360 		dmode |= DMODE_ERL;
361 	if (sc->features & SF_PCI_RM)
362 		dmode |= DMODE_ERMP;
363 	if (sc->features & SF_PCI_BOF)
364 		dmode |= DMODE_BOF;
365 	if (sc->features & SF_PCI_CLS)
366 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL,
367 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL) |
368 		    DCNTL_CLSE);
369 	if (sc->features & SF_PCI_WRI)
370 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3,
371 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3) |
372 		    CTEST3_WRIE);
373 	if (sc->maxburst) {
374 		int ctest5 = bus_space_read_1(sc->sc_rt, sc->sc_rh,
375 		    SIOP_CTEST5);
376 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
377 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) &
378 		    ~CTEST4_BDIS);
379 		dmode &= ~DMODE_BL_MASK;
380 		dmode |= ((sc->maxburst - 1) << DMODE_BL_SHIFT) & DMODE_BL_MASK;
381 		ctest5 &= ~CTEST5_BBCK;
382 		ctest5 |= (sc->maxburst - 1) & CTEST5_BBCK;
383 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5, ctest5);
384 	} else {
385 		bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4,
386 		    bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4) |
387 		    CTEST4_BDIS);
388 	}
389 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DMODE, dmode);
390 }
391