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>
68 #include <linux/gfp.h>
69 #include <linux/highmem.h>
71 #include <linux/mmc/host.h>
72 #include <linux/mmc/sdio.h>
78 #include <mach/board.h>
83 #define DRIVER_NAME "at91_mci"
85 static inline int at91mci_is_mci1rev2xx(void)
87 return ( cpu_is_at91sam9260()
88 || cpu_is_at91sam9263()
90 || cpu_is_at91sam9rl()
91 || cpu_is_at91sam9g10()
92 || cpu_is_at91sam9g20()
96 #define FL_SENT_COMMAND (1 << 0)
97 #define FL_SENT_STOP (1 << 1)
99 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
100 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
101 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
103 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
104 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
106 #define MCI_BLKSIZE 512
107 #define MCI_MAXBLKSIZE 4095
108 #define MCI_BLKATONCE 256
109 #define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
112 * Low level type for this driver
116 struct mmc_host
*mmc
;
117 struct mmc_command
*cmd
;
118 struct mmc_request
*request
;
120 void __iomem
*baseaddr
;
123 struct at91_mmc_data
*board
;
129 * Flag indicating when the command has been sent. This is used to
130 * work out whether or not to send the stop
133 /* flag for current bus settings */
136 /* DMA buffer used for transmitting */
137 unsigned int* buffer
;
138 dma_addr_t physical_address
;
139 unsigned int total_length
;
141 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
144 /* Latest in the scatterlist that has been enabled for transfer */
147 /* Timer for timeouts */
148 struct timer_list timer
;
152 * Reset the controller and restore most of the state
154 static void at91_reset_host(struct at91mci_host
*host
)
162 local_irq_save(flags
);
163 imr
= at91_mci_read(host
, AT91_MCI_IMR
);
165 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff);
167 /* save current state */
168 mr
= at91_mci_read(host
, AT91_MCI_MR
) & 0x7fff;
169 sdcr
= at91_mci_read(host
, AT91_MCI_SDCR
);
170 dtor
= at91_mci_read(host
, AT91_MCI_DTOR
);
172 /* reset the controller */
173 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
| AT91_MCI_SWRST
);
176 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
177 at91_mci_write(host
, AT91_MCI_MR
, mr
);
178 at91_mci_write(host
, AT91_MCI_SDCR
, sdcr
);
179 at91_mci_write(host
, AT91_MCI_DTOR
, dtor
);
180 at91_mci_write(host
, AT91_MCI_IER
, imr
);
182 /* make sure sdio interrupts will fire */
183 at91_mci_read(host
, AT91_MCI_SR
);
185 local_irq_restore(flags
);
188 static void at91_timeout_timer(unsigned long data
)
190 struct at91mci_host
*host
;
192 host
= (struct at91mci_host
*)data
;
195 dev_err(host
->mmc
->parent
, "Timeout waiting end of packet\n");
197 if (host
->cmd
&& host
->cmd
->data
) {
198 host
->cmd
->data
->error
= -ETIMEDOUT
;
201 host
->cmd
->error
= -ETIMEDOUT
;
203 host
->request
->cmd
->error
= -ETIMEDOUT
;
206 at91_reset_host(host
);
207 mmc_request_done(host
->mmc
, host
->request
);
212 * Copy from sg to a dma block - used for transfers
214 static inline void at91_mci_sg_to_dma(struct at91mci_host
*host
, struct mmc_data
*data
)
216 unsigned int len
, i
, size
;
217 unsigned *dmabuf
= host
->buffer
;
219 size
= data
->blksz
* data
->blocks
;
222 /* MCI1 rev2xx Data Write Operation and number of bytes erratum */
223 if (at91mci_is_mci1rev2xx())
224 if (host
->total_length
== 12)
225 memset(dmabuf
, 0, 12);
228 * Just loop through all entries. Size might not
229 * be the entire list though so make sure that
230 * we do not transfer too much.
232 for (i
= 0; i
< len
; i
++) {
233 struct scatterlist
*sg
;
235 unsigned int *sgbuffer
;
239 sgbuffer
= kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
240 amount
= min(size
, sg
->length
);
243 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
246 for (index
= 0; index
< (amount
/ 4); index
++)
247 *dmabuf
++ = swab32(sgbuffer
[index
]);
249 char *tmpv
= (char *)dmabuf
;
250 memcpy(tmpv
, sgbuffer
, amount
);
252 dmabuf
= (unsigned *)tmpv
;
255 kunmap_atomic(sgbuffer
, KM_BIO_SRC_IRQ
);
262 * Check that we didn't get a request to transfer
263 * more data than can fit into the SG list.
269 * Handle after a dma read
271 static void at91_mci_post_dma_read(struct at91mci_host
*host
)
273 struct mmc_command
*cmd
;
274 struct mmc_data
*data
;
275 unsigned int len
, i
, size
;
276 unsigned *dmabuf
= host
->buffer
;
278 pr_debug("post dma read\n");
282 pr_debug("no command\n");
288 pr_debug("no data\n");
292 size
= data
->blksz
* data
->blocks
;
295 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_ENDRX
);
296 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_RXBUFF
);
298 for (i
= 0; i
< len
; i
++) {
299 struct scatterlist
*sg
;
301 unsigned int *sgbuffer
;
305 sgbuffer
= kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
306 amount
= min(size
, sg
->length
);
309 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
311 for (index
= 0; index
< (amount
/ 4); index
++)
312 sgbuffer
[index
] = swab32(*dmabuf
++);
314 char *tmpv
= (char *)dmabuf
;
315 memcpy(sgbuffer
, tmpv
, amount
);
317 dmabuf
= (unsigned *)tmpv
;
320 flush_kernel_dcache_page(sg_page(sg
));
321 kunmap_atomic(sgbuffer
, KM_BIO_SRC_IRQ
);
322 data
->bytes_xfered
+= amount
;
327 pr_debug("post dma read done\n");
331 * Handle transmitted data
333 static void at91_mci_handle_transmitted(struct at91mci_host
*host
)
335 struct mmc_command
*cmd
;
336 struct mmc_data
*data
;
338 pr_debug("Handling the transmit\n");
340 /* Disable the transfer */
341 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
343 /* Now wait for cmd ready */
344 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_TXBUFE
);
352 if (cmd
->data
->blocks
> 1) {
353 pr_debug("multiple write : wait for BLKE...\n");
354 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_BLKE
);
356 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
360 * Update bytes tranfered count during a write operation
362 static void at91_mci_update_bytes_xfered(struct at91mci_host
*host
)
364 struct mmc_data
*data
;
366 /* always deal with the effective request (and not the current cmd) */
368 if (host
->request
->cmd
&& host
->request
->cmd
->error
!= 0)
371 if (host
->request
->data
) {
372 data
= host
->request
->data
;
373 if (data
->flags
& MMC_DATA_WRITE
) {
374 /* card is in IDLE mode now */
375 pr_debug("-> bytes_xfered %d, total_length = %d\n",
376 data
->bytes_xfered
, host
->total_length
);
377 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
383 /*Handle after command sent ready*/
384 static int at91_mci_handle_cmdrdy(struct at91mci_host
*host
)
388 else if (!host
->cmd
->data
) {
389 if (host
->flags
& FL_SENT_STOP
) {
390 /*After multi block write, we must wait for NOTBUSY*/
391 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
393 } else if (host
->cmd
->data
->flags
& MMC_DATA_WRITE
) {
394 /*After sendding multi-block-write command, start DMA transfer*/
395 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_TXBUFE
| AT91_MCI_BLKE
);
396 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTEN
);
399 /* command not completed, have to wait */
405 * Enable the controller
407 static void at91_mci_enable(struct at91mci_host
*host
)
411 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
412 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff);
413 at91_mci_write(host
, AT91_MCI_DTOR
, AT91_MCI_DTOMUL_1M
| AT91_MCI_DTOCYC
);
414 mr
= AT91_MCI_PDCMODE
| 0x34a;
416 if (at91mci_is_mci1rev2xx())
417 mr
|= AT91_MCI_RDPROOF
| AT91_MCI_WRPROOF
;
419 at91_mci_write(host
, AT91_MCI_MR
, mr
);
421 /* use Slot A or B (only one at same time) */
422 at91_mci_write(host
, AT91_MCI_SDCR
, host
->board
->slot_b
);
426 * Disable the controller
428 static void at91_mci_disable(struct at91mci_host
*host
)
430 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
| AT91_MCI_SWRST
);
436 static void at91_mci_send_command(struct at91mci_host
*host
, struct mmc_command
*cmd
)
438 unsigned int cmdr
, mr
;
439 unsigned int block_length
;
440 struct mmc_data
*data
= cmd
->data
;
443 unsigned int ier
= 0;
447 /* Needed for leaving busy state before CMD1 */
448 if ((at91_mci_read(host
, AT91_MCI_SR
) & AT91_MCI_RTOE
) && (cmd
->opcode
== 1)) {
449 pr_debug("Clearing timeout\n");
450 at91_mci_write(host
, AT91_MCI_ARGR
, 0);
451 at91_mci_write(host
, AT91_MCI_CMDR
, AT91_MCI_OPDCMD
);
452 while (!(at91_mci_read(host
, AT91_MCI_SR
) & AT91_MCI_CMDRDY
)) {
454 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host
, AT91_MCI_SR
));
460 if (mmc_resp_type(cmd
) == MMC_RSP_NONE
)
461 cmdr
|= AT91_MCI_RSPTYP_NONE
;
463 /* if a response is expected then allow maximum response latancy */
464 cmdr
|= AT91_MCI_MAXLAT
;
465 /* set 136 bit response for R2, 48 bit response otherwise */
466 if (mmc_resp_type(cmd
) == MMC_RSP_R2
)
467 cmdr
|= AT91_MCI_RSPTYP_136
;
469 cmdr
|= AT91_MCI_RSPTYP_48
;
474 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
475 if (data
->blksz
& 0x3) {
476 pr_debug("Unsupported block size\n");
477 cmd
->error
= -EINVAL
;
478 mmc_request_done(host
->mmc
, host
->request
);
481 if (data
->flags
& MMC_DATA_STREAM
) {
482 pr_debug("Stream commands not supported\n");
483 cmd
->error
= -EINVAL
;
484 mmc_request_done(host
->mmc
, host
->request
);
489 block_length
= data
->blksz
;
490 blocks
= data
->blocks
;
492 /* always set data start - also set direction flag for read */
493 if (data
->flags
& MMC_DATA_READ
)
494 cmdr
|= (AT91_MCI_TRDIR
| AT91_MCI_TRCMD_START
);
495 else if (data
->flags
& MMC_DATA_WRITE
)
496 cmdr
|= AT91_MCI_TRCMD_START
;
498 if (cmd
->opcode
== SD_IO_RW_EXTENDED
) {
499 cmdr
|= AT91_MCI_TRTYP_SDIO_BLOCK
;
501 if (data
->flags
& MMC_DATA_STREAM
)
502 cmdr
|= AT91_MCI_TRTYP_STREAM
;
503 if (data
->blocks
> 1)
504 cmdr
|= AT91_MCI_TRTYP_MULTIPLE
;
512 if (host
->flags
& FL_SENT_STOP
)
513 cmdr
|= AT91_MCI_TRCMD_STOP
;
515 if (host
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
516 cmdr
|= AT91_MCI_OPDCMD
;
519 * Set the arguments and send the command
521 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
522 cmd
->opcode
, cmdr
, cmd
->arg
, blocks
, block_length
, at91_mci_read(host
, AT91_MCI_MR
));
525 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTDIS
| ATMEL_PDC_RXTDIS
);
526 at91_mci_write(host
, ATMEL_PDC_RPR
, 0);
527 at91_mci_write(host
, ATMEL_PDC_RCR
, 0);
528 at91_mci_write(host
, ATMEL_PDC_RNPR
, 0);
529 at91_mci_write(host
, ATMEL_PDC_RNCR
, 0);
530 at91_mci_write(host
, ATMEL_PDC_TPR
, 0);
531 at91_mci_write(host
, ATMEL_PDC_TCR
, 0);
532 at91_mci_write(host
, ATMEL_PDC_TNPR
, 0);
533 at91_mci_write(host
, ATMEL_PDC_TNCR
, 0);
534 ier
= AT91_MCI_CMDRDY
;
536 /* zero block length and PDC mode */
537 mr
= at91_mci_read(host
, AT91_MCI_MR
) & 0x5fff;
538 mr
|= (data
->blksz
& 0x3) ? AT91_MCI_PDCFBYTE
: 0;
539 mr
|= (block_length
<< 16);
540 mr
|= AT91_MCI_PDCMODE
;
541 at91_mci_write(host
, AT91_MCI_MR
, mr
);
543 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
544 at91_mci_write(host
, AT91_MCI_BLKR
,
545 AT91_MCI_BLKR_BCNT(blocks
) |
546 AT91_MCI_BLKR_BLKLEN(block_length
));
549 * Disable the PDC controller
551 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
553 if (cmdr
& AT91_MCI_TRCMD_START
) {
554 data
->bytes_xfered
= 0;
555 host
->transfer_index
= 0;
556 host
->in_use_index
= 0;
557 if (cmdr
& AT91_MCI_TRDIR
) {
561 host
->total_length
= 0;
563 at91_mci_write(host
, ATMEL_PDC_RPR
, host
->physical_address
);
564 at91_mci_write(host
, ATMEL_PDC_RCR
, (data
->blksz
& 0x3) ?
565 (blocks
* block_length
) : (blocks
* block_length
) / 4);
566 at91_mci_write(host
, ATMEL_PDC_RNPR
, 0);
567 at91_mci_write(host
, ATMEL_PDC_RNCR
, 0);
569 ier
= AT91_MCI_ENDRX
/* | AT91_MCI_RXBUFF */;
575 host
->total_length
= block_length
* blocks
;
577 * MCI1 rev2xx Data Write Operation and
578 * number of bytes erratum
580 if (at91mci_is_mci1rev2xx())
581 if (host
->total_length
< 12)
582 host
->total_length
= 12;
584 at91_mci_sg_to_dma(host
, data
);
586 pr_debug("Transmitting %d bytes\n", host
->total_length
);
588 at91_mci_write(host
, ATMEL_PDC_TPR
, host
->physical_address
);
589 at91_mci_write(host
, ATMEL_PDC_TCR
, (data
->blksz
& 0x3) ?
590 host
->total_length
: host
->total_length
/ 4);
592 ier
= AT91_MCI_CMDRDY
;
598 * Send the command and then enable the PDC - not the other way round as
599 * the data sheet says
602 at91_mci_write(host
, AT91_MCI_ARGR
, cmd
->arg
);
603 at91_mci_write(host
, AT91_MCI_CMDR
, cmdr
);
605 if (cmdr
& AT91_MCI_TRCMD_START
) {
606 if (cmdr
& AT91_MCI_TRDIR
)
607 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTEN
);
610 /* Enable selected interrupts */
611 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_ERRORS
| ier
);
615 * Process the next step in the request
617 static void at91_mci_process_next(struct at91mci_host
*host
)
619 if (!(host
->flags
& FL_SENT_COMMAND
)) {
620 host
->flags
|= FL_SENT_COMMAND
;
621 at91_mci_send_command(host
, host
->request
->cmd
);
623 else if ((!(host
->flags
& FL_SENT_STOP
)) && host
->request
->stop
) {
624 host
->flags
|= FL_SENT_STOP
;
625 at91_mci_send_command(host
, host
->request
->stop
);
627 del_timer(&host
->timer
);
628 /* the at91rm9200 mci controller hangs after some transfers,
629 * and the workaround is to reset it after each transfer.
631 if (cpu_is_at91rm9200())
632 at91_reset_host(host
);
633 mmc_request_done(host
->mmc
, host
->request
);
638 * Handle a command that has been completed
640 static void at91_mci_completed_command(struct at91mci_host
*host
, unsigned int status
)
642 struct mmc_command
*cmd
= host
->cmd
;
643 struct mmc_data
*data
= cmd
->data
;
645 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff & ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
647 cmd
->resp
[0] = at91_mci_read(host
, AT91_MCI_RSPR(0));
648 cmd
->resp
[1] = at91_mci_read(host
, AT91_MCI_RSPR(1));
649 cmd
->resp
[2] = at91_mci_read(host
, AT91_MCI_RSPR(2));
650 cmd
->resp
[3] = at91_mci_read(host
, AT91_MCI_RSPR(3));
652 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
653 status
, at91_mci_read(host
, AT91_MCI_SR
),
654 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2], cmd
->resp
[3]);
656 if (status
& AT91_MCI_ERRORS
) {
657 if ((status
& AT91_MCI_RCRCE
) && !(mmc_resp_type(cmd
) & MMC_RSP_CRC
)) {
661 if (status
& (AT91_MCI_DTOE
| AT91_MCI_DCRCE
)) {
663 if (status
& AT91_MCI_DTOE
)
664 data
->error
= -ETIMEDOUT
;
665 else if (status
& AT91_MCI_DCRCE
)
666 data
->error
= -EILSEQ
;
669 if (status
& AT91_MCI_RTOE
)
670 cmd
->error
= -ETIMEDOUT
;
671 else if (status
& AT91_MCI_RCRCE
)
672 cmd
->error
= -EILSEQ
;
677 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
678 cmd
->error
, data
? data
->error
: 0,
679 cmd
->opcode
, cmd
->retries
);
685 at91_mci_process_next(host
);
689 * Handle an MMC request
691 static void at91_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
693 struct at91mci_host
*host
= mmc_priv(mmc
);
697 /* more than 1s timeout needed with slow SD cards */
698 mod_timer(&host
->timer
, jiffies
+ msecs_to_jiffies(2000));
700 at91_mci_process_next(host
);
706 static void at91_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
709 struct at91mci_host
*host
= mmc_priv(mmc
);
710 unsigned long at91_master_clock
= clk_get_rate(host
->mci_clk
);
712 host
->bus_mode
= ios
->bus_mode
;
714 if (ios
->clock
== 0) {
715 /* Disable the MCI controller */
716 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
);
720 /* Enable the MCI controller */
721 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
723 if ((at91_master_clock
% (ios
->clock
* 2)) == 0)
724 clkdiv
= ((at91_master_clock
/ ios
->clock
) / 2) - 1;
726 clkdiv
= (at91_master_clock
/ ios
->clock
) / 2;
728 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv
,
729 at91_master_clock
/ (2 * (clkdiv
+ 1)));
731 if (ios
->bus_width
== MMC_BUS_WIDTH_4
&& host
->board
->wire4
) {
732 pr_debug("MMC: Setting controller bus width to 4\n");
733 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) | AT91_MCI_SDCBUS
);
736 pr_debug("MMC: Setting controller bus width to 1\n");
737 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) & ~AT91_MCI_SDCBUS
);
740 /* Set the clock divider */
741 at91_mci_write(host
, AT91_MCI_MR
, (at91_mci_read(host
, AT91_MCI_MR
) & ~AT91_MCI_CLKDIV
) | clkdiv
);
743 /* maybe switch power to the card */
744 if (host
->board
->vcc_pin
) {
745 switch (ios
->power_mode
) {
747 gpio_set_value(host
->board
->vcc_pin
, 0);
750 gpio_set_value(host
->board
->vcc_pin
, 1);
761 * Handle an interrupt
763 static irqreturn_t
at91_mci_irq(int irq
, void *devid
)
765 struct at91mci_host
*host
= devid
;
767 unsigned int int_status
, int_mask
;
769 int_status
= at91_mci_read(host
, AT91_MCI_SR
);
770 int_mask
= at91_mci_read(host
, AT91_MCI_IMR
);
772 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status
, int_mask
,
773 int_status
& int_mask
);
775 int_status
= int_status
& int_mask
;
777 if (int_status
& AT91_MCI_ERRORS
) {
780 if (int_status
& AT91_MCI_UNRE
)
781 pr_debug("MMC: Underrun error\n");
782 if (int_status
& AT91_MCI_OVRE
)
783 pr_debug("MMC: Overrun error\n");
784 if (int_status
& AT91_MCI_DTOE
)
785 pr_debug("MMC: Data timeout\n");
786 if (int_status
& AT91_MCI_DCRCE
)
787 pr_debug("MMC: CRC error in data\n");
788 if (int_status
& AT91_MCI_RTOE
)
789 pr_debug("MMC: Response timeout\n");
790 if (int_status
& AT91_MCI_RENDE
)
791 pr_debug("MMC: Response end bit error\n");
792 if (int_status
& AT91_MCI_RCRCE
)
793 pr_debug("MMC: Response CRC error\n");
794 if (int_status
& AT91_MCI_RDIRE
)
795 pr_debug("MMC: Response direction error\n");
796 if (int_status
& AT91_MCI_RINDE
)
797 pr_debug("MMC: Response index error\n");
799 /* Only continue processing if no errors */
801 if (int_status
& AT91_MCI_TXBUFE
) {
802 pr_debug("TX buffer empty\n");
803 at91_mci_handle_transmitted(host
);
806 if (int_status
& AT91_MCI_ENDRX
) {
808 at91_mci_post_dma_read(host
);
811 if (int_status
& AT91_MCI_RXBUFF
) {
812 pr_debug("RX buffer full\n");
813 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
814 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_RXBUFF
| AT91_MCI_ENDRX
);
818 if (int_status
& AT91_MCI_ENDTX
)
819 pr_debug("Transmit has ended\n");
821 if (int_status
& AT91_MCI_NOTBUSY
) {
822 pr_debug("Card is ready\n");
823 at91_mci_update_bytes_xfered(host
);
827 if (int_status
& AT91_MCI_DTIP
)
828 pr_debug("Data transfer in progress\n");
830 if (int_status
& AT91_MCI_BLKE
) {
831 pr_debug("Block transfer has ended\n");
832 if (host
->request
->data
&& host
->request
->data
->blocks
> 1) {
833 /* multi block write : complete multi write
834 * command and send stop */
837 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
841 if (int_status
& AT91_MCI_SDIOIRQA
)
842 mmc_signal_sdio_irq(host
->mmc
);
844 if (int_status
& AT91_MCI_SDIOIRQB
)
845 mmc_signal_sdio_irq(host
->mmc
);
847 if (int_status
& AT91_MCI_TXRDY
)
848 pr_debug("Ready to transmit\n");
850 if (int_status
& AT91_MCI_RXRDY
)
851 pr_debug("Ready to receive\n");
853 if (int_status
& AT91_MCI_CMDRDY
) {
854 pr_debug("Command ready\n");
855 completed
= at91_mci_handle_cmdrdy(host
);
860 pr_debug("Completed command\n");
861 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff & ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
862 at91_mci_completed_command(host
, int_status
);
864 at91_mci_write(host
, AT91_MCI_IDR
, int_status
& ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
869 static irqreturn_t
at91_mmc_det_irq(int irq
, void *_host
)
871 struct at91mci_host
*host
= _host
;
874 /* entering this ISR means that we have configured det_pin:
875 * we can use its value in board structure */
876 present
= !gpio_get_value(host
->board
->det_pin
);
879 * we expect this irq on both insert and remove,
880 * and use a short delay to debounce.
882 if (present
!= host
->present
) {
883 host
->present
= present
;
884 pr_debug("%s: card %s\n", mmc_hostname(host
->mmc
),
885 present
? "insert" : "remove");
887 pr_debug("****** Resetting SD-card bus width ******\n");
888 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) & ~AT91_MCI_SDCBUS
);
890 /* 0.5s needed because of early card detect switch firing */
891 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
896 static int at91_mci_get_ro(struct mmc_host
*mmc
)
898 struct at91mci_host
*host
= mmc_priv(mmc
);
900 if (host
->board
->wp_pin
)
901 return !!gpio_get_value(host
->board
->wp_pin
);
903 * Board doesn't support read only detection; let the mmc core
909 static void at91_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
911 struct at91mci_host
*host
= mmc_priv(mmc
);
913 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host
->mmc
),
914 host
->board
->slot_b
? 'B':'A', enable
? "enable" : "disable");
915 at91_mci_write(host
, enable
? AT91_MCI_IER
: AT91_MCI_IDR
,
916 host
->board
->slot_b
? AT91_MCI_SDIOIRQB
: AT91_MCI_SDIOIRQA
);
920 static const struct mmc_host_ops at91_mci_ops
= {
921 .request
= at91_mci_request
,
922 .set_ios
= at91_mci_set_ios
,
923 .get_ro
= at91_mci_get_ro
,
924 .enable_sdio_irq
= at91_mci_enable_sdio_irq
,
928 * Probe for the device
930 static int __init
at91_mci_probe(struct platform_device
*pdev
)
932 struct mmc_host
*mmc
;
933 struct at91mci_host
*host
;
934 struct resource
*res
;
937 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
941 if (!request_mem_region(res
->start
, resource_size(res
), DRIVER_NAME
))
944 mmc
= mmc_alloc_host(sizeof(struct at91mci_host
), &pdev
->dev
);
947 dev_dbg(&pdev
->dev
, "couldn't allocate mmc host\n");
951 mmc
->ops
= &at91_mci_ops
;
953 mmc
->f_max
= 25000000;
954 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
957 mmc
->max_blk_size
= MCI_MAXBLKSIZE
;
958 mmc
->max_blk_count
= MCI_BLKATONCE
;
959 mmc
->max_req_size
= MCI_BUFSIZE
;
960 mmc
->max_segs
= MCI_BLKATONCE
;
961 mmc
->max_seg_size
= MCI_BUFSIZE
;
963 host
= mmc_priv(mmc
);
966 host
->board
= pdev
->dev
.platform_data
;
967 if (host
->board
->wire4
) {
968 if (at91mci_is_mci1rev2xx())
969 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
971 dev_warn(&pdev
->dev
, "4 wire bus mode not supported"
972 " - using 1 wire\n");
975 host
->buffer
= dma_alloc_coherent(&pdev
->dev
, MCI_BUFSIZE
,
976 &host
->physical_address
, GFP_KERNEL
);
979 dev_err(&pdev
->dev
, "Can't allocate transmit buffer\n");
983 /* Add SDIO capability when available */
984 if (at91mci_is_mci1rev2xx()) {
985 /* at91mci MCI1 rev2xx sdio interrupt erratum */
986 if (host
->board
->wire4
|| !host
->board
->slot_b
)
987 mmc
->caps
|= MMC_CAP_SDIO_IRQ
;
991 * Reserve GPIOs ... board init code makes sure these pins are set
992 * up as GPIOs with the right direction (input, except for vcc)
994 if (host
->board
->det_pin
) {
995 ret
= gpio_request(host
->board
->det_pin
, "mmc_detect");
997 dev_dbg(&pdev
->dev
, "couldn't claim card detect pin\n");
1001 if (host
->board
->wp_pin
) {
1002 ret
= gpio_request(host
->board
->wp_pin
, "mmc_wp");
1004 dev_dbg(&pdev
->dev
, "couldn't claim wp sense pin\n");
1008 if (host
->board
->vcc_pin
) {
1009 ret
= gpio_request(host
->board
->vcc_pin
, "mmc_vcc");
1011 dev_dbg(&pdev
->dev
, "couldn't claim vcc switch pin\n");
1019 host
->mci_clk
= clk_get(&pdev
->dev
, "mci_clk");
1020 if (IS_ERR(host
->mci_clk
)) {
1022 dev_dbg(&pdev
->dev
, "no mci_clk?\n");
1029 host
->baseaddr
= ioremap(res
->start
, resource_size(res
));
1030 if (!host
->baseaddr
) {
1038 clk_enable(host
->mci_clk
); /* Enable the peripheral clock */
1039 at91_mci_disable(host
);
1040 at91_mci_enable(host
);
1043 * Allocate the MCI interrupt
1045 host
->irq
= platform_get_irq(pdev
, 0);
1046 ret
= request_irq(host
->irq
, at91_mci_irq
, IRQF_SHARED
,
1047 mmc_hostname(mmc
), host
);
1049 dev_dbg(&pdev
->dev
, "request MCI interrupt failed\n");
1053 setup_timer(&host
->timer
, at91_timeout_timer
, (unsigned long)host
);
1055 platform_set_drvdata(pdev
, mmc
);
1058 * Add host to MMC layer
1060 if (host
->board
->det_pin
) {
1061 host
->present
= !gpio_get_value(host
->board
->det_pin
);
1069 * monitor card insertion/removal if we can
1071 if (host
->board
->det_pin
) {
1072 ret
= request_irq(gpio_to_irq(host
->board
->det_pin
),
1073 at91_mmc_det_irq
, 0, mmc_hostname(mmc
), host
);
1075 dev_warn(&pdev
->dev
, "request MMC detect irq failed\n");
1077 device_init_wakeup(&pdev
->dev
, 1);
1080 pr_debug("Added MCI driver\n");
1085 clk_disable(host
->mci_clk
);
1086 iounmap(host
->baseaddr
);
1088 clk_put(host
->mci_clk
);
1090 if (host
->board
->vcc_pin
)
1091 gpio_free(host
->board
->vcc_pin
);
1093 if (host
->board
->wp_pin
)
1094 gpio_free(host
->board
->wp_pin
);
1096 if (host
->board
->det_pin
)
1097 gpio_free(host
->board
->det_pin
);
1100 dma_free_coherent(&pdev
->dev
, MCI_BUFSIZE
,
1101 host
->buffer
, host
->physical_address
);
1105 release_mem_region(res
->start
, resource_size(res
));
1106 dev_err(&pdev
->dev
, "probe failed, err %d\n", ret
);
1113 static int __exit
at91_mci_remove(struct platform_device
*pdev
)
1115 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1116 struct at91mci_host
*host
;
1117 struct resource
*res
;
1122 host
= mmc_priv(mmc
);
1125 dma_free_coherent(&pdev
->dev
, MCI_BUFSIZE
,
1126 host
->buffer
, host
->physical_address
);
1128 if (host
->board
->det_pin
) {
1129 if (device_can_wakeup(&pdev
->dev
))
1130 free_irq(gpio_to_irq(host
->board
->det_pin
), host
);
1131 device_init_wakeup(&pdev
->dev
, 0);
1132 gpio_free(host
->board
->det_pin
);
1135 at91_mci_disable(host
);
1136 del_timer_sync(&host
->timer
);
1137 mmc_remove_host(mmc
);
1138 free_irq(host
->irq
, host
);
1140 clk_disable(host
->mci_clk
); /* Disable the peripheral clock */
1141 clk_put(host
->mci_clk
);
1143 if (host
->board
->vcc_pin
)
1144 gpio_free(host
->board
->vcc_pin
);
1145 if (host
->board
->wp_pin
)
1146 gpio_free(host
->board
->wp_pin
);
1148 iounmap(host
->baseaddr
);
1149 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1150 release_mem_region(res
->start
, resource_size(res
));
1153 platform_set_drvdata(pdev
, NULL
);
1154 pr_debug("MCI Removed\n");
1160 static int at91_mci_suspend(struct platform_device
*pdev
, pm_message_t state
)
1162 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1163 struct at91mci_host
*host
= mmc_priv(mmc
);
1166 if (host
->board
->det_pin
&& device_may_wakeup(&pdev
->dev
))
1167 enable_irq_wake(host
->board
->det_pin
);
1170 ret
= mmc_suspend_host(mmc
);
1175 static int at91_mci_resume(struct platform_device
*pdev
)
1177 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1178 struct at91mci_host
*host
= mmc_priv(mmc
);
1181 if (host
->board
->det_pin
&& device_may_wakeup(&pdev
->dev
))
1182 disable_irq_wake(host
->board
->det_pin
);
1185 ret
= mmc_resume_host(mmc
);
1190 #define at91_mci_suspend NULL
1191 #define at91_mci_resume NULL
1194 static struct platform_driver at91_mci_driver
= {
1195 .remove
= __exit_p(at91_mci_remove
),
1196 .suspend
= at91_mci_suspend
,
1197 .resume
= at91_mci_resume
,
1199 .name
= DRIVER_NAME
,
1200 .owner
= THIS_MODULE
,
1204 static int __init
at91_mci_init(void)
1206 return platform_driver_probe(&at91_mci_driver
, at91_mci_probe
);
1209 static void __exit
at91_mci_exit(void)
1211 platform_driver_unregister(&at91_mci_driver
);
1214 module_init(at91_mci_init
);
1215 module_exit(at91_mci_exit
);
1217 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1218 MODULE_AUTHOR("Nick Randell");
1219 MODULE_LICENSE("GPL");
1220 MODULE_ALIAS("platform:at91_mci");