at91_mci.c 30.4 KB
Newer Older
1
/*
P
Pierre Ossman 已提交
2
 *  linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver
3 4 5 6 7 8 9 10 11 12 13
 *
 *  Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
 *
 *  Copyright (C) 2006 Malcolm Noyes
 *
 * 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.
 */

/*
A
Andrew Victor 已提交
14
   This is the AT91 MCI driver that has been tested with both MMC cards
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
   and SD-cards.  Boards that support write protect are now supported.
   The CCAT91SBC001 board does not support SD cards.

   The three entry points are at91_mci_request, at91_mci_set_ios
   and at91_mci_get_ro.

   SET IOS
     This configures the device to put it into the correct mode and clock speed
     required.

   MCI REQUEST
     MCI request processes the commands sent in the mmc_request structure. This
     can consist of a processing command and a stop command in the case of
     multiple block transfers.

     There are three main types of request, commands, reads and writes.

     Commands are straight forward. The command is submitted to the controller and
     the request function returns. When the controller generates an interrupt to indicate
     the command is finished, the response to the command are read and the mmc_request_done
     function called to end the request.

     Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
     controller to manage the transfers.

     A read is done from the controller directly to the scatterlist passed in from the request.
A
Andrew Victor 已提交
41 42
     Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte
     swapped in the scatterlist buffers.  AT91SAM926x are not affected by this bug.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

     The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY

     A write is slightly different in that the bytes to write are read from the scatterlist
     into a dma memory buffer (this is in case the source buffer should be read only). The
     entire write buffer is then done from this single dma memory buffer.

     The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY

   GET RO
     Gets the status of the write protect pin, if available.
*/

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/dma-mapping.h>
#include <linux/clk.h>
67
#include <linux/atmel_pdc.h>
68
#include <linux/gfp.h>
69 70 71 72 73

#include <linux/mmc/host.h>

#include <asm/io.h>
#include <asm/irq.h>
74 75
#include <asm/gpio.h>

76 77 78
#include <mach/board.h>
#include <mach/cpu.h>
#include <mach/at91_mci.h>
79 80 81

#define DRIVER_NAME "at91_mci"

82 83 84 85 86 87 88 89 90 91 92
static inline int at91mci_is_mci1rev2xx(void)
{
	return (   cpu_is_at91sam9260()
		|| cpu_is_at91sam9263()
		|| cpu_is_at91cap9()
		|| cpu_is_at91sam9rl()
		|| cpu_is_at91sam9g10()
		|| cpu_is_at91sam9g20()
		);
}

93 94
#define FL_SENT_COMMAND	(1 << 0)
#define FL_SENT_STOP	(1 << 1)
95

96 97
#define AT91_MCI_ERRORS	(AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE	\
		| AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE		\
98
		| AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
99

A
Andrew Victor 已提交
100 101
#define at91_mci_read(host, reg)	__raw_readl((host)->baseaddr + (reg))
#define at91_mci_write(host, reg, val)	__raw_writel((val), (host)->baseaddr + (reg))
102

103 104 105 106
#define MCI_BLKSIZE 		512
#define MCI_MAXBLKSIZE 		4095
#define MCI_BLKATONCE 		256
#define MCI_BUFSIZE 		(MCI_BLKSIZE * MCI_BLKATONCE)
107 108 109 110 111 112 113 114 115 116

/*
 * Low level type for this driver
 */
struct at91mci_host
{
	struct mmc_host *mmc;
	struct mmc_command *cmd;
	struct mmc_request *request;

A
Andrew Victor 已提交
117
	void __iomem *baseaddr;
118
	int irq;
A
Andrew Victor 已提交
119

120 121 122
	struct at91_mmc_data *board;
	int present;

123 124
	struct clk *mci_clk;

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	/*
	 * Flag indicating when the command has been sent. This is used to
	 * work out whether or not to send the stop
	 */
	unsigned int flags;
	/* flag for current bus settings */
	u32 bus_mode;

	/* DMA buffer used for transmitting */
	unsigned int* buffer;
	dma_addr_t physical_address;
	unsigned int total_length;

	/* Latest in the scatterlist that has been enabled for transfer, but not freed */
	int in_use_index;

	/* Latest in the scatterlist that has been enabled for transfer */
	int transfer_index;
M
Marc Pignat 已提交
143 144 145

	/* Timer for timeouts */
	struct timer_list timer;
146 147
};

M
Marc Pignat 已提交
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 176 177 178 179 180 181 182 183 184
/*
 * Reset the controller and restore most of the state
 */
static void at91_reset_host(struct at91mci_host *host)
{
	unsigned long flags;
	u32 mr;
	u32 sdcr;
	u32 dtor;
	u32 imr;

	local_irq_save(flags);
	imr = at91_mci_read(host, AT91_MCI_IMR);

	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);

	/* save current state */
	mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
	sdcr = at91_mci_read(host, AT91_MCI_SDCR);
	dtor = at91_mci_read(host, AT91_MCI_DTOR);

	/* reset the controller */
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);

	/* restore state */
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
	at91_mci_write(host, AT91_MCI_MR, mr);
	at91_mci_write(host, AT91_MCI_SDCR, sdcr);
	at91_mci_write(host, AT91_MCI_DTOR, dtor);
	at91_mci_write(host, AT91_MCI_IER, imr);

	/* make sure sdio interrupts will fire */
	at91_mci_read(host, AT91_MCI_SR);

	local_irq_restore(flags);
}

M
Marc Pignat 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static void at91_timeout_timer(unsigned long data)
{
	struct at91mci_host *host;

	host = (struct at91mci_host *)data;

	if (host->request) {
		dev_err(host->mmc->parent, "Timeout waiting end of packet\n");

		if (host->cmd && host->cmd->data) {
			host->cmd->data->error = -ETIMEDOUT;
		} else {
			if (host->cmd)
				host->cmd->error = -ETIMEDOUT;
			else
				host->request->cmd->error = -ETIMEDOUT;
		}

M
Marc Pignat 已提交
203
		at91_reset_host(host);
M
Marc Pignat 已提交
204 205 206 207
		mmc_request_done(host->mmc, host->request);
	}
}

208 209 210
/*
 * Copy from sg to a dma block - used for transfers
 */
N
Nicolas Ferre 已提交
211
static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
212 213 214 215
{
	unsigned int len, i, size;
	unsigned *dmabuf = host->buffer;

216
	size = data->blksz * data->blocks;
217 218
	len = data->sg_len;

219 220
	/* MCI1 rev2xx Data Write Operation and number of bytes erratum */
	if (at91mci_is_mci1rev2xx())
221 222 223
		if (host->total_length == 12)
			memset(dmabuf, 0, 12);

224 225 226 227 228 229 230 231 232 233 234 235
	/*
	 * Just loop through all entries. Size might not
	 * be the entire list though so make sure that
	 * we do not transfer too much.
	 */
	for (i = 0; i < len; i++) {
		struct scatterlist *sg;
		int amount;
		unsigned int *sgbuffer;

		sg = &data->sg[i];

J
Jens Axboe 已提交
236
		sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
237 238 239
		amount = min(size, sg->length);
		size -= amount;

A
Andrew Victor 已提交
240 241 242 243 244
		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */
			int index;

			for (index = 0; index < (amount / 4); index++)
				*dmabuf++ = swab32(sgbuffer[index]);
245
		} else {
246 247 248 249
			char *tmpv = (char *)dmabuf;
			memcpy(tmpv, sgbuffer, amount);
			tmpv += amount;
			dmabuf = (unsigned *)tmpv;
250
		}
251

252
		kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

		if (size == 0)
			break;
	}

	/*
	 * Check that we didn't get a request to transfer
	 * more data than can fit into the SG list.
	 */
	BUG_ON(size != 0);
}

/*
 * Handle after a dma read
 */
N
Nicolas Ferre 已提交
268
static void at91_mci_post_dma_read(struct at91mci_host *host)
269 270 271
{
	struct mmc_command *cmd;
	struct mmc_data *data;
272 273
	unsigned int len, i, size;
	unsigned *dmabuf = host->buffer;
274

275
	pr_debug("post dma read\n");
276 277 278

	cmd = host->cmd;
	if (!cmd) {
279
		pr_debug("no command\n");
280 281 282 283 284
		return;
	}

	data = cmd->data;
	if (!data) {
285
		pr_debug("no data\n");
286 287 288
		return;
	}

289 290
	size = data->blksz * data->blocks;
	len = data->sg_len;
291

292 293
	at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
	at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
294

295 296 297 298
	for (i = 0; i < len; i++) {
		struct scatterlist *sg;
		int amount;
		unsigned int *sgbuffer;
299

300
		sg = &data->sg[i];
301

302 303 304
		sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
		amount = min(size, sg->length);
		size -= amount;
305

A
Andrew Victor 已提交
306 307
		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */
			int index;
308 309 310 311 312 313 314
			for (index = 0; index < (amount / 4); index++)
				sgbuffer[index] = swab32(*dmabuf++);
		} else {
			char *tmpv = (char *)dmabuf;
			memcpy(sgbuffer, tmpv, amount);
			tmpv += amount;
			dmabuf = (unsigned *)tmpv;
315
		}
A
Andrew Victor 已提交
316

317
		flush_kernel_dcache_page(sg_page(sg));
318
		kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
319 320 321
		data->bytes_xfered += amount;
		if (size == 0)
			break;
322 323
	}

324
	pr_debug("post dma read done\n");
325 326 327 328 329 330 331 332 333 334
}

/*
 * Handle transmitted data
 */
static void at91_mci_handle_transmitted(struct at91mci_host *host)
{
	struct mmc_command *cmd;
	struct mmc_data *data;

335
	pr_debug("Handling the transmit\n");
336 337

	/* Disable the transfer */
338
	at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
339 340

	/* Now wait for cmd ready */
A
Andrew Victor 已提交
341
	at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
342 343 344 345 346 347 348

	cmd = host->cmd;
	if (!cmd) return;

	data = cmd->data;
	if (!data) return;

P
Pierre Ossman 已提交
349
	if (cmd->data->blocks > 1) {
350 351 352 353
		pr_debug("multiple write : wait for BLKE...\n");
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
	} else
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
354 355 356 357 358 359 360 361
}

/*
 * Update bytes tranfered count during a write operation
 */
static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
{
	struct mmc_data *data;
362

363 364 365 366 367 368 369 370 371 372 373
	/* always deal with the effective request (and not the current cmd) */

	if (host->request->cmd && host->request->cmd->error != 0)
		return;

	if (host->request->data) {
		data = host->request->data;
		if (data->flags & MMC_DATA_WRITE) {
			/* card is in IDLE mode now */
			pr_debug("-> bytes_xfered %d, total_length = %d\n",
				data->bytes_xfered, host->total_length);
374
			data->bytes_xfered = data->blksz * data->blocks;
375 376
		}
	}
377 378
}

379

380 381 382 383 384 385 386 387 388 389 390 391
/*Handle after command sent ready*/
static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
{
	if (!host->cmd)
		return 1;
	else if (!host->cmd->data) {
		if (host->flags & FL_SENT_STOP) {
			/*After multi block write, we must wait for NOTBUSY*/
			at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
		} else return 1;
	} else if (host->cmd->data->flags & MMC_DATA_WRITE) {
		/*After sendding multi-block-write command, start DMA transfer*/
392
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
393 394 395 396 397 398 399 400
		at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
	}

	/* command not completed, have to wait */
	return 0;
}


401 402 403
/*
 * Enable the controller
 */
A
Andrew Victor 已提交
404
static void at91_mci_enable(struct at91mci_host *host)
405
{
406 407
	unsigned int mr;

A
Andrew Victor 已提交
408
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
A
Andrew Victor 已提交
409
	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
A
Andrew Victor 已提交
410
	at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
411 412
	mr = AT91_MCI_PDCMODE | 0x34a;

413
	if (at91mci_is_mci1rev2xx())
414 415 416
		mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;

	at91_mci_write(host, AT91_MCI_MR, mr);
A
Andrew Victor 已提交
417 418 419

	/* use Slot A or B (only one at same time) */
	at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
420 421 422 423 424
}

/*
 * Disable the controller
 */
A
Andrew Victor 已提交
425
static void at91_mci_disable(struct at91mci_host *host)
426
{
A
Andrew Victor 已提交
427
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
428 429 430 431 432
}

/*
 * Send a command
 */
433
static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
434 435 436 437 438 439 440 441 442 443
{
	unsigned int cmdr, mr;
	unsigned int block_length;
	struct mmc_data *data = cmd->data;

	unsigned int blocks;
	unsigned int ier = 0;

	host->cmd = cmd;

444
	/* Needed for leaving busy state before CMD1 */
A
Andrew Victor 已提交
445
	if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
446
		pr_debug("Clearing timeout\n");
A
Andrew Victor 已提交
447 448 449
		at91_mci_write(host, AT91_MCI_ARGR, 0);
		at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
		while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
450
			/* spin */
A
Andrew Victor 已提交
451
			pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
452 453
		}
	}
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	cmdr = cmd->opcode;

	if (mmc_resp_type(cmd) == MMC_RSP_NONE)
		cmdr |= AT91_MCI_RSPTYP_NONE;
	else {
		/* if a response is expected then allow maximum response latancy */
		cmdr |= AT91_MCI_MAXLAT;
		/* set 136 bit response for R2, 48 bit response otherwise */
		if (mmc_resp_type(cmd) == MMC_RSP_R2)
			cmdr |= AT91_MCI_RSPTYP_136;
		else
			cmdr |= AT91_MCI_RSPTYP_48;
	}

	if (data) {
470

471 472 473 474 475 476 477 478 479 480 481 482 483
		if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
			if (data->blksz & 0x3) {
				pr_debug("Unsupported block size\n");
				cmd->error = -EINVAL;
				mmc_request_done(host->mmc, host->request);
				return;
			}
			if (data->flags & MMC_DATA_STREAM) {
				pr_debug("Stream commands not supported\n");
				cmd->error = -EINVAL;
				mmc_request_done(host->mmc, host->request);
				return;
			}
484 485
		}

486
		block_length = data->blksz;
487 488 489 490 491 492 493 494 495 496
		blocks = data->blocks;

		/* always set data start - also set direction flag for read */
		if (data->flags & MMC_DATA_READ)
			cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
		else if (data->flags & MMC_DATA_WRITE)
			cmdr |= AT91_MCI_TRCMD_START;

		if (data->flags & MMC_DATA_STREAM)
			cmdr |= AT91_MCI_TRTYP_STREAM;
P
Pierre Ossman 已提交
497
		if (data->blocks > 1)
498 499 500 501 502 503 504
			cmdr |= AT91_MCI_TRTYP_MULTIPLE;
	}
	else {
		block_length = 0;
		blocks = 0;
	}

505
	if (host->flags & FL_SENT_STOP)
506 507 508 509 510 511 512 513
		cmdr |= AT91_MCI_TRCMD_STOP;

	if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
		cmdr |= AT91_MCI_OPDCMD;

	/*
	 * Set the arguments and send the command
	 */
A
Andrew Victor 已提交
514
	pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
A
Andrew Victor 已提交
515
		cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
516 517

	if (!data) {
518 519 520 521 522 523 524 525 526
		at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
		at91_mci_write(host, ATMEL_PDC_RPR, 0);
		at91_mci_write(host, ATMEL_PDC_RCR, 0);
		at91_mci_write(host, ATMEL_PDC_RNPR, 0);
		at91_mci_write(host, ATMEL_PDC_RNCR, 0);
		at91_mci_write(host, ATMEL_PDC_TPR, 0);
		at91_mci_write(host, ATMEL_PDC_TCR, 0);
		at91_mci_write(host, ATMEL_PDC_TNPR, 0);
		at91_mci_write(host, ATMEL_PDC_TNCR, 0);
527 528 529
		ier = AT91_MCI_CMDRDY;
	} else {
		/* zero block length and PDC mode */
530
		mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
531 532 533 534
		mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
		mr |= (block_length << 16);
		mr |= AT91_MCI_PDCMODE;
		at91_mci_write(host, AT91_MCI_MR, mr);
A
Andrew Victor 已提交
535

536
		if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
M
Marc Pignat 已提交
537 538 539 540
			at91_mci_write(host, AT91_MCI_BLKR,
				AT91_MCI_BLKR_BCNT(blocks) |
				AT91_MCI_BLKR_BLKLEN(block_length));

541 542 543 544
		/*
		 * Disable the PDC controller
		 */
		at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
545

546 547 548 549 550 551 552 553 554 555
		if (cmdr & AT91_MCI_TRCMD_START) {
			data->bytes_xfered = 0;
			host->transfer_index = 0;
			host->in_use_index = 0;
			if (cmdr & AT91_MCI_TRDIR) {
				/*
				 * Handle a read
				 */
				host->total_length = 0;

556 557 558 559 560 561
				at91_mci_write(host, ATMEL_PDC_RPR, host->physical_address);
				at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ?
					(blocks * block_length) : (blocks * block_length) / 4);
				at91_mci_write(host, ATMEL_PDC_RNPR, 0);
				at91_mci_write(host, ATMEL_PDC_RNCR, 0);

562 563 564 565 566 567 568
				ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
			}
			else {
				/*
				 * Handle a write
				 */
				host->total_length = block_length * blocks;
569
				/*
570
				 * MCI1 rev2xx Data Write Operation and
571 572
				 * number of bytes erratum
				 */
573
				if (at91mci_is_mci1rev2xx())
574 575
					if (host->total_length < 12)
						host->total_length = 12;
576

577 578 579 580 581
				at91_mci_sg_to_dma(host, data);

				pr_debug("Transmitting %d bytes\n", host->total_length);

				at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
582 583 584
				at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
						host->total_length : host->total_length / 4);

585 586
				ier = AT91_MCI_CMDRDY;
			}
587 588 589 590 591 592 593 594
		}
	}

	/*
	 * Send the command and then enable the PDC - not the other way round as
	 * the data sheet says
	 */

A
Andrew Victor 已提交
595 596
	at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
	at91_mci_write(host, AT91_MCI_CMDR, cmdr);
597 598 599

	if (cmdr & AT91_MCI_TRCMD_START) {
		if (cmdr & AT91_MCI_TRDIR)
600
			at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
601 602
	}

603
	/* Enable selected interrupts */
604
	at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
605 606 607 608 609
}

/*
 * Process the next step in the request
 */
N
Nicolas Ferre 已提交
610
static void at91_mci_process_next(struct at91mci_host *host)
611 612 613
{
	if (!(host->flags & FL_SENT_COMMAND)) {
		host->flags |= FL_SENT_COMMAND;
614
		at91_mci_send_command(host, host->request->cmd);
615 616 617
	}
	else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
		host->flags |= FL_SENT_STOP;
618
		at91_mci_send_command(host, host->request->stop);
M
Marc Pignat 已提交
619 620
	} else {
		del_timer(&host->timer);
M
Marc Pignat 已提交
621 622 623 624 625
		/* the at91rm9200 mci controller hangs after some transfers,
		 * and the workaround is to reset it after each transfer.
		 */
		if (cpu_is_at91rm9200())
			at91_reset_host(host);
626
		mmc_request_done(host->mmc, host->request);
M
Marc Pignat 已提交
627
	}
628 629 630 631 632
}

/*
 * Handle a command that has been completed
 */
633
static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
634 635
{
	struct mmc_command *cmd = host->cmd;
636
	struct mmc_data *data = cmd->data;
637

638
	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
639

A
Andrew Victor 已提交
640 641 642 643
	cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
	cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
	cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
	cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
644

645 646 647
	pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
		 status, at91_mci_read(host, AT91_MCI_SR),
		 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
648

649
	if (status & AT91_MCI_ERRORS) {
650
		if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
P
Pierre Ossman 已提交
651
			cmd->error = 0;
652 653
		}
		else {
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
			if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
				if (data) {
					if (status & AT91_MCI_DTOE)
						data->error = -ETIMEDOUT;
					else if (status & AT91_MCI_DCRCE)
						data->error = -EILSEQ;
				}
			} else {
				if (status & AT91_MCI_RTOE)
					cmd->error = -ETIMEDOUT;
				else if (status & AT91_MCI_RCRCE)
					cmd->error = -EILSEQ;
				else
					cmd->error = -EIO;
			}
669

670 671 672
			pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
				cmd->error, data ? data->error : 0,
				 cmd->opcode, cmd->retries);
673 674 675
		}
	}
	else
P
Pierre Ossman 已提交
676
		cmd->error = 0;
677

N
Nicolas Ferre 已提交
678
	at91_mci_process_next(host);
679 680 681 682 683 684 685 686 687 688 689
}

/*
 * Handle an MMC request
 */
static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
	struct at91mci_host *host = mmc_priv(mmc);
	host->request = mrq;
	host->flags = 0;

690 691
	/* more than 1s timeout needed with slow SD cards */
	mod_timer(&host->timer, jiffies +  msecs_to_jiffies(2000));
M
Marc Pignat 已提交
692

N
Nicolas Ferre 已提交
693
	at91_mci_process_next(host);
694 695 696 697 698 699 700 701 702
}

/*
 * Set the IOS
 */
static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{
	int clkdiv;
	struct at91mci_host *host = mmc_priv(mmc);
703
	unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
704

705
	host->bus_mode = ios->bus_mode;
706 707 708

	if (ios->clock == 0) {
		/* Disable the MCI controller */
A
Andrew Victor 已提交
709
		at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
710 711 712 713
		clkdiv = 0;
	}
	else {
		/* Enable the MCI controller */
A
Andrew Victor 已提交
714
		at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
715 716 717 718 719 720

		if ((at91_master_clock % (ios->clock * 2)) == 0)
			clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
		else
			clkdiv = (at91_master_clock / ios->clock) / 2;

721
		pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
722 723 724
			at91_master_clock / (2 * (clkdiv + 1)));
	}
	if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
725
		pr_debug("MMC: Setting controller bus width to 4\n");
A
Andrew Victor 已提交
726
		at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
727 728
	}
	else {
729
		pr_debug("MMC: Setting controller bus width to 1\n");
A
Andrew Victor 已提交
730
		at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
731 732 733
	}

	/* Set the clock divider */
A
Andrew Victor 已提交
734
	at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
735 736

	/* maybe switch power to the card */
737
	if (host->board->vcc_pin) {
738 739
		switch (ios->power_mode) {
			case MMC_POWER_OFF:
740
				gpio_set_value(host->board->vcc_pin, 0);
741 742
				break;
			case MMC_POWER_UP:
743
				gpio_set_value(host->board->vcc_pin, 1);
744
				break;
M
Marc Pignat 已提交
745 746 747 748
			case MMC_POWER_ON:
				break;
			default:
				WARN_ON(1);
749 750 751 752 753 754 755
		}
	}
}

/*
 * Handle an interrupt
 */
756
static irqreturn_t at91_mci_irq(int irq, void *devid)
757 758 759
{
	struct at91mci_host *host = devid;
	int completed = 0;
760
	unsigned int int_status, int_mask;
761

A
Andrew Victor 已提交
762
	int_status = at91_mci_read(host, AT91_MCI_SR);
763
	int_mask = at91_mci_read(host, AT91_MCI_IMR);
764

A
Andrew Victor 已提交
765
	pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
766
		int_status & int_mask);
767

768 769 770
	int_status = int_status & int_mask;

	if (int_status & AT91_MCI_ERRORS) {
771
		completed = 1;
772

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
		if (int_status & AT91_MCI_UNRE)
			pr_debug("MMC: Underrun error\n");
		if (int_status & AT91_MCI_OVRE)
			pr_debug("MMC: Overrun error\n");
		if (int_status & AT91_MCI_DTOE)
			pr_debug("MMC: Data timeout\n");
		if (int_status & AT91_MCI_DCRCE)
			pr_debug("MMC: CRC error in data\n");
		if (int_status & AT91_MCI_RTOE)
			pr_debug("MMC: Response timeout\n");
		if (int_status & AT91_MCI_RENDE)
			pr_debug("MMC: Response end bit error\n");
		if (int_status & AT91_MCI_RCRCE)
			pr_debug("MMC: Response CRC error\n");
		if (int_status & AT91_MCI_RDIRE)
			pr_debug("MMC: Response direction error\n");
		if (int_status & AT91_MCI_RINDE)
			pr_debug("MMC: Response index error\n");
	} else {
		/* Only continue processing if no errors */
793 794

		if (int_status & AT91_MCI_TXBUFE) {
795
			pr_debug("TX buffer empty\n");
796 797 798
			at91_mci_handle_transmitted(host);
		}

799 800 801 802 803
		if (int_status & AT91_MCI_ENDRX) {
			pr_debug("ENDRX\n");
			at91_mci_post_dma_read(host);
		}

804
		if (int_status & AT91_MCI_RXBUFF) {
805
			pr_debug("RX buffer full\n");
806 807 808
			at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
			at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
			completed = 1;
809 810
		}

811
		if (int_status & AT91_MCI_ENDTX)
812
			pr_debug("Transmit has ended\n");
813 814

		if (int_status & AT91_MCI_NOTBUSY) {
815
			pr_debug("Card is ready\n");
816
			at91_mci_update_bytes_xfered(host);
817
			completed = 1;
818 819
		}

820
		if (int_status & AT91_MCI_DTIP)
821
			pr_debug("Data transfer in progress\n");
822

823
		if (int_status & AT91_MCI_BLKE) {
824
			pr_debug("Block transfer has ended\n");
825 826 827 828 829 830 831
			if (host->request->data && host->request->data->blocks > 1) {
				/* multi block write : complete multi write
				 * command and send stop */
				completed = 1;
			} else {
				at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
			}
832
		}
833

834 835 836 837 838 839
		if (int_status & AT91_MCI_SDIOIRQA)
			mmc_signal_sdio_irq(host->mmc);

		if (int_status & AT91_MCI_SDIOIRQB)
			mmc_signal_sdio_irq(host->mmc);

840
		if (int_status & AT91_MCI_TXRDY)
841
			pr_debug("Ready to transmit\n");
842

843
		if (int_status & AT91_MCI_RXRDY)
844
			pr_debug("Ready to receive\n");
845 846

		if (int_status & AT91_MCI_CMDRDY) {
847
			pr_debug("Command ready\n");
848
			completed = at91_mci_handle_cmdrdy(host);
849 850 851 852
		}
	}

	if (completed) {
853
		pr_debug("Completed command\n");
854
		at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
855
		at91_mci_completed_command(host, int_status);
856
	} else
857
		at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
858 859 860 861

	return IRQ_HANDLED;
}

862
static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
863 864
{
	struct at91mci_host *host = _host;
865
	int present = !gpio_get_value(irq_to_gpio(irq));
866 867 868 869 870 871 872

	/*
	 * we expect this irq on both insert and remove,
	 * and use a short delay to debounce.
	 */
	if (present != host->present) {
		host->present = present;
873
		pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
874 875
			present ? "insert" : "remove");
		if (!present) {
876
			pr_debug("****** Resetting SD-card bus width ******\n");
A
Andrew Victor 已提交
877
			at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
878
		}
879 880
		/* 0.5s needed because of early card detect switch firing */
		mmc_detect_change(host->mmc, msecs_to_jiffies(500));
881 882 883 884
	}
	return IRQ_HANDLED;
}

D
David Brownell 已提交
885
static int at91_mci_get_ro(struct mmc_host *mmc)
886 887 888
{
	struct at91mci_host *host = mmc_priv(mmc);

889 890 891 892 893 894 895
	if (host->board->wp_pin)
		return !!gpio_get_value(host->board->wp_pin);
	/*
	 * Board doesn't support read only detection; let the mmc core
	 * decide what to do.
	 */
	return -ENOSYS;
896 897
}

898 899 900 901 902 903 904 905 906 907 908
static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
{
	struct at91mci_host *host = mmc_priv(mmc);

	pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
		host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
	at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
		host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);

}

909
static const struct mmc_host_ops at91_mci_ops = {
910 911 912
	.request	= at91_mci_request,
	.set_ios	= at91_mci_set_ios,
	.get_ro		= at91_mci_get_ro,
913
	.enable_sdio_irq = at91_mci_enable_sdio_irq,
914 915 916 917 918
};

/*
 * Probe for the device
 */
D
David Brownell 已提交
919
static int __init at91_mci_probe(struct platform_device *pdev)
920 921 922
{
	struct mmc_host *mmc;
	struct at91mci_host *host;
923
	struct resource *res;
924 925
	int ret;

926 927 928 929 930 931 932
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENXIO;

	if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
		return -EBUSY;

933 934
	mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
	if (!mmc) {
935 936 937
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
		goto fail6;
938 939 940 941 942 943
	}

	mmc->ops = &at91_mci_ops;
	mmc->f_min = 375000;
	mmc->f_max = 25000000;
	mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
944
	mmc->caps = 0;
945

946 947 948
	mmc->max_blk_size  = MCI_MAXBLKSIZE;
	mmc->max_blk_count = MCI_BLKATONCE;
	mmc->max_req_size  = MCI_BUFSIZE;
949 950 951
	mmc->max_phys_segs = MCI_BLKATONCE;
	mmc->max_hw_segs   = MCI_BLKATONCE;
	mmc->max_seg_size  = MCI_BUFSIZE;
952

953 954 955 956 957
	host = mmc_priv(mmc);
	host->mmc = mmc;
	host->bus_mode = 0;
	host->board = pdev->dev.platform_data;
	if (host->board->wire4) {
958
		if (at91mci_is_mci1rev2xx())
959 960
			mmc->caps |= MMC_CAP_4_BIT_DATA;
		else
961
			dev_warn(&pdev->dev, "4 wire bus mode not supported"
962
				" - using 1 wire\n");
963 964
	}

965 966 967 968 969 970 971 972
	host->buffer = dma_alloc_coherent(&pdev->dev, MCI_BUFSIZE,
					&host->physical_address, GFP_KERNEL);
	if (!host->buffer) {
		ret = -ENOMEM;
		dev_err(&pdev->dev, "Can't allocate transmit buffer\n");
		goto fail5;
	}

973
	/* Add SDIO capability when available */
974 975
	if (at91mci_is_mci1rev2xx()) {
		/* at91mci MCI1 rev2xx sdio interrupt erratum */
976 977 978 979
		if (host->board->wire4 || !host->board->slot_b)
			mmc->caps |= MMC_CAP_SDIO_IRQ;
	}

980 981 982 983 984 985 986 987
	/*
	 * Reserve GPIOs ... board init code makes sure these pins are set
	 * up as GPIOs with the right direction (input, except for vcc)
	 */
	if (host->board->det_pin) {
		ret = gpio_request(host->board->det_pin, "mmc_detect");
		if (ret < 0) {
			dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
988
			goto fail4b;
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
		}
	}
	if (host->board->wp_pin) {
		ret = gpio_request(host->board->wp_pin, "mmc_wp");
		if (ret < 0) {
			dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
			goto fail4;
		}
	}
	if (host->board->vcc_pin) {
		ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
		if (ret < 0) {
			dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
			goto fail3;
		}
	}

1006 1007 1008
	/*
	 * Get Clock
	 */
1009 1010
	host->mci_clk = clk_get(&pdev->dev, "mci_clk");
	if (IS_ERR(host->mci_clk)) {
1011 1012 1013
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "no mci_clk?\n");
		goto fail2;
1014 1015
	}

1016 1017 1018 1019 1020
	/*
	 * Map I/O region
	 */
	host->baseaddr = ioremap(res->start, res->end - res->start + 1);
	if (!host->baseaddr) {
1021 1022
		ret = -ENOMEM;
		goto fail1;
1023
	}
A
Andrew Victor 已提交
1024 1025 1026 1027

	/*
	 * Reset hardware
	 */
1028
	clk_enable(host->mci_clk);		/* Enable the peripheral clock */
A
Andrew Victor 已提交
1029 1030 1031
	at91_mci_disable(host);
	at91_mci_enable(host);

1032 1033 1034
	/*
	 * Allocate the MCI interrupt
	 */
1035
	host->irq = platform_get_irq(pdev, 0);
1036 1037
	ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
			mmc_hostname(mmc), host);
1038
	if (ret) {
1039 1040
		dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
		goto fail0;
1041 1042
	}

1043 1044
	setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);

1045 1046 1047 1048 1049
	platform_set_drvdata(pdev, mmc);

	/*
	 * Add host to MMC layer
	 */
1050
	if (host->board->det_pin) {
1051
		host->present = !gpio_get_value(host->board->det_pin);
1052
	}
1053 1054 1055 1056 1057 1058 1059 1060 1061
	else
		host->present = -1;

	mmc_add_host(mmc);

	/*
	 * monitor card insertion/removal if we can
	 */
	if (host->board->det_pin) {
1062 1063
		ret = request_irq(gpio_to_irq(host->board->det_pin),
				at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
1064
		if (ret)
1065 1066 1067
			dev_warn(&pdev->dev, "request MMC detect irq failed\n");
		else
			device_init_wakeup(&pdev->dev, 1);
1068 1069
	}

A
Andrew Victor 已提交
1070
	pr_debug("Added MCI driver\n");
1071 1072

	return 0;
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

fail0:
	clk_disable(host->mci_clk);
	iounmap(host->baseaddr);
fail1:
	clk_put(host->mci_clk);
fail2:
	if (host->board->vcc_pin)
		gpio_free(host->board->vcc_pin);
fail3:
	if (host->board->wp_pin)
		gpio_free(host->board->wp_pin);
fail4:
	if (host->board->det_pin)
		gpio_free(host->board->det_pin);
1088 1089 1090 1091
fail4b:
	if (host->buffer)
		dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
				host->buffer, host->physical_address);
1092 1093 1094 1095 1096 1097
fail5:
	mmc_free_host(mmc);
fail6:
	release_mem_region(res->start, res->end - res->start + 1);
	dev_err(&pdev->dev, "probe failed, err %d\n", ret);
	return ret;
1098 1099 1100 1101 1102
}

/*
 * Remove a device
 */
D
David Brownell 已提交
1103
static int __exit at91_mci_remove(struct platform_device *pdev)
1104 1105 1106
{
	struct mmc_host *mmc = platform_get_drvdata(pdev);
	struct at91mci_host *host;
1107
	struct resource *res;
1108 1109 1110 1111 1112 1113

	if (!mmc)
		return -1;

	host = mmc_priv(mmc);

1114 1115 1116 1117
	if (host->buffer)
		dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
				host->buffer, host->physical_address);

A
Anti Sullin 已提交
1118
	if (host->board->det_pin) {
1119 1120
		if (device_can_wakeup(&pdev->dev))
			free_irq(gpio_to_irq(host->board->det_pin), host);
1121
		device_init_wakeup(&pdev->dev, 0);
1122
		gpio_free(host->board->det_pin);
1123 1124
	}

A
Andrew Victor 已提交
1125
	at91_mci_disable(host);
M
Marc Pignat 已提交
1126
	del_timer_sync(&host->timer);
1127 1128
	mmc_remove_host(mmc);
	free_irq(host->irq, host);
1129

1130 1131
	clk_disable(host->mci_clk);			/* Disable the peripheral clock */
	clk_put(host->mci_clk);
1132

1133 1134 1135 1136 1137
	if (host->board->vcc_pin)
		gpio_free(host->board->vcc_pin);
	if (host->board->wp_pin)
		gpio_free(host->board->wp_pin);

1138 1139 1140
	iounmap(host->baseaddr);
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(res->start, res->end - res->start + 1);
1141

1142 1143
	mmc_free_host(mmc);
	platform_set_drvdata(pdev, NULL);
1144
	pr_debug("MCI Removed\n");
1145 1146 1147 1148 1149 1150 1151 1152

	return 0;
}

#ifdef CONFIG_PM
static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct mmc_host *mmc = platform_get_drvdata(pdev);
1153
	struct at91mci_host *host = mmc_priv(mmc);
1154 1155
	int ret = 0;

A
Anti Sullin 已提交
1156
	if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1157 1158
		enable_irq_wake(host->board->det_pin);

1159 1160 1161 1162 1163 1164 1165 1166 1167
	if (mmc)
		ret = mmc_suspend_host(mmc, state);

	return ret;
}

static int at91_mci_resume(struct platform_device *pdev)
{
	struct mmc_host *mmc = platform_get_drvdata(pdev);
1168
	struct at91mci_host *host = mmc_priv(mmc);
1169 1170
	int ret = 0;

A
Anti Sullin 已提交
1171
	if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1172 1173
		disable_irq_wake(host->board->det_pin);

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	if (mmc)
		ret = mmc_resume_host(mmc);

	return ret;
}
#else
#define at91_mci_suspend	NULL
#define at91_mci_resume		NULL
#endif

static struct platform_driver at91_mci_driver = {
D
David Brownell 已提交
1185
	.remove		= __exit_p(at91_mci_remove),
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	.suspend	= at91_mci_suspend,
	.resume		= at91_mci_resume,
	.driver		= {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
	},
};

static int __init at91_mci_init(void)
{
D
David Brownell 已提交
1196
	return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
}

static void __exit at91_mci_exit(void)
{
	platform_driver_unregister(&at91_mci_driver);
}

module_init(at91_mci_init);
module_exit(at91_mci_exit);

MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
MODULE_AUTHOR("Nick Randell");
MODULE_LICENSE("GPL");
1210
MODULE_ALIAS("platform:at91_mci");