2 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2007 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 as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/delay.h>
13 #include <linux/highmem.h>
14 #include <linux/pci.h>
15 #include <linux/dma-mapping.h>
17 #include <linux/mmc/host.h>
19 #include <asm/scatterlist.h>
23 #define DRIVER_NAME "sdhci"
25 #define DBG(f, x...) \
26 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
28 static unsigned int debug_quirks = 0;
30 #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
31 #define SDHCI_QUIRK_FORCE_DMA (1<<1)
32 /* Controller doesn't like some resets when there is no card inserted. */
33 #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
34 #define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
35 #define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
36 #define SDHCI_QUIRK_BROKEN_DMA (1<<5)
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 |
54 SDHCI_QUIRK_NO_CARD_NO_RESET,
58 .vendor = PCI_VENDOR_ID_TI,
59 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
60 .subvendor = PCI_ANY_ID,
61 .subdevice = PCI_ANY_ID,
62 .driver_data = SDHCI_QUIRK_FORCE_DMA,
66 .vendor = PCI_VENDOR_ID_ENE,
67 .device = PCI_DEVICE_ID_ENE_CB712_SD,
68 .subvendor = PCI_ANY_ID,
69 .subdevice = PCI_ANY_ID,
70 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
71 SDHCI_QUIRK_BROKEN_DMA,
75 .vendor = PCI_VENDOR_ID_ENE,
76 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
77 .subvendor = PCI_ANY_ID,
78 .subdevice = PCI_ANY_ID,
79 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
80 SDHCI_QUIRK_BROKEN_DMA,
84 .vendor = PCI_VENDOR_ID_ENE,
85 .device = PCI_DEVICE_ID_ENE_CB714_SD,
86 .subvendor = PCI_ANY_ID,
87 .subdevice = PCI_ANY_ID,
88 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
89 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
93 .vendor = PCI_VENDOR_ID_ENE,
94 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
95 .subvendor = PCI_ANY_ID,
96 .subdevice = PCI_ANY_ID,
97 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
98 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
101 { /* Generic SD host controller */
102 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
105 { /* end: all zeroes */ },
108 MODULE_DEVICE_TABLE(pci, pci_ids);
110 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
111 static void sdhci_finish_data(struct sdhci_host *);
113 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
114 static void sdhci_finish_command(struct sdhci_host *);
116 static void sdhci_dumpregs(struct sdhci_host *host)
118 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
120 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
121 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
122 readw(host->ioaddr + SDHCI_HOST_VERSION));
123 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
124 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
125 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
126 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
127 readl(host->ioaddr + SDHCI_ARGUMENT),
128 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
129 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
130 readl(host->ioaddr + SDHCI_PRESENT_STATE),
131 readb(host->ioaddr + SDHCI_HOST_CONTROL));
132 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
133 readb(host->ioaddr + SDHCI_POWER_CONTROL),
134 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
135 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
136 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
137 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
138 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
139 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
140 readl(host->ioaddr + SDHCI_INT_STATUS));
141 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
142 readl(host->ioaddr + SDHCI_INT_ENABLE),
143 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
144 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
145 readw(host->ioaddr + SDHCI_ACMD12_ERR),
146 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
147 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
148 readl(host->ioaddr + SDHCI_CAPABILITIES),
149 readl(host->ioaddr + SDHCI_MAX_CURRENT));
151 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
154 /*****************************************************************************\
156 * Low level functions *
158 \*****************************************************************************/
160 static void sdhci_reset(struct sdhci_host *host, u8 mask)
162 unsigned long timeout;
164 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
165 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
170 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
172 if (mask & SDHCI_RESET_ALL)
175 /* Wait max 100 ms */
178 /* hw clears the bit when it's done */
179 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
181 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
182 mmc_hostname(host->mmc), (int)mask);
183 sdhci_dumpregs(host);
191 static void sdhci_init(struct sdhci_host *host)
195 sdhci_reset(host, SDHCI_RESET_ALL);
197 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
198 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
199 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
200 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
201 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
202 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
204 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
205 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
208 static void sdhci_activate_led(struct sdhci_host *host)
212 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
213 ctrl |= SDHCI_CTRL_LED;
214 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
217 static void sdhci_deactivate_led(struct sdhci_host *host)
221 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
222 ctrl &= ~SDHCI_CTRL_LED;
223 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
226 /*****************************************************************************\
230 \*****************************************************************************/
232 static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
234 return page_address(host->cur_sg->page) + host->cur_sg->offset;
237 static inline int sdhci_next_sg(struct sdhci_host* host)
240 * Skip to next SG entry.
248 if (host->num_sg > 0) {
250 host->remain = host->cur_sg->length;
256 static void sdhci_read_block_pio(struct sdhci_host *host)
258 int blksize, chunk_remain;
263 DBG("PIO reading\n");
265 blksize = host->data->blksz;
269 buffer = sdhci_sg_to_buffer(host) + host->offset;
272 if (chunk_remain == 0) {
273 data = readl(host->ioaddr + SDHCI_BUFFER);
274 chunk_remain = min(blksize, 4);
277 size = min(host->remain, chunk_remain);
279 chunk_remain -= size;
281 host->offset += size;
282 host->remain -= size;
285 *buffer = data & 0xFF;
291 if (host->remain == 0) {
292 if (sdhci_next_sg(host) == 0) {
293 BUG_ON(blksize != 0);
296 buffer = sdhci_sg_to_buffer(host);
301 static void sdhci_write_block_pio(struct sdhci_host *host)
303 int blksize, chunk_remain;
308 DBG("PIO writing\n");
310 blksize = host->data->blksz;
315 buffer = sdhci_sg_to_buffer(host) + host->offset;
318 size = min(host->remain, chunk_remain);
320 chunk_remain -= size;
322 host->offset += size;
323 host->remain -= size;
327 data |= (u32)*buffer << 24;
332 if (chunk_remain == 0) {
333 writel(data, host->ioaddr + SDHCI_BUFFER);
334 chunk_remain = min(blksize, 4);
337 if (host->remain == 0) {
338 if (sdhci_next_sg(host) == 0) {
339 BUG_ON(blksize != 0);
342 buffer = sdhci_sg_to_buffer(host);
347 static void sdhci_transfer_pio(struct sdhci_host *host)
353 if (host->num_sg == 0)
356 if (host->data->flags & MMC_DATA_READ)
357 mask = SDHCI_DATA_AVAILABLE;
359 mask = SDHCI_SPACE_AVAILABLE;
361 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
362 if (host->data->flags & MMC_DATA_READ)
363 sdhci_read_block_pio(host);
365 sdhci_write_block_pio(host);
367 if (host->num_sg == 0)
371 DBG("PIO transfer complete.\n");
374 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
377 unsigned target_timeout, current_timeout;
385 BUG_ON(data->blksz * data->blocks > 524288);
386 BUG_ON(data->blksz > host->mmc->max_blk_size);
387 BUG_ON(data->blocks > 65535);
390 host->data_early = 0;
393 target_timeout = data->timeout_ns / 1000 +
394 data->timeout_clks / host->clock;
397 * Figure out needed cycles.
398 * We do this in steps in order to fit inside a 32 bit int.
399 * The first step is the minimum timeout, which will have a
400 * minimum resolution of 6 bits:
401 * (1) 2^13*1000 > 2^22,
402 * (2) host->timeout_clk < 2^16
407 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
408 while (current_timeout < target_timeout) {
410 current_timeout <<= 1;
416 printk(KERN_WARNING "%s: Too large timeout requested!\n",
417 mmc_hostname(host->mmc));
421 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
423 if (host->flags & SDHCI_USE_DMA) {
426 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
427 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
430 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
432 host->cur_sg = data->sg;
433 host->num_sg = data->sg_len;
436 host->remain = host->cur_sg->length;
439 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
440 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
441 host->ioaddr + SDHCI_BLOCK_SIZE);
442 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
445 static void sdhci_set_transfer_mode(struct sdhci_host *host,
446 struct mmc_data *data)
453 WARN_ON(!host->data);
455 mode = SDHCI_TRNS_BLK_CNT_EN;
456 if (data->blocks > 1)
457 mode |= SDHCI_TRNS_MULTI;
458 if (data->flags & MMC_DATA_READ)
459 mode |= SDHCI_TRNS_READ;
460 if (host->flags & SDHCI_USE_DMA)
461 mode |= SDHCI_TRNS_DMA;
463 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
466 static void sdhci_finish_data(struct sdhci_host *host)
468 struct mmc_data *data;
476 if (host->flags & SDHCI_USE_DMA) {
477 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
478 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
482 * Controller doesn't count down when in single block mode.
484 if (data->blocks == 1)
485 blocks = (data->error == 0) ? 0 : 1;
487 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
488 data->bytes_xfered = data->blksz * (data->blocks - blocks);
490 if (!data->error && blocks) {
491 printk(KERN_ERR "%s: Controller signalled completion even "
492 "though there were blocks left.\n",
493 mmc_hostname(host->mmc));
499 * The controller needs a reset of internal state machines
500 * upon error conditions.
503 sdhci_reset(host, SDHCI_RESET_CMD);
504 sdhci_reset(host, SDHCI_RESET_DATA);
507 sdhci_send_command(host, data->stop);
509 tasklet_schedule(&host->finish_tasklet);
512 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
516 unsigned long timeout;
523 mask = SDHCI_CMD_INHIBIT;
524 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
525 mask |= SDHCI_DATA_INHIBIT;
527 /* We shouldn't wait for data inihibit for stop commands, even
528 though they might use busy signaling */
529 if (host->mrq->data && (cmd == host->mrq->data->stop))
530 mask &= ~SDHCI_DATA_INHIBIT;
532 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
534 printk(KERN_ERR "%s: Controller never released "
535 "inhibit bit(s).\n", mmc_hostname(host->mmc));
536 sdhci_dumpregs(host);
538 tasklet_schedule(&host->finish_tasklet);
545 mod_timer(&host->timer, jiffies + 10 * HZ);
549 sdhci_prepare_data(host, cmd->data);
551 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
553 sdhci_set_transfer_mode(host, cmd->data);
555 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
556 printk(KERN_ERR "%s: Unsupported response type!\n",
557 mmc_hostname(host->mmc));
558 cmd->error = -EINVAL;
559 tasklet_schedule(&host->finish_tasklet);
563 if (!(cmd->flags & MMC_RSP_PRESENT))
564 flags = SDHCI_CMD_RESP_NONE;
565 else if (cmd->flags & MMC_RSP_136)
566 flags = SDHCI_CMD_RESP_LONG;
567 else if (cmd->flags & MMC_RSP_BUSY)
568 flags = SDHCI_CMD_RESP_SHORT_BUSY;
570 flags = SDHCI_CMD_RESP_SHORT;
572 if (cmd->flags & MMC_RSP_CRC)
573 flags |= SDHCI_CMD_CRC;
574 if (cmd->flags & MMC_RSP_OPCODE)
575 flags |= SDHCI_CMD_INDEX;
577 flags |= SDHCI_CMD_DATA;
579 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
580 host->ioaddr + SDHCI_COMMAND);
583 static void sdhci_finish_command(struct sdhci_host *host)
587 BUG_ON(host->cmd == NULL);
589 if (host->cmd->flags & MMC_RSP_PRESENT) {
590 if (host->cmd->flags & MMC_RSP_136) {
591 /* CRC is stripped so we need to do some shifting. */
592 for (i = 0;i < 4;i++) {
593 host->cmd->resp[i] = readl(host->ioaddr +
594 SDHCI_RESPONSE + (3-i)*4) << 8;
596 host->cmd->resp[i] |=
598 SDHCI_RESPONSE + (3-i)*4-1);
601 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
605 host->cmd->error = 0;
607 if (host->data && host->data_early)
608 sdhci_finish_data(host);
610 if (!host->cmd->data)
611 tasklet_schedule(&host->finish_tasklet);
616 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
620 unsigned long timeout;
622 if (clock == host->clock)
625 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
630 for (div = 1;div < 256;div *= 2) {
631 if ((host->max_clk / div) <= clock)
636 clk = div << SDHCI_DIVIDER_SHIFT;
637 clk |= SDHCI_CLOCK_INT_EN;
638 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
642 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
643 & SDHCI_CLOCK_INT_STABLE)) {
645 printk(KERN_ERR "%s: Internal clock never "
646 "stabilised.\n", mmc_hostname(host->mmc));
647 sdhci_dumpregs(host);
654 clk |= SDHCI_CLOCK_CARD_EN;
655 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
661 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
665 if (host->power == power)
668 if (power == (unsigned short)-1) {
669 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
674 * Spec says that we should clear the power reg before setting
675 * a new value. Some controllers don't seem to like this though.
677 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
678 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
680 pwr = SDHCI_POWER_ON;
682 switch (1 << power) {
683 case MMC_VDD_165_195:
684 pwr |= SDHCI_POWER_180;
688 pwr |= SDHCI_POWER_300;
692 pwr |= SDHCI_POWER_330;
698 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
704 /*****************************************************************************\
708 \*****************************************************************************/
710 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
712 struct sdhci_host *host;
715 host = mmc_priv(mmc);
717 spin_lock_irqsave(&host->lock, flags);
719 WARN_ON(host->mrq != NULL);
721 sdhci_activate_led(host);
725 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
726 host->mrq->cmd->error = -ENOMEDIUM;
727 tasklet_schedule(&host->finish_tasklet);
729 sdhci_send_command(host, mrq->cmd);
732 spin_unlock_irqrestore(&host->lock, flags);
735 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
737 struct sdhci_host *host;
741 host = mmc_priv(mmc);
743 spin_lock_irqsave(&host->lock, flags);
746 * Reset the chip on each power off.
747 * Should clear out any weird states.
749 if (ios->power_mode == MMC_POWER_OFF) {
750 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
754 sdhci_set_clock(host, ios->clock);
756 if (ios->power_mode == MMC_POWER_OFF)
757 sdhci_set_power(host, -1);
759 sdhci_set_power(host, ios->vdd);
761 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
763 if (ios->bus_width == MMC_BUS_WIDTH_4)
764 ctrl |= SDHCI_CTRL_4BITBUS;
766 ctrl &= ~SDHCI_CTRL_4BITBUS;
768 if (ios->timing == MMC_TIMING_SD_HS)
769 ctrl |= SDHCI_CTRL_HISPD;
771 ctrl &= ~SDHCI_CTRL_HISPD;
773 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
776 * Some (ENE) controllers go apeshit on some ios operation,
777 * signalling timeout and CRC errors even on CMD0. Resetting
778 * it on each ios seems to solve the problem.
780 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
781 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
784 spin_unlock_irqrestore(&host->lock, flags);
787 static int sdhci_get_ro(struct mmc_host *mmc)
789 struct sdhci_host *host;
793 host = mmc_priv(mmc);
795 spin_lock_irqsave(&host->lock, flags);
797 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
799 spin_unlock_irqrestore(&host->lock, flags);
801 return !(present & SDHCI_WRITE_PROTECT);
804 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
806 struct sdhci_host *host;
810 host = mmc_priv(mmc);
812 spin_lock_irqsave(&host->lock, flags);
814 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
816 ier &= ~SDHCI_INT_CARD_INT;
818 ier |= SDHCI_INT_CARD_INT;
820 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
821 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
825 spin_unlock_irqrestore(&host->lock, flags);
828 static const struct mmc_host_ops sdhci_ops = {
829 .request = sdhci_request,
830 .set_ios = sdhci_set_ios,
831 .get_ro = sdhci_get_ro,
832 .enable_sdio_irq = sdhci_enable_sdio_irq,
835 /*****************************************************************************\
839 \*****************************************************************************/
841 static void sdhci_tasklet_card(unsigned long param)
843 struct sdhci_host *host;
846 host = (struct sdhci_host*)param;
848 spin_lock_irqsave(&host->lock, flags);
850 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
852 printk(KERN_ERR "%s: Card removed during transfer!\n",
853 mmc_hostname(host->mmc));
854 printk(KERN_ERR "%s: Resetting controller.\n",
855 mmc_hostname(host->mmc));
857 sdhci_reset(host, SDHCI_RESET_CMD);
858 sdhci_reset(host, SDHCI_RESET_DATA);
860 host->mrq->cmd->error = -ENOMEDIUM;
861 tasklet_schedule(&host->finish_tasklet);
865 spin_unlock_irqrestore(&host->lock, flags);
867 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
870 static void sdhci_tasklet_finish(unsigned long param)
872 struct sdhci_host *host;
874 struct mmc_request *mrq;
876 host = (struct sdhci_host*)param;
878 spin_lock_irqsave(&host->lock, flags);
880 del_timer(&host->timer);
885 * The controller needs a reset of internal state machines
886 * upon error conditions.
888 if (mrq->cmd->error ||
889 (mrq->data && (mrq->data->error ||
890 (mrq->data->stop && mrq->data->stop->error)))) {
892 /* Some controllers need this kick or reset won't work here */
893 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
896 /* This is to force an update */
899 sdhci_set_clock(host, clock);
902 /* Spec says we should do both at the same time, but Ricoh
903 controllers do not like that. */
904 sdhci_reset(host, SDHCI_RESET_CMD);
905 sdhci_reset(host, SDHCI_RESET_DATA);
912 sdhci_deactivate_led(host);
915 spin_unlock_irqrestore(&host->lock, flags);
917 mmc_request_done(host->mmc, mrq);
920 static void sdhci_timeout_timer(unsigned long data)
922 struct sdhci_host *host;
925 host = (struct sdhci_host*)data;
927 spin_lock_irqsave(&host->lock, flags);
930 printk(KERN_ERR "%s: Timeout waiting for hardware "
931 "interrupt.\n", mmc_hostname(host->mmc));
932 sdhci_dumpregs(host);
935 host->data->error = -ETIMEDOUT;
936 sdhci_finish_data(host);
939 host->cmd->error = -ETIMEDOUT;
941 host->mrq->cmd->error = -ETIMEDOUT;
943 tasklet_schedule(&host->finish_tasklet);
948 spin_unlock_irqrestore(&host->lock, flags);
951 /*****************************************************************************\
953 * Interrupt handling *
955 \*****************************************************************************/
957 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
959 BUG_ON(intmask == 0);
962 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
963 "though no command operation was in progress.\n",
964 mmc_hostname(host->mmc), (unsigned)intmask);
965 sdhci_dumpregs(host);
969 if (intmask & SDHCI_INT_TIMEOUT)
970 host->cmd->error = -ETIMEDOUT;
971 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
973 host->cmd->error = -EILSEQ;
975 if (host->cmd->error)
976 tasklet_schedule(&host->finish_tasklet);
977 else if (intmask & SDHCI_INT_RESPONSE)
978 sdhci_finish_command(host);
981 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
983 BUG_ON(intmask == 0);
987 * A data end interrupt is sent together with the response
988 * for the stop command.
990 if (intmask & SDHCI_INT_DATA_END)
993 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
994 "though no data operation was in progress.\n",
995 mmc_hostname(host->mmc), (unsigned)intmask);
996 sdhci_dumpregs(host);
1001 if (intmask & SDHCI_INT_DATA_TIMEOUT)
1002 host->data->error = -ETIMEDOUT;
1003 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1004 host->data->error = -EILSEQ;
1006 if (host->data->error)
1007 sdhci_finish_data(host);
1009 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1010 sdhci_transfer_pio(host);
1013 * We currently don't do anything fancy with DMA
1014 * boundaries, but as we can't disable the feature
1015 * we need to at least restart the transfer.
1017 if (intmask & SDHCI_INT_DMA_END)
1018 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1019 host->ioaddr + SDHCI_DMA_ADDRESS);
1021 if (intmask & SDHCI_INT_DATA_END) {
1024 * Data managed to finish before the
1025 * command completed. Make sure we do
1026 * things in the proper order.
1028 host->data_early = 1;
1030 sdhci_finish_data(host);
1036 static irqreturn_t sdhci_irq(int irq, void *dev_id)
1039 struct sdhci_host* host = dev_id;
1043 spin_lock(&host->lock);
1045 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1047 if (!intmask || intmask == 0xffffffff) {
1052 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
1054 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1055 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1056 host->ioaddr + SDHCI_INT_STATUS);
1057 tasklet_schedule(&host->card_tasklet);
1060 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1062 if (intmask & SDHCI_INT_CMD_MASK) {
1063 writel(intmask & SDHCI_INT_CMD_MASK,
1064 host->ioaddr + SDHCI_INT_STATUS);
1065 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1068 if (intmask & SDHCI_INT_DATA_MASK) {
1069 writel(intmask & SDHCI_INT_DATA_MASK,
1070 host->ioaddr + SDHCI_INT_STATUS);
1071 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1074 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1076 intmask &= ~SDHCI_INT_ERROR;
1078 if (intmask & SDHCI_INT_BUS_POWER) {
1079 printk(KERN_ERR "%s: Card is consuming too much power!\n",
1080 mmc_hostname(host->mmc));
1081 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
1084 intmask &= ~SDHCI_INT_BUS_POWER;
1086 if (intmask & SDHCI_INT_CARD_INT)
1089 intmask &= ~SDHCI_INT_CARD_INT;
1092 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
1093 mmc_hostname(host->mmc), intmask);
1094 sdhci_dumpregs(host);
1096 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
1099 result = IRQ_HANDLED;
1103 spin_unlock(&host->lock);
1106 * We have to delay this as it calls back into the driver.
1109 mmc_signal_sdio_irq(host->mmc);
1114 /*****************************************************************************\
1118 \*****************************************************************************/
1122 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1124 struct sdhci_chip *chip;
1127 chip = pci_get_drvdata(pdev);
1131 DBG("Suspending...\n");
1133 for (i = 0;i < chip->num_slots;i++) {
1134 if (!chip->hosts[i])
1136 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1138 for (i--;i >= 0;i--)
1139 mmc_resume_host(chip->hosts[i]->mmc);
1144 pci_save_state(pdev);
1145 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1147 for (i = 0;i < chip->num_slots;i++) {
1148 if (!chip->hosts[i])
1150 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1153 pci_disable_device(pdev);
1154 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1159 static int sdhci_resume (struct pci_dev *pdev)
1161 struct sdhci_chip *chip;
1164 chip = pci_get_drvdata(pdev);
1168 DBG("Resuming...\n");
1170 pci_set_power_state(pdev, PCI_D0);
1171 pci_restore_state(pdev);
1172 ret = pci_enable_device(pdev);
1176 for (i = 0;i < chip->num_slots;i++) {
1177 if (!chip->hosts[i])
1179 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1180 pci_set_master(pdev);
1181 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
1182 IRQF_SHARED, chip->hosts[i]->slot_descr,
1186 sdhci_init(chip->hosts[i]);
1188 ret = mmc_resume_host(chip->hosts[i]->mmc);
1196 #else /* CONFIG_PM */
1198 #define sdhci_suspend NULL
1199 #define sdhci_resume NULL
1201 #endif /* CONFIG_PM */
1203 /*****************************************************************************\
1205 * Device probing/removal *
1207 \*****************************************************************************/
1209 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1212 unsigned int version;
1213 struct sdhci_chip *chip;
1214 struct mmc_host *mmc;
1215 struct sdhci_host *host;
1220 chip = pci_get_drvdata(pdev);
1223 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1227 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1229 if (first_bar > 5) {
1230 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1234 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1235 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1239 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1240 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1241 "You may experience problems.\n");
1244 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1245 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1249 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1250 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1254 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1258 host = mmc_priv(mmc);
1262 chip->hosts[slot] = host;
1264 host->bar = first_bar + slot;
1266 host->addr = pci_resource_start(pdev, host->bar);
1267 host->irq = pdev->irq;
1269 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1271 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1273 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1277 host->ioaddr = ioremap_nocache(host->addr,
1278 pci_resource_len(pdev, host->bar));
1279 if (!host->ioaddr) {
1284 sdhci_reset(host, SDHCI_RESET_ALL);
1286 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1287 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1289 printk(KERN_ERR "%s: Unknown controller version (%d). "
1290 "You may experience problems.\n", host->slot_descr,
1294 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1296 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1297 host->flags |= SDHCI_USE_DMA;
1298 else if (!(caps & SDHCI_CAN_DO_DMA))
1299 DBG("Controller doesn't have DMA capability\n");
1301 host->flags |= SDHCI_USE_DMA;
1303 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1304 (host->flags & SDHCI_USE_DMA)) {
1305 DBG("Disabling DMA as it is marked broken");
1306 host->flags &= ~SDHCI_USE_DMA;
1309 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1310 (host->flags & SDHCI_USE_DMA)) {
1311 printk(KERN_WARNING "%s: Will use DMA "
1312 "mode even though HW doesn't fully "
1313 "claim to support it.\n", host->slot_descr);
1316 if (host->flags & SDHCI_USE_DMA) {
1317 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1318 printk(KERN_WARNING "%s: No suitable DMA available. "
1319 "Falling back to PIO.\n", host->slot_descr);
1320 host->flags &= ~SDHCI_USE_DMA;
1324 if (host->flags & SDHCI_USE_DMA)
1325 pci_set_master(pdev);
1326 else /* XXX: Hack to get MMC layer to avoid highmem */
1330 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1331 if (host->max_clk == 0) {
1332 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1333 "frequency.\n", host->slot_descr);
1337 host->max_clk *= 1000000;
1340 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1341 if (host->timeout_clk == 0) {
1342 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1343 "frequency.\n", host->slot_descr);
1347 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1348 host->timeout_clk *= 1000;
1351 * Set host parameters.
1353 mmc->ops = &sdhci_ops;
1354 mmc->f_min = host->max_clk / 256;
1355 mmc->f_max = host->max_clk;
1356 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
1358 if (caps & SDHCI_CAN_DO_HISPD)
1359 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1362 if (caps & SDHCI_CAN_VDD_330)
1363 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1364 if (caps & SDHCI_CAN_VDD_300)
1365 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1366 if (caps & SDHCI_CAN_VDD_180)
1367 mmc->ocr_avail |= MMC_VDD_165_195;
1369 if (mmc->ocr_avail == 0) {
1370 printk(KERN_ERR "%s: Hardware doesn't report any "
1371 "support voltages.\n", host->slot_descr);
1376 spin_lock_init(&host->lock);
1379 * Maximum number of segments. Hardware cannot do scatter lists.
1381 if (host->flags & SDHCI_USE_DMA)
1382 mmc->max_hw_segs = 1;
1384 mmc->max_hw_segs = 16;
1385 mmc->max_phys_segs = 16;
1388 * Maximum number of sectors in one transfer. Limited by DMA boundary
1391 mmc->max_req_size = 524288;
1394 * Maximum segment size. Could be one segment with the maximum number
1397 mmc->max_seg_size = mmc->max_req_size;
1400 * Maximum block size. This varies from controller to controller and
1401 * is specified in the capabilities register.
1403 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1404 if (mmc->max_blk_size >= 3) {
1405 printk(KERN_WARNING "%s: Invalid maximum block size, assuming 512\n",
1407 mmc->max_blk_size = 512;
1409 mmc->max_blk_size = 512 << mmc->max_blk_size;
1412 * Maximum block count.
1414 mmc->max_blk_count = 65535;
1419 tasklet_init(&host->card_tasklet,
1420 sdhci_tasklet_card, (unsigned long)host);
1421 tasklet_init(&host->finish_tasklet,
1422 sdhci_tasklet_finish, (unsigned long)host);
1424 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
1426 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1427 host->slot_descr, host);
1433 #ifdef CONFIG_MMC_DEBUG
1434 sdhci_dumpregs(host);
1441 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1442 host->addr, host->irq,
1443 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1448 tasklet_kill(&host->card_tasklet);
1449 tasklet_kill(&host->finish_tasklet);
1451 iounmap(host->ioaddr);
1453 pci_release_region(pdev, host->bar);
1460 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1462 struct sdhci_chip *chip;
1463 struct mmc_host *mmc;
1464 struct sdhci_host *host;
1466 chip = pci_get_drvdata(pdev);
1467 host = chip->hosts[slot];
1470 chip->hosts[slot] = NULL;
1472 mmc_remove_host(mmc);
1474 sdhci_reset(host, SDHCI_RESET_ALL);
1476 free_irq(host->irq, host);
1478 del_timer_sync(&host->timer);
1480 tasklet_kill(&host->card_tasklet);
1481 tasklet_kill(&host->finish_tasklet);
1483 iounmap(host->ioaddr);
1485 pci_release_region(pdev, host->bar);
1490 static int __devinit sdhci_probe(struct pci_dev *pdev,
1491 const struct pci_device_id *ent)
1495 struct sdhci_chip *chip;
1497 BUG_ON(pdev == NULL);
1498 BUG_ON(ent == NULL);
1500 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1502 printk(KERN_INFO DRIVER_NAME
1503 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1504 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1507 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1511 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1512 DBG("found %d slot(s)\n", slots);
1516 ret = pci_enable_device(pdev);
1520 chip = kzalloc(sizeof(struct sdhci_chip) +
1521 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1528 chip->quirks = ent->driver_data;
1531 chip->quirks = debug_quirks;
1533 chip->num_slots = slots;
1534 pci_set_drvdata(pdev, chip);
1536 for (i = 0;i < slots;i++) {
1537 ret = sdhci_probe_slot(pdev, i);
1539 for (i--;i >= 0;i--)
1540 sdhci_remove_slot(pdev, i);
1548 pci_set_drvdata(pdev, NULL);
1552 pci_disable_device(pdev);
1556 static void __devexit sdhci_remove(struct pci_dev *pdev)
1559 struct sdhci_chip *chip;
1561 chip = pci_get_drvdata(pdev);
1564 for (i = 0;i < chip->num_slots;i++)
1565 sdhci_remove_slot(pdev, i);
1567 pci_set_drvdata(pdev, NULL);
1572 pci_disable_device(pdev);
1575 static struct pci_driver sdhci_driver = {
1576 .name = DRIVER_NAME,
1577 .id_table = pci_ids,
1578 .probe = sdhci_probe,
1579 .remove = __devexit_p(sdhci_remove),
1580 .suspend = sdhci_suspend,
1581 .resume = sdhci_resume,
1584 /*****************************************************************************\
1586 * Driver init/exit *
1588 \*****************************************************************************/
1590 static int __init sdhci_drv_init(void)
1592 printk(KERN_INFO DRIVER_NAME
1593 ": Secure Digital Host Controller Interface driver\n");
1594 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1596 return pci_register_driver(&sdhci_driver);
1599 static void __exit sdhci_drv_exit(void)
1603 pci_unregister_driver(&sdhci_driver);
1606 module_init(sdhci_drv_init);
1607 module_exit(sdhci_drv_exit);
1609 module_param(debug_quirks, uint, 0444);
1611 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1612 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1613 MODULE_LICENSE("GPL");
1615 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");