1 // SPDX-License-Identifier: GPL-2.0
3 * DMA support use of SYS DMAC with SDHI SD/SDIO controller
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
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmaengine.h>
14 #include <linux/mfd/tmio.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/pagemap.h>
20 #include <linux/scatterlist.h>
21 #include <linux/sys_soc.h>
23 #include "renesas_sdhi.h"
26 #define TMIO_MMC_MIN_DMA_LEN 8
28 static const struct renesas_sdhi_of_data of_default_cfg = {
29 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
32 static const struct renesas_sdhi_of_data of_rz_compatible = {
33 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_32BIT_DATA_PORT |
35 .tmio_ocr_mask = MMC_VDD_32_33,
36 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
37 MMC_CAP_WAIT_WHILE_BUSY,
40 static const struct renesas_sdhi_of_data of_rcar_gen1_compatible = {
41 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL,
42 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
43 MMC_CAP_WAIT_WHILE_BUSY,
44 .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
47 /* Definitions for sampling clocks */
48 static struct renesas_sdhi_scc rcar_gen2_scc_taps[] = {
50 .clk_rate = 156000000,
59 static const struct renesas_sdhi_of_data of_rcar_gen2_compatible = {
60 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
61 TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
62 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
63 MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
64 .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
65 .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
66 .dma_rx_offset = 0x2000,
68 .taps = rcar_gen2_scc_taps,
69 .taps_num = ARRAY_SIZE(rcar_gen2_scc_taps),
70 .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
73 static const struct of_device_id renesas_sdhi_sys_dmac_of_match[] = {
74 { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
75 { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
76 { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
77 { .compatible = "renesas,sdhi-r7s72100", .data = &of_rz_compatible, },
78 { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
79 { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
80 { .compatible = "renesas,sdhi-r8a7743", .data = &of_rcar_gen2_compatible, },
81 { .compatible = "renesas,sdhi-r8a7745", .data = &of_rcar_gen2_compatible, },
82 { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
83 { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
84 { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
85 { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
86 { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
87 { .compatible = "renesas,rcar-gen1-sdhi", .data = &of_rcar_gen1_compatible, },
88 { .compatible = "renesas,rcar-gen2-sdhi", .data = &of_rcar_gen2_compatible, },
89 { .compatible = "renesas,sdhi-shmobile" },
92 MODULE_DEVICE_TABLE(of, renesas_sdhi_sys_dmac_of_match);
94 static void renesas_sdhi_sys_dmac_enable_dma(struct tmio_mmc_host *host,
97 struct renesas_sdhi *priv = host_to_priv(host);
99 if (!host->chan_tx || !host->chan_rx)
102 if (priv->dma_priv.enable)
103 priv->dma_priv.enable(host, enable);
106 static void renesas_sdhi_sys_dmac_abort_dma(struct tmio_mmc_host *host)
108 renesas_sdhi_sys_dmac_enable_dma(host, false);
111 dmaengine_terminate_sync(host->chan_rx);
113 dmaengine_terminate_sync(host->chan_tx);
115 renesas_sdhi_sys_dmac_enable_dma(host, true);
118 static void renesas_sdhi_sys_dmac_dataend_dma(struct tmio_mmc_host *host)
120 struct renesas_sdhi *priv = host_to_priv(host);
122 complete(&priv->dma_priv.dma_dataend);
125 static void renesas_sdhi_sys_dmac_dma_callback(void *arg)
127 struct tmio_mmc_host *host = arg;
128 struct renesas_sdhi *priv = host_to_priv(host);
130 spin_lock_irq(&host->lock);
135 if (host->data->flags & MMC_DATA_READ)
136 dma_unmap_sg(host->chan_rx->device->dev,
137 host->sg_ptr, host->sg_len,
140 dma_unmap_sg(host->chan_tx->device->dev,
141 host->sg_ptr, host->sg_len,
144 spin_unlock_irq(&host->lock);
146 wait_for_completion(&priv->dma_priv.dma_dataend);
148 spin_lock_irq(&host->lock);
149 tmio_mmc_do_data_irq(host);
151 spin_unlock_irq(&host->lock);
154 static void renesas_sdhi_sys_dmac_start_dma_rx(struct tmio_mmc_host *host)
156 struct renesas_sdhi *priv = host_to_priv(host);
157 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
158 struct dma_async_tx_descriptor *desc = NULL;
159 struct dma_chan *chan = host->chan_rx;
162 bool aligned = true, multiple = true;
163 unsigned int align = 1; /* 2-byte alignment */
165 for_each_sg(sg, sg_tmp, host->sg_len, i) {
166 if (sg_tmp->offset & align)
168 if (sg_tmp->length & align) {
174 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
175 (align & PAGE_MASK))) || !multiple) {
180 if (sg->length < TMIO_MMC_MIN_DMA_LEN)
183 /* The only sg element can be unaligned, use our bounce buffer then */
185 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
186 host->sg_ptr = &host->bounce_sg;
190 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
192 desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_DEV_TO_MEM,
196 reinit_completion(&priv->dma_priv.dma_dataend);
197 desc->callback = renesas_sdhi_sys_dmac_dma_callback;
198 desc->callback_param = host;
200 cookie = dmaengine_submit(desc);
209 /* DMA failed, fall back to PIO */
210 renesas_sdhi_sys_dmac_enable_dma(host, false);
213 host->chan_rx = NULL;
214 dma_release_channel(chan);
215 /* Free the Tx channel too */
216 chan = host->chan_tx;
218 host->chan_tx = NULL;
219 dma_release_channel(chan);
221 dev_warn(&host->pdev->dev,
222 "DMA failed: %d, falling back to PIO\n", ret);
226 static void renesas_sdhi_sys_dmac_start_dma_tx(struct tmio_mmc_host *host)
228 struct renesas_sdhi *priv = host_to_priv(host);
229 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
230 struct dma_async_tx_descriptor *desc = NULL;
231 struct dma_chan *chan = host->chan_tx;
234 bool aligned = true, multiple = true;
235 unsigned int align = 1; /* 2-byte alignment */
237 for_each_sg(sg, sg_tmp, host->sg_len, i) {
238 if (sg_tmp->offset & align)
240 if (sg_tmp->length & align) {
246 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
247 (align & PAGE_MASK))) || !multiple) {
252 if (sg->length < TMIO_MMC_MIN_DMA_LEN)
255 /* The only sg element can be unaligned, use our bounce buffer then */
257 void *sg_vaddr = kmap_local_page(sg_page(sg));
259 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
260 memcpy(host->bounce_buf, sg_vaddr + sg->offset, host->bounce_sg.length);
261 kunmap_local(sg_vaddr);
262 host->sg_ptr = &host->bounce_sg;
266 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
268 desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_MEM_TO_DEV,
272 reinit_completion(&priv->dma_priv.dma_dataend);
273 desc->callback = renesas_sdhi_sys_dmac_dma_callback;
274 desc->callback_param = host;
276 cookie = dmaengine_submit(desc);
285 /* DMA failed, fall back to PIO */
286 renesas_sdhi_sys_dmac_enable_dma(host, false);
289 host->chan_tx = NULL;
290 dma_release_channel(chan);
291 /* Free the Rx channel too */
292 chan = host->chan_rx;
294 host->chan_rx = NULL;
295 dma_release_channel(chan);
297 dev_warn(&host->pdev->dev,
298 "DMA failed: %d, falling back to PIO\n", ret);
302 static void renesas_sdhi_sys_dmac_start_dma(struct tmio_mmc_host *host,
303 struct mmc_data *data)
305 if (data->flags & MMC_DATA_READ) {
307 renesas_sdhi_sys_dmac_start_dma_rx(host);
310 renesas_sdhi_sys_dmac_start_dma_tx(host);
314 static void renesas_sdhi_sys_dmac_issue_tasklet_fn(unsigned long priv)
316 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
317 struct dma_chan *chan = NULL;
319 spin_lock_irq(&host->lock);
322 if (host->data->flags & MMC_DATA_READ)
323 chan = host->chan_rx;
325 chan = host->chan_tx;
328 spin_unlock_irq(&host->lock);
330 tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
333 dma_async_issue_pending(chan);
336 static void renesas_sdhi_sys_dmac_request_dma(struct tmio_mmc_host *host,
337 struct tmio_mmc_data *pdata)
339 struct renesas_sdhi *priv = host_to_priv(host);
341 /* We can only either use DMA for both Tx and Rx or not use it at all */
342 if (!host->pdev->dev.of_node &&
343 (!pdata->chan_priv_tx || !pdata->chan_priv_rx))
346 if (!host->chan_tx && !host->chan_rx) {
347 struct resource *res = platform_get_resource(host->pdev,
349 struct dma_slave_config cfg = {};
357 dma_cap_set(DMA_SLAVE, mask);
359 host->chan_tx = dma_request_slave_channel_compat(mask,
360 priv->dma_priv.filter, pdata->chan_priv_tx,
361 &host->pdev->dev, "tx");
362 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
368 cfg.direction = DMA_MEM_TO_DEV;
369 cfg.dst_addr = res->start +
370 (CTL_SD_DATA_PORT << host->bus_shift);
371 cfg.dst_addr_width = priv->dma_priv.dma_buswidth;
372 if (!cfg.dst_addr_width)
373 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
375 ret = dmaengine_slave_config(host->chan_tx, &cfg);
379 host->chan_rx = dma_request_slave_channel_compat(mask,
380 priv->dma_priv.filter, pdata->chan_priv_rx,
381 &host->pdev->dev, "rx");
382 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
388 cfg.direction = DMA_DEV_TO_MEM;
389 cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
390 cfg.src_addr_width = priv->dma_priv.dma_buswidth;
391 if (!cfg.src_addr_width)
392 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
394 ret = dmaengine_slave_config(host->chan_rx, &cfg);
398 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
399 if (!host->bounce_buf)
402 init_completion(&priv->dma_priv.dma_dataend);
403 tasklet_init(&host->dma_issue,
404 renesas_sdhi_sys_dmac_issue_tasklet_fn,
405 (unsigned long)host);
408 renesas_sdhi_sys_dmac_enable_dma(host, true);
414 dma_release_channel(host->chan_rx);
415 host->chan_rx = NULL;
418 dma_release_channel(host->chan_tx);
419 host->chan_tx = NULL;
422 static void renesas_sdhi_sys_dmac_release_dma(struct tmio_mmc_host *host)
425 struct dma_chan *chan = host->chan_tx;
427 host->chan_tx = NULL;
428 dma_release_channel(chan);
431 struct dma_chan *chan = host->chan_rx;
433 host->chan_rx = NULL;
434 dma_release_channel(chan);
436 if (host->bounce_buf) {
437 free_pages((unsigned long)host->bounce_buf, 0);
438 host->bounce_buf = NULL;
442 static const struct tmio_mmc_dma_ops renesas_sdhi_sys_dmac_dma_ops = {
443 .start = renesas_sdhi_sys_dmac_start_dma,
444 .enable = renesas_sdhi_sys_dmac_enable_dma,
445 .request = renesas_sdhi_sys_dmac_request_dma,
446 .release = renesas_sdhi_sys_dmac_release_dma,
447 .abort = renesas_sdhi_sys_dmac_abort_dma,
448 .dataend = renesas_sdhi_sys_dmac_dataend_dma,
451 static int renesas_sdhi_sys_dmac_probe(struct platform_device *pdev)
453 return renesas_sdhi_probe(pdev, &renesas_sdhi_sys_dmac_dma_ops,
454 of_device_get_match_data(&pdev->dev), NULL);
457 static const struct dev_pm_ops renesas_sdhi_sys_dmac_dev_pm_ops = {
458 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
459 pm_runtime_force_resume)
460 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
461 tmio_mmc_host_runtime_resume,
465 static struct platform_driver renesas_sys_dmac_sdhi_driver = {
467 .name = "sh_mobile_sdhi",
468 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
469 .pm = &renesas_sdhi_sys_dmac_dev_pm_ops,
470 .of_match_table = renesas_sdhi_sys_dmac_of_match,
472 .probe = renesas_sdhi_sys_dmac_probe,
473 .remove = renesas_sdhi_remove,
476 module_platform_driver(renesas_sys_dmac_sdhi_driver);
478 MODULE_DESCRIPTION("Renesas SDHI driver");
479 MODULE_AUTHOR("Magnus Damm");
480 MODULE_LICENSE("GPL v2");
481 MODULE_ALIAS("platform:sh_mobile_sdhi");