2 * DMA support use of SYS DMAC with SDHI SD/SDIO controller
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
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.
14 #include <linux/device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/dmaengine.h>
17 #include <linux/mfd/tmio.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/pagemap.h>
23 #include <linux/scatterlist.h>
24 #include <linux/sys_soc.h>
26 #include "renesas_sdhi.h"
29 #define TMIO_MMC_MIN_DMA_LEN 8
31 static const struct renesas_sdhi_of_data of_default_cfg
= {
32 .tmio_flags
= TMIO_MMC_HAS_IDLE_WAIT
,
35 static const struct renesas_sdhi_of_data of_rz_compatible
= {
36 .tmio_flags
= TMIO_MMC_HAS_IDLE_WAIT
| TMIO_MMC_32BIT_DATA_PORT
|
38 .tmio_ocr_mask
= MMC_VDD_32_33
,
39 .capabilities
= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_SDIO_IRQ
,
42 static const struct renesas_sdhi_of_data of_rcar_gen1_compatible
= {
43 .tmio_flags
= TMIO_MMC_HAS_IDLE_WAIT
| TMIO_MMC_CLK_ACTUAL
,
44 .capabilities
= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_SDIO_IRQ
,
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
|
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
= 0xffffffff,
74 /* Definitions for sampling clocks */
75 static struct renesas_sdhi_scc rcar_gen3_scc_taps
[] = {
82 static const struct renesas_sdhi_of_data of_rcar_r8a7795_compatible
= {
83 .tmio_flags
= TMIO_MMC_HAS_IDLE_WAIT
| TMIO_MMC_CLK_ACTUAL
|
84 TMIO_MMC_HAVE_CBSY
| TMIO_MMC_MIN_RCAR2
|
85 TMIO_MMC_HAVE_4TAP_HS400
,
86 .capabilities
= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_SDIO_IRQ
|
88 .capabilities2
= MMC_CAP2_NO_WRITE_PROTECT
,
91 .taps
= rcar_gen3_scc_taps
,
92 .taps_num
= ARRAY_SIZE(rcar_gen3_scc_taps
),
95 static const struct renesas_sdhi_of_data of_rcar_gen3_compatible
= {
96 .tmio_flags
= TMIO_MMC_HAS_IDLE_WAIT
| TMIO_MMC_CLK_ACTUAL
|
97 TMIO_MMC_HAVE_CBSY
| TMIO_MMC_MIN_RCAR2
,
98 .capabilities
= MMC_CAP_SD_HIGHSPEED
| MMC_CAP_SDIO_IRQ
|
100 .capabilities2
= MMC_CAP2_NO_WRITE_PROTECT
,
102 .scc_offset
= 0x1000,
103 .taps
= rcar_gen3_scc_taps
,
104 .taps_num
= ARRAY_SIZE(rcar_gen3_scc_taps
),
107 static const struct of_device_id renesas_sdhi_sys_dmac_of_match
[] = {
108 { .compatible
= "renesas,sdhi-sh73a0", .data
= &of_default_cfg
, },
109 { .compatible
= "renesas,sdhi-r8a73a4", .data
= &of_default_cfg
, },
110 { .compatible
= "renesas,sdhi-r8a7740", .data
= &of_default_cfg
, },
111 { .compatible
= "renesas,sdhi-r7s72100", .data
= &of_rz_compatible
, },
112 { .compatible
= "renesas,sdhi-r8a7778", .data
= &of_rcar_gen1_compatible
, },
113 { .compatible
= "renesas,sdhi-r8a7779", .data
= &of_rcar_gen1_compatible
, },
114 { .compatible
= "renesas,sdhi-r8a7743", .data
= &of_rcar_gen2_compatible
, },
115 { .compatible
= "renesas,sdhi-r8a7745", .data
= &of_rcar_gen2_compatible
, },
116 { .compatible
= "renesas,sdhi-r8a7790", .data
= &of_rcar_gen2_compatible
, },
117 { .compatible
= "renesas,sdhi-r8a7791", .data
= &of_rcar_gen2_compatible
, },
118 { .compatible
= "renesas,sdhi-r8a7792", .data
= &of_rcar_gen2_compatible
, },
119 { .compatible
= "renesas,sdhi-r8a7793", .data
= &of_rcar_gen2_compatible
, },
120 { .compatible
= "renesas,sdhi-r8a7794", .data
= &of_rcar_gen2_compatible
, },
121 { .compatible
= "renesas,sdhi-r8a7795", .data
= &of_rcar_r8a7795_compatible
, },
122 { .compatible
= "renesas,sdhi-r8a7796", .data
= &of_rcar_r8a7795_compatible
, },
123 { .compatible
= "renesas,rcar-gen1-sdhi", .data
= &of_rcar_gen1_compatible
, },
124 { .compatible
= "renesas,rcar-gen2-sdhi", .data
= &of_rcar_gen2_compatible
, },
125 { .compatible
= "renesas,rcar-gen3-sdhi", .data
= &of_rcar_gen3_compatible
, },
126 { .compatible
= "renesas,sdhi-shmobile" },
129 MODULE_DEVICE_TABLE(of
, renesas_sdhi_sys_dmac_of_match
);
131 static void renesas_sdhi_sys_dmac_enable_dma(struct tmio_mmc_host
*host
,
134 struct renesas_sdhi
*priv
= host_to_priv(host
);
136 if (!host
->chan_tx
|| !host
->chan_rx
)
139 if (priv
->dma_priv
.enable
)
140 priv
->dma_priv
.enable(host
, enable
);
143 static void renesas_sdhi_sys_dmac_abort_dma(struct tmio_mmc_host
*host
)
145 renesas_sdhi_sys_dmac_enable_dma(host
, false);
148 dmaengine_terminate_all(host
->chan_rx
);
150 dmaengine_terminate_all(host
->chan_tx
);
152 renesas_sdhi_sys_dmac_enable_dma(host
, true);
155 static void renesas_sdhi_sys_dmac_dataend_dma(struct tmio_mmc_host
*host
)
157 struct renesas_sdhi
*priv
= host_to_priv(host
);
159 complete(&priv
->dma_priv
.dma_dataend
);
162 static void renesas_sdhi_sys_dmac_dma_callback(void *arg
)
164 struct tmio_mmc_host
*host
= arg
;
165 struct renesas_sdhi
*priv
= host_to_priv(host
);
167 spin_lock_irq(&host
->lock
);
172 if (host
->data
->flags
& MMC_DATA_READ
)
173 dma_unmap_sg(host
->chan_rx
->device
->dev
,
174 host
->sg_ptr
, host
->sg_len
,
177 dma_unmap_sg(host
->chan_tx
->device
->dev
,
178 host
->sg_ptr
, host
->sg_len
,
181 spin_unlock_irq(&host
->lock
);
183 wait_for_completion(&priv
->dma_priv
.dma_dataend
);
185 spin_lock_irq(&host
->lock
);
186 tmio_mmc_do_data_irq(host
);
188 spin_unlock_irq(&host
->lock
);
191 static void renesas_sdhi_sys_dmac_start_dma_rx(struct tmio_mmc_host
*host
)
193 struct renesas_sdhi
*priv
= host_to_priv(host
);
194 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
195 struct dma_async_tx_descriptor
*desc
= NULL
;
196 struct dma_chan
*chan
= host
->chan_rx
;
199 bool aligned
= true, multiple
= true;
200 unsigned int align
= (1 << host
->pdata
->alignment_shift
) - 1;
202 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
203 if (sg_tmp
->offset
& align
)
205 if (sg_tmp
->length
& align
) {
211 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_SIZE
||
212 (align
& PAGE_MASK
))) || !multiple
) {
217 if (sg
->length
< TMIO_MMC_MIN_DMA_LEN
) {
218 host
->force_pio
= true;
222 /* The only sg element can be unaligned, use our bounce buffer then */
224 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
225 host
->sg_ptr
= &host
->bounce_sg
;
229 ret
= dma_map_sg(chan
->device
->dev
, sg
, host
->sg_len
, DMA_FROM_DEVICE
);
231 desc
= dmaengine_prep_slave_sg(chan
, sg
, ret
, DMA_DEV_TO_MEM
,
235 reinit_completion(&priv
->dma_priv
.dma_dataend
);
236 desc
->callback
= renesas_sdhi_sys_dmac_dma_callback
;
237 desc
->callback_param
= host
;
239 cookie
= dmaengine_submit(desc
);
247 /* DMA failed, fall back to PIO */
248 renesas_sdhi_sys_dmac_enable_dma(host
, false);
251 host
->chan_rx
= NULL
;
252 dma_release_channel(chan
);
253 /* Free the Tx channel too */
254 chan
= host
->chan_tx
;
256 host
->chan_tx
= NULL
;
257 dma_release_channel(chan
);
259 dev_warn(&host
->pdev
->dev
,
260 "DMA failed: %d, falling back to PIO\n", ret
);
264 static void renesas_sdhi_sys_dmac_start_dma_tx(struct tmio_mmc_host
*host
)
266 struct renesas_sdhi
*priv
= host_to_priv(host
);
267 struct scatterlist
*sg
= host
->sg_ptr
, *sg_tmp
;
268 struct dma_async_tx_descriptor
*desc
= NULL
;
269 struct dma_chan
*chan
= host
->chan_tx
;
272 bool aligned
= true, multiple
= true;
273 unsigned int align
= (1 << host
->pdata
->alignment_shift
) - 1;
275 for_each_sg(sg
, sg_tmp
, host
->sg_len
, i
) {
276 if (sg_tmp
->offset
& align
)
278 if (sg_tmp
->length
& align
) {
284 if ((!aligned
&& (host
->sg_len
> 1 || sg
->length
> PAGE_SIZE
||
285 (align
& PAGE_MASK
))) || !multiple
) {
290 if (sg
->length
< TMIO_MMC_MIN_DMA_LEN
) {
291 host
->force_pio
= true;
295 /* The only sg element can be unaligned, use our bounce buffer then */
298 void *sg_vaddr
= tmio_mmc_kmap_atomic(sg
, &flags
);
300 sg_init_one(&host
->bounce_sg
, host
->bounce_buf
, sg
->length
);
301 memcpy(host
->bounce_buf
, sg_vaddr
, host
->bounce_sg
.length
);
302 tmio_mmc_kunmap_atomic(sg
, &flags
, sg_vaddr
);
303 host
->sg_ptr
= &host
->bounce_sg
;
307 ret
= dma_map_sg(chan
->device
->dev
, sg
, host
->sg_len
, DMA_TO_DEVICE
);
309 desc
= dmaengine_prep_slave_sg(chan
, sg
, ret
, DMA_MEM_TO_DEV
,
313 reinit_completion(&priv
->dma_priv
.dma_dataend
);
314 desc
->callback
= renesas_sdhi_sys_dmac_dma_callback
;
315 desc
->callback_param
= host
;
317 cookie
= dmaengine_submit(desc
);
325 /* DMA failed, fall back to PIO */
326 renesas_sdhi_sys_dmac_enable_dma(host
, false);
329 host
->chan_tx
= NULL
;
330 dma_release_channel(chan
);
331 /* Free the Rx channel too */
332 chan
= host
->chan_rx
;
334 host
->chan_rx
= NULL
;
335 dma_release_channel(chan
);
337 dev_warn(&host
->pdev
->dev
,
338 "DMA failed: %d, falling back to PIO\n", ret
);
342 static void renesas_sdhi_sys_dmac_start_dma(struct tmio_mmc_host
*host
,
343 struct mmc_data
*data
)
345 if (data
->flags
& MMC_DATA_READ
) {
347 renesas_sdhi_sys_dmac_start_dma_rx(host
);
350 renesas_sdhi_sys_dmac_start_dma_tx(host
);
354 static void renesas_sdhi_sys_dmac_issue_tasklet_fn(unsigned long priv
)
356 struct tmio_mmc_host
*host
= (struct tmio_mmc_host
*)priv
;
357 struct dma_chan
*chan
= NULL
;
359 spin_lock_irq(&host
->lock
);
362 if (host
->data
->flags
& MMC_DATA_READ
)
363 chan
= host
->chan_rx
;
365 chan
= host
->chan_tx
;
368 spin_unlock_irq(&host
->lock
);
370 tmio_mmc_enable_mmc_irqs(host
, TMIO_STAT_DATAEND
);
373 dma_async_issue_pending(chan
);
376 static void renesas_sdhi_sys_dmac_request_dma(struct tmio_mmc_host
*host
,
377 struct tmio_mmc_data
*pdata
)
379 struct renesas_sdhi
*priv
= host_to_priv(host
);
381 /* We can only either use DMA for both Tx and Rx or not use it at all */
382 if (!host
->pdev
->dev
.of_node
&&
383 (!pdata
->chan_priv_tx
|| !pdata
->chan_priv_rx
))
386 if (!host
->chan_tx
&& !host
->chan_rx
) {
387 struct resource
*res
= platform_get_resource(host
->pdev
,
389 struct dma_slave_config cfg
= {};
397 dma_cap_set(DMA_SLAVE
, mask
);
399 host
->chan_tx
= dma_request_slave_channel_compat(mask
,
400 priv
->dma_priv
.filter
, pdata
->chan_priv_tx
,
401 &host
->pdev
->dev
, "tx");
402 dev_dbg(&host
->pdev
->dev
, "%s: TX: got channel %p\n", __func__
,
408 cfg
.direction
= DMA_MEM_TO_DEV
;
409 cfg
.dst_addr
= res
->start
+
410 (CTL_SD_DATA_PORT
<< host
->bus_shift
);
411 cfg
.dst_addr_width
= priv
->dma_priv
.dma_buswidth
;
412 if (!cfg
.dst_addr_width
)
413 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
415 ret
= dmaengine_slave_config(host
->chan_tx
, &cfg
);
419 host
->chan_rx
= dma_request_slave_channel_compat(mask
,
420 priv
->dma_priv
.filter
, pdata
->chan_priv_rx
,
421 &host
->pdev
->dev
, "rx");
422 dev_dbg(&host
->pdev
->dev
, "%s: RX: got channel %p\n", __func__
,
428 cfg
.direction
= DMA_DEV_TO_MEM
;
429 cfg
.src_addr
= cfg
.dst_addr
+ host
->pdata
->dma_rx_offset
;
430 cfg
.src_addr_width
= priv
->dma_priv
.dma_buswidth
;
431 if (!cfg
.src_addr_width
)
432 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
434 ret
= dmaengine_slave_config(host
->chan_rx
, &cfg
);
438 host
->bounce_buf
= (u8
*)__get_free_page(GFP_KERNEL
| GFP_DMA
);
439 if (!host
->bounce_buf
)
442 init_completion(&priv
->dma_priv
.dma_dataend
);
443 tasklet_init(&host
->dma_issue
,
444 renesas_sdhi_sys_dmac_issue_tasklet_fn
,
445 (unsigned long)host
);
448 renesas_sdhi_sys_dmac_enable_dma(host
, true);
454 dma_release_channel(host
->chan_rx
);
455 host
->chan_rx
= NULL
;
458 dma_release_channel(host
->chan_tx
);
459 host
->chan_tx
= NULL
;
462 static void renesas_sdhi_sys_dmac_release_dma(struct tmio_mmc_host
*host
)
465 struct dma_chan
*chan
= host
->chan_tx
;
467 host
->chan_tx
= NULL
;
468 dma_release_channel(chan
);
471 struct dma_chan
*chan
= host
->chan_rx
;
473 host
->chan_rx
= NULL
;
474 dma_release_channel(chan
);
476 if (host
->bounce_buf
) {
477 free_pages((unsigned long)host
->bounce_buf
, 0);
478 host
->bounce_buf
= NULL
;
482 static const struct tmio_mmc_dma_ops renesas_sdhi_sys_dmac_dma_ops
= {
483 .start
= renesas_sdhi_sys_dmac_start_dma
,
484 .enable
= renesas_sdhi_sys_dmac_enable_dma
,
485 .request
= renesas_sdhi_sys_dmac_request_dma
,
486 .release
= renesas_sdhi_sys_dmac_release_dma
,
487 .abort
= renesas_sdhi_sys_dmac_abort_dma
,
488 .dataend
= renesas_sdhi_sys_dmac_dataend_dma
,
492 * Whitelist of specific R-Car Gen3 SoC ES versions to use this DMAC
493 * implementation. Currently empty as all supported ES versions use
496 static const struct soc_device_attribute gen3_soc_whitelist
[] = {
500 static int renesas_sdhi_sys_dmac_probe(struct platform_device
*pdev
)
502 if ((of_device_get_match_data(&pdev
->dev
) == &of_rcar_gen3_compatible
||
503 of_device_get_match_data(&pdev
->dev
) == &of_rcar_r8a7795_compatible
) &&
504 !soc_device_match(gen3_soc_whitelist
))
507 return renesas_sdhi_probe(pdev
, &renesas_sdhi_sys_dmac_dma_ops
);
510 static const struct dev_pm_ops renesas_sdhi_sys_dmac_dev_pm_ops
= {
511 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
512 pm_runtime_force_resume
)
513 SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend
,
514 tmio_mmc_host_runtime_resume
,
518 static struct platform_driver renesas_sys_dmac_sdhi_driver
= {
520 .name
= "sh_mobile_sdhi",
521 .pm
= &renesas_sdhi_sys_dmac_dev_pm_ops
,
522 .of_match_table
= renesas_sdhi_sys_dmac_of_match
,
524 .probe
= renesas_sdhi_sys_dmac_probe
,
525 .remove
= renesas_sdhi_remove
,
528 module_platform_driver(renesas_sys_dmac_sdhi_driver
);
530 MODULE_DESCRIPTION("Renesas SDHI driver");
531 MODULE_AUTHOR("Magnus Damm");
532 MODULE_LICENSE("GPL v2");
533 MODULE_ALIAS("platform:sh_mobile_sdhi");