renesas_sdhi_internal_dmac.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * DMA support for Internal DMAC with SDHI SD/SDIO controller
  3. *
  4. * Copyright (C) 2016-17 Renesas Electronics Corporation
  5. * Copyright (C) 2016-17 Horms Solutions, Simon Horman
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/io-64-nonatomic-hi-lo.h>
  14. #include <linux/mfd/tmio.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/sys_soc.h>
  21. #include "renesas_sdhi.h"
  22. #include "tmio_mmc.h"
  23. #define DM_CM_DTRAN_MODE 0x820
  24. #define DM_CM_DTRAN_CTRL 0x828
  25. #define DM_CM_RST 0x830
  26. #define DM_CM_INFO1 0x840
  27. #define DM_CM_INFO1_MASK 0x848
  28. #define DM_CM_INFO2 0x850
  29. #define DM_CM_INFO2_MASK 0x858
  30. #define DM_DTRAN_ADDR 0x880
  31. /* DM_CM_DTRAN_MODE */
  32. #define DTRAN_MODE_CH_NUM_CH0 0 /* "downstream" = for write commands */
  33. #define DTRAN_MODE_CH_NUM_CH1 BIT(16) /* "uptream" = for read commands */
  34. #define DTRAN_MODE_BUS_WID_TH (BIT(5) | BIT(4))
  35. #define DTRAN_MODE_ADDR_MODE BIT(0) /* 1 = Increment address */
  36. /* DM_CM_DTRAN_CTRL */
  37. #define DTRAN_CTRL_DM_START BIT(0)
  38. /* DM_CM_RST */
  39. #define RST_DTRANRST1 BIT(9)
  40. #define RST_DTRANRST0 BIT(8)
  41. #define RST_RESERVED_BITS GENMASK_ULL(32, 0)
  42. /* DM_CM_INFO1 and DM_CM_INFO1_MASK */
  43. #define INFO1_CLEAR 0
  44. #define INFO1_DTRANEND1 BIT(17)
  45. #define INFO1_DTRANEND0 BIT(16)
  46. /* DM_CM_INFO2 and DM_CM_INFO2_MASK */
  47. #define INFO2_DTRANERR1 BIT(17)
  48. #define INFO2_DTRANERR0 BIT(16)
  49. /*
  50. * Specification of this driver:
  51. * - host->chan_{rx,tx} will be used as a flag of enabling/disabling the dma
  52. * - Since this SDHI DMAC register set has 16 but 32-bit width, we
  53. * need a custom accessor.
  54. */
  55. /* Definitions for sampling clocks */
  56. static struct renesas_sdhi_scc rcar_gen3_scc_taps[] = {
  57. {
  58. .clk_rate = 0,
  59. .tap = 0x00000300,
  60. },
  61. };
  62. static const struct renesas_sdhi_of_data of_rcar_gen3_compatible = {
  63. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_WRPROTECT_DISABLE |
  64. TMIO_MMC_CLK_ACTUAL | TMIO_MMC_HAVE_CBSY |
  65. TMIO_MMC_MIN_RCAR2,
  66. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  67. MMC_CAP_CMD23,
  68. .bus_shift = 2,
  69. .scc_offset = 0x1000,
  70. .taps = rcar_gen3_scc_taps,
  71. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  72. /* Gen3 SDHI DMAC can handle 0xffffffff blk count, but seg = 1 */
  73. .max_blk_count = 0xffffffff,
  74. .max_segs = 1,
  75. };
  76. static const struct of_device_id renesas_sdhi_internal_dmac_of_match[] = {
  77. { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
  78. { .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_gen3_compatible, },
  79. {},
  80. };
  81. MODULE_DEVICE_TABLE(of, renesas_sdhi_internal_dmac_of_match);
  82. static void
  83. renesas_sdhi_internal_dmac_dm_write(struct tmio_mmc_host *host,
  84. int addr, u64 val)
  85. {
  86. writeq(val, host->ctl + addr);
  87. }
  88. static void
  89. renesas_sdhi_internal_dmac_enable_dma(struct tmio_mmc_host *host, bool enable)
  90. {
  91. if (!host->chan_tx || !host->chan_rx)
  92. return;
  93. if (!enable)
  94. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_INFO1,
  95. INFO1_CLEAR);
  96. if (host->dma->enable)
  97. host->dma->enable(host, enable);
  98. }
  99. static void
  100. renesas_sdhi_internal_dmac_abort_dma(struct tmio_mmc_host *host) {
  101. u64 val = RST_DTRANRST1 | RST_DTRANRST0;
  102. renesas_sdhi_internal_dmac_enable_dma(host, false);
  103. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  104. RST_RESERVED_BITS & ~val);
  105. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  106. RST_RESERVED_BITS | val);
  107. renesas_sdhi_internal_dmac_enable_dma(host, true);
  108. }
  109. static void
  110. renesas_sdhi_internal_dmac_dataend_dma(struct tmio_mmc_host *host) {
  111. tasklet_schedule(&host->dma_complete);
  112. }
  113. static void
  114. renesas_sdhi_internal_dmac_start_dma(struct tmio_mmc_host *host,
  115. struct mmc_data *data)
  116. {
  117. struct scatterlist *sg = host->sg_ptr;
  118. u32 dtran_mode = DTRAN_MODE_BUS_WID_TH | DTRAN_MODE_ADDR_MODE;
  119. enum dma_data_direction dir;
  120. int ret;
  121. u32 irq_mask;
  122. /* This DMAC cannot handle if sg_len is not 1 */
  123. WARN_ON(host->sg_len > 1);
  124. /* This DMAC cannot handle if buffer is not 8-bytes alignment */
  125. if (!IS_ALIGNED(sg->offset, 8)) {
  126. host->force_pio = true;
  127. renesas_sdhi_internal_dmac_enable_dma(host, false);
  128. return;
  129. }
  130. if (data->flags & MMC_DATA_READ) {
  131. dtran_mode |= DTRAN_MODE_CH_NUM_CH1;
  132. dir = DMA_FROM_DEVICE;
  133. irq_mask = TMIO_STAT_RXRDY;
  134. } else {
  135. dtran_mode |= DTRAN_MODE_CH_NUM_CH0;
  136. dir = DMA_TO_DEVICE;
  137. irq_mask = TMIO_STAT_TXRQ;
  138. }
  139. ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, dir);
  140. if (ret < 0)
  141. return;
  142. renesas_sdhi_internal_dmac_enable_dma(host, true);
  143. /* disable PIO irqs to avoid "PIO IRQ in DMA mode!" */
  144. tmio_mmc_disable_mmc_irqs(host, irq_mask);
  145. /* set dma parameters */
  146. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_MODE,
  147. dtran_mode);
  148. renesas_sdhi_internal_dmac_dm_write(host, DM_DTRAN_ADDR,
  149. sg->dma_address);
  150. }
  151. static void renesas_sdhi_internal_dmac_issue_tasklet_fn(unsigned long arg)
  152. {
  153. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  154. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  155. /* start the DMAC */
  156. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_CTRL,
  157. DTRAN_CTRL_DM_START);
  158. }
  159. static void renesas_sdhi_internal_dmac_complete_tasklet_fn(unsigned long arg)
  160. {
  161. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  162. enum dma_data_direction dir;
  163. spin_lock_irq(&host->lock);
  164. if (!host->data)
  165. goto out;
  166. if (host->data->flags & MMC_DATA_READ)
  167. dir = DMA_FROM_DEVICE;
  168. else
  169. dir = DMA_TO_DEVICE;
  170. renesas_sdhi_internal_dmac_enable_dma(host, false);
  171. dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->sg_len, dir);
  172. tmio_mmc_do_data_irq(host);
  173. out:
  174. spin_unlock_irq(&host->lock);
  175. }
  176. static void
  177. renesas_sdhi_internal_dmac_request_dma(struct tmio_mmc_host *host,
  178. struct tmio_mmc_data *pdata)
  179. {
  180. /* Each value is set to non-zero to assume "enabling" each DMA */
  181. host->chan_rx = host->chan_tx = (void *)0xdeadbeaf;
  182. tasklet_init(&host->dma_complete,
  183. renesas_sdhi_internal_dmac_complete_tasklet_fn,
  184. (unsigned long)host);
  185. tasklet_init(&host->dma_issue,
  186. renesas_sdhi_internal_dmac_issue_tasklet_fn,
  187. (unsigned long)host);
  188. }
  189. static void
  190. renesas_sdhi_internal_dmac_release_dma(struct tmio_mmc_host *host)
  191. {
  192. /* Each value is set to zero to assume "disabling" each DMA */
  193. host->chan_rx = host->chan_tx = NULL;
  194. }
  195. static const struct tmio_mmc_dma_ops renesas_sdhi_internal_dmac_dma_ops = {
  196. .start = renesas_sdhi_internal_dmac_start_dma,
  197. .enable = renesas_sdhi_internal_dmac_enable_dma,
  198. .request = renesas_sdhi_internal_dmac_request_dma,
  199. .release = renesas_sdhi_internal_dmac_release_dma,
  200. .abort = renesas_sdhi_internal_dmac_abort_dma,
  201. .dataend = renesas_sdhi_internal_dmac_dataend_dma,
  202. };
  203. /*
  204. * Whitelist of specific R-Car Gen3 SoC ES versions to use this DMAC
  205. * implementation as others may use a different implementation.
  206. */
  207. static const struct soc_device_attribute gen3_soc_whitelist[] = {
  208. { .soc_id = "r8a7795", .revision = "ES1.*" },
  209. { .soc_id = "r8a7795", .revision = "ES2.0" },
  210. { .soc_id = "r8a7796", .revision = "ES1.0" },
  211. { /* sentinel */ }
  212. };
  213. static int renesas_sdhi_internal_dmac_probe(struct platform_device *pdev)
  214. {
  215. if (!soc_device_match(gen3_soc_whitelist))
  216. return -ENODEV;
  217. return renesas_sdhi_probe(pdev, &renesas_sdhi_internal_dmac_dma_ops);
  218. }
  219. static const struct dev_pm_ops renesas_sdhi_internal_dmac_dev_pm_ops = {
  220. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  221. pm_runtime_force_resume)
  222. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  223. tmio_mmc_host_runtime_resume,
  224. NULL)
  225. };
  226. static struct platform_driver renesas_internal_dmac_sdhi_driver = {
  227. .driver = {
  228. .name = "renesas_sdhi_internal_dmac",
  229. .pm = &renesas_sdhi_internal_dmac_dev_pm_ops,
  230. .of_match_table = renesas_sdhi_internal_dmac_of_match,
  231. },
  232. .probe = renesas_sdhi_internal_dmac_probe,
  233. .remove = renesas_sdhi_remove,
  234. };
  235. module_platform_driver(renesas_internal_dmac_sdhi_driver);
  236. MODULE_DESCRIPTION("Renesas SDHI driver for internal DMAC");
  237. MODULE_AUTHOR("Yoshihiro Shimoda");
  238. MODULE_LICENSE("GPL v2");