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 <mach/board.h>
77 #include <mach/at91_mci.h>
79 #define DRIVER_NAME "at91_mci"
81 #define FL_SENT_COMMAND (1 << 0)
82 #define FL_SENT_STOP (1 << 1)
84 #define AT91_MCI_ERRORS (AT91_MCI_RINDE | AT91_MCI_RDIRE | AT91_MCI_RCRCE \
85 | AT91_MCI_RENDE | AT91_MCI_RTOE | AT91_MCI_DCRCE \
86 | AT91_MCI_DTOE | AT91_MCI_OVRE | AT91_MCI_UNRE)
88 #define at91_mci_read(host, reg) __raw_readl((host)->baseaddr + (reg))
89 #define at91_mci_write(host, reg, val) __raw_writel((val), (host)->baseaddr + (reg))
91 #define MCI_BLKSIZE 512
92 #define MCI_MAXBLKSIZE 4095
93 #define MCI_BLKATONCE 256
94 #define MCI_BUFSIZE (MCI_BLKSIZE * MCI_BLKATONCE)
97 * Low level type for this driver
101 struct mmc_host
*mmc
;
102 struct mmc_command
*cmd
;
103 struct mmc_request
*request
;
105 void __iomem
*baseaddr
;
108 struct at91_mmc_data
*board
;
114 * Flag indicating when the command has been sent. This is used to
115 * work out whether or not to send the stop
118 /* flag for current bus settings */
121 /* DMA buffer used for transmitting */
122 unsigned int* buffer
;
123 dma_addr_t physical_address
;
124 unsigned int total_length
;
126 /* Latest in the scatterlist that has been enabled for transfer, but not freed */
129 /* Latest in the scatterlist that has been enabled for transfer */
132 /* Timer for timeouts */
133 struct timer_list timer
;
137 * Reset the controller and restore most of the state
139 static void at91_reset_host(struct at91mci_host
*host
)
147 local_irq_save(flags
);
148 imr
= at91_mci_read(host
, AT91_MCI_IMR
);
150 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff);
152 /* save current state */
153 mr
= at91_mci_read(host
, AT91_MCI_MR
) & 0x7fff;
154 sdcr
= at91_mci_read(host
, AT91_MCI_SDCR
);
155 dtor
= at91_mci_read(host
, AT91_MCI_DTOR
);
157 /* reset the controller */
158 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
| AT91_MCI_SWRST
);
161 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
162 at91_mci_write(host
, AT91_MCI_MR
, mr
);
163 at91_mci_write(host
, AT91_MCI_SDCR
, sdcr
);
164 at91_mci_write(host
, AT91_MCI_DTOR
, dtor
);
165 at91_mci_write(host
, AT91_MCI_IER
, imr
);
167 /* make sure sdio interrupts will fire */
168 at91_mci_read(host
, AT91_MCI_SR
);
170 local_irq_restore(flags
);
173 static void at91_timeout_timer(unsigned long data
)
175 struct at91mci_host
*host
;
177 host
= (struct at91mci_host
*)data
;
180 dev_err(host
->mmc
->parent
, "Timeout waiting end of packet\n");
182 if (host
->cmd
&& host
->cmd
->data
) {
183 host
->cmd
->data
->error
= -ETIMEDOUT
;
186 host
->cmd
->error
= -ETIMEDOUT
;
188 host
->request
->cmd
->error
= -ETIMEDOUT
;
191 at91_reset_host(host
);
192 mmc_request_done(host
->mmc
, host
->request
);
197 * Copy from sg to a dma block - used for transfers
199 static inline void at91_mci_sg_to_dma(struct at91mci_host
*host
, struct mmc_data
*data
)
201 unsigned int len
, i
, size
;
202 unsigned *dmabuf
= host
->buffer
;
204 size
= data
->blksz
* data
->blocks
;
207 /* AT91SAM926[0/3] Data Write Operation and number of bytes erratum */
208 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
209 if (host
->total_length
== 12)
210 memset(dmabuf
, 0, 12);
213 * Just loop through all entries. Size might not
214 * be the entire list though so make sure that
215 * we do not transfer too much.
217 for (i
= 0; i
< len
; i
++) {
218 struct scatterlist
*sg
;
220 unsigned int *sgbuffer
;
224 sgbuffer
= kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
225 amount
= min(size
, sg
->length
);
228 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
231 for (index
= 0; index
< (amount
/ 4); index
++)
232 *dmabuf
++ = swab32(sgbuffer
[index
]);
234 char *tmpv
= (char *)dmabuf
;
235 memcpy(tmpv
, sgbuffer
, amount
);
237 dmabuf
= (unsigned *)tmpv
;
240 kunmap_atomic(((void *)sgbuffer
) - sg
->offset
, KM_BIO_SRC_IRQ
);
247 * Check that we didn't get a request to transfer
248 * more data than can fit into the SG list.
256 static void at91_mci_pre_dma_read(struct at91mci_host
*host
)
259 struct scatterlist
*sg
;
260 struct mmc_command
*cmd
;
261 struct mmc_data
*data
;
263 pr_debug("pre dma read\n");
267 pr_debug("no command\n");
273 pr_debug("no data\n");
277 for (i
= 0; i
< 2; i
++) {
278 /* nothing left to transfer */
279 if (host
->transfer_index
>= data
->sg_len
) {
280 pr_debug("Nothing left to transfer (index = %d)\n", host
->transfer_index
);
284 /* Check to see if this needs filling */
286 if (at91_mci_read(host
, ATMEL_PDC_RCR
) != 0) {
287 pr_debug("Transfer active in current\n");
292 if (at91_mci_read(host
, ATMEL_PDC_RNCR
) != 0) {
293 pr_debug("Transfer active in next\n");
298 /* Setup the next transfer */
299 pr_debug("Using transfer index %d\n", host
->transfer_index
);
301 sg
= &data
->sg
[host
->transfer_index
++];
302 pr_debug("sg = %p\n", sg
);
304 sg
->dma_address
= dma_map_page(NULL
, sg_page(sg
), sg
->offset
, sg
->length
, DMA_FROM_DEVICE
);
306 pr_debug("dma address = %08X, length = %d\n", sg
->dma_address
, sg
->length
);
309 at91_mci_write(host
, ATMEL_PDC_RPR
, sg
->dma_address
);
310 at91_mci_write(host
, ATMEL_PDC_RCR
, (data
->blksz
& 0x3) ? sg
->length
: sg
->length
/ 4);
313 at91_mci_write(host
, ATMEL_PDC_RNPR
, sg
->dma_address
);
314 at91_mci_write(host
, ATMEL_PDC_RNCR
, (data
->blksz
& 0x3) ? sg
->length
: sg
->length
/ 4);
318 pr_debug("pre dma read done\n");
322 * Handle after a dma read
324 static void at91_mci_post_dma_read(struct at91mci_host
*host
)
326 struct mmc_command
*cmd
;
327 struct mmc_data
*data
;
329 pr_debug("post dma read\n");
333 pr_debug("no command\n");
339 pr_debug("no data\n");
343 while (host
->in_use_index
< host
->transfer_index
) {
344 struct scatterlist
*sg
;
346 pr_debug("finishing index %d\n", host
->in_use_index
);
348 sg
= &data
->sg
[host
->in_use_index
++];
350 pr_debug("Unmapping page %08X\n", sg
->dma_address
);
352 dma_unmap_page(NULL
, sg
->dma_address
, sg
->length
, DMA_FROM_DEVICE
);
354 if (cpu_is_at91rm9200()) { /* AT91RM9200 errata */
355 unsigned int *buffer
;
358 /* Swap the contents of the buffer */
359 buffer
= kmap_atomic(sg_page(sg
), KM_BIO_SRC_IRQ
) + sg
->offset
;
360 pr_debug("buffer = %p, length = %d\n", buffer
, sg
->length
);
362 for (index
= 0; index
< (sg
->length
/ 4); index
++)
363 buffer
[index
] = swab32(buffer
[index
]);
365 kunmap_atomic(buffer
, KM_BIO_SRC_IRQ
);
368 flush_dcache_page(sg_page(sg
));
370 data
->bytes_xfered
+= sg
->length
;
373 /* Is there another transfer to trigger? */
374 if (host
->transfer_index
< data
->sg_len
)
375 at91_mci_pre_dma_read(host
);
377 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_ENDRX
);
378 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_RXBUFF
);
381 pr_debug("post dma read done\n");
385 * Handle transmitted data
387 static void at91_mci_handle_transmitted(struct at91mci_host
*host
)
389 struct mmc_command
*cmd
;
390 struct mmc_data
*data
;
392 pr_debug("Handling the transmit\n");
394 /* Disable the transfer */
395 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
397 /* Now wait for cmd ready */
398 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_TXBUFE
);
406 if (cmd
->data
->blocks
> 1) {
407 pr_debug("multiple write : wait for BLKE...\n");
408 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_BLKE
);
410 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
414 * Update bytes tranfered count during a write operation
416 static void at91_mci_update_bytes_xfered(struct at91mci_host
*host
)
418 struct mmc_data
*data
;
420 /* always deal with the effective request (and not the current cmd) */
422 if (host
->request
->cmd
&& host
->request
->cmd
->error
!= 0)
425 if (host
->request
->data
) {
426 data
= host
->request
->data
;
427 if (data
->flags
& MMC_DATA_WRITE
) {
428 /* card is in IDLE mode now */
429 pr_debug("-> bytes_xfered %d, total_length = %d\n",
430 data
->bytes_xfered
, host
->total_length
);
431 data
->bytes_xfered
= data
->blksz
* data
->blocks
;
437 /*Handle after command sent ready*/
438 static int at91_mci_handle_cmdrdy(struct at91mci_host
*host
)
442 else if (!host
->cmd
->data
) {
443 if (host
->flags
& FL_SENT_STOP
) {
444 /*After multi block write, we must wait for NOTBUSY*/
445 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
447 } else if (host
->cmd
->data
->flags
& MMC_DATA_WRITE
) {
448 /*After sendding multi-block-write command, start DMA transfer*/
449 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_TXBUFE
| AT91_MCI_BLKE
);
450 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTEN
);
453 /* command not completed, have to wait */
459 * Enable the controller
461 static void at91_mci_enable(struct at91mci_host
*host
)
465 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
466 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff);
467 at91_mci_write(host
, AT91_MCI_DTOR
, AT91_MCI_DTOMUL_1M
| AT91_MCI_DTOCYC
);
468 mr
= AT91_MCI_PDCMODE
| 0x34a;
470 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
471 mr
|= AT91_MCI_RDPROOF
| AT91_MCI_WRPROOF
;
473 at91_mci_write(host
, AT91_MCI_MR
, mr
);
475 /* use Slot A or B (only one at same time) */
476 at91_mci_write(host
, AT91_MCI_SDCR
, host
->board
->slot_b
);
480 * Disable the controller
482 static void at91_mci_disable(struct at91mci_host
*host
)
484 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
| AT91_MCI_SWRST
);
490 static void at91_mci_send_command(struct at91mci_host
*host
, struct mmc_command
*cmd
)
492 unsigned int cmdr
, mr
;
493 unsigned int block_length
;
494 struct mmc_data
*data
= cmd
->data
;
497 unsigned int ier
= 0;
501 /* Needed for leaving busy state before CMD1 */
502 if ((at91_mci_read(host
, AT91_MCI_SR
) & AT91_MCI_RTOE
) && (cmd
->opcode
== 1)) {
503 pr_debug("Clearing timeout\n");
504 at91_mci_write(host
, AT91_MCI_ARGR
, 0);
505 at91_mci_write(host
, AT91_MCI_CMDR
, AT91_MCI_OPDCMD
);
506 while (!(at91_mci_read(host
, AT91_MCI_SR
) & AT91_MCI_CMDRDY
)) {
508 pr_debug("Clearing: SR = %08X\n", at91_mci_read(host
, AT91_MCI_SR
));
514 if (mmc_resp_type(cmd
) == MMC_RSP_NONE
)
515 cmdr
|= AT91_MCI_RSPTYP_NONE
;
517 /* if a response is expected then allow maximum response latancy */
518 cmdr
|= AT91_MCI_MAXLAT
;
519 /* set 136 bit response for R2, 48 bit response otherwise */
520 if (mmc_resp_type(cmd
) == MMC_RSP_R2
)
521 cmdr
|= AT91_MCI_RSPTYP_136
;
523 cmdr
|= AT91_MCI_RSPTYP_48
;
528 if (cpu_is_at91rm9200() || cpu_is_at91sam9261()) {
529 if (data
->blksz
& 0x3) {
530 pr_debug("Unsupported block size\n");
531 cmd
->error
= -EINVAL
;
532 mmc_request_done(host
->mmc
, host
->request
);
535 if (data
->flags
& MMC_DATA_STREAM
) {
536 pr_debug("Stream commands not supported\n");
537 cmd
->error
= -EINVAL
;
538 mmc_request_done(host
->mmc
, host
->request
);
543 block_length
= data
->blksz
;
544 blocks
= data
->blocks
;
546 /* always set data start - also set direction flag for read */
547 if (data
->flags
& MMC_DATA_READ
)
548 cmdr
|= (AT91_MCI_TRDIR
| AT91_MCI_TRCMD_START
);
549 else if (data
->flags
& MMC_DATA_WRITE
)
550 cmdr
|= AT91_MCI_TRCMD_START
;
552 if (data
->flags
& MMC_DATA_STREAM
)
553 cmdr
|= AT91_MCI_TRTYP_STREAM
;
554 if (data
->blocks
> 1)
555 cmdr
|= AT91_MCI_TRTYP_MULTIPLE
;
562 if (host
->flags
& FL_SENT_STOP
)
563 cmdr
|= AT91_MCI_TRCMD_STOP
;
565 if (host
->bus_mode
== MMC_BUSMODE_OPENDRAIN
)
566 cmdr
|= AT91_MCI_OPDCMD
;
569 * Set the arguments and send the command
571 pr_debug("Sending command %d as %08X, arg = %08X, blocks = %d, length = %d (MR = %08X)\n",
572 cmd
->opcode
, cmdr
, cmd
->arg
, blocks
, block_length
, at91_mci_read(host
, AT91_MCI_MR
));
575 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_TXTDIS
| ATMEL_PDC_RXTDIS
);
576 at91_mci_write(host
, ATMEL_PDC_RPR
, 0);
577 at91_mci_write(host
, ATMEL_PDC_RCR
, 0);
578 at91_mci_write(host
, ATMEL_PDC_RNPR
, 0);
579 at91_mci_write(host
, ATMEL_PDC_RNCR
, 0);
580 at91_mci_write(host
, ATMEL_PDC_TPR
, 0);
581 at91_mci_write(host
, ATMEL_PDC_TCR
, 0);
582 at91_mci_write(host
, ATMEL_PDC_TNPR
, 0);
583 at91_mci_write(host
, ATMEL_PDC_TNCR
, 0);
584 ier
= AT91_MCI_CMDRDY
;
586 /* zero block length and PDC mode */
587 mr
= at91_mci_read(host
, AT91_MCI_MR
) & 0x5fff;
588 mr
|= (data
->blksz
& 0x3) ? AT91_MCI_PDCFBYTE
: 0;
589 mr
|= (block_length
<< 16);
590 mr
|= AT91_MCI_PDCMODE
;
591 at91_mci_write(host
, AT91_MCI_MR
, mr
);
593 if (!(cpu_is_at91rm9200() || cpu_is_at91sam9261()))
594 at91_mci_write(host
, AT91_MCI_BLKR
,
595 AT91_MCI_BLKR_BCNT(blocks
) |
596 AT91_MCI_BLKR_BLKLEN(block_length
));
599 * Disable the PDC controller
601 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
603 if (cmdr
& AT91_MCI_TRCMD_START
) {
604 data
->bytes_xfered
= 0;
605 host
->transfer_index
= 0;
606 host
->in_use_index
= 0;
607 if (cmdr
& AT91_MCI_TRDIR
) {
611 host
->total_length
= 0;
613 at91_mci_pre_dma_read(host
);
614 ier
= AT91_MCI_ENDRX
/* | AT91_MCI_RXBUFF */;
620 host
->total_length
= block_length
* blocks
;
622 * AT91SAM926[0/3] Data Write Operation and
623 * number of bytes erratum
625 if (cpu_is_at91sam9260 () || cpu_is_at91sam9263())
626 if (host
->total_length
< 12)
627 host
->total_length
= 12;
629 at91_mci_sg_to_dma(host
, data
);
631 pr_debug("Transmitting %d bytes\n", host
->total_length
);
633 at91_mci_write(host
, ATMEL_PDC_TPR
, host
->physical_address
);
634 at91_mci_write(host
, ATMEL_PDC_TCR
, (data
->blksz
& 0x3) ?
635 host
->total_length
: host
->total_length
/ 4);
637 ier
= AT91_MCI_CMDRDY
;
643 * Send the command and then enable the PDC - not the other way round as
644 * the data sheet says
647 at91_mci_write(host
, AT91_MCI_ARGR
, cmd
->arg
);
648 at91_mci_write(host
, AT91_MCI_CMDR
, cmdr
);
650 if (cmdr
& AT91_MCI_TRCMD_START
) {
651 if (cmdr
& AT91_MCI_TRDIR
)
652 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTEN
);
655 /* Enable selected interrupts */
656 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_ERRORS
| ier
);
660 * Process the next step in the request
662 static void at91_mci_process_next(struct at91mci_host
*host
)
664 if (!(host
->flags
& FL_SENT_COMMAND
)) {
665 host
->flags
|= FL_SENT_COMMAND
;
666 at91_mci_send_command(host
, host
->request
->cmd
);
668 else if ((!(host
->flags
& FL_SENT_STOP
)) && host
->request
->stop
) {
669 host
->flags
|= FL_SENT_STOP
;
670 at91_mci_send_command(host
, host
->request
->stop
);
672 del_timer(&host
->timer
);
673 /* the at91rm9200 mci controller hangs after some transfers,
674 * and the workaround is to reset it after each transfer.
676 if (cpu_is_at91rm9200())
677 at91_reset_host(host
);
678 mmc_request_done(host
->mmc
, host
->request
);
683 * Handle a command that has been completed
685 static void at91_mci_completed_command(struct at91mci_host
*host
, unsigned int status
)
687 struct mmc_command
*cmd
= host
->cmd
;
688 struct mmc_data
*data
= cmd
->data
;
690 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff & ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
692 cmd
->resp
[0] = at91_mci_read(host
, AT91_MCI_RSPR(0));
693 cmd
->resp
[1] = at91_mci_read(host
, AT91_MCI_RSPR(1));
694 cmd
->resp
[2] = at91_mci_read(host
, AT91_MCI_RSPR(2));
695 cmd
->resp
[3] = at91_mci_read(host
, AT91_MCI_RSPR(3));
697 pr_debug("Status = %08X/%08x [%08X %08X %08X %08X]\n",
698 status
, at91_mci_read(host
, AT91_MCI_SR
),
699 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2], cmd
->resp
[3]);
701 if (status
& AT91_MCI_ERRORS
) {
702 if ((status
& AT91_MCI_RCRCE
) && !(mmc_resp_type(cmd
) & MMC_RSP_CRC
)) {
706 if (status
& (AT91_MCI_DTOE
| AT91_MCI_DCRCE
)) {
708 if (status
& AT91_MCI_DTOE
)
709 data
->error
= -ETIMEDOUT
;
710 else if (status
& AT91_MCI_DCRCE
)
711 data
->error
= -EILSEQ
;
714 if (status
& AT91_MCI_RTOE
)
715 cmd
->error
= -ETIMEDOUT
;
716 else if (status
& AT91_MCI_RCRCE
)
717 cmd
->error
= -EILSEQ
;
722 pr_debug("Error detected and set to %d/%d (cmd = %d, retries = %d)\n",
723 cmd
->error
, data
? data
->error
: 0,
724 cmd
->opcode
, cmd
->retries
);
730 at91_mci_process_next(host
);
734 * Handle an MMC request
736 static void at91_mci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
738 struct at91mci_host
*host
= mmc_priv(mmc
);
742 /* more than 1s timeout needed with slow SD cards */
743 mod_timer(&host
->timer
, jiffies
+ msecs_to_jiffies(2000));
745 at91_mci_process_next(host
);
751 static void at91_mci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
754 struct at91mci_host
*host
= mmc_priv(mmc
);
755 unsigned long at91_master_clock
= clk_get_rate(host
->mci_clk
);
757 host
->bus_mode
= ios
->bus_mode
;
759 if (ios
->clock
== 0) {
760 /* Disable the MCI controller */
761 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIDIS
);
765 /* Enable the MCI controller */
766 at91_mci_write(host
, AT91_MCI_CR
, AT91_MCI_MCIEN
);
768 if ((at91_master_clock
% (ios
->clock
* 2)) == 0)
769 clkdiv
= ((at91_master_clock
/ ios
->clock
) / 2) - 1;
771 clkdiv
= (at91_master_clock
/ ios
->clock
) / 2;
773 pr_debug("clkdiv = %d. mcck = %ld\n", clkdiv
,
774 at91_master_clock
/ (2 * (clkdiv
+ 1)));
776 if (ios
->bus_width
== MMC_BUS_WIDTH_4
&& host
->board
->wire4
) {
777 pr_debug("MMC: Setting controller bus width to 4\n");
778 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) | AT91_MCI_SDCBUS
);
781 pr_debug("MMC: Setting controller bus width to 1\n");
782 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) & ~AT91_MCI_SDCBUS
);
785 /* Set the clock divider */
786 at91_mci_write(host
, AT91_MCI_MR
, (at91_mci_read(host
, AT91_MCI_MR
) & ~AT91_MCI_CLKDIV
) | clkdiv
);
788 /* maybe switch power to the card */
789 if (host
->board
->vcc_pin
) {
790 switch (ios
->power_mode
) {
792 gpio_set_value(host
->board
->vcc_pin
, 0);
795 gpio_set_value(host
->board
->vcc_pin
, 1);
806 * Handle an interrupt
808 static irqreturn_t
at91_mci_irq(int irq
, void *devid
)
810 struct at91mci_host
*host
= devid
;
812 unsigned int int_status
, int_mask
;
814 int_status
= at91_mci_read(host
, AT91_MCI_SR
);
815 int_mask
= at91_mci_read(host
, AT91_MCI_IMR
);
817 pr_debug("MCI irq: status = %08X, %08X, %08X\n", int_status
, int_mask
,
818 int_status
& int_mask
);
820 int_status
= int_status
& int_mask
;
822 if (int_status
& AT91_MCI_ERRORS
) {
825 if (int_status
& AT91_MCI_UNRE
)
826 pr_debug("MMC: Underrun error\n");
827 if (int_status
& AT91_MCI_OVRE
)
828 pr_debug("MMC: Overrun error\n");
829 if (int_status
& AT91_MCI_DTOE
)
830 pr_debug("MMC: Data timeout\n");
831 if (int_status
& AT91_MCI_DCRCE
)
832 pr_debug("MMC: CRC error in data\n");
833 if (int_status
& AT91_MCI_RTOE
)
834 pr_debug("MMC: Response timeout\n");
835 if (int_status
& AT91_MCI_RENDE
)
836 pr_debug("MMC: Response end bit error\n");
837 if (int_status
& AT91_MCI_RCRCE
)
838 pr_debug("MMC: Response CRC error\n");
839 if (int_status
& AT91_MCI_RDIRE
)
840 pr_debug("MMC: Response direction error\n");
841 if (int_status
& AT91_MCI_RINDE
)
842 pr_debug("MMC: Response index error\n");
844 /* Only continue processing if no errors */
846 if (int_status
& AT91_MCI_TXBUFE
) {
847 pr_debug("TX buffer empty\n");
848 at91_mci_handle_transmitted(host
);
851 if (int_status
& AT91_MCI_ENDRX
) {
853 at91_mci_post_dma_read(host
);
856 if (int_status
& AT91_MCI_RXBUFF
) {
857 pr_debug("RX buffer full\n");
858 at91_mci_write(host
, ATMEL_PDC_PTCR
, ATMEL_PDC_RXTDIS
| ATMEL_PDC_TXTDIS
);
859 at91_mci_write(host
, AT91_MCI_IDR
, AT91_MCI_RXBUFF
| AT91_MCI_ENDRX
);
863 if (int_status
& AT91_MCI_ENDTX
)
864 pr_debug("Transmit has ended\n");
866 if (int_status
& AT91_MCI_NOTBUSY
) {
867 pr_debug("Card is ready\n");
868 at91_mci_update_bytes_xfered(host
);
872 if (int_status
& AT91_MCI_DTIP
)
873 pr_debug("Data transfer in progress\n");
875 if (int_status
& AT91_MCI_BLKE
) {
876 pr_debug("Block transfer has ended\n");
877 if (host
->request
->data
&& host
->request
->data
->blocks
> 1) {
878 /* multi block write : complete multi write
879 * command and send stop */
882 at91_mci_write(host
, AT91_MCI_IER
, AT91_MCI_NOTBUSY
);
886 if (int_status
& AT91_MCI_SDIOIRQA
)
887 mmc_signal_sdio_irq(host
->mmc
);
889 if (int_status
& AT91_MCI_SDIOIRQB
)
890 mmc_signal_sdio_irq(host
->mmc
);
892 if (int_status
& AT91_MCI_TXRDY
)
893 pr_debug("Ready to transmit\n");
895 if (int_status
& AT91_MCI_RXRDY
)
896 pr_debug("Ready to receive\n");
898 if (int_status
& AT91_MCI_CMDRDY
) {
899 pr_debug("Command ready\n");
900 completed
= at91_mci_handle_cmdrdy(host
);
905 pr_debug("Completed command\n");
906 at91_mci_write(host
, AT91_MCI_IDR
, 0xffffffff & ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
907 at91_mci_completed_command(host
, int_status
);
909 at91_mci_write(host
, AT91_MCI_IDR
, int_status
& ~(AT91_MCI_SDIOIRQA
| AT91_MCI_SDIOIRQB
));
914 static irqreturn_t
at91_mmc_det_irq(int irq
, void *_host
)
916 struct at91mci_host
*host
= _host
;
917 int present
= !gpio_get_value(irq_to_gpio(irq
));
920 * we expect this irq on both insert and remove,
921 * and use a short delay to debounce.
923 if (present
!= host
->present
) {
924 host
->present
= present
;
925 pr_debug("%s: card %s\n", mmc_hostname(host
->mmc
),
926 present
? "insert" : "remove");
928 pr_debug("****** Resetting SD-card bus width ******\n");
929 at91_mci_write(host
, AT91_MCI_SDCR
, at91_mci_read(host
, AT91_MCI_SDCR
) & ~AT91_MCI_SDCBUS
);
931 /* 0.5s needed because of early card detect switch firing */
932 mmc_detect_change(host
->mmc
, msecs_to_jiffies(500));
937 static int at91_mci_get_ro(struct mmc_host
*mmc
)
939 struct at91mci_host
*host
= mmc_priv(mmc
);
941 if (host
->board
->wp_pin
)
942 return !!gpio_get_value(host
->board
->wp_pin
);
944 * Board doesn't support read only detection; let the mmc core
950 static void at91_mci_enable_sdio_irq(struct mmc_host
*mmc
, int enable
)
952 struct at91mci_host
*host
= mmc_priv(mmc
);
954 pr_debug("%s: sdio_irq %c : %s\n", mmc_hostname(host
->mmc
),
955 host
->board
->slot_b
? 'B':'A', enable
? "enable" : "disable");
956 at91_mci_write(host
, enable
? AT91_MCI_IER
: AT91_MCI_IDR
,
957 host
->board
->slot_b
? AT91_MCI_SDIOIRQB
: AT91_MCI_SDIOIRQA
);
961 static const struct mmc_host_ops at91_mci_ops
= {
962 .request
= at91_mci_request
,
963 .set_ios
= at91_mci_set_ios
,
964 .get_ro
= at91_mci_get_ro
,
965 .enable_sdio_irq
= at91_mci_enable_sdio_irq
,
969 * Probe for the device
971 static int __init
at91_mci_probe(struct platform_device
*pdev
)
973 struct mmc_host
*mmc
;
974 struct at91mci_host
*host
;
975 struct resource
*res
;
978 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
982 if (!request_mem_region(res
->start
, res
->end
- res
->start
+ 1, DRIVER_NAME
))
985 mmc
= mmc_alloc_host(sizeof(struct at91mci_host
), &pdev
->dev
);
988 dev_dbg(&pdev
->dev
, "couldn't allocate mmc host\n");
992 mmc
->ops
= &at91_mci_ops
;
994 mmc
->f_max
= 25000000;
995 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
996 mmc
->caps
= MMC_CAP_SDIO_IRQ
;
998 mmc
->max_blk_size
= MCI_MAXBLKSIZE
;
999 mmc
->max_blk_count
= MCI_BLKATONCE
;
1000 mmc
->max_req_size
= MCI_BUFSIZE
;
1002 host
= mmc_priv(mmc
);
1005 host
->board
= pdev
->dev
.platform_data
;
1006 if (host
->board
->wire4
) {
1007 if (cpu_is_at91sam9260() || cpu_is_at91sam9263())
1008 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1010 dev_warn(&pdev
->dev
, "4 wire bus mode not supported"
1011 " - using 1 wire\n");
1014 host
->buffer
= dma_alloc_coherent(&pdev
->dev
, MCI_BUFSIZE
,
1015 &host
->physical_address
, GFP_KERNEL
);
1016 if (!host
->buffer
) {
1018 dev_err(&pdev
->dev
, "Can't allocate transmit buffer\n");
1023 * Reserve GPIOs ... board init code makes sure these pins are set
1024 * up as GPIOs with the right direction (input, except for vcc)
1026 if (host
->board
->det_pin
) {
1027 ret
= gpio_request(host
->board
->det_pin
, "mmc_detect");
1029 dev_dbg(&pdev
->dev
, "couldn't claim card detect pin\n");
1033 if (host
->board
->wp_pin
) {
1034 ret
= gpio_request(host
->board
->wp_pin
, "mmc_wp");
1036 dev_dbg(&pdev
->dev
, "couldn't claim wp sense pin\n");
1040 if (host
->board
->vcc_pin
) {
1041 ret
= gpio_request(host
->board
->vcc_pin
, "mmc_vcc");
1043 dev_dbg(&pdev
->dev
, "couldn't claim vcc switch pin\n");
1051 host
->mci_clk
= clk_get(&pdev
->dev
, "mci_clk");
1052 if (IS_ERR(host
->mci_clk
)) {
1054 dev_dbg(&pdev
->dev
, "no mci_clk?\n");
1061 host
->baseaddr
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
1062 if (!host
->baseaddr
) {
1070 clk_enable(host
->mci_clk
); /* Enable the peripheral clock */
1071 at91_mci_disable(host
);
1072 at91_mci_enable(host
);
1075 * Allocate the MCI interrupt
1077 host
->irq
= platform_get_irq(pdev
, 0);
1078 ret
= request_irq(host
->irq
, at91_mci_irq
, IRQF_SHARED
,
1079 mmc_hostname(mmc
), host
);
1081 dev_dbg(&pdev
->dev
, "request MCI interrupt failed\n");
1085 setup_timer(&host
->timer
, at91_timeout_timer
, (unsigned long)host
);
1087 platform_set_drvdata(pdev
, mmc
);
1090 * Add host to MMC layer
1092 if (host
->board
->det_pin
) {
1093 host
->present
= !gpio_get_value(host
->board
->det_pin
);
1101 * monitor card insertion/removal if we can
1103 if (host
->board
->det_pin
) {
1104 ret
= request_irq(gpio_to_irq(host
->board
->det_pin
),
1105 at91_mmc_det_irq
, 0, mmc_hostname(mmc
), host
);
1107 dev_warn(&pdev
->dev
, "request MMC detect irq failed\n");
1109 device_init_wakeup(&pdev
->dev
, 1);
1112 pr_debug("Added MCI driver\n");
1117 clk_disable(host
->mci_clk
);
1118 iounmap(host
->baseaddr
);
1120 clk_put(host
->mci_clk
);
1122 if (host
->board
->vcc_pin
)
1123 gpio_free(host
->board
->vcc_pin
);
1125 if (host
->board
->wp_pin
)
1126 gpio_free(host
->board
->wp_pin
);
1128 if (host
->board
->det_pin
)
1129 gpio_free(host
->board
->det_pin
);
1132 dma_free_coherent(&pdev
->dev
, MCI_BUFSIZE
,
1133 host
->buffer
, host
->physical_address
);
1137 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1138 dev_err(&pdev
->dev
, "probe failed, err %d\n", ret
);
1145 static int __exit
at91_mci_remove(struct platform_device
*pdev
)
1147 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1148 struct at91mci_host
*host
;
1149 struct resource
*res
;
1154 host
= mmc_priv(mmc
);
1157 dma_free_coherent(&pdev
->dev
, MCI_BUFSIZE
,
1158 host
->buffer
, host
->physical_address
);
1160 if (host
->board
->det_pin
) {
1161 if (device_can_wakeup(&pdev
->dev
))
1162 free_irq(gpio_to_irq(host
->board
->det_pin
), host
);
1163 device_init_wakeup(&pdev
->dev
, 0);
1164 gpio_free(host
->board
->det_pin
);
1167 at91_mci_disable(host
);
1168 del_timer_sync(&host
->timer
);
1169 mmc_remove_host(mmc
);
1170 free_irq(host
->irq
, host
);
1172 clk_disable(host
->mci_clk
); /* Disable the peripheral clock */
1173 clk_put(host
->mci_clk
);
1175 if (host
->board
->vcc_pin
)
1176 gpio_free(host
->board
->vcc_pin
);
1177 if (host
->board
->wp_pin
)
1178 gpio_free(host
->board
->wp_pin
);
1180 iounmap(host
->baseaddr
);
1181 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1182 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1185 platform_set_drvdata(pdev
, NULL
);
1186 pr_debug("MCI Removed\n");
1192 static int at91_mci_suspend(struct platform_device
*pdev
, pm_message_t state
)
1194 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1195 struct at91mci_host
*host
= mmc_priv(mmc
);
1198 if (host
->board
->det_pin
&& device_may_wakeup(&pdev
->dev
))
1199 enable_irq_wake(host
->board
->det_pin
);
1202 ret
= mmc_suspend_host(mmc
, state
);
1207 static int at91_mci_resume(struct platform_device
*pdev
)
1209 struct mmc_host
*mmc
= platform_get_drvdata(pdev
);
1210 struct at91mci_host
*host
= mmc_priv(mmc
);
1213 if (host
->board
->det_pin
&& device_may_wakeup(&pdev
->dev
))
1214 disable_irq_wake(host
->board
->det_pin
);
1217 ret
= mmc_resume_host(mmc
);
1222 #define at91_mci_suspend NULL
1223 #define at91_mci_resume NULL
1226 static struct platform_driver at91_mci_driver
= {
1227 .remove
= __exit_p(at91_mci_remove
),
1228 .suspend
= at91_mci_suspend
,
1229 .resume
= at91_mci_resume
,
1231 .name
= DRIVER_NAME
,
1232 .owner
= THIS_MODULE
,
1236 static int __init
at91_mci_init(void)
1238 return platform_driver_probe(&at91_mci_driver
, at91_mci_probe
);
1241 static void __exit
at91_mci_exit(void)
1243 platform_driver_unregister(&at91_mci_driver
);
1246 module_init(at91_mci_init
);
1247 module_exit(at91_mci_exit
);
1249 MODULE_DESCRIPTION("AT91 Multimedia Card Interface driver");
1250 MODULE_AUTHOR("Nick Randell");
1251 MODULE_LICENSE("GPL");
1252 MODULE_ALIAS("platform:at91_mci");