mmc: at91_mci: modify cache flush routines
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / host / at91_mci.c
blob336d9f553f3e85ae6240b5a22c83ae0eb33fe3b0
1 /*
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
19 and at91_mci_get_ro.
21 SET IOS
22 This configures the device to put it into the correct mode and clock speed
23 required.
25 MCI REQUEST
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
52 GET RO
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>
68 #include <linux/gfp.h>
70 #include <linux/mmc/host.h>
72 #include <asm/io.h>
73 #include <asm/irq.h>
74 #include <asm/gpio.h>
76 #include <mach/board.h>
77 #include <mach/cpu.h>
78 #include <mach/at91_mci.h>
80 #define DRIVER_NAME "at91_mci"
82 static inline int at91mci_is_mci1rev2xx(void)
84 return ( cpu_is_at91sam9260()
85 || cpu_is_at91sam9263()
86 || cpu_is_at91cap9()
87 || cpu_is_at91sam9rl()
88 || cpu_is_at91sam9g10()
89 || cpu_is_at91sam9g20()
93 #define FL_SENT_COMMAND (1 << 0)
94 #define FL_SENT_STOP (1 << 1)
96 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
97 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
98 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
100 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
101 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
103 #define MCI_BLKSIZE 512
104 #define MCI_MAXBLKSIZE 4095
105 #define MCI_BLKATONCE 256
106 #define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
109 * Low level type for this driver
111 struct at91mci_host
113 struct mmc_host *mmc;
114 struct mmc_command *cmd;
115 struct mmc_request *request;
117 void __iomem *baseaddr;
118 int irq;
120 struct at91_mmc_data *board;
121 int present;
123 struct clk *mci_clk;
126 * Flag indicating when the command has been sent. This is used to
127 * work out whether or not to send the stop
129 unsigned int flags;
130 /* flag for current bus settings */
131 u32 bus_mode;
133 /* DMA buffer used for transmitting */
134 unsigned int* buffer;
135 dma_addr_t physical_address;
136 unsigned int total_length;
138 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
139 int in_use_index;
141 /* Latest in the scatterlist that has been enabled for transfer */
142 int transfer_index;
144 /* Timer for timeouts */
145 struct timer_list timer;
149 * Reset the controller and restore most of the state
151 static void at91_reset_host(struct at91mci_host *host)
153 unsigned long flags;
154 u32 mr;
155 u32 sdcr;
156 u32 dtor;
157 u32 imr;
159 local_irq_save(flags);
160 imr = at91_mci_read(host, AT91_MCI_IMR);
162 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
164 /* save current state */
165 mr = at91_mci_read(host, AT91_MCI_MR) & 0x7fff;
166 sdcr = at91_mci_read(host, AT91_MCI_SDCR);
167 dtor = at91_mci_read(host, AT91_MCI_DTOR);
169 /* reset the controller */
170 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
172 /* restore state */
173 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
174 at91_mci_write(host, AT91_MCI_MR, mr);
175 at91_mci_write(host, AT91_MCI_SDCR, sdcr);
176 at91_mci_write(host, AT91_MCI_DTOR, dtor);
177 at91_mci_write(host, AT91_MCI_IER, imr);
179 /* make sure sdio interrupts will fire */
180 at91_mci_read(host, AT91_MCI_SR);
182 local_irq_restore(flags);
185 static void at91_timeout_timer(unsigned long data)
187 struct at91mci_host *host;
189 host = (struct at91mci_host *)data;
191 if (host->request) {
192 dev_err(host->mmc->parent, "Timeout waiting end of packet\n");
194 if (host->cmd && host->cmd->data) {
195 host->cmd->data->error = -ETIMEDOUT;
196 } else {
197 if (host->cmd)
198 host->cmd->error = -ETIMEDOUT;
199 else
200 host->request->cmd->error = -ETIMEDOUT;
203 at91_reset_host(host);
204 mmc_request_done(host->mmc, host->request);
209 * Copy from sg to a dma block - used for transfers
211 static inline void at91_mci_sg_to_dma(struct at91mci_host *host, struct mmc_data *data)
213 unsigned int len, i, size;
214 unsigned *dmabuf = host->buffer;
216 size = data->blksz * data->blocks;
217 len = data->sg_len;
219 /* MCI1 rev2xx Data Write Operation and number of bytes erratum */
220 if (at91mci_is_mci1rev2xx())
221 if (host->total_length == 12)
222 memset(dmabuf, 0, 12);
225 * Just loop through all entries. Size might not
226 * be the entire list though so make sure that
227 * we do not transfer too much.
229 for (i = 0; i < len; i++) {
230 struct scatterlist *sg;
231 int amount;
232 unsigned int *sgbuffer;
234 sg = &data->sg[i];
236 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
237 amount = min(size, sg->length);
238 size -= amount;
240 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
241 int index;
243 for (index = 0; index < (amount / 4); index++)
244 *dmabuf++ = swab32(sgbuffer[index]);
245 } else {
246 char *tmpv = (char *)dmabuf;
247 memcpy(tmpv, sgbuffer, amount);
248 tmpv += amount;
249 dmabuf = (unsigned *)tmpv;
252 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
254 if (size == 0)
255 break;
259 * Check that we didn't get a request to transfer
260 * more data than can fit into the SG list.
262 BUG_ON(size != 0);
266 * Handle after a dma read
268 static void at91_mci_post_dma_read(struct at91mci_host *host)
270 struct mmc_command *cmd;
271 struct mmc_data *data;
272 unsigned int len, i, size;
273 unsigned *dmabuf = host->buffer;
275 pr_debug("post dma read\n");
277 cmd = host->cmd;
278 if (!cmd) {
279 pr_debug("no command\n");
280 return;
283 data = cmd->data;
284 if (!data) {
285 pr_debug("no data\n");
286 return;
289 size = data->blksz * data->blocks;
290 len = data->sg_len;
292 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_ENDRX);
293 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_RXBUFF);
295 for (i = 0; i < len; i++) {
296 struct scatterlist *sg;
297 int amount;
298 unsigned int *sgbuffer;
300 sg = &data->sg[i];
302 sgbuffer = kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
303 amount = min(size, sg->length);
304 size -= amount;
306 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
307 int index;
308 for (index = 0; index < (amount / 4); index++)
309 sgbuffer[index] = swab32(*dmabuf++);
310 } else {
311 char *tmpv = (char *)dmabuf;
312 memcpy(sgbuffer, tmpv, amount);
313 tmpv += amount;
314 dmabuf = (unsigned *)tmpv;
317 flush_kernel_dcache_page(sg_page(sg));
318 kunmap_atomic(sgbuffer, KM_BIO_SRC_IRQ);
319 data->bytes_xfered += amount;
320 if (size == 0)
321 break;
324 pr_debug("post dma read done\n");
328 * Handle transmitted data
330 static void at91_mci_handle_transmitted(struct at91mci_host *host)
332 struct mmc_command *cmd;
333 struct mmc_data *data;
335 pr_debug("Handling the transmit\n");
337 /* Disable the transfer */
338 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
340 /* Now wait for cmd ready */
341 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_TXBUFE);
343 cmd = host->cmd;
344 if (!cmd) return;
346 data = cmd->data;
347 if (!data) return;
349 if (cmd->data->blocks > 1) {
350 pr_debug("multiple write : wait for BLKE...\n");
351 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_BLKE);
352 } else
353 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
357 * Update bytes tranfered count during a write operation
359 static void at91_mci_update_bytes_xfered(struct at91mci_host *host)
361 struct mmc_data *data;
363 /* always deal with the effective request (and not the current cmd) */
365 if (host->request->cmd && host->request->cmd->error != 0)
366 return;
368 if (host->request->data) {
369 data = host->request->data;
370 if (data->flags & MMC_DATA_WRITE) {
371 /* card is in IDLE mode now */
372 pr_debug("-> bytes_xfered %d, total_length = %d\n",
373 data->bytes_xfered, host->total_length);
374 data->bytes_xfered = data->blksz * data->blocks;
380 /*Handle after command sent ready*/
381 static int at91_mci_handle_cmdrdy(struct at91mci_host *host)
383 if (!host->cmd)
384 return 1;
385 else if (!host->cmd->data) {
386 if (host->flags & FL_SENT_STOP) {
387 /*After multi block write, we must wait for NOTBUSY*/
388 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
389 } else return 1;
390 } else if (host->cmd->data->flags & MMC_DATA_WRITE) {
391 /*After sendding multi-block-write command, start DMA transfer*/
392 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_TXBUFE | AT91_MCI_BLKE);
393 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
396 /* command not completed, have to wait */
397 return 0;
402 * Enable the controller
404 static void at91_mci_enable(struct at91mci_host *host)
406 unsigned int mr;
408 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
409 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff);
410 at91_mci_write(host, AT91_MCI_DTOR, AT91_MCI_DTOMUL_1M | AT91_MCI_DTOCYC);
411 mr = AT91_MCI_PDCMODE | 0x34a;
413 if (at91mci_is_mci1rev2xx())
414 mr |= AT91_MCI_RDPROOF | AT91_MCI_WRPROOF;
416 at91_mci_write(host, AT91_MCI_MR, mr);
418 /* use Slot A or B (only one at same time) */
419 at91_mci_write(host, AT91_MCI_SDCR, host->board->slot_b);
423 * Disable the controller
425 static void at91_mci_disable(struct at91mci_host *host)
427 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS | AT91_MCI_SWRST);
431 * Send a command
433 static void at91_mci_send_command(struct at91mci_host *host, struct mmc_command *cmd)
435 unsigned int cmdr, mr;
436 unsigned int block_length;
437 struct mmc_data *data = cmd->data;
439 unsigned int blocks;
440 unsigned int ier = 0;
442 host->cmd = cmd;
444 /* Needed for leaving busy state before CMD1 */
445 if ((at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_RTOE) && (cmd->opcode == 1)) {
446 pr_debug("Clearing timeout\n");
447 at91_mci_write(host, AT91_MCI_ARGR, 0);
448 at91_mci_write(host, AT91_MCI_CMDR, AT91_MCI_OPDCMD);
449 while (!(at91_mci_read(host, AT91_MCI_SR) & AT91_MCI_CMDRDY)) {
450 /* spin */
451 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host, AT91_MCI_SR));
455 cmdr = cmd->opcode;
457 if (mmc_resp_type(cmd) == MMC_RSP_NONE)
458 cmdr |= AT91_MCI_RSPTYP_NONE;
459 else {
460 /* if a response is expected then allow maximum response latancy */
461 cmdr |= AT91_MCI_MAXLAT;
462 /* set 136 bit response for R2, 48 bit response otherwise */
463 if (mmc_resp_type(cmd) == MMC_RSP_R2)
464 cmdr |= AT91_MCI_RSPTYP_136;
465 else
466 cmdr |= AT91_MCI_RSPTYP_48;
469 if (data) {
471 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
472 if (data->blksz & 0x3) {
473 pr_debug("Unsupported block size\n");
474 cmd->error = -EINVAL;
475 mmc_request_done(host->mmc, host->request);
476 return;
478 if (data->flags & MMC_DATA_STREAM) {
479 pr_debug("Stream commands not supported\n");
480 cmd->error = -EINVAL;
481 mmc_request_done(host->mmc, host->request);
482 return;
486 block_length = data->blksz;
487 blocks = data->blocks;
489 /* always set data start - also set direction flag for read */
490 if (data->flags & MMC_DATA_READ)
491 cmdr |= (AT91_MCI_TRDIR | AT91_MCI_TRCMD_START);
492 else if (data->flags & MMC_DATA_WRITE)
493 cmdr |= AT91_MCI_TRCMD_START;
495 if (data->flags & MMC_DATA_STREAM)
496 cmdr |= AT91_MCI_TRTYP_STREAM;
497 if (data->blocks > 1)
498 cmdr |= AT91_MCI_TRTYP_MULTIPLE;
500 else {
501 block_length = 0;
502 blocks = 0;
505 if (host->flags & FL_SENT_STOP)
506 cmdr |= AT91_MCI_TRCMD_STOP;
508 if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
509 cmdr |= AT91_MCI_OPDCMD;
512 * Set the arguments and send the command
514 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
515 cmd->opcode, cmdr, cmd->arg, blocks, block_length, at91_mci_read(host, AT91_MCI_MR));
517 if (!data) {
518 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS | ATMEL_PDC_RXTDIS);
519 at91_mci_write(host, ATMEL_PDC_RPR, 0);
520 at91_mci_write(host, ATMEL_PDC_RCR, 0);
521 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
522 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
523 at91_mci_write(host, ATMEL_PDC_TPR, 0);
524 at91_mci_write(host, ATMEL_PDC_TCR, 0);
525 at91_mci_write(host, ATMEL_PDC_TNPR, 0);
526 at91_mci_write(host, ATMEL_PDC_TNCR, 0);
527 ier = AT91_MCI_CMDRDY;
528 } else {
529 /* zero block length and PDC mode */
530 mr = at91_mci_read(host, AT91_MCI_MR) & 0x5fff;
531 mr |= (data->blksz & 0x3) ? AT91_MCI_PDCFBYTE : 0;
532 mr |= (block_length << 16);
533 mr |= AT91_MCI_PDCMODE;
534 at91_mci_write(host, AT91_MCI_MR, mr);
536 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
537 at91_mci_write(host, AT91_MCI_BLKR,
538 AT91_MCI_BLKR_BCNT(blocks) |
539 AT91_MCI_BLKR_BLKLEN(block_length));
542 * Disable the PDC controller
544 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
546 if (cmdr & AT91_MCI_TRCMD_START) {
547 data->bytes_xfered = 0;
548 host->transfer_index = 0;
549 host->in_use_index = 0;
550 if (cmdr & AT91_MCI_TRDIR) {
552 * Handle a read
554 host->total_length = 0;
556 at91_mci_write(host, ATMEL_PDC_RPR, host->physical_address);
557 at91_mci_write(host, ATMEL_PDC_RCR, (data->blksz & 0x3) ?
558 (blocks * block_length) : (blocks * block_length) / 4);
559 at91_mci_write(host, ATMEL_PDC_RNPR, 0);
560 at91_mci_write(host, ATMEL_PDC_RNCR, 0);
562 ier = AT91_MCI_ENDRX /* | AT91_MCI_RXBUFF */;
564 else {
566 * Handle a write
568 host->total_length = block_length * blocks;
570 * MCI1 rev2xx Data Write Operation and
571 * number of bytes erratum
573 if (at91mci_is_mci1rev2xx())
574 if (host->total_length < 12)
575 host->total_length = 12;
577 at91_mci_sg_to_dma(host, data);
579 pr_debug("Transmitting %d bytes\n", host->total_length);
581 at91_mci_write(host, ATMEL_PDC_TPR, host->physical_address);
582 at91_mci_write(host, ATMEL_PDC_TCR, (data->blksz & 0x3) ?
583 host->total_length : host->total_length / 4);
585 ier = AT91_MCI_CMDRDY;
591 * Send the command and then enable the PDC - not the other way round as
592 * the data sheet says
595 at91_mci_write(host, AT91_MCI_ARGR, cmd->arg);
596 at91_mci_write(host, AT91_MCI_CMDR, cmdr);
598 if (cmdr & AT91_MCI_TRCMD_START) {
599 if (cmdr & AT91_MCI_TRDIR)
600 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
603 /* Enable selected interrupts */
604 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_ERRORS | ier);
608 * Process the next step in the request
610 static void at91_mci_process_next(struct at91mci_host *host)
612 if (!(host->flags & FL_SENT_COMMAND)) {
613 host->flags |= FL_SENT_COMMAND;
614 at91_mci_send_command(host, host->request->cmd);
616 else if ((!(host->flags & FL_SENT_STOP)) && host->request->stop) {
617 host->flags |= FL_SENT_STOP;
618 at91_mci_send_command(host, host->request->stop);
619 } else {
620 del_timer(&host->timer);
621 /* the at91rm9200 mci controller hangs after some transfers,
622 * and the workaround is to reset it after each transfer.
624 if (cpu_is_at91rm9200())
625 at91_reset_host(host);
626 mmc_request_done(host->mmc, host->request);
631 * Handle a command that has been completed
633 static void at91_mci_completed_command(struct at91mci_host *host, unsigned int status)
635 struct mmc_command *cmd = host->cmd;
636 struct mmc_data *data = cmd->data;
638 at91_mci_write(host, AT91_MCI_IDR, 0xffffffff & ~(AT91_MCI_SDIOIRQA | AT91_MCI_SDIOIRQB));
640 cmd->resp[0] = at91_mci_read(host, AT91_MCI_RSPR(0));
641 cmd->resp[1] = at91_mci_read(host, AT91_MCI_RSPR(1));
642 cmd->resp[2] = at91_mci_read(host, AT91_MCI_RSPR(2));
643 cmd->resp[3] = at91_mci_read(host, AT91_MCI_RSPR(3));
645 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
646 status, at91_mci_read(host, AT91_MCI_SR),
647 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
649 if (status & AT91_MCI_ERRORS) {
650 if ((status & AT91_MCI_RCRCE) && !(mmc_resp_type(cmd) & MMC_RSP_CRC)) {
651 cmd->error = 0;
653 else {
654 if (status & (AT91_MCI_DTOE | AT91_MCI_DCRCE)) {
655 if (data) {
656 if (status & AT91_MCI_DTOE)
657 data->error = -ETIMEDOUT;
658 else if (status & AT91_MCI_DCRCE)
659 data->error = -EILSEQ;
661 } else {
662 if (status & AT91_MCI_RTOE)
663 cmd->error = -ETIMEDOUT;
664 else if (status & AT91_MCI_RCRCE)
665 cmd->error = -EILSEQ;
666 else
667 cmd->error = -EIO;
670 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
671 cmd->error, data ? data->error : 0,
672 cmd->opcode, cmd->retries);
675 else
676 cmd->error = 0;
678 at91_mci_process_next(host);
682 * Handle an MMC request
684 static void at91_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
686 struct at91mci_host *host = mmc_priv(mmc);
687 host->request = mrq;
688 host->flags = 0;
690 /* more than 1s timeout needed with slow SD cards */
691 mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000));
693 at91_mci_process_next(host);
697 * Set the IOS
699 static void at91_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
701 int clkdiv;
702 struct at91mci_host *host = mmc_priv(mmc);
703 unsigned long at91_master_clock = clk_get_rate(host->mci_clk);
705 host->bus_mode = ios->bus_mode;
707 if (ios->clock == 0) {
708 /* Disable the MCI controller */
709 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIDIS);
710 clkdiv = 0;
712 else {
713 /* Enable the MCI controller */
714 at91_mci_write(host, AT91_MCI_CR, AT91_MCI_MCIEN);
716 if ((at91_master_clock % (ios->clock * 2)) == 0)
717 clkdiv = ((at91_master_clock / ios->clock) / 2) - 1;
718 else
719 clkdiv = (at91_master_clock / ios->clock) / 2;
721 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv,
722 at91_master_clock / (2 * (clkdiv + 1)));
724 if (ios->bus_width == MMC_BUS_WIDTH_4 && host->board->wire4) {
725 pr_debug("MMC: Setting controller bus width to 4\n");
726 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) | AT91_MCI_SDCBUS);
728 else {
729 pr_debug("MMC: Setting controller bus width to 1\n");
730 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
733 /* Set the clock divider */
734 at91_mci_write(host, AT91_MCI_MR, (at91_mci_read(host, AT91_MCI_MR) & ~AT91_MCI_CLKDIV) | clkdiv);
736 /* maybe switch power to the card */
737 if (host->board->vcc_pin) {
738 switch (ios->power_mode) {
739 case MMC_POWER_OFF:
740 gpio_set_value(host->board->vcc_pin, 0);
741 break;
742 case MMC_POWER_UP:
743 gpio_set_value(host->board->vcc_pin, 1);
744 break;
745 case MMC_POWER_ON:
746 break;
747 default:
748 WARN_ON(1);
754 * Handle an interrupt
756 static irqreturn_t at91_mci_irq(int irq, void *devid)
758 struct at91mci_host *host = devid;
759 int completed = 0;
760 unsigned int int_status, int_mask;
762 int_status = at91_mci_read(host, AT91_MCI_SR);
763 int_mask = at91_mci_read(host, AT91_MCI_IMR);
765 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status, int_mask,
766 int_status & int_mask);
768 int_status = int_status & int_mask;
770 if (int_status & AT91_MCI_ERRORS) {
771 completed = 1;
773 if (int_status & AT91_MCI_UNRE)
774 pr_debug("MMC: Underrun error\n");
775 if (int_status & AT91_MCI_OVRE)
776 pr_debug("MMC: Overrun error\n");
777 if (int_status & AT91_MCI_DTOE)
778 pr_debug("MMC: Data timeout\n");
779 if (int_status & AT91_MCI_DCRCE)
780 pr_debug("MMC: CRC error in data\n");
781 if (int_status & AT91_MCI_RTOE)
782 pr_debug("MMC: Response timeout\n");
783 if (int_status & AT91_MCI_RENDE)
784 pr_debug("MMC: Response end bit error\n");
785 if (int_status & AT91_MCI_RCRCE)
786 pr_debug("MMC: Response CRC error\n");
787 if (int_status & AT91_MCI_RDIRE)
788 pr_debug("MMC: Response direction error\n");
789 if (int_status & AT91_MCI_RINDE)
790 pr_debug("MMC: Response index error\n");
791 } else {
792 /* Only continue processing if no errors */
794 if (int_status & AT91_MCI_TXBUFE) {
795 pr_debug("TX buffer empty\n");
796 at91_mci_handle_transmitted(host);
799 if (int_status & AT91_MCI_ENDRX) {
800 pr_debug("ENDRX\n");
801 at91_mci_post_dma_read(host);
804 if (int_status & AT91_MCI_RXBUFF) {
805 pr_debug("RX buffer full\n");
806 at91_mci_write(host, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS | ATMEL_PDC_TXTDIS);
807 at91_mci_write(host, AT91_MCI_IDR, AT91_MCI_RXBUFF | AT91_MCI_ENDRX);
808 completed = 1;
811 if (int_status & AT91_MCI_ENDTX)
812 pr_debug("Transmit has ended\n");
814 if (int_status & AT91_MCI_NOTBUSY) {
815 pr_debug("Card is ready\n");
816 at91_mci_update_bytes_xfered(host);
817 completed = 1;
820 if (int_status & AT91_MCI_DTIP)
821 pr_debug("Data transfer in progress\n");
823 if (int_status & AT91_MCI_BLKE) {
824 pr_debug("Block transfer has ended\n");
825 if (host->request->data && host->request->data->blocks > 1) {
826 /* multi block write : complete multi write
827 * command and send stop */
828 completed = 1;
829 } else {
830 at91_mci_write(host, AT91_MCI_IER, AT91_MCI_NOTBUSY);
834 if (int_status & AT91_MCI_SDIOIRQA)
835 mmc_signal_sdio_irq(host->mmc);
837 if (int_status & AT91_MCI_SDIOIRQB)
838 mmc_signal_sdio_irq(host->mmc);
840 if (int_status & AT91_MCI_TXRDY)
841 pr_debug("Ready to transmit\n");
843 if (int_status & AT91_MCI_RXRDY)
844 pr_debug("Ready to receive\n");
846 if (int_status & AT91_MCI_CMDRDY) {
847 pr_debug("Command ready\n");
848 completed = at91_mci_handle_cmdrdy(host);
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));
859 return IRQ_HANDLED;
862 static irqreturn_t at91_mmc_det_irq(int irq, void *_host)
864 struct at91mci_host *host = _host;
865 int present = !gpio_get_value(irq_to_gpio(irq));
868 * we expect this irq on both insert and remove,
869 * and use a short delay to debounce.
871 if (present != host->present) {
872 host->present = present;
873 pr_debug("%s: card %s\n", mmc_hostname(host->mmc),
874 present ? "insert" : "remove");
875 if (!present) {
876 pr_debug("****** Resetting SD-card bus width ******\n");
877 at91_mci_write(host, AT91_MCI_SDCR, at91_mci_read(host, AT91_MCI_SDCR) & ~AT91_MCI_SDCBUS);
879 /* 0.5s needed because of early card detect switch firing */
880 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
882 return IRQ_HANDLED;
885 static int at91_mci_get_ro(struct mmc_host *mmc)
887 struct at91mci_host *host = mmc_priv(mmc);
889 if (host->board->wp_pin)
890 return !!gpio_get_value(host->board->wp_pin);
892 * Board doesn't support read only detection; let the mmc core
893 * decide what to do.
895 return -ENOSYS;
898 static void at91_mci_enable_sdio_irq(struct mmc_host *mmc, int enable)
900 struct at91mci_host *host = mmc_priv(mmc);
902 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host->mmc),
903 host->board->slot_b ? 'B':'A', enable ? "enable" : "disable");
904 at91_mci_write(host, enable ? AT91_MCI_IER : AT91_MCI_IDR,
905 host->board->slot_b ? AT91_MCI_SDIOIRQB : AT91_MCI_SDIOIRQA);
909 static const struct mmc_host_ops at91_mci_ops = {
910 .request = at91_mci_request,
911 .set_ios = at91_mci_set_ios,
912 .get_ro = at91_mci_get_ro,
913 .enable_sdio_irq = at91_mci_enable_sdio_irq,
917 * Probe for the device
919 static int __init at91_mci_probe(struct platform_device *pdev)
921 struct mmc_host *mmc;
922 struct at91mci_host *host;
923 struct resource *res;
924 int ret;
926 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
927 if (!res)
928 return -ENXIO;
930 if (!request_mem_region(res->start, res->end - res->start + 1, DRIVER_NAME))
931 return -EBUSY;
933 mmc = mmc_alloc_host(sizeof(struct at91mci_host), &pdev->dev);
934 if (!mmc) {
935 ret = -ENOMEM;
936 dev_dbg(&pdev->dev, "couldn't allocate mmc host\n");
937 goto fail6;
940 mmc->ops = &at91_mci_ops;
941 mmc->f_min = 375000;
942 mmc->f_max = 25000000;
943 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
944 mmc->caps = 0;
946 mmc->max_blk_size = MCI_MAXBLKSIZE;
947 mmc->max_blk_count = MCI_BLKATONCE;
948 mmc->max_req_size = MCI_BUFSIZE;
949 mmc->max_phys_segs = MCI_BLKATONCE;
950 mmc->max_hw_segs = MCI_BLKATONCE;
951 mmc->max_seg_size = MCI_BUFSIZE;
953 host = mmc_priv(mmc);
954 host->mmc = mmc;
955 host->bus_mode = 0;
956 host->board = pdev->dev.platform_data;
957 if (host->board->wire4) {
958 if (at91mci_is_mci1rev2xx())
959 mmc->caps |= MMC_CAP_4_BIT_DATA;
960 else
961 dev_warn(&pdev->dev, "4 wire bus mode not supported"
962 " - using 1 wire\n");
965 host->buffer = dma_alloc_coherent(&pdev->dev, MCI_BUFSIZE,
966 &host->physical_address, GFP_KERNEL);
967 if (!host->buffer) {
968 ret = -ENOMEM;
969 dev_err(&pdev->dev, "Can't allocate transmit buffer\n");
970 goto fail5;
973 /* Add SDIO capability when available */
974 if (at91mci_is_mci1rev2xx()) {
975 /* at91mci MCI1 rev2xx sdio interrupt erratum */
976 if (host->board->wire4 || !host->board->slot_b)
977 mmc->caps |= MMC_CAP_SDIO_IRQ;
981 * Reserve GPIOs ... board init code makes sure these pins are set
982 * up as GPIOs with the right direction (input, except for vcc)
984 if (host->board->det_pin) {
985 ret = gpio_request(host->board->det_pin, "mmc_detect");
986 if (ret < 0) {
987 dev_dbg(&pdev->dev, "couldn't claim card detect pin\n");
988 goto fail4b;
991 if (host->board->wp_pin) {
992 ret = gpio_request(host->board->wp_pin, "mmc_wp");
993 if (ret < 0) {
994 dev_dbg(&pdev->dev, "couldn't claim wp sense pin\n");
995 goto fail4;
998 if (host->board->vcc_pin) {
999 ret = gpio_request(host->board->vcc_pin, "mmc_vcc");
1000 if (ret < 0) {
1001 dev_dbg(&pdev->dev, "couldn't claim vcc switch pin\n");
1002 goto fail3;
1007 * Get Clock
1009 host->mci_clk = clk_get(&pdev->dev, "mci_clk");
1010 if (IS_ERR(host->mci_clk)) {
1011 ret = -ENODEV;
1012 dev_dbg(&pdev->dev, "no mci_clk?\n");
1013 goto fail2;
1017 * Map I/O region
1019 host->baseaddr = ioremap(res->start, res->end - res->start + 1);
1020 if (!host->baseaddr) {
1021 ret = -ENOMEM;
1022 goto fail1;
1026 * Reset hardware
1028 clk_enable(host->mci_clk); /* Enable the peripheral clock */
1029 at91_mci_disable(host);
1030 at91_mci_enable(host);
1033 * Allocate the MCI interrupt
1035 host->irq = platform_get_irq(pdev, 0);
1036 ret = request_irq(host->irq, at91_mci_irq, IRQF_SHARED,
1037 mmc_hostname(mmc), host);
1038 if (ret) {
1039 dev_dbg(&pdev->dev, "request MCI interrupt failed\n");
1040 goto fail0;
1043 setup_timer(&host->timer, at91_timeout_timer, (unsigned long)host);
1045 platform_set_drvdata(pdev, mmc);
1048 * Add host to MMC layer
1050 if (host->board->det_pin) {
1051 host->present = !gpio_get_value(host->board->det_pin);
1053 else
1054 host->present = -1;
1056 mmc_add_host(mmc);
1059 * monitor card insertion/removal if we can
1061 if (host->board->det_pin) {
1062 ret = request_irq(gpio_to_irq(host->board->det_pin),
1063 at91_mmc_det_irq, 0, mmc_hostname(mmc), host);
1064 if (ret)
1065 dev_warn(&pdev->dev, "request MMC detect irq failed\n");
1066 else
1067 device_init_wakeup(&pdev->dev, 1);
1070 pr_debug("Added MCI driver\n");
1072 return 0;
1074 fail0:
1075 clk_disable(host->mci_clk);
1076 iounmap(host->baseaddr);
1077 fail1:
1078 clk_put(host->mci_clk);
1079 fail2:
1080 if (host->board->vcc_pin)
1081 gpio_free(host->board->vcc_pin);
1082 fail3:
1083 if (host->board->wp_pin)
1084 gpio_free(host->board->wp_pin);
1085 fail4:
1086 if (host->board->det_pin)
1087 gpio_free(host->board->det_pin);
1088 fail4b:
1089 if (host->buffer)
1090 dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
1091 host->buffer, host->physical_address);
1092 fail5:
1093 mmc_free_host(mmc);
1094 fail6:
1095 release_mem_region(res->start, res->end - res->start + 1);
1096 dev_err(&pdev->dev, "probe failed, err %d\n", ret);
1097 return ret;
1101 * Remove a device
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;
1109 if (!mmc)
1110 return -1;
1112 host = mmc_priv(mmc);
1114 if (host->buffer)
1115 dma_free_coherent(&pdev->dev, MCI_BUFSIZE,
1116 host->buffer, host->physical_address);
1118 if (host->board->det_pin) {
1119 if (device_can_wakeup(&pdev->dev))
1120 free_irq(gpio_to_irq(host->board->det_pin), host);
1121 device_init_wakeup(&pdev->dev, 0);
1122 gpio_free(host->board->det_pin);
1125 at91_mci_disable(host);
1126 del_timer_sync(&host->timer);
1127 mmc_remove_host(mmc);
1128 free_irq(host->irq, host);
1130 clk_disable(host->mci_clk); /* Disable the peripheral clock */
1131 clk_put(host->mci_clk);
1133 if (host->board->vcc_pin)
1134 gpio_free(host->board->vcc_pin);
1135 if (host->board->wp_pin)
1136 gpio_free(host->board->wp_pin);
1138 iounmap(host->baseaddr);
1139 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1140 release_mem_region(res->start, res->end - res->start + 1);
1142 mmc_free_host(mmc);
1143 platform_set_drvdata(pdev, NULL);
1144 pr_debug("MCI Removed\n");
1146 return 0;
1149 #ifdef CONFIG_PM
1150 static int at91_mci_suspend(struct platform_device *pdev, pm_message_t state)
1152 struct mmc_host *mmc = platform_get_drvdata(pdev);
1153 struct at91mci_host *host = mmc_priv(mmc);
1154 int ret = 0;
1156 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1157 enable_irq_wake(host->board->det_pin);
1159 if (mmc)
1160 ret = mmc_suspend_host(mmc, state);
1162 return ret;
1165 static int at91_mci_resume(struct platform_device *pdev)
1167 struct mmc_host *mmc = platform_get_drvdata(pdev);
1168 struct at91mci_host *host = mmc_priv(mmc);
1169 int ret = 0;
1171 if (host->board->det_pin && device_may_wakeup(&pdev->dev))
1172 disable_irq_wake(host->board->det_pin);
1174 if (mmc)
1175 ret = mmc_resume_host(mmc);
1177 return ret;
1179 #else
1180 #define at91_mci_suspend NULL
1181 #define at91_mci_resume NULL
1182 #endif
1184 static struct platform_driver at91_mci_driver = {
1185 .remove = __exit_p(at91_mci_remove),
1186 .suspend = at91_mci_suspend,
1187 .resume = at91_mci_resume,
1188 .driver = {
1189 .name = DRIVER_NAME,
1190 .owner = THIS_MODULE,
1194 static int __init at91_mci_init(void)
1196 return platform_driver_probe(&at91_mci_driver, at91_mci_probe);
1199 static void __exit at91_mci_exit(void)
1201 platform_driver_unregister(&at91_mci_driver);
1204 module_init(at91_mci_init);
1205 module_exit(at91_mci_exit);
1207 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1208 MODULE_AUTHOR("Nick Randell");
1209 MODULE_LICENSE("GPL");
1210 MODULE_ALIAS("platform:at91_mci");