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/module.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/slab.h>
22 #include <linux/scatterlist.h>
23 #include <linux/regulator/consumer.h>
25 #include <linux/leds.h>
27 #include <linux/mmc/mmc.h>
28 #include <linux/mmc/host.h>
32 #define DRIVER_NAME "sdhci"
34 #define DBG(f, x...) \
35 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
37 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
38 defined(CONFIG_MMC_SDHCI_MODULE))
39 #define SDHCI_USE_LEDS_CLASS
42 #define MAX_TUNING_LOOP 40
44 static unsigned int debug_quirks = 0;
46 static void sdhci_finish_data(struct sdhci_host *);
48 static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49 static void sdhci_finish_command(struct sdhci_host *);
50 static int sdhci_execute_tuning(struct mmc_host *mmc);
51 static void sdhci_tuning_timer(unsigned long data);
53 static void sdhci_dumpregs(struct sdhci_host *host)
55 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
56 mmc_hostname(host->mmc));
58 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
59 sdhci_readl(host, SDHCI_DMA_ADDRESS),
60 sdhci_readw(host, SDHCI_HOST_VERSION));
61 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
62 sdhci_readw(host, SDHCI_BLOCK_SIZE),
63 sdhci_readw(host, SDHCI_BLOCK_COUNT));
64 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
65 sdhci_readl(host, SDHCI_ARGUMENT),
66 sdhci_readw(host, SDHCI_TRANSFER_MODE));
67 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
68 sdhci_readl(host, SDHCI_PRESENT_STATE),
69 sdhci_readb(host, SDHCI_HOST_CONTROL));
70 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
71 sdhci_readb(host, SDHCI_POWER_CONTROL),
72 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
73 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
74 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
75 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
76 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
77 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
78 sdhci_readl(host, SDHCI_INT_STATUS));
79 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
80 sdhci_readl(host, SDHCI_INT_ENABLE),
81 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
82 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
83 sdhci_readw(host, SDHCI_ACMD12_ERR),
84 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
85 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
86 sdhci_readl(host, SDHCI_CAPABILITIES),
87 sdhci_readl(host, SDHCI_CAPABILITIES_1));
88 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
89 sdhci_readw(host, SDHCI_COMMAND),
90 sdhci_readl(host, SDHCI_MAX_CURRENT));
91 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
92 sdhci_readw(host, SDHCI_HOST_CONTROL2));
94 if (host->flags & SDHCI_USE_ADMA)
95 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
96 readl(host->ioaddr + SDHCI_ADMA_ERROR),
97 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
99 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
102 /*****************************************************************************\
104 * Low level functions *
106 \*****************************************************************************/
108 static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
112 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
115 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
116 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
119 static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
121 sdhci_clear_set_irqs(host, 0, irqs);
124 static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
126 sdhci_clear_set_irqs(host, irqs, 0);
129 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
133 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
136 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
138 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
141 sdhci_unmask_irqs(host, irqs);
143 sdhci_mask_irqs(host, irqs);
146 static void sdhci_enable_card_detection(struct sdhci_host *host)
148 sdhci_set_card_detection(host, true);
151 static void sdhci_disable_card_detection(struct sdhci_host *host)
153 sdhci_set_card_detection(host, false);
156 static void sdhci_reset(struct sdhci_host *host, u8 mask)
158 unsigned long timeout;
159 u32 uninitialized_var(ier);
161 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
162 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
167 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
168 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
170 if (host->ops->platform_reset_enter)
171 host->ops->platform_reset_enter(host, mask);
173 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
175 if (mask & SDHCI_RESET_ALL)
178 /* Wait max 100 ms */
181 /* hw clears the bit when it's done */
182 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
184 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
185 mmc_hostname(host->mmc), (int)mask);
186 sdhci_dumpregs(host);
193 if (host->ops->platform_reset_exit)
194 host->ops->platform_reset_exit(host, mask);
196 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
197 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
200 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
202 static void sdhci_init(struct sdhci_host *host, int soft)
205 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
207 sdhci_reset(host, SDHCI_RESET_ALL);
209 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
210 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
211 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
212 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
213 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
216 /* force clock reconfiguration */
218 sdhci_set_ios(host->mmc, &host->mmc->ios);
222 static void sdhci_reinit(struct sdhci_host *host)
225 sdhci_enable_card_detection(host);
228 static void sdhci_activate_led(struct sdhci_host *host)
232 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
233 ctrl |= SDHCI_CTRL_LED;
234 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
237 static void sdhci_deactivate_led(struct sdhci_host *host)
241 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
242 ctrl &= ~SDHCI_CTRL_LED;
243 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
246 #ifdef SDHCI_USE_LEDS_CLASS
247 static void sdhci_led_control(struct led_classdev *led,
248 enum led_brightness brightness)
250 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
253 spin_lock_irqsave(&host->lock, flags);
255 if (brightness == LED_OFF)
256 sdhci_deactivate_led(host);
258 sdhci_activate_led(host);
260 spin_unlock_irqrestore(&host->lock, flags);
264 /*****************************************************************************\
268 \*****************************************************************************/
270 static void sdhci_read_block_pio(struct sdhci_host *host)
273 size_t blksize, len, chunk;
274 u32 uninitialized_var(scratch);
277 DBG("PIO reading\n");
279 blksize = host->data->blksz;
282 local_irq_save(flags);
285 if (!sg_miter_next(&host->sg_miter))
288 len = min(host->sg_miter.length, blksize);
291 host->sg_miter.consumed = len;
293 buf = host->sg_miter.addr;
297 scratch = sdhci_readl(host, SDHCI_BUFFER);
301 *buf = scratch & 0xFF;
310 sg_miter_stop(&host->sg_miter);
312 local_irq_restore(flags);
315 static void sdhci_write_block_pio(struct sdhci_host *host)
318 size_t blksize, len, chunk;
322 DBG("PIO writing\n");
324 blksize = host->data->blksz;
328 local_irq_save(flags);
331 if (!sg_miter_next(&host->sg_miter))
334 len = min(host->sg_miter.length, blksize);
337 host->sg_miter.consumed = len;
339 buf = host->sg_miter.addr;
342 scratch |= (u32)*buf << (chunk * 8);
348 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
349 sdhci_writel(host, scratch, SDHCI_BUFFER);
356 sg_miter_stop(&host->sg_miter);
358 local_irq_restore(flags);
361 static void sdhci_transfer_pio(struct sdhci_host *host)
367 if (host->blocks == 0)
370 if (host->data->flags & MMC_DATA_READ)
371 mask = SDHCI_DATA_AVAILABLE;
373 mask = SDHCI_SPACE_AVAILABLE;
376 * Some controllers (JMicron JMB38x) mess up the buffer bits
377 * for transfers < 4 bytes. As long as it is just one block,
378 * we can ignore the bits.
380 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
381 (host->data->blocks == 1))
384 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
385 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
388 if (host->data->flags & MMC_DATA_READ)
389 sdhci_read_block_pio(host);
391 sdhci_write_block_pio(host);
394 if (host->blocks == 0)
398 DBG("PIO transfer complete.\n");
401 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
403 local_irq_save(*flags);
404 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
407 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
409 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
410 local_irq_restore(*flags);
413 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
415 __le32 *dataddr = (__le32 __force *)(desc + 4);
416 __le16 *cmdlen = (__le16 __force *)desc;
418 /* SDHCI specification says ADMA descriptors should be 4 byte
419 * aligned, so using 16 or 32bit operations should be safe. */
421 cmdlen[0] = cpu_to_le16(cmd);
422 cmdlen[1] = cpu_to_le16(len);
424 dataddr[0] = cpu_to_le32(addr);
427 static int sdhci_adma_table_pre(struct sdhci_host *host,
428 struct mmc_data *data)
435 dma_addr_t align_addr;
438 struct scatterlist *sg;
444 * The spec does not specify endianness of descriptor table.
445 * We currently guess that it is LE.
448 if (data->flags & MMC_DATA_READ)
449 direction = DMA_FROM_DEVICE;
451 direction = DMA_TO_DEVICE;
454 * The ADMA descriptor table is mapped further down as we
455 * need to fill it with data first.
458 host->align_addr = dma_map_single(mmc_dev(host->mmc),
459 host->align_buffer, 128 * 4, direction);
460 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
462 BUG_ON(host->align_addr & 0x3);
464 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
465 data->sg, data->sg_len, direction);
466 if (host->sg_count == 0)
469 desc = host->adma_desc;
470 align = host->align_buffer;
472 align_addr = host->align_addr;
474 for_each_sg(data->sg, sg, host->sg_count, i) {
475 addr = sg_dma_address(sg);
476 len = sg_dma_len(sg);
479 * The SDHCI specification states that ADMA
480 * addresses must be 32-bit aligned. If they
481 * aren't, then we use a bounce buffer for
482 * the (up to three) bytes that screw up the
485 offset = (4 - (addr & 0x3)) & 0x3;
487 if (data->flags & MMC_DATA_WRITE) {
488 buffer = sdhci_kmap_atomic(sg, &flags);
489 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
490 memcpy(align, buffer, offset);
491 sdhci_kunmap_atomic(buffer, &flags);
495 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
497 BUG_ON(offset > 65536);
511 sdhci_set_adma_desc(desc, addr, len, 0x21);
515 * If this triggers then we have a calculation bug
518 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
521 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
523 * Mark the last descriptor as the terminating descriptor
525 if (desc != host->adma_desc) {
527 desc[0] |= 0x2; /* end */
531 * Add a terminating entry.
534 /* nop, end, valid */
535 sdhci_set_adma_desc(desc, 0, 0, 0x3);
539 * Resync align buffer as we might have changed it.
541 if (data->flags & MMC_DATA_WRITE) {
542 dma_sync_single_for_device(mmc_dev(host->mmc),
543 host->align_addr, 128 * 4, direction);
546 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
547 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
548 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
550 BUG_ON(host->adma_addr & 0x3);
555 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
556 data->sg_len, direction);
558 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
564 static void sdhci_adma_table_post(struct sdhci_host *host,
565 struct mmc_data *data)
569 struct scatterlist *sg;
575 if (data->flags & MMC_DATA_READ)
576 direction = DMA_FROM_DEVICE;
578 direction = DMA_TO_DEVICE;
580 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
581 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
583 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
586 if (data->flags & MMC_DATA_READ) {
587 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
588 data->sg_len, direction);
590 align = host->align_buffer;
592 for_each_sg(data->sg, sg, host->sg_count, i) {
593 if (sg_dma_address(sg) & 0x3) {
594 size = 4 - (sg_dma_address(sg) & 0x3);
596 buffer = sdhci_kmap_atomic(sg, &flags);
597 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
598 memcpy(buffer, align, size);
599 sdhci_kunmap_atomic(buffer, &flags);
606 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
607 data->sg_len, direction);
610 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
613 struct mmc_data *data = cmd->data;
614 unsigned target_timeout, current_timeout;
617 * If the host controller provides us with an incorrect timeout
618 * value, just skip the check and use 0xE. The hardware may take
619 * longer to time out, but that's much better than having a too-short
622 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
625 /* Unspecified timeout, assume max */
626 if (!data && !cmd->cmd_timeout_ms)
631 target_timeout = cmd->cmd_timeout_ms * 1000;
633 target_timeout = data->timeout_ns / 1000;
635 target_timeout += data->timeout_clks / host->clock;
639 * Figure out needed cycles.
640 * We do this in steps in order to fit inside a 32 bit int.
641 * The first step is the minimum timeout, which will have a
642 * minimum resolution of 6 bits:
643 * (1) 2^13*1000 > 2^22,
644 * (2) host->timeout_clk < 2^16
649 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
650 while (current_timeout < target_timeout) {
652 current_timeout <<= 1;
658 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
659 mmc_hostname(host->mmc), cmd->opcode);
666 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
668 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
669 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
671 if (host->flags & SDHCI_REQ_USE_DMA)
672 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
674 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
677 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
681 struct mmc_data *data = cmd->data;
686 if (data || (cmd->flags & MMC_RSP_BUSY)) {
687 count = sdhci_calc_timeout(host, cmd);
688 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
695 BUG_ON(data->blksz * data->blocks > 524288);
696 BUG_ON(data->blksz > host->mmc->max_blk_size);
697 BUG_ON(data->blocks > 65535);
700 host->data_early = 0;
701 host->data->bytes_xfered = 0;
703 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
704 host->flags |= SDHCI_REQ_USE_DMA;
707 * FIXME: This doesn't account for merging when mapping the
710 if (host->flags & SDHCI_REQ_USE_DMA) {
712 struct scatterlist *sg;
715 if (host->flags & SDHCI_USE_ADMA) {
716 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
719 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
723 if (unlikely(broken)) {
724 for_each_sg(data->sg, sg, data->sg_len, i) {
725 if (sg->length & 0x3) {
726 DBG("Reverting to PIO because of "
727 "transfer size (%d)\n",
729 host->flags &= ~SDHCI_REQ_USE_DMA;
737 * The assumption here being that alignment is the same after
738 * translation to device address space.
740 if (host->flags & SDHCI_REQ_USE_DMA) {
742 struct scatterlist *sg;
745 if (host->flags & SDHCI_USE_ADMA) {
747 * As we use 3 byte chunks to work around
748 * alignment problems, we need to check this
751 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
754 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
758 if (unlikely(broken)) {
759 for_each_sg(data->sg, sg, data->sg_len, i) {
760 if (sg->offset & 0x3) {
761 DBG("Reverting to PIO because of "
763 host->flags &= ~SDHCI_REQ_USE_DMA;
770 if (host->flags & SDHCI_REQ_USE_DMA) {
771 if (host->flags & SDHCI_USE_ADMA) {
772 ret = sdhci_adma_table_pre(host, data);
775 * This only happens when someone fed
776 * us an invalid request.
779 host->flags &= ~SDHCI_REQ_USE_DMA;
781 sdhci_writel(host, host->adma_addr,
787 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
788 data->sg, data->sg_len,
789 (data->flags & MMC_DATA_READ) ?
794 * This only happens when someone fed
795 * us an invalid request.
798 host->flags &= ~SDHCI_REQ_USE_DMA;
800 WARN_ON(sg_cnt != 1);
801 sdhci_writel(host, sg_dma_address(data->sg),
808 * Always adjust the DMA selection as some controllers
809 * (e.g. JMicron) can't do PIO properly when the selection
812 if (host->version >= SDHCI_SPEC_200) {
813 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
814 ctrl &= ~SDHCI_CTRL_DMA_MASK;
815 if ((host->flags & SDHCI_REQ_USE_DMA) &&
816 (host->flags & SDHCI_USE_ADMA))
817 ctrl |= SDHCI_CTRL_ADMA32;
819 ctrl |= SDHCI_CTRL_SDMA;
820 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
823 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
826 flags = SG_MITER_ATOMIC;
827 if (host->data->flags & MMC_DATA_READ)
828 flags |= SG_MITER_TO_SG;
830 flags |= SG_MITER_FROM_SG;
831 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
832 host->blocks = data->blocks;
835 sdhci_set_transfer_irqs(host);
837 /* Set the DMA boundary value and block size */
838 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
839 data->blksz), SDHCI_BLOCK_SIZE);
840 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
843 static void sdhci_set_transfer_mode(struct sdhci_host *host,
844 struct mmc_command *cmd)
847 struct mmc_data *data = cmd->data;
852 WARN_ON(!host->data);
854 mode = SDHCI_TRNS_BLK_CNT_EN;
855 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
856 mode |= SDHCI_TRNS_MULTI;
858 * If we are sending CMD23, CMD12 never gets sent
859 * on successful completion (so no Auto-CMD12).
861 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
862 mode |= SDHCI_TRNS_AUTO_CMD12;
863 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
864 mode |= SDHCI_TRNS_AUTO_CMD23;
865 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
869 if (data->flags & MMC_DATA_READ)
870 mode |= SDHCI_TRNS_READ;
871 if (host->flags & SDHCI_REQ_USE_DMA)
872 mode |= SDHCI_TRNS_DMA;
874 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
877 static void sdhci_finish_data(struct sdhci_host *host)
879 struct mmc_data *data;
886 if (host->flags & SDHCI_REQ_USE_DMA) {
887 if (host->flags & SDHCI_USE_ADMA)
888 sdhci_adma_table_post(host, data);
890 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
891 data->sg_len, (data->flags & MMC_DATA_READ) ?
892 DMA_FROM_DEVICE : DMA_TO_DEVICE);
897 * The specification states that the block count register must
898 * be updated, but it does not specify at what point in the
899 * data flow. That makes the register entirely useless to read
900 * back so we have to assume that nothing made it to the card
901 * in the event of an error.
904 data->bytes_xfered = 0;
906 data->bytes_xfered = data->blksz * data->blocks;
909 * Need to send CMD12 if -
910 * a) open-ended multiblock transfer (no CMD23)
911 * b) error in multiblock transfer
918 * The controller needs a reset of internal state machines
919 * upon error conditions.
922 sdhci_reset(host, SDHCI_RESET_CMD);
923 sdhci_reset(host, SDHCI_RESET_DATA);
926 sdhci_send_command(host, data->stop);
928 tasklet_schedule(&host->finish_tasklet);
931 static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
935 unsigned long timeout;
942 mask = SDHCI_CMD_INHIBIT;
943 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
944 mask |= SDHCI_DATA_INHIBIT;
946 /* We shouldn't wait for data inihibit for stop commands, even
947 though they might use busy signaling */
948 if (host->mrq->data && (cmd == host->mrq->data->stop))
949 mask &= ~SDHCI_DATA_INHIBIT;
951 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
953 printk(KERN_ERR "%s: Controller never released "
954 "inhibit bit(s).\n", mmc_hostname(host->mmc));
955 sdhci_dumpregs(host);
957 tasklet_schedule(&host->finish_tasklet);
964 mod_timer(&host->timer, jiffies + 10 * HZ);
968 sdhci_prepare_data(host, cmd);
970 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
972 sdhci_set_transfer_mode(host, cmd);
974 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
975 printk(KERN_ERR "%s: Unsupported response type!\n",
976 mmc_hostname(host->mmc));
977 cmd->error = -EINVAL;
978 tasklet_schedule(&host->finish_tasklet);
982 if (!(cmd->flags & MMC_RSP_PRESENT))
983 flags = SDHCI_CMD_RESP_NONE;
984 else if (cmd->flags & MMC_RSP_136)
985 flags = SDHCI_CMD_RESP_LONG;
986 else if (cmd->flags & MMC_RSP_BUSY)
987 flags = SDHCI_CMD_RESP_SHORT_BUSY;
989 flags = SDHCI_CMD_RESP_SHORT;
991 if (cmd->flags & MMC_RSP_CRC)
992 flags |= SDHCI_CMD_CRC;
993 if (cmd->flags & MMC_RSP_OPCODE)
994 flags |= SDHCI_CMD_INDEX;
996 /* CMD19 is special in that the Data Present Select should be set */
997 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
998 flags |= SDHCI_CMD_DATA;
1000 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1003 static void sdhci_finish_command(struct sdhci_host *host)
1007 BUG_ON(host->cmd == NULL);
1009 if (host->cmd->flags & MMC_RSP_PRESENT) {
1010 if (host->cmd->flags & MMC_RSP_136) {
1011 /* CRC is stripped so we need to do some shifting. */
1012 for (i = 0;i < 4;i++) {
1013 host->cmd->resp[i] = sdhci_readl(host,
1014 SDHCI_RESPONSE + (3-i)*4) << 8;
1016 host->cmd->resp[i] |=
1018 SDHCI_RESPONSE + (3-i)*4-1);
1021 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1025 host->cmd->error = 0;
1027 /* Finished CMD23, now send actual command. */
1028 if (host->cmd == host->mrq->sbc) {
1030 sdhci_send_command(host, host->mrq->cmd);
1033 /* Processed actual command. */
1034 if (host->data && host->data_early)
1035 sdhci_finish_data(host);
1037 if (!host->cmd->data)
1038 tasklet_schedule(&host->finish_tasklet);
1044 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1046 int div = 0; /* Initialized for compiler warning */
1048 unsigned long timeout;
1050 if (clock == host->clock)
1053 if (host->ops->set_clock) {
1054 host->ops->set_clock(host, clock);
1055 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1059 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1064 if (host->version >= SDHCI_SPEC_300) {
1066 * Check if the Host Controller supports Programmable Clock
1069 if (host->clk_mul) {
1073 * We need to figure out whether the Host Driver needs
1074 * to select Programmable Clock Mode, or the value can
1075 * be set automatically by the Host Controller based on
1076 * the Preset Value registers.
1078 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1079 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1080 for (div = 1; div <= 1024; div++) {
1081 if (((host->max_clk * host->clk_mul) /
1086 * Set Programmable Clock Mode in the Clock
1089 clk = SDHCI_PROG_CLOCK_MODE;
1093 /* Version 3.00 divisors must be a multiple of 2. */
1094 if (host->max_clk <= clock)
1097 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1099 if ((host->max_clk / div) <= clock)
1106 /* Version 2.00 divisors must be a power of 2. */
1107 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1108 if ((host->max_clk / div) <= clock)
1114 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1115 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1116 << SDHCI_DIVIDER_HI_SHIFT;
1117 clk |= SDHCI_CLOCK_INT_EN;
1118 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1120 /* Wait max 20 ms */
1122 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1123 & SDHCI_CLOCK_INT_STABLE)) {
1125 printk(KERN_ERR "%s: Internal clock never "
1126 "stabilised.\n", mmc_hostname(host->mmc));
1127 sdhci_dumpregs(host);
1134 clk |= SDHCI_CLOCK_CARD_EN;
1135 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1138 host->clock = clock;
1141 static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1145 if (power != (unsigned short)-1) {
1146 switch (1 << power) {
1147 case MMC_VDD_165_195:
1148 pwr = SDHCI_POWER_180;
1152 pwr = SDHCI_POWER_300;
1156 pwr = SDHCI_POWER_330;
1163 if (host->pwr == pwr)
1169 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1174 * Spec says that we should clear the power reg before setting
1175 * a new value. Some controllers don't seem to like this though.
1177 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1178 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1181 * At least the Marvell CaFe chip gets confused if we set the voltage
1182 * and set turn on power at the same time, so set the voltage first.
1184 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1185 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1187 pwr |= SDHCI_POWER_ON;
1189 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1192 * Some controllers need an extra 10ms delay of 10ms before they
1193 * can apply clock after applying power
1195 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1199 /*****************************************************************************\
1203 \*****************************************************************************/
1205 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1207 struct sdhci_host *host;
1209 unsigned long flags;
1211 host = mmc_priv(mmc);
1213 spin_lock_irqsave(&host->lock, flags);
1215 WARN_ON(host->mrq != NULL);
1217 #ifndef SDHCI_USE_LEDS_CLASS
1218 sdhci_activate_led(host);
1222 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1223 * requests if Auto-CMD12 is enabled.
1225 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1227 mrq->data->stop = NULL;
1234 /* If polling, assume that the card is always present. */
1235 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1238 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1241 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1242 host->mrq->cmd->error = -ENOMEDIUM;
1243 tasklet_schedule(&host->finish_tasklet);
1247 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1249 * Check if the re-tuning timer has already expired and there
1250 * is no on-going data transfer. If so, we need to execute
1251 * tuning procedure before sending command.
1253 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1254 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1255 spin_unlock_irqrestore(&host->lock, flags);
1256 sdhci_execute_tuning(mmc);
1257 spin_lock_irqsave(&host->lock, flags);
1259 /* Restore original mmc_request structure */
1263 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1264 sdhci_send_command(host, mrq->sbc);
1266 sdhci_send_command(host, mrq->cmd);
1270 spin_unlock_irqrestore(&host->lock, flags);
1273 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1275 struct sdhci_host *host;
1276 unsigned long flags;
1279 host = mmc_priv(mmc);
1281 spin_lock_irqsave(&host->lock, flags);
1283 if (host->flags & SDHCI_DEVICE_DEAD)
1287 * Reset the chip on each power off.
1288 * Should clear out any weird states.
1290 if (ios->power_mode == MMC_POWER_OFF) {
1291 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1295 sdhci_set_clock(host, ios->clock);
1297 if (ios->power_mode == MMC_POWER_OFF)
1298 sdhci_set_power(host, -1);
1300 sdhci_set_power(host, ios->vdd);
1302 if (host->ops->platform_send_init_74_clocks)
1303 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1306 * If your platform has 8-bit width support but is not a v3 controller,
1307 * or if it requires special setup code, you should implement that in
1308 * platform_8bit_width().
1310 if (host->ops->platform_8bit_width)
1311 host->ops->platform_8bit_width(host, ios->bus_width);
1313 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1314 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1315 ctrl &= ~SDHCI_CTRL_4BITBUS;
1316 if (host->version >= SDHCI_SPEC_300)
1317 ctrl |= SDHCI_CTRL_8BITBUS;
1319 if (host->version >= SDHCI_SPEC_300)
1320 ctrl &= ~SDHCI_CTRL_8BITBUS;
1321 if (ios->bus_width == MMC_BUS_WIDTH_4)
1322 ctrl |= SDHCI_CTRL_4BITBUS;
1324 ctrl &= ~SDHCI_CTRL_4BITBUS;
1326 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1329 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1331 if ((ios->timing == MMC_TIMING_SD_HS ||
1332 ios->timing == MMC_TIMING_MMC_HS)
1333 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1334 ctrl |= SDHCI_CTRL_HISPD;
1336 ctrl &= ~SDHCI_CTRL_HISPD;
1338 if (host->version >= SDHCI_SPEC_300) {
1342 /* In case of UHS-I modes, set High Speed Enable */
1343 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1344 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1345 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1346 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1347 (ios->timing == MMC_TIMING_UHS_SDR12))
1348 ctrl |= SDHCI_CTRL_HISPD;
1350 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1351 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1352 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1354 * We only need to set Driver Strength if the
1355 * preset value enable is not set.
1357 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1358 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1359 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1360 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1361 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1363 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1366 * According to SDHC Spec v3.00, if the Preset Value
1367 * Enable in the Host Control 2 register is set, we
1368 * need to reset SD Clock Enable before changing High
1369 * Speed Enable to avoid generating clock gliches.
1372 /* Reset SD Clock Enable */
1373 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1374 clk &= ~SDHCI_CLOCK_CARD_EN;
1375 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1377 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1379 /* Re-enable SD Clock */
1380 clock = host->clock;
1382 sdhci_set_clock(host, clock);
1386 /* Reset SD Clock Enable */
1387 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1388 clk &= ~SDHCI_CLOCK_CARD_EN;
1389 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1391 if (host->ops->set_uhs_signaling)
1392 host->ops->set_uhs_signaling(host, ios->timing);
1394 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1395 /* Select Bus Speed Mode for host */
1396 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1397 if (ios->timing == MMC_TIMING_UHS_SDR12)
1398 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1399 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1400 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1401 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1402 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1403 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1404 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1405 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1406 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1407 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1410 /* Re-enable SD Clock */
1411 clock = host->clock;
1413 sdhci_set_clock(host, clock);
1415 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1418 * Some (ENE) controllers go apeshit on some ios operation,
1419 * signalling timeout and CRC errors even on CMD0. Resetting
1420 * it on each ios seems to solve the problem.
1422 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1423 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1427 spin_unlock_irqrestore(&host->lock, flags);
1430 static int check_ro(struct sdhci_host *host)
1432 unsigned long flags;
1435 spin_lock_irqsave(&host->lock, flags);
1437 if (host->flags & SDHCI_DEVICE_DEAD)
1439 else if (host->ops->get_ro)
1440 is_readonly = host->ops->get_ro(host);
1442 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1443 & SDHCI_WRITE_PROTECT);
1445 spin_unlock_irqrestore(&host->lock, flags);
1447 /* This quirk needs to be replaced by a callback-function later */
1448 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1449 !is_readonly : is_readonly;
1452 #define SAMPLE_COUNT 5
1454 static int sdhci_get_ro(struct mmc_host *mmc)
1456 struct sdhci_host *host;
1459 host = mmc_priv(mmc);
1461 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1462 return check_ro(host);
1465 for (i = 0; i < SAMPLE_COUNT; i++) {
1466 if (check_ro(host)) {
1467 if (++ro_count > SAMPLE_COUNT / 2)
1475 static void sdhci_hw_reset(struct mmc_host *mmc)
1477 struct sdhci_host *host = mmc_priv(mmc);
1479 if (host->ops && host->ops->hw_reset)
1480 host->ops->hw_reset(host);
1483 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1485 struct sdhci_host *host;
1486 unsigned long flags;
1488 host = mmc_priv(mmc);
1490 spin_lock_irqsave(&host->lock, flags);
1492 if (host->flags & SDHCI_DEVICE_DEAD)
1496 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1498 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
1502 spin_unlock_irqrestore(&host->lock, flags);
1505 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1506 struct mmc_ios *ios)
1508 struct sdhci_host *host;
1513 host = mmc_priv(mmc);
1516 * Signal Voltage Switching is only applicable for Host Controllers
1519 if (host->version < SDHCI_SPEC_300)
1523 * We first check whether the request is to set signalling voltage
1524 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1526 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1527 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1528 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1529 ctrl &= ~SDHCI_CTRL_VDD_180;
1530 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1533 usleep_range(5000, 5500);
1535 /* 3.3V regulator output should be stable within 5 ms */
1536 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1537 if (!(ctrl & SDHCI_CTRL_VDD_180))
1540 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1541 "signalling voltage failed\n");
1544 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1545 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1547 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1548 clk &= ~SDHCI_CLOCK_CARD_EN;
1549 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1551 /* Check whether DAT[3:0] is 0000 */
1552 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1553 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1554 SDHCI_DATA_LVL_SHIFT)) {
1556 * Enable 1.8V Signal Enable in the Host Control2
1559 ctrl |= SDHCI_CTRL_VDD_180;
1560 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1563 usleep_range(5000, 5500);
1565 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1566 if (ctrl & SDHCI_CTRL_VDD_180) {
1567 /* Provide SDCLK again and wait for 1ms*/
1568 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1569 clk |= SDHCI_CLOCK_CARD_EN;
1570 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1571 usleep_range(1000, 1500);
1574 * If DAT[3:0] level is 1111b, then the card
1575 * was successfully switched to 1.8V signaling.
1577 present_state = sdhci_readl(host,
1578 SDHCI_PRESENT_STATE);
1579 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1580 SDHCI_DATA_LVL_MASK)
1586 * If we are here, that means the switch to 1.8V signaling
1587 * failed. We power cycle the card, and retry initialization
1588 * sequence by setting S18R to 0.
1590 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1591 pwr &= ~SDHCI_POWER_ON;
1592 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1594 /* Wait for 1ms as per the spec */
1595 usleep_range(1000, 1500);
1596 pwr |= SDHCI_POWER_ON;
1597 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1599 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1600 "voltage failed, retrying with S18R set to 0\n");
1603 /* No signal voltage switch required */
1607 static int sdhci_execute_tuning(struct mmc_host *mmc)
1609 struct sdhci_host *host;
1612 int tuning_loop_counter = MAX_TUNING_LOOP;
1613 unsigned long timeout;
1616 host = mmc_priv(mmc);
1618 disable_irq(host->irq);
1619 spin_lock(&host->lock);
1621 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1624 * Host Controller needs tuning only in case of SDR104 mode
1625 * and for SDR50 mode when Use Tuning for SDR50 is set in
1626 * Capabilities register.
1628 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1629 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1630 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1631 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1633 spin_unlock(&host->lock);
1634 enable_irq(host->irq);
1638 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1641 * As per the Host Controller spec v3.00, tuning command
1642 * generates Buffer Read Ready interrupt, so enable that.
1644 * Note: The spec clearly says that when tuning sequence
1645 * is being performed, the controller does not generate
1646 * interrupts other than Buffer Read Ready interrupt. But
1647 * to make sure we don't hit a controller bug, we _only_
1648 * enable Buffer Read Ready interrupt here.
1650 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1651 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1654 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1655 * of loops reaches 40 times or a timeout of 150ms occurs.
1659 struct mmc_command cmd = {0};
1660 struct mmc_request mrq = {0};
1662 if (!tuning_loop_counter && !timeout)
1665 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1667 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1676 * In response to CMD19, the card sends 64 bytes of tuning
1677 * block to the Host Controller. So we set the block size
1680 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1683 * The tuning block is sent by the card to the host controller.
1684 * So we set the TRNS_READ bit in the Transfer Mode register.
1685 * This also takes care of setting DMA Enable and Multi Block
1686 * Select in the same register to 0.
1688 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1690 sdhci_send_command(host, &cmd);
1695 spin_unlock(&host->lock);
1696 enable_irq(host->irq);
1698 /* Wait for Buffer Read Ready interrupt */
1699 wait_event_interruptible_timeout(host->buf_ready_int,
1700 (host->tuning_done == 1),
1701 msecs_to_jiffies(50));
1702 disable_irq(host->irq);
1703 spin_lock(&host->lock);
1705 if (!host->tuning_done) {
1706 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1707 "Buffer Read Ready interrupt during tuning "
1708 "procedure, falling back to fixed sampling "
1710 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1711 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1712 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1713 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1719 host->tuning_done = 0;
1721 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1722 tuning_loop_counter--;
1725 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1728 * The Host Driver has exhausted the maximum number of loops allowed,
1729 * so use fixed sampling frequency.
1731 if (!tuning_loop_counter || !timeout) {
1732 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1733 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1735 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1736 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1737 " failed, falling back to fixed sampling"
1745 * If this is the very first time we are here, we start the retuning
1746 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1747 * flag won't be set, we check this condition before actually starting
1750 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1751 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1752 mod_timer(&host->tuning_timer, jiffies +
1753 host->tuning_count * HZ);
1754 /* Tuning mode 1 limits the maximum data length to 4MB */
1755 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1757 host->flags &= ~SDHCI_NEEDS_RETUNING;
1758 /* Reload the new initial value for timer */
1759 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1760 mod_timer(&host->tuning_timer, jiffies +
1761 host->tuning_count * HZ);
1765 * In case tuning fails, host controllers which support re-tuning can
1766 * try tuning again at a later time, when the re-tuning timer expires.
1767 * So for these controllers, we return 0. Since there might be other
1768 * controllers who do not have this capability, we return error for
1771 if (err && host->tuning_count &&
1772 host->tuning_mode == SDHCI_TUNING_MODE_1)
1775 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1776 spin_unlock(&host->lock);
1777 enable_irq(host->irq);
1782 static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1784 struct sdhci_host *host;
1786 unsigned long flags;
1788 host = mmc_priv(mmc);
1790 /* Host Controller v3.00 defines preset value registers */
1791 if (host->version < SDHCI_SPEC_300)
1794 spin_lock_irqsave(&host->lock, flags);
1796 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1799 * We only enable or disable Preset Value if they are not already
1800 * enabled or disabled respectively. Otherwise, we bail out.
1802 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1803 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1804 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1805 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1806 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1807 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1810 spin_unlock_irqrestore(&host->lock, flags);
1813 static const struct mmc_host_ops sdhci_ops = {
1814 .request = sdhci_request,
1815 .set_ios = sdhci_set_ios,
1816 .get_ro = sdhci_get_ro,
1817 .hw_reset = sdhci_hw_reset,
1818 .enable_sdio_irq = sdhci_enable_sdio_irq,
1819 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
1820 .execute_tuning = sdhci_execute_tuning,
1821 .enable_preset_value = sdhci_enable_preset_value,
1824 /*****************************************************************************\
1828 \*****************************************************************************/
1830 static void sdhci_tasklet_card(unsigned long param)
1832 struct sdhci_host *host;
1833 unsigned long flags;
1835 host = (struct sdhci_host*)param;
1837 spin_lock_irqsave(&host->lock, flags);
1839 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
1841 printk(KERN_ERR "%s: Card removed during transfer!\n",
1842 mmc_hostname(host->mmc));
1843 printk(KERN_ERR "%s: Resetting controller.\n",
1844 mmc_hostname(host->mmc));
1846 sdhci_reset(host, SDHCI_RESET_CMD);
1847 sdhci_reset(host, SDHCI_RESET_DATA);
1849 host->mrq->cmd->error = -ENOMEDIUM;
1850 tasklet_schedule(&host->finish_tasklet);
1854 spin_unlock_irqrestore(&host->lock, flags);
1856 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
1859 static void sdhci_tasklet_finish(unsigned long param)
1861 struct sdhci_host *host;
1862 unsigned long flags;
1863 struct mmc_request *mrq;
1865 host = (struct sdhci_host*)param;
1868 * If this tasklet gets rescheduled while running, it will
1869 * be run again afterwards but without any active request.
1874 spin_lock_irqsave(&host->lock, flags);
1876 del_timer(&host->timer);
1881 * The controller needs a reset of internal state machines
1882 * upon error conditions.
1884 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1885 ((mrq->cmd && mrq->cmd->error) ||
1886 (mrq->data && (mrq->data->error ||
1887 (mrq->data->stop && mrq->data->stop->error))) ||
1888 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
1890 /* Some controllers need this kick or reset won't work here */
1891 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
1894 /* This is to force an update */
1895 clock = host->clock;
1897 sdhci_set_clock(host, clock);
1900 /* Spec says we should do both at the same time, but Ricoh
1901 controllers do not like that. */
1902 sdhci_reset(host, SDHCI_RESET_CMD);
1903 sdhci_reset(host, SDHCI_RESET_DATA);
1910 #ifndef SDHCI_USE_LEDS_CLASS
1911 sdhci_deactivate_led(host);
1915 spin_unlock_irqrestore(&host->lock, flags);
1917 mmc_request_done(host->mmc, mrq);
1920 static void sdhci_timeout_timer(unsigned long data)
1922 struct sdhci_host *host;
1923 unsigned long flags;
1925 host = (struct sdhci_host*)data;
1927 spin_lock_irqsave(&host->lock, flags);
1930 printk(KERN_ERR "%s: Timeout waiting for hardware "
1931 "interrupt.\n", mmc_hostname(host->mmc));
1932 sdhci_dumpregs(host);
1935 host->data->error = -ETIMEDOUT;
1936 sdhci_finish_data(host);
1939 host->cmd->error = -ETIMEDOUT;
1941 host->mrq->cmd->error = -ETIMEDOUT;
1943 tasklet_schedule(&host->finish_tasklet);
1948 spin_unlock_irqrestore(&host->lock, flags);
1951 static void sdhci_tuning_timer(unsigned long data)
1953 struct sdhci_host *host;
1954 unsigned long flags;
1956 host = (struct sdhci_host *)data;
1958 spin_lock_irqsave(&host->lock, flags);
1960 host->flags |= SDHCI_NEEDS_RETUNING;
1962 spin_unlock_irqrestore(&host->lock, flags);
1965 /*****************************************************************************\
1967 * Interrupt handling *
1969 \*****************************************************************************/
1971 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1973 BUG_ON(intmask == 0);
1976 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1977 "though no command operation was in progress.\n",
1978 mmc_hostname(host->mmc), (unsigned)intmask);
1979 sdhci_dumpregs(host);
1983 if (intmask & SDHCI_INT_TIMEOUT)
1984 host->cmd->error = -ETIMEDOUT;
1985 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1987 host->cmd->error = -EILSEQ;
1989 if (host->cmd->error) {
1990 tasklet_schedule(&host->finish_tasklet);
1995 * The host can send and interrupt when the busy state has
1996 * ended, allowing us to wait without wasting CPU cycles.
1997 * Unfortunately this is overloaded on the "data complete"
1998 * interrupt, so we need to take some care when handling
2001 * Note: The 1.0 specification is a bit ambiguous about this
2002 * feature so there might be some problems with older
2005 if (host->cmd->flags & MMC_RSP_BUSY) {
2006 if (host->cmd->data)
2007 DBG("Cannot wait for busy signal when also "
2008 "doing a data transfer");
2009 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
2012 /* The controller does not support the end-of-busy IRQ,
2013 * fall through and take the SDHCI_INT_RESPONSE */
2016 if (intmask & SDHCI_INT_RESPONSE)
2017 sdhci_finish_command(host);
2020 #ifdef CONFIG_MMC_DEBUG
2021 static void sdhci_show_adma_error(struct sdhci_host *host)
2023 const char *name = mmc_hostname(host->mmc);
2024 u8 *desc = host->adma_desc;
2029 sdhci_dumpregs(host);
2032 dma = (__le32 *)(desc + 4);
2033 len = (__le16 *)(desc + 2);
2036 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2037 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2046 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2049 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2051 BUG_ON(intmask == 0);
2053 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2054 if (intmask & SDHCI_INT_DATA_AVAIL) {
2055 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2056 MMC_SEND_TUNING_BLOCK) {
2057 host->tuning_done = 1;
2058 wake_up(&host->buf_ready_int);
2065 * The "data complete" interrupt is also used to
2066 * indicate that a busy state has ended. See comment
2067 * above in sdhci_cmd_irq().
2069 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2070 if (intmask & SDHCI_INT_DATA_END) {
2071 sdhci_finish_command(host);
2076 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
2077 "though no data operation was in progress.\n",
2078 mmc_hostname(host->mmc), (unsigned)intmask);
2079 sdhci_dumpregs(host);
2084 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2085 host->data->error = -ETIMEDOUT;
2086 else if (intmask & SDHCI_INT_DATA_END_BIT)
2087 host->data->error = -EILSEQ;
2088 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2089 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2091 host->data->error = -EILSEQ;
2092 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2093 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
2094 sdhci_show_adma_error(host);
2095 host->data->error = -EIO;
2098 if (host->data->error)
2099 sdhci_finish_data(host);
2101 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2102 sdhci_transfer_pio(host);
2105 * We currently don't do anything fancy with DMA
2106 * boundaries, but as we can't disable the feature
2107 * we need to at least restart the transfer.
2109 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2110 * should return a valid address to continue from, but as
2111 * some controllers are faulty, don't trust them.
2113 if (intmask & SDHCI_INT_DMA_END) {
2114 u32 dmastart, dmanow;
2115 dmastart = sg_dma_address(host->data->sg);
2116 dmanow = dmastart + host->data->bytes_xfered;
2118 * Force update to the next DMA block boundary.
2121 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2122 SDHCI_DEFAULT_BOUNDARY_SIZE;
2123 host->data->bytes_xfered = dmanow - dmastart;
2124 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2126 mmc_hostname(host->mmc), dmastart,
2127 host->data->bytes_xfered, dmanow);
2128 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2131 if (intmask & SDHCI_INT_DATA_END) {
2134 * Data managed to finish before the
2135 * command completed. Make sure we do
2136 * things in the proper order.
2138 host->data_early = 1;
2140 sdhci_finish_data(host);
2146 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2149 struct sdhci_host* host = dev_id;
2153 spin_lock(&host->lock);
2155 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2157 if (!intmask || intmask == 0xffffffff) {
2162 DBG("*** %s got interrupt: 0x%08x\n",
2163 mmc_hostname(host->mmc), intmask);
2165 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2166 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2170 * There is a observation on i.mx esdhc. INSERT bit will be
2171 * immediately set again when it gets cleared, if a card is
2172 * inserted. We have to mask the irq to prevent interrupt
2173 * storm which will freeze the system. And the REMOVE gets
2174 * the same situation.
2176 * More testing are needed here to ensure it works for other
2179 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2180 SDHCI_INT_CARD_REMOVE);
2181 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2182 SDHCI_INT_CARD_INSERT);
2184 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2185 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2186 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2187 tasklet_schedule(&host->card_tasklet);
2190 if (intmask & SDHCI_INT_CMD_MASK) {
2191 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2193 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2196 if (intmask & SDHCI_INT_DATA_MASK) {
2197 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2199 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2202 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2204 intmask &= ~SDHCI_INT_ERROR;
2206 if (intmask & SDHCI_INT_BUS_POWER) {
2207 printk(KERN_ERR "%s: Card is consuming too much power!\n",
2208 mmc_hostname(host->mmc));
2209 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
2212 intmask &= ~SDHCI_INT_BUS_POWER;
2214 if (intmask & SDHCI_INT_CARD_INT)
2217 intmask &= ~SDHCI_INT_CARD_INT;
2220 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
2221 mmc_hostname(host->mmc), intmask);
2222 sdhci_dumpregs(host);
2224 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2227 result = IRQ_HANDLED;
2231 spin_unlock(&host->lock);
2234 * We have to delay this as it calls back into the driver.
2237 mmc_signal_sdio_irq(host->mmc);
2242 /*****************************************************************************\
2246 \*****************************************************************************/
2250 int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
2254 sdhci_disable_card_detection(host);
2256 /* Disable tuning since we are suspending */
2257 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2258 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2259 host->flags &= ~SDHCI_NEEDS_RETUNING;
2260 mod_timer(&host->tuning_timer, jiffies +
2261 host->tuning_count * HZ);
2264 ret = mmc_suspend_host(host->mmc);
2268 free_irq(host->irq, host);
2271 ret = regulator_disable(host->vmmc);
2276 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2278 int sdhci_resume_host(struct sdhci_host *host)
2283 int ret = regulator_enable(host->vmmc);
2289 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2290 if (host->ops->enable_dma)
2291 host->ops->enable_dma(host);
2294 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2295 mmc_hostname(host->mmc), host);
2299 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2302 ret = mmc_resume_host(host->mmc);
2303 sdhci_enable_card_detection(host);
2305 /* Set the re-tuning expiration flag */
2306 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2307 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2308 host->flags |= SDHCI_NEEDS_RETUNING;
2313 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2315 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2318 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2319 val |= SDHCI_WAKE_ON_INT;
2320 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2323 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2325 #endif /* CONFIG_PM */
2327 /*****************************************************************************\
2329 * Device allocation/registration *
2331 \*****************************************************************************/
2333 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2336 struct mmc_host *mmc;
2337 struct sdhci_host *host;
2339 WARN_ON(dev == NULL);
2341 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2343 return ERR_PTR(-ENOMEM);
2345 host = mmc_priv(mmc);
2351 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2353 int sdhci_add_host(struct sdhci_host *host)
2355 struct mmc_host *mmc;
2357 u32 max_current_caps;
2358 unsigned int ocr_avail;
2361 WARN_ON(host == NULL);
2368 host->quirks = debug_quirks;
2370 sdhci_reset(host, SDHCI_RESET_ALL);
2372 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2373 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2374 >> SDHCI_SPEC_VER_SHIFT;
2375 if (host->version > SDHCI_SPEC_300) {
2376 printk(KERN_ERR "%s: Unknown controller version (%d). "
2377 "You may experience problems.\n", mmc_hostname(mmc),
2381 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2382 sdhci_readl(host, SDHCI_CAPABILITIES);
2384 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2385 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2387 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2388 host->flags |= SDHCI_USE_SDMA;
2389 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2390 DBG("Controller doesn't have SDMA capability\n");
2392 host->flags |= SDHCI_USE_SDMA;
2394 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2395 (host->flags & SDHCI_USE_SDMA)) {
2396 DBG("Disabling DMA as it is marked broken\n");
2397 host->flags &= ~SDHCI_USE_SDMA;
2400 if ((host->version >= SDHCI_SPEC_200) &&
2401 (caps[0] & SDHCI_CAN_DO_ADMA2))
2402 host->flags |= SDHCI_USE_ADMA;
2404 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2405 (host->flags & SDHCI_USE_ADMA)) {
2406 DBG("Disabling ADMA as it is marked broken\n");
2407 host->flags &= ~SDHCI_USE_ADMA;
2410 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2411 if (host->ops->enable_dma) {
2412 if (host->ops->enable_dma(host)) {
2413 printk(KERN_WARNING "%s: No suitable DMA "
2414 "available. Falling back to PIO.\n",
2417 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2422 if (host->flags & SDHCI_USE_ADMA) {
2424 * We need to allocate descriptors for all sg entries
2425 * (128) and potentially one alignment transfer for
2426 * each of those entries.
2428 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2429 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2430 if (!host->adma_desc || !host->align_buffer) {
2431 kfree(host->adma_desc);
2432 kfree(host->align_buffer);
2433 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2434 "buffers. Falling back to standard DMA.\n",
2436 host->flags &= ~SDHCI_USE_ADMA;
2441 * If we use DMA, then it's up to the caller to set the DMA
2442 * mask, but PIO does not need the hw shim so we set a new
2443 * mask here in that case.
2445 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2446 host->dma_mask = DMA_BIT_MASK(64);
2447 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2450 if (host->version >= SDHCI_SPEC_300)
2451 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
2452 >> SDHCI_CLOCK_BASE_SHIFT;
2454 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
2455 >> SDHCI_CLOCK_BASE_SHIFT;
2457 host->max_clk *= 1000000;
2458 if (host->max_clk == 0 || host->quirks &
2459 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
2460 if (!host->ops->get_max_clock) {
2462 "%s: Hardware doesn't specify base clock "
2463 "frequency.\n", mmc_hostname(mmc));
2466 host->max_clk = host->ops->get_max_clock(host);
2470 * In case of Host Controller v3.00, find out whether clock
2471 * multiplier is supported.
2473 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2474 SDHCI_CLOCK_MUL_SHIFT;
2477 * In case the value in Clock Multiplier is 0, then programmable
2478 * clock mode is not supported, otherwise the actual clock
2479 * multiplier is one more than the value of Clock Multiplier
2480 * in the Capabilities Register.
2486 * Set host parameters.
2488 mmc->ops = &sdhci_ops;
2489 mmc->f_max = host->max_clk;
2490 if (host->ops->get_min_clock)
2491 mmc->f_min = host->ops->get_min_clock(host);
2492 else if (host->version >= SDHCI_SPEC_300) {
2493 if (host->clk_mul) {
2494 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2495 mmc->f_max = host->max_clk * host->clk_mul;
2497 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2499 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
2502 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2503 if (host->timeout_clk == 0) {
2504 if (host->ops->get_timeout_clock) {
2505 host->timeout_clk = host->ops->get_timeout_clock(host);
2506 } else if (!(host->quirks &
2507 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2509 "%s: Hardware doesn't specify timeout clock "
2510 "frequency.\n", mmc_hostname(mmc));
2514 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2515 host->timeout_clk *= 1000;
2517 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2518 host->timeout_clk = mmc->f_max / 1000;
2520 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
2522 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2524 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2525 host->flags |= SDHCI_AUTO_CMD12;
2527 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2528 if ((host->version >= SDHCI_SPEC_300) &&
2529 ((host->flags & SDHCI_USE_ADMA) ||
2530 !(host->flags & SDHCI_USE_SDMA))) {
2531 host->flags |= SDHCI_AUTO_CMD23;
2532 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2534 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2538 * A controller may support 8-bit width, but the board itself
2539 * might not have the pins brought out. Boards that support
2540 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2541 * their platform code before calling sdhci_add_host(), and we
2542 * won't assume 8-bit width for hosts without that CAP.
2544 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
2545 mmc->caps |= MMC_CAP_4_BIT_DATA;
2547 if (caps[0] & SDHCI_CAN_DO_HISPD)
2548 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2550 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2551 mmc_card_is_removable(mmc))
2552 mmc->caps |= MMC_CAP_NEEDS_POLL;
2554 /* UHS-I mode(s) supported by the host controller. */
2555 if (host->version >= SDHCI_SPEC_300)
2556 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2558 /* SDR104 supports also implies SDR50 support */
2559 if (caps[1] & SDHCI_SUPPORT_SDR104)
2560 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2561 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2562 mmc->caps |= MMC_CAP_UHS_SDR50;
2564 if (caps[1] & SDHCI_SUPPORT_DDR50)
2565 mmc->caps |= MMC_CAP_UHS_DDR50;
2567 /* Does the host needs tuning for SDR50? */
2568 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2569 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2571 /* Driver Type(s) (A, C, D) supported by the host */
2572 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2573 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2574 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2575 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2576 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2577 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2579 /* Initial value for re-tuning timer count */
2580 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2581 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2584 * In case Re-tuning Timer is not disabled, the actual value of
2585 * re-tuning timer will be 2 ^ (n - 1).
2587 if (host->tuning_count)
2588 host->tuning_count = 1 << (host->tuning_count - 1);
2590 /* Re-tuning mode supported by the Host Controller */
2591 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2592 SDHCI_RETUNING_MODE_SHIFT;
2596 * According to SD Host Controller spec v3.00, if the Host System
2597 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2598 * the value is meaningful only if Voltage Support in the Capabilities
2599 * register is set. The actual current value is 4 times the register
2602 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2604 if (caps[0] & SDHCI_CAN_VDD_330) {
2605 int max_current_330;
2607 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
2609 max_current_330 = ((max_current_caps &
2610 SDHCI_MAX_CURRENT_330_MASK) >>
2611 SDHCI_MAX_CURRENT_330_SHIFT) *
2612 SDHCI_MAX_CURRENT_MULTIPLIER;
2614 if (max_current_330 > 150)
2615 mmc->caps |= MMC_CAP_SET_XPC_330;
2617 if (caps[0] & SDHCI_CAN_VDD_300) {
2618 int max_current_300;
2620 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
2622 max_current_300 = ((max_current_caps &
2623 SDHCI_MAX_CURRENT_300_MASK) >>
2624 SDHCI_MAX_CURRENT_300_SHIFT) *
2625 SDHCI_MAX_CURRENT_MULTIPLIER;
2627 if (max_current_300 > 150)
2628 mmc->caps |= MMC_CAP_SET_XPC_300;
2630 if (caps[0] & SDHCI_CAN_VDD_180) {
2631 int max_current_180;
2633 ocr_avail |= MMC_VDD_165_195;
2635 max_current_180 = ((max_current_caps &
2636 SDHCI_MAX_CURRENT_180_MASK) >>
2637 SDHCI_MAX_CURRENT_180_SHIFT) *
2638 SDHCI_MAX_CURRENT_MULTIPLIER;
2640 if (max_current_180 > 150)
2641 mmc->caps |= MMC_CAP_SET_XPC_180;
2643 /* Maximum current capabilities of the host at 1.8V */
2644 if (max_current_180 >= 800)
2645 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2646 else if (max_current_180 >= 600)
2647 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2648 else if (max_current_180 >= 400)
2649 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2651 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
2654 mmc->ocr_avail = ocr_avail;
2655 mmc->ocr_avail_sdio = ocr_avail;
2656 if (host->ocr_avail_sdio)
2657 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2658 mmc->ocr_avail_sd = ocr_avail;
2659 if (host->ocr_avail_sd)
2660 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2661 else /* normal SD controllers don't support 1.8V */
2662 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2663 mmc->ocr_avail_mmc = ocr_avail;
2664 if (host->ocr_avail_mmc)
2665 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
2667 if (mmc->ocr_avail == 0) {
2668 printk(KERN_ERR "%s: Hardware doesn't report any "
2669 "support voltages.\n", mmc_hostname(mmc));
2673 spin_lock_init(&host->lock);
2676 * Maximum number of segments. Depends on if the hardware
2677 * can do scatter/gather or not.
2679 if (host->flags & SDHCI_USE_ADMA)
2680 mmc->max_segs = 128;
2681 else if (host->flags & SDHCI_USE_SDMA)
2684 mmc->max_segs = 128;
2687 * Maximum number of sectors in one transfer. Limited by DMA boundary
2690 mmc->max_req_size = 524288;
2693 * Maximum segment size. Could be one segment with the maximum number
2694 * of bytes. When doing hardware scatter/gather, each entry cannot
2695 * be larger than 64 KiB though.
2697 if (host->flags & SDHCI_USE_ADMA) {
2698 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2699 mmc->max_seg_size = 65535;
2701 mmc->max_seg_size = 65536;
2703 mmc->max_seg_size = mmc->max_req_size;
2707 * Maximum block size. This varies from controller to controller and
2708 * is specified in the capabilities register.
2710 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2711 mmc->max_blk_size = 2;
2713 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
2714 SDHCI_MAX_BLOCK_SHIFT;
2715 if (mmc->max_blk_size >= 3) {
2716 printk(KERN_WARNING "%s: Invalid maximum block size, "
2717 "assuming 512 bytes\n", mmc_hostname(mmc));
2718 mmc->max_blk_size = 0;
2722 mmc->max_blk_size = 512 << mmc->max_blk_size;
2725 * Maximum block count.
2727 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
2732 tasklet_init(&host->card_tasklet,
2733 sdhci_tasklet_card, (unsigned long)host);
2734 tasklet_init(&host->finish_tasklet,
2735 sdhci_tasklet_finish, (unsigned long)host);
2737 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
2739 if (host->version >= SDHCI_SPEC_300) {
2740 init_waitqueue_head(&host->buf_ready_int);
2742 /* Initialize re-tuning timer */
2743 init_timer(&host->tuning_timer);
2744 host->tuning_timer.data = (unsigned long)host;
2745 host->tuning_timer.function = sdhci_tuning_timer;
2748 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2749 mmc_hostname(mmc), host);
2753 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2754 if (IS_ERR(host->vmmc)) {
2755 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2758 regulator_enable(host->vmmc);
2761 sdhci_init(host, 0);
2763 #ifdef CONFIG_MMC_DEBUG
2764 sdhci_dumpregs(host);
2767 #ifdef SDHCI_USE_LEDS_CLASS
2768 snprintf(host->led_name, sizeof(host->led_name),
2769 "%s::", mmc_hostname(mmc));
2770 host->led.name = host->led_name;
2771 host->led.brightness = LED_OFF;
2772 host->led.default_trigger = mmc_hostname(mmc);
2773 host->led.brightness_set = sdhci_led_control;
2775 ret = led_classdev_register(mmc_dev(mmc), &host->led);
2784 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
2785 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
2786 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2787 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
2789 sdhci_enable_card_detection(host);
2793 #ifdef SDHCI_USE_LEDS_CLASS
2795 sdhci_reset(host, SDHCI_RESET_ALL);
2796 free_irq(host->irq, host);
2799 tasklet_kill(&host->card_tasklet);
2800 tasklet_kill(&host->finish_tasklet);
2805 EXPORT_SYMBOL_GPL(sdhci_add_host);
2807 void sdhci_remove_host(struct sdhci_host *host, int dead)
2809 unsigned long flags;
2812 spin_lock_irqsave(&host->lock, flags);
2814 host->flags |= SDHCI_DEVICE_DEAD;
2817 printk(KERN_ERR "%s: Controller removed during "
2818 " transfer!\n", mmc_hostname(host->mmc));
2820 host->mrq->cmd->error = -ENOMEDIUM;
2821 tasklet_schedule(&host->finish_tasklet);
2824 spin_unlock_irqrestore(&host->lock, flags);
2827 sdhci_disable_card_detection(host);
2829 mmc_remove_host(host->mmc);
2831 #ifdef SDHCI_USE_LEDS_CLASS
2832 led_classdev_unregister(&host->led);
2836 sdhci_reset(host, SDHCI_RESET_ALL);
2838 free_irq(host->irq, host);
2840 del_timer_sync(&host->timer);
2841 if (host->version >= SDHCI_SPEC_300)
2842 del_timer_sync(&host->tuning_timer);
2844 tasklet_kill(&host->card_tasklet);
2845 tasklet_kill(&host->finish_tasklet);
2848 regulator_disable(host->vmmc);
2849 regulator_put(host->vmmc);
2852 kfree(host->adma_desc);
2853 kfree(host->align_buffer);
2855 host->adma_desc = NULL;
2856 host->align_buffer = NULL;
2859 EXPORT_SYMBOL_GPL(sdhci_remove_host);
2861 void sdhci_free_host(struct sdhci_host *host)
2863 mmc_free_host(host->mmc);
2866 EXPORT_SYMBOL_GPL(sdhci_free_host);
2868 /*****************************************************************************\
2870 * Driver init/exit *
2872 \*****************************************************************************/
2874 static int __init sdhci_drv_init(void)
2876 printk(KERN_INFO DRIVER_NAME
2877 ": Secure Digital Host Controller Interface driver\n");
2878 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2883 static void __exit sdhci_drv_exit(void)
2887 module_init(sdhci_drv_init);
2888 module_exit(sdhci_drv_exit);
2890 module_param(debug_quirks, uint, 0444);
2892 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
2893 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
2894 MODULE_LICENSE("GPL");
2896 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");