spi-dw-mid.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Special handling for DW core on Intel MID platform
  3. *
  4. * Copyright (c) 2009, 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/types.h>
  21. #include "spi-dw.h"
  22. #ifdef CONFIG_SPI_DW_MID_DMA
  23. #include <linux/intel_mid_dma.h>
  24. #include <linux/pci.h>
  25. #define RX_BUSY 0
  26. #define TX_BUSY 1
  27. struct mid_dma {
  28. struct intel_mid_dma_slave dmas_tx;
  29. struct intel_mid_dma_slave dmas_rx;
  30. };
  31. static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
  32. {
  33. struct dw_spi *dws = param;
  34. return dws->dma_dev == chan->device->dev;
  35. }
  36. static int mid_spi_dma_init(struct dw_spi *dws)
  37. {
  38. struct mid_dma *dw_dma = dws->dma_priv;
  39. struct pci_dev *dma_dev;
  40. struct intel_mid_dma_slave *rxs, *txs;
  41. dma_cap_mask_t mask;
  42. /*
  43. * Get pci device for DMA controller, currently it could only
  44. * be the DMA controller of Medfield
  45. */
  46. dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
  47. if (!dma_dev)
  48. return -ENODEV;
  49. dws->dma_dev = &dma_dev->dev;
  50. dma_cap_zero(mask);
  51. dma_cap_set(DMA_SLAVE, mask);
  52. /* 1. Init rx channel */
  53. dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws);
  54. if (!dws->rxchan)
  55. goto err_exit;
  56. rxs = &dw_dma->dmas_rx;
  57. rxs->hs_mode = LNW_DMA_HW_HS;
  58. rxs->cfg_mode = LNW_DMA_PER_TO_MEM;
  59. dws->rxchan->private = rxs;
  60. /* 2. Init tx channel */
  61. dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws);
  62. if (!dws->txchan)
  63. goto free_rxchan;
  64. txs = &dw_dma->dmas_tx;
  65. txs->hs_mode = LNW_DMA_HW_HS;
  66. txs->cfg_mode = LNW_DMA_MEM_TO_PER;
  67. dws->txchan->private = txs;
  68. dws->dma_inited = 1;
  69. return 0;
  70. free_rxchan:
  71. dma_release_channel(dws->rxchan);
  72. err_exit:
  73. return -EBUSY;
  74. }
  75. static void mid_spi_dma_exit(struct dw_spi *dws)
  76. {
  77. if (!dws->dma_inited)
  78. return;
  79. dmaengine_terminate_all(dws->txchan);
  80. dma_release_channel(dws->txchan);
  81. dmaengine_terminate_all(dws->rxchan);
  82. dma_release_channel(dws->rxchan);
  83. }
  84. /*
  85. * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
  86. * channel will clear a corresponding bit.
  87. */
  88. static void dw_spi_dma_tx_done(void *arg)
  89. {
  90. struct dw_spi *dws = arg;
  91. if (test_and_clear_bit(TX_BUSY, &dws->dma_chan_busy) & BIT(RX_BUSY))
  92. return;
  93. dw_spi_xfer_done(dws);
  94. }
  95. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws)
  96. {
  97. struct dma_slave_config txconf;
  98. struct dma_async_tx_descriptor *txdesc;
  99. if (!dws->tx_dma)
  100. return NULL;
  101. txconf.direction = DMA_MEM_TO_DEV;
  102. txconf.dst_addr = dws->dma_addr;
  103. txconf.dst_maxburst = LNW_DMA_MSIZE_16;
  104. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  105. txconf.dst_addr_width = dws->dma_width;
  106. txconf.device_fc = false;
  107. dmaengine_slave_config(dws->txchan, &txconf);
  108. memset(&dws->tx_sgl, 0, sizeof(dws->tx_sgl));
  109. dws->tx_sgl.dma_address = dws->tx_dma;
  110. dws->tx_sgl.length = dws->len;
  111. txdesc = dmaengine_prep_slave_sg(dws->txchan,
  112. &dws->tx_sgl,
  113. 1,
  114. DMA_MEM_TO_DEV,
  115. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  116. txdesc->callback = dw_spi_dma_tx_done;
  117. txdesc->callback_param = dws;
  118. return txdesc;
  119. }
  120. /*
  121. * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
  122. * channel will clear a corresponding bit.
  123. */
  124. static void dw_spi_dma_rx_done(void *arg)
  125. {
  126. struct dw_spi *dws = arg;
  127. if (test_and_clear_bit(RX_BUSY, &dws->dma_chan_busy) & BIT(TX_BUSY))
  128. return;
  129. dw_spi_xfer_done(dws);
  130. }
  131. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws)
  132. {
  133. struct dma_slave_config rxconf;
  134. struct dma_async_tx_descriptor *rxdesc;
  135. if (!dws->rx_dma)
  136. return NULL;
  137. rxconf.direction = DMA_DEV_TO_MEM;
  138. rxconf.src_addr = dws->dma_addr;
  139. rxconf.src_maxburst = LNW_DMA_MSIZE_16;
  140. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  141. rxconf.src_addr_width = dws->dma_width;
  142. rxconf.device_fc = false;
  143. dmaengine_slave_config(dws->rxchan, &rxconf);
  144. memset(&dws->rx_sgl, 0, sizeof(dws->rx_sgl));
  145. dws->rx_sgl.dma_address = dws->rx_dma;
  146. dws->rx_sgl.length = dws->len;
  147. rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
  148. &dws->rx_sgl,
  149. 1,
  150. DMA_DEV_TO_MEM,
  151. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  152. rxdesc->callback = dw_spi_dma_rx_done;
  153. rxdesc->callback_param = dws;
  154. return rxdesc;
  155. }
  156. static void dw_spi_dma_setup(struct dw_spi *dws)
  157. {
  158. u16 dma_ctrl = 0;
  159. spi_enable_chip(dws, 0);
  160. dw_writew(dws, DW_SPI_DMARDLR, 0xf);
  161. dw_writew(dws, DW_SPI_DMATDLR, 0x10);
  162. if (dws->tx_dma)
  163. dma_ctrl |= SPI_DMA_TDMAE;
  164. if (dws->rx_dma)
  165. dma_ctrl |= SPI_DMA_RDMAE;
  166. dw_writew(dws, DW_SPI_DMACR, dma_ctrl);
  167. spi_enable_chip(dws, 1);
  168. }
  169. static int mid_spi_dma_transfer(struct dw_spi *dws, int cs_change)
  170. {
  171. struct dma_async_tx_descriptor *txdesc, *rxdesc;
  172. /* 1. setup DMA related registers */
  173. if (cs_change)
  174. dw_spi_dma_setup(dws);
  175. /* 2. Prepare the TX dma transfer */
  176. txdesc = dw_spi_dma_prepare_tx(dws);
  177. /* 3. Prepare the RX dma transfer */
  178. rxdesc = dw_spi_dma_prepare_rx(dws);
  179. /* rx must be started before tx due to spi instinct */
  180. if (rxdesc) {
  181. set_bit(RX_BUSY, &dws->dma_chan_busy);
  182. dmaengine_submit(rxdesc);
  183. dma_async_issue_pending(dws->rxchan);
  184. }
  185. if (txdesc) {
  186. set_bit(TX_BUSY, &dws->dma_chan_busy);
  187. dmaengine_submit(txdesc);
  188. dma_async_issue_pending(dws->txchan);
  189. }
  190. return 0;
  191. }
  192. static struct dw_spi_dma_ops mid_dma_ops = {
  193. .dma_init = mid_spi_dma_init,
  194. .dma_exit = mid_spi_dma_exit,
  195. .dma_transfer = mid_spi_dma_transfer,
  196. };
  197. #endif
  198. /* Some specific info for SPI0 controller on Intel MID */
  199. /* HW info for MRST CLk Control Unit, one 32b reg */
  200. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  201. #define MRST_CLK_SPI0_REG 0xff11d86c
  202. #define CLK_SPI_BDIV_OFFSET 0
  203. #define CLK_SPI_BDIV_MASK 0x00000007
  204. #define CLK_SPI_CDIV_OFFSET 9
  205. #define CLK_SPI_CDIV_MASK 0x00000e00
  206. #define CLK_SPI_DISABLE_OFFSET 8
  207. int dw_spi_mid_init(struct dw_spi *dws)
  208. {
  209. void __iomem *clk_reg;
  210. u32 clk_cdiv;
  211. clk_reg = ioremap_nocache(MRST_CLK_SPI0_REG, 16);
  212. if (!clk_reg)
  213. return -ENOMEM;
  214. /* get SPI controller operating freq info */
  215. clk_cdiv = (readl(clk_reg) & CLK_SPI_CDIV_MASK) >> CLK_SPI_CDIV_OFFSET;
  216. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  217. iounmap(clk_reg);
  218. dws->num_cs = 16;
  219. dws->fifo_len = 40; /* FIFO has 40 words buffer */
  220. #ifdef CONFIG_SPI_DW_MID_DMA
  221. dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL);
  222. if (!dws->dma_priv)
  223. return -ENOMEM;
  224. dws->dma_ops = &mid_dma_ops;
  225. #endif
  226. return 0;
  227. }