pxamci.c 20.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
P
Pierre Ossman 已提交
2
 *  linux/drivers/mmc/host/pxa.c - PXA MMCI driver
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
 *
 * 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.
 *
 *  This hardware is really sick:
 *   - No way to clear interrupts.
 *   - Have to turn off the clock whenever we touch the device.
 *   - Doesn't tell you how many data blocks were transferred.
 *  Yuck!
 *
 *	1 and 3 byte data transfers not supported
 *	max block length up to 1023
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
22
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
23 24 25
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
26 27
#include <linux/clk.h>
#include <linux/err.h>
L
Linus Torvalds 已提交
28
#include <linux/mmc/host.h>
29
#include <linux/io.h>
D
Daniel Ribeiro 已提交
30
#include <linux/regulator/consumer.h>
31
#include <linux/gpio.h>
32
#include <linux/gfp.h>
L
Linus Torvalds 已提交
33 34 35

#include <asm/sizes.h>

36
#include <mach/hardware.h>
37
#include <mach/dma.h>
38
#include <mach/mmc.h>
L
Linus Torvalds 已提交
39 40 41 42 43 44

#include "pxamci.h"

#define DRIVER_NAME	"pxa2xx-mci"

#define NR_SG	1
R
Russell King 已提交
45
#define CLKRT_OFF	(~0)
L
Linus Torvalds 已提交
46

47 48 49
#define mmc_has_26MHz()		(cpu_is_pxa300() || cpu_is_pxa310() \
				|| cpu_is_pxa935())

L
Linus Torvalds 已提交
50 51 52 53 54
struct pxamci_host {
	struct mmc_host		*mmc;
	spinlock_t		lock;
	struct resource		*res;
	void __iomem		*base;
55 56
	struct clk		*clk;
	unsigned long		clkrate;
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	int			irq;
	int			dma;
	unsigned int		clkrt;
	unsigned int		cmdat;
	unsigned int		imask;
	unsigned int		power_mode;
	struct pxamci_platform_data *pdata;

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

	dma_addr_t		sg_dma;
	struct pxa_dma_desc	*sg_cpu;
	unsigned int		dma_len;

	unsigned int		dma_dir;
74 75
	unsigned int		dma_drcmrrx;
	unsigned int		dma_drcmrtx;
D
Daniel Ribeiro 已提交
76 77

	struct regulator	*vcc;
L
Linus Torvalds 已提交
78 79
};

D
Daniel Ribeiro 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
static inline void pxamci_init_ocr(struct pxamci_host *host)
{
#ifdef CONFIG_REGULATOR
	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_mask)
			dev_warn(mmc_dev(host->mmc),
				"ocr_mask/setpower will not be used\n");
	}
#endif
	if (host->vcc == NULL) {
		/* fall-back to platform data */
		host->mmc->ocr_avail = host->pdata ?
			host->pdata->ocr_mask :
			MMC_VDD_32_33 | MMC_VDD_33_34;
	}
}

102 103 104
static inline int pxamci_set_power(struct pxamci_host *host,
				    unsigned char power_mode,
				    unsigned int vdd)
D
Daniel Ribeiro 已提交
105
{
106 107
	int on;

108 109 110 111 112 113 114 115 116 117 118 119 120
	if (host->vcc) {
		int ret;

		if (power_mode == MMC_POWER_UP) {
			ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
			if (ret)
				return ret;
		} else if (power_mode == MMC_POWER_OFF) {
			ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
			if (ret)
				return ret;
		}
	}
121 122 123 124 125 126
	if (!host->vcc && host->pdata &&
	    gpio_is_valid(host->pdata->gpio_power)) {
		on = ((1 << vdd) & host->pdata->ocr_mask);
		gpio_set_value(host->pdata->gpio_power,
			       !!on ^ host->pdata->gpio_power_invert);
	}
D
Daniel Ribeiro 已提交
127 128
	if (!host->vcc && host->pdata && host->pdata->setpower)
		host->pdata->setpower(mmc_dev(host->mmc), vdd);
129 130

	return 0;
D
Daniel Ribeiro 已提交
131 132
}

L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
static void pxamci_stop_clock(struct pxamci_host *host)
{
	if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
		unsigned long timeout = 10000;
		unsigned int v;

		writel(STOP_CLOCK, host->base + MMC_STRPCL);

		do {
			v = readl(host->base + MMC_STAT);
			if (!(v & STAT_CLK_EN))
				break;
			udelay(1);
		} while (timeout--);

		if (v & STAT_CLK_EN)
			dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
	}
}

static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
{
	unsigned long flags;

	spin_lock_irqsave(&host->lock, flags);
	host->imask &= ~mask;
	writel(host->imask, host->base + MMC_I_MASK);
	spin_unlock_irqrestore(&host->lock, flags);
}

static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
{
	unsigned long flags;

	spin_lock_irqsave(&host->lock, flags);
	host->imask |= mask;
	writel(host->imask, host->base + MMC_I_MASK);
	spin_unlock_irqrestore(&host->lock, flags);
}

static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
{
	unsigned int nob = data->blocks;
176
	unsigned long long clks;
L
Linus Torvalds 已提交
177
	unsigned int timeout;
178
	bool dalgn = 0;
L
Linus Torvalds 已提交
179 180 181 182 183 184 185 186 187
	u32 dcmd;
	int i;

	host->data = data;

	if (data->flags & MMC_DATA_STREAM)
		nob = 0xffff;

	writel(nob, host->base + MMC_NOB);
188
	writel(data->blksz, host->base + MMC_BLKLEN);
L
Linus Torvalds 已提交
189

190
	clks = (unsigned long long)data->timeout_ns * host->clkrate;
191 192
	do_div(clks, 1000000000UL);
	timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
L
Linus Torvalds 已提交
193 194 195 196
	writel((timeout + 255) / 256, host->base + MMC_RDTO);

	if (data->flags & MMC_DATA_READ) {
		host->dma_dir = DMA_FROM_DEVICE;
M
Matt Reimer 已提交
197
		dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
198 199
		DRCMR(host->dma_drcmrtx) = 0;
		DRCMR(host->dma_drcmrrx) = host->dma | DRCMR_MAPVLD;
L
Linus Torvalds 已提交
200 201
	} else {
		host->dma_dir = DMA_TO_DEVICE;
M
Matt Reimer 已提交
202
		dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
203 204
		DRCMR(host->dma_drcmrrx) = 0;
		DRCMR(host->dma_drcmrtx) = host->dma | DRCMR_MAPVLD;
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212
	}

	dcmd |= DCMD_BURST32 | DCMD_WIDTH1;

	host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
				   host->dma_dir);

	for (i = 0; i < host->dma_len; i++) {
213 214 215 216
		unsigned int length = sg_dma_len(&data->sg[i]);
		host->sg_cpu[i].dcmd = dcmd | length;
		if (length & 31 && !(data->flags & MMC_DATA_READ))
			host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
217 218 219
		/* Not aligned to 8-byte boundary? */
		if (sg_dma_address(&data->sg[i]) & 0x7)
			dalgn = 1;
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232
		if (data->flags & MMC_DATA_READ) {
			host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
			host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
		} else {
			host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
			host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
		}
		host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
					sizeof(struct pxa_dma_desc);
	}
	host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
	wmb();

233 234 235 236 237 238 239 240
	/*
	 * The PXA27x DMA controller encounters overhead when working with
	 * unaligned (to 8-byte boundaries) data, so switch on byte alignment
	 * mode only if we have unaligned data.
	 */
	if (dalgn)
		DALGN |= (1 << host->dma);
	else
241
		DALGN &= ~(1 << host->dma);
L
Linus Torvalds 已提交
242
	DDADR(host->dma) = host->sg_dma;
243 244 245 246 247 248 249 250 251

	/*
	 * workaround for erratum #91:
	 * only start DMA now if we are doing a read,
	 * otherwise we wait until CMD/RESP has finished
	 * before starting DMA.
	 */
	if (!cpu_is_pxa27x() || data->flags & MMC_DATA_READ)
		DCSR(host->dma) = DCSR_RUN;
L
Linus Torvalds 已提交
252 253 254 255 256 257 258 259 260 261
}

static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
{
	WARN_ON(host->cmd != NULL);
	host->cmd = cmd;

	if (cmd->flags & MMC_RSP_BUSY)
		cmdat |= CMDAT_BUSY;

R
Russell King 已提交
262 263
#define RSP_TYPE(x)	((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
	switch (RSP_TYPE(mmc_resp_type(cmd))) {
P
Philip Langdale 已提交
264
	case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
L
Linus Torvalds 已提交
265 266
		cmdat |= CMDAT_RESP_SHORT;
		break;
R
Russell King 已提交
267
	case RSP_TYPE(MMC_RSP_R3):
L
Linus Torvalds 已提交
268 269
		cmdat |= CMDAT_RESP_R3;
		break;
R
Russell King 已提交
270
	case RSP_TYPE(MMC_RSP_R2):
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		cmdat |= CMDAT_RESP_R2;
		break;
	default:
		break;
	}

	writel(cmd->opcode, host->base + MMC_CMD);
	writel(cmd->arg >> 16, host->base + MMC_ARGH);
	writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
	writel(cmdat, host->base + MMC_CMDAT);
	writel(host->clkrt, host->base + MMC_CLKRT);

	writel(START_CLOCK, host->base + MMC_STRPCL);

	pxamci_enable_irq(host, END_CMD_RES);
}

static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
{
	host->mrq = NULL;
	host->cmd = NULL;
	host->data = NULL;
	mmc_request_done(host->mmc, mrq);
}

static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
{
	struct mmc_command *cmd = host->cmd;
	int i;
	u32 v;

	if (!cmd)
		return 0;

	host->cmd = NULL;

	/*
	 * Did I mention this is Sick.  We always need to
	 * discard the upper 8 bits of the first 16-bit word.
	 */
	v = readl(host->base + MMC_RES) & 0xffff;
	for (i = 0; i < 4; i++) {
		u32 w1 = readl(host->base + MMC_RES) & 0xffff;
		u32 w2 = readl(host->base + MMC_RES) & 0xffff;
		cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
		v = w2;
	}

	if (stat & STAT_TIME_OUT_RESPONSE) {
P
Pierre Ossman 已提交
320
		cmd->error = -ETIMEDOUT;
L
Linus Torvalds 已提交
321 322 323 324
	} else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
		/*
		 * workaround for erratum #42:
		 * Intel PXA27x Family Processor Specification Update Rev 001
325 326
		 * A bogus CRC error can appear if the msb of a 136 bit
		 * response is a one.
L
Linus Torvalds 已提交
327
		 */
328 329
		if (cpu_is_pxa27x() &&
		    (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000))
330
			pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
331 332
		else
			cmd->error = -EILSEQ;
L
Linus Torvalds 已提交
333 334 335
	}

	pxamci_disable_irq(host, END_CMD_RES);
P
Pierre Ossman 已提交
336
	if (host->data && !cmd->error) {
L
Linus Torvalds 已提交
337
		pxamci_enable_irq(host, DATA_TRAN_DONE);
338 339 340 341 342 343
		/*
		 * workaround for erratum #91, if doing write
		 * enable DMA late
		 */
		if (cpu_is_pxa27x() && host->data->flags & MMC_DATA_WRITE)
			DCSR(host->dma) = DCSR_RUN;
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	} else {
		pxamci_finish_request(host, host->mrq);
	}

	return 1;
}

static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
{
	struct mmc_data *data = host->data;

	if (!data)
		return 0;

	DCSR(host->dma) = 0;
V
Vernon Sauder 已提交
359
	dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
L
Linus Torvalds 已提交
360 361 362
		     host->dma_dir);

	if (stat & STAT_READ_TIME_OUT)
P
Pierre Ossman 已提交
363
		data->error = -ETIMEDOUT;
L
Linus Torvalds 已提交
364
	else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
P
Pierre Ossman 已提交
365
		data->error = -EILSEQ;
L
Linus Torvalds 已提交
366 367 368 369 370 371 372

	/*
	 * There appears to be a hardware design bug here.  There seems to
	 * be no way to find out how much data was transferred to the card.
	 * This means that if there was an error on any block, we mark all
	 * data blocks as being in error.
	 */
P
Pierre Ossman 已提交
373
	if (!data->error)
374
		data->bytes_xfered = data->blocks * data->blksz;
L
Linus Torvalds 已提交
375 376 377 378 379 380
	else
		data->bytes_xfered = 0;

	pxamci_disable_irq(host, DATA_TRAN_DONE);

	host->data = NULL;
381
	if (host->mrq->stop) {
L
Linus Torvalds 已提交
382
		pxamci_stop_clock(host);
383
		pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
L
Linus Torvalds 已提交
384 385 386 387 388 389 390
	} else {
		pxamci_finish_request(host, host->mrq);
	}

	return 1;
}

391
static irqreturn_t pxamci_irq(int irq, void *devid)
L
Linus Torvalds 已提交
392 393 394 395 396
{
	struct pxamci_host *host = devid;
	unsigned int ireg;
	int handled = 0;

397
	ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
L
Linus Torvalds 已提交
398 399 400 401

	if (ireg) {
		unsigned stat = readl(host->base + MMC_STAT);

402
		pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
L
Linus Torvalds 已提交
403 404 405 406 407

		if (ireg & END_CMD_RES)
			handled |= pxamci_cmd_done(host, stat);
		if (ireg & DATA_TRAN_DONE)
			handled |= pxamci_data_done(host, stat);
408 409 410 411
		if (ireg & SDIO_INT) {
			mmc_signal_sdio_irq(host->mmc);
			handled = 1;
		}
L
Linus Torvalds 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	}

	return IRQ_RETVAL(handled);
}

static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
	struct pxamci_host *host = mmc_priv(mmc);
	unsigned int cmdat;

	WARN_ON(host->mrq != NULL);

	host->mrq = mrq;

	pxamci_stop_clock(host);

	cmdat = host->cmdat;
	host->cmdat &= ~CMDAT_INIT;

	if (mrq->data) {
		pxamci_setup_data(host, mrq->data);

		cmdat &= ~CMDAT_BUSY;
		cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
		if (mrq->data->flags & MMC_DATA_WRITE)
			cmdat |= CMDAT_WRITE;

		if (mrq->data->flags & MMC_DATA_STREAM)
			cmdat |= CMDAT_STREAM;
	}

	pxamci_start_cmd(host, mrq->cmd, cmdat);
}

446 447 448 449
static int pxamci_get_ro(struct mmc_host *mmc)
{
	struct pxamci_host *host = mmc_priv(mmc);

450 451 452 453 454 455
	if (host->pdata && gpio_is_valid(host->pdata->gpio_card_ro)) {
		if (host->pdata->gpio_card_ro_invert)
			return !gpio_get_value(host->pdata->gpio_card_ro);
		else
			return gpio_get_value(host->pdata->gpio_card_ro);
	}
456
	if (host->pdata && host->pdata->get_ro)
457 458 459 460 461 462
		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;
463 464
}

L
Linus Torvalds 已提交
465 466 467 468 469
static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
	struct pxamci_host *host = mmc_priv(mmc);

	if (ios->clock) {
470 471 472
		unsigned long rate = host->clkrate;
		unsigned int clk = rate / ios->clock;

R
Russell King 已提交
473 474 475
		if (host->clkrt == CLKRT_OFF)
			clk_enable(host->clk);

476
		if (ios->clock == 26000000) {
477
			/* to support 26MHz */
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
			host->clkrt = 7;
		} else {
			/* to handle (19.5MHz, 26MHz) */
			if (!clk)
				clk = 1;

			/*
			 * clk might result in a lower divisor than we
			 * desire.  check for that condition and adjust
			 * as appropriate.
			 */
			if (rate / clk > ios->clock)
				clk <<= 1;
			host->clkrt = fls(clk) - 1;
		}
L
Linus Torvalds 已提交
493 494 495 496 497 498

		/*
		 * we write clkrt on the next command
		 */
	} else {
		pxamci_stop_clock(host);
R
Russell King 已提交
499 500 501 502
		if (host->clkrt != CLKRT_OFF) {
			host->clkrt = CLKRT_OFF;
			clk_disable(host->clk);
		}
L
Linus Torvalds 已提交
503 504 505
	}

	if (host->power_mode != ios->power_mode) {
506 507
		int ret;

L
Linus Torvalds 已提交
508 509
		host->power_mode = ios->power_mode;

510 511 512 513 514 515 516 517 518 519 520
		ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
		if (ret) {
			dev_err(mmc_dev(mmc), "unable to set power\n");
			/*
			 * The .set_ios() function in the mmc_host_ops
			 * struct return void, and failing to set the
			 * power should be rare so we print an error and
			 * return here.
			 */
			return;
		}
L
Linus Torvalds 已提交
521 522 523 524 525

		if (ios->power_mode == MMC_POWER_ON)
			host->cmdat |= CMDAT_INIT;
	}

526 527 528 529 530
	if (ios->bus_width == MMC_BUS_WIDTH_4)
		host->cmdat |= CMDAT_SD_4DAT;
	else
		host->cmdat &= ~CMDAT_SD_4DAT;

531 532
	dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
		host->clkrt, host->cmdat);
L
Linus Torvalds 已提交
533 534
}

535 536 537 538 539 540 541 542 543 544
static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
{
	struct pxamci_host *pxa_host = mmc_priv(host);

	if (enable)
		pxamci_enable_irq(pxa_host, SDIO_INT);
	else
		pxamci_disable_irq(pxa_host, SDIO_INT);
}

545
static const struct mmc_host_ops pxamci_ops = {
546 547 548 549
	.request		= pxamci_request,
	.get_ro			= pxamci_get_ro,
	.set_ios		= pxamci_set_ios,
	.enable_sdio_irq	= pxamci_enable_sdio_irq,
L
Linus Torvalds 已提交
550 551
};

552
static void pxamci_dma_irq(int dma, void *devid)
L
Linus Torvalds 已提交
553
{
554 555 556 557 558 559 560 561 562 563 564 565
	struct pxamci_host *host = devid;
	int dcsr = DCSR(dma);
	DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;

	if (dcsr & DCSR_ENDINTR) {
		writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
	} else {
		printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
		       mmc_hostname(host->mmc), dma, dcsr);
		host->data->error = -EIO;
		pxamci_data_done(host, 0);
	}
L
Linus Torvalds 已提交
566 567
}

568
static irqreturn_t pxamci_detect_irq(int irq, void *devid)
L
Linus Torvalds 已提交
569
{
570 571
	struct pxamci_host *host = mmc_priv(devid);

572
	mmc_detect_change(devid, msecs_to_jiffies(host->pdata->detect_delay_ms));
L
Linus Torvalds 已提交
573 574 575
	return IRQ_HANDLED;
}

576
static int pxamci_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
577 578 579
{
	struct mmc_host *mmc;
	struct pxamci_host *host = NULL;
580
	struct resource *r, *dmarx, *dmatx;
581
	int ret, irq, gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
L
Linus Torvalds 已提交
582 583 584

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	irq = platform_get_irq(pdev, 0);
585
	if (!r || irq < 0)
L
Linus Torvalds 已提交
586 587 588 589 590 591
		return -ENXIO;

	r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
	if (!r)
		return -EBUSY;

592
	mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603
	if (!mmc) {
		ret = -ENOMEM;
		goto out;
	}

	mmc->ops = &pxamci_ops;

	/*
	 * We can do SG-DMA, but we don't because we never know how much
	 * data we successfully wrote to the card.
	 */
604
	mmc->max_segs = NR_SG;
L
Linus Torvalds 已提交
605 606 607 608 609 610

	/*
	 * Our hardware DMA can handle a maximum of one page per SG entry.
	 */
	mmc->max_seg_size = PAGE_SIZE;

611
	/*
612
	 * Block length register is only 10 bits before PXA27x.
613
	 */
614
	mmc->max_blk_size = cpu_is_pxa25x() ? 1023 : 2048;
615

616 617 618 619 620
	/*
	 * Block count register is 16 bits.
	 */
	mmc->max_blk_count = 65535;

L
Linus Torvalds 已提交
621 622 623 624
	host = mmc_priv(mmc);
	host->mmc = mmc;
	host->dma = -1;
	host->pdata = pdev->dev.platform_data;
R
Russell King 已提交
625
	host->clkrt = CLKRT_OFF;
626

627
	host->clk = clk_get(&pdev->dev, NULL);
628 629 630 631 632 633 634 635 636 637 638 639
	if (IS_ERR(host->clk)) {
		ret = PTR_ERR(host->clk);
		host->clk = NULL;
		goto out;
	}

	host->clkrate = clk_get_rate(host->clk);

	/*
	 * Calculate minimum clock rate, rounding up.
	 */
	mmc->f_min = (host->clkrate + 63) / 64;
640
	mmc->f_max = (mmc_has_26MHz()) ? 26000000 : host->clkrate;
641

D
Daniel Ribeiro 已提交
642 643
	pxamci_init_ocr(host);

644
	mmc->caps = 0;
645
	host->cmdat = 0;
646
	if (!cpu_is_pxa25x()) {
647 648
		mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
		host->cmdat |= CMDAT_SDIO_INT_EN;
649
		if (mmc_has_26MHz())
650 651
			mmc->caps |= MMC_CAP_MMC_HIGHSPEED |
				     MMC_CAP_SD_HIGHSPEED;
652
	}
L
Linus Torvalds 已提交
653

654
	host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
	if (!host->sg_cpu) {
		ret = -ENOMEM;
		goto out;
	}

	spin_lock_init(&host->lock);
	host->res = r;
	host->irq = irq;
	host->imask = MMC_I_MASK_ALL;

	host->base = ioremap(r->start, SZ_4K);
	if (!host->base) {
		ret = -ENOMEM;
		goto out;
	}

	/*
	 * Ensure that the host controller is shut down, and setup
	 * with our defaults.
	 */
	pxamci_stop_clock(host);
	writel(0, host->base + MMC_SPI);
	writel(64, host->base + MMC_RESTO);
	writel(host->imask, host->base + MMC_I_MASK);

	host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
				    pxamci_dma_irq, host);
	if (host->dma < 0) {
		ret = -EBUSY;
		goto out;
	}

	ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
	if (ret)
		goto out;

691
	platform_set_drvdata(pdev, mmc);
L
Linus Torvalds 已提交
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706
	dmarx = platform_get_resource(pdev, IORESOURCE_DMA, 0);
	if (!dmarx) {
		ret = -ENXIO;
		goto out;
	}
	host->dma_drcmrrx = dmarx->start;

	dmatx = platform_get_resource(pdev, IORESOURCE_DMA, 1);
	if (!dmatx) {
		ret = -ENXIO;
		goto out;
	}
	host->dma_drcmrtx = dmatx->start;

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
	if (host->pdata) {
		gpio_cd = host->pdata->gpio_card_detect;
		gpio_ro = host->pdata->gpio_card_ro;
		gpio_power = host->pdata->gpio_power;
	}
	if (gpio_is_valid(gpio_power)) {
		ret = gpio_request(gpio_power, "mmc card power");
		if (ret) {
			dev_err(&pdev->dev, "Failed requesting gpio_power %d\n", gpio_power);
			goto out;
		}
		gpio_direction_output(gpio_power,
				      host->pdata->gpio_power_invert);
	}
	if (gpio_is_valid(gpio_ro)) {
		ret = gpio_request(gpio_ro, "mmc card read only");
		if (ret) {
724
			dev_err(&pdev->dev, "Failed requesting gpio_ro %d\n", gpio_ro);
725 726 727 728 729 730 731
			goto err_gpio_ro;
		}
		gpio_direction_input(gpio_ro);
	}
	if (gpio_is_valid(gpio_cd)) {
		ret = gpio_request(gpio_cd, "mmc card detect");
		if (ret) {
732
			dev_err(&pdev->dev, "Failed requesting gpio_cd %d\n", gpio_cd);
733 734 735 736 737 738 739 740 741 742 743 744 745
			goto err_gpio_cd;
		}
		gpio_direction_input(gpio_cd);

		ret = request_irq(gpio_to_irq(gpio_cd), pxamci_detect_irq,
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				  "mmc card detect", mmc);
		if (ret) {
			dev_err(&pdev->dev, "failed to request card detect IRQ\n");
			goto err_request_irq;
		}
	}

L
Linus Torvalds 已提交
746
	if (host->pdata && host->pdata->init)
747
		host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
L
Linus Torvalds 已提交
748

749 750 751 752 753
	if (gpio_is_valid(gpio_power) && host->pdata->setpower)
		dev_warn(&pdev->dev, "gpio_power and setpower() both defined\n");
	if (gpio_is_valid(gpio_ro) && host->pdata->get_ro)
		dev_warn(&pdev->dev, "gpio_ro and get_ro() both defined\n");

L
Linus Torvalds 已提交
754 755 756 757
	mmc_add_host(mmc);

	return 0;

758 759 760 761 762 763
err_request_irq:
	gpio_free(gpio_cd);
err_gpio_cd:
	gpio_free(gpio_ro);
err_gpio_ro:
	gpio_free(gpio_power);
L
Linus Torvalds 已提交
764 765 766 767 768 769 770
 out:
	if (host) {
		if (host->dma >= 0)
			pxa_free_dma(host->dma);
		if (host->base)
			iounmap(host->base);
		if (host->sg_cpu)
771
			dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
772 773
		if (host->clk)
			clk_put(host->clk);
L
Linus Torvalds 已提交
774 775 776 777 778 779 780
	}
	if (mmc)
		mmc_free_host(mmc);
	release_resource(r);
	return ret;
}

781
static int pxamci_remove(struct platform_device *pdev)
L
Linus Torvalds 已提交
782
{
783
	struct mmc_host *mmc = platform_get_drvdata(pdev);
784
	int gpio_cd = -1, gpio_ro = -1, gpio_power = -1;
L
Linus Torvalds 已提交
785

786
	platform_set_drvdata(pdev, NULL);
L
Linus Torvalds 已提交
787 788 789 790

	if (mmc) {
		struct pxamci_host *host = mmc_priv(mmc);

791 792
		mmc_remove_host(mmc);

793 794 795 796 797 798 799 800 801 802 803 804 805
		if (host->pdata) {
			gpio_cd = host->pdata->gpio_card_detect;
			gpio_ro = host->pdata->gpio_card_ro;
			gpio_power = host->pdata->gpio_power;
		}
		if (gpio_is_valid(gpio_cd)) {
			free_irq(gpio_to_irq(gpio_cd), mmc);
			gpio_free(gpio_cd);
		}
		if (gpio_is_valid(gpio_ro))
			gpio_free(gpio_ro);
		if (gpio_is_valid(gpio_power))
			gpio_free(gpio_power);
D
Daniel Ribeiro 已提交
806 807 808
		if (host->vcc)
			regulator_put(host->vcc);

L
Linus Torvalds 已提交
809
		if (host->pdata && host->pdata->exit)
810
			host->pdata->exit(&pdev->dev, mmc);
L
Linus Torvalds 已提交
811 812 813 814 815 816

		pxamci_stop_clock(host);
		writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
		       END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
		       host->base + MMC_I_MASK);

817 818
		DRCMR(host->dma_drcmrrx) = 0;
		DRCMR(host->dma_drcmrtx) = 0;
L
Linus Torvalds 已提交
819 820 821 822

		free_irq(host->irq, host);
		pxa_free_dma(host->dma);
		iounmap(host->base);
823
		dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
L
Linus Torvalds 已提交
824

825 826
		clk_put(host->clk);

L
Linus Torvalds 已提交
827 828 829 830 831 832 833 834
		release_resource(host->res);

		mmc_free_host(mmc);
	}
	return 0;
}

#ifdef CONFIG_PM
835
static int pxamci_suspend(struct device *dev)
L
Linus Torvalds 已提交
836
{
837
	struct mmc_host *mmc = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
838 839
	int ret = 0;

840
	if (mmc)
841
		ret = mmc_suspend_host(mmc);
L
Linus Torvalds 已提交
842 843 844 845

	return ret;
}

846
static int pxamci_resume(struct device *dev)
L
Linus Torvalds 已提交
847
{
848
	struct mmc_host *mmc = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
849 850
	int ret = 0;

851
	if (mmc)
L
Linus Torvalds 已提交
852 853 854 855
		ret = mmc_resume_host(mmc);

	return ret;
}
856

857
static const struct dev_pm_ops pxamci_pm_ops = {
858 859 860
	.suspend	= pxamci_suspend,
	.resume		= pxamci_resume,
};
L
Linus Torvalds 已提交
861 862
#endif

863
static struct platform_driver pxamci_driver = {
L
Linus Torvalds 已提交
864 865
	.probe		= pxamci_probe,
	.remove		= pxamci_remove,
866 867
	.driver		= {
		.name	= DRIVER_NAME,
868
		.owner	= THIS_MODULE,
869 870 871
#ifdef CONFIG_PM
		.pm	= &pxamci_pm_ops,
#endif
872
	},
L
Linus Torvalds 已提交
873 874 875 876
};

static int __init pxamci_init(void)
{
877
	return platform_driver_register(&pxamci_driver);
L
Linus Torvalds 已提交
878 879 880 881
}

static void __exit pxamci_exit(void)
{
882
	platform_driver_unregister(&pxamci_driver);
L
Linus Torvalds 已提交
883 884 885 886 887 888 889
}

module_init(pxamci_init);
module_exit(pxamci_exit);

MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
MODULE_LICENSE("GPL");
890
MODULE_ALIAS("platform:pxa2xx-mci");