atmel-mci: Platform code for supporting multiple mmc slots
[linux-2.6/cjktty.git] / drivers / mmc / host / atmel-mci.c
blob8170905a040149db94f8430074c037e659285400
1 /*
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.
9 */
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>
28 #include <asm/io.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)
37 enum {
38 EVENT_CMD_COMPLETE = 0,
39 EVENT_XFER_COMPLETE,
40 EVENT_DATA_COMPLETE,
41 EVENT_DATA_ERROR,
44 enum atmel_mci_state {
45 STATE_SENDING_CMD = 0,
46 STATE_SENDING_DATA,
47 STATE_DATA_BUSY,
48 STATE_SENDING_STOP,
49 STATE_DATA_ERROR,
52 struct atmel_mci {
53 struct mmc_host *mmc;
54 void __iomem *regs;
56 struct scatterlist *sg;
57 unsigned int pio_offset;
59 struct mmc_request *mrq;
60 struct mmc_command *cmd;
61 struct mmc_data *data;
63 u32 cmd_status;
64 u32 data_status;
65 u32 stop_cmdr;
67 u32 mode_reg;
68 u32 sdc_reg;
70 struct tasklet_struct tasklet;
71 unsigned long pending_events;
72 unsigned long completed_events;
73 enum atmel_mci_state state;
75 int present;
76 int detect_pin;
77 int wp_pin;
79 /* For detect pin debouncing */
80 struct timer_list detect_timer;
82 unsigned long bus_hz;
83 unsigned long mapbase;
84 struct clk *mck;
85 struct platform_device *pdev;
88 #define atmci_test_and_clear_pending(host, event) \
89 test_and_clear_bit(event, &host->pending_events)
90 #define atmci_set_completed(host, event) \
91 set_bit(event, &host->completed_events)
92 #define atmci_set_pending(host, event) \
93 set_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);
110 if (mrq) {
111 cmd = mrq->cmd;
112 data = mrq->data;
113 stop = mrq->stop;
115 if (cmd)
116 seq_printf(s,
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);
121 if (data)
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);
125 if (stop)
126 seq_printf(s,
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);
135 return 0;
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,
146 .read = seq_read,
147 .llseek = seq_lseek,
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[] = {
155 [0] = "CMDRDY",
156 [1] = "RXRDY",
157 [2] = "TXRDY",
158 [3] = "BLKE",
159 [4] = "DTIP",
160 [5] = "NOTBUSY",
161 [8] = "SDIOIRQA",
162 [9] = "SDIOIRQB",
163 [16] = "RINDE",
164 [17] = "RDIRE",
165 [18] = "RCRCE",
166 [19] = "RENDE",
167 [20] = "RTOE",
168 [21] = "DCRCE",
169 [22] = "DTOE",
170 [30] = "OVRE",
171 [31] = "UNRE",
173 unsigned int i;
175 seq_printf(s, "%s:\t0x%08x", regname, value);
176 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
177 if (value & (1 << i)) {
178 if (sr_bit[i])
179 seq_printf(s, " %s", sr_bit[i]);
180 else
181 seq_puts(s, " UNKNOWN");
184 seq_putc(s, '\n');
187 static int atmci_regs_show(struct seq_file *s, void *v)
189 struct atmel_mci *host = s->private;
190 u32 *buf;
192 buf = kmalloc(MCI_REGS_SIZE, GFP_KERNEL);
193 if (!buf)
194 return -ENOMEM;
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",
204 buf[MCI_MR / 4],
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",
212 buf[MCI_BLKR / 4],
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]);
221 kfree(buf);
223 return 0;
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,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = single_release,
239 static void atmci_init_debugfs(struct atmel_mci *host)
241 struct mmc_host *mmc;
242 struct dentry *root;
243 struct dentry *node;
245 mmc = host->mmc;
246 root = mmc->debugfs_root;
247 if (!root)
248 return;
250 node = debugfs_create_file("regs", S_IRUSR, root, host,
251 &atmci_regs_fops);
252 if (IS_ERR(node))
253 return;
254 if (!node)
255 goto err;
257 node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops);
258 if (!node)
259 goto err;
261 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
262 if (!node)
263 goto err;
265 node = debugfs_create_x32("pending_events", S_IRUSR, root,
266 (u32 *)&host->pending_events);
267 if (!node)
268 goto err;
270 node = debugfs_create_x32("completed_events", S_IRUSR, root,
271 (u32 *)&host->completed_events);
272 if (!node)
273 goto err;
275 return;
277 err:
278 dev_err(&host->pdev->dev,
279 "failed to initialize debugfs for controller\n");
282 static inline unsigned int ns_to_clocks(struct atmel_mci *host,
283 unsigned int ns)
285 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
288 static void atmci_set_timeout(struct atmel_mci *host,
289 struct mmc_data *data)
291 static unsigned dtomul_to_shift[] = {
292 0, 4, 7, 8, 10, 12, 16, 20
294 unsigned timeout;
295 unsigned dtocyc;
296 unsigned dtomul;
298 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
300 for (dtomul = 0; dtomul < 8; dtomul++) {
301 unsigned shift = dtomul_to_shift[dtomul];
302 dtocyc = (timeout + (1 << shift) - 1) >> shift;
303 if (dtocyc < 15)
304 break;
307 if (dtomul >= 8) {
308 dtomul = 7;
309 dtocyc = 15;
312 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
313 dtocyc << dtomul_to_shift[dtomul]);
314 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
318 * Return mask with command flags to be enabled for this command.
320 static u32 atmci_prepare_command(struct mmc_host *mmc,
321 struct mmc_command *cmd)
323 struct mmc_data *data;
324 u32 cmdr;
326 cmd->error = -EINPROGRESS;
328 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
330 if (cmd->flags & MMC_RSP_PRESENT) {
331 if (cmd->flags & MMC_RSP_136)
332 cmdr |= MCI_CMDR_RSPTYP_136BIT;
333 else
334 cmdr |= MCI_CMDR_RSPTYP_48BIT;
338 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
339 * it's too difficult to determine whether this is an ACMD or
340 * not. Better make it 64.
342 cmdr |= MCI_CMDR_MAXLAT_64CYC;
344 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
345 cmdr |= MCI_CMDR_OPDCMD;
347 data = cmd->data;
348 if (data) {
349 cmdr |= MCI_CMDR_START_XFER;
350 if (data->flags & MMC_DATA_STREAM)
351 cmdr |= MCI_CMDR_STREAM;
352 else if (data->blocks > 1)
353 cmdr |= MCI_CMDR_MULTI_BLOCK;
354 else
355 cmdr |= MCI_CMDR_BLOCK;
357 if (data->flags & MMC_DATA_READ)
358 cmdr |= MCI_CMDR_TRDIR_READ;
361 return cmdr;
364 static void atmci_start_command(struct atmel_mci *host,
365 struct mmc_command *cmd,
366 u32 cmd_flags)
368 WARN_ON(host->cmd);
369 host->cmd = cmd;
371 dev_vdbg(&host->mmc->class_dev,
372 "start command: ARGR=0x%08x CMDR=0x%08x\n",
373 cmd->arg, cmd_flags);
375 mci_writel(host, ARGR, cmd->arg);
376 mci_writel(host, CMDR, cmd_flags);
379 static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
381 struct atmel_mci *host = mmc_priv(mmc);
383 atmci_start_command(host, data->stop, host->stop_cmdr);
384 mci_writel(host, IER, MCI_CMDRDY);
387 static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
389 struct atmel_mci *host = mmc_priv(mmc);
391 WARN_ON(host->cmd || host->data);
392 host->mrq = NULL;
394 mmc_request_done(mmc, mrq);
398 * Returns a mask of interrupt flags to be enabled after the whole
399 * request has been prepared.
401 static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
403 struct atmel_mci *host = mmc_priv(mmc);
404 u32 iflags;
406 data->error = -EINPROGRESS;
408 WARN_ON(host->data);
409 host->sg = NULL;
410 host->data = data;
412 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
413 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
415 iflags = ATMCI_DATA_ERROR_FLAGS;
416 host->sg = data->sg;
417 host->pio_offset = 0;
418 if (data->flags & MMC_DATA_READ)
419 iflags |= MCI_RXRDY;
420 else
421 iflags |= MCI_TXRDY;
423 return iflags;
426 static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
428 struct atmel_mci *host = mmc_priv(mmc);
429 struct mmc_data *data;
430 struct mmc_command *cmd;
431 u32 iflags;
432 u32 cmdflags = 0;
434 iflags = mci_readl(host, IMR);
435 if (iflags)
436 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
437 mci_readl(host, IMR));
439 WARN_ON(host->mrq != NULL);
442 * We may "know" the card is gone even though there's still an
443 * electrical connection. If so, we really need to communicate
444 * this to the MMC core since there won't be any more
445 * interrupts as the card is completely removed. Otherwise,
446 * the MMC core might believe the card is still there even
447 * though the card was just removed very slowly.
449 if (!host->present) {
450 mrq->cmd->error = -ENOMEDIUM;
451 mmc_request_done(mmc, mrq);
452 return;
455 host->mrq = mrq;
456 host->pending_events = 0;
457 host->completed_events = 0;
458 host->state = STATE_SENDING_CMD;
460 /* We don't support multiple blocks of weird lengths. */
461 data = mrq->data;
462 if (data) {
463 if (data->blocks > 1 && data->blksz & 3)
464 goto fail;
465 atmci_set_timeout(host, data);
467 /* Must set block count/size before sending command */
468 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
469 | MCI_BLKLEN(data->blksz));
472 iflags = MCI_CMDRDY;
473 cmd = mrq->cmd;
474 cmdflags = atmci_prepare_command(mmc, cmd);
475 atmci_start_command(host, cmd, cmdflags);
477 if (data)
478 iflags |= atmci_submit_data(mmc, data);
480 if (mrq->stop) {
481 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
482 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
483 if (!(data->flags & MMC_DATA_WRITE))
484 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
485 if (data->flags & MMC_DATA_STREAM)
486 host->stop_cmdr |= MCI_CMDR_STREAM;
487 else
488 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
492 * We could have enabled interrupts earlier, but I suspect
493 * that would open up a nice can of interesting race
494 * conditions (e.g. command and data complete, but stop not
495 * prepared yet.)
497 mci_writel(host, IER, iflags);
499 return;
501 fail:
502 host->mrq = NULL;
503 mrq->cmd->error = -EINVAL;
504 mmc_request_done(mmc, mrq);
507 static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
509 struct atmel_mci *host = mmc_priv(mmc);
511 host->sdc_reg &= ~MCI_SDCBUS_MASK;
512 switch (ios->bus_width) {
513 case MMC_BUS_WIDTH_1:
514 host->sdc_reg |= MCI_SDCBUS_1BIT;
515 break;
516 case MMC_BUS_WIDTH_4:
517 host->sdc_reg = MCI_SDCBUS_4BIT;
518 break;
521 if (ios->clock) {
522 u32 clkdiv;
524 if (!host->mode_reg)
525 clk_enable(host->mck);
527 /* Set clock rate */
528 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
529 if (clkdiv > 255) {
530 dev_warn(&mmc->class_dev,
531 "clock %u too slow; using %lu\n",
532 ios->clock, host->bus_hz / (2 * 256));
533 clkdiv = 255;
536 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
537 | MCI_MR_RDPROOF;
539 mci_writel(host, CR, MCI_CR_MCIEN);
540 mci_writel(host, MR, host->mode_reg);
541 mci_writel(host, SDCR, host->sdc_reg);
542 } else {
543 mci_writel(host, CR, MCI_CR_MCIDIS);
544 if (host->mode_reg) {
545 mci_readl(host, MR);
546 clk_disable(host->mck);
548 host->mode_reg = 0;
551 switch (ios->power_mode) {
552 default:
554 * TODO: None of the currently available AVR32-based
555 * boards allow MMC power to be turned off. Implement
556 * power control when this can be tested properly.
558 break;
562 static int atmci_get_ro(struct mmc_host *mmc)
564 int read_only = 0;
565 struct atmel_mci *host = mmc_priv(mmc);
567 if (gpio_is_valid(host->wp_pin)) {
568 read_only = gpio_get_value(host->wp_pin);
569 dev_dbg(&mmc->class_dev, "card is %s\n",
570 read_only ? "read-only" : "read-write");
571 } else {
572 dev_dbg(&mmc->class_dev,
573 "no pin for checking read-only switch."
574 " Assuming write-enable.\n");
577 return read_only;
580 static struct mmc_host_ops atmci_ops = {
581 .request = atmci_request,
582 .set_ios = atmci_set_ios,
583 .get_ro = atmci_get_ro,
586 static void atmci_command_complete(struct atmel_mci *host,
587 struct mmc_command *cmd)
589 u32 status = host->cmd_status;
591 /* Read the response from the card (up to 16 bytes) */
592 cmd->resp[0] = mci_readl(host, RSPR);
593 cmd->resp[1] = mci_readl(host, RSPR);
594 cmd->resp[2] = mci_readl(host, RSPR);
595 cmd->resp[3] = mci_readl(host, RSPR);
597 if (status & MCI_RTOE)
598 cmd->error = -ETIMEDOUT;
599 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
600 cmd->error = -EILSEQ;
601 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
602 cmd->error = -EIO;
603 else
604 cmd->error = 0;
606 if (cmd->error) {
607 dev_dbg(&host->mmc->class_dev,
608 "command error: status=0x%08x\n", status);
610 if (cmd->data) {
611 host->data = NULL;
612 mci_writel(host, IDR, MCI_NOTBUSY
613 | MCI_TXRDY | MCI_RXRDY
614 | ATMCI_DATA_ERROR_FLAGS);
619 static void atmci_detect_change(unsigned long data)
621 struct atmel_mci *host = (struct atmel_mci *)data;
622 struct mmc_request *mrq = host->mrq;
623 int present;
626 * atmci_remove() sets detect_pin to -1 before freeing the
627 * interrupt. We must not re-enable the interrupt if it has
628 * been freed.
630 smp_rmb();
631 if (!gpio_is_valid(host->detect_pin))
632 return;
634 enable_irq(gpio_to_irq(host->detect_pin));
635 present = !gpio_get_value(host->detect_pin);
637 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
638 present, host->present);
640 if (present != host->present) {
641 dev_dbg(&host->mmc->class_dev, "card %s\n",
642 present ? "inserted" : "removed");
643 host->present = present;
645 /* Reset controller if card is gone */
646 if (!present) {
647 mci_writel(host, CR, MCI_CR_SWRST);
648 mci_writel(host, IDR, ~0UL);
649 mci_writel(host, CR, MCI_CR_MCIEN);
652 /* Clean up queue if present */
653 if (mrq) {
655 * Reset controller to terminate any ongoing
656 * commands or data transfers.
658 mci_writel(host, CR, MCI_CR_SWRST);
659 mci_readl(host, SR);
661 host->data = NULL;
662 host->cmd = NULL;
664 switch (host->state) {
665 case STATE_SENDING_CMD:
666 mrq->cmd->error = -ENOMEDIUM;
667 if (!mrq->data)
668 break;
669 /* fall through */
670 case STATE_SENDING_DATA:
671 mrq->data->error = -ENOMEDIUM;
672 break;
673 case STATE_DATA_BUSY:
674 case STATE_DATA_ERROR:
675 if (mrq->data->error == -EINPROGRESS)
676 mrq->data->error = -ENOMEDIUM;
677 if (!mrq->stop)
678 break;
679 /* fall through */
680 case STATE_SENDING_STOP:
681 mrq->stop->error = -ENOMEDIUM;
682 break;
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;
698 struct mmc_command *cmd = host->cmd;
699 enum atmel_mci_state state = host->state;
700 enum atmel_mci_state prev_state;
701 u32 status;
703 state = host->state;
705 dev_vdbg(&mmc->class_dev,
706 "tasklet: state %u pending/completed/mask %lx/%lx/%x\n",
707 state, host->pending_events, host->completed_events,
708 mci_readl(host, IMR));
710 do {
711 prev_state = state;
713 switch (state) {
714 case STATE_SENDING_CMD:
715 if (!atmci_test_and_clear_pending(host,
716 EVENT_CMD_COMPLETE))
717 break;
719 host->cmd = NULL;
720 atmci_set_completed(host, EVENT_CMD_COMPLETE);
721 atmci_command_complete(host, mrq->cmd);
722 if (!mrq->data || cmd->error) {
723 atmci_request_end(mmc, host->mrq);
724 break;
727 prev_state = state = STATE_SENDING_DATA;
728 /* fall through */
730 case STATE_SENDING_DATA:
731 if (atmci_test_and_clear_pending(host,
732 EVENT_DATA_ERROR)) {
733 if (data->stop)
734 send_stop_cmd(host->mmc, data);
735 state = STATE_DATA_ERROR;
736 break;
739 if (!atmci_test_and_clear_pending(host,
740 EVENT_XFER_COMPLETE))
741 break;
743 atmci_set_completed(host, EVENT_XFER_COMPLETE);
744 prev_state = state = STATE_DATA_BUSY;
745 /* fall through */
747 case STATE_DATA_BUSY:
748 if (!atmci_test_and_clear_pending(host,
749 EVENT_DATA_COMPLETE))
750 break;
752 host->data = NULL;
753 atmci_set_completed(host, EVENT_DATA_COMPLETE);
754 status = host->data_status;
755 if (unlikely(status & ATMCI_DATA_ERROR_FLAGS)) {
756 if (status & MCI_DTOE) {
757 dev_dbg(&mmc->class_dev,
758 "data timeout error\n");
759 data->error = -ETIMEDOUT;
760 } else if (status & MCI_DCRCE) {
761 dev_dbg(&mmc->class_dev,
762 "data CRC error\n");
763 data->error = -EILSEQ;
764 } else {
765 dev_dbg(&mmc->class_dev,
766 "data FIFO error (status=%08x)\n",
767 status);
768 data->error = -EIO;
770 } else {
771 data->bytes_xfered = data->blocks * data->blksz;
772 data->error = 0;
775 if (!data->stop) {
776 atmci_request_end(mmc, host->mrq);
777 prev_state = state;
778 break;
781 prev_state = state = STATE_SENDING_STOP;
782 if (!data->error)
783 send_stop_cmd(host->mmc, data);
784 /* fall through */
786 case STATE_SENDING_STOP:
787 if (!atmci_test_and_clear_pending(host,
788 EVENT_CMD_COMPLETE))
789 break;
791 host->cmd = NULL;
792 atmci_command_complete(host, mrq->stop);
793 atmci_request_end(mmc, host->mrq);
794 prev_state = state;
795 break;
797 case STATE_DATA_ERROR:
798 if (!atmci_test_and_clear_pending(host,
799 EVENT_XFER_COMPLETE))
800 break;
802 state = STATE_DATA_BUSY;
803 break;
805 } while (state != prev_state);
807 host->state = state;
810 static void atmci_read_data_pio(struct atmel_mci *host)
812 struct scatterlist *sg = host->sg;
813 void *buf = sg_virt(sg);
814 unsigned int offset = host->pio_offset;
815 struct mmc_data *data = host->data;
816 u32 value;
817 u32 status;
818 unsigned int nbytes = 0;
820 do {
821 value = mci_readl(host, RDR);
822 if (likely(offset + 4 <= sg->length)) {
823 put_unaligned(value, (u32 *)(buf + offset));
825 offset += 4;
826 nbytes += 4;
828 if (offset == sg->length) {
829 host->sg = sg = sg_next(sg);
830 if (!sg)
831 goto done;
833 offset = 0;
834 buf = sg_virt(sg);
836 } else {
837 unsigned int remaining = sg->length - offset;
838 memcpy(buf + offset, &value, remaining);
839 nbytes += remaining;
841 flush_dcache_page(sg_page(sg));
842 host->sg = sg = sg_next(sg);
843 if (!sg)
844 goto done;
846 offset = 4 - remaining;
847 buf = sg_virt(sg);
848 memcpy(buf, (u8 *)&value + remaining, offset);
849 nbytes += offset;
852 status = mci_readl(host, SR);
853 if (status & ATMCI_DATA_ERROR_FLAGS) {
854 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
855 | ATMCI_DATA_ERROR_FLAGS));
856 host->data_status = status;
857 atmci_set_pending(host, EVENT_DATA_ERROR);
858 tasklet_schedule(&host->tasklet);
859 break;
861 } while (status & MCI_RXRDY);
863 host->pio_offset = offset;
864 data->bytes_xfered += nbytes;
866 return;
868 done:
869 mci_writel(host, IDR, MCI_RXRDY);
870 mci_writel(host, IER, MCI_NOTBUSY);
871 data->bytes_xfered += nbytes;
872 atmci_set_pending(host, EVENT_XFER_COMPLETE);
875 static void atmci_write_data_pio(struct atmel_mci *host)
877 struct scatterlist *sg = host->sg;
878 void *buf = sg_virt(sg);
879 unsigned int offset = host->pio_offset;
880 struct mmc_data *data = host->data;
881 u32 value;
882 u32 status;
883 unsigned int nbytes = 0;
885 do {
886 if (likely(offset + 4 <= sg->length)) {
887 value = get_unaligned((u32 *)(buf + offset));
888 mci_writel(host, TDR, value);
890 offset += 4;
891 nbytes += 4;
892 if (offset == sg->length) {
893 host->sg = sg = sg_next(sg);
894 if (!sg)
895 goto done;
897 offset = 0;
898 buf = sg_virt(sg);
900 } else {
901 unsigned int remaining = sg->length - offset;
903 value = 0;
904 memcpy(&value, buf + offset, remaining);
905 nbytes += remaining;
907 host->sg = sg = sg_next(sg);
908 if (!sg) {
909 mci_writel(host, TDR, value);
910 goto done;
913 offset = 4 - remaining;
914 buf = sg_virt(sg);
915 memcpy((u8 *)&value + remaining, buf, offset);
916 mci_writel(host, TDR, value);
917 nbytes += offset;
920 status = mci_readl(host, SR);
921 if (status & ATMCI_DATA_ERROR_FLAGS) {
922 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
923 | ATMCI_DATA_ERROR_FLAGS));
924 host->data_status = status;
925 atmci_set_pending(host, EVENT_DATA_ERROR);
926 tasklet_schedule(&host->tasklet);
927 break;
929 } while (status & MCI_TXRDY);
931 host->pio_offset = offset;
932 data->bytes_xfered += nbytes;
934 return;
936 done:
937 mci_writel(host, IDR, MCI_TXRDY);
938 mci_writel(host, IER, MCI_NOTBUSY);
939 data->bytes_xfered += nbytes;
940 atmci_set_pending(host, EVENT_XFER_COMPLETE);
943 static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
945 struct atmel_mci *host = mmc_priv(mmc);
947 mci_writel(host, IDR, MCI_CMDRDY);
949 host->cmd_status = status;
950 atmci_set_pending(host, EVENT_CMD_COMPLETE);
951 tasklet_schedule(&host->tasklet);
954 static irqreturn_t atmci_interrupt(int irq, void *dev_id)
956 struct mmc_host *mmc = dev_id;
957 struct atmel_mci *host = mmc_priv(mmc);
958 u32 status, mask, pending;
959 unsigned int pass_count = 0;
961 spin_lock(&mmc->lock);
963 do {
964 status = mci_readl(host, SR);
965 mask = mci_readl(host, IMR);
966 pending = status & mask;
967 if (!pending)
968 break;
970 if (pending & ATMCI_DATA_ERROR_FLAGS) {
971 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
972 | MCI_RXRDY | MCI_TXRDY);
973 pending &= mci_readl(host, IMR);
974 host->data_status = status;
975 atmci_set_pending(host, EVENT_DATA_ERROR);
976 tasklet_schedule(&host->tasklet);
978 if (pending & MCI_NOTBUSY) {
979 mci_writel(host, IDR,
980 ATMCI_DATA_ERROR_FLAGS | MCI_NOTBUSY);
981 host->data_status = status;
982 atmci_set_pending(host, EVENT_DATA_COMPLETE);
983 tasklet_schedule(&host->tasklet);
985 if (pending & MCI_RXRDY)
986 atmci_read_data_pio(host);
987 if (pending & MCI_TXRDY)
988 atmci_write_data_pio(host);
990 if (pending & MCI_CMDRDY)
991 atmci_cmd_interrupt(mmc, status);
992 } while (pass_count++ < 5);
994 spin_unlock(&mmc->lock);
996 return pass_count ? IRQ_HANDLED : IRQ_NONE;
999 static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
1001 struct mmc_host *mmc = dev_id;
1002 struct atmel_mci *host = mmc_priv(mmc);
1005 * Disable interrupts until the pin has stabilized and check
1006 * the state then. Use mod_timer() since we may be in the
1007 * middle of the timer routine when this interrupt triggers.
1009 disable_irq_nosync(irq);
1010 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
1012 return IRQ_HANDLED;
1015 static int __init atmci_probe(struct platform_device *pdev)
1017 struct mci_platform_data *pdata;
1018 struct mci_slot_pdata *slot;
1019 struct atmel_mci *host;
1020 struct mmc_host *mmc;
1021 struct resource *regs;
1022 u32 sdc_reg;
1023 int irq;
1024 int ret;
1026 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1027 if (!regs)
1028 return -ENXIO;
1029 pdata = pdev->dev.platform_data;
1030 if (!pdata)
1031 return -ENXIO;
1032 irq = platform_get_irq(pdev, 0);
1033 if (irq < 0)
1034 return irq;
1036 /* TODO: Allow using several slots at once */
1037 if (pdata->slot[0].bus_width) {
1038 sdc_reg = MCI_SDCSEL_SLOT_A;
1039 slot = &pdata->slot[0];
1040 } else if (pdata->slot[1].bus_width) {
1041 sdc_reg = MCI_SDCSEL_SLOT_B;
1042 slot = &pdata->slot[1];
1043 } else {
1044 return -EINVAL;
1047 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1048 if (!mmc)
1049 return -ENOMEM;
1051 host = mmc_priv(mmc);
1052 host->pdev = pdev;
1053 host->mmc = mmc;
1054 host->detect_pin = slot->detect_pin;
1055 host->wp_pin = slot->wp_pin;
1056 host->sdc_reg = sdc_reg;
1058 host->mck = clk_get(&pdev->dev, "mci_clk");
1059 if (IS_ERR(host->mck)) {
1060 ret = PTR_ERR(host->mck);
1061 goto err_clk_get;
1064 ret = -ENOMEM;
1065 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1066 if (!host->regs)
1067 goto err_ioremap;
1069 clk_enable(host->mck);
1070 mci_writel(host, CR, MCI_CR_SWRST);
1071 host->bus_hz = clk_get_rate(host->mck);
1072 clk_disable(host->mck);
1074 host->mapbase = regs->start;
1076 mmc->ops = &atmci_ops;
1077 mmc->f_min = (host->bus_hz + 511) / 512;
1078 mmc->f_max = host->bus_hz / 2;
1079 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
1080 if (slot->bus_width >= 4)
1081 mmc->caps |= MMC_CAP_4_BIT_DATA;
1083 mmc->max_hw_segs = 64;
1084 mmc->max_phys_segs = 64;
1085 mmc->max_req_size = 32768 * 512;
1086 mmc->max_blk_size = 32768;
1087 mmc->max_blk_count = 512;
1089 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1091 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
1092 if (ret)
1093 goto err_request_irq;
1095 /* Assume card is present if we don't have a detect pin */
1096 host->present = 1;
1097 if (gpio_is_valid(host->detect_pin)) {
1098 if (gpio_request(host->detect_pin, "mmc_detect")) {
1099 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1100 host->detect_pin = -1;
1101 } else {
1102 host->present = !gpio_get_value(host->detect_pin);
1106 if (!gpio_is_valid(host->detect_pin))
1107 mmc->caps |= MMC_CAP_NEEDS_POLL;
1109 if (gpio_is_valid(host->wp_pin)) {
1110 if (gpio_request(host->wp_pin, "mmc_wp")) {
1111 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1112 host->wp_pin = -1;
1116 platform_set_drvdata(pdev, host);
1118 mmc_add_host(mmc);
1120 if (gpio_is_valid(host->detect_pin)) {
1121 setup_timer(&host->detect_timer, atmci_detect_change,
1122 (unsigned long)host);
1124 ret = request_irq(gpio_to_irq(host->detect_pin),
1125 atmci_detect_interrupt,
1126 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1127 "mmc-detect", mmc);
1128 if (ret) {
1129 dev_dbg(&mmc->class_dev,
1130 "could not request IRQ %d for detect pin\n",
1131 gpio_to_irq(host->detect_pin));
1132 gpio_free(host->detect_pin);
1133 host->detect_pin = -1;
1137 dev_info(&mmc->class_dev,
1138 "Atmel MCI controller at 0x%08lx irq %d\n",
1139 host->mapbase, irq);
1141 atmci_init_debugfs(host);
1143 return 0;
1145 err_request_irq:
1146 iounmap(host->regs);
1147 err_ioremap:
1148 clk_put(host->mck);
1149 err_clk_get:
1150 mmc_free_host(mmc);
1151 return ret;
1154 static int __exit atmci_remove(struct platform_device *pdev)
1156 struct atmel_mci *host = platform_get_drvdata(pdev);
1158 platform_set_drvdata(pdev, NULL);
1160 if (host) {
1161 /* Debugfs stuff is cleaned up by mmc core */
1163 if (gpio_is_valid(host->detect_pin)) {
1164 int pin = host->detect_pin;
1166 /* Make sure the timer doesn't enable the interrupt */
1167 host->detect_pin = -1;
1168 smp_wmb();
1170 free_irq(gpio_to_irq(pin), host->mmc);
1171 del_timer_sync(&host->detect_timer);
1172 gpio_free(pin);
1175 mmc_remove_host(host->mmc);
1177 clk_enable(host->mck);
1178 mci_writel(host, IDR, ~0UL);
1179 mci_writel(host, CR, MCI_CR_MCIDIS);
1180 mci_readl(host, SR);
1181 clk_disable(host->mck);
1183 if (gpio_is_valid(host->wp_pin))
1184 gpio_free(host->wp_pin);
1186 free_irq(platform_get_irq(pdev, 0), host->mmc);
1187 iounmap(host->regs);
1189 clk_put(host->mck);
1191 mmc_free_host(host->mmc);
1193 return 0;
1196 static struct platform_driver atmci_driver = {
1197 .remove = __exit_p(atmci_remove),
1198 .driver = {
1199 .name = "atmel_mci",
1203 static int __init atmci_init(void)
1205 return platform_driver_probe(&atmci_driver, atmci_probe);
1208 static void __exit atmci_exit(void)
1210 platform_driver_unregister(&atmci_driver);
1213 module_init(atmci_init);
1214 module_exit(atmci_exit);
1216 MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1217 MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1218 MODULE_LICENSE("GPL v2");