dw_spi_mid.c 5.9 KB

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