spi-dw-mid.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Special handling for DW core on Intel MID platform
  3. *
  4. * Copyright (c) 2009, 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. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dmaengine.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/types.h>
  25. #include "spi-dw.h"
  26. #ifdef CONFIG_SPI_DW_MID_DMA
  27. #include <linux/intel_mid_dma.h>
  28. #include <linux/pci.h>
  29. struct mid_dma {
  30. struct intel_mid_dma_slave dmas_tx;
  31. struct intel_mid_dma_slave dmas_rx;
  32. };
  33. static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
  34. {
  35. struct dw_spi *dws = param;
  36. return dws->dmac && (&dws->dmac->dev == chan->device->dev);
  37. }
  38. static int mid_spi_dma_init(struct dw_spi *dws)
  39. {
  40. struct mid_dma *dw_dma = dws->dma_priv;
  41. struct intel_mid_dma_slave *rxs, *txs;
  42. dma_cap_mask_t mask;
  43. /*
  44. * Get pci device for DMA controller, currently it could only
  45. * be the DMA controller of either Moorestown or Medfield
  46. */
  47. dws->dmac = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0813, NULL);
  48. if (!dws->dmac)
  49. dws->dmac = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
  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 -1;
  74. }
  75. static void mid_spi_dma_exit(struct dw_spi *dws)
  76. {
  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 int mid_spi_dma_transfer(struct dw_spi *dws, int cs_change)
  95. {
  96. struct dma_async_tx_descriptor *txdesc = NULL, *rxdesc = NULL;
  97. struct dma_chan *txchan, *rxchan;
  98. struct dma_slave_config txconf, rxconf;
  99. u16 dma_ctrl = 0;
  100. /* 1. setup DMA related registers */
  101. if (cs_change) {
  102. spi_enable_chip(dws, 0);
  103. dw_writew(dws, DW_SPI_DMARDLR, 0xf);
  104. dw_writew(dws, DW_SPI_DMATDLR, 0x10);
  105. if (dws->tx_dma)
  106. dma_ctrl |= 0x2;
  107. if (dws->rx_dma)
  108. dma_ctrl |= 0x1;
  109. dw_writew(dws, DW_SPI_DMACR, dma_ctrl);
  110. spi_enable_chip(dws, 1);
  111. }
  112. dws->dma_chan_done = 0;
  113. txchan = dws->txchan;
  114. rxchan = dws->rxchan;
  115. /* 2. Prepare the TX dma transfer */
  116. txconf.direction = DMA_MEM_TO_DEV;
  117. txconf.dst_addr = dws->dma_addr;
  118. txconf.dst_maxburst = LNW_DMA_MSIZE_16;
  119. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  120. txconf.dst_addr_width = dws->dma_width;
  121. txconf.device_fc = false;
  122. txchan->device->device_control(txchan, DMA_SLAVE_CONFIG,
  123. (unsigned long) &txconf);
  124. memset(&dws->tx_sgl, 0, sizeof(dws->tx_sgl));
  125. dws->tx_sgl.dma_address = dws->tx_dma;
  126. dws->tx_sgl.length = dws->len;
  127. txdesc = dmaengine_prep_slave_sg(txchan,
  128. &dws->tx_sgl,
  129. 1,
  130. DMA_MEM_TO_DEV,
  131. DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_DEST_UNMAP);
  132. txdesc->callback = dw_spi_dma_done;
  133. txdesc->callback_param = dws;
  134. /* 3. Prepare the RX dma transfer */
  135. rxconf.direction = DMA_DEV_TO_MEM;
  136. rxconf.src_addr = dws->dma_addr;
  137. rxconf.src_maxburst = LNW_DMA_MSIZE_16;
  138. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  139. rxconf.src_addr_width = dws->dma_width;
  140. rxconf.device_fc = false;
  141. rxchan->device->device_control(rxchan, DMA_SLAVE_CONFIG,
  142. (unsigned long) &rxconf);
  143. memset(&dws->rx_sgl, 0, sizeof(dws->rx_sgl));
  144. dws->rx_sgl.dma_address = dws->rx_dma;
  145. dws->rx_sgl.length = dws->len;
  146. rxdesc = dmaengine_prep_slave_sg(rxchan,
  147. &dws->rx_sgl,
  148. 1,
  149. DMA_DEV_TO_MEM,
  150. DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_DEST_UNMAP);
  151. rxdesc->callback = dw_spi_dma_done;
  152. rxdesc->callback_param = dws;
  153. /* rx must be started before tx due to spi instinct */
  154. rxdesc->tx_submit(rxdesc);
  155. txdesc->tx_submit(txdesc);
  156. return 0;
  157. }
  158. static struct dw_spi_dma_ops mid_dma_ops = {
  159. .dma_init = mid_spi_dma_init,
  160. .dma_exit = mid_spi_dma_exit,
  161. .dma_transfer = mid_spi_dma_transfer,
  162. };
  163. #endif
  164. /* Some specific info for SPI0 controller on Moorestown */
  165. /* HW info for MRST CLk Control Unit, one 32b reg */
  166. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  167. #define MRST_CLK_SPI0_REG 0xff11d86c
  168. #define CLK_SPI_BDIV_OFFSET 0
  169. #define CLK_SPI_BDIV_MASK 0x00000007
  170. #define CLK_SPI_CDIV_OFFSET 9
  171. #define CLK_SPI_CDIV_MASK 0x00000e00
  172. #define CLK_SPI_DISABLE_OFFSET 8
  173. int dw_spi_mid_init(struct dw_spi *dws)
  174. {
  175. void __iomem *clk_reg;
  176. u32 clk_cdiv;
  177. clk_reg = ioremap_nocache(MRST_CLK_SPI0_REG, 16);
  178. if (!clk_reg)
  179. return -ENOMEM;
  180. /* get SPI controller operating freq info */
  181. clk_cdiv = (readl(clk_reg) & CLK_SPI_CDIV_MASK) >> CLK_SPI_CDIV_OFFSET;
  182. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  183. iounmap(clk_reg);
  184. dws->num_cs = 16;
  185. #ifdef CONFIG_SPI_DW_MID_DMA
  186. dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL);
  187. if (!dws->dma_priv)
  188. return -ENOMEM;
  189. dws->dma_ops = &mid_dma_ops;
  190. #endif
  191. return 0;
  192. }