spi-pxa2xx-dma.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * PXA2xx SPI DMA engine support.
  3. *
  4. * Copyright (C) 2013, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/pxa2xx_ssp.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/sizes.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/spi/pxa2xx_spi.h>
  19. #include "spi-pxa2xx.h"
  20. static void pxa2xx_spi_dma_transfer_complete(struct driver_data *drv_data,
  21. bool error)
  22. {
  23. struct spi_message *msg = drv_data->cur_msg;
  24. /*
  25. * It is possible that one CPU is handling ROR interrupt and other
  26. * just gets DMA completion. Calling pump_transfers() twice for the
  27. * same transfer leads to problems thus we prevent concurrent calls
  28. * by using ->dma_running.
  29. */
  30. if (atomic_dec_and_test(&drv_data->dma_running)) {
  31. /*
  32. * If the other CPU is still handling the ROR interrupt we
  33. * might not know about the error yet. So we re-check the
  34. * ROR bit here before we clear the status register.
  35. */
  36. if (!error) {
  37. u32 status = pxa2xx_spi_read(drv_data, SSSR)
  38. & drv_data->mask_sr;
  39. error = status & SSSR_ROR;
  40. }
  41. /* Clear status & disable interrupts */
  42. pxa2xx_spi_write(drv_data, SSCR1,
  43. pxa2xx_spi_read(drv_data, SSCR1)
  44. & ~drv_data->dma_cr1);
  45. write_SSSR_CS(drv_data, drv_data->clear_sr);
  46. if (!pxa25x_ssp_comp(drv_data))
  47. pxa2xx_spi_write(drv_data, SSTO, 0);
  48. if (!error) {
  49. msg->actual_length += drv_data->len;
  50. msg->state = pxa2xx_spi_next_transfer(drv_data);
  51. } else {
  52. /* In case we got an error we disable the SSP now */
  53. pxa2xx_spi_write(drv_data, SSCR0,
  54. pxa2xx_spi_read(drv_data, SSCR0)
  55. & ~SSCR0_SSE);
  56. msg->state = ERROR_STATE;
  57. }
  58. tasklet_schedule(&drv_data->pump_transfers);
  59. }
  60. }
  61. static void pxa2xx_spi_dma_callback(void *data)
  62. {
  63. pxa2xx_spi_dma_transfer_complete(data, false);
  64. }
  65. static struct dma_async_tx_descriptor *
  66. pxa2xx_spi_dma_prepare_one(struct driver_data *drv_data,
  67. enum dma_transfer_direction dir)
  68. {
  69. struct chip_data *chip = drv_data->cur_chip;
  70. struct spi_transfer *xfer = drv_data->cur_transfer;
  71. enum dma_slave_buswidth width;
  72. struct dma_slave_config cfg;
  73. struct dma_chan *chan;
  74. struct sg_table *sgt;
  75. int ret;
  76. switch (drv_data->n_bytes) {
  77. case 1:
  78. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  79. break;
  80. case 2:
  81. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  82. break;
  83. default:
  84. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  85. break;
  86. }
  87. memset(&cfg, 0, sizeof(cfg));
  88. cfg.direction = dir;
  89. if (dir == DMA_MEM_TO_DEV) {
  90. cfg.dst_addr = drv_data->ssdr_physical;
  91. cfg.dst_addr_width = width;
  92. cfg.dst_maxburst = chip->dma_burst_size;
  93. sgt = &xfer->tx_sg;
  94. chan = drv_data->master->dma_tx;
  95. } else {
  96. cfg.src_addr = drv_data->ssdr_physical;
  97. cfg.src_addr_width = width;
  98. cfg.src_maxburst = chip->dma_burst_size;
  99. sgt = &xfer->rx_sg;
  100. chan = drv_data->master->dma_rx;
  101. }
  102. ret = dmaengine_slave_config(chan, &cfg);
  103. if (ret) {
  104. dev_warn(&drv_data->pdev->dev, "DMA slave config failed\n");
  105. return NULL;
  106. }
  107. return dmaengine_prep_slave_sg(chan, sgt->sgl, sgt->nents, dir,
  108. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  109. }
  110. irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
  111. {
  112. u32 status;
  113. status = pxa2xx_spi_read(drv_data, SSSR) & drv_data->mask_sr;
  114. if (status & SSSR_ROR) {
  115. dev_err(&drv_data->pdev->dev, "FIFO overrun\n");
  116. dmaengine_terminate_async(drv_data->master->dma_rx);
  117. dmaengine_terminate_async(drv_data->master->dma_tx);
  118. pxa2xx_spi_dma_transfer_complete(drv_data, true);
  119. return IRQ_HANDLED;
  120. }
  121. return IRQ_NONE;
  122. }
  123. int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
  124. {
  125. struct dma_async_tx_descriptor *tx_desc, *rx_desc;
  126. int err = 0;
  127. tx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_MEM_TO_DEV);
  128. if (!tx_desc) {
  129. dev_err(&drv_data->pdev->dev,
  130. "failed to get DMA TX descriptor\n");
  131. err = -EBUSY;
  132. goto err_tx;
  133. }
  134. rx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_DEV_TO_MEM);
  135. if (!rx_desc) {
  136. dev_err(&drv_data->pdev->dev,
  137. "failed to get DMA RX descriptor\n");
  138. err = -EBUSY;
  139. goto err_rx;
  140. }
  141. /* We are ready when RX completes */
  142. rx_desc->callback = pxa2xx_spi_dma_callback;
  143. rx_desc->callback_param = drv_data;
  144. dmaengine_submit(rx_desc);
  145. dmaengine_submit(tx_desc);
  146. return 0;
  147. err_rx:
  148. dmaengine_terminate_async(drv_data->master->dma_tx);
  149. err_tx:
  150. return err;
  151. }
  152. void pxa2xx_spi_dma_start(struct driver_data *drv_data)
  153. {
  154. dma_async_issue_pending(drv_data->master->dma_rx);
  155. dma_async_issue_pending(drv_data->master->dma_tx);
  156. atomic_set(&drv_data->dma_running, 1);
  157. }
  158. int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
  159. {
  160. struct pxa2xx_spi_master *pdata = drv_data->master_info;
  161. struct device *dev = &drv_data->pdev->dev;
  162. struct spi_master *master = drv_data->master;
  163. dma_cap_mask_t mask;
  164. dma_cap_zero(mask);
  165. dma_cap_set(DMA_SLAVE, mask);
  166. master->dma_tx = dma_request_slave_channel_compat(mask,
  167. pdata->dma_filter, pdata->tx_param, dev, "tx");
  168. if (!master->dma_tx)
  169. return -ENODEV;
  170. master->dma_rx = dma_request_slave_channel_compat(mask,
  171. pdata->dma_filter, pdata->rx_param, dev, "rx");
  172. if (!master->dma_rx) {
  173. dma_release_channel(master->dma_tx);
  174. master->dma_tx = NULL;
  175. return -ENODEV;
  176. }
  177. return 0;
  178. }
  179. void pxa2xx_spi_dma_release(struct driver_data *drv_data)
  180. {
  181. struct spi_master *master = drv_data->master;
  182. if (master->dma_rx) {
  183. dmaengine_terminate_sync(master->dma_rx);
  184. dma_release_channel(master->dma_rx);
  185. master->dma_rx = NULL;
  186. }
  187. if (master->dma_tx) {
  188. dmaengine_terminate_sync(master->dma_tx);
  189. dma_release_channel(master->dma_tx);
  190. master->dma_tx = NULL;
  191. }
  192. }
  193. int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
  194. struct spi_device *spi,
  195. u8 bits_per_word, u32 *burst_code,
  196. u32 *threshold)
  197. {
  198. struct pxa2xx_spi_chip *chip_info = spi->controller_data;
  199. /*
  200. * If the DMA burst size is given in chip_info we use that,
  201. * otherwise we use the default. Also we use the default FIFO
  202. * thresholds for now.
  203. */
  204. *burst_code = chip_info ? chip_info->dma_burst_size : 1;
  205. *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)
  206. | SSCR1_TxTresh(TX_THRESH_DFLT);
  207. return 0;
  208. }