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 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
99 if (mask & SDHCI_RESET_ALL) {
106 static void sdhci_init(struct sdhci_host *host)
110 sdhci_reset(host, SDHCI_RESET_ALL);
112 intmask = ~(SDHCI_INT_CARD_INT | SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
114 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
115 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
117 /* This is unknown magic. */
118 writeb(0xE, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
121 static void sdhci_activate_led(struct sdhci_host *host)
125 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
126 ctrl |= SDHCI_CTRL_LED;
127 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
130 static void sdhci_deactivate_led(struct sdhci_host *host)
134 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
135 ctrl &= ~SDHCI_CTRL_LED;
136 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
139 /*****************************************************************************\
143 \*****************************************************************************/
145 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
147 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
148 return host->mapped_sg + host->cur_sg->offset;
151 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
153 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
156 static inline int sdhci_next_sg(struct sdhci_host* host)
159 * Skip to next SG entry.
167 if (host->num_sg > 0) {
169 host->remain = host->cur_sg->length;
175 static void sdhci_transfer_pio(struct sdhci_host *host)
180 unsigned long max_jiffies;
184 if (host->num_sg == 0)
188 if (host->data->flags & MMC_DATA_READ)
189 mask = SDHCI_DATA_AVAILABLE;
191 mask = SDHCI_SPACE_AVAILABLE;
193 buffer = sdhci_kmap_sg(host) + host->offset;
195 /* Transfer shouldn't take more than 5 s */
196 max_jiffies = jiffies + HZ * 5;
198 while (host->size > 0) {
199 if (time_after(jiffies, max_jiffies)) {
200 printk(KERN_ERR "%s: PIO transfer stalled. "
201 "Please report this to "
202 BUGMAIL ".\n", mmc_hostname(host->mmc));
203 sdhci_dumpregs(host);
205 sdhci_kunmap_sg(host);
207 host->data->error = MMC_ERR_FAILED;
208 sdhci_finish_data(host);
212 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
215 size = min(host->size, host->remain);
218 if (host->data->flags & MMC_DATA_READ)
219 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
221 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
223 } else if (size >= 2) {
224 if (host->data->flags & MMC_DATA_READ)
225 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
227 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
230 if (host->data->flags & MMC_DATA_READ)
231 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
233 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
238 host->offset += size;
239 host->remain -= size;
244 if (host->remain == 0) {
245 sdhci_kunmap_sg(host);
246 if (sdhci_next_sg(host) == 0) {
247 DBG("PIO transfer: %d bytes\n", bytes);
250 buffer = sdhci_kmap_sg(host);
254 sdhci_kunmap_sg(host);
256 DBG("PIO transfer: %d bytes\n", bytes);
259 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
266 writew(0, host->ioaddr + SDHCI_TRANSFER_MODE);
270 DBG("blksz %04x blks %04x flags %08x\n",
271 1 << data->blksz_bits, data->blocks, data->flags);
272 DBG("tsac %d ms nsac %d clk\n",
273 data->timeout_ns / 1000000, data->timeout_clks);
275 mode = SDHCI_TRNS_BLK_CNT_EN;
276 if (data->blocks > 1)
277 mode |= SDHCI_TRNS_MULTI;
278 if (data->flags & MMC_DATA_READ)
279 mode |= SDHCI_TRNS_READ;
280 if (host->flags & SDHCI_USE_DMA)
281 mode |= SDHCI_TRNS_DMA;
283 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
285 writew(1 << data->blksz_bits, host->ioaddr + SDHCI_BLOCK_SIZE);
286 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
288 if (host->flags & SDHCI_USE_DMA) {
291 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
292 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
295 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
297 host->size = (1 << data->blksz_bits) * data->blocks;
299 host->cur_sg = data->sg;
300 host->num_sg = data->sg_len;
303 host->remain = host->cur_sg->length;
307 static void sdhci_finish_data(struct sdhci_host *host)
309 struct mmc_data *data;
318 if (host->flags & SDHCI_USE_DMA) {
319 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
320 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
322 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
323 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
324 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
326 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
327 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
328 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
332 * Controller doesn't count down when in single block mode.
334 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
337 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
338 data->bytes_xfered = (1 << data->blksz_bits) * (data->blocks - blocks);
340 if ((data->error == MMC_ERR_NONE) && blocks) {
341 printk(KERN_ERR "%s: Controller signalled completion even "
342 "though there were blocks left. Please report this "
343 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
344 data->error = MMC_ERR_FAILED;
347 if (host->size != 0) {
348 printk(KERN_ERR "%s: %d bytes were left untransferred. "
349 "Please report this to " BUGMAIL ".\n",
350 mmc_hostname(host->mmc), host->size);
351 data->error = MMC_ERR_FAILED;
354 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
358 * The controller needs a reset of internal state machines
359 * upon error conditions.
361 if (data->error != MMC_ERR_NONE) {
362 sdhci_reset(host, SDHCI_RESET_CMD);
363 sdhci_reset(host, SDHCI_RESET_DATA);
366 sdhci_send_command(host, data->stop);
368 tasklet_schedule(&host->finish_tasklet);
371 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
375 unsigned long max_jiffies;
379 DBG("Sending cmd (%x)\n", cmd->opcode);
382 max_jiffies = jiffies + (HZ + 99)/100;
384 if (time_after(jiffies, max_jiffies)) {
385 printk(KERN_ERR "%s: Controller never released "
386 "inhibit bits. Please report this to "
387 BUGMAIL ".\n", mmc_hostname(host->mmc));
388 sdhci_dumpregs(host);
389 cmd->error = MMC_ERR_FAILED;
390 tasklet_schedule(&host->finish_tasklet);
393 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
394 } while (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT));
396 mod_timer(&host->timer, jiffies + 10 * HZ);
400 sdhci_prepare_data(host, cmd->data);
402 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
404 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
405 printk(KERN_ERR "%s: Unsupported response type! "
406 "Please report this to " BUGMAIL ".\n",
407 mmc_hostname(host->mmc));
408 cmd->error = MMC_ERR_INVALID;
409 tasklet_schedule(&host->finish_tasklet);
413 if (!(cmd->flags & MMC_RSP_PRESENT))
414 flags = SDHCI_CMD_RESP_NONE;
415 else if (cmd->flags & MMC_RSP_136)
416 flags = SDHCI_CMD_RESP_LONG;
417 else if (cmd->flags & MMC_RSP_BUSY)
418 flags = SDHCI_CMD_RESP_SHORT_BUSY;
420 flags = SDHCI_CMD_RESP_SHORT;
422 if (cmd->flags & MMC_RSP_CRC)
423 flags |= SDHCI_CMD_CRC;
424 if (cmd->flags & MMC_RSP_OPCODE)
425 flags |= SDHCI_CMD_INDEX;
427 flags |= SDHCI_CMD_DATA;
429 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
430 host->ioaddr + SDHCI_COMMAND);
433 static void sdhci_finish_command(struct sdhci_host *host)
437 BUG_ON(host->cmd == NULL);
439 if (host->cmd->flags & MMC_RSP_PRESENT) {
440 if (host->cmd->flags & MMC_RSP_136) {
441 /* CRC is stripped so we need to do some shifting. */
442 for (i = 0;i < 4;i++) {
443 host->cmd->resp[i] = readl(host->ioaddr +
444 SDHCI_RESPONSE + (3-i)*4) << 8;
446 host->cmd->resp[i] |=
448 SDHCI_RESPONSE + (3-i)*4-1);
451 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
455 host->cmd->error = MMC_ERR_NONE;
457 DBG("Ending cmd (%x)\n", host->cmd->opcode);
459 if (host->cmd->data) {
462 host->data = host->cmd->data;
464 if (!(host->flags & SDHCI_USE_DMA)) {
466 * Don't enable the interrupts until now to make sure we
467 * get stable handling of the FIFO.
469 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
470 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
471 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
473 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
474 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
475 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
478 * The buffer interrupts are to unreliable so we
479 * start the transfer immediatly.
481 sdhci_transfer_pio(host);
484 tasklet_schedule(&host->finish_tasklet);
489 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
493 unsigned long max_jiffies;
495 if (clock == host->clock)
498 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
503 for (div = 1;div < 256;div *= 2) {
504 if ((host->max_clk / div) <= clock)
509 clk = div << SDHCI_DIVIDER_SHIFT;
510 clk |= SDHCI_CLOCK_INT_EN;
511 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
514 max_jiffies = jiffies + (HZ + 99)/100;
516 if (time_after(jiffies, max_jiffies)) {
517 printk(KERN_ERR "%s: Internal clock never stabilised. "
518 "Please report this to " BUGMAIL ".\n",
519 mmc_hostname(host->mmc));
520 sdhci_dumpregs(host);
523 clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL);
524 } while (!(clk & SDHCI_CLOCK_INT_STABLE));
526 clk |= SDHCI_CLOCK_CARD_EN;
527 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
533 /*****************************************************************************\
537 \*****************************************************************************/
539 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
541 struct sdhci_host *host;
544 host = mmc_priv(mmc);
546 spin_lock_irqsave(&host->lock, flags);
548 WARN_ON(host->mrq != NULL);
550 sdhci_activate_led(host);
554 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
555 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
556 tasklet_schedule(&host->finish_tasklet);
558 sdhci_send_command(host, mrq->cmd);
560 spin_unlock_irqrestore(&host->lock, flags);
563 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
565 struct sdhci_host *host;
569 host = mmc_priv(mmc);
571 spin_lock_irqsave(&host->lock, flags);
573 DBG("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
574 ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
575 ios->vdd, ios->bus_width);
578 * Reset the chip on each power off.
579 * Should clear out any weird states.
581 if (ios->power_mode == MMC_POWER_OFF) {
582 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
583 spin_unlock_irqrestore(&host->lock, flags);
585 spin_lock_irqsave(&host->lock, flags);
588 sdhci_set_clock(host, ios->clock);
590 if (ios->power_mode == MMC_POWER_OFF)
591 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
593 writeb(0xFF, host->ioaddr + SDHCI_POWER_CONTROL);
595 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
596 if (ios->bus_width == MMC_BUS_WIDTH_4)
597 ctrl |= SDHCI_CTRL_4BITBUS;
599 ctrl &= ~SDHCI_CTRL_4BITBUS;
600 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
602 spin_unlock_irqrestore(&host->lock, flags);
605 static int sdhci_get_ro(struct mmc_host *mmc)
607 struct sdhci_host *host;
611 host = mmc_priv(mmc);
613 spin_lock_irqsave(&host->lock, flags);
615 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
617 spin_unlock_irqrestore(&host->lock, flags);
619 return !(present & SDHCI_WRITE_PROTECT);
622 static struct mmc_host_ops sdhci_ops = {
623 .request = sdhci_request,
624 .set_ios = sdhci_set_ios,
625 .get_ro = sdhci_get_ro,
628 /*****************************************************************************\
632 \*****************************************************************************/
634 static void sdhci_tasklet_card(unsigned long param)
636 struct sdhci_host *host;
639 host = (struct sdhci_host*)param;
641 spin_lock_irqsave(&host->lock, flags);
643 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
645 printk(KERN_ERR "%s: Card removed during transfer!\n",
646 mmc_hostname(host->mmc));
647 printk(KERN_ERR "%s: Resetting controller.\n",
648 mmc_hostname(host->mmc));
650 sdhci_reset(host, SDHCI_RESET_CMD);
651 sdhci_reset(host, SDHCI_RESET_DATA);
653 host->mrq->cmd->error = MMC_ERR_FAILED;
654 tasklet_schedule(&host->finish_tasklet);
658 spin_unlock_irqrestore(&host->lock, flags);
660 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
663 static void sdhci_tasklet_finish(unsigned long param)
665 struct sdhci_host *host;
667 struct mmc_request *mrq;
669 host = (struct sdhci_host*)param;
671 spin_lock_irqsave(&host->lock, flags);
673 del_timer(&host->timer);
677 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
680 * The controller needs a reset of internal state machines
681 * upon error conditions.
683 if ((mrq->cmd->error != MMC_ERR_NONE) ||
684 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
685 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
686 sdhci_reset(host, SDHCI_RESET_CMD);
687 sdhci_reset(host, SDHCI_RESET_DATA);
694 sdhci_deactivate_led(host);
696 spin_unlock_irqrestore(&host->lock, flags);
698 mmc_request_done(host->mmc, mrq);
701 static void sdhci_timeout_timer(unsigned long data)
703 struct sdhci_host *host;
706 host = (struct sdhci_host*)data;
708 spin_lock_irqsave(&host->lock, flags);
711 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
712 "Please report this to " BUGMAIL ".\n",
713 mmc_hostname(host->mmc));
714 sdhci_dumpregs(host);
717 host->data->error = MMC_ERR_TIMEOUT;
718 sdhci_finish_data(host);
721 host->cmd->error = MMC_ERR_TIMEOUT;
723 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
725 tasklet_schedule(&host->finish_tasklet);
729 spin_unlock_irqrestore(&host->lock, flags);
732 /*****************************************************************************\
734 * Interrupt handling *
736 \*****************************************************************************/
738 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
740 BUG_ON(intmask == 0);
743 printk(KERN_ERR "%s: Got command interrupt even though no "
744 "command operation was in progress.\n",
745 mmc_hostname(host->mmc));
746 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
747 mmc_hostname(host->mmc));
748 sdhci_dumpregs(host);
752 if (intmask & SDHCI_INT_RESPONSE)
753 sdhci_finish_command(host);
755 if (intmask & SDHCI_INT_TIMEOUT)
756 host->cmd->error = MMC_ERR_TIMEOUT;
757 else if (intmask & SDHCI_INT_CRC)
758 host->cmd->error = MMC_ERR_BADCRC;
759 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
760 host->cmd->error = MMC_ERR_FAILED;
762 host->cmd->error = MMC_ERR_INVALID;
764 tasklet_schedule(&host->finish_tasklet);
768 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
770 BUG_ON(intmask == 0);
774 * A data end interrupt is sent together with the response
775 * for the stop command.
777 if (intmask & SDHCI_INT_DATA_END)
780 printk(KERN_ERR "%s: Got data interrupt even though no "
781 "data operation was in progress.\n",
782 mmc_hostname(host->mmc));
783 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
784 mmc_hostname(host->mmc));
785 sdhci_dumpregs(host);
790 if (intmask & SDHCI_INT_DATA_TIMEOUT)
791 host->data->error = MMC_ERR_TIMEOUT;
792 else if (intmask & SDHCI_INT_DATA_CRC)
793 host->data->error = MMC_ERR_BADCRC;
794 else if (intmask & SDHCI_INT_DATA_END_BIT)
795 host->data->error = MMC_ERR_FAILED;
797 if (host->data->error != MMC_ERR_NONE)
798 sdhci_finish_data(host);
800 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
801 sdhci_transfer_pio(host);
803 if (intmask & SDHCI_INT_DATA_END)
804 sdhci_finish_data(host);
808 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
811 struct sdhci_host* host = dev_id;
814 spin_lock(&host->lock);
816 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
823 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
825 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
826 tasklet_schedule(&host->card_tasklet);
828 if (intmask & SDHCI_INT_CMD_MASK) {
829 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
831 writel(intmask & SDHCI_INT_CMD_MASK,
832 host->ioaddr + SDHCI_INT_STATUS);
835 if (intmask & SDHCI_INT_DATA_MASK) {
836 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
838 writel(intmask & SDHCI_INT_DATA_MASK,
839 host->ioaddr + SDHCI_INT_STATUS);
842 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
844 if (intmask & SDHCI_INT_CARD_INT) {
845 printk(KERN_ERR "%s: Unexpected card interrupt. Please "
846 "report this to " BUGMAIL ".\n",
847 mmc_hostname(host->mmc));
848 sdhci_dumpregs(host);
851 if (intmask & SDHCI_INT_BUS_POWER) {
852 printk(KERN_ERR "%s: Unexpected bus power interrupt. Please "
853 "report this to " BUGMAIL ".\n",
854 mmc_hostname(host->mmc));
855 sdhci_dumpregs(host);
858 if (intmask & SDHCI_INT_ACMD12ERR) {
859 printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please "
860 "report this to " BUGMAIL ".\n",
861 mmc_hostname(host->mmc));
862 sdhci_dumpregs(host);
864 writew(~0, host->ioaddr + SDHCI_ACMD12_ERR);
868 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
870 result = IRQ_HANDLED;
873 spin_unlock(&host->lock);
878 /*****************************************************************************\
882 \*****************************************************************************/
886 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
888 struct sdhci_chip *chip;
891 chip = pci_get_drvdata(pdev);
895 DBG("Suspending...\n");
897 for (i = 0;i < chip->num_slots;i++) {
900 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
903 mmc_resume_host(chip->hosts[i]->mmc);
908 pci_save_state(pdev);
909 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
910 pci_disable_device(pdev);
911 pci_set_power_state(pdev, pci_choose_state(pdev, state));
916 static int sdhci_resume (struct pci_dev *pdev)
918 struct sdhci_chip *chip;
921 chip = pci_get_drvdata(pdev);
925 DBG("Resuming...\n");
927 pci_set_power_state(pdev, PCI_D0);
928 pci_restore_state(pdev);
929 pci_enable_device(pdev);
931 for (i = 0;i < chip->num_slots;i++) {
934 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
935 pci_set_master(pdev);
936 sdhci_init(chip->hosts[i]);
937 ret = mmc_resume_host(chip->hosts[i]->mmc);
945 #else /* CONFIG_PM */
947 #define sdhci_suspend NULL
948 #define sdhci_resume NULL
950 #endif /* CONFIG_PM */
952 /*****************************************************************************\
954 * Device probing/removal *
956 \*****************************************************************************/
958 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
961 struct sdhci_chip *chip;
962 struct mmc_host *mmc;
963 struct sdhci_host *host;
968 chip = pci_get_drvdata(pdev);
971 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
975 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
978 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
982 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
983 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
987 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
988 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
992 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
996 host = mmc_priv(mmc);
999 host->bar = first_bar + slot;
1001 host->addr = pci_resource_start(pdev, host->bar);
1002 host->irq = pdev->irq;
1004 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1006 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1008 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1012 host->ioaddr = ioremap_nocache(host->addr,
1013 pci_resource_len(pdev, host->bar));
1014 if (!host->ioaddr) {
1019 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1021 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1022 host->flags |= SDHCI_USE_DMA;
1024 if (host->flags & SDHCI_USE_DMA) {
1025 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1026 printk(KERN_WARNING "%s: No suitable DMA available. "
1027 "Falling back to PIO.\n", host->slot_descr);
1028 host->flags &= ~SDHCI_USE_DMA;
1032 if (host->flags & SDHCI_USE_DMA)
1033 pci_set_master(pdev);
1034 else /* XXX: Hack to get MMC layer to avoid highmem */
1037 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1038 host->max_clk *= 1000000;
1041 * Set host parameters.
1043 mmc->ops = &sdhci_ops;
1044 mmc->f_min = host->max_clk / 256;
1045 mmc->f_max = host->max_clk;
1046 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1047 mmc->caps = MMC_CAP_4_BIT_DATA;
1049 spin_lock_init(&host->lock);
1052 * Maximum number of segments. Hardware cannot do scatter lists.
1054 if (host->flags & SDHCI_USE_DMA)
1055 mmc->max_hw_segs = 1;
1057 mmc->max_hw_segs = 16;
1058 mmc->max_phys_segs = 16;
1061 * Maximum number of sectors in one transfer. Limited by sector
1064 mmc->max_sectors = 0x3FFF;
1067 * Maximum segment size. Could be one segment with the maximum number
1070 mmc->max_seg_size = mmc->max_sectors * 512;
1075 tasklet_init(&host->card_tasklet,
1076 sdhci_tasklet_card, (unsigned long)host);
1077 tasklet_init(&host->finish_tasklet,
1078 sdhci_tasklet_finish, (unsigned long)host);
1080 setup_timer(&host->timer, sdhci_timeout_timer, (int)host);
1082 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1083 host->slot_descr, host);
1089 #ifdef CONFIG_MMC_DEBUG
1090 sdhci_dumpregs(host);
1094 chip->hosts[slot] = host;
1098 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1099 host->addr, host->irq,
1100 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1105 tasklet_kill(&host->card_tasklet);
1106 tasklet_kill(&host->finish_tasklet);
1108 iounmap(host->ioaddr);
1110 pci_release_region(pdev, host->bar);
1117 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1119 struct sdhci_chip *chip;
1120 struct mmc_host *mmc;
1121 struct sdhci_host *host;
1123 chip = pci_get_drvdata(pdev);
1124 host = chip->hosts[slot];
1127 chip->hosts[slot] = NULL;
1129 mmc_remove_host(mmc);
1131 sdhci_reset(host, SDHCI_RESET_ALL);
1133 free_irq(host->irq, host);
1135 del_timer_sync(&host->timer);
1137 tasklet_kill(&host->card_tasklet);
1138 tasklet_kill(&host->finish_tasklet);
1140 iounmap(host->ioaddr);
1142 pci_release_region(pdev, host->bar);
1147 static int __devinit sdhci_probe(struct pci_dev *pdev,
1148 const struct pci_device_id *ent)
1152 struct sdhci_chip *chip;
1154 BUG_ON(pdev == NULL);
1155 BUG_ON(ent == NULL);
1157 DBG("found at %s\n", pci_name(pdev));
1159 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1163 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1164 DBG("found %d slot(s)\n", slots);
1168 ret = pci_enable_device(pdev);
1172 chip = kzalloc(sizeof(struct sdhci_chip) +
1173 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1181 chip->num_slots = slots;
1182 pci_set_drvdata(pdev, chip);
1184 for (i = 0;i < slots;i++) {
1185 ret = sdhci_probe_slot(pdev, i);
1187 for (i--;i >= 0;i--)
1188 sdhci_remove_slot(pdev, i);
1196 pci_set_drvdata(pdev, NULL);
1200 pci_disable_device(pdev);
1204 static void __devexit sdhci_remove(struct pci_dev *pdev)
1207 struct sdhci_chip *chip;
1209 chip = pci_get_drvdata(pdev);
1212 for (i = 0;i < chip->num_slots;i++)
1213 sdhci_remove_slot(pdev, i);
1215 pci_set_drvdata(pdev, NULL);
1220 pci_disable_device(pdev);
1223 static struct pci_driver sdhci_driver = {
1224 .name = DRIVER_NAME,
1225 .id_table = pci_ids,
1226 .probe = sdhci_probe,
1227 .remove = __devexit_p(sdhci_remove),
1228 .suspend = sdhci_suspend,
1229 .resume = sdhci_resume,
1232 /*****************************************************************************\
1234 * Driver init/exit *
1236 \*****************************************************************************/
1238 static int __init sdhci_drv_init(void)
1240 printk(KERN_INFO DRIVER_NAME
1241 ": Secure Digital Host Controller Interface driver, "
1242 DRIVER_VERSION "\n");
1243 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1245 return pci_register_driver(&sdhci_driver);
1248 static void __exit sdhci_drv_exit(void)
1252 pci_unregister_driver(&sdhci_driver);
1255 module_init(sdhci_drv_init);
1256 module_exit(sdhci_drv_exit);
1258 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1259 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1260 MODULE_VERSION(DRIVER_VERSION);
1261 MODULE_LICENSE("GPL");