renesas_sdhi_internal_dmac.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. .bus_shift = 2,
  79. .scc_offset = 0x1000,
  80. .taps = rcar_gen3_scc_taps,
  81. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  82. /* Gen3 SDHI DMAC can handle 0xffffffff blk count, but seg = 1 */
  83. .max_blk_count = 0xffffffff,
  84. .max_segs = 1,
  85. };
  86. static const struct of_device_id renesas_sdhi_internal_dmac_of_match[] = {
  87. { .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_gen3_compatible, },
  88. { .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_gen3_compatible, },
  89. { .compatible = "renesas,rcar-gen3-sdhi", .data = &of_rcar_gen3_compatible, },
  90. {},
  91. };
  92. MODULE_DEVICE_TABLE(of, renesas_sdhi_internal_dmac_of_match);
  93. static void
  94. renesas_sdhi_internal_dmac_dm_write(struct tmio_mmc_host *host,
  95. int addr, u64 val)
  96. {
  97. writeq(val, host->ctl + addr);
  98. }
  99. static void
  100. renesas_sdhi_internal_dmac_enable_dma(struct tmio_mmc_host *host, bool enable)
  101. {
  102. struct renesas_sdhi *priv = host_to_priv(host);
  103. if (!host->chan_tx || !host->chan_rx)
  104. return;
  105. if (!enable)
  106. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_INFO1,
  107. INFO1_CLEAR);
  108. if (priv->dma_priv.enable)
  109. priv->dma_priv.enable(host, enable);
  110. }
  111. static void
  112. renesas_sdhi_internal_dmac_abort_dma(struct tmio_mmc_host *host) {
  113. u64 val = RST_DTRANRST1 | RST_DTRANRST0;
  114. renesas_sdhi_internal_dmac_enable_dma(host, false);
  115. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  116. RST_RESERVED_BITS & ~val);
  117. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_RST,
  118. RST_RESERVED_BITS | val);
  119. if (host->data && host->data->flags & MMC_DATA_READ)
  120. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  121. renesas_sdhi_internal_dmac_enable_dma(host, true);
  122. }
  123. static void
  124. renesas_sdhi_internal_dmac_dataend_dma(struct tmio_mmc_host *host) {
  125. struct renesas_sdhi *priv = host_to_priv(host);
  126. tasklet_schedule(&priv->dma_priv.dma_complete);
  127. }
  128. static void
  129. renesas_sdhi_internal_dmac_start_dma(struct tmio_mmc_host *host,
  130. struct mmc_data *data)
  131. {
  132. struct scatterlist *sg = host->sg_ptr;
  133. u32 dtran_mode = DTRAN_MODE_BUS_WID_TH | DTRAN_MODE_ADDR_MODE;
  134. enum dma_data_direction dir;
  135. int ret;
  136. /* This DMAC cannot handle if sg_len is not 1 */
  137. WARN_ON(host->sg_len > 1);
  138. /* This DMAC cannot handle if buffer is not 8-bytes alignment */
  139. if (!IS_ALIGNED(sg->offset, 8))
  140. goto force_pio;
  141. if (data->flags & MMC_DATA_READ) {
  142. dtran_mode |= DTRAN_MODE_CH_NUM_CH1;
  143. dir = DMA_FROM_DEVICE;
  144. if (test_bit(SDHI_INTERNAL_DMAC_ONE_RX_ONLY, &global_flags) &&
  145. test_and_set_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags))
  146. goto force_pio;
  147. } else {
  148. dtran_mode |= DTRAN_MODE_CH_NUM_CH0;
  149. dir = DMA_TO_DEVICE;
  150. }
  151. ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, dir);
  152. if (ret == 0)
  153. goto force_pio;
  154. renesas_sdhi_internal_dmac_enable_dma(host, true);
  155. /* set dma parameters */
  156. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_MODE,
  157. dtran_mode);
  158. renesas_sdhi_internal_dmac_dm_write(host, DM_DTRAN_ADDR,
  159. sg->dma_address);
  160. return;
  161. force_pio:
  162. host->force_pio = true;
  163. renesas_sdhi_internal_dmac_enable_dma(host, false);
  164. }
  165. static void renesas_sdhi_internal_dmac_issue_tasklet_fn(unsigned long arg)
  166. {
  167. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  168. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  169. /* start the DMAC */
  170. renesas_sdhi_internal_dmac_dm_write(host, DM_CM_DTRAN_CTRL,
  171. DTRAN_CTRL_DM_START);
  172. }
  173. static void renesas_sdhi_internal_dmac_complete_tasklet_fn(unsigned long arg)
  174. {
  175. struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
  176. enum dma_data_direction dir;
  177. spin_lock_irq(&host->lock);
  178. if (!host->data)
  179. goto out;
  180. if (host->data->flags & MMC_DATA_READ)
  181. dir = DMA_FROM_DEVICE;
  182. else
  183. dir = DMA_TO_DEVICE;
  184. renesas_sdhi_internal_dmac_enable_dma(host, false);
  185. dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->sg_len, dir);
  186. if (dir == DMA_FROM_DEVICE)
  187. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  188. tmio_mmc_do_data_irq(host);
  189. out:
  190. spin_unlock_irq(&host->lock);
  191. }
  192. static void
  193. renesas_sdhi_internal_dmac_request_dma(struct tmio_mmc_host *host,
  194. struct tmio_mmc_data *pdata)
  195. {
  196. struct renesas_sdhi *priv = host_to_priv(host);
  197. /* Each value is set to non-zero to assume "enabling" each DMA */
  198. host->chan_rx = host->chan_tx = (void *)0xdeadbeaf;
  199. tasklet_init(&priv->dma_priv.dma_complete,
  200. renesas_sdhi_internal_dmac_complete_tasklet_fn,
  201. (unsigned long)host);
  202. tasklet_init(&host->dma_issue,
  203. renesas_sdhi_internal_dmac_issue_tasklet_fn,
  204. (unsigned long)host);
  205. }
  206. static void
  207. renesas_sdhi_internal_dmac_release_dma(struct tmio_mmc_host *host)
  208. {
  209. /* Each value is set to zero to assume "disabling" each DMA */
  210. host->chan_rx = host->chan_tx = NULL;
  211. }
  212. static const struct tmio_mmc_dma_ops renesas_sdhi_internal_dmac_dma_ops = {
  213. .start = renesas_sdhi_internal_dmac_start_dma,
  214. .enable = renesas_sdhi_internal_dmac_enable_dma,
  215. .request = renesas_sdhi_internal_dmac_request_dma,
  216. .release = renesas_sdhi_internal_dmac_release_dma,
  217. .abort = renesas_sdhi_internal_dmac_abort_dma,
  218. .dataend = renesas_sdhi_internal_dmac_dataend_dma,
  219. };
  220. /*
  221. * Whitelist of specific R-Car Gen3 SoC ES versions to use this DMAC
  222. * implementation as others may use a different implementation.
  223. */
  224. static const struct soc_device_attribute gen3_soc_whitelist[] = {
  225. { .soc_id = "r8a7795", .revision = "ES1.*",
  226. .data = (void *)BIT(SDHI_INTERNAL_DMAC_ONE_RX_ONLY) },
  227. { .soc_id = "r8a7795", .revision = "ES2.0" },
  228. { .soc_id = "r8a7796", .revision = "ES1.0",
  229. .data = (void *)BIT(SDHI_INTERNAL_DMAC_ONE_RX_ONLY) },
  230. { .soc_id = "r8a77995", .revision = "ES1.0" },
  231. { /* sentinel */ }
  232. };
  233. static int renesas_sdhi_internal_dmac_probe(struct platform_device *pdev)
  234. {
  235. const struct soc_device_attribute *soc = soc_device_match(gen3_soc_whitelist);
  236. if (!soc)
  237. return -ENODEV;
  238. global_flags |= (unsigned long)soc->data;
  239. return renesas_sdhi_probe(pdev, &renesas_sdhi_internal_dmac_dma_ops);
  240. }
  241. static const struct dev_pm_ops renesas_sdhi_internal_dmac_dev_pm_ops = {
  242. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  243. pm_runtime_force_resume)
  244. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  245. tmio_mmc_host_runtime_resume,
  246. NULL)
  247. };
  248. static struct platform_driver renesas_internal_dmac_sdhi_driver = {
  249. .driver = {
  250. .name = "renesas_sdhi_internal_dmac",
  251. .pm = &renesas_sdhi_internal_dmac_dev_pm_ops,
  252. .of_match_table = renesas_sdhi_internal_dmac_of_match,
  253. },
  254. .probe = renesas_sdhi_internal_dmac_probe,
  255. .remove = renesas_sdhi_remove,
  256. };
  257. module_platform_driver(renesas_internal_dmac_sdhi_driver);
  258. MODULE_DESCRIPTION("Renesas SDHI driver for internal DMAC");
  259. MODULE_AUTHOR("Yoshihiro Shimoda");
  260. MODULE_LICENSE("GPL v2");