spi-dw-mid.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. dma_release_channel(dws->txchan);
  78. dma_release_channel(dws->rxchan);
  79. }
  80. /*
  81. * dws->dma_chan_done is cleared before the dma transfer starts,
  82. * callback for rx/tx channel will each increment it by 1.
  83. * Reaching 2 means the whole spi transaction is done.
  84. */
  85. static void dw_spi_dma_done(void *arg)
  86. {
  87. struct dw_spi *dws = arg;
  88. if (++dws->dma_chan_done != 2)
  89. return;
  90. dw_spi_xfer_done(dws);
  91. }
  92. static int mid_spi_dma_transfer(struct dw_spi *dws, int cs_change)
  93. {
  94. struct dma_async_tx_descriptor *txdesc = NULL, *rxdesc = NULL;
  95. struct dma_chan *txchan, *rxchan;
  96. struct dma_slave_config txconf, rxconf;
  97. u16 dma_ctrl = 0;
  98. /* 1. setup DMA related registers */
  99. if (cs_change) {
  100. spi_enable_chip(dws, 0);
  101. dw_writew(dws, DW_SPI_DMARDLR, 0xf);
  102. dw_writew(dws, DW_SPI_DMATDLR, 0x10);
  103. if (dws->tx_dma)
  104. dma_ctrl |= 0x2;
  105. if (dws->rx_dma)
  106. dma_ctrl |= 0x1;
  107. dw_writew(dws, DW_SPI_DMACR, dma_ctrl);
  108. spi_enable_chip(dws, 1);
  109. }
  110. dws->dma_chan_done = 0;
  111. txchan = dws->txchan;
  112. rxchan = dws->rxchan;
  113. /* 2. Prepare the TX dma transfer */
  114. txconf.direction = DMA_MEM_TO_DEV;
  115. txconf.dst_addr = dws->dma_addr;
  116. txconf.dst_maxburst = LNW_DMA_MSIZE_16;
  117. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  118. txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  119. txconf.device_fc = false;
  120. txchan->device->device_control(txchan, DMA_SLAVE_CONFIG,
  121. (unsigned long) &txconf);
  122. memset(&dws->tx_sgl, 0, sizeof(dws->tx_sgl));
  123. dws->tx_sgl.dma_address = dws->tx_dma;
  124. dws->tx_sgl.length = dws->len;
  125. txdesc = dmaengine_prep_slave_sg(txchan,
  126. &dws->tx_sgl,
  127. 1,
  128. DMA_MEM_TO_DEV,
  129. DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_DEST_UNMAP);
  130. txdesc->callback = dw_spi_dma_done;
  131. txdesc->callback_param = dws;
  132. /* 3. Prepare the RX dma transfer */
  133. rxconf.direction = DMA_DEV_TO_MEM;
  134. rxconf.src_addr = dws->dma_addr;
  135. rxconf.src_maxburst = LNW_DMA_MSIZE_16;
  136. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  137. rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  138. rxconf.device_fc = false;
  139. rxchan->device->device_control(rxchan, DMA_SLAVE_CONFIG,
  140. (unsigned long) &rxconf);
  141. memset(&dws->rx_sgl, 0, sizeof(dws->rx_sgl));
  142. dws->rx_sgl.dma_address = dws->rx_dma;
  143. dws->rx_sgl.length = dws->len;
  144. rxdesc = dmaengine_prep_slave_sg(rxchan,
  145. &dws->rx_sgl,
  146. 1,
  147. DMA_DEV_TO_MEM,
  148. DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_DEST_UNMAP);
  149. rxdesc->callback = dw_spi_dma_done;
  150. rxdesc->callback_param = dws;
  151. /* rx must be started before tx due to spi instinct */
  152. rxdesc->tx_submit(rxdesc);
  153. txdesc->tx_submit(txdesc);
  154. return 0;
  155. }
  156. static struct dw_spi_dma_ops mid_dma_ops = {
  157. .dma_init = mid_spi_dma_init,
  158. .dma_exit = mid_spi_dma_exit,
  159. .dma_transfer = mid_spi_dma_transfer,
  160. };
  161. #endif
  162. /* Some specific info for SPI0 controller on Moorestown */
  163. /* HW info for MRST CLk Control Unit, one 32b reg */
  164. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  165. #define MRST_CLK_SPI0_REG 0xff11d86c
  166. #define CLK_SPI_BDIV_OFFSET 0
  167. #define CLK_SPI_BDIV_MASK 0x00000007
  168. #define CLK_SPI_CDIV_OFFSET 9
  169. #define CLK_SPI_CDIV_MASK 0x00000e00
  170. #define CLK_SPI_DISABLE_OFFSET 8
  171. int dw_spi_mid_init(struct dw_spi *dws)
  172. {
  173. void __iomem *clk_reg;
  174. u32 clk_cdiv;
  175. clk_reg = ioremap_nocache(MRST_CLK_SPI0_REG, 16);
  176. if (!clk_reg)
  177. return -ENOMEM;
  178. /* get SPI controller operating freq info */
  179. clk_cdiv = (readl(clk_reg) & CLK_SPI_CDIV_MASK) >> CLK_SPI_CDIV_OFFSET;
  180. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  181. iounmap(clk_reg);
  182. dws->num_cs = 16;
  183. dws->fifo_len = 40; /* FIFO has 40 words buffer */
  184. #ifdef CONFIG_SPI_DW_MID_DMA
  185. dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL);
  186. if (!dws->dma_priv)
  187. return -ENOMEM;
  188. dws->dma_ops = &mid_dma_ops;
  189. #endif
  190. return 0;
  191. }