renesas_sdhi_internal_dmac.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMA support for Internal DMAC with SDHI SD/SDIO controller
  4. *
  5. * Copyright (C) 2016-19 Renesas Electronics Corporation
  6. * Copyright (C) 2016-17 Horms Solutions, Simon Horman
  7. * Copyright (C) 2018-19 Sang Engineering, Wolfram Sang
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/io-64-nonatomic-hi-lo.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 DM_CM_DTRAN_MODE 0x820
  26. #define DM_CM_DTRAN_CTRL 0x828
  27. #define DM_CM_RST 0x830
  28. #define DM_CM_INFO1 0x840
  29. #define DM_CM_INFO1_MASK 0x848
  30. #define DM_CM_INFO2 0x850
  31. #define DM_CM_INFO2_MASK 0x858
  32. #define DM_DTRAN_ADDR 0x880
  33. /* DM_CM_DTRAN_MODE */
  34. #define DTRAN_MODE_CH_NUM_CH0 0 /* "downstream" = for write commands */
  35. #define DTRAN_MODE_CH_NUM_CH1 BIT(16) /* "upstream" = for read commands */
  36. #define DTRAN_MODE_BUS_WIDTH (BIT(5) | BIT(4))
  37. #define DTRAN_MODE_ADDR_MODE BIT(0) /* 1 = Increment address, 0 = Fixed */
  38. /* DM_CM_DTRAN_CTRL */
  39. #define DTRAN_CTRL_DM_START BIT(0)
  40. /* DM_CM_RST */
  41. #define RST_DTRANRST1 BIT(9)
  42. #define RST_DTRANRST0 BIT(8)
  43. #define RST_RESERVED_BITS GENMASK_ULL(31, 0)
  44. /* DM_CM_INFO1 and DM_CM_INFO1_MASK */
  45. #define INFO1_MASK_CLEAR GENMASK_ULL(31, 0)
  46. #define INFO1_DTRANEND1 BIT(20)
  47. #define INFO1_DTRANEND1_OLD BIT(17)
  48. #define INFO1_DTRANEND0 BIT(16)
  49. /* DM_CM_INFO2 and DM_CM_INFO2_MASK */
  50. #define INFO2_MASK_CLEAR GENMASK_ULL(31, 0)
  51. #define INFO2_DTRANERR1 BIT(17)
  52. #define INFO2_DTRANERR0 BIT(16)
  53. enum renesas_sdhi_dma_cookie {
  54. COOKIE_UNMAPPED,
  55. COOKIE_PRE_MAPPED,
  56. COOKIE_MAPPED,
  57. };
  58. /*
  59. * Specification of this driver:
  60. * - host->chan_{rx,tx} will be used as a flag of enabling/disabling the dma
  61. * - Since this SDHI DMAC register set has 16 but 32-bit width, we
  62. * need a custom accessor.
  63. */
  64. static unsigned long global_flags;
  65. /*
  66. * Workaround for avoiding to use RX DMAC by multiple channels. On R-Car M3-W
  67. * ES1.0, when multiple SDHI channels use RX DMAC simultaneously, sometimes
  68. * hundreds of data bytes are not stored into the system memory even if the
  69. * DMAC interrupt happened. So, this driver then uses one RX DMAC channel only.
  70. */
  71. #define SDHI_INTERNAL_DMAC_RX_IN_USE 0
  72. /* Definitions for sampling clocks */
  73. static struct renesas_sdhi_scc rcar_gen3_scc_taps[] = {
  74. {
  75. .clk_rate = 0,
  76. .tap = 0x00000300,
  77. .tap_hs400_4tap = 0x00000100,
  78. },
  79. };
  80. static const struct renesas_sdhi_of_data of_data_rza2 = {
  81. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
  82. TMIO_MMC_HAVE_CBSY,
  83. .tmio_ocr_mask = MMC_VDD_32_33,
  84. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  85. MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
  86. .bus_shift = 2,
  87. .scc_offset = 0 - 0x1000,
  88. .taps = rcar_gen3_scc_taps,
  89. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  90. /* DMAC can handle 32bit blk count but only 1 segment */
  91. .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
  92. .max_segs = 1,
  93. };
  94. static const struct renesas_sdhi_of_data of_data_rcar_gen3 = {
  95. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
  96. TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
  97. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  98. MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
  99. .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT | MMC_CAP2_MERGE_CAPABLE,
  100. .bus_shift = 2,
  101. .scc_offset = 0x1000,
  102. .taps = rcar_gen3_scc_taps,
  103. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  104. /* DMAC can handle 32bit blk count but only 1 segment */
  105. .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
  106. .max_segs = 1,
  107. .sdhi_flags = SDHI_FLAG_NEED_CLKH_FALLBACK,
  108. };
  109. static const struct renesas_sdhi_of_data of_data_rcar_gen3_no_sdh_fallback = {
  110. .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
  111. TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
  112. .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
  113. MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
  114. .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT | MMC_CAP2_MERGE_CAPABLE,
  115. .bus_shift = 2,
  116. .scc_offset = 0x1000,
  117. .taps = rcar_gen3_scc_taps,
  118. .taps_num = ARRAY_SIZE(rcar_gen3_scc_taps),
  119. /* DMAC can handle 32bit blk count but only 1 segment */
  120. .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
  121. .max_segs = 1,
  122. };
  123. static const u8 r8a7796_es13_calib_table[2][SDHI_CALIB_TABLE_MAX] = {
  124. { 3, 3, 3, 3, 3, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 15,
  125. 16, 16, 16, 16, 16, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25 },
  126. { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 11,
  127. 12, 17, 18, 18, 18, 18, 18, 18, 18, 19, 20, 21, 22, 23, 25, 25 }
  128. };
  129. static const u8 r8a77965_calib_table[2][SDHI_CALIB_TABLE_MAX] = {
  130. { 1, 2, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 16,
  131. 17, 18, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31 },
  132. { 2, 3, 4, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  133. 17, 17, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 31, 31, 31 }
  134. };
  135. static const u8 r8a77990_calib_table[2][SDHI_CALIB_TABLE_MAX] = {
  136. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  137. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  138. { 0, 0, 0, 1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 8, 9, 10,
  139. 11, 12, 13, 15, 16, 17, 17, 18, 18, 19, 20, 22, 24, 25, 26, 26 }
  140. };
  141. static const struct renesas_sdhi_quirks sdhi_quirks_4tap_nohs400 = {
  142. .hs400_disabled = true,
  143. .hs400_4taps = true,
  144. };
  145. static const struct renesas_sdhi_quirks sdhi_quirks_4tap_nohs400_one_rx = {
  146. .hs400_disabled = true,
  147. .hs400_4taps = true,
  148. .dma_one_rx_only = true,
  149. .old_info1_layout = true,
  150. };
  151. static const struct renesas_sdhi_quirks sdhi_quirks_4tap = {
  152. .hs400_4taps = true,
  153. .hs400_bad_taps = BIT(2) | BIT(3) | BIT(6) | BIT(7),
  154. .manual_tap_correction = true,
  155. };
  156. static const struct renesas_sdhi_quirks sdhi_quirks_nohs400 = {
  157. .hs400_disabled = true,
  158. };
  159. static const struct renesas_sdhi_quirks sdhi_quirks_fixed_addr = {
  160. .fixed_addr_mode = true,
  161. };
  162. static const struct renesas_sdhi_quirks sdhi_quirks_bad_taps1357 = {
  163. .hs400_bad_taps = BIT(1) | BIT(3) | BIT(5) | BIT(7),
  164. .manual_tap_correction = true,
  165. };
  166. static const struct renesas_sdhi_quirks sdhi_quirks_bad_taps2367 = {
  167. .hs400_bad_taps = BIT(2) | BIT(3) | BIT(6) | BIT(7),
  168. .manual_tap_correction = true,
  169. };
  170. static const struct renesas_sdhi_quirks sdhi_quirks_r8a7796_es13 = {
  171. .hs400_4taps = true,
  172. .hs400_bad_taps = BIT(2) | BIT(3) | BIT(6) | BIT(7),
  173. .hs400_calib_table = r8a7796_es13_calib_table,
  174. .manual_tap_correction = true,
  175. };
  176. static const struct renesas_sdhi_quirks sdhi_quirks_r8a77965 = {
  177. .hs400_bad_taps = BIT(2) | BIT(3) | BIT(6) | BIT(7),
  178. .hs400_calib_table = r8a77965_calib_table,
  179. .manual_tap_correction = true,
  180. };
  181. static const struct renesas_sdhi_quirks sdhi_quirks_r8a77990 = {
  182. .hs400_calib_table = r8a77990_calib_table,
  183. .manual_tap_correction = true,
  184. };
  185. static const struct renesas_sdhi_quirks sdhi_quirks_rzg2l = {
  186. .fixed_addr_mode = true,
  187. .hs400_disabled = true,
  188. };
  189. /*
  190. * Note for r8a7796 / r8a774a1: we can't distinguish ES1.1 and 1.2 as of now.
  191. * So, we want to treat them equally and only have a match for ES1.2 to enforce
  192. * this if there ever will be a way to distinguish ES1.2.
  193. */
  194. static const struct soc_device_attribute sdhi_quirks_match[] = {
  195. { .soc_id = "r8a774a1", .revision = "ES1.[012]", .data = &sdhi_quirks_4tap_nohs400 },
  196. { .soc_id = "r8a7795", .revision = "ES2.0", .data = &sdhi_quirks_4tap },
  197. { .soc_id = "r8a7796", .revision = "ES1.0", .data = &sdhi_quirks_4tap_nohs400_one_rx },
  198. { .soc_id = "r8a7796", .revision = "ES1.[12]", .data = &sdhi_quirks_4tap_nohs400 },
  199. { .soc_id = "r8a7796", .revision = "ES1.*", .data = &sdhi_quirks_r8a7796_es13 },
  200. { .soc_id = "r8a77980", .revision = "ES1.*", .data = &sdhi_quirks_nohs400 },
  201. { /* Sentinel. */ }
  202. };
  203. static const struct renesas_sdhi_of_data_with_quirks of_r8a7795_compatible = {
  204. .of_data = &of_data_rcar_gen3,
  205. .quirks = &sdhi_quirks_bad_taps2367,
  206. };
  207. static const struct renesas_sdhi_of_data_with_quirks of_r8a77961_compatible = {
  208. .of_data = &of_data_rcar_gen3,
  209. .quirks = &sdhi_quirks_bad_taps1357,
  210. };
  211. static const struct renesas_sdhi_of_data_with_quirks of_r8a77965_compatible = {
  212. .of_data = &of_data_rcar_gen3,
  213. .quirks = &sdhi_quirks_r8a77965,
  214. };
  215. static const struct renesas_sdhi_of_data_with_quirks of_r8a77970_compatible = {
  216. .of_data = &of_data_rcar_gen3_no_sdh_fallback,
  217. .quirks = &sdhi_quirks_nohs400,
  218. };
  219. static const struct renesas_sdhi_of_data_with_quirks of_r8a77990_compatible = {
  220. .of_data = &of_data_rcar_gen3,
  221. .quirks = &sdhi_quirks_r8a77990,
  222. };
  223. static const struct renesas_sdhi_of_data_with_quirks of_rzg2l_compatible = {
  224. .of_data = &of_data_rcar_gen3,
  225. .quirks = &sdhi_quirks_rzg2l,
  226. };
  227. static const struct renesas_sdhi_of_data_with_quirks of_rcar_gen3_compatible = {
  228. .of_data = &of_data_rcar_gen3,
  229. };
  230. static const struct renesas_sdhi_of_data_with_quirks of_rcar_gen3_nohs400_compatible = {
  231. .of_data = &of_data_rcar_gen3,
  232. .quirks = &sdhi_quirks_nohs400,
  233. };
  234. static const struct renesas_sdhi_of_data_with_quirks of_rza2_compatible = {
  235. .of_data = &of_data_rza2,
  236. .quirks = &sdhi_quirks_fixed_addr,
  237. };
  238. static const struct of_device_id renesas_sdhi_internal_dmac_of_match[] = {
  239. { .compatible = "renesas,sdhi-r7s9210", .data = &of_rza2_compatible, },
  240. { .compatible = "renesas,sdhi-mmc-r8a77470", .data = &of_rcar_gen3_compatible, },
  241. { .compatible = "renesas,sdhi-r8a7795", .data = &of_r8a7795_compatible, },
  242. { .compatible = "renesas,sdhi-r8a77961", .data = &of_r8a77961_compatible, },
  243. { .compatible = "renesas,sdhi-r8a77965", .data = &of_r8a77965_compatible, },
  244. { .compatible = "renesas,sdhi-r8a77970", .data = &of_r8a77970_compatible, },
  245. { .compatible = "renesas,sdhi-r8a77990", .data = &of_r8a77990_compatible, },
  246. { .compatible = "renesas,sdhi-r8a77995", .data = &of_rcar_gen3_nohs400_compatible, },
  247. { .compatible = "renesas,sdhi-r9a09g011", .data = &of_rzg2l_compatible, },
  248. { .compatible = "renesas,sdhi-r9a09g057", .data = &of_rzg2l_compatible, },
  249. { .compatible = "renesas,rzg2l-sdhi", .data = &of_rzg2l_compatible, },
  250. { .compatible = "renesas,rcar-gen3-sdhi", .data = &of_rcar_gen3_compatible, },
  251. { .compatible = "renesas,rcar-gen4-sdhi", .data = &of_rcar_gen3_compatible, },
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(of, renesas_sdhi_internal_dmac_of_match);
  255. static void
  256. renesas_sdhi_internal_dmac_enable_dma(struct tmio_mmc_host *host, bool enable)
  257. {
  258. struct renesas_sdhi *priv = host_to_priv(host);
  259. u32 dma_irqs = INFO1_DTRANEND0 |
  260. (sdhi_has_quirk(priv, old_info1_layout) ?
  261. INFO1_DTRANEND1_OLD : INFO1_DTRANEND1);
  262. if (!host->chan_tx || !host->chan_rx)
  263. return;
  264. writel(enable ? ~dma_irqs : INFO1_MASK_CLEAR, host->ctl + DM_CM_INFO1_MASK);
  265. if (priv->dma_priv.enable)
  266. priv->dma_priv.enable(host, enable);
  267. }
  268. static void
  269. renesas_sdhi_internal_dmac_abort_dma(struct tmio_mmc_host *host)
  270. {
  271. u64 val = RST_DTRANRST1 | RST_DTRANRST0;
  272. renesas_sdhi_internal_dmac_enable_dma(host, false);
  273. writel(RST_RESERVED_BITS & ~val, host->ctl + DM_CM_RST);
  274. writel(RST_RESERVED_BITS | val, host->ctl + DM_CM_RST);
  275. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  276. renesas_sdhi_internal_dmac_enable_dma(host, true);
  277. }
  278. static bool renesas_sdhi_internal_dmac_dma_irq(struct tmio_mmc_host *host)
  279. {
  280. struct renesas_sdhi *priv = host_to_priv(host);
  281. struct renesas_sdhi_dma *dma_priv = &priv->dma_priv;
  282. u32 dma_irqs = INFO1_DTRANEND0 |
  283. (sdhi_has_quirk(priv, old_info1_layout) ?
  284. INFO1_DTRANEND1_OLD : INFO1_DTRANEND1);
  285. u32 status = readl(host->ctl + DM_CM_INFO1);
  286. if (status & dma_irqs) {
  287. writel(status ^ dma_irqs, host->ctl + DM_CM_INFO1);
  288. set_bit(SDHI_DMA_END_FLAG_DMA, &dma_priv->end_flags);
  289. if (test_bit(SDHI_DMA_END_FLAG_ACCESS, &dma_priv->end_flags))
  290. queue_work(system_bh_wq, &dma_priv->dma_complete);
  291. }
  292. return status & dma_irqs;
  293. }
  294. static void
  295. renesas_sdhi_internal_dmac_dataend_dma(struct tmio_mmc_host *host)
  296. {
  297. struct renesas_sdhi *priv = host_to_priv(host);
  298. struct renesas_sdhi_dma *dma_priv = &priv->dma_priv;
  299. set_bit(SDHI_DMA_END_FLAG_ACCESS, &dma_priv->end_flags);
  300. if (test_bit(SDHI_DMA_END_FLAG_DMA, &dma_priv->end_flags) ||
  301. host->data->error)
  302. queue_work(system_bh_wq, &dma_priv->dma_complete);
  303. }
  304. /*
  305. * renesas_sdhi_internal_dmac_map() will be called with two different
  306. * sg pointers in two mmc_data by .pre_req(), but tmio host can have a single
  307. * sg_ptr only. So, renesas_sdhi_internal_dmac_{un}map() should use a sg
  308. * pointer in a mmc_data instead of host->sg_ptr.
  309. */
  310. static void
  311. renesas_sdhi_internal_dmac_unmap(struct tmio_mmc_host *host,
  312. struct mmc_data *data,
  313. enum renesas_sdhi_dma_cookie cookie)
  314. {
  315. bool unmap = cookie == COOKIE_UNMAPPED ? (data->host_cookie != cookie) :
  316. (data->host_cookie == cookie);
  317. if (unmap) {
  318. dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
  319. mmc_get_dma_dir(data));
  320. data->host_cookie = COOKIE_UNMAPPED;
  321. }
  322. }
  323. static bool
  324. renesas_sdhi_internal_dmac_map(struct tmio_mmc_host *host,
  325. struct mmc_data *data,
  326. enum renesas_sdhi_dma_cookie cookie)
  327. {
  328. if (data->host_cookie == COOKIE_PRE_MAPPED)
  329. return true;
  330. if (!dma_map_sg(&host->pdev->dev, data->sg, data->sg_len,
  331. mmc_get_dma_dir(data)))
  332. return false;
  333. data->host_cookie = cookie;
  334. /* This DMAC needs buffers to be 128-byte aligned */
  335. if (!IS_ALIGNED(sg_dma_address(data->sg), 128)) {
  336. renesas_sdhi_internal_dmac_unmap(host, data, cookie);
  337. return false;
  338. }
  339. return true;
  340. }
  341. static void
  342. renesas_sdhi_internal_dmac_start_dma(struct tmio_mmc_host *host,
  343. struct mmc_data *data)
  344. {
  345. struct renesas_sdhi *priv = host_to_priv(host);
  346. struct scatterlist *sg = host->sg_ptr;
  347. u32 dtran_mode = DTRAN_MODE_BUS_WIDTH;
  348. if (!sdhi_has_quirk(priv, fixed_addr_mode))
  349. dtran_mode |= DTRAN_MODE_ADDR_MODE;
  350. if (!renesas_sdhi_internal_dmac_map(host, data, COOKIE_MAPPED))
  351. goto force_pio;
  352. if (data->flags & MMC_DATA_READ) {
  353. dtran_mode |= DTRAN_MODE_CH_NUM_CH1;
  354. if (sdhi_has_quirk(priv, dma_one_rx_only) &&
  355. test_and_set_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags))
  356. goto force_pio_with_unmap;
  357. } else {
  358. dtran_mode |= DTRAN_MODE_CH_NUM_CH0;
  359. }
  360. priv->dma_priv.end_flags = 0;
  361. renesas_sdhi_internal_dmac_enable_dma(host, true);
  362. /* set dma parameters */
  363. writel(dtran_mode, host->ctl + DM_CM_DTRAN_MODE);
  364. writel(sg_dma_address(sg), host->ctl + DM_DTRAN_ADDR);
  365. host->dma_on = true;
  366. return;
  367. force_pio_with_unmap:
  368. renesas_sdhi_internal_dmac_unmap(host, data, COOKIE_UNMAPPED);
  369. force_pio:
  370. renesas_sdhi_internal_dmac_enable_dma(host, false);
  371. }
  372. static void renesas_sdhi_internal_dmac_issue_work_fn(struct work_struct *work)
  373. {
  374. struct tmio_mmc_host *host = from_work(host, work, dma_issue);
  375. struct renesas_sdhi *priv = host_to_priv(host);
  376. tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
  377. if (!host->cmd->error) {
  378. /* start the DMAC */
  379. writel(DTRAN_CTRL_DM_START, host->ctl + DM_CM_DTRAN_CTRL);
  380. } else {
  381. /* on CMD errors, simulate DMA end immediately */
  382. set_bit(SDHI_DMA_END_FLAG_DMA, &priv->dma_priv.end_flags);
  383. if (test_bit(SDHI_DMA_END_FLAG_ACCESS, &priv->dma_priv.end_flags))
  384. queue_work(system_bh_wq, &priv->dma_priv.dma_complete);
  385. }
  386. }
  387. static bool renesas_sdhi_internal_dmac_complete(struct tmio_mmc_host *host)
  388. {
  389. enum dma_data_direction dir;
  390. if (!host->dma_on)
  391. return false;
  392. if (!host->data)
  393. return false;
  394. if (host->data->flags & MMC_DATA_READ)
  395. dir = DMA_FROM_DEVICE;
  396. else
  397. dir = DMA_TO_DEVICE;
  398. renesas_sdhi_internal_dmac_enable_dma(host, false);
  399. renesas_sdhi_internal_dmac_unmap(host, host->data, COOKIE_MAPPED);
  400. if (dir == DMA_FROM_DEVICE)
  401. clear_bit(SDHI_INTERNAL_DMAC_RX_IN_USE, &global_flags);
  402. host->dma_on = false;
  403. return true;
  404. }
  405. static void renesas_sdhi_internal_dmac_complete_work_fn(struct work_struct *work)
  406. {
  407. struct renesas_sdhi_dma *dma_priv = from_work(dma_priv, work, dma_complete);
  408. struct renesas_sdhi *priv = container_of(dma_priv, typeof(*priv), dma_priv);
  409. struct tmio_mmc_host *host = priv->host;
  410. spin_lock_irq(&host->lock);
  411. if (!renesas_sdhi_internal_dmac_complete(host))
  412. goto out;
  413. tmio_mmc_do_data_irq(host);
  414. out:
  415. spin_unlock_irq(&host->lock);
  416. }
  417. static void renesas_sdhi_internal_dmac_end_dma(struct tmio_mmc_host *host)
  418. {
  419. if (host->data)
  420. renesas_sdhi_internal_dmac_complete(host);
  421. }
  422. static void renesas_sdhi_internal_dmac_post_req(struct mmc_host *mmc,
  423. struct mmc_request *mrq,
  424. int err)
  425. {
  426. struct tmio_mmc_host *host = mmc_priv(mmc);
  427. struct mmc_data *data = mrq->data;
  428. if (!data)
  429. return;
  430. renesas_sdhi_internal_dmac_unmap(host, data, COOKIE_UNMAPPED);
  431. }
  432. static void renesas_sdhi_internal_dmac_pre_req(struct mmc_host *mmc,
  433. struct mmc_request *mrq)
  434. {
  435. struct tmio_mmc_host *host = mmc_priv(mmc);
  436. struct mmc_data *data = mrq->data;
  437. if (!data)
  438. return;
  439. data->host_cookie = COOKIE_UNMAPPED;
  440. renesas_sdhi_internal_dmac_map(host, data, COOKIE_PRE_MAPPED);
  441. }
  442. static void
  443. renesas_sdhi_internal_dmac_request_dma(struct tmio_mmc_host *host,
  444. struct tmio_mmc_data *pdata)
  445. {
  446. struct renesas_sdhi *priv = host_to_priv(host);
  447. /* Disable DMAC interrupts initially */
  448. writel(INFO1_MASK_CLEAR, host->ctl + DM_CM_INFO1_MASK);
  449. writel(INFO2_MASK_CLEAR, host->ctl + DM_CM_INFO2_MASK);
  450. writel(0, host->ctl + DM_CM_INFO1);
  451. writel(0, host->ctl + DM_CM_INFO2);
  452. /* Each value is set to non-zero to assume "enabling" each DMA */
  453. host->chan_rx = host->chan_tx = (void *)0xdeadbeaf;
  454. INIT_WORK(&priv->dma_priv.dma_complete,
  455. renesas_sdhi_internal_dmac_complete_work_fn);
  456. INIT_WORK(&host->dma_issue,
  457. renesas_sdhi_internal_dmac_issue_work_fn);
  458. /* Add pre_req and post_req */
  459. host->ops.pre_req = renesas_sdhi_internal_dmac_pre_req;
  460. host->ops.post_req = renesas_sdhi_internal_dmac_post_req;
  461. }
  462. static void
  463. renesas_sdhi_internal_dmac_release_dma(struct tmio_mmc_host *host)
  464. {
  465. /* Each value is set to zero to assume "disabling" each DMA */
  466. host->chan_rx = host->chan_tx = NULL;
  467. }
  468. static const struct tmio_mmc_dma_ops renesas_sdhi_internal_dmac_dma_ops = {
  469. .start = renesas_sdhi_internal_dmac_start_dma,
  470. .enable = renesas_sdhi_internal_dmac_enable_dma,
  471. .request = renesas_sdhi_internal_dmac_request_dma,
  472. .release = renesas_sdhi_internal_dmac_release_dma,
  473. .abort = renesas_sdhi_internal_dmac_abort_dma,
  474. .dataend = renesas_sdhi_internal_dmac_dataend_dma,
  475. .end = renesas_sdhi_internal_dmac_end_dma,
  476. .dma_irq = renesas_sdhi_internal_dmac_dma_irq,
  477. };
  478. static int renesas_sdhi_internal_dmac_probe(struct platform_device *pdev)
  479. {
  480. const struct soc_device_attribute *attr;
  481. const struct renesas_sdhi_of_data_with_quirks *of_data_quirks;
  482. const struct renesas_sdhi_quirks *quirks;
  483. struct device *dev = &pdev->dev;
  484. of_data_quirks = of_device_get_match_data(&pdev->dev);
  485. quirks = of_data_quirks->quirks;
  486. attr = soc_device_match(sdhi_quirks_match);
  487. if (attr)
  488. quirks = attr->data;
  489. /* value is max of SD_SECCNT. Confirmed by HW engineers */
  490. dma_set_max_seg_size(dev, 0xffffffff);
  491. return renesas_sdhi_probe(pdev, &renesas_sdhi_internal_dmac_dma_ops,
  492. of_data_quirks->of_data, quirks);
  493. }
  494. static const struct dev_pm_ops renesas_sdhi_internal_dmac_dev_pm_ops = {
  495. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  496. pm_runtime_force_resume)
  497. SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
  498. tmio_mmc_host_runtime_resume,
  499. NULL)
  500. };
  501. static struct platform_driver renesas_internal_dmac_sdhi_driver = {
  502. .driver = {
  503. .name = "renesas_sdhi_internal_dmac",
  504. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  505. .pm = &renesas_sdhi_internal_dmac_dev_pm_ops,
  506. .of_match_table = renesas_sdhi_internal_dmac_of_match,
  507. },
  508. .probe = renesas_sdhi_internal_dmac_probe,
  509. .remove_new = renesas_sdhi_remove,
  510. };
  511. module_platform_driver(renesas_internal_dmac_sdhi_driver);
  512. MODULE_DESCRIPTION("Renesas SDHI driver for internal DMAC");
  513. MODULE_AUTHOR("Yoshihiro Shimoda");
  514. MODULE_LICENSE("GPL v2");