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
48 * The ADMA2 descriptor table size is calculated as the maximum number of
49 * segments (128), times 2 to allow for an alignment descriptor for each
50 * segment, plus 1 for a nop end descriptor, all multipled by the 32-bit
51 * descriptor size (8).
53 #define ADMA_SIZE ((128 * 2 + 1) * 8)
55 static unsigned int debug_quirks = 0;
56 static unsigned int debug_quirks2;
58 static void sdhci_finish_data(struct sdhci_host *);
60 static void sdhci_finish_command(struct sdhci_host *);
61 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
62 static void sdhci_tuning_timer(unsigned long data);
63 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
65 #ifdef CONFIG_PM_RUNTIME
66 static int sdhci_runtime_pm_get(struct sdhci_host *host);
67 static int sdhci_runtime_pm_put(struct sdhci_host *host);
68 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
69 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
71 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
75 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
79 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
82 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
87 static void sdhci_dumpregs(struct sdhci_host *host)
89 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
90 mmc_hostname(host->mmc));
92 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
93 sdhci_readl(host, SDHCI_DMA_ADDRESS),
94 sdhci_readw(host, SDHCI_HOST_VERSION));
95 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
96 sdhci_readw(host, SDHCI_BLOCK_SIZE),
97 sdhci_readw(host, SDHCI_BLOCK_COUNT));
98 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
99 sdhci_readl(host, SDHCI_ARGUMENT),
100 sdhci_readw(host, SDHCI_TRANSFER_MODE));
101 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
102 sdhci_readl(host, SDHCI_PRESENT_STATE),
103 sdhci_readb(host, SDHCI_HOST_CONTROL));
104 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
105 sdhci_readb(host, SDHCI_POWER_CONTROL),
106 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
107 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
108 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
109 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
110 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
111 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
112 sdhci_readl(host, SDHCI_INT_STATUS));
113 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
114 sdhci_readl(host, SDHCI_INT_ENABLE),
115 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
116 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
117 sdhci_readw(host, SDHCI_ACMD12_ERR),
118 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
119 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
120 sdhci_readl(host, SDHCI_CAPABILITIES),
121 sdhci_readl(host, SDHCI_CAPABILITIES_1));
122 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
123 sdhci_readw(host, SDHCI_COMMAND),
124 sdhci_readl(host, SDHCI_MAX_CURRENT));
125 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
126 sdhci_readw(host, SDHCI_HOST_CONTROL2));
128 if (host->flags & SDHCI_USE_ADMA)
129 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
130 readl(host->ioaddr + SDHCI_ADMA_ERROR),
131 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
133 pr_debug(DRIVER_NAME ": ===========================================\n");
136 /*****************************************************************************\
138 * Low level functions *
140 \*****************************************************************************/
142 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
147 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
151 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
155 SDHCI_INT_CARD_INSERT;
157 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
160 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
161 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
164 static void sdhci_enable_card_detection(struct sdhci_host *host)
166 sdhci_set_card_detection(host, true);
169 static void sdhci_disable_card_detection(struct sdhci_host *host)
171 sdhci_set_card_detection(host, false);
174 void sdhci_reset(struct sdhci_host *host, u8 mask)
176 unsigned long timeout;
178 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
180 if (mask & SDHCI_RESET_ALL) {
182 /* Reset-all turns off SD Bus Power */
183 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
184 sdhci_runtime_pm_bus_off(host);
187 /* Wait max 100 ms */
190 /* hw clears the bit when it's done */
191 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
193 pr_err("%s: Reset 0x%x never completed.\n",
194 mmc_hostname(host->mmc), (int)mask);
195 sdhci_dumpregs(host);
202 EXPORT_SYMBOL_GPL(sdhci_reset);
204 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
206 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
207 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
212 host->ops->reset(host, mask);
214 if (mask & SDHCI_RESET_ALL) {
215 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
216 if (host->ops->enable_dma)
217 host->ops->enable_dma(host);
220 /* Resetting the controller clears many */
221 host->preset_enabled = false;
225 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
227 static void sdhci_init(struct sdhci_host *host, int soft)
230 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
232 sdhci_do_reset(host, SDHCI_RESET_ALL);
234 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
235 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
236 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
237 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
240 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
241 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
244 /* force clock reconfiguration */
246 sdhci_set_ios(host->mmc, &host->mmc->ios);
250 static void sdhci_reinit(struct sdhci_host *host)
254 * Retuning stuffs are affected by different cards inserted and only
255 * applicable to UHS-I cards. So reset these fields to their initial
256 * value when card is removed.
258 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
259 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
261 del_timer_sync(&host->tuning_timer);
262 host->flags &= ~SDHCI_NEEDS_RETUNING;
263 host->mmc->max_blk_count =
264 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
266 sdhci_enable_card_detection(host);
269 static void sdhci_activate_led(struct sdhci_host *host)
273 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
274 ctrl |= SDHCI_CTRL_LED;
275 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
278 static void sdhci_deactivate_led(struct sdhci_host *host)
282 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
283 ctrl &= ~SDHCI_CTRL_LED;
284 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
287 #ifdef SDHCI_USE_LEDS_CLASS
288 static void sdhci_led_control(struct led_classdev *led,
289 enum led_brightness brightness)
291 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
294 spin_lock_irqsave(&host->lock, flags);
296 if (host->runtime_suspended)
299 if (brightness == LED_OFF)
300 sdhci_deactivate_led(host);
302 sdhci_activate_led(host);
304 spin_unlock_irqrestore(&host->lock, flags);
308 /*****************************************************************************\
312 \*****************************************************************************/
314 static void sdhci_read_block_pio(struct sdhci_host *host)
317 size_t blksize, len, chunk;
318 u32 uninitialized_var(scratch);
321 DBG("PIO reading\n");
323 blksize = host->data->blksz;
326 local_irq_save(flags);
329 if (!sg_miter_next(&host->sg_miter))
332 len = min(host->sg_miter.length, blksize);
335 host->sg_miter.consumed = len;
337 buf = host->sg_miter.addr;
341 scratch = sdhci_readl(host, SDHCI_BUFFER);
345 *buf = scratch & 0xFF;
354 sg_miter_stop(&host->sg_miter);
356 local_irq_restore(flags);
359 static void sdhci_write_block_pio(struct sdhci_host *host)
362 size_t blksize, len, chunk;
366 DBG("PIO writing\n");
368 blksize = host->data->blksz;
372 local_irq_save(flags);
375 if (!sg_miter_next(&host->sg_miter))
378 len = min(host->sg_miter.length, blksize);
381 host->sg_miter.consumed = len;
383 buf = host->sg_miter.addr;
386 scratch |= (u32)*buf << (chunk * 8);
392 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
393 sdhci_writel(host, scratch, SDHCI_BUFFER);
400 sg_miter_stop(&host->sg_miter);
402 local_irq_restore(flags);
405 static void sdhci_transfer_pio(struct sdhci_host *host)
411 if (host->blocks == 0)
414 if (host->data->flags & MMC_DATA_READ)
415 mask = SDHCI_DATA_AVAILABLE;
417 mask = SDHCI_SPACE_AVAILABLE;
420 * Some controllers (JMicron JMB38x) mess up the buffer bits
421 * for transfers < 4 bytes. As long as it is just one block,
422 * we can ignore the bits.
424 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
425 (host->data->blocks == 1))
428 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
429 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
432 if (host->data->flags & MMC_DATA_READ)
433 sdhci_read_block_pio(host);
435 sdhci_write_block_pio(host);
438 if (host->blocks == 0)
442 DBG("PIO transfer complete.\n");
445 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
447 local_irq_save(*flags);
448 return kmap_atomic(sg_page(sg)) + sg->offset;
451 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
453 kunmap_atomic(buffer);
454 local_irq_restore(*flags);
457 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
459 __le32 *dataddr = (__le32 __force *)(desc + 4);
460 __le16 *cmdlen = (__le16 __force *)desc;
462 /* SDHCI specification says ADMA descriptors should be 4 byte
463 * aligned, so using 16 or 32bit operations should be safe. */
465 cmdlen[0] = cpu_to_le16(cmd);
466 cmdlen[1] = cpu_to_le16(len);
468 dataddr[0] = cpu_to_le32(addr);
471 static int sdhci_adma_table_pre(struct sdhci_host *host,
472 struct mmc_data *data)
479 dma_addr_t align_addr;
482 struct scatterlist *sg;
488 * The spec does not specify endianness of descriptor table.
489 * We currently guess that it is LE.
492 if (data->flags & MMC_DATA_READ)
493 direction = DMA_FROM_DEVICE;
495 direction = DMA_TO_DEVICE;
497 host->align_addr = dma_map_single(mmc_dev(host->mmc),
498 host->align_buffer, 128 * 4, direction);
499 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
501 BUG_ON(host->align_addr & 0x3);
503 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
504 data->sg, data->sg_len, direction);
505 if (host->sg_count == 0)
508 desc = host->adma_desc;
509 align = host->align_buffer;
511 align_addr = host->align_addr;
513 for_each_sg(data->sg, sg, host->sg_count, i) {
514 addr = sg_dma_address(sg);
515 len = sg_dma_len(sg);
518 * The SDHCI specification states that ADMA
519 * addresses must be 32-bit aligned. If they
520 * aren't, then we use a bounce buffer for
521 * the (up to three) bytes that screw up the
524 offset = (4 - (addr & 0x3)) & 0x3;
526 if (data->flags & MMC_DATA_WRITE) {
527 buffer = sdhci_kmap_atomic(sg, &flags);
528 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
529 (PAGE_SIZE - offset));
530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
535 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
537 BUG_ON(offset > 65536);
551 sdhci_set_adma_desc(desc, addr, len, 0x21);
555 * If this triggers then we have a calculation bug
558 WARN_ON((desc - host->adma_desc) >= ADMA_SIZE);
561 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
563 * Mark the last descriptor as the terminating descriptor
565 if (desc != host->adma_desc) {
567 desc[0] |= 0x2; /* end */
571 * Add a terminating entry.
574 /* nop, end, valid */
575 sdhci_set_adma_desc(desc, 0, 0, 0x3);
579 * Resync align buffer as we might have changed it.
581 if (data->flags & MMC_DATA_WRITE) {
582 dma_sync_single_for_device(mmc_dev(host->mmc),
583 host->align_addr, 128 * 4, direction);
589 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
595 static void sdhci_adma_table_post(struct sdhci_host *host,
596 struct mmc_data *data)
600 struct scatterlist *sg;
607 if (data->flags & MMC_DATA_READ)
608 direction = DMA_FROM_DEVICE;
610 direction = DMA_TO_DEVICE;
612 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
615 /* Do a quick scan of the SG list for any unaligned mappings */
616 has_unaligned = false;
617 for_each_sg(data->sg, sg, host->sg_count, i)
618 if (sg_dma_address(sg) & 3) {
619 has_unaligned = true;
623 if (has_unaligned && data->flags & MMC_DATA_READ) {
624 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
625 data->sg_len, direction);
627 align = host->align_buffer;
629 for_each_sg(data->sg, sg, host->sg_count, i) {
630 if (sg_dma_address(sg) & 0x3) {
631 size = 4 - (sg_dma_address(sg) & 0x3);
633 buffer = sdhci_kmap_atomic(sg, &flags);
634 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
636 memcpy(buffer, align, size);
637 sdhci_kunmap_atomic(buffer, &flags);
644 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
645 data->sg_len, direction);
648 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
651 struct mmc_data *data = cmd->data;
652 unsigned target_timeout, current_timeout;
655 * If the host controller provides us with an incorrect timeout
656 * value, just skip the check and use 0xE. The hardware may take
657 * longer to time out, but that's much better than having a too-short
660 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
663 /* Unspecified timeout, assume max */
664 if (!data && !cmd->busy_timeout)
669 target_timeout = cmd->busy_timeout * 1000;
671 target_timeout = data->timeout_ns / 1000;
673 target_timeout += data->timeout_clks / host->clock;
677 * Figure out needed cycles.
678 * We do this in steps in order to fit inside a 32 bit int.
679 * The first step is the minimum timeout, which will have a
680 * minimum resolution of 6 bits:
681 * (1) 2^13*1000 > 2^22,
682 * (2) host->timeout_clk < 2^16
687 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
688 while (current_timeout < target_timeout) {
690 current_timeout <<= 1;
696 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
697 mmc_hostname(host->mmc), count, cmd->opcode);
704 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
706 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
707 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
709 if (host->flags & SDHCI_REQ_USE_DMA)
710 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
712 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
714 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
715 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
718 static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
722 if (host->ops->set_timeout) {
723 host->ops->set_timeout(host, cmd);
725 count = sdhci_calc_timeout(host, cmd);
726 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
730 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
733 struct mmc_data *data = cmd->data;
738 if (data || (cmd->flags & MMC_RSP_BUSY))
739 sdhci_set_timeout(host, cmd);
745 BUG_ON(data->blksz * data->blocks > 524288);
746 BUG_ON(data->blksz > host->mmc->max_blk_size);
747 BUG_ON(data->blocks > 65535);
750 host->data_early = 0;
751 host->data->bytes_xfered = 0;
753 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
754 host->flags |= SDHCI_REQ_USE_DMA;
757 * FIXME: This doesn't account for merging when mapping the
760 if (host->flags & SDHCI_REQ_USE_DMA) {
762 struct scatterlist *sg;
765 if (host->flags & SDHCI_USE_ADMA) {
766 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
769 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
773 if (unlikely(broken)) {
774 for_each_sg(data->sg, sg, data->sg_len, i) {
775 if (sg->length & 0x3) {
776 DBG("Reverting to PIO because of "
777 "transfer size (%d)\n",
779 host->flags &= ~SDHCI_REQ_USE_DMA;
787 * The assumption here being that alignment is the same after
788 * translation to device address space.
790 if (host->flags & SDHCI_REQ_USE_DMA) {
792 struct scatterlist *sg;
795 if (host->flags & SDHCI_USE_ADMA) {
797 * As we use 3 byte chunks to work around
798 * alignment problems, we need to check this
801 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
804 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
808 if (unlikely(broken)) {
809 for_each_sg(data->sg, sg, data->sg_len, i) {
810 if (sg->offset & 0x3) {
811 DBG("Reverting to PIO because of "
813 host->flags &= ~SDHCI_REQ_USE_DMA;
820 if (host->flags & SDHCI_REQ_USE_DMA) {
821 if (host->flags & SDHCI_USE_ADMA) {
822 ret = sdhci_adma_table_pre(host, data);
825 * This only happens when someone fed
826 * us an invalid request.
829 host->flags &= ~SDHCI_REQ_USE_DMA;
831 sdhci_writel(host, host->adma_addr,
837 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
838 data->sg, data->sg_len,
839 (data->flags & MMC_DATA_READ) ?
844 * This only happens when someone fed
845 * us an invalid request.
848 host->flags &= ~SDHCI_REQ_USE_DMA;
850 WARN_ON(sg_cnt != 1);
851 sdhci_writel(host, sg_dma_address(data->sg),
858 * Always adjust the DMA selection as some controllers
859 * (e.g. JMicron) can't do PIO properly when the selection
862 if (host->version >= SDHCI_SPEC_200) {
863 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
864 ctrl &= ~SDHCI_CTRL_DMA_MASK;
865 if ((host->flags & SDHCI_REQ_USE_DMA) &&
866 (host->flags & SDHCI_USE_ADMA))
867 ctrl |= SDHCI_CTRL_ADMA32;
869 ctrl |= SDHCI_CTRL_SDMA;
870 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
873 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
876 flags = SG_MITER_ATOMIC;
877 if (host->data->flags & MMC_DATA_READ)
878 flags |= SG_MITER_TO_SG;
880 flags |= SG_MITER_FROM_SG;
881 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
882 host->blocks = data->blocks;
885 sdhci_set_transfer_irqs(host);
887 /* Set the DMA boundary value and block size */
888 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
889 data->blksz), SDHCI_BLOCK_SIZE);
890 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
893 static void sdhci_set_transfer_mode(struct sdhci_host *host,
894 struct mmc_command *cmd)
897 struct mmc_data *data = cmd->data;
900 /* clear Auto CMD settings for no data CMDs */
901 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
902 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
903 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
907 WARN_ON(!host->data);
909 mode = SDHCI_TRNS_BLK_CNT_EN;
910 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
911 mode |= SDHCI_TRNS_MULTI;
913 * If we are sending CMD23, CMD12 never gets sent
914 * on successful completion (so no Auto-CMD12).
916 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
917 mode |= SDHCI_TRNS_AUTO_CMD12;
918 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
919 mode |= SDHCI_TRNS_AUTO_CMD23;
920 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
924 if (data->flags & MMC_DATA_READ)
925 mode |= SDHCI_TRNS_READ;
926 if (host->flags & SDHCI_REQ_USE_DMA)
927 mode |= SDHCI_TRNS_DMA;
929 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
932 static void sdhci_finish_data(struct sdhci_host *host)
934 struct mmc_data *data;
941 if (host->flags & SDHCI_REQ_USE_DMA) {
942 if (host->flags & SDHCI_USE_ADMA)
943 sdhci_adma_table_post(host, data);
945 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
946 data->sg_len, (data->flags & MMC_DATA_READ) ?
947 DMA_FROM_DEVICE : DMA_TO_DEVICE);
952 * The specification states that the block count register must
953 * be updated, but it does not specify at what point in the
954 * data flow. That makes the register entirely useless to read
955 * back so we have to assume that nothing made it to the card
956 * in the event of an error.
959 data->bytes_xfered = 0;
961 data->bytes_xfered = data->blksz * data->blocks;
964 * Need to send CMD12 if -
965 * a) open-ended multiblock transfer (no CMD23)
966 * b) error in multiblock transfer
973 * The controller needs a reset of internal state machines
974 * upon error conditions.
977 sdhci_do_reset(host, SDHCI_RESET_CMD);
978 sdhci_do_reset(host, SDHCI_RESET_DATA);
981 sdhci_send_command(host, data->stop);
983 tasklet_schedule(&host->finish_tasklet);
986 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
990 unsigned long timeout;
997 mask = SDHCI_CMD_INHIBIT;
998 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
999 mask |= SDHCI_DATA_INHIBIT;
1001 /* We shouldn't wait for data inihibit for stop commands, even
1002 though they might use busy signaling */
1003 if (host->mrq->data && (cmd == host->mrq->data->stop))
1004 mask &= ~SDHCI_DATA_INHIBIT;
1006 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1008 pr_err("%s: Controller never released "
1009 "inhibit bit(s).\n", mmc_hostname(host->mmc));
1010 sdhci_dumpregs(host);
1012 tasklet_schedule(&host->finish_tasklet);
1020 if (!cmd->data && cmd->busy_timeout > 9000)
1021 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1024 mod_timer(&host->timer, timeout);
1027 host->busy_handle = 0;
1029 sdhci_prepare_data(host, cmd);
1031 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1033 sdhci_set_transfer_mode(host, cmd);
1035 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1036 pr_err("%s: Unsupported response type!\n",
1037 mmc_hostname(host->mmc));
1038 cmd->error = -EINVAL;
1039 tasklet_schedule(&host->finish_tasklet);
1043 if (!(cmd->flags & MMC_RSP_PRESENT))
1044 flags = SDHCI_CMD_RESP_NONE;
1045 else if (cmd->flags & MMC_RSP_136)
1046 flags = SDHCI_CMD_RESP_LONG;
1047 else if (cmd->flags & MMC_RSP_BUSY)
1048 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1050 flags = SDHCI_CMD_RESP_SHORT;
1052 if (cmd->flags & MMC_RSP_CRC)
1053 flags |= SDHCI_CMD_CRC;
1054 if (cmd->flags & MMC_RSP_OPCODE)
1055 flags |= SDHCI_CMD_INDEX;
1057 /* CMD19 is special in that the Data Present Select should be set */
1058 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1059 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1060 flags |= SDHCI_CMD_DATA;
1062 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1064 EXPORT_SYMBOL_GPL(sdhci_send_command);
1066 static void sdhci_finish_command(struct sdhci_host *host)
1070 BUG_ON(host->cmd == NULL);
1072 if (host->cmd->flags & MMC_RSP_PRESENT) {
1073 if (host->cmd->flags & MMC_RSP_136) {
1074 /* CRC is stripped so we need to do some shifting. */
1075 for (i = 0;i < 4;i++) {
1076 host->cmd->resp[i] = sdhci_readl(host,
1077 SDHCI_RESPONSE + (3-i)*4) << 8;
1079 host->cmd->resp[i] |=
1081 SDHCI_RESPONSE + (3-i)*4-1);
1084 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1088 host->cmd->error = 0;
1090 /* Finished CMD23, now send actual command. */
1091 if (host->cmd == host->mrq->sbc) {
1093 sdhci_send_command(host, host->mrq->cmd);
1096 /* Processed actual command. */
1097 if (host->data && host->data_early)
1098 sdhci_finish_data(host);
1100 if (!host->cmd->data)
1101 tasklet_schedule(&host->finish_tasklet);
1107 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1111 switch (host->timing) {
1112 case MMC_TIMING_UHS_SDR12:
1113 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1115 case MMC_TIMING_UHS_SDR25:
1116 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1118 case MMC_TIMING_UHS_SDR50:
1119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1121 case MMC_TIMING_UHS_SDR104:
1122 case MMC_TIMING_MMC_HS200:
1123 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1125 case MMC_TIMING_UHS_DDR50:
1126 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1129 pr_warn("%s: Invalid UHS-I mode selected\n",
1130 mmc_hostname(host->mmc));
1131 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1137 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1139 int div = 0; /* Initialized for compiler warning */
1140 int real_div = div, clk_mul = 1;
1142 unsigned long timeout;
1144 host->mmc->actual_clock = 0;
1146 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1151 if (host->version >= SDHCI_SPEC_300) {
1152 if (host->preset_enabled) {
1155 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1156 pre_val = sdhci_get_preset_value(host);
1157 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1158 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1159 if (host->clk_mul &&
1160 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1161 clk = SDHCI_PROG_CLOCK_MODE;
1163 clk_mul = host->clk_mul;
1165 real_div = max_t(int, 1, div << 1);
1171 * Check if the Host Controller supports Programmable Clock
1174 if (host->clk_mul) {
1175 for (div = 1; div <= 1024; div++) {
1176 if ((host->max_clk * host->clk_mul / div)
1181 * Set Programmable Clock Mode in the Clock
1184 clk = SDHCI_PROG_CLOCK_MODE;
1186 clk_mul = host->clk_mul;
1189 /* Version 3.00 divisors must be a multiple of 2. */
1190 if (host->max_clk <= clock)
1193 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1195 if ((host->max_clk / div) <= clock)
1203 /* Version 2.00 divisors must be a power of 2. */
1204 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1205 if ((host->max_clk / div) <= clock)
1214 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1215 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1216 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1217 << SDHCI_DIVIDER_HI_SHIFT;
1218 clk |= SDHCI_CLOCK_INT_EN;
1219 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1221 /* Wait max 20 ms */
1223 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1224 & SDHCI_CLOCK_INT_STABLE)) {
1226 pr_err("%s: Internal clock never "
1227 "stabilised.\n", mmc_hostname(host->mmc));
1228 sdhci_dumpregs(host);
1235 clk |= SDHCI_CLOCK_CARD_EN;
1236 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1238 EXPORT_SYMBOL_GPL(sdhci_set_clock);
1240 static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1243 struct mmc_host *mmc = host->mmc;
1246 if (!IS_ERR(mmc->supply.vmmc)) {
1247 spin_unlock_irq(&host->lock);
1248 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1249 spin_lock_irq(&host->lock);
1253 if (mode != MMC_POWER_OFF) {
1255 case MMC_VDD_165_195:
1256 pwr = SDHCI_POWER_180;
1260 pwr = SDHCI_POWER_300;
1264 pwr = SDHCI_POWER_330;
1271 if (host->pwr == pwr)
1277 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1278 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1279 sdhci_runtime_pm_bus_off(host);
1283 * Spec says that we should clear the power reg before setting
1284 * a new value. Some controllers don't seem to like this though.
1286 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1287 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1290 * At least the Marvell CaFe chip gets confused if we set the
1291 * voltage and set turn on power at the same time, so set the
1294 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1295 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1297 pwr |= SDHCI_POWER_ON;
1299 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1301 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1302 sdhci_runtime_pm_bus_on(host);
1305 * Some controllers need an extra 10ms delay of 10ms before
1306 * they can apply clock after applying power
1308 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1313 /*****************************************************************************\
1317 \*****************************************************************************/
1319 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1321 struct sdhci_host *host;
1323 unsigned long flags;
1326 host = mmc_priv(mmc);
1328 sdhci_runtime_pm_get(host);
1330 spin_lock_irqsave(&host->lock, flags);
1332 WARN_ON(host->mrq != NULL);
1334 #ifndef SDHCI_USE_LEDS_CLASS
1335 sdhci_activate_led(host);
1339 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1340 * requests if Auto-CMD12 is enabled.
1342 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1344 mrq->data->stop = NULL;
1352 * Firstly check card presence from cd-gpio. The return could
1353 * be one of the following possibilities:
1354 * negative: cd-gpio is not available
1355 * zero: cd-gpio is used, and card is removed
1356 * one: cd-gpio is used, and card is present
1358 present = mmc_gpio_get_cd(host->mmc);
1360 /* If polling, assume that the card is always present. */
1361 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1364 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1368 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1369 host->mrq->cmd->error = -ENOMEDIUM;
1370 tasklet_schedule(&host->finish_tasklet);
1374 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1376 * Check if the re-tuning timer has already expired and there
1377 * is no on-going data transfer and DAT0 is not busy. If so,
1378 * we need to execute tuning procedure before sending command.
1380 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1381 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1382 (present_state & SDHCI_DATA_0_LVL_MASK)) {
1384 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1386 mmc->card->type == MMC_TYPE_MMC ?
1387 MMC_SEND_TUNING_BLOCK_HS200 :
1388 MMC_SEND_TUNING_BLOCK;
1390 /* Here we need to set the host->mrq to NULL,
1391 * in case the pending finish_tasklet
1392 * finishes it incorrectly.
1396 spin_unlock_irqrestore(&host->lock, flags);
1397 sdhci_execute_tuning(mmc, tuning_opcode);
1398 spin_lock_irqsave(&host->lock, flags);
1400 /* Restore original mmc_request structure */
1405 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1406 sdhci_send_command(host, mrq->sbc);
1408 sdhci_send_command(host, mrq->cmd);
1412 spin_unlock_irqrestore(&host->lock, flags);
1415 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1419 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1420 if (width == MMC_BUS_WIDTH_8) {
1421 ctrl &= ~SDHCI_CTRL_4BITBUS;
1422 if (host->version >= SDHCI_SPEC_300)
1423 ctrl |= SDHCI_CTRL_8BITBUS;
1425 if (host->version >= SDHCI_SPEC_300)
1426 ctrl &= ~SDHCI_CTRL_8BITBUS;
1427 if (width == MMC_BUS_WIDTH_4)
1428 ctrl |= SDHCI_CTRL_4BITBUS;
1430 ctrl &= ~SDHCI_CTRL_4BITBUS;
1432 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1434 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1436 void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1440 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1441 /* Select Bus Speed Mode for host */
1442 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1443 if ((timing == MMC_TIMING_MMC_HS200) ||
1444 (timing == MMC_TIMING_UHS_SDR104))
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1446 else if (timing == MMC_TIMING_UHS_SDR12)
1447 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1448 else if (timing == MMC_TIMING_UHS_SDR25)
1449 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1450 else if (timing == MMC_TIMING_UHS_SDR50)
1451 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1452 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1453 (timing == MMC_TIMING_MMC_DDR52))
1454 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1455 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1457 EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1459 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1461 unsigned long flags;
1463 struct mmc_host *mmc = host->mmc;
1465 spin_lock_irqsave(&host->lock, flags);
1467 if (host->flags & SDHCI_DEVICE_DEAD) {
1468 spin_unlock_irqrestore(&host->lock, flags);
1469 if (!IS_ERR(mmc->supply.vmmc) &&
1470 ios->power_mode == MMC_POWER_OFF)
1471 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1476 * Reset the chip on each power off.
1477 * Should clear out any weird states.
1479 if (ios->power_mode == MMC_POWER_OFF) {
1480 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1484 if (host->version >= SDHCI_SPEC_300 &&
1485 (ios->power_mode == MMC_POWER_UP) &&
1486 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1487 sdhci_enable_preset_value(host, false);
1489 if (!ios->clock || ios->clock != host->clock) {
1490 host->ops->set_clock(host, ios->clock);
1491 host->clock = ios->clock;
1493 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1495 host->timeout_clk = host->mmc->actual_clock ?
1496 host->mmc->actual_clock / 1000 :
1498 host->mmc->max_busy_timeout =
1499 host->ops->get_max_timeout_count ?
1500 host->ops->get_max_timeout_count(host) :
1502 host->mmc->max_busy_timeout /= host->timeout_clk;
1506 sdhci_set_power(host, ios->power_mode, ios->vdd);
1508 if (host->ops->platform_send_init_74_clocks)
1509 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1511 host->ops->set_bus_width(host, ios->bus_width);
1513 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1515 if ((ios->timing == MMC_TIMING_SD_HS ||
1516 ios->timing == MMC_TIMING_MMC_HS)
1517 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1518 ctrl |= SDHCI_CTRL_HISPD;
1520 ctrl &= ~SDHCI_CTRL_HISPD;
1522 if (host->version >= SDHCI_SPEC_300) {
1525 /* In case of UHS-I modes, set High Speed Enable */
1526 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1527 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1528 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1529 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1530 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1531 (ios->timing == MMC_TIMING_UHS_SDR25))
1532 ctrl |= SDHCI_CTRL_HISPD;
1534 if (!host->preset_enabled) {
1535 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1537 * We only need to set Driver Strength if the
1538 * preset value enable is not set.
1540 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1541 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1542 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1543 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1544 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1545 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1547 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1550 * According to SDHC Spec v3.00, if the Preset Value
1551 * Enable in the Host Control 2 register is set, we
1552 * need to reset SD Clock Enable before changing High
1553 * Speed Enable to avoid generating clock gliches.
1556 /* Reset SD Clock Enable */
1557 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1558 clk &= ~SDHCI_CLOCK_CARD_EN;
1559 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1561 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1563 /* Re-enable SD Clock */
1564 host->ops->set_clock(host, host->clock);
1567 /* Reset SD Clock Enable */
1568 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1569 clk &= ~SDHCI_CLOCK_CARD_EN;
1570 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1572 host->ops->set_uhs_signaling(host, ios->timing);
1573 host->timing = ios->timing;
1575 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1576 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1577 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1578 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1579 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1580 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1583 sdhci_enable_preset_value(host, true);
1584 preset = sdhci_get_preset_value(host);
1585 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1586 >> SDHCI_PRESET_DRV_SHIFT;
1589 /* Re-enable SD Clock */
1590 host->ops->set_clock(host, host->clock);
1592 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1595 * Some (ENE) controllers go apeshit on some ios operation,
1596 * signalling timeout and CRC errors even on CMD0. Resetting
1597 * it on each ios seems to solve the problem.
1599 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1600 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1603 spin_unlock_irqrestore(&host->lock, flags);
1606 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1608 struct sdhci_host *host = mmc_priv(mmc);
1610 sdhci_runtime_pm_get(host);
1611 sdhci_do_set_ios(host, ios);
1612 sdhci_runtime_pm_put(host);
1615 static int sdhci_do_get_cd(struct sdhci_host *host)
1617 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1619 if (host->flags & SDHCI_DEVICE_DEAD)
1622 /* If polling/nonremovable, assume that the card is always present. */
1623 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1624 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1627 /* Try slot gpio detect */
1628 if (!IS_ERR_VALUE(gpio_cd))
1631 /* Host native card detect */
1632 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1635 static int sdhci_get_cd(struct mmc_host *mmc)
1637 struct sdhci_host *host = mmc_priv(mmc);
1640 sdhci_runtime_pm_get(host);
1641 ret = sdhci_do_get_cd(host);
1642 sdhci_runtime_pm_put(host);
1646 static int sdhci_check_ro(struct sdhci_host *host)
1648 unsigned long flags;
1651 spin_lock_irqsave(&host->lock, flags);
1653 if (host->flags & SDHCI_DEVICE_DEAD)
1655 else if (host->ops->get_ro)
1656 is_readonly = host->ops->get_ro(host);
1658 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1659 & SDHCI_WRITE_PROTECT);
1661 spin_unlock_irqrestore(&host->lock, flags);
1663 /* This quirk needs to be replaced by a callback-function later */
1664 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1665 !is_readonly : is_readonly;
1668 #define SAMPLE_COUNT 5
1670 static int sdhci_do_get_ro(struct sdhci_host *host)
1674 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1675 return sdhci_check_ro(host);
1678 for (i = 0; i < SAMPLE_COUNT; i++) {
1679 if (sdhci_check_ro(host)) {
1680 if (++ro_count > SAMPLE_COUNT / 2)
1688 static void sdhci_hw_reset(struct mmc_host *mmc)
1690 struct sdhci_host *host = mmc_priv(mmc);
1692 if (host->ops && host->ops->hw_reset)
1693 host->ops->hw_reset(host);
1696 static int sdhci_get_ro(struct mmc_host *mmc)
1698 struct sdhci_host *host = mmc_priv(mmc);
1701 sdhci_runtime_pm_get(host);
1702 ret = sdhci_do_get_ro(host);
1703 sdhci_runtime_pm_put(host);
1707 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1709 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1711 host->ier |= SDHCI_INT_CARD_INT;
1713 host->ier &= ~SDHCI_INT_CARD_INT;
1715 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1716 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1721 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1723 struct sdhci_host *host = mmc_priv(mmc);
1724 unsigned long flags;
1726 sdhci_runtime_pm_get(host);
1728 spin_lock_irqsave(&host->lock, flags);
1730 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1732 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1734 sdhci_enable_sdio_irq_nolock(host, enable);
1735 spin_unlock_irqrestore(&host->lock, flags);
1737 sdhci_runtime_pm_put(host);
1740 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1741 struct mmc_ios *ios)
1743 struct mmc_host *mmc = host->mmc;
1748 * Signal Voltage Switching is only applicable for Host Controllers
1751 if (host->version < SDHCI_SPEC_300)
1754 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1756 switch (ios->signal_voltage) {
1757 case MMC_SIGNAL_VOLTAGE_330:
1758 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1759 ctrl &= ~SDHCI_CTRL_VDD_180;
1760 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1762 if (!IS_ERR(mmc->supply.vqmmc)) {
1763 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1766 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1772 usleep_range(5000, 5500);
1774 /* 3.3V regulator output should be stable within 5 ms */
1775 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1776 if (!(ctrl & SDHCI_CTRL_VDD_180))
1779 pr_warn("%s: 3.3V regulator output did not became stable\n",
1783 case MMC_SIGNAL_VOLTAGE_180:
1784 if (!IS_ERR(mmc->supply.vqmmc)) {
1785 ret = regulator_set_voltage(mmc->supply.vqmmc,
1788 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1795 * Enable 1.8V Signal Enable in the Host Control2
1798 ctrl |= SDHCI_CTRL_VDD_180;
1799 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1801 /* 1.8V regulator output should be stable within 5 ms */
1802 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1803 if (ctrl & SDHCI_CTRL_VDD_180)
1806 pr_warn("%s: 1.8V regulator output did not became stable\n",
1810 case MMC_SIGNAL_VOLTAGE_120:
1811 if (!IS_ERR(mmc->supply.vqmmc)) {
1812 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1815 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1822 /* No signal voltage switch required */
1827 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1828 struct mmc_ios *ios)
1830 struct sdhci_host *host = mmc_priv(mmc);
1833 if (host->version < SDHCI_SPEC_300)
1835 sdhci_runtime_pm_get(host);
1836 err = sdhci_do_start_signal_voltage_switch(host, ios);
1837 sdhci_runtime_pm_put(host);
1841 static int sdhci_card_busy(struct mmc_host *mmc)
1843 struct sdhci_host *host = mmc_priv(mmc);
1846 sdhci_runtime_pm_get(host);
1847 /* Check whether DAT[3:0] is 0000 */
1848 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1849 sdhci_runtime_pm_put(host);
1851 return !(present_state & SDHCI_DATA_LVL_MASK);
1854 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1856 struct sdhci_host *host = mmc_priv(mmc);
1858 int tuning_loop_counter = MAX_TUNING_LOOP;
1860 unsigned long flags;
1862 sdhci_runtime_pm_get(host);
1863 spin_lock_irqsave(&host->lock, flags);
1866 * The Host Controller needs tuning only in case of SDR104 mode
1867 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1868 * Capabilities register.
1869 * If the Host Controller supports the HS200 mode then the
1870 * tuning function has to be executed.
1872 switch (host->timing) {
1873 case MMC_TIMING_MMC_HS200:
1874 case MMC_TIMING_UHS_SDR104:
1877 case MMC_TIMING_UHS_SDR50:
1878 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1879 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1884 spin_unlock_irqrestore(&host->lock, flags);
1885 sdhci_runtime_pm_put(host);
1889 if (host->ops->platform_execute_tuning) {
1890 spin_unlock_irqrestore(&host->lock, flags);
1891 err = host->ops->platform_execute_tuning(host, opcode);
1892 sdhci_runtime_pm_put(host);
1896 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1897 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1898 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1901 * As per the Host Controller spec v3.00, tuning command
1902 * generates Buffer Read Ready interrupt, so enable that.
1904 * Note: The spec clearly says that when tuning sequence
1905 * is being performed, the controller does not generate
1906 * interrupts other than Buffer Read Ready interrupt. But
1907 * to make sure we don't hit a controller bug, we _only_
1908 * enable Buffer Read Ready interrupt here.
1910 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1911 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1914 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1915 * of loops reaches 40 times or a timeout of 150ms occurs.
1918 struct mmc_command cmd = {0};
1919 struct mmc_request mrq = {NULL};
1921 cmd.opcode = opcode;
1923 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1928 if (tuning_loop_counter-- == 0)
1935 * In response to CMD19, the card sends 64 bytes of tuning
1936 * block to the Host Controller. So we set the block size
1939 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1940 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1941 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1943 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1944 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1947 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1952 * The tuning block is sent by the card to the host controller.
1953 * So we set the TRNS_READ bit in the Transfer Mode register.
1954 * This also takes care of setting DMA Enable and Multi Block
1955 * Select in the same register to 0.
1957 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1959 sdhci_send_command(host, &cmd);
1964 spin_unlock_irqrestore(&host->lock, flags);
1965 /* Wait for Buffer Read Ready interrupt */
1966 wait_event_interruptible_timeout(host->buf_ready_int,
1967 (host->tuning_done == 1),
1968 msecs_to_jiffies(50));
1969 spin_lock_irqsave(&host->lock, flags);
1971 if (!host->tuning_done) {
1972 pr_info(DRIVER_NAME ": Timeout waiting for "
1973 "Buffer Read Ready interrupt during tuning "
1974 "procedure, falling back to fixed sampling "
1976 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1977 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1978 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1979 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1985 host->tuning_done = 0;
1987 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1989 /* eMMC spec does not require a delay between tuning cycles */
1990 if (opcode == MMC_SEND_TUNING_BLOCK)
1992 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1995 * The Host Driver has exhausted the maximum number of loops allowed,
1996 * so use fixed sampling frequency.
1998 if (tuning_loop_counter < 0) {
1999 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2000 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2002 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2003 pr_info(DRIVER_NAME ": Tuning procedure"
2004 " failed, falling back to fixed sampling"
2011 * If this is the very first time we are here, we start the retuning
2012 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2013 * flag won't be set, we check this condition before actually starting
2016 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2017 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2018 host->flags |= SDHCI_USING_RETUNING_TIMER;
2019 mod_timer(&host->tuning_timer, jiffies +
2020 host->tuning_count * HZ);
2021 /* Tuning mode 1 limits the maximum data length to 4MB */
2022 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2023 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2024 host->flags &= ~SDHCI_NEEDS_RETUNING;
2025 /* Reload the new initial value for timer */
2026 mod_timer(&host->tuning_timer, jiffies +
2027 host->tuning_count * HZ);
2031 * In case tuning fails, host controllers which support re-tuning can
2032 * try tuning again at a later time, when the re-tuning timer expires.
2033 * So for these controllers, we return 0. Since there might be other
2034 * controllers who do not have this capability, we return error for
2035 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2036 * a retuning timer to do the retuning for the card.
2038 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2041 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2042 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2043 spin_unlock_irqrestore(&host->lock, flags);
2044 sdhci_runtime_pm_put(host);
2050 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2052 /* Host Controller v3.00 defines preset value registers */
2053 if (host->version < SDHCI_SPEC_300)
2057 * We only enable or disable Preset Value if they are not already
2058 * enabled or disabled respectively. Otherwise, we bail out.
2060 if (host->preset_enabled != enable) {
2061 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2064 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2066 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2068 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2071 host->flags |= SDHCI_PV_ENABLED;
2073 host->flags &= ~SDHCI_PV_ENABLED;
2075 host->preset_enabled = enable;
2079 static void sdhci_card_event(struct mmc_host *mmc)
2081 struct sdhci_host *host = mmc_priv(mmc);
2082 unsigned long flags;
2084 /* First check if client has provided their own card event */
2085 if (host->ops->card_event)
2086 host->ops->card_event(host);
2088 spin_lock_irqsave(&host->lock, flags);
2090 /* Check host->mrq first in case we are runtime suspended */
2091 if (host->mrq && !sdhci_do_get_cd(host)) {
2092 pr_err("%s: Card removed during transfer!\n",
2093 mmc_hostname(host->mmc));
2094 pr_err("%s: Resetting controller.\n",
2095 mmc_hostname(host->mmc));
2097 sdhci_do_reset(host, SDHCI_RESET_CMD);
2098 sdhci_do_reset(host, SDHCI_RESET_DATA);
2100 host->mrq->cmd->error = -ENOMEDIUM;
2101 tasklet_schedule(&host->finish_tasklet);
2104 spin_unlock_irqrestore(&host->lock, flags);
2107 static const struct mmc_host_ops sdhci_ops = {
2108 .request = sdhci_request,
2109 .set_ios = sdhci_set_ios,
2110 .get_cd = sdhci_get_cd,
2111 .get_ro = sdhci_get_ro,
2112 .hw_reset = sdhci_hw_reset,
2113 .enable_sdio_irq = sdhci_enable_sdio_irq,
2114 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2115 .execute_tuning = sdhci_execute_tuning,
2116 .card_event = sdhci_card_event,
2117 .card_busy = sdhci_card_busy,
2120 /*****************************************************************************\
2124 \*****************************************************************************/
2126 static void sdhci_tasklet_finish(unsigned long param)
2128 struct sdhci_host *host;
2129 unsigned long flags;
2130 struct mmc_request *mrq;
2132 host = (struct sdhci_host*)param;
2134 spin_lock_irqsave(&host->lock, flags);
2137 * If this tasklet gets rescheduled while running, it will
2138 * be run again afterwards but without any active request.
2141 spin_unlock_irqrestore(&host->lock, flags);
2145 del_timer(&host->timer);
2150 * The controller needs a reset of internal state machines
2151 * upon error conditions.
2153 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2154 ((mrq->cmd && mrq->cmd->error) ||
2155 (mrq->sbc && mrq->sbc->error) ||
2156 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2157 (mrq->data->stop && mrq->data->stop->error))) ||
2158 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2160 /* Some controllers need this kick or reset won't work here */
2161 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2162 /* This is to force an update */
2163 host->ops->set_clock(host, host->clock);
2165 /* Spec says we should do both at the same time, but Ricoh
2166 controllers do not like that. */
2167 sdhci_do_reset(host, SDHCI_RESET_CMD);
2168 sdhci_do_reset(host, SDHCI_RESET_DATA);
2175 #ifndef SDHCI_USE_LEDS_CLASS
2176 sdhci_deactivate_led(host);
2180 spin_unlock_irqrestore(&host->lock, flags);
2182 mmc_request_done(host->mmc, mrq);
2183 sdhci_runtime_pm_put(host);
2186 static void sdhci_timeout_timer(unsigned long data)
2188 struct sdhci_host *host;
2189 unsigned long flags;
2191 host = (struct sdhci_host*)data;
2193 spin_lock_irqsave(&host->lock, flags);
2196 pr_err("%s: Timeout waiting for hardware "
2197 "interrupt.\n", mmc_hostname(host->mmc));
2198 sdhci_dumpregs(host);
2201 host->data->error = -ETIMEDOUT;
2202 sdhci_finish_data(host);
2205 host->cmd->error = -ETIMEDOUT;
2207 host->mrq->cmd->error = -ETIMEDOUT;
2209 tasklet_schedule(&host->finish_tasklet);
2214 spin_unlock_irqrestore(&host->lock, flags);
2217 static void sdhci_tuning_timer(unsigned long data)
2219 struct sdhci_host *host;
2220 unsigned long flags;
2222 host = (struct sdhci_host *)data;
2224 spin_lock_irqsave(&host->lock, flags);
2226 host->flags |= SDHCI_NEEDS_RETUNING;
2228 spin_unlock_irqrestore(&host->lock, flags);
2231 /*****************************************************************************\
2233 * Interrupt handling *
2235 \*****************************************************************************/
2237 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
2239 BUG_ON(intmask == 0);
2242 pr_err("%s: Got command interrupt 0x%08x even "
2243 "though no command operation was in progress.\n",
2244 mmc_hostname(host->mmc), (unsigned)intmask);
2245 sdhci_dumpregs(host);
2249 if (intmask & SDHCI_INT_TIMEOUT)
2250 host->cmd->error = -ETIMEDOUT;
2251 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2253 host->cmd->error = -EILSEQ;
2255 if (host->cmd->error) {
2256 tasklet_schedule(&host->finish_tasklet);
2261 * The host can send and interrupt when the busy state has
2262 * ended, allowing us to wait without wasting CPU cycles.
2263 * Unfortunately this is overloaded on the "data complete"
2264 * interrupt, so we need to take some care when handling
2267 * Note: The 1.0 specification is a bit ambiguous about this
2268 * feature so there might be some problems with older
2271 if (host->cmd->flags & MMC_RSP_BUSY) {
2272 if (host->cmd->data)
2273 DBG("Cannot wait for busy signal when also "
2274 "doing a data transfer");
2275 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2276 && !host->busy_handle) {
2277 /* Mark that command complete before busy is ended */
2278 host->busy_handle = 1;
2282 /* The controller does not support the end-of-busy IRQ,
2283 * fall through and take the SDHCI_INT_RESPONSE */
2284 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2285 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2286 *mask &= ~SDHCI_INT_DATA_END;
2289 if (intmask & SDHCI_INT_RESPONSE)
2290 sdhci_finish_command(host);
2293 #ifdef CONFIG_MMC_DEBUG
2294 static void sdhci_show_adma_error(struct sdhci_host *host)
2296 const char *name = mmc_hostname(host->mmc);
2297 u8 *desc = host->adma_desc;
2302 sdhci_dumpregs(host);
2305 dma = (__le32 *)(desc + 4);
2306 len = (__le16 *)(desc + 2);
2309 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2310 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2319 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2322 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2325 BUG_ON(intmask == 0);
2327 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2328 if (intmask & SDHCI_INT_DATA_AVAIL) {
2329 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2330 if (command == MMC_SEND_TUNING_BLOCK ||
2331 command == MMC_SEND_TUNING_BLOCK_HS200) {
2332 host->tuning_done = 1;
2333 wake_up(&host->buf_ready_int);
2340 * The "data complete" interrupt is also used to
2341 * indicate that a busy state has ended. See comment
2342 * above in sdhci_cmd_irq().
2344 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2345 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2346 host->cmd->error = -ETIMEDOUT;
2347 tasklet_schedule(&host->finish_tasklet);
2350 if (intmask & SDHCI_INT_DATA_END) {
2352 * Some cards handle busy-end interrupt
2353 * before the command completed, so make
2354 * sure we do things in the proper order.
2356 if (host->busy_handle)
2357 sdhci_finish_command(host);
2359 host->busy_handle = 1;
2364 pr_err("%s: Got data interrupt 0x%08x even "
2365 "though no data operation was in progress.\n",
2366 mmc_hostname(host->mmc), (unsigned)intmask);
2367 sdhci_dumpregs(host);
2372 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2373 host->data->error = -ETIMEDOUT;
2374 else if (intmask & SDHCI_INT_DATA_END_BIT)
2375 host->data->error = -EILSEQ;
2376 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2377 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2379 host->data->error = -EILSEQ;
2380 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2381 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2382 sdhci_show_adma_error(host);
2383 host->data->error = -EIO;
2384 if (host->ops->adma_workaround)
2385 host->ops->adma_workaround(host, intmask);
2388 if (host->data->error)
2389 sdhci_finish_data(host);
2391 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2392 sdhci_transfer_pio(host);
2395 * We currently don't do anything fancy with DMA
2396 * boundaries, but as we can't disable the feature
2397 * we need to at least restart the transfer.
2399 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2400 * should return a valid address to continue from, but as
2401 * some controllers are faulty, don't trust them.
2403 if (intmask & SDHCI_INT_DMA_END) {
2404 u32 dmastart, dmanow;
2405 dmastart = sg_dma_address(host->data->sg);
2406 dmanow = dmastart + host->data->bytes_xfered;
2408 * Force update to the next DMA block boundary.
2411 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2412 SDHCI_DEFAULT_BOUNDARY_SIZE;
2413 host->data->bytes_xfered = dmanow - dmastart;
2414 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2416 mmc_hostname(host->mmc), dmastart,
2417 host->data->bytes_xfered, dmanow);
2418 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2421 if (intmask & SDHCI_INT_DATA_END) {
2424 * Data managed to finish before the
2425 * command completed. Make sure we do
2426 * things in the proper order.
2428 host->data_early = 1;
2430 sdhci_finish_data(host);
2436 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2438 irqreturn_t result = IRQ_NONE;
2439 struct sdhci_host *host = dev_id;
2440 u32 intmask, mask, unexpected = 0;
2443 spin_lock(&host->lock);
2445 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2446 spin_unlock(&host->lock);
2450 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2451 if (!intmask || intmask == 0xffffffff) {
2457 /* Clear selected interrupts. */
2458 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2459 SDHCI_INT_BUS_POWER);
2460 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2462 DBG("*** %s got interrupt: 0x%08x\n",
2463 mmc_hostname(host->mmc), intmask);
2465 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2466 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2470 * There is a observation on i.mx esdhc. INSERT
2471 * bit will be immediately set again when it gets
2472 * cleared, if a card is inserted. We have to mask
2473 * the irq to prevent interrupt storm which will
2474 * freeze the system. And the REMOVE gets the
2477 * More testing are needed here to ensure it works
2478 * for other platforms though.
2480 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2481 SDHCI_INT_CARD_REMOVE);
2482 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2483 SDHCI_INT_CARD_INSERT;
2484 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2485 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2487 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2488 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2490 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2491 SDHCI_INT_CARD_REMOVE);
2492 result = IRQ_WAKE_THREAD;
2495 if (intmask & SDHCI_INT_CMD_MASK)
2496 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2499 if (intmask & SDHCI_INT_DATA_MASK)
2500 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2502 if (intmask & SDHCI_INT_BUS_POWER)
2503 pr_err("%s: Card is consuming too much power!\n",
2504 mmc_hostname(host->mmc));
2506 if (intmask & SDHCI_INT_CARD_INT) {
2507 sdhci_enable_sdio_irq_nolock(host, false);
2508 host->thread_isr |= SDHCI_INT_CARD_INT;
2509 result = IRQ_WAKE_THREAD;
2512 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2513 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2514 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2515 SDHCI_INT_CARD_INT);
2518 unexpected |= intmask;
2519 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2522 if (result == IRQ_NONE)
2523 result = IRQ_HANDLED;
2525 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2526 } while (intmask && --max_loops);
2528 spin_unlock(&host->lock);
2531 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2532 mmc_hostname(host->mmc), unexpected);
2533 sdhci_dumpregs(host);
2539 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2541 struct sdhci_host *host = dev_id;
2542 unsigned long flags;
2545 spin_lock_irqsave(&host->lock, flags);
2546 isr = host->thread_isr;
2547 host->thread_isr = 0;
2548 spin_unlock_irqrestore(&host->lock, flags);
2550 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2551 sdhci_card_event(host->mmc);
2552 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2555 if (isr & SDHCI_INT_CARD_INT) {
2556 sdio_run_irqs(host->mmc);
2558 spin_lock_irqsave(&host->lock, flags);
2559 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2560 sdhci_enable_sdio_irq_nolock(host, true);
2561 spin_unlock_irqrestore(&host->lock, flags);
2564 return isr ? IRQ_HANDLED : IRQ_NONE;
2567 /*****************************************************************************\
2571 \*****************************************************************************/
2574 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2577 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2578 | SDHCI_WAKE_ON_INT;
2580 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2582 /* Avoid fake wake up */
2583 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2584 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2585 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2587 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2589 static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2592 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2593 | SDHCI_WAKE_ON_INT;
2595 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2597 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2600 int sdhci_suspend_host(struct sdhci_host *host)
2602 sdhci_disable_card_detection(host);
2604 /* Disable tuning since we are suspending */
2605 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2606 del_timer_sync(&host->tuning_timer);
2607 host->flags &= ~SDHCI_NEEDS_RETUNING;
2610 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2612 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2613 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2614 free_irq(host->irq, host);
2616 sdhci_enable_irq_wakeups(host);
2617 enable_irq_wake(host->irq);
2622 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2624 int sdhci_resume_host(struct sdhci_host *host)
2628 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2629 if (host->ops->enable_dma)
2630 host->ops->enable_dma(host);
2633 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2634 ret = request_threaded_irq(host->irq, sdhci_irq,
2635 sdhci_thread_irq, IRQF_SHARED,
2636 mmc_hostname(host->mmc), host);
2640 sdhci_disable_irq_wakeups(host);
2641 disable_irq_wake(host->irq);
2644 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2645 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2646 /* Card keeps power but host controller does not */
2647 sdhci_init(host, 0);
2650 sdhci_do_set_ios(host, &host->mmc->ios);
2652 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2656 sdhci_enable_card_detection(host);
2658 /* Set the re-tuning expiration flag */
2659 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2660 host->flags |= SDHCI_NEEDS_RETUNING;
2665 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2666 #endif /* CONFIG_PM */
2668 #ifdef CONFIG_PM_RUNTIME
2670 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2672 return pm_runtime_get_sync(host->mmc->parent);
2675 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2677 pm_runtime_mark_last_busy(host->mmc->parent);
2678 return pm_runtime_put_autosuspend(host->mmc->parent);
2681 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2683 if (host->runtime_suspended || host->bus_on)
2685 host->bus_on = true;
2686 pm_runtime_get_noresume(host->mmc->parent);
2689 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2691 if (host->runtime_suspended || !host->bus_on)
2693 host->bus_on = false;
2694 pm_runtime_put_noidle(host->mmc->parent);
2697 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2699 unsigned long flags;
2701 /* Disable tuning since we are suspending */
2702 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2703 del_timer_sync(&host->tuning_timer);
2704 host->flags &= ~SDHCI_NEEDS_RETUNING;
2707 spin_lock_irqsave(&host->lock, flags);
2708 host->ier &= SDHCI_INT_CARD_INT;
2709 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2710 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2711 spin_unlock_irqrestore(&host->lock, flags);
2713 synchronize_hardirq(host->irq);
2715 spin_lock_irqsave(&host->lock, flags);
2716 host->runtime_suspended = true;
2717 spin_unlock_irqrestore(&host->lock, flags);
2721 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2723 int sdhci_runtime_resume_host(struct sdhci_host *host)
2725 unsigned long flags;
2726 int host_flags = host->flags;
2728 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2729 if (host->ops->enable_dma)
2730 host->ops->enable_dma(host);
2733 sdhci_init(host, 0);
2735 /* Force clock and power re-program */
2738 sdhci_do_set_ios(host, &host->mmc->ios);
2740 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2741 if ((host_flags & SDHCI_PV_ENABLED) &&
2742 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2743 spin_lock_irqsave(&host->lock, flags);
2744 sdhci_enable_preset_value(host, true);
2745 spin_unlock_irqrestore(&host->lock, flags);
2748 /* Set the re-tuning expiration flag */
2749 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2750 host->flags |= SDHCI_NEEDS_RETUNING;
2752 spin_lock_irqsave(&host->lock, flags);
2754 host->runtime_suspended = false;
2756 /* Enable SDIO IRQ */
2757 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2758 sdhci_enable_sdio_irq_nolock(host, true);
2760 /* Enable Card Detection */
2761 sdhci_enable_card_detection(host);
2763 spin_unlock_irqrestore(&host->lock, flags);
2767 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2771 /*****************************************************************************\
2773 * Device allocation/registration *
2775 \*****************************************************************************/
2777 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2780 struct mmc_host *mmc;
2781 struct sdhci_host *host;
2783 WARN_ON(dev == NULL);
2785 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2787 return ERR_PTR(-ENOMEM);
2789 host = mmc_priv(mmc);
2795 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2797 int sdhci_add_host(struct sdhci_host *host)
2799 struct mmc_host *mmc;
2800 u32 caps[2] = {0, 0};
2801 u32 max_current_caps;
2802 unsigned int ocr_avail;
2803 unsigned int override_timeout_clk;
2806 WARN_ON(host == NULL);
2813 host->quirks = debug_quirks;
2815 host->quirks2 = debug_quirks2;
2817 override_timeout_clk = host->timeout_clk;
2819 sdhci_do_reset(host, SDHCI_RESET_ALL);
2821 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2822 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2823 >> SDHCI_SPEC_VER_SHIFT;
2824 if (host->version > SDHCI_SPEC_300) {
2825 pr_err("%s: Unknown controller version (%d). "
2826 "You may experience problems.\n", mmc_hostname(mmc),
2830 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2831 sdhci_readl(host, SDHCI_CAPABILITIES);
2833 if (host->version >= SDHCI_SPEC_300)
2834 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2836 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2838 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2839 host->flags |= SDHCI_USE_SDMA;
2840 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2841 DBG("Controller doesn't have SDMA capability\n");
2843 host->flags |= SDHCI_USE_SDMA;
2845 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2846 (host->flags & SDHCI_USE_SDMA)) {
2847 DBG("Disabling DMA as it is marked broken\n");
2848 host->flags &= ~SDHCI_USE_SDMA;
2851 if ((host->version >= SDHCI_SPEC_200) &&
2852 (caps[0] & SDHCI_CAN_DO_ADMA2))
2853 host->flags |= SDHCI_USE_ADMA;
2855 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2856 (host->flags & SDHCI_USE_ADMA)) {
2857 DBG("Disabling ADMA as it is marked broken\n");
2858 host->flags &= ~SDHCI_USE_ADMA;
2861 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2862 if (host->ops->enable_dma) {
2863 if (host->ops->enable_dma(host)) {
2864 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2867 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2872 if (host->flags & SDHCI_USE_ADMA) {
2874 * We need to allocate descriptors for all sg entries
2875 * (128) and potentially one alignment transfer for
2876 * each of those entries.
2878 host->adma_desc = dma_alloc_coherent(mmc_dev(mmc),
2879 ADMA_SIZE, &host->adma_addr,
2881 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2882 if (!host->adma_desc || !host->align_buffer) {
2883 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
2884 host->adma_desc, host->adma_addr);
2885 kfree(host->align_buffer);
2886 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
2888 host->flags &= ~SDHCI_USE_ADMA;
2889 host->adma_desc = NULL;
2890 host->align_buffer = NULL;
2891 } else if (host->adma_addr & 3) {
2892 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2894 host->flags &= ~SDHCI_USE_ADMA;
2895 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
2896 host->adma_desc, host->adma_addr);
2897 kfree(host->align_buffer);
2898 host->adma_desc = NULL;
2899 host->align_buffer = NULL;
2904 * If we use DMA, then it's up to the caller to set the DMA
2905 * mask, but PIO does not need the hw shim so we set a new
2906 * mask here in that case.
2908 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2909 host->dma_mask = DMA_BIT_MASK(64);
2910 mmc_dev(mmc)->dma_mask = &host->dma_mask;
2913 if (host->version >= SDHCI_SPEC_300)
2914 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
2915 >> SDHCI_CLOCK_BASE_SHIFT;
2917 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
2918 >> SDHCI_CLOCK_BASE_SHIFT;
2920 host->max_clk *= 1000000;
2921 if (host->max_clk == 0 || host->quirks &
2922 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
2923 if (!host->ops->get_max_clock) {
2924 pr_err("%s: Hardware doesn't specify base clock "
2925 "frequency.\n", mmc_hostname(mmc));
2928 host->max_clk = host->ops->get_max_clock(host);
2932 * In case of Host Controller v3.00, find out whether clock
2933 * multiplier is supported.
2935 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2936 SDHCI_CLOCK_MUL_SHIFT;
2939 * In case the value in Clock Multiplier is 0, then programmable
2940 * clock mode is not supported, otherwise the actual clock
2941 * multiplier is one more than the value of Clock Multiplier
2942 * in the Capabilities Register.
2948 * Set host parameters.
2950 mmc->ops = &sdhci_ops;
2951 mmc->f_max = host->max_clk;
2952 if (host->ops->get_min_clock)
2953 mmc->f_min = host->ops->get_min_clock(host);
2954 else if (host->version >= SDHCI_SPEC_300) {
2955 if (host->clk_mul) {
2956 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2957 mmc->f_max = host->max_clk * host->clk_mul;
2959 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2961 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
2963 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2964 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
2965 SDHCI_TIMEOUT_CLK_SHIFT;
2966 if (host->timeout_clk == 0) {
2967 if (host->ops->get_timeout_clock) {
2969 host->ops->get_timeout_clock(host);
2971 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
2977 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2978 host->timeout_clk *= 1000;
2980 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
2981 host->ops->get_max_timeout_count(host) : 1 << 27;
2982 mmc->max_busy_timeout /= host->timeout_clk;
2985 if (override_timeout_clk)
2986 host->timeout_clk = override_timeout_clk;
2988 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2989 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2991 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2992 host->flags |= SDHCI_AUTO_CMD12;
2994 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2995 if ((host->version >= SDHCI_SPEC_300) &&
2996 ((host->flags & SDHCI_USE_ADMA) ||
2997 !(host->flags & SDHCI_USE_SDMA))) {
2998 host->flags |= SDHCI_AUTO_CMD23;
2999 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3001 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3005 * A controller may support 8-bit width, but the board itself
3006 * might not have the pins brought out. Boards that support
3007 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3008 * their platform code before calling sdhci_add_host(), and we
3009 * won't assume 8-bit width for hosts without that CAP.
3011 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
3012 mmc->caps |= MMC_CAP_4_BIT_DATA;
3014 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3015 mmc->caps &= ~MMC_CAP_CMD23;
3017 if (caps[0] & SDHCI_CAN_DO_HISPD)
3018 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3020 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3021 !(mmc->caps & MMC_CAP_NONREMOVABLE))
3022 mmc->caps |= MMC_CAP_NEEDS_POLL;
3024 /* If there are external regulators, get them */
3025 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3026 return -EPROBE_DEFER;
3028 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
3029 if (!IS_ERR(mmc->supply.vqmmc)) {
3030 ret = regulator_enable(mmc->supply.vqmmc);
3031 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3033 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3034 SDHCI_SUPPORT_SDR50 |
3035 SDHCI_SUPPORT_DDR50);
3037 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3038 mmc_hostname(mmc), ret);
3039 mmc->supply.vqmmc = NULL;
3043 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3044 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3045 SDHCI_SUPPORT_DDR50);
3047 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3048 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3049 SDHCI_SUPPORT_DDR50))
3050 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3052 /* SDR104 supports also implies SDR50 support */
3053 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3054 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3055 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3056 * field can be promoted to support HS200.
3058 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) {
3059 mmc->caps2 |= MMC_CAP2_HS200;
3060 if (IS_ERR(mmc->supply.vqmmc) ||
3061 !regulator_is_supported_voltage
3062 (mmc->supply.vqmmc, 1100000, 1300000))
3063 mmc->caps2 &= ~MMC_CAP2_HS200_1_2V_SDR;
3065 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3066 mmc->caps |= MMC_CAP_UHS_SDR50;
3068 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3069 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3070 mmc->caps |= MMC_CAP_UHS_DDR50;
3072 /* Does the host need tuning for SDR50? */
3073 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3074 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3076 /* Does the host need tuning for SDR104 / HS200? */
3077 if (mmc->caps2 & MMC_CAP2_HS200)
3078 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3080 /* Driver Type(s) (A, C, D) supported by the host */
3081 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3082 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3083 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3084 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3085 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3086 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3088 /* Initial value for re-tuning timer count */
3089 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3090 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3093 * In case Re-tuning Timer is not disabled, the actual value of
3094 * re-tuning timer will be 2 ^ (n - 1).
3096 if (host->tuning_count)
3097 host->tuning_count = 1 << (host->tuning_count - 1);
3099 /* Re-tuning mode supported by the Host Controller */
3100 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3101 SDHCI_RETUNING_MODE_SHIFT;
3106 * According to SD Host Controller spec v3.00, if the Host System
3107 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3108 * the value is meaningful only if Voltage Support in the Capabilities
3109 * register is set. The actual current value is 4 times the register
3112 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3113 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3114 int curr = regulator_get_current_limit(mmc->supply.vmmc);
3117 /* convert to SDHCI_MAX_CURRENT format */
3118 curr = curr/1000; /* convert to mA */
3119 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3121 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3123 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3124 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3125 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3129 if (caps[0] & SDHCI_CAN_VDD_330) {
3130 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3132 mmc->max_current_330 = ((max_current_caps &
3133 SDHCI_MAX_CURRENT_330_MASK) >>
3134 SDHCI_MAX_CURRENT_330_SHIFT) *
3135 SDHCI_MAX_CURRENT_MULTIPLIER;
3137 if (caps[0] & SDHCI_CAN_VDD_300) {
3138 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3140 mmc->max_current_300 = ((max_current_caps &
3141 SDHCI_MAX_CURRENT_300_MASK) >>
3142 SDHCI_MAX_CURRENT_300_SHIFT) *
3143 SDHCI_MAX_CURRENT_MULTIPLIER;
3145 if (caps[0] & SDHCI_CAN_VDD_180) {
3146 ocr_avail |= MMC_VDD_165_195;
3148 mmc->max_current_180 = ((max_current_caps &
3149 SDHCI_MAX_CURRENT_180_MASK) >>
3150 SDHCI_MAX_CURRENT_180_SHIFT) *
3151 SDHCI_MAX_CURRENT_MULTIPLIER;
3154 /* If OCR set by external regulators, use it instead */
3156 ocr_avail = mmc->ocr_avail;
3159 ocr_avail &= host->ocr_mask;
3161 mmc->ocr_avail = ocr_avail;
3162 mmc->ocr_avail_sdio = ocr_avail;
3163 if (host->ocr_avail_sdio)
3164 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3165 mmc->ocr_avail_sd = ocr_avail;
3166 if (host->ocr_avail_sd)
3167 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3168 else /* normal SD controllers don't support 1.8V */
3169 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3170 mmc->ocr_avail_mmc = ocr_avail;
3171 if (host->ocr_avail_mmc)
3172 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3174 if (mmc->ocr_avail == 0) {
3175 pr_err("%s: Hardware doesn't report any "
3176 "support voltages.\n", mmc_hostname(mmc));
3180 spin_lock_init(&host->lock);
3183 * Maximum number of segments. Depends on if the hardware
3184 * can do scatter/gather or not.
3186 if (host->flags & SDHCI_USE_ADMA)
3187 mmc->max_segs = 128;
3188 else if (host->flags & SDHCI_USE_SDMA)
3191 mmc->max_segs = 128;
3194 * Maximum number of sectors in one transfer. Limited by DMA boundary
3197 mmc->max_req_size = 524288;
3200 * Maximum segment size. Could be one segment with the maximum number
3201 * of bytes. When doing hardware scatter/gather, each entry cannot
3202 * be larger than 64 KiB though.
3204 if (host->flags & SDHCI_USE_ADMA) {
3205 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3206 mmc->max_seg_size = 65535;
3208 mmc->max_seg_size = 65536;
3210 mmc->max_seg_size = mmc->max_req_size;
3214 * Maximum block size. This varies from controller to controller and
3215 * is specified in the capabilities register.
3217 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3218 mmc->max_blk_size = 2;
3220 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3221 SDHCI_MAX_BLOCK_SHIFT;
3222 if (mmc->max_blk_size >= 3) {
3223 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3225 mmc->max_blk_size = 0;
3229 mmc->max_blk_size = 512 << mmc->max_blk_size;
3232 * Maximum block count.
3234 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3239 tasklet_init(&host->finish_tasklet,
3240 sdhci_tasklet_finish, (unsigned long)host);
3242 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3244 if (host->version >= SDHCI_SPEC_300) {
3245 init_waitqueue_head(&host->buf_ready_int);
3247 /* Initialize re-tuning timer */
3248 init_timer(&host->tuning_timer);
3249 host->tuning_timer.data = (unsigned long)host;
3250 host->tuning_timer.function = sdhci_tuning_timer;
3253 sdhci_init(host, 0);
3255 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3256 IRQF_SHARED, mmc_hostname(mmc), host);
3258 pr_err("%s: Failed to request IRQ %d: %d\n",
3259 mmc_hostname(mmc), host->irq, ret);
3263 #ifdef CONFIG_MMC_DEBUG
3264 sdhci_dumpregs(host);
3267 #ifdef SDHCI_USE_LEDS_CLASS
3268 snprintf(host->led_name, sizeof(host->led_name),
3269 "%s::", mmc_hostname(mmc));
3270 host->led.name = host->led_name;
3271 host->led.brightness = LED_OFF;
3272 host->led.default_trigger = mmc_hostname(mmc);
3273 host->led.brightness_set = sdhci_led_control;
3275 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3277 pr_err("%s: Failed to register LED device: %d\n",
3278 mmc_hostname(mmc), ret);
3287 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3288 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3289 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3290 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3292 sdhci_enable_card_detection(host);
3296 #ifdef SDHCI_USE_LEDS_CLASS
3298 sdhci_do_reset(host, SDHCI_RESET_ALL);
3299 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3300 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3301 free_irq(host->irq, host);
3304 tasklet_kill(&host->finish_tasklet);
3309 EXPORT_SYMBOL_GPL(sdhci_add_host);
3311 void sdhci_remove_host(struct sdhci_host *host, int dead)
3313 struct mmc_host *mmc = host->mmc;
3314 unsigned long flags;
3317 spin_lock_irqsave(&host->lock, flags);
3319 host->flags |= SDHCI_DEVICE_DEAD;
3322 pr_err("%s: Controller removed during "
3323 " transfer!\n", mmc_hostname(mmc));
3325 host->mrq->cmd->error = -ENOMEDIUM;
3326 tasklet_schedule(&host->finish_tasklet);
3329 spin_unlock_irqrestore(&host->lock, flags);
3332 sdhci_disable_card_detection(host);
3334 mmc_remove_host(mmc);
3336 #ifdef SDHCI_USE_LEDS_CLASS
3337 led_classdev_unregister(&host->led);
3341 sdhci_do_reset(host, SDHCI_RESET_ALL);
3343 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3344 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3345 free_irq(host->irq, host);
3347 del_timer_sync(&host->timer);
3349 tasklet_kill(&host->finish_tasklet);
3351 if (!IS_ERR(mmc->supply.vqmmc))
3352 regulator_disable(mmc->supply.vqmmc);
3354 if (host->adma_desc)
3355 dma_free_coherent(mmc_dev(mmc), ADMA_SIZE,
3356 host->adma_desc, host->adma_addr);
3357 kfree(host->align_buffer);
3359 host->adma_desc = NULL;
3360 host->align_buffer = NULL;
3363 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3365 void sdhci_free_host(struct sdhci_host *host)
3367 mmc_free_host(host->mmc);
3370 EXPORT_SYMBOL_GPL(sdhci_free_host);
3372 /*****************************************************************************\
3374 * Driver init/exit *
3376 \*****************************************************************************/
3378 static int __init sdhci_drv_init(void)
3381 ": Secure Digital Host Controller Interface driver\n");
3382 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3387 static void __exit sdhci_drv_exit(void)
3391 module_init(sdhci_drv_init);
3392 module_exit(sdhci_drv_exit);
3394 module_param(debug_quirks, uint, 0444);
3395 module_param(debug_quirks2, uint, 0444);
3397 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3398 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3399 MODULE_LICENSE("GPL");
3401 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3402 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");