renesas_sdhi_internal_dmac.c 10 KB

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