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]);
224 static int atmci_regs_open(struct inode *inode, struct file *file)
226 return single_open(file, atmci_regs_show, inode->i_private);
229 static const struct file_operations atmci_regs_fops = {
230 .owner = THIS_MODULE,
231 .open = atmci_regs_open,
234 .release = single_release,
237 static void atmci_init_debugfs(struct atmel_mci *host)
239 struct mmc_host *mmc;
242 struct resource *res;
245 root = mmc->debugfs_root;
249 node = debugfs_create_file("regs", S_IRUSR, root, host,
256 res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
257 node->d_inode->i_size = res->end - res->start + 1;
259 node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops);
263 node = debugfs_create_x32("pending_events", S_IRUSR, root,
264 (u32 *)&host->pending_events);
268 node = debugfs_create_x32("completed_events", S_IRUSR, root,
269 (u32 *)&host->completed_events);
276 dev_err(&host->pdev->dev,
277 "failed to initialize debugfs for controller\n");
280 static void atmci_enable(struct atmel_mci *host)
282 clk_enable(host->mck);
283 mci_writel(host, CR, MCI_CR_MCIEN);
284 mci_writel(host, MR, host->mode_reg);
285 mci_writel(host, SDCR, host->sdc_reg);
288 static void atmci_disable(struct atmel_mci *host)
290 mci_writel(host, CR, MCI_CR_SWRST);
292 /* Stall until write is complete, then disable the bus clock */
294 clk_disable(host->mck);
297 static inline unsigned int ns_to_clocks(struct atmel_mci *host,
300 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
303 static void atmci_set_timeout(struct atmel_mci *host,
304 struct mmc_data *data)
306 static unsigned dtomul_to_shift[] = {
307 0, 4, 7, 8, 10, 12, 16, 20
313 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
315 for (dtomul = 0; dtomul < 8; dtomul++) {
316 unsigned shift = dtomul_to_shift[dtomul];
317 dtocyc = (timeout + (1 << shift) - 1) >> shift;
327 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
328 dtocyc << dtomul_to_shift[dtomul]);
329 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
333 * Return mask with command flags to be enabled for this command.
335 static u32 atmci_prepare_command(struct mmc_host *mmc,
336 struct mmc_command *cmd)
338 struct mmc_data *data;
341 cmd->error = -EINPROGRESS;
343 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
345 if (cmd->flags & MMC_RSP_PRESENT) {
346 if (cmd->flags & MMC_RSP_136)
347 cmdr |= MCI_CMDR_RSPTYP_136BIT;
349 cmdr |= MCI_CMDR_RSPTYP_48BIT;
353 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
354 * it's too difficult to determine whether this is an ACMD or
355 * not. Better make it 64.
357 cmdr |= MCI_CMDR_MAXLAT_64CYC;
359 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
360 cmdr |= MCI_CMDR_OPDCMD;
364 cmdr |= MCI_CMDR_START_XFER;
365 if (data->flags & MMC_DATA_STREAM)
366 cmdr |= MCI_CMDR_STREAM;
367 else if (data->blocks > 1)
368 cmdr |= MCI_CMDR_MULTI_BLOCK;
370 cmdr |= MCI_CMDR_BLOCK;
372 if (data->flags & MMC_DATA_READ)
373 cmdr |= MCI_CMDR_TRDIR_READ;
379 static void atmci_start_command(struct atmel_mci *host,
380 struct mmc_command *cmd,
383 /* Must read host->cmd after testing event flags */
388 dev_vdbg(&host->mmc->class_dev,
389 "start command: ARGR=0x%08x CMDR=0x%08x\n",
390 cmd->arg, cmd_flags);
392 mci_writel(host, ARGR, cmd->arg);
393 mci_writel(host, CMDR, cmd_flags);
396 static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
398 struct atmel_mci *host = mmc_priv(mmc);
400 atmci_start_command(host, data->stop, host->stop_cmdr);
401 mci_writel(host, IER, MCI_CMDRDY);
404 static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
406 struct atmel_mci *host = mmc_priv(mmc);
408 WARN_ON(host->cmd || host->data);
413 mmc_request_done(mmc, mrq);
417 * Returns a mask of interrupt flags to be enabled after the whole
418 * request has been prepared.
420 static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
422 struct atmel_mci *host = mmc_priv(mmc);
425 data->error = -EINPROGRESS;
431 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
432 | MCI_BLKLEN(data->blksz));
433 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
434 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
436 iflags = ATMCI_DATA_ERROR_FLAGS;
438 host->pio_offset = 0;
439 if (data->flags & MMC_DATA_READ)
447 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
449 struct atmel_mci *host = mmc_priv(mmc);
450 struct mmc_data *data;
451 struct mmc_command *cmd;
455 iflags = mci_readl(host, IMR);
457 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
458 mci_readl(host, IMR));
460 WARN_ON(host->mrq != NULL);
463 * We may "know" the card is gone even though there's still an
464 * electrical connection. If so, we really need to communicate
465 * this to the MMC core since there won't be any more
466 * interrupts as the card is completely removed. Otherwise,
467 * the MMC core might believe the card is still there even
468 * though the card was just removed very slowly.
470 if (!host->present) {
471 mrq->cmd->error = -ENOMEDIUM;
472 mmc_request_done(mmc, mrq);
477 host->pending_events = 0;
478 host->completed_events = 0;
482 /* We don't support multiple blocks of weird lengths. */
485 if (data->blocks > 1 && data->blksz & 3)
487 atmci_set_timeout(host, data);
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);
1064 if (gpio_is_valid(host->wp_pin)) {
1065 if (gpio_request(host->wp_pin, "mmc_wp")) {
1066 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1071 platform_set_drvdata(pdev, host);
1075 if (gpio_is_valid(host->detect_pin)) {
1076 setup_timer(&host->detect_timer, atmci_detect_change,
1077 (unsigned long)host);
1079 ret = request_irq(gpio_to_irq(host->detect_pin),
1080 atmci_detect_interrupt,
1081 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1084 dev_dbg(&mmc->class_dev,
1085 "could not request IRQ %d for detect pin\n",
1086 gpio_to_irq(host->detect_pin));
1087 gpio_free(host->detect_pin);
1088 host->detect_pin = -1;
1092 dev_info(&mmc->class_dev,
1093 "Atmel MCI controller at 0x%08lx irq %d\n",
1094 host->mapbase, irq);
1096 atmci_init_debugfs(host);
1101 iounmap(host->regs);
1109 static int __exit atmci_remove(struct platform_device *pdev)
1111 struct atmel_mci *host = platform_get_drvdata(pdev);
1113 platform_set_drvdata(pdev, NULL);
1116 /* Debugfs stuff is cleaned up by mmc core */
1118 if (gpio_is_valid(host->detect_pin)) {
1119 int pin = host->detect_pin;
1121 /* Make sure the timer doesn't enable the interrupt */
1122 host->detect_pin = -1;
1125 free_irq(gpio_to_irq(pin), host->mmc);
1126 del_timer_sync(&host->detect_timer);
1130 mmc_remove_host(host->mmc);
1132 clk_enable(host->mck);
1133 mci_writel(host, IDR, ~0UL);
1134 mci_writel(host, CR, MCI_CR_MCIDIS);
1135 mci_readl(host, SR);
1136 clk_disable(host->mck);
1138 if (gpio_is_valid(host->wp_pin))
1139 gpio_free(host->wp_pin);
1141 free_irq(platform_get_irq(pdev, 0), host->mmc);
1142 iounmap(host->regs);
1146 mmc_free_host(host->mmc);
1151 static struct platform_driver atmci_driver = {
1152 .remove = __exit_p(atmci_remove),
1154 .name = "atmel_mci",
1158 static int __init atmci_init(void)
1160 return platform_driver_probe(&atmci_driver, atmci_probe);
1163 static void __exit atmci_exit(void)
1165 platform_driver_unregister(&atmci_driver);
1168 module_init(atmci_init);
1169 module_exit(atmci_exit);
1171 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1172 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1173 MODULE_LICENSE("GPL v2");