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 const struct pci_device_id pci_ids[] __devinitdata = {
32 /* handle any SD host controller */
33 {PCI_DEVICE_CLASS((PCI_CLASS_SYSTEM_SDHCI << 8), 0xFFFF00)},
34 { /* end: all zeroes */ },
37 MODULE_DEVICE_TABLE(pci, pci_ids);
39 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
40 static void sdhci_finish_data(struct sdhci_host *);
42 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
43 static void sdhci_finish_command(struct sdhci_host *);
45 static void sdhci_dumpregs(struct sdhci_host *host)
47 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
49 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
50 readl(host->ioaddr + SDHCI_DMA_ADDRESS),
51 readw(host->ioaddr + SDHCI_HOST_VERSION));
52 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
53 readw(host->ioaddr + SDHCI_BLOCK_SIZE),
54 readw(host->ioaddr + SDHCI_BLOCK_COUNT));
55 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
56 readl(host->ioaddr + SDHCI_ARGUMENT),
57 readw(host->ioaddr + SDHCI_TRANSFER_MODE));
58 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
59 readl(host->ioaddr + SDHCI_PRESENT_STATE),
60 readb(host->ioaddr + SDHCI_HOST_CONTROL));
61 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
62 readb(host->ioaddr + SDHCI_POWER_CONTROL),
63 readb(host->ioaddr + SDHCI_BLOCK_GAP_CONTROL));
64 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
65 readb(host->ioaddr + SDHCI_WALK_UP_CONTROL),
66 readw(host->ioaddr + SDHCI_CLOCK_CONTROL));
67 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
68 readb(host->ioaddr + SDHCI_TIMEOUT_CONTROL),
69 readl(host->ioaddr + SDHCI_INT_STATUS));
70 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
71 readl(host->ioaddr + SDHCI_INT_ENABLE),
72 readl(host->ioaddr + SDHCI_SIGNAL_ENABLE));
73 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
74 readw(host->ioaddr + SDHCI_ACMD12_ERR),
75 readw(host->ioaddr + SDHCI_SLOT_INT_STATUS));
76 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
77 readl(host->ioaddr + SDHCI_CAPABILITIES),
78 readl(host->ioaddr + SDHCI_MAX_CURRENT));
80 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
83 /*****************************************************************************\
85 * Low level functions *
87 \*****************************************************************************/
89 static void sdhci_reset(struct sdhci_host *host, u8 mask)
91 unsigned long timeout;
93 writeb(mask, host->ioaddr + SDHCI_SOFTWARE_RESET);
95 if (mask & SDHCI_RESET_ALL)
101 /* hw clears the bit when it's done */
102 while (readb(host->ioaddr + SDHCI_SOFTWARE_RESET) & mask) {
104 printk(KERN_ERR "%s: Reset 0x%x never completed. "
105 "Please report this to " BUGMAIL ".\n",
106 mmc_hostname(host->mmc), (int)mask);
107 sdhci_dumpregs(host);
115 static void sdhci_init(struct sdhci_host *host)
119 sdhci_reset(host, SDHCI_RESET_ALL);
121 intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
122 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
123 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
124 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT |
125 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL |
126 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE;
128 writel(intmask, host->ioaddr + SDHCI_INT_ENABLE);
129 writel(intmask, host->ioaddr + SDHCI_SIGNAL_ENABLE);
132 static void sdhci_activate_led(struct sdhci_host *host)
136 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
137 ctrl |= SDHCI_CTRL_LED;
138 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
141 static void sdhci_deactivate_led(struct sdhci_host *host)
145 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
146 ctrl &= ~SDHCI_CTRL_LED;
147 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
150 /*****************************************************************************\
154 \*****************************************************************************/
156 static inline char* sdhci_kmap_sg(struct sdhci_host* host)
158 host->mapped_sg = kmap_atomic(host->cur_sg->page, KM_BIO_SRC_IRQ);
159 return host->mapped_sg + host->cur_sg->offset;
162 static inline void sdhci_kunmap_sg(struct sdhci_host* host)
164 kunmap_atomic(host->mapped_sg, KM_BIO_SRC_IRQ);
167 static inline int sdhci_next_sg(struct sdhci_host* host)
170 * Skip to next SG entry.
178 if (host->num_sg > 0) {
180 host->remain = host->cur_sg->length;
186 static void sdhci_read_block_pio(struct sdhci_host *host)
188 int blksize, chunk_remain;
193 DBG("PIO reading\n");
195 blksize = host->data->blksz;
199 buffer = sdhci_kmap_sg(host) + host->offset;
202 if (chunk_remain == 0) {
203 data = readl(host->ioaddr + SDHCI_BUFFER);
204 chunk_remain = min(blksize, 4);
207 size = min(host->size, host->remain);
208 size = min(size, chunk_remain);
210 chunk_remain -= size;
212 host->offset += size;
213 host->remain -= size;
216 *buffer = data & 0xFF;
222 if (host->remain == 0) {
223 sdhci_kunmap_sg(host);
224 if (sdhci_next_sg(host) == 0) {
225 BUG_ON(blksize != 0);
228 buffer = sdhci_kmap_sg(host);
232 sdhci_kunmap_sg(host);
235 static void sdhci_write_block_pio(struct sdhci_host *host)
237 int blksize, chunk_remain;
242 DBG("PIO writing\n");
244 blksize = host->data->blksz;
249 buffer = sdhci_kmap_sg(host) + host->offset;
252 size = min(host->size, host->remain);
253 size = min(size, chunk_remain);
255 chunk_remain -= size;
257 host->offset += size;
258 host->remain -= size;
262 data |= (u32)*buffer << 24;
267 if (chunk_remain == 0) {
268 writel(data, host->ioaddr + SDHCI_BUFFER);
269 chunk_remain = min(blksize, 4);
272 if (host->remain == 0) {
273 sdhci_kunmap_sg(host);
274 if (sdhci_next_sg(host) == 0) {
275 BUG_ON(blksize != 0);
278 buffer = sdhci_kmap_sg(host);
282 sdhci_kunmap_sg(host);
285 static void sdhci_transfer_pio(struct sdhci_host *host)
294 if (host->data->flags & MMC_DATA_READ)
295 mask = SDHCI_DATA_AVAILABLE;
297 mask = SDHCI_SPACE_AVAILABLE;
299 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) & mask) {
300 if (host->data->flags & MMC_DATA_READ)
301 sdhci_read_block_pio(host);
303 sdhci_write_block_pio(host);
308 BUG_ON(host->num_sg == 0);
311 DBG("PIO transfer complete.\n");
314 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
317 unsigned target_timeout, current_timeout;
324 DBG("blksz %04x blks %04x flags %08x\n",
325 data->blksz, data->blocks, data->flags);
326 DBG("tsac %d ms nsac %d clk\n",
327 data->timeout_ns / 1000000, data->timeout_clks);
330 BUG_ON(data->blksz * data->blocks > 524288);
333 target_timeout = data->timeout_ns / 1000 +
334 data->timeout_clks / host->clock;
337 * Figure out needed cycles.
338 * We do this in steps in order to fit inside a 32 bit int.
339 * The first step is the minimum timeout, which will have a
340 * minimum resolution of 6 bits:
341 * (1) 2^13*1000 > 2^22,
342 * (2) host->timeout_clk < 2^16
347 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
348 while (current_timeout < target_timeout) {
350 current_timeout <<= 1;
356 printk(KERN_WARNING "%s: Too large timeout requested!\n",
357 mmc_hostname(host->mmc));
361 writeb(count, host->ioaddr + SDHCI_TIMEOUT_CONTROL);
363 if (host->flags & SDHCI_USE_DMA) {
366 count = pci_map_sg(host->chip->pdev, data->sg, data->sg_len,
367 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
370 writel(sg_dma_address(data->sg), host->ioaddr + SDHCI_DMA_ADDRESS);
372 host->size = data->blksz * data->blocks;
374 host->cur_sg = data->sg;
375 host->num_sg = data->sg_len;
378 host->remain = host->cur_sg->length;
381 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
382 writew(SDHCI_MAKE_BLKSZ(7, data->blksz),
383 host->ioaddr + SDHCI_BLOCK_SIZE);
384 writew(data->blocks, host->ioaddr + SDHCI_BLOCK_COUNT);
387 static void sdhci_set_transfer_mode(struct sdhci_host *host,
388 struct mmc_data *data)
397 mode = SDHCI_TRNS_BLK_CNT_EN;
398 if (data->blocks > 1)
399 mode |= SDHCI_TRNS_MULTI;
400 if (data->flags & MMC_DATA_READ)
401 mode |= SDHCI_TRNS_READ;
402 if (host->flags & SDHCI_USE_DMA)
403 mode |= SDHCI_TRNS_DMA;
405 writew(mode, host->ioaddr + SDHCI_TRANSFER_MODE);
408 static void sdhci_finish_data(struct sdhci_host *host)
410 struct mmc_data *data;
418 if (host->flags & SDHCI_USE_DMA) {
419 pci_unmap_sg(host->chip->pdev, data->sg, data->sg_len,
420 (data->flags & MMC_DATA_READ)?PCI_DMA_FROMDEVICE:PCI_DMA_TODEVICE);
424 * Controller doesn't count down when in single block mode.
426 if ((data->blocks == 1) && (data->error == MMC_ERR_NONE))
429 blocks = readw(host->ioaddr + SDHCI_BLOCK_COUNT);
430 data->bytes_xfered = data->blksz * (data->blocks - blocks);
432 if ((data->error == MMC_ERR_NONE) && blocks) {
433 printk(KERN_ERR "%s: Controller signalled completion even "
434 "though there were blocks left. Please report this "
435 "to " BUGMAIL ".\n", mmc_hostname(host->mmc));
436 data->error = MMC_ERR_FAILED;
439 if (host->size != 0) {
440 printk(KERN_ERR "%s: %d bytes were left untransferred. "
441 "Please report this to " BUGMAIL ".\n",
442 mmc_hostname(host->mmc), host->size);
443 data->error = MMC_ERR_FAILED;
446 DBG("Ending data transfer (%d bytes)\n", data->bytes_xfered);
450 * The controller needs a reset of internal state machines
451 * upon error conditions.
453 if (data->error != MMC_ERR_NONE) {
454 sdhci_reset(host, SDHCI_RESET_CMD);
455 sdhci_reset(host, SDHCI_RESET_DATA);
458 sdhci_send_command(host, data->stop);
460 tasklet_schedule(&host->finish_tasklet);
463 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
466 unsigned long timeout;
470 DBG("Sending cmd (%x)\n", cmd->opcode);
474 while (readl(host->ioaddr + SDHCI_PRESENT_STATE) &
475 (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
477 printk(KERN_ERR "%s: Controller never released "
478 "inhibit bits. Please report this to "
479 BUGMAIL ".\n", mmc_hostname(host->mmc));
480 sdhci_dumpregs(host);
481 cmd->error = MMC_ERR_FAILED;
482 tasklet_schedule(&host->finish_tasklet);
489 mod_timer(&host->timer, jiffies + 10 * HZ);
493 sdhci_prepare_data(host, cmd->data);
495 writel(cmd->arg, host->ioaddr + SDHCI_ARGUMENT);
497 sdhci_set_transfer_mode(host, cmd->data);
499 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
500 printk(KERN_ERR "%s: Unsupported response type! "
501 "Please report this to " BUGMAIL ".\n",
502 mmc_hostname(host->mmc));
503 cmd->error = MMC_ERR_INVALID;
504 tasklet_schedule(&host->finish_tasklet);
508 if (!(cmd->flags & MMC_RSP_PRESENT))
509 flags = SDHCI_CMD_RESP_NONE;
510 else if (cmd->flags & MMC_RSP_136)
511 flags = SDHCI_CMD_RESP_LONG;
512 else if (cmd->flags & MMC_RSP_BUSY)
513 flags = SDHCI_CMD_RESP_SHORT_BUSY;
515 flags = SDHCI_CMD_RESP_SHORT;
517 if (cmd->flags & MMC_RSP_CRC)
518 flags |= SDHCI_CMD_CRC;
519 if (cmd->flags & MMC_RSP_OPCODE)
520 flags |= SDHCI_CMD_INDEX;
522 flags |= SDHCI_CMD_DATA;
524 writel(SDHCI_MAKE_CMD(cmd->opcode, flags),
525 host->ioaddr + SDHCI_COMMAND);
528 static void sdhci_finish_command(struct sdhci_host *host)
532 BUG_ON(host->cmd == NULL);
534 if (host->cmd->flags & MMC_RSP_PRESENT) {
535 if (host->cmd->flags & MMC_RSP_136) {
536 /* CRC is stripped so we need to do some shifting. */
537 for (i = 0;i < 4;i++) {
538 host->cmd->resp[i] = readl(host->ioaddr +
539 SDHCI_RESPONSE + (3-i)*4) << 8;
541 host->cmd->resp[i] |=
543 SDHCI_RESPONSE + (3-i)*4-1);
546 host->cmd->resp[0] = readl(host->ioaddr + SDHCI_RESPONSE);
550 host->cmd->error = MMC_ERR_NONE;
552 DBG("Ending cmd (%x)\n", host->cmd->opcode);
555 host->data = host->cmd->data;
557 tasklet_schedule(&host->finish_tasklet);
562 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
566 unsigned long timeout;
568 if (clock == host->clock)
571 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
576 for (div = 1;div < 256;div *= 2) {
577 if ((host->max_clk / div) <= clock)
582 clk = div << SDHCI_DIVIDER_SHIFT;
583 clk |= SDHCI_CLOCK_INT_EN;
584 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
588 while (!((clk = readw(host->ioaddr + SDHCI_CLOCK_CONTROL))
589 & SDHCI_CLOCK_INT_STABLE)) {
591 printk(KERN_ERR "%s: Internal clock never stabilised. "
592 "Please report this to " BUGMAIL ".\n",
593 mmc_hostname(host->mmc));
594 sdhci_dumpregs(host);
601 clk |= SDHCI_CLOCK_CARD_EN;
602 writew(clk, host->ioaddr + SDHCI_CLOCK_CONTROL);
608 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
612 if (host->power == power)
615 writeb(0, host->ioaddr + SDHCI_POWER_CONTROL);
617 if (power == (unsigned short)-1)
620 pwr = SDHCI_POWER_ON;
626 pwr |= SDHCI_POWER_180;
631 pwr |= SDHCI_POWER_300;
636 pwr |= SDHCI_POWER_330;
642 writeb(pwr, host->ioaddr + SDHCI_POWER_CONTROL);
648 /*****************************************************************************\
652 \*****************************************************************************/
654 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
656 struct sdhci_host *host;
659 host = mmc_priv(mmc);
661 spin_lock_irqsave(&host->lock, flags);
663 WARN_ON(host->mrq != NULL);
665 sdhci_activate_led(host);
669 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
670 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
671 tasklet_schedule(&host->finish_tasklet);
673 sdhci_send_command(host, mrq->cmd);
675 spin_unlock_irqrestore(&host->lock, flags);
678 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
680 struct sdhci_host *host;
684 host = mmc_priv(mmc);
686 spin_lock_irqsave(&host->lock, flags);
689 * Reset the chip on each power off.
690 * Should clear out any weird states.
692 if (ios->power_mode == MMC_POWER_OFF) {
693 writel(0, host->ioaddr + SDHCI_SIGNAL_ENABLE);
697 sdhci_set_clock(host, ios->clock);
699 if (ios->power_mode == MMC_POWER_OFF)
700 sdhci_set_power(host, -1);
702 sdhci_set_power(host, ios->vdd);
704 ctrl = readb(host->ioaddr + SDHCI_HOST_CONTROL);
705 if (ios->bus_width == MMC_BUS_WIDTH_4)
706 ctrl |= SDHCI_CTRL_4BITBUS;
708 ctrl &= ~SDHCI_CTRL_4BITBUS;
709 writeb(ctrl, host->ioaddr + SDHCI_HOST_CONTROL);
711 spin_unlock_irqrestore(&host->lock, flags);
714 static int sdhci_get_ro(struct mmc_host *mmc)
716 struct sdhci_host *host;
720 host = mmc_priv(mmc);
722 spin_lock_irqsave(&host->lock, flags);
724 present = readl(host->ioaddr + SDHCI_PRESENT_STATE);
726 spin_unlock_irqrestore(&host->lock, flags);
728 return !(present & SDHCI_WRITE_PROTECT);
731 static struct mmc_host_ops sdhci_ops = {
732 .request = sdhci_request,
733 .set_ios = sdhci_set_ios,
734 .get_ro = sdhci_get_ro,
737 /*****************************************************************************\
741 \*****************************************************************************/
743 static void sdhci_tasklet_card(unsigned long param)
745 struct sdhci_host *host;
748 host = (struct sdhci_host*)param;
750 spin_lock_irqsave(&host->lock, flags);
752 if (!(readl(host->ioaddr + SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
754 printk(KERN_ERR "%s: Card removed during transfer!\n",
755 mmc_hostname(host->mmc));
756 printk(KERN_ERR "%s: Resetting controller.\n",
757 mmc_hostname(host->mmc));
759 sdhci_reset(host, SDHCI_RESET_CMD);
760 sdhci_reset(host, SDHCI_RESET_DATA);
762 host->mrq->cmd->error = MMC_ERR_FAILED;
763 tasklet_schedule(&host->finish_tasklet);
767 spin_unlock_irqrestore(&host->lock, flags);
769 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
772 static void sdhci_tasklet_finish(unsigned long param)
774 struct sdhci_host *host;
776 struct mmc_request *mrq;
778 host = (struct sdhci_host*)param;
780 spin_lock_irqsave(&host->lock, flags);
782 del_timer(&host->timer);
786 DBG("Ending request, cmd (%x)\n", mrq->cmd->opcode);
789 * The controller needs a reset of internal state machines
790 * upon error conditions.
792 if ((mrq->cmd->error != MMC_ERR_NONE) ||
793 (mrq->data && ((mrq->data->error != MMC_ERR_NONE) ||
794 (mrq->data->stop && (mrq->data->stop->error != MMC_ERR_NONE))))) {
795 sdhci_reset(host, SDHCI_RESET_CMD);
796 sdhci_reset(host, SDHCI_RESET_DATA);
803 sdhci_deactivate_led(host);
805 spin_unlock_irqrestore(&host->lock, flags);
807 mmc_request_done(host->mmc, mrq);
810 static void sdhci_timeout_timer(unsigned long data)
812 struct sdhci_host *host;
815 host = (struct sdhci_host*)data;
817 spin_lock_irqsave(&host->lock, flags);
820 printk(KERN_ERR "%s: Timeout waiting for hardware interrupt. "
821 "Please report this to " BUGMAIL ".\n",
822 mmc_hostname(host->mmc));
823 sdhci_dumpregs(host);
826 host->data->error = MMC_ERR_TIMEOUT;
827 sdhci_finish_data(host);
830 host->cmd->error = MMC_ERR_TIMEOUT;
832 host->mrq->cmd->error = MMC_ERR_TIMEOUT;
834 tasklet_schedule(&host->finish_tasklet);
838 spin_unlock_irqrestore(&host->lock, flags);
841 /*****************************************************************************\
843 * Interrupt handling *
845 \*****************************************************************************/
847 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
849 BUG_ON(intmask == 0);
852 printk(KERN_ERR "%s: Got command interrupt even though no "
853 "command operation was in progress.\n",
854 mmc_hostname(host->mmc));
855 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
856 mmc_hostname(host->mmc));
857 sdhci_dumpregs(host);
861 if (intmask & SDHCI_INT_RESPONSE)
862 sdhci_finish_command(host);
864 if (intmask & SDHCI_INT_TIMEOUT)
865 host->cmd->error = MMC_ERR_TIMEOUT;
866 else if (intmask & SDHCI_INT_CRC)
867 host->cmd->error = MMC_ERR_BADCRC;
868 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX))
869 host->cmd->error = MMC_ERR_FAILED;
871 host->cmd->error = MMC_ERR_INVALID;
873 tasklet_schedule(&host->finish_tasklet);
877 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
879 BUG_ON(intmask == 0);
883 * A data end interrupt is sent together with the response
884 * for the stop command.
886 if (intmask & SDHCI_INT_DATA_END)
889 printk(KERN_ERR "%s: Got data interrupt even though no "
890 "data operation was in progress.\n",
891 mmc_hostname(host->mmc));
892 printk(KERN_ERR "%s: Please report this to " BUGMAIL ".\n",
893 mmc_hostname(host->mmc));
894 sdhci_dumpregs(host);
899 if (intmask & SDHCI_INT_DATA_TIMEOUT)
900 host->data->error = MMC_ERR_TIMEOUT;
901 else if (intmask & SDHCI_INT_DATA_CRC)
902 host->data->error = MMC_ERR_BADCRC;
903 else if (intmask & SDHCI_INT_DATA_END_BIT)
904 host->data->error = MMC_ERR_FAILED;
906 if (host->data->error != MMC_ERR_NONE)
907 sdhci_finish_data(host);
909 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
910 sdhci_transfer_pio(host);
912 if (intmask & SDHCI_INT_DATA_END)
913 sdhci_finish_data(host);
917 static irqreturn_t sdhci_irq(int irq, void *dev_id, struct pt_regs *regs)
920 struct sdhci_host* host = dev_id;
923 spin_lock(&host->lock);
925 intmask = readl(host->ioaddr + SDHCI_INT_STATUS);
932 DBG("*** %s got interrupt: 0x%08x\n", host->slot_descr, intmask);
934 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
935 writel(intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE),
936 host->ioaddr + SDHCI_INT_STATUS);
937 tasklet_schedule(&host->card_tasklet);
940 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
942 if (intmask & SDHCI_INT_CMD_MASK) {
943 writel(intmask & SDHCI_INT_CMD_MASK,
944 host->ioaddr + SDHCI_INT_STATUS);
945 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
948 if (intmask & SDHCI_INT_DATA_MASK) {
949 writel(intmask & SDHCI_INT_DATA_MASK,
950 host->ioaddr + SDHCI_INT_STATUS);
951 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
954 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
956 if (intmask & SDHCI_INT_BUS_POWER) {
957 printk(KERN_ERR "%s: Card is consuming too much power!\n",
958 mmc_hostname(host->mmc));
959 writel(SDHCI_INT_BUS_POWER, host->ioaddr + SDHCI_INT_STATUS);
962 intmask &= SDHCI_INT_BUS_POWER;
965 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x. Please "
966 "report this to " BUGMAIL ".\n",
967 mmc_hostname(host->mmc), intmask);
968 sdhci_dumpregs(host);
970 writel(intmask, host->ioaddr + SDHCI_INT_STATUS);
973 result = IRQ_HANDLED;
976 spin_unlock(&host->lock);
981 /*****************************************************************************\
985 \*****************************************************************************/
989 static int sdhci_suspend (struct pci_dev *pdev, pm_message_t state)
991 struct sdhci_chip *chip;
994 chip = pci_get_drvdata(pdev);
998 DBG("Suspending...\n");
1000 for (i = 0;i < chip->num_slots;i++) {
1001 if (!chip->hosts[i])
1003 ret = mmc_suspend_host(chip->hosts[i]->mmc, state);
1005 for (i--;i >= 0;i--)
1006 mmc_resume_host(chip->hosts[i]->mmc);
1011 pci_save_state(pdev);
1012 pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
1013 pci_disable_device(pdev);
1014 pci_set_power_state(pdev, pci_choose_state(pdev, state));
1019 static int sdhci_resume (struct pci_dev *pdev)
1021 struct sdhci_chip *chip;
1024 chip = pci_get_drvdata(pdev);
1028 DBG("Resuming...\n");
1030 pci_set_power_state(pdev, PCI_D0);
1031 pci_restore_state(pdev);
1032 pci_enable_device(pdev);
1034 for (i = 0;i < chip->num_slots;i++) {
1035 if (!chip->hosts[i])
1037 if (chip->hosts[i]->flags & SDHCI_USE_DMA)
1038 pci_set_master(pdev);
1039 sdhci_init(chip->hosts[i]);
1040 ret = mmc_resume_host(chip->hosts[i]->mmc);
1048 #else /* CONFIG_PM */
1050 #define sdhci_suspend NULL
1051 #define sdhci_resume NULL
1053 #endif /* CONFIG_PM */
1055 /*****************************************************************************\
1057 * Device probing/removal *
1059 \*****************************************************************************/
1061 static int __devinit sdhci_probe_slot(struct pci_dev *pdev, int slot)
1064 struct sdhci_chip *chip;
1065 struct mmc_host *mmc;
1066 struct sdhci_host *host;
1071 chip = pci_get_drvdata(pdev);
1074 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &first_bar);
1078 first_bar &= PCI_SLOT_INFO_FIRST_BAR_MASK;
1080 if (first_bar > 5) {
1081 printk(KERN_ERR DRIVER_NAME ": Invalid first BAR. Aborting.\n");
1085 if (!(pci_resource_flags(pdev, first_bar + slot) & IORESOURCE_MEM)) {
1086 printk(KERN_ERR DRIVER_NAME ": BAR is not iomem. Aborting.\n");
1090 if (pci_resource_len(pdev, first_bar + slot) != 0x100) {
1091 printk(KERN_ERR DRIVER_NAME ": Invalid iomem size. Aborting.\n");
1095 mmc = mmc_alloc_host(sizeof(struct sdhci_host), &pdev->dev);
1099 host = mmc_priv(mmc);
1102 host->bar = first_bar + slot;
1104 host->addr = pci_resource_start(pdev, host->bar);
1105 host->irq = pdev->irq;
1107 DBG("slot %d at 0x%08lx, irq %d\n", slot, host->addr, host->irq);
1109 snprintf(host->slot_descr, 20, "sdhci:slot%d", slot);
1111 ret = pci_request_region(pdev, host->bar, host->slot_descr);
1115 host->ioaddr = ioremap_nocache(host->addr,
1116 pci_resource_len(pdev, host->bar));
1117 if (!host->ioaddr) {
1122 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
1124 if ((caps & SDHCI_CAN_DO_DMA) && ((pdev->class & 0x0000FF) == 0x01))
1125 host->flags |= SDHCI_USE_DMA;
1127 if (host->flags & SDHCI_USE_DMA) {
1128 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
1129 printk(KERN_WARNING "%s: No suitable DMA available. "
1130 "Falling back to PIO.\n", host->slot_descr);
1131 host->flags &= ~SDHCI_USE_DMA;
1135 if (host->flags & SDHCI_USE_DMA)
1136 pci_set_master(pdev);
1137 else /* XXX: Hack to get MMC layer to avoid highmem */
1141 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1142 if (host->max_clk == 0) {
1143 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
1144 "frequency.\n", host->slot_descr);
1148 host->max_clk *= 1000000;
1151 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1152 if (host->timeout_clk == 0) {
1153 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
1154 "frequency.\n", host->slot_descr);
1158 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1159 host->timeout_clk *= 1000;
1162 * Set host parameters.
1164 mmc->ops = &sdhci_ops;
1165 mmc->f_min = host->max_clk / 256;
1166 mmc->f_max = host->max_clk;
1167 mmc->caps = MMC_CAP_4_BIT_DATA;
1170 if (caps & SDHCI_CAN_VDD_330)
1171 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1172 else if (caps & SDHCI_CAN_VDD_300)
1173 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1174 else if (caps & SDHCI_CAN_VDD_180)
1175 mmc->ocr_avail |= MMC_VDD_17_18|MMC_VDD_18_19;
1177 if (mmc->ocr_avail == 0) {
1178 printk(KERN_ERR "%s: Hardware doesn't report any "
1179 "support voltages.\n", host->slot_descr);
1184 spin_lock_init(&host->lock);
1187 * Maximum number of segments. Hardware cannot do scatter lists.
1189 if (host->flags & SDHCI_USE_DMA)
1190 mmc->max_hw_segs = 1;
1192 mmc->max_hw_segs = 16;
1193 mmc->max_phys_segs = 16;
1196 * Maximum number of sectors in one transfer. Limited by DMA boundary
1197 * size (512KiB), which means (512 KiB/512=) 1024 entries.
1199 mmc->max_sectors = 1024;
1202 * Maximum segment size. Could be one segment with the maximum number
1205 mmc->max_seg_size = mmc->max_sectors * 512;
1210 tasklet_init(&host->card_tasklet,
1211 sdhci_tasklet_card, (unsigned long)host);
1212 tasklet_init(&host->finish_tasklet,
1213 sdhci_tasklet_finish, (unsigned long)host);
1215 setup_timer(&host->timer, sdhci_timeout_timer, (long)host);
1217 ret = request_irq(host->irq, sdhci_irq, SA_SHIRQ,
1218 host->slot_descr, host);
1224 #ifdef CONFIG_MMC_DEBUG
1225 sdhci_dumpregs(host);
1229 chip->hosts[slot] = host;
1233 printk(KERN_INFO "%s: SDHCI at 0x%08lx irq %d %s\n", mmc_hostname(mmc),
1234 host->addr, host->irq,
1235 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1240 tasklet_kill(&host->card_tasklet);
1241 tasklet_kill(&host->finish_tasklet);
1243 iounmap(host->ioaddr);
1245 pci_release_region(pdev, host->bar);
1252 static void sdhci_remove_slot(struct pci_dev *pdev, int slot)
1254 struct sdhci_chip *chip;
1255 struct mmc_host *mmc;
1256 struct sdhci_host *host;
1258 chip = pci_get_drvdata(pdev);
1259 host = chip->hosts[slot];
1262 chip->hosts[slot] = NULL;
1264 mmc_remove_host(mmc);
1266 sdhci_reset(host, SDHCI_RESET_ALL);
1268 free_irq(host->irq, host);
1270 del_timer_sync(&host->timer);
1272 tasklet_kill(&host->card_tasklet);
1273 tasklet_kill(&host->finish_tasklet);
1275 iounmap(host->ioaddr);
1277 pci_release_region(pdev, host->bar);
1282 static int __devinit sdhci_probe(struct pci_dev *pdev,
1283 const struct pci_device_id *ent)
1287 struct sdhci_chip *chip;
1289 BUG_ON(pdev == NULL);
1290 BUG_ON(ent == NULL);
1292 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
1294 printk(KERN_INFO DRIVER_NAME
1295 ": SDHCI controller found at %s [%04x:%04x] (rev %x)\n",
1296 pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
1299 ret = pci_read_config_byte(pdev, PCI_SLOT_INFO, &slots);
1303 slots = PCI_SLOT_INFO_SLOTS(slots) + 1;
1304 DBG("found %d slot(s)\n", slots);
1308 ret = pci_enable_device(pdev);
1312 chip = kzalloc(sizeof(struct sdhci_chip) +
1313 sizeof(struct sdhci_host*) * slots, GFP_KERNEL);
1321 chip->num_slots = slots;
1322 pci_set_drvdata(pdev, chip);
1324 for (i = 0;i < slots;i++) {
1325 ret = sdhci_probe_slot(pdev, i);
1327 for (i--;i >= 0;i--)
1328 sdhci_remove_slot(pdev, i);
1336 pci_set_drvdata(pdev, NULL);
1340 pci_disable_device(pdev);
1344 static void __devexit sdhci_remove(struct pci_dev *pdev)
1347 struct sdhci_chip *chip;
1349 chip = pci_get_drvdata(pdev);
1352 for (i = 0;i < chip->num_slots;i++)
1353 sdhci_remove_slot(pdev, i);
1355 pci_set_drvdata(pdev, NULL);
1360 pci_disable_device(pdev);
1363 static struct pci_driver sdhci_driver = {
1364 .name = DRIVER_NAME,
1365 .id_table = pci_ids,
1366 .probe = sdhci_probe,
1367 .remove = __devexit_p(sdhci_remove),
1368 .suspend = sdhci_suspend,
1369 .resume = sdhci_resume,
1372 /*****************************************************************************\
1374 * Driver init/exit *
1376 \*****************************************************************************/
1378 static int __init sdhci_drv_init(void)
1380 printk(KERN_INFO DRIVER_NAME
1381 ": Secure Digital Host Controller Interface driver, "
1382 DRIVER_VERSION "\n");
1383 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1385 return pci_register_driver(&sdhci_driver);
1388 static void __exit sdhci_drv_exit(void)
1392 pci_unregister_driver(&sdhci_driver);
1395 module_init(sdhci_drv_init);
1396 module_exit(sdhci_drv_exit);
1398 MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
1399 MODULE_DESCRIPTION("Secure Digital Host Controller Interface driver");
1400 MODULE_VERSION(DRIVER_VERSION);
1401 MODULE_LICENSE("GPL");