renesas_sdhi_sys_dmac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMA support use of SYS DMAC with SDHI SD/SDIO controller
  4. *
  5. * Copyright (C) 2016-19 Renesas Electronics Corporation
  6. * Copyright (C) 2016-19 Sang Engineering, Wolfram Sang
  7. * Copyright (C) 2017 Horms Solutions, Simon Horman
  8. * Copyright (C) 2010-2011 Guennadi Liakhovetski
  9. */
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/platform_data/tmio.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/sys_soc.h>
  23. #include "renesas_sdhi.h"
  24. #include "tmio_mmc.h"
  25. #define TMIO_MMC_MIN_DMA_LEN 8
  26. static const struct renesas_sdhi_of_data of_default_cfg = {
  27. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
  28. };
  29. static const struct renesas_sdhi_of_data of_rz_compatible = {
  30. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_32BIT_DATA_PORT |
  31. TMIO_MMC_HAVE_CBSY,
  32. .tmio_ocr_mask = MMC_VDD_32_33,
  33. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  34. MMC_CAP_WAIT_WHILE_BUSY,
  35. };
  36. static const struct renesas_sdhi_of_data of_rcar_gen1_compatible = {
  37. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL,
  38. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  39. MMC_CAP_WAIT_WHILE_BUSY,
  40. .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
  41. };
  42. /* Definitions for sampling clocks */
  43. static struct renesas_sdhi_scc rcar_gen2_scc_taps[] = {
  44. {
  45. .clk_rate = 156000000,
  46. .tap = 0x00000703,
  47. },
  48. {
  49. .clk_rate = 0,
  50. .tap = 0x00000300,
  51. },
  52. };
  53. static const struct renesas_sdhi_of_data of_rcar_gen2_compatible = {
  54. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
  55. TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2 |
  56. TMIO_MMC_32BIT_DATA_PORT,
  57. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  58. MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
  59. .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
  60. .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
  61. .dma_rx_offset = 0x2000,
  62. .scc_offset = 0x0300,
  63. .taps = rcar_gen2_scc_taps,
  64. .taps_num = ARRAY_SIZE(rcar_gen2_scc_taps),
  65. .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
  66. };
  67. static const struct of_device_id renesas_sdhi_sys_dmac_of_match[] = {
  68. { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
  69. { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
  70. { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
  71. { .compatible = "renesas,sdhi-r7s72100", .data = &of_rz_compatible, },
  72. { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
  73. { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
  74. { .compatible = "renesas,sdhi-r8a7743", .data = &of_rcar_gen2_compatible, },
  75. { .compatible = "renesas,sdhi-r8a7745", .data = &of_rcar_gen2_compatible, },
  76. { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
  77. { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
  78. { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
  79. { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
  80. { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
  81. { .compatible = "renesas,rcar-gen1-sdhi", .data = &of_rcar_gen1_compatible, },
  82. { .compatible = "renesas,rcar-gen2-sdhi", .data = &of_rcar_gen2_compatible, },
  83. { .compatible = "renesas,sdhi-shmobile" },
  84. {},
  85. };
  86. MODULE_DEVICE_TABLE(of, renesas_sdhi_sys_dmac_of_match);
  87. static void renesas_sdhi_sys_dmac_enable_dma(struct tmio_mmc_host *host,
  88. bool enable)
  89. {
  90. struct renesas_sdhi *priv = host_to_priv(host);
  91. if (!host->chan_tx || !host->chan_rx)
  92. return;
  93. if (priv->dma_priv.enable)
  94. priv->dma_priv.enable(host, enable);
  95. }
  96. static void renesas_sdhi_sys_dmac_abort_dma(struct tmio_mmc_host *host)
  97. {
  98. renesas_sdhi_sys_dmac_enable_dma(host, false);
  99. if (host->chan_rx)
  100. dmaengine_terminate_sync(host->chan_rx);
  101. if (host->chan_tx)
  102. dmaengine_terminate_sync(host->chan_tx);
  103. renesas_sdhi_sys_dmac_enable_dma(host, true);
  104. }
  105. static void renesas_sdhi_sys_dmac_dataend_dma(struct tmio_mmc_host *host)
  106. {
  107. struct renesas_sdhi *priv = host_to_priv(host);
  108. complete(&priv->dma_priv.dma_dataend);
  109. }
  110. static void renesas_sdhi_sys_dmac_dma_callback(void *arg)
  111. {
  112. struct tmio_mmc_host *host = arg;
  113. struct renesas_sdhi *priv = host_to_priv(host);
  114. spin_lock_irq(&host->lock);
  115. if (!host->data)
  116. goto out;
  117. if (host->data->flags & MMC_DATA_READ)
  118. dma_unmap_sg(host->chan_rx->device->dev,
  119. host->sg_ptr, host->sg_len,
  120. DMA_FROM_DEVICE);
  121. else
  122. dma_unmap_sg(host->chan_tx->device->dev,
  123. host->sg_ptr, host->sg_len,
  124. DMA_TO_DEVICE);
  125. spin_unlock_irq(&host->lock);
  126. wait_for_completion(&priv->dma_priv.dma_dataend);
  127. spin_lock_irq(&host->lock);
  128. tmio_mmc_do_data_irq(host);
  129. out:
  130. spin_unlock_irq(&host->lock);
  131. }
  132. static void renesas_sdhi_sys_dmac_start_dma_rx(struct tmio_mmc_host *host)
  133. {
  134. struct renesas_sdhi *priv = host_to_priv(host);
  135. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  136. struct dma_async_tx_descriptor *desc = NULL;
  137. struct dma_chan *chan = host->chan_rx;
  138. dma_cookie_t cookie;
  139. int ret, i;
  140. bool aligned = true, multiple = true;
  141. unsigned int align = 1; /* 2-byte alignment */
  142. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  143. if (sg_tmp->offset & align)
  144. aligned = false;
  145. if (sg_tmp->length & align) {
  146. multiple = false;
  147. break;
  148. }
  149. }
  150. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
  151. (align & PAGE_MASK))) || !multiple) {
  152. ret = -EINVAL;
  153. goto pio;
  154. }
  155. if (sg->length < TMIO_MMC_MIN_DMA_LEN)
  156. return;
  157. /* The only sg element can be unaligned, use our bounce buffer then */
  158. if (!aligned) {
  159. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  160. host->sg_ptr = &host->bounce_sg;
  161. sg = host->sg_ptr;
  162. }
  163. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
  164. if (ret > 0)
  165. desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_DEV_TO_MEM,
  166. DMA_CTRL_ACK);
  167. if (desc) {
  168. reinit_completion(&priv->dma_priv.dma_dataend);
  169. desc->callback = renesas_sdhi_sys_dmac_dma_callback;
  170. desc->callback_param = host;
  171. cookie = dmaengine_submit(desc);
  172. if (cookie < 0) {
  173. desc = NULL;
  174. ret = cookie;
  175. }
  176. host->dma_on = true;
  177. }
  178. pio:
  179. if (!desc) {
  180. /* DMA failed, fall back to PIO */
  181. renesas_sdhi_sys_dmac_enable_dma(host, false);
  182. if (ret >= 0)
  183. ret = -EIO;
  184. host->chan_rx = NULL;
  185. dma_release_channel(chan);
  186. /* Free the Tx channel too */
  187. chan = host->chan_tx;
  188. if (chan) {
  189. host->chan_tx = NULL;
  190. dma_release_channel(chan);
  191. }
  192. dev_warn(&host->pdev->dev,
  193. "DMA failed: %d, falling back to PIO\n", ret);
  194. }
  195. }
  196. static void renesas_sdhi_sys_dmac_start_dma_tx(struct tmio_mmc_host *host)
  197. {
  198. struct renesas_sdhi *priv = host_to_priv(host);
  199. struct scatterlist *sg = host->sg_ptr, *sg_tmp;
  200. struct dma_async_tx_descriptor *desc = NULL;
  201. struct dma_chan *chan = host->chan_tx;
  202. dma_cookie_t cookie;
  203. int ret, i;
  204. bool aligned = true, multiple = true;
  205. unsigned int align = 1; /* 2-byte alignment */
  206. for_each_sg(sg, sg_tmp, host->sg_len, i) {
  207. if (sg_tmp->offset & align)
  208. aligned = false;
  209. if (sg_tmp->length & align) {
  210. multiple = false;
  211. break;
  212. }
  213. }
  214. if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
  215. (align & PAGE_MASK))) || !multiple) {
  216. ret = -EINVAL;
  217. goto pio;
  218. }
  219. if (sg->length < TMIO_MMC_MIN_DMA_LEN)
  220. return;
  221. /* The only sg element can be unaligned, use our bounce buffer then */
  222. if (!aligned) {
  223. void *sg_vaddr = kmap_local_page(sg_page(sg));
  224. sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
  225. memcpy(host->bounce_buf, sg_vaddr + sg->offset, host->bounce_sg.length);
  226. kunmap_local(sg_vaddr);
  227. host->sg_ptr = &host->bounce_sg;
  228. sg = host->sg_ptr;
  229. }
  230. ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
  231. if (ret > 0)
  232. desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_MEM_TO_DEV,
  233. DMA_CTRL_ACK);
  234. if (desc) {
  235. reinit_completion(&priv->dma_priv.dma_dataend);
  236. desc->callback = renesas_sdhi_sys_dmac_dma_callback;
  237. desc->callback_param = host;
  238. cookie = dmaengine_submit(desc);
  239. if (cookie < 0) {
  240. desc = NULL;
  241. ret = cookie;
  242. }
  243. host->dma_on = true;
  244. }
  245. pio:
  246. if (!desc) {
  247. /* DMA failed, fall back to PIO */
  248. renesas_sdhi_sys_dmac_enable_dma(host, false);
  249. if (ret >= 0)
  250. ret = -EIO;
  251. host->chan_tx = NULL;
  252. dma_release_channel(chan);
  253. /* Free the Rx channel too */
  254. chan = host->chan_rx;
  255. if (chan) {
  256. host->chan_rx = NULL;
  257. dma_release_channel(chan);
  258. }
  259. dev_warn(&host->pdev->dev,
  260. "DMA failed: %d, falling back to PIO\n", ret);
  261. }
  262. }
  263. static void renesas_sdhi_sys_dmac_start_dma(struct tmio_mmc_host *host,
  264. struct mmc_data *data)
  265. {
  266. if (data->flags & MMC_DATA_READ) {
  267. if (host->chan_rx)
  268. renesas_sdhi_sys_dmac_start_dma_rx(host);
  269. } else {
  270. if (host->chan_tx)
  271. renesas_sdhi_sys_dmac_start_dma_tx(host);
  272. }
  273. }
  274. static void renesas_sdhi_sys_dmac_issue_work_fn(struct work_struct *work)
  275. {
  276. struct tmio_mmc_host *host = from_work(host, work, dma_issue);
  277. struct dma_chan *chan = NULL;
  278. spin_lock_irq(&host->lock);
  279. if (host->data) {
  280. if (host->data->flags & MMC_DATA_READ)
  281. chan = host->chan_rx;
  282. else
  283. chan = host->chan_tx;
  284. }
  285. spin_unlock_irq(&host->lock);
  286. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  287. if (chan)
  288. dma_async_issue_pending(chan);
  289. }
  290. static void renesas_sdhi_sys_dmac_request_dma(struct tmio_mmc_host *host,
  291. struct tmio_mmc_data *pdata)
  292. {
  293. struct renesas_sdhi *priv = host_to_priv(host);
  294. /* We can only either use DMA for both Tx and Rx or not use it at all */
  295. if (!host->pdev->dev.of_node &&
  296. (!pdata->chan_priv_tx || !pdata->chan_priv_rx))
  297. return;
  298. if (!host->chan_tx && !host->chan_rx) {
  299. struct resource *res = platform_get_resource(host->pdev,
  300. IORESOURCE_MEM, 0);
  301. struct dma_slave_config cfg = {};
  302. dma_cap_mask_t mask;
  303. int ret;
  304. if (!res)
  305. return;
  306. dma_cap_zero(mask);
  307. dma_cap_set(DMA_SLAVE, mask);
  308. host->chan_tx = dma_request_slave_channel_compat(mask,
  309. priv->dma_priv.filter, pdata->chan_priv_tx,
  310. &host->pdev->dev, "tx");
  311. dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
  312. host->chan_tx);
  313. if (!host->chan_tx)
  314. return;
  315. cfg.direction = DMA_MEM_TO_DEV;
  316. cfg.dst_addr = res->start +
  317. (CTL_SD_DATA_PORT << host->bus_shift);
  318. cfg.dst_addr_width = priv->dma_priv.dma_buswidth;
  319. if (!cfg.dst_addr_width)
  320. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  321. cfg.src_addr = 0;
  322. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  323. if (ret < 0)
  324. goto ecfgtx;
  325. host->chan_rx = dma_request_slave_channel_compat(mask,
  326. priv->dma_priv.filter, pdata->chan_priv_rx,
  327. &host->pdev->dev, "rx");
  328. dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
  329. host->chan_rx);
  330. if (!host->chan_rx)
  331. goto ereqrx;
  332. cfg.direction = DMA_DEV_TO_MEM;
  333. cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
  334. cfg.src_addr_width = priv->dma_priv.dma_buswidth;
  335. if (!cfg.src_addr_width)
  336. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  337. cfg.dst_addr = 0;
  338. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  339. if (ret < 0)
  340. goto ecfgrx;
  341. host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
  342. if (!host->bounce_buf)
  343. goto ebouncebuf;
  344. init_completion(&priv->dma_priv.dma_dataend);
  345. INIT_WORK(&host->dma_issue,
  346. renesas_sdhi_sys_dmac_issue_work_fn);
  347. }
  348. renesas_sdhi_sys_dmac_enable_dma(host, true);
  349. return;
  350. ebouncebuf:
  351. ecfgrx:
  352. dma_release_channel(host->chan_rx);
  353. host->chan_rx = NULL;
  354. ereqrx:
  355. ecfgtx:
  356. dma_release_channel(host->chan_tx);
  357. host->chan_tx = NULL;
  358. }
  359. static void renesas_sdhi_sys_dmac_release_dma(struct tmio_mmc_host *host)
  360. {
  361. if (host->chan_tx) {
  362. struct dma_chan *chan = host->chan_tx;
  363. host->chan_tx = NULL;
  364. dma_release_channel(chan);
  365. }
  366. if (host->chan_rx) {
  367. struct dma_chan *chan = host->chan_rx;
  368. host->chan_rx = NULL;
  369. dma_release_channel(chan);
  370. }
  371. if (host->bounce_buf) {
  372. free_pages((unsigned long)host->bounce_buf, 0);
  373. host->bounce_buf = NULL;
  374. }
  375. }
  376. static const struct tmio_mmc_dma_ops renesas_sdhi_sys_dmac_dma_ops = {
  377. .start = renesas_sdhi_sys_dmac_start_dma,
  378. .enable = renesas_sdhi_sys_dmac_enable_dma,
  379. .request = renesas_sdhi_sys_dmac_request_dma,
  380. .release = renesas_sdhi_sys_dmac_release_dma,
  381. .abort = renesas_sdhi_sys_dmac_abort_dma,
  382. .dataend = renesas_sdhi_sys_dmac_dataend_dma,
  383. };
  384. static int renesas_sdhi_sys_dmac_probe(struct platform_device *pdev)
  385. {
  386. return renesas_sdhi_probe(pdev, &renesas_sdhi_sys_dmac_dma_ops,
  387. of_device_get_match_data(&pdev->dev), NULL);
  388. }
  389. static const struct dev_pm_ops renesas_sdhi_sys_dmac_dev_pm_ops = {
  390. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  391. pm_runtime_force_resume)
  392. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  393. tmio_mmc_host_runtime_resume,
  394. NULL)
  395. };
  396. static struct platform_driver renesas_sys_dmac_sdhi_driver = {
  397. .driver = {
  398. .name = "sh_mobile_sdhi",
  399. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  400. .pm = &renesas_sdhi_sys_dmac_dev_pm_ops,
  401. .of_match_table = renesas_sdhi_sys_dmac_of_match,
  402. },
  403. .probe = renesas_sdhi_sys_dmac_probe,
  404. .remove = renesas_sdhi_remove,
  405. };
  406. module_platform_driver(renesas_sys_dmac_sdhi_driver);
  407. MODULE_DESCRIPTION("Renesas SDHI driver");
  408. MODULE_AUTHOR("Magnus Damm");
  409. MODULE_LICENSE("GPL v2");
  410. MODULE_ALIAS("platform:sh_mobile_sdhi");