2 * Atmel MultiMedia Card Interface driver
4 * Copyright (C) 2004-2008 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/blkdev.h>
11 #include <linux/clk.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gpio.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/seq_file.h>
23 #include <linux/stat.h>
25 #include <linux/mmc/host.h>
27 #include <asm/atmel-mci.h>
29 #include <asm/unaligned.h>
31 #include <mach/board.h>
33 #include "atmel-mci-regs.h"
35 #define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
38 EVENT_CMD_COMPLETE
= 0,
50 struct scatterlist
*sg
;
51 unsigned int pio_offset
;
53 struct mmc_request
*mrq
;
54 struct mmc_command
*cmd
;
55 struct mmc_data
*data
;
65 struct tasklet_struct tasklet
;
66 unsigned long pending_events
;
67 unsigned long completed_events
;
73 /* For detect pin debouncing */
74 struct timer_list detect_timer
;
77 unsigned long mapbase
;
79 struct platform_device
*pdev
;
82 #define atmci_is_completed(host, event) \
83 test_bit(event, &host->completed_events)
84 #define atmci_test_and_clear_pending(host, event) \
85 test_and_clear_bit(event, &host->pending_events)
86 #define atmci_test_and_set_completed(host, event) \
87 test_and_set_bit(event, &host->completed_events)
88 #define atmci_set_completed(host, event) \
89 set_bit(event, &host->completed_events)
90 #define atmci_set_pending(host, event) \
91 set_bit(event, &host->pending_events)
92 #define atmci_clear_pending(host, event) \
93 clear_bit(event, &host->pending_events)
96 * The debugfs stuff below is mostly optimized away when
97 * CONFIG_DEBUG_FS is not set.
99 static int atmci_req_show(struct seq_file
*s
, void *v
)
101 struct atmel_mci
*host
= s
->private;
102 struct mmc_request
*mrq
= host
->mrq
;
103 struct mmc_command
*cmd
;
104 struct mmc_command
*stop
;
105 struct mmc_data
*data
;
107 /* Make sure we get a consistent snapshot */
108 spin_lock_irq(&host
->mmc
->lock
);
117 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
118 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
119 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
120 cmd
->resp
[2], cmd
->error
);
122 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
123 data
->bytes_xfered
, data
->blocks
,
124 data
->blksz
, data
->flags
, data
->error
);
127 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
128 stop
->opcode
, stop
->arg
, stop
->flags
,
129 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
130 stop
->resp
[2], stop
->error
);
133 spin_unlock_irq(&host
->mmc
->lock
);
138 static int atmci_req_open(struct inode
*inode
, struct file
*file
)
140 return single_open(file
, atmci_req_show
, inode
->i_private
);
143 static const struct file_operations atmci_req_fops
= {
144 .owner
= THIS_MODULE
,
145 .open
= atmci_req_open
,
148 .release
= single_release
,
151 static void atmci_show_status_reg(struct seq_file
*s
,
152 const char *regname
, u32 value
)
154 static const char *sr_bit
[] = {
175 seq_printf(s
, "%s:\t0x%08x", regname
, value
);
176 for (i
= 0; i
< ARRAY_SIZE(sr_bit
); i
++) {
177 if (value
& (1 << i
)) {
179 seq_printf(s
, " %s", sr_bit
[i
]);
181 seq_puts(s
, " UNKNOWN");
187 static int atmci_regs_show(struct seq_file
*s
, void *v
)
189 struct atmel_mci
*host
= s
->private;
192 buf
= kmalloc(MCI_REGS_SIZE
, GFP_KERNEL
);
196 /* Grab a more or less consistent snapshot */
197 spin_lock_irq(&host
->mmc
->lock
);
198 clk_enable(host
->mck
);
199 memcpy_fromio(buf
, host
->regs
, MCI_REGS_SIZE
);
200 clk_disable(host
->mck
);
201 spin_unlock_irq(&host
->mmc
->lock
);
203 seq_printf(s
, "MR:\t0x%08x%s%s CLKDIV=%u\n",
205 buf
[MCI_MR
/ 4] & MCI_MR_RDPROOF
? " RDPROOF" : "",
206 buf
[MCI_MR
/ 4] & MCI_MR_WRPROOF
? " WRPROOF" : "",
207 buf
[MCI_MR
/ 4] & 0xff);
208 seq_printf(s
, "DTOR:\t0x%08x\n", buf
[MCI_DTOR
/ 4]);
209 seq_printf(s
, "SDCR:\t0x%08x\n", buf
[MCI_SDCR
/ 4]);
210 seq_printf(s
, "ARGR:\t0x%08x\n", buf
[MCI_ARGR
/ 4]);
211 seq_printf(s
, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
213 buf
[MCI_BLKR
/ 4] & 0xffff,
214 (buf
[MCI_BLKR
/ 4] >> 16) & 0xffff);
216 /* Don't read RSPR and RDR; it will consume the data there */
218 atmci_show_status_reg(s
, "SR", buf
[MCI_SR
/ 4]);
219 atmci_show_status_reg(s
, "IMR", buf
[MCI_IMR
/ 4]);
226 static int atmci_regs_open(struct inode
*inode
, struct file
*file
)
228 return single_open(file
, atmci_regs_show
, inode
->i_private
);
231 static const struct file_operations atmci_regs_fops
= {
232 .owner
= THIS_MODULE
,
233 .open
= atmci_regs_open
,
236 .release
= single_release
,
239 static void atmci_init_debugfs(struct atmel_mci
*host
)
241 struct mmc_host
*mmc
;
246 root
= mmc
->debugfs_root
;
250 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
257 node
= debugfs_create_file("req", S_IRUSR
, root
, host
, &atmci_req_fops
);
261 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
262 (u32
*)&host
->pending_events
);
266 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
267 (u32
*)&host
->completed_events
);
274 dev_err(&host
->pdev
->dev
,
275 "failed to initialize debugfs for controller\n");
278 static void atmci_enable(struct atmel_mci
*host
)
280 clk_enable(host
->mck
);
281 mci_writel(host
, CR
, MCI_CR_MCIEN
);
282 mci_writel(host
, MR
, host
->mode_reg
);
283 mci_writel(host
, SDCR
, host
->sdc_reg
);
286 static void atmci_disable(struct atmel_mci
*host
)
288 mci_writel(host
, CR
, MCI_CR_SWRST
);
290 /* Stall until write is complete, then disable the bus clock */
292 clk_disable(host
->mck
);
295 static inline unsigned int ns_to_clocks(struct atmel_mci
*host
,
298 return (ns
* (host
->bus_hz
/ 1000000) + 999) / 1000;
301 static void atmci_set_timeout(struct atmel_mci
*host
,
302 struct mmc_data
*data
)
304 static unsigned dtomul_to_shift
[] = {
305 0, 4, 7, 8, 10, 12, 16, 20
311 timeout
= ns_to_clocks(host
, data
->timeout_ns
) + data
->timeout_clks
;
313 for (dtomul
= 0; dtomul
< 8; dtomul
++) {
314 unsigned shift
= dtomul_to_shift
[dtomul
];
315 dtocyc
= (timeout
+ (1 << shift
) - 1) >> shift
;
325 dev_vdbg(&host
->mmc
->class_dev
, "setting timeout to %u cycles\n",
326 dtocyc
<< dtomul_to_shift
[dtomul
]);
327 mci_writel(host
, DTOR
, (MCI_DTOMUL(dtomul
) | MCI_DTOCYC(dtocyc
)));
331 * Return mask with command flags to be enabled for this command.
333 static u32
atmci_prepare_command(struct mmc_host
*mmc
,
334 struct mmc_command
*cmd
)
336 struct mmc_data
*data
;
339 cmd
->error
= -EINPROGRESS
;
341 cmdr
= MCI_CMDR_CMDNB(cmd
->opcode
);
343 if (cmd
->flags
& MMC_RSP_PRESENT
) {
344 if (cmd
->flags
& MMC_RSP_136
)
345 cmdr
|= MCI_CMDR_RSPTYP_136BIT
;
347 cmdr
|= MCI_CMDR_RSPTYP_48BIT
;
351 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
352 * it's too difficult to determine whether this is an ACMD or
353 * not. Better make it 64.
355 cmdr
|= MCI_CMDR_MAXLAT_64CYC
;
357 if (mmc
->ios
.bus_mode
== MMC_BUSMODE_OPENDRAIN
)
358 cmdr
|= MCI_CMDR_OPDCMD
;
362 cmdr
|= MCI_CMDR_START_XFER
;
363 if (data
->flags
& MMC_DATA_STREAM
)
364 cmdr
|= MCI_CMDR_STREAM
;
365 else if (data
->blocks
> 1)
366 cmdr
|= MCI_CMDR_MULTI_BLOCK
;
368 cmdr
|= MCI_CMDR_BLOCK
;
370 if (data
->flags
& MMC_DATA_READ
)
371 cmdr
|= MCI_CMDR_TRDIR_READ
;
377 static void atmci_start_command(struct atmel_mci
*host
,
378 struct mmc_command
*cmd
,
381 /* Must read host->cmd after testing event flags */
386 dev_vdbg(&host
->mmc
->class_dev
,
387 "start command: ARGR=0x%08x CMDR=0x%08x\n",
388 cmd
->arg
, cmd_flags
);
390 mci_writel(host
, ARGR
, cmd
->arg
);
391 mci_writel(host
, CMDR
, cmd_flags
);
394 static void send_stop_cmd(struct mmc_host
*mmc
, struct mmc_data
*data
)
396 struct atmel_mci
*host
= mmc_priv(mmc
);
398 atmci_start_command(host
, data
->stop
, host
->stop_cmdr
);
399 mci_writel(host
, IER
, MCI_CMDRDY
);
402 static void atmci_request_end(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
404 struct atmel_mci
*host
= mmc_priv(mmc
);
406 WARN_ON(host
->cmd
|| host
->data
);
411 mmc_request_done(mmc
, mrq
);
415 * Returns a mask of interrupt flags to be enabled after the whole
416 * request has been prepared.
418 static u32
atmci_submit_data(struct mmc_host
*mmc
, struct mmc_data
*data
)
420 struct atmel_mci
*host
= mmc_priv(mmc
);
423 data
->error
= -EINPROGRESS
;
429 dev_vdbg(&mmc
->class_dev
, "BLKR=0x%08x\n",
430 MCI_BCNT(data
->blocks
) | MCI_BLKLEN(data
->blksz
));
432 iflags
= ATMCI_DATA_ERROR_FLAGS
;
434 host
->pio_offset
= 0;
435 if (data
->flags
& MMC_DATA_READ
)
443 static void atmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
445 struct atmel_mci
*host
= mmc_priv(mmc
);
446 struct mmc_data
*data
;
447 struct mmc_command
*cmd
;
451 iflags
= mci_readl(host
, IMR
);
453 dev_warn(&mmc
->class_dev
, "WARNING: IMR=0x%08x\n",
454 mci_readl(host
, IMR
));
456 WARN_ON(host
->mrq
!= NULL
);
459 * We may "know" the card is gone even though there's still an
460 * electrical connection. If so, we really need to communicate
461 * this to the MMC core since there won't be any more
462 * interrupts as the card is completely removed. Otherwise,
463 * the MMC core might believe the card is still there even
464 * though the card was just removed very slowly.
466 if (!host
->present
) {
467 mrq
->cmd
->error
= -ENOMEDIUM
;
468 mmc_request_done(mmc
, mrq
);
473 host
->pending_events
= 0;
474 host
->completed_events
= 0;
478 /* We don't support multiple blocks of weird lengths. */
481 if (data
->blocks
> 1 && data
->blksz
& 3)
483 atmci_set_timeout(host
, data
);
485 /* Must set block count/size before sending command */
486 mci_writel(host
, BLKR
, MCI_BCNT(data
->blocks
)
487 | MCI_BLKLEN(data
->blksz
));
492 cmdflags
= atmci_prepare_command(mmc
, cmd
);
493 atmci_start_command(host
, cmd
, cmdflags
);
496 iflags
|= atmci_submit_data(mmc
, data
);
499 host
->stop_cmdr
= atmci_prepare_command(mmc
, mrq
->stop
);
500 host
->stop_cmdr
|= MCI_CMDR_STOP_XFER
;
501 if (!(data
->flags
& MMC_DATA_WRITE
))
502 host
->stop_cmdr
|= MCI_CMDR_TRDIR_READ
;
503 if (data
->flags
& MMC_DATA_STREAM
)
504 host
->stop_cmdr
|= MCI_CMDR_STREAM
;
506 host
->stop_cmdr
|= MCI_CMDR_MULTI_BLOCK
;
510 * We could have enabled interrupts earlier, but I suspect
511 * that would open up a nice can of interesting race
512 * conditions (e.g. command and data complete, but stop not
515 mci_writel(host
, IER
, iflags
);
522 mrq
->cmd
->error
= -EINVAL
;
523 mmc_request_done(mmc
, mrq
);
526 static void atmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
528 struct atmel_mci
*host
= mmc_priv(mmc
);
534 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, 2 * ios
->clock
) - 1;
536 dev_warn(&mmc
->class_dev
,
537 "clock %u too slow; using %lu\n",
538 ios
->clock
, host
->bus_hz
/ (2 * 256));
542 host
->mode_reg
= MCI_MR_CLKDIV(clkdiv
) | MCI_MR_WRPROOF
546 switch (ios
->bus_width
) {
547 case MMC_BUS_WIDTH_1
:
550 case MMC_BUS_WIDTH_4
:
551 host
->sdc_reg
= MCI_SDCBUS_4BIT
;
555 switch (ios
->power_mode
) {
557 /* Send init sequence (74 clock cycles) */
559 mci_writel(host
, CMDR
, MCI_CMDR_SPCMD_INIT
);
560 while (!(mci_readl(host
, SR
) & MCI_CMDRDY
))
566 * TODO: None of the currently available AVR32-based
567 * boards allow MMC power to be turned off. Implement
568 * power control when this can be tested properly.
574 static int atmci_get_ro(struct mmc_host
*mmc
)
577 struct atmel_mci
*host
= mmc_priv(mmc
);
579 if (gpio_is_valid(host
->wp_pin
)) {
580 read_only
= gpio_get_value(host
->wp_pin
);
581 dev_dbg(&mmc
->class_dev
, "card is %s\n",
582 read_only
? "read-only" : "read-write");
584 dev_dbg(&mmc
->class_dev
,
585 "no pin for checking read-only switch."
586 " Assuming write-enable.\n");
592 static struct mmc_host_ops atmci_ops
= {
593 .request
= atmci_request
,
594 .set_ios
= atmci_set_ios
,
595 .get_ro
= atmci_get_ro
,
598 static void atmci_command_complete(struct atmel_mci
*host
,
599 struct mmc_command
*cmd
, u32 status
)
601 /* Read the response from the card (up to 16 bytes) */
602 cmd
->resp
[0] = mci_readl(host
, RSPR
);
603 cmd
->resp
[1] = mci_readl(host
, RSPR
);
604 cmd
->resp
[2] = mci_readl(host
, RSPR
);
605 cmd
->resp
[3] = mci_readl(host
, RSPR
);
607 if (status
& MCI_RTOE
)
608 cmd
->error
= -ETIMEDOUT
;
609 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& MCI_RCRCE
))
610 cmd
->error
= -EILSEQ
;
611 else if (status
& (MCI_RINDE
| MCI_RDIRE
| MCI_RENDE
))
617 dev_dbg(&host
->mmc
->class_dev
,
618 "command error: status=0x%08x\n", status
);
622 mci_writel(host
, IDR
, MCI_NOTBUSY
623 | MCI_TXRDY
| MCI_RXRDY
624 | ATMCI_DATA_ERROR_FLAGS
);
629 static void atmci_detect_change(unsigned long data
)
631 struct atmel_mci
*host
= (struct atmel_mci
*)data
;
632 struct mmc_request
*mrq
= host
->mrq
;
636 * atmci_remove() sets detect_pin to -1 before freeing the
637 * interrupt. We must not re-enable the interrupt if it has
641 if (!gpio_is_valid(host
->detect_pin
))
644 enable_irq(gpio_to_irq(host
->detect_pin
));
645 present
= !gpio_get_value(host
->detect_pin
);
647 dev_vdbg(&host
->pdev
->dev
, "detect change: %d (was %d)\n",
648 present
, host
->present
);
650 if (present
!= host
->present
) {
651 dev_dbg(&host
->mmc
->class_dev
, "card %s\n",
652 present
? "inserted" : "removed");
653 host
->present
= present
;
655 /* Reset controller if card is gone */
657 mci_writel(host
, CR
, MCI_CR_SWRST
);
658 mci_writel(host
, IDR
, ~0UL);
659 mci_writel(host
, CR
, MCI_CR_MCIEN
);
662 /* Clean up queue if present */
665 * Reset controller to terminate any ongoing
666 * commands or data transfers.
668 mci_writel(host
, CR
, MCI_CR_SWRST
);
670 if (!atmci_is_completed(host
, EVENT_CMD_COMPLETE
))
671 mrq
->cmd
->error
= -ENOMEDIUM
;
673 if (mrq
->data
&& !atmci_is_completed(host
,
674 EVENT_DATA_COMPLETE
)) {
676 mrq
->data
->error
= -ENOMEDIUM
;
678 if (mrq
->stop
&& !atmci_is_completed(host
,
679 EVENT_STOP_COMPLETE
))
680 mrq
->stop
->error
= -ENOMEDIUM
;
683 atmci_request_end(host
->mmc
, mrq
);
686 mmc_detect_change(host
->mmc
, 0);
690 static void atmci_tasklet_func(unsigned long priv
)
692 struct mmc_host
*mmc
= (struct mmc_host
*)priv
;
693 struct atmel_mci
*host
= mmc_priv(mmc
);
694 struct mmc_request
*mrq
= host
->mrq
;
695 struct mmc_data
*data
= host
->data
;
697 dev_vdbg(&mmc
->class_dev
,
698 "tasklet: pending/completed/mask %lx/%lx/%x\n",
699 host
->pending_events
, host
->completed_events
,
700 mci_readl(host
, IMR
));
702 if (atmci_test_and_clear_pending(host
, EVENT_CMD_COMPLETE
)) {
704 * host->cmd must be set to NULL before the interrupt
705 * handler sees EVENT_CMD_COMPLETE
709 atmci_set_completed(host
, EVENT_CMD_COMPLETE
);
710 atmci_command_complete(host
, mrq
->cmd
, host
->cmd_status
);
712 if (!mrq
->cmd
->error
&& mrq
->stop
713 && atmci_is_completed(host
, EVENT_XFER_COMPLETE
)
714 && !atmci_test_and_set_completed(host
,
716 send_stop_cmd(host
->mmc
, mrq
->data
);
718 if (atmci_test_and_clear_pending(host
, EVENT_STOP_COMPLETE
)) {
720 * host->cmd must be set to NULL before the interrupt
721 * handler sees EVENT_STOP_COMPLETE
725 atmci_set_completed(host
, EVENT_STOP_COMPLETE
);
726 atmci_command_complete(host
, mrq
->stop
, host
->stop_status
);
728 if (atmci_test_and_clear_pending(host
, EVENT_DATA_ERROR
)) {
729 u32 status
= host
->data_status
;
731 dev_vdbg(&mmc
->class_dev
, "data error: status=%08x\n", status
);
733 atmci_set_completed(host
, EVENT_DATA_ERROR
);
734 atmci_set_completed(host
, EVENT_DATA_COMPLETE
);
736 if (status
& MCI_DTOE
) {
737 dev_dbg(&mmc
->class_dev
,
738 "data timeout error\n");
739 data
->error
= -ETIMEDOUT
;
740 } else if (status
& MCI_DCRCE
) {
741 dev_dbg(&mmc
->class_dev
, "data CRC error\n");
742 data
->error
= -EILSEQ
;
744 dev_dbg(&mmc
->class_dev
,
745 "data FIFO error (status=%08x)\n",
750 if (host
->present
&& data
->stop
751 && atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
752 && !atmci_test_and_set_completed(
753 host
, EVENT_STOP_SENT
))
754 send_stop_cmd(host
->mmc
, data
);
758 if (atmci_test_and_clear_pending(host
, EVENT_DATA_COMPLETE
)) {
759 atmci_set_completed(host
, EVENT_DATA_COMPLETE
);
761 if (!atmci_is_completed(host
, EVENT_DATA_ERROR
)) {
762 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
769 if (host
->mrq
&& !host
->cmd
&& !host
->data
)
770 atmci_request_end(mmc
, host
->mrq
);
773 static void atmci_read_data_pio(struct atmel_mci
*host
)
775 struct scatterlist
*sg
= host
->sg
;
776 void *buf
= sg_virt(sg
);
777 unsigned int offset
= host
->pio_offset
;
778 struct mmc_data
*data
= host
->data
;
781 unsigned int nbytes
= 0;
784 value
= mci_readl(host
, RDR
);
785 if (likely(offset
+ 4 <= sg
->length
)) {
786 put_unaligned(value
, (u32
*)(buf
+ offset
));
791 if (offset
== sg
->length
) {
792 host
->sg
= sg
= sg_next(sg
);
800 unsigned int remaining
= sg
->length
- offset
;
801 memcpy(buf
+ offset
, &value
, remaining
);
804 flush_dcache_page(sg_page(sg
));
805 host
->sg
= sg
= sg_next(sg
);
809 offset
= 4 - remaining
;
811 memcpy(buf
, (u8
*)&value
+ remaining
, offset
);
815 status
= mci_readl(host
, SR
);
816 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
817 mci_writel(host
, IDR
, (MCI_NOTBUSY
| MCI_RXRDY
818 | ATMCI_DATA_ERROR_FLAGS
));
819 host
->data_status
= status
;
820 atmci_set_pending(host
, EVENT_DATA_ERROR
);
821 tasklet_schedule(&host
->tasklet
);
824 } while (status
& MCI_RXRDY
);
826 host
->pio_offset
= offset
;
827 data
->bytes_xfered
+= nbytes
;
832 mci_writel(host
, IDR
, MCI_RXRDY
);
833 mci_writel(host
, IER
, MCI_NOTBUSY
);
834 data
->bytes_xfered
+= nbytes
;
835 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
836 if (data
->stop
&& atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
837 && !atmci_test_and_set_completed(host
, EVENT_STOP_SENT
))
838 send_stop_cmd(host
->mmc
, data
);
841 static void atmci_write_data_pio(struct atmel_mci
*host
)
843 struct scatterlist
*sg
= host
->sg
;
844 void *buf
= sg_virt(sg
);
845 unsigned int offset
= host
->pio_offset
;
846 struct mmc_data
*data
= host
->data
;
849 unsigned int nbytes
= 0;
852 if (likely(offset
+ 4 <= sg
->length
)) {
853 value
= get_unaligned((u32
*)(buf
+ offset
));
854 mci_writel(host
, TDR
, value
);
858 if (offset
== sg
->length
) {
859 host
->sg
= sg
= sg_next(sg
);
867 unsigned int remaining
= sg
->length
- offset
;
870 memcpy(&value
, buf
+ offset
, remaining
);
873 host
->sg
= sg
= sg_next(sg
);
875 mci_writel(host
, TDR
, value
);
879 offset
= 4 - remaining
;
881 memcpy((u8
*)&value
+ remaining
, buf
, offset
);
882 mci_writel(host
, TDR
, value
);
886 status
= mci_readl(host
, SR
);
887 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
888 mci_writel(host
, IDR
, (MCI_NOTBUSY
| MCI_TXRDY
889 | ATMCI_DATA_ERROR_FLAGS
));
890 host
->data_status
= status
;
891 atmci_set_pending(host
, EVENT_DATA_ERROR
);
892 tasklet_schedule(&host
->tasklet
);
895 } while (status
& MCI_TXRDY
);
897 host
->pio_offset
= offset
;
898 data
->bytes_xfered
+= nbytes
;
903 mci_writel(host
, IDR
, MCI_TXRDY
);
904 mci_writel(host
, IER
, MCI_NOTBUSY
);
905 data
->bytes_xfered
+= nbytes
;
906 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
907 if (data
->stop
&& atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
908 && !atmci_test_and_set_completed(host
, EVENT_STOP_SENT
))
909 send_stop_cmd(host
->mmc
, data
);
912 static void atmci_cmd_interrupt(struct mmc_host
*mmc
, u32 status
)
914 struct atmel_mci
*host
= mmc_priv(mmc
);
916 mci_writel(host
, IDR
, MCI_CMDRDY
);
918 if (atmci_is_completed(host
, EVENT_STOP_SENT
)) {
919 host
->stop_status
= status
;
920 atmci_set_pending(host
, EVENT_STOP_COMPLETE
);
922 host
->cmd_status
= status
;
923 atmci_set_pending(host
, EVENT_CMD_COMPLETE
);
926 tasklet_schedule(&host
->tasklet
);
929 static irqreturn_t
atmci_interrupt(int irq
, void *dev_id
)
931 struct mmc_host
*mmc
= dev_id
;
932 struct atmel_mci
*host
= mmc_priv(mmc
);
933 u32 status
, mask
, pending
;
934 unsigned int pass_count
= 0;
936 spin_lock(&mmc
->lock
);
939 status
= mci_readl(host
, SR
);
940 mask
= mci_readl(host
, IMR
);
941 pending
= status
& mask
;
945 if (pending
& ATMCI_DATA_ERROR_FLAGS
) {
946 mci_writel(host
, IDR
, ATMCI_DATA_ERROR_FLAGS
947 | MCI_RXRDY
| MCI_TXRDY
);
948 pending
&= mci_readl(host
, IMR
);
949 host
->data_status
= status
;
950 atmci_set_pending(host
, EVENT_DATA_ERROR
);
951 tasklet_schedule(&host
->tasklet
);
953 if (pending
& MCI_NOTBUSY
) {
954 mci_writel(host
, IDR
, (MCI_NOTBUSY
955 | ATMCI_DATA_ERROR_FLAGS
));
956 atmci_set_pending(host
, EVENT_DATA_COMPLETE
);
957 tasklet_schedule(&host
->tasklet
);
959 if (pending
& MCI_RXRDY
)
960 atmci_read_data_pio(host
);
961 if (pending
& MCI_TXRDY
)
962 atmci_write_data_pio(host
);
964 if (pending
& MCI_CMDRDY
)
965 atmci_cmd_interrupt(mmc
, status
);
966 } while (pass_count
++ < 5);
968 spin_unlock(&mmc
->lock
);
970 return pass_count
? IRQ_HANDLED
: IRQ_NONE
;
973 static irqreturn_t
atmci_detect_interrupt(int irq
, void *dev_id
)
975 struct mmc_host
*mmc
= dev_id
;
976 struct atmel_mci
*host
= mmc_priv(mmc
);
979 * Disable interrupts until the pin has stabilized and check
980 * the state then. Use mod_timer() since we may be in the
981 * middle of the timer routine when this interrupt triggers.
983 disable_irq_nosync(irq
);
984 mod_timer(&host
->detect_timer
, jiffies
+ msecs_to_jiffies(20));
989 static int __init
atmci_probe(struct platform_device
*pdev
)
991 struct mci_platform_data
*pdata
;
992 struct atmel_mci
*host
;
993 struct mmc_host
*mmc
;
994 struct resource
*regs
;
998 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1001 pdata
= pdev
->dev
.platform_data
;
1004 irq
= platform_get_irq(pdev
, 0);
1008 mmc
= mmc_alloc_host(sizeof(struct atmel_mci
), &pdev
->dev
);
1012 host
= mmc_priv(mmc
);
1015 host
->detect_pin
= pdata
->detect_pin
;
1016 host
->wp_pin
= pdata
->wp_pin
;
1018 host
->mck
= clk_get(&pdev
->dev
, "mci_clk");
1019 if (IS_ERR(host
->mck
)) {
1020 ret
= PTR_ERR(host
->mck
);
1025 host
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
1029 clk_enable(host
->mck
);
1030 mci_writel(host
, CR
, MCI_CR_SWRST
);
1031 host
->bus_hz
= clk_get_rate(host
->mck
);
1032 clk_disable(host
->mck
);
1034 host
->mapbase
= regs
->start
;
1036 mmc
->ops
= &atmci_ops
;
1037 mmc
->f_min
= (host
->bus_hz
+ 511) / 512;
1038 mmc
->f_max
= host
->bus_hz
/ 2;
1039 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1040 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1042 mmc
->max_hw_segs
= 64;
1043 mmc
->max_phys_segs
= 64;
1044 mmc
->max_req_size
= 32768 * 512;
1045 mmc
->max_blk_size
= 32768;
1046 mmc
->max_blk_count
= 512;
1048 tasklet_init(&host
->tasklet
, atmci_tasklet_func
, (unsigned long)mmc
);
1050 ret
= request_irq(irq
, atmci_interrupt
, 0, pdev
->dev
.bus_id
, mmc
);
1052 goto err_request_irq
;
1054 /* Assume card is present if we don't have a detect pin */
1056 if (gpio_is_valid(host
->detect_pin
)) {
1057 if (gpio_request(host
->detect_pin
, "mmc_detect")) {
1058 dev_dbg(&mmc
->class_dev
, "no detect pin available\n");
1059 host
->detect_pin
= -1;
1061 host
->present
= !gpio_get_value(host
->detect_pin
);
1065 if (!gpio_is_valid(host
->detect_pin
))
1066 mmc
->caps
|= MMC_CAP_NEEDS_POLL
;
1068 if (gpio_is_valid(host
->wp_pin
)) {
1069 if (gpio_request(host
->wp_pin
, "mmc_wp")) {
1070 dev_dbg(&mmc
->class_dev
, "no WP pin available\n");
1075 platform_set_drvdata(pdev
, host
);
1079 if (gpio_is_valid(host
->detect_pin
)) {
1080 setup_timer(&host
->detect_timer
, atmci_detect_change
,
1081 (unsigned long)host
);
1083 ret
= request_irq(gpio_to_irq(host
->detect_pin
),
1084 atmci_detect_interrupt
,
1085 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
1088 dev_dbg(&mmc
->class_dev
,
1089 "could not request IRQ %d for detect pin\n",
1090 gpio_to_irq(host
->detect_pin
));
1091 gpio_free(host
->detect_pin
);
1092 host
->detect_pin
= -1;
1096 dev_info(&mmc
->class_dev
,
1097 "Atmel MCI controller at 0x%08lx irq %d\n",
1098 host
->mapbase
, irq
);
1100 atmci_init_debugfs(host
);
1105 iounmap(host
->regs
);
1113 static int __exit
atmci_remove(struct platform_device
*pdev
)
1115 struct atmel_mci
*host
= platform_get_drvdata(pdev
);
1117 platform_set_drvdata(pdev
, NULL
);
1120 /* Debugfs stuff is cleaned up by mmc core */
1122 if (gpio_is_valid(host
->detect_pin
)) {
1123 int pin
= host
->detect_pin
;
1125 /* Make sure the timer doesn't enable the interrupt */
1126 host
->detect_pin
= -1;
1129 free_irq(gpio_to_irq(pin
), host
->mmc
);
1130 del_timer_sync(&host
->detect_timer
);
1134 mmc_remove_host(host
->mmc
);
1136 clk_enable(host
->mck
);
1137 mci_writel(host
, IDR
, ~0UL);
1138 mci_writel(host
, CR
, MCI_CR_MCIDIS
);
1139 mci_readl(host
, SR
);
1140 clk_disable(host
->mck
);
1142 if (gpio_is_valid(host
->wp_pin
))
1143 gpio_free(host
->wp_pin
);
1145 free_irq(platform_get_irq(pdev
, 0), host
->mmc
);
1146 iounmap(host
->regs
);
1150 mmc_free_host(host
->mmc
);
1155 static struct platform_driver atmci_driver
= {
1156 .remove
= __exit_p(atmci_remove
),
1158 .name
= "atmel_mci",
1162 static int __init
atmci_init(void)
1164 return platform_driver_probe(&atmci_driver
, atmci_probe
);
1167 static void __exit
atmci_exit(void)
1169 platform_driver_unregister(&atmci_driver
);
1172 module_init(atmci_init
);
1173 module_exit(atmci_exit
);
1175 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1176 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1177 MODULE_LICENSE("GPL v2");