xref: /netbsd-src/sys/dev/pci/btvmeii.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: btvmeii.c,v 1.9 2003/01/31 00:07:40 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1999
5  * 	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for the Bit3/SBS PCI-VME adapter Model 2706.
31  * Uses the common Tundra Universe code.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: btvmeii.c,v 1.9 2003/01/31 00:07:40 thorpej Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
45 
46 #include <machine/bus.h>
47 #include <sys/malloc.h>
48 #include <sys/extent.h>
49 
50 #include <dev/pci/ppbreg.h>
51 
52 #include <dev/vme/vmereg.h>
53 #include <dev/vme/vmevar.h>
54 
55 #include <dev/pci/universe_pci_var.h>
56 
57 static int b3_2706_match __P((struct device *, struct cfdata *, void *));
58 static void b3_2706_attach __P((struct device *, struct device *, void *));
59 
60 /* exported via tag structs */
61 int b3_2706_map_vme __P((void *, vme_addr_t, vme_size_t,
62 		      vme_am_t, vme_datasize_t, vme_swap_t,
63 		      bus_space_tag_t *, bus_space_handle_t *, vme_mapresc_t*));
64 void b3_2706_unmap_vme __P((void *, vme_mapresc_t));
65 
66 int b3_2706_vme_probe __P((void *, vme_addr_t, vme_size_t, vme_am_t,
67 			vme_datasize_t,
68 			int (*)(void *, bus_space_tag_t, bus_space_handle_t),
69 			void *));
70 
71 int b3_2706_map_vmeint __P((void *, int, int, vme_intr_handle_t *));
72 void *b3_2706_establish_vmeint __P((void *, vme_intr_handle_t, int,
73 				 int (*)(void *), void *));
74 void b3_2706_disestablish_vmeint __P((void *, void *));
75 void b3_2706_vmeint __P((void *, int, int));
76 
77 int b3_2706_dmamap_create __P((void *, vme_size_t,
78 			    vme_am_t, vme_datasize_t, vme_swap_t,
79 			    int, vme_size_t, vme_addr_t,
80 			    int, bus_dmamap_t *));
81 void b3_2706_dmamap_destroy __P((void *, bus_dmamap_t));
82 
83 int b3_2706_dmamem_alloc __P((void *, vme_size_t,
84 			      vme_am_t, vme_datasize_t, vme_swap_t,
85 			      bus_dma_segment_t *, int, int *, int));
86 void b3_2706_dmamem_free __P((void *, bus_dma_segment_t *, int));
87 
88 struct b3_2706_vmemaprescs {
89 	int wnd;
90 	unsigned long pcibase, maplen;
91 	bus_space_handle_t handle;
92 	u_int32_t len;
93 };
94 
95 struct b3_2706_vmeintrhand {
96 	TAILQ_ENTRY(b3_2706_vmeintrhand) ih_next;
97 	int (*ih_fun) __P((void*));
98 	void *ih_arg;
99 	int ih_level;
100 	int ih_vector;
101 	int ih_prior;
102 	u_long ih_count;
103 };
104 
105 struct b3_2706_softc {
106 	struct device sc_dev;
107 	struct univ_pci_data univdata;
108 	bus_space_tag_t swapt, vmet;
109 	bus_space_handle_t swaph;
110 	bus_addr_t vmepbase;
111 
112 	int windowused[8];
113 	struct b3_2706_vmemaprescs vmemaprescs[8];
114 	struct extent *vmeext;
115 	char vmemap[EXTENT_FIXED_STORAGE_SIZE(8)];
116 
117 	struct vme_chipset_tag sc_vct;
118 
119 	/* list of VME interrupt handlers */
120 	TAILQ_HEAD(, b3_2706_vmeintrhand) intrhdls;
121 	int strayintrs;
122 };
123 
124 CFATTACH_DECL(btvmeii, sizeof(struct b3_2706_softc),
125     b3_2706_match, b3_2706_attach, NULL, NULL);
126 
127 /*
128  * The adapter consists of a DEC PCI-PCI-bridge with two
129  * PCI devices behind it: A Tundra Universe as device 4 and
130  * some FPGA with glue logics as device 8.
131  * As long as the autoconf code doesn't provide more support
132  * for dependant devices, we have to duplicate a part of the
133  * "ppb" functions here.
134  */
135 
136 static int
137 b3_2706_match(parent, match, aux)
138 	struct device *parent;
139 	struct cfdata *match;
140 	void *aux;
141 {
142 	struct pci_attach_args *pa = aux;
143 	pci_chipset_tag_t pc = pa->pa_pc;
144 	int secbus;
145 	pcitag_t tag;
146 	pcireg_t id;
147 
148 	if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC)
149 	    || (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_DEC_21152))
150 		return (0);
151 
152 	secbus = PPB_BUSINFO_SECONDARY(pci_conf_read(pc, pa->pa_tag,
153 						     PPB_REG_BUSINFO));
154 	if (secbus == 0) {
155 		printf("b3_2706_match: ppb not configured\n");
156 		return (0);
157 	}
158 
159 	tag = pci_make_tag(pc, secbus, 4, 0);
160 	id = pci_conf_read(pc, tag, PCI_ID_REG);
161 
162 	if ((PCI_VENDOR(id) != PCI_VENDOR_NEWBRIDGE)
163 	    || (PCI_PRODUCT(id) != PCI_PRODUCT_NEWBRIDGE_CA91CX42)) {
164 #ifdef DEBUG
165 		printf("b3_2706_match: no tundra\n");
166 #endif
167 		return (0);
168 	}
169 
170 	tag = pci_make_tag(pc, secbus, 8, 0);
171 	id = pci_conf_read(pc, tag, PCI_ID_REG);
172 
173 	if ((PCI_VENDOR(id) != PCI_VENDOR_BIT3)
174 	    || (PCI_PRODUCT(id) != PCI_PRODUCT_BIT3_PCIVME2706)) {
175 #ifdef DEBUG
176 		printf("b3_2706_match: no bit3 chip\n");
177 #endif
178 		return (0);
179 	}
180 
181 	return (5); /* beat "ppb" */
182 }
183 
184 static void
185 b3_2706_attach(parent, self, aux)
186 	struct device *parent, *self;
187 	void *aux;
188 {
189 	struct b3_2706_softc *sc = (struct b3_2706_softc *)self;
190 	struct pci_attach_args *pa = aux;
191 	pci_chipset_tag_t pc = pa->pa_pc;
192 	struct pci_attach_args aa;
193 	int secbus;
194 	pcireg_t intr;
195 	pcitag_t tag;
196 	bus_addr_t swappbase;
197 	int i;
198 
199 	struct vmebus_attach_args vaa;
200 
201 	aprint_naive(": VME bus adapter\n");
202 	aprint_normal("\n");
203 
204 	secbus = PPB_BUSINFO_SECONDARY(pci_conf_read(pc, pa->pa_tag,
205 						     PPB_REG_BUSINFO));
206 
207 	memcpy(&aa, pa, sizeof(struct pci_attach_args));
208 	aa.pa_device = 4;
209 	aa.pa_function = 0;
210 	aa.pa_tag = pci_make_tag(pc, secbus, 4, 0);
211 	aa.pa_intrswiz += 4;
212 	intr = pci_conf_read(pc, aa.pa_tag, PCI_INTERRUPT_REG);
213 	/*
214 	 * swizzle it based on the number of
215 	 * busses we're behind and our device
216 	 * number.
217 	 */
218 	aa.pa_intrpin =	((1 + aa.pa_intrswiz - 1) % 4) + 1;
219 	aa.pa_intrline = PCI_INTERRUPT_LINE(intr);
220 
221 	if (univ_pci_attach(&sc->univdata, &aa, self->dv_xname,
222 			    b3_2706_vmeint, sc)) {
223 		aprint_error("%s: error initializing universe chip\n",
224 		       self->dv_xname);
225 		return;
226 	}
227 
228 	/*
229 	 * don't waste KVM - the byteswap register is aliased in
230 	 * a 512k window, we need it only once
231 	 */
232 	tag = pci_make_tag(pc, secbus, 8, 0);
233 	sc->swapt = pa->pa_memt;
234 	if (pci_mapreg_info(pc, tag, 0x10,
235 			    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
236 			    &swappbase, 0, 0) ||
237 	    bus_space_map(sc->swapt, swappbase, 4, 0, &sc->swaph)) {
238 		aprint_error("%s: can't map byteswap register\n",
239 		    self->dv_xname);
240 		return;
241 	}
242 	/*
243 	 * Set up cycle specific byteswap mode.
244 	 * XXX Readback yields "all-ones" for me, and it doesn't seem
245 	 * to matter what I write into the register - the data don't
246 	 * get swapped. Adapter fault or documentation bug?
247 	 */
248 	bus_space_write_4(sc->swapt, sc->swaph, 0, 0x00000490);
249 
250 	/* VME space is mapped as needed */
251 	sc->vmet = pa->pa_memt;
252 	if (pci_mapreg_info(pc, tag, 0x14,
253 			    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
254 			    &sc->vmepbase, 0, 0)) {
255 		aprint_error("%s: VME range not assigned\n", self->dv_xname);
256 		return;
257 	}
258 #ifdef BIT3DEBUG
259 	aprint_debug("%s: VME window @%lx\n", self->dv_xname,
260 	    (long)sc->vmepbase);
261 #endif
262 
263 	for (i = 0; i < 8; i++) {
264 		sc->windowused[i] = 0;
265 	}
266 	sc->vmeext = extent_create("pcivme", sc->vmepbase,
267 				   sc->vmepbase + 32*1024*1024 - 1, M_DEVBUF,
268 				   sc->vmemap, sizeof(sc->vmemap),
269 				   EX_NOCOALESCE);
270 
271 	sc->sc_vct.cookie = self;
272 	sc->sc_vct.vct_probe = b3_2706_vme_probe;
273 	sc->sc_vct.vct_map = b3_2706_map_vme;
274 	sc->sc_vct.vct_unmap = b3_2706_unmap_vme;
275 	sc->sc_vct.vct_int_map = b3_2706_map_vmeint;
276 	sc->sc_vct.vct_int_establish = b3_2706_establish_vmeint;
277 	sc->sc_vct.vct_int_disestablish = b3_2706_disestablish_vmeint;
278 	sc->sc_vct.vct_dmamap_create = b3_2706_dmamap_create;
279 	sc->sc_vct.vct_dmamap_destroy = b3_2706_dmamap_destroy;
280 	sc->sc_vct.vct_dmamem_alloc = b3_2706_dmamem_alloc;
281 	sc->sc_vct.vct_dmamem_free = b3_2706_dmamem_free;
282 
283 	vaa.va_vct = &(sc->sc_vct);
284 	vaa.va_bdt = pa->pa_dmat; /* XXX */
285 	vaa.va_slaveconfig = 0; /* XXX CSR window? */
286 
287 	config_found(self, &vaa, 0);
288 }
289 
290 #define sc ((struct b3_2706_softc*)vsc)
291 
292 int
293 b3_2706_map_vme(vsc, vmeaddr, len, am, datasizes, swap, tag, handle, resc)
294 	void *vsc;
295 	vme_addr_t vmeaddr;
296 	vme_size_t len;
297 	vme_am_t am;
298 	vme_datasize_t datasizes;
299 	vme_swap_t swap;
300 	bus_space_tag_t *tag;
301 	bus_space_handle_t *handle;
302 	vme_mapresc_t *resc;
303 {
304 	int idx, i, wnd, res;
305 	unsigned long boundary, maplen, pcibase;
306 	vme_addr_t vmebase, vmeend;
307 	static int windoworder[8] = {1, 2, 3, 5, 6, 7, 0, 4};
308 
309 	/* prefer windows with fine granularity for small mappings */
310 	wnd = -1;
311 	if (len <= 32*1024)
312 		idx = 6;
313 	else
314 		idx = 0;
315 	for (i = 0; i < 8; i++) {
316 		if (!sc->windowused[windoworder[idx]]) {
317 			wnd = windoworder[idx];
318 			sc->windowused[wnd] = 1;
319 			break;
320 		}
321 		idx = (idx + 1) % 8;
322 	}
323 	if (wnd == -1)
324 		return (ENOSPC);
325 
326 	boundary = (wnd & 3) ? 64*1024 : 4*1024;
327 
328 	/* first mapped address */
329 	vmebase = vmeaddr & ~(boundary - 1);
330 	/* base of last mapped page */
331 	vmeend = (vmeaddr + len - 1) & ~(boundary - 1);
332 	/* bytes in outgoing window required */
333 	maplen = vmeend - vmebase + boundary;
334 
335 	if (extent_alloc(sc->vmeext, maplen, boundary, 0, EX_FAST, &pcibase)) {
336 		sc->windowused[wnd] = 0;
337 		return (ENOMEM);
338 	}
339 
340 	res = univ_pci_mapvme(&sc->univdata, wnd, vmebase, maplen,
341 			      am, datasizes, pcibase);
342 	if (res) {
343 		extent_free(sc->vmeext, pcibase, maplen, 0);
344 		sc->windowused[wnd] = 0;
345 		return (res);
346 	}
347 
348 	res = bus_space_map(sc->vmet, pcibase + (vmeaddr - vmebase), len,
349 			    0, handle);
350 	if (res) {
351 		univ_pci_unmapvme(&sc->univdata, wnd);
352 		extent_free(sc->vmeext, pcibase, maplen, 0);
353 		sc->windowused[wnd] = 0;
354 		return (res);
355 	}
356 
357 	*tag = sc->vmet;
358 
359 	/*
360 	 * save all data needed for later unmapping
361 	 */
362 	sc->vmemaprescs[wnd].wnd = wnd;
363 	sc->vmemaprescs[wnd].pcibase = pcibase;
364 	sc->vmemaprescs[wnd].maplen = maplen;
365 	sc->vmemaprescs[wnd].handle = *handle;
366 	sc->vmemaprescs[wnd].len = len;
367 	*resc = &sc->vmemaprescs[wnd];
368 	return (0);
369 }
370 
371 void
372 b3_2706_unmap_vme(vsc, resc)
373 	void *vsc;
374 	vme_mapresc_t resc;
375 {
376 	struct b3_2706_vmemaprescs *r = resc;
377 
378 	bus_space_unmap(sc->vmet, r->handle, r->len);
379 	extent_free(sc->vmeext, r->pcibase, r->maplen, 0);
380 
381 	if (!sc->windowused[r->wnd])
382 		panic("b3_2706_unmap_vme: bad window");
383 	univ_pci_unmapvme(&sc->univdata, r->wnd);
384 	sc->windowused[r->wnd] = 0;
385 }
386 
387 int
388 b3_2706_vme_probe(vsc, addr, len, am, datasize, callback, cbarg)
389 	void *vsc;
390 	vme_addr_t addr;
391 	vme_size_t len;
392 	vme_am_t am;
393 	vme_datasize_t datasize;
394 	int (*callback) __P((void *, bus_space_tag_t, bus_space_handle_t));
395 	void *cbarg;
396 {
397 	bus_space_tag_t tag;
398 	bus_space_handle_t handle;
399 	vme_mapresc_t resc;
400 	int res, i;
401 	volatile u_int32_t dummy;
402 
403 	res = b3_2706_map_vme(vsc, addr, len, am, datasize, 0,
404 			      &tag, &handle, &resc);
405 	if (res)
406 		return (res);
407 
408 	if (univ_pci_vmebuserr(&sc->univdata, 1))
409 		printf("b3_2706_vme_badaddr: TA bit not clean - reset\n");
410 
411 	if (callback)
412 		res = (*callback)(cbarg, tag, handle);
413 	else {
414 		for (i = 0; i < len;) {
415 			switch (datasize) {
416 			    case VME_D8:
417 				dummy = bus_space_read_1(tag, handle, i);
418 				i++;
419 				break;
420 			    case VME_D16:
421 				dummy = bus_space_read_2(tag, handle, i);
422 				i += 2;
423 				break;
424 			    case VME_D32:
425 				dummy = bus_space_read_4(tag, handle, i);
426 				i += 4;
427 				break;
428 			    default:
429 				panic("b3_2706_vme_probe: invalid datasize %x",
430 				      datasize);
431 			}
432 		}
433 	}
434 
435 	if (univ_pci_vmebuserr(&sc->univdata, 0)) {
436 #ifdef BIT3DEBUG
437 		printf("b3_2706_vme_badaddr: caught TA\n");
438 #endif
439 		univ_pci_vmebuserr(&sc->univdata, 1);
440 		res = EIO;
441 	}
442 
443 	b3_2706_unmap_vme(vsc, resc);
444 	return (res);
445 }
446 
447 int
448 b3_2706_map_vmeint(vsc, level, vector, handlep)
449 	void *vsc;
450 	int level, vector;
451 	vme_intr_handle_t *handlep;
452 {
453 
454 	*handlep = (void *)(long)((level << 8) | vector); /* XXX */
455 	return (0);
456 }
457 
458 void *
459 b3_2706_establish_vmeint(vsc, handle, prior, func, arg)
460 	void *vsc;
461 	vme_intr_handle_t handle;
462 	int prior;
463 	int (*func) __P((void *));
464 	void *arg;
465 {
466 	struct b3_2706_vmeintrhand *ih;
467 	long lv;
468 	int s;
469 
470 	/* no point in sleeping unless someone can free memory. */
471 	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
472 	if (ih == NULL)
473 		panic("b3_2706_map_vmeint: can't malloc handler info");
474 
475 	lv = (long)handle; /* XXX */
476 
477 	ih->ih_fun = func;
478 	ih->ih_arg = arg;
479 	ih->ih_level = lv >> 8;
480 	ih->ih_vector = lv & 0xff;
481 	ih->ih_prior = prior;
482 	ih->ih_count = 0;
483 
484 	s = splhigh();
485 	TAILQ_INSERT_TAIL(&(sc->intrhdls), ih, ih_next);
486 	splx(s);
487 
488 	return (ih);
489 }
490 
491 void
492 b3_2706_disestablish_vmeint(vsc, cookie)
493 	void *vsc;
494 	void *cookie;
495 {
496 	struct b3_2706_vmeintrhand *ih = cookie;
497 	int s;
498 
499 	if (!ih) {
500 		printf("b3_2706_unmap_vmeint: NULL arg\n");
501 		return;
502 	}
503 
504 	s = splhigh();
505 	TAILQ_REMOVE(&(sc->intrhdls), ih, ih_next);
506 	splx(s);
507 
508 	free(ih, M_DEVBUF);
509 }
510 
511 void
512 b3_2706_vmeint(vsc, level, vector)
513 	void *vsc;
514 	int level, vector;
515 {
516 	struct b3_2706_vmeintrhand *ih;
517 	int found;
518 
519 #ifdef BIT3DEBUG
520 	printf("b3_2706_vmeint: VME IRQ %d, vec %x\n", level, vector);
521 #endif
522 	found = 0;
523 
524 	for (ih = sc->intrhdls.tqh_first; ih;
525 	     ih = ih->ih_next.tqe_next) {
526 		if ((ih->ih_level == level) &&
527 		    ((ih->ih_vector == -1) ||
528 		     (ih->ih_vector == vector))) {
529 			int s, res;
530 			/*
531 			 * We should raise the interrupt level
532 			 * to ih->ih_prior here. How to do this
533 			 * machine-independantly?
534 			 * To be safe, raise to the maximum.
535 			 */
536 			s = splhigh();
537 			found |= (res = (*(ih->ih_fun))(ih->ih_arg));
538 			splx(s);
539 			if (res)
540 				ih->ih_count++;
541 			if (res == 1)
542 				break;
543 		}
544 	}
545 	if (!found)
546 		sc->strayintrs++;
547 }
548 
549 int
550 b3_2706_dmamap_create(vsc, len, am, datasize, swap,
551 		      nsegs, segsz, bound,
552 		      flags, mapp)
553 	void *vsc;
554 	vme_size_t len;
555 	vme_am_t am;
556 	vme_datasize_t datasize;
557 	vme_swap_t swap;
558 	int nsegs;
559 	vme_size_t segsz;
560 	vme_addr_t bound;
561 	int flags;
562 	bus_dmamap_t *mapp;
563 {
564 	return (EINVAL);
565 }
566 
567 void
568 b3_2706_dmamap_destroy(vsc, map)
569 	void *vsc;
570 	bus_dmamap_t map;
571 {
572 }
573 
574 int
575 b3_2706_dmamem_alloc(vsc, len, am, datasizes, swap, segs, nsegs, rsegs, flags)
576 	void *vsc;
577 	vme_size_t len;
578 	vme_am_t am;
579 	vme_datasize_t datasizes;
580 	vme_swap_t swap;
581 	bus_dma_segment_t *segs;
582 	int nsegs;
583 	int *rsegs;
584 	int flags;
585 {
586 	return (EINVAL);
587 }
588 
589 void
590 b3_2706_dmamem_free(vsc, segs, nsegs)
591 	void *vsc;
592 	bus_dma_segment_t *segs;
593 	int nsegs;
594 {
595 }
596 
597 #undef sc
598