202 cb->info = INFO_WAIT_RESP; 203} 204 205static int 206bcm_dma_init(device_t dev) 207{ 208 struct bcm_dma_softc *sc = device_get_softc(dev); 209 uint32_t mask; 210 struct bcm_dma_ch *ch; 211 void *cb_virt; 212 vm_paddr_t cb_phys; 213 int err; 214 int i; 215 216 /* disable and clear interrupt status */ 217 bus_write_4(sc->sc_mem, BCM_DMA_ENABLE, 0); 218 bus_write_4(sc->sc_mem, BCM_DMA_INT_STATUS, 0); 219 220 /* Allocate DMA chunks control blocks */ 221 /* p.40 of spec - control block should be 32-bit aligned */ 222 err = bus_dma_tag_create(bus_get_dma_tag(dev), 223 1, 0, BUS_SPACE_MAXADDR_32BIT, 224 BUS_SPACE_MAXADDR, NULL, NULL, 225 sizeof(struct bcm_dma_cb), 1, 226 sizeof(struct bcm_dma_cb), 227 BUS_DMA_ALLOCNOW, NULL, NULL, 228 &sc->sc_dma_tag); 229 230 if (err) { 231 device_printf(dev, "failed allocate DMA tag"); 232 return (err); 233 } 234 235 /* setup initial settings */ 236 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 237 ch = &sc->sc_dma_ch[i]; 238 239 err = bus_dmamem_alloc(sc->sc_dma_tag, &cb_virt, 240 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 241 &ch->dma_map); 242 if (err) { 243 device_printf(dev, "cannot allocate DMA memory\n"); 244 break; 245 } 246 247 /* 248 * Least alignment for busdma-allocated stuff is cache 249 * line size, so just make sure nothing stupid happend 250 * and we got properly aligned address 251 */ 252 if ((uintptr_t)cb_virt & 0x1f) { 253 device_printf(dev, 254 "DMA address is not 32-bytes aligned: %p\n", 255 (void*)cb_virt); 256 break; 257 } 258 259 err = bus_dmamap_load(sc->sc_dma_tag, ch->dma_map, cb_virt, 260 sizeof(struct bcm_dma_cb), bcm_dmamap_cb, &cb_phys, 261 BUS_DMA_WAITOK); 262 if (err) { 263 device_printf(dev, "cannot load DMA memory\n"); 264 break; 265 } 266 267 bzero(ch, sizeof(struct bcm_dma_ch)); 268 ch->ch = i; 269 ch->cb = cb_virt; 270 ch->vc_cb = cb_phys; 271 ch->intr_func = NULL; 272 ch->intr_arg = NULL; 273 ch->flags = BCM_DMA_CH_UNMAP; 274 275 ch->cb->info = INFO_WAIT_RESP; 276 277 /* reset DMA engine */ 278 bcm_dma_reset(dev, i); 279 } 280 281 /* now use DMA2/DMA3 only */ 282 sc->sc_dma_ch[2].flags = BCM_DMA_CH_FREE; 283 sc->sc_dma_ch[3].flags = BCM_DMA_CH_FREE; 284 285 /* enable DMAs */ 286 mask = 0; 287 288 for (i = 0; i < BCM_DMA_CH_MAX; i++) 289 if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) 290 mask |= (1 << i); 291 292 bus_write_4(sc->sc_mem, BCM_DMA_ENABLE, mask); 293 294 return (0); 295} 296 297/* 298 * Allocate DMA channel for further use, returns channel # or 299 * BCM_DMA_CH_INVALID 300 */ 301int 302bcm_dma_allocate(int req_ch) 303{ 304 struct bcm_dma_softc *sc = bcm_dma_sc; 305 int ch = BCM_DMA_CH_INVALID; 306 int i; 307 308 if (req_ch >= BCM_DMA_CH_MAX) 309 return (BCM_DMA_CH_INVALID); 310 311 /* Auto(req_ch < 0) or CH specified */ 312 mtx_lock(&sc->sc_mtx); 313 314 if (req_ch < 0) { 315 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 316 if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) { 317 ch = i; 318 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; 319 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; 320 break; 321 } 322 } 323 } 324 else { 325 if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) { 326 ch = req_ch; 327 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; 328 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; 329 } 330 } 331 332 mtx_unlock(&sc->sc_mtx); 333 return (ch); 334} 335 336/* 337 * Frees allocated channel. Returns 0 on success, -1 otherwise 338 */ 339int 340bcm_dma_free(int ch) 341{ 342 struct bcm_dma_softc *sc = bcm_dma_sc; 343 344 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 345 return (-1); 346 347 mtx_lock(&sc->sc_mtx); 348 if (sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED) { 349 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_FREE; 350 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_USED; 351 sc->sc_dma_ch[ch].intr_func = NULL; 352 sc->sc_dma_ch[ch].intr_arg = NULL; 353 354 /* reset DMA engine */ 355 bcm_dma_reset(sc->sc_dev, ch); 356 } 357 358 mtx_unlock(&sc->sc_mtx); 359 return (0); 360} 361 362/* 363 * Assign handler function for channel interrupt 364 * Returns 0 on success, -1 otherwise 365 */ 366int 367bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg) 368{ 369 struct bcm_dma_softc *sc = bcm_dma_sc; 370 struct bcm_dma_cb *cb; 371 372 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 373 return (-1); 374 375 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 376 return (-1); 377 378 sc->sc_dma_ch[ch].intr_func = func; 379 sc->sc_dma_ch[ch].intr_arg = arg; 380 cb = sc->sc_dma_ch[ch].cb; 381 cb->info |= INFO_INT_EN; 382 383 return (0); 384} 385 386/* 387 * Setup DMA source parameters 388 * ch - channel number 389 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if 390 * source is physical memory 391 * inc_addr - BCM_DMA_INC_ADDR if source address 392 * should be increased after each access or 393 * BCM_DMA_SAME_ADDR if address should remain 394 * the same 395 * width - size of read operation, BCM_DMA_32BIT 396 * for 32bit bursts, BCM_DMA_128BIT for 128 bits 397 * 398 * Returns 0 on success, -1 otherwise 399 */ 400int 401bcm_dma_setup_src(int ch, int dreq, int inc_addr, int width) 402{ 403 struct bcm_dma_softc *sc = bcm_dma_sc; 404 uint32_t info; 405 406 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 407 return (-1); 408 409 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 410 return (-1); 411 412 info = sc->sc_dma_ch[ch].cb->info; 413 info &= ~INFO_PERMAP_MASK; 414 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK; 415 416 if (dreq) 417 info |= INFO_S_DREQ; 418 else 419 info &= ~INFO_S_DREQ; 420 421 if (width == BCM_DMA_128BIT) 422 info |= INFO_S_WIDTH; 423 else 424 info &= ~INFO_S_WIDTH; 425 426 if (inc_addr == BCM_DMA_INC_ADDR) 427 info |= INFO_S_INC; 428 else 429 info &= ~INFO_S_INC; 430 431 sc->sc_dma_ch[ch].cb->info = info; 432 433 return (0); 434} 435 436/* 437 * Setup DMA destination parameters 438 * ch - channel number 439 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if 440 * destination is physical memory 441 * inc_addr - BCM_DMA_INC_ADDR if source address 442 * should be increased after each access or 443 * BCM_DMA_SAME_ADDR if address should remain 444 * the same 445 * width - size of write operation, BCM_DMA_32BIT 446 * for 32bit bursts, BCM_DMA_128BIT for 128 bits 447 * 448 * Returns 0 on success, -1 otherwise 449 */ 450int 451bcm_dma_setup_dst(int ch, int dreq, int inc_addr, int width) 452{ 453 struct bcm_dma_softc *sc = bcm_dma_sc; 454 uint32_t info; 455 456 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 457 return (-1); 458 459 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 460 return (-1); 461 462 info = sc->sc_dma_ch[ch].cb->info; 463 info &= ~INFO_PERMAP_MASK; 464 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK; 465 466 if (dreq) 467 info |= INFO_D_DREQ; 468 else 469 info &= ~INFO_D_DREQ; 470 471 if (width == BCM_DMA_128BIT) 472 info |= INFO_D_WIDTH; 473 else 474 info &= ~INFO_D_WIDTH; 475 476 if (inc_addr == BCM_DMA_INC_ADDR) 477 info |= INFO_D_INC; 478 else 479 info &= ~INFO_D_INC; 480 481 sc->sc_dma_ch[ch].cb->info = info; 482 483 return (0); 484} 485 486#ifdef DEBUG 487void 488bcm_dma_cb_dump(struct bcm_dma_cb *cb) 489{ 490 491 printf("DMA CB "); 492 printf("INFO: %8.8x ", cb->info); 493 printf("SRC: %8.8x ", cb->src); 494 printf("DST: %8.8x ", cb->dst); 495 printf("LEN: %8.8x ", cb->len); 496 printf("\n"); 497 printf("STRIDE: %8.8x ", cb->stride); 498 printf("NEXT: %8.8x ", cb->next); 499 printf("RSVD1: %8.8x ", cb->rsvd1); 500 printf("RSVD2: %8.8x ", cb->rsvd2); 501 printf("\n"); 502} 503 504void 505bcm_dma_reg_dump(int ch) 506{ 507 struct bcm_dma_softc *sc = bcm_dma_sc; 508 int i; 509 uint32_t reg; 510 511 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 512 return; 513 514 printf("DMA%d: ", ch); 515 for (i = 0; i < MAX_REG; i++) { 516 reg = bus_read_4(sc->sc_mem, BCM_DMA_CH(ch) + i*4); 517 printf("%8.8x ", reg); 518 } 519 printf("\n"); 520} 521#endif 522 523/* 524 * Start DMA transaction 525 * ch - channel number 526 * src, dst - source and destination address in 527 * ARM physical memory address space. 528 * len - amount of bytes to be transfered 529 * 530 * Returns 0 on success, -1 otherwise 531 */ 532int 533bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len) 534{ 535 struct bcm_dma_softc *sc = bcm_dma_sc; 536 struct bcm_dma_cb *cb; 537 538 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 539 return (-1); 540 541 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 542 return (-1); 543 544 cb = sc->sc_dma_ch[ch].cb; 545 if (BCM2835_ARM_IS_IO(src)) 546 cb->src = IO_TO_VCBUS(src); 547 else 548 cb->src = PHYS_TO_VCBUS(src); 549 if (BCM2835_ARM_IS_IO(dst)) 550 cb->dst = IO_TO_VCBUS(dst); 551 else 552 cb->dst = PHYS_TO_VCBUS(dst); 553 cb->len = len; 554 555 bus_dmamap_sync(sc->sc_dma_tag, 556 sc->sc_dma_ch[ch].dma_map, BUS_DMASYNC_PREWRITE); 557 558 bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch), 559 sc->sc_dma_ch[ch].vc_cb); 560 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), CS_ACTIVE); 561 562#ifdef DEBUG 563 bcm_dma_cb_dump(sc->sc_dma_ch[ch].cb); 564 bcm_dma_reg_dump(ch); 565#endif 566 567 return (0); 568} 569 570/* 571 * Get length requested for DMA transaction 572 * ch - channel number 573 * 574 * Returns size of transaction, 0 if channel is invalid 575 */ 576uint32_t 577bcm_dma_length(int ch) 578{ 579 struct bcm_dma_softc *sc = bcm_dma_sc; 580 struct bcm_dma_cb *cb; 581 582 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 583 return (0); 584 585 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 586 return (0); 587 588 cb = sc->sc_dma_ch[ch].cb; 589 590 return (cb->len); 591} 592 593static void 594bcm_dma_intr(void *arg) 595{ 596 struct bcm_dma_softc *sc = bcm_dma_sc; 597 struct bcm_dma_ch *ch = (struct bcm_dma_ch *)arg; 598 uint32_t cs, debug; 599 600 /* my interrupt? */ 601 cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch->ch)); 602 603 if (!(cs & (CS_INT | CS_ERR))) 604 return; 605 606 /* running? */ 607 if (!(ch->flags & BCM_DMA_CH_USED)) { 608 device_printf(sc->sc_dev, 609 "unused DMA intr CH=%d, CS=%x\n", ch->ch, cs); 610 return; 611 } 612 613 if (cs & CS_ERR) { 614 debug = bus_read_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch)); 615 device_printf(sc->sc_dev, "DMA error %d on CH%d\n", 616 debug & DEBUG_ERROR_MASK, ch->ch); 617 bus_write_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch), 618 debug & DEBUG_ERROR_MASK); 619 bcm_dma_reset(sc->sc_dev, ch->ch); 620 } 621 622 if (cs & CS_INT) { 623 /* acknowledge interrupt */ 624 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch->ch), 625 CS_INT | CS_END); 626 627 /* Prepare for possible access to len field */ 628 bus_dmamap_sync(sc->sc_dma_tag, ch->dma_map, 629 BUS_DMASYNC_POSTWRITE); 630 631 /* save callback function and argument */ 632 if (ch->intr_func) 633 ch->intr_func(ch->ch, ch->intr_arg); 634 } 635} 636 637static int 638bcm_dma_probe(device_t dev) 639{ 640 641 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-dma")) 642 return (ENXIO); 643 644 device_set_desc(dev, "BCM2835 DMA Controller"); 645 return (BUS_PROBE_DEFAULT); 646} 647 648static int 649bcm_dma_attach(device_t dev) 650{ 651 struct bcm_dma_softc *sc = device_get_softc(dev); 652 int rid, err = 0; 653 int i; 654 655 sc->sc_dev = dev; 656 657 if (bcm_dma_sc) 658 return (ENXIO); 659 660 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 661 sc->sc_irq[i] = NULL; 662 sc->sc_intrhand[i] = NULL; 663 } 664 665 /* DMA0 - DMA14 */ 666 rid = 0; 667 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 668 if (sc->sc_mem == NULL) { 669 device_printf(dev, "could not allocate memory resource\n"); 670 return (ENXIO); 671 } 672 673 /* IRQ DMA0 - DMA11 XXX NOT USE DMA12(spurious?) */ 674 for (rid = 0; rid < BCM_DMA_CH_MAX; rid++) { 675 sc->sc_irq[rid] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 676 RF_ACTIVE); 677 if (sc->sc_irq[rid] == NULL) { 678 device_printf(dev, "cannot allocate interrupt\n"); 679 err = ENXIO; 680 goto fail; 681 } 682 if (bus_setup_intr(dev, sc->sc_irq[rid], INTR_TYPE_MISC | INTR_MPSAFE, 683 NULL, bcm_dma_intr, &sc->sc_dma_ch[rid], 684 &sc->sc_intrhand[rid])) { 685 device_printf(dev, "cannot setup interrupt handler\n"); 686 err = ENXIO; 687 goto fail; 688 } 689 } 690 691 mtx_init(&sc->sc_mtx, "bcmdma", "bcmdma", MTX_DEF); 692 bcm_dma_sc = sc; 693 694 err = bcm_dma_init(dev); 695 if (err) 696 goto fail; 697 698 return (err); 699 700fail: 701 if (sc->sc_mem) 702 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem); 703 704 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 705 if (sc->sc_intrhand[i]) 706 bus_teardown_intr(dev, sc->sc_irq[i], sc->sc_intrhand[i]); 707 if (sc->sc_irq[i]) 708 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq[i]); 709 } 710 711 return (err); 712} 713 714static device_method_t bcm_dma_methods[] = { 715 DEVMETHOD(device_probe, bcm_dma_probe), 716 DEVMETHOD(device_attach, bcm_dma_attach), 717 { 0, 0 } 718}; 719 720static driver_t bcm_dma_driver = { 721 "bcm_dma", 722 bcm_dma_methods, 723 sizeof(struct bcm_dma_softc), 724}; 725 726static devclass_t bcm_dma_devclass; 727 728DRIVER_MODULE(bcm_dma, simplebus, bcm_dma_driver, bcm_dma_devclass, 0, 0); 729MODULE_VERSION(bcm_dma, 1);
| 202 cb->info = INFO_WAIT_RESP; 203} 204 205static int 206bcm_dma_init(device_t dev) 207{ 208 struct bcm_dma_softc *sc = device_get_softc(dev); 209 uint32_t mask; 210 struct bcm_dma_ch *ch; 211 void *cb_virt; 212 vm_paddr_t cb_phys; 213 int err; 214 int i; 215 216 /* disable and clear interrupt status */ 217 bus_write_4(sc->sc_mem, BCM_DMA_ENABLE, 0); 218 bus_write_4(sc->sc_mem, BCM_DMA_INT_STATUS, 0); 219 220 /* Allocate DMA chunks control blocks */ 221 /* p.40 of spec - control block should be 32-bit aligned */ 222 err = bus_dma_tag_create(bus_get_dma_tag(dev), 223 1, 0, BUS_SPACE_MAXADDR_32BIT, 224 BUS_SPACE_MAXADDR, NULL, NULL, 225 sizeof(struct bcm_dma_cb), 1, 226 sizeof(struct bcm_dma_cb), 227 BUS_DMA_ALLOCNOW, NULL, NULL, 228 &sc->sc_dma_tag); 229 230 if (err) { 231 device_printf(dev, "failed allocate DMA tag"); 232 return (err); 233 } 234 235 /* setup initial settings */ 236 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 237 ch = &sc->sc_dma_ch[i]; 238 239 err = bus_dmamem_alloc(sc->sc_dma_tag, &cb_virt, 240 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 241 &ch->dma_map); 242 if (err) { 243 device_printf(dev, "cannot allocate DMA memory\n"); 244 break; 245 } 246 247 /* 248 * Least alignment for busdma-allocated stuff is cache 249 * line size, so just make sure nothing stupid happend 250 * and we got properly aligned address 251 */ 252 if ((uintptr_t)cb_virt & 0x1f) { 253 device_printf(dev, 254 "DMA address is not 32-bytes aligned: %p\n", 255 (void*)cb_virt); 256 break; 257 } 258 259 err = bus_dmamap_load(sc->sc_dma_tag, ch->dma_map, cb_virt, 260 sizeof(struct bcm_dma_cb), bcm_dmamap_cb, &cb_phys, 261 BUS_DMA_WAITOK); 262 if (err) { 263 device_printf(dev, "cannot load DMA memory\n"); 264 break; 265 } 266 267 bzero(ch, sizeof(struct bcm_dma_ch)); 268 ch->ch = i; 269 ch->cb = cb_virt; 270 ch->vc_cb = cb_phys; 271 ch->intr_func = NULL; 272 ch->intr_arg = NULL; 273 ch->flags = BCM_DMA_CH_UNMAP; 274 275 ch->cb->info = INFO_WAIT_RESP; 276 277 /* reset DMA engine */ 278 bcm_dma_reset(dev, i); 279 } 280 281 /* now use DMA2/DMA3 only */ 282 sc->sc_dma_ch[2].flags = BCM_DMA_CH_FREE; 283 sc->sc_dma_ch[3].flags = BCM_DMA_CH_FREE; 284 285 /* enable DMAs */ 286 mask = 0; 287 288 for (i = 0; i < BCM_DMA_CH_MAX; i++) 289 if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) 290 mask |= (1 << i); 291 292 bus_write_4(sc->sc_mem, BCM_DMA_ENABLE, mask); 293 294 return (0); 295} 296 297/* 298 * Allocate DMA channel for further use, returns channel # or 299 * BCM_DMA_CH_INVALID 300 */ 301int 302bcm_dma_allocate(int req_ch) 303{ 304 struct bcm_dma_softc *sc = bcm_dma_sc; 305 int ch = BCM_DMA_CH_INVALID; 306 int i; 307 308 if (req_ch >= BCM_DMA_CH_MAX) 309 return (BCM_DMA_CH_INVALID); 310 311 /* Auto(req_ch < 0) or CH specified */ 312 mtx_lock(&sc->sc_mtx); 313 314 if (req_ch < 0) { 315 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 316 if (sc->sc_dma_ch[i].flags & BCM_DMA_CH_FREE) { 317 ch = i; 318 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; 319 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; 320 break; 321 } 322 } 323 } 324 else { 325 if (sc->sc_dma_ch[req_ch].flags & BCM_DMA_CH_FREE) { 326 ch = req_ch; 327 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_FREE; 328 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_USED; 329 } 330 } 331 332 mtx_unlock(&sc->sc_mtx); 333 return (ch); 334} 335 336/* 337 * Frees allocated channel. Returns 0 on success, -1 otherwise 338 */ 339int 340bcm_dma_free(int ch) 341{ 342 struct bcm_dma_softc *sc = bcm_dma_sc; 343 344 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 345 return (-1); 346 347 mtx_lock(&sc->sc_mtx); 348 if (sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED) { 349 sc->sc_dma_ch[ch].flags |= BCM_DMA_CH_FREE; 350 sc->sc_dma_ch[ch].flags &= ~BCM_DMA_CH_USED; 351 sc->sc_dma_ch[ch].intr_func = NULL; 352 sc->sc_dma_ch[ch].intr_arg = NULL; 353 354 /* reset DMA engine */ 355 bcm_dma_reset(sc->sc_dev, ch); 356 } 357 358 mtx_unlock(&sc->sc_mtx); 359 return (0); 360} 361 362/* 363 * Assign handler function for channel interrupt 364 * Returns 0 on success, -1 otherwise 365 */ 366int 367bcm_dma_setup_intr(int ch, void (*func)(int, void *), void *arg) 368{ 369 struct bcm_dma_softc *sc = bcm_dma_sc; 370 struct bcm_dma_cb *cb; 371 372 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 373 return (-1); 374 375 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 376 return (-1); 377 378 sc->sc_dma_ch[ch].intr_func = func; 379 sc->sc_dma_ch[ch].intr_arg = arg; 380 cb = sc->sc_dma_ch[ch].cb; 381 cb->info |= INFO_INT_EN; 382 383 return (0); 384} 385 386/* 387 * Setup DMA source parameters 388 * ch - channel number 389 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if 390 * source is physical memory 391 * inc_addr - BCM_DMA_INC_ADDR if source address 392 * should be increased after each access or 393 * BCM_DMA_SAME_ADDR if address should remain 394 * the same 395 * width - size of read operation, BCM_DMA_32BIT 396 * for 32bit bursts, BCM_DMA_128BIT for 128 bits 397 * 398 * Returns 0 on success, -1 otherwise 399 */ 400int 401bcm_dma_setup_src(int ch, int dreq, int inc_addr, int width) 402{ 403 struct bcm_dma_softc *sc = bcm_dma_sc; 404 uint32_t info; 405 406 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 407 return (-1); 408 409 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 410 return (-1); 411 412 info = sc->sc_dma_ch[ch].cb->info; 413 info &= ~INFO_PERMAP_MASK; 414 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK; 415 416 if (dreq) 417 info |= INFO_S_DREQ; 418 else 419 info &= ~INFO_S_DREQ; 420 421 if (width == BCM_DMA_128BIT) 422 info |= INFO_S_WIDTH; 423 else 424 info &= ~INFO_S_WIDTH; 425 426 if (inc_addr == BCM_DMA_INC_ADDR) 427 info |= INFO_S_INC; 428 else 429 info &= ~INFO_S_INC; 430 431 sc->sc_dma_ch[ch].cb->info = info; 432 433 return (0); 434} 435 436/* 437 * Setup DMA destination parameters 438 * ch - channel number 439 * dreq - hardware DREQ # or BCM_DMA_DREQ_NONE if 440 * destination is physical memory 441 * inc_addr - BCM_DMA_INC_ADDR if source address 442 * should be increased after each access or 443 * BCM_DMA_SAME_ADDR if address should remain 444 * the same 445 * width - size of write operation, BCM_DMA_32BIT 446 * for 32bit bursts, BCM_DMA_128BIT for 128 bits 447 * 448 * Returns 0 on success, -1 otherwise 449 */ 450int 451bcm_dma_setup_dst(int ch, int dreq, int inc_addr, int width) 452{ 453 struct bcm_dma_softc *sc = bcm_dma_sc; 454 uint32_t info; 455 456 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 457 return (-1); 458 459 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 460 return (-1); 461 462 info = sc->sc_dma_ch[ch].cb->info; 463 info &= ~INFO_PERMAP_MASK; 464 info |= (dreq << INFO_PERMAP_SHIFT) & INFO_PERMAP_MASK; 465 466 if (dreq) 467 info |= INFO_D_DREQ; 468 else 469 info &= ~INFO_D_DREQ; 470 471 if (width == BCM_DMA_128BIT) 472 info |= INFO_D_WIDTH; 473 else 474 info &= ~INFO_D_WIDTH; 475 476 if (inc_addr == BCM_DMA_INC_ADDR) 477 info |= INFO_D_INC; 478 else 479 info &= ~INFO_D_INC; 480 481 sc->sc_dma_ch[ch].cb->info = info; 482 483 return (0); 484} 485 486#ifdef DEBUG 487void 488bcm_dma_cb_dump(struct bcm_dma_cb *cb) 489{ 490 491 printf("DMA CB "); 492 printf("INFO: %8.8x ", cb->info); 493 printf("SRC: %8.8x ", cb->src); 494 printf("DST: %8.8x ", cb->dst); 495 printf("LEN: %8.8x ", cb->len); 496 printf("\n"); 497 printf("STRIDE: %8.8x ", cb->stride); 498 printf("NEXT: %8.8x ", cb->next); 499 printf("RSVD1: %8.8x ", cb->rsvd1); 500 printf("RSVD2: %8.8x ", cb->rsvd2); 501 printf("\n"); 502} 503 504void 505bcm_dma_reg_dump(int ch) 506{ 507 struct bcm_dma_softc *sc = bcm_dma_sc; 508 int i; 509 uint32_t reg; 510 511 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 512 return; 513 514 printf("DMA%d: ", ch); 515 for (i = 0; i < MAX_REG; i++) { 516 reg = bus_read_4(sc->sc_mem, BCM_DMA_CH(ch) + i*4); 517 printf("%8.8x ", reg); 518 } 519 printf("\n"); 520} 521#endif 522 523/* 524 * Start DMA transaction 525 * ch - channel number 526 * src, dst - source and destination address in 527 * ARM physical memory address space. 528 * len - amount of bytes to be transfered 529 * 530 * Returns 0 on success, -1 otherwise 531 */ 532int 533bcm_dma_start(int ch, vm_paddr_t src, vm_paddr_t dst, int len) 534{ 535 struct bcm_dma_softc *sc = bcm_dma_sc; 536 struct bcm_dma_cb *cb; 537 538 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 539 return (-1); 540 541 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 542 return (-1); 543 544 cb = sc->sc_dma_ch[ch].cb; 545 if (BCM2835_ARM_IS_IO(src)) 546 cb->src = IO_TO_VCBUS(src); 547 else 548 cb->src = PHYS_TO_VCBUS(src); 549 if (BCM2835_ARM_IS_IO(dst)) 550 cb->dst = IO_TO_VCBUS(dst); 551 else 552 cb->dst = PHYS_TO_VCBUS(dst); 553 cb->len = len; 554 555 bus_dmamap_sync(sc->sc_dma_tag, 556 sc->sc_dma_ch[ch].dma_map, BUS_DMASYNC_PREWRITE); 557 558 bus_write_4(sc->sc_mem, BCM_DMA_CBADDR(ch), 559 sc->sc_dma_ch[ch].vc_cb); 560 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch), CS_ACTIVE); 561 562#ifdef DEBUG 563 bcm_dma_cb_dump(sc->sc_dma_ch[ch].cb); 564 bcm_dma_reg_dump(ch); 565#endif 566 567 return (0); 568} 569 570/* 571 * Get length requested for DMA transaction 572 * ch - channel number 573 * 574 * Returns size of transaction, 0 if channel is invalid 575 */ 576uint32_t 577bcm_dma_length(int ch) 578{ 579 struct bcm_dma_softc *sc = bcm_dma_sc; 580 struct bcm_dma_cb *cb; 581 582 if (ch < 0 || ch >= BCM_DMA_CH_MAX) 583 return (0); 584 585 if (!(sc->sc_dma_ch[ch].flags & BCM_DMA_CH_USED)) 586 return (0); 587 588 cb = sc->sc_dma_ch[ch].cb; 589 590 return (cb->len); 591} 592 593static void 594bcm_dma_intr(void *arg) 595{ 596 struct bcm_dma_softc *sc = bcm_dma_sc; 597 struct bcm_dma_ch *ch = (struct bcm_dma_ch *)arg; 598 uint32_t cs, debug; 599 600 /* my interrupt? */ 601 cs = bus_read_4(sc->sc_mem, BCM_DMA_CS(ch->ch)); 602 603 if (!(cs & (CS_INT | CS_ERR))) 604 return; 605 606 /* running? */ 607 if (!(ch->flags & BCM_DMA_CH_USED)) { 608 device_printf(sc->sc_dev, 609 "unused DMA intr CH=%d, CS=%x\n", ch->ch, cs); 610 return; 611 } 612 613 if (cs & CS_ERR) { 614 debug = bus_read_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch)); 615 device_printf(sc->sc_dev, "DMA error %d on CH%d\n", 616 debug & DEBUG_ERROR_MASK, ch->ch); 617 bus_write_4(sc->sc_mem, BCM_DMA_DEBUG(ch->ch), 618 debug & DEBUG_ERROR_MASK); 619 bcm_dma_reset(sc->sc_dev, ch->ch); 620 } 621 622 if (cs & CS_INT) { 623 /* acknowledge interrupt */ 624 bus_write_4(sc->sc_mem, BCM_DMA_CS(ch->ch), 625 CS_INT | CS_END); 626 627 /* Prepare for possible access to len field */ 628 bus_dmamap_sync(sc->sc_dma_tag, ch->dma_map, 629 BUS_DMASYNC_POSTWRITE); 630 631 /* save callback function and argument */ 632 if (ch->intr_func) 633 ch->intr_func(ch->ch, ch->intr_arg); 634 } 635} 636 637static int 638bcm_dma_probe(device_t dev) 639{ 640 641 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-dma")) 642 return (ENXIO); 643 644 device_set_desc(dev, "BCM2835 DMA Controller"); 645 return (BUS_PROBE_DEFAULT); 646} 647 648static int 649bcm_dma_attach(device_t dev) 650{ 651 struct bcm_dma_softc *sc = device_get_softc(dev); 652 int rid, err = 0; 653 int i; 654 655 sc->sc_dev = dev; 656 657 if (bcm_dma_sc) 658 return (ENXIO); 659 660 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 661 sc->sc_irq[i] = NULL; 662 sc->sc_intrhand[i] = NULL; 663 } 664 665 /* DMA0 - DMA14 */ 666 rid = 0; 667 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 668 if (sc->sc_mem == NULL) { 669 device_printf(dev, "could not allocate memory resource\n"); 670 return (ENXIO); 671 } 672 673 /* IRQ DMA0 - DMA11 XXX NOT USE DMA12(spurious?) */ 674 for (rid = 0; rid < BCM_DMA_CH_MAX; rid++) { 675 sc->sc_irq[rid] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 676 RF_ACTIVE); 677 if (sc->sc_irq[rid] == NULL) { 678 device_printf(dev, "cannot allocate interrupt\n"); 679 err = ENXIO; 680 goto fail; 681 } 682 if (bus_setup_intr(dev, sc->sc_irq[rid], INTR_TYPE_MISC | INTR_MPSAFE, 683 NULL, bcm_dma_intr, &sc->sc_dma_ch[rid], 684 &sc->sc_intrhand[rid])) { 685 device_printf(dev, "cannot setup interrupt handler\n"); 686 err = ENXIO; 687 goto fail; 688 } 689 } 690 691 mtx_init(&sc->sc_mtx, "bcmdma", "bcmdma", MTX_DEF); 692 bcm_dma_sc = sc; 693 694 err = bcm_dma_init(dev); 695 if (err) 696 goto fail; 697 698 return (err); 699 700fail: 701 if (sc->sc_mem) 702 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem); 703 704 for (i = 0; i < BCM_DMA_CH_MAX; i++) { 705 if (sc->sc_intrhand[i]) 706 bus_teardown_intr(dev, sc->sc_irq[i], sc->sc_intrhand[i]); 707 if (sc->sc_irq[i]) 708 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq[i]); 709 } 710 711 return (err); 712} 713 714static device_method_t bcm_dma_methods[] = { 715 DEVMETHOD(device_probe, bcm_dma_probe), 716 DEVMETHOD(device_attach, bcm_dma_attach), 717 { 0, 0 } 718}; 719 720static driver_t bcm_dma_driver = { 721 "bcm_dma", 722 bcm_dma_methods, 723 sizeof(struct bcm_dma_softc), 724}; 725 726static devclass_t bcm_dma_devclass; 727 728DRIVER_MODULE(bcm_dma, simplebus, bcm_dma_driver, bcm_dma_devclass, 0, 0); 729MODULE_VERSION(bcm_dma, 1);
|