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.11"
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;
34 static const struct pci_device_id pci_ids[] __devinitdata = {
35 /* handle any SD host controller */
36 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
37 { /* end: all zeroes */ },
40 MODULE_DEVICE_TABLE(pci, pci_ids);
42 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
43 static void sdhci_finish_data(struct sdhci_host *);
45 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46 static void sdhci_finish_command(struct sdhci_host *);
48 static void sdhci_dumpregs(struct sdhci_host *host)
50 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
52 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
53 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
54 readw(host->ioaddr + SDHCI_HOST_VERSION));
55 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
56 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
57 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
58 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
59 readl(host->ioaddr + SDHCI_ARGUMENT),
60 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
61 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
62 readl(host->ioaddr + SDHCI_PRESENT_STATE),
63 readb(host->ioaddr + SDHCI_HOST_CONTROL));
64 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
65 readb(host->ioaddr + SDHCI_POWER_CONTROL),
66 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
67 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
68 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
69 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
70 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
71 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
72 readl(host->ioaddr + SDHCI_INT_STATUS));
73 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
74 readl(host->ioaddr + SDHCI_INT_ENABLE),
75 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
76 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
77 readw(host->ioaddr + SDHCI_ACMD12_ERR),
78 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
79 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
80 readl(host->ioaddr + SDHCI_CAPABILITIES),
81 readl(host->ioaddr + SDHCI_MAX_CURRENT));
83 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
86 /*****************************************************************************\
88 * Low level functions *
90 \*****************************************************************************/
92 static void sdhci_reset(struct sdhci_host *host, u8 mask)
94 unsigned long timeout;
96 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
98 if (mask & SDHCI_RESET_ALL)
101 /* Wait max 100 ms */
104 /* hw clears the bit when it's done */
105 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
107 printk(KERN_ERR "%s: Reset 0x%x never completed. "
108 "Please report this to " BUGMAIL ".\n",
109 mmc_hostname(host->mmc), (int)mask);
110 sdhci_dumpregs(host);
118 static void sdhci_init(struct sdhci_host *host)
122 sdhci_reset(host, SDHCI_RESET_ALL);
124 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
125 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
126 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
127 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
128 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
129 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
131 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
132 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
135 static void sdhci_activate_led(struct sdhci_host *host)
139 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
140 ctrl |= SDHCI_CTRL_LED;
141 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
144 static void sdhci_deactivate_led(struct sdhci_host *host)
148 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
149 ctrl &= ~SDHCI_CTRL_LED;
150 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
153 /*****************************************************************************\
157 \*****************************************************************************/
159 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
161 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
162 return host->mapped_sg + host->cur_sg->offset;
165 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
167 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
170 static inline int sdhci_next_sg(struct sdhci_host* host)
173 * Skip to next SG entry.
181 if (host->num_sg > 0) {
183 host->remain = host->cur_sg->length;
189 static void sdhci_read_block_pio(struct sdhci_host *host)
191 int blksize, chunk_remain;
196 DBG("PIO reading\n");
198 blksize = host->data->blksz;
202 buffer = sdhci_kmap_sg(host) + host->offset;
205 if (chunk_remain == 0) {
206 data = readl(host->ioaddr + SDHCI_BUFFER);
207 chunk_remain = min(blksize, 4);
210 size = min(host->size, host->remain);
211 size = min(size, chunk_remain);
213 chunk_remain -= size;
215 host->offset += size;
216 host->remain -= size;
219 *buffer = data & 0xFF;
225 if (host->remain == 0) {
226 sdhci_kunmap_sg(host);
227 if (sdhci_next_sg(host) == 0) {
228 BUG_ON(blksize != 0);
231 buffer = sdhci_kmap_sg(host);
235 sdhci_kunmap_sg(host);
238 static void sdhci_write_block_pio(struct sdhci_host *host)
240 int blksize, chunk_remain;
245 DBG("PIO writing\n");
247 blksize = host->data->blksz;
252 buffer = sdhci_kmap_sg(host) + host->offset;
255 size = min(host->size, host->remain);
256 size = min(size, chunk_remain);
258 chunk_remain -= size;
260 host->offset += size;
261 host->remain -= size;
265 data |= (u32)*buffer << 24;
270 if (chunk_remain == 0) {
271 writel(data, host->ioaddr + SDHCI_BUFFER);
272 chunk_remain = min(blksize, 4);
275 if (host->remain == 0) {
276 sdhci_kunmap_sg(host);
277 if (sdhci_next_sg(host) == 0) {
278 BUG_ON(blksize != 0);
281 buffer = sdhci_kmap_sg(host);
285 sdhci_kunmap_sg(host);
288 static void sdhci_transfer_pio(struct sdhci_host *host)
297 if (host->data->flags & MMC_DATA_READ)
298 mask = SDHCI_DATA_AVAILABLE;
300 mask = SDHCI_SPACE_AVAILABLE;
302 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
303 if (host->data->flags & MMC_DATA_READ)
304 sdhci_read_block_pio(host);
306 sdhci_write_block_pio(host);
311 BUG_ON(host->num_sg == 0);
314 DBG("PIO transfer complete.\n");
317 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
320 unsigned target_timeout, current_timeout;
327 DBG("blksz %04x blks %04x flags %08x\n",
328 data->blksz, data->blocks, data->flags);
329 DBG("tsac %d ms nsac %d clk\n",
330 data->timeout_ns / 1000000, data->timeout_clks);
333 BUG_ON(data->blksz * data->blocks > 524288);
334 BUG_ON(data->blksz > host->max_block);
335 BUG_ON(data->blocks > 65535);
338 target_timeout = data->timeout_ns / 1000 +
339 data->timeout_clks / host->clock;
342 * Figure out needed cycles.
343 * We do this in steps in order to fit inside a 32 bit int.
344 * The first step is the minimum timeout, which will have a
345 * minimum resolution of 6 bits:
346 * (1) 2^13*1000 > 2^22,
347 * (2) host->timeout_clk < 2^16
352 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
353 while (current_timeout < target_timeout) {
355 current_timeout <<= 1;
361 printk(KERN_WARNING "%s: Too large timeout requested!\n",
362 mmc_hostname(host->mmc));
366 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
368 if (host->flags & SDHCI_USE_DMA) {
371 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
372 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
375 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
377 host->size = data->blksz * data->blocks;
379 host->cur_sg = data->sg;
380 host->num_sg = data->sg_len;
383 host->remain = host->cur_sg->length;
386 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
387 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
388 host->ioaddr + SDHCI_BLOCK_SIZE);
389 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
392 static void sdhci_set_transfer_mode(struct sdhci_host *host,
393 struct mmc_data *data)
402 mode = SDHCI_TRNS_BLK_CNT_EN;
403 if (data->blocks > 1)
404 mode |= SDHCI_TRNS_MULTI;
405 if (data->flags & MMC_DATA_READ)
406 mode |= SDHCI_TRNS_READ;
407 if (host->flags & SDHCI_USE_DMA)
408 mode |= SDHCI_TRNS_DMA;
410 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
413 static void sdhci_finish_data(struct sdhci_host *host)
415 struct mmc_data *data;
423 if (host->flags & SDHCI_USE_DMA) {
424 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
425 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
429 * Controller doesn't count down when in single block mode.
431 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
434 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
435 data->bytes_xfered = data->blksz * (data->blocks - blocks);
437 if ((data->error == MMC_ERR_NONE) && blocks) {
438 printk(KERN_ERR "%s: Controller signalled completion even "
439 "though there were blocks left. Please report this "
440 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
441 data->error = MMC_ERR_FAILED;
444 if (host->size != 0) {
445 printk(KERN_ERR "%s: %d bytes were left untransferred. "
446 "Please report this to " BUGMAIL ".\n",
447 mmc_hostname(host->mmc), host->size);
448 data->error = MMC_ERR_FAILED;
451 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
455 * The controller needs a reset of internal state machines
456 * upon error conditions.
458 if (data->error != MMC_ERR_NONE) {
459 sdhci_reset(host, SDHCI_RESET_CMD);
460 sdhci_reset(host, SDHCI_RESET_DATA);
463 sdhci_send_command(host, data->stop);
465 tasklet_schedule(&host->finish_tasklet);
468 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
472 unsigned long timeout;
476 DBG("Sending cmd (%x)\n", cmd->opcode);
481 mask = SDHCI_CMD_INHIBIT;
482 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
483 mask |= SDHCI_DATA_INHIBIT;
485 /* We shouldn't wait for data inihibit for stop commands, even
486 though they might use busy signaling */
487 if (host->mrq->data && (cmd == host->mrq->data->stop))
488 mask &= ~SDHCI_DATA_INHIBIT;
490 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
492 printk(KERN_ERR "%s: Controller never released "
493 "inhibit bit(s). Please report this to "
494 BUGMAIL ".\n", mmc_hostname(host->mmc));
495 sdhci_dumpregs(host);
496 cmd->error = MMC_ERR_FAILED;
497 tasklet_schedule(&host->finish_tasklet);
504 mod_timer(&host->timer, jiffies + 10 * HZ);
508 sdhci_prepare_data(host, cmd->data);
510 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
512 sdhci_set_transfer_mode(host, cmd->data);
514 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
515 printk(KERN_ERR "%s: Unsupported response type! "
516 "Please report this to " BUGMAIL ".\n",
517 mmc_hostname(host->mmc));
518 cmd->error = MMC_ERR_INVALID;
519 tasklet_schedule(&host->finish_tasklet);
523 if (!(cmd->flags & MMC_RSP_PRESENT))
524 flags = SDHCI_CMD_RESP_NONE;
525 else if (cmd->flags & MMC_RSP_136)
526 flags = SDHCI_CMD_RESP_LONG;
527 else if (cmd->flags & MMC_RSP_BUSY)
528 flags = SDHCI_CMD_RESP_SHORT_BUSY;
530 flags = SDHCI_CMD_RESP_SHORT;
532 if (cmd->flags & MMC_RSP_CRC)
533 flags |= SDHCI_CMD_CRC;
534 if (cmd->flags & MMC_RSP_OPCODE)
535 flags |= SDHCI_CMD_INDEX;
537 flags |= SDHCI_CMD_DATA;
539 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
540 host->ioaddr + SDHCI_COMMAND);
543 static void sdhci_finish_command(struct sdhci_host *host)
547 BUG_ON(host->cmd == NULL);
549 if (host->cmd->flags & MMC_RSP_PRESENT) {
550 if (host->cmd->flags & MMC_RSP_136) {
551 /* CRC is stripped so we need to do some shifting. */
552 for (i = 0;i < 4;i++) {
553 host->cmd->resp[i] = readl(host->ioaddr +
554 SDHCI_RESPONSE + (3-i)*4) << 8;
556 host->cmd->resp[i] |=
558 SDHCI_RESPONSE + (3-i)*4-1);
561 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
565 host->cmd->error = MMC_ERR_NONE;
567 DBG("Ending cmd (%x)\n", host->cmd->opcode);
570 host->data = host->cmd->data;
572 tasklet_schedule(&host->finish_tasklet);
577 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
581 unsigned long timeout;
583 if (clock == host->clock)
586 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
591 for (div = 1;div < 256;div *= 2) {
592 if ((host->max_clk / div) <= clock)
597 clk = div << SDHCI_DIVIDER_SHIFT;
598 clk |= SDHCI_CLOCK_INT_EN;
599 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
603 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
604 & SDHCI_CLOCK_INT_STABLE)) {
606 printk(KERN_ERR "%s: Internal clock never stabilised. "
607 "Please report this to " BUGMAIL ".\n",
608 mmc_hostname(host->mmc));
609 sdhci_dumpregs(host);
616 clk |= SDHCI_CLOCK_CARD_EN;
617 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
623 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
627 if (host->power == power)
630 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
632 if (power == (unsigned short)-1)
635 pwr = SDHCI_POWER_ON;
641 pwr |= SDHCI_POWER_180;
646 pwr |= SDHCI_POWER_300;
651 pwr |= SDHCI_POWER_330;
657 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
663 /*****************************************************************************\
667 \*****************************************************************************/
669 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
671 struct sdhci_host *host;
674 host = mmc_priv(mmc);
676 spin_lock_irqsave(&host->lock, flags);
678 WARN_ON(host->mrq != NULL);
680 sdhci_activate_led(host);
684 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
685 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
686 tasklet_schedule(&host->finish_tasklet);
688 sdhci_send_command(host, mrq->cmd);
690 spin_unlock_irqrestore(&host->lock, flags);
693 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
695 struct sdhci_host *host;
699 host = mmc_priv(mmc);
701 spin_lock_irqsave(&host->lock, flags);
704 * Reset the chip on each power off.
705 * Should clear out any weird states.
707 if (ios->power_mode == MMC_POWER_OFF) {
708 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
712 sdhci_set_clock(host, ios->clock);
714 if (ios->power_mode == MMC_POWER_OFF)
715 sdhci_set_power(host, -1);
717 sdhci_set_power(host, ios->vdd);
719 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
720 if (ios->bus_width == MMC_BUS_WIDTH_4)
721 ctrl |= SDHCI_CTRL_4BITBUS;
723 ctrl &= ~SDHCI_CTRL_4BITBUS;
724 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
726 spin_unlock_irqrestore(&host->lock, flags);
729 static int sdhci_get_ro(struct mmc_host *mmc)
731 struct sdhci_host *host;
735 host = mmc_priv(mmc);
737 spin_lock_irqsave(&host->lock, flags);
739 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
741 spin_unlock_irqrestore(&host->lock, flags);
743 return !(present & SDHCI_WRITE_PROTECT);
746 static struct mmc_host_ops sdhci_ops = {
747 .request = sdhci_request,
748 .set_ios = sdhci_set_ios,
749 .get_ro = sdhci_get_ro,
752 /*****************************************************************************\
756 \*****************************************************************************/
758 static void sdhci_tasklet_card(unsigned long param)
760 struct sdhci_host *host;
763 host = (struct sdhci_host*)param;
765 spin_lock_irqsave(&host->lock, flags);
767 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
769 printk(KERN_ERR "%s: Card removed during transfer!\n",
770 mmc_hostname(host->mmc));
771 printk(KERN_ERR "%s: Resetting controller.\n",
772 mmc_hostname(host->mmc));
774 sdhci_reset(host, SDHCI_RESET_CMD);
775 sdhci_reset(host, SDHCI_RESET_DATA);
777 host->mrq->cmd->error = MMC_ERR_FAILED;
778 tasklet_schedule(&host->finish_tasklet);
782 spin_unlock_irqrestore(&host->lock, flags);
784 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
787 static void sdhci_tasklet_finish(unsigned long param)
789 struct sdhci_host *host;
791 struct mmc_request *mrq;
793 host = (struct sdhci_host*)param;
795 spin_lock_irqsave(&host->lock, flags);
797 del_timer(&host->timer);
801 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
804 * The controller needs a reset of internal state machines
805 * upon error conditions.
807 if ((mrq->cmd->error != MMC_ERR_NONE) ||
808 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
809 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
810 sdhci_reset(host, SDHCI_RESET_CMD);
811 sdhci_reset(host, SDHCI_RESET_DATA);
818 sdhci_deactivate_led(host);
820 spin_unlock_irqrestore(&host->lock, flags);
822 mmc_request_done(host->mmc, mrq);
825 static void sdhci_timeout_timer(unsigned long data)
827 struct sdhci_host *host;
830 host = (struct sdhci_host*)data;
832 spin_lock_irqsave(&host->lock, flags);
835 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
836 "Please report this to " BUGMAIL ".\n",
837 mmc_hostname(host->mmc));
838 sdhci_dumpregs(host);
841 host->data->error = MMC_ERR_TIMEOUT;
842 sdhci_finish_data(host);
845 host->cmd->error = MMC_ERR_TIMEOUT;
847 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
849 tasklet_schedule(&host->finish_tasklet);
853 spin_unlock_irqrestore(&host->lock, flags);
856 /*****************************************************************************\
858 * Interrupt handling *
860 \*****************************************************************************/
862 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
864 BUG_ON(intmask == 0);
867 printk(KERN_ERR "%s: Got command interrupt even though no "
868 "command operation was in progress.\n",
869 mmc_hostname(host->mmc));
870 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
871 mmc_hostname(host->mmc));
872 sdhci_dumpregs(host);
876 if (intmask & SDHCI_INT_RESPONSE)
877 sdhci_finish_command(host);
879 if (intmask & SDHCI_INT_TIMEOUT)
880 host->cmd->error = MMC_ERR_TIMEOUT;
881 else if (intmask & SDHCI_INT_CRC)
882 host->cmd->error = MMC_ERR_BADCRC;
883 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
884 host->cmd->error = MMC_ERR_FAILED;
886 host->cmd->error = MMC_ERR_INVALID;
888 tasklet_schedule(&host->finish_tasklet);
892 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
894 BUG_ON(intmask == 0);
898 * A data end interrupt is sent together with the response
899 * for the stop command.
901 if (intmask & SDHCI_INT_DATA_END)
904 printk(KERN_ERR "%s: Got data interrupt even though no "
905 "data operation was in progress.\n",
906 mmc_hostname(host->mmc));
907 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
908 mmc_hostname(host->mmc));
909 sdhci_dumpregs(host);
914 if (intmask & SDHCI_INT_DATA_TIMEOUT)
915 host->data->error = MMC_ERR_TIMEOUT;
916 else if (intmask & SDHCI_INT_DATA_CRC)
917 host->data->error = MMC_ERR_BADCRC;
918 else if (intmask & SDHCI_INT_DATA_END_BIT)
919 host->data->error = MMC_ERR_FAILED;
921 if (host->data->error != MMC_ERR_NONE)
922 sdhci_finish_data(host);
924 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
925 sdhci_transfer_pio(host);
927 if (intmask & SDHCI_INT_DATA_END)
928 sdhci_finish_data(host);
932 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
935 struct sdhci_host* host = dev_id;
938 spin_lock(&host->lock);
940 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
947 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
949 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
950 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
951 host->ioaddr + SDHCI_INT_STATUS);
952 tasklet_schedule(&host->card_tasklet);
955 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
957 if (intmask & SDHCI_INT_CMD_MASK) {
958 writel(intmask & SDHCI_INT_CMD_MASK,
959 host->ioaddr + SDHCI_INT_STATUS);
960 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
963 if (intmask & SDHCI_INT_DATA_MASK) {
964 writel(intmask & SDHCI_INT_DATA_MASK,
965 host->ioaddr + SDHCI_INT_STATUS);
966 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
969 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
971 if (intmask & SDHCI_INT_BUS_POWER) {
972 printk(KERN_ERR "%s: Card is consuming too much power!\n",
973 mmc_hostname(host->mmc));
974 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
977 intmask &= SDHCI_INT_BUS_POWER;
980 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
981 "report this to " BUGMAIL ".\n",
982 mmc_hostname(host->mmc), intmask);
983 sdhci_dumpregs(host);
985 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
988 result = IRQ_HANDLED;
991 spin_unlock(&host->lock);
996 /*****************************************************************************\
1000 \*****************************************************************************/
1004 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
1006 struct sdhci_chip *chip;
1009 chip = pci_get_drvdata(pdev);
1013 DBG("Suspending...\n");
1015 for (i = 0;i < chip->num_slots;i++) {
1016 if (!chip->hosts[i])
1018 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1020 for (i--;i >= 0;i--)
1021 mmc_resume_host(chip->hosts[i]->mmc);
1026 pci_save_state(pdev);
1027 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1028 pci_disable_device(pdev);
1029 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1034 static int sdhci_resume (struct pci_dev *pdev)
1036 struct sdhci_chip *chip;
1039 chip = pci_get_drvdata(pdev);
1043 DBG("Resuming...\n");
1045 pci_set_power_state(pdev, PCI_D0);
1046 pci_restore_state(pdev);
1047 pci_enable_device(pdev);
1049 for (i = 0;i < chip->num_slots;i++) {
1050 if (!chip->hosts[i])
1052 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1053 pci_set_master(pdev);
1054 sdhci_init(chip->hosts[i]);
1055 ret = mmc_resume_host(chip->hosts[i]->mmc);
1063 #else /* CONFIG_PM */
1065 #define sdhci_suspend NULL
1066 #define sdhci_resume NULL
1068 #endif /* CONFIG_PM */
1070 /*****************************************************************************\
1072 * Device probing/removal *
1074 \*****************************************************************************/
1076 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1079 unsigned int version;
1080 struct sdhci_chip *chip;
1081 struct mmc_host *mmc;
1082 struct sdhci_host *host;
1087 chip = pci_get_drvdata(pdev);
1090 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1094 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1096 if (first_bar > 5) {
1097 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1101 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1102 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1106 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1107 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1111 if ((pdev->class & 0x0000FF) == PCI_SDHCI_IFVENDOR) {
1112 printk(KERN_ERR DRIVER_NAME ": Vendor specific interface. Aborting.\n");
1116 if ((pdev->class & 0x0000FF) > PCI_SDHCI_IFVENDOR) {
1117 printk(KERN_ERR DRIVER_NAME ": Unknown interface. Aborting.\n");
1121 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1125 host = mmc_priv(mmc);
1128 host->bar = first_bar + slot;
1130 host->addr = pci_resource_start(pdev, host->bar);
1131 host->irq = pdev->irq;
1133 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1135 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1137 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1141 host->ioaddr = ioremap_nocache(host->addr,
1142 pci_resource_len(pdev, host->bar));
1143 if (!host->ioaddr) {
1148 sdhci_reset(host, SDHCI_RESET_ALL);
1150 version = readw(host->ioaddr + SDHCI_HOST_VERSION);
1151 version = (version & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
1153 printk(KERN_ERR "%s: Unknown controller version (%d). "
1154 "Cowardly refusing to continue.\n", host->slot_descr,
1160 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1163 DBG("DMA forced off\n");
1164 else if (debug_forcedma) {
1165 DBG("DMA forced on\n");
1166 host->flags |= SDHCI_USE_DMA;
1167 } else if ((pdev->class & 0x0000FF) != PCI_SDHCI_IFDMA)
1168 DBG("Controller doesn't have DMA interface\n");
1169 else if (!(caps & SDHCI_CAN_DO_DMA))
1170 DBG("Controller doesn't have DMA capability\n");
1172 host->flags |= SDHCI_USE_DMA;
1174 if (host->flags & SDHCI_USE_DMA) {
1175 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1176 printk(KERN_WARNING "%s: No suitable DMA available. "
1177 "Falling back to PIO.\n", host->slot_descr);
1178 host->flags &= ~SDHCI_USE_DMA;
1182 if (host->flags & SDHCI_USE_DMA)
1183 pci_set_master(pdev);
1184 else /* XXX: Hack to get MMC layer to avoid highmem */
1188 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1189 if (host->max_clk == 0) {
1190 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1191 "frequency.\n", host->slot_descr);
1195 host->max_clk *= 1000000;
1198 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1199 if (host->timeout_clk == 0) {
1200 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1201 "frequency.\n", host->slot_descr);
1205 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1206 host->timeout_clk *= 1000;
1208 host->max_block = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1209 if (host->max_block >= 3) {
1210 printk(KERN_ERR "%s: Invalid maximum block size.\n",
1215 host->max_block = 512 << host->max_block;
1218 * Set host parameters.
1220 mmc->ops = &sdhci_ops;
1221 mmc->f_min = host->max_clk / 256;
1222 mmc->f_max = host->max_clk;
1223 mmc->caps = MMC_CAP_4_BIT_DATA;
1226 if (caps & SDHCI_CAN_VDD_330)
1227 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1228 else if (caps & SDHCI_CAN_VDD_300)
1229 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1230 else if (caps & SDHCI_CAN_VDD_180)
1231 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1233 if (mmc->ocr_avail == 0) {
1234 printk(KERN_ERR "%s: Hardware doesn't report any "
1235 "support voltages.\n", host->slot_descr);
1240 spin_lock_init(&host->lock);
1243 * Maximum number of segments. Hardware cannot do scatter lists.
1245 if (host->flags & SDHCI_USE_DMA)
1246 mmc->max_hw_segs = 1;
1248 mmc->max_hw_segs = 16;
1249 mmc->max_phys_segs = 16;
1252 * Maximum number of sectors in one transfer. Limited by DMA boundary
1253 * size (512KiB), which means (512 KiB/512=) 1024 entries.
1255 mmc->max_sectors = 1024;
1258 * Maximum segment size. Could be one segment with the maximum number
1261 mmc->max_seg_size = mmc->max_sectors * 512;
1266 tasklet_init(&host->card_tasklet,
1267 sdhci_tasklet_card, (unsigned long)host);
1268 tasklet_init(&host->finish_tasklet,
1269 sdhci_tasklet_finish, (unsigned long)host);
1271 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1273 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1274 host->slot_descr, host);
1280 #ifdef CONFIG_MMC_DEBUG
1281 sdhci_dumpregs(host);
1285 chip->hosts[slot] = host;
1289 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1290 host->addr, host->irq,
1291 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1296 tasklet_kill(&host->card_tasklet);
1297 tasklet_kill(&host->finish_tasklet);
1299 iounmap(host->ioaddr);
1301 pci_release_region(pdev, host->bar);
1308 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1310 struct sdhci_chip *chip;
1311 struct mmc_host *mmc;
1312 struct sdhci_host *host;
1314 chip = pci_get_drvdata(pdev);
1315 host = chip->hosts[slot];
1318 chip->hosts[slot] = NULL;
1320 mmc_remove_host(mmc);
1322 sdhci_reset(host, SDHCI_RESET_ALL);
1324 free_irq(host->irq, host);
1326 del_timer_sync(&host->timer);
1328 tasklet_kill(&host->card_tasklet);
1329 tasklet_kill(&host->finish_tasklet);
1331 iounmap(host->ioaddr);
1333 pci_release_region(pdev, host->bar);
1338 static int __devinit sdhci_probe(struct pci_dev *pdev,
1339 const struct pci_device_id *ent)
1343 struct sdhci_chip *chip;
1345 BUG_ON(pdev == NULL);
1346 BUG_ON(ent == NULL);
1348 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1350 printk(KERN_INFO DRIVER_NAME
1351 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1352 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1355 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1359 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1360 DBG("found %d slot(s)\n", slots);
1364 ret = pci_enable_device(pdev);
1368 chip = kzalloc(sizeof(struct sdhci_chip) +
1369 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1377 chip->num_slots = slots;
1378 pci_set_drvdata(pdev, chip);
1380 for (i = 0;i < slots;i++) {
1381 ret = sdhci_probe_slot(pdev, i);
1383 for (i--;i >= 0;i--)
1384 sdhci_remove_slot(pdev, i);
1392 pci_set_drvdata(pdev, NULL);
1396 pci_disable_device(pdev);
1400 static void __devexit sdhci_remove(struct pci_dev *pdev)
1403 struct sdhci_chip *chip;
1405 chip = pci_get_drvdata(pdev);
1408 for (i = 0;i < chip->num_slots;i++)
1409 sdhci_remove_slot(pdev, i);
1411 pci_set_drvdata(pdev, NULL);
1416 pci_disable_device(pdev);
1419 static struct pci_driver sdhci_driver = {
1420 .name = DRIVER_NAME,
1421 .id_table = pci_ids,
1422 .probe = sdhci_probe,
1423 .remove = __devexit_p(sdhci_remove),
1424 .suspend = sdhci_suspend,
1425 .resume = sdhci_resume,
1428 /*****************************************************************************\
1430 * Driver init/exit *
1432 \*****************************************************************************/
1434 static int __init sdhci_drv_init(void)
1436 printk(KERN_INFO DRIVER_NAME
1437 ": Secure Digital Host Controller Interface driver, "
1438 DRIVER_VERSION "\n");
1439 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1441 return pci_register_driver(&sdhci_driver);
1444 static void __exit sdhci_drv_exit(void)
1448 pci_unregister_driver(&sdhci_driver);
1451 module_init(sdhci_drv_init);
1452 module_exit(sdhci_drv_exit);
1454 module_param(debug_nodma, uint, 0444);
1455 module_param(debug_forcedma, uint, 0444);
1457 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1458 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1459 MODULE_VERSION(DRIVER_VERSION);
1460 MODULE_LICENSE("GPL");
1462 MODULE_PARM_DESC(debug_nodma, "Forcefully disable DMA transfers. (default 0)");
1463 MODULE_PARM_DESC(debug_forcedma, "Forcefully enable DMA transfers. (default 0)");