Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'topic/dma' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi into HEAD

+312 -110
+101 -110
drivers/spi/spi-s3c64xx.c
··· 381 381 #else 382 382 383 383 static void prepare_dma(struct s3c64xx_spi_dma_data *dma, 384 - unsigned len, dma_addr_t buf) 384 + struct sg_table *sgt) 385 385 { 386 386 struct s3c64xx_spi_driver_data *sdd; 387 387 struct dma_slave_config config; ··· 407 407 dmaengine_slave_config(dma->ch, &config); 408 408 } 409 409 410 - desc = dmaengine_prep_slave_single(dma->ch, buf, len, 411 - dma->direction, DMA_PREP_INTERRUPT); 410 + desc = dmaengine_prep_slave_sg(dma->ch, sgt->sgl, sgt->nents, 411 + dma->direction, DMA_PREP_INTERRUPT); 412 412 413 413 desc->callback = s3c64xx_spi_dmacb; 414 414 desc->callback_param = dma; ··· 515 515 chcfg |= S3C64XX_SPI_CH_TXCH_ON; 516 516 if (dma_mode) { 517 517 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON; 518 + #ifndef CONFIG_S3C_DMA 519 + prepare_dma(&sdd->tx_dma, &xfer->tx_sg); 520 + #else 518 521 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma); 522 + #endif 519 523 } else { 520 524 switch (sdd->cur_bpw) { 521 525 case 32: ··· 551 547 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff) 552 548 | S3C64XX_SPI_PACKET_CNT_EN, 553 549 regs + S3C64XX_SPI_PACKET_CNT); 550 + #ifndef CONFIG_S3C_DMA 551 + prepare_dma(&sdd->rx_dma, &xfer->rx_sg); 552 + #else 554 553 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma); 554 + #endif 555 555 } 556 556 } 557 557 558 558 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG); 559 559 writel(chcfg, regs + S3C64XX_SPI_CH_CFG); 560 - } 561 - 562 - static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd, 563 - struct spi_device *spi) 564 - { 565 - if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */ 566 - if (sdd->tgl_spi != spi) { /* if last mssg on diff device */ 567 - /* Deselect the last toggled device */ 568 - if (spi->cs_gpio >= 0) 569 - gpio_set_value(spi->cs_gpio, 570 - spi->mode & SPI_CS_HIGH ? 0 : 1); 571 - } 572 - sdd->tgl_spi = NULL; 573 - } 574 - 575 - if (spi->cs_gpio >= 0) 576 - gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 1 : 0); 577 560 } 578 561 579 562 static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd, ··· 584 593 return RX_FIFO_LVL(status, sdd); 585 594 } 586 595 587 - static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd, 588 - struct spi_transfer *xfer, int dma_mode) 596 + static int wait_for_dma(struct s3c64xx_spi_driver_data *sdd, 597 + struct spi_transfer *xfer) 589 598 { 590 599 void __iomem *regs = sdd->regs; 591 600 unsigned long val; 601 + u32 status; 592 602 int ms; 593 603 594 604 /* millisecs to xfer 'len' bytes @ 'cur_speed' */ 595 605 ms = xfer->len * 8 * 1000 / sdd->cur_speed; 596 606 ms += 10; /* some tolerance */ 597 607 598 - if (dma_mode) { 599 - val = msecs_to_jiffies(ms) + 10; 600 - val = wait_for_completion_timeout(&sdd->xfer_completion, val); 601 - } else { 602 - u32 status; 603 - val = msecs_to_loops(ms); 604 - do { 608 + val = msecs_to_jiffies(ms) + 10; 609 + val = wait_for_completion_timeout(&sdd->xfer_completion, val); 610 + 611 + /* 612 + * If the previous xfer was completed within timeout, then 613 + * proceed further else return -EIO. 614 + * DmaTx returns after simply writing data in the FIFO, 615 + * w/o waiting for real transmission on the bus to finish. 616 + * DmaRx returns only after Dma read data from FIFO which 617 + * needs bus transmission to finish, so we don't worry if 618 + * Xfer involved Rx(with or without Tx). 619 + */ 620 + if (val && !xfer->rx_buf) { 621 + val = msecs_to_loops(10); 622 + status = readl(regs + S3C64XX_SPI_STATUS); 623 + while ((TX_FIFO_LVL(status, sdd) 624 + || !S3C64XX_SPI_ST_TX_DONE(status, sdd)) 625 + && --val) { 626 + cpu_relax(); 605 627 status = readl(regs + S3C64XX_SPI_STATUS); 606 - } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val); 607 - } 608 - 609 - if (dma_mode) { 610 - u32 status; 611 - 612 - /* 613 - * If the previous xfer was completed within timeout, then 614 - * proceed further else return -EIO. 615 - * DmaTx returns after simply writing data in the FIFO, 616 - * w/o waiting for real transmission on the bus to finish. 617 - * DmaRx returns only after Dma read data from FIFO which 618 - * needs bus transmission to finish, so we don't worry if 619 - * Xfer involved Rx(with or without Tx). 620 - */ 621 - if (val && !xfer->rx_buf) { 622 - val = msecs_to_loops(10); 623 - status = readl(regs + S3C64XX_SPI_STATUS); 624 - while ((TX_FIFO_LVL(status, sdd) 625 - || !S3C64XX_SPI_ST_TX_DONE(status, sdd)) 626 - && --val) { 627 - cpu_relax(); 628 - status = readl(regs + S3C64XX_SPI_STATUS); 629 - } 630 - 631 628 } 632 629 633 - /* If timed out while checking rx/tx status return error */ 634 - if (!val) 635 - return -EIO; 636 - } else { 637 - int loops; 638 - u32 cpy_len; 639 - u8 *buf; 640 - 641 - /* If it was only Tx */ 642 - if (!xfer->rx_buf) { 643 - sdd->state &= ~TXBUSY; 644 - return 0; 645 - } 646 - 647 - /* 648 - * If the receive length is bigger than the controller fifo 649 - * size, calculate the loops and read the fifo as many times. 650 - * loops = length / max fifo size (calculated by using the 651 - * fifo mask). 652 - * For any size less than the fifo size the below code is 653 - * executed atleast once. 654 - */ 655 - loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1); 656 - buf = xfer->rx_buf; 657 - do { 658 - /* wait for data to be received in the fifo */ 659 - cpy_len = s3c64xx_spi_wait_for_timeout(sdd, 660 - (loops ? ms : 0)); 661 - 662 - switch (sdd->cur_bpw) { 663 - case 32: 664 - ioread32_rep(regs + S3C64XX_SPI_RX_DATA, 665 - buf, cpy_len / 4); 666 - break; 667 - case 16: 668 - ioread16_rep(regs + S3C64XX_SPI_RX_DATA, 669 - buf, cpy_len / 2); 670 - break; 671 - default: 672 - ioread8_rep(regs + S3C64XX_SPI_RX_DATA, 673 - buf, cpy_len); 674 - break; 675 - } 676 - 677 - buf = buf + cpy_len; 678 - } while (loops--); 679 - sdd->state &= ~RXBUSY; 680 630 } 631 + 632 + /* If timed out while checking rx/tx status return error */ 633 + if (!val) 634 + return -EIO; 681 635 682 636 return 0; 683 637 } 684 638 685 - static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd, 686 - struct spi_device *spi) 639 + static int wait_for_pio(struct s3c64xx_spi_driver_data *sdd, 640 + struct spi_transfer *xfer) 687 641 { 688 - if (sdd->tgl_spi == spi) 689 - sdd->tgl_spi = NULL; 642 + void __iomem *regs = sdd->regs; 643 + unsigned long val; 644 + u32 status; 645 + int loops; 646 + u32 cpy_len; 647 + u8 *buf; 648 + int ms; 690 649 691 - if (spi->cs_gpio >= 0) 692 - gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 0 : 1); 650 + /* millisecs to xfer 'len' bytes @ 'cur_speed' */ 651 + ms = xfer->len * 8 * 1000 / sdd->cur_speed; 652 + ms += 10; /* some tolerance */ 653 + 654 + val = msecs_to_loops(ms); 655 + do { 656 + status = readl(regs + S3C64XX_SPI_STATUS); 657 + } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val); 658 + 659 + 660 + /* If it was only Tx */ 661 + if (!xfer->rx_buf) { 662 + sdd->state &= ~TXBUSY; 663 + return 0; 664 + } 665 + 666 + /* 667 + * If the receive length is bigger than the controller fifo 668 + * size, calculate the loops and read the fifo as many times. 669 + * loops = length / max fifo size (calculated by using the 670 + * fifo mask). 671 + * For any size less than the fifo size the below code is 672 + * executed atleast once. 673 + */ 674 + loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1); 675 + buf = xfer->rx_buf; 676 + do { 677 + /* wait for data to be received in the fifo */ 678 + cpy_len = s3c64xx_spi_wait_for_timeout(sdd, 679 + (loops ? ms : 0)); 680 + 681 + switch (sdd->cur_bpw) { 682 + case 32: 683 + ioread32_rep(regs + S3C64XX_SPI_RX_DATA, 684 + buf, cpy_len / 4); 685 + break; 686 + case 16: 687 + ioread16_rep(regs + S3C64XX_SPI_RX_DATA, 688 + buf, cpy_len / 2); 689 + break; 690 + default: 691 + ioread8_rep(regs + S3C64XX_SPI_RX_DATA, 692 + buf, cpy_len); 693 + break; 694 + } 695 + 696 + buf = buf + cpy_len; 697 + } while (loops--); 698 + sdd->state &= ~RXBUSY; 699 + 700 + return 0; 693 701 } 694 702 695 703 static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd) ··· 919 929 920 930 spin_unlock_irqrestore(&sdd->lock, flags); 921 931 922 - status = wait_for_xfer(sdd, xfer, use_dma); 932 + if (use_dma) 933 + status = wait_for_dma(sdd, xfer); 934 + else 935 + status = wait_for_pio(sdd, xfer); 923 936 924 937 if (status) { 925 938 dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n", ··· 1085 1092 1086 1093 pm_runtime_put(&sdd->pdev->dev); 1087 1094 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL); 1088 - disable_cs(sdd, spi); 1089 1095 return 0; 1090 1096 1091 1097 setup_exit: 1092 1098 pm_runtime_put(&sdd->pdev->dev); 1093 1099 /* setup() returns with device de-selected */ 1094 1100 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL); 1095 - disable_cs(sdd, spi); 1096 1101 1097 1102 gpio_free(cs->line); 1098 1103 spi_set_ctldata(spi, NULL);
+180
drivers/spi/spi.c
··· 24 24 #include <linux/device.h> 25 25 #include <linux/init.h> 26 26 #include <linux/cache.h> 27 + #include <linux/dma-mapping.h> 28 + #include <linux/dmaengine.h> 27 29 #include <linux/mutex.h> 28 30 #include <linux/of_device.h> 29 31 #include <linux/of_irq.h> ··· 582 580 spi->master->set_cs(spi, !enable); 583 581 } 584 582 583 + static int spi_map_buf(struct spi_master *master, struct device *dev, 584 + struct sg_table *sgt, void *buf, size_t len, 585 + enum dma_data_direction dir) 586 + { 587 + const bool vmalloced_buf = is_vmalloc_addr(buf); 588 + const int desc_len = vmalloced_buf ? PAGE_SIZE : master->max_dma_len; 589 + const int sgs = DIV_ROUND_UP(len, desc_len); 590 + struct page *vm_page; 591 + void *sg_buf; 592 + size_t min; 593 + int i, ret; 594 + 595 + ret = sg_alloc_table(sgt, sgs, GFP_KERNEL); 596 + if (ret != 0) 597 + return ret; 598 + 599 + for (i = 0; i < sgs; i++) { 600 + min = min_t(size_t, len, desc_len); 601 + 602 + if (vmalloced_buf) { 603 + vm_page = vmalloc_to_page(buf); 604 + if (!vm_page) { 605 + sg_free_table(sgt); 606 + return -ENOMEM; 607 + } 608 + sg_buf = page_address(vm_page) + 609 + ((size_t)buf & ~PAGE_MASK); 610 + } else { 611 + sg_buf = buf; 612 + } 613 + 614 + sg_set_buf(&sgt->sgl[i], sg_buf, min); 615 + 616 + buf += min; 617 + len -= min; 618 + } 619 + 620 + ret = dma_map_sg(dev, sgt->sgl, sgt->nents, dir); 621 + if (ret < 0) { 622 + sg_free_table(sgt); 623 + return ret; 624 + } 625 + 626 + sgt->nents = ret; 627 + 628 + return 0; 629 + } 630 + 631 + static void spi_unmap_buf(struct spi_master *master, struct device *dev, 632 + struct sg_table *sgt, enum dma_data_direction dir) 633 + { 634 + if (sgt->orig_nents) { 635 + dma_unmap_sg(dev, sgt->sgl, sgt->orig_nents, dir); 636 + sg_free_table(sgt); 637 + } 638 + } 639 + 640 + static int spi_map_msg(struct spi_master *master, struct spi_message *msg) 641 + { 642 + struct device *tx_dev, *rx_dev; 643 + struct spi_transfer *xfer; 644 + void *tmp; 645 + unsigned int max_tx, max_rx; 646 + int ret; 647 + 648 + if (master->flags & (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX)) { 649 + max_tx = 0; 650 + max_rx = 0; 651 + 652 + list_for_each_entry(xfer, &msg->transfers, transfer_list) { 653 + if ((master->flags & SPI_MASTER_MUST_TX) && 654 + !xfer->tx_buf) 655 + max_tx = max(xfer->len, max_tx); 656 + if ((master->flags & SPI_MASTER_MUST_RX) && 657 + !xfer->rx_buf) 658 + max_rx = max(xfer->len, max_rx); 659 + } 660 + 661 + if (max_tx) { 662 + tmp = krealloc(master->dummy_tx, max_tx, 663 + GFP_KERNEL | GFP_DMA); 664 + if (!tmp) 665 + return -ENOMEM; 666 + master->dummy_tx = tmp; 667 + memset(tmp, 0, max_tx); 668 + } 669 + 670 + if (max_rx) { 671 + tmp = krealloc(master->dummy_rx, max_rx, 672 + GFP_KERNEL | GFP_DMA); 673 + if (!tmp) 674 + return -ENOMEM; 675 + master->dummy_rx = tmp; 676 + } 677 + 678 + if (max_tx || max_rx) { 679 + list_for_each_entry(xfer, &msg->transfers, 680 + transfer_list) { 681 + if (!xfer->tx_buf) 682 + xfer->tx_buf = master->dummy_tx; 683 + if (!xfer->rx_buf) 684 + xfer->rx_buf = master->dummy_rx; 685 + } 686 + } 687 + } 688 + 689 + if (!master->can_dma) 690 + return 0; 691 + 692 + tx_dev = &master->dma_tx->dev->device; 693 + rx_dev = &master->dma_rx->dev->device; 694 + 695 + list_for_each_entry(xfer, &msg->transfers, transfer_list) { 696 + if (!master->can_dma(master, msg->spi, xfer)) 697 + continue; 698 + 699 + if (xfer->tx_buf != NULL) { 700 + ret = spi_map_buf(master, tx_dev, &xfer->tx_sg, 701 + (void *)xfer->tx_buf, xfer->len, 702 + DMA_TO_DEVICE); 703 + if (ret != 0) 704 + return ret; 705 + } 706 + 707 + if (xfer->rx_buf != NULL) { 708 + ret = spi_map_buf(master, rx_dev, &xfer->rx_sg, 709 + xfer->rx_buf, xfer->len, 710 + DMA_FROM_DEVICE); 711 + if (ret != 0) { 712 + spi_unmap_buf(master, tx_dev, &xfer->tx_sg, 713 + DMA_TO_DEVICE); 714 + return ret; 715 + } 716 + } 717 + } 718 + 719 + master->cur_msg_mapped = true; 720 + 721 + return 0; 722 + } 723 + 724 + static int spi_unmap_msg(struct spi_master *master, struct spi_message *msg) 725 + { 726 + struct spi_transfer *xfer; 727 + struct device *tx_dev, *rx_dev; 728 + 729 + if (!master->cur_msg_mapped || !master->can_dma) 730 + return 0; 731 + 732 + tx_dev = &master->dma_tx->dev->device; 733 + rx_dev = &master->dma_rx->dev->device; 734 + 735 + list_for_each_entry(xfer, &msg->transfers, transfer_list) { 736 + if (!master->can_dma(master, msg->spi, xfer)) 737 + continue; 738 + 739 + spi_unmap_buf(master, rx_dev, &xfer->rx_sg, DMA_FROM_DEVICE); 740 + spi_unmap_buf(master, tx_dev, &xfer->tx_sg, DMA_TO_DEVICE); 741 + } 742 + 743 + return 0; 744 + } 745 + 585 746 /* 586 747 * spi_transfer_one_message - Default implementation of transfer_one_message() 587 748 * ··· 851 686 } 852 687 master->busy = false; 853 688 spin_unlock_irqrestore(&master->queue_lock, flags); 689 + kfree(master->dummy_rx); 690 + master->dummy_rx = NULL; 691 + kfree(master->dummy_tx); 692 + master->dummy_tx = NULL; 854 693 if (master->unprepare_transfer_hardware && 855 694 master->unprepare_transfer_hardware(master)) 856 695 dev_err(&master->dev, ··· 919 750 return; 920 751 } 921 752 master->cur_msg_prepared = true; 753 + } 754 + 755 + ret = spi_map_msg(master, master->cur_msg); 756 + if (ret) { 757 + master->cur_msg->status = ret; 758 + spi_finalize_current_message(master); 759 + return; 922 760 } 923 761 924 762 ret = master->transfer_one_message(master, master->cur_msg); ··· 1016 840 1017 841 queue_kthread_work(&master->kworker, &master->pump_messages); 1018 842 spin_unlock_irqrestore(&master->queue_lock, flags); 843 + 844 + spi_unmap_msg(master, mesg); 1019 845 1020 846 if (master->cur_msg_prepared && master->unprepare_message) { 1021 847 ret = master->unprepare_message(master, mesg); ··· 1552 1374 mutex_init(&master->bus_lock_mutex); 1553 1375 master->bus_lock_flag = 0; 1554 1376 init_completion(&master->xfer_completion); 1377 + if (!master->max_dma_len) 1378 + master->max_dma_len = INT_MAX; 1555 1379 1556 1380 /* register the device, then userspace will see it. 1557 1381 * registration fails if the bus ID is in use.
+31
include/linux/spi/spi.h
··· 24 24 #include <linux/slab.h> 25 25 #include <linux/kthread.h> 26 26 #include <linux/completion.h> 27 + #include <linux/scatterlist.h> 28 + 29 + struct dma_chan; 27 30 28 31 /* 29 32 * INTERFACES between SPI master-side drivers and SPI infrastructure. ··· 269 266 * @auto_runtime_pm: the core should ensure a runtime PM reference is held 270 267 * while the hardware is prepared, using the parent 271 268 * device for the spidev 269 + * @max_dma_len: Maximum length of a DMA transfer for the device. 272 270 * @prepare_transfer_hardware: a message will soon arrive from the queue 273 271 * so the subsystem requests the driver to prepare the transfer hardware 274 272 * by issuing this call ··· 349 345 #define SPI_MASTER_HALF_DUPLEX BIT(0) /* can't do full duplex */ 350 346 #define SPI_MASTER_NO_RX BIT(1) /* can't do buffer read */ 351 347 #define SPI_MASTER_NO_TX BIT(2) /* can't do buffer write */ 348 + #define SPI_MASTER_MUST_RX BIT(3) /* requires rx */ 349 + #define SPI_MASTER_MUST_TX BIT(4) /* requires tx */ 352 350 353 351 /* lock and mutex for SPI bus locking */ 354 352 spinlock_t bus_lock_spinlock; ··· 393 387 void (*cleanup)(struct spi_device *spi); 394 388 395 389 /* 390 + * Used to enable core support for DMA handling, if can_dma() 391 + * exists and returns true then the transfer will be mapped 392 + * prior to transfer_one() being called. The driver should 393 + * not modify or store xfer and dma_tx and dma_rx must be set 394 + * while the device is prepared. 395 + */ 396 + bool (*can_dma)(struct spi_master *master, 397 + struct spi_device *spi, 398 + struct spi_transfer *xfer); 399 + 400 + /* 396 401 * These hooks are for drivers that want to use the generic 397 402 * master transfer queueing mechanism. If these are used, the 398 403 * transfer() function above must NOT be specified by the driver. ··· 421 404 bool rt; 422 405 bool auto_runtime_pm; 423 406 bool cur_msg_prepared; 407 + bool cur_msg_mapped; 424 408 struct completion xfer_completion; 409 + size_t max_dma_len; 425 410 426 411 int (*prepare_transfer_hardware)(struct spi_master *master); 427 412 int (*transfer_one_message)(struct spi_master *master, ··· 444 425 445 426 /* gpio chip select */ 446 427 int *cs_gpios; 428 + 429 + /* DMA channels for use with core dmaengine helpers */ 430 + struct dma_chan *dma_tx; 431 + struct dma_chan *dma_rx; 432 + 433 + /* dummy data for full duplex devices */ 434 + void *dummy_rx; 435 + void *dummy_tx; 447 436 }; 448 437 449 438 static inline void *spi_master_get_devdata(struct spi_master *master) ··· 536 509 * (optionally) changing the chipselect status, then starting 537 510 * the next transfer or completing this @spi_message. 538 511 * @transfer_list: transfers are sequenced through @spi_message.transfers 512 + * @tx_sg: Scatterlist for transmit, currently not for client use 513 + * @rx_sg: Scatterlist for receive, currently not for client use 539 514 * 540 515 * SPI transfers always write the same number of bytes as they read. 541 516 * Protocol drivers should always provide @rx_buf and/or @tx_buf. ··· 605 576 606 577 dma_addr_t tx_dma; 607 578 dma_addr_t rx_dma; 579 + struct sg_table tx_sg; 580 + struct sg_table rx_sg; 608 581 609 582 unsigned cs_change:1; 610 583 unsigned tx_nbits:3;