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 static unsigned int debug_quirks = 0;
48 static unsigned int debug_quirks2;
50 static void sdhci_finish_data(struct sdhci_host *);
52 static void sdhci_finish_command(struct sdhci_host *);
53 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
54 static void sdhci_tuning_timer(unsigned long data);
55 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
57 #ifdef CONFIG_PM_RUNTIME
58 static int sdhci_runtime_pm_get(struct sdhci_host *host);
59 static int sdhci_runtime_pm_put(struct sdhci_host *host);
60 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
61 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
63 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
67 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
71 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
74 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
79 static void sdhci_dumpregs(struct sdhci_host *host)
81 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
82 mmc_hostname(host->mmc));
84 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
85 sdhci_readl(host, SDHCI_DMA_ADDRESS),
86 sdhci_readw(host, SDHCI_HOST_VERSION));
87 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
88 sdhci_readw(host, SDHCI_BLOCK_SIZE),
89 sdhci_readw(host, SDHCI_BLOCK_COUNT));
90 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
91 sdhci_readl(host, SDHCI_ARGUMENT),
92 sdhci_readw(host, SDHCI_TRANSFER_MODE));
93 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
94 sdhci_readl(host, SDHCI_PRESENT_STATE),
95 sdhci_readb(host, SDHCI_HOST_CONTROL));
96 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
97 sdhci_readb(host, SDHCI_POWER_CONTROL),
98 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
99 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
100 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
101 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
102 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
103 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
104 sdhci_readl(host, SDHCI_INT_STATUS));
105 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
106 sdhci_readl(host, SDHCI_INT_ENABLE),
107 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
108 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
109 sdhci_readw(host, SDHCI_ACMD12_ERR),
110 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
111 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
112 sdhci_readl(host, SDHCI_CAPABILITIES),
113 sdhci_readl(host, SDHCI_CAPABILITIES_1));
114 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
115 sdhci_readw(host, SDHCI_COMMAND),
116 sdhci_readl(host, SDHCI_MAX_CURRENT));
117 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
118 sdhci_readw(host, SDHCI_HOST_CONTROL2));
120 if (host->flags & SDHCI_USE_ADMA)
121 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
122 readl(host->ioaddr + SDHCI_ADMA_ERROR),
123 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
125 pr_debug(DRIVER_NAME ": ===========================================\n");
128 /*****************************************************************************\
130 * Low level functions *
132 \*****************************************************************************/
134 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
138 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
139 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
143 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
146 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
147 SDHCI_INT_CARD_INSERT;
149 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
152 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
153 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
156 static void sdhci_enable_card_detection(struct sdhci_host *host)
158 sdhci_set_card_detection(host, true);
161 static void sdhci_disable_card_detection(struct sdhci_host *host)
163 sdhci_set_card_detection(host, false);
166 static void sdhci_reset(struct sdhci_host *host, u8 mask)
168 unsigned long timeout;
169 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
170 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
175 if (host->ops->platform_reset_enter)
176 host->ops->platform_reset_enter(host, mask);
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 if (host->ops->platform_reset_exit)
203 host->ops->platform_reset_exit(host, mask);
205 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET) {
206 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
207 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
210 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
211 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
212 host->ops->enable_dma(host);
216 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
218 static void sdhci_init(struct sdhci_host *host, int soft)
221 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
223 sdhci_reset(host, SDHCI_RESET_ALL);
225 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
226 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
227 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
228 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
231 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
232 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
235 /* force clock reconfiguration */
237 sdhci_set_ios(host->mmc, &host->mmc->ios);
241 static void sdhci_reinit(struct sdhci_host *host)
245 * Retuning stuffs are affected by different cards inserted and only
246 * applicable to UHS-I cards. So reset these fields to their initial
247 * value when card is removed.
249 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
250 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
252 del_timer_sync(&host->tuning_timer);
253 host->flags &= ~SDHCI_NEEDS_RETUNING;
254 host->mmc->max_blk_count =
255 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
257 sdhci_enable_card_detection(host);
260 static void sdhci_activate_led(struct sdhci_host *host)
264 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
265 ctrl |= SDHCI_CTRL_LED;
266 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
269 static void sdhci_deactivate_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 #ifdef SDHCI_USE_LEDS_CLASS
279 static void sdhci_led_control(struct led_classdev *led,
280 enum led_brightness brightness)
282 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
285 spin_lock_irqsave(&host->lock, flags);
287 if (host->runtime_suspended)
290 if (brightness == LED_OFF)
291 sdhci_deactivate_led(host);
293 sdhci_activate_led(host);
295 spin_unlock_irqrestore(&host->lock, flags);
299 /*****************************************************************************\
303 \*****************************************************************************/
305 static void sdhci_read_block_pio(struct sdhci_host *host)
308 size_t blksize, len, chunk;
309 u32 uninitialized_var(scratch);
312 DBG("PIO reading\n");
314 blksize = host->data->blksz;
317 local_irq_save(flags);
320 if (!sg_miter_next(&host->sg_miter))
323 len = min(host->sg_miter.length, blksize);
326 host->sg_miter.consumed = len;
328 buf = host->sg_miter.addr;
332 scratch = sdhci_readl(host, SDHCI_BUFFER);
336 *buf = scratch & 0xFF;
345 sg_miter_stop(&host->sg_miter);
347 local_irq_restore(flags);
350 static void sdhci_write_block_pio(struct sdhci_host *host)
353 size_t blksize, len, chunk;
357 DBG("PIO writing\n");
359 blksize = host->data->blksz;
363 local_irq_save(flags);
366 if (!sg_miter_next(&host->sg_miter))
369 len = min(host->sg_miter.length, blksize);
372 host->sg_miter.consumed = len;
374 buf = host->sg_miter.addr;
377 scratch |= (u32)*buf << (chunk * 8);
383 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
384 sdhci_writel(host, scratch, SDHCI_BUFFER);
391 sg_miter_stop(&host->sg_miter);
393 local_irq_restore(flags);
396 static void sdhci_transfer_pio(struct sdhci_host *host)
402 if (host->blocks == 0)
405 if (host->data->flags & MMC_DATA_READ)
406 mask = SDHCI_DATA_AVAILABLE;
408 mask = SDHCI_SPACE_AVAILABLE;
411 * Some controllers (JMicron JMB38x) mess up the buffer bits
412 * for transfers < 4 bytes. As long as it is just one block,
413 * we can ignore the bits.
415 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
416 (host->data->blocks == 1))
419 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
420 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
423 if (host->data->flags & MMC_DATA_READ)
424 sdhci_read_block_pio(host);
426 sdhci_write_block_pio(host);
429 if (host->blocks == 0)
433 DBG("PIO transfer complete.\n");
436 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
438 local_irq_save(*flags);
439 return kmap_atomic(sg_page(sg)) + sg->offset;
442 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
444 kunmap_atomic(buffer);
445 local_irq_restore(*flags);
448 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
450 __le32 *dataddr = (__le32 __force *)(desc + 4);
451 __le16 *cmdlen = (__le16 __force *)desc;
453 /* SDHCI specification says ADMA descriptors should be 4 byte
454 * aligned, so using 16 or 32bit operations should be safe. */
456 cmdlen[0] = cpu_to_le16(cmd);
457 cmdlen[1] = cpu_to_le16(len);
459 dataddr[0] = cpu_to_le32(addr);
462 static int sdhci_adma_table_pre(struct sdhci_host *host,
463 struct mmc_data *data)
470 dma_addr_t align_addr;
473 struct scatterlist *sg;
479 * The spec does not specify endianness of descriptor table.
480 * We currently guess that it is LE.
483 if (data->flags & MMC_DATA_READ)
484 direction = DMA_FROM_DEVICE;
486 direction = DMA_TO_DEVICE;
489 * The ADMA descriptor table is mapped further down as we
490 * need to fill it with data first.
493 host->align_addr = dma_map_single(mmc_dev(host->mmc),
494 host->align_buffer, 128 * 4, direction);
495 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
497 BUG_ON(host->align_addr & 0x3);
499 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
500 data->sg, data->sg_len, direction);
501 if (host->sg_count == 0)
504 desc = host->adma_desc;
505 align = host->align_buffer;
507 align_addr = host->align_addr;
509 for_each_sg(data->sg, sg, host->sg_count, i) {
510 addr = sg_dma_address(sg);
511 len = sg_dma_len(sg);
514 * The SDHCI specification states that ADMA
515 * addresses must be 32-bit aligned. If they
516 * aren't, then we use a bounce buffer for
517 * the (up to three) bytes that screw up the
520 offset = (4 - (addr & 0x3)) & 0x3;
522 if (data->flags & MMC_DATA_WRITE) {
523 buffer = sdhci_kmap_atomic(sg, &flags);
524 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
525 memcpy(align, buffer, offset);
526 sdhci_kunmap_atomic(buffer, &flags);
530 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
532 BUG_ON(offset > 65536);
546 sdhci_set_adma_desc(desc, addr, len, 0x21);
550 * If this triggers then we have a calculation bug
553 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
556 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
558 * Mark the last descriptor as the terminating descriptor
560 if (desc != host->adma_desc) {
562 desc[0] |= 0x2; /* end */
566 * Add a terminating entry.
569 /* nop, end, valid */
570 sdhci_set_adma_desc(desc, 0, 0, 0x3);
574 * Resync align buffer as we might have changed it.
576 if (data->flags & MMC_DATA_WRITE) {
577 dma_sync_single_for_device(mmc_dev(host->mmc),
578 host->align_addr, 128 * 4, direction);
581 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
582 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
583 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
585 BUG_ON(host->adma_addr & 0x3);
590 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
591 data->sg_len, direction);
593 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
599 static void sdhci_adma_table_post(struct sdhci_host *host,
600 struct mmc_data *data)
604 struct scatterlist *sg;
610 if (data->flags & MMC_DATA_READ)
611 direction = DMA_FROM_DEVICE;
613 direction = DMA_TO_DEVICE;
615 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
616 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
618 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
621 if (data->flags & MMC_DATA_READ) {
622 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
623 data->sg_len, direction);
625 align = host->align_buffer;
627 for_each_sg(data->sg, sg, host->sg_count, i) {
628 if (sg_dma_address(sg) & 0x3) {
629 size = 4 - (sg_dma_address(sg) & 0x3);
631 buffer = sdhci_kmap_atomic(sg, &flags);
632 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
633 memcpy(buffer, align, size);
634 sdhci_kunmap_atomic(buffer, &flags);
641 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
642 data->sg_len, direction);
645 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
648 struct mmc_data *data = cmd->data;
649 unsigned target_timeout, current_timeout;
652 * If the host controller provides us with an incorrect timeout
653 * value, just skip the check and use 0xE. The hardware may take
654 * longer to time out, but that's much better than having a too-short
657 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
660 /* Unspecified timeout, assume max */
661 if (!data && !cmd->busy_timeout)
666 target_timeout = cmd->busy_timeout * 1000;
668 target_timeout = data->timeout_ns / 1000;
670 target_timeout += data->timeout_clks / host->clock;
674 * Figure out needed cycles.
675 * We do this in steps in order to fit inside a 32 bit int.
676 * The first step is the minimum timeout, which will have a
677 * minimum resolution of 6 bits:
678 * (1) 2^13*1000 > 2^22,
679 * (2) host->timeout_clk < 2^16
684 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
685 while (current_timeout < target_timeout) {
687 current_timeout <<= 1;
693 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
694 mmc_hostname(host->mmc), count, cmd->opcode);
701 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
703 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
704 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
706 if (host->flags & SDHCI_REQ_USE_DMA)
707 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
709 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
711 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
712 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
715 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
719 struct mmc_data *data = cmd->data;
724 if (data || (cmd->flags & MMC_RSP_BUSY)) {
725 count = sdhci_calc_timeout(host, cmd);
726 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
733 BUG_ON(data->blksz * data->blocks > 524288);
734 BUG_ON(data->blksz > host->mmc->max_blk_size);
735 BUG_ON(data->blocks > 65535);
738 host->data_early = 0;
739 host->data->bytes_xfered = 0;
741 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
742 host->flags |= SDHCI_REQ_USE_DMA;
745 * FIXME: This doesn't account for merging when mapping the
748 if (host->flags & SDHCI_REQ_USE_DMA) {
750 struct scatterlist *sg;
753 if (host->flags & SDHCI_USE_ADMA) {
754 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
757 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
761 if (unlikely(broken)) {
762 for_each_sg(data->sg, sg, data->sg_len, i) {
763 if (sg->length & 0x3) {
764 DBG("Reverting to PIO because of "
765 "transfer size (%d)\n",
767 host->flags &= ~SDHCI_REQ_USE_DMA;
775 * The assumption here being that alignment is the same after
776 * translation to device address space.
778 if (host->flags & SDHCI_REQ_USE_DMA) {
780 struct scatterlist *sg;
783 if (host->flags & SDHCI_USE_ADMA) {
785 * As we use 3 byte chunks to work around
786 * alignment problems, we need to check this
789 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
792 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
796 if (unlikely(broken)) {
797 for_each_sg(data->sg, sg, data->sg_len, i) {
798 if (sg->offset & 0x3) {
799 DBG("Reverting to PIO because of "
801 host->flags &= ~SDHCI_REQ_USE_DMA;
808 if (host->flags & SDHCI_REQ_USE_DMA) {
809 if (host->flags & SDHCI_USE_ADMA) {
810 ret = sdhci_adma_table_pre(host, data);
813 * This only happens when someone fed
814 * us an invalid request.
817 host->flags &= ~SDHCI_REQ_USE_DMA;
819 sdhci_writel(host, host->adma_addr,
825 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
826 data->sg, data->sg_len,
827 (data->flags & MMC_DATA_READ) ?
832 * This only happens when someone fed
833 * us an invalid request.
836 host->flags &= ~SDHCI_REQ_USE_DMA;
838 WARN_ON(sg_cnt != 1);
839 sdhci_writel(host, sg_dma_address(data->sg),
846 * Always adjust the DMA selection as some controllers
847 * (e.g. JMicron) can't do PIO properly when the selection
850 if (host->version >= SDHCI_SPEC_200) {
851 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
852 ctrl &= ~SDHCI_CTRL_DMA_MASK;
853 if ((host->flags & SDHCI_REQ_USE_DMA) &&
854 (host->flags & SDHCI_USE_ADMA))
855 ctrl |= SDHCI_CTRL_ADMA32;
857 ctrl |= SDHCI_CTRL_SDMA;
858 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
861 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
864 flags = SG_MITER_ATOMIC;
865 if (host->data->flags & MMC_DATA_READ)
866 flags |= SG_MITER_TO_SG;
868 flags |= SG_MITER_FROM_SG;
869 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
870 host->blocks = data->blocks;
873 sdhci_set_transfer_irqs(host);
875 /* Set the DMA boundary value and block size */
876 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
877 data->blksz), SDHCI_BLOCK_SIZE);
878 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
881 static void sdhci_set_transfer_mode(struct sdhci_host *host,
882 struct mmc_command *cmd)
885 struct mmc_data *data = cmd->data;
888 /* clear Auto CMD settings for no data CMDs */
889 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
890 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
891 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
895 WARN_ON(!host->data);
897 mode = SDHCI_TRNS_BLK_CNT_EN;
898 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
899 mode |= SDHCI_TRNS_MULTI;
901 * If we are sending CMD23, CMD12 never gets sent
902 * on successful completion (so no Auto-CMD12).
904 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
905 mode |= SDHCI_TRNS_AUTO_CMD12;
906 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
907 mode |= SDHCI_TRNS_AUTO_CMD23;
908 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
912 if (data->flags & MMC_DATA_READ)
913 mode |= SDHCI_TRNS_READ;
914 if (host->flags & SDHCI_REQ_USE_DMA)
915 mode |= SDHCI_TRNS_DMA;
917 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
920 static void sdhci_finish_data(struct sdhci_host *host)
922 struct mmc_data *data;
929 if (host->flags & SDHCI_REQ_USE_DMA) {
930 if (host->flags & SDHCI_USE_ADMA)
931 sdhci_adma_table_post(host, data);
933 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
934 data->sg_len, (data->flags & MMC_DATA_READ) ?
935 DMA_FROM_DEVICE : DMA_TO_DEVICE);
940 * The specification states that the block count register must
941 * be updated, but it does not specify at what point in the
942 * data flow. That makes the register entirely useless to read
943 * back so we have to assume that nothing made it to the card
944 * in the event of an error.
947 data->bytes_xfered = 0;
949 data->bytes_xfered = data->blksz * data->blocks;
952 * Need to send CMD12 if -
953 * a) open-ended multiblock transfer (no CMD23)
954 * b) error in multiblock transfer
961 * The controller needs a reset of internal state machines
962 * upon error conditions.
965 sdhci_reset(host, SDHCI_RESET_CMD);
966 sdhci_reset(host, SDHCI_RESET_DATA);
969 sdhci_send_command(host, data->stop);
971 tasklet_schedule(&host->finish_tasklet);
974 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
978 unsigned long timeout;
985 mask = SDHCI_CMD_INHIBIT;
986 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
987 mask |= SDHCI_DATA_INHIBIT;
989 /* We shouldn't wait for data inihibit for stop commands, even
990 though they might use busy signaling */
991 if (host->mrq->data && (cmd == host->mrq->data->stop))
992 mask &= ~SDHCI_DATA_INHIBIT;
994 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
996 pr_err("%s: Controller never released "
997 "inhibit bit(s).\n", mmc_hostname(host->mmc));
998 sdhci_dumpregs(host);
1000 tasklet_schedule(&host->finish_tasklet);
1008 if (!cmd->data && cmd->busy_timeout > 9000)
1009 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1012 mod_timer(&host->timer, timeout);
1016 sdhci_prepare_data(host, cmd);
1018 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1020 sdhci_set_transfer_mode(host, cmd);
1022 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1023 pr_err("%s: Unsupported response type!\n",
1024 mmc_hostname(host->mmc));
1025 cmd->error = -EINVAL;
1026 tasklet_schedule(&host->finish_tasklet);
1030 if (!(cmd->flags & MMC_RSP_PRESENT))
1031 flags = SDHCI_CMD_RESP_NONE;
1032 else if (cmd->flags & MMC_RSP_136)
1033 flags = SDHCI_CMD_RESP_LONG;
1034 else if (cmd->flags & MMC_RSP_BUSY)
1035 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1037 flags = SDHCI_CMD_RESP_SHORT;
1039 if (cmd->flags & MMC_RSP_CRC)
1040 flags |= SDHCI_CMD_CRC;
1041 if (cmd->flags & MMC_RSP_OPCODE)
1042 flags |= SDHCI_CMD_INDEX;
1044 /* CMD19 is special in that the Data Present Select should be set */
1045 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1046 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1047 flags |= SDHCI_CMD_DATA;
1049 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1051 EXPORT_SYMBOL_GPL(sdhci_send_command);
1053 static void sdhci_finish_command(struct sdhci_host *host)
1057 BUG_ON(host->cmd == NULL);
1059 if (host->cmd->flags & MMC_RSP_PRESENT) {
1060 if (host->cmd->flags & MMC_RSP_136) {
1061 /* CRC is stripped so we need to do some shifting. */
1062 for (i = 0;i < 4;i++) {
1063 host->cmd->resp[i] = sdhci_readl(host,
1064 SDHCI_RESPONSE + (3-i)*4) << 8;
1066 host->cmd->resp[i] |=
1068 SDHCI_RESPONSE + (3-i)*4-1);
1071 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1075 host->cmd->error = 0;
1077 /* Finished CMD23, now send actual command. */
1078 if (host->cmd == host->mrq->sbc) {
1080 sdhci_send_command(host, host->mrq->cmd);
1083 /* Processed actual command. */
1084 if (host->data && host->data_early)
1085 sdhci_finish_data(host);
1087 if (!host->cmd->data)
1088 tasklet_schedule(&host->finish_tasklet);
1094 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1096 u16 ctrl, preset = 0;
1098 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1100 switch (ctrl & SDHCI_CTRL_UHS_MASK) {
1101 case SDHCI_CTRL_UHS_SDR12:
1102 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1104 case SDHCI_CTRL_UHS_SDR25:
1105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1107 case SDHCI_CTRL_UHS_SDR50:
1108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1110 case SDHCI_CTRL_UHS_SDR104:
1111 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1113 case SDHCI_CTRL_UHS_DDR50:
1114 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1117 pr_warn("%s: Invalid UHS-I mode selected\n",
1118 mmc_hostname(host->mmc));
1119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1125 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1127 int div = 0; /* Initialized for compiler warning */
1128 int real_div = div, clk_mul = 1;
1130 unsigned long timeout;
1132 if (clock && clock == host->clock)
1135 host->mmc->actual_clock = 0;
1137 if (host->ops->set_clock) {
1138 host->ops->set_clock(host, clock);
1139 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1143 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1148 if (host->version >= SDHCI_SPEC_300) {
1149 if (sdhci_readw(host, SDHCI_HOST_CONTROL2) &
1150 SDHCI_CTRL_PRESET_VAL_ENABLE) {
1153 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1154 pre_val = sdhci_get_preset_value(host);
1155 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1156 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1157 if (host->clk_mul &&
1158 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1159 clk = SDHCI_PROG_CLOCK_MODE;
1161 clk_mul = host->clk_mul;
1163 real_div = max_t(int, 1, div << 1);
1169 * Check if the Host Controller supports Programmable Clock
1172 if (host->clk_mul) {
1173 for (div = 1; div <= 1024; div++) {
1174 if ((host->max_clk * host->clk_mul / div)
1179 * Set Programmable Clock Mode in the Clock
1182 clk = SDHCI_PROG_CLOCK_MODE;
1184 clk_mul = host->clk_mul;
1187 /* Version 3.00 divisors must be a multiple of 2. */
1188 if (host->max_clk <= clock)
1191 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1193 if ((host->max_clk / div) <= clock)
1201 /* Version 2.00 divisors must be a power of 2. */
1202 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1203 if ((host->max_clk / div) <= clock)
1212 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1214 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1215 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1216 << SDHCI_DIVIDER_HI_SHIFT;
1217 clk |= SDHCI_CLOCK_INT_EN;
1218 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1220 /* Wait max 20 ms */
1222 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1223 & SDHCI_CLOCK_INT_STABLE)) {
1225 pr_err("%s: Internal clock never "
1226 "stabilised.\n", mmc_hostname(host->mmc));
1227 sdhci_dumpregs(host);
1234 clk |= SDHCI_CLOCK_CARD_EN;
1235 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1238 host->clock = clock;
1241 static inline void sdhci_update_clock(struct sdhci_host *host)
1245 clock = host->clock;
1247 sdhci_set_clock(host, clock);
1250 static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
1254 if (power != (unsigned short)-1) {
1255 switch (1 << power) {
1256 case MMC_VDD_165_195:
1257 pwr = SDHCI_POWER_180;
1261 pwr = SDHCI_POWER_300;
1265 pwr = SDHCI_POWER_330;
1272 if (host->pwr == pwr)
1278 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1279 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1280 sdhci_runtime_pm_bus_off(host);
1285 * Spec says that we should clear the power reg before setting
1286 * a new value. Some controllers don't seem to like this though.
1288 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1289 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1292 * At least the Marvell CaFe chip gets confused if we set the voltage
1293 * and set turn on power at the same time, so set the voltage first.
1295 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1296 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1298 pwr |= SDHCI_POWER_ON;
1300 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1302 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1303 sdhci_runtime_pm_bus_on(host);
1306 * Some controllers need an extra 10ms delay of 10ms before they
1307 * can apply clock after applying power
1309 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1315 /*****************************************************************************\
1319 \*****************************************************************************/
1321 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1323 struct sdhci_host *host;
1325 unsigned long flags;
1328 host = mmc_priv(mmc);
1330 sdhci_runtime_pm_get(host);
1332 spin_lock_irqsave(&host->lock, flags);
1334 WARN_ON(host->mrq != NULL);
1336 #ifndef SDHCI_USE_LEDS_CLASS
1337 sdhci_activate_led(host);
1341 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1342 * requests if Auto-CMD12 is enabled.
1344 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1346 mrq->data->stop = NULL;
1354 * Firstly check card presence from cd-gpio. The return could
1355 * be one of the following possibilities:
1356 * negative: cd-gpio is not available
1357 * zero: cd-gpio is used, and card is removed
1358 * one: cd-gpio is used, and card is present
1360 present = mmc_gpio_get_cd(host->mmc);
1362 /* If polling, assume that the card is always present. */
1363 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1366 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1370 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1371 host->mrq->cmd->error = -ENOMEDIUM;
1372 tasklet_schedule(&host->finish_tasklet);
1376 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1378 * Check if the re-tuning timer has already expired and there
1379 * is no on-going data transfer. If so, we need to execute
1380 * tuning procedure before sending command.
1382 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1383 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1385 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1387 mmc->card->type == MMC_TYPE_MMC ?
1388 MMC_SEND_TUNING_BLOCK_HS200 :
1389 MMC_SEND_TUNING_BLOCK;
1391 /* Here we need to set the host->mrq to NULL,
1392 * in case the pending finish_tasklet
1393 * finishes it incorrectly.
1397 spin_unlock_irqrestore(&host->lock, flags);
1398 sdhci_execute_tuning(mmc, tuning_opcode);
1399 spin_lock_irqsave(&host->lock, flags);
1401 /* Restore original mmc_request structure */
1406 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1407 sdhci_send_command(host, mrq->sbc);
1409 sdhci_send_command(host, mrq->cmd);
1413 spin_unlock_irqrestore(&host->lock, flags);
1416 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1418 unsigned long flags;
1422 spin_lock_irqsave(&host->lock, flags);
1424 if (host->flags & SDHCI_DEVICE_DEAD) {
1425 spin_unlock_irqrestore(&host->lock, flags);
1426 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1427 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1432 * Reset the chip on each power off.
1433 * Should clear out any weird states.
1435 if (ios->power_mode == MMC_POWER_OFF) {
1436 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1440 if (host->version >= SDHCI_SPEC_300 &&
1441 (ios->power_mode == MMC_POWER_UP) &&
1442 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1443 sdhci_enable_preset_value(host, false);
1445 sdhci_set_clock(host, ios->clock);
1447 if (ios->power_mode == MMC_POWER_OFF)
1448 vdd_bit = sdhci_set_power(host, -1);
1450 vdd_bit = sdhci_set_power(host, ios->vdd);
1452 if (host->vmmc && vdd_bit != -1) {
1453 spin_unlock_irqrestore(&host->lock, flags);
1454 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1455 spin_lock_irqsave(&host->lock, flags);
1458 if (host->ops->platform_send_init_74_clocks)
1459 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1462 * If your platform has 8-bit width support but is not a v3 controller,
1463 * or if it requires special setup code, you should implement that in
1464 * platform_bus_width().
1466 if (host->ops->platform_bus_width) {
1467 host->ops->platform_bus_width(host, ios->bus_width);
1469 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1470 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1471 ctrl &= ~SDHCI_CTRL_4BITBUS;
1472 if (host->version >= SDHCI_SPEC_300)
1473 ctrl |= SDHCI_CTRL_8BITBUS;
1475 if (host->version >= SDHCI_SPEC_300)
1476 ctrl &= ~SDHCI_CTRL_8BITBUS;
1477 if (ios->bus_width == MMC_BUS_WIDTH_4)
1478 ctrl |= SDHCI_CTRL_4BITBUS;
1480 ctrl &= ~SDHCI_CTRL_4BITBUS;
1482 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1485 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1487 if ((ios->timing == MMC_TIMING_SD_HS ||
1488 ios->timing == MMC_TIMING_MMC_HS)
1489 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1490 ctrl |= SDHCI_CTRL_HISPD;
1492 ctrl &= ~SDHCI_CTRL_HISPD;
1494 if (host->version >= SDHCI_SPEC_300) {
1497 /* In case of UHS-I modes, set High Speed Enable */
1498 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1499 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1500 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1501 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1502 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1503 (ios->timing == MMC_TIMING_UHS_SDR25))
1504 ctrl |= SDHCI_CTRL_HISPD;
1506 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1507 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1508 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1510 * We only need to set Driver Strength if the
1511 * preset value enable is not set.
1513 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1514 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1515 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1516 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1517 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1519 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1522 * According to SDHC Spec v3.00, if the Preset Value
1523 * Enable in the Host Control 2 register is set, we
1524 * need to reset SD Clock Enable before changing High
1525 * Speed Enable to avoid generating clock gliches.
1528 /* Reset SD Clock Enable */
1529 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1530 clk &= ~SDHCI_CLOCK_CARD_EN;
1531 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1533 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1535 /* Re-enable SD Clock */
1536 sdhci_update_clock(host);
1540 /* Reset SD Clock Enable */
1541 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1542 clk &= ~SDHCI_CLOCK_CARD_EN;
1543 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1545 if (host->ops->set_uhs_signaling)
1546 host->ops->set_uhs_signaling(host, ios->timing);
1548 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1549 /* Select Bus Speed Mode for host */
1550 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1551 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1552 (ios->timing == MMC_TIMING_UHS_SDR104))
1553 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1554 else if (ios->timing == MMC_TIMING_UHS_SDR12)
1555 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1556 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1557 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1558 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1559 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1560 else if ((ios->timing == MMC_TIMING_UHS_DDR50) ||
1561 (ios->timing == MMC_TIMING_MMC_DDR52))
1562 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1563 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1566 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1567 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1568 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1569 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1570 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1571 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1574 sdhci_enable_preset_value(host, true);
1575 preset = sdhci_get_preset_value(host);
1576 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1577 >> SDHCI_PRESET_DRV_SHIFT;
1580 /* Re-enable SD Clock */
1581 sdhci_update_clock(host);
1583 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1586 * Some (ENE) controllers go apeshit on some ios operation,
1587 * signalling timeout and CRC errors even on CMD0. Resetting
1588 * it on each ios seems to solve the problem.
1590 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1591 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1594 spin_unlock_irqrestore(&host->lock, flags);
1597 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1599 struct sdhci_host *host = mmc_priv(mmc);
1601 sdhci_runtime_pm_get(host);
1602 sdhci_do_set_ios(host, ios);
1603 sdhci_runtime_pm_put(host);
1606 static int sdhci_do_get_cd(struct sdhci_host *host)
1608 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1610 if (host->flags & SDHCI_DEVICE_DEAD)
1613 /* If polling/nonremovable, assume that the card is always present. */
1614 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1615 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1618 /* Try slot gpio detect */
1619 if (!IS_ERR_VALUE(gpio_cd))
1622 /* Host native card detect */
1623 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1626 static int sdhci_get_cd(struct mmc_host *mmc)
1628 struct sdhci_host *host = mmc_priv(mmc);
1631 sdhci_runtime_pm_get(host);
1632 ret = sdhci_do_get_cd(host);
1633 sdhci_runtime_pm_put(host);
1637 static int sdhci_check_ro(struct sdhci_host *host)
1639 unsigned long flags;
1642 spin_lock_irqsave(&host->lock, flags);
1644 if (host->flags & SDHCI_DEVICE_DEAD)
1646 else if (host->ops->get_ro)
1647 is_readonly = host->ops->get_ro(host);
1649 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1650 & SDHCI_WRITE_PROTECT);
1652 spin_unlock_irqrestore(&host->lock, flags);
1654 /* This quirk needs to be replaced by a callback-function later */
1655 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1656 !is_readonly : is_readonly;
1659 #define SAMPLE_COUNT 5
1661 static int sdhci_do_get_ro(struct sdhci_host *host)
1665 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1666 return sdhci_check_ro(host);
1669 for (i = 0; i < SAMPLE_COUNT; i++) {
1670 if (sdhci_check_ro(host)) {
1671 if (++ro_count > SAMPLE_COUNT / 2)
1679 static void sdhci_hw_reset(struct mmc_host *mmc)
1681 struct sdhci_host *host = mmc_priv(mmc);
1683 if (host->ops && host->ops->hw_reset)
1684 host->ops->hw_reset(host);
1687 static int sdhci_get_ro(struct mmc_host *mmc)
1689 struct sdhci_host *host = mmc_priv(mmc);
1692 sdhci_runtime_pm_get(host);
1693 ret = sdhci_do_get_ro(host);
1694 sdhci_runtime_pm_put(host);
1698 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1700 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1702 host->ier |= SDHCI_INT_CARD_INT;
1704 host->ier &= ~SDHCI_INT_CARD_INT;
1706 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1707 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1712 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1714 struct sdhci_host *host = mmc_priv(mmc);
1715 unsigned long flags;
1717 sdhci_runtime_pm_get(host);
1719 spin_lock_irqsave(&host->lock, flags);
1721 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1723 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1725 sdhci_enable_sdio_irq_nolock(host, enable);
1726 spin_unlock_irqrestore(&host->lock, flags);
1728 sdhci_runtime_pm_put(host);
1731 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1732 struct mmc_ios *ios)
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);
1753 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
1755 pr_warning("%s: Switching to 3.3V signalling voltage "
1756 " failed\n", mmc_hostname(host->mmc));
1761 usleep_range(5000, 5500);
1763 /* 3.3V regulator output should be stable within 5 ms */
1764 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1765 if (!(ctrl & SDHCI_CTRL_VDD_180))
1768 pr_warning("%s: 3.3V regulator output did not became stable\n",
1769 mmc_hostname(host->mmc));
1772 case MMC_SIGNAL_VOLTAGE_180:
1774 ret = regulator_set_voltage(host->vqmmc,
1777 pr_warning("%s: Switching to 1.8V signalling voltage "
1778 " failed\n", mmc_hostname(host->mmc));
1784 * Enable 1.8V Signal Enable in the Host Control2
1787 ctrl |= SDHCI_CTRL_VDD_180;
1788 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1791 usleep_range(5000, 5500);
1793 /* 1.8V regulator output should be stable within 5 ms */
1794 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1795 if (ctrl & SDHCI_CTRL_VDD_180)
1798 pr_warning("%s: 1.8V regulator output did not became stable\n",
1799 mmc_hostname(host->mmc));
1802 case MMC_SIGNAL_VOLTAGE_120:
1804 ret = regulator_set_voltage(host->vqmmc, 1100000, 1300000);
1806 pr_warning("%s: Switching to 1.2V signalling voltage "
1807 " failed\n", mmc_hostname(host->mmc));
1813 /* No signal voltage switch required */
1818 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1819 struct mmc_ios *ios)
1821 struct sdhci_host *host = mmc_priv(mmc);
1824 if (host->version < SDHCI_SPEC_300)
1826 sdhci_runtime_pm_get(host);
1827 err = sdhci_do_start_signal_voltage_switch(host, ios);
1828 sdhci_runtime_pm_put(host);
1832 static int sdhci_card_busy(struct mmc_host *mmc)
1834 struct sdhci_host *host = mmc_priv(mmc);
1837 sdhci_runtime_pm_get(host);
1838 /* Check whether DAT[3:0] is 0000 */
1839 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1840 sdhci_runtime_pm_put(host);
1842 return !(present_state & SDHCI_DATA_LVL_MASK);
1845 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1847 struct sdhci_host *host;
1849 int tuning_loop_counter = MAX_TUNING_LOOP;
1850 unsigned long timeout;
1852 bool requires_tuning_nonuhs = false;
1853 unsigned long flags;
1855 host = mmc_priv(mmc);
1857 sdhci_runtime_pm_get(host);
1858 spin_lock_irqsave(&host->lock, flags);
1860 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1863 * The Host Controller needs tuning only in case of SDR104 mode
1864 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1865 * Capabilities register.
1866 * If the Host Controller supports the HS200 mode then the
1867 * tuning function has to be executed.
1869 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1870 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1871 host->flags & SDHCI_SDR104_NEEDS_TUNING))
1872 requires_tuning_nonuhs = true;
1874 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1875 requires_tuning_nonuhs)
1876 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1878 spin_unlock_irqrestore(&host->lock, flags);
1879 sdhci_runtime_pm_put(host);
1883 if (host->ops->platform_execute_tuning) {
1884 spin_unlock_irqrestore(&host->lock, flags);
1885 err = host->ops->platform_execute_tuning(host, opcode);
1886 sdhci_runtime_pm_put(host);
1890 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1893 * As per the Host Controller spec v3.00, tuning command
1894 * generates Buffer Read Ready interrupt, so enable that.
1896 * Note: The spec clearly says that when tuning sequence
1897 * is being performed, the controller does not generate
1898 * interrupts other than Buffer Read Ready interrupt. But
1899 * to make sure we don't hit a controller bug, we _only_
1900 * enable Buffer Read Ready interrupt here.
1902 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1903 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1906 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1907 * of loops reaches 40 times or a timeout of 150ms occurs.
1911 struct mmc_command cmd = {0};
1912 struct mmc_request mrq = {NULL};
1914 if (!tuning_loop_counter && !timeout)
1917 cmd.opcode = opcode;
1919 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1928 * In response to CMD19, the card sends 64 bytes of tuning
1929 * block to the Host Controller. So we set the block size
1932 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1933 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1934 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1936 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1937 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1940 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1945 * The tuning block is sent by the card to the host controller.
1946 * So we set the TRNS_READ bit in the Transfer Mode register.
1947 * This also takes care of setting DMA Enable and Multi Block
1948 * Select in the same register to 0.
1950 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1952 sdhci_send_command(host, &cmd);
1957 spin_unlock_irqrestore(&host->lock, flags);
1958 /* Wait for Buffer Read Ready interrupt */
1959 wait_event_interruptible_timeout(host->buf_ready_int,
1960 (host->tuning_done == 1),
1961 msecs_to_jiffies(50));
1962 spin_lock_irqsave(&host->lock, flags);
1964 if (!host->tuning_done) {
1965 pr_info(DRIVER_NAME ": Timeout waiting for "
1966 "Buffer Read Ready interrupt during tuning "
1967 "procedure, falling back to fixed sampling "
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1970 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1971 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1972 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1978 host->tuning_done = 0;
1980 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1981 tuning_loop_counter--;
1984 /* eMMC spec does not require a delay between tuning cycles */
1985 if (opcode == MMC_SEND_TUNING_BLOCK)
1987 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1990 * The Host Driver has exhausted the maximum number of loops allowed,
1991 * so use fixed sampling frequency.
1993 if (!tuning_loop_counter || !timeout) {
1994 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1995 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1998 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1999 pr_info(DRIVER_NAME ": Tuning procedure"
2000 " failed, falling back to fixed sampling"
2008 * If this is the very first time we are here, we start the retuning
2009 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2010 * flag won't be set, we check this condition before actually starting
2013 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2014 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2015 host->flags |= SDHCI_USING_RETUNING_TIMER;
2016 mod_timer(&host->tuning_timer, jiffies +
2017 host->tuning_count * HZ);
2018 /* Tuning mode 1 limits the maximum data length to 4MB */
2019 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2020 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2021 host->flags &= ~SDHCI_NEEDS_RETUNING;
2022 /* Reload the new initial value for timer */
2023 mod_timer(&host->tuning_timer, jiffies +
2024 host->tuning_count * HZ);
2028 * In case tuning fails, host controllers which support re-tuning can
2029 * try tuning again at a later time, when the re-tuning timer expires.
2030 * So for these controllers, we return 0. Since there might be other
2031 * controllers who do not have this capability, we return error for
2032 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2033 * a retuning timer to do the retuning for the card.
2035 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2038 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2039 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2040 spin_unlock_irqrestore(&host->lock, flags);
2041 sdhci_runtime_pm_put(host);
2047 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2051 /* Host Controller v3.00 defines preset value registers */
2052 if (host->version < SDHCI_SPEC_300)
2055 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2058 * We only enable or disable Preset Value if they are not already
2059 * enabled or disabled respectively. Otherwise, we bail out.
2061 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2062 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2063 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2064 host->flags |= SDHCI_PV_ENABLED;
2065 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2066 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2067 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2068 host->flags &= ~SDHCI_PV_ENABLED;
2072 static void sdhci_card_event(struct mmc_host *mmc)
2074 struct sdhci_host *host = mmc_priv(mmc);
2075 unsigned long flags;
2077 /* First check if client has provided their own card event */
2078 if (host->ops->card_event)
2079 host->ops->card_event(host);
2081 spin_lock_irqsave(&host->lock, flags);
2083 /* Check host->mrq first in case we are runtime suspended */
2084 if (host->mrq && !sdhci_do_get_cd(host)) {
2085 pr_err("%s: Card removed during transfer!\n",
2086 mmc_hostname(host->mmc));
2087 pr_err("%s: Resetting controller.\n",
2088 mmc_hostname(host->mmc));
2090 sdhci_reset(host, SDHCI_RESET_CMD);
2091 sdhci_reset(host, SDHCI_RESET_DATA);
2093 host->mrq->cmd->error = -ENOMEDIUM;
2094 tasklet_schedule(&host->finish_tasklet);
2097 spin_unlock_irqrestore(&host->lock, flags);
2100 static const struct mmc_host_ops sdhci_ops = {
2101 .request = sdhci_request,
2102 .set_ios = sdhci_set_ios,
2103 .get_cd = sdhci_get_cd,
2104 .get_ro = sdhci_get_ro,
2105 .hw_reset = sdhci_hw_reset,
2106 .enable_sdio_irq = sdhci_enable_sdio_irq,
2107 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2108 .execute_tuning = sdhci_execute_tuning,
2109 .card_event = sdhci_card_event,
2110 .card_busy = sdhci_card_busy,
2113 /*****************************************************************************\
2117 \*****************************************************************************/
2119 static void sdhci_tasklet_finish(unsigned long param)
2121 struct sdhci_host *host;
2122 unsigned long flags;
2123 struct mmc_request *mrq;
2125 host = (struct sdhci_host*)param;
2127 spin_lock_irqsave(&host->lock, flags);
2130 * If this tasklet gets rescheduled while running, it will
2131 * be run again afterwards but without any active request.
2134 spin_unlock_irqrestore(&host->lock, flags);
2138 del_timer(&host->timer);
2143 * The controller needs a reset of internal state machines
2144 * upon error conditions.
2146 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2147 ((mrq->cmd && mrq->cmd->error) ||
2148 (mrq->data && (mrq->data->error ||
2149 (mrq->data->stop && mrq->data->stop->error))) ||
2150 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2152 /* Some controllers need this kick or reset won't work here */
2153 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2154 /* This is to force an update */
2155 sdhci_update_clock(host);
2157 /* Spec says we should do both at the same time, but Ricoh
2158 controllers do not like that. */
2159 sdhci_reset(host, SDHCI_RESET_CMD);
2160 sdhci_reset(host, SDHCI_RESET_DATA);
2167 #ifndef SDHCI_USE_LEDS_CLASS
2168 sdhci_deactivate_led(host);
2172 spin_unlock_irqrestore(&host->lock, flags);
2174 mmc_request_done(host->mmc, mrq);
2175 sdhci_runtime_pm_put(host);
2178 static void sdhci_timeout_timer(unsigned long data)
2180 struct sdhci_host *host;
2181 unsigned long flags;
2183 host = (struct sdhci_host*)data;
2185 spin_lock_irqsave(&host->lock, flags);
2188 pr_err("%s: Timeout waiting for hardware "
2189 "interrupt.\n", mmc_hostname(host->mmc));
2190 sdhci_dumpregs(host);
2193 host->data->error = -ETIMEDOUT;
2194 sdhci_finish_data(host);
2197 host->cmd->error = -ETIMEDOUT;
2199 host->mrq->cmd->error = -ETIMEDOUT;
2201 tasklet_schedule(&host->finish_tasklet);
2206 spin_unlock_irqrestore(&host->lock, flags);
2209 static void sdhci_tuning_timer(unsigned long data)
2211 struct sdhci_host *host;
2212 unsigned long flags;
2214 host = (struct sdhci_host *)data;
2216 spin_lock_irqsave(&host->lock, flags);
2218 host->flags |= SDHCI_NEEDS_RETUNING;
2220 spin_unlock_irqrestore(&host->lock, flags);
2223 /*****************************************************************************\
2225 * Interrupt handling *
2227 \*****************************************************************************/
2229 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2231 BUG_ON(intmask == 0);
2234 pr_err("%s: Got command interrupt 0x%08x even "
2235 "though no command operation was in progress.\n",
2236 mmc_hostname(host->mmc), (unsigned)intmask);
2237 sdhci_dumpregs(host);
2241 if (intmask & SDHCI_INT_TIMEOUT)
2242 host->cmd->error = -ETIMEDOUT;
2243 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2245 host->cmd->error = -EILSEQ;
2247 if (host->cmd->error) {
2248 tasklet_schedule(&host->finish_tasklet);
2253 * The host can send and interrupt when the busy state has
2254 * ended, allowing us to wait without wasting CPU cycles.
2255 * Unfortunately this is overloaded on the "data complete"
2256 * interrupt, so we need to take some care when handling
2259 * Note: The 1.0 specification is a bit ambiguous about this
2260 * feature so there might be some problems with older
2263 if (host->cmd->flags & MMC_RSP_BUSY) {
2264 if (host->cmd->data)
2265 DBG("Cannot wait for busy signal when also "
2266 "doing a data transfer");
2267 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
2270 /* The controller does not support the end-of-busy IRQ,
2271 * fall through and take the SDHCI_INT_RESPONSE */
2274 if (intmask & SDHCI_INT_RESPONSE)
2275 sdhci_finish_command(host);
2278 #ifdef CONFIG_MMC_DEBUG
2279 static void sdhci_show_adma_error(struct sdhci_host *host)
2281 const char *name = mmc_hostname(host->mmc);
2282 u8 *desc = host->adma_desc;
2287 sdhci_dumpregs(host);
2290 dma = (__le32 *)(desc + 4);
2291 len = (__le16 *)(desc + 2);
2294 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2295 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2304 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2307 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2310 BUG_ON(intmask == 0);
2312 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2313 if (intmask & SDHCI_INT_DATA_AVAIL) {
2314 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2315 if (command == MMC_SEND_TUNING_BLOCK ||
2316 command == MMC_SEND_TUNING_BLOCK_HS200) {
2317 host->tuning_done = 1;
2318 wake_up(&host->buf_ready_int);
2325 * The "data complete" interrupt is also used to
2326 * indicate that a busy state has ended. See comment
2327 * above in sdhci_cmd_irq().
2329 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2330 if (intmask & SDHCI_INT_DATA_END) {
2331 sdhci_finish_command(host);
2336 pr_err("%s: Got data interrupt 0x%08x even "
2337 "though no data operation was in progress.\n",
2338 mmc_hostname(host->mmc), (unsigned)intmask);
2339 sdhci_dumpregs(host);
2344 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2345 host->data->error = -ETIMEDOUT;
2346 else if (intmask & SDHCI_INT_DATA_END_BIT)
2347 host->data->error = -EILSEQ;
2348 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2349 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2351 host->data->error = -EILSEQ;
2352 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2353 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2354 sdhci_show_adma_error(host);
2355 host->data->error = -EIO;
2356 if (host->ops->adma_workaround)
2357 host->ops->adma_workaround(host, intmask);
2360 if (host->data->error)
2361 sdhci_finish_data(host);
2363 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2364 sdhci_transfer_pio(host);
2367 * We currently don't do anything fancy with DMA
2368 * boundaries, but as we can't disable the feature
2369 * we need to at least restart the transfer.
2371 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2372 * should return a valid address to continue from, but as
2373 * some controllers are faulty, don't trust them.
2375 if (intmask & SDHCI_INT_DMA_END) {
2376 u32 dmastart, dmanow;
2377 dmastart = sg_dma_address(host->data->sg);
2378 dmanow = dmastart + host->data->bytes_xfered;
2380 * Force update to the next DMA block boundary.
2383 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2384 SDHCI_DEFAULT_BOUNDARY_SIZE;
2385 host->data->bytes_xfered = dmanow - dmastart;
2386 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2388 mmc_hostname(host->mmc), dmastart,
2389 host->data->bytes_xfered, dmanow);
2390 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2393 if (intmask & SDHCI_INT_DATA_END) {
2396 * Data managed to finish before the
2397 * command completed. Make sure we do
2398 * things in the proper order.
2400 host->data_early = 1;
2402 sdhci_finish_data(host);
2408 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2410 irqreturn_t result = IRQ_NONE;
2411 struct sdhci_host *host = dev_id;
2412 u32 intmask, mask, unexpected = 0;
2415 spin_lock(&host->lock);
2417 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2418 spin_unlock(&host->lock);
2422 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2423 if (!intmask || intmask == 0xffffffff) {
2429 /* Clear selected interrupts. */
2430 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2431 SDHCI_INT_BUS_POWER);
2432 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2434 DBG("*** %s got interrupt: 0x%08x\n",
2435 mmc_hostname(host->mmc), intmask);
2437 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2438 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2442 * There is a observation on i.mx esdhc. INSERT
2443 * bit will be immediately set again when it gets
2444 * cleared, if a card is inserted. We have to mask
2445 * the irq to prevent interrupt storm which will
2446 * freeze the system. And the REMOVE gets the
2449 * More testing are needed here to ensure it works
2450 * for other platforms though.
2452 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2453 SDHCI_INT_CARD_REMOVE);
2454 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2455 SDHCI_INT_CARD_INSERT;
2456 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2457 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2459 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2460 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2462 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2463 SDHCI_INT_CARD_REMOVE);
2464 result = IRQ_WAKE_THREAD;
2467 if (intmask & SDHCI_INT_CMD_MASK)
2468 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2470 if (intmask & SDHCI_INT_DATA_MASK)
2471 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2473 if (intmask & SDHCI_INT_BUS_POWER)
2474 pr_err("%s: Card is consuming too much power!\n",
2475 mmc_hostname(host->mmc));
2477 if (intmask & SDHCI_INT_CARD_INT) {
2478 sdhci_enable_sdio_irq_nolock(host, false);
2479 host->thread_isr |= SDHCI_INT_CARD_INT;
2480 result = IRQ_WAKE_THREAD;
2483 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2484 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2485 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2486 SDHCI_INT_CARD_INT);
2489 unexpected |= intmask;
2490 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2493 if (result == IRQ_NONE)
2494 result = IRQ_HANDLED;
2496 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2497 } while (intmask && --max_loops);
2499 spin_unlock(&host->lock);
2502 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2503 mmc_hostname(host->mmc), unexpected);
2504 sdhci_dumpregs(host);
2510 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2512 struct sdhci_host *host = dev_id;
2513 unsigned long flags;
2516 spin_lock_irqsave(&host->lock, flags);
2517 isr = host->thread_isr;
2518 host->thread_isr = 0;
2519 spin_unlock_irqrestore(&host->lock, flags);
2521 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2522 sdhci_card_event(host->mmc);
2523 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2526 if (isr & SDHCI_INT_CARD_INT) {
2527 sdio_run_irqs(host->mmc);
2529 spin_lock_irqsave(&host->lock, flags);
2530 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2531 sdhci_enable_sdio_irq_nolock(host, true);
2532 spin_unlock_irqrestore(&host->lock, flags);
2535 return isr ? IRQ_HANDLED : IRQ_NONE;
2538 /*****************************************************************************\
2542 \*****************************************************************************/
2545 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2548 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2549 | SDHCI_WAKE_ON_INT;
2551 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2553 /* Avoid fake wake up */
2554 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2555 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2556 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2558 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2560 void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2563 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2564 | SDHCI_WAKE_ON_INT;
2566 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2568 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2570 EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
2572 int sdhci_suspend_host(struct sdhci_host *host)
2574 if (host->ops->platform_suspend)
2575 host->ops->platform_suspend(host);
2577 sdhci_disable_card_detection(host);
2579 /* Disable tuning since we are suspending */
2580 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2581 del_timer_sync(&host->tuning_timer);
2582 host->flags &= ~SDHCI_NEEDS_RETUNING;
2585 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2587 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2588 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2589 free_irq(host->irq, host);
2591 sdhci_enable_irq_wakeups(host);
2592 enable_irq_wake(host->irq);
2597 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2599 int sdhci_resume_host(struct sdhci_host *host)
2603 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2604 if (host->ops->enable_dma)
2605 host->ops->enable_dma(host);
2608 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2609 ret = request_threaded_irq(host->irq, sdhci_irq,
2610 sdhci_thread_irq, IRQF_SHARED,
2611 mmc_hostname(host->mmc), host);
2615 sdhci_disable_irq_wakeups(host);
2616 disable_irq_wake(host->irq);
2619 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2620 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2621 /* Card keeps power but host controller does not */
2622 sdhci_init(host, 0);
2625 sdhci_do_set_ios(host, &host->mmc->ios);
2627 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2631 sdhci_enable_card_detection(host);
2633 if (host->ops->platform_resume)
2634 host->ops->platform_resume(host);
2636 /* Set the re-tuning expiration flag */
2637 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2638 host->flags |= SDHCI_NEEDS_RETUNING;
2643 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2644 #endif /* CONFIG_PM */
2646 #ifdef CONFIG_PM_RUNTIME
2648 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2650 return pm_runtime_get_sync(host->mmc->parent);
2653 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2655 pm_runtime_mark_last_busy(host->mmc->parent);
2656 return pm_runtime_put_autosuspend(host->mmc->parent);
2659 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2661 if (host->runtime_suspended || host->bus_on)
2663 host->bus_on = true;
2664 pm_runtime_get_noresume(host->mmc->parent);
2667 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2669 if (host->runtime_suspended || !host->bus_on)
2671 host->bus_on = false;
2672 pm_runtime_put_noidle(host->mmc->parent);
2675 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2677 unsigned long flags;
2680 /* Disable tuning since we are suspending */
2681 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2682 del_timer_sync(&host->tuning_timer);
2683 host->flags &= ~SDHCI_NEEDS_RETUNING;
2686 spin_lock_irqsave(&host->lock, flags);
2687 host->ier &= SDHCI_INT_CARD_INT;
2688 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2689 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2690 spin_unlock_irqrestore(&host->lock, flags);
2692 synchronize_hardirq(host->irq);
2694 spin_lock_irqsave(&host->lock, flags);
2695 host->runtime_suspended = true;
2696 spin_unlock_irqrestore(&host->lock, flags);
2700 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2702 int sdhci_runtime_resume_host(struct sdhci_host *host)
2704 unsigned long flags;
2705 int ret = 0, host_flags = host->flags;
2707 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2708 if (host->ops->enable_dma)
2709 host->ops->enable_dma(host);
2712 sdhci_init(host, 0);
2714 /* Force clock and power re-program */
2717 sdhci_do_set_ios(host, &host->mmc->ios);
2719 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2720 if ((host_flags & SDHCI_PV_ENABLED) &&
2721 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2722 spin_lock_irqsave(&host->lock, flags);
2723 sdhci_enable_preset_value(host, true);
2724 spin_unlock_irqrestore(&host->lock, flags);
2727 /* Set the re-tuning expiration flag */
2728 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2729 host->flags |= SDHCI_NEEDS_RETUNING;
2731 spin_lock_irqsave(&host->lock, flags);
2733 host->runtime_suspended = false;
2735 /* Enable SDIO IRQ */
2736 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2737 sdhci_enable_sdio_irq_nolock(host, true);
2739 /* Enable Card Detection */
2740 sdhci_enable_card_detection(host);
2742 spin_unlock_irqrestore(&host->lock, flags);
2746 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2750 /*****************************************************************************\
2752 * Device allocation/registration *
2754 \*****************************************************************************/
2756 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2759 struct mmc_host *mmc;
2760 struct sdhci_host *host;
2762 WARN_ON(dev == NULL);
2764 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2766 return ERR_PTR(-ENOMEM);
2768 host = mmc_priv(mmc);
2774 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2776 int sdhci_add_host(struct sdhci_host *host)
2778 struct mmc_host *mmc;
2779 u32 caps[2] = {0, 0};
2780 u32 max_current_caps;
2781 unsigned int ocr_avail;
2784 WARN_ON(host == NULL);
2791 host->quirks = debug_quirks;
2793 host->quirks2 = debug_quirks2;
2795 sdhci_reset(host, SDHCI_RESET_ALL);
2797 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2798 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2799 >> SDHCI_SPEC_VER_SHIFT;
2800 if (host->version > SDHCI_SPEC_300) {
2801 pr_err("%s: Unknown controller version (%d). "
2802 "You may experience problems.\n", mmc_hostname(mmc),
2806 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2807 sdhci_readl(host, SDHCI_CAPABILITIES);
2809 if (host->version >= SDHCI_SPEC_300)
2810 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2812 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2814 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2815 host->flags |= SDHCI_USE_SDMA;
2816 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2817 DBG("Controller doesn't have SDMA capability\n");
2819 host->flags |= SDHCI_USE_SDMA;
2821 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2822 (host->flags & SDHCI_USE_SDMA)) {
2823 DBG("Disabling DMA as it is marked broken\n");
2824 host->flags &= ~SDHCI_USE_SDMA;
2827 if ((host->version >= SDHCI_SPEC_200) &&
2828 (caps[0] & SDHCI_CAN_DO_ADMA2))
2829 host->flags |= SDHCI_USE_ADMA;
2831 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2832 (host->flags & SDHCI_USE_ADMA)) {
2833 DBG("Disabling ADMA as it is marked broken\n");
2834 host->flags &= ~SDHCI_USE_ADMA;
2837 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2838 if (host->ops->enable_dma) {
2839 if (host->ops->enable_dma(host)) {
2840 pr_warning("%s: No suitable DMA "
2841 "available. Falling back to PIO.\n",
2844 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2849 if (host->flags & SDHCI_USE_ADMA) {
2851 * We need to allocate descriptors for all sg entries
2852 * (128) and potentially one alignment transfer for
2853 * each of those entries.
2855 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2856 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2857 if (!host->adma_desc || !host->align_buffer) {
2858 kfree(host->adma_desc);
2859 kfree(host->align_buffer);
2860 pr_warning("%s: Unable to allocate ADMA "
2861 "buffers. Falling back to standard DMA.\n",
2863 host->flags &= ~SDHCI_USE_ADMA;
2868 * If we use DMA, then it's up to the caller to set the DMA
2869 * mask, but PIO does not need the hw shim so we set a new
2870 * mask here in that case.
2872 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2873 host->dma_mask = DMA_BIT_MASK(64);
2874 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2877 if (host->version >= SDHCI_SPEC_300)
2878 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
2879 >> SDHCI_CLOCK_BASE_SHIFT;
2881 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
2882 >> SDHCI_CLOCK_BASE_SHIFT;
2884 host->max_clk *= 1000000;
2885 if (host->max_clk == 0 || host->quirks &
2886 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
2887 if (!host->ops->get_max_clock) {
2888 pr_err("%s: Hardware doesn't specify base clock "
2889 "frequency.\n", mmc_hostname(mmc));
2892 host->max_clk = host->ops->get_max_clock(host);
2896 * In case of Host Controller v3.00, find out whether clock
2897 * multiplier is supported.
2899 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2900 SDHCI_CLOCK_MUL_SHIFT;
2903 * In case the value in Clock Multiplier is 0, then programmable
2904 * clock mode is not supported, otherwise the actual clock
2905 * multiplier is one more than the value of Clock Multiplier
2906 * in the Capabilities Register.
2912 * Set host parameters.
2914 mmc->ops = &sdhci_ops;
2915 mmc->f_max = host->max_clk;
2916 if (host->ops->get_min_clock)
2917 mmc->f_min = host->ops->get_min_clock(host);
2918 else if (host->version >= SDHCI_SPEC_300) {
2919 if (host->clk_mul) {
2920 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2921 mmc->f_max = host->max_clk * host->clk_mul;
2923 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2925 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
2928 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2929 if (host->timeout_clk == 0) {
2930 if (host->ops->get_timeout_clock) {
2931 host->timeout_clk = host->ops->get_timeout_clock(host);
2932 } else if (!(host->quirks &
2933 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2934 pr_err("%s: Hardware doesn't specify timeout clock "
2935 "frequency.\n", mmc_hostname(mmc));
2939 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2940 host->timeout_clk *= 1000;
2942 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2943 host->timeout_clk = mmc->f_max / 1000;
2945 mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
2947 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2948 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2950 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2951 host->flags |= SDHCI_AUTO_CMD12;
2953 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2954 if ((host->version >= SDHCI_SPEC_300) &&
2955 ((host->flags & SDHCI_USE_ADMA) ||
2956 !(host->flags & SDHCI_USE_SDMA))) {
2957 host->flags |= SDHCI_AUTO_CMD23;
2958 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2960 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2964 * A controller may support 8-bit width, but the board itself
2965 * might not have the pins brought out. Boards that support
2966 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2967 * their platform code before calling sdhci_add_host(), and we
2968 * won't assume 8-bit width for hosts without that CAP.
2970 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
2971 mmc->caps |= MMC_CAP_4_BIT_DATA;
2973 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2974 mmc->caps &= ~MMC_CAP_CMD23;
2976 if (caps[0] & SDHCI_CAN_DO_HISPD)
2977 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2979 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2980 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
2981 mmc->caps |= MMC_CAP_NEEDS_POLL;
2983 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2984 host->vqmmc = regulator_get_optional(mmc_dev(mmc), "vqmmc");
2985 if (IS_ERR_OR_NULL(host->vqmmc)) {
2986 if (PTR_ERR(host->vqmmc) < 0) {
2987 pr_info("%s: no vqmmc regulator found\n",
2992 ret = regulator_enable(host->vqmmc);
2993 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2995 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2996 SDHCI_SUPPORT_SDR50 |
2997 SDHCI_SUPPORT_DDR50);
2999 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3000 mmc_hostname(mmc), ret);
3005 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3006 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3007 SDHCI_SUPPORT_DDR50);
3009 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3010 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3011 SDHCI_SUPPORT_DDR50))
3012 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3014 /* SDR104 supports also implies SDR50 support */
3015 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3016 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3017 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3018 * field can be promoted to support HS200.
3020 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3021 mmc->caps2 |= MMC_CAP2_HS200;
3022 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3023 mmc->caps |= MMC_CAP_UHS_SDR50;
3025 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3026 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3027 mmc->caps |= MMC_CAP_UHS_DDR50;
3029 /* Does the host need tuning for SDR50? */
3030 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3031 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3033 /* Does the host need tuning for SDR104 / HS200? */
3034 if (mmc->caps2 & MMC_CAP2_HS200)
3035 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3037 /* Driver Type(s) (A, C, D) supported by the host */
3038 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3039 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3040 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3041 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3042 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3043 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3045 /* Initial value for re-tuning timer count */
3046 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3047 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3050 * In case Re-tuning Timer is not disabled, the actual value of
3051 * re-tuning timer will be 2 ^ (n - 1).
3053 if (host->tuning_count)
3054 host->tuning_count = 1 << (host->tuning_count - 1);
3056 /* Re-tuning mode supported by the Host Controller */
3057 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3058 SDHCI_RETUNING_MODE_SHIFT;
3062 host->vmmc = regulator_get_optional(mmc_dev(mmc), "vmmc");
3063 if (IS_ERR_OR_NULL(host->vmmc)) {
3064 if (PTR_ERR(host->vmmc) < 0) {
3065 pr_info("%s: no vmmc regulator found\n",
3071 #ifdef CONFIG_REGULATOR
3073 * Voltage range check makes sense only if regulator reports
3074 * any voltage value.
3076 if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
3077 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
3079 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
3080 caps[0] &= ~SDHCI_CAN_VDD_330;
3081 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
3082 caps[0] &= ~SDHCI_CAN_VDD_300;
3083 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
3085 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
3086 caps[0] &= ~SDHCI_CAN_VDD_180;
3088 #endif /* CONFIG_REGULATOR */
3091 * According to SD Host Controller spec v3.00, if the Host System
3092 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3093 * the value is meaningful only if Voltage Support in the Capabilities
3094 * register is set. The actual current value is 4 times the register
3097 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3098 if (!max_current_caps && host->vmmc) {
3099 u32 curr = regulator_get_current_limit(host->vmmc);
3102 /* convert to SDHCI_MAX_CURRENT format */
3103 curr = curr/1000; /* convert to mA */
3104 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3106 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3108 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3109 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3110 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3114 if (caps[0] & SDHCI_CAN_VDD_330) {
3115 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3117 mmc->max_current_330 = ((max_current_caps &
3118 SDHCI_MAX_CURRENT_330_MASK) >>
3119 SDHCI_MAX_CURRENT_330_SHIFT) *
3120 SDHCI_MAX_CURRENT_MULTIPLIER;
3122 if (caps[0] & SDHCI_CAN_VDD_300) {
3123 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3125 mmc->max_current_300 = ((max_current_caps &
3126 SDHCI_MAX_CURRENT_300_MASK) >>
3127 SDHCI_MAX_CURRENT_300_SHIFT) *
3128 SDHCI_MAX_CURRENT_MULTIPLIER;
3130 if (caps[0] & SDHCI_CAN_VDD_180) {
3131 ocr_avail |= MMC_VDD_165_195;
3133 mmc->max_current_180 = ((max_current_caps &
3134 SDHCI_MAX_CURRENT_180_MASK) >>
3135 SDHCI_MAX_CURRENT_180_SHIFT) *
3136 SDHCI_MAX_CURRENT_MULTIPLIER;
3140 ocr_avail = host->ocr_mask;
3142 mmc->ocr_avail = ocr_avail;
3143 mmc->ocr_avail_sdio = ocr_avail;
3144 if (host->ocr_avail_sdio)
3145 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3146 mmc->ocr_avail_sd = ocr_avail;
3147 if (host->ocr_avail_sd)
3148 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3149 else /* normal SD controllers don't support 1.8V */
3150 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3151 mmc->ocr_avail_mmc = ocr_avail;
3152 if (host->ocr_avail_mmc)
3153 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3155 if (mmc->ocr_avail == 0) {
3156 pr_err("%s: Hardware doesn't report any "
3157 "support voltages.\n", mmc_hostname(mmc));
3161 spin_lock_init(&host->lock);
3164 * Maximum number of segments. Depends on if the hardware
3165 * can do scatter/gather or not.
3167 if (host->flags & SDHCI_USE_ADMA)
3168 mmc->max_segs = 128;
3169 else if (host->flags & SDHCI_USE_SDMA)
3172 mmc->max_segs = 128;
3175 * Maximum number of sectors in one transfer. Limited by DMA boundary
3178 mmc->max_req_size = 524288;
3181 * Maximum segment size. Could be one segment with the maximum number
3182 * of bytes. When doing hardware scatter/gather, each entry cannot
3183 * be larger than 64 KiB though.
3185 if (host->flags & SDHCI_USE_ADMA) {
3186 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3187 mmc->max_seg_size = 65535;
3189 mmc->max_seg_size = 65536;
3191 mmc->max_seg_size = mmc->max_req_size;
3195 * Maximum block size. This varies from controller to controller and
3196 * is specified in the capabilities register.
3198 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3199 mmc->max_blk_size = 2;
3201 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3202 SDHCI_MAX_BLOCK_SHIFT;
3203 if (mmc->max_blk_size >= 3) {
3204 pr_warning("%s: Invalid maximum block size, "
3205 "assuming 512 bytes\n", mmc_hostname(mmc));
3206 mmc->max_blk_size = 0;
3210 mmc->max_blk_size = 512 << mmc->max_blk_size;
3213 * Maximum block count.
3215 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3220 tasklet_init(&host->finish_tasklet,
3221 sdhci_tasklet_finish, (unsigned long)host);
3223 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3225 if (host->version >= SDHCI_SPEC_300) {
3226 init_waitqueue_head(&host->buf_ready_int);
3228 /* Initialize re-tuning timer */
3229 init_timer(&host->tuning_timer);
3230 host->tuning_timer.data = (unsigned long)host;
3231 host->tuning_timer.function = sdhci_tuning_timer;
3234 sdhci_init(host, 0);
3236 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3237 IRQF_SHARED, mmc_hostname(mmc), host);
3239 pr_err("%s: Failed to request IRQ %d: %d\n",
3240 mmc_hostname(mmc), host->irq, ret);
3244 #ifdef CONFIG_MMC_DEBUG
3245 sdhci_dumpregs(host);
3248 #ifdef SDHCI_USE_LEDS_CLASS
3249 snprintf(host->led_name, sizeof(host->led_name),
3250 "%s::", mmc_hostname(mmc));
3251 host->led.name = host->led_name;
3252 host->led.brightness = LED_OFF;
3253 host->led.default_trigger = mmc_hostname(mmc);
3254 host->led.brightness_set = sdhci_led_control;
3256 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3258 pr_err("%s: Failed to register LED device: %d\n",
3259 mmc_hostname(mmc), ret);
3268 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3269 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3270 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3271 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3273 sdhci_enable_card_detection(host);
3277 #ifdef SDHCI_USE_LEDS_CLASS
3279 sdhci_reset(host, SDHCI_RESET_ALL);
3280 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3281 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3282 free_irq(host->irq, host);
3285 tasklet_kill(&host->finish_tasklet);
3290 EXPORT_SYMBOL_GPL(sdhci_add_host);
3292 void sdhci_remove_host(struct sdhci_host *host, int dead)
3294 unsigned long flags;
3297 spin_lock_irqsave(&host->lock, flags);
3299 host->flags |= SDHCI_DEVICE_DEAD;
3302 pr_err("%s: Controller removed during "
3303 " transfer!\n", mmc_hostname(host->mmc));
3305 host->mrq->cmd->error = -ENOMEDIUM;
3306 tasklet_schedule(&host->finish_tasklet);
3309 spin_unlock_irqrestore(&host->lock, flags);
3312 sdhci_disable_card_detection(host);
3314 mmc_remove_host(host->mmc);
3316 #ifdef SDHCI_USE_LEDS_CLASS
3317 led_classdev_unregister(&host->led);
3321 sdhci_reset(host, SDHCI_RESET_ALL);
3323 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3324 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3325 free_irq(host->irq, host);
3327 del_timer_sync(&host->timer);
3329 tasklet_kill(&host->finish_tasklet);
3332 regulator_disable(host->vmmc);
3333 regulator_put(host->vmmc);
3337 regulator_disable(host->vqmmc);
3338 regulator_put(host->vqmmc);
3341 kfree(host->adma_desc);
3342 kfree(host->align_buffer);
3344 host->adma_desc = NULL;
3345 host->align_buffer = NULL;
3348 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3350 void sdhci_free_host(struct sdhci_host *host)
3352 mmc_free_host(host->mmc);
3355 EXPORT_SYMBOL_GPL(sdhci_free_host);
3357 /*****************************************************************************\
3359 * Driver init/exit *
3361 \*****************************************************************************/
3363 static int __init sdhci_drv_init(void)
3366 ": Secure Digital Host Controller Interface driver\n");
3367 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3372 static void __exit sdhci_drv_exit(void)
3376 module_init(sdhci_drv_init);
3377 module_exit(sdhci_drv_exit);
3379 module_param(debug_quirks, uint, 0444);
3380 module_param(debug_quirks2, uint, 0444);
3382 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3383 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3384 MODULE_LICENSE("GPL");
3386 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3387 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");