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>
24 #include <linux/pm_runtime.h>
26 #include <linux/leds.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/slot-gpio.h>
35 #define DRIVER_NAME "sdhci"
37 #define DBG(f, x...) \
38 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
40 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42 #define SDHCI_USE_LEDS_CLASS
45 #define MAX_TUNING_LOOP 40
47 #define ADMA_SIZE ((128 * 2 + 1) * 4)
49 static unsigned int debug_quirks = 0;
50 static unsigned int debug_quirks2;
52 static void sdhci_finish_data(struct sdhci_host *);
54 static void sdhci_finish_command(struct sdhci_host *);
55 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
56 static void sdhci_tuning_timer(unsigned long data);
57 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
59 #ifdef CONFIG_PM_RUNTIME
60 static int sdhci_runtime_pm_get(struct sdhci_host *host);
61 static int sdhci_runtime_pm_put(struct sdhci_host *host);
62 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
63 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
65 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
69 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
73 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
76 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
81 static void sdhci_dumpregs(struct sdhci_host *host)
83 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
84 mmc_hostname(host->mmc));
86 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
87 sdhci_readl(host, SDHCI_DMA_ADDRESS),
88 sdhci_readw(host, SDHCI_HOST_VERSION));
89 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
90 sdhci_readw(host, SDHCI_BLOCK_SIZE),
91 sdhci_readw(host, SDHCI_BLOCK_COUNT));
92 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
93 sdhci_readl(host, SDHCI_ARGUMENT),
94 sdhci_readw(host, SDHCI_TRANSFER_MODE));
95 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
96 sdhci_readl(host, SDHCI_PRESENT_STATE),
97 sdhci_readb(host, SDHCI_HOST_CONTROL));
98 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
99 sdhci_readb(host, SDHCI_POWER_CONTROL),
100 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
101 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
102 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
103 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
104 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
105 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
106 sdhci_readl(host, SDHCI_INT_STATUS));
107 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
108 sdhci_readl(host, SDHCI_INT_ENABLE),
109 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
110 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
111 sdhci_readw(host, SDHCI_ACMD12_ERR),
112 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
113 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
114 sdhci_readl(host, SDHCI_CAPABILITIES),
115 sdhci_readl(host, SDHCI_CAPABILITIES_1));
116 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
117 sdhci_readw(host, SDHCI_COMMAND),
118 sdhci_readl(host, SDHCI_MAX_CURRENT));
119 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
120 sdhci_readw(host, SDHCI_HOST_CONTROL2));
122 if (host->flags & SDHCI_USE_ADMA)
123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127 pr_debug(DRIVER_NAME ": ===========================================\n");
130 /*****************************************************************************\
132 * Low level functions *
134 \*****************************************************************************/
136 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
140 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
141 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
145 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
148 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
149 SDHCI_INT_CARD_INSERT;
151 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
154 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
155 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
158 static void sdhci_enable_card_detection(struct sdhci_host *host)
160 sdhci_set_card_detection(host, true);
163 static void sdhci_disable_card_detection(struct sdhci_host *host)
165 sdhci_set_card_detection(host, false);
168 void sdhci_reset(struct sdhci_host *host, u8 mask)
170 unsigned long timeout;
172 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
174 if (mask & SDHCI_RESET_ALL) {
176 /* Reset-all turns off SD Bus Power */
177 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
178 sdhci_runtime_pm_bus_off(host);
181 /* Wait max 100 ms */
184 /* hw clears the bit when it's done */
185 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
187 pr_err("%s: Reset 0x%x never completed.\n",
188 mmc_hostname(host->mmc), (int)mask);
189 sdhci_dumpregs(host);
196 EXPORT_SYMBOL_GPL(sdhci_reset);
198 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
200 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
201 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
206 host->ops->reset(host, mask);
208 if (mask & SDHCI_RESET_ALL) {
209 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
210 if (host->ops->enable_dma)
211 host->ops->enable_dma(host);
214 /* Resetting the controller clears many */
215 host->preset_enabled = false;
219 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
221 static void sdhci_init(struct sdhci_host *host, int soft)
224 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
226 sdhci_do_reset(host, SDHCI_RESET_ALL);
228 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
229 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
230 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
231 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
234 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
235 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
238 /* force clock reconfiguration */
240 sdhci_set_ios(host->mmc, &host->mmc->ios);
244 static void sdhci_reinit(struct sdhci_host *host)
248 * Retuning stuffs are affected by different cards inserted and only
249 * applicable to UHS-I cards. So reset these fields to their initial
250 * value when card is removed.
252 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
253 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
255 del_timer_sync(&host->tuning_timer);
256 host->flags &= ~SDHCI_NEEDS_RETUNING;
257 host->mmc->max_blk_count =
258 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
260 sdhci_enable_card_detection(host);
263 static void sdhci_activate_led(struct sdhci_host *host)
267 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
268 ctrl |= SDHCI_CTRL_LED;
269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
272 static void sdhci_deactivate_led(struct sdhci_host *host)
276 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
277 ctrl &= ~SDHCI_CTRL_LED;
278 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
281 #ifdef SDHCI_USE_LEDS_CLASS
282 static void sdhci_led_control(struct led_classdev *led,
283 enum led_brightness brightness)
285 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
288 spin_lock_irqsave(&host->lock, flags);
290 if (host->runtime_suspended)
293 if (brightness == LED_OFF)
294 sdhci_deactivate_led(host);
296 sdhci_activate_led(host);
298 spin_unlock_irqrestore(&host->lock, flags);
302 /*****************************************************************************\
306 \*****************************************************************************/
308 static void sdhci_read_block_pio(struct sdhci_host *host)
311 size_t blksize, len, chunk;
312 u32 uninitialized_var(scratch);
315 DBG("PIO reading\n");
317 blksize = host->data->blksz;
320 local_irq_save(flags);
323 if (!sg_miter_next(&host->sg_miter))
326 len = min(host->sg_miter.length, blksize);
329 host->sg_miter.consumed = len;
331 buf = host->sg_miter.addr;
335 scratch = sdhci_readl(host, SDHCI_BUFFER);
339 *buf = scratch & 0xFF;
348 sg_miter_stop(&host->sg_miter);
350 local_irq_restore(flags);
353 static void sdhci_write_block_pio(struct sdhci_host *host)
356 size_t blksize, len, chunk;
360 DBG("PIO writing\n");
362 blksize = host->data->blksz;
366 local_irq_save(flags);
369 if (!sg_miter_next(&host->sg_miter))
372 len = min(host->sg_miter.length, blksize);
375 host->sg_miter.consumed = len;
377 buf = host->sg_miter.addr;
380 scratch |= (u32)*buf << (chunk * 8);
386 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
387 sdhci_writel(host, scratch, SDHCI_BUFFER);
394 sg_miter_stop(&host->sg_miter);
396 local_irq_restore(flags);
399 static void sdhci_transfer_pio(struct sdhci_host *host)
405 if (host->blocks == 0)
408 if (host->data->flags & MMC_DATA_READ)
409 mask = SDHCI_DATA_AVAILABLE;
411 mask = SDHCI_SPACE_AVAILABLE;
414 * Some controllers (JMicron JMB38x) mess up the buffer bits
415 * for transfers < 4 bytes. As long as it is just one block,
416 * we can ignore the bits.
418 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
419 (host->data->blocks == 1))
422 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
423 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
426 if (host->data->flags & MMC_DATA_READ)
427 sdhci_read_block_pio(host);
429 sdhci_write_block_pio(host);
432 if (host->blocks == 0)
436 DBG("PIO transfer complete.\n");
439 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
441 local_irq_save(*flags);
442 return kmap_atomic(sg_page(sg)) + sg->offset;
445 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
447 kunmap_atomic(buffer);
448 local_irq_restore(*flags);
451 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
453 __le32 *dataddr = (__le32 __force *)(desc + 4);
454 __le16 *cmdlen = (__le16 __force *)desc;
456 /* SDHCI specification says ADMA descriptors should be 4 byte
457 * aligned, so using 16 or 32bit operations should be safe. */
459 cmdlen[0] = cpu_to_le16(cmd);
460 cmdlen[1] = cpu_to_le16(len);
462 dataddr[0] = cpu_to_le32(addr);
465 static int sdhci_adma_table_pre(struct sdhci_host *host,
466 struct mmc_data *data)
473 dma_addr_t align_addr;
476 struct scatterlist *sg;
482 * The spec does not specify endianness of descriptor table.
483 * We currently guess that it is LE.
486 if (data->flags & MMC_DATA_READ)
487 direction = DMA_FROM_DEVICE;
489 direction = DMA_TO_DEVICE;
491 host->align_addr = dma_map_single(mmc_dev(host->mmc),
492 host->align_buffer, 128 * 4, direction);
493 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
495 BUG_ON(host->align_addr & 0x3);
497 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
498 data->sg, data->sg_len, direction);
499 if (host->sg_count == 0)
502 desc = host->adma_desc;
503 align = host->align_buffer;
505 align_addr = host->align_addr;
507 for_each_sg(data->sg, sg, host->sg_count, i) {
508 addr = sg_dma_address(sg);
509 len = sg_dma_len(sg);
512 * The SDHCI specification states that ADMA
513 * addresses must be 32-bit aligned. If they
514 * aren't, then we use a bounce buffer for
515 * the (up to three) bytes that screw up the
518 offset = (4 - (addr & 0x3)) & 0x3;
520 if (data->flags & MMC_DATA_WRITE) {
521 buffer = sdhci_kmap_atomic(sg, &flags);
522 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
523 memcpy(align, buffer, offset);
524 sdhci_kunmap_atomic(buffer, &flags);
528 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
530 BUG_ON(offset > 65536);
544 sdhci_set_adma_desc(desc, addr, len, 0x21);
548 * If this triggers then we have a calculation bug
551 WARN_ON((desc - host->adma_desc) > ADMA_SIZE);
554 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
556 * Mark the last descriptor as the terminating descriptor
558 if (desc != host->adma_desc) {
560 desc[0] |= 0x2; /* end */
564 * Add a terminating entry.
567 /* nop, end, valid */
568 sdhci_set_adma_desc(desc, 0, 0, 0x3);
572 * Resync align buffer as we might have changed it.
574 if (data->flags & MMC_DATA_WRITE) {
575 dma_sync_single_for_device(mmc_dev(host->mmc),
576 host->align_addr, 128 * 4, direction);
582 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
588 static void sdhci_adma_table_post(struct sdhci_host *host,
589 struct mmc_data *data)
593 struct scatterlist *sg;
600 if (data->flags & MMC_DATA_READ)
601 direction = DMA_FROM_DEVICE;
603 direction = DMA_TO_DEVICE;
605 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
608 /* Do a quick scan of the SG list for any unaligned mappings */
609 has_unaligned = false;
610 for_each_sg(data->sg, sg, host->sg_count, i)
611 if (sg_dma_address(sg) & 3) {
612 has_unaligned = true;
616 if (has_unaligned && data->flags & MMC_DATA_READ) {
617 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
618 data->sg_len, direction);
620 align = host->align_buffer;
622 for_each_sg(data->sg, sg, host->sg_count, i) {
623 if (sg_dma_address(sg) & 0x3) {
624 size = 4 - (sg_dma_address(sg) & 0x3);
626 buffer = sdhci_kmap_atomic(sg, &flags);
627 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
628 memcpy(buffer, align, size);
629 sdhci_kunmap_atomic(buffer, &flags);
636 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
637 data->sg_len, direction);
640 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
643 struct mmc_data *data = cmd->data;
644 unsigned target_timeout, current_timeout;
647 * If the host controller provides us with an incorrect timeout
648 * value, just skip the check and use 0xE. The hardware may take
649 * longer to time out, but that's much better than having a too-short
652 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
655 /* Unspecified timeout, assume max */
656 if (!data && !cmd->busy_timeout)
661 target_timeout = cmd->busy_timeout * 1000;
663 target_timeout = data->timeout_ns / 1000;
665 target_timeout += data->timeout_clks / host->clock;
669 * Figure out needed cycles.
670 * We do this in steps in order to fit inside a 32 bit int.
671 * The first step is the minimum timeout, which will have a
672 * minimum resolution of 6 bits:
673 * (1) 2^13*1000 > 2^22,
674 * (2) host->timeout_clk < 2^16
679 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
680 while (current_timeout < target_timeout) {
682 current_timeout <<= 1;
688 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
689 mmc_hostname(host->mmc), count, cmd->opcode);
696 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
698 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
699 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
701 if (host->flags & SDHCI_REQ_USE_DMA)
702 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
704 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
706 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
707 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
710 static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
714 if (host->ops->set_timeout) {
715 host->ops->set_timeout(host, cmd);
717 count = sdhci_calc_timeout(host, cmd);
718 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
722 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
725 struct mmc_data *data = cmd->data;
730 if (data || (cmd->flags & MMC_RSP_BUSY))
731 sdhci_set_timeout(host, cmd);
737 BUG_ON(data->blksz * data->blocks > 524288);
738 BUG_ON(data->blksz > host->mmc->max_blk_size);
739 BUG_ON(data->blocks > 65535);
742 host->data_early = 0;
743 host->data->bytes_xfered = 0;
745 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
746 host->flags |= SDHCI_REQ_USE_DMA;
749 * FIXME: This doesn't account for merging when mapping the
752 if (host->flags & SDHCI_REQ_USE_DMA) {
754 struct scatterlist *sg;
757 if (host->flags & SDHCI_USE_ADMA) {
758 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
761 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
765 if (unlikely(broken)) {
766 for_each_sg(data->sg, sg, data->sg_len, i) {
767 if (sg->length & 0x3) {
768 DBG("Reverting to PIO because of "
769 "transfer size (%d)\n",
771 host->flags &= ~SDHCI_REQ_USE_DMA;
779 * The assumption here being that alignment is the same after
780 * translation to device address space.
782 if (host->flags & SDHCI_REQ_USE_DMA) {
784 struct scatterlist *sg;
787 if (host->flags & SDHCI_USE_ADMA) {
789 * As we use 3 byte chunks to work around
790 * alignment problems, we need to check this
793 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
800 if (unlikely(broken)) {
801 for_each_sg(data->sg, sg, data->sg_len, i) {
802 if (sg->offset & 0x3) {
803 DBG("Reverting to PIO because of "
805 host->flags &= ~SDHCI_REQ_USE_DMA;
812 if (host->flags & SDHCI_REQ_USE_DMA) {
813 if (host->flags & SDHCI_USE_ADMA) {
814 ret = sdhci_adma_table_pre(host, data);
817 * This only happens when someone fed
818 * us an invalid request.
821 host->flags &= ~SDHCI_REQ_USE_DMA;
823 sdhci_writel(host, host->adma_addr,
829 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
830 data->sg, data->sg_len,
831 (data->flags & MMC_DATA_READ) ?
836 * This only happens when someone fed
837 * us an invalid request.
840 host->flags &= ~SDHCI_REQ_USE_DMA;
842 WARN_ON(sg_cnt != 1);
843 sdhci_writel(host, sg_dma_address(data->sg),
850 * Always adjust the DMA selection as some controllers
851 * (e.g. JMicron) can't do PIO properly when the selection
854 if (host->version >= SDHCI_SPEC_200) {
855 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
856 ctrl &= ~SDHCI_CTRL_DMA_MASK;
857 if ((host->flags & SDHCI_REQ_USE_DMA) &&
858 (host->flags & SDHCI_USE_ADMA))
859 ctrl |= SDHCI_CTRL_ADMA32;
861 ctrl |= SDHCI_CTRL_SDMA;
862 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
865 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
868 flags = SG_MITER_ATOMIC;
869 if (host->data->flags & MMC_DATA_READ)
870 flags |= SG_MITER_TO_SG;
872 flags |= SG_MITER_FROM_SG;
873 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
874 host->blocks = data->blocks;
877 sdhci_set_transfer_irqs(host);
879 /* Set the DMA boundary value and block size */
880 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
881 data->blksz), SDHCI_BLOCK_SIZE);
882 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
885 static void sdhci_set_transfer_mode(struct sdhci_host *host,
886 struct mmc_command *cmd)
889 struct mmc_data *data = cmd->data;
892 /* clear Auto CMD settings for no data CMDs */
893 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
894 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
895 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
899 WARN_ON(!host->data);
901 mode = SDHCI_TRNS_BLK_CNT_EN;
902 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
903 mode |= SDHCI_TRNS_MULTI;
905 * If we are sending CMD23, CMD12 never gets sent
906 * on successful completion (so no Auto-CMD12).
908 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
909 mode |= SDHCI_TRNS_AUTO_CMD12;
910 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
911 mode |= SDHCI_TRNS_AUTO_CMD23;
912 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
916 if (data->flags & MMC_DATA_READ)
917 mode |= SDHCI_TRNS_READ;
918 if (host->flags & SDHCI_REQ_USE_DMA)
919 mode |= SDHCI_TRNS_DMA;
921 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
924 static void sdhci_finish_data(struct sdhci_host *host)
926 struct mmc_data *data;
933 if (host->flags & SDHCI_REQ_USE_DMA) {
934 if (host->flags & SDHCI_USE_ADMA)
935 sdhci_adma_table_post(host, data);
937 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
938 data->sg_len, (data->flags & MMC_DATA_READ) ?
939 DMA_FROM_DEVICE : DMA_TO_DEVICE);
944 * The specification states that the block count register must
945 * be updated, but it does not specify at what point in the
946 * data flow. That makes the register entirely useless to read
947 * back so we have to assume that nothing made it to the card
948 * in the event of an error.
951 data->bytes_xfered = 0;
953 data->bytes_xfered = data->blksz * data->blocks;
956 * Need to send CMD12 if -
957 * a) open-ended multiblock transfer (no CMD23)
958 * b) error in multiblock transfer
965 * The controller needs a reset of internal state machines
966 * upon error conditions.
969 sdhci_do_reset(host, SDHCI_RESET_CMD);
970 sdhci_do_reset(host, SDHCI_RESET_DATA);
973 sdhci_send_command(host, data->stop);
975 tasklet_schedule(&host->finish_tasklet);
978 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
982 unsigned long timeout;
989 mask = SDHCI_CMD_INHIBIT;
990 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
991 mask |= SDHCI_DATA_INHIBIT;
993 /* We shouldn't wait for data inihibit for stop commands, even
994 though they might use busy signaling */
995 if (host->mrq->data && (cmd == host->mrq->data->stop))
996 mask &= ~SDHCI_DATA_INHIBIT;
998 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1000 pr_err("%s: Controller never released "
1001 "inhibit bit(s).\n", mmc_hostname(host->mmc));
1002 sdhci_dumpregs(host);
1004 tasklet_schedule(&host->finish_tasklet);
1012 if (!cmd->data && cmd->busy_timeout > 9000)
1013 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1016 mod_timer(&host->timer, timeout);
1020 sdhci_prepare_data(host, cmd);
1022 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1024 sdhci_set_transfer_mode(host, cmd);
1026 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1027 pr_err("%s: Unsupported response type!\n",
1028 mmc_hostname(host->mmc));
1029 cmd->error = -EINVAL;
1030 tasklet_schedule(&host->finish_tasklet);
1034 if (!(cmd->flags & MMC_RSP_PRESENT))
1035 flags = SDHCI_CMD_RESP_NONE;
1036 else if (cmd->flags & MMC_RSP_136)
1037 flags = SDHCI_CMD_RESP_LONG;
1038 else if (cmd->flags & MMC_RSP_BUSY)
1039 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1041 flags = SDHCI_CMD_RESP_SHORT;
1043 if (cmd->flags & MMC_RSP_CRC)
1044 flags |= SDHCI_CMD_CRC;
1045 if (cmd->flags & MMC_RSP_OPCODE)
1046 flags |= SDHCI_CMD_INDEX;
1048 /* CMD19 is special in that the Data Present Select should be set */
1049 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1050 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1051 flags |= SDHCI_CMD_DATA;
1053 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1055 EXPORT_SYMBOL_GPL(sdhci_send_command);
1057 static void sdhci_finish_command(struct sdhci_host *host)
1061 BUG_ON(host->cmd == NULL);
1063 if (host->cmd->flags & MMC_RSP_PRESENT) {
1064 if (host->cmd->flags & MMC_RSP_136) {
1065 /* CRC is stripped so we need to do some shifting. */
1066 for (i = 0;i < 4;i++) {
1067 host->cmd->resp[i] = sdhci_readl(host,
1068 SDHCI_RESPONSE + (3-i)*4) << 8;
1070 host->cmd->resp[i] |=
1072 SDHCI_RESPONSE + (3-i)*4-1);
1075 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1079 host->cmd->error = 0;
1081 /* Finished CMD23, now send actual command. */
1082 if (host->cmd == host->mrq->sbc) {
1084 sdhci_send_command(host, host->mrq->cmd);
1087 /* Processed actual command. */
1088 if (host->data && host->data_early)
1089 sdhci_finish_data(host);
1091 if (!host->cmd->data)
1092 tasklet_schedule(&host->finish_tasklet);
1098 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1102 switch (host->timing) {
1103 case MMC_TIMING_UHS_SDR12:
1104 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1106 case MMC_TIMING_UHS_SDR25:
1107 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1109 case MMC_TIMING_UHS_SDR50:
1110 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1112 case MMC_TIMING_UHS_SDR104:
1113 case MMC_TIMING_MMC_HS200:
1114 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1116 case MMC_TIMING_UHS_DDR50:
1117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1120 pr_warn("%s: Invalid UHS-I mode selected\n",
1121 mmc_hostname(host->mmc));
1122 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1128 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1130 int div = 0; /* Initialized for compiler warning */
1131 int real_div = div, clk_mul = 1;
1133 unsigned long timeout;
1135 host->mmc->actual_clock = 0;
1137 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1142 if (host->version >= SDHCI_SPEC_300) {
1143 if (host->preset_enabled) {
1146 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1147 pre_val = sdhci_get_preset_value(host);
1148 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1149 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1150 if (host->clk_mul &&
1151 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1152 clk = SDHCI_PROG_CLOCK_MODE;
1154 clk_mul = host->clk_mul;
1156 real_div = max_t(int, 1, div << 1);
1162 * Check if the Host Controller supports Programmable Clock
1165 if (host->clk_mul) {
1166 for (div = 1; div <= 1024; div++) {
1167 if ((host->max_clk * host->clk_mul / div)
1172 * Set Programmable Clock Mode in the Clock
1175 clk = SDHCI_PROG_CLOCK_MODE;
1177 clk_mul = host->clk_mul;
1180 /* Version 3.00 divisors must be a multiple of 2. */
1181 if (host->max_clk <= clock)
1184 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1186 if ((host->max_clk / div) <= clock)
1194 /* Version 2.00 divisors must be a power of 2. */
1195 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1196 if ((host->max_clk / div) <= clock)
1205 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1206 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1207 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1208 << SDHCI_DIVIDER_HI_SHIFT;
1209 clk |= SDHCI_CLOCK_INT_EN;
1210 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1212 /* Wait max 20 ms */
1214 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1215 & SDHCI_CLOCK_INT_STABLE)) {
1217 pr_err("%s: Internal clock never "
1218 "stabilised.\n", mmc_hostname(host->mmc));
1219 sdhci_dumpregs(host);
1226 clk |= SDHCI_CLOCK_CARD_EN;
1227 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1229 EXPORT_SYMBOL_GPL(sdhci_set_clock);
1231 static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1234 struct mmc_host *mmc = host->mmc;
1237 if (!IS_ERR(mmc->supply.vmmc)) {
1238 spin_unlock_irq(&host->lock);
1239 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1240 spin_lock_irq(&host->lock);
1244 if (mode != MMC_POWER_OFF) {
1246 case MMC_VDD_165_195:
1247 pwr = SDHCI_POWER_180;
1251 pwr = SDHCI_POWER_300;
1255 pwr = SDHCI_POWER_330;
1262 if (host->pwr == pwr)
1268 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1269 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1270 sdhci_runtime_pm_bus_off(host);
1274 * Spec says that we should clear the power reg before setting
1275 * a new value. Some controllers don't seem to like this though.
1277 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1278 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1281 * At least the Marvell CaFe chip gets confused if we set the
1282 * voltage and set turn on power at the same time, so set the
1285 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1286 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1288 pwr |= SDHCI_POWER_ON;
1290 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1292 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1293 sdhci_runtime_pm_bus_on(host);
1296 * Some controllers need an extra 10ms delay of 10ms before
1297 * they can apply clock after applying power
1299 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1304 /*****************************************************************************\
1308 \*****************************************************************************/
1310 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1312 struct sdhci_host *host;
1314 unsigned long flags;
1317 host = mmc_priv(mmc);
1319 sdhci_runtime_pm_get(host);
1321 spin_lock_irqsave(&host->lock, flags);
1323 WARN_ON(host->mrq != NULL);
1325 #ifndef SDHCI_USE_LEDS_CLASS
1326 sdhci_activate_led(host);
1330 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1331 * requests if Auto-CMD12 is enabled.
1333 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1335 mrq->data->stop = NULL;
1343 * Firstly check card presence from cd-gpio. The return could
1344 * be one of the following possibilities:
1345 * negative: cd-gpio is not available
1346 * zero: cd-gpio is used, and card is removed
1347 * one: cd-gpio is used, and card is present
1349 present = mmc_gpio_get_cd(host->mmc);
1351 /* If polling, assume that the card is always present. */
1352 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1355 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1359 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1360 host->mrq->cmd->error = -ENOMEDIUM;
1361 tasklet_schedule(&host->finish_tasklet);
1365 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1367 * Check if the re-tuning timer has already expired and there
1368 * is no on-going data transfer. If so, we need to execute
1369 * tuning procedure before sending command.
1371 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1372 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1374 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1376 mmc->card->type == MMC_TYPE_MMC ?
1377 MMC_SEND_TUNING_BLOCK_HS200 :
1378 MMC_SEND_TUNING_BLOCK;
1380 /* Here we need to set the host->mrq to NULL,
1381 * in case the pending finish_tasklet
1382 * finishes it incorrectly.
1386 spin_unlock_irqrestore(&host->lock, flags);
1387 sdhci_execute_tuning(mmc, tuning_opcode);
1388 spin_lock_irqsave(&host->lock, flags);
1390 /* Restore original mmc_request structure */
1395 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1396 sdhci_send_command(host, mrq->sbc);
1398 sdhci_send_command(host, mrq->cmd);
1402 spin_unlock_irqrestore(&host->lock, flags);
1405 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1409 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1410 if (width == MMC_BUS_WIDTH_8) {
1411 ctrl &= ~SDHCI_CTRL_4BITBUS;
1412 if (host->version >= SDHCI_SPEC_300)
1413 ctrl |= SDHCI_CTRL_8BITBUS;
1415 if (host->version >= SDHCI_SPEC_300)
1416 ctrl &= ~SDHCI_CTRL_8BITBUS;
1417 if (width == MMC_BUS_WIDTH_4)
1418 ctrl |= SDHCI_CTRL_4BITBUS;
1420 ctrl &= ~SDHCI_CTRL_4BITBUS;
1422 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1424 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1426 void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1430 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1431 /* Select Bus Speed Mode for host */
1432 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1433 if ((timing == MMC_TIMING_MMC_HS200) ||
1434 (timing == MMC_TIMING_UHS_SDR104))
1435 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1436 else if (timing == MMC_TIMING_UHS_SDR12)
1437 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1438 else if (timing == MMC_TIMING_UHS_SDR25)
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1440 else if (timing == MMC_TIMING_UHS_SDR50)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1442 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1443 (timing == MMC_TIMING_MMC_DDR52))
1444 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1445 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1447 EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1449 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1451 unsigned long flags;
1453 struct mmc_host *mmc = host->mmc;
1455 spin_lock_irqsave(&host->lock, flags);
1457 if (host->flags & SDHCI_DEVICE_DEAD) {
1458 spin_unlock_irqrestore(&host->lock, flags);
1459 if (!IS_ERR(mmc->supply.vmmc) &&
1460 ios->power_mode == MMC_POWER_OFF)
1461 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1466 * Reset the chip on each power off.
1467 * Should clear out any weird states.
1469 if (ios->power_mode == MMC_POWER_OFF) {
1470 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1474 if (host->version >= SDHCI_SPEC_300 &&
1475 (ios->power_mode == MMC_POWER_UP) &&
1476 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1477 sdhci_enable_preset_value(host, false);
1479 if (!ios->clock || ios->clock != host->clock) {
1480 host->ops->set_clock(host, ios->clock);
1481 host->clock = ios->clock;
1483 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1485 host->timeout_clk = host->mmc->actual_clock ?
1486 host->mmc->actual_clock / 1000 :
1488 host->mmc->max_busy_timeout =
1489 host->ops->get_max_timeout_count ?
1490 host->ops->get_max_timeout_count(host) :
1492 host->mmc->max_busy_timeout /= host->timeout_clk;
1496 sdhci_set_power(host, ios->power_mode, ios->vdd);
1498 if (host->ops->platform_send_init_74_clocks)
1499 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1501 host->ops->set_bus_width(host, ios->bus_width);
1503 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1505 if ((ios->timing == MMC_TIMING_SD_HS ||
1506 ios->timing == MMC_TIMING_MMC_HS)
1507 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1508 ctrl |= SDHCI_CTRL_HISPD;
1510 ctrl &= ~SDHCI_CTRL_HISPD;
1512 if (host->version >= SDHCI_SPEC_300) {
1515 /* In case of UHS-I modes, set High Speed Enable */
1516 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1517 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1518 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1519 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1520 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1521 (ios->timing == MMC_TIMING_UHS_SDR25))
1522 ctrl |= SDHCI_CTRL_HISPD;
1524 if (!host->preset_enabled) {
1525 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1527 * We only need to set Driver Strength if the
1528 * preset value enable is not set.
1530 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1531 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1532 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1533 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1534 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1535 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1537 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1540 * According to SDHC Spec v3.00, if the Preset Value
1541 * Enable in the Host Control 2 register is set, we
1542 * need to reset SD Clock Enable before changing High
1543 * Speed Enable to avoid generating clock gliches.
1546 /* Reset SD Clock Enable */
1547 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1548 clk &= ~SDHCI_CLOCK_CARD_EN;
1549 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1551 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1553 /* Re-enable SD Clock */
1554 host->ops->set_clock(host, host->clock);
1557 /* Reset SD Clock Enable */
1558 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1559 clk &= ~SDHCI_CLOCK_CARD_EN;
1560 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1562 host->ops->set_uhs_signaling(host, ios->timing);
1563 host->timing = ios->timing;
1565 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1566 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1567 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1568 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1569 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1570 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1573 sdhci_enable_preset_value(host, true);
1574 preset = sdhci_get_preset_value(host);
1575 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1576 >> SDHCI_PRESET_DRV_SHIFT;
1579 /* Re-enable SD Clock */
1580 host->ops->set_clock(host, host->clock);
1582 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1585 * Some (ENE) controllers go apeshit on some ios operation,
1586 * signalling timeout and CRC errors even on CMD0. Resetting
1587 * it on each ios seems to solve the problem.
1589 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1590 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1593 spin_unlock_irqrestore(&host->lock, flags);
1596 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1598 struct sdhci_host *host = mmc_priv(mmc);
1600 sdhci_runtime_pm_get(host);
1601 sdhci_do_set_ios(host, ios);
1602 sdhci_runtime_pm_put(host);
1605 static int sdhci_do_get_cd(struct sdhci_host *host)
1607 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1609 if (host->flags & SDHCI_DEVICE_DEAD)
1612 /* If polling/nonremovable, assume that the card is always present. */
1613 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1614 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1617 /* Try slot gpio detect */
1618 if (!IS_ERR_VALUE(gpio_cd))
1621 /* Host native card detect */
1622 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1625 static int sdhci_get_cd(struct mmc_host *mmc)
1627 struct sdhci_host *host = mmc_priv(mmc);
1630 sdhci_runtime_pm_get(host);
1631 ret = sdhci_do_get_cd(host);
1632 sdhci_runtime_pm_put(host);
1636 static int sdhci_check_ro(struct sdhci_host *host)
1638 unsigned long flags;
1641 spin_lock_irqsave(&host->lock, flags);
1643 if (host->flags & SDHCI_DEVICE_DEAD)
1645 else if (host->ops->get_ro)
1646 is_readonly = host->ops->get_ro(host);
1648 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1649 & SDHCI_WRITE_PROTECT);
1651 spin_unlock_irqrestore(&host->lock, flags);
1653 /* This quirk needs to be replaced by a callback-function later */
1654 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1655 !is_readonly : is_readonly;
1658 #define SAMPLE_COUNT 5
1660 static int sdhci_do_get_ro(struct sdhci_host *host)
1664 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1665 return sdhci_check_ro(host);
1668 for (i = 0; i < SAMPLE_COUNT; i++) {
1669 if (sdhci_check_ro(host)) {
1670 if (++ro_count > SAMPLE_COUNT / 2)
1678 static void sdhci_hw_reset(struct mmc_host *mmc)
1680 struct sdhci_host *host = mmc_priv(mmc);
1682 if (host->ops && host->ops->hw_reset)
1683 host->ops->hw_reset(host);
1686 static int sdhci_get_ro(struct mmc_host *mmc)
1688 struct sdhci_host *host = mmc_priv(mmc);
1691 sdhci_runtime_pm_get(host);
1692 ret = sdhci_do_get_ro(host);
1693 sdhci_runtime_pm_put(host);
1697 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1699 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1701 host->ier |= SDHCI_INT_CARD_INT;
1703 host->ier &= ~SDHCI_INT_CARD_INT;
1705 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1706 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1711 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1713 struct sdhci_host *host = mmc_priv(mmc);
1714 unsigned long flags;
1716 sdhci_runtime_pm_get(host);
1718 spin_lock_irqsave(&host->lock, flags);
1720 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1722 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1724 sdhci_enable_sdio_irq_nolock(host, enable);
1725 spin_unlock_irqrestore(&host->lock, flags);
1727 sdhci_runtime_pm_put(host);
1730 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1731 struct mmc_ios *ios)
1733 struct mmc_host *mmc = host->mmc;
1738 * Signal Voltage Switching is only applicable for Host Controllers
1741 if (host->version < SDHCI_SPEC_300)
1744 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1746 switch (ios->signal_voltage) {
1747 case MMC_SIGNAL_VOLTAGE_330:
1748 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1749 ctrl &= ~SDHCI_CTRL_VDD_180;
1750 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1752 if (!IS_ERR(mmc->supply.vqmmc)) {
1753 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1756 pr_warning("%s: Switching to 3.3V signalling voltage "
1757 " failed\n", mmc_hostname(mmc));
1762 usleep_range(5000, 5500);
1764 /* 3.3V regulator output should be stable within 5 ms */
1765 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1766 if (!(ctrl & SDHCI_CTRL_VDD_180))
1769 pr_warning("%s: 3.3V regulator output did not became stable\n",
1773 case MMC_SIGNAL_VOLTAGE_180:
1774 if (!IS_ERR(mmc->supply.vqmmc)) {
1775 ret = regulator_set_voltage(mmc->supply.vqmmc,
1778 pr_warning("%s: Switching to 1.8V signalling voltage "
1779 " failed\n", mmc_hostname(mmc));
1785 * Enable 1.8V Signal Enable in the Host Control2
1788 ctrl |= SDHCI_CTRL_VDD_180;
1789 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1791 /* 1.8V regulator output should be stable within 5 ms */
1792 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1793 if (ctrl & SDHCI_CTRL_VDD_180)
1796 pr_warning("%s: 1.8V regulator output did not became stable\n",
1800 case MMC_SIGNAL_VOLTAGE_120:
1801 if (!IS_ERR(mmc->supply.vqmmc)) {
1802 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1805 pr_warning("%s: Switching to 1.2V signalling voltage "
1806 " failed\n", mmc_hostname(mmc));
1812 /* No signal voltage switch required */
1817 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1818 struct mmc_ios *ios)
1820 struct sdhci_host *host = mmc_priv(mmc);
1823 if (host->version < SDHCI_SPEC_300)
1825 sdhci_runtime_pm_get(host);
1826 err = sdhci_do_start_signal_voltage_switch(host, ios);
1827 sdhci_runtime_pm_put(host);
1831 static int sdhci_card_busy(struct mmc_host *mmc)
1833 struct sdhci_host *host = mmc_priv(mmc);
1836 sdhci_runtime_pm_get(host);
1837 /* Check whether DAT[3:0] is 0000 */
1838 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1839 sdhci_runtime_pm_put(host);
1841 return !(present_state & SDHCI_DATA_LVL_MASK);
1844 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1846 struct sdhci_host *host = mmc_priv(mmc);
1848 int tuning_loop_counter = MAX_TUNING_LOOP;
1850 unsigned long flags;
1852 sdhci_runtime_pm_get(host);
1853 spin_lock_irqsave(&host->lock, flags);
1856 * The Host Controller needs tuning only in case of SDR104 mode
1857 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1858 * Capabilities register.
1859 * If the Host Controller supports the HS200 mode then the
1860 * tuning function has to be executed.
1862 switch (host->timing) {
1863 case MMC_TIMING_MMC_HS200:
1864 case MMC_TIMING_UHS_SDR104:
1867 case MMC_TIMING_UHS_SDR50:
1868 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1869 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1874 spin_unlock_irqrestore(&host->lock, flags);
1875 sdhci_runtime_pm_put(host);
1879 if (host->ops->platform_execute_tuning) {
1880 spin_unlock_irqrestore(&host->lock, flags);
1881 err = host->ops->platform_execute_tuning(host, opcode);
1882 sdhci_runtime_pm_put(host);
1886 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1887 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1888 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1891 * As per the Host Controller spec v3.00, tuning command
1892 * generates Buffer Read Ready interrupt, so enable that.
1894 * Note: The spec clearly says that when tuning sequence
1895 * is being performed, the controller does not generate
1896 * interrupts other than Buffer Read Ready interrupt. But
1897 * to make sure we don't hit a controller bug, we _only_
1898 * enable Buffer Read Ready interrupt here.
1900 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1901 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1904 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1905 * of loops reaches 40 times or a timeout of 150ms occurs.
1908 struct mmc_command cmd = {0};
1909 struct mmc_request mrq = {NULL};
1911 cmd.opcode = opcode;
1913 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1918 if (tuning_loop_counter-- == 0)
1925 * In response to CMD19, the card sends 64 bytes of tuning
1926 * block to the Host Controller. So we set the block size
1929 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1930 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1931 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1933 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1934 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1937 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1942 * The tuning block is sent by the card to the host controller.
1943 * So we set the TRNS_READ bit in the Transfer Mode register.
1944 * This also takes care of setting DMA Enable and Multi Block
1945 * Select in the same register to 0.
1947 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1949 sdhci_send_command(host, &cmd);
1954 spin_unlock_irqrestore(&host->lock, flags);
1955 /* Wait for Buffer Read Ready interrupt */
1956 wait_event_interruptible_timeout(host->buf_ready_int,
1957 (host->tuning_done == 1),
1958 msecs_to_jiffies(50));
1959 spin_lock_irqsave(&host->lock, flags);
1961 if (!host->tuning_done) {
1962 pr_info(DRIVER_NAME ": Timeout waiting for "
1963 "Buffer Read Ready interrupt during tuning "
1964 "procedure, falling back to fixed sampling "
1966 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1967 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1968 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1969 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1975 host->tuning_done = 0;
1977 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1979 /* eMMC spec does not require a delay between tuning cycles */
1980 if (opcode == MMC_SEND_TUNING_BLOCK)
1982 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1985 * The Host Driver has exhausted the maximum number of loops allowed,
1986 * so use fixed sampling frequency.
1988 if (tuning_loop_counter < 0) {
1989 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1990 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1992 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1993 pr_info(DRIVER_NAME ": Tuning procedure"
1994 " failed, falling back to fixed sampling"
2001 * If this is the very first time we are here, we start the retuning
2002 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2003 * flag won't be set, we check this condition before actually starting
2006 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2007 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2008 host->flags |= SDHCI_USING_RETUNING_TIMER;
2009 mod_timer(&host->tuning_timer, jiffies +
2010 host->tuning_count * HZ);
2011 /* Tuning mode 1 limits the maximum data length to 4MB */
2012 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2013 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2014 host->flags &= ~SDHCI_NEEDS_RETUNING;
2015 /* Reload the new initial value for timer */
2016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
2021 * In case tuning fails, host controllers which support re-tuning can
2022 * try tuning again at a later time, when the re-tuning timer expires.
2023 * So for these controllers, we return 0. Since there might be other
2024 * controllers who do not have this capability, we return error for
2025 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2026 * a retuning timer to do the retuning for the card.
2028 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2031 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2032 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2033 spin_unlock_irqrestore(&host->lock, flags);
2034 sdhci_runtime_pm_put(host);
2040 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2042 /* Host Controller v3.00 defines preset value registers */
2043 if (host->version < SDHCI_SPEC_300)
2047 * We only enable or disable Preset Value if they are not already
2048 * enabled or disabled respectively. Otherwise, we bail out.
2050 if (host->preset_enabled != enable) {
2051 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2054 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2056 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2058 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2061 host->flags |= SDHCI_PV_ENABLED;
2063 host->flags &= ~SDHCI_PV_ENABLED;
2065 host->preset_enabled = enable;
2069 static void sdhci_card_event(struct mmc_host *mmc)
2071 struct sdhci_host *host = mmc_priv(mmc);
2072 unsigned long flags;
2074 /* First check if client has provided their own card event */
2075 if (host->ops->card_event)
2076 host->ops->card_event(host);
2078 spin_lock_irqsave(&host->lock, flags);
2080 /* Check host->mrq first in case we are runtime suspended */
2081 if (host->mrq && !sdhci_do_get_cd(host)) {
2082 pr_err("%s: Card removed during transfer!\n",
2083 mmc_hostname(host->mmc));
2084 pr_err("%s: Resetting controller.\n",
2085 mmc_hostname(host->mmc));
2087 sdhci_do_reset(host, SDHCI_RESET_CMD);
2088 sdhci_do_reset(host, SDHCI_RESET_DATA);
2090 host->mrq->cmd->error = -ENOMEDIUM;
2091 tasklet_schedule(&host->finish_tasklet);
2094 spin_unlock_irqrestore(&host->lock, flags);
2097 static const struct mmc_host_ops sdhci_ops = {
2098 .request = sdhci_request,
2099 .set_ios = sdhci_set_ios,
2100 .get_cd = sdhci_get_cd,
2101 .get_ro = sdhci_get_ro,
2102 .hw_reset = sdhci_hw_reset,
2103 .enable_sdio_irq = sdhci_enable_sdio_irq,
2104 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2105 .execute_tuning = sdhci_execute_tuning,
2106 .card_event = sdhci_card_event,
2107 .card_busy = sdhci_card_busy,
2110 /*****************************************************************************\
2114 \*****************************************************************************/
2116 static void sdhci_tasklet_finish(unsigned long param)
2118 struct sdhci_host *host;
2119 unsigned long flags;
2120 struct mmc_request *mrq;
2122 host = (struct sdhci_host*)param;
2124 spin_lock_irqsave(&host->lock, flags);
2127 * If this tasklet gets rescheduled while running, it will
2128 * be run again afterwards but without any active request.
2131 spin_unlock_irqrestore(&host->lock, flags);
2135 del_timer(&host->timer);
2140 * The controller needs a reset of internal state machines
2141 * upon error conditions.
2143 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2144 ((mrq->cmd && mrq->cmd->error) ||
2145 (mrq->data && (mrq->data->error ||
2146 (mrq->data->stop && mrq->data->stop->error))) ||
2147 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2149 /* Some controllers need this kick or reset won't work here */
2150 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2151 /* This is to force an update */
2152 host->ops->set_clock(host, host->clock);
2154 /* Spec says we should do both at the same time, but Ricoh
2155 controllers do not like that. */
2156 sdhci_do_reset(host, SDHCI_RESET_CMD);
2157 sdhci_do_reset(host, SDHCI_RESET_DATA);
2164 #ifndef SDHCI_USE_LEDS_CLASS
2165 sdhci_deactivate_led(host);
2169 spin_unlock_irqrestore(&host->lock, flags);
2171 mmc_request_done(host->mmc, mrq);
2172 sdhci_runtime_pm_put(host);
2175 static void sdhci_timeout_timer(unsigned long data)
2177 struct sdhci_host *host;
2178 unsigned long flags;
2180 host = (struct sdhci_host*)data;
2182 spin_lock_irqsave(&host->lock, flags);
2185 pr_err("%s: Timeout waiting for hardware "
2186 "interrupt.\n", mmc_hostname(host->mmc));
2187 sdhci_dumpregs(host);
2190 host->data->error = -ETIMEDOUT;
2191 sdhci_finish_data(host);
2194 host->cmd->error = -ETIMEDOUT;
2196 host->mrq->cmd->error = -ETIMEDOUT;
2198 tasklet_schedule(&host->finish_tasklet);
2203 spin_unlock_irqrestore(&host->lock, flags);
2206 static void sdhci_tuning_timer(unsigned long data)
2208 struct sdhci_host *host;
2209 unsigned long flags;
2211 host = (struct sdhci_host *)data;
2213 spin_lock_irqsave(&host->lock, flags);
2215 host->flags |= SDHCI_NEEDS_RETUNING;
2217 spin_unlock_irqrestore(&host->lock, flags);
2220 /*****************************************************************************\
2222 * Interrupt handling *
2224 \*****************************************************************************/
2226 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2228 BUG_ON(intmask == 0);
2231 pr_err("%s: Got command interrupt 0x%08x even "
2232 "though no command operation was in progress.\n",
2233 mmc_hostname(host->mmc), (unsigned)intmask);
2234 sdhci_dumpregs(host);
2238 if (intmask & SDHCI_INT_TIMEOUT)
2239 host->cmd->error = -ETIMEDOUT;
2240 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2242 host->cmd->error = -EILSEQ;
2244 if (host->cmd->error) {
2245 tasklet_schedule(&host->finish_tasklet);
2250 * The host can send and interrupt when the busy state has
2251 * ended, allowing us to wait without wasting CPU cycles.
2252 * Unfortunately this is overloaded on the "data complete"
2253 * interrupt, so we need to take some care when handling
2256 * Note: The 1.0 specification is a bit ambiguous about this
2257 * feature so there might be some problems with older
2260 if (host->cmd->flags & MMC_RSP_BUSY) {
2261 if (host->cmd->data)
2262 DBG("Cannot wait for busy signal when also "
2263 "doing a data transfer");
2264 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
2267 /* The controller does not support the end-of-busy IRQ,
2268 * fall through and take the SDHCI_INT_RESPONSE */
2271 if (intmask & SDHCI_INT_RESPONSE)
2272 sdhci_finish_command(host);
2275 #ifdef CONFIG_MMC_DEBUG
2276 static void sdhci_show_adma_error(struct sdhci_host *host)
2278 const char *name = mmc_hostname(host->mmc);
2279 u8 *desc = host->adma_desc;
2284 sdhci_dumpregs(host);
2287 dma = (__le32 *)(desc + 4);
2288 len = (__le16 *)(desc + 2);
2291 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2292 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2301 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2304 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2307 BUG_ON(intmask == 0);
2309 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2310 if (intmask & SDHCI_INT_DATA_AVAIL) {
2311 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2312 if (command == MMC_SEND_TUNING_BLOCK ||
2313 command == MMC_SEND_TUNING_BLOCK_HS200) {
2314 host->tuning_done = 1;
2315 wake_up(&host->buf_ready_int);
2322 * The "data complete" interrupt is also used to
2323 * indicate that a busy state has ended. See comment
2324 * above in sdhci_cmd_irq().
2326 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2327 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2328 host->cmd->error = -ETIMEDOUT;
2329 tasklet_schedule(&host->finish_tasklet);
2332 if (intmask & SDHCI_INT_DATA_END) {
2333 sdhci_finish_command(host);
2338 pr_err("%s: Got data interrupt 0x%08x even "
2339 "though no data operation was in progress.\n",
2340 mmc_hostname(host->mmc), (unsigned)intmask);
2341 sdhci_dumpregs(host);
2346 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2347 host->data->error = -ETIMEDOUT;
2348 else if (intmask & SDHCI_INT_DATA_END_BIT)
2349 host->data->error = -EILSEQ;
2350 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2351 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2353 host->data->error = -EILSEQ;
2354 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2355 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2356 sdhci_show_adma_error(host);
2357 host->data->error = -EIO;
2358 if (host->ops->adma_workaround)
2359 host->ops->adma_workaround(host, intmask);
2362 if (host->data->error)
2363 sdhci_finish_data(host);
2365 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2366 sdhci_transfer_pio(host);
2369 * We currently don't do anything fancy with DMA
2370 * boundaries, but as we can't disable the feature
2371 * we need to at least restart the transfer.
2373 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2374 * should return a valid address to continue from, but as
2375 * some controllers are faulty, don't trust them.
2377 if (intmask & SDHCI_INT_DMA_END) {
2378 u32 dmastart, dmanow;
2379 dmastart = sg_dma_address(host->data->sg);
2380 dmanow = dmastart + host->data->bytes_xfered;
2382 * Force update to the next DMA block boundary.
2385 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2386 SDHCI_DEFAULT_BOUNDARY_SIZE;
2387 host->data->bytes_xfered = dmanow - dmastart;
2388 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2390 mmc_hostname(host->mmc), dmastart,
2391 host->data->bytes_xfered, dmanow);
2392 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2395 if (intmask & SDHCI_INT_DATA_END) {
2398 * Data managed to finish before the
2399 * command completed. Make sure we do
2400 * things in the proper order.
2402 host->data_early = 1;
2404 sdhci_finish_data(host);
2410 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2412 irqreturn_t result = IRQ_NONE;
2413 struct sdhci_host *host = dev_id;
2414 u32 intmask, mask, unexpected = 0;
2417 spin_lock(&host->lock);
2419 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2420 spin_unlock(&host->lock);
2424 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2425 if (!intmask || intmask == 0xffffffff) {
2431 /* Clear selected interrupts. */
2432 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2433 SDHCI_INT_BUS_POWER);
2434 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2436 DBG("*** %s got interrupt: 0x%08x\n",
2437 mmc_hostname(host->mmc), intmask);
2439 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2440 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2444 * There is a observation on i.mx esdhc. INSERT
2445 * bit will be immediately set again when it gets
2446 * cleared, if a card is inserted. We have to mask
2447 * the irq to prevent interrupt storm which will
2448 * freeze the system. And the REMOVE gets the
2451 * More testing are needed here to ensure it works
2452 * for other platforms though.
2454 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2455 SDHCI_INT_CARD_REMOVE);
2456 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2457 SDHCI_INT_CARD_INSERT;
2458 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2459 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2461 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2462 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2464 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2465 SDHCI_INT_CARD_REMOVE);
2466 result = IRQ_WAKE_THREAD;
2469 if (intmask & SDHCI_INT_CMD_MASK)
2470 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2472 if (intmask & SDHCI_INT_DATA_MASK)
2473 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2475 if (intmask & SDHCI_INT_BUS_POWER)
2476 pr_err("%s: Card is consuming too much power!\n",
2477 mmc_hostname(host->mmc));
2479 if (intmask & SDHCI_INT_CARD_INT) {
2480 sdhci_enable_sdio_irq_nolock(host, false);
2481 host->thread_isr |= SDHCI_INT_CARD_INT;
2482 result = IRQ_WAKE_THREAD;
2485 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2486 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2487 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2488 SDHCI_INT_CARD_INT);
2491 unexpected |= intmask;
2492 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2495 if (result == IRQ_NONE)
2496 result = IRQ_HANDLED;
2498 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2499 } while (intmask && --max_loops);
2501 spin_unlock(&host->lock);
2504 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2505 mmc_hostname(host->mmc), unexpected);
2506 sdhci_dumpregs(host);
2512 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2514 struct sdhci_host *host = dev_id;
2515 unsigned long flags;
2518 spin_lock_irqsave(&host->lock, flags);
2519 isr = host->thread_isr;
2520 host->thread_isr = 0;
2521 spin_unlock_irqrestore(&host->lock, flags);
2523 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2524 sdhci_card_event(host->mmc);
2525 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2528 if (isr & SDHCI_INT_CARD_INT) {
2529 sdio_run_irqs(host->mmc);
2531 spin_lock_irqsave(&host->lock, flags);
2532 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2533 sdhci_enable_sdio_irq_nolock(host, true);
2534 spin_unlock_irqrestore(&host->lock, flags);
2537 return isr ? IRQ_HANDLED : IRQ_NONE;
2540 /*****************************************************************************\
2544 \*****************************************************************************/
2547 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2550 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2551 | SDHCI_WAKE_ON_INT;
2553 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2555 /* Avoid fake wake up */
2556 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2557 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2558 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2560 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2562 static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2565 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2566 | SDHCI_WAKE_ON_INT;
2568 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2570 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2573 int sdhci_suspend_host(struct sdhci_host *host)
2575 sdhci_disable_card_detection(host);
2577 /* Disable tuning since we are suspending */
2578 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2579 del_timer_sync(&host->tuning_timer);
2580 host->flags &= ~SDHCI_NEEDS_RETUNING;
2583 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2585 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2586 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2587 free_irq(host->irq, host);
2589 sdhci_enable_irq_wakeups(host);
2590 enable_irq_wake(host->irq);
2595 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2597 int sdhci_resume_host(struct sdhci_host *host)
2601 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2602 if (host->ops->enable_dma)
2603 host->ops->enable_dma(host);
2606 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2607 ret = request_threaded_irq(host->irq, sdhci_irq,
2608 sdhci_thread_irq, IRQF_SHARED,
2609 mmc_hostname(host->mmc), host);
2613 sdhci_disable_irq_wakeups(host);
2614 disable_irq_wake(host->irq);
2617 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2618 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2619 /* Card keeps power but host controller does not */
2620 sdhci_init(host, 0);
2623 sdhci_do_set_ios(host, &host->mmc->ios);
2625 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2629 sdhci_enable_card_detection(host);
2631 /* Set the re-tuning expiration flag */
2632 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2633 host->flags |= SDHCI_NEEDS_RETUNING;
2638 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2639 #endif /* CONFIG_PM */
2641 #ifdef CONFIG_PM_RUNTIME
2643 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2645 return pm_runtime_get_sync(host->mmc->parent);
2648 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2650 pm_runtime_mark_last_busy(host->mmc->parent);
2651 return pm_runtime_put_autosuspend(host->mmc->parent);
2654 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2656 if (host->runtime_suspended || host->bus_on)
2658 host->bus_on = true;
2659 pm_runtime_get_noresume(host->mmc->parent);
2662 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2664 if (host->runtime_suspended || !host->bus_on)
2666 host->bus_on = false;
2667 pm_runtime_put_noidle(host->mmc->parent);
2670 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2672 unsigned long flags;
2674 /* Disable tuning since we are suspending */
2675 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2676 del_timer_sync(&host->tuning_timer);
2677 host->flags &= ~SDHCI_NEEDS_RETUNING;
2680 spin_lock_irqsave(&host->lock, flags);
2681 host->ier &= SDHCI_INT_CARD_INT;
2682 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2683 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2684 spin_unlock_irqrestore(&host->lock, flags);
2686 synchronize_hardirq(host->irq);
2688 spin_lock_irqsave(&host->lock, flags);
2689 host->runtime_suspended = true;
2690 spin_unlock_irqrestore(&host->lock, flags);
2694 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2696 int sdhci_runtime_resume_host(struct sdhci_host *host)
2698 unsigned long flags;
2699 int host_flags = host->flags;
2701 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2702 if (host->ops->enable_dma)
2703 host->ops->enable_dma(host);
2706 sdhci_init(host, 0);
2708 /* Force clock and power re-program */
2711 sdhci_do_set_ios(host, &host->mmc->ios);
2713 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2714 if ((host_flags & SDHCI_PV_ENABLED) &&
2715 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2716 spin_lock_irqsave(&host->lock, flags);
2717 sdhci_enable_preset_value(host, true);
2718 spin_unlock_irqrestore(&host->lock, flags);
2721 /* Set the re-tuning expiration flag */
2722 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2723 host->flags |= SDHCI_NEEDS_RETUNING;
2725 spin_lock_irqsave(&host->lock, flags);
2727 host->runtime_suspended = false;
2729 /* Enable SDIO IRQ */
2730 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2731 sdhci_enable_sdio_irq_nolock(host, true);
2733 /* Enable Card Detection */
2734 sdhci_enable_card_detection(host);
2736 spin_unlock_irqrestore(&host->lock, flags);
2740 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2744 /*****************************************************************************\
2746 * Device allocation/registration *
2748 \*****************************************************************************/
2750 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2753 struct mmc_host *mmc;
2754 struct sdhci_host *host;
2756 WARN_ON(dev == NULL);
2758 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2760 return ERR_PTR(-ENOMEM);
2762 host = mmc_priv(mmc);
2768 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2770 int sdhci_add_host(struct sdhci_host *host)
2772 struct mmc_host *mmc;
2773 u32 caps[2] = {0, 0};
2774 u32 max_current_caps;
2775 unsigned int ocr_avail;
2778 WARN_ON(host == NULL);
2785 host->quirks = debug_quirks;
2787 host->quirks2 = debug_quirks2;
2789 sdhci_do_reset(host, SDHCI_RESET_ALL);
2791 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2792 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2793 >> SDHCI_SPEC_VER_SHIFT;
2794 if (host->version > SDHCI_SPEC_300) {
2795 pr_err("%s: Unknown controller version (%d). "
2796 "You may experience problems.\n", mmc_hostname(mmc),
2800 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2801 sdhci_readl(host, SDHCI_CAPABILITIES);
2803 if (host->version >= SDHCI_SPEC_300)
2804 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2806 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2808 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2809 host->flags |= SDHCI_USE_SDMA;
2810 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2811 DBG("Controller doesn't have SDMA capability\n");
2813 host->flags |= SDHCI_USE_SDMA;
2815 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2816 (host->flags & SDHCI_USE_SDMA)) {
2817 DBG("Disabling DMA as it is marked broken\n");
2818 host->flags &= ~SDHCI_USE_SDMA;
2821 if ((host->version >= SDHCI_SPEC_200) &&
2822 (caps[0] & SDHCI_CAN_DO_ADMA2))
2823 host->flags |= SDHCI_USE_ADMA;
2825 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2826 (host->flags & SDHCI_USE_ADMA)) {
2827 DBG("Disabling ADMA as it is marked broken\n");
2828 host->flags &= ~SDHCI_USE_ADMA;
2831 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2832 if (host->ops->enable_dma) {
2833 if (host->ops->enable_dma(host)) {
2834 pr_warning("%s: No suitable DMA "
2835 "available. Falling back to PIO.\n",
2838 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2843 if (host->flags & SDHCI_USE_ADMA) {
2845 * We need to allocate descriptors for all sg entries
2846 * (128) and potentially one alignment transfer for
2847 * each of those entries.
2849 host->adma_desc = dma_alloc_coherent(mmc_dev(mmc),
2850 ADMA_SIZE, &host->adma_addr,
2852 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2853 if (!host->adma_desc || !host->align_buffer) {
2854 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
2855 host->adma_desc, host->adma_addr);
2856 kfree(host->align_buffer);
2857 pr_warning("%s: Unable to allocate ADMA "
2858 "buffers. Falling back to standard DMA.\n",
2860 host->flags &= ~SDHCI_USE_ADMA;
2861 host->adma_desc = NULL;
2862 host->align_buffer = NULL;
2863 } else if (host->adma_addr & 3) {
2864 pr_warning("%s: unable to allocate aligned ADMA descriptor\n",
2866 host->flags &= ~SDHCI_USE_ADMA;
2867 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
2868 host->adma_desc, host->adma_addr);
2869 kfree(host->align_buffer);
2870 host->adma_desc = NULL;
2871 host->align_buffer = NULL;
2876 * If we use DMA, then it's up to the caller to set the DMA
2877 * mask, but PIO does not need the hw shim so we set a new
2878 * mask here in that case.
2880 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2881 host->dma_mask = DMA_BIT_MASK(64);
2882 mmc_dev(mmc)->dma_mask = &host->dma_mask;
2885 if (host->version >= SDHCI_SPEC_300)
2886 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
2887 >> SDHCI_CLOCK_BASE_SHIFT;
2889 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
2890 >> SDHCI_CLOCK_BASE_SHIFT;
2892 host->max_clk *= 1000000;
2893 if (host->max_clk == 0 || host->quirks &
2894 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
2895 if (!host->ops->get_max_clock) {
2896 pr_err("%s: Hardware doesn't specify base clock "
2897 "frequency.\n", mmc_hostname(mmc));
2900 host->max_clk = host->ops->get_max_clock(host);
2904 * In case of Host Controller v3.00, find out whether clock
2905 * multiplier is supported.
2907 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2908 SDHCI_CLOCK_MUL_SHIFT;
2911 * In case the value in Clock Multiplier is 0, then programmable
2912 * clock mode is not supported, otherwise the actual clock
2913 * multiplier is one more than the value of Clock Multiplier
2914 * in the Capabilities Register.
2920 * Set host parameters.
2922 mmc->ops = &sdhci_ops;
2923 mmc->f_max = host->max_clk;
2924 if (host->ops->get_min_clock)
2925 mmc->f_min = host->ops->get_min_clock(host);
2926 else if (host->version >= SDHCI_SPEC_300) {
2927 if (host->clk_mul) {
2928 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2929 mmc->f_max = host->max_clk * host->clk_mul;
2931 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2933 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
2935 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2936 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2937 SDHCI_TIMEOUT_CLK_SHIFT;
2938 if (host->timeout_clk == 0) {
2939 if (host->ops->get_timeout_clock) {
2941 host->ops->get_timeout_clock(host);
2943 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2949 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2950 host->timeout_clk *= 1000;
2952 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
2953 host->ops->get_max_timeout_count(host) : 1 << 27;
2954 mmc->max_busy_timeout /= host->timeout_clk;
2957 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2958 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2960 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2961 host->flags |= SDHCI_AUTO_CMD12;
2963 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2964 if ((host->version >= SDHCI_SPEC_300) &&
2965 ((host->flags & SDHCI_USE_ADMA) ||
2966 !(host->flags & SDHCI_USE_SDMA))) {
2967 host->flags |= SDHCI_AUTO_CMD23;
2968 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2970 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2974 * A controller may support 8-bit width, but the board itself
2975 * might not have the pins brought out. Boards that support
2976 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2977 * their platform code before calling sdhci_add_host(), and we
2978 * won't assume 8-bit width for hosts without that CAP.
2980 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
2981 mmc->caps |= MMC_CAP_4_BIT_DATA;
2983 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2984 mmc->caps &= ~MMC_CAP_CMD23;
2986 if (caps[0] & SDHCI_CAN_DO_HISPD)
2987 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2989 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2990 !(mmc->caps & MMC_CAP_NONREMOVABLE))
2991 mmc->caps |= MMC_CAP_NEEDS_POLL;
2993 /* If there are external regulators, get them */
2994 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
2995 return -EPROBE_DEFER;
2997 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2998 if (!IS_ERR(mmc->supply.vqmmc)) {
2999 ret = regulator_enable(mmc->supply.vqmmc);
3000 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3002 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3003 SDHCI_SUPPORT_SDR50 |
3004 SDHCI_SUPPORT_DDR50);
3006 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3007 mmc_hostname(mmc), ret);
3008 mmc->supply.vqmmc = NULL;
3012 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3013 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3014 SDHCI_SUPPORT_DDR50);
3016 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3017 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3018 SDHCI_SUPPORT_DDR50))
3019 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3021 /* SDR104 supports also implies SDR50 support */
3022 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3023 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3024 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3025 * field can be promoted to support HS200.
3027 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3028 mmc->caps2 |= MMC_CAP2_HS200;
3029 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3030 mmc->caps |= MMC_CAP_UHS_SDR50;
3032 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3033 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3034 mmc->caps |= MMC_CAP_UHS_DDR50;
3036 /* Does the host need tuning for SDR50? */
3037 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3038 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3040 /* Does the host need tuning for SDR104 / HS200? */
3041 if (mmc->caps2 & MMC_CAP2_HS200)
3042 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3044 /* Driver Type(s) (A, C, D) supported by the host */
3045 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3046 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3047 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3048 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3049 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3050 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3052 /* Initial value for re-tuning timer count */
3053 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3054 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3057 * In case Re-tuning Timer is not disabled, the actual value of
3058 * re-tuning timer will be 2 ^ (n - 1).
3060 if (host->tuning_count)
3061 host->tuning_count = 1 << (host->tuning_count - 1);
3063 /* Re-tuning mode supported by the Host Controller */
3064 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3065 SDHCI_RETUNING_MODE_SHIFT;
3070 * According to SD Host Controller spec v3.00, if the Host System
3071 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3072 * the value is meaningful only if Voltage Support in the Capabilities
3073 * register is set. The actual current value is 4 times the register
3076 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3077 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3078 int curr = regulator_get_current_limit(mmc->supply.vmmc);
3081 /* convert to SDHCI_MAX_CURRENT format */
3082 curr = curr/1000; /* convert to mA */
3083 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3085 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3087 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3088 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3089 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3093 if (caps[0] & SDHCI_CAN_VDD_330) {
3094 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3096 mmc->max_current_330 = ((max_current_caps &
3097 SDHCI_MAX_CURRENT_330_MASK) >>
3098 SDHCI_MAX_CURRENT_330_SHIFT) *
3099 SDHCI_MAX_CURRENT_MULTIPLIER;
3101 if (caps[0] & SDHCI_CAN_VDD_300) {
3102 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3104 mmc->max_current_300 = ((max_current_caps &
3105 SDHCI_MAX_CURRENT_300_MASK) >>
3106 SDHCI_MAX_CURRENT_300_SHIFT) *
3107 SDHCI_MAX_CURRENT_MULTIPLIER;
3109 if (caps[0] & SDHCI_CAN_VDD_180) {
3110 ocr_avail |= MMC_VDD_165_195;
3112 mmc->max_current_180 = ((max_current_caps &
3113 SDHCI_MAX_CURRENT_180_MASK) >>
3114 SDHCI_MAX_CURRENT_180_SHIFT) *
3115 SDHCI_MAX_CURRENT_MULTIPLIER;
3118 /* If OCR set by external regulators, use it instead */
3120 ocr_avail = mmc->ocr_avail;
3123 ocr_avail &= host->ocr_mask;
3125 mmc->ocr_avail = ocr_avail;
3126 mmc->ocr_avail_sdio = ocr_avail;
3127 if (host->ocr_avail_sdio)
3128 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3129 mmc->ocr_avail_sd = ocr_avail;
3130 if (host->ocr_avail_sd)
3131 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3132 else /* normal SD controllers don't support 1.8V */
3133 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3134 mmc->ocr_avail_mmc = ocr_avail;
3135 if (host->ocr_avail_mmc)
3136 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3138 if (mmc->ocr_avail == 0) {
3139 pr_err("%s: Hardware doesn't report any "
3140 "support voltages.\n", mmc_hostname(mmc));
3144 spin_lock_init(&host->lock);
3147 * Maximum number of segments. Depends on if the hardware
3148 * can do scatter/gather or not.
3150 if (host->flags & SDHCI_USE_ADMA)
3151 mmc->max_segs = 128;
3152 else if (host->flags & SDHCI_USE_SDMA)
3155 mmc->max_segs = 128;
3158 * Maximum number of sectors in one transfer. Limited by DMA boundary
3161 mmc->max_req_size = 524288;
3164 * Maximum segment size. Could be one segment with the maximum number
3165 * of bytes. When doing hardware scatter/gather, each entry cannot
3166 * be larger than 64 KiB though.
3168 if (host->flags & SDHCI_USE_ADMA) {
3169 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3170 mmc->max_seg_size = 65535;
3172 mmc->max_seg_size = 65536;
3174 mmc->max_seg_size = mmc->max_req_size;
3178 * Maximum block size. This varies from controller to controller and
3179 * is specified in the capabilities register.
3181 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3182 mmc->max_blk_size = 2;
3184 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3185 SDHCI_MAX_BLOCK_SHIFT;
3186 if (mmc->max_blk_size >= 3) {
3187 pr_warning("%s: Invalid maximum block size, "
3188 "assuming 512 bytes\n", mmc_hostname(mmc));
3189 mmc->max_blk_size = 0;
3193 mmc->max_blk_size = 512 << mmc->max_blk_size;
3196 * Maximum block count.
3198 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3203 tasklet_init(&host->finish_tasklet,
3204 sdhci_tasklet_finish, (unsigned long)host);
3206 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3208 if (host->version >= SDHCI_SPEC_300) {
3209 init_waitqueue_head(&host->buf_ready_int);
3211 /* Initialize re-tuning timer */
3212 init_timer(&host->tuning_timer);
3213 host->tuning_timer.data = (unsigned long)host;
3214 host->tuning_timer.function = sdhci_tuning_timer;
3217 sdhci_init(host, 0);
3219 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3220 IRQF_SHARED, mmc_hostname(mmc), host);
3222 pr_err("%s: Failed to request IRQ %d: %d\n",
3223 mmc_hostname(mmc), host->irq, ret);
3227 #ifdef CONFIG_MMC_DEBUG
3228 sdhci_dumpregs(host);
3231 #ifdef SDHCI_USE_LEDS_CLASS
3232 snprintf(host->led_name, sizeof(host->led_name),
3233 "%s::", mmc_hostname(mmc));
3234 host->led.name = host->led_name;
3235 host->led.brightness = LED_OFF;
3236 host->led.default_trigger = mmc_hostname(mmc);
3237 host->led.brightness_set = sdhci_led_control;
3239 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3241 pr_err("%s: Failed to register LED device: %d\n",
3242 mmc_hostname(mmc), ret);
3251 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3252 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3253 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3254 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3256 sdhci_enable_card_detection(host);
3260 #ifdef SDHCI_USE_LEDS_CLASS
3262 sdhci_do_reset(host, SDHCI_RESET_ALL);
3263 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3264 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3265 free_irq(host->irq, host);
3268 tasklet_kill(&host->finish_tasklet);
3273 EXPORT_SYMBOL_GPL(sdhci_add_host);
3275 void sdhci_remove_host(struct sdhci_host *host, int dead)
3277 struct mmc_host *mmc = host->mmc;
3278 unsigned long flags;
3281 spin_lock_irqsave(&host->lock, flags);
3283 host->flags |= SDHCI_DEVICE_DEAD;
3286 pr_err("%s: Controller removed during "
3287 " transfer!\n", mmc_hostname(mmc));
3289 host->mrq->cmd->error = -ENOMEDIUM;
3290 tasklet_schedule(&host->finish_tasklet);
3293 spin_unlock_irqrestore(&host->lock, flags);
3296 sdhci_disable_card_detection(host);
3298 mmc_remove_host(mmc);
3300 #ifdef SDHCI_USE_LEDS_CLASS
3301 led_classdev_unregister(&host->led);
3305 sdhci_do_reset(host, SDHCI_RESET_ALL);
3307 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3308 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3309 free_irq(host->irq, host);
3311 del_timer_sync(&host->timer);
3313 tasklet_kill(&host->finish_tasklet);
3315 if (!IS_ERR(mmc->supply.vmmc))
3316 regulator_disable(mmc->supply.vmmc);
3318 if (!IS_ERR(mmc->supply.vqmmc))
3319 regulator_disable(mmc->supply.vqmmc);
3321 if (host->adma_desc)
3322 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
3323 host->adma_desc, host->adma_addr);
3324 kfree(host->align_buffer);
3326 host->adma_desc = NULL;
3327 host->align_buffer = NULL;
3330 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3332 void sdhci_free_host(struct sdhci_host *host)
3334 mmc_free_host(host->mmc);
3337 EXPORT_SYMBOL_GPL(sdhci_free_host);
3339 /*****************************************************************************\
3341 * Driver init/exit *
3343 \*****************************************************************************/
3345 static int __init sdhci_drv_init(void)
3348 ": Secure Digital Host Controller Interface driver\n");
3349 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3354 static void __exit sdhci_drv_exit(void)
3358 module_init(sdhci_drv_init);
3359 module_exit(sdhci_drv_exit);
3361 module_param(debug_quirks, uint, 0444);
3362 module_param(debug_quirks2, uint, 0444);
3364 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3365 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3366 MODULE_LICENSE("GPL");
3368 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3369 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");