spi-dw-mid.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 int mid_spi_dma_transfer(struct dw_spi *dws, int cs_change)
  95. {
  96. struct dma_async_tx_descriptor *txdesc, *rxdesc;
  97. struct dma_slave_config txconf, rxconf;
  98. u16 dma_ctrl = 0;
  99. /* 1. setup DMA related registers */
  100. if (cs_change) {
  101. spi_enable_chip(dws, 0);
  102. dw_writew(dws, DW_SPI_DMARDLR, 0xf);
  103. dw_writew(dws, DW_SPI_DMATDLR, 0x10);
  104. if (dws->tx_dma)
  105. dma_ctrl |= SPI_DMA_TDMAE;
  106. if (dws->rx_dma)
  107. dma_ctrl |= SPI_DMA_RDMAE;
  108. dw_writew(dws, DW_SPI_DMACR, dma_ctrl);
  109. spi_enable_chip(dws, 1);
  110. }
  111. dws->dma_chan_done = 0;
  112. /* 2. Prepare the TX dma transfer */
  113. txconf.direction = DMA_MEM_TO_DEV;
  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 = dws->dma_width;
  118. txconf.device_fc = false;
  119. dmaengine_slave_config(dws->txchan, &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 = dmaengine_prep_slave_sg(dws->txchan,
  124. &dws->tx_sgl,
  125. 1,
  126. DMA_MEM_TO_DEV,
  127. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  128. txdesc->callback = dw_spi_dma_done;
  129. txdesc->callback_param = dws;
  130. /* 3. Prepare the RX dma transfer */
  131. rxconf.direction = DMA_DEV_TO_MEM;
  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 = dws->dma_width;
  136. rxconf.device_fc = false;
  137. dmaengine_slave_config(dws->rxchan, &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 = dmaengine_prep_slave_sg(dws->rxchan,
  142. &dws->rx_sgl,
  143. 1,
  144. DMA_DEV_TO_MEM,
  145. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  146. rxdesc->callback = dw_spi_dma_done;
  147. rxdesc->callback_param = dws;
  148. /* rx must be started before tx due to spi instinct */
  149. dmaengine_submit(rxdesc);
  150. dma_async_issue_pending(dws->rxchan);
  151. dmaengine_submit(txdesc);
  152. dma_async_issue_pending(dws->txchan);
  153. return 0;
  154. }
  155. static struct dw_spi_dma_ops mid_dma_ops = {
  156. .dma_init = mid_spi_dma_init,
  157. .dma_exit = mid_spi_dma_exit,
  158. .dma_transfer = mid_spi_dma_transfer,
  159. };
  160. #endif
  161. /* Some specific info for SPI0 controller on Intel MID */
  162. /* HW info for MRST CLk Control Unit, one 32b reg */
  163. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  164. #define MRST_CLK_SPI0_REG 0xff11d86c
  165. #define CLK_SPI_BDIV_OFFSET 0
  166. #define CLK_SPI_BDIV_MASK 0x00000007
  167. #define CLK_SPI_CDIV_OFFSET 9
  168. #define CLK_SPI_CDIV_MASK 0x00000e00
  169. #define CLK_SPI_DISABLE_OFFSET 8
  170. int dw_spi_mid_init(struct dw_spi *dws)
  171. {
  172. void __iomem *clk_reg;
  173. u32 clk_cdiv;
  174. clk_reg = ioremap_nocache(MRST_CLK_SPI0_REG, 16);
  175. if (!clk_reg)
  176. return -ENOMEM;
  177. /* get SPI controller operating freq info */
  178. clk_cdiv = (readl(clk_reg) & CLK_SPI_CDIV_MASK) >> CLK_SPI_CDIV_OFFSET;
  179. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  180. iounmap(clk_reg);
  181. dws->num_cs = 16;
  182. dws->fifo_len = 40; /* FIFO has 40 words buffer */
  183. #ifdef CONFIG_SPI_DW_MID_DMA
  184. dws->dma_priv = kzalloc(sizeof(struct mid_dma), GFP_KERNEL);
  185. if (!dws->dma_priv)
  186. return -ENOMEM;
  187. dws->dma_ops = &mid_dma_ops;
  188. #endif
  189. return 0;
  190. }