2 * linux/drivers/mmc/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2006 Pierre Ossman, All Rights Reserved.
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.
11 #include <linux/delay.h>
12 #include <linux/highmem.h>
13 #include <linux/pci.h>
14 #include <linux/dma-mapping.h>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/protocol.h>
19 #include <asm/scatterlist.h>
23 #define DRIVER_NAME "sdhci"
24 #define DRIVER_VERSION "0.12"
26 #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
28 #define DBG(f, x...) \
29 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
31 static unsigned int debug_nodma = 0;
32 static unsigned int debug_forcedma = 0;
33 static unsigned int debug_quirks = 0;
35 #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
36 #define SDHCI_QUIRK_FORCE_DMA (1<<1)
38 static const struct pci_device_id pci_ids[] __devinitdata = {
40 .vendor = PCI_VENDOR_ID_RICOH,
41 .device = PCI_DEVICE_ID_RICOH_R5C822,
42 .subvendor = PCI_VENDOR_ID_IBM,
43 .subdevice = PCI_ANY_ID,
44 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
45 SDHCI_QUIRK_FORCE_DMA,
49 .vendor = PCI_VENDOR_ID_RICOH,
50 .device = PCI_DEVICE_ID_RICOH_R5C822,
51 .subvendor = PCI_ANY_ID,
52 .subdevice = PCI_ANY_ID,
53 .driver_data = SDHCI_QUIRK_FORCE_DMA,
57 .vendor = PCI_VENDOR_ID_TI,
58 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
59 .subvendor = PCI_ANY_ID,
60 .subdevice = PCI_ANY_ID,
61 .driver_data = SDHCI_QUIRK_FORCE_DMA,
64 { /* Generic SD host controller */
65 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
68 { /* end: all zeroes */ },
71 MODULE_DEVICE_TABLE(pci, pci_ids);
73 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
74 static void sdhci_finish_data(struct sdhci_host *);
76 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
77 static void sdhci_finish_command(struct sdhci_host *);
79 static void sdhci_dumpregs(struct sdhci_host *host)
81 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
83 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
84 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
85 readw(host->ioaddr + SDHCI_HOST_VERSION));
86 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
87 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
88 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
89 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
90 readl(host->ioaddr + SDHCI_ARGUMENT),
91 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
92 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
93 readl(host->ioaddr + SDHCI_PRESENT_STATE),
94 readb(host->ioaddr + SDHCI_HOST_CONTROL));
95 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
96 readb(host->ioaddr + SDHCI_POWER_CONTROL),
97 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
98 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
99 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
100 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
101 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
102 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
103 readl(host->ioaddr + SDHCI_INT_STATUS));
104 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
105 readl(host->ioaddr + SDHCI_INT_ENABLE),
106 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
107 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
108 readw(host->ioaddr + SDHCI_ACMD12_ERR),
109 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
110 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
111 readl(host->ioaddr + SDHCI_CAPABILITIES),
112 readl(host->ioaddr + SDHCI_MAX_CURRENT));
114 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
117 /*****************************************************************************\
119 * Low level functions *
121 \*****************************************************************************/
123 static void sdhci_reset(struct sdhci_host *host, u8 mask)
125 unsigned long timeout;
127 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
129 if (mask & SDHCI_RESET_ALL)
132 /* Wait max 100 ms */
135 /* hw clears the bit when it's done */
136 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
138 printk(KERN_ERR "%s: Reset 0x%x never completed. "
139 "Please report this to " BUGMAIL ".\n",
140 mmc_hostname(host->mmc), (int)mask);
141 sdhci_dumpregs(host);
149 static void sdhci_init(struct sdhci_host *host)
153 sdhci_reset(host, SDHCI_RESET_ALL);
155 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
156 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
157 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
158 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
159 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
160 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
162 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
163 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
166 static void sdhci_activate_led(struct sdhci_host *host)
170 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
171 ctrl |= SDHCI_CTRL_LED;
172 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
175 static void sdhci_deactivate_led(struct sdhci_host *host)
179 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
180 ctrl &= ~SDHCI_CTRL_LED;
181 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
184 /*****************************************************************************\
188 \*****************************************************************************/
190 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
192 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
193 return host->mapped_sg + host->cur_sg->offset;
196 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
198 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
201 static inline int sdhci_next_sg(struct sdhci_host* host)
204 * Skip to next SG entry.
212 if (host->num_sg > 0) {
214 host->remain = host->cur_sg->length;
220 static void sdhci_read_block_pio(struct sdhci_host *host)
222 int blksize, chunk_remain;
227 DBG("PIO reading\n");
229 blksize = host->data->blksz;
233 buffer = sdhci_kmap_sg(host) + host->offset;
236 if (chunk_remain == 0) {
237 data = readl(host->ioaddr + SDHCI_BUFFER);
238 chunk_remain = min(blksize, 4);
241 size = min(host->size, host->remain);
242 size = min(size, chunk_remain);
244 chunk_remain -= size;
246 host->offset += size;
247 host->remain -= size;
250 *buffer = data & 0xFF;
256 if (host->remain == 0) {
257 sdhci_kunmap_sg(host);
258 if (sdhci_next_sg(host) == 0) {
259 BUG_ON(blksize != 0);
262 buffer = sdhci_kmap_sg(host);
266 sdhci_kunmap_sg(host);
269 static void sdhci_write_block_pio(struct sdhci_host *host)
271 int blksize, chunk_remain;
276 DBG("PIO writing\n");
278 blksize = host->data->blksz;
283 buffer = sdhci_kmap_sg(host) + host->offset;
286 size = min(host->size, host->remain);
287 size = min(size, chunk_remain);
289 chunk_remain -= size;
291 host->offset += size;
292 host->remain -= size;
296 data |= (u32)*buffer << 24;
301 if (chunk_remain == 0) {
302 writel(data, host->ioaddr + SDHCI_BUFFER);
303 chunk_remain = min(blksize, 4);
306 if (host->remain == 0) {
307 sdhci_kunmap_sg(host);
308 if (sdhci_next_sg(host) == 0) {
309 BUG_ON(blksize != 0);
312 buffer = sdhci_kmap_sg(host);
316 sdhci_kunmap_sg(host);
319 static void sdhci_transfer_pio(struct sdhci_host *host)
328 if (host->data->flags & MMC_DATA_READ)
329 mask = SDHCI_DATA_AVAILABLE;
331 mask = SDHCI_SPACE_AVAILABLE;
333 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
334 if (host->data->flags & MMC_DATA_READ)
335 sdhci_read_block_pio(host);
337 sdhci_write_block_pio(host);
342 BUG_ON(host->num_sg == 0);
345 DBG("PIO transfer complete.\n");
348 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
351 unsigned target_timeout, current_timeout;
358 DBG("blksz %04x blks %04x flags %08x\n",
359 data->blksz, data->blocks, data->flags);
360 DBG("tsac %d ms nsac %d clk\n",
361 data->timeout_ns / 1000000, data->timeout_clks);
364 BUG_ON(data->blksz * data->blocks > 524288);
365 BUG_ON(data->blksz > host->max_block);
366 BUG_ON(data->blocks > 65535);
369 target_timeout = data->timeout_ns / 1000 +
370 data->timeout_clks / host->clock;
373 * Figure out needed cycles.
374 * We do this in steps in order to fit inside a 32 bit int.
375 * The first step is the minimum timeout, which will have a
376 * minimum resolution of 6 bits:
377 * (1) 2^13*1000 > 2^22,
378 * (2) host->timeout_clk < 2^16
383 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
384 while (current_timeout < target_timeout) {
386 current_timeout <<= 1;
392 printk(KERN_WARNING "%s: Too large timeout requested!\n",
393 mmc_hostname(host->mmc));
397 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
399 if (host->flags & SDHCI_USE_DMA) {
402 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
403 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
406 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
408 host->size = data->blksz * data->blocks;
410 host->cur_sg = data->sg;
411 host->num_sg = data->sg_len;
414 host->remain = host->cur_sg->length;
417 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
418 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
419 host->ioaddr + SDHCI_BLOCK_SIZE);
420 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
423 static void sdhci_set_transfer_mode(struct sdhci_host *host,
424 struct mmc_data *data)
433 mode = SDHCI_TRNS_BLK_CNT_EN;
434 if (data->blocks > 1)
435 mode |= SDHCI_TRNS_MULTI;
436 if (data->flags & MMC_DATA_READ)
437 mode |= SDHCI_TRNS_READ;
438 if (host->flags & SDHCI_USE_DMA)
439 mode |= SDHCI_TRNS_DMA;
441 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
444 static void sdhci_finish_data(struct sdhci_host *host)
446 struct mmc_data *data;
454 if (host->flags & SDHCI_USE_DMA) {
455 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
456 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
460 * Controller doesn't count down when in single block mode.
462 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
465 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
466 data->bytes_xfered = data->blksz * (data->blocks - blocks);
468 if ((data->error == MMC_ERR_NONE) && blocks) {
469 printk(KERN_ERR "%s: Controller signalled completion even "
470 "though there were blocks left. Please report this "
471 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
472 data->error = MMC_ERR_FAILED;
473 } else if (host->size != 0) {
474 printk(KERN_ERR "%s: %d bytes were left untransferred. "
475 "Please report this to " BUGMAIL ".\n",
476 mmc_hostname(host->mmc), host->size);
477 data->error = MMC_ERR_FAILED;
480 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
484 * The controller needs a reset of internal state machines
485 * upon error conditions.
487 if (data->error != MMC_ERR_NONE) {
488 sdhci_reset(host, SDHCI_RESET_CMD);
489 sdhci_reset(host, SDHCI_RESET_DATA);
492 sdhci_send_command(host, data->stop);
494 tasklet_schedule(&host->finish_tasklet);
497 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
501 unsigned long timeout;
505 DBG("Sending cmd (%x)\n", cmd->opcode);
510 mask = SDHCI_CMD_INHIBIT;
511 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
512 mask |= SDHCI_DATA_INHIBIT;
514 /* We shouldn't wait for data inihibit for stop commands, even
515 though they might use busy signaling */
516 if (host->mrq->data && (cmd == host->mrq->data->stop))
517 mask &= ~SDHCI_DATA_INHIBIT;
519 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
521 printk(KERN_ERR "%s: Controller never released "
522 "inhibit bit(s). Please report this to "
523 BUGMAIL ".\n", mmc_hostname(host->mmc));
524 sdhci_dumpregs(host);
525 cmd->error = MMC_ERR_FAILED;
526 tasklet_schedule(&host->finish_tasklet);
533 mod_timer(&host->timer, jiffies + 10 * HZ);
537 sdhci_prepare_data(host, cmd->data);
539 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
541 sdhci_set_transfer_mode(host, cmd->data);
543 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
544 printk(KERN_ERR "%s: Unsupported response type! "
545 "Please report this to " BUGMAIL ".\n",
546 mmc_hostname(host->mmc));
547 cmd->error = MMC_ERR_INVALID;
548 tasklet_schedule(&host->finish_tasklet);
552 if (!(cmd->flags & MMC_RSP_PRESENT))
553 flags = SDHCI_CMD_RESP_NONE;
554 else if (cmd->flags & MMC_RSP_136)
555 flags = SDHCI_CMD_RESP_LONG;
556 else if (cmd->flags & MMC_RSP_BUSY)
557 flags = SDHCI_CMD_RESP_SHORT_BUSY;
559 flags = SDHCI_CMD_RESP_SHORT;
561 if (cmd->flags & MMC_RSP_CRC)
562 flags |= SDHCI_CMD_CRC;
563 if (cmd->flags & MMC_RSP_OPCODE)
564 flags |= SDHCI_CMD_INDEX;
566 flags |= SDHCI_CMD_DATA;
568 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
569 host->ioaddr + SDHCI_COMMAND);
572 static void sdhci_finish_command(struct sdhci_host *host)
576 BUG_ON(host->cmd == NULL);
578 if (host->cmd->flags & MMC_RSP_PRESENT) {
579 if (host->cmd->flags & MMC_RSP_136) {
580 /* CRC is stripped so we need to do some shifting. */
581 for (i = 0;i < 4;i++) {
582 host->cmd->resp[i] = readl(host->ioaddr +
583 SDHCI_RESPONSE + (3-i)*4) << 8;
585 host->cmd->resp[i] |=
587 SDHCI_RESPONSE + (3-i)*4-1);
590 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
594 host->cmd->error = MMC_ERR_NONE;
596 DBG("Ending cmd (%x)\n", host->cmd->opcode);
599 host->data = host->cmd->data;
601 tasklet_schedule(&host->finish_tasklet);
606 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
610 unsigned long timeout;
612 if (clock == host->clock)
615 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
620 for (div = 1;div < 256;div *= 2) {
621 if ((host->max_clk / div) <= clock)
626 clk = div << SDHCI_DIVIDER_SHIFT;
627 clk |= SDHCI_CLOCK_INT_EN;
628 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
632 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
633 & SDHCI_CLOCK_INT_STABLE)) {
635 printk(KERN_ERR "%s: Internal clock never stabilised. "
636 "Please report this to " BUGMAIL ".\n",
637 mmc_hostname(host->mmc));
638 sdhci_dumpregs(host);
645 clk |= SDHCI_CLOCK_CARD_EN;
646 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
652 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
656 if (host->power == power)
659 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
661 if (power == (unsigned short)-1)
664 pwr = SDHCI_POWER_ON;
670 pwr |= SDHCI_POWER_180;
675 pwr |= SDHCI_POWER_300;
680 pwr |= SDHCI_POWER_330;
686 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
692 /*****************************************************************************\
696 \*****************************************************************************/
698 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
700 struct sdhci_host *host;
703 host = mmc_priv(mmc);
705 spin_lock_irqsave(&host->lock, flags);
707 WARN_ON(host->mrq != NULL);
709 sdhci_activate_led(host);
713 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
714 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
715 tasklet_schedule(&host->finish_tasklet);
717 sdhci_send_command(host, mrq->cmd);
719 spin_unlock_irqrestore(&host->lock, flags);
722 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
724 struct sdhci_host *host;
728 host = mmc_priv(mmc);
730 spin_lock_irqsave(&host->lock, flags);
733 * Reset the chip on each power off.
734 * Should clear out any weird states.
736 if (ios->power_mode == MMC_POWER_OFF) {
737 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
741 sdhci_set_clock(host, ios->clock);
743 if (ios->power_mode == MMC_POWER_OFF)
744 sdhci_set_power(host, -1);
746 sdhci_set_power(host, ios->vdd);
748 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
749 if (ios->bus_width == MMC_BUS_WIDTH_4)
750 ctrl |= SDHCI_CTRL_4BITBUS;
752 ctrl &= ~SDHCI_CTRL_4BITBUS;
753 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
755 spin_unlock_irqrestore(&host->lock, flags);
758 static int sdhci_get_ro(struct mmc_host *mmc)
760 struct sdhci_host *host;
764 host = mmc_priv(mmc);
766 spin_lock_irqsave(&host->lock, flags);
768 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
770 spin_unlock_irqrestore(&host->lock, flags);
772 return !(present & SDHCI_WRITE_PROTECT);
775 static struct mmc_host_ops sdhci_ops = {
776 .request = sdhci_request,
777 .set_ios = sdhci_set_ios,
778 .get_ro = sdhci_get_ro,
781 /*****************************************************************************\
785 \*****************************************************************************/
787 static void sdhci_tasklet_card(unsigned long param)
789 struct sdhci_host *host;
792 host = (struct sdhci_host*)param;
794 spin_lock_irqsave(&host->lock, flags);
796 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
798 printk(KERN_ERR "%s: Card removed during transfer!\n",
799 mmc_hostname(host->mmc));
800 printk(KERN_ERR "%s: Resetting controller.\n",
801 mmc_hostname(host->mmc));
803 sdhci_reset(host, SDHCI_RESET_CMD);
804 sdhci_reset(host, SDHCI_RESET_DATA);
806 host->mrq->cmd->error = MMC_ERR_FAILED;
807 tasklet_schedule(&host->finish_tasklet);
811 spin_unlock_irqrestore(&host->lock, flags);
813 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
816 static void sdhci_tasklet_finish(unsigned long param)
818 struct sdhci_host *host;
820 struct mmc_request *mrq;
822 host = (struct sdhci_host*)param;
824 spin_lock_irqsave(&host->lock, flags);
826 del_timer(&host->timer);
830 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
833 * The controller needs a reset of internal state machines
834 * upon error conditions.
836 if ((mrq->cmd->error != MMC_ERR_NONE) ||
837 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
838 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
840 /* Some controllers need this kick or reset won't work here */
841 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
844 /* This is to force an update */
847 sdhci_set_clock(host, clock);
850 /* Spec says we should do both at the same time, but Ricoh
851 controllers do not like that. */
852 sdhci_reset(host, SDHCI_RESET_CMD);
853 sdhci_reset(host, SDHCI_RESET_DATA);
860 sdhci_deactivate_led(host);
862 spin_unlock_irqrestore(&host->lock, flags);
864 mmc_request_done(host->mmc, mrq);
867 static void sdhci_timeout_timer(unsigned long data)
869 struct sdhci_host *host;
872 host = (struct sdhci_host*)data;
874 spin_lock_irqsave(&host->lock, flags);
877 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
878 "Please report this to " BUGMAIL ".\n",
879 mmc_hostname(host->mmc));
880 sdhci_dumpregs(host);
883 host->data->error = MMC_ERR_TIMEOUT;
884 sdhci_finish_data(host);
887 host->cmd->error = MMC_ERR_TIMEOUT;
889 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
891 tasklet_schedule(&host->finish_tasklet);
895 spin_unlock_irqrestore(&host->lock, flags);
898 /*****************************************************************************\
900 * Interrupt handling *
902 \*****************************************************************************/
904 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
906 BUG_ON(intmask == 0);
909 printk(KERN_ERR "%s: Got command interrupt even though no "
910 "command operation was in progress.\n",
911 mmc_hostname(host->mmc));
912 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
913 mmc_hostname(host->mmc));
914 sdhci_dumpregs(host);
918 if (intmask & SDHCI_INT_RESPONSE)
919 sdhci_finish_command(host);
921 if (intmask & SDHCI_INT_TIMEOUT)
922 host->cmd->error = MMC_ERR_TIMEOUT;
923 else if (intmask & SDHCI_INT_CRC)
924 host->cmd->error = MMC_ERR_BADCRC;
925 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
926 host->cmd->error = MMC_ERR_FAILED;
928 host->cmd->error = MMC_ERR_INVALID;
930 tasklet_schedule(&host->finish_tasklet);
934 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
936 BUG_ON(intmask == 0);
940 * A data end interrupt is sent together with the response
941 * for the stop command.
943 if (intmask & SDHCI_INT_DATA_END)
946 printk(KERN_ERR "%s: Got data interrupt even though no "
947 "data operation was in progress.\n",
948 mmc_hostname(host->mmc));
949 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
950 mmc_hostname(host->mmc));
951 sdhci_dumpregs(host);
956 if (intmask & SDHCI_INT_DATA_TIMEOUT)
957 host->data->error = MMC_ERR_TIMEOUT;
958 else if (intmask & SDHCI_INT_DATA_CRC)
959 host->data->error = MMC_ERR_BADCRC;
960 else if (intmask & SDHCI_INT_DATA_END_BIT)
961 host->data->error = MMC_ERR_FAILED;
963 if (host->data->error != MMC_ERR_NONE)
964 sdhci_finish_data(host);
966 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
967 sdhci_transfer_pio(host);
969 if (intmask & SDHCI_INT_DATA_END)
970 sdhci_finish_data(host);
974 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
977 struct sdhci_host* host = dev_id;
980 spin_lock(&host->lock);
982 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
989 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
991 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
992 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
993 host->ioaddr + SDHCI_INT_STATUS);
994 tasklet_schedule(&host->card_tasklet);
997 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
999 if (intmask & SDHCI_INT_CMD_MASK) {
1000 writel(intmask & SDHCI_INT_CMD_MASK,
1001 host->ioaddr + SDHCI_INT_STATUS);
1002 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1005 if (intmask & SDHCI_INT_DATA_MASK) {
1006 writel(intmask & SDHCI_INT_DATA_MASK,
1007 host->ioaddr + SDHCI_INT_STATUS);
1008 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1011 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1013 if (intmask & SDHCI_INT_BUS_POWER) {
1014 printk(KERN_ERR "%s: Card is consuming too much power!\n",
1015 mmc_hostname(host->mmc));
1016 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
1019 intmask &= SDHCI_INT_BUS_POWER;
1022 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
1023 "report this to " BUGMAIL ".\n",
1024 mmc_hostname(host->mmc), intmask);
1025 sdhci_dumpregs(host);
1027 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
1030 result = IRQ_HANDLED;
1033 spin_unlock(&host->lock);
1038 /*****************************************************************************\
1042 \*****************************************************************************/
1046 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1048 struct sdhci_chip *chip;
1051 chip = pci_get_drvdata(pdev);
1055 DBG("Suspending...\n");
1057 for (i = 0;i < chip->num_slots;i++) {
1058 if (!chip->hosts[i])
1060 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1062 for (i--;i >= 0;i--)
1063 mmc_resume_host(chip->hosts[i]->mmc);
1068 pci_save_state(pdev);
1069 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1070 pci_disable_device(pdev);
1071 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1076 static int sdhci_resume (struct pci_dev *pdev)
1078 struct sdhci_chip *chip;
1081 chip = pci_get_drvdata(pdev);
1085 DBG("Resuming...\n");
1087 pci_set_power_state(pdev, PCI_D0);
1088 pci_restore_state(pdev);
1089 pci_enable_device(pdev);
1091 for (i = 0;i < chip->num_slots;i++) {
1092 if (!chip->hosts[i])
1094 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1095 pci_set_master(pdev);
1096 sdhci_init(chip->hosts[i]);
1097 ret = mmc_resume_host(chip->hosts[i]->mmc);
1105 #else /* CONFIG_PM */
1107 #define sdhci_suspend NULL
1108 #define sdhci_resume NULL
1110 #endif /* CONFIG_PM */
1112 /*****************************************************************************\
1114 * Device probing/removal *
1116 \*****************************************************************************/
1118 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1121 unsigned int version;
1122 struct sdhci_chip *chip;
1123 struct mmc_host *mmc;
1124 struct sdhci_host *host;
1129 chip = pci_get_drvdata(pdev);
1132 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1136 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1138 if (first_bar > 5) {
1139 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1143 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1144 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1148 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1149 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1153 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1154 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1158 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1159 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1163 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1167 host = mmc_priv(mmc);
1170 host->bar = first_bar + slot;
1172 host->addr = pci_resource_start(pdev, host->bar);
1173 host->irq = pdev->irq;
1175 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1177 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1179 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1183 host->ioaddr = ioremap_nocache(host->addr,
1184 pci_resource_len(pdev, host->bar));
1185 if (!host->ioaddr) {
1190 sdhci_reset(host, SDHCI_RESET_ALL);
1192 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1193 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1195 printk(KERN_ERR "%s: Unknown controller version (%d). "
1196 "You may experience problems.\n", host->slot_descr,
1200 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1203 DBG("DMA forced off\n");
1204 else if (debug_forcedma) {
1205 DBG("DMA forced on\n");
1206 host->flags |= SDHCI_USE_DMA;
1207 } else if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1208 host->flags |= SDHCI_USE_DMA;
1209 else if ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA)
1210 DBG("Controller doesn't have DMA interface\n");
1211 else if (!(caps & SDHCI_CAN_DO_DMA))
1212 DBG("Controller doesn't have DMA capability\n");
1214 host->flags |= SDHCI_USE_DMA;
1216 if (host->flags & SDHCI_USE_DMA) {
1217 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1218 printk(KERN_WARNING "%s: No suitable DMA available. "
1219 "Falling back to PIO.\n", host->slot_descr);
1220 host->flags &= ~SDHCI_USE_DMA;
1224 if (host->flags & SDHCI_USE_DMA)
1225 pci_set_master(pdev);
1226 else /* XXX: Hack to get MMC layer to avoid highmem */
1230 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1231 if (host->max_clk == 0) {
1232 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1233 "frequency.\n", host->slot_descr);
1237 host->max_clk *= 1000000;
1240 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1241 if (host->timeout_clk == 0) {
1242 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1243 "frequency.\n", host->slot_descr);
1247 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1248 host->timeout_clk *= 1000;
1250 host->max_block = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1251 if (host->max_block >= 3) {
1252 printk(KERN_ERR "%s: Invalid maximum block size.\n",
1257 host->max_block = 512 << host->max_block;
1260 * Set host parameters.
1262 mmc->ops = &sdhci_ops;
1263 mmc->f_min = host->max_clk / 256;
1264 mmc->f_max = host->max_clk;
1265 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_BYTEBLOCK;
1268 if (caps & SDHCI_CAN_VDD_330)
1269 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1270 else if (caps & SDHCI_CAN_VDD_300)
1271 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1272 else if (caps & SDHCI_CAN_VDD_180)
1273 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1275 if (mmc->ocr_avail == 0) {
1276 printk(KERN_ERR "%s: Hardware doesn't report any "
1277 "support voltages.\n", host->slot_descr);
1282 spin_lock_init(&host->lock);
1285 * Maximum number of segments. Hardware cannot do scatter lists.
1287 if (host->flags & SDHCI_USE_DMA)
1288 mmc->max_hw_segs = 1;
1290 mmc->max_hw_segs = 16;
1291 mmc->max_phys_segs = 16;
1294 * Maximum number of sectors in one transfer. Limited by DMA boundary
1295 * size (512KiB), which means (512 KiB/512=) 1024 entries.
1297 mmc->max_sectors = 1024;
1300 * Maximum segment size. Could be one segment with the maximum number
1303 mmc->max_seg_size = mmc->max_sectors * 512;
1308 tasklet_init(&host->card_tasklet,
1309 sdhci_tasklet_card, (unsigned long)host);
1310 tasklet_init(&host->finish_tasklet,
1311 sdhci_tasklet_finish, (unsigned long)host);
1313 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1315 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1316 host->slot_descr, host);
1322 #ifdef CONFIG_MMC_DEBUG
1323 sdhci_dumpregs(host);
1327 chip->hosts[slot] = host;
1331 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1332 host->addr, host->irq,
1333 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1338 tasklet_kill(&host->card_tasklet);
1339 tasklet_kill(&host->finish_tasklet);
1341 iounmap(host->ioaddr);
1343 pci_release_region(pdev, host->bar);
1350 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1352 struct sdhci_chip *chip;
1353 struct mmc_host *mmc;
1354 struct sdhci_host *host;
1356 chip = pci_get_drvdata(pdev);
1357 host = chip->hosts[slot];
1360 chip->hosts[slot] = NULL;
1362 mmc_remove_host(mmc);
1364 sdhci_reset(host, SDHCI_RESET_ALL);
1366 free_irq(host->irq, host);
1368 del_timer_sync(&host->timer);
1370 tasklet_kill(&host->card_tasklet);
1371 tasklet_kill(&host->finish_tasklet);
1373 iounmap(host->ioaddr);
1375 pci_release_region(pdev, host->bar);
1380 static int __devinit sdhci_probe(struct pci_dev *pdev,
1381 const struct pci_device_id *ent)
1385 struct sdhci_chip *chip;
1387 BUG_ON(pdev == NULL);
1388 BUG_ON(ent == NULL);
1390 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1392 printk(KERN_INFO DRIVER_NAME
1393 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1394 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1397 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1401 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1402 DBG("found %d slot(s)\n", slots);
1406 ret = pci_enable_device(pdev);
1410 chip = kzalloc(sizeof(struct sdhci_chip) +
1411 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1418 chip->quirks = ent->driver_data;
1421 chip->quirks = debug_quirks;
1423 chip->num_slots = slots;
1424 pci_set_drvdata(pdev, chip);
1426 for (i = 0;i < slots;i++) {
1427 ret = sdhci_probe_slot(pdev, i);
1429 for (i--;i >= 0;i--)
1430 sdhci_remove_slot(pdev, i);
1438 pci_set_drvdata(pdev, NULL);
1442 pci_disable_device(pdev);
1446 static void __devexit sdhci_remove(struct pci_dev *pdev)
1449 struct sdhci_chip *chip;
1451 chip = pci_get_drvdata(pdev);
1454 for (i = 0;i < chip->num_slots;i++)
1455 sdhci_remove_slot(pdev, i);
1457 pci_set_drvdata(pdev, NULL);
1462 pci_disable_device(pdev);
1465 static struct pci_driver sdhci_driver = {
1466 .name = DRIVER_NAME,
1467 .id_table = pci_ids,
1468 .probe = sdhci_probe,
1469 .remove = __devexit_p(sdhci_remove),
1470 .suspend = sdhci_suspend,
1471 .resume = sdhci_resume,
1474 /*****************************************************************************\
1476 * Driver init/exit *
1478 \*****************************************************************************/
1480 static int __init sdhci_drv_init(void)
1482 printk(KERN_INFO DRIVER_NAME
1483 ": Secure Digital Host Controller Interface driver, "
1484 DRIVER_VERSION "\n");
1485 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1487 return pci_register_driver(&sdhci_driver);
1490 static void __exit sdhci_drv_exit(void)
1494 pci_unregister_driver(&sdhci_driver);
1497 module_init(sdhci_drv_init);
1498 module_exit(sdhci_drv_exit);
1500 module_param(debug_nodma, uint, 0444);
1501 module_param(debug_forcedma, uint, 0444);
1502 module_param(debug_quirks, uint, 0444);
1504 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1505 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1506 MODULE_VERSION(DRIVER_VERSION);
1507 MODULE_LICENSE("GPL");
1509 MODULE_PARM_DESC(debug_nodma, "Forcefully disable DMA transfers. (default 0)");
1510 MODULE_PARM_DESC(debug_forcedma, "Forcefully enable DMA transfers. (default 0)");
1511 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");