712-spi-qup-Fix-DMA-mode-to-work-correctly.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. From ed56e6322b067a898a25bda1774eb1180a832246 Mon Sep 17 00:00:00 2001
  2. From: Andy Gross <andy.gross@linaro.org>
  3. Date: Tue, 2 Feb 2016 17:00:53 -0600
  4. Subject: [PATCH] spi: qup: Fix DMA mode to work correctly
  5. This patch fixes a few issues with the DMA mode. The QUP needs to be
  6. placed in the run mode before the DMA transactions are executed. The
  7. conditions for being able to DMA vary between revisions of the QUP.
  8. This is due to v1.1.1 using ADM DMA and later revisions using BAM.
  9. Change-Id: Ib1f876eaa05d079e0bca4358d2b25b2940986089
  10. Signed-off-by: Andy Gross <andy.gross@linaro.org>
  11. ---
  12. drivers/spi/spi-qup.c | 95 ++++++++++++++++++++++++++++++++++-----------------
  13. 1 file changed, 63 insertions(+), 32 deletions(-)
  14. --- a/drivers/spi/spi-qup.c
  15. +++ b/drivers/spi/spi-qup.c
  16. @@ -143,6 +143,7 @@ struct spi_qup {
  17. struct spi_transfer *xfer;
  18. struct completion done;
  19. + struct completion dma_tx_done;
  20. int error;
  21. int w_size; /* bytes per SPI word */
  22. int n_words;
  23. @@ -285,16 +286,16 @@ static void spi_qup_fifo_write(struct sp
  24. static void spi_qup_dma_done(void *data)
  25. {
  26. - struct spi_qup *qup = data;
  27. + struct completion *done = data;
  28. - complete(&qup->done);
  29. + complete(done);
  30. }
  31. static int spi_qup_prep_sg(struct spi_master *master, struct spi_transfer *xfer,
  32. enum dma_transfer_direction dir,
  33. - dma_async_tx_callback callback)
  34. + dma_async_tx_callback callback,
  35. + void *data)
  36. {
  37. - struct spi_qup *qup = spi_master_get_devdata(master);
  38. unsigned long flags = DMA_PREP_INTERRUPT | DMA_PREP_FENCE;
  39. struct dma_async_tx_descriptor *desc;
  40. struct scatterlist *sgl;
  41. @@ -313,11 +314,11 @@ static int spi_qup_prep_sg(struct spi_ma
  42. }
  43. desc = dmaengine_prep_slave_sg(chan, sgl, nents, dir, flags);
  44. - if (!desc)
  45. - return -EINVAL;
  46. + if (IS_ERR_OR_NULL(desc))
  47. + return desc ? PTR_ERR(desc) : -EINVAL;
  48. desc->callback = callback;
  49. - desc->callback_param = qup;
  50. + desc->callback_param = data;
  51. cookie = dmaengine_submit(desc);
  52. @@ -333,18 +334,29 @@ static void spi_qup_dma_terminate(struct
  53. dmaengine_terminate_all(master->dma_rx);
  54. }
  55. -static int spi_qup_do_dma(struct spi_master *master, struct spi_transfer *xfer)
  56. +static int spi_qup_do_dma(struct spi_master *master, struct spi_transfer *xfer,
  57. +unsigned long timeout)
  58. {
  59. + struct spi_qup *qup = spi_master_get_devdata(master);
  60. dma_async_tx_callback rx_done = NULL, tx_done = NULL;
  61. int ret;
  62. + /* before issuing the descriptors, set the QUP to run */
  63. + ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  64. + if (ret) {
  65. + dev_warn(qup->dev, "cannot set RUN state\n");
  66. + return ret;
  67. + }
  68. +
  69. if (xfer->rx_buf)
  70. rx_done = spi_qup_dma_done;
  71. - else if (xfer->tx_buf)
  72. +
  73. + if (xfer->tx_buf)
  74. tx_done = spi_qup_dma_done;
  75. if (xfer->rx_buf) {
  76. - ret = spi_qup_prep_sg(master, xfer, DMA_DEV_TO_MEM, rx_done);
  77. + ret = spi_qup_prep_sg(master, xfer, DMA_DEV_TO_MEM, rx_done,
  78. + &qup->done);
  79. if (ret)
  80. return ret;
  81. @@ -352,17 +364,26 @@ static int spi_qup_do_dma(struct spi_mas
  82. }
  83. if (xfer->tx_buf) {
  84. - ret = spi_qup_prep_sg(master, xfer, DMA_MEM_TO_DEV, tx_done);
  85. + ret = spi_qup_prep_sg(master, xfer, DMA_MEM_TO_DEV, tx_done,
  86. + &qup->dma_tx_done);
  87. if (ret)
  88. return ret;
  89. dma_async_issue_pending(master->dma_tx);
  90. }
  91. - return 0;
  92. + if (xfer->rx_buf && !wait_for_completion_timeout(&qup->done, timeout))
  93. + return -ETIMEDOUT;
  94. +
  95. + if (xfer->tx_buf &&
  96. + !wait_for_completion_timeout(&qup->dma_tx_done, timeout))
  97. + ret = -ETIMEDOUT;
  98. +
  99. + return ret;
  100. }
  101. -static int spi_qup_do_pio(struct spi_master *master, struct spi_transfer *xfer)
  102. +static int spi_qup_do_pio(struct spi_master *master, struct spi_transfer *xfer,
  103. + unsigned long timeout)
  104. {
  105. struct spi_qup *qup = spi_master_get_devdata(master);
  106. int ret;
  107. @@ -382,6 +403,15 @@ static int spi_qup_do_pio(struct spi_mas
  108. if (qup->mode == QUP_IO_M_MODE_FIFO)
  109. spi_qup_fifo_write(qup, xfer);
  110. + ret = spi_qup_set_state(qup, QUP_STATE_RUN);
  111. + if (ret) {
  112. + dev_warn(qup->dev, "cannot set RUN state\n");
  113. + return ret;
  114. + }
  115. +
  116. + if (!wait_for_completion_timeout(&qup->done, timeout))
  117. + return -ETIMEDOUT;
  118. +
  119. return 0;
  120. }
  121. @@ -430,7 +460,6 @@ static irqreturn_t spi_qup_qup_irq(int i
  122. dev_warn(controller->dev, "CLK_OVER_RUN\n");
  123. if (spi_err & SPI_ERROR_CLK_UNDER_RUN)
  124. dev_warn(controller->dev, "CLK_UNDER_RUN\n");
  125. -
  126. error = -EIO;
  127. }
  128. @@ -619,6 +648,7 @@ static int spi_qup_transfer_one(struct s
  129. timeout = 100 * msecs_to_jiffies(timeout);
  130. reinit_completion(&controller->done);
  131. + reinit_completion(&controller->dma_tx_done);
  132. spin_lock_irqsave(&controller->lock, flags);
  133. controller->xfer = xfer;
  134. @@ -628,21 +658,13 @@ static int spi_qup_transfer_one(struct s
  135. spin_unlock_irqrestore(&controller->lock, flags);
  136. if (spi_qup_is_dma_xfer(controller->mode))
  137. - ret = spi_qup_do_dma(master, xfer);
  138. + ret = spi_qup_do_dma(master, xfer, timeout);
  139. else
  140. - ret = spi_qup_do_pio(master, xfer);
  141. + ret = spi_qup_do_pio(master, xfer, timeout);
  142. if (ret)
  143. goto exit;
  144. - if (spi_qup_set_state(controller, QUP_STATE_RUN)) {
  145. - dev_warn(controller->dev, "cannot set EXECUTE state\n");
  146. - goto exit;
  147. - }
  148. -
  149. - if (!wait_for_completion_timeout(&controller->done, timeout))
  150. - ret = -ETIMEDOUT;
  151. -
  152. exit:
  153. spi_qup_set_state(controller, QUP_STATE_RESET);
  154. spin_lock_irqsave(&controller->lock, flags);
  155. @@ -664,15 +686,23 @@ static bool spi_qup_can_dma(struct spi_m
  156. size_t dma_align = dma_get_cache_alignment();
  157. int n_words;
  158. - if (xfer->rx_buf && (xfer->len % qup->in_blk_sz ||
  159. - IS_ERR_OR_NULL(master->dma_rx) ||
  160. - !IS_ALIGNED((size_t)xfer->rx_buf, dma_align)))
  161. - return false;
  162. + if (xfer->rx_buf) {
  163. + if (!IS_ALIGNED((size_t)xfer->rx_buf, dma_align) ||
  164. + IS_ERR_OR_NULL(master->dma_rx))
  165. + return false;
  166. - if (xfer->tx_buf && (xfer->len % qup->out_blk_sz ||
  167. - IS_ERR_OR_NULL(master->dma_tx) ||
  168. - !IS_ALIGNED((size_t)xfer->tx_buf, dma_align)))
  169. - return false;
  170. + if (qup->qup_v1 && (xfer->len % qup->in_blk_sz))
  171. + return false;
  172. + }
  173. +
  174. + if (xfer->tx_buf) {
  175. + if (!IS_ALIGNED((size_t)xfer->tx_buf, dma_align) ||
  176. + IS_ERR_OR_NULL(master->dma_tx))
  177. + return false;
  178. +
  179. + if (qup->qup_v1 && (xfer->len % qup->out_blk_sz))
  180. + return false;
  181. + }
  182. n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
  183. if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
  184. @@ -875,6 +905,7 @@ static int spi_qup_probe(struct platform
  185. spin_lock_init(&controller->lock);
  186. init_completion(&controller->done);
  187. + init_completion(&controller->dma_tx_done);
  188. iomode = readl_relaxed(base + QUP_IO_M_MODES);