renesas_sdhi_sys_dmac.c 15 KB

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