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 #ifdef CONFIG_MMC_DEBUG
35 #define DBG(f, x...) \
36 printk(KERN_DEBUG DRIVER_NAME " [%s()]: " f, __func__,## x)
38 #define DBG(f, x...) do { } while (0)
41 static const struct pci_device_id pci_ids[] __devinitdata = {
42 /* handle any SD host controller */
43 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
44 { /* end: all zeroes */ },
47 MODULE_DEVICE_TABLE(pci, pci_ids);
49 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
50 static void sdhci_finish_data(struct sdhci_host *);
52 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
53 static void sdhci_finish_command(struct sdhci_host *);
55 static void sdhci_dumpregs(struct sdhci_host *host)
57 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
59 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
60 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
61 readw(host->ioaddr + SDHCI_HOST_VERSION));
62 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
63 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
64 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
65 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
66 readl(host->ioaddr + SDHCI_ARGUMENT),
67 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
68 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
69 readl(host->ioaddr + SDHCI_PRESENT_STATE),
70 readb(host->ioaddr + SDHCI_HOST_CONTROL));
71 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
72 readb(host->ioaddr + SDHCI_POWER_CONTROL),
73 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
74 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
75 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
76 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
77 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
78 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
79 readl(host->ioaddr + SDHCI_INT_STATUS));
80 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
81 readl(host->ioaddr + SDHCI_INT_ENABLE),
82 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
83 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
84 readw(host->ioaddr + SDHCI_ACMD12_ERR),
85 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
86 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
87 readl(host->ioaddr + SDHCI_CAPABILITIES),
88 readl(host->ioaddr + SDHCI_MAX_CURRENT));
90 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
93 /*****************************************************************************\
95 * Low level functions *
97 \*****************************************************************************/
99 static void sdhci_reset(struct sdhci_host *host, u8 mask)
101 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
103 if (mask & SDHCI_RESET_ALL) {
110 static void sdhci_init(struct sdhci_host *host)
114 sdhci_reset(host, SDHCI_RESET_ALL);
116 intmask = ~(SDHCI_INT_CARD_INT | SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
118 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
119 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
121 /* This is unknown magic. */
122 writeb(0xE, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
125 static void sdhci_activate_led(struct sdhci_host *host)
129 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
130 ctrl |= SDHCI_CTRL_LED;
131 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
134 static void sdhci_deactivate_led(struct sdhci_host *host)
138 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
139 ctrl &= ~SDHCI_CTRL_LED;
140 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
143 /*****************************************************************************\
147 \*****************************************************************************/
149 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
151 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
152 return host->mapped_sg + host->cur_sg->offset;
155 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
157 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
160 static inline int sdhci_next_sg(struct sdhci_host* host)
163 * Skip to next SG entry.
171 if (host->num_sg > 0) {
173 host->remain = host->cur_sg->length;
179 static void sdhci_transfer_pio(struct sdhci_host *host)
184 unsigned long max_jiffies;
188 if (host->num_sg == 0)
192 if (host->data->flags & MMC_DATA_READ)
193 mask = SDHCI_DATA_AVAILABLE;
195 mask = SDHCI_SPACE_AVAILABLE;
197 buffer = sdhci_kmap_sg(host) + host->offset;
199 /* Transfer shouldn't take more than 5 s */
200 max_jiffies = jiffies + HZ * 5;
202 while (host->size > 0) {
203 if (time_after(jiffies, max_jiffies)) {
204 printk(KERN_ERR "%s: PIO transfer stalled. "
205 "Please report this to "
206 BUGMAIL ".\n", mmc_hostname(host->mmc));
207 sdhci_dumpregs(host);
209 sdhci_kunmap_sg(host);
211 host->data->error = MMC_ERR_FAILED;
212 sdhci_finish_data(host);
216 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask))
219 size = min(host->size, host->remain);
222 if (host->data->flags & MMC_DATA_READ)
223 *(u32*)buffer = readl(host->ioaddr + SDHCI_BUFFER);
225 writel(*(u32*)buffer, host->ioaddr + SDHCI_BUFFER);
227 } else if (size >= 2) {
228 if (host->data->flags & MMC_DATA_READ)
229 *(u16*)buffer = readw(host->ioaddr + SDHCI_BUFFER);
231 writew(*(u16*)buffer, host->ioaddr + SDHCI_BUFFER);
234 if (host->data->flags & MMC_DATA_READ)
235 *(u8*)buffer = readb(host->ioaddr + SDHCI_BUFFER);
237 writeb(*(u8*)buffer, host->ioaddr + SDHCI_BUFFER);
242 host->offset += size;
243 host->remain -= size;
248 if (host->remain == 0) {
249 sdhci_kunmap_sg(host);
250 if (sdhci_next_sg(host) == 0) {
251 DBG("PIO transfer: %d bytes\n", bytes);
254 buffer = sdhci_kmap_sg(host);
258 sdhci_kunmap_sg(host);
260 DBG("PIO transfer: %d bytes\n", bytes);
263 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
270 writew(0, host->ioaddr + SDHCI_TRANSFER_MODE);
274 DBG("blksz %04x blks %04x flags %08x\n",
275 1 << data->blksz_bits, data->blocks, data->flags);
276 DBG("tsac %d ms nsac %d clk\n",
277 data->timeout_ns / 1000000, data->timeout_clks);
279 mode = SDHCI_TRNS_BLK_CNT_EN;
280 if (data->blocks > 1)
281 mode |= SDHCI_TRNS_MULTI;
282 if (data->flags & MMC_DATA_READ)
283 mode |= SDHCI_TRNS_READ;
284 if (host->flags & SDHCI_USE_DMA)
285 mode |= SDHCI_TRNS_DMA;
287 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
289 writew(1 << data->blksz_bits, host->ioaddr + SDHCI_BLOCK_SIZE);
290 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
292 if (host->flags & SDHCI_USE_DMA) {
295 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
296 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
299 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
301 host->size = (1 << data->blksz_bits) * data->blocks;
303 host->cur_sg = data->sg;
304 host->num_sg = data->sg_len;
307 host->remain = host->cur_sg->length;
311 static void sdhci_finish_data(struct sdhci_host *host)
313 struct mmc_data *data;
322 if (host->flags & SDHCI_USE_DMA) {
323 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
324 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
326 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
327 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
328 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
330 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
331 intmask &= ~(SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL);
332 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
336 * Controller doesn't count down when in single block mode.
338 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
341 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
342 data->bytes_xfered = (1 << data->blksz_bits) * (data->blocks - blocks);
344 if ((data->error == MMC_ERR_NONE) && blocks) {
345 printk(KERN_ERR "%s: Controller signalled completion even "
346 "though there were blocks left. Please report this "
347 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
348 data->error = MMC_ERR_FAILED;
351 if (host->size != 0) {
352 printk(KERN_ERR "%s: %d bytes were left untransferred. "
353 "Please report this to " BUGMAIL ".\n",
354 mmc_hostname(host->mmc), host->size);
355 data->error = MMC_ERR_FAILED;
358 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
362 * The controller needs a reset of internal state machines
363 * upon error conditions.
365 if (data->error != MMC_ERR_NONE) {
366 sdhci_reset(host, SDHCI_RESET_CMD);
367 sdhci_reset(host, SDHCI_RESET_DATA);
370 sdhci_send_command(host, data->stop);
372 tasklet_schedule(&host->finish_tasklet);
375 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
379 unsigned long max_jiffies;
383 DBG("Sending cmd (%x)\n", cmd->opcode);
386 max_jiffies = jiffies + (HZ + 99)/100;
388 if (time_after(jiffies, max_jiffies)) {
389 printk(KERN_ERR "%s: Controller never released "
390 "inhibit bits. Please report this to "
391 BUGMAIL ".\n", mmc_hostname(host->mmc));
392 sdhci_dumpregs(host);
393 cmd->error = MMC_ERR_FAILED;
394 tasklet_schedule(&host->finish_tasklet);
397 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
398 } while (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT));
400 mod_timer(&host->timer, jiffies + 10 * HZ);
404 sdhci_prepare_data(host, cmd->data);
406 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
408 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
409 printk(KERN_ERR "%s: Unsupported response type! "
410 "Please report this to " BUGMAIL ".\n",
411 mmc_hostname(host->mmc));
412 cmd->error = MMC_ERR_INVALID;
413 tasklet_schedule(&host->finish_tasklet);
417 if (!(cmd->flags & MMC_RSP_PRESENT))
418 flags = SDHCI_CMD_RESP_NONE;
419 else if (cmd->flags & MMC_RSP_136)
420 flags = SDHCI_CMD_RESP_LONG;
421 else if (cmd->flags & MMC_RSP_BUSY)
422 flags = SDHCI_CMD_RESP_SHORT_BUSY;
424 flags = SDHCI_CMD_RESP_SHORT;
426 if (cmd->flags & MMC_RSP_CRC)
427 flags |= SDHCI_CMD_CRC;
428 if (cmd->flags & MMC_RSP_OPCODE)
429 flags |= SDHCI_CMD_INDEX;
431 flags |= SDHCI_CMD_DATA;
433 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
434 host->ioaddr + SDHCI_COMMAND);
437 static void sdhci_finish_command(struct sdhci_host *host)
441 BUG_ON(host->cmd == NULL);
443 if (host->cmd->flags & MMC_RSP_PRESENT) {
444 if (host->cmd->flags & MMC_RSP_136) {
445 /* CRC is stripped so we need to do some shifting. */
446 for (i = 0;i < 4;i++) {
447 host->cmd->resp[i] = readl(host->ioaddr +
448 SDHCI_RESPONSE + (3-i)*4) << 8;
450 host->cmd->resp[i] |=
452 SDHCI_RESPONSE + (3-i)*4-1);
455 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
459 host->cmd->error = MMC_ERR_NONE;
461 DBG("Ending cmd (%x)\n", host->cmd->opcode);
463 if (host->cmd->data) {
466 host->data = host->cmd->data;
468 if (!(host->flags & SDHCI_USE_DMA)) {
470 * Don't enable the interrupts until now to make sure we
471 * get stable handling of the FIFO.
473 intmask = readl(host->ioaddr + SDHCI_INT_ENABLE);
474 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
475 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
477 intmask = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
478 intmask |= SDHCI_INT_BUF_EMPTY | SDHCI_INT_BUF_FULL;
479 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
482 * The buffer interrupts are to unreliable so we
483 * start the transfer immediatly.
485 sdhci_transfer_pio(host);
488 tasklet_schedule(&host->finish_tasklet);
493 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
497 unsigned long max_jiffies;
499 if (clock == host->clock)
502 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
507 for (div = 1;div < 256;div *= 2) {
508 if ((host->max_clk / div) <= clock)
513 clk = div << SDHCI_DIVIDER_SHIFT;
514 clk |= SDHCI_CLOCK_INT_EN;
515 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
518 max_jiffies = jiffies + (HZ + 99)/100;
520 if (time_after(jiffies, max_jiffies)) {
521 printk(KERN_ERR "%s: Internal clock never stabilised. "
522 "Please report this to " BUGMAIL ".\n",
523 mmc_hostname(host->mmc));
524 sdhci_dumpregs(host);
527 clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL);
528 } while (!(clk & SDHCI_CLOCK_INT_STABLE));
530 clk |= SDHCI_CLOCK_CARD_EN;
531 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
537 /*****************************************************************************\
541 \*****************************************************************************/
543 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
545 struct sdhci_host *host;
548 host = mmc_priv(mmc);
550 spin_lock_irqsave(&host->lock, flags);
552 WARN_ON(host->mrq != NULL);
554 sdhci_activate_led(host);
558 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
559 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
560 tasklet_schedule(&host->finish_tasklet);
562 sdhci_send_command(host, mrq->cmd);
564 spin_unlock_irqrestore(&host->lock, flags);
567 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
569 struct sdhci_host *host;
573 host = mmc_priv(mmc);
575 spin_lock_irqsave(&host->lock, flags);
577 DBG("clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
578 ios->clock, ios->bus_mode, ios->power_mode, ios->chip_select,
579 ios->vdd, ios->bus_width);
582 * Reset the chip on each power off.
583 * Should clear out any weird states.
585 if (ios->power_mode == MMC_POWER_OFF) {
586 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
587 spin_unlock_irqrestore(&host->lock, flags);
589 spin_lock_irqsave(&host->lock, flags);
592 sdhci_set_clock(host, ios->clock);
594 if (ios->power_mode == MMC_POWER_OFF)
595 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
597 writeb(0xFF, host->ioaddr + SDHCI_POWER_CONTROL);
599 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
600 if (ios->bus_width == MMC_BUS_WIDTH_4)
601 ctrl |= SDHCI_CTRL_4BITBUS;
603 ctrl &= ~SDHCI_CTRL_4BITBUS;
604 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
606 spin_unlock_irqrestore(&host->lock, flags);
609 static int sdhci_get_ro(struct mmc_host *mmc)
611 struct sdhci_host *host;
615 host = mmc_priv(mmc);
617 spin_lock_irqsave(&host->lock, flags);
619 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
621 spin_unlock_irqrestore(&host->lock, flags);
623 return !(present & SDHCI_WRITE_PROTECT);
626 static struct mmc_host_ops sdhci_ops = {
627 .request = sdhci_request,
628 .set_ios = sdhci_set_ios,
629 .get_ro = sdhci_get_ro,
632 /*****************************************************************************\
636 \*****************************************************************************/
638 static void sdhci_tasklet_card(unsigned long param)
640 struct sdhci_host *host;
643 host = (struct sdhci_host*)param;
645 spin_lock_irqsave(&host->lock, flags);
647 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
649 printk(KERN_ERR "%s: Card removed during transfer!\n",
650 mmc_hostname(host->mmc));
651 printk(KERN_ERR "%s: Resetting controller.\n",
652 mmc_hostname(host->mmc));
654 sdhci_reset(host, SDHCI_RESET_CMD);
655 sdhci_reset(host, SDHCI_RESET_DATA);
657 host->mrq->cmd->error = MMC_ERR_FAILED;
658 tasklet_schedule(&host->finish_tasklet);
662 spin_unlock_irqrestore(&host->lock, flags);
664 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
667 static void sdhci_tasklet_finish(unsigned long param)
669 struct sdhci_host *host;
671 struct mmc_request *mrq;
673 host = (struct sdhci_host*)param;
675 spin_lock_irqsave(&host->lock, flags);
677 del_timer(&host->timer);
681 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
684 * The controller needs a reset of internal state machines
685 * upon error conditions.
687 if ((mrq->cmd->error != MMC_ERR_NONE) ||
688 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
689 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
690 sdhci_reset(host, SDHCI_RESET_CMD);
691 sdhci_reset(host, SDHCI_RESET_DATA);
698 sdhci_deactivate_led(host);
700 spin_unlock_irqrestore(&host->lock, flags);
702 mmc_request_done(host->mmc, mrq);
705 static void sdhci_timeout_timer(unsigned long data)
707 struct sdhci_host *host;
710 host = (struct sdhci_host*)data;
712 spin_lock_irqsave(&host->lock, flags);
715 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
716 "Please report this to " BUGMAIL ".\n",
717 mmc_hostname(host->mmc));
718 sdhci_dumpregs(host);
721 host->data->error = MMC_ERR_TIMEOUT;
722 sdhci_finish_data(host);
725 host->cmd->error = MMC_ERR_TIMEOUT;
727 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
729 tasklet_schedule(&host->finish_tasklet);
733 spin_unlock_irqrestore(&host->lock, flags);
736 /*****************************************************************************\
738 * Interrupt handling *
740 \*****************************************************************************/
742 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
744 BUG_ON(intmask == 0);
747 printk(KERN_ERR "%s: Got command interrupt even though no "
748 "command operation was in progress.\n",
749 mmc_hostname(host->mmc));
750 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
751 mmc_hostname(host->mmc));
752 sdhci_dumpregs(host);
756 if (intmask & SDHCI_INT_RESPONSE)
757 sdhci_finish_command(host);
759 if (intmask & SDHCI_INT_TIMEOUT)
760 host->cmd->error = MMC_ERR_TIMEOUT;
761 else if (intmask & SDHCI_INT_CRC)
762 host->cmd->error = MMC_ERR_BADCRC;
763 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
764 host->cmd->error = MMC_ERR_FAILED;
766 host->cmd->error = MMC_ERR_INVALID;
768 tasklet_schedule(&host->finish_tasklet);
772 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
774 BUG_ON(intmask == 0);
778 * A data end interrupt is sent together with the response
779 * for the stop command.
781 if (intmask & SDHCI_INT_DATA_END)
784 printk(KERN_ERR "%s: Got data interrupt even though no "
785 "data operation was in progress.\n",
786 mmc_hostname(host->mmc));
787 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
788 mmc_hostname(host->mmc));
789 sdhci_dumpregs(host);
794 if (intmask & SDHCI_INT_DATA_TIMEOUT)
795 host->data->error = MMC_ERR_TIMEOUT;
796 else if (intmask & SDHCI_INT_DATA_CRC)
797 host->data->error = MMC_ERR_BADCRC;
798 else if (intmask & SDHCI_INT_DATA_END_BIT)
799 host->data->error = MMC_ERR_FAILED;
801 if (host->data->error != MMC_ERR_NONE)
802 sdhci_finish_data(host);
804 if (intmask & (SDHCI_INT_BUF_FULL | SDHCI_INT_BUF_EMPTY))
805 sdhci_transfer_pio(host);
807 if (intmask & SDHCI_INT_DATA_END)
808 sdhci_finish_data(host);
812 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
815 struct sdhci_host* host = dev_id;
818 spin_lock(&host->lock);
820 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
827 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
829 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE))
830 tasklet_schedule(&host->card_tasklet);
832 if (intmask & SDHCI_INT_CMD_MASK) {
833 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
835 writel(intmask & SDHCI_INT_CMD_MASK,
836 host->ioaddr + SDHCI_INT_STATUS);
839 if (intmask & SDHCI_INT_DATA_MASK) {
840 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
842 writel(intmask & SDHCI_INT_DATA_MASK,
843 host->ioaddr + SDHCI_INT_STATUS);
846 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
848 if (intmask & SDHCI_INT_CARD_INT) {
849 printk(KERN_ERR "%s: Unexpected card interrupt. Please "
850 "report this to " BUGMAIL ".\n",
851 mmc_hostname(host->mmc));
852 sdhci_dumpregs(host);
855 if (intmask & SDHCI_INT_BUS_POWER) {
856 printk(KERN_ERR "%s: Unexpected bus power interrupt. Please "
857 "report this to " BUGMAIL ".\n",
858 mmc_hostname(host->mmc));
859 sdhci_dumpregs(host);
862 if (intmask & SDHCI_INT_ACMD12ERR) {
863 printk(KERN_ERR "%s: Unexpected auto CMD12 error. Please "
864 "report this to " BUGMAIL ".\n",
865 mmc_hostname(host->mmc));
866 sdhci_dumpregs(host);
868 writew(~0, host->ioaddr + SDHCI_ACMD12_ERR);
872 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
874 result = IRQ_HANDLED;
877 spin_unlock(&host->lock);
882 /*****************************************************************************\
886 \*****************************************************************************/
890 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
892 struct sdhci_chip *chip;
895 chip = pci_get_drvdata(pdev);
899 DBG("Suspending...\n");
901 for (i = 0;i < chip->num_slots;i++) {
904 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
907 mmc_resume_host(chip->hosts[i]->mmc);
912 pci_save_state(pdev);
913 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
914 pci_disable_device(pdev);
915 pci_set_power_state(pdev, pci_choose_state(pdev, state));
920 static int sdhci_resume (struct pci_dev *pdev)
922 struct sdhci_chip *chip;
925 chip = pci_get_drvdata(pdev);
929 DBG("Resuming...\n");
931 pci_set_power_state(pdev, PCI_D0);
932 pci_restore_state(pdev);
933 pci_enable_device(pdev);
935 for (i = 0;i < chip->num_slots;i++) {
938 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
939 pci_set_master(pdev);
940 sdhci_init(chip->hosts[i]);
941 ret = mmc_resume_host(chip->hosts[i]->mmc);
949 #else /* CONFIG_PM */
951 #define sdhci_suspend NULL
952 #define sdhci_resume NULL
954 #endif /* CONFIG_PM */
956 /*****************************************************************************\
958 * Device probing/removal *
960 \*****************************************************************************/
962 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
965 struct sdhci_chip *chip;
966 struct mmc_host *mmc;
967 struct sdhci_host *host;
972 chip = pci_get_drvdata(pdev);
975 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
979 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
982 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
986 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
987 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
991 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
992 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
996 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1000 host = mmc_priv(mmc);
1003 host->bar = first_bar + slot;
1005 host->addr = pci_resource_start(pdev, host->bar);
1006 host->irq = pdev->irq;
1008 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1010 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1012 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1016 host->ioaddr = ioremap_nocache(host->addr,
1017 pci_resource_len(pdev, host->bar));
1018 if (!host->ioaddr) {
1023 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1025 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1026 host->flags |= SDHCI_USE_DMA;
1028 if (host->flags & SDHCI_USE_DMA) {
1029 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1030 printk(KERN_WARNING "%s: No suitable DMA available. "
1031 "Falling back to PIO.\n", host->slot_descr);
1032 host->flags &= ~SDHCI_USE_DMA;
1036 if (host->flags & SDHCI_USE_DMA)
1037 pci_set_master(pdev);
1038 else /* XXX: Hack to get MMC layer to avoid highmem */
1041 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1042 host->max_clk *= 1000000;
1045 * Set host parameters.
1047 mmc->ops = &sdhci_ops;
1048 mmc->f_min = host->max_clk / 256;
1049 mmc->f_max = host->max_clk;
1050 mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
1051 mmc->caps = MMC_CAP_4_BIT_DATA;
1053 spin_lock_init(&host->lock);
1056 * Maximum number of segments. Hardware cannot do scatter lists.
1058 if (host->flags & SDHCI_USE_DMA)
1059 mmc->max_hw_segs = 1;
1061 mmc->max_hw_segs = 16;
1062 mmc->max_phys_segs = 16;
1065 * Maximum number of sectors in one transfer. Limited by sector
1068 mmc->max_sectors = 0x3FFF;
1071 * Maximum segment size. Could be one segment with the maximum number
1074 mmc->max_seg_size = mmc->max_sectors * 512;
1079 tasklet_init(&host->card_tasklet,
1080 sdhci_tasklet_card, (unsigned long)host);
1081 tasklet_init(&host->finish_tasklet,
1082 sdhci_tasklet_finish, (unsigned long)host);
1084 setup_timer(&host->timer, sdhci_timeout_timer, (int)host);
1086 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1087 host->slot_descr, host);
1093 #ifdef CONFIG_MMC_DEBUG
1094 sdhci_dumpregs(host);
1098 chip->hosts[slot] = host;
1102 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1103 host->addr, host->irq,
1104 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1109 tasklet_kill(&host->card_tasklet);
1110 tasklet_kill(&host->finish_tasklet);
1112 iounmap(host->ioaddr);
1114 pci_release_region(pdev, host->bar);
1121 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1123 struct sdhci_chip *chip;
1124 struct mmc_host *mmc;
1125 struct sdhci_host *host;
1127 chip = pci_get_drvdata(pdev);
1128 host = chip->hosts[slot];
1131 chip->hosts[slot] = NULL;
1133 mmc_remove_host(mmc);
1135 sdhci_reset(host, SDHCI_RESET_ALL);
1137 free_irq(host->irq, host);
1139 del_timer_sync(&host->timer);
1141 tasklet_kill(&host->card_tasklet);
1142 tasklet_kill(&host->finish_tasklet);
1144 iounmap(host->ioaddr);
1146 pci_release_region(pdev, host->bar);
1151 static int __devinit sdhci_probe(struct pci_dev *pdev,
1152 const struct pci_device_id *ent)
1156 struct sdhci_chip *chip;
1158 BUG_ON(pdev == NULL);
1159 BUG_ON(ent == NULL);
1161 DBG("found at %s\n", pci_name(pdev));
1163 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1167 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1168 DBG("found %d slot(s)\n", slots);
1172 ret = pci_enable_device(pdev);
1176 chip = kzalloc(sizeof(struct sdhci_chip) +
1177 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1185 chip->num_slots = slots;
1186 pci_set_drvdata(pdev, chip);
1188 for (i = 0;i < slots;i++) {
1189 ret = sdhci_probe_slot(pdev, i);
1191 for (i--;i >= 0;i--)
1192 sdhci_remove_slot(pdev, i);
1200 pci_set_drvdata(pdev, NULL);
1204 pci_disable_device(pdev);
1208 static void __devexit sdhci_remove(struct pci_dev *pdev)
1211 struct sdhci_chip *chip;
1213 chip = pci_get_drvdata(pdev);
1216 for (i = 0;i < chip->num_slots;i++)
1217 sdhci_remove_slot(pdev, i);
1219 pci_set_drvdata(pdev, NULL);
1224 pci_disable_device(pdev);
1227 static struct pci_driver sdhci_driver = {
1228 .name = DRIVER_NAME,
1229 .id_table = pci_ids,
1230 .probe = sdhci_probe,
1231 .remove = __devexit_p(sdhci_remove),
1232 .suspend = sdhci_suspend,
1233 .resume = sdhci_resume,
1236 /*****************************************************************************\
1238 * Driver init/exit *
1240 \*****************************************************************************/
1242 static int __init sdhci_drv_init(void)
1244 printk(KERN_INFO DRIVER_NAME
1245 ": Secure Digital Host Controller Interface driver, "
1246 DRIVER_VERSION "\n");
1247 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1249 return pci_register_driver(&sdhci_driver);
1252 static void __exit sdhci_drv_exit(void)
1256 pci_unregister_driver(&sdhci_driver);
1259 module_init(sdhci_drv_init);
1260 module_exit(sdhci_drv_exit);
1262 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1263 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1264 MODULE_VERSION(DRIVER_VERSION);
1265 MODULE_LICENSE("GPL");