spi-dw-mid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/pci.h>
  24. #include <linux/platform_data/dma-dw.h>
  25. #define RX_BUSY 0
  26. #define TX_BUSY 1
  27. static struct dw_dma_slave mid_dma_tx = { .dst_id = 1 };
  28. static struct dw_dma_slave mid_dma_rx = { .src_id = 0 };
  29. static bool mid_spi_dma_chan_filter(struct dma_chan *chan, void *param)
  30. {
  31. struct dw_dma_slave *s = param;
  32. if (s->dma_dev != chan->device->dev)
  33. return false;
  34. chan->private = s;
  35. return true;
  36. }
  37. static int mid_spi_dma_init(struct dw_spi *dws)
  38. {
  39. struct pci_dev *dma_dev;
  40. struct dw_dma_slave *tx = dws->dma_tx;
  41. struct dw_dma_slave *rx = dws->dma_rx;
  42. dma_cap_mask_t mask;
  43. /*
  44. * Get pci device for DMA controller, currently it could only
  45. * be the DMA controller of Medfield
  46. */
  47. dma_dev = pci_get_device(PCI_VENDOR_ID_INTEL, 0x0827, NULL);
  48. if (!dma_dev)
  49. return -ENODEV;
  50. dma_cap_zero(mask);
  51. dma_cap_set(DMA_SLAVE, mask);
  52. /* 1. Init rx channel */
  53. rx->dma_dev = &dma_dev->dev;
  54. dws->rxchan = dma_request_channel(mask, mid_spi_dma_chan_filter, rx);
  55. if (!dws->rxchan)
  56. goto err_exit;
  57. dws->master->dma_rx = dws->rxchan;
  58. /* 2. Init tx channel */
  59. tx->dma_dev = &dma_dev->dev;
  60. dws->txchan = dma_request_channel(mask, mid_spi_dma_chan_filter, tx);
  61. if (!dws->txchan)
  62. goto free_rxchan;
  63. dws->master->dma_tx = dws->txchan;
  64. dws->dma_inited = 1;
  65. return 0;
  66. free_rxchan:
  67. dma_release_channel(dws->rxchan);
  68. err_exit:
  69. return -EBUSY;
  70. }
  71. static void mid_spi_dma_exit(struct dw_spi *dws)
  72. {
  73. if (!dws->dma_inited)
  74. return;
  75. dmaengine_terminate_sync(dws->txchan);
  76. dma_release_channel(dws->txchan);
  77. dmaengine_terminate_sync(dws->rxchan);
  78. dma_release_channel(dws->rxchan);
  79. }
  80. static irqreturn_t dma_transfer(struct dw_spi *dws)
  81. {
  82. u16 irq_status = dw_readl(dws, DW_SPI_ISR);
  83. if (!irq_status)
  84. return IRQ_NONE;
  85. dw_readl(dws, DW_SPI_ICR);
  86. spi_reset_chip(dws);
  87. dev_err(&dws->master->dev, "%s: FIFO overrun/underrun\n", __func__);
  88. dws->master->cur_msg->status = -EIO;
  89. spi_finalize_current_transfer(dws->master);
  90. return IRQ_HANDLED;
  91. }
  92. static bool mid_spi_can_dma(struct spi_master *master, struct spi_device *spi,
  93. struct spi_transfer *xfer)
  94. {
  95. struct dw_spi *dws = spi_master_get_devdata(master);
  96. if (!dws->dma_inited)
  97. return false;
  98. return xfer->len > dws->fifo_len;
  99. }
  100. static enum dma_slave_buswidth convert_dma_width(u32 dma_width) {
  101. if (dma_width == 1)
  102. return DMA_SLAVE_BUSWIDTH_1_BYTE;
  103. else if (dma_width == 2)
  104. return DMA_SLAVE_BUSWIDTH_2_BYTES;
  105. return DMA_SLAVE_BUSWIDTH_UNDEFINED;
  106. }
  107. /*
  108. * dws->dma_chan_busy is set before the dma transfer starts, callback for tx
  109. * channel will clear a corresponding bit.
  110. */
  111. static void dw_spi_dma_tx_done(void *arg)
  112. {
  113. struct dw_spi *dws = arg;
  114. clear_bit(TX_BUSY, &dws->dma_chan_busy);
  115. if (test_bit(RX_BUSY, &dws->dma_chan_busy))
  116. return;
  117. spi_finalize_current_transfer(dws->master);
  118. }
  119. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_tx(struct dw_spi *dws,
  120. struct spi_transfer *xfer)
  121. {
  122. struct dma_slave_config txconf;
  123. struct dma_async_tx_descriptor *txdesc;
  124. if (!xfer->tx_buf)
  125. return NULL;
  126. memset(&txconf, 0, sizeof(txconf));
  127. txconf.direction = DMA_MEM_TO_DEV;
  128. txconf.dst_addr = dws->dma_addr;
  129. txconf.dst_maxburst = 16;
  130. txconf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  131. txconf.dst_addr_width = convert_dma_width(dws->dma_width);
  132. txconf.device_fc = false;
  133. dmaengine_slave_config(dws->txchan, &txconf);
  134. txdesc = dmaengine_prep_slave_sg(dws->txchan,
  135. xfer->tx_sg.sgl,
  136. xfer->tx_sg.nents,
  137. DMA_MEM_TO_DEV,
  138. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  139. if (!txdesc)
  140. return NULL;
  141. txdesc->callback = dw_spi_dma_tx_done;
  142. txdesc->callback_param = dws;
  143. return txdesc;
  144. }
  145. /*
  146. * dws->dma_chan_busy is set before the dma transfer starts, callback for rx
  147. * channel will clear a corresponding bit.
  148. */
  149. static void dw_spi_dma_rx_done(void *arg)
  150. {
  151. struct dw_spi *dws = arg;
  152. clear_bit(RX_BUSY, &dws->dma_chan_busy);
  153. if (test_bit(TX_BUSY, &dws->dma_chan_busy))
  154. return;
  155. spi_finalize_current_transfer(dws->master);
  156. }
  157. static struct dma_async_tx_descriptor *dw_spi_dma_prepare_rx(struct dw_spi *dws,
  158. struct spi_transfer *xfer)
  159. {
  160. struct dma_slave_config rxconf;
  161. struct dma_async_tx_descriptor *rxdesc;
  162. if (!xfer->rx_buf)
  163. return NULL;
  164. memset(&rxconf, 0, sizeof(rxconf));
  165. rxconf.direction = DMA_DEV_TO_MEM;
  166. rxconf.src_addr = dws->dma_addr;
  167. rxconf.src_maxburst = 16;
  168. rxconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  169. rxconf.src_addr_width = convert_dma_width(dws->dma_width);
  170. rxconf.device_fc = false;
  171. dmaengine_slave_config(dws->rxchan, &rxconf);
  172. rxdesc = dmaengine_prep_slave_sg(dws->rxchan,
  173. xfer->rx_sg.sgl,
  174. xfer->rx_sg.nents,
  175. DMA_DEV_TO_MEM,
  176. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  177. if (!rxdesc)
  178. return NULL;
  179. rxdesc->callback = dw_spi_dma_rx_done;
  180. rxdesc->callback_param = dws;
  181. return rxdesc;
  182. }
  183. static int mid_spi_dma_setup(struct dw_spi *dws, struct spi_transfer *xfer)
  184. {
  185. u16 imr = 0, dma_ctrl = 0;
  186. dw_writel(dws, DW_SPI_DMARDLR, 0xf);
  187. dw_writel(dws, DW_SPI_DMATDLR, 0x10);
  188. if (xfer->tx_buf) {
  189. dma_ctrl |= SPI_DMA_TDMAE;
  190. imr |= SPI_INT_TXOI;
  191. }
  192. if (xfer->rx_buf) {
  193. dma_ctrl |= SPI_DMA_RDMAE;
  194. imr |= SPI_INT_RXUI | SPI_INT_RXOI;
  195. }
  196. dw_writel(dws, DW_SPI_DMACR, dma_ctrl);
  197. /* Set the interrupt mask */
  198. spi_umask_intr(dws, imr);
  199. dws->transfer_handler = dma_transfer;
  200. return 0;
  201. }
  202. static int mid_spi_dma_transfer(struct dw_spi *dws, struct spi_transfer *xfer)
  203. {
  204. struct dma_async_tx_descriptor *txdesc, *rxdesc;
  205. /* Prepare the TX dma transfer */
  206. txdesc = dw_spi_dma_prepare_tx(dws, xfer);
  207. /* Prepare the RX dma transfer */
  208. rxdesc = dw_spi_dma_prepare_rx(dws, xfer);
  209. /* rx must be started before tx due to spi instinct */
  210. if (rxdesc) {
  211. set_bit(RX_BUSY, &dws->dma_chan_busy);
  212. dmaengine_submit(rxdesc);
  213. dma_async_issue_pending(dws->rxchan);
  214. }
  215. if (txdesc) {
  216. set_bit(TX_BUSY, &dws->dma_chan_busy);
  217. dmaengine_submit(txdesc);
  218. dma_async_issue_pending(dws->txchan);
  219. }
  220. return 1;
  221. }
  222. static void mid_spi_dma_stop(struct dw_spi *dws)
  223. {
  224. if (test_bit(TX_BUSY, &dws->dma_chan_busy)) {
  225. dmaengine_terminate_sync(dws->txchan);
  226. clear_bit(TX_BUSY, &dws->dma_chan_busy);
  227. }
  228. if (test_bit(RX_BUSY, &dws->dma_chan_busy)) {
  229. dmaengine_terminate_sync(dws->rxchan);
  230. clear_bit(RX_BUSY, &dws->dma_chan_busy);
  231. }
  232. }
  233. static const struct dw_spi_dma_ops mid_dma_ops = {
  234. .dma_init = mid_spi_dma_init,
  235. .dma_exit = mid_spi_dma_exit,
  236. .dma_setup = mid_spi_dma_setup,
  237. .can_dma = mid_spi_can_dma,
  238. .dma_transfer = mid_spi_dma_transfer,
  239. .dma_stop = mid_spi_dma_stop,
  240. };
  241. #endif
  242. /* Some specific info for SPI0 controller on Intel MID */
  243. /* HW info for MRST Clk Control Unit, 32b reg per controller */
  244. #define MRST_SPI_CLK_BASE 100000000 /* 100m */
  245. #define MRST_CLK_SPI_REG 0xff11d86c
  246. #define CLK_SPI_BDIV_OFFSET 0
  247. #define CLK_SPI_BDIV_MASK 0x00000007
  248. #define CLK_SPI_CDIV_OFFSET 9
  249. #define CLK_SPI_CDIV_MASK 0x00000e00
  250. #define CLK_SPI_DISABLE_OFFSET 8
  251. int dw_spi_mid_init(struct dw_spi *dws)
  252. {
  253. void __iomem *clk_reg;
  254. u32 clk_cdiv;
  255. clk_reg = ioremap_nocache(MRST_CLK_SPI_REG, 16);
  256. if (!clk_reg)
  257. return -ENOMEM;
  258. /* Get SPI controller operating freq info */
  259. clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
  260. clk_cdiv &= CLK_SPI_CDIV_MASK;
  261. clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
  262. dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
  263. iounmap(clk_reg);
  264. #ifdef CONFIG_SPI_DW_MID_DMA
  265. dws->dma_tx = &mid_dma_tx;
  266. dws->dma_rx = &mid_dma_rx;
  267. dws->dma_ops = &mid_dma_ops;
  268. #endif
  269. return 0;
  270. }