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.
12 * Note that PIO transfer is rather crappy atm. The buffer full/empty
13 * interrupts aren't reliable so we currently transfer the entire buffer
14 * directly. Patches to solve the problem are welcome.
17 #include <linux/delay.h>
18 #include <linux/highmem.h>
19 #include <linux/pci.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/mmc/host.h>
23 #include <linux/mmc/protocol.h>
25 #include <asm/scatterlist.h>
29 #define DRIVER_NAME "sdhci"
30 #define DRIVER_VERSION "0.11"
32 #define BUGMAIL "<sdhci-devel@list.drzeus.cx>"
34 #define DBG(f, x...) \
35 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
37 static const struct pci_device_id pci_ids[] __devinitdata = {
38 /* handle any SD host controller */
39 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
40 { /* end: all zeroes */ },
43 MODULE_DEVICE_TABLE(pci, pci_ids);
45 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
46 static void sdhci_finish_data(struct sdhci_host *);
48 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49 static void sdhci_finish_command(struct sdhci_host *);
51 static void sdhci_dumpregs(struct sdhci_host *host)
53 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
55 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
56 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
57 readw(host->ioaddr + SDHCI_HOST_VERSION));
58 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
59 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
60 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
61 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
62 readl(host->ioaddr + SDHCI_ARGUMENT),
63 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
64 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
65 readl(host->ioaddr + SDHCI_PRESENT_STATE),
66 readb(host->ioaddr + SDHCI_HOST_CONTROL));
67 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
68 readb(host->ioaddr + SDHCI_POWER_CONTROL),
69 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
70 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
71 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
72 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
73 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
74 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
75 readl(host->ioaddr + SDHCI_INT_STATUS));
76 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
77 readl(host->ioaddr + SDHCI_INT_ENABLE),
78 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
79 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
80 readw(host->ioaddr + SDHCI_ACMD12_ERR),
81 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
82 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
83 readl(host->ioaddr + SDHCI_CAPABILITIES),
84 readl(host->ioaddr + SDHCI_MAX_CURRENT));
86 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
89 /*****************************************************************************\
91 * Low level functions *
93 \*****************************************************************************/
95 static void sdhci_reset(struct sdhci_host *host, u8 mask)
97 unsigned long timeout;
99 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
101 if (mask & SDHCI_RESET_ALL)
104 /* Wait max 100 ms */
107 /* hw clears the bit when it's done */
108 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
110 printk(KERN_ERR "%s: Reset 0x%x never completed. "
111 "Please report this to " BUGMAIL ".\n",
112 mmc_hostname(host->mmc), (int)mask);
113 sdhci_dumpregs(host);
121 static void sdhci_init(struct sdhci_host *host)
125 sdhci_reset(host, SDHCI_RESET_ALL);
127 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
128 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
129 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
130 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
131 SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL |
132 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
134 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
135 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
138 static void sdhci_activate_led(struct sdhci_host *host)
142 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
143 ctrl |= SDHCI_CTRL_LED;
144 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
147 static void sdhci_deactivate_led(struct sdhci_host *host)
151 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
152 ctrl &= ~SDHCI_CTRL_LED;
153 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
156 /*****************************************************************************\
160 \*****************************************************************************/
162 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
164 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
165 return host->mapped_sg + host->cur_sg->offset;
168 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
170 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
173 static inline int sdhci_next_sg(struct sdhci_host* host)
176 * Skip to next SG entry.
184 if (host->num_sg > 0) {
186 host->remain = host->cur_sg->length;
192 static void sdhci_transfer_pio(struct sdhci_host *host)
197 unsigned long max_jiffies;
201 if (host->num_sg == 0)
205 if (host->data->flags & MMC_DATA_READ)
206 mask = SDHCI_DATA_AVAILABLE;
208 mask = SDHCI_SPACE_AVAILABLE;
210 buffer = sdhci_kmap_sg(host) + host->offset;
212 /* Transfer shouldn't take more than 5 s */
213 max_jiffies = jiffies + HZ * 5;
215 while (host->size > 0) {
216 if (time_after(jiffies, max_jiffies)) {
217 printk(KERN_ERR "%s: PIO transfer stalled. "
218 "Please report this to "
219 BUGMAIL ".\n", mmc_hostname(host->mmc));
220 sdhci_dumpregs(host);
222 sdhci_kunmap_sg(host);
224 host->data->error = MMC_ERR_FAILED;
225 sdhci_finish_data(host);
229 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
232 size = min(host->size, host->remain);
235 if (host->data->flags & MMC_DATA_READ)
236 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
238 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
240 } else if (size >= 2) {
241 if (host->data->flags & MMC_DATA_READ)
242 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
244 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
247 if (host->data->flags & MMC_DATA_READ)
248 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
250 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
255 host->offset += size;
256 host->remain -= size;
261 if (host->remain == 0) {
262 sdhci_kunmap_sg(host);
263 if (sdhci_next_sg(host) == 0) {
264 DBG("PIO transfer: %d bytes\n", bytes);
267 buffer = sdhci_kmap_sg(host);
271 sdhci_kunmap_sg(host);
273 DBG("PIO transfer: %d bytes\n", bytes);
276 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
279 unsigned target_timeout, current_timeout;
286 DBG("blksz %04x blks %04x flags %08x\n",
287 data->blksz, data->blocks, data->flags);
288 DBG("tsac %d ms nsac %d clk\n",
289 data->timeout_ns / 1000000, data->timeout_clks);
292 target_timeout = data->timeout_ns / 1000 +
293 data->timeout_clks / host->clock;
296 * Figure out needed cycles.
297 * We do this in steps in order to fit inside a 32 bit int.
298 * The first step is the minimum timeout, which will have a
299 * minimum resolution of 6 bits:
300 * (1) 2^13*1000 > 2^22,
301 * (2) host->timeout_clk < 2^16
306 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
307 while (current_timeout < target_timeout) {
309 current_timeout <<= 1;
315 printk(KERN_WARNING "%s: Too large timeout requested!\n",
316 mmc_hostname(host->mmc));
320 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
322 if (host->flags & SDHCI_USE_DMA) {
325 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
326 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
329 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
331 host->size = data->blksz * data->blocks;
333 host->cur_sg = data->sg;
334 host->num_sg = data->sg_len;
337 host->remain = host->cur_sg->length;
340 writew(data->blksz, host->ioaddr + SDHCI_BLOCK_SIZE);
341 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
344 static void sdhci_set_transfer_mode(struct sdhci_host *host,
345 struct mmc_data *data)
354 mode = SDHCI_TRNS_BLK_CNT_EN;
355 if (data->blocks > 1)
356 mode |= SDHCI_TRNS_MULTI;
357 if (data->flags & MMC_DATA_READ)
358 mode |= SDHCI_TRNS_READ;
359 if (host->flags & SDHCI_USE_DMA)
360 mode |= SDHCI_TRNS_DMA;
362 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
365 static void sdhci_finish_data(struct sdhci_host *host)
367 struct mmc_data *data;
375 if (host->flags & SDHCI_USE_DMA) {
376 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
377 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
381 * Controller doesn't count down when in single block mode.
383 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
386 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
387 data->bytes_xfered = data->blksz * (data->blocks - blocks);
389 if ((data->error == MMC_ERR_NONE) && blocks) {
390 printk(KERN_ERR "%s: Controller signalled completion even "
391 "though there were blocks left. Please report this "
392 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
393 data->error = MMC_ERR_FAILED;
396 if (host->size != 0) {
397 printk(KERN_ERR "%s: %d bytes were left untransferred. "
398 "Please report this to " BUGMAIL ".\n",
399 mmc_hostname(host->mmc), host->size);
400 data->error = MMC_ERR_FAILED;
403 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
407 * The controller needs a reset of internal state machines
408 * upon error conditions.
410 if (data->error != MMC_ERR_NONE) {
411 sdhci_reset(host, SDHCI_RESET_CMD);
412 sdhci_reset(host, SDHCI_RESET_DATA);
415 sdhci_send_command(host, data->stop);
417 tasklet_schedule(&host->finish_tasklet);
420 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
423 unsigned long timeout;
427 DBG("Sending cmd (%x)\n", cmd->opcode);
431 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) &
432 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
434 printk(KERN_ERR "%s: Controller never released "
435 "inhibit bits. Please report this to "
436 BUGMAIL ".\n", mmc_hostname(host->mmc));
437 sdhci_dumpregs(host);
438 cmd->error = MMC_ERR_FAILED;
439 tasklet_schedule(&host->finish_tasklet);
446 mod_timer(&host->timer, jiffies + 10 * HZ);
450 sdhci_prepare_data(host, cmd->data);
452 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
454 sdhci_set_transfer_mode(host, cmd->data);
456 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
457 printk(KERN_ERR "%s: Unsupported response type! "
458 "Please report this to " BUGMAIL ".\n",
459 mmc_hostname(host->mmc));
460 cmd->error = MMC_ERR_INVALID;
461 tasklet_schedule(&host->finish_tasklet);
465 if (!(cmd->flags & MMC_RSP_PRESENT))
466 flags = SDHCI_CMD_RESP_NONE;
467 else if (cmd->flags & MMC_RSP_136)
468 flags = SDHCI_CMD_RESP_LONG;
469 else if (cmd->flags & MMC_RSP_BUSY)
470 flags = SDHCI_CMD_RESP_SHORT_BUSY;
472 flags = SDHCI_CMD_RESP_SHORT;
474 if (cmd->flags & MMC_RSP_CRC)
475 flags |= SDHCI_CMD_CRC;
476 if (cmd->flags & MMC_RSP_OPCODE)
477 flags |= SDHCI_CMD_INDEX;
479 flags |= SDHCI_CMD_DATA;
481 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
482 host->ioaddr + SDHCI_COMMAND);
485 static void sdhci_finish_command(struct sdhci_host *host)
489 BUG_ON(host->cmd == NULL);
491 if (host->cmd->flags & MMC_RSP_PRESENT) {
492 if (host->cmd->flags & MMC_RSP_136) {
493 /* CRC is stripped so we need to do some shifting. */
494 for (i = 0;i < 4;i++) {
495 host->cmd->resp[i] = readl(host->ioaddr +
496 SDHCI_RESPONSE + (3-i)*4) << 8;
498 host->cmd->resp[i] |=
500 SDHCI_RESPONSE + (3-i)*4-1);
503 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
507 host->cmd->error = MMC_ERR_NONE;
509 DBG("Ending cmd (%x)\n", host->cmd->opcode);
512 host->data = host->cmd->data;
514 tasklet_schedule(&host->finish_tasklet);
519 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
523 unsigned long timeout;
525 if (clock == host->clock)
528 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
533 for (div = 1;div < 256;div *= 2) {
534 if ((host->max_clk / div) <= clock)
539 clk = div << SDHCI_DIVIDER_SHIFT;
540 clk |= SDHCI_CLOCK_INT_EN;
541 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
545 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
546 & SDHCI_CLOCK_INT_STABLE)) {
548 printk(KERN_ERR "%s: Internal clock never stabilised. "
549 "Please report this to " BUGMAIL ".\n",
550 mmc_hostname(host->mmc));
551 sdhci_dumpregs(host);
558 clk |= SDHCI_CLOCK_CARD_EN;
559 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
565 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
569 if (host->power == power)
572 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
574 if (power == (unsigned short)-1)
577 pwr = SDHCI_POWER_ON;
583 pwr |= SDHCI_POWER_180;
588 pwr |= SDHCI_POWER_300;
593 pwr |= SDHCI_POWER_330;
599 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
605 /*****************************************************************************\
609 \*****************************************************************************/
611 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
613 struct sdhci_host *host;
616 host = mmc_priv(mmc);
618 spin_lock_irqsave(&host->lock, flags);
620 WARN_ON(host->mrq != NULL);
622 sdhci_activate_led(host);
626 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
627 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
628 tasklet_schedule(&host->finish_tasklet);
630 sdhci_send_command(host, mrq->cmd);
632 spin_unlock_irqrestore(&host->lock, flags);
635 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
637 struct sdhci_host *host;
641 host = mmc_priv(mmc);
643 spin_lock_irqsave(&host->lock, flags);
646 * Reset the chip on each power off.
647 * Should clear out any weird states.
649 if (ios->power_mode == MMC_POWER_OFF) {
650 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
654 sdhci_set_clock(host, ios->clock);
656 if (ios->power_mode == MMC_POWER_OFF)
657 sdhci_set_power(host, -1);
659 sdhci_set_power(host, ios->vdd);
661 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
662 if (ios->bus_width == MMC_BUS_WIDTH_4)
663 ctrl |= SDHCI_CTRL_4BITBUS;
665 ctrl &= ~SDHCI_CTRL_4BITBUS;
666 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
668 spin_unlock_irqrestore(&host->lock, flags);
671 static int sdhci_get_ro(struct mmc_host *mmc)
673 struct sdhci_host *host;
677 host = mmc_priv(mmc);
679 spin_lock_irqsave(&host->lock, flags);
681 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
683 spin_unlock_irqrestore(&host->lock, flags);
685 return !(present & SDHCI_WRITE_PROTECT);
688 static struct mmc_host_ops sdhci_ops = {
689 .request = sdhci_request,
690 .set_ios = sdhci_set_ios,
691 .get_ro = sdhci_get_ro,
694 /*****************************************************************************\
698 \*****************************************************************************/
700 static void sdhci_tasklet_card(unsigned long param)
702 struct sdhci_host *host;
705 host = (struct sdhci_host*)param;
707 spin_lock_irqsave(&host->lock, flags);
709 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
711 printk(KERN_ERR "%s: Card removed during transfer!\n",
712 mmc_hostname(host->mmc));
713 printk(KERN_ERR "%s: Resetting controller.\n",
714 mmc_hostname(host->mmc));
716 sdhci_reset(host, SDHCI_RESET_CMD);
717 sdhci_reset(host, SDHCI_RESET_DATA);
719 host->mrq->cmd->error = MMC_ERR_FAILED;
720 tasklet_schedule(&host->finish_tasklet);
724 spin_unlock_irqrestore(&host->lock, flags);
726 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
729 static void sdhci_tasklet_finish(unsigned long param)
731 struct sdhci_host *host;
733 struct mmc_request *mrq;
735 host = (struct sdhci_host*)param;
737 spin_lock_irqsave(&host->lock, flags);
739 del_timer(&host->timer);
743 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
746 * The controller needs a reset of internal state machines
747 * upon error conditions.
749 if ((mrq->cmd->error != MMC_ERR_NONE) ||
750 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
751 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
752 sdhci_reset(host, SDHCI_RESET_CMD);
753 sdhci_reset(host, SDHCI_RESET_DATA);
760 sdhci_deactivate_led(host);
762 spin_unlock_irqrestore(&host->lock, flags);
764 mmc_request_done(host->mmc, mrq);
767 static void sdhci_timeout_timer(unsigned long data)
769 struct sdhci_host *host;
772 host = (struct sdhci_host*)data;
774 spin_lock_irqsave(&host->lock, flags);
777 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
778 "Please report this to " BUGMAIL ".\n",
779 mmc_hostname(host->mmc));
780 sdhci_dumpregs(host);
783 host->data->error = MMC_ERR_TIMEOUT;
784 sdhci_finish_data(host);
787 host->cmd->error = MMC_ERR_TIMEOUT;
789 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
791 tasklet_schedule(&host->finish_tasklet);
795 spin_unlock_irqrestore(&host->lock, flags);
798 /*****************************************************************************\
800 * Interrupt handling *
802 \*****************************************************************************/
804 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
806 BUG_ON(intmask == 0);
809 printk(KERN_ERR "%s: Got command interrupt even though no "
810 "command operation was in progress.\n",
811 mmc_hostname(host->mmc));
812 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
813 mmc_hostname(host->mmc));
814 sdhci_dumpregs(host);
818 if (intmask & SDHCI_INT_RESPONSE)
819 sdhci_finish_command(host);
821 if (intmask & SDHCI_INT_TIMEOUT)
822 host->cmd->error = MMC_ERR_TIMEOUT;
823 else if (intmask & SDHCI_INT_CRC)
824 host->cmd->error = MMC_ERR_BADCRC;
825 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
826 host->cmd->error = MMC_ERR_FAILED;
828 host->cmd->error = MMC_ERR_INVALID;
830 tasklet_schedule(&host->finish_tasklet);
834 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
836 BUG_ON(intmask == 0);
840 * A data end interrupt is sent together with the response
841 * for the stop command.
843 if (intmask & SDHCI_INT_DATA_END)
846 printk(KERN_ERR "%s: Got data interrupt even though no "
847 "data operation was in progress.\n",
848 mmc_hostname(host->mmc));
849 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
850 mmc_hostname(host->mmc));
851 sdhci_dumpregs(host);
856 if (intmask & SDHCI_INT_DATA_TIMEOUT)
857 host->data->error = MMC_ERR_TIMEOUT;
858 else if (intmask & SDHCI_INT_DATA_CRC)
859 host->data->error = MMC_ERR_BADCRC;
860 else if (intmask & SDHCI_INT_DATA_END_BIT)
861 host->data->error = MMC_ERR_FAILED;
863 if (host->data->error != MMC_ERR_NONE)
864 sdhci_finish_data(host);
866 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
867 sdhci_transfer_pio(host);
869 if (intmask & SDHCI_INT_DATA_END)
870 sdhci_finish_data(host);
874 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
877 struct sdhci_host* host = dev_id;
880 spin_lock(&host->lock);
882 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
889 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
891 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
892 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
893 host->ioaddr + SDHCI_INT_STATUS);
894 tasklet_schedule(&host->card_tasklet);
897 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
899 if (intmask & SDHCI_INT_CMD_MASK) {
900 writel(intmask & SDHCI_INT_CMD_MASK,
901 host->ioaddr + SDHCI_INT_STATUS);
902 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
905 if (intmask & SDHCI_INT_DATA_MASK) {
906 writel(intmask & SDHCI_INT_DATA_MASK,
907 host->ioaddr + SDHCI_INT_STATUS);
908 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
911 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
913 if (intmask & SDHCI_INT_BUS_POWER) {
914 printk(KERN_ERR "%s: Card is consuming too much power!\n",
915 mmc_hostname(host->mmc));
916 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
919 intmask &= SDHCI_INT_BUS_POWER;
922 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
923 "report this to " BUGMAIL ".\n",
924 mmc_hostname(host->mmc), intmask);
925 sdhci_dumpregs(host);
927 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
930 result = IRQ_HANDLED;
933 spin_unlock(&host->lock);
938 /*****************************************************************************\
942 \*****************************************************************************/
946 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
948 struct sdhci_chip *chip;
951 chip = pci_get_drvdata(pdev);
955 DBG("Suspending...\n");
957 for (i = 0;i < chip->num_slots;i++) {
960 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
963 mmc_resume_host(chip->hosts[i]->mmc);
968 pci_save_state(pdev);
969 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
970 pci_disable_device(pdev);
971 pci_set_power_state(pdev, pci_choose_state(pdev, state));
976 static int sdhci_resume (struct pci_dev *pdev)
978 struct sdhci_chip *chip;
981 chip = pci_get_drvdata(pdev);
985 DBG("Resuming...\n");
987 pci_set_power_state(pdev, PCI_D0);
988 pci_restore_state(pdev);
989 pci_enable_device(pdev);
991 for (i = 0;i < chip->num_slots;i++) {
994 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
995 pci_set_master(pdev);
996 sdhci_init(chip->hosts[i]);
997 ret = mmc_resume_host(chip->hosts[i]->mmc);
1005 #else /* CONFIG_PM */
1007 #define sdhci_suspend NULL
1008 #define sdhci_resume NULL
1010 #endif /* CONFIG_PM */
1012 /*****************************************************************************\
1014 * Device probing/removal *
1016 \*****************************************************************************/
1018 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1021 struct sdhci_chip *chip;
1022 struct mmc_host *mmc;
1023 struct sdhci_host *host;
1028 chip = pci_get_drvdata(pdev);
1031 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1035 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1037 if (first_bar > 5) {
1038 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1042 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1043 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1047 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1048 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1052 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1056 host = mmc_priv(mmc);
1059 host->bar = first_bar + slot;
1061 host->addr = pci_resource_start(pdev, host->bar);
1062 host->irq = pdev->irq;
1064 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1066 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1068 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1072 host->ioaddr = ioremap_nocache(host->addr,
1073 pci_resource_len(pdev, host->bar));
1074 if (!host->ioaddr) {
1079 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1081 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1082 host->flags |= SDHCI_USE_DMA;
1084 if (host->flags & SDHCI_USE_DMA) {
1085 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1086 printk(KERN_WARNING "%s: No suitable DMA available. "
1087 "Falling back to PIO.\n", host->slot_descr);
1088 host->flags &= ~SDHCI_USE_DMA;
1092 if (host->flags & SDHCI_USE_DMA)
1093 pci_set_master(pdev);
1094 else /* XXX: Hack to get MMC layer to avoid highmem */
1098 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1099 if (host->max_clk == 0) {
1100 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1101 "frequency.\n", host->slot_descr);
1105 host->max_clk *= 1000000;
1108 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1109 if (host->timeout_clk == 0) {
1110 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1111 "frequency.\n", host->slot_descr);
1115 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1116 host->timeout_clk *= 1000;
1119 * Set host parameters.
1121 mmc->ops = &sdhci_ops;
1122 mmc->f_min = host->max_clk / 256;
1123 mmc->f_max = host->max_clk;
1124 mmc->caps = MMC_CAP_4_BIT_DATA;
1127 if (caps & SDHCI_CAN_VDD_330)
1128 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1129 else if (caps & SDHCI_CAN_VDD_300)
1130 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1131 else if (caps & SDHCI_CAN_VDD_180)
1132 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1134 if (mmc->ocr_avail == 0) {
1135 printk(KERN_ERR "%s: Hardware doesn't report any "
1136 "support voltages.\n", host->slot_descr);
1141 spin_lock_init(&host->lock);
1144 * Maximum number of segments. Hardware cannot do scatter lists.
1146 if (host->flags & SDHCI_USE_DMA)
1147 mmc->max_hw_segs = 1;
1149 mmc->max_hw_segs = 16;
1150 mmc->max_phys_segs = 16;
1153 * Maximum number of sectors in one transfer. Limited by sector
1156 mmc->max_sectors = 0x3FFF;
1159 * Maximum segment size. Could be one segment with the maximum number
1162 mmc->max_seg_size = mmc->max_sectors * 512;
1167 tasklet_init(&host->card_tasklet,
1168 sdhci_tasklet_card, (unsigned long)host);
1169 tasklet_init(&host->finish_tasklet,
1170 sdhci_tasklet_finish, (unsigned long)host);
1172 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1174 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1175 host->slot_descr, host);
1181 #ifdef CONFIG_MMC_DEBUG
1182 sdhci_dumpregs(host);
1186 chip->hosts[slot] = host;
1190 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1191 host->addr, host->irq,
1192 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1197 tasklet_kill(&host->card_tasklet);
1198 tasklet_kill(&host->finish_tasklet);
1200 iounmap(host->ioaddr);
1202 pci_release_region(pdev, host->bar);
1209 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1211 struct sdhci_chip *chip;
1212 struct mmc_host *mmc;
1213 struct sdhci_host *host;
1215 chip = pci_get_drvdata(pdev);
1216 host = chip->hosts[slot];
1219 chip->hosts[slot] = NULL;
1221 mmc_remove_host(mmc);
1223 sdhci_reset(host, SDHCI_RESET_ALL);
1225 free_irq(host->irq, host);
1227 del_timer_sync(&host->timer);
1229 tasklet_kill(&host->card_tasklet);
1230 tasklet_kill(&host->finish_tasklet);
1232 iounmap(host->ioaddr);
1234 pci_release_region(pdev, host->bar);
1239 static int __devinit sdhci_probe(struct pci_dev *pdev,
1240 const struct pci_device_id *ent)
1244 struct sdhci_chip *chip;
1246 BUG_ON(pdev == NULL);
1247 BUG_ON(ent == NULL);
1249 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1251 printk(KERN_INFO DRIVER_NAME
1252 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1253 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1256 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1260 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1261 DBG("found %d slot(s)\n", slots);
1265 ret = pci_enable_device(pdev);
1269 chip = kzalloc(sizeof(struct sdhci_chip) +
1270 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1278 chip->num_slots = slots;
1279 pci_set_drvdata(pdev, chip);
1281 for (i = 0;i < slots;i++) {
1282 ret = sdhci_probe_slot(pdev, i);
1284 for (i--;i >= 0;i--)
1285 sdhci_remove_slot(pdev, i);
1293 pci_set_drvdata(pdev, NULL);
1297 pci_disable_device(pdev);
1301 static void __devexit sdhci_remove(struct pci_dev *pdev)
1304 struct sdhci_chip *chip;
1306 chip = pci_get_drvdata(pdev);
1309 for (i = 0;i < chip->num_slots;i++)
1310 sdhci_remove_slot(pdev, i);
1312 pci_set_drvdata(pdev, NULL);
1317 pci_disable_device(pdev);
1320 static struct pci_driver sdhci_driver = {
1321 .name = DRIVER_NAME,
1322 .id_table = pci_ids,
1323 .probe = sdhci_probe,
1324 .remove = __devexit_p(sdhci_remove),
1325 .suspend = sdhci_suspend,
1326 .resume = sdhci_resume,
1329 /*****************************************************************************\
1331 * Driver init/exit *
1333 \*****************************************************************************/
1335 static int __init sdhci_drv_init(void)
1337 printk(KERN_INFO DRIVER_NAME
1338 ": Secure Digital Host Controller Interface driver, "
1339 DRIVER_VERSION "\n");
1340 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1342 return pci_register_driver(&sdhci_driver);
1345 static void __exit sdhci_drv_exit(void)
1349 pci_unregister_driver(&sdhci_driver);
1352 module_init(sdhci_drv_init);
1353 module_exit(sdhci_drv_exit);
1355 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1356 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1357 MODULE_VERSION(DRIVER_VERSION);
1358 MODULE_LICENSE("GPL");