at91_mci.c 26.6 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 69 70 71 72

#include <linux/mmc/host.h>

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

75 76
#include <asm/mach/mmc.h>
#include <asm/arch/board.h>
A
Andrew Victor 已提交
77
#include <asm/arch/cpu.h>
78
#include <asm/arch/at91_mci.h>
79 80 81

#define DRIVER_NAME "at91_mci"

82 83
#define FL_SENT_COMMAND	(1 << 0)
#define FL_SENT_STOP	(1 << 1)
84

85 86
#define AT91_MCI_ERRORS	(AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE	\
		| AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE		\
87
		| AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
88

A
Andrew Victor 已提交
89 90
#define at91_mci_read(host, reg)	__raw_readl((host)->baseaddr + (reg))
#define at91_mci_write(host, reg, val)	__raw_writel((val), (host)->baseaddr + (reg))
91 92 93 94 95 96 97 98 99 100 101


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

A
Andrew Victor 已提交
102
	void __iomem *baseaddr;
103
	int irq;
A
Andrew Victor 已提交
104

105 106 107
	struct at91_mmc_data *board;
	int present;

108 109
	struct clk *mci_clk;

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	/*
	 * 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;
};

/*
 * Copy from sg to a dma block - used for transfers
 */
N
Nicolas Ferre 已提交
133
static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
{
	unsigned int len, i, size;
	unsigned *dmabuf = host->buffer;

	size = host->total_length;
	len = data->sg_len;

	/*
	 * 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 已提交
153
		sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
154 155 156
		amount = min(size, sg->length);
		size -= amount;

A
Andrew Victor 已提交
157 158 159 160 161 162 163 164
		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */
			int index;

			for (index = 0; index < (amount / 4); index++)
				*dmabuf++ = swab32(sgbuffer[index]);
		}
		else
			memcpy(dmabuf, sgbuffer, amount);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

		kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);

		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);
}

/*
 * Prepare a dma read
 */
N
Nicolas Ferre 已提交
182
static void at91_mci_pre_dma_read(struct at91mci_host *host)
183 184 185 186 187 188
{
	int i;
	struct scatterlist *sg;
	struct mmc_command *cmd;
	struct mmc_data *data;

189
	pr_debug("pre dma read\n");
190 191 192

	cmd = host->cmd;
	if (!cmd) {
193
		pr_debug("no command\n");
194 195 196 197 198
		return;
	}

	data = cmd->data;
	if (!data) {
199
		pr_debug("no data\n");
200 201 202 203 204 205
		return;
	}

	for (i = 0; i < 2; i++) {
		/* nothing left to transfer */
		if (host->transfer_index >= data->sg_len) {
206
			pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
207 208 209 210 211
			break;
		}

		/* Check to see if this needs filling */
		if (i == 0) {
212
			if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
213
				pr_debug("Transfer active in current\n");
214 215 216 217
				continue;
			}
		}
		else {
218
			if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
219
				pr_debug("Transfer active in next\n");
220 221 222 223 224
				continue;
			}
		}

		/* Setup the next transfer */
225
		pr_debug("Using transfer index %d\n", host->transfer_index);
226 227

		sg = &data->sg[host->transfer_index++];
228
		pr_debug("sg = %p\n", sg);
229

J
Jens Axboe 已提交
230
		sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
231

232
		pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
233 234

		if (i == 0) {
235 236
			at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
			at91_mci_write(host, ATMEL_PDC_RCR, sg->length / 4);
237 238
		}
		else {
239 240
			at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
			at91_mci_write(host, ATMEL_PDC_RNCR, sg->length / 4);
241 242 243
		}
	}

244
	pr_debug("pre dma read done\n");
245 246 247 248 249
}

/*
 * Handle after a dma read
 */
N
Nicolas Ferre 已提交
250
static void at91_mci_post_dma_read(struct at91mci_host *host)
251 252 253 254
{
	struct mmc_command *cmd;
	struct mmc_data *data;

255
	pr_debug("post dma read\n");
256 257 258

	cmd = host->cmd;
	if (!cmd) {
259
		pr_debug("no command\n");
260 261 262 263 264
		return;
	}

	data = cmd->data;
	if (!data) {
265
		pr_debug("no data\n");
266 267 268 269 270 271
		return;
	}

	while (host->in_use_index < host->transfer_index) {
		struct scatterlist *sg;

272
		pr_debug("finishing index %d\n", host->in_use_index);
273 274 275

		sg = &data->sg[host->in_use_index++];

276
		pr_debug("Unmapping page %08X\n", sg->dma_address);
277 278 279 280 281

		dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);

		data->bytes_xfered += sg->length;

A
Andrew Victor 已提交
282
		if (cpu_is_at91rm9200()) {	/* AT91RM9200 errata */
283
			unsigned int *buffer;
A
Andrew Victor 已提交
284
			int index;
285

286
			/* Swap the contents of the buffer */
J
Jens Axboe 已提交
287
			buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
288 289
			pr_debug("buffer = %p, length = %d\n", buffer, sg->length);

A
Andrew Victor 已提交
290 291
			for (index = 0; index < (sg->length / 4); index++)
				buffer[index] = swab32(buffer[index]);
292 293

			kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
294
		}
A
Andrew Victor 已提交
295

J
Jens Axboe 已提交
296
		flush_dcache_page(sg_page(sg));
297 298 299 300
	}

	/* Is there another transfer to trigger? */
	if (host->transfer_index < data->sg_len)
N
Nicolas Ferre 已提交
301
		at91_mci_pre_dma_read(host);
302
	else {
303
		at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
A
Andrew Victor 已提交
304
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
305 306
	}

307
	pr_debug("post dma read done\n");
308 309 310 311 312 313 314 315 316 317
}

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

318
	pr_debug("Handling the transmit\n");
319 320

	/* Disable the transfer */
321
	at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
322 323

	/* Now wait for cmd ready */
A
Andrew Victor 已提交
324
	at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
325 326 327 328 329 330 331

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

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

P
Pierre Ossman 已提交
332
	if (cmd->data->blocks > 1) {
333 334 335 336 337
		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);

338 339 340
	data->bytes_xfered = host->total_length;
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
/*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*/
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE);
		at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
		at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
	}

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


363 364 365
/*
 * Enable the controller
 */
A
Andrew Victor 已提交
366
static void at91_mci_enable(struct at91mci_host *host)
367
{
368 369
	unsigned int mr;

A
Andrew Victor 已提交
370
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
A
Andrew Victor 已提交
371
	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
A
Andrew Victor 已提交
372
	at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
373 374 375 376 377 378
	mr = AT91_MCI_PDCMODE | 0x34a;

	if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
		mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;

	at91_mci_write(host, AT91_MCI_MR, mr);
A
Andrew Victor 已提交
379 380 381

	/* use Slot A or B (only one at same time) */
	at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
382 383 384 385 386
}

/*
 * Disable the controller
 */
A
Andrew Victor 已提交
387
static void at91_mci_disable(struct at91mci_host *host)
388
{
A
Andrew Victor 已提交
389
	at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
390 391 392 393 394
}

/*
 * Send a command
 */
395
static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
396 397 398 399 400 401 402 403 404 405
{
	unsigned int cmdr, mr;
	unsigned int block_length;
	struct mmc_data *data = cmd->data;

	unsigned int blocks;
	unsigned int ier = 0;

	host->cmd = cmd;

406
	/* Needed for leaving busy state before CMD1 */
A
Andrew Victor 已提交
407
	if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
408
		pr_debug("Clearing timeout\n");
A
Andrew Victor 已提交
409 410 411
		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)) {
412
			/* spin */
A
Andrew Victor 已提交
413
			pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
414 415
		}
	}
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	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) {
432 433 434 435 436 437 438 439

		if ( data->blksz & 0x3 ) {
			pr_debug("Unsupported block size\n");
			cmd->error = -EINVAL;
			mmc_request_done(host->mmc, host->request);
			return;
		}

440
		block_length = data->blksz;
441 442 443 444 445 446 447 448 449 450
		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 已提交
451
		if (data->blocks > 1)
452 453 454 455 456 457 458
			cmdr |= AT91_MCI_TRTYP_MULTIPLE;
	}
	else {
		block_length = 0;
		blocks = 0;
	}

459
	if (host->flags & FL_SENT_STOP)
460 461 462 463 464 465 466 467
		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 已提交
468
	pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
A
Andrew Victor 已提交
469
		cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
470 471

	if (!data) {
472 473 474 475 476 477 478 479 480
		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);
481 482 483 484 485
		ier = AT91_MCI_CMDRDY;
	} else {
		/* zero block length and PDC mode */
		mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
		at91_mci_write(host, AT91_MCI_MR, mr | (block_length << 16) | AT91_MCI_PDCMODE);
A
Andrew Victor 已提交
486

487 488 489 490
		/*
		 * Disable the PDC controller
		 */
		at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
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
		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->buffer = NULL;
				host->total_length = 0;

				at91_mci_pre_dma_read(host);
				ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
			}
			else {
				/*
				 * Handle a write
				 */
				host->total_length = block_length * blocks;
				host->buffer = dma_alloc_coherent(NULL,
						host->total_length,
						&host->physical_address, GFP_KERNEL);

				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);
				at91_mci_write(host, ATMEL_PDC_TCR, host->total_length / 4);
				ier = AT91_MCI_CMDRDY;
			}
523 524 525 526 527 528 529 530
		}
	}

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

A
Andrew Victor 已提交
531 532
	at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
	at91_mci_write(host, AT91_MCI_CMDR, cmdr);
533 534 535

	if (cmdr & AT91_MCI_TRCMD_START) {
		if (cmdr & AT91_MCI_TRDIR)
536
			at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
537 538
	}

539
	/* Enable selected interrupts */
540
	at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
541 542 543 544 545
}

/*
 * Process the next step in the request
 */
N
Nicolas Ferre 已提交
546
static void at91_mci_process_next(struct at91mci_host *host)
547 548 549
{
	if (!(host->flags & FL_SENT_COMMAND)) {
		host->flags |= FL_SENT_COMMAND;
550
		at91_mci_send_command(host, host->request->cmd);
551 552 553
	}
	else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
		host->flags |= FL_SENT_STOP;
554
		at91_mci_send_command(host, host->request->stop);
555 556 557 558 559 560 561 562
	}
	else
		mmc_request_done(host->mmc, host->request);
}

/*
 * Handle a command that has been completed
 */
N
Nicolas Ferre 已提交
563
static void at91_mci_completed_command(struct at91mci_host *host)
564 565 566 567
{
	struct mmc_command *cmd = host->cmd;
	unsigned int status;

A
Andrew Victor 已提交
568
	at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
569

A
Andrew Victor 已提交
570 571 572 573
	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));
574 575 576 577 578 579

	if (host->buffer) {
		dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
		host->buffer = NULL;
	}

A
Andrew Victor 已提交
580
	status = at91_mci_read(host, AT91_MCI_SR);
581

582
	pr_debug("Status = %08X [%08X %08X %08X %08X]\n",
583 584
		 status, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);

585
	if (status & AT91_MCI_ERRORS) {
586
		if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
P
Pierre Ossman 已提交
587
			cmd->error = 0;
588 589 590
		}
		else {
			if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
P
Pierre Ossman 已提交
591
				cmd->error = -ETIMEDOUT;
592
			else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
P
Pierre Ossman 已提交
593
				cmd->error = -EILSEQ;
594
			else
P
Pierre Ossman 已提交
595
				cmd->error = -EIO;
596

597
			pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
598 599 600 601
				 cmd->error, cmd->opcode, cmd->retries);
		}
	}
	else
P
Pierre Ossman 已提交
602
		cmd->error = 0;
603

N
Nicolas Ferre 已提交
604
	at91_mci_process_next(host);
605 606 607 608 609 610 611 612 613 614 615
}

/*
 * 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;

N
Nicolas Ferre 已提交
616
	at91_mci_process_next(host);
617 618 619 620 621 622 623 624 625
}

/*
 * 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);
626
	unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
627

628
	host->bus_mode = ios->bus_mode;
629 630 631

	if (ios->clock == 0) {
		/* Disable the MCI controller */
A
Andrew Victor 已提交
632
		at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
633 634 635 636
		clkdiv = 0;
	}
	else {
		/* Enable the MCI controller */
A
Andrew Victor 已提交
637
		at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
638 639 640 641 642 643

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

644
		pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
645 646 647
			at91_master_clock / (2 * (clkdiv + 1)));
	}
	if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
648
		pr_debug("MMC: Setting controller bus width to 4\n");
A
Andrew Victor 已提交
649
		at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
650 651
	}
	else {
652
		pr_debug("MMC: Setting controller bus width to 1\n");
A
Andrew Victor 已提交
653
		at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
654 655 656
	}

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

	/* maybe switch power to the card */
660
	if (host->board->vcc_pin) {
661 662
		switch (ios->power_mode) {
			case MMC_POWER_OFF:
663
				gpio_set_value(host->board->vcc_pin, 0);
664 665
				break;
			case MMC_POWER_UP:
666
				gpio_set_value(host->board->vcc_pin, 1);
667
				break;
M
Marc Pignat 已提交
668 669 670 671
			case MMC_POWER_ON:
				break;
			default:
				WARN_ON(1);
672 673 674 675 676 677 678
		}
	}
}

/*
 * Handle an interrupt
 */
679
static irqreturn_t at91_mci_irq(int irq, void *devid)
680 681 682
{
	struct at91mci_host *host = devid;
	int completed = 0;
683
	unsigned int int_status, int_mask;
684

A
Andrew Victor 已提交
685
	int_status = at91_mci_read(host, AT91_MCI_SR);
686
	int_mask = at91_mci_read(host, AT91_MCI_IMR);
687

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

691 692 693
	int_status = int_status & int_mask;

	if (int_status & AT91_MCI_ERRORS) {
694
		completed = 1;
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
		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 */
716 717

		if (int_status & AT91_MCI_TXBUFE) {
718
			pr_debug("TX buffer empty\n");
719 720 721
			at91_mci_handle_transmitted(host);
		}

722 723 724 725 726
		if (int_status & AT91_MCI_ENDRX) {
			pr_debug("ENDRX\n");
			at91_mci_post_dma_read(host);
		}

727
		if (int_status & AT91_MCI_RXBUFF) {
728
			pr_debug("RX buffer full\n");
729 730 731
			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;
732 733
		}

734
		if (int_status & AT91_MCI_ENDTX)
735
			pr_debug("Transmit has ended\n");
736 737

		if (int_status & AT91_MCI_NOTBUSY) {
738
			pr_debug("Card is ready\n");
739
			completed = 1;
740 741
		}

742
		if (int_status & AT91_MCI_DTIP)
743
			pr_debug("Data transfer in progress\n");
744

745
		if (int_status & AT91_MCI_BLKE) {
746
			pr_debug("Block transfer has ended\n");
747 748
			completed = 1;
		}
749

750
		if (int_status & AT91_MCI_TXRDY)
751
			pr_debug("Ready to transmit\n");
752

753
		if (int_status & AT91_MCI_RXRDY)
754
			pr_debug("Ready to receive\n");
755 756

		if (int_status & AT91_MCI_CMDRDY) {
757
			pr_debug("Command ready\n");
758
			completed = at91_mci_handle_cmdrdy(host);
759 760 761 762
		}
	}

	if (completed) {
763
		pr_debug("Completed command\n");
A
Andrew Victor 已提交
764
		at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
N
Nicolas Ferre 已提交
765
		at91_mci_completed_command(host);
766 767
	} else
		at91_mci_write(host, AT91_MCI_IDR, int_status);
768 769 770 771

	return IRQ_HANDLED;
}

772
static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
773 774
{
	struct at91mci_host *host = _host;
775
	int present = !gpio_get_value(irq_to_gpio(irq));
776 777 778 779 780 781 782

	/*
	 * we expect this irq on both insert and remove,
	 * and use a short delay to debounce.
	 */
	if (present != host->present) {
		host->present = present;
783
		pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
784 785
			present ? "insert" : "remove");
		if (!present) {
786
			pr_debug("****** Resetting SD-card bus width ******\n");
A
Andrew Victor 已提交
787
			at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
788 789 790 791 792 793
		}
		mmc_detect_change(host->mmc, msecs_to_jiffies(100));
	}
	return IRQ_HANDLED;
}

D
David Brownell 已提交
794
static int at91_mci_get_ro(struct mmc_host *mmc)
795 796 797 798 799
{
	int read_only = 0;
	struct at91mci_host *host = mmc_priv(mmc);

	if (host->board->wp_pin) {
800
		read_only = gpio_get_value(host->board->wp_pin);
801 802 803 804 805 806 807 808 809 810
		printk(KERN_WARNING "%s: card is %s\n", mmc_hostname(mmc),
				(read_only ? "read-only" : "read-write") );
	}
	else {
		printk(KERN_WARNING "%s: host does not support reading read-only "
				"switch.  Assuming write-enable.\n", mmc_hostname(mmc));
	}
	return read_only;
}

811
static const struct mmc_host_ops at91_mci_ops = {
812 813 814 815 816 817 818 819
	.request	= at91_mci_request,
	.set_ios	= at91_mci_set_ios,
	.get_ro		= at91_mci_get_ro,
};

/*
 * Probe for the device
 */
D
David Brownell 已提交
820
static int __init at91_mci_probe(struct platform_device *pdev)
821 822 823
{
	struct mmc_host *mmc;
	struct at91mci_host *host;
824
	struct resource *res;
825 826
	int ret;

827 828 829 830 831 832 833
	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;

834 835
	mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
	if (!mmc) {
836 837 838
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
		goto fail6;
839 840 841 842 843 844 845
	}

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

846
	mmc->max_blk_size = 4095;
847
	mmc->max_blk_count = mmc->max_req_size;
848

849 850 851 852 853 854
	host = mmc_priv(mmc);
	host->mmc = mmc;
	host->buffer = NULL;
	host->bus_mode = 0;
	host->board = pdev->dev.platform_data;
	if (host->board->wire4) {
855 856 857
		if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
			mmc->caps |= MMC_CAP_4_BIT_DATA;
		else
858
			dev_warn(&pdev->dev, "4 wire bus mode not supported"
859
				" - using 1 wire\n");
860 861
	}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	/*
	 * 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");
			goto fail5;
		}
	}
	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;
		}
	}

888 889 890
	/*
	 * Get Clock
	 */
891 892
	host->mci_clk = clk_get(&pdev->dev, "mci_clk");
	if (IS_ERR(host->mci_clk)) {
893 894 895
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "no mci_clk?\n");
		goto fail2;
896 897
	}

898 899 900 901 902
	/*
	 * Map I/O region
	 */
	host->baseaddr = ioremap(res->start, res->end - res->start + 1);
	if (!host->baseaddr) {
903 904
		ret = -ENOMEM;
		goto fail1;
905
	}
A
Andrew Victor 已提交
906 907 908 909

	/*
	 * Reset hardware
	 */
910
	clk_enable(host->mci_clk);		/* Enable the peripheral clock */
A
Andrew Victor 已提交
911 912 913
	at91_mci_disable(host);
	at91_mci_enable(host);

914 915 916
	/*
	 * Allocate the MCI interrupt
	 */
917
	host->irq = platform_get_irq(pdev, 0);
918 919
	ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
			mmc_hostname(mmc), host);
920
	if (ret) {
921 922
		dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
		goto fail0;
923 924 925 926 927 928 929
	}

	platform_set_drvdata(pdev, mmc);

	/*
	 * Add host to MMC layer
	 */
930
	if (host->board->det_pin) {
931
		host->present = !gpio_get_value(host->board->det_pin);
932
	}
933 934 935 936 937 938 939 940 941
	else
		host->present = -1;

	mmc_add_host(mmc);

	/*
	 * monitor card insertion/removal if we can
	 */
	if (host->board->det_pin) {
942 943
		ret = request_irq(gpio_to_irq(host->board->det_pin),
				at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
944
		if (ret)
945 946 947
			dev_warn(&pdev->dev, "request MMC detect irq failed\n");
		else
			device_init_wakeup(&pdev->dev, 1);
948 949
	}

A
Andrew Victor 已提交
950
	pr_debug("Added MCI driver\n");
951 952

	return 0;
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

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);
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;
974 975 976 977 978
}

/*
 * Remove a device
 */
D
David Brownell 已提交
979
static int __exit at91_mci_remove(struct platform_device *pdev)
980 981 982
{
	struct mmc_host *mmc = platform_get_drvdata(pdev);
	struct at91mci_host *host;
983
	struct resource *res;
984 985 986 987 988 989

	if (!mmc)
		return -1;

	host = mmc_priv(mmc);

A
Anti Sullin 已提交
990
	if (host->board->det_pin) {
991 992
		if (device_can_wakeup(&pdev->dev))
			free_irq(gpio_to_irq(host->board->det_pin), host);
993
		device_init_wakeup(&pdev->dev, 0);
994
		gpio_free(host->board->det_pin);
995 996
	}

A
Andrew Victor 已提交
997
	at91_mci_disable(host);
998 999
	mmc_remove_host(mmc);
	free_irq(host->irq, host);
1000

1001 1002
	clk_disable(host->mci_clk);			/* Disable the peripheral clock */
	clk_put(host->mci_clk);
1003

1004 1005 1006 1007 1008
	if (host->board->vcc_pin)
		gpio_free(host->board->vcc_pin);
	if (host->board->wp_pin)
		gpio_free(host->board->wp_pin);

1009 1010 1011
	iounmap(host->baseaddr);
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(res->start, res->end - res->start + 1);
1012

1013 1014
	mmc_free_host(mmc);
	platform_set_drvdata(pdev, NULL);
1015
	pr_debug("MCI Removed\n");
1016 1017 1018 1019 1020 1021 1022 1023

	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);
1024
	struct at91mci_host *host = mmc_priv(mmc);
1025 1026
	int ret = 0;

A
Anti Sullin 已提交
1027
	if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1028 1029
		enable_irq_wake(host->board->det_pin);

1030 1031 1032 1033 1034 1035 1036 1037 1038
	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);
1039
	struct at91mci_host *host = mmc_priv(mmc);
1040 1041
	int ret = 0;

A
Anti Sullin 已提交
1042
	if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1043 1044
		disable_irq_wake(host->board->det_pin);

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	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 已提交
1056
	.remove		= __exit_p(at91_mci_remove),
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	.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 已提交
1067
	return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
}

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");
1081
MODULE_ALIAS("platform:at91_mci");