2 * linux/drivers/mmc/host/at91_mci.c - ATMEL AT91 MCI Driver
4 * Copyright (C) 2005 Cougar Creek Computing Devices Ltd, All Rights Reserved
6 * Copyright (C) 2006 Malcolm Noyes
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 This is the AT91 MCI driver that has been tested with both MMC cards
15 and SD-cards. Boards that support write protect are now supported.
16 The CCAT91SBC001 board does not support SD cards.
18 The three entry points are at91_mci_request, at91_mci_set_ios
22 This configures the device to put it into the correct mode and clock speed
26 MCI request processes the commands sent in the mmc_request structure. This
27 can consist of a processing command and a stop command in the case of
28 multiple block transfers.
30 There are three main types of request, commands, reads and writes.
32 Commands are straight forward. The command is submitted to the controller and
33 the request function returns. When the controller generates an interrupt to indicate
34 the command is finished, the response to the command are read and the mmc_request_done
35 function called to end the request.
37 Reads and writes work in a similar manner to normal commands but involve the PDC (DMA)
38 controller to manage the transfers.
40 A read is done from the controller directly to the scatterlist passed in from the request.
41 Due to a bug in the AT91RM9200 controller, when a read is completed, all the words are byte
42 swapped in the scatterlist buffers. AT91SAM926x are not affected by this bug.
44 The sequence of read interrupts is: ENDRX, RXBUFF, CMDRDY
46 A write is slightly different in that the bytes to write are read from the scatterlist
47 into a dma memory buffer (this is in case the source buffer should be read only). The
48 entire write buffer is then done from this single dma memory buffer.
50 The sequence of write interrupts is: ENDTX, TXBUFE, NOTBUSY, CMDRDY
53 Gets the status of the write protect pin, if available.
56 #include <linux/module.h>
57 #include <linux/moduleparam.h>
58 #include <linux/init.h>
59 #include <linux/ioport.h>
60 #include <linux/platform_device.h>
61 #include <linux/interrupt.h>
62 #include <linux/blkdev.h>
63 #include <linux/delay.h>
64 #include <linux/err.h>
65 #include <linux/dma-mapping.h>
66 #include <linux/clk.h>
67 #include <linux/atmel_pdc.h>
69 #include <linux/mmc/host.h>
75 #include <asm/mach/mmc.h>
76 #include <asm/arch/board.h>
77 #include <asm/arch/cpu.h>
78 #include <asm/arch/at91_mci.h>
80 #define DRIVER_NAME "at91_mci"
82 #define FL_SENT_COMMAND (1 << 0)
83 #define FL_SENT_STOP (1 << 1)
85 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
86 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
87 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
89 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
90 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
94 * Low level type for this driver
99 struct mmc_command *cmd;
100 struct mmc_request *request;
102 void __iomem *baseaddr;
105 struct at91_mmc_data *board;
111 * Flag indicating when the command has been sent. This is used to
112 * work out whether or not to send the stop
115 /* flag for current bus settings */
118 /* DMA buffer used for transmitting */
119 unsigned int* buffer;
120 dma_addr_t physical_address;
121 unsigned int total_length;
123 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
126 /* Latest in the scatterlist that has been enabled for transfer */
129 /* Timer for timeouts */
130 struct timer_list timer;
134 * Reset the controller and restore most of the state
136 static void at91_reset_host(struct at91mci_host *host)
144 local_irq_save(flags);
145 imr = at91_mci_read(host, AT91_MCI_IMR);
147 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
149 /* save current state */
150 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
151 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
152 dtor = at91_mci_read(host, AT91_MCI_DTOR);
154 /* reset the controller */
155 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
158 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
159 at91_mci_write(host, AT91_MCI_MR, mr);
160 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
161 at91_mci_write(host, AT91_MCI_DTOR, dtor);
162 at91_mci_write(host, AT91_MCI_IER, imr);
164 /* make sure sdio interrupts will fire */
165 at91_mci_read(host, AT91_MCI_SR);
167 local_irq_restore(flags);
170 static void at91_timeout_timer(unsigned long data)
172 struct at91mci_host *host;
174 host = (struct at91mci_host *)data;
177 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
179 if (host->cmd && host->cmd->data) {
180 host->cmd->data->error = -ETIMEDOUT;
183 host->cmd->error = -ETIMEDOUT;
185 host->request->cmd->error = -ETIMEDOUT;
188 at91_reset_host(host);
189 mmc_request_done(host->mmc, host->request);
194 * Copy from sg to a dma block - used for transfers
196 static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
198 unsigned int len, i, size;
199 unsigned *dmabuf = host->buffer;
201 size = host->total_length;
205 * Just loop through all entries. Size might not
206 * be the entire list though so make sure that
207 * we do not transfer too much.
209 for (i = 0; i < len; i++) {
210 struct scatterlist *sg;
212 unsigned int *sgbuffer;
216 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
217 amount = min(size, sg->length);
220 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
223 for (index = 0; index < (amount / 4); index++)
224 *dmabuf++ = swab32(sgbuffer[index]);
227 memcpy(dmabuf, sgbuffer, amount);
229 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
236 * Check that we didn't get a request to transfer
237 * more data than can fit into the SG list.
245 static void at91_mci_pre_dma_read(struct at91mci_host *host)
248 struct scatterlist *sg;
249 struct mmc_command *cmd;
250 struct mmc_data *data;
252 pr_debug("pre dma read\n");
256 pr_debug("no command\n");
262 pr_debug("no data\n");
266 for (i = 0; i < 2; i++) {
267 /* nothing left to transfer */
268 if (host->transfer_index >= data->sg_len) {
269 pr_debug("Nothing left to transfer (index = %d)\n", host->transfer_index);
273 /* Check to see if this needs filling */
275 if (at91_mci_read(host, ATMEL_PDC_RCR) != 0) {
276 pr_debug("Transfer active in current\n");
281 if (at91_mci_read(host, ATMEL_PDC_RNCR) != 0) {
282 pr_debug("Transfer active in next\n");
287 /* Setup the next transfer */
288 pr_debug("Using transfer index %d\n", host->transfer_index);
290 sg = &data->sg[host->transfer_index++];
291 pr_debug("sg = %p\n", sg);
293 sg->dma_address = dma_map_page(NULL, sg_page(sg), sg->offset, sg->length, DMA_FROM_DEVICE);
295 pr_debug("dma address = %08X, length = %d\n", sg->dma_address, sg->length);
298 at91_mci_write(host, ATMEL_PDC_RPR, sg->dma_address);
299 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
302 at91_mci_write(host, ATMEL_PDC_RNPR, sg->dma_address);
303 at91_mci_write(host, ATMEL_PDC_RNCR, (data->blksz & 0x3) ? sg->length : sg->length / 4);
307 pr_debug("pre dma read done\n");
311 * Handle after a dma read
313 static void at91_mci_post_dma_read(struct at91mci_host *host)
315 struct mmc_command *cmd;
316 struct mmc_data *data;
318 pr_debug("post dma read\n");
322 pr_debug("no command\n");
328 pr_debug("no data\n");
332 while (host->in_use_index < host->transfer_index) {
333 struct scatterlist *sg;
335 pr_debug("finishing index %d\n", host->in_use_index);
337 sg = &data->sg[host->in_use_index++];
339 pr_debug("Unmapping page %08X\n", sg->dma_address);
341 dma_unmap_page(NULL, sg->dma_address, sg->length, DMA_FROM_DEVICE);
343 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
344 unsigned int *buffer;
347 /* Swap the contents of the buffer */
348 buffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
349 pr_debug("buffer = %p, length = %d\n", buffer, sg->length);
351 for (index = 0; index < (sg->length / 4); index++)
352 buffer[index] = swab32(buffer[index]);
354 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
357 flush_dcache_page(sg_page(sg));
359 data->bytes_xfered += sg->length;
362 /* Is there another transfer to trigger? */
363 if (host->transfer_index < data->sg_len)
364 at91_mci_pre_dma_read(host);
366 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
367 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
370 pr_debug("post dma read done\n");
374 * Handle transmitted data
376 static void at91_mci_handle_transmitted(struct at91mci_host *host)
378 struct mmc_command *cmd;
379 struct mmc_data *data;
381 pr_debug("Handling the transmit\n");
383 /* Disable the transfer */
384 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
386 /* Now wait for cmd ready */
387 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
395 if (cmd->data->blocks > 1) {
396 pr_debug("multiple write : wait for BLKE...\n");
397 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
399 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
403 * Update bytes tranfered count during a write operation
405 static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
407 struct mmc_data *data;
409 /* always deal with the effective request (and not the current cmd) */
411 if (host->request->cmd && host->request->cmd->error != 0)
414 if (host->request->data) {
415 data = host->request->data;
416 if (data->flags & MMC_DATA_WRITE) {
417 /* card is in IDLE mode now */
418 pr_debug("-> bytes_xfered %d, total_length = %d\n",
419 data->bytes_xfered, host->total_length);
420 data->bytes_xfered = host->total_length;
426 /*Handle after command sent ready*/
427 static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
431 else if (!host->cmd->data) {
432 if (host->flags & FL_SENT_STOP) {
433 /*After multi block write, we must wait for NOTBUSY*/
434 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
436 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
437 /*After sendding multi-block-write command, start DMA transfer*/
438 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
439 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
442 /* command not completed, have to wait */
448 * Enable the controller
450 static void at91_mci_enable(struct at91mci_host *host)
454 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
455 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
456 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
457 mr = AT91_MCI_PDCMODE | 0x34a;
459 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
460 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
462 at91_mci_write(host, AT91_MCI_MR, mr);
464 /* use Slot A or B (only one at same time) */
465 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
469 * Disable the controller
471 static void at91_mci_disable(struct at91mci_host *host)
473 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
479 static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
481 unsigned int cmdr, mr;
482 unsigned int block_length;
483 struct mmc_data *data = cmd->data;
486 unsigned int ier = 0;
490 /* Needed for leaving busy state before CMD1 */
491 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
492 pr_debug("Clearing timeout\n");
493 at91_mci_write(host, AT91_MCI_ARGR, 0);
494 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
495 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
497 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
503 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
504 cmdr |= AT91_MCI_RSPTYP_NONE;
506 /* if a response is expected then allow maximum response latancy */
507 cmdr |= AT91_MCI_MAXLAT;
508 /* set 136 bit response for R2, 48 bit response otherwise */
509 if (mmc_resp_type(cmd) == MMC_RSP_R2)
510 cmdr |= AT91_MCI_RSPTYP_136;
512 cmdr |= AT91_MCI_RSPTYP_48;
517 if ( cpu_is_at91rm9200() && (data->blksz & 0x3) ) {
518 pr_debug("Unsupported block size\n");
519 cmd->error = -EINVAL;
520 mmc_request_done(host->mmc, host->request);
524 block_length = data->blksz;
525 blocks = data->blocks;
527 /* always set data start - also set direction flag for read */
528 if (data->flags & MMC_DATA_READ)
529 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
530 else if (data->flags & MMC_DATA_WRITE)
531 cmdr |= AT91_MCI_TRCMD_START;
533 if (data->flags & MMC_DATA_STREAM)
534 cmdr |= AT91_MCI_TRTYP_STREAM;
535 if (data->blocks > 1)
536 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
543 if (host->flags & FL_SENT_STOP)
544 cmdr |= AT91_MCI_TRCMD_STOP;
546 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
547 cmdr |= AT91_MCI_OPDCMD;
550 * Set the arguments and send the command
552 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
553 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
556 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
557 at91_mci_write(host, ATMEL_PDC_RPR, 0);
558 at91_mci_write(host, ATMEL_PDC_RCR, 0);
559 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
560 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
561 at91_mci_write(host, ATMEL_PDC_TPR, 0);
562 at91_mci_write(host, ATMEL_PDC_TCR, 0);
563 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
564 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
565 ier = AT91_MCI_CMDRDY;
567 /* zero block length and PDC mode */
568 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
569 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
570 mr |= (block_length << 16);
571 mr |= AT91_MCI_PDCMODE;
572 at91_mci_write(host, AT91_MCI_MR, mr);
574 if (!cpu_is_at91rm9200())
575 at91_mci_write(host, AT91_MCI_BLKR,
576 AT91_MCI_BLKR_BCNT(blocks) |
577 AT91_MCI_BLKR_BLKLEN(block_length));
580 * Disable the PDC controller
582 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
584 if (cmdr & AT91_MCI_TRCMD_START) {
585 data->bytes_xfered = 0;
586 host->transfer_index = 0;
587 host->in_use_index = 0;
588 if (cmdr & AT91_MCI_TRDIR) {
593 host->total_length = 0;
595 at91_mci_pre_dma_read(host);
596 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
602 host->total_length = block_length * blocks;
603 host->buffer = dma_alloc_coherent(NULL,
605 &host->physical_address, GFP_KERNEL);
607 at91_mci_sg_to_dma(host, data);
609 pr_debug("Transmitting %d bytes\n", host->total_length);
611 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
612 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
613 host->total_length : host->total_length / 4);
615 ier = AT91_MCI_CMDRDY;
621 * Send the command and then enable the PDC - not the other way round as
622 * the data sheet says
625 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
626 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
628 if (cmdr & AT91_MCI_TRCMD_START) {
629 if (cmdr & AT91_MCI_TRDIR)
630 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
633 /* Enable selected interrupts */
634 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
638 * Process the next step in the request
640 static void at91_mci_process_next(struct at91mci_host *host)
642 if (!(host->flags & FL_SENT_COMMAND)) {
643 host->flags |= FL_SENT_COMMAND;
644 at91_mci_send_command(host, host->request->cmd);
646 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
647 host->flags |= FL_SENT_STOP;
648 at91_mci_send_command(host, host->request->stop);
650 del_timer(&host->timer);
651 /* the at91rm9200 mci controller hangs after some transfers,
652 * and the workaround is to reset it after each transfer.
654 if (cpu_is_at91rm9200())
655 at91_reset_host(host);
656 mmc_request_done(host->mmc, host->request);
661 * Handle a command that has been completed
663 static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
665 struct mmc_command *cmd = host->cmd;
667 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
669 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
670 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
671 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
672 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
675 dma_free_coherent(NULL, host->total_length, host->buffer, host->physical_address);
679 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
680 status, at91_mci_read(host, AT91_MCI_SR),
681 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
683 if (status & AT91_MCI_ERRORS) {
684 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
688 if (status & (AT91_MCI_RTOE | AT91_MCI_DTOE))
689 cmd->error = -ETIMEDOUT;
690 else if (status & (AT91_MCI_RCRCE | AT91_MCI_DCRCE))
691 cmd->error = -EILSEQ;
695 pr_debug("Error detected and set to %d (cmd = %d, retries = %d)\n",
696 cmd->error, cmd->opcode, cmd->retries);
702 at91_mci_process_next(host);
706 * Handle an MMC request
708 static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
710 struct at91mci_host *host = mmc_priv(mmc);
714 mod_timer(&host->timer, jiffies + HZ);
716 at91_mci_process_next(host);
722 static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
725 struct at91mci_host *host = mmc_priv(mmc);
726 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
728 host->bus_mode = ios->bus_mode;
730 if (ios->clock == 0) {
731 /* Disable the MCI controller */
732 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
736 /* Enable the MCI controller */
737 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
739 if ((at91_master_clock % (ios->clock * 2)) == 0)
740 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
742 clkdiv = (at91_master_clock / ios->clock) / 2;
744 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
745 at91_master_clock / (2 * (clkdiv + 1)));
747 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
748 pr_debug("MMC: Setting controller bus width to 4\n");
749 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
752 pr_debug("MMC: Setting controller bus width to 1\n");
753 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
756 /* Set the clock divider */
757 at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
759 /* maybe switch power to the card */
760 if (host->board->vcc_pin) {
761 switch (ios->power_mode) {
763 gpio_set_value(host->board->vcc_pin, 0);
766 gpio_set_value(host->board->vcc_pin, 1);
777 * Handle an interrupt
779 static irqreturn_t at91_mci_irq(int irq, void *devid)
781 struct at91mci_host *host = devid;
783 unsigned int int_status, int_mask;
785 int_status = at91_mci_read(host, AT91_MCI_SR);
786 int_mask = at91_mci_read(host, AT91_MCI_IMR);
788 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
789 int_status & int_mask);
791 int_status = int_status & int_mask;
793 if (int_status & AT91_MCI_ERRORS) {
796 if (int_status & AT91_MCI_UNRE)
797 pr_debug("MMC: Underrun error\n");
798 if (int_status & AT91_MCI_OVRE)
799 pr_debug("MMC: Overrun error\n");
800 if (int_status & AT91_MCI_DTOE)
801 pr_debug("MMC: Data timeout\n");
802 if (int_status & AT91_MCI_DCRCE)
803 pr_debug("MMC: CRC error in data\n");
804 if (int_status & AT91_MCI_RTOE)
805 pr_debug("MMC: Response timeout\n");
806 if (int_status & AT91_MCI_RENDE)
807 pr_debug("MMC: Response end bit error\n");
808 if (int_status & AT91_MCI_RCRCE)
809 pr_debug("MMC: Response CRC error\n");
810 if (int_status & AT91_MCI_RDIRE)
811 pr_debug("MMC: Response direction error\n");
812 if (int_status & AT91_MCI_RINDE)
813 pr_debug("MMC: Response index error\n");
815 /* Only continue processing if no errors */
817 if (int_status & AT91_MCI_TXBUFE) {
818 pr_debug("TX buffer empty\n");
819 at91_mci_handle_transmitted(host);
822 if (int_status & AT91_MCI_ENDRX) {
824 at91_mci_post_dma_read(host);
827 if (int_status & AT91_MCI_RXBUFF) {
828 pr_debug("RX buffer full\n");
829 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
830 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
834 if (int_status & AT91_MCI_ENDTX)
835 pr_debug("Transmit has ended\n");
837 if (int_status & AT91_MCI_NOTBUSY) {
838 pr_debug("Card is ready\n");
839 at91_mci_update_bytes_xfered(host);
843 if (int_status & AT91_MCI_DTIP)
844 pr_debug("Data transfer in progress\n");
846 if (int_status & AT91_MCI_BLKE) {
847 pr_debug("Block transfer has ended\n");
848 if (host->request->data && host->request->data->blocks > 1) {
849 /* multi block write : complete multi write
850 * command and send stop */
853 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
857 if (int_status & AT91_MCI_SDIOIRQA)
858 mmc_signal_sdio_irq(host->mmc);
860 if (int_status & AT91_MCI_SDIOIRQB)
861 mmc_signal_sdio_irq(host->mmc);
863 if (int_status & AT91_MCI_TXRDY)
864 pr_debug("Ready to transmit\n");
866 if (int_status & AT91_MCI_RXRDY)
867 pr_debug("Ready to receive\n");
869 if (int_status & AT91_MCI_CMDRDY) {
870 pr_debug("Command ready\n");
871 completed = at91_mci_handle_cmdrdy(host);
876 pr_debug("Completed command\n");
877 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
878 at91_mci_completed_command(host, int_status);
880 at91_mci_write(host, AT91_MCI_IDR, int_status & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
885 static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
887 struct at91mci_host *host = _host;
888 int present = !gpio_get_value(irq_to_gpio(irq));
891 * we expect this irq on both insert and remove,
892 * and use a short delay to debounce.
894 if (present != host->present) {
895 host->present = present;
896 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
897 present ? "insert" : "remove");
899 pr_debug("****** Resetting SD-card bus width ******\n");
900 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
902 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
907 static int at91_mci_get_ro(struct mmc_host *mmc)
909 struct at91mci_host *host = mmc_priv(mmc);
911 if (host->board->wp_pin)
912 return !!gpio_get_value(host->board->wp_pin);
914 * Board doesn't support read only detection; let the mmc core
920 static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
922 struct at91mci_host *host = mmc_priv(mmc);
924 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
925 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
926 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
927 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
931 static const struct mmc_host_ops at91_mci_ops = {
932 .request = at91_mci_request,
933 .set_ios = at91_mci_set_ios,
934 .get_ro = at91_mci_get_ro,
935 .enable_sdio_irq = at91_mci_enable_sdio_irq,
939 * Probe for the device
941 static int __init at91_mci_probe(struct platform_device *pdev)
943 struct mmc_host *mmc;
944 struct at91mci_host *host;
945 struct resource *res;
948 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
952 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
955 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
958 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
962 mmc->ops = &at91_mci_ops;
964 mmc->f_max = 25000000;
965 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
966 mmc->caps = MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
968 mmc->max_blk_size = 4095;
969 mmc->max_blk_count = mmc->max_req_size;
971 host = mmc_priv(mmc);
975 host->board = pdev->dev.platform_data;
976 if (host->board->wire4) {
977 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
978 mmc->caps |= MMC_CAP_4_BIT_DATA;
980 dev_warn(&pdev->dev, "4 wire bus mode not supported"
981 " - using 1 wire\n");
985 * Reserve GPIOs ... board init code makes sure these pins are set
986 * up as GPIOs with the right direction (input, except for vcc)
988 if (host->board->det_pin) {
989 ret = gpio_request(host->board->det_pin, "mmc_detect");
991 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
995 if (host->board->wp_pin) {
996 ret = gpio_request(host->board->wp_pin, "mmc_wp");
998 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
1002 if (host->board->vcc_pin) {
1003 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1005 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1013 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1014 if (IS_ERR(host->mci_clk)) {
1016 dev_dbg(&pdev->dev, "no mci_clk?\n");
1023 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1024 if (!host->baseaddr) {
1032 clk_enable(host->mci_clk); /* Enable the peripheral clock */
1033 at91_mci_disable(host);
1034 at91_mci_enable(host);
1037 * Allocate the MCI interrupt
1039 host->irq = platform_get_irq(pdev, 0);
1040 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1041 mmc_hostname(mmc), host);
1043 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1047 platform_set_drvdata(pdev, mmc);
1050 * Add host to MMC layer
1052 if (host->board->det_pin) {
1053 host->present = !gpio_get_value(host->board->det_pin);
1060 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1063 * monitor card insertion/removal if we can
1065 if (host->board->det_pin) {
1066 ret = request_irq(gpio_to_irq(host->board->det_pin),
1067 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
1069 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1071 device_init_wakeup(&pdev->dev, 1);
1074 pr_debug("Added MCI driver\n");
1079 clk_disable(host->mci_clk);
1080 iounmap(host->baseaddr);
1082 clk_put(host->mci_clk);
1084 if (host->board->vcc_pin)
1085 gpio_free(host->board->vcc_pin);
1087 if (host->board->wp_pin)
1088 gpio_free(host->board->wp_pin);
1090 if (host->board->det_pin)
1091 gpio_free(host->board->det_pin);
1095 release_mem_region(res->start, res->end - res->start + 1);
1096 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1103 static int __exit at91_mci_remove(struct platform_device *pdev)
1105 struct mmc_host *mmc = platform_get_drvdata(pdev);
1106 struct at91mci_host *host;
1107 struct resource *res;
1112 host = mmc_priv(mmc);
1114 if (host->board->det_pin) {
1115 if (device_can_wakeup(&pdev->dev))
1116 free_irq(gpio_to_irq(host->board->det_pin), host);
1117 device_init_wakeup(&pdev->dev, 0);
1118 gpio_free(host->board->det_pin);
1121 at91_mci_disable(host);
1122 del_timer_sync(&host->timer);
1123 mmc_remove_host(mmc);
1124 free_irq(host->irq, host);
1126 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1127 clk_put(host->mci_clk);
1129 if (host->board->vcc_pin)
1130 gpio_free(host->board->vcc_pin);
1131 if (host->board->wp_pin)
1132 gpio_free(host->board->wp_pin);
1134 iounmap(host->baseaddr);
1135 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1136 release_mem_region(res->start, res->end - res->start + 1);
1139 platform_set_drvdata(pdev, NULL);
1140 pr_debug("MCI Removed\n");
1146 static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1148 struct mmc_host *mmc = platform_get_drvdata(pdev);
1149 struct at91mci_host *host = mmc_priv(mmc);
1152 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1153 enable_irq_wake(host->board->det_pin);
1156 ret = mmc_suspend_host(mmc, state);
1161 static int at91_mci_resume(struct platform_device *pdev)
1163 struct mmc_host *mmc = platform_get_drvdata(pdev);
1164 struct at91mci_host *host = mmc_priv(mmc);
1167 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1168 disable_irq_wake(host->board->det_pin);
1171 ret = mmc_resume_host(mmc);
1176 #define at91_mci_suspend NULL
1177 #define at91_mci_resume NULL
1180 static struct platform_driver at91_mci_driver = {
1181 .remove = __exit_p(at91_mci_remove),
1182 .suspend = at91_mci_suspend,
1183 .resume = at91_mci_resume,
1185 .name = DRIVER_NAME,
1186 .owner = THIS_MODULE,
1190 static int __init at91_mci_init(void)
1192 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
1195 static void __exit at91_mci_exit(void)
1197 platform_driver_unregister(&at91_mci_driver);
1200 module_init(at91_mci_init);
1201 module_exit(at91_mci_exit);
1203 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1204 MODULE_AUTHOR("Nick Randell");
1205 MODULE_LICENSE("GPL");
1206 MODULE_ALIAS("platform:at91_mci");