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>
19 #include <linux/platform_device.h>
20 #include <linux/pagemap.h>
21 #include <linux/scatterlist.h>
22 #include <linux/sys_soc.h>
24 #include "renesas_sdhi.h"
27 #define TMIO_MMC_MIN_DMA_LEN 8
29 static const struct renesas_sdhi_of_data of_default_cfg = {
30 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
33 static const struct renesas_sdhi_of_data of_rz_compatible = {
34 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_32BIT_DATA_PORT |
36 .tmio_ocr_mask = MMC_VDD_32_33,
37 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
38 MMC_CAP_WAIT_WHILE_BUSY,
41 static const struct renesas_sdhi_of_data of_rcar_gen1_compatible = {
42 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL,
43 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
44 MMC_CAP_WAIT_WHILE_BUSY,
45 .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
48 /* Definitions for sampling clocks */
49 static struct renesas_sdhi_scc rcar_gen2_scc_taps[] = {
51 .clk_rate = 156000000,
60 static const struct renesas_sdhi_of_data of_rcar_gen2_compatible = {
61 .tmio_flags = TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
62 TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
63 .capabilities = MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
64 MMC_CAP_CMD23 | MMC_CAP_WAIT_WHILE_BUSY,
65 .capabilities2 = MMC_CAP2_NO_WRITE_PROTECT,
66 .dma_buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES,
67 .dma_rx_offset = 0x2000,
69 .taps = rcar_gen2_scc_taps,
70 .taps_num = ARRAY_SIZE(rcar_gen2_scc_taps),
71 .max_blk_count = UINT_MAX / TMIO_MAX_BLK_SIZE,
74 static const struct of_device_id renesas_sdhi_sys_dmac_of_match[] = {
75 { .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
76 { .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
77 { .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
78 { .compatible = "renesas,sdhi-r7s72100", .data = &of_rz_compatible, },
79 { .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
80 { .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
81 { .compatible = "renesas,sdhi-r8a7743", .data = &of_rcar_gen2_compatible, },
82 { .compatible = "renesas,sdhi-r8a7745", .data = &of_rcar_gen2_compatible, },
83 { .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
84 { .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
85 { .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
86 { .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
87 { .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
88 { .compatible = "renesas,rcar-gen1-sdhi", .data = &of_rcar_gen1_compatible, },
89 { .compatible = "renesas,rcar-gen2-sdhi", .data = &of_rcar_gen2_compatible, },
90 { .compatible = "renesas,sdhi-shmobile" },
93 MODULE_DEVICE_TABLE(of, renesas_sdhi_sys_dmac_of_match);
95 static void renesas_sdhi_sys_dmac_enable_dma(struct tmio_mmc_host *host,
98 struct renesas_sdhi *priv = host_to_priv(host);
100 if (!host->chan_tx || !host->chan_rx)
103 if (priv->dma_priv.enable)
104 priv->dma_priv.enable(host, enable);
107 static void renesas_sdhi_sys_dmac_abort_dma(struct tmio_mmc_host *host)
109 renesas_sdhi_sys_dmac_enable_dma(host, false);
112 dmaengine_terminate_sync(host->chan_rx);
114 dmaengine_terminate_sync(host->chan_tx);
116 renesas_sdhi_sys_dmac_enable_dma(host, true);
119 static void renesas_sdhi_sys_dmac_dataend_dma(struct tmio_mmc_host *host)
121 struct renesas_sdhi *priv = host_to_priv(host);
123 complete(&priv->dma_priv.dma_dataend);
126 static void renesas_sdhi_sys_dmac_dma_callback(void *arg)
128 struct tmio_mmc_host *host = arg;
129 struct renesas_sdhi *priv = host_to_priv(host);
131 spin_lock_irq(&host->lock);
136 if (host->data->flags & MMC_DATA_READ)
137 dma_unmap_sg(host->chan_rx->device->dev,
138 host->sg_ptr, host->sg_len,
141 dma_unmap_sg(host->chan_tx->device->dev,
142 host->sg_ptr, host->sg_len,
145 spin_unlock_irq(&host->lock);
147 wait_for_completion(&priv->dma_priv.dma_dataend);
149 spin_lock_irq(&host->lock);
150 tmio_mmc_do_data_irq(host);
152 spin_unlock_irq(&host->lock);
155 static void renesas_sdhi_sys_dmac_start_dma_rx(struct tmio_mmc_host *host)
157 struct renesas_sdhi *priv = host_to_priv(host);
158 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
159 struct dma_async_tx_descriptor *desc = NULL;
160 struct dma_chan *chan = host->chan_rx;
163 bool aligned = true, multiple = true;
164 unsigned int align = 1; /* 2-byte alignment */
166 for_each_sg(sg, sg_tmp, host->sg_len, i) {
167 if (sg_tmp->offset & align)
169 if (sg_tmp->length & align) {
175 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
176 (align & PAGE_MASK))) || !multiple) {
181 if (sg->length < TMIO_MMC_MIN_DMA_LEN)
184 /* The only sg element can be unaligned, use our bounce buffer then */
186 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
187 host->sg_ptr = &host->bounce_sg;
191 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
193 desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_DEV_TO_MEM,
197 reinit_completion(&priv->dma_priv.dma_dataend);
198 desc->callback = renesas_sdhi_sys_dmac_dma_callback;
199 desc->callback_param = host;
201 cookie = dmaengine_submit(desc);
210 /* DMA failed, fall back to PIO */
211 renesas_sdhi_sys_dmac_enable_dma(host, false);
214 host->chan_rx = NULL;
215 dma_release_channel(chan);
216 /* Free the Tx channel too */
217 chan = host->chan_tx;
219 host->chan_tx = NULL;
220 dma_release_channel(chan);
222 dev_warn(&host->pdev->dev,
223 "DMA failed: %d, falling back to PIO\n", ret);
227 static void renesas_sdhi_sys_dmac_start_dma_tx(struct tmio_mmc_host *host)
229 struct renesas_sdhi *priv = host_to_priv(host);
230 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
231 struct dma_async_tx_descriptor *desc = NULL;
232 struct dma_chan *chan = host->chan_tx;
235 bool aligned = true, multiple = true;
236 unsigned int align = 1; /* 2-byte alignment */
238 for_each_sg(sg, sg_tmp, host->sg_len, i) {
239 if (sg_tmp->offset & align)
241 if (sg_tmp->length & align) {
247 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
248 (align & PAGE_MASK))) || !multiple) {
253 if (sg->length < TMIO_MMC_MIN_DMA_LEN)
256 /* The only sg element can be unaligned, use our bounce buffer then */
258 void *sg_vaddr = kmap_local_page(sg_page(sg));
260 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
261 memcpy(host->bounce_buf, sg_vaddr + sg->offset, host->bounce_sg.length);
262 kunmap_local(sg_vaddr);
263 host->sg_ptr = &host->bounce_sg;
267 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
269 desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_MEM_TO_DEV,
273 reinit_completion(&priv->dma_priv.dma_dataend);
274 desc->callback = renesas_sdhi_sys_dmac_dma_callback;
275 desc->callback_param = host;
277 cookie = dmaengine_submit(desc);
286 /* DMA failed, fall back to PIO */
287 renesas_sdhi_sys_dmac_enable_dma(host, false);
290 host->chan_tx = NULL;
291 dma_release_channel(chan);
292 /* Free the Rx channel too */
293 chan = host->chan_rx;
295 host->chan_rx = NULL;
296 dma_release_channel(chan);
298 dev_warn(&host->pdev->dev,
299 "DMA failed: %d, falling back to PIO\n", ret);
303 static void renesas_sdhi_sys_dmac_start_dma(struct tmio_mmc_host *host,
304 struct mmc_data *data)
306 if (data->flags & MMC_DATA_READ) {
308 renesas_sdhi_sys_dmac_start_dma_rx(host);
311 renesas_sdhi_sys_dmac_start_dma_tx(host);
315 static void renesas_sdhi_sys_dmac_issue_tasklet_fn(unsigned long priv)
317 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
318 struct dma_chan *chan = NULL;
320 spin_lock_irq(&host->lock);
323 if (host->data->flags & MMC_DATA_READ)
324 chan = host->chan_rx;
326 chan = host->chan_tx;
329 spin_unlock_irq(&host->lock);
331 tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);
334 dma_async_issue_pending(chan);
337 static void renesas_sdhi_sys_dmac_request_dma(struct tmio_mmc_host *host,
338 struct tmio_mmc_data *pdata)
340 struct renesas_sdhi *priv = host_to_priv(host);
342 /* We can only either use DMA for both Tx and Rx or not use it at all */
343 if (!host->pdev->dev.of_node &&
344 (!pdata->chan_priv_tx || !pdata->chan_priv_rx))
347 if (!host->chan_tx && !host->chan_rx) {
348 struct resource *res = platform_get_resource(host->pdev,
350 struct dma_slave_config cfg = {};
358 dma_cap_set(DMA_SLAVE, mask);
360 host->chan_tx = dma_request_slave_channel_compat(mask,
361 priv->dma_priv.filter, pdata->chan_priv_tx,
362 &host->pdev->dev, "tx");
363 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
369 cfg.direction = DMA_MEM_TO_DEV;
370 cfg.dst_addr = res->start +
371 (CTL_SD_DATA_PORT << host->bus_shift);
372 cfg.dst_addr_width = priv->dma_priv.dma_buswidth;
373 if (!cfg.dst_addr_width)
374 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
376 ret = dmaengine_slave_config(host->chan_tx, &cfg);
380 host->chan_rx = dma_request_slave_channel_compat(mask,
381 priv->dma_priv.filter, pdata->chan_priv_rx,
382 &host->pdev->dev, "rx");
383 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
389 cfg.direction = DMA_DEV_TO_MEM;
390 cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
391 cfg.src_addr_width = priv->dma_priv.dma_buswidth;
392 if (!cfg.src_addr_width)
393 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
395 ret = dmaengine_slave_config(host->chan_rx, &cfg);
399 host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
400 if (!host->bounce_buf)
403 init_completion(&priv->dma_priv.dma_dataend);
404 tasklet_init(&host->dma_issue,
405 renesas_sdhi_sys_dmac_issue_tasklet_fn,
406 (unsigned long)host);
409 renesas_sdhi_sys_dmac_enable_dma(host, true);
415 dma_release_channel(host->chan_rx);
416 host->chan_rx = NULL;
419 dma_release_channel(host->chan_tx);
420 host->chan_tx = NULL;
423 static void renesas_sdhi_sys_dmac_release_dma(struct tmio_mmc_host *host)
426 struct dma_chan *chan = host->chan_tx;
428 host->chan_tx = NULL;
429 dma_release_channel(chan);
432 struct dma_chan *chan = host->chan_rx;
434 host->chan_rx = NULL;
435 dma_release_channel(chan);
437 if (host->bounce_buf) {
438 free_pages((unsigned long)host->bounce_buf, 0);
439 host->bounce_buf = NULL;
443 static const struct tmio_mmc_dma_ops renesas_sdhi_sys_dmac_dma_ops = {
444 .start = renesas_sdhi_sys_dmac_start_dma,
445 .enable = renesas_sdhi_sys_dmac_enable_dma,
446 .request = renesas_sdhi_sys_dmac_request_dma,
447 .release = renesas_sdhi_sys_dmac_release_dma,
448 .abort = renesas_sdhi_sys_dmac_abort_dma,
449 .dataend = renesas_sdhi_sys_dmac_dataend_dma,
452 static int renesas_sdhi_sys_dmac_probe(struct platform_device *pdev)
454 return renesas_sdhi_probe(pdev, &renesas_sdhi_sys_dmac_dma_ops,
455 of_device_get_match_data(&pdev->dev), NULL);
458 static const struct dev_pm_ops renesas_sdhi_sys_dmac_dev_pm_ops = {
459 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
460 pm_runtime_force_resume)
461 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
462 tmio_mmc_host_runtime_resume,
466 static struct platform_driver renesas_sys_dmac_sdhi_driver = {
468 .name = "sh_mobile_sdhi",
469 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
470 .pm = &renesas_sdhi_sys_dmac_dev_pm_ops,
471 .of_match_table = renesas_sdhi_sys_dmac_of_match,
473 .probe = renesas_sdhi_sys_dmac_probe,
474 .remove_new = renesas_sdhi_remove,
477 module_platform_driver(renesas_sys_dmac_sdhi_driver);
479 MODULE_DESCRIPTION("Renesas SDHI driver");
480 MODULE_AUTHOR("Magnus Damm");
481 MODULE_LICENSE("GPL v2");
482 MODULE_ALIAS("platform:sh_mobile_sdhi");