btvmeii.c revision 1.7 1 /* $NetBSD: btvmeii.c,v 1.7 2002/09/30 20:37:11 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.7 2002/09/30 20:37:11 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 printf("\n");
202
203 secbus = PPB_BUSINFO_SECONDARY(pci_conf_read(pc, pa->pa_tag,
204 PPB_REG_BUSINFO));
205
206 memcpy(&aa, pa, sizeof(struct pci_attach_args));
207 aa.pa_device = 4;
208 aa.pa_function = 0;
209 aa.pa_tag = pci_make_tag(pc, secbus, 4, 0);
210 aa.pa_intrswiz += 4;
211 intr = pci_conf_read(pc, aa.pa_tag, PCI_INTERRUPT_REG);
212 /*
213 * swizzle it based on the number of
214 * busses we're behind and our device
215 * number.
216 */
217 aa.pa_intrpin = ((1 + aa.pa_intrswiz - 1) % 4) + 1;
218 aa.pa_intrline = PCI_INTERRUPT_LINE(intr);
219
220 if (univ_pci_attach(&sc->univdata, &aa, self->dv_xname,
221 b3_2706_vmeint, sc)) {
222 printf("%s: error initializing universe chip\n",
223 self->dv_xname);
224 return;
225 }
226
227 /*
228 * don't waste KVM - the byteswap register is aliased in
229 * a 512k window, we need it only once
230 */
231 tag = pci_make_tag(pc, secbus, 8, 0);
232 sc->swapt = pa->pa_memt;
233 if (pci_mapreg_info(pc, tag, 0x10,
234 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
235 &swappbase, 0, 0) ||
236 bus_space_map(sc->swapt, swappbase, 4, 0, &sc->swaph)) {
237 printf("%s: can't map byteswap register\n", self->dv_xname);
238 return;
239 }
240 /*
241 * Set up cycle specific byteswap mode.
242 * XXX Readback yields "all-ones" for me, and it doesn't seem
243 * to matter what I write into the register - the data don't
244 * get swapped. Adapter fault or documentation bug?
245 */
246 bus_space_write_4(sc->swapt, sc->swaph, 0, 0x00000490);
247
248 /* VME space is mapped as needed */
249 sc->vmet = pa->pa_memt;
250 if (pci_mapreg_info(pc, tag, 0x14,
251 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
252 &sc->vmepbase, 0, 0)) {
253 printf("%s: VME range not assigned\n", self->dv_xname);
254 return;
255 }
256 #ifdef BIT3DEBUG
257 printf("%s: VME window @%lx\n", self->dv_xname, (long)sc->vmepbase);
258 #endif
259
260 for (i = 0; i < 8; i++) {
261 sc->windowused[i] = 0;
262 }
263 sc->vmeext = extent_create("pcivme", sc->vmepbase,
264 sc->vmepbase + 32*1024*1024 - 1, M_DEVBUF,
265 sc->vmemap, sizeof(sc->vmemap),
266 EX_NOCOALESCE);
267
268 sc->sc_vct.cookie = self;
269 sc->sc_vct.vct_probe = b3_2706_vme_probe;
270 sc->sc_vct.vct_map = b3_2706_map_vme;
271 sc->sc_vct.vct_unmap = b3_2706_unmap_vme;
272 sc->sc_vct.vct_int_map = b3_2706_map_vmeint;
273 sc->sc_vct.vct_int_establish = b3_2706_establish_vmeint;
274 sc->sc_vct.vct_int_disestablish = b3_2706_disestablish_vmeint;
275 sc->sc_vct.vct_dmamap_create = b3_2706_dmamap_create;
276 sc->sc_vct.vct_dmamap_destroy = b3_2706_dmamap_destroy;
277 sc->sc_vct.vct_dmamem_alloc = b3_2706_dmamem_alloc;
278 sc->sc_vct.vct_dmamem_free = b3_2706_dmamem_free;
279
280 vaa.va_vct = &(sc->sc_vct);
281 vaa.va_bdt = pa->pa_dmat; /* XXX */
282 vaa.va_slaveconfig = 0; /* XXX CSR window? */
283
284 config_found(self, &vaa, 0);
285 }
286
287 #define sc ((struct b3_2706_softc*)vsc)
288
289 int
290 b3_2706_map_vme(vsc, vmeaddr, len, am, datasizes, swap, tag, handle, resc)
291 void *vsc;
292 vme_addr_t vmeaddr;
293 vme_size_t len;
294 vme_am_t am;
295 vme_datasize_t datasizes;
296 vme_swap_t swap;
297 bus_space_tag_t *tag;
298 bus_space_handle_t *handle;
299 vme_mapresc_t *resc;
300 {
301 int idx, i, wnd, res;
302 unsigned long boundary, maplen, pcibase;
303 vme_addr_t vmebase, vmeend;
304 static int windoworder[8] = {1, 2, 3, 5, 6, 7, 0, 4};
305
306 /* prefer windows with fine granularity for small mappings */
307 wnd = -1;
308 if (len <= 32*1024)
309 idx = 6;
310 else
311 idx = 0;
312 for (i = 0; i < 8; i++) {
313 if (!sc->windowused[windoworder[idx]]) {
314 wnd = windoworder[idx];
315 sc->windowused[wnd] = 1;
316 break;
317 }
318 idx = (idx + 1) % 8;
319 }
320 if (wnd == -1)
321 return (ENOSPC);
322
323 boundary = (wnd & 3) ? 64*1024 : 4*1024;
324
325 /* first mapped address */
326 vmebase = vmeaddr & ~(boundary - 1);
327 /* base of last mapped page */
328 vmeend = (vmeaddr + len - 1) & ~(boundary - 1);
329 /* bytes in outgoing window required */
330 maplen = vmeend - vmebase + boundary;
331
332 if (extent_alloc(sc->vmeext, maplen, boundary, 0, EX_FAST, &pcibase)) {
333 sc->windowused[wnd] = 0;
334 return (ENOMEM);
335 }
336
337 res = univ_pci_mapvme(&sc->univdata, wnd, vmebase, maplen,
338 am, datasizes, pcibase);
339 if (res) {
340 extent_free(sc->vmeext, pcibase, maplen, 0);
341 sc->windowused[wnd] = 0;
342 return (res);
343 }
344
345 res = bus_space_map(sc->vmet, pcibase + (vmeaddr - vmebase), len,
346 0, handle);
347 if (res) {
348 univ_pci_unmapvme(&sc->univdata, wnd);
349 extent_free(sc->vmeext, pcibase, maplen, 0);
350 sc->windowused[wnd] = 0;
351 return (res);
352 }
353
354 *tag = sc->vmet;
355
356 /*
357 * save all data needed for later unmapping
358 */
359 sc->vmemaprescs[wnd].wnd = wnd;
360 sc->vmemaprescs[wnd].pcibase = pcibase;
361 sc->vmemaprescs[wnd].maplen = maplen;
362 sc->vmemaprescs[wnd].handle = *handle;
363 sc->vmemaprescs[wnd].len = len;
364 *resc = &sc->vmemaprescs[wnd];
365 return (0);
366 }
367
368 void
369 b3_2706_unmap_vme(vsc, resc)
370 void *vsc;
371 vme_mapresc_t resc;
372 {
373 struct b3_2706_vmemaprescs *r = resc;
374
375 bus_space_unmap(sc->vmet, r->handle, r->len);
376 extent_free(sc->vmeext, r->pcibase, r->maplen, 0);
377
378 if (!sc->windowused[r->wnd])
379 panic("b3_2706_unmap_vme: bad window");
380 univ_pci_unmapvme(&sc->univdata, r->wnd);
381 sc->windowused[r->wnd] = 0;
382 }
383
384 int
385 b3_2706_vme_probe(vsc, addr, len, am, datasize, callback, cbarg)
386 void *vsc;
387 vme_addr_t addr;
388 vme_size_t len;
389 vme_am_t am;
390 vme_datasize_t datasize;
391 int (*callback) __P((void *, bus_space_tag_t, bus_space_handle_t));
392 void *cbarg;
393 {
394 bus_space_tag_t tag;
395 bus_space_handle_t handle;
396 vme_mapresc_t resc;
397 int res, i;
398 volatile u_int32_t dummy;
399
400 res = b3_2706_map_vme(vsc, addr, len, am, datasize, 0,
401 &tag, &handle, &resc);
402 if (res)
403 return (res);
404
405 if (univ_pci_vmebuserr(&sc->univdata, 1))
406 printf("b3_2706_vme_badaddr: TA bit not clean - reset\n");
407
408 if (callback)
409 res = (*callback)(cbarg, tag, handle);
410 else {
411 for (i = 0; i < len;) {
412 switch (datasize) {
413 case VME_D8:
414 dummy = bus_space_read_1(tag, handle, i);
415 i++;
416 break;
417 case VME_D16:
418 dummy = bus_space_read_2(tag, handle, i);
419 i += 2;
420 break;
421 case VME_D32:
422 dummy = bus_space_read_4(tag, handle, i);
423 i += 4;
424 break;
425 default:
426 panic("b3_2706_vme_probe: invalid datasize %x",
427 datasize);
428 }
429 }
430 }
431
432 if (univ_pci_vmebuserr(&sc->univdata, 0)) {
433 #ifdef BIT3DEBUG
434 printf("b3_2706_vme_badaddr: caught TA\n");
435 #endif
436 univ_pci_vmebuserr(&sc->univdata, 1);
437 res = EIO;
438 }
439
440 b3_2706_unmap_vme(vsc, resc);
441 return (res);
442 }
443
444 int
445 b3_2706_map_vmeint(vsc, level, vector, handlep)
446 void *vsc;
447 int level, vector;
448 vme_intr_handle_t *handlep;
449 {
450
451 *handlep = (void *)(long)((level << 8) | vector); /* XXX */
452 return (0);
453 }
454
455 void *
456 b3_2706_establish_vmeint(vsc, handle, prior, func, arg)
457 void *vsc;
458 vme_intr_handle_t handle;
459 int prior;
460 int (*func) __P((void *));
461 void *arg;
462 {
463 struct b3_2706_vmeintrhand *ih;
464 long lv;
465 int s;
466
467 /* no point in sleeping unless someone can free memory. */
468 ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
469 if (ih == NULL)
470 panic("b3_2706_map_vmeint: can't malloc handler info");
471
472 lv = (long)handle; /* XXX */
473
474 ih->ih_fun = func;
475 ih->ih_arg = arg;
476 ih->ih_level = lv >> 8;
477 ih->ih_vector = lv & 0xff;
478 ih->ih_prior = prior;
479 ih->ih_count = 0;
480
481 s = splhigh();
482 TAILQ_INSERT_TAIL(&(sc->intrhdls), ih, ih_next);
483 splx(s);
484
485 return (ih);
486 }
487
488 void
489 b3_2706_disestablish_vmeint(vsc, cookie)
490 void *vsc;
491 void *cookie;
492 {
493 struct b3_2706_vmeintrhand *ih = cookie;
494 int s;
495
496 if (!ih) {
497 printf("b3_2706_unmap_vmeint: NULL arg\n");
498 return;
499 }
500
501 s = splhigh();
502 TAILQ_REMOVE(&(sc->intrhdls), ih, ih_next);
503 splx(s);
504
505 free(ih, M_DEVBUF);
506 }
507
508 void
509 b3_2706_vmeint(vsc, level, vector)
510 void *vsc;
511 int level, vector;
512 {
513 struct b3_2706_vmeintrhand *ih;
514 int found;
515
516 #ifdef BIT3DEBUG
517 printf("b3_2706_vmeint: VME IRQ %d, vec %x\n", level, vector);
518 #endif
519 found = 0;
520
521 for (ih = sc->intrhdls.tqh_first; ih;
522 ih = ih->ih_next.tqe_next) {
523 if ((ih->ih_level == level) &&
524 ((ih->ih_vector == -1) ||
525 (ih->ih_vector == vector))) {
526 int s, res;
527 /*
528 * We should raise the interrupt level
529 * to ih->ih_prior here. How to do this
530 * machine-independantly?
531 * To be safe, raise to the maximum.
532 */
533 s = splhigh();
534 found |= (res = (*(ih->ih_fun))(ih->ih_arg));
535 splx(s);
536 if (res)
537 ih->ih_count++;
538 if (res == 1)
539 break;
540 }
541 }
542 if (!found)
543 sc->strayintrs++;
544 }
545
546 int
547 b3_2706_dmamap_create(vsc, len, am, datasize, swap,
548 nsegs, segsz, bound,
549 flags, mapp)
550 void *vsc;
551 vme_size_t len;
552 vme_am_t am;
553 vme_datasize_t datasize;
554 vme_swap_t swap;
555 int nsegs;
556 vme_size_t segsz;
557 vme_addr_t bound;
558 int flags;
559 bus_dmamap_t *mapp;
560 {
561 return (EINVAL);
562 }
563
564 void
565 b3_2706_dmamap_destroy(vsc, map)
566 void *vsc;
567 bus_dmamap_t map;
568 {
569 }
570
571 int
572 b3_2706_dmamem_alloc(vsc, len, am, datasizes, swap, segs, nsegs, rsegs, flags)
573 void *vsc;
574 vme_size_t len;
575 vme_am_t am;
576 vme_datasize_t datasizes;
577 vme_swap_t swap;
578 bus_dma_segment_t *segs;
579 int nsegs;
580 int *rsegs;
581 int flags;
582 {
583 return (EINVAL);
584 }
585
586 void
587 b3_2706_dmamem_free(vsc, segs, nsegs)
588 void *vsc;
589 bus_dma_segment_t *segs;
590 int nsegs;
591 {
592 }
593
594 #undef sc
595