710-spi-qup-Make-sure-mode-is-only-determined-once.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. From 93f99afbc534e00d72d58336061823055ee820f1 Mon Sep 17 00:00:00 2001
  2. From: Andy Gross <andy.gross@linaro.org>
  3. Date: Tue, 12 Apr 2016 09:11:47 -0500
  4. Subject: [PATCH] spi: qup: Make sure mode is only determined once
  5. This patch calculates the mode once. All decisions on the current
  6. transaction
  7. is made using the mode instead of use_dma
  8. Signed-off-by: Andy Gross <andy.gross@linaro.org>
  9. Change-Id: If3cdd924355e037d77dc8201a72895fac0461aa5
  10. ---
  11. drivers/spi/spi-qup.c | 96 +++++++++++++++++++--------------------------------
  12. 1 file changed, 36 insertions(+), 60 deletions(-)
  13. --- a/drivers/spi/spi-qup.c
  14. +++ b/drivers/spi/spi-qup.c
  15. @@ -150,13 +150,20 @@ struct spi_qup {
  16. int rx_bytes;
  17. int qup_v1;
  18. - int use_dma;
  19. + int mode;
  20. struct dma_slave_config rx_conf;
  21. struct dma_slave_config tx_conf;
  22. - int mode;
  23. };
  24. +static inline bool spi_qup_is_dma_xfer(int mode)
  25. +{
  26. + if (mode == QUP_IO_M_MODE_DMOV || mode == QUP_IO_M_MODE_BAM)
  27. + return true;
  28. +
  29. + return false;
  30. +}
  31. +
  32. static inline bool spi_qup_is_valid_state(struct spi_qup *controller)
  33. {
  34. u32 opstate = readl_relaxed(controller->base + QUP_STATE);
  35. @@ -427,7 +434,7 @@ static irqreturn_t spi_qup_qup_irq(int i
  36. error = -EIO;
  37. }
  38. - if (!controller->use_dma) {
  39. + if (!spi_qup_is_dma_xfer(controller->mode)) {
  40. if (opflags & QUP_OP_IN_SERVICE_FLAG)
  41. spi_qup_fifo_read(controller, xfer);
  42. @@ -446,43 +453,11 @@ static irqreturn_t spi_qup_qup_irq(int i
  43. return IRQ_HANDLED;
  44. }
  45. -static u32
  46. -spi_qup_get_mode(struct spi_master *master, struct spi_transfer *xfer)
  47. -{
  48. - struct spi_qup *qup = spi_master_get_devdata(master);
  49. - u32 mode;
  50. - size_t dma_align = dma_get_cache_alignment();
  51. -
  52. - qup->w_size = 4;
  53. -
  54. - if (xfer->bits_per_word <= 8)
  55. - qup->w_size = 1;
  56. - else if (xfer->bits_per_word <= 16)
  57. - qup->w_size = 2;
  58. -
  59. - qup->n_words = xfer->len / qup->w_size;
  60. -
  61. - if (!IS_ERR_OR_NULL(master->dma_rx) &&
  62. - IS_ALIGNED((size_t)xfer->tx_buf, dma_align) &&
  63. - IS_ALIGNED((size_t)xfer->rx_buf, dma_align) &&
  64. - !is_vmalloc_addr(xfer->tx_buf) &&
  65. - !is_vmalloc_addr(xfer->rx_buf) &&
  66. - (xfer->len > 3*qup->in_blk_sz))
  67. - qup->use_dma = 1;
  68. -
  69. - if (qup->n_words <= (qup->in_fifo_sz / sizeof(u32)))
  70. - mode = QUP_IO_M_MODE_FIFO;
  71. - else
  72. - mode = QUP_IO_M_MODE_BLOCK;
  73. -
  74. - return mode;
  75. -}
  76. -
  77. /* set clock freq ... bits per word */
  78. static int spi_qup_io_config(struct spi_device *spi, struct spi_transfer *xfer)
  79. {
  80. struct spi_qup *controller = spi_master_get_devdata(spi->master);
  81. - u32 config, iomode, mode, control;
  82. + u32 config, iomode, control;
  83. int ret, n_words;
  84. if (spi->mode & SPI_LOOP && xfer->len > controller->in_fifo_sz) {
  85. @@ -503,24 +478,22 @@ static int spi_qup_io_config(struct spi_
  86. return -EIO;
  87. }
  88. - controller->mode = mode = spi_qup_get_mode(spi->master, xfer);
  89. + controller->w_size = DIV_ROUND_UP(xfer->bits_per_word, 8);
  90. + controller->n_words = xfer->len / controller->w_size;
  91. n_words = controller->n_words;
  92. - if (mode == QUP_IO_M_MODE_FIFO) {
  93. + if (n_words <= (controller->in_fifo_sz / sizeof(u32))) {
  94. + controller->mode = QUP_IO_M_MODE_FIFO;
  95. writel_relaxed(n_words, controller->base + QUP_MX_READ_CNT);
  96. writel_relaxed(n_words, controller->base + QUP_MX_WRITE_CNT);
  97. /* must be zero for FIFO */
  98. writel_relaxed(0, controller->base + QUP_MX_INPUT_CNT);
  99. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  100. controller->use_dma = 0;
  101. - } else if (!controller->use_dma) {
  102. - writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT);
  103. - writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT);
  104. - /* must be zero for BLOCK and BAM */
  105. - writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  106. - writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  107. - } else {
  108. - mode = QUP_IO_M_MODE_BAM;
  109. + } else if (spi->master->can_dma &&
  110. + spi->master->can_dma(spi->master, spi, xfer) &&
  111. + spi->master->cur_msg_mapped) {
  112. + controller->mode = QUP_IO_M_MODE_BAM;
  113. writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  114. writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  115. @@ -541,19 +514,26 @@ static int spi_qup_io_config(struct spi_
  116. writel_relaxed(0, controller->base + QUP_MX_OUTPUT_CNT);
  117. }
  118. + } else {
  119. + controller->mode = QUP_IO_M_MODE_BLOCK;
  120. + writel_relaxed(n_words, controller->base + QUP_MX_INPUT_CNT);
  121. + writel_relaxed(n_words, controller->base + QUP_MX_OUTPUT_CNT);
  122. + /* must be zero for BLOCK and BAM */
  123. + writel_relaxed(0, controller->base + QUP_MX_READ_CNT);
  124. + writel_relaxed(0, controller->base + QUP_MX_WRITE_CNT);
  125. }
  126. iomode = readl_relaxed(controller->base + QUP_IO_M_MODES);
  127. /* Set input and output transfer mode */
  128. iomode &= ~(QUP_IO_M_INPUT_MODE_MASK | QUP_IO_M_OUTPUT_MODE_MASK);
  129. - if (!controller->use_dma)
  130. + if (!spi_qup_is_dma_xfer(controller->mode))
  131. iomode &= ~(QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN);
  132. else
  133. iomode |= QUP_IO_M_PACK_EN | QUP_IO_M_UNPACK_EN;
  134. - iomode |= (mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
  135. - iomode |= (mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
  136. + iomode |= (controller->mode << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT);
  137. + iomode |= (controller->mode << QUP_IO_M_INPUT_MODE_MASK_SHIFT);
  138. writel_relaxed(iomode, controller->base + QUP_IO_M_MODES);
  139. @@ -594,7 +574,7 @@ static int spi_qup_io_config(struct spi_
  140. config |= xfer->bits_per_word - 1;
  141. config |= QUP_CONFIG_SPI_MODE;
  142. - if (controller->use_dma) {
  143. + if (spi_qup_is_dma_xfer(controller->mode)) {
  144. if (!xfer->tx_buf)
  145. config |= QUP_CONFIG_NO_OUTPUT;
  146. if (!xfer->rx_buf)
  147. @@ -612,7 +592,7 @@ static int spi_qup_io_config(struct spi_
  148. * status change in BAM mode
  149. */
  150. - if (mode == QUP_IO_M_MODE_BAM)
  151. + if (spi_qup_is_dma_xfer(controller->mode))
  152. mask = QUP_OP_IN_SERVICE_FLAG | QUP_OP_OUT_SERVICE_FLAG;
  153. writel_relaxed(mask, controller->base + QUP_OPERATIONAL_MASK);
  154. @@ -646,7 +626,7 @@ static int spi_qup_transfer_one(struct s
  155. controller->tx_bytes = 0;
  156. spin_unlock_irqrestore(&controller->lock, flags);
  157. - if (controller->use_dma)
  158. + if (spi_qup_is_dma_xfer(controller->mode))
  159. ret = spi_qup_do_dma(master, xfer);
  160. else
  161. ret = spi_qup_do_pio(master, xfer);
  162. @@ -670,7 +650,7 @@ exit:
  163. ret = controller->error;
  164. spin_unlock_irqrestore(&controller->lock, flags);
  165. - if (ret && controller->use_dma)
  166. + if (ret && spi_qup_is_dma_xfer(controller->mode))
  167. spi_qup_dma_terminate(master, xfer);
  168. return ret;
  169. @@ -681,9 +661,7 @@ static bool spi_qup_can_dma(struct spi_m
  170. {
  171. struct spi_qup *qup = spi_master_get_devdata(master);
  172. size_t dma_align = dma_get_cache_alignment();
  173. - u32 mode;
  174. -
  175. - qup->use_dma = 0;
  176. + int n_words;
  177. if (xfer->rx_buf && (xfer->len % qup->in_blk_sz ||
  178. IS_ERR_OR_NULL(master->dma_rx) ||
  179. @@ -695,12 +673,10 @@ static bool spi_qup_can_dma(struct spi_m
  180. !IS_ALIGNED((size_t)xfer->tx_buf, dma_align)))
  181. return false;
  182. - mode = spi_qup_get_mode(master, xfer);
  183. - if (mode == QUP_IO_M_MODE_FIFO)
  184. + n_words = xfer->len / DIV_ROUND_UP(xfer->bits_per_word, 8);
  185. + if (n_words <= (qup->in_fifo_sz / sizeof(u32)))
  186. return false;
  187. - qup->use_dma = 1;
  188. -
  189. return true;
  190. }