2 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2008 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 as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * Thanks to the following companies for their support:
13 * - JMicron (hardware and technical support)
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/scatterlist.h>
22 #include <linux/leds.h>
24 #include <linux/mmc/host.h>
28 #define DRIVER_NAME "sdhci"
30 #define DBG(f, x...) \
31 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
33 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
34 defined(CONFIG_MMC_SDHCI_MODULE))
35 #define SDHCI_USE_LEDS_CLASS
38 static unsigned int debug_quirks = 0;
40 static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
41 static void sdhci_finish_data(struct sdhci_host *);
43 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
44 static void sdhci_finish_command(struct sdhci_host *);
46 static void sdhci_dumpregs(struct sdhci_host *host)
48 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
50 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
51 sdhci_readl(host, SDHCI_DMA_ADDRESS),
52 sdhci_readw(host, SDHCI_HOST_VERSION));
53 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
54 sdhci_readw(host, SDHCI_BLOCK_SIZE),
55 sdhci_readw(host, SDHCI_BLOCK_COUNT));
56 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
57 sdhci_readl(host, SDHCI_ARGUMENT),
58 sdhci_readw(host, SDHCI_TRANSFER_MODE));
59 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
60 sdhci_readl(host, SDHCI_PRESENT_STATE),
61 sdhci_readb(host, SDHCI_HOST_CONTROL));
62 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
63 sdhci_readb(host, SDHCI_POWER_CONTROL),
64 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
65 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
66 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
67 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
68 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
69 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
70 sdhci_readl(host, SDHCI_INT_STATUS));
71 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
72 sdhci_readl(host, SDHCI_INT_ENABLE),
73 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
74 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
75 sdhci_readw(host, SDHCI_ACMD12_ERR),
76 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
77 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
78 sdhci_readl(host, SDHCI_CAPABILITIES),
79 sdhci_readl(host, SDHCI_MAX_CURRENT));
81 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
84 /*****************************************************************************\
86 * Low level functions *
88 \*****************************************************************************/
90 static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
94 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
97 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
98 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
101 static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
103 sdhci_clear_set_irqs(host, 0, irqs);
106 static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
108 sdhci_clear_set_irqs(host, irqs, 0);
111 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
113 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
115 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
119 sdhci_unmask_irqs(host, irqs);
121 sdhci_mask_irqs(host, irqs);
124 static void sdhci_enable_card_detection(struct sdhci_host *host)
126 sdhci_set_card_detection(host, true);
129 static void sdhci_disable_card_detection(struct sdhci_host *host)
131 sdhci_set_card_detection(host, false);
134 static void sdhci_reset(struct sdhci_host *host, u8 mask)
136 unsigned long timeout;
137 u32 uninitialized_var(ier);
139 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
140 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
145 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
146 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
148 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
150 if (mask & SDHCI_RESET_ALL)
153 /* Wait max 100 ms */
156 /* hw clears the bit when it's done */
157 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
159 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
160 mmc_hostname(host->mmc), (int)mask);
161 sdhci_dumpregs(host);
168 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
169 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
172 static void sdhci_init(struct sdhci_host *host)
174 sdhci_reset(host, SDHCI_RESET_ALL);
176 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
177 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
178 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
179 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
180 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
183 static void sdhci_reinit(struct sdhci_host *host)
186 sdhci_enable_card_detection(host);
189 static void sdhci_activate_led(struct sdhci_host *host)
193 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
194 ctrl |= SDHCI_CTRL_LED;
195 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
198 static void sdhci_deactivate_led(struct sdhci_host *host)
202 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
203 ctrl &= ~SDHCI_CTRL_LED;
204 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
207 #ifdef SDHCI_USE_LEDS_CLASS
208 static void sdhci_led_control(struct led_classdev *led,
209 enum led_brightness brightness)
211 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
214 spin_lock_irqsave(&host->lock, flags);
216 if (brightness == LED_OFF)
217 sdhci_deactivate_led(host);
219 sdhci_activate_led(host);
221 spin_unlock_irqrestore(&host->lock, flags);
225 /*****************************************************************************\
229 \*****************************************************************************/
231 static void sdhci_read_block_pio(struct sdhci_host *host)
234 size_t blksize, len, chunk;
235 u32 uninitialized_var(scratch);
238 DBG("PIO reading\n");
240 blksize = host->data->blksz;
243 local_irq_save(flags);
246 if (!sg_miter_next(&host->sg_miter))
249 len = min(host->sg_miter.length, blksize);
252 host->sg_miter.consumed = len;
254 buf = host->sg_miter.addr;
258 scratch = sdhci_readl(host, SDHCI_BUFFER);
262 *buf = scratch & 0xFF;
271 sg_miter_stop(&host->sg_miter);
273 local_irq_restore(flags);
276 static void sdhci_write_block_pio(struct sdhci_host *host)
279 size_t blksize, len, chunk;
283 DBG("PIO writing\n");
285 blksize = host->data->blksz;
289 local_irq_save(flags);
292 if (!sg_miter_next(&host->sg_miter))
295 len = min(host->sg_miter.length, blksize);
298 host->sg_miter.consumed = len;
300 buf = host->sg_miter.addr;
303 scratch |= (u32)*buf << (chunk * 8);
309 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
310 sdhci_writel(host, scratch, SDHCI_BUFFER);
317 sg_miter_stop(&host->sg_miter);
319 local_irq_restore(flags);
322 static void sdhci_transfer_pio(struct sdhci_host *host)
328 if (host->blocks == 0)
331 if (host->data->flags & MMC_DATA_READ)
332 mask = SDHCI_DATA_AVAILABLE;
334 mask = SDHCI_SPACE_AVAILABLE;
337 * Some controllers (JMicron JMB38x) mess up the buffer bits
338 * for transfers < 4 bytes. As long as it is just one block,
339 * we can ignore the bits.
341 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
342 (host->data->blocks == 1))
345 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
346 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
349 if (host->data->flags & MMC_DATA_READ)
350 sdhci_read_block_pio(host);
352 sdhci_write_block_pio(host);
355 if (host->blocks == 0)
359 DBG("PIO transfer complete.\n");
362 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
364 local_irq_save(*flags);
365 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
368 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
370 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
371 local_irq_restore(*flags);
374 static int sdhci_adma_table_pre(struct sdhci_host *host,
375 struct mmc_data *data)
382 dma_addr_t align_addr;
385 struct scatterlist *sg;
391 * The spec does not specify endianness of descriptor table.
392 * We currently guess that it is LE.
395 if (data->flags & MMC_DATA_READ)
396 direction = DMA_FROM_DEVICE;
398 direction = DMA_TO_DEVICE;
401 * The ADMA descriptor table is mapped further down as we
402 * need to fill it with data first.
405 host->align_addr = dma_map_single(mmc_dev(host->mmc),
406 host->align_buffer, 128 * 4, direction);
407 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
409 BUG_ON(host->align_addr & 0x3);
411 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
412 data->sg, data->sg_len, direction);
413 if (host->sg_count == 0)
416 desc = host->adma_desc;
417 align = host->align_buffer;
419 align_addr = host->align_addr;
421 for_each_sg(data->sg, sg, host->sg_count, i) {
422 addr = sg_dma_address(sg);
423 len = sg_dma_len(sg);
426 * The SDHCI specification states that ADMA
427 * addresses must be 32-bit aligned. If they
428 * aren't, then we use a bounce buffer for
429 * the (up to three) bytes that screw up the
432 offset = (4 - (addr & 0x3)) & 0x3;
434 if (data->flags & MMC_DATA_WRITE) {
435 buffer = sdhci_kmap_atomic(sg, &flags);
436 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
437 memcpy(align, buffer, offset);
438 sdhci_kunmap_atomic(buffer, &flags);
441 desc[7] = (align_addr >> 24) & 0xff;
442 desc[6] = (align_addr >> 16) & 0xff;
443 desc[5] = (align_addr >> 8) & 0xff;
444 desc[4] = (align_addr >> 0) & 0xff;
446 BUG_ON(offset > 65536);
448 desc[3] = (offset >> 8) & 0xff;
449 desc[2] = (offset >> 0) & 0xff;
452 desc[0] = 0x21; /* tran, valid */
463 desc[7] = (addr >> 24) & 0xff;
464 desc[6] = (addr >> 16) & 0xff;
465 desc[5] = (addr >> 8) & 0xff;
466 desc[4] = (addr >> 0) & 0xff;
470 desc[3] = (len >> 8) & 0xff;
471 desc[2] = (len >> 0) & 0xff;
474 desc[0] = 0x21; /* tran, valid */
479 * If this triggers then we have a calculation bug
482 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
486 * Add a terminating entry.
497 desc[0] = 0x03; /* nop, end, valid */
500 * Resync align buffer as we might have changed it.
502 if (data->flags & MMC_DATA_WRITE) {
503 dma_sync_single_for_device(mmc_dev(host->mmc),
504 host->align_addr, 128 * 4, direction);
507 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
508 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
509 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
511 BUG_ON(host->adma_addr & 0x3);
516 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
517 data->sg_len, direction);
519 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
525 static void sdhci_adma_table_post(struct sdhci_host *host,
526 struct mmc_data *data)
530 struct scatterlist *sg;
536 if (data->flags & MMC_DATA_READ)
537 direction = DMA_FROM_DEVICE;
539 direction = DMA_TO_DEVICE;
541 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
542 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
544 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
547 if (data->flags & MMC_DATA_READ) {
548 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
549 data->sg_len, direction);
551 align = host->align_buffer;
553 for_each_sg(data->sg, sg, host->sg_count, i) {
554 if (sg_dma_address(sg) & 0x3) {
555 size = 4 - (sg_dma_address(sg) & 0x3);
557 buffer = sdhci_kmap_atomic(sg, &flags);
558 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
559 memcpy(buffer, align, size);
560 sdhci_kunmap_atomic(buffer, &flags);
567 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
568 data->sg_len, direction);
571 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
574 unsigned target_timeout, current_timeout;
577 * If the host controller provides us with an incorrect timeout
578 * value, just skip the check and use 0xE. The hardware may take
579 * longer to time out, but that's much better than having a too-short
582 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
586 target_timeout = data->timeout_ns / 1000 +
587 data->timeout_clks / host->clock;
590 * Figure out needed cycles.
591 * We do this in steps in order to fit inside a 32 bit int.
592 * The first step is the minimum timeout, which will have a
593 * minimum resolution of 6 bits:
594 * (1) 2^13*1000 > 2^22,
595 * (2) host->timeout_clk < 2^16
600 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
601 while (current_timeout < target_timeout) {
603 current_timeout <<= 1;
609 printk(KERN_WARNING "%s: Too large timeout requested!\n",
610 mmc_hostname(host->mmc));
617 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
619 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
620 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
622 if (host->flags & SDHCI_REQ_USE_DMA)
623 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
625 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
628 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
640 BUG_ON(data->blksz * data->blocks > 524288);
641 BUG_ON(data->blksz > host->mmc->max_blk_size);
642 BUG_ON(data->blocks > 65535);
645 host->data_early = 0;
647 count = sdhci_calc_timeout(host, data);
648 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
650 if (host->flags & SDHCI_USE_DMA)
651 host->flags |= SDHCI_REQ_USE_DMA;
654 * FIXME: This doesn't account for merging when mapping the
657 if (host->flags & SDHCI_REQ_USE_DMA) {
659 struct scatterlist *sg;
662 if (host->flags & SDHCI_USE_ADMA) {
663 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
666 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
670 if (unlikely(broken)) {
671 for_each_sg(data->sg, sg, data->sg_len, i) {
672 if (sg->length & 0x3) {
673 DBG("Reverting to PIO because of "
674 "transfer size (%d)\n",
676 host->flags &= ~SDHCI_REQ_USE_DMA;
684 * The assumption here being that alignment is the same after
685 * translation to device address space.
687 if (host->flags & SDHCI_REQ_USE_DMA) {
689 struct scatterlist *sg;
692 if (host->flags & SDHCI_USE_ADMA) {
694 * As we use 3 byte chunks to work around
695 * alignment problems, we need to check this
698 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
701 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
705 if (unlikely(broken)) {
706 for_each_sg(data->sg, sg, data->sg_len, i) {
707 if (sg->offset & 0x3) {
708 DBG("Reverting to PIO because of "
710 host->flags &= ~SDHCI_REQ_USE_DMA;
717 if (host->flags & SDHCI_REQ_USE_DMA) {
718 if (host->flags & SDHCI_USE_ADMA) {
719 ret = sdhci_adma_table_pre(host, data);
722 * This only happens when someone fed
723 * us an invalid request.
726 host->flags &= ~SDHCI_REQ_USE_DMA;
728 sdhci_writel(host, host->adma_addr,
734 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
735 data->sg, data->sg_len,
736 (data->flags & MMC_DATA_READ) ?
741 * This only happens when someone fed
742 * us an invalid request.
745 host->flags &= ~SDHCI_REQ_USE_DMA;
747 WARN_ON(sg_cnt != 1);
748 sdhci_writel(host, sg_dma_address(data->sg),
755 * Always adjust the DMA selection as some controllers
756 * (e.g. JMicron) can't do PIO properly when the selection
759 if (host->version >= SDHCI_SPEC_200) {
760 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
761 ctrl &= ~SDHCI_CTRL_DMA_MASK;
762 if ((host->flags & SDHCI_REQ_USE_DMA) &&
763 (host->flags & SDHCI_USE_ADMA))
764 ctrl |= SDHCI_CTRL_ADMA32;
766 ctrl |= SDHCI_CTRL_SDMA;
767 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
770 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
771 sg_miter_start(&host->sg_miter,
772 data->sg, data->sg_len, SG_MITER_ATOMIC);
773 host->blocks = data->blocks;
776 sdhci_set_transfer_irqs(host);
778 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
779 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
780 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
783 static void sdhci_set_transfer_mode(struct sdhci_host *host,
784 struct mmc_data *data)
791 WARN_ON(!host->data);
793 mode = SDHCI_TRNS_BLK_CNT_EN;
794 if (data->blocks > 1)
795 mode |= SDHCI_TRNS_MULTI;
796 if (data->flags & MMC_DATA_READ)
797 mode |= SDHCI_TRNS_READ;
798 if (host->flags & SDHCI_REQ_USE_DMA)
799 mode |= SDHCI_TRNS_DMA;
801 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
804 static void sdhci_finish_data(struct sdhci_host *host)
806 struct mmc_data *data;
813 if (host->flags & SDHCI_REQ_USE_DMA) {
814 if (host->flags & SDHCI_USE_ADMA)
815 sdhci_adma_table_post(host, data);
817 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
818 data->sg_len, (data->flags & MMC_DATA_READ) ?
819 DMA_FROM_DEVICE : DMA_TO_DEVICE);
824 * The specification states that the block count register must
825 * be updated, but it does not specify at what point in the
826 * data flow. That makes the register entirely useless to read
827 * back so we have to assume that nothing made it to the card
828 * in the event of an error.
831 data->bytes_xfered = 0;
833 data->bytes_xfered = data->blksz * data->blocks;
837 * The controller needs a reset of internal state machines
838 * upon error conditions.
841 sdhci_reset(host, SDHCI_RESET_CMD);
842 sdhci_reset(host, SDHCI_RESET_DATA);
845 sdhci_send_command(host, data->stop);
847 tasklet_schedule(&host->finish_tasklet);
850 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
854 unsigned long timeout;
861 mask = SDHCI_CMD_INHIBIT;
862 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
863 mask |= SDHCI_DATA_INHIBIT;
865 /* We shouldn't wait for data inihibit for stop commands, even
866 though they might use busy signaling */
867 if (host->mrq->data && (cmd == host->mrq->data->stop))
868 mask &= ~SDHCI_DATA_INHIBIT;
870 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
872 printk(KERN_ERR "%s: Controller never released "
873 "inhibit bit(s).\n", mmc_hostname(host->mmc));
874 sdhci_dumpregs(host);
876 tasklet_schedule(&host->finish_tasklet);
883 mod_timer(&host->timer, jiffies + 10 * HZ);
887 sdhci_prepare_data(host, cmd->data);
889 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
891 sdhci_set_transfer_mode(host, cmd->data);
893 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
894 printk(KERN_ERR "%s: Unsupported response type!\n",
895 mmc_hostname(host->mmc));
896 cmd->error = -EINVAL;
897 tasklet_schedule(&host->finish_tasklet);
901 if (!(cmd->flags & MMC_RSP_PRESENT))
902 flags = SDHCI_CMD_RESP_NONE;
903 else if (cmd->flags & MMC_RSP_136)
904 flags = SDHCI_CMD_RESP_LONG;
905 else if (cmd->flags & MMC_RSP_BUSY)
906 flags = SDHCI_CMD_RESP_SHORT_BUSY;
908 flags = SDHCI_CMD_RESP_SHORT;
910 if (cmd->flags & MMC_RSP_CRC)
911 flags |= SDHCI_CMD_CRC;
912 if (cmd->flags & MMC_RSP_OPCODE)
913 flags |= SDHCI_CMD_INDEX;
915 flags |= SDHCI_CMD_DATA;
917 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
920 static void sdhci_finish_command(struct sdhci_host *host)
924 BUG_ON(host->cmd == NULL);
926 if (host->cmd->flags & MMC_RSP_PRESENT) {
927 if (host->cmd->flags & MMC_RSP_136) {
928 /* CRC is stripped so we need to do some shifting. */
929 for (i = 0;i < 4;i++) {
930 host->cmd->resp[i] = sdhci_readl(host,
931 SDHCI_RESPONSE + (3-i)*4) << 8;
933 host->cmd->resp[i] |=
935 SDHCI_RESPONSE + (3-i)*4-1);
938 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
942 host->cmd->error = 0;
944 if (host->data && host->data_early)
945 sdhci_finish_data(host);
947 if (!host->cmd->data)
948 tasklet_schedule(&host->finish_tasklet);
953 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
957 unsigned long timeout;
959 if (clock == host->clock)
962 if (host->ops->set_clock) {
963 host->ops->set_clock(host, clock);
964 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
968 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
973 for (div = 1;div < 256;div *= 2) {
974 if ((host->max_clk / div) <= clock)
979 clk = div << SDHCI_DIVIDER_SHIFT;
980 clk |= SDHCI_CLOCK_INT_EN;
981 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
985 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
986 & SDHCI_CLOCK_INT_STABLE)) {
988 printk(KERN_ERR "%s: Internal clock never "
989 "stabilised.\n", mmc_hostname(host->mmc));
990 sdhci_dumpregs(host);
997 clk |= SDHCI_CLOCK_CARD_EN;
998 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1001 host->clock = clock;
1004 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1008 if (power == (unsigned short)-1)
1011 switch (1 << power) {
1012 case MMC_VDD_165_195:
1013 pwr = SDHCI_POWER_180;
1017 pwr = SDHCI_POWER_300;
1021 pwr = SDHCI_POWER_330;
1028 if (host->pwr == pwr)
1034 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1039 * Spec says that we should clear the power reg before setting
1040 * a new value. Some controllers don't seem to like this though.
1042 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1043 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1046 * At least the Marvell CaFe chip gets confused if we set the voltage
1047 * and set turn on power at the same time, so set the voltage first.
1049 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
1050 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1052 pwr |= SDHCI_POWER_ON;
1054 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1057 /*****************************************************************************\
1061 \*****************************************************************************/
1063 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1065 struct sdhci_host *host;
1067 unsigned long flags;
1069 host = mmc_priv(mmc);
1071 spin_lock_irqsave(&host->lock, flags);
1073 WARN_ON(host->mrq != NULL);
1075 #ifndef SDHCI_USE_LEDS_CLASS
1076 sdhci_activate_led(host);
1081 /* If polling, assume that the card is always present. */
1082 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1085 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1088 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1089 host->mrq->cmd->error = -ENOMEDIUM;
1090 tasklet_schedule(&host->finish_tasklet);
1092 sdhci_send_command(host, mrq->cmd);
1095 spin_unlock_irqrestore(&host->lock, flags);
1098 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1100 struct sdhci_host *host;
1101 unsigned long flags;
1104 host = mmc_priv(mmc);
1106 spin_lock_irqsave(&host->lock, flags);
1108 if (host->flags & SDHCI_DEVICE_DEAD)
1112 * Reset the chip on each power off.
1113 * Should clear out any weird states.
1115 if (ios->power_mode == MMC_POWER_OFF) {
1116 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1120 sdhci_set_clock(host, ios->clock);
1122 if (ios->power_mode == MMC_POWER_OFF)
1123 sdhci_set_power(host, -1);
1125 sdhci_set_power(host, ios->vdd);
1127 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1129 if (ios->bus_width == MMC_BUS_WIDTH_4)
1130 ctrl |= SDHCI_CTRL_4BITBUS;
1132 ctrl &= ~SDHCI_CTRL_4BITBUS;
1134 if (ios->timing == MMC_TIMING_SD_HS)
1135 ctrl |= SDHCI_CTRL_HISPD;
1137 ctrl &= ~SDHCI_CTRL_HISPD;
1139 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1142 * Some (ENE) controllers go apeshit on some ios operation,
1143 * signalling timeout and CRC errors even on CMD0. Resetting
1144 * it on each ios seems to solve the problem.
1146 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1147 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1151 spin_unlock_irqrestore(&host->lock, flags);
1154 static int sdhci_get_ro(struct mmc_host *mmc)
1156 struct sdhci_host *host;
1157 unsigned long flags;
1160 host = mmc_priv(mmc);
1162 spin_lock_irqsave(&host->lock, flags);
1164 if (host->flags & SDHCI_DEVICE_DEAD)
1167 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
1169 spin_unlock_irqrestore(&host->lock, flags);
1171 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1172 return !!(present & SDHCI_WRITE_PROTECT);
1173 return !(present & SDHCI_WRITE_PROTECT);
1176 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1178 struct sdhci_host *host;
1179 unsigned long flags;
1181 host = mmc_priv(mmc);
1183 spin_lock_irqsave(&host->lock, flags);
1185 if (host->flags & SDHCI_DEVICE_DEAD)
1189 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1191 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
1195 spin_unlock_irqrestore(&host->lock, flags);
1198 static const struct mmc_host_ops sdhci_ops = {
1199 .request = sdhci_request,
1200 .set_ios = sdhci_set_ios,
1201 .get_ro = sdhci_get_ro,
1202 .enable_sdio_irq = sdhci_enable_sdio_irq,
1205 /*****************************************************************************\
1209 \*****************************************************************************/
1211 static void sdhci_tasklet_card(unsigned long param)
1213 struct sdhci_host *host;
1214 unsigned long flags;
1216 host = (struct sdhci_host*)param;
1218 spin_lock_irqsave(&host->lock, flags);
1220 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
1222 printk(KERN_ERR "%s: Card removed during transfer!\n",
1223 mmc_hostname(host->mmc));
1224 printk(KERN_ERR "%s: Resetting controller.\n",
1225 mmc_hostname(host->mmc));
1227 sdhci_reset(host, SDHCI_RESET_CMD);
1228 sdhci_reset(host, SDHCI_RESET_DATA);
1230 host->mrq->cmd->error = -ENOMEDIUM;
1231 tasklet_schedule(&host->finish_tasklet);
1235 spin_unlock_irqrestore(&host->lock, flags);
1237 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
1240 static void sdhci_tasklet_finish(unsigned long param)
1242 struct sdhci_host *host;
1243 unsigned long flags;
1244 struct mmc_request *mrq;
1246 host = (struct sdhci_host*)param;
1248 spin_lock_irqsave(&host->lock, flags);
1250 del_timer(&host->timer);
1255 * The controller needs a reset of internal state machines
1256 * upon error conditions.
1258 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1260 (mrq->data && (mrq->data->error ||
1261 (mrq->data->stop && mrq->data->stop->error))) ||
1262 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1264 /* Some controllers need this kick or reset won't work here */
1265 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
1268 /* This is to force an update */
1269 clock = host->clock;
1271 sdhci_set_clock(host, clock);
1274 /* Spec says we should do both at the same time, but Ricoh
1275 controllers do not like that. */
1276 sdhci_reset(host, SDHCI_RESET_CMD);
1277 sdhci_reset(host, SDHCI_RESET_DATA);
1284 #ifndef SDHCI_USE_LEDS_CLASS
1285 sdhci_deactivate_led(host);
1289 spin_unlock_irqrestore(&host->lock, flags);
1291 mmc_request_done(host->mmc, mrq);
1294 static void sdhci_timeout_timer(unsigned long data)
1296 struct sdhci_host *host;
1297 unsigned long flags;
1299 host = (struct sdhci_host*)data;
1301 spin_lock_irqsave(&host->lock, flags);
1304 printk(KERN_ERR "%s: Timeout waiting for hardware "
1305 "interrupt.\n", mmc_hostname(host->mmc));
1306 sdhci_dumpregs(host);
1309 host->data->error = -ETIMEDOUT;
1310 sdhci_finish_data(host);
1313 host->cmd->error = -ETIMEDOUT;
1315 host->mrq->cmd->error = -ETIMEDOUT;
1317 tasklet_schedule(&host->finish_tasklet);
1322 spin_unlock_irqrestore(&host->lock, flags);
1325 /*****************************************************************************\
1327 * Interrupt handling *
1329 \*****************************************************************************/
1331 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1333 BUG_ON(intmask == 0);
1336 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1337 "though no command operation was in progress.\n",
1338 mmc_hostname(host->mmc), (unsigned)intmask);
1339 sdhci_dumpregs(host);
1343 if (intmask & SDHCI_INT_TIMEOUT)
1344 host->cmd->error = -ETIMEDOUT;
1345 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1347 host->cmd->error = -EILSEQ;
1349 if (host->cmd->error) {
1350 tasklet_schedule(&host->finish_tasklet);
1355 * The host can send and interrupt when the busy state has
1356 * ended, allowing us to wait without wasting CPU cycles.
1357 * Unfortunately this is overloaded on the "data complete"
1358 * interrupt, so we need to take some care when handling
1361 * Note: The 1.0 specification is a bit ambiguous about this
1362 * feature so there might be some problems with older
1365 if (host->cmd->flags & MMC_RSP_BUSY) {
1366 if (host->cmd->data)
1367 DBG("Cannot wait for busy signal when also "
1368 "doing a data transfer");
1369 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
1372 /* The controller does not support the end-of-busy IRQ,
1373 * fall through and take the SDHCI_INT_RESPONSE */
1376 if (intmask & SDHCI_INT_RESPONSE)
1377 sdhci_finish_command(host);
1380 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1382 BUG_ON(intmask == 0);
1386 * The "data complete" interrupt is also used to
1387 * indicate that a busy state has ended. See comment
1388 * above in sdhci_cmd_irq().
1390 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1391 if (intmask & SDHCI_INT_DATA_END) {
1392 sdhci_finish_command(host);
1397 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1398 "though no data operation was in progress.\n",
1399 mmc_hostname(host->mmc), (unsigned)intmask);
1400 sdhci_dumpregs(host);
1405 if (intmask & SDHCI_INT_DATA_TIMEOUT)
1406 host->data->error = -ETIMEDOUT;
1407 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1408 host->data->error = -EILSEQ;
1409 else if (intmask & SDHCI_INT_ADMA_ERROR)
1410 host->data->error = -EIO;
1412 if (host->data->error)
1413 sdhci_finish_data(host);
1415 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
1416 sdhci_transfer_pio(host);
1419 * We currently don't do anything fancy with DMA
1420 * boundaries, but as we can't disable the feature
1421 * we need to at least restart the transfer.
1423 if (intmask & SDHCI_INT_DMA_END)
1424 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1427 if (intmask & SDHCI_INT_DATA_END) {
1430 * Data managed to finish before the
1431 * command completed. Make sure we do
1432 * things in the proper order.
1434 host->data_early = 1;
1436 sdhci_finish_data(host);
1442 static irqreturn_t sdhci_irq(int irq, void *dev_id)
1445 struct sdhci_host* host = dev_id;
1449 spin_lock(&host->lock);
1451 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
1453 if (!intmask || intmask == 0xffffffff) {
1458 DBG("*** %s got interrupt: 0x%08x\n",
1459 mmc_hostname(host->mmc), intmask);
1461 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
1462 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1463 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
1464 tasklet_schedule(&host->card_tasklet);
1467 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
1469 if (intmask & SDHCI_INT_CMD_MASK) {
1470 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1472 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
1475 if (intmask & SDHCI_INT_DATA_MASK) {
1476 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1478 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
1481 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1483 intmask &= ~SDHCI_INT_ERROR;
1485 if (intmask & SDHCI_INT_BUS_POWER) {
1486 printk(KERN_ERR "%s: Card is consuming too much power!\n",
1487 mmc_hostname(host->mmc));
1488 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
1491 intmask &= ~SDHCI_INT_BUS_POWER;
1493 if (intmask & SDHCI_INT_CARD_INT)
1496 intmask &= ~SDHCI_INT_CARD_INT;
1499 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
1500 mmc_hostname(host->mmc), intmask);
1501 sdhci_dumpregs(host);
1503 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
1506 result = IRQ_HANDLED;
1510 spin_unlock(&host->lock);
1513 * We have to delay this as it calls back into the driver.
1516 mmc_signal_sdio_irq(host->mmc);
1521 /*****************************************************************************\
1525 \*****************************************************************************/
1529 int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
1533 sdhci_disable_card_detection(host);
1535 ret = mmc_suspend_host(host->mmc, state);
1539 free_irq(host->irq, host);
1544 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
1546 int sdhci_resume_host(struct sdhci_host *host)
1550 if (host->flags & SDHCI_USE_DMA) {
1551 if (host->ops->enable_dma)
1552 host->ops->enable_dma(host);
1555 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1556 mmc_hostname(host->mmc), host);
1563 ret = mmc_resume_host(host->mmc);
1567 sdhci_enable_card_detection(host);
1572 EXPORT_SYMBOL_GPL(sdhci_resume_host);
1574 #endif /* CONFIG_PM */
1576 /*****************************************************************************\
1578 * Device allocation/registration *
1580 \*****************************************************************************/
1582 struct sdhci_host *sdhci_alloc_host(struct device *dev,
1585 struct mmc_host *mmc;
1586 struct sdhci_host *host;
1588 WARN_ON(dev == NULL);
1590 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
1592 return ERR_PTR(-ENOMEM);
1594 host = mmc_priv(mmc);
1600 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
1602 int sdhci_add_host(struct sdhci_host *host)
1604 struct mmc_host *mmc;
1608 WARN_ON(host == NULL);
1615 host->quirks = debug_quirks;
1617 sdhci_reset(host, SDHCI_RESET_ALL);
1619 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
1620 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1621 >> SDHCI_SPEC_VER_SHIFT;
1622 if (host->version > SDHCI_SPEC_200) {
1623 printk(KERN_ERR "%s: Unknown controller version (%d). "
1624 "You may experience problems.\n", mmc_hostname(mmc),
1628 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
1630 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
1631 host->flags |= SDHCI_USE_DMA;
1632 else if (!(caps & SDHCI_CAN_DO_DMA))
1633 DBG("Controller doesn't have DMA capability\n");
1635 host->flags |= SDHCI_USE_DMA;
1637 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
1638 (host->flags & SDHCI_USE_DMA)) {
1639 DBG("Disabling DMA as it is marked broken\n");
1640 host->flags &= ~SDHCI_USE_DMA;
1643 if (host->flags & SDHCI_USE_DMA) {
1644 if ((host->version >= SDHCI_SPEC_200) &&
1645 (caps & SDHCI_CAN_DO_ADMA2))
1646 host->flags |= SDHCI_USE_ADMA;
1649 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1650 (host->flags & SDHCI_USE_ADMA)) {
1651 DBG("Disabling ADMA as it is marked broken\n");
1652 host->flags &= ~SDHCI_USE_ADMA;
1655 if (host->flags & SDHCI_USE_DMA) {
1656 if (host->ops->enable_dma) {
1657 if (host->ops->enable_dma(host)) {
1658 printk(KERN_WARNING "%s: No suitable DMA "
1659 "available. Falling back to PIO.\n",
1661 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
1666 if (host->flags & SDHCI_USE_ADMA) {
1668 * We need to allocate descriptors for all sg entries
1669 * (128) and potentially one alignment transfer for
1670 * each of those entries.
1672 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1673 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1674 if (!host->adma_desc || !host->align_buffer) {
1675 kfree(host->adma_desc);
1676 kfree(host->align_buffer);
1677 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1678 "buffers. Falling back to standard DMA.\n",
1680 host->flags &= ~SDHCI_USE_ADMA;
1685 * If we use DMA, then it's up to the caller to set the DMA
1686 * mask, but PIO does not need the hw shim so we set a new
1687 * mask here in that case.
1689 if (!(host->flags & SDHCI_USE_DMA)) {
1690 host->dma_mask = DMA_BIT_MASK(64);
1691 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1695 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1696 host->max_clk *= 1000000;
1697 if (host->max_clk == 0) {
1698 if (!host->ops->get_max_clock) {
1700 "%s: Hardware doesn't specify base clock "
1701 "frequency.\n", mmc_hostname(mmc));
1704 host->max_clk = host->ops->get_max_clock(host);
1708 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1709 if (host->timeout_clk == 0) {
1710 if (!host->ops->get_timeout_clock) {
1712 "%s: Hardware doesn't specify timeout clock "
1713 "frequency.\n", mmc_hostname(mmc));
1716 host->timeout_clk = host->ops->get_timeout_clock(host);
1718 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1719 host->timeout_clk *= 1000;
1722 * Set host parameters.
1724 mmc->ops = &sdhci_ops;
1725 mmc->f_min = host->max_clk / 256;
1726 mmc->f_max = host->max_clk;
1727 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
1729 if (caps & SDHCI_CAN_DO_HISPD)
1730 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1732 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1733 mmc->caps |= MMC_CAP_NEEDS_POLL;
1736 if (caps & SDHCI_CAN_VDD_330)
1737 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
1738 if (caps & SDHCI_CAN_VDD_300)
1739 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
1740 if (caps & SDHCI_CAN_VDD_180)
1741 mmc->ocr_avail |= MMC_VDD_165_195;
1743 if (mmc->ocr_avail == 0) {
1744 printk(KERN_ERR "%s: Hardware doesn't report any "
1745 "support voltages.\n", mmc_hostname(mmc));
1749 spin_lock_init(&host->lock);
1752 * Maximum number of segments. Depends on if the hardware
1753 * can do scatter/gather or not.
1755 if (host->flags & SDHCI_USE_ADMA)
1756 mmc->max_hw_segs = 128;
1757 else if (host->flags & SDHCI_USE_DMA)
1758 mmc->max_hw_segs = 1;
1760 mmc->max_hw_segs = 128;
1761 mmc->max_phys_segs = 128;
1764 * Maximum number of sectors in one transfer. Limited by DMA boundary
1767 mmc->max_req_size = 524288;
1770 * Maximum segment size. Could be one segment with the maximum number
1771 * of bytes. When doing hardware scatter/gather, each entry cannot
1772 * be larger than 64 KiB though.
1774 if (host->flags & SDHCI_USE_ADMA)
1775 mmc->max_seg_size = 65536;
1777 mmc->max_seg_size = mmc->max_req_size;
1780 * Maximum block size. This varies from controller to controller and
1781 * is specified in the capabilities register.
1783 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1784 mmc->max_blk_size = 2;
1786 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1787 SDHCI_MAX_BLOCK_SHIFT;
1788 if (mmc->max_blk_size >= 3) {
1789 printk(KERN_WARNING "%s: Invalid maximum block size, "
1790 "assuming 512 bytes\n", mmc_hostname(mmc));
1791 mmc->max_blk_size = 0;
1795 mmc->max_blk_size = 512 << mmc->max_blk_size;
1798 * Maximum block count.
1800 mmc->max_blk_count = 65535;
1805 tasklet_init(&host->card_tasklet,
1806 sdhci_tasklet_card, (unsigned long)host);
1807 tasklet_init(&host->finish_tasklet,
1808 sdhci_tasklet_finish, (unsigned long)host);
1810 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
1812 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1813 mmc_hostname(mmc), host);
1819 #ifdef CONFIG_MMC_DEBUG
1820 sdhci_dumpregs(host);
1823 #ifdef SDHCI_USE_LEDS_CLASS
1824 snprintf(host->led_name, sizeof(host->led_name),
1825 "%s::", mmc_hostname(mmc));
1826 host->led.name = host->led_name;
1827 host->led.brightness = LED_OFF;
1828 host->led.default_trigger = mmc_hostname(mmc);
1829 host->led.brightness_set = sdhci_led_control;
1831 ret = led_classdev_register(mmc_dev(mmc), &host->led);
1840 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
1841 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
1842 (host->flags & SDHCI_USE_ADMA)?"A":"",
1843 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1845 sdhci_enable_card_detection(host);
1849 #ifdef SDHCI_USE_LEDS_CLASS
1851 sdhci_reset(host, SDHCI_RESET_ALL);
1852 free_irq(host->irq, host);
1855 tasklet_kill(&host->card_tasklet);
1856 tasklet_kill(&host->finish_tasklet);
1861 EXPORT_SYMBOL_GPL(sdhci_add_host);
1863 void sdhci_remove_host(struct sdhci_host *host, int dead)
1865 unsigned long flags;
1868 spin_lock_irqsave(&host->lock, flags);
1870 host->flags |= SDHCI_DEVICE_DEAD;
1873 printk(KERN_ERR "%s: Controller removed during "
1874 " transfer!\n", mmc_hostname(host->mmc));
1876 host->mrq->cmd->error = -ENOMEDIUM;
1877 tasklet_schedule(&host->finish_tasklet);
1880 spin_unlock_irqrestore(&host->lock, flags);
1883 sdhci_disable_card_detection(host);
1885 mmc_remove_host(host->mmc);
1887 #ifdef SDHCI_USE_LEDS_CLASS
1888 led_classdev_unregister(&host->led);
1892 sdhci_reset(host, SDHCI_RESET_ALL);
1894 free_irq(host->irq, host);
1896 del_timer_sync(&host->timer);
1898 tasklet_kill(&host->card_tasklet);
1899 tasklet_kill(&host->finish_tasklet);
1901 kfree(host->adma_desc);
1902 kfree(host->align_buffer);
1904 host->adma_desc = NULL;
1905 host->align_buffer = NULL;
1908 EXPORT_SYMBOL_GPL(sdhci_remove_host);
1910 void sdhci_free_host(struct sdhci_host *host)
1912 mmc_free_host(host->mmc);
1915 EXPORT_SYMBOL_GPL(sdhci_free_host);
1917 /*****************************************************************************\
1919 * Driver init/exit *
1921 \*****************************************************************************/
1923 static int __init sdhci_drv_init(void)
1925 printk(KERN_INFO DRIVER_NAME
1926 ": Secure Digital Host Controller Interface driver\n");
1927 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1932 static void __exit sdhci_drv_exit(void)
1936 module_init(sdhci_drv_init);
1937 module_exit(sdhci_drv_exit);
1939 module_param(debug_quirks, uint, 0444);
1941 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
1942 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
1943 MODULE_LICENSE("GPL");
1945 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");