spi-dw-mid.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. struct mid_dma {
  26. struct intel_mid_dma_slave dmas_tx;
  27. struct intel_mid_dma_slave dmas_rx;
  28. };
  29. static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
  30. {
  31. struct dw_spi *dws = param;
  32. return dws->dma_dev == chan->device->dev;
  33. }
  34. static int mid_spi_dma_init(struct dw_spi *dws)
  35. {
  36. struct mid_dma *dw_dma = dws->dma_priv;
  37. struct pci_dev *dma_dev;
  38. struct intel_mid_dma_slave *rxs, *txs;
  39. dma_cap_mask_t mask;
  40. /*
  41. * Get pci device for DMA controller, currently it could only
  42. * be the DMA controller of Medfield
  43. */
  44. dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
  45. if (!dma_dev)
  46. return -ENODEV;
  47. dws->dma_dev = &dma_dev->dev;
  48. dma_cap_zero(mask);
  49. dma_cap_set(DMA_SLAVE, mask);
  50. /* 1. Init rx channel */
  51. dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws);
  52. if (!dws->rxchan)
  53. goto err_exit;
  54. rxs = &dw_dma->dmas_rx;
  55. rxs->hs_mode = LNW_DMA_HW_HS;
  56. rxs->cfg_mode = LNW_DMA_PER_TO_MEM;
  57. dws->rxchan->private = rxs;
  58. /* 2. Init tx channel */
  59. dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, dws);
  60. if (!dws->txchan)
  61. goto free_rxchan;
  62. txs = &dw_dma->dmas_tx;
  63. txs->hs_mode = LNW_DMA_HW_HS;
  64. txs->cfg_mode = LNW_DMA_MEM_TO_PER;
  65. dws->txchan->private = txs;
  66. dws->dma_inited = 1;
  67. return 0;
  68. free_rxchan:
  69. dma_release_channel(dws->rxchan);
  70. err_exit:
  71. return -EBUSY;
  72. }
  73. static void mid_spi_dma_exit(struct dw_spi *dws)
  74. {
  75. if (!dws->dma_inited)
  76. return;
  77. dmaengine_terminate_all(dws->txchan);
  78. dma_release_channel(dws->txchan);
  79. dmaengine_terminate_all(dws->rxchan);
  80. dma_release_channel(dws->rxchan);
  81. }
  82. /*
  83. * dws->dma_chan_done is cleared before the dma transfer starts,
  84. * callback for rx/tx channel will each increment it by 1.
  85. * Reaching 2 means the whole spi transaction is done.
  86. */
  87. static void dw_spi_dma_done(void *arg)
  88. {
  89. struct dw_spi *dws = arg;
  90. if (++dws->dma_chan_done != 2)
  91. return;
  92. dw_spi_xfer_done(dws);
  93. }
  94. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws)
  95. {
  96. struct dma_slave_config txconf;
  97. struct dma_async_tx_descriptor *txdesc;
  98. txconf.direction = DMA_MEM_TO_DEV;
  99. txconf.dst_addr = dws->dma_addr;
  100. txconf.dst_maxburst = LNW_DMA_MSIZE_16;
  101. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  102. txconf.dst_addr_width = dws->dma_width;
  103. txconf.device_fc = false;
  104. dmaengine_slave_config(dws->txchan, &txconf);
  105. memset(&dws->tx_sgl, 0, sizeof(dws->tx_sgl));
  106. dws->tx_sgl.dma_address = dws->tx_dma;
  107. dws->tx_sgl.length = dws->len;
  108. txdesc = dmaengine_prep_slave_sg(dws->txchan,
  109. &dws->tx_sgl,
  110. 1,
  111. DMA_MEM_TO_DEV,
  112. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  113. txdesc->callback = dw_spi_dma_done;
  114. txdesc->callback_param = dws;
  115. return txdesc;
  116. }
  117. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws)
  118. {
  119. struct dma_slave_config rxconf;
  120. struct dma_async_tx_descriptor *rxdesc;
  121. rxconf.direction = DMA_DEV_TO_MEM;
  122. rxconf.src_addr = dws->dma_addr;
  123. rxconf.src_maxburst = LNW_DMA_MSIZE_16;
  124. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  125. rxconf.src_addr_width = dws->dma_width;
  126. rxconf.device_fc = false;
  127. dmaengine_slave_config(dws->rxchan, &rxconf);
  128. memset(&dws->rx_sgl, 0, sizeof(dws->rx_sgl));
  129. dws->rx_sgl.dma_address = dws->rx_dma;
  130. dws->rx_sgl.length = dws->len;
  131. rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
  132. &dws->rx_sgl,
  133. 1,
  134. DMA_DEV_TO_MEM,
  135. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  136. rxdesc->callback = dw_spi_dma_done;
  137. rxdesc->callback_param = dws;
  138. return rxdesc;
  139. }
  140. static void dw_spi_dma_setup(struct dw_spi *dws)
  141. {
  142. u16 dma_ctrl = 0;
  143. spi_enable_chip(dws, 0);
  144. dw_writew(dws, DW_SPI_DMARDLR, 0xf);
  145. dw_writew(dws, DW_SPI_DMATDLR, 0x10);
  146. if (dws->tx_dma)
  147. dma_ctrl |= SPI_DMA_TDMAE;
  148. if (dws->rx_dma)
  149. dma_ctrl |= SPI_DMA_RDMAE;
  150. dw_writew(dws, DW_SPI_DMACR, dma_ctrl);
  151. spi_enable_chip(dws, 1);
  152. }
  153. static int mid_spi_dma_transfer(struct dw_spi *dws, int cs_change)
  154. {
  155. struct dma_async_tx_descriptor *txdesc, *rxdesc;
  156. /* 1. setup DMA related registers */
  157. if (cs_change)
  158. dw_spi_dma_setup(dws);
  159. dws->dma_chan_done = 0;
  160. /* 2. Prepare the TX dma transfer */
  161. txdesc = dw_spi_dma_prepare_tx(dws);
  162. /* 3. Prepare the RX dma transfer */
  163. rxdesc = dw_spi_dma_prepare_rx(dws);
  164. /* rx must be started before tx due to spi instinct */
  165. dmaengine_submit(rxdesc);
  166. dma_async_issue_pending(dws->rxchan);
  167. dmaengine_submit(txdesc);
  168. dma_async_issue_pending(dws->txchan);
  169. return 0;
  170. }
  171. static struct dw_spi_dma_ops mid_dma_ops = {
  172. .dma_init = mid_spi_dma_init,
  173. .dma_exit = mid_spi_dma_exit,
  174. .dma_transfer = mid_spi_dma_transfer,
  175. };
  176. #endif
  177. /* Some specific info for SPI0 controller on Intel MID */
  178. /* HW info for MRST CLk Control Unit, one 32b reg */
  179. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  180. #define MRST_CLK_SPI0_REG 0xff11d86c
  181. #define CLK_SPI_BDIV_OFFSET 0
  182. #define CLK_SPI_BDIV_MASK 0x00000007
  183. #define CLK_SPI_CDIV_OFFSET 9
  184. #define CLK_SPI_CDIV_MASK 0x00000e00
  185. #define CLK_SPI_DISABLE_OFFSET 8
  186. int dw_spi_mid_init(struct dw_spi *dws)
  187. {
  188. void __iomem *clk_reg;
  189. u32 clk_cdiv;
  190. clk_reg = ioremap_nocache(MRST_CLK_SPI0_REG, 16);
  191. if (!clk_reg)
  192. return -ENOMEM;
  193. /* get SPI controller operating freq info */
  194. clk_cdiv = (readl(clk_reg) & CLK_SPI_CDIV_MASK) >> CLK_SPI_CDIV_OFFSET;
  195. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  196. iounmap(clk_reg);
  197. dws->num_cs = 16;
  198. dws->fifo_len = 40; /* FIFO has 40 words buffer */
  199. #ifdef CONFIG_SPI_DW_MID_DMA
  200. dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL);
  201. if (!dws->dma_priv)
  202. return -ENOMEM;
  203. dws->dma_ops = &mid_dma_ops;
  204. #endif
  205. return 0;
  206. }