1 /* $NetBSD: btvmeii.c,v 1.4 2001/11/13 07:48:41 lukem 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.4 2001/11/13 07:48:41 lukem Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 41 #include <dev/pci/pcireg.h> 42 #include <dev/pci/pcivar.h> 43 #include <dev/pci/pcidevs.h> 44 45 #include <machine/bus.h> 46 #include <sys/malloc.h> 47 #include <sys/extent.h> 48 49 #include <dev/pci/ppbreg.h> 50 51 #include <dev/vme/vmereg.h> 52 #include <dev/vme/vmevar.h> 53 54 #include <dev/pci/universe_pci_var.h> 55 56 static int b3_2706_match __P((struct device *, struct cfdata *, void *)); 57 static void b3_2706_attach __P((struct device *, struct device *, void *)); 58 59 /* exported via tag structs */ 60 int b3_2706_map_vme __P((void *, vme_addr_t, vme_size_t, 61 vme_am_t, vme_datasize_t, vme_swap_t, 62 bus_space_tag_t *, bus_space_handle_t *, vme_mapresc_t*)); 63 void b3_2706_unmap_vme __P((void *, vme_mapresc_t)); 64 65 int b3_2706_vme_probe __P((void *, vme_addr_t, vme_size_t, vme_am_t, 66 vme_datasize_t, 67 int (*)(void *, bus_space_tag_t, bus_space_handle_t), 68 void *)); 69 70 int b3_2706_map_vmeint __P((void *, int, int, vme_intr_handle_t *)); 71 void *b3_2706_establish_vmeint __P((void *, vme_intr_handle_t, int, 72 int (*)(void *), void *)); 73 void b3_2706_disestablish_vmeint __P((void *, void *)); 74 void b3_2706_vmeint __P((void *, int, int)); 75 76 int b3_2706_dmamap_create __P((void *, vme_size_t, 77 vme_am_t, vme_datasize_t, vme_swap_t, 78 int, vme_size_t, vme_addr_t, 79 int, bus_dmamap_t *)); 80 void b3_2706_dmamap_destroy __P((void *, bus_dmamap_t)); 81 82 int b3_2706_dmamem_alloc __P((void *, vme_size_t, 83 vme_am_t, vme_datasize_t, vme_swap_t, 84 bus_dma_segment_t *, int, int *, int)); 85 void b3_2706_dmamem_free __P((void *, bus_dma_segment_t *, int)); 86 87 struct b3_2706_vmemaprescs { 88 int wnd; 89 unsigned long pcibase, maplen; 90 bus_space_handle_t handle; 91 u_int32_t len; 92 }; 93 94 struct b3_2706_vmeintrhand { 95 TAILQ_ENTRY(b3_2706_vmeintrhand) ih_next; 96 int (*ih_fun) __P((void*)); 97 void *ih_arg; 98 int ih_level; 99 int ih_vector; 100 int ih_prior; 101 u_long ih_count; 102 }; 103 104 struct b3_2706_softc { 105 struct device sc_dev; 106 struct univ_pci_data univdata; 107 bus_space_tag_t swapt, vmet; 108 bus_space_handle_t swaph; 109 bus_addr_t vmepbase; 110 111 int windowused[8]; 112 struct b3_2706_vmemaprescs vmemaprescs[8]; 113 struct extent *vmeext; 114 char vmemap[EXTENT_FIXED_STORAGE_SIZE(8)]; 115 116 struct vme_chipset_tag sc_vct; 117 118 /* list of VME interrupt handlers */ 119 TAILQ_HEAD(, b3_2706_vmeintrhand) intrhdls; 120 int strayintrs; 121 }; 122 123 struct cfattach btvmeii_ca = { 124 sizeof(struct b3_2706_softc), b3_2706_match, b3_2706_attach, 125 #if 0 126 b3_2706_detach 127 #endif 128 }; 129 130 /* 131 * The adapter consists of a DEC PCI-PCI-bridge with two 132 * PCI devices behind it: A Tundra Universe as device 4 and 133 * some FPGA with glue logics as device 8. 134 * As long as the autoconf code doesn't provide more support 135 * for dependant devices, we have to duplicate a part of the 136 * "ppb" functions here. 137 */ 138 139 static int 140 b3_2706_match(parent, match, aux) 141 struct device *parent; 142 struct cfdata *match; 143 void *aux; 144 { 145 struct pci_attach_args *pa = aux; 146 pci_chipset_tag_t pc = pa->pa_pc; 147 int secbus; 148 pcitag_t tag; 149 pcireg_t id; 150 151 if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR_DEC) 152 || (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_DEC_21152)) 153 return (0); 154 155 secbus = PPB_BUSINFO_SECONDARY(pci_conf_read(pc, pa->pa_tag, 156 PPB_REG_BUSINFO)); 157 if (secbus == 0) { 158 printf("b3_2706_match: ppb not configured\n"); 159 return (0); 160 } 161 162 tag = pci_make_tag(pc, secbus, 4, 0); 163 id = pci_conf_read(pc, tag, PCI_ID_REG); 164 165 if ((PCI_VENDOR(id) != PCI_VENDOR_NEWBRIDGE) 166 || (PCI_PRODUCT(id) != PCI_PRODUCT_NEWBRIDGE_CA91CX42)) { 167 #ifdef DEBUG 168 printf("b3_2706_match: no tundra\n"); 169 #endif 170 return (0); 171 } 172 173 tag = pci_make_tag(pc, secbus, 8, 0); 174 id = pci_conf_read(pc, tag, PCI_ID_REG); 175 176 if ((PCI_VENDOR(id) != PCI_VENDOR_BIT3) 177 || (PCI_PRODUCT(id) != PCI_PRODUCT_BIT3_PCIVME2706)) { 178 #ifdef DEBUG 179 printf("b3_2706_match: no bit3 chip\n"); 180 #endif 181 return (0); 182 } 183 184 return (5); /* beat "ppb" */ 185 } 186 187 static void 188 b3_2706_attach(parent, self, aux) 189 struct device *parent, *self; 190 void *aux; 191 { 192 struct b3_2706_softc *sc = (struct b3_2706_softc *)self; 193 struct pci_attach_args *pa = aux; 194 pci_chipset_tag_t pc = pa->pa_pc; 195 struct pci_attach_args aa; 196 int secbus; 197 pcireg_t intr; 198 pcitag_t tag; 199 bus_addr_t swappbase; 200 int i; 201 202 struct vmebus_attach_args vaa; 203 204 printf("\n"); 205 206 secbus = PPB_BUSINFO_SECONDARY(pci_conf_read(pc, pa->pa_tag, 207 PPB_REG_BUSINFO)); 208 209 memcpy(&aa, pa, sizeof(struct pci_attach_args)); 210 aa.pa_device = 4; 211 aa.pa_function = 0; 212 aa.pa_tag = pci_make_tag(pc, secbus, 4, 0); 213 aa.pa_intrswiz += 4; 214 intr = pci_conf_read(pc, aa.pa_tag, PCI_INTERRUPT_REG); 215 /* 216 * swizzle it based on the number of 217 * busses we're behind and our device 218 * number. 219 */ 220 aa.pa_intrpin = ((1 + aa.pa_intrswiz - 1) % 4) + 1; 221 aa.pa_intrline = PCI_INTERRUPT_LINE(intr); 222 223 if (univ_pci_attach(&sc->univdata, &aa, self->dv_xname, 224 b3_2706_vmeint, sc)) { 225 printf("%s: error initializing universe chip\n", 226 self->dv_xname); 227 return; 228 } 229 230 /* 231 * don't waste KVM - the byteswap register is aliased in 232 * a 512k window, we need it only once 233 */ 234 tag = pci_make_tag(pc, secbus, 8, 0); 235 sc->swapt = pa->pa_memt; 236 if (pci_mapreg_info(pc, tag, 0x10, 237 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 238 &swappbase, 0, 0) || 239 bus_space_map(sc->swapt, swappbase, 4, 0, &sc->swaph)) { 240 printf("%s: can't map byteswap register\n", self->dv_xname); 241 return; 242 } 243 /* 244 * Set up cycle specific byteswap mode. 245 * XXX Readback yields "all-ones" for me, and it doesn't seem 246 * to matter what I write into the register - the data don't 247 * get swapped. Adapter fault or documentation bug? 248 */ 249 bus_space_write_4(sc->swapt, sc->swaph, 0, 0x00000490); 250 251 /* VME space is mapped as needed */ 252 sc->vmet = pa->pa_memt; 253 if (pci_mapreg_info(pc, tag, 0x14, 254 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 255 &sc->vmepbase, 0, 0)) { 256 printf("%s: VME range not assigned\n", self->dv_xname); 257 return; 258 } 259 #ifdef BIT3DEBUG 260 printf("%s: VME window @%lx\n", self->dv_xname, (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 extern int cold; 470 471 /* no point in sleeping unless someone can free memory. */ 472 ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK); 473 if (ih == NULL) 474 panic("b3_2706_map_vmeint: can't malloc handler info"); 475 476 lv = (long)handle; /* XXX */ 477 478 ih->ih_fun = func; 479 ih->ih_arg = arg; 480 ih->ih_level = lv >> 8; 481 ih->ih_vector = lv & 0xff; 482 ih->ih_prior = prior; 483 ih->ih_count = 0; 484 485 s = splhigh(); 486 TAILQ_INSERT_TAIL(&(sc->intrhdls), ih, ih_next); 487 splx(s); 488 489 return (ih); 490 } 491 492 void 493 b3_2706_disestablish_vmeint(vsc, cookie) 494 void *vsc; 495 void *cookie; 496 { 497 struct b3_2706_vmeintrhand *ih = cookie; 498 int s; 499 500 if (!ih) { 501 printf("b3_2706_unmap_vmeint: NULL arg\n"); 502 return; 503 } 504 505 s = splhigh(); 506 TAILQ_REMOVE(&(sc->intrhdls), ih, ih_next); 507 splx(s); 508 509 free(ih, M_DEVBUF); 510 } 511 512 void 513 b3_2706_vmeint(vsc, level, vector) 514 void *vsc; 515 int level, vector; 516 { 517 struct b3_2706_vmeintrhand *ih; 518 int found; 519 520 #ifdef BIT3DEBUG 521 printf("b3_2706_vmeint: VME IRQ %d, vec %x\n", level, vector); 522 #endif 523 found = 0; 524 525 for (ih = sc->intrhdls.tqh_first; ih; 526 ih = ih->ih_next.tqe_next) { 527 if ((ih->ih_level == level) && 528 ((ih->ih_vector == -1) || 529 (ih->ih_vector == vector))) { 530 int s, res; 531 /* 532 * We should raise the interrupt level 533 * to ih->ih_prior here. How to do this 534 * machine-independantly? 535 * To be safe, raise to the maximum. 536 */ 537 s = splhigh(); 538 found |= (res = (*(ih->ih_fun))(ih->ih_arg)); 539 splx(s); 540 if (res) 541 ih->ih_count++; 542 if (res == 1) 543 break; 544 } 545 } 546 if (!found) 547 sc->strayintrs++; 548 } 549 550 int 551 b3_2706_dmamap_create(vsc, len, am, datasize, swap, 552 nsegs, segsz, bound, 553 flags, mapp) 554 void *vsc; 555 vme_size_t len; 556 vme_am_t am; 557 vme_datasize_t datasize; 558 vme_swap_t swap; 559 int nsegs; 560 vme_size_t segsz; 561 vme_addr_t bound; 562 int flags; 563 bus_dmamap_t *mapp; 564 { 565 return (EINVAL); 566 } 567 568 void 569 b3_2706_dmamap_destroy(vsc, map) 570 void *vsc; 571 bus_dmamap_t map; 572 { 573 } 574 575 int 576 b3_2706_dmamem_alloc(vsc, len, am, datasizes, swap, segs, nsegs, rsegs, flags) 577 void *vsc; 578 vme_size_t len; 579 vme_am_t am; 580 vme_datasize_t datasizes; 581 vme_swap_t swap; 582 bus_dma_segment_t *segs; 583 int nsegs; 584 int *rsegs; 585 int flags; 586 { 587 return (EINVAL); 588 } 589 590 void 591 b3_2706_dmamem_free(vsc, segs, nsegs) 592 void *vsc; 593 bus_dma_segment_t *segs; 594 int nsegs; 595 { 596 } 597 598 #undef sc 599