renesas_sdhi_sys_dmac.c 14.7 KB
Newer Older
1
/*
2
 * DMA support use of SYS DMAC with SDHI SD/SDIO controller
3
 *
4 5 6
 * Copyright (C) 2016-17 Renesas Electronics Corporation
 * Copyright (C) 2016-17 Sang Engineering, Wolfram Sang
 * Copyright (C) 2017 Horms Solutions, Simon Horman
7 8 9 10 11 12 13 14
 * Copyright (C) 2010-2011 Guennadi Liakhovetski
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
15
#include <linux/dma-mapping.h>
16 17 18
#include <linux/dmaengine.h>
#include <linux/mfd/tmio.h>
#include <linux/mmc/host.h>
19 20
#include <linux/mod_devicetable.h>
#include <linux/module.h>
21
#include <linux/of_device.h>
22 23
#include <linux/pagemap.h>
#include <linux/scatterlist.h>
24
#include <linux/sys_soc.h>
25

26
#include "renesas_sdhi.h"
27 28 29 30
#include "tmio_mmc.h"

#define TMIO_MMC_MIN_DMA_LEN 8

31 32 33 34 35
static const struct renesas_sdhi_of_data of_default_cfg = {
	.tmio_flags = TMIO_MMC_HAS_IDLE_WAIT,
};

static const struct renesas_sdhi_of_data of_rz_compatible = {
36 37
	.tmio_flags	= TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_32BIT_DATA_PORT |
			  TMIO_MMC_HAVE_CBSY,
38 39 40 41 42
	.tmio_ocr_mask	= MMC_VDD_32_33,
	.capabilities	= MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ,
};

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,
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
};

/* Definitions for sampling clocks */
static struct renesas_sdhi_scc rcar_gen2_scc_taps[] = {
	{
		.clk_rate = 156000000,
		.tap = 0x00000703,
	},
	{
		.clk_rate = 0,
		.tap = 0x00000300,
	},
};

static const struct renesas_sdhi_of_data of_rcar_gen2_compatible = {
61 62
	.tmio_flags	= TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
			  TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
63 64
	.capabilities	= MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
			  MMC_CAP_CMD23,
65
	.capabilities2	= MMC_CAP2_NO_WRITE_PROTECT,
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	.dma_buswidth	= DMA_SLAVE_BUSWIDTH_4_BYTES,
	.dma_rx_offset	= 0x2000,
	.scc_offset	= 0x0300,
	.taps		= rcar_gen2_scc_taps,
	.taps_num	= ARRAY_SIZE(rcar_gen2_scc_taps),
};

/* Definitions for sampling clocks */
static struct renesas_sdhi_scc rcar_gen3_scc_taps[] = {
	{
		.clk_rate = 0,
		.tap = 0x00000300,
	},
};

81 82 83 84 85 86 87 88 89 90 91 92 93
static const struct renesas_sdhi_of_data of_rcar_r8a7795_compatible = {
	.tmio_flags	= TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
			  TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2 |
			  TMIO_MMC_HAVE_4TAP_HS400,
	.capabilities	= MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
			  MMC_CAP_CMD23,
	.capabilities2	= MMC_CAP2_NO_WRITE_PROTECT,
	.bus_shift	= 2,
	.scc_offset	= 0x1000,
	.taps		= rcar_gen3_scc_taps,
	.taps_num	= ARRAY_SIZE(rcar_gen3_scc_taps),
};

94
static const struct renesas_sdhi_of_data of_rcar_gen3_compatible = {
95 96
	.tmio_flags	= TMIO_MMC_HAS_IDLE_WAIT | TMIO_MMC_CLK_ACTUAL |
			  TMIO_MMC_HAVE_CBSY | TMIO_MMC_MIN_RCAR2,
97 98
	.capabilities	= MMC_CAP_SD_HIGHSPEED | MMC_CAP_SDIO_IRQ |
			  MMC_CAP_CMD23,
99
	.capabilities2	= MMC_CAP2_NO_WRITE_PROTECT,
100 101 102 103 104 105 106 107 108 109 110 111 112
	.bus_shift	= 2,
	.scc_offset	= 0x1000,
	.taps		= rcar_gen3_scc_taps,
	.taps_num	= ARRAY_SIZE(rcar_gen3_scc_taps),
};

static const struct of_device_id renesas_sdhi_sys_dmac_of_match[] = {
	{ .compatible = "renesas,sdhi-sh73a0", .data = &of_default_cfg, },
	{ .compatible = "renesas,sdhi-r8a73a4", .data = &of_default_cfg, },
	{ .compatible = "renesas,sdhi-r8a7740", .data = &of_default_cfg, },
	{ .compatible = "renesas,sdhi-r7s72100", .data = &of_rz_compatible, },
	{ .compatible = "renesas,sdhi-r8a7778", .data = &of_rcar_gen1_compatible, },
	{ .compatible = "renesas,sdhi-r8a7779", .data = &of_rcar_gen1_compatible, },
113 114
	{ .compatible = "renesas,sdhi-r8a7743", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,sdhi-r8a7745", .data = &of_rcar_gen2_compatible, },
115 116 117 118 119
	{ .compatible = "renesas,sdhi-r8a7790", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,sdhi-r8a7791", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,sdhi-r8a7792", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,sdhi-r8a7793", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,sdhi-r8a7794", .data = &of_rcar_gen2_compatible, },
120 121
	{ .compatible = "renesas,sdhi-r8a7795", .data = &of_rcar_r8a7795_compatible, },
	{ .compatible = "renesas,sdhi-r8a7796", .data = &of_rcar_r8a7795_compatible, },
122 123 124 125
	{ .compatible = "renesas,rcar-gen1-sdhi", .data = &of_rcar_gen1_compatible, },
	{ .compatible = "renesas,rcar-gen2-sdhi", .data = &of_rcar_gen2_compatible, },
	{ .compatible = "renesas,rcar-gen3-sdhi", .data = &of_rcar_gen3_compatible, },
	{ .compatible = "renesas,sdhi-shmobile" },
126 127 128 129
	{},
};
MODULE_DEVICE_TABLE(of, renesas_sdhi_sys_dmac_of_match);

130 131
static void renesas_sdhi_sys_dmac_enable_dma(struct tmio_mmc_host *host,
					     bool enable)
132
{
133 134
	struct renesas_sdhi *priv = host_to_priv(host);

135 136 137
	if (!host->chan_tx || !host->chan_rx)
		return;

138 139
	if (priv->dma_priv.enable)
		priv->dma_priv.enable(host, enable);
140 141
}

142
static void renesas_sdhi_sys_dmac_abort_dma(struct tmio_mmc_host *host)
143
{
144
	renesas_sdhi_sys_dmac_enable_dma(host, false);
145 146 147 148 149 150

	if (host->chan_rx)
		dmaengine_terminate_all(host->chan_rx);
	if (host->chan_tx)
		dmaengine_terminate_all(host->chan_tx);

151
	renesas_sdhi_sys_dmac_enable_dma(host, true);
152 153
}

154 155
static void renesas_sdhi_sys_dmac_dataend_dma(struct tmio_mmc_host *host)
{
156 157 158
	struct renesas_sdhi *priv = host_to_priv(host);

	complete(&priv->dma_priv.dma_dataend);
159 160
}

161
static void renesas_sdhi_sys_dmac_dma_callback(void *arg)
162 163
{
	struct tmio_mmc_host *host = arg;
164
	struct renesas_sdhi *priv = host_to_priv(host);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	spin_lock_irq(&host->lock);

	if (!host->data)
		goto out;

	if (host->data->flags & MMC_DATA_READ)
		dma_unmap_sg(host->chan_rx->device->dev,
			     host->sg_ptr, host->sg_len,
			     DMA_FROM_DEVICE);
	else
		dma_unmap_sg(host->chan_tx->device->dev,
			     host->sg_ptr, host->sg_len,
			     DMA_TO_DEVICE);

180 181
	spin_unlock_irq(&host->lock);

182
	wait_for_completion(&priv->dma_priv.dma_dataend);
183 184

	spin_lock_irq(&host->lock);
185 186 187 188 189
	tmio_mmc_do_data_irq(host);
out:
	spin_unlock_irq(&host->lock);
}

190
static void renesas_sdhi_sys_dmac_start_dma_rx(struct tmio_mmc_host *host)
191
{
192
	struct renesas_sdhi *priv = host_to_priv(host);
193 194 195 196 197 198
	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
	struct dma_async_tx_descriptor *desc = NULL;
	struct dma_chan *chan = host->chan_rx;
	dma_cookie_t cookie;
	int ret, i;
	bool aligned = true, multiple = true;
199
	unsigned int align = (1 << host->pdata->alignment_shift) - 1;
200 201 202 203 204 205 206 207 208 209

	for_each_sg(sg, sg_tmp, host->sg_len, i) {
		if (sg_tmp->offset & align)
			aligned = false;
		if (sg_tmp->length & align) {
			multiple = false;
			break;
		}
	}

210
	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
			  (align & PAGE_MASK))) || !multiple) {
		ret = -EINVAL;
		goto pio;
	}

	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
		host->force_pio = true;
		return;
	}

	/* The only sg element can be unaligned, use our bounce buffer then */
	if (!aligned) {
		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
		host->sg_ptr = &host->bounce_sg;
		sg = host->sg_ptr;
	}

	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
	if (ret > 0)
230 231
		desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_DEV_TO_MEM,
					       DMA_CTRL_ACK);
232 233

	if (desc) {
234
		reinit_completion(&priv->dma_priv.dma_dataend);
235
		desc->callback = renesas_sdhi_sys_dmac_dma_callback;
236 237
		desc->callback_param = host;

238 239 240 241 242 243 244 245 246
		cookie = dmaengine_submit(desc);
		if (cookie < 0) {
			desc = NULL;
			ret = cookie;
		}
	}
pio:
	if (!desc) {
		/* DMA failed, fall back to PIO */
247
		renesas_sdhi_sys_dmac_enable_dma(host, false);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		if (ret >= 0)
			ret = -EIO;
		host->chan_rx = NULL;
		dma_release_channel(chan);
		/* Free the Tx channel too */
		chan = host->chan_tx;
		if (chan) {
			host->chan_tx = NULL;
			dma_release_channel(chan);
		}
		dev_warn(&host->pdev->dev,
			 "DMA failed: %d, falling back to PIO\n", ret);
	}
}

263
static void renesas_sdhi_sys_dmac_start_dma_tx(struct tmio_mmc_host *host)
264
{
265
	struct renesas_sdhi *priv = host_to_priv(host);
266 267 268 269 270 271
	struct scatterlist *sg = host->sg_ptr, *sg_tmp;
	struct dma_async_tx_descriptor *desc = NULL;
	struct dma_chan *chan = host->chan_tx;
	dma_cookie_t cookie;
	int ret, i;
	bool aligned = true, multiple = true;
272
	unsigned int align = (1 << host->pdata->alignment_shift) - 1;
273 274 275 276 277 278 279 280 281 282

	for_each_sg(sg, sg_tmp, host->sg_len, i) {
		if (sg_tmp->offset & align)
			aligned = false;
		if (sg_tmp->length & align) {
			multiple = false;
			break;
		}
	}

283
	if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_SIZE ||
284 285 286 287 288 289 290 291 292 293 294 295 296 297
			  (align & PAGE_MASK))) || !multiple) {
		ret = -EINVAL;
		goto pio;
	}

	if (sg->length < TMIO_MMC_MIN_DMA_LEN) {
		host->force_pio = true;
		return;
	}

	/* The only sg element can be unaligned, use our bounce buffer then */
	if (!aligned) {
		unsigned long flags;
		void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
298

299 300 301 302 303 304 305 306 307
		sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
		memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
		tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
		host->sg_ptr = &host->bounce_sg;
		sg = host->sg_ptr;
	}

	ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
	if (ret > 0)
308 309
		desc = dmaengine_prep_slave_sg(chan, sg, ret, DMA_MEM_TO_DEV,
					       DMA_CTRL_ACK);
310 311

	if (desc) {
312
		reinit_completion(&priv->dma_priv.dma_dataend);
313
		desc->callback = renesas_sdhi_sys_dmac_dma_callback;
314 315
		desc->callback_param = host;

316 317 318 319 320 321 322 323 324
		cookie = dmaengine_submit(desc);
		if (cookie < 0) {
			desc = NULL;
			ret = cookie;
		}
	}
pio:
	if (!desc) {
		/* DMA failed, fall back to PIO */
325
		renesas_sdhi_sys_dmac_enable_dma(host, false);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		if (ret >= 0)
			ret = -EIO;
		host->chan_tx = NULL;
		dma_release_channel(chan);
		/* Free the Rx channel too */
		chan = host->chan_rx;
		if (chan) {
			host->chan_rx = NULL;
			dma_release_channel(chan);
		}
		dev_warn(&host->pdev->dev,
			 "DMA failed: %d, falling back to PIO\n", ret);
	}
}

341
static void renesas_sdhi_sys_dmac_start_dma(struct tmio_mmc_host *host,
342
					    struct mmc_data *data)
343 344 345
{
	if (data->flags & MMC_DATA_READ) {
		if (host->chan_rx)
346
			renesas_sdhi_sys_dmac_start_dma_rx(host);
347 348
	} else {
		if (host->chan_tx)
349
			renesas_sdhi_sys_dmac_start_dma_tx(host);
350 351 352
	}
}

353
static void renesas_sdhi_sys_dmac_issue_tasklet_fn(unsigned long priv)
354 355 356 357 358 359
{
	struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
	struct dma_chan *chan = NULL;

	spin_lock_irq(&host->lock);

360
	if (host->data) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374
		if (host->data->flags & MMC_DATA_READ)
			chan = host->chan_rx;
		else
			chan = host->chan_tx;
	}

	spin_unlock_irq(&host->lock);

	tmio_mmc_enable_mmc_irqs(host, TMIO_STAT_DATAEND);

	if (chan)
		dma_async_issue_pending(chan);
}

375 376
static void renesas_sdhi_sys_dmac_request_dma(struct tmio_mmc_host *host,
					      struct tmio_mmc_data *pdata)
377
{
378 379
	struct renesas_sdhi *priv = host_to_priv(host);

380
	/* We can only either use DMA for both Tx and Rx or not use it at all */
381 382
	if (!host->pdev->dev.of_node &&
	    (!pdata->chan_priv_tx || !pdata->chan_priv_rx))
383 384 385
		return;

	if (!host->chan_tx && !host->chan_rx) {
386 387 388
		struct resource *res = platform_get_resource(host->pdev,
							     IORESOURCE_MEM, 0);
		struct dma_slave_config cfg = {};
389
		dma_cap_mask_t mask;
390 391 392 393
		int ret;

		if (!res)
			return;
394 395 396 397

		dma_cap_zero(mask);
		dma_cap_set(DMA_SLAVE, mask);

398
		host->chan_tx = dma_request_slave_channel_compat(mask,
399
					priv->dma_priv.filter, pdata->chan_priv_tx,
400
					&host->pdev->dev, "tx");
401 402 403 404 405 406
		dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
			host->chan_tx);

		if (!host->chan_tx)
			return;

407
		cfg.direction = DMA_MEM_TO_DEV;
408 409
		cfg.dst_addr = res->start +
			(CTL_SD_DATA_PORT << host->bus_shift);
410
		cfg.dst_addr_width = priv->dma_priv.dma_buswidth;
411 412
		if (!cfg.dst_addr_width)
			cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
413 414 415 416 417
		cfg.src_addr = 0;
		ret = dmaengine_slave_config(host->chan_tx, &cfg);
		if (ret < 0)
			goto ecfgtx;

418
		host->chan_rx = dma_request_slave_channel_compat(mask,
419
					priv->dma_priv.filter, pdata->chan_priv_rx,
420
					&host->pdev->dev, "rx");
421 422 423 424 425 426
		dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
			host->chan_rx);

		if (!host->chan_rx)
			goto ereqrx;

427
		cfg.direction = DMA_DEV_TO_MEM;
428
		cfg.src_addr = cfg.dst_addr + host->pdata->dma_rx_offset;
429
		cfg.src_addr_width = priv->dma_priv.dma_buswidth;
430 431
		if (!cfg.src_addr_width)
			cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
432 433 434 435 436
		cfg.dst_addr = 0;
		ret = dmaengine_slave_config(host->chan_rx, &cfg);
		if (ret < 0)
			goto ecfgrx;

437 438 439 440
		host->bounce_buf = (u8 *)__get_free_page(GFP_KERNEL | GFP_DMA);
		if (!host->bounce_buf)
			goto ebouncebuf;

441
		init_completion(&priv->dma_priv.dma_dataend);
442 443 444
		tasklet_init(&host->dma_issue,
			     renesas_sdhi_sys_dmac_issue_tasklet_fn,
			     (unsigned long)host);
445
	}
446

447
	renesas_sdhi_sys_dmac_enable_dma(host, true);
448 449

	return;
450 451

ebouncebuf:
452
ecfgrx:
453 454
	dma_release_channel(host->chan_rx);
	host->chan_rx = NULL;
455
ereqrx:
456
ecfgtx:
457 458
	dma_release_channel(host->chan_tx);
	host->chan_tx = NULL;
459 460
}

461
static void renesas_sdhi_sys_dmac_release_dma(struct tmio_mmc_host *host)
462 463 464
{
	if (host->chan_tx) {
		struct dma_chan *chan = host->chan_tx;
465

466 467 468 469 470
		host->chan_tx = NULL;
		dma_release_channel(chan);
	}
	if (host->chan_rx) {
		struct dma_chan *chan = host->chan_rx;
471

472 473 474 475 476 477 478 479
		host->chan_rx = NULL;
		dma_release_channel(chan);
	}
	if (host->bounce_buf) {
		free_pages((unsigned long)host->bounce_buf, 0);
		host->bounce_buf = NULL;
	}
}
480

481 482 483 484 485 486
static const struct tmio_mmc_dma_ops renesas_sdhi_sys_dmac_dma_ops = {
	.start = renesas_sdhi_sys_dmac_start_dma,
	.enable = renesas_sdhi_sys_dmac_enable_dma,
	.request = renesas_sdhi_sys_dmac_request_dma,
	.release = renesas_sdhi_sys_dmac_release_dma,
	.abort = renesas_sdhi_sys_dmac_abort_dma,
487
	.dataend = renesas_sdhi_sys_dmac_dataend_dma,
488 489
};

490 491 492 493 494 495 496 497 498
/*
 * Whitelist of specific R-Car Gen3 SoC ES versions to use this DMAC
 * implementation. Currently empty as all supported ES versions use
 * the internal DMAC.
 */
static const struct soc_device_attribute gen3_soc_whitelist[] = {
        { /* sentinel */ }
};

499
static int renesas_sdhi_sys_dmac_probe(struct platform_device *pdev)
500
{
501 502 503 504
	if (of_device_get_match_data(&pdev->dev) == &of_rcar_gen3_compatible &&
	    !soc_device_match(gen3_soc_whitelist))
		return -ENODEV;

505
	return renesas_sdhi_probe(pdev, &renesas_sdhi_sys_dmac_dma_ops);
506
}
507 508 509

static const struct dev_pm_ops renesas_sdhi_sys_dmac_dev_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
510
				pm_runtime_force_resume)
511
	SET_RUNTIME_PM_OPS(tmio_mmc_host_runtime_suspend,
512 513
			   tmio_mmc_host_runtime_resume,
			   NULL)
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
};

static struct platform_driver renesas_sys_dmac_sdhi_driver = {
	.driver		= {
		.name	= "sh_mobile_sdhi",
		.pm	= &renesas_sdhi_sys_dmac_dev_pm_ops,
		.of_match_table = renesas_sdhi_sys_dmac_of_match,
	},
	.probe		= renesas_sdhi_sys_dmac_probe,
	.remove		= renesas_sdhi_remove,
};

module_platform_driver(renesas_sys_dmac_sdhi_driver);

MODULE_DESCRIPTION("Renesas SDHI driver");
MODULE_AUTHOR("Magnus Damm");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:sh_mobile_sdhi");