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;
244 struct resource *res;
247 root = mmc->debugfs_root;
251 node = debugfs_create_file("regs", S_IRUSR, root, host,
258 res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
259 node->d_inode->i_size = res->end - res->start + 1;
261 node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops);
265 node = debugfs_create_x32("pending_events", S_IRUSR, root,
266 (u32 *)&host->pending_events);
270 node = debugfs_create_x32("completed_events", S_IRUSR, root,
271 (u32 *)&host->completed_events);
278 dev_err(&host->pdev->dev,
279 "failed to initialize debugfs for controller\n");
282 static void atmci_enable(struct atmel_mci *host)
284 clk_enable(host->mck);
285 mci_writel(host, CR, MCI_CR_MCIEN);
286 mci_writel(host, MR, host->mode_reg);
287 mci_writel(host, SDCR, host->sdc_reg);
290 static void atmci_disable(struct atmel_mci *host)
292 mci_writel(host, CR, MCI_CR_SWRST);
294 /* Stall until write is complete, then disable the bus clock */
296 clk_disable(host->mck);
299 static inline unsigned int ns_to_clocks(struct atmel_mci *host,
302 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
305 static void atmci_set_timeout(struct atmel_mci *host,
306 struct mmc_data *data)
308 static unsigned dtomul_to_shift[] = {
309 0, 4, 7, 8, 10, 12, 16, 20
315 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
317 for (dtomul = 0; dtomul < 8; dtomul++) {
318 unsigned shift = dtomul_to_shift[dtomul];
319 dtocyc = (timeout + (1 << shift) - 1) >> shift;
329 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
330 dtocyc << dtomul_to_shift[dtomul]);
331 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
335 * Return mask with command flags to be enabled for this command.
337 static u32 atmci_prepare_command(struct mmc_host *mmc,
338 struct mmc_command *cmd)
340 struct mmc_data *data;
343 cmd->error = -EINPROGRESS;
345 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
347 if (cmd->flags & MMC_RSP_PRESENT) {
348 if (cmd->flags & MMC_RSP_136)
349 cmdr |= MCI_CMDR_RSPTYP_136BIT;
351 cmdr |= MCI_CMDR_RSPTYP_48BIT;
355 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
356 * it's too difficult to determine whether this is an ACMD or
357 * not. Better make it 64.
359 cmdr |= MCI_CMDR_MAXLAT_64CYC;
361 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
362 cmdr |= MCI_CMDR_OPDCMD;
366 cmdr |= MCI_CMDR_START_XFER;
367 if (data->flags & MMC_DATA_STREAM)
368 cmdr |= MCI_CMDR_STREAM;
369 else if (data->blocks > 1)
370 cmdr |= MCI_CMDR_MULTI_BLOCK;
372 cmdr |= MCI_CMDR_BLOCK;
374 if (data->flags & MMC_DATA_READ)
375 cmdr |= MCI_CMDR_TRDIR_READ;
381 static void atmci_start_command(struct atmel_mci *host,
382 struct mmc_command *cmd,
385 /* Must read host->cmd after testing event flags */
390 dev_vdbg(&host->mmc->class_dev,
391 "start command: ARGR=0x%08x CMDR=0x%08x\n",
392 cmd->arg, cmd_flags);
394 mci_writel(host, ARGR, cmd->arg);
395 mci_writel(host, CMDR, cmd_flags);
398 static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
400 struct atmel_mci *host = mmc_priv(mmc);
402 atmci_start_command(host, data->stop, host->stop_cmdr);
403 mci_writel(host, IER, MCI_CMDRDY);
406 static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
408 struct atmel_mci *host = mmc_priv(mmc);
410 WARN_ON(host->cmd || host->data);
415 mmc_request_done(mmc, mrq);
419 * Returns a mask of interrupt flags to be enabled after the whole
420 * request has been prepared.
422 static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
424 struct atmel_mci *host = mmc_priv(mmc);
427 data->error = -EINPROGRESS;
433 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
434 | MCI_BLKLEN(data->blksz));
435 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
436 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
438 iflags = ATMCI_DATA_ERROR_FLAGS;
440 host->pio_offset = 0;
441 if (data->flags & MMC_DATA_READ)
449 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
451 struct atmel_mci *host = mmc_priv(mmc);
452 struct mmc_data *data;
453 struct mmc_command *cmd;
457 iflags = mci_readl(host, IMR);
459 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
460 mci_readl(host, IMR));
462 WARN_ON(host->mrq != NULL);
465 * We may "know" the card is gone even though there's still an
466 * electrical connection. If so, we really need to communicate
467 * this to the MMC core since there won't be any more
468 * interrupts as the card is completely removed. Otherwise,
469 * the MMC core might believe the card is still there even
470 * though the card was just removed very slowly.
472 if (!host->present) {
473 mrq->cmd->error = -ENOMEDIUM;
474 mmc_request_done(mmc, mrq);
479 host->pending_events = 0;
480 host->completed_events = 0;
484 /* We don't support multiple blocks of weird lengths. */
487 if (data->blocks > 1 && data->blksz & 3)
489 atmci_set_timeout(host, data);
494 cmdflags = atmci_prepare_command(mmc, cmd);
495 atmci_start_command(host, cmd, cmdflags);
498 iflags |= atmci_submit_data(mmc, data);
501 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
502 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
503 if (!(data->flags & MMC_DATA_WRITE))
504 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
505 if (data->flags & MMC_DATA_STREAM)
506 host->stop_cmdr |= MCI_CMDR_STREAM;
508 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
512 * We could have enabled interrupts earlier, but I suspect
513 * that would open up a nice can of interesting race
514 * conditions (e.g. command and data complete, but stop not
517 mci_writel(host, IER, iflags);
524 mrq->cmd->error = -EINVAL;
525 mmc_request_done(mmc, mrq);
528 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
530 struct atmel_mci *host = mmc_priv(mmc);
536 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
538 dev_warn(&mmc->class_dev,
539 "clock %u too slow; using %lu\n",
540 ios->clock, host->bus_hz / (2 * 256));
544 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
548 switch (ios->bus_width) {
549 case MMC_BUS_WIDTH_1:
552 case MMC_BUS_WIDTH_4:
553 host->sdc_reg = MCI_SDCBUS_4BIT;
557 switch (ios->power_mode) {
559 /* Send init sequence (74 clock cycles) */
561 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
562 while (!(mci_readl(host, SR) & MCI_CMDRDY))
568 * TODO: None of the currently available AVR32-based
569 * boards allow MMC power to be turned off. Implement
570 * power control when this can be tested properly.
576 static int atmci_get_ro(struct mmc_host *mmc)
579 struct atmel_mci *host = mmc_priv(mmc);
581 if (gpio_is_valid(host->wp_pin)) {
582 read_only = gpio_get_value(host->wp_pin);
583 dev_dbg(&mmc->class_dev, "card is %s\n",
584 read_only ? "read-only" : "read-write");
586 dev_dbg(&mmc->class_dev,
587 "no pin for checking read-only switch."
588 " Assuming write-enable.\n");
594 static struct mmc_host_ops atmci_ops = {
595 .request = atmci_request,
596 .set_ios = atmci_set_ios,
597 .get_ro = atmci_get_ro,
600 static void atmci_command_complete(struct atmel_mci *host,
601 struct mmc_command *cmd, u32 status)
603 /* Read the response from the card (up to 16 bytes) */
604 cmd->resp[0] = mci_readl(host, RSPR);
605 cmd->resp[1] = mci_readl(host, RSPR);
606 cmd->resp[2] = mci_readl(host, RSPR);
607 cmd->resp[3] = mci_readl(host, RSPR);
609 if (status & MCI_RTOE)
610 cmd->error = -ETIMEDOUT;
611 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
612 cmd->error = -EILSEQ;
613 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
619 dev_dbg(&host->mmc->class_dev,
620 "command error: status=0x%08x\n", status);
624 mci_writel(host, IDR, MCI_NOTBUSY
625 | MCI_TXRDY | MCI_RXRDY
626 | ATMCI_DATA_ERROR_FLAGS);
631 static void atmci_detect_change(unsigned long data)
633 struct atmel_mci *host = (struct atmel_mci *)data;
634 struct mmc_request *mrq = host->mrq;
638 * atmci_remove() sets detect_pin to -1 before freeing the
639 * interrupt. We must not re-enable the interrupt if it has
643 if (!gpio_is_valid(host->detect_pin))
646 enable_irq(gpio_to_irq(host->detect_pin));
647 present = !gpio_get_value(host->detect_pin);
649 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
650 present, host->present);
652 if (present != host->present) {
653 dev_dbg(&host->mmc->class_dev, "card %s\n",
654 present ? "inserted" : "removed");
655 host->present = present;
657 /* Reset controller if card is gone */
659 mci_writel(host, CR, MCI_CR_SWRST);
660 mci_writel(host, IDR, ~0UL);
661 mci_writel(host, CR, MCI_CR_MCIEN);
664 /* Clean up queue if present */
667 * Reset controller to terminate any ongoing
668 * commands or data transfers.
670 mci_writel(host, CR, MCI_CR_SWRST);
672 if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
673 mrq->cmd->error = -ENOMEDIUM;
675 if (mrq->data && !atmci_is_completed(host,
676 EVENT_DATA_COMPLETE)) {
678 mrq->data->error = -ENOMEDIUM;
680 if (mrq->stop && !atmci_is_completed(host,
681 EVENT_STOP_COMPLETE))
682 mrq->stop->error = -ENOMEDIUM;
685 atmci_request_end(host->mmc, mrq);
688 mmc_detect_change(host->mmc, 0);
692 static void atmci_tasklet_func(unsigned long priv)
694 struct mmc_host *mmc = (struct mmc_host *)priv;
695 struct atmel_mci *host = mmc_priv(mmc);
696 struct mmc_request *mrq = host->mrq;
697 struct mmc_data *data = host->data;
699 dev_vdbg(&mmc->class_dev,
700 "tasklet: pending/completed/mask %lx/%lx/%x\n",
701 host->pending_events, host->completed_events,
702 mci_readl(host, IMR));
704 if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
706 * host->cmd must be set to NULL before the interrupt
707 * handler sees EVENT_CMD_COMPLETE
711 atmci_set_completed(host, EVENT_CMD_COMPLETE);
712 atmci_command_complete(host, mrq->cmd, host->cmd_status);
714 if (!mrq->cmd->error && mrq->stop
715 && atmci_is_completed(host, EVENT_XFER_COMPLETE)
716 && !atmci_test_and_set_completed(host,
718 send_stop_cmd(host->mmc, mrq->data);
720 if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
722 * host->cmd must be set to NULL before the interrupt
723 * handler sees EVENT_STOP_COMPLETE
727 atmci_set_completed(host, EVENT_STOP_COMPLETE);
728 atmci_command_complete(host, mrq->stop, host->stop_status);
730 if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
731 u32 status = host->data_status;
733 dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
735 atmci_set_completed(host, EVENT_DATA_ERROR);
736 atmci_set_completed(host, EVENT_DATA_COMPLETE);
738 if (status & MCI_DTOE) {
739 dev_dbg(&mmc->class_dev,
740 "data timeout error\n");
741 data->error = -ETIMEDOUT;
742 } else if (status & MCI_DCRCE) {
743 dev_dbg(&mmc->class_dev, "data CRC error\n");
744 data->error = -EILSEQ;
746 dev_dbg(&mmc->class_dev,
747 "data FIFO error (status=%08x)\n",
752 if (host->present && data->stop
753 && atmci_is_completed(host, EVENT_CMD_COMPLETE)
754 && !atmci_test_and_set_completed(
755 host, EVENT_STOP_SENT))
756 send_stop_cmd(host->mmc, data);
760 if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
761 atmci_set_completed(host, EVENT_DATA_COMPLETE);
763 if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
764 data->bytes_xfered = data->blocks * data->blksz;
771 if (host->mrq && !host->cmd && !host->data)
772 atmci_request_end(mmc, host->mrq);
775 static void atmci_read_data_pio(struct atmel_mci *host)
777 struct scatterlist *sg = host->sg;
778 void *buf = sg_virt(sg);
779 unsigned int offset = host->pio_offset;
780 struct mmc_data *data = host->data;
783 unsigned int nbytes = 0;
786 value = mci_readl(host, RDR);
787 if (likely(offset + 4 <= sg->length)) {
788 put_unaligned(value, (u32 *)(buf + offset));
793 if (offset == sg->length) {
794 host->sg = sg = sg_next(sg);
802 unsigned int remaining = sg->length - offset;
803 memcpy(buf + offset, &value, remaining);
806 flush_dcache_page(sg_page(sg));
807 host->sg = sg = sg_next(sg);
811 offset = 4 - remaining;
813 memcpy(buf, (u8 *)&value + remaining, offset);
817 status = mci_readl(host, SR);
818 if (status & ATMCI_DATA_ERROR_FLAGS) {
819 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
820 | ATMCI_DATA_ERROR_FLAGS));
821 host->data_status = status;
822 atmci_set_pending(host, EVENT_DATA_ERROR);
823 tasklet_schedule(&host->tasklet);
826 } while (status & MCI_RXRDY);
828 host->pio_offset = offset;
829 data->bytes_xfered += nbytes;
834 mci_writel(host, IDR, MCI_RXRDY);
835 mci_writel(host, IER, MCI_NOTBUSY);
836 data->bytes_xfered += nbytes;
837 atmci_set_completed(host, EVENT_XFER_COMPLETE);
838 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
839 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
840 send_stop_cmd(host->mmc, data);
843 static void atmci_write_data_pio(struct atmel_mci *host)
845 struct scatterlist *sg = host->sg;
846 void *buf = sg_virt(sg);
847 unsigned int offset = host->pio_offset;
848 struct mmc_data *data = host->data;
851 unsigned int nbytes = 0;
854 if (likely(offset + 4 <= sg->length)) {
855 value = get_unaligned((u32 *)(buf + offset));
856 mci_writel(host, TDR, value);
860 if (offset == sg->length) {
861 host->sg = sg = sg_next(sg);
869 unsigned int remaining = sg->length - offset;
872 memcpy(&value, buf + offset, remaining);
875 host->sg = sg = sg_next(sg);
877 mci_writel(host, TDR, value);
881 offset = 4 - remaining;
883 memcpy((u8 *)&value + remaining, buf, offset);
884 mci_writel(host, TDR, value);
888 status = mci_readl(host, SR);
889 if (status & ATMCI_DATA_ERROR_FLAGS) {
890 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
891 | ATMCI_DATA_ERROR_FLAGS));
892 host->data_status = status;
893 atmci_set_pending(host, EVENT_DATA_ERROR);
894 tasklet_schedule(&host->tasklet);
897 } while (status & MCI_TXRDY);
899 host->pio_offset = offset;
900 data->bytes_xfered += nbytes;
905 mci_writel(host, IDR, MCI_TXRDY);
906 mci_writel(host, IER, MCI_NOTBUSY);
907 data->bytes_xfered += nbytes;
908 atmci_set_completed(host, EVENT_XFER_COMPLETE);
909 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
910 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
911 send_stop_cmd(host->mmc, data);
914 static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
916 struct atmel_mci *host = mmc_priv(mmc);
918 mci_writel(host, IDR, MCI_CMDRDY);
920 if (atmci_is_completed(host, EVENT_STOP_SENT)) {
921 host->stop_status = status;
922 atmci_set_pending(host, EVENT_STOP_COMPLETE);
924 host->cmd_status = status;
925 atmci_set_pending(host, EVENT_CMD_COMPLETE);
928 tasklet_schedule(&host->tasklet);
931 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
933 struct mmc_host *mmc = dev_id;
934 struct atmel_mci *host = mmc_priv(mmc);
935 u32 status, mask, pending;
936 unsigned int pass_count = 0;
938 spin_lock(&mmc->lock);
941 status = mci_readl(host, SR);
942 mask = mci_readl(host, IMR);
943 pending = status & mask;
947 if (pending & ATMCI_DATA_ERROR_FLAGS) {
948 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
949 | MCI_RXRDY | MCI_TXRDY);
950 pending &= mci_readl(host, IMR);
951 host->data_status = status;
952 atmci_set_pending(host, EVENT_DATA_ERROR);
953 tasklet_schedule(&host->tasklet);
955 if (pending & MCI_NOTBUSY) {
956 mci_writel(host, IDR, (MCI_NOTBUSY
957 | ATMCI_DATA_ERROR_FLAGS));
958 atmci_set_pending(host, EVENT_DATA_COMPLETE);
959 tasklet_schedule(&host->tasklet);
961 if (pending & MCI_RXRDY)
962 atmci_read_data_pio(host);
963 if (pending & MCI_TXRDY)
964 atmci_write_data_pio(host);
966 if (pending & MCI_CMDRDY)
967 atmci_cmd_interrupt(mmc, status);
968 } while (pass_count++ < 5);
970 spin_unlock(&mmc->lock);
972 return pass_count ? IRQ_HANDLED : IRQ_NONE;
975 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
977 struct mmc_host *mmc = dev_id;
978 struct atmel_mci *host = mmc_priv(mmc);
981 * Disable interrupts until the pin has stabilized and check
982 * the state then. Use mod_timer() since we may be in the
983 * middle of the timer routine when this interrupt triggers.
985 disable_irq_nosync(irq);
986 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
991 static int __init atmci_probe(struct platform_device *pdev)
993 struct mci_platform_data *pdata;
994 struct atmel_mci *host;
995 struct mmc_host *mmc;
996 struct resource *regs;
1000 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1003 pdata = pdev->dev.platform_data;
1006 irq = platform_get_irq(pdev, 0);
1010 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1014 host = mmc_priv(mmc);
1017 host->detect_pin = pdata->detect_pin;
1018 host->wp_pin = pdata->wp_pin;
1020 host->mck = clk_get(&pdev->dev, "mci_clk");
1021 if (IS_ERR(host->mck)) {
1022 ret = PTR_ERR(host->mck);
1027 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1031 clk_enable(host->mck);
1032 mci_writel(host, CR, MCI_CR_SWRST);
1033 host->bus_hz = clk_get_rate(host->mck);
1034 clk_disable(host->mck);
1036 host->mapbase = regs->start;
1038 mmc->ops = &atmci_ops;
1039 mmc->f_min = (host->bus_hz + 511) / 512;
1040 mmc->f_max = host->bus_hz / 2;
1041 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1042 mmc->caps |= MMC_CAP_4_BIT_DATA;
1044 mmc->max_hw_segs = 64;
1045 mmc->max_phys_segs = 64;
1046 mmc->max_req_size = 32768 * 512;
1047 mmc->max_blk_size = 32768;
1048 mmc->max_blk_count = 512;
1050 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1052 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
1054 goto err_request_irq;
1056 /* Assume card is present if we don't have a detect pin */
1058 if (gpio_is_valid(host->detect_pin)) {
1059 if (gpio_request(host->detect_pin, "mmc_detect")) {
1060 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1061 host->detect_pin = -1;
1063 host->present = !gpio_get_value(host->detect_pin);
1066 if (gpio_is_valid(host->wp_pin)) {
1067 if (gpio_request(host->wp_pin, "mmc_wp")) {
1068 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1073 platform_set_drvdata(pdev, host);
1077 if (gpio_is_valid(host->detect_pin)) {
1078 setup_timer(&host->detect_timer, atmci_detect_change,
1079 (unsigned long)host);
1081 ret = request_irq(gpio_to_irq(host->detect_pin),
1082 atmci_detect_interrupt,
1083 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1086 dev_dbg(&mmc->class_dev,
1087 "could not request IRQ %d for detect pin\n",
1088 gpio_to_irq(host->detect_pin));
1089 gpio_free(host->detect_pin);
1090 host->detect_pin = -1;
1094 dev_info(&mmc->class_dev,
1095 "Atmel MCI controller at 0x%08lx irq %d\n",
1096 host->mapbase, irq);
1098 atmci_init_debugfs(host);
1103 iounmap(host->regs);
1111 static int __exit atmci_remove(struct platform_device *pdev)
1113 struct atmel_mci *host = platform_get_drvdata(pdev);
1115 platform_set_drvdata(pdev, NULL);
1118 /* Debugfs stuff is cleaned up by mmc core */
1120 if (gpio_is_valid(host->detect_pin)) {
1121 int pin = host->detect_pin;
1123 /* Make sure the timer doesn't enable the interrupt */
1124 host->detect_pin = -1;
1127 free_irq(gpio_to_irq(pin), host->mmc);
1128 del_timer_sync(&host->detect_timer);
1132 mmc_remove_host(host->mmc);
1134 clk_enable(host->mck);
1135 mci_writel(host, IDR, ~0UL);
1136 mci_writel(host, CR, MCI_CR_MCIDIS);
1137 mci_readl(host, SR);
1138 clk_disable(host->mck);
1140 if (gpio_is_valid(host->wp_pin))
1141 gpio_free(host->wp_pin);
1143 free_irq(platform_get_irq(pdev, 0), host->mmc);
1144 iounmap(host->regs);
1148 mmc_free_host(host->mmc);
1153 static struct platform_driver atmci_driver = {
1154 .remove = __exit_p(atmci_remove),
1156 .name = "atmel_mci",
1160 static int __init atmci_init(void)
1162 return platform_driver_probe(&atmci_driver, atmci_probe);
1165 static void __exit atmci_exit(void)
1167 platform_driver_unregister(&atmci_driver);
1170 module_init(atmci_init);
1171 module_exit(atmci_exit);
1173 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1174 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1175 MODULE_LICENSE("GPL v2");