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>
16 #include <linux/scatterlist.h>
18 #include <linux/mmc/host.h>
20 #include <asm/scatterlist.h>
24 #define DRIVER_NAME "sdhci"
26 #define DBG(f, x...) \
27 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
29 static unsigned int debug_quirks = 0;
31 #define SDHCI_QUIRK_CLOCK_BEFORE_RESET (1<<0)
32 #define SDHCI_QUIRK_FORCE_DMA (1<<1)
33 /* Controller doesn't like some resets when there is no card inserted. */
34 #define SDHCI_QUIRK_NO_CARD_NO_RESET (1<<2)
35 #define SDHCI_QUIRK_SINGLE_POWER_WRITE (1<<3)
36 #define SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS (1<<4)
37 #define SDHCI_QUIRK_BROKEN_DMA (1<<5)
39 static const struct pci_device_id pci_ids[] __devinitdata = {
41 .vendor = PCI_VENDOR_ID_RICOH,
42 .device = PCI_DEVICE_ID_RICOH_R5C822,
43 .subvendor = PCI_VENDOR_ID_IBM,
44 .subdevice = PCI_ANY_ID,
45 .driver_data = SDHCI_QUIRK_CLOCK_BEFORE_RESET |
46 SDHCI_QUIRK_FORCE_DMA,
50 .vendor = PCI_VENDOR_ID_RICOH,
51 .device = PCI_DEVICE_ID_RICOH_R5C822,
52 .subvendor = PCI_ANY_ID,
53 .subdevice = PCI_ANY_ID,
54 .driver_data = SDHCI_QUIRK_FORCE_DMA |
55 SDHCI_QUIRK_NO_CARD_NO_RESET,
59 .vendor = PCI_VENDOR_ID_TI,
60 .device = PCI_DEVICE_ID_TI_XX21_XX11_SD,
61 .subvendor = PCI_ANY_ID,
62 .subdevice = PCI_ANY_ID,
63 .driver_data = SDHCI_QUIRK_FORCE_DMA,
67 .vendor = PCI_VENDOR_ID_ENE,
68 .device = PCI_DEVICE_ID_ENE_CB712_SD,
69 .subvendor = PCI_ANY_ID,
70 .subdevice = PCI_ANY_ID,
71 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
72 SDHCI_QUIRK_BROKEN_DMA,
76 .vendor = PCI_VENDOR_ID_ENE,
77 .device = PCI_DEVICE_ID_ENE_CB712_SD_2,
78 .subvendor = PCI_ANY_ID,
79 .subdevice = PCI_ANY_ID,
80 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
81 SDHCI_QUIRK_BROKEN_DMA,
85 .vendor = PCI_VENDOR_ID_ENE,
86 .device = PCI_DEVICE_ID_ENE_CB714_SD,
87 .subvendor = PCI_ANY_ID,
88 .subdevice = PCI_ANY_ID,
89 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
90 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
94 .vendor = PCI_VENDOR_ID_ENE,
95 .device = PCI_DEVICE_ID_ENE_CB714_SD_2,
96 .subvendor = PCI_ANY_ID,
97 .subdevice = PCI_ANY_ID,
98 .driver_data = SDHCI_QUIRK_SINGLE_POWER_WRITE |
99 SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS,
102 { /* Generic SD host controller */
103 PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)
106 { /* end: all zeroes */ },
109 MODULE_DEVICE_TABLE(pci, pci_ids);
111 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
112 static void sdhci_finish_data(struct sdhci_host *);
114 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
115 static void sdhci_finish_command(struct sdhci_host *);
117 static void sdhci_dumpregs(struct sdhci_host *host)
119 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
121 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
122 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
123 readw(host->ioaddr + SDHCI_HOST_VERSION));
124 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
125 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
126 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
127 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
128 readl(host->ioaddr + SDHCI_ARGUMENT),
129 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
130 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
131 readl(host->ioaddr + SDHCI_PRESENT_STATE),
132 readb(host->ioaddr + SDHCI_HOST_CONTROL));
133 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
134 readb(host->ioaddr + SDHCI_POWER_CONTROL),
135 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
136 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
137 readb(host->ioaddr + SDHCI_WAKE_UP_CONTROL),
138 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
139 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
140 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
141 readl(host->ioaddr + SDHCI_INT_STATUS));
142 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
143 readl(host->ioaddr + SDHCI_INT_ENABLE),
144 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
145 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
146 readw(host->ioaddr + SDHCI_ACMD12_ERR),
147 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
148 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
149 readl(host->ioaddr + SDHCI_CAPABILITIES),
150 readl(host->ioaddr + SDHCI_MAX_CURRENT));
152 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
155 /*****************************************************************************\
157 * Low level functions *
159 \*****************************************************************************/
161 static void sdhci_reset(struct sdhci_host *host, u8 mask)
163 unsigned long timeout;
165 if (host->chip->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
166 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) &
171 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
173 if (mask & SDHCI_RESET_ALL)
176 /* Wait max 100 ms */
179 /* hw clears the bit when it's done */
180 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
182 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
183 mmc_hostname(host->mmc), (int)mask);
184 sdhci_dumpregs(host);
192 static void sdhci_init(struct sdhci_host *host)
196 sdhci_reset(host, SDHCI_RESET_ALL);
198 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
199 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
200 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
201 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
202 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
203 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
205 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
206 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
209 static void sdhci_activate_led(struct sdhci_host *host)
213 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
214 ctrl |= SDHCI_CTRL_LED;
215 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
218 static void sdhci_deactivate_led(struct sdhci_host *host)
222 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
223 ctrl &= ~SDHCI_CTRL_LED;
224 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
227 /*****************************************************************************\
231 \*****************************************************************************/
233 static inline char* sdhci_sg_to_buffer(struct sdhci_host* host)
235 return sg_virt(host->cur_sg);
238 static inline int sdhci_next_sg(struct sdhci_host* host)
241 * Skip to next SG entry.
249 if (host->num_sg > 0) {
251 host->remain = host->cur_sg->length;
257 static void sdhci_read_block_pio(struct sdhci_host *host)
259 int blksize, chunk_remain;
264 DBG("PIO reading\n");
266 blksize = host->data->blksz;
270 buffer = sdhci_sg_to_buffer(host) + host->offset;
273 if (chunk_remain == 0) {
274 data = readl(host->ioaddr + SDHCI_BUFFER);
275 chunk_remain = min(blksize, 4);
278 size = min(host->remain, chunk_remain);
280 chunk_remain -= size;
282 host->offset += size;
283 host->remain -= size;
286 *buffer = data & 0xFF;
292 if (host->remain == 0) {
293 if (sdhci_next_sg(host) == 0) {
294 BUG_ON(blksize != 0);
297 buffer = sdhci_sg_to_buffer(host);
302 static void sdhci_write_block_pio(struct sdhci_host *host)
304 int blksize, chunk_remain;
309 DBG("PIO writing\n");
311 blksize = host->data->blksz;
316 buffer = sdhci_sg_to_buffer(host) + host->offset;
319 size = min(host->remain, chunk_remain);
321 chunk_remain -= size;
323 host->offset += size;
324 host->remain -= size;
328 data |= (u32)*buffer << 24;
333 if (chunk_remain == 0) {
334 writel(data, host->ioaddr + SDHCI_BUFFER);
335 chunk_remain = min(blksize, 4);
338 if (host->remain == 0) {
339 if (sdhci_next_sg(host) == 0) {
340 BUG_ON(blksize != 0);
343 buffer = sdhci_sg_to_buffer(host);
348 static void sdhci_transfer_pio(struct sdhci_host *host)
354 if (host->num_sg == 0)
357 if (host->data->flags & MMC_DATA_READ)
358 mask = SDHCI_DATA_AVAILABLE;
360 mask = SDHCI_SPACE_AVAILABLE;
362 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
363 if (host->data->flags & MMC_DATA_READ)
364 sdhci_read_block_pio(host);
366 sdhci_write_block_pio(host);
368 if (host->num_sg == 0)
372 DBG("PIO transfer complete.\n");
375 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
378 unsigned target_timeout, current_timeout;
386 BUG_ON(data->blksz * data->blocks > 524288);
387 BUG_ON(data->blksz > host->mmc->max_blk_size);
388 BUG_ON(data->blocks > 65535);
391 host->data_early = 0;
394 target_timeout = data->timeout_ns / 1000 +
395 data->timeout_clks / host->clock;
398 * Figure out needed cycles.
399 * We do this in steps in order to fit inside a 32 bit int.
400 * The first step is the minimum timeout, which will have a
401 * minimum resolution of 6 bits:
402 * (1) 2^13*1000 > 2^22,
403 * (2) host->timeout_clk < 2^16
408 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
409 while (current_timeout < target_timeout) {
411 current_timeout <<= 1;
417 printk(KERN_WARNING "%s: Too large timeout requested!\n",
418 mmc_hostname(host->mmc));
422 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
424 if (host->flags & SDHCI_USE_DMA) {
427 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
428 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
431 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
433 host->cur_sg = data->sg;
434 host->num_sg = data->sg_len;
437 host->remain = host->cur_sg->length;
440 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
441 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
442 host->ioaddr + SDHCI_BLOCK_SIZE);
443 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
446 static void sdhci_set_transfer_mode(struct sdhci_host *host,
447 struct mmc_data *data)
454 WARN_ON(!host->data);
456 mode = SDHCI_TRNS_BLK_CNT_EN;
457 if (data->blocks > 1)
458 mode |= SDHCI_TRNS_MULTI;
459 if (data->flags & MMC_DATA_READ)
460 mode |= SDHCI_TRNS_READ;
461 if (host->flags & SDHCI_USE_DMA)
462 mode |= SDHCI_TRNS_DMA;
464 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
467 static void sdhci_finish_data(struct sdhci_host *host)
469 struct mmc_data *data;
477 if (host->flags & SDHCI_USE_DMA) {
478 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
479 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
483 * Controller doesn't count down when in single block mode.
485 if (data->blocks == 1)
486 blocks = (data->error == 0) ? 0 : 1;
488 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
489 data->bytes_xfered = data->blksz * (data->blocks - blocks);
491 if (!data->error && blocks) {
492 printk(KERN_ERR "%s: Controller signalled completion even "
493 "though there were blocks left.\n",
494 mmc_hostname(host->mmc));
500 * The controller needs a reset of internal state machines
501 * upon error conditions.
504 sdhci_reset(host, SDHCI_RESET_CMD);
505 sdhci_reset(host, SDHCI_RESET_DATA);
508 sdhci_send_command(host, data->stop);
510 tasklet_schedule(&host->finish_tasklet);
513 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
517 unsigned long timeout;
524 mask = SDHCI_CMD_INHIBIT;
525 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
526 mask |= SDHCI_DATA_INHIBIT;
528 /* We shouldn't wait for data inihibit for stop commands, even
529 though they might use busy signaling */
530 if (host->mrq->data && (cmd == host->mrq->data->stop))
531 mask &= ~SDHCI_DATA_INHIBIT;
533 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
535 printk(KERN_ERR "%s: Controller never released "
536 "inhibit bit(s).\n", mmc_hostname(host->mmc));
537 sdhci_dumpregs(host);
539 tasklet_schedule(&host->finish_tasklet);
546 mod_timer(&host->timer, jiffies + 10 * HZ);
550 sdhci_prepare_data(host, cmd->data);
552 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
554 sdhci_set_transfer_mode(host, cmd->data);
556 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
557 printk(KERN_ERR "%s: Unsupported response type!\n",
558 mmc_hostname(host->mmc));
559 cmd->error = -EINVAL;
560 tasklet_schedule(&host->finish_tasklet);
564 if (!(cmd->flags & MMC_RSP_PRESENT))
565 flags = SDHCI_CMD_RESP_NONE;
566 else if (cmd->flags & MMC_RSP_136)
567 flags = SDHCI_CMD_RESP_LONG;
568 else if (cmd->flags & MMC_RSP_BUSY)
569 flags = SDHCI_CMD_RESP_SHORT_BUSY;
571 flags = SDHCI_CMD_RESP_SHORT;
573 if (cmd->flags & MMC_RSP_CRC)
574 flags |= SDHCI_CMD_CRC;
575 if (cmd->flags & MMC_RSP_OPCODE)
576 flags |= SDHCI_CMD_INDEX;
578 flags |= SDHCI_CMD_DATA;
580 writew(SDHCI_MAKE_CMD(cmd->opcode, flags),
581 host->ioaddr + SDHCI_COMMAND);
584 static void sdhci_finish_command(struct sdhci_host *host)
588 BUG_ON(host->cmd == NULL);
590 if (host->cmd->flags & MMC_RSP_PRESENT) {
591 if (host->cmd->flags & MMC_RSP_136) {
592 /* CRC is stripped so we need to do some shifting. */
593 for (i = 0;i < 4;i++) {
594 host->cmd->resp[i] = readl(host->ioaddr +
595 SDHCI_RESPONSE + (3-i)*4) << 8;
597 host->cmd->resp[i] |=
599 SDHCI_RESPONSE + (3-i)*4-1);
602 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
606 host->cmd->error = 0;
608 if (host->data && host->data_early)
609 sdhci_finish_data(host);
611 if (!host->cmd->data)
612 tasklet_schedule(&host->finish_tasklet);
617 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
621 unsigned long timeout;
623 if (clock == host->clock)
626 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
631 for (div = 1;div < 256;div *= 2) {
632 if ((host->max_clk / div) <= clock)
637 clk = div << SDHCI_DIVIDER_SHIFT;
638 clk |= SDHCI_CLOCK_INT_EN;
639 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
643 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
644 & SDHCI_CLOCK_INT_STABLE)) {
646 printk(KERN_ERR "%s: Internal clock never "
647 "stabilised.\n", mmc_hostname(host->mmc));
648 sdhci_dumpregs(host);
655 clk |= SDHCI_CLOCK_CARD_EN;
656 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
662 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
666 if (host->power == power)
669 if (power == (unsigned short)-1) {
670 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
675 * Spec says that we should clear the power reg before setting
676 * a new value. Some controllers don't seem to like this though.
678 if (!(host->chip->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
679 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
681 pwr = SDHCI_POWER_ON;
683 switch (1 << power) {
684 case MMC_VDD_165_195:
685 pwr |= SDHCI_POWER_180;
689 pwr |= SDHCI_POWER_300;
693 pwr |= SDHCI_POWER_330;
699 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
705 /*****************************************************************************\
709 \*****************************************************************************/
711 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
713 struct sdhci_host *host;
716 host = mmc_priv(mmc);
718 spin_lock_irqsave(&host->lock, flags);
720 WARN_ON(host->mrq != NULL);
722 sdhci_activate_led(host);
726 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
727 host->mrq->cmd->error = -ENOMEDIUM;
728 tasklet_schedule(&host->finish_tasklet);
730 sdhci_send_command(host, mrq->cmd);
733 spin_unlock_irqrestore(&host->lock, flags);
736 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
738 struct sdhci_host *host;
742 host = mmc_priv(mmc);
744 spin_lock_irqsave(&host->lock, flags);
747 * Reset the chip on each power off.
748 * Should clear out any weird states.
750 if (ios->power_mode == MMC_POWER_OFF) {
751 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
755 sdhci_set_clock(host, ios->clock);
757 if (ios->power_mode == MMC_POWER_OFF)
758 sdhci_set_power(host, -1);
760 sdhci_set_power(host, ios->vdd);
762 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
764 if (ios->bus_width == MMC_BUS_WIDTH_4)
765 ctrl |= SDHCI_CTRL_4BITBUS;
767 ctrl &= ~SDHCI_CTRL_4BITBUS;
769 if (ios->timing == MMC_TIMING_SD_HS)
770 ctrl |= SDHCI_CTRL_HISPD;
772 ctrl &= ~SDHCI_CTRL_HISPD;
774 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
777 * Some (ENE) controllers go apeshit on some ios operation,
778 * signalling timeout and CRC errors even on CMD0. Resetting
779 * it on each ios seems to solve the problem.
781 if(host->chip->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
782 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
785 spin_unlock_irqrestore(&host->lock, flags);
788 static int sdhci_get_ro(struct mmc_host *mmc)
790 struct sdhci_host *host;
794 host = mmc_priv(mmc);
796 spin_lock_irqsave(&host->lock, flags);
798 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
800 spin_unlock_irqrestore(&host->lock, flags);
802 return !(present & SDHCI_WRITE_PROTECT);
805 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
807 struct sdhci_host *host;
811 host = mmc_priv(mmc);
813 spin_lock_irqsave(&host->lock, flags);
815 ier = readl(host->ioaddr + SDHCI_INT_ENABLE);
817 ier &= ~SDHCI_INT_CARD_INT;
819 ier |= SDHCI_INT_CARD_INT;
821 writel(ier, host->ioaddr + SDHCI_INT_ENABLE);
822 writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
826 spin_unlock_irqrestore(&host->lock, flags);
829 static const struct mmc_host_ops sdhci_ops = {
830 .request = sdhci_request,
831 .set_ios = sdhci_set_ios,
832 .get_ro = sdhci_get_ro,
833 .enable_sdio_irq = sdhci_enable_sdio_irq,
836 /*****************************************************************************\
840 \*****************************************************************************/
842 static void sdhci_tasklet_card(unsigned long param)
844 struct sdhci_host *host;
847 host = (struct sdhci_host*)param;
849 spin_lock_irqsave(&host->lock, flags);
851 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
853 printk(KERN_ERR "%s: Card removed during transfer!\n",
854 mmc_hostname(host->mmc));
855 printk(KERN_ERR "%s: Resetting controller.\n",
856 mmc_hostname(host->mmc));
858 sdhci_reset(host, SDHCI_RESET_CMD);
859 sdhci_reset(host, SDHCI_RESET_DATA);
861 host->mrq->cmd->error = -ENOMEDIUM;
862 tasklet_schedule(&host->finish_tasklet);
866 spin_unlock_irqrestore(&host->lock, flags);
868 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
871 static void sdhci_tasklet_finish(unsigned long param)
873 struct sdhci_host *host;
875 struct mmc_request *mrq;
877 host = (struct sdhci_host*)param;
879 spin_lock_irqsave(&host->lock, flags);
881 del_timer(&host->timer);
886 * The controller needs a reset of internal state machines
887 * upon error conditions.
889 if (mrq->cmd->error ||
890 (mrq->data && (mrq->data->error ||
891 (mrq->data->stop && mrq->data->stop->error)))) {
893 /* Some controllers need this kick or reset won't work here */
894 if (host->chip->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
897 /* This is to force an update */
900 sdhci_set_clock(host, clock);
903 /* Spec says we should do both at the same time, but Ricoh
904 controllers do not like that. */
905 sdhci_reset(host, SDHCI_RESET_CMD);
906 sdhci_reset(host, SDHCI_RESET_DATA);
913 sdhci_deactivate_led(host);
916 spin_unlock_irqrestore(&host->lock, flags);
918 mmc_request_done(host->mmc, mrq);
921 static void sdhci_timeout_timer(unsigned long data)
923 struct sdhci_host *host;
926 host = (struct sdhci_host*)data;
928 spin_lock_irqsave(&host->lock, flags);
931 printk(KERN_ERR "%s: Timeout waiting for hardware "
932 "interrupt.\n", mmc_hostname(host->mmc));
933 sdhci_dumpregs(host);
936 host->data->error = -ETIMEDOUT;
937 sdhci_finish_data(host);
940 host->cmd->error = -ETIMEDOUT;
942 host->mrq->cmd->error = -ETIMEDOUT;
944 tasklet_schedule(&host->finish_tasklet);
949 spin_unlock_irqrestore(&host->lock, flags);
952 /*****************************************************************************\
954 * Interrupt handling *
956 \*****************************************************************************/
958 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
960 BUG_ON(intmask == 0);
963 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
964 "though no command operation was in progress.\n",
965 mmc_hostname(host->mmc), (unsigned)intmask);
966 sdhci_dumpregs(host);
970 if (intmask & SDHCI_INT_TIMEOUT)
971 host->cmd->error = -ETIMEDOUT;
972 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
974 host->cmd->error = -EILSEQ;
976 if (host->cmd->error)
977 tasklet_schedule(&host->finish_tasklet);
978 else if (intmask & SDHCI_INT_RESPONSE)
979 sdhci_finish_command(host);
982 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
984 BUG_ON(intmask == 0);
988 * A data end interrupt is sent together with the response
989 * for the stop command.
991 if (intmask & SDHCI_INT_DATA_END)
994 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
995 "though no data operation was in progress.\n",
996 mmc_hostname(host->mmc), (unsigned)intmask);
997 sdhci_dumpregs(host);
1002 if (intmask & SDHCI_INT_DATA_TIMEOUT)
1003 host->data->error = -ETIMEDOUT;
1004 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1005 host->data->error = -EILSEQ;
1007 if (host->data->error)
1008 sdhci_finish_data(host);
1010 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1011 sdhci_transfer_pio(host);
1014 * We currently don't do anything fancy with DMA
1015 * boundaries, but as we can't disable the feature
1016 * we need to at least restart the transfer.
1018 if (intmask & SDHCI_INT_DMA_END)
1019 writel(readl(host->ioaddr + SDHCI_DMA_ADDRESS),
1020 host->ioaddr + SDHCI_DMA_ADDRESS);
1022 if (intmask & SDHCI_INT_DATA_END) {
1025 * Data managed to finish before the
1026 * command completed. Make sure we do
1027 * things in the proper order.
1029 host->data_early = 1;
1031 sdhci_finish_data(host);
1037 static irqreturn_t sdhci_irq(int irq, void *dev_id)
1040 struct sdhci_host* host = dev_id;
1044 spin_lock(&host->lock);
1046 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
1048 if (!intmask || intmask == 0xffffffff) {
1053 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
1055 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1056 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
1057 host->ioaddr + SDHCI_INT_STATUS);
1058 tasklet_schedule(&host->card_tasklet);
1061 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1063 if (intmask & SDHCI_INT_CMD_MASK) {
1064 writel(intmask & SDHCI_INT_CMD_MASK,
1065 host->ioaddr + SDHCI_INT_STATUS);
1066 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1069 if (intmask & SDHCI_INT_DATA_MASK) {
1070 writel(intmask & SDHCI_INT_DATA_MASK,
1071 host->ioaddr + SDHCI_INT_STATUS);
1072 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1075 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1077 intmask &= ~SDHCI_INT_ERROR;
1079 if (intmask & SDHCI_INT_BUS_POWER) {
1080 printk(KERN_ERR "%s: Card is consuming too much power!\n",
1081 mmc_hostname(host->mmc));
1082 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
1085 intmask &= ~SDHCI_INT_BUS_POWER;
1087 if (intmask & SDHCI_INT_CARD_INT)
1090 intmask &= ~SDHCI_INT_CARD_INT;
1093 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
1094 mmc_hostname(host->mmc), intmask);
1095 sdhci_dumpregs(host);
1097 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
1100 result = IRQ_HANDLED;
1104 spin_unlock(&host->lock);
1107 * We have to delay this as it calls back into the driver.
1110 mmc_signal_sdio_irq(host->mmc);
1115 /*****************************************************************************\
1119 \*****************************************************************************/
1123 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1125 struct sdhci_chip *chip;
1128 chip = pci_get_drvdata(pdev);
1132 DBG("Suspending...\n");
1134 for (i = 0;i < chip->num_slots;i++) {
1135 if (!chip->hosts[i])
1137 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1139 for (i--;i >= 0;i--)
1140 mmc_resume_host(chip->hosts[i]->mmc);
1145 pci_save_state(pdev);
1146 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1148 for (i = 0;i < chip->num_slots;i++) {
1149 if (!chip->hosts[i])
1151 free_irq(chip->hosts[i]->irq, chip->hosts[i]);
1154 pci_disable_device(pdev);
1155 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1160 static int sdhci_resume (struct pci_dev *pdev)
1162 struct sdhci_chip *chip;
1165 chip = pci_get_drvdata(pdev);
1169 DBG("Resuming...\n");
1171 pci_set_power_state(pdev, PCI_D0);
1172 pci_restore_state(pdev);
1173 ret = pci_enable_device(pdev);
1177 for (i = 0;i < chip->num_slots;i++) {
1178 if (!chip->hosts[i])
1180 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1181 pci_set_master(pdev);
1182 ret = request_irq(chip->hosts[i]->irq, sdhci_irq,
1183 IRQF_SHARED, chip->hosts[i]->slot_descr,
1187 sdhci_init(chip->hosts[i]);
1189 ret = mmc_resume_host(chip->hosts[i]->mmc);
1197 #else /* CONFIG_PM */
1199 #define sdhci_suspend NULL
1200 #define sdhci_resume NULL
1202 #endif /* CONFIG_PM */
1204 /*****************************************************************************\
1206 * Device probing/removal *
1208 \*****************************************************************************/
1210 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1213 unsigned int version;
1214 struct sdhci_chip *chip;
1215 struct mmc_host *mmc;
1216 struct sdhci_host *host;
1221 chip = pci_get_drvdata(pdev);
1224 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1228 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1230 if (first_bar > 5) {
1231 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1235 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1236 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1240 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1241 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. "
1242 "You may experience problems.\n");
1245 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1246 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1250 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1251 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1255 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1259 host = mmc_priv(mmc);
1263 chip->hosts[slot] = host;
1265 host->bar = first_bar + slot;
1267 host->addr = pci_resource_start(pdev, host->bar);
1268 host->irq = pdev->irq;
1270 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1272 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1274 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1278 host->ioaddr = ioremap_nocache(host->addr,
1279 pci_resource_len(pdev, host->bar));
1280 if (!host->ioaddr) {
1285 sdhci_reset(host, SDHCI_RESET_ALL);
1287 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1288 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1290 printk(KERN_ERR "%s: Unknown controller version (%d). "
1291 "You may experience problems.\n", host->slot_descr,
1295 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1297 if (chip->quirks & SDHCI_QUIRK_FORCE_DMA)
1298 host->flags |= SDHCI_USE_DMA;
1299 else if (!(caps & SDHCI_CAN_DO_DMA))
1300 DBG("Controller doesn't have DMA capability\n");
1302 host->flags |= SDHCI_USE_DMA;
1304 if ((chip->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1305 (host->flags & SDHCI_USE_DMA)) {
1306 DBG("Disabling DMA as it is marked broken");
1307 host->flags &= ~SDHCI_USE_DMA;
1310 if (((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA) &&
1311 (host->flags & SDHCI_USE_DMA)) {
1312 printk(KERN_WARNING "%s: Will use DMA "
1313 "mode even though HW doesn't fully "
1314 "claim to support it.\n", host->slot_descr);
1317 if (host->flags & SDHCI_USE_DMA) {
1318 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1319 printk(KERN_WARNING "%s: No suitable DMA available. "
1320 "Falling back to PIO.\n", host->slot_descr);
1321 host->flags &= ~SDHCI_USE_DMA;
1325 if (host->flags & SDHCI_USE_DMA)
1326 pci_set_master(pdev);
1327 else /* XXX: Hack to get MMC layer to avoid highmem */
1331 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1332 if (host->max_clk == 0) {
1333 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1334 "frequency.\n", host->slot_descr);
1338 host->max_clk *= 1000000;
1341 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1342 if (host->timeout_clk == 0) {
1343 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1344 "frequency.\n", host->slot_descr);
1348 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1349 host->timeout_clk *= 1000;
1352 * Set host parameters.
1354 mmc->ops = &sdhci_ops;
1355 mmc->f_min = host->max_clk / 256;
1356 mmc->f_max = host->max_clk;
1357 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_MULTIWRITE | MMC_CAP_SDIO_IRQ;
1359 if (caps & SDHCI_CAN_DO_HISPD)
1360 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1363 if (caps & SDHCI_CAN_VDD_330)
1364 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1365 if (caps & SDHCI_CAN_VDD_300)
1366 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1367 if (caps & SDHCI_CAN_VDD_180)
1368 mmc->ocr_avail |= MMC_VDD_165_195;
1370 if (mmc->ocr_avail == 0) {
1371 printk(KERN_ERR "%s: Hardware doesn't report any "
1372 "support voltages.\n", host->slot_descr);
1377 spin_lock_init(&host->lock);
1380 * Maximum number of segments. Hardware cannot do scatter lists.
1382 if (host->flags & SDHCI_USE_DMA)
1383 mmc->max_hw_segs = 1;
1385 mmc->max_hw_segs = 16;
1386 mmc->max_phys_segs = 16;
1389 * Maximum number of sectors in one transfer. Limited by DMA boundary
1392 mmc->max_req_size = 524288;
1395 * Maximum segment size. Could be one segment with the maximum number
1398 mmc->max_seg_size = mmc->max_req_size;
1401 * Maximum block size. This varies from controller to controller and
1402 * is specified in the capabilities register.
1404 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1405 if (mmc->max_blk_size >= 3) {
1406 printk(KERN_WARNING "%s: Invalid maximum block size, assuming 512\n",
1408 mmc->max_blk_size = 512;
1410 mmc->max_blk_size = 512 << mmc->max_blk_size;
1413 * Maximum block count.
1415 mmc->max_blk_count = 65535;
1420 tasklet_init(&host->card_tasklet,
1421 sdhci_tasklet_card, (unsigned long)host);
1422 tasklet_init(&host->finish_tasklet,
1423 sdhci_tasklet_finish, (unsigned long)host);
1425 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
1427 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1428 host->slot_descr, host);
1434 #ifdef CONFIG_MMC_DEBUG
1435 sdhci_dumpregs(host);
1442 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1443 host->addr, host->irq,
1444 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1449 tasklet_kill(&host->card_tasklet);
1450 tasklet_kill(&host->finish_tasklet);
1452 iounmap(host->ioaddr);
1454 pci_release_region(pdev, host->bar);
1461 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1463 struct sdhci_chip *chip;
1464 struct mmc_host *mmc;
1465 struct sdhci_host *host;
1467 chip = pci_get_drvdata(pdev);
1468 host = chip->hosts[slot];
1471 chip->hosts[slot] = NULL;
1473 mmc_remove_host(mmc);
1475 sdhci_reset(host, SDHCI_RESET_ALL);
1477 free_irq(host->irq, host);
1479 del_timer_sync(&host->timer);
1481 tasklet_kill(&host->card_tasklet);
1482 tasklet_kill(&host->finish_tasklet);
1484 iounmap(host->ioaddr);
1486 pci_release_region(pdev, host->bar);
1491 static int __devinit sdhci_probe(struct pci_dev *pdev,
1492 const struct pci_device_id *ent)
1496 struct sdhci_chip *chip;
1498 BUG_ON(pdev == NULL);
1499 BUG_ON(ent == NULL);
1501 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1503 printk(KERN_INFO DRIVER_NAME
1504 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1505 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1508 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1512 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1513 DBG("found %d slot(s)\n", slots);
1517 ret = pci_enable_device(pdev);
1521 chip = kzalloc(sizeof(struct sdhci_chip) +
1522 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1529 chip->quirks = ent->driver_data;
1532 chip->quirks = debug_quirks;
1534 chip->num_slots = slots;
1535 pci_set_drvdata(pdev, chip);
1537 for (i = 0;i < slots;i++) {
1538 ret = sdhci_probe_slot(pdev, i);
1540 for (i--;i >= 0;i--)
1541 sdhci_remove_slot(pdev, i);
1549 pci_set_drvdata(pdev, NULL);
1553 pci_disable_device(pdev);
1557 static void __devexit sdhci_remove(struct pci_dev *pdev)
1560 struct sdhci_chip *chip;
1562 chip = pci_get_drvdata(pdev);
1565 for (i = 0;i < chip->num_slots;i++)
1566 sdhci_remove_slot(pdev, i);
1568 pci_set_drvdata(pdev, NULL);
1573 pci_disable_device(pdev);
1576 static struct pci_driver sdhci_driver = {
1577 .name = DRIVER_NAME,
1578 .id_table = pci_ids,
1579 .probe = sdhci_probe,
1580 .remove = __devexit_p(sdhci_remove),
1581 .suspend = sdhci_suspend,
1582 .resume = sdhci_resume,
1585 /*****************************************************************************\
1587 * Driver init/exit *
1589 \*****************************************************************************/
1591 static int __init sdhci_drv_init(void)
1593 printk(KERN_INFO DRIVER_NAME
1594 ": Secure Digital Host Controller Interface driver\n");
1595 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1597 return pci_register_driver(&sdhci_driver);
1600 static void __exit sdhci_drv_exit(void)
1604 pci_unregister_driver(&sdhci_driver);
1607 module_init(sdhci_drv_init);
1608 module_exit(sdhci_drv_exit);
1610 module_param(debug_quirks, uint, 0444);
1612 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1613 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1614 MODULE_LICENSE("GPL");
1616 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");