renesas_sdhi_internal_dmac.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/bitops.h>
  12. #include <linux/device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/io-64-nonatomic-hi-lo.h>
  15. #include <linux/mfd/tmio.h>
  16. #include <linux/mmc/host.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/sys_soc.h>
  22. #include "renesas_sdhi.h"
  23. #include "tmio_mmc.h"
  24. #define DM_CM_DTRAN_MODE 0x820
  25. #define DM_CM_DTRAN_CTRL 0x828
  26. #define DM_CM_RST 0x830
  27. #define DM_CM_INFO1 0x840
  28. #define DM_CM_INFO1_MASK 0x848
  29. #define DM_CM_INFO2 0x850
  30. #define DM_CM_INFO2_MASK 0x858
  31. #define DM_DTRAN_ADDR 0x880
  32. /* DM_CM_DTRAN_MODE */
  33. #define DTRAN_MODE_CH_NUM_CH0 0 /* "downstream" = for write commands */
  34. #define DTRAN_MODE_CH_NUM_CH1 BIT(16) /* "uptream" = for read commands */
  35. #define DTRAN_MODE_BUS_WID_TH (BIT(5) | BIT(4))
  36. #define DTRAN_MODE_ADDR_MODE BIT(0) /* 1 = Increment address */
  37. /* DM_CM_DTRAN_CTRL */
  38. #define DTRAN_CTRL_DM_START BIT(0)
  39. /* DM_CM_RST */
  40. #define RST_DTRANRST1 BIT(9)
  41. #define RST_DTRANRST0 BIT(8)
  42. #define RST_RESERVED_BITS GENMASK_ULL(32, 0)
  43. /* DM_CM_INFO1 and DM_CM_INFO1_MASK */
  44. #define INFO1_CLEAR 0
  45. #define INFO1_DTRANEND1 BIT(17)
  46. #define INFO1_DTRANEND0 BIT(16)
  47. /* DM_CM_INFO2 and DM_CM_INFO2_MASK */
  48. #define INFO2_DTRANERR1 BIT(17)
  49. #define INFO2_DTRANERR0 BIT(16)
  50. /*
  51. * Specification of this driver:
  52. * - host->chan_{rx,tx} will be used as a flag of enabling/disabling the dma
  53. * - Since this SDHI DMAC register set has 16 but 32-bit width, we
  54. * need a custom accessor.
  55. */
  56. static unsigned long global_flags;
  57. /*
  58. * Workaround for avoiding to use RX DMAC by multiple channels.
  59. * On R-Car H3 ES1.* and M3-W ES1.0, when multiple SDHI channels use
  60. * RX DMAC simultaneously, sometimes hundreds of bytes data are not
  61. * stored into the system memory even if the DMAC interrupt happened.
  62. * So, this driver then uses one RX DMAC channel only.
  63. */
  64. #define SDHI_INTERNAL_DMAC_ONE_RX_ONLY 0
  65. #define SDHI_INTERNAL_DMAC_RX_IN_USE 1
  66. /* Definitions for sampling clocks */
  67. static struct renesas_sdhi_scc rcar_gen3_scc_taps[] = {
  68. {
  69. .clk_rate = 0,
  70. .tap = 0x00000300,
  71. },
  72. };
  73. static const struct renesas_sdhi_of_data of_rcar_gen3_compatible = {
  74. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
  75. TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
  76. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  77. MMC_CAP_CMD23,
  78. .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
  79. .bus_shift = 2,
  80. .scc_offset = 0x1000,
  81. .taps = rcar_gen3_scc_taps,
  82. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  83. /* DMAC can handle 0xffffffff blk count but only 1 segment */
  84. .max_blk_count = 0xffffffff,
  85. .max_segs = 1,
  86. };
  87. static const struct of_device_id renesas_sdhi_internal_dmac_of_match[] = {
  88. { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
  89. { .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_gen3_compatible, },
  90. { .compatible = "renesas,rcar-gen3-sdhi", .data = &of_rcar_gen3_compatible, },
  91. {},
  92. };
  93. MODULE_DEVICE_TABLE(of, renesas_sdhi_internal_dmac_of_match);
  94. static void
  95. renesas_sdhi_internal_dmac_dm_write(struct tmio_mmc_host *host,
  96. int addr, u64 val)
  97. {
  98. writeq(val, host->ctl + addr);
  99. }
  100. static void
  101. renesas_sdhi_internal_dmac_enable_dma(struct tmio_mmc_host *host, bool enable)
  102. {
  103. struct renesas_sdhi *priv = host_to_priv(host);
  104. if (!host->chan_tx || !host->chan_rx)
  105. return;
  106. if (!enable)
  107. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_INFO1,
  108. INFO1_CLEAR);
  109. if (priv->dma_priv.enable)
  110. priv->dma_priv.enable(host, enable);
  111. }
  112. static void
  113. renesas_sdhi_internal_dmac_abort_dma(struct tmio_mmc_host *host) {
  114. u64 val = RST_DTRANRST1 | RST_DTRANRST0;
  115. renesas_sdhi_internal_dmac_enable_dma(host, false);
  116. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  117. RST_RESERVED_BITS & ~val);
  118. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  119. RST_RESERVED_BITS | val);
  120. if (host->data && host->data->flags & MMC_DATA_READ)
  121. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  122. renesas_sdhi_internal_dmac_enable_dma(host, true);
  123. }
  124. static void
  125. renesas_sdhi_internal_dmac_dataend_dma(struct tmio_mmc_host *host) {
  126. struct renesas_sdhi *priv = host_to_priv(host);
  127. tasklet_schedule(&priv->dma_priv.dma_complete);
  128. }
  129. static void
  130. renesas_sdhi_internal_dmac_start_dma(struct tmio_mmc_host *host,
  131. struct mmc_data *data)
  132. {
  133. struct scatterlist *sg = host->sg_ptr;
  134. u32 dtran_mode = DTRAN_MODE_BUS_WID_TH | DTRAN_MODE_ADDR_MODE;
  135. if (!dma_map_sg(&host->pdev->dev, sg, host->sg_len,
  136. mmc_get_dma_dir(data)))
  137. goto force_pio;
  138. /* This DMAC cannot handle if buffer is not 8-bytes alignment */
  139. if (!IS_ALIGNED(sg_dma_address(sg), 8)) {
  140. dma_unmap_sg(&host->pdev->dev, sg, host->sg_len,
  141. mmc_get_dma_dir(data));
  142. goto force_pio;
  143. }
  144. if (data->flags & MMC_DATA_READ) {
  145. dtran_mode |= DTRAN_MODE_CH_NUM_CH1;
  146. if (test_bit(SDHI_INTERNAL_DMAC_ONE_RX_ONLY, &global_flags) &&
  147. test_and_set_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags))
  148. goto force_pio;
  149. } else {
  150. dtran_mode |= DTRAN_MODE_CH_NUM_CH0;
  151. }
  152. renesas_sdhi_internal_dmac_enable_dma(host, true);
  153. /* set dma parameters */
  154. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_MODE,
  155. dtran_mode);
  156. renesas_sdhi_internal_dmac_dm_write(host, DM_DTRAN_ADDR,
  157. sg_dma_address(sg));
  158. return;
  159. force_pio:
  160. host->force_pio = true;
  161. renesas_sdhi_internal_dmac_enable_dma(host, false);
  162. }
  163. static void renesas_sdhi_internal_dmac_issue_tasklet_fn(unsigned long arg)
  164. {
  165. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  166. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  167. /* start the DMAC */
  168. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_CTRL,
  169. DTRAN_CTRL_DM_START);
  170. }
  171. static void renesas_sdhi_internal_dmac_complete_tasklet_fn(unsigned long arg)
  172. {
  173. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  174. enum dma_data_direction dir;
  175. spin_lock_irq(&host->lock);
  176. if (!host->data)
  177. goto out;
  178. if (host->data->flags & MMC_DATA_READ)
  179. dir = DMA_FROM_DEVICE;
  180. else
  181. dir = DMA_TO_DEVICE;
  182. renesas_sdhi_internal_dmac_enable_dma(host, false);
  183. dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->sg_len, dir);
  184. if (dir == DMA_FROM_DEVICE)
  185. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  186. tmio_mmc_do_data_irq(host);
  187. out:
  188. spin_unlock_irq(&host->lock);
  189. }
  190. static void
  191. renesas_sdhi_internal_dmac_request_dma(struct tmio_mmc_host *host,
  192. struct tmio_mmc_data *pdata)
  193. {
  194. struct renesas_sdhi *priv = host_to_priv(host);
  195. /* Each value is set to non-zero to assume "enabling" each DMA */
  196. host->chan_rx = host->chan_tx = (void *)0xdeadbeaf;
  197. tasklet_init(&priv->dma_priv.dma_complete,
  198. renesas_sdhi_internal_dmac_complete_tasklet_fn,
  199. (unsigned long)host);
  200. tasklet_init(&host->dma_issue,
  201. renesas_sdhi_internal_dmac_issue_tasklet_fn,
  202. (unsigned long)host);
  203. }
  204. static void
  205. renesas_sdhi_internal_dmac_release_dma(struct tmio_mmc_host *host)
  206. {
  207. /* Each value is set to zero to assume "disabling" each DMA */
  208. host->chan_rx = host->chan_tx = NULL;
  209. }
  210. static const struct tmio_mmc_dma_ops renesas_sdhi_internal_dmac_dma_ops = {
  211. .start = renesas_sdhi_internal_dmac_start_dma,
  212. .enable = renesas_sdhi_internal_dmac_enable_dma,
  213. .request = renesas_sdhi_internal_dmac_request_dma,
  214. .release = renesas_sdhi_internal_dmac_release_dma,
  215. .abort = renesas_sdhi_internal_dmac_abort_dma,
  216. .dataend = renesas_sdhi_internal_dmac_dataend_dma,
  217. };
  218. /*
  219. * Whitelist of specific R-Car Gen3 SoC ES versions to use this DMAC
  220. * implementation as others may use a different implementation.
  221. */
  222. static const struct soc_device_attribute gen3_soc_whitelist[] = {
  223. /* specific ones */
  224. { .soc_id = "r8a7795", .revision = "ES1.*",
  225. .data = (void *)BIT(SDHI_INTERNAL_DMAC_ONE_RX_ONLY) },
  226. { .soc_id = "r8a7796", .revision = "ES1.0",
  227. .data = (void *)BIT(SDHI_INTERNAL_DMAC_ONE_RX_ONLY) },
  228. /* generic ones */
  229. { .soc_id = "r8a7795" },
  230. { .soc_id = "r8a7796" },
  231. { .soc_id = "r8a77965" },
  232. { .soc_id = "r8a77980" },
  233. { .soc_id = "r8a77995" },
  234. { /* sentinel */ }
  235. };
  236. static int renesas_sdhi_internal_dmac_probe(struct platform_device *pdev)
  237. {
  238. const struct soc_device_attribute *soc = soc_device_match(gen3_soc_whitelist);
  239. if (!soc)
  240. return -ENODEV;
  241. global_flags |= (unsigned long)soc->data;
  242. return renesas_sdhi_probe(pdev, &renesas_sdhi_internal_dmac_dma_ops);
  243. }
  244. static const struct dev_pm_ops renesas_sdhi_internal_dmac_dev_pm_ops = {
  245. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  246. pm_runtime_force_resume)
  247. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  248. tmio_mmc_host_runtime_resume,
  249. NULL)
  250. };
  251. static struct platform_driver renesas_internal_dmac_sdhi_driver = {
  252. .driver = {
  253. .name = "renesas_sdhi_internal_dmac",
  254. .pm = &renesas_sdhi_internal_dmac_dev_pm_ops,
  255. .of_match_table = renesas_sdhi_internal_dmac_of_match,
  256. },
  257. .probe = renesas_sdhi_internal_dmac_probe,
  258. .remove = renesas_sdhi_remove,
  259. };
  260. module_platform_driver(renesas_internal_dmac_sdhi_driver);
  261. MODULE_DESCRIPTION("Renesas SDHI driver for internal DMAC");
  262. MODULE_AUTHOR("Yoshihiro Shimoda");
  263. MODULE_LICENSE("GPL v2");