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/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/scatterlist.h>
20 #include <linux/seq_file.h>
21 #include <linux/stat.h>
23 #include <linux/mmc/host.h>
25 #include <asm/atmel-mci.h>
27 #include <asm/unaligned.h>
29 #include <asm/arch/board.h>
30 #include <asm/arch/gpio.h>
32 #include "atmel-mci-regs.h"
34 #define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
37 EVENT_CMD_COMPLETE
= 0,
49 struct scatterlist
*sg
;
50 unsigned int pio_offset
;
52 struct mmc_request
*mrq
;
53 struct mmc_command
*cmd
;
54 struct mmc_data
*data
;
64 struct tasklet_struct tasklet
;
65 unsigned long pending_events
;
66 unsigned long completed_events
;
72 /* For detect pin debouncing */
73 struct timer_list detect_timer
;
76 unsigned long mapbase
;
78 struct platform_device
*pdev
;
81 #define atmci_is_completed(host, event) \
82 test_bit(event, &host->completed_events)
83 #define atmci_test_and_clear_pending(host, event) \
84 test_and_clear_bit(event, &host->pending_events)
85 #define atmci_test_and_set_completed(host, event) \
86 test_and_set_bit(event, &host->completed_events)
87 #define atmci_set_completed(host, event) \
88 set_bit(event, &host->completed_events)
89 #define atmci_set_pending(host, event) \
90 set_bit(event, &host->pending_events)
91 #define atmci_clear_pending(host, event) \
92 clear_bit(event, &host->pending_events)
95 * The debugfs stuff below is mostly optimized away when
96 * CONFIG_DEBUG_FS is not set.
98 static int atmci_req_show(struct seq_file
*s
, void *v
)
100 struct atmel_mci
*host
= s
->private;
101 struct mmc_request
*mrq
= host
->mrq
;
102 struct mmc_command
*cmd
;
103 struct mmc_command
*stop
;
104 struct mmc_data
*data
;
106 /* Make sure we get a consistent snapshot */
107 spin_lock_irq(&host
->mmc
->lock
);
116 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
117 cmd
->opcode
, cmd
->arg
, cmd
->flags
,
118 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2],
119 cmd
->resp
[2], cmd
->error
);
121 seq_printf(s
, "DATA %u / %u * %u flg %x err %d\n",
122 data
->bytes_xfered
, data
->blocks
,
123 data
->blksz
, data
->flags
, data
->error
);
126 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
127 stop
->opcode
, stop
->arg
, stop
->flags
,
128 stop
->resp
[0], stop
->resp
[1], stop
->resp
[2],
129 stop
->resp
[2], stop
->error
);
132 spin_unlock_irq(&host
->mmc
->lock
);
137 static int atmci_req_open(struct inode
*inode
, struct file
*file
)
139 return single_open(file
, atmci_req_show
, inode
->i_private
);
142 static const struct file_operations atmci_req_fops
= {
143 .owner
= THIS_MODULE
,
144 .open
= atmci_req_open
,
147 .release
= single_release
,
150 static void atmci_show_status_reg(struct seq_file
*s
,
151 const char *regname
, u32 value
)
153 static const char *sr_bit
[] = {
174 seq_printf(s
, "%s:\t0x%08x", regname
, value
);
175 for (i
= 0; i
< ARRAY_SIZE(sr_bit
); i
++) {
176 if (value
& (1 << i
)) {
178 seq_printf(s
, " %s", sr_bit
[i
]);
180 seq_puts(s
, " UNKNOWN");
186 static int atmci_regs_show(struct seq_file
*s
, void *v
)
188 struct atmel_mci
*host
= s
->private;
191 buf
= kmalloc(MCI_REGS_SIZE
, GFP_KERNEL
);
195 /* Grab a more or less consistent snapshot */
196 spin_lock_irq(&host
->mmc
->lock
);
197 memcpy_fromio(buf
, host
->regs
, MCI_REGS_SIZE
);
198 spin_unlock_irq(&host
->mmc
->lock
);
200 seq_printf(s
, "MR:\t0x%08x%s%s CLKDIV=%u\n",
202 buf
[MCI_MR
/ 4] & MCI_MR_RDPROOF
? " RDPROOF" : "",
203 buf
[MCI_MR
/ 4] & MCI_MR_WRPROOF
? " WRPROOF" : "",
204 buf
[MCI_MR
/ 4] & 0xff);
205 seq_printf(s
, "DTOR:\t0x%08x\n", buf
[MCI_DTOR
/ 4]);
206 seq_printf(s
, "SDCR:\t0x%08x\n", buf
[MCI_SDCR
/ 4]);
207 seq_printf(s
, "ARGR:\t0x%08x\n", buf
[MCI_ARGR
/ 4]);
208 seq_printf(s
, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
210 buf
[MCI_BLKR
/ 4] & 0xffff,
211 (buf
[MCI_BLKR
/ 4] >> 16) & 0xffff);
213 /* Don't read RSPR and RDR; it will consume the data there */
215 atmci_show_status_reg(s
, "SR", buf
[MCI_SR
/ 4]);
216 atmci_show_status_reg(s
, "IMR", buf
[MCI_IMR
/ 4]);
221 static int atmci_regs_open(struct inode
*inode
, struct file
*file
)
223 return single_open(file
, atmci_regs_show
, inode
->i_private
);
226 static const struct file_operations atmci_regs_fops
= {
227 .owner
= THIS_MODULE
,
228 .open
= atmci_regs_open
,
231 .release
= single_release
,
234 static void atmci_init_debugfs(struct atmel_mci
*host
)
236 struct mmc_host
*mmc
;
239 struct resource
*res
;
242 root
= mmc
->debugfs_root
;
246 node
= debugfs_create_file("regs", S_IRUSR
, root
, host
,
253 res
= platform_get_resource(host
->pdev
, IORESOURCE_MEM
, 0);
254 node
->d_inode
->i_size
= res
->end
- res
->start
+ 1;
256 node
= debugfs_create_file("req", S_IRUSR
, root
, host
, &atmci_req_fops
);
260 node
= debugfs_create_x32("pending_events", S_IRUSR
, root
,
261 (u32
*)&host
->pending_events
);
265 node
= debugfs_create_x32("completed_events", S_IRUSR
, root
,
266 (u32
*)&host
->completed_events
);
273 dev_err(&host
->pdev
->dev
,
274 "failed to initialize debugfs for controller\n");
277 static void atmci_enable(struct atmel_mci
*host
)
279 clk_enable(host
->mck
);
280 mci_writel(host
, CR
, MCI_CR_MCIEN
);
281 mci_writel(host
, MR
, host
->mode_reg
);
282 mci_writel(host
, SDCR
, host
->sdc_reg
);
285 static void atmci_disable(struct atmel_mci
*host
)
287 mci_writel(host
, CR
, MCI_CR_SWRST
);
289 /* Stall until write is complete, then disable the bus clock */
291 clk_disable(host
->mck
);
294 static inline unsigned int ns_to_clocks(struct atmel_mci
*host
,
297 return (ns
* (host
->bus_hz
/ 1000000) + 999) / 1000;
300 static void atmci_set_timeout(struct atmel_mci
*host
,
301 struct mmc_data
*data
)
303 static unsigned dtomul_to_shift
[] = {
304 0, 4, 7, 8, 10, 12, 16, 20
310 timeout
= ns_to_clocks(host
, data
->timeout_ns
) + data
->timeout_clks
;
312 for (dtomul
= 0; dtomul
< 8; dtomul
++) {
313 unsigned shift
= dtomul_to_shift
[dtomul
];
314 dtocyc
= (timeout
+ (1 << shift
) - 1) >> shift
;
324 dev_vdbg(&host
->mmc
->class_dev
, "setting timeout to %u cycles\n",
325 dtocyc
<< dtomul_to_shift
[dtomul
]);
326 mci_writel(host
, DTOR
, (MCI_DTOMUL(dtomul
) | MCI_DTOCYC(dtocyc
)));
330 * Return mask with command flags to be enabled for this command.
332 static u32
atmci_prepare_command(struct mmc_host
*mmc
,
333 struct mmc_command
*cmd
)
335 struct mmc_data
*data
;
338 cmd
->error
= -EINPROGRESS
;
340 cmdr
= MCI_CMDR_CMDNB(cmd
->opcode
);
342 if (cmd
->flags
& MMC_RSP_PRESENT
) {
343 if (cmd
->flags
& MMC_RSP_136
)
344 cmdr
|= MCI_CMDR_RSPTYP_136BIT
;
346 cmdr
|= MCI_CMDR_RSPTYP_48BIT
;
350 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
351 * it's too difficult to determine whether this is an ACMD or
352 * not. Better make it 64.
354 cmdr
|= MCI_CMDR_MAXLAT_64CYC
;
356 if (mmc
->ios
.bus_mode
== MMC_BUSMODE_OPENDRAIN
)
357 cmdr
|= MCI_CMDR_OPDCMD
;
361 cmdr
|= MCI_CMDR_START_XFER
;
362 if (data
->flags
& MMC_DATA_STREAM
)
363 cmdr
|= MCI_CMDR_STREAM
;
364 else if (data
->blocks
> 1)
365 cmdr
|= MCI_CMDR_MULTI_BLOCK
;
367 cmdr
|= MCI_CMDR_BLOCK
;
369 if (data
->flags
& MMC_DATA_READ
)
370 cmdr
|= MCI_CMDR_TRDIR_READ
;
376 static void atmci_start_command(struct atmel_mci
*host
,
377 struct mmc_command
*cmd
,
380 /* Must read host->cmd after testing event flags */
385 dev_vdbg(&host
->mmc
->class_dev
,
386 "start command: ARGR=0x%08x CMDR=0x%08x\n",
387 cmd
->arg
, cmd_flags
);
389 mci_writel(host
, ARGR
, cmd
->arg
);
390 mci_writel(host
, CMDR
, cmd_flags
);
393 static void send_stop_cmd(struct mmc_host
*mmc
, struct mmc_data
*data
)
395 struct atmel_mci
*host
= mmc_priv(mmc
);
397 atmci_start_command(host
, data
->stop
, host
->stop_cmdr
);
398 mci_writel(host
, IER
, MCI_CMDRDY
);
401 static void atmci_request_end(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
403 struct atmel_mci
*host
= mmc_priv(mmc
);
405 WARN_ON(host
->cmd
|| host
->data
);
410 mmc_request_done(mmc
, mrq
);
414 * Returns a mask of interrupt flags to be enabled after the whole
415 * request has been prepared.
417 static u32
atmci_submit_data(struct mmc_host
*mmc
, struct mmc_data
*data
)
419 struct atmel_mci
*host
= mmc_priv(mmc
);
422 data
->error
= -EINPROGRESS
;
428 mci_writel(host
, BLKR
, MCI_BCNT(data
->blocks
)
429 | MCI_BLKLEN(data
->blksz
));
430 dev_vdbg(&mmc
->class_dev
, "BLKR=0x%08x\n",
431 MCI_BCNT(data
->blocks
) | MCI_BLKLEN(data
->blksz
));
433 iflags
= ATMCI_DATA_ERROR_FLAGS
;
435 host
->pio_offset
= 0;
436 if (data
->flags
& MMC_DATA_READ
)
444 static void atmci_request(struct mmc_host
*mmc
, struct mmc_request
*mrq
)
446 struct atmel_mci
*host
= mmc_priv(mmc
);
447 struct mmc_data
*data
;
448 struct mmc_command
*cmd
;
452 iflags
= mci_readl(host
, IMR
);
454 dev_warn(&mmc
->class_dev
, "WARNING: IMR=0x%08x\n",
455 mci_readl(host
, IMR
));
457 WARN_ON(host
->mrq
!= NULL
);
460 * We may "know" the card is gone even though there's still an
461 * electrical connection. If so, we really need to communicate
462 * this to the MMC core since there won't be any more
463 * interrupts as the card is completely removed. Otherwise,
464 * the MMC core might believe the card is still there even
465 * though the card was just removed very slowly.
467 if (!host
->present
) {
468 mrq
->cmd
->error
= -ENOMEDIUM
;
469 mmc_request_done(mmc
, mrq
);
474 host
->pending_events
= 0;
475 host
->completed_events
= 0;
479 /* We don't support multiple blocks of weird lengths. */
482 if (data
->blocks
> 1 && data
->blksz
& 3)
484 atmci_set_timeout(host
, data
);
489 cmdflags
= atmci_prepare_command(mmc
, cmd
);
490 atmci_start_command(host
, cmd
, cmdflags
);
493 iflags
|= atmci_submit_data(mmc
, data
);
496 host
->stop_cmdr
= atmci_prepare_command(mmc
, mrq
->stop
);
497 host
->stop_cmdr
|= MCI_CMDR_STOP_XFER
;
498 if (!(data
->flags
& MMC_DATA_WRITE
))
499 host
->stop_cmdr
|= MCI_CMDR_TRDIR_READ
;
500 if (data
->flags
& MMC_DATA_STREAM
)
501 host
->stop_cmdr
|= MCI_CMDR_STREAM
;
503 host
->stop_cmdr
|= MCI_CMDR_MULTI_BLOCK
;
507 * We could have enabled interrupts earlier, but I suspect
508 * that would open up a nice can of interesting race
509 * conditions (e.g. command and data complete, but stop not
512 mci_writel(host
, IER
, iflags
);
519 mrq
->cmd
->error
= -EINVAL
;
520 mmc_request_done(mmc
, mrq
);
523 static void atmci_set_ios(struct mmc_host
*mmc
, struct mmc_ios
*ios
)
525 struct atmel_mci
*host
= mmc_priv(mmc
);
531 clkdiv
= DIV_ROUND_UP(host
->bus_hz
, 2 * ios
->clock
) - 1;
533 dev_warn(&mmc
->class_dev
,
534 "clock %u too slow; using %lu\n",
535 ios
->clock
, host
->bus_hz
/ (2 * 256));
539 host
->mode_reg
= MCI_MR_CLKDIV(clkdiv
) | MCI_MR_WRPROOF
543 switch (ios
->bus_width
) {
544 case MMC_BUS_WIDTH_1
:
547 case MMC_BUS_WIDTH_4
:
548 host
->sdc_reg
= MCI_SDCBUS_4BIT
;
552 switch (ios
->power_mode
) {
554 /* Send init sequence (74 clock cycles) */
556 mci_writel(host
, CMDR
, MCI_CMDR_SPCMD_INIT
);
557 while (!(mci_readl(host
, SR
) & MCI_CMDRDY
))
563 * TODO: None of the currently available AVR32-based
564 * boards allow MMC power to be turned off. Implement
565 * power control when this can be tested properly.
571 static int atmci_get_ro(struct mmc_host
*mmc
)
574 struct atmel_mci
*host
= mmc_priv(mmc
);
576 if (host
->wp_pin
>= 0) {
577 read_only
= gpio_get_value(host
->wp_pin
);
578 dev_dbg(&mmc
->class_dev
, "card is %s\n",
579 read_only
? "read-only" : "read-write");
581 dev_dbg(&mmc
->class_dev
,
582 "no pin for checking read-only switch."
583 " Assuming write-enable.\n");
589 static struct mmc_host_ops atmci_ops
= {
590 .request
= atmci_request
,
591 .set_ios
= atmci_set_ios
,
592 .get_ro
= atmci_get_ro
,
595 static void atmci_command_complete(struct atmel_mci
*host
,
596 struct mmc_command
*cmd
, u32 status
)
598 /* Read the response from the card (up to 16 bytes) */
599 cmd
->resp
[0] = mci_readl(host
, RSPR
);
600 cmd
->resp
[1] = mci_readl(host
, RSPR
);
601 cmd
->resp
[2] = mci_readl(host
, RSPR
);
602 cmd
->resp
[3] = mci_readl(host
, RSPR
);
604 if (status
& MCI_RTOE
)
605 cmd
->error
= -ETIMEDOUT
;
606 else if ((cmd
->flags
& MMC_RSP_CRC
) && (status
& MCI_RCRCE
))
607 cmd
->error
= -EILSEQ
;
608 else if (status
& (MCI_RINDE
| MCI_RDIRE
| MCI_RENDE
))
614 dev_dbg(&host
->mmc
->class_dev
,
615 "command error: status=0x%08x\n", status
);
619 mci_writel(host
, IDR
, MCI_NOTBUSY
620 | MCI_TXRDY
| MCI_RXRDY
621 | ATMCI_DATA_ERROR_FLAGS
);
626 static void atmci_detect_change(unsigned long data
)
628 struct atmel_mci
*host
= (struct atmel_mci
*)data
;
629 struct mmc_request
*mrq
= host
->mrq
;
633 * atmci_remove() sets detect_pin to -1 before freeing the
634 * interrupt. We must not re-enable the interrupt if it has
638 if (host
->detect_pin
< 0)
641 enable_irq(gpio_to_irq(host
->detect_pin
));
642 present
= !gpio_get_value(host
->detect_pin
);
644 dev_vdbg(&host
->pdev
->dev
, "detect change: %d (was %d)\n",
645 present
, host
->present
);
647 if (present
!= host
->present
) {
648 dev_dbg(&host
->mmc
->class_dev
, "card %s\n",
649 present
? "inserted" : "removed");
650 host
->present
= present
;
652 /* Reset controller if card is gone */
654 mci_writel(host
, CR
, MCI_CR_SWRST
);
655 mci_writel(host
, IDR
, ~0UL);
656 mci_writel(host
, CR
, MCI_CR_MCIEN
);
659 /* Clean up queue if present */
662 * Reset controller to terminate any ongoing
663 * commands or data transfers.
665 mci_writel(host
, CR
, MCI_CR_SWRST
);
667 if (!atmci_is_completed(host
, EVENT_CMD_COMPLETE
))
668 mrq
->cmd
->error
= -ENOMEDIUM
;
670 if (mrq
->data
&& !atmci_is_completed(host
,
671 EVENT_DATA_COMPLETE
)) {
673 mrq
->data
->error
= -ENOMEDIUM
;
675 if (mrq
->stop
&& !atmci_is_completed(host
,
676 EVENT_STOP_COMPLETE
))
677 mrq
->stop
->error
= -ENOMEDIUM
;
680 atmci_request_end(host
->mmc
, mrq
);
683 mmc_detect_change(host
->mmc
, 0);
687 static void atmci_tasklet_func(unsigned long priv
)
689 struct mmc_host
*mmc
= (struct mmc_host
*)priv
;
690 struct atmel_mci
*host
= mmc_priv(mmc
);
691 struct mmc_request
*mrq
= host
->mrq
;
692 struct mmc_data
*data
= host
->data
;
694 dev_vdbg(&mmc
->class_dev
,
695 "tasklet: pending/completed/mask %lx/%lx/%x\n",
696 host
->pending_events
, host
->completed_events
,
697 mci_readl(host
, IMR
));
699 if (atmci_test_and_clear_pending(host
, EVENT_CMD_COMPLETE
)) {
701 * host->cmd must be set to NULL before the interrupt
702 * handler sees EVENT_CMD_COMPLETE
706 atmci_set_completed(host
, EVENT_CMD_COMPLETE
);
707 atmci_command_complete(host
, mrq
->cmd
, host
->cmd_status
);
709 if (!mrq
->cmd
->error
&& mrq
->stop
710 && atmci_is_completed(host
, EVENT_XFER_COMPLETE
)
711 && !atmci_test_and_set_completed(host
,
713 send_stop_cmd(host
->mmc
, mrq
->data
);
715 if (atmci_test_and_clear_pending(host
, EVENT_STOP_COMPLETE
)) {
717 * host->cmd must be set to NULL before the interrupt
718 * handler sees EVENT_STOP_COMPLETE
722 atmci_set_completed(host
, EVENT_STOP_COMPLETE
);
723 atmci_command_complete(host
, mrq
->stop
, host
->stop_status
);
725 if (atmci_test_and_clear_pending(host
, EVENT_DATA_ERROR
)) {
726 u32 status
= host
->data_status
;
728 dev_vdbg(&mmc
->class_dev
, "data error: status=%08x\n", status
);
730 atmci_set_completed(host
, EVENT_DATA_ERROR
);
731 atmci_set_completed(host
, EVENT_DATA_COMPLETE
);
733 if (status
& MCI_DTOE
) {
734 dev_dbg(&mmc
->class_dev
,
735 "data timeout error\n");
736 data
->error
= -ETIMEDOUT
;
737 } else if (status
& MCI_DCRCE
) {
738 dev_dbg(&mmc
->class_dev
, "data CRC error\n");
739 data
->error
= -EILSEQ
;
741 dev_dbg(&mmc
->class_dev
,
742 "data FIFO error (status=%08x)\n",
747 if (host
->present
&& data
->stop
748 && atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
749 && !atmci_test_and_set_completed(
750 host
, EVENT_STOP_SENT
))
751 send_stop_cmd(host
->mmc
, data
);
755 if (atmci_test_and_clear_pending(host
, EVENT_DATA_COMPLETE
)) {
756 atmci_set_completed(host
, EVENT_DATA_COMPLETE
);
758 if (!atmci_is_completed(host
, EVENT_DATA_ERROR
)) {
759 data
->bytes_xfered
= data
->blocks
* data
->blksz
;
766 if (host
->mrq
&& !host
->cmd
&& !host
->data
)
767 atmci_request_end(mmc
, host
->mrq
);
770 static void atmci_read_data_pio(struct atmel_mci
*host
)
772 struct scatterlist
*sg
= host
->sg
;
773 void *buf
= sg_virt(sg
);
774 unsigned int offset
= host
->pio_offset
;
775 struct mmc_data
*data
= host
->data
;
778 unsigned int nbytes
= 0;
781 value
= mci_readl(host
, RDR
);
782 if (likely(offset
+ 4 <= sg
->length
)) {
783 put_unaligned(value
, (u32
*)(buf
+ offset
));
788 if (offset
== sg
->length
) {
789 host
->sg
= sg
= sg_next(sg
);
797 unsigned int remaining
= sg
->length
- offset
;
798 memcpy(buf
+ offset
, &value
, remaining
);
801 flush_dcache_page(sg_page(sg
));
802 host
->sg
= sg
= sg_next(sg
);
806 offset
= 4 - remaining
;
808 memcpy(buf
, (u8
*)&value
+ remaining
, offset
);
812 status
= mci_readl(host
, SR
);
813 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
814 mci_writel(host
, IDR
, (MCI_NOTBUSY
| MCI_RXRDY
815 | ATMCI_DATA_ERROR_FLAGS
));
816 host
->data_status
= status
;
817 atmci_set_pending(host
, EVENT_DATA_ERROR
);
818 tasklet_schedule(&host
->tasklet
);
821 } while (status
& MCI_RXRDY
);
823 host
->pio_offset
= offset
;
824 data
->bytes_xfered
+= nbytes
;
829 mci_writel(host
, IDR
, MCI_RXRDY
);
830 mci_writel(host
, IER
, MCI_NOTBUSY
);
831 data
->bytes_xfered
+= nbytes
;
832 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
833 if (data
->stop
&& atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
834 && !atmci_test_and_set_completed(host
, EVENT_STOP_SENT
))
835 send_stop_cmd(host
->mmc
, data
);
838 static void atmci_write_data_pio(struct atmel_mci
*host
)
840 struct scatterlist
*sg
= host
->sg
;
841 void *buf
= sg_virt(sg
);
842 unsigned int offset
= host
->pio_offset
;
843 struct mmc_data
*data
= host
->data
;
846 unsigned int nbytes
= 0;
849 if (likely(offset
+ 4 <= sg
->length
)) {
850 value
= get_unaligned((u32
*)(buf
+ offset
));
851 mci_writel(host
, TDR
, value
);
855 if (offset
== sg
->length
) {
856 host
->sg
= sg
= sg_next(sg
);
864 unsigned int remaining
= sg
->length
- offset
;
867 memcpy(&value
, buf
+ offset
, remaining
);
870 host
->sg
= sg
= sg_next(sg
);
872 mci_writel(host
, TDR
, value
);
876 offset
= 4 - remaining
;
878 memcpy((u8
*)&value
+ remaining
, buf
, offset
);
879 mci_writel(host
, TDR
, value
);
883 status
= mci_readl(host
, SR
);
884 if (status
& ATMCI_DATA_ERROR_FLAGS
) {
885 mci_writel(host
, IDR
, (MCI_NOTBUSY
| MCI_TXRDY
886 | ATMCI_DATA_ERROR_FLAGS
));
887 host
->data_status
= status
;
888 atmci_set_pending(host
, EVENT_DATA_ERROR
);
889 tasklet_schedule(&host
->tasklet
);
892 } while (status
& MCI_TXRDY
);
894 host
->pio_offset
= offset
;
895 data
->bytes_xfered
+= nbytes
;
900 mci_writel(host
, IDR
, MCI_TXRDY
);
901 mci_writel(host
, IER
, MCI_NOTBUSY
);
902 data
->bytes_xfered
+= nbytes
;
903 atmci_set_completed(host
, EVENT_XFER_COMPLETE
);
904 if (data
->stop
&& atmci_is_completed(host
, EVENT_CMD_COMPLETE
)
905 && !atmci_test_and_set_completed(host
, EVENT_STOP_SENT
))
906 send_stop_cmd(host
->mmc
, data
);
909 static void atmci_cmd_interrupt(struct mmc_host
*mmc
, u32 status
)
911 struct atmel_mci
*host
= mmc_priv(mmc
);
913 mci_writel(host
, IDR
, MCI_CMDRDY
);
915 if (atmci_is_completed(host
, EVENT_STOP_SENT
)) {
916 host
->stop_status
= status
;
917 atmci_set_pending(host
, EVENT_STOP_COMPLETE
);
919 host
->cmd_status
= status
;
920 atmci_set_pending(host
, EVENT_CMD_COMPLETE
);
923 tasklet_schedule(&host
->tasklet
);
926 static irqreturn_t
atmci_interrupt(int irq
, void *dev_id
)
928 struct mmc_host
*mmc
= dev_id
;
929 struct atmel_mci
*host
= mmc_priv(mmc
);
930 u32 status
, mask
, pending
;
931 unsigned int pass_count
= 0;
933 spin_lock(&mmc
->lock
);
936 status
= mci_readl(host
, SR
);
937 mask
= mci_readl(host
, IMR
);
938 pending
= status
& mask
;
942 if (pending
& ATMCI_DATA_ERROR_FLAGS
) {
943 mci_writel(host
, IDR
, ATMCI_DATA_ERROR_FLAGS
944 | MCI_RXRDY
| MCI_TXRDY
);
945 pending
&= mci_readl(host
, IMR
);
946 host
->data_status
= status
;
947 atmci_set_pending(host
, EVENT_DATA_ERROR
);
948 tasklet_schedule(&host
->tasklet
);
950 if (pending
& MCI_NOTBUSY
) {
951 mci_writel(host
, IDR
, (MCI_NOTBUSY
952 | ATMCI_DATA_ERROR_FLAGS
));
953 atmci_set_pending(host
, EVENT_DATA_COMPLETE
);
954 tasklet_schedule(&host
->tasklet
);
956 if (pending
& MCI_RXRDY
)
957 atmci_read_data_pio(host
);
958 if (pending
& MCI_TXRDY
)
959 atmci_write_data_pio(host
);
961 if (pending
& MCI_CMDRDY
)
962 atmci_cmd_interrupt(mmc
, status
);
963 } while (pass_count
++ < 5);
965 spin_unlock(&mmc
->lock
);
967 return pass_count
? IRQ_HANDLED
: IRQ_NONE
;
970 static irqreturn_t
atmci_detect_interrupt(int irq
, void *dev_id
)
972 struct mmc_host
*mmc
= dev_id
;
973 struct atmel_mci
*host
= mmc_priv(mmc
);
976 * Disable interrupts until the pin has stabilized and check
977 * the state then. Use mod_timer() since we may be in the
978 * middle of the timer routine when this interrupt triggers.
980 disable_irq_nosync(irq
);
981 mod_timer(&host
->detect_timer
, jiffies
+ msecs_to_jiffies(20));
986 static int __init
atmci_probe(struct platform_device
*pdev
)
988 struct mci_platform_data
*pdata
;
989 struct atmel_mci
*host
;
990 struct mmc_host
*mmc
;
991 struct resource
*regs
;
995 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
998 pdata
= pdev
->dev
.platform_data
;
1001 irq
= platform_get_irq(pdev
, 0);
1005 mmc
= mmc_alloc_host(sizeof(struct atmel_mci
), &pdev
->dev
);
1009 host
= mmc_priv(mmc
);
1012 host
->detect_pin
= pdata
->detect_pin
;
1013 host
->wp_pin
= pdata
->wp_pin
;
1015 host
->mck
= clk_get(&pdev
->dev
, "mci_clk");
1016 if (IS_ERR(host
->mck
)) {
1017 ret
= PTR_ERR(host
->mck
);
1022 host
->regs
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
1026 clk_enable(host
->mck
);
1027 mci_writel(host
, CR
, MCI_CR_SWRST
);
1028 host
->bus_hz
= clk_get_rate(host
->mck
);
1029 clk_disable(host
->mck
);
1031 host
->mapbase
= regs
->start
;
1033 mmc
->ops
= &atmci_ops
;
1034 mmc
->f_min
= (host
->bus_hz
+ 511) / 512;
1035 mmc
->f_max
= host
->bus_hz
/ 2;
1036 mmc
->ocr_avail
= MMC_VDD_32_33
| MMC_VDD_33_34
;
1037 mmc
->caps
|= MMC_CAP_4_BIT_DATA
;
1039 mmc
->max_hw_segs
= 64;
1040 mmc
->max_phys_segs
= 64;
1041 mmc
->max_req_size
= 32768 * 512;
1042 mmc
->max_blk_size
= 32768;
1043 mmc
->max_blk_count
= 512;
1045 tasklet_init(&host
->tasklet
, atmci_tasklet_func
, (unsigned long)mmc
);
1047 ret
= request_irq(irq
, atmci_interrupt
, 0, pdev
->dev
.bus_id
, mmc
);
1049 goto err_request_irq
;
1051 /* Assume card is present if we don't have a detect pin */
1053 if (host
->detect_pin
>= 0) {
1054 if (gpio_request(host
->detect_pin
, "mmc_detect")) {
1055 dev_dbg(&mmc
->class_dev
, "no detect pin available\n");
1056 host
->detect_pin
= -1;
1058 host
->present
= !gpio_get_value(host
->detect_pin
);
1061 if (host
->wp_pin
>= 0) {
1062 if (gpio_request(host
->wp_pin
, "mmc_wp")) {
1063 dev_dbg(&mmc
->class_dev
, "no WP pin available\n");
1068 platform_set_drvdata(pdev
, host
);
1072 if (host
->detect_pin
>= 0) {
1073 setup_timer(&host
->detect_timer
, atmci_detect_change
,
1074 (unsigned long)host
);
1076 ret
= request_irq(gpio_to_irq(host
->detect_pin
),
1077 atmci_detect_interrupt
,
1078 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
1081 dev_dbg(&mmc
->class_dev
,
1082 "could not request IRQ %d for detect pin\n",
1083 gpio_to_irq(host
->detect_pin
));
1084 gpio_free(host
->detect_pin
);
1085 host
->detect_pin
= -1;
1089 dev_info(&mmc
->class_dev
,
1090 "Atmel MCI controller at 0x%08lx irq %d\n",
1091 host
->mapbase
, irq
);
1093 atmci_init_debugfs(host
);
1098 iounmap(host
->regs
);
1106 static int __exit
atmci_remove(struct platform_device
*pdev
)
1108 struct atmel_mci
*host
= platform_get_drvdata(pdev
);
1110 platform_set_drvdata(pdev
, NULL
);
1113 /* Debugfs stuff is cleaned up by mmc core */
1115 if (host
->detect_pin
>= 0) {
1116 int pin
= host
->detect_pin
;
1118 /* Make sure the timer doesn't enable the interrupt */
1119 host
->detect_pin
= -1;
1122 free_irq(gpio_to_irq(pin
), host
->mmc
);
1123 del_timer_sync(&host
->detect_timer
);
1127 mmc_remove_host(host
->mmc
);
1129 clk_enable(host
->mck
);
1130 mci_writel(host
, IDR
, ~0UL);
1131 mci_writel(host
, CR
, MCI_CR_MCIDIS
);
1132 mci_readl(host
, SR
);
1133 clk_disable(host
->mck
);
1135 if (host
->wp_pin
>= 0)
1136 gpio_free(host
->wp_pin
);
1138 free_irq(platform_get_irq(pdev
, 0), host
->mmc
);
1139 iounmap(host
->regs
);
1143 mmc_free_host(host
->mmc
);
1148 static struct platform_driver atmci_driver
= {
1149 .remove
= __exit_p(atmci_remove
),
1151 .name
= "atmel_mci",
1155 static int __init
atmci_init(void)
1157 return platform_driver_probe(&atmci_driver
, atmci_probe
);
1160 static void __exit
atmci_exit(void)
1162 platform_driver_unregister(&atmci_driver
);
1165 module_init(atmci_init
);
1166 module_exit(atmci_exit
);
1168 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1169 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1170 MODULE_LICENSE("GPL v2");