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 <asm/arch/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 memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
199 spin_unlock_irq(&host->mmc->lock);
201 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
203 buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
204 buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
205 buf[MCI_MR / 4] & 0xff);
206 seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
207 seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
208 seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
209 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
211 buf[MCI_BLKR / 4] & 0xffff,
212 (buf[MCI_BLKR / 4] >> 16) & 0xffff);
214 /* Don't read RSPR and RDR; it will consume the data there */
216 atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
217 atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
222 static int atmci_regs_open(struct inode *inode, struct file *file)
224 return single_open(file, atmci_regs_show, inode->i_private);
227 static const struct file_operations atmci_regs_fops = {
228 .owner = THIS_MODULE,
229 .open = atmci_regs_open,
232 .release = single_release,
235 static void atmci_init_debugfs(struct atmel_mci *host)
237 struct mmc_host *mmc;
240 struct resource *res;
243 root = mmc->debugfs_root;
247 node = debugfs_create_file("regs", S_IRUSR, root, host,
254 res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
255 node->d_inode->i_size = res->end - res->start + 1;
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 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
430 | MCI_BLKLEN(data->blksz));
431 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
432 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
434 iflags = ATMCI_DATA_ERROR_FLAGS;
436 host->pio_offset = 0;
437 if (data->flags & MMC_DATA_READ)
445 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
447 struct atmel_mci *host = mmc_priv(mmc);
448 struct mmc_data *data;
449 struct mmc_command *cmd;
453 iflags = mci_readl(host, IMR);
455 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
456 mci_readl(host, IMR));
458 WARN_ON(host->mrq != NULL);
461 * We may "know" the card is gone even though there's still an
462 * electrical connection. If so, we really need to communicate
463 * this to the MMC core since there won't be any more
464 * interrupts as the card is completely removed. Otherwise,
465 * the MMC core might believe the card is still there even
466 * though the card was just removed very slowly.
468 if (!host->present) {
469 mrq->cmd->error = -ENOMEDIUM;
470 mmc_request_done(mmc, mrq);
475 host->pending_events = 0;
476 host->completed_events = 0;
480 /* We don't support multiple blocks of weird lengths. */
483 if (data->blocks > 1 && data->blksz & 3)
485 atmci_set_timeout(host, data);
490 cmdflags = atmci_prepare_command(mmc, cmd);
491 atmci_start_command(host, cmd, cmdflags);
494 iflags |= atmci_submit_data(mmc, data);
497 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
498 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
499 if (!(data->flags & MMC_DATA_WRITE))
500 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
501 if (data->flags & MMC_DATA_STREAM)
502 host->stop_cmdr |= MCI_CMDR_STREAM;
504 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
508 * We could have enabled interrupts earlier, but I suspect
509 * that would open up a nice can of interesting race
510 * conditions (e.g. command and data complete, but stop not
513 mci_writel(host, IER, iflags);
520 mrq->cmd->error = -EINVAL;
521 mmc_request_done(mmc, mrq);
524 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
526 struct atmel_mci *host = mmc_priv(mmc);
532 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
534 dev_warn(&mmc->class_dev,
535 "clock %u too slow; using %lu\n",
536 ios->clock, host->bus_hz / (2 * 256));
540 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
544 switch (ios->bus_width) {
545 case MMC_BUS_WIDTH_1:
548 case MMC_BUS_WIDTH_4:
549 host->sdc_reg = MCI_SDCBUS_4BIT;
553 switch (ios->power_mode) {
555 /* Send init sequence (74 clock cycles) */
557 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
558 while (!(mci_readl(host, SR) & MCI_CMDRDY))
564 * TODO: None of the currently available AVR32-based
565 * boards allow MMC power to be turned off. Implement
566 * power control when this can be tested properly.
572 static int atmci_get_ro(struct mmc_host *mmc)
575 struct atmel_mci *host = mmc_priv(mmc);
577 if (gpio_is_valid(host->wp_pin)) {
578 read_only = gpio_get_value(host->wp_pin);
579 dev_dbg(&mmc->class_dev, "card is %s\n",
580 read_only ? "read-only" : "read-write");
582 dev_dbg(&mmc->class_dev,
583 "no pin for checking read-only switch."
584 " Assuming write-enable.\n");
590 static struct mmc_host_ops atmci_ops = {
591 .request = atmci_request,
592 .set_ios = atmci_set_ios,
593 .get_ro = atmci_get_ro,
596 static void atmci_command_complete(struct atmel_mci *host,
597 struct mmc_command *cmd, u32 status)
599 /* Read the response from the card (up to 16 bytes) */
600 cmd->resp[0] = mci_readl(host, RSPR);
601 cmd->resp[1] = mci_readl(host, RSPR);
602 cmd->resp[2] = mci_readl(host, RSPR);
603 cmd->resp[3] = mci_readl(host, RSPR);
605 if (status & MCI_RTOE)
606 cmd->error = -ETIMEDOUT;
607 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
608 cmd->error = -EILSEQ;
609 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
615 dev_dbg(&host->mmc->class_dev,
616 "command error: status=0x%08x\n", status);
620 mci_writel(host, IDR, MCI_NOTBUSY
621 | MCI_TXRDY | MCI_RXRDY
622 | ATMCI_DATA_ERROR_FLAGS);
627 static void atmci_detect_change(unsigned long data)
629 struct atmel_mci *host = (struct atmel_mci *)data;
630 struct mmc_request *mrq = host->mrq;
634 * atmci_remove() sets detect_pin to -1 before freeing the
635 * interrupt. We must not re-enable the interrupt if it has
639 if (!gpio_is_valid(host->detect_pin))
642 enable_irq(gpio_to_irq(host->detect_pin));
643 present = !gpio_get_value(host->detect_pin);
645 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
646 present, host->present);
648 if (present != host->present) {
649 dev_dbg(&host->mmc->class_dev, "card %s\n",
650 present ? "inserted" : "removed");
651 host->present = present;
653 /* Reset controller if card is gone */
655 mci_writel(host, CR, MCI_CR_SWRST);
656 mci_writel(host, IDR, ~0UL);
657 mci_writel(host, CR, MCI_CR_MCIEN);
660 /* Clean up queue if present */
663 * Reset controller to terminate any ongoing
664 * commands or data transfers.
666 mci_writel(host, CR, MCI_CR_SWRST);
668 if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
669 mrq->cmd->error = -ENOMEDIUM;
671 if (mrq->data && !atmci_is_completed(host,
672 EVENT_DATA_COMPLETE)) {
674 mrq->data->error = -ENOMEDIUM;
676 if (mrq->stop && !atmci_is_completed(host,
677 EVENT_STOP_COMPLETE))
678 mrq->stop->error = -ENOMEDIUM;
681 atmci_request_end(host->mmc, mrq);
684 mmc_detect_change(host->mmc, 0);
688 static void atmci_tasklet_func(unsigned long priv)
690 struct mmc_host *mmc = (struct mmc_host *)priv;
691 struct atmel_mci *host = mmc_priv(mmc);
692 struct mmc_request *mrq = host->mrq;
693 struct mmc_data *data = host->data;
695 dev_vdbg(&mmc->class_dev,
696 "tasklet: pending/completed/mask %lx/%lx/%x\n",
697 host->pending_events, host->completed_events,
698 mci_readl(host, IMR));
700 if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
702 * host->cmd must be set to NULL before the interrupt
703 * handler sees EVENT_CMD_COMPLETE
707 atmci_set_completed(host, EVENT_CMD_COMPLETE);
708 atmci_command_complete(host, mrq->cmd, host->cmd_status);
710 if (!mrq->cmd->error && mrq->stop
711 && atmci_is_completed(host, EVENT_XFER_COMPLETE)
712 && !atmci_test_and_set_completed(host,
714 send_stop_cmd(host->mmc, mrq->data);
716 if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
718 * host->cmd must be set to NULL before the interrupt
719 * handler sees EVENT_STOP_COMPLETE
723 atmci_set_completed(host, EVENT_STOP_COMPLETE);
724 atmci_command_complete(host, mrq->stop, host->stop_status);
726 if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
727 u32 status = host->data_status;
729 dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
731 atmci_set_completed(host, EVENT_DATA_ERROR);
732 atmci_set_completed(host, EVENT_DATA_COMPLETE);
734 if (status & MCI_DTOE) {
735 dev_dbg(&mmc->class_dev,
736 "data timeout error\n");
737 data->error = -ETIMEDOUT;
738 } else if (status & MCI_DCRCE) {
739 dev_dbg(&mmc->class_dev, "data CRC error\n");
740 data->error = -EILSEQ;
742 dev_dbg(&mmc->class_dev,
743 "data FIFO error (status=%08x)\n",
748 if (host->present && data->stop
749 && atmci_is_completed(host, EVENT_CMD_COMPLETE)
750 && !atmci_test_and_set_completed(
751 host, EVENT_STOP_SENT))
752 send_stop_cmd(host->mmc, data);
756 if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
757 atmci_set_completed(host, EVENT_DATA_COMPLETE);
759 if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
760 data->bytes_xfered = data->blocks * data->blksz;
767 if (host->mrq && !host->cmd && !host->data)
768 atmci_request_end(mmc, host->mrq);
771 static void atmci_read_data_pio(struct atmel_mci *host)
773 struct scatterlist *sg = host->sg;
774 void *buf = sg_virt(sg);
775 unsigned int offset = host->pio_offset;
776 struct mmc_data *data = host->data;
779 unsigned int nbytes = 0;
782 value = mci_readl(host, RDR);
783 if (likely(offset + 4 <= sg->length)) {
784 put_unaligned(value, (u32 *)(buf + offset));
789 if (offset == sg->length) {
790 host->sg = sg = sg_next(sg);
798 unsigned int remaining = sg->length - offset;
799 memcpy(buf + offset, &value, remaining);
802 flush_dcache_page(sg_page(sg));
803 host->sg = sg = sg_next(sg);
807 offset = 4 - remaining;
809 memcpy(buf, (u8 *)&value + remaining, offset);
813 status = mci_readl(host, SR);
814 if (status & ATMCI_DATA_ERROR_FLAGS) {
815 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
816 | ATMCI_DATA_ERROR_FLAGS));
817 host->data_status = status;
818 atmci_set_pending(host, EVENT_DATA_ERROR);
819 tasklet_schedule(&host->tasklet);
822 } while (status & MCI_RXRDY);
824 host->pio_offset = offset;
825 data->bytes_xfered += nbytes;
830 mci_writel(host, IDR, MCI_RXRDY);
831 mci_writel(host, IER, MCI_NOTBUSY);
832 data->bytes_xfered += nbytes;
833 atmci_set_completed(host, EVENT_XFER_COMPLETE);
834 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
835 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
836 send_stop_cmd(host->mmc, data);
839 static void atmci_write_data_pio(struct atmel_mci *host)
841 struct scatterlist *sg = host->sg;
842 void *buf = sg_virt(sg);
843 unsigned int offset = host->pio_offset;
844 struct mmc_data *data = host->data;
847 unsigned int nbytes = 0;
850 if (likely(offset + 4 <= sg->length)) {
851 value = get_unaligned((u32 *)(buf + offset));
852 mci_writel(host, TDR, value);
856 if (offset == sg->length) {
857 host->sg = sg = sg_next(sg);
865 unsigned int remaining = sg->length - offset;
868 memcpy(&value, buf + offset, remaining);
871 host->sg = sg = sg_next(sg);
873 mci_writel(host, TDR, value);
877 offset = 4 - remaining;
879 memcpy((u8 *)&value + remaining, buf, offset);
880 mci_writel(host, TDR, value);
884 status = mci_readl(host, SR);
885 if (status & ATMCI_DATA_ERROR_FLAGS) {
886 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
887 | ATMCI_DATA_ERROR_FLAGS));
888 host->data_status = status;
889 atmci_set_pending(host, EVENT_DATA_ERROR);
890 tasklet_schedule(&host->tasklet);
893 } while (status & MCI_TXRDY);
895 host->pio_offset = offset;
896 data->bytes_xfered += nbytes;
901 mci_writel(host, IDR, MCI_TXRDY);
902 mci_writel(host, IER, MCI_NOTBUSY);
903 data->bytes_xfered += nbytes;
904 atmci_set_completed(host, EVENT_XFER_COMPLETE);
905 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
906 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
907 send_stop_cmd(host->mmc, data);
910 static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
912 struct atmel_mci *host = mmc_priv(mmc);
914 mci_writel(host, IDR, MCI_CMDRDY);
916 if (atmci_is_completed(host, EVENT_STOP_SENT)) {
917 host->stop_status = status;
918 atmci_set_pending(host, EVENT_STOP_COMPLETE);
920 host->cmd_status = status;
921 atmci_set_pending(host, EVENT_CMD_COMPLETE);
924 tasklet_schedule(&host->tasklet);
927 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
929 struct mmc_host *mmc = dev_id;
930 struct atmel_mci *host = mmc_priv(mmc);
931 u32 status, mask, pending;
932 unsigned int pass_count = 0;
934 spin_lock(&mmc->lock);
937 status = mci_readl(host, SR);
938 mask = mci_readl(host, IMR);
939 pending = status & mask;
943 if (pending & ATMCI_DATA_ERROR_FLAGS) {
944 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
945 | MCI_RXRDY | MCI_TXRDY);
946 pending &= mci_readl(host, IMR);
947 host->data_status = status;
948 atmci_set_pending(host, EVENT_DATA_ERROR);
949 tasklet_schedule(&host->tasklet);
951 if (pending & MCI_NOTBUSY) {
952 mci_writel(host, IDR, (MCI_NOTBUSY
953 | ATMCI_DATA_ERROR_FLAGS));
954 atmci_set_pending(host, EVENT_DATA_COMPLETE);
955 tasklet_schedule(&host->tasklet);
957 if (pending & MCI_RXRDY)
958 atmci_read_data_pio(host);
959 if (pending & MCI_TXRDY)
960 atmci_write_data_pio(host);
962 if (pending & MCI_CMDRDY)
963 atmci_cmd_interrupt(mmc, status);
964 } while (pass_count++ < 5);
966 spin_unlock(&mmc->lock);
968 return pass_count ? IRQ_HANDLED : IRQ_NONE;
971 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
973 struct mmc_host *mmc = dev_id;
974 struct atmel_mci *host = mmc_priv(mmc);
977 * Disable interrupts until the pin has stabilized and check
978 * the state then. Use mod_timer() since we may be in the
979 * middle of the timer routine when this interrupt triggers.
981 disable_irq_nosync(irq);
982 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
987 static int __init atmci_probe(struct platform_device *pdev)
989 struct mci_platform_data *pdata;
990 struct atmel_mci *host;
991 struct mmc_host *mmc;
992 struct resource *regs;
996 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
999 pdata = pdev->dev.platform_data;
1002 irq = platform_get_irq(pdev, 0);
1006 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1010 host = mmc_priv(mmc);
1013 host->detect_pin = pdata->detect_pin;
1014 host->wp_pin = pdata->wp_pin;
1016 host->mck = clk_get(&pdev->dev, "mci_clk");
1017 if (IS_ERR(host->mck)) {
1018 ret = PTR_ERR(host->mck);
1023 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1027 clk_enable(host->mck);
1028 mci_writel(host, CR, MCI_CR_SWRST);
1029 host->bus_hz = clk_get_rate(host->mck);
1030 clk_disable(host->mck);
1032 host->mapbase = regs->start;
1034 mmc->ops = &atmci_ops;
1035 mmc->f_min = (host->bus_hz + 511) / 512;
1036 mmc->f_max = host->bus_hz / 2;
1037 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1038 mmc->caps |= MMC_CAP_4_BIT_DATA;
1040 mmc->max_hw_segs = 64;
1041 mmc->max_phys_segs = 64;
1042 mmc->max_req_size = 32768 * 512;
1043 mmc->max_blk_size = 32768;
1044 mmc->max_blk_count = 512;
1046 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1048 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
1050 goto err_request_irq;
1052 /* Assume card is present if we don't have a detect pin */
1054 if (gpio_is_valid(host->detect_pin)) {
1055 if (gpio_request(host->detect_pin, "mmc_detect")) {
1056 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1057 host->detect_pin = -1;
1059 host->present = !gpio_get_value(host->detect_pin);
1062 if (gpio_is_valid(host->wp_pin)) {
1063 if (gpio_request(host->wp_pin, "mmc_wp")) {
1064 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1069 platform_set_drvdata(pdev, host);
1073 if (gpio_is_valid(host->detect_pin)) {
1074 setup_timer(&host->detect_timer, atmci_detect_change,
1075 (unsigned long)host);
1077 ret = request_irq(gpio_to_irq(host->detect_pin),
1078 atmci_detect_interrupt,
1079 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1082 dev_dbg(&mmc->class_dev,
1083 "could not request IRQ %d for detect pin\n",
1084 gpio_to_irq(host->detect_pin));
1085 gpio_free(host->detect_pin);
1086 host->detect_pin = -1;
1090 dev_info(&mmc->class_dev,
1091 "Atmel MCI controller at 0x%08lx irq %d\n",
1092 host->mapbase, irq);
1094 atmci_init_debugfs(host);
1099 iounmap(host->regs);
1107 static int __exit atmci_remove(struct platform_device *pdev)
1109 struct atmel_mci *host = platform_get_drvdata(pdev);
1111 platform_set_drvdata(pdev, NULL);
1114 /* Debugfs stuff is cleaned up by mmc core */
1116 if (gpio_is_valid(host->detect_pin)) {
1117 int pin = host->detect_pin;
1119 /* Make sure the timer doesn't enable the interrupt */
1120 host->detect_pin = -1;
1123 free_irq(gpio_to_irq(pin), host->mmc);
1124 del_timer_sync(&host->detect_timer);
1128 mmc_remove_host(host->mmc);
1130 clk_enable(host->mck);
1131 mci_writel(host, IDR, ~0UL);
1132 mci_writel(host, CR, MCI_CR_MCIDIS);
1133 mci_readl(host, SR);
1134 clk_disable(host->mck);
1136 if (gpio_is_valid(host->wp_pin))
1137 gpio_free(host->wp_pin);
1139 free_irq(platform_get_irq(pdev, 0), host->mmc);
1140 iounmap(host->regs);
1144 mmc_free_host(host->mmc);
1149 static struct platform_driver atmci_driver = {
1150 .remove = __exit_p(atmci_remove),
1152 .name = "atmel_mci",
1156 static int __init atmci_init(void)
1158 return platform_driver_probe(&atmci_driver, atmci_probe);
1161 static void __exit atmci_exit(void)
1163 platform_driver_unregister(&atmci_driver);
1166 module_init(atmci_init);
1167 module_exit(atmci_exit);
1169 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1170 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1171 MODULE_LICENSE("GPL v2");