mxcmmc.c 25.4 KB
Newer Older
1 2 3 4 5 6
/*
 *  linux/drivers/mmc/host/mxcmmc.c - Freescale i.MX MMCI driver
 *
 *  This is a driver for the SDHC controller found in Freescale MX2/MX3
 *  SoCs. It is basically the same hardware as found on MX1 (imxmmc.c).
 *  Unlike the hardware found on MX1, this hardware just works and does
D
Daniel Mack 已提交
7
 *  not need all the quirks found in imxmmc.c, hence the separate driver.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 *  Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
 *  Copyright (C) 2006 Pavel Pisa, PiKRON <ppisa@pikron.com>
 *
 *  derived from pxamci.c by Russell King
 *
 * 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/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/blkdev.h>
#include <linux/dma-mapping.h>
#include <linux/mmc/host.h>
#include <linux/mmc/card.h>
#include <linux/delay.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/gpio.h>
34
#include <linux/regulator/consumer.h>
S
Sascha Hauer 已提交
35
#include <linux/dmaengine.h>
36
#include <linux/types.h>
37 38 39 40 41 42

#include <asm/dma.h>
#include <asm/irq.h>
#include <asm/sizes.h>
#include <mach/mmc.h>

S
Sascha Hauer 已提交
43
#include <mach/dma.h>
44
#include <mach/hardware.h>
45

46
#define DRIVER_NAME "mxc-mmc"
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#define MMC_REG_STR_STP_CLK		0x00
#define MMC_REG_STATUS			0x04
#define MMC_REG_CLK_RATE		0x08
#define MMC_REG_CMD_DAT_CONT		0x0C
#define MMC_REG_RES_TO			0x10
#define MMC_REG_READ_TO			0x14
#define MMC_REG_BLK_LEN			0x18
#define MMC_REG_NOB			0x1C
#define MMC_REG_REV_NO			0x20
#define MMC_REG_INT_CNTR		0x24
#define MMC_REG_CMD			0x28
#define MMC_REG_ARG			0x2C
#define MMC_REG_RES_FIFO		0x34
#define MMC_REG_BUFFER_ACCESS		0x38

#define STR_STP_CLK_RESET               (1 << 3)
#define STR_STP_CLK_START_CLK           (1 << 1)
#define STR_STP_CLK_STOP_CLK            (1 << 0)

#define STATUS_CARD_INSERTION		(1 << 31)
#define STATUS_CARD_REMOVAL		(1 << 30)
#define STATUS_YBUF_EMPTY		(1 << 29)
#define STATUS_XBUF_EMPTY		(1 << 28)
#define STATUS_YBUF_FULL		(1 << 27)
#define STATUS_XBUF_FULL		(1 << 26)
#define STATUS_BUF_UND_RUN		(1 << 25)
#define STATUS_BUF_OVFL			(1 << 24)
#define STATUS_SDIO_INT_ACTIVE		(1 << 14)
#define STATUS_END_CMD_RESP		(1 << 13)
#define STATUS_WRITE_OP_DONE		(1 << 12)
#define STATUS_DATA_TRANS_DONE		(1 << 11)
#define STATUS_READ_OP_DONE		(1 << 11)
#define STATUS_WR_CRC_ERROR_CODE_MASK	(3 << 10)
#define STATUS_CARD_BUS_CLK_RUN		(1 << 8)
#define STATUS_BUF_READ_RDY		(1 << 7)
#define STATUS_BUF_WRITE_RDY		(1 << 6)
#define STATUS_RESP_CRC_ERR		(1 << 5)
#define STATUS_CRC_READ_ERR		(1 << 3)
#define STATUS_CRC_WRITE_ERR		(1 << 2)
#define STATUS_TIME_OUT_RESP		(1 << 1)
#define STATUS_TIME_OUT_READ		(1 << 0)
#define STATUS_ERR_MASK			0x2f

#define CMD_DAT_CONT_CMD_RESP_LONG_OFF	(1 << 12)
#define CMD_DAT_CONT_STOP_READWAIT	(1 << 11)
#define CMD_DAT_CONT_START_READWAIT	(1 << 10)
#define CMD_DAT_CONT_BUS_WIDTH_4	(2 << 8)
#define CMD_DAT_CONT_INIT		(1 << 7)
#define CMD_DAT_CONT_WRITE		(1 << 4)
#define CMD_DAT_CONT_DATA_ENABLE	(1 << 3)
#define CMD_DAT_CONT_RESPONSE_48BIT_CRC	(1 << 0)
#define CMD_DAT_CONT_RESPONSE_136BIT	(2 << 0)
#define CMD_DAT_CONT_RESPONSE_48BIT	(3 << 0)

#define INT_SDIO_INT_WKP_EN		(1 << 18)
#define INT_CARD_INSERTION_WKP_EN	(1 << 17)
#define INT_CARD_REMOVAL_WKP_EN		(1 << 16)
#define INT_CARD_INSERTION_EN		(1 << 15)
#define INT_CARD_REMOVAL_EN		(1 << 14)
#define INT_SDIO_IRQ_EN			(1 << 13)
#define INT_DAT0_EN			(1 << 12)
#define INT_BUF_READ_EN			(1 << 4)
#define INT_BUF_WRITE_EN		(1 << 3)
#define INT_END_CMD_RES_EN		(1 << 2)
#define INT_WRITE_OP_DONE_EN		(1 << 1)
#define INT_READ_OP_EN			(1 << 0)

struct mxcmci_host {
	struct mmc_host		*mmc;
	struct resource		*res;
	void __iomem		*base;
	int			irq;
	int			detect_irq;
S
Sascha Hauer 已提交
121 122
	struct dma_chan		*dma;
	struct dma_async_tx_descriptor *desc;
123
	int			do_dma;
124
	int			default_irq_mask;
125
	int			use_sdio;
126 127 128 129 130 131 132 133 134 135 136 137 138
	unsigned int		power_mode;
	struct imxmmc_platform_data *pdata;

	struct mmc_request	*req;
	struct mmc_command	*cmd;
	struct mmc_data		*data;

	unsigned int		datasize;
	unsigned int		dma_dir;

	u16			rev_no;
	unsigned int		cmdat;

139 140
	struct clk		*clk_ipg;
	struct clk		*clk_per;
141 142 143 144

	int			clock;

	struct work_struct	datawork;
145
	spinlock_t		lock;
146 147

	struct regulator	*vcc;
S
Sascha Hauer 已提交
148 149 150 151 152

	int			burstlen;
	int			dmareq;
	struct dma_slave_config dma_slave_config;
	struct imx_dma_data	dma_data;
153 154
};

155 156
static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios);

157 158 159 160 161 162 163 164 165 166 167 168
static inline void mxcmci_init_ocr(struct mxcmci_host *host)
{
	host->vcc = regulator_get(mmc_dev(host->mmc), "vmmc");

	if (IS_ERR(host->vcc)) {
		host->vcc = NULL;
	} else {
		host->mmc->ocr_avail = mmc_regulator_get_ocrmask(host->vcc);
		if (host->pdata && host->pdata->ocr_avail)
			dev_warn(mmc_dev(host->mmc),
				"pdata->ocr_avail will not be used\n");
	}
169

170 171 172 173 174 175 176 177 178
	if (host->vcc == NULL) {
		/* fall-back to platform data */
		if (host->pdata && host->pdata->ocr_avail)
			host->mmc->ocr_avail = host->pdata->ocr_avail;
		else
			host->mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
	}
}

179 180 181
static inline void mxcmci_set_power(struct mxcmci_host *host,
				    unsigned char power_mode,
				    unsigned int vdd)
182
{
183 184 185 186 187 188 189
	if (host->vcc) {
		if (power_mode == MMC_POWER_UP)
			mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
		else if (power_mode == MMC_POWER_OFF)
			mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
	}

190 191 192 193
	if (host->pdata && host->pdata->setpower)
		host->pdata->setpower(mmc_dev(host->mmc), vdd);
}

194 195 196 197 198 199 200 201 202
static inline int mxcmci_use_dma(struct mxcmci_host *host)
{
	return host->do_dma;
}

static void mxcmci_softreset(struct mxcmci_host *host)
{
	int i;

D
Daniel Mack 已提交
203 204
	dev_dbg(mmc_dev(host->mmc), "mxcmci_softreset\n");

205 206 207 208 209 210 211 212 213 214
	/* reset sequence */
	writew(STR_STP_CLK_RESET, host->base + MMC_REG_STR_STP_CLK);
	writew(STR_STP_CLK_RESET | STR_STP_CLK_START_CLK,
			host->base + MMC_REG_STR_STP_CLK);

	for (i = 0; i < 8; i++)
		writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);

	writew(0xff, host->base + MMC_REG_RES_TO);
}
S
Sascha Hauer 已提交
215
static int mxcmci_setup_dma(struct mmc_host *mmc);
216

217
static int mxcmci_setup_data(struct mxcmci_host *host, struct mmc_data *data)
218 219 220 221 222
{
	unsigned int nob = data->blocks;
	unsigned int blksz = data->blksz;
	unsigned int datasize = nob * blksz;
	struct scatterlist *sg;
223
	enum dma_transfer_direction slave_dirn;
S
Sascha Hauer 已提交
224 225
	int i, nents;

226 227 228 229 230 231 232 233 234 235
	if (data->flags & MMC_DATA_STREAM)
		nob = 0xffff;

	host->data = data;
	data->bytes_xfered = 0;

	writew(nob, host->base + MMC_REG_NOB);
	writew(blksz, host->base + MMC_REG_BLK_LEN);
	host->datasize = datasize;

S
Sascha Hauer 已提交
236 237 238
	if (!mxcmci_use_dma(host))
		return 0;

239 240 241
	for_each_sg(data->sg, sg, data->sg_len, i) {
		if (sg->offset & 3 || sg->length & 3) {
			host->do_dma = 0;
242
			return 0;
243 244 245
		}
	}

246
	if (data->flags & MMC_DATA_READ) {
247
		host->dma_dir = DMA_FROM_DEVICE;
248 249
		slave_dirn = DMA_DEV_TO_MEM;
	} else {
250
		host->dma_dir = DMA_TO_DEVICE;
251 252
		slave_dirn = DMA_MEM_TO_DEV;
	}
253

S
Sascha Hauer 已提交
254 255 256 257 258
	nents = dma_map_sg(host->dma->device->dev, data->sg,
				     data->sg_len,  host->dma_dir);
	if (nents != data->sg_len)
		return -EINVAL;

259
	host->desc = dmaengine_prep_slave_sg(host->dma,
260
		data->sg, data->sg_len, slave_dirn,
S
Sascha Hauer 已提交
261
		DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
262

S
Sascha Hauer 已提交
263 264 265 266 267
	if (!host->desc) {
		dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
				host->dma_dir);
		host->do_dma = 0;
		return 0; /* Fall back to PIO */
268
	}
269 270
	wmb();

S
Sascha Hauer 已提交
271
	dmaengine_submit(host->desc);
272
	dma_async_issue_pending(host->dma);
S
Sascha Hauer 已提交
273

274
	return 0;
275 276 277 278 279
}

static int mxcmci_start_cmd(struct mxcmci_host *host, struct mmc_command *cmd,
		unsigned int cmdat)
{
280
	u32 int_cntr = host->default_irq_mask;
281 282
	unsigned long flags;

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	WARN_ON(host->cmd != NULL);
	host->cmd = cmd;

	switch (mmc_resp_type(cmd)) {
	case MMC_RSP_R1: /* short CRC, OPCODE */
	case MMC_RSP_R1B:/* short CRC, OPCODE, BUSY */
		cmdat |= CMD_DAT_CONT_RESPONSE_48BIT_CRC;
		break;
	case MMC_RSP_R2: /* long 136 bit + CRC */
		cmdat |= CMD_DAT_CONT_RESPONSE_136BIT;
		break;
	case MMC_RSP_R3: /* short */
		cmdat |= CMD_DAT_CONT_RESPONSE_48BIT;
		break;
	case MMC_RSP_NONE:
		break;
	default:
		dev_err(mmc_dev(host->mmc), "unhandled response type 0x%x\n",
				mmc_resp_type(cmd));
		cmd->error = -EINVAL;
		return -EINVAL;
	}

306 307
	int_cntr = INT_END_CMD_RES_EN;

308
	if (mxcmci_use_dma(host))
309 310 311 312 313 314 315
		int_cntr |= INT_READ_OP_EN | INT_WRITE_OP_DONE_EN;

	spin_lock_irqsave(&host->lock, flags);
	if (host->use_sdio)
		int_cntr |= INT_SDIO_IRQ_EN;
	writel(int_cntr, host->base + MMC_REG_INT_CNTR);
	spin_unlock_irqrestore(&host->lock, flags);
316 317 318 319 320 321 322 323 324 325 326

	writew(cmd->opcode, host->base + MMC_REG_CMD);
	writel(cmd->arg, host->base + MMC_REG_ARG);
	writew(cmdat, host->base + MMC_REG_CMD_DAT_CONT);

	return 0;
}

static void mxcmci_finish_request(struct mxcmci_host *host,
		struct mmc_request *req)
{
327
	u32 int_cntr = host->default_irq_mask;
328 329 330 331 332 333 334
	unsigned long flags;

	spin_lock_irqsave(&host->lock, flags);
	if (host->use_sdio)
		int_cntr |= INT_SDIO_IRQ_EN;
	writel(int_cntr, host->base + MMC_REG_INT_CNTR);
	spin_unlock_irqrestore(&host->lock, flags);
335 336 337 338 339 340 341 342 343 344 345 346 347 348

	host->req = NULL;
	host->cmd = NULL;
	host->data = NULL;

	mmc_request_done(host->mmc, req);
}

static int mxcmci_finish_data(struct mxcmci_host *host, unsigned int stat)
{
	struct mmc_data *data = host->data;
	int data_error;

	if (mxcmci_use_dma(host)) {
S
Sascha Hauer 已提交
349 350
		dmaengine_terminate_all(host->dma);
		dma_unmap_sg(host->dma->device->dev, data->sg, data->sg_len,
351 352 353 354 355 356 357
				host->dma_dir);
	}

	if (stat & STATUS_ERR_MASK) {
		dev_dbg(mmc_dev(host->mmc), "request failed. status: 0x%08x\n",
				stat);
		if (stat & STATUS_CRC_READ_ERR) {
D
Daniel Mack 已提交
358
			dev_err(mmc_dev(host->mmc), "%s: -EILSEQ\n", __func__);
359 360 361
			data->error = -EILSEQ;
		} else if (stat & STATUS_CRC_WRITE_ERR) {
			u32 err_code = (stat >> 9) & 0x3;
D
Daniel Mack 已提交
362 363 364
			if (err_code == 2) { /* No CRC response */
				dev_err(mmc_dev(host->mmc),
					"%s: No CRC -ETIMEDOUT\n", __func__);
365
				data->error = -ETIMEDOUT;
D
Daniel Mack 已提交
366 367 368
			} else {
				dev_err(mmc_dev(host->mmc),
					"%s: -EILSEQ\n", __func__);
369
				data->error = -EILSEQ;
D
Daniel Mack 已提交
370
			}
371
		} else if (stat & STATUS_TIME_OUT_READ) {
D
Daniel Mack 已提交
372 373
			dev_err(mmc_dev(host->mmc),
				"%s: read -ETIMEDOUT\n", __func__);
374 375
			data->error = -ETIMEDOUT;
		} else {
D
Daniel Mack 已提交
376
			dev_err(mmc_dev(host->mmc), "%s: -EIO\n", __func__);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			data->error = -EIO;
		}
	} else {
		data->bytes_xfered = host->datasize;
	}

	data_error = data->error;

	host->data = NULL;

	return data_error;
}

static void mxcmci_read_response(struct mxcmci_host *host, unsigned int stat)
{
	struct mmc_command *cmd = host->cmd;
	int i;
	u32 a, b, c;

	if (!cmd)
		return;

	if (stat & STATUS_TIME_OUT_RESP) {
		dev_dbg(mmc_dev(host->mmc), "CMD TIMEOUT\n");
		cmd->error = -ETIMEDOUT;
	} else if (stat & STATUS_RESP_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
		dev_dbg(mmc_dev(host->mmc), "cmd crc error\n");
		cmd->error = -EILSEQ;
	}

	if (cmd->flags & MMC_RSP_PRESENT) {
		if (cmd->flags & MMC_RSP_136) {
			for (i = 0; i < 4; i++) {
				a = readw(host->base + MMC_REG_RES_FIFO);
				b = readw(host->base + MMC_REG_RES_FIFO);
				cmd->resp[i] = a << 16 | b;
			}
		} else {
			a = readw(host->base + MMC_REG_RES_FIFO);
			b = readw(host->base + MMC_REG_RES_FIFO);
			c = readw(host->base + MMC_REG_RES_FIFO);
			cmd->resp[0] = a << 24 | b << 8 | c >> 8;
		}
	}
}

static int mxcmci_poll_status(struct mxcmci_host *host, u32 mask)
{
	u32 stat;
	unsigned long timeout = jiffies + HZ;

	do {
		stat = readl(host->base + MMC_REG_STATUS);
		if (stat & STATUS_ERR_MASK)
			return stat;
432 433 434
		if (time_after(jiffies, timeout)) {
			mxcmci_softreset(host);
			mxcmci_set_clk_rate(host, host->clock);
435
			return STATUS_TIME_OUT_READ;
436
		}
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		if (stat & mask)
			return 0;
		cpu_relax();
	} while (1);
}

static int mxcmci_pull(struct mxcmci_host *host, void *_buf, int bytes)
{
	unsigned int stat;
	u32 *buf = _buf;

	while (bytes > 3) {
		stat = mxcmci_poll_status(host,
				STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
		if (stat)
			return stat;
		*buf++ = readl(host->base + MMC_REG_BUFFER_ACCESS);
		bytes -= 4;
	}

	if (bytes) {
		u8 *b = (u8 *)buf;
		u32 tmp;

		stat = mxcmci_poll_status(host,
				STATUS_BUF_READ_RDY | STATUS_READ_OP_DONE);
		if (stat)
			return stat;
		tmp = readl(host->base + MMC_REG_BUFFER_ACCESS);
		memcpy(b, &tmp, bytes);
	}

	return 0;
}

static int mxcmci_push(struct mxcmci_host *host, void *_buf, int bytes)
{
	unsigned int stat;
	u32 *buf = _buf;

	while (bytes > 3) {
		stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
		if (stat)
			return stat;
		writel(*buf++, host->base + MMC_REG_BUFFER_ACCESS);
		bytes -= 4;
	}

	if (bytes) {
		u8 *b = (u8 *)buf;
		u32 tmp;

		stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
		if (stat)
			return stat;

		memcpy(&tmp, b, bytes);
		writel(tmp, host->base + MMC_REG_BUFFER_ACCESS);
	}

	stat = mxcmci_poll_status(host, STATUS_BUF_WRITE_RDY);
	if (stat)
		return stat;

	return 0;
}

static int mxcmci_transfer_data(struct mxcmci_host *host)
{
	struct mmc_data *data = host->req->data;
	struct scatterlist *sg;
	int stat, i;

	host->data = data;
	host->datasize = 0;

	if (data->flags & MMC_DATA_READ) {
		for_each_sg(data->sg, sg, data->sg_len, i) {
			stat = mxcmci_pull(host, sg_virt(sg), sg->length);
			if (stat)
				return stat;
			host->datasize += sg->length;
		}
	} else {
		for_each_sg(data->sg, sg, data->sg_len, i) {
			stat = mxcmci_push(host, sg_virt(sg), sg->length);
			if (stat)
				return stat;
			host->datasize += sg->length;
		}
		stat = mxcmci_poll_status(host, STATUS_WRITE_OP_DONE);
		if (stat)
			return stat;
	}
	return 0;
}

static void mxcmci_datawork(struct work_struct *work)
{
	struct mxcmci_host *host = container_of(work, struct mxcmci_host,
						  datawork);
	int datastat = mxcmci_transfer_data(host);
539 540 541

	writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
		host->base + MMC_REG_STATUS);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
	mxcmci_finish_data(host, datastat);

	if (host->req->stop) {
		if (mxcmci_start_cmd(host, host->req->stop, 0)) {
			mxcmci_finish_request(host, host->req);
			return;
		}
	} else {
		mxcmci_finish_request(host, host->req);
	}
}

static void mxcmci_data_done(struct mxcmci_host *host, unsigned int stat)
{
	struct mmc_data *data = host->data;
	int data_error;

	if (!data)
		return;

	data_error = mxcmci_finish_data(host, stat);

	mxcmci_read_response(host, stat);
	host->cmd = NULL;

	if (host->req->stop) {
		if (mxcmci_start_cmd(host, host->req->stop, 0)) {
			mxcmci_finish_request(host, host->req);
			return;
		}
	} else {
		mxcmci_finish_request(host, host->req);
	}
}

static void mxcmci_cmd_done(struct mxcmci_host *host, unsigned int stat)
{
	mxcmci_read_response(host, stat);
	host->cmd = NULL;

	if (!host->data && host->req) {
		mxcmci_finish_request(host, host->req);
		return;
	}

	/* For the DMA case the DMA engine handles the data transfer
588
	 * automatically. For non DMA we have to do it ourselves.
589 590 591 592 593 594 595 596 597 598
	 * Don't do it in interrupt context though.
	 */
	if (!mxcmci_use_dma(host) && host->data)
		schedule_work(&host->datawork);

}

static irqreturn_t mxcmci_irq(int irq, void *devid)
{
	struct mxcmci_host *host = devid;
599 600
	unsigned long flags;
	bool sdio_irq;
601 602 603
	u32 stat;

	stat = readl(host->base + MMC_REG_STATUS);
604 605
	writel(stat & ~(STATUS_SDIO_INT_ACTIVE | STATUS_DATA_TRANS_DONE |
			STATUS_WRITE_OP_DONE), host->base + MMC_REG_STATUS);
606 607 608

	dev_dbg(mmc_dev(host->mmc), "%s: 0x%08x\n", __func__, stat);

609 610 611 612
	spin_lock_irqsave(&host->lock, flags);
	sdio_irq = (stat & STATUS_SDIO_INT_ACTIVE) && host->use_sdio;
	spin_unlock_irqrestore(&host->lock, flags);

613 614 615 616 617
	if (mxcmci_use_dma(host) &&
	    (stat & (STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE)))
		writel(STATUS_READ_OP_DONE | STATUS_WRITE_OP_DONE,
			host->base + MMC_REG_STATUS);

618 619 620 621 622
	if (sdio_irq) {
		writel(STATUS_SDIO_INT_ACTIVE, host->base + MMC_REG_STATUS);
		mmc_signal_sdio_irq(host->mmc);
	}

623 624
	if (stat & STATUS_END_CMD_RESP)
		mxcmci_cmd_done(host, stat);
625

626 627 628
	if (mxcmci_use_dma(host) &&
		  (stat & (STATUS_DATA_TRANS_DONE | STATUS_WRITE_OP_DONE)))
		mxcmci_data_done(host, stat);
S
Sascha Hauer 已提交
629

630 631 632
	if (host->default_irq_mask &&
		  (stat & (STATUS_CARD_INSERTION | STATUS_CARD_REMOVAL)))
		mmc_detect_change(host->mmc, msecs_to_jiffies(200));
S
Sascha Hauer 已提交
633

634 635 636 637 638 639 640
	return IRQ_HANDLED;
}

static void mxcmci_request(struct mmc_host *mmc, struct mmc_request *req)
{
	struct mxcmci_host *host = mmc_priv(mmc);
	unsigned int cmdat = host->cmdat;
641
	int error;
642 643 644 645 646

	WARN_ON(host->req != NULL);

	host->req = req;
	host->cmdat &= ~CMD_DAT_CONT_INIT;
S
Sascha Hauer 已提交
647 648 649 650

	if (host->dma)
		host->do_dma = 1;

651
	if (req->data) {
652 653 654 655 656 657
		error = mxcmci_setup_data(host, req->data);
		if (error) {
			req->cmd->error = error;
			goto out;
		}

658 659 660 661 662 663 664

		cmdat |= CMD_DAT_CONT_DATA_ENABLE;

		if (req->data->flags & MMC_DATA_WRITE)
			cmdat |= CMD_DAT_CONT_WRITE;
	}

665
	error = mxcmci_start_cmd(host, req->cmd, cmdat);
S
Sascha Hauer 已提交
666

667 668
out:
	if (error)
669 670 671 672 673 674 675
		mxcmci_finish_request(host, req);
}

static void mxcmci_set_clk_rate(struct mxcmci_host *host, unsigned int clk_ios)
{
	unsigned int divider;
	int prescaler = 0;
676
	unsigned int clk_in = clk_get_rate(host->clk_per);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

	while (prescaler <= 0x800) {
		for (divider = 1; divider <= 0xF; divider++) {
			int x;

			x = (clk_in / (divider + 1));

			if (prescaler)
				x /= (prescaler * 2);

			if (x <= clk_ios)
				break;
		}
		if (divider < 0x10)
			break;

		if (prescaler == 0)
			prescaler = 1;
		else
			prescaler <<= 1;
	}

	writew((prescaler << 4) | divider, host->base + MMC_REG_CLK_RATE);

	dev_dbg(mmc_dev(host->mmc), "scaler: %d divider: %d in: %d out: %d\n",
			prescaler, divider, clk_in, clk_ios);
}

S
Sascha Hauer 已提交
705 706 707 708 709 710 711 712 713 714 715
static int mxcmci_setup_dma(struct mmc_host *mmc)
{
	struct mxcmci_host *host = mmc_priv(mmc);
	struct dma_slave_config *config = &host->dma_slave_config;

	config->dst_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
	config->src_addr = host->res->start + MMC_REG_BUFFER_ACCESS;
	config->dst_addr_width = 4;
	config->src_addr_width = 4;
	config->dst_maxburst = host->burstlen;
	config->src_maxburst = host->burstlen;
716
	config->device_fc = false;
S
Sascha Hauer 已提交
717 718 719 720

	return dmaengine_slave_config(host->dma, config);
}

721 722 723
static void mxcmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
	struct mxcmci_host *host = mmc_priv(mmc);
S
Sascha Hauer 已提交
724 725
	int burstlen, ret;

726
	/*
727 728
	 * use burstlen of 64 (16 words) in 4 bit mode (--> reg value  0)
	 * use burstlen of 16 (4 words) in 1 bit mode (--> reg value 16)
729 730
	 */
	if (ios->bus_width == MMC_BUS_WIDTH_4)
S
Sascha Hauer 已提交
731
		burstlen = 16;
732 733
	else
		burstlen = 4;
S
Sascha Hauer 已提交
734 735 736 737 738 739 740 741 742

	if (mxcmci_use_dma(host) && burstlen != host->burstlen) {
		host->burstlen = burstlen;
		ret = mxcmci_setup_dma(mmc);
		if (ret) {
			dev_err(mmc_dev(host->mmc),
				"failed to config DMA channel. Falling back to PIO\n");
			dma_release_channel(host->dma);
			host->do_dma = 0;
743
			host->dma = NULL;
S
Sascha Hauer 已提交
744 745
		}
	}
746 747 748 749 750 751 752

	if (ios->bus_width == MMC_BUS_WIDTH_4)
		host->cmdat |= CMD_DAT_CONT_BUS_WIDTH_4;
	else
		host->cmdat &= ~CMD_DAT_CONT_BUS_WIDTH_4;

	if (host->power_mode != ios->power_mode) {
753
		mxcmci_set_power(host, ios->power_mode, ios->vdd);
754
		host->power_mode = ios->power_mode;
755

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		if (ios->power_mode == MMC_POWER_ON)
			host->cmdat |= CMD_DAT_CONT_INIT;
	}

	if (ios->clock) {
		mxcmci_set_clk_rate(host, ios->clock);
		writew(STR_STP_CLK_START_CLK, host->base + MMC_REG_STR_STP_CLK);
	} else {
		writew(STR_STP_CLK_STOP_CLK, host->base + MMC_REG_STR_STP_CLK);
	}

	host->clock = ios->clock;
}

static irqreturn_t mxcmci_detect_irq(int irq, void *data)
{
	struct mmc_host *mmc = data;

	dev_dbg(mmc_dev(mmc), "%s\n", __func__);

	mmc_detect_change(mmc, msecs_to_jiffies(250));
	return IRQ_HANDLED;
}

static int mxcmci_get_ro(struct mmc_host *mmc)
{
	struct mxcmci_host *host = mmc_priv(mmc);

	if (host->pdata && host->pdata->get_ro)
		return !!host->pdata->get_ro(mmc_dev(mmc));
	/*
	 * Board doesn't support read only detection; let the mmc core
	 * decide what to do.
	 */
	return -ENOSYS;
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static void mxcmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
{
	struct mxcmci_host *host = mmc_priv(mmc);
	unsigned long flags;
	u32 int_cntr;

	spin_lock_irqsave(&host->lock, flags);
	host->use_sdio = enable;
	int_cntr = readl(host->base + MMC_REG_INT_CNTR);

	if (enable)
		int_cntr |= INT_SDIO_IRQ_EN;
	else
		int_cntr &= ~INT_SDIO_IRQ_EN;

	writel(int_cntr, host->base + MMC_REG_INT_CNTR);
	spin_unlock_irqrestore(&host->lock, flags);
}
811

812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
static void mxcmci_init_card(struct mmc_host *host, struct mmc_card *card)
{
	/*
	 * MX3 SoCs have a silicon bug which corrupts CRC calculation of
	 * multi-block transfers when connected SDIO peripheral doesn't
	 * drive the BUSY line as required by the specs.
	 * One way to prevent this is to only allow 1-bit transfers.
	 */

	if (cpu_is_mx3() && card->type == MMC_TYPE_SDIO)
		host->caps &= ~MMC_CAP_4_BIT_DATA;
	else
		host->caps |= MMC_CAP_4_BIT_DATA;
}

S
Sascha Hauer 已提交
827 828 829 830 831 832 833 834 835 836 837 838
static bool filter(struct dma_chan *chan, void *param)
{
	struct mxcmci_host *host = param;

	if (!imx_dma_is_general_purpose(chan))
		return false;

	chan->private = &host->dma_data;

	return true;
}

839
static const struct mmc_host_ops mxcmci_ops = {
840 841 842 843
	.request		= mxcmci_request,
	.set_ios		= mxcmci_set_ios,
	.get_ro			= mxcmci_get_ro,
	.enable_sdio_irq	= mxcmci_enable_sdio_irq,
844
	.init_card		= mxcmci_init_card,
845 846 847 848 849 850
};

static int mxcmci_probe(struct platform_device *pdev)
{
	struct mmc_host *mmc;
	struct mxcmci_host *host = NULL;
851
	struct resource *iores, *r;
852
	int ret = 0, irq;
S
Sascha Hauer 已提交
853
	dma_cap_mask_t mask;
854

855
	pr_info("i.MX SDHC driver\n");
856

857
	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
858
	irq = platform_get_irq(pdev, 0);
859
	if (!iores || irq < 0)
860 861
		return -EINVAL;

862
	r = request_mem_region(iores->start, resource_size(iores), pdev->name);
863 864 865 866 867 868 869 870 871 872
	if (!r)
		return -EBUSY;

	mmc = mmc_alloc_host(sizeof(struct mxcmci_host), &pdev->dev);
	if (!mmc) {
		ret = -ENOMEM;
		goto out_release_mem;
	}

	mmc->ops = &mxcmci_ops;
873
	mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
874 875

	/* MMC core transfer sizes tunable parameters */
876
	mmc->max_segs = 64;
877 878 879
	mmc->max_blk_size = 2048;
	mmc->max_blk_count = 65535;
	mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
880
	mmc->max_seg_size = mmc->max_req_size;
881 882 883 884 885 886 887 888 889 890

	host = mmc_priv(mmc);
	host->base = ioremap(r->start, resource_size(r));
	if (!host->base) {
		ret = -ENOMEM;
		goto out_free;
	}

	host->mmc = mmc;
	host->pdata = pdev->dev.platform_data;
891
	spin_lock_init(&host->lock);
892

893
	mxcmci_init_ocr(host);
894

895 896 897 898 899 900
	if (host->pdata && host->pdata->dat3_card_detect)
		host->default_irq_mask =
			INT_CARD_INSERTION_EN | INT_CARD_REMOVAL_EN;
	else
		host->default_irq_mask = 0;

901 902 903
	host->res = r;
	host->irq = irq;

904 905 906
	host->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
	if (IS_ERR(host->clk_ipg)) {
		ret = PTR_ERR(host->clk_ipg);
907 908
		goto out_iounmap;
	}
909 910 911 912 913 914 915 916 917

	host->clk_per = devm_clk_get(&pdev->dev, "per");
	if (IS_ERR(host->clk_per)) {
		ret = PTR_ERR(host->clk_per);
		goto out_iounmap;
	}

	clk_prepare_enable(host->clk_per);
	clk_prepare_enable(host->clk_ipg);
918 919 920 921 922 923 924 925 926 927 928

	mxcmci_softreset(host);

	host->rev_no = readw(host->base + MMC_REG_REV_NO);
	if (host->rev_no != 0x400) {
		ret = -ENODEV;
		dev_err(mmc_dev(host->mmc), "wrong rev.no. 0x%08x. aborting.\n",
			host->rev_no);
		goto out_clk_put;
	}

929 930
	mmc->f_min = clk_get_rate(host->clk_per) >> 16;
	mmc->f_max = clk_get_rate(host->clk_per) >> 1;
931 932 933 934

	/* recommended in data sheet */
	writew(0x2db4, host->base + MMC_REG_READ_TO);

935
	writel(host->default_irq_mask, host->base + MMC_REG_INT_CNTR);
936 937

	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
S
Sascha Hauer 已提交
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
	if (r) {
		host->dmareq = r->start;
		host->dma_data.peripheral_type = IMX_DMATYPE_SDHC;
		host->dma_data.priority = DMA_PRIO_LOW;
		host->dma_data.dma_request = host->dmareq;
		dma_cap_zero(mask);
		dma_cap_set(DMA_SLAVE, mask);
		host->dma = dma_request_channel(mask, filter, host);
		if (host->dma)
			mmc->max_seg_size = dma_get_max_seg_size(
					host->dma->device->dev);
	}

	if (!host->dma)
		dev_info(mmc_dev(host->mmc), "dma not available. Using PIO\n");
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

	INIT_WORK(&host->datawork, mxcmci_datawork);

	ret = request_irq(host->irq, mxcmci_irq, 0, DRIVER_NAME, host);
	if (ret)
		goto out_free_dma;

	platform_set_drvdata(pdev, mmc);

	if (host->pdata && host->pdata->init) {
		ret = host->pdata->init(&pdev->dev, mxcmci_detect_irq,
				host->mmc);
		if (ret)
			goto out_free_irq;
	}

	mmc_add_host(mmc);

	return 0;

out_free_irq:
	free_irq(host->irq, host);
out_free_dma:
S
Sascha Hauer 已提交
976 977
	if (host->dma)
		dma_release_channel(host->dma);
978
out_clk_put:
979 980
	clk_disable_unprepare(host->clk_per);
	clk_disable_unprepare(host->clk_ipg);
981 982 983 984 985
out_iounmap:
	iounmap(host->base);
out_free:
	mmc_free_host(mmc);
out_release_mem:
986
	release_mem_region(iores->start, resource_size(iores));
987 988 989 990 991 992 993 994 995 996 997 998
	return ret;
}

static int mxcmci_remove(struct platform_device *pdev)
{
	struct mmc_host *mmc = platform_get_drvdata(pdev);
	struct mxcmci_host *host = mmc_priv(mmc);

	platform_set_drvdata(pdev, NULL);

	mmc_remove_host(mmc);

999 1000 1001
	if (host->vcc)
		regulator_put(host->vcc);

1002 1003 1004 1005 1006
	if (host->pdata && host->pdata->exit)
		host->pdata->exit(&pdev->dev, mmc);

	free_irq(host->irq, host);
	iounmap(host->base);
S
Sascha Hauer 已提交
1007 1008 1009 1010

	if (host->dma)
		dma_release_channel(host->dma);

1011 1012
	clk_disable_unprepare(host->clk_per);
	clk_disable_unprepare(host->clk_ipg);
1013 1014 1015 1016 1017 1018 1019 1020 1021

	release_mem_region(host->res->start, resource_size(host->res));

	mmc_free_host(mmc);

	return 0;
}

#ifdef CONFIG_PM
1022
static int mxcmci_suspend(struct device *dev)
1023
{
1024 1025
	struct mmc_host *mmc = dev_get_drvdata(dev);
	struct mxcmci_host *host = mmc_priv(mmc);
1026 1027 1028
	int ret = 0;

	if (mmc)
1029
		ret = mmc_suspend_host(mmc);
1030 1031
	clk_disable_unprepare(host->clk_per);
	clk_disable_unprepare(host->clk_ipg);
1032 1033 1034 1035

	return ret;
}

1036
static int mxcmci_resume(struct device *dev)
1037
{
1038 1039
	struct mmc_host *mmc = dev_get_drvdata(dev);
	struct mxcmci_host *host = mmc_priv(mmc);
1040 1041
	int ret = 0;

1042 1043
	clk_prepare_enable(host->clk_per);
	clk_prepare_enable(host->clk_ipg);
1044
	if (mmc)
1045 1046 1047 1048
		ret = mmc_resume_host(mmc);

	return ret;
}
1049 1050 1051 1052 1053 1054

static const struct dev_pm_ops mxcmci_pm_ops = {
	.suspend	= mxcmci_suspend,
	.resume		= mxcmci_resume,
};
#endif
1055 1056 1057 1058 1059 1060 1061

static struct platform_driver mxcmci_driver = {
	.probe		= mxcmci_probe,
	.remove		= mxcmci_remove,
	.driver		= {
		.name		= DRIVER_NAME,
		.owner		= THIS_MODULE,
1062 1063 1064
#ifdef CONFIG_PM
		.pm	= &mxcmci_pm_ops,
#endif
1065 1066 1067
	}
};

1068
module_platform_driver(mxcmci_driver);
1069 1070 1071 1072 1073

MODULE_DESCRIPTION("i.MX Multimedia Card Interface Driver");
MODULE_AUTHOR("Sascha Hauer, Pengutronix");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:imx-mmc");