spi-pxa2xx-dma.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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->master->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 =
  70. spi_get_ctldata(drv_data->master->cur_msg->spi);
  71. struct spi_transfer *xfer = drv_data->cur_transfer;
  72. enum dma_slave_buswidth width;
  73. struct dma_slave_config cfg;
  74. struct dma_chan *chan;
  75. struct sg_table *sgt;
  76. int ret;
  77. switch (drv_data->n_bytes) {
  78. case 1:
  79. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  80. break;
  81. case 2:
  82. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  83. break;
  84. default:
  85. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  86. break;
  87. }
  88. memset(&cfg, 0, sizeof(cfg));
  89. cfg.direction = dir;
  90. if (dir == DMA_MEM_TO_DEV) {
  91. cfg.dst_addr = drv_data->ssdr_physical;
  92. cfg.dst_addr_width = width;
  93. cfg.dst_maxburst = chip->dma_burst_size;
  94. sgt = &xfer->tx_sg;
  95. chan = drv_data->master->dma_tx;
  96. } else {
  97. cfg.src_addr = drv_data->ssdr_physical;
  98. cfg.src_addr_width = width;
  99. cfg.src_maxburst = chip->dma_burst_size;
  100. sgt = &xfer->rx_sg;
  101. chan = drv_data->master->dma_rx;
  102. }
  103. ret = dmaengine_slave_config(chan, &cfg);
  104. if (ret) {
  105. dev_warn(&drv_data->pdev->dev, "DMA slave config failed\n");
  106. return NULL;
  107. }
  108. return dmaengine_prep_slave_sg(chan, sgt->sgl, sgt->nents, dir,
  109. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  110. }
  111. irqreturn_t pxa2xx_spi_dma_transfer(struct driver_data *drv_data)
  112. {
  113. u32 status;
  114. status = pxa2xx_spi_read(drv_data, SSSR) & drv_data->mask_sr;
  115. if (status & SSSR_ROR) {
  116. dev_err(&drv_data->pdev->dev, "FIFO overrun\n");
  117. dmaengine_terminate_async(drv_data->master->dma_rx);
  118. dmaengine_terminate_async(drv_data->master->dma_tx);
  119. pxa2xx_spi_dma_transfer_complete(drv_data, true);
  120. return IRQ_HANDLED;
  121. }
  122. return IRQ_NONE;
  123. }
  124. int pxa2xx_spi_dma_prepare(struct driver_data *drv_data, u32 dma_burst)
  125. {
  126. struct dma_async_tx_descriptor *tx_desc, *rx_desc;
  127. int err;
  128. tx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_MEM_TO_DEV);
  129. if (!tx_desc) {
  130. dev_err(&drv_data->pdev->dev,
  131. "failed to get DMA TX descriptor\n");
  132. err = -EBUSY;
  133. goto err_tx;
  134. }
  135. rx_desc = pxa2xx_spi_dma_prepare_one(drv_data, DMA_DEV_TO_MEM);
  136. if (!rx_desc) {
  137. dev_err(&drv_data->pdev->dev,
  138. "failed to get DMA RX descriptor\n");
  139. err = -EBUSY;
  140. goto err_rx;
  141. }
  142. /* We are ready when RX completes */
  143. rx_desc->callback = pxa2xx_spi_dma_callback;
  144. rx_desc->callback_param = drv_data;
  145. dmaengine_submit(rx_desc);
  146. dmaengine_submit(tx_desc);
  147. return 0;
  148. err_rx:
  149. dmaengine_terminate_async(drv_data->master->dma_tx);
  150. err_tx:
  151. return err;
  152. }
  153. void pxa2xx_spi_dma_start(struct driver_data *drv_data)
  154. {
  155. dma_async_issue_pending(drv_data->master->dma_rx);
  156. dma_async_issue_pending(drv_data->master->dma_tx);
  157. atomic_set(&drv_data->dma_running, 1);
  158. }
  159. int pxa2xx_spi_dma_setup(struct driver_data *drv_data)
  160. {
  161. struct pxa2xx_spi_master *pdata = drv_data->master_info;
  162. struct device *dev = &drv_data->pdev->dev;
  163. struct spi_master *master = drv_data->master;
  164. dma_cap_mask_t mask;
  165. dma_cap_zero(mask);
  166. dma_cap_set(DMA_SLAVE, mask);
  167. master->dma_tx = dma_request_slave_channel_compat(mask,
  168. pdata->dma_filter, pdata->tx_param, dev, "tx");
  169. if (!master->dma_tx)
  170. return -ENODEV;
  171. master->dma_rx = dma_request_slave_channel_compat(mask,
  172. pdata->dma_filter, pdata->rx_param, dev, "rx");
  173. if (!master->dma_rx) {
  174. dma_release_channel(master->dma_tx);
  175. master->dma_tx = NULL;
  176. return -ENODEV;
  177. }
  178. return 0;
  179. }
  180. void pxa2xx_spi_dma_release(struct driver_data *drv_data)
  181. {
  182. struct spi_master *master = drv_data->master;
  183. if (master->dma_rx) {
  184. dmaengine_terminate_sync(master->dma_rx);
  185. dma_release_channel(master->dma_rx);
  186. master->dma_rx = NULL;
  187. }
  188. if (master->dma_tx) {
  189. dmaengine_terminate_sync(master->dma_tx);
  190. dma_release_channel(master->dma_tx);
  191. master->dma_tx = NULL;
  192. }
  193. }
  194. int pxa2xx_spi_set_dma_burst_and_threshold(struct chip_data *chip,
  195. struct spi_device *spi,
  196. u8 bits_per_word, u32 *burst_code,
  197. u32 *threshold)
  198. {
  199. struct pxa2xx_spi_chip *chip_info = spi->controller_data;
  200. /*
  201. * If the DMA burst size is given in chip_info we use that,
  202. * otherwise we use the default. Also we use the default FIFO
  203. * thresholds for now.
  204. */
  205. *burst_code = chip_info ? chip_info->dma_burst_size : 1;
  206. *threshold = SSCR1_RxTresh(RX_THRESH_DFLT)
  207. | SSCR1_TxTresh(TX_THRESH_DFLT);
  208. return 0;
  209. }