2 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
4 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * Thanks to the following companies for their support:
13 * - JMicron (hardware and technical support)
16 #include <linux/delay.h>
17 #include <linux/highmem.h>
19 #include <linux/module.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/slab.h>
22 #include <linux/scatterlist.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/leds.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/host.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/slot-gpio.h>
35 #define DRIVER_NAME "sdhci"
37 #define DBG(f, x...) \
38 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
40 #if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42 #define SDHCI_USE_LEDS_CLASS
45 #define MAX_TUNING_LOOP 40
47 #define ADMA_SIZE ((128 * 2 + 1) * 4)
49 static unsigned int debug_quirks = 0;
50 static unsigned int debug_quirks2;
52 static void sdhci_finish_data(struct sdhci_host *);
54 static void sdhci_finish_command(struct sdhci_host *);
55 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
56 static void sdhci_tuning_timer(unsigned long data);
57 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
59 #ifdef CONFIG_PM_RUNTIME
60 static int sdhci_runtime_pm_get(struct sdhci_host *host);
61 static int sdhci_runtime_pm_put(struct sdhci_host *host);
62 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
63 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
65 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
69 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
73 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
76 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
81 static void sdhci_dumpregs(struct sdhci_host *host)
83 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
84 mmc_hostname(host->mmc));
86 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
87 sdhci_readl(host, SDHCI_DMA_ADDRESS),
88 sdhci_readw(host, SDHCI_HOST_VERSION));
89 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
90 sdhci_readw(host, SDHCI_BLOCK_SIZE),
91 sdhci_readw(host, SDHCI_BLOCK_COUNT));
92 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
93 sdhci_readl(host, SDHCI_ARGUMENT),
94 sdhci_readw(host, SDHCI_TRANSFER_MODE));
95 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
96 sdhci_readl(host, SDHCI_PRESENT_STATE),
97 sdhci_readb(host, SDHCI_HOST_CONTROL));
98 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
99 sdhci_readb(host, SDHCI_POWER_CONTROL),
100 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
101 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
102 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
103 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
104 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
105 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
106 sdhci_readl(host, SDHCI_INT_STATUS));
107 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
108 sdhci_readl(host, SDHCI_INT_ENABLE),
109 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
110 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
111 sdhci_readw(host, SDHCI_ACMD12_ERR),
112 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
113 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
114 sdhci_readl(host, SDHCI_CAPABILITIES),
115 sdhci_readl(host, SDHCI_CAPABILITIES_1));
116 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
117 sdhci_readw(host, SDHCI_COMMAND),
118 sdhci_readl(host, SDHCI_MAX_CURRENT));
119 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
120 sdhci_readw(host, SDHCI_HOST_CONTROL2));
122 if (host->flags & SDHCI_USE_ADMA)
123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127 pr_debug(DRIVER_NAME ": ===========================================\n");
130 /*****************************************************************************\
132 * Low level functions *
134 \*****************************************************************************/
136 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
140 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
141 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
145 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
148 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
149 SDHCI_INT_CARD_INSERT;
151 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
154 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
155 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
158 static void sdhci_enable_card_detection(struct sdhci_host *host)
160 sdhci_set_card_detection(host, true);
163 static void sdhci_disable_card_detection(struct sdhci_host *host)
165 sdhci_set_card_detection(host, false);
168 void sdhci_reset(struct sdhci_host *host, u8 mask)
170 unsigned long timeout;
172 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
174 if (mask & SDHCI_RESET_ALL) {
176 /* Reset-all turns off SD Bus Power */
177 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
178 sdhci_runtime_pm_bus_off(host);
181 /* Wait max 100 ms */
184 /* hw clears the bit when it's done */
185 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
187 pr_err("%s: Reset 0x%x never completed.\n",
188 mmc_hostname(host->mmc), (int)mask);
189 sdhci_dumpregs(host);
196 EXPORT_SYMBOL_GPL(sdhci_reset);
198 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
200 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
201 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
206 host->ops->reset(host, mask);
208 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
209 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
210 host->ops->enable_dma(host);
214 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
216 static void sdhci_init(struct sdhci_host *host, int soft)
219 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
221 sdhci_do_reset(host, SDHCI_RESET_ALL);
223 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
224 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
225 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
226 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
229 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
230 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
233 /* force clock reconfiguration */
235 sdhci_set_ios(host->mmc, &host->mmc->ios);
239 static void sdhci_reinit(struct sdhci_host *host)
243 * Retuning stuffs are affected by different cards inserted and only
244 * applicable to UHS-I cards. So reset these fields to their initial
245 * value when card is removed.
247 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
248 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
250 del_timer_sync(&host->tuning_timer);
251 host->flags &= ~SDHCI_NEEDS_RETUNING;
252 host->mmc->max_blk_count =
253 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
255 sdhci_enable_card_detection(host);
258 static void sdhci_activate_led(struct sdhci_host *host)
262 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
263 ctrl |= SDHCI_CTRL_LED;
264 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
267 static void sdhci_deactivate_led(struct sdhci_host *host)
271 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
272 ctrl &= ~SDHCI_CTRL_LED;
273 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
276 #ifdef SDHCI_USE_LEDS_CLASS
277 static void sdhci_led_control(struct led_classdev *led,
278 enum led_brightness brightness)
280 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
283 spin_lock_irqsave(&host->lock, flags);
285 if (host->runtime_suspended)
288 if (brightness == LED_OFF)
289 sdhci_deactivate_led(host);
291 sdhci_activate_led(host);
293 spin_unlock_irqrestore(&host->lock, flags);
297 /*****************************************************************************\
301 \*****************************************************************************/
303 static void sdhci_read_block_pio(struct sdhci_host *host)
306 size_t blksize, len, chunk;
307 u32 uninitialized_var(scratch);
310 DBG("PIO reading\n");
312 blksize = host->data->blksz;
315 local_irq_save(flags);
318 if (!sg_miter_next(&host->sg_miter))
321 len = min(host->sg_miter.length, blksize);
324 host->sg_miter.consumed = len;
326 buf = host->sg_miter.addr;
330 scratch = sdhci_readl(host, SDHCI_BUFFER);
334 *buf = scratch & 0xFF;
343 sg_miter_stop(&host->sg_miter);
345 local_irq_restore(flags);
348 static void sdhci_write_block_pio(struct sdhci_host *host)
351 size_t blksize, len, chunk;
355 DBG("PIO writing\n");
357 blksize = host->data->blksz;
361 local_irq_save(flags);
364 if (!sg_miter_next(&host->sg_miter))
367 len = min(host->sg_miter.length, blksize);
370 host->sg_miter.consumed = len;
372 buf = host->sg_miter.addr;
375 scratch |= (u32)*buf << (chunk * 8);
381 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
382 sdhci_writel(host, scratch, SDHCI_BUFFER);
389 sg_miter_stop(&host->sg_miter);
391 local_irq_restore(flags);
394 static void sdhci_transfer_pio(struct sdhci_host *host)
400 if (host->blocks == 0)
403 if (host->data->flags & MMC_DATA_READ)
404 mask = SDHCI_DATA_AVAILABLE;
406 mask = SDHCI_SPACE_AVAILABLE;
409 * Some controllers (JMicron JMB38x) mess up the buffer bits
410 * for transfers < 4 bytes. As long as it is just one block,
411 * we can ignore the bits.
413 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
414 (host->data->blocks == 1))
417 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
418 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
421 if (host->data->flags & MMC_DATA_READ)
422 sdhci_read_block_pio(host);
424 sdhci_write_block_pio(host);
427 if (host->blocks == 0)
431 DBG("PIO transfer complete.\n");
434 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
436 local_irq_save(*flags);
437 return kmap_atomic(sg_page(sg)) + sg->offset;
440 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
442 kunmap_atomic(buffer);
443 local_irq_restore(*flags);
446 static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
448 __le32 *dataddr = (__le32 __force *)(desc + 4);
449 __le16 *cmdlen = (__le16 __force *)desc;
451 /* SDHCI specification says ADMA descriptors should be 4 byte
452 * aligned, so using 16 or 32bit operations should be safe. */
454 cmdlen[0] = cpu_to_le16(cmd);
455 cmdlen[1] = cpu_to_le16(len);
457 dataddr[0] = cpu_to_le32(addr);
460 static int sdhci_adma_table_pre(struct sdhci_host *host,
461 struct mmc_data *data)
468 dma_addr_t align_addr;
471 struct scatterlist *sg;
477 * The spec does not specify endianness of descriptor table.
478 * We currently guess that it is LE.
481 if (data->flags & MMC_DATA_READ)
482 direction = DMA_FROM_DEVICE;
484 direction = DMA_TO_DEVICE;
486 host->align_addr = dma_map_single(mmc_dev(host->mmc),
487 host->align_buffer, 128 * 4, direction);
488 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
490 BUG_ON(host->align_addr & 0x3);
492 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
493 data->sg, data->sg_len, direction);
494 if (host->sg_count == 0)
497 desc = host->adma_desc;
498 align = host->align_buffer;
500 align_addr = host->align_addr;
502 for_each_sg(data->sg, sg, host->sg_count, i) {
503 addr = sg_dma_address(sg);
504 len = sg_dma_len(sg);
507 * The SDHCI specification states that ADMA
508 * addresses must be 32-bit aligned. If they
509 * aren't, then we use a bounce buffer for
510 * the (up to three) bytes that screw up the
513 offset = (4 - (addr & 0x3)) & 0x3;
515 if (data->flags & MMC_DATA_WRITE) {
516 buffer = sdhci_kmap_atomic(sg, &flags);
517 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
518 memcpy(align, buffer, offset);
519 sdhci_kunmap_atomic(buffer, &flags);
523 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
525 BUG_ON(offset > 65536);
539 sdhci_set_adma_desc(desc, addr, len, 0x21);
543 * If this triggers then we have a calculation bug
546 WARN_ON((desc - host->adma_desc) > ADMA_SIZE);
549 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
551 * Mark the last descriptor as the terminating descriptor
553 if (desc != host->adma_desc) {
555 desc[0] |= 0x2; /* end */
559 * Add a terminating entry.
562 /* nop, end, valid */
563 sdhci_set_adma_desc(desc, 0, 0, 0x3);
567 * Resync align buffer as we might have changed it.
569 if (data->flags & MMC_DATA_WRITE) {
570 dma_sync_single_for_device(mmc_dev(host->mmc),
571 host->align_addr, 128 * 4, direction);
577 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
583 static void sdhci_adma_table_post(struct sdhci_host *host,
584 struct mmc_data *data)
588 struct scatterlist *sg;
595 if (data->flags & MMC_DATA_READ)
596 direction = DMA_FROM_DEVICE;
598 direction = DMA_TO_DEVICE;
600 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
603 /* Do a quick scan of the SG list for any unaligned mappings */
604 has_unaligned = false;
605 for_each_sg(data->sg, sg, host->sg_count, i)
606 if (sg_dma_address(sg) & 3) {
607 has_unaligned = true;
611 if (has_unaligned && data->flags & MMC_DATA_READ) {
612 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
613 data->sg_len, direction);
615 align = host->align_buffer;
617 for_each_sg(data->sg, sg, host->sg_count, i) {
618 if (sg_dma_address(sg) & 0x3) {
619 size = 4 - (sg_dma_address(sg) & 0x3);
621 buffer = sdhci_kmap_atomic(sg, &flags);
622 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
623 memcpy(buffer, align, size);
624 sdhci_kunmap_atomic(buffer, &flags);
631 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
632 data->sg_len, direction);
635 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
638 struct mmc_data *data = cmd->data;
639 unsigned target_timeout, current_timeout;
642 * If the host controller provides us with an incorrect timeout
643 * value, just skip the check and use 0xE. The hardware may take
644 * longer to time out, but that's much better than having a too-short
647 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
650 /* Unspecified timeout, assume max */
651 if (!data && !cmd->busy_timeout)
656 target_timeout = cmd->busy_timeout * 1000;
658 target_timeout = data->timeout_ns / 1000;
660 target_timeout += data->timeout_clks / host->clock;
664 * Figure out needed cycles.
665 * We do this in steps in order to fit inside a 32 bit int.
666 * The first step is the minimum timeout, which will have a
667 * minimum resolution of 6 bits:
668 * (1) 2^13*1000 > 2^22,
669 * (2) host->timeout_clk < 2^16
674 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
675 while (current_timeout < target_timeout) {
677 current_timeout <<= 1;
683 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
684 mmc_hostname(host->mmc), count, cmd->opcode);
691 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
693 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
694 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
696 if (host->flags & SDHCI_REQ_USE_DMA)
697 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
699 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
701 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
702 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
705 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
709 struct mmc_data *data = cmd->data;
714 if (data || (cmd->flags & MMC_RSP_BUSY)) {
715 count = sdhci_calc_timeout(host, cmd);
716 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
723 BUG_ON(data->blksz * data->blocks > 524288);
724 BUG_ON(data->blksz > host->mmc->max_blk_size);
725 BUG_ON(data->blocks > 65535);
728 host->data_early = 0;
729 host->data->bytes_xfered = 0;
731 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
732 host->flags |= SDHCI_REQ_USE_DMA;
735 * FIXME: This doesn't account for merging when mapping the
738 if (host->flags & SDHCI_REQ_USE_DMA) {
740 struct scatterlist *sg;
743 if (host->flags & SDHCI_USE_ADMA) {
744 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
747 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
751 if (unlikely(broken)) {
752 for_each_sg(data->sg, sg, data->sg_len, i) {
753 if (sg->length & 0x3) {
754 DBG("Reverting to PIO because of "
755 "transfer size (%d)\n",
757 host->flags &= ~SDHCI_REQ_USE_DMA;
765 * The assumption here being that alignment is the same after
766 * translation to device address space.
768 if (host->flags & SDHCI_REQ_USE_DMA) {
770 struct scatterlist *sg;
773 if (host->flags & SDHCI_USE_ADMA) {
775 * As we use 3 byte chunks to work around
776 * alignment problems, we need to check this
779 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
782 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
786 if (unlikely(broken)) {
787 for_each_sg(data->sg, sg, data->sg_len, i) {
788 if (sg->offset & 0x3) {
789 DBG("Reverting to PIO because of "
791 host->flags &= ~SDHCI_REQ_USE_DMA;
798 if (host->flags & SDHCI_REQ_USE_DMA) {
799 if (host->flags & SDHCI_USE_ADMA) {
800 ret = sdhci_adma_table_pre(host, data);
803 * This only happens when someone fed
804 * us an invalid request.
807 host->flags &= ~SDHCI_REQ_USE_DMA;
809 sdhci_writel(host, host->adma_addr,
815 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
816 data->sg, data->sg_len,
817 (data->flags & MMC_DATA_READ) ?
822 * This only happens when someone fed
823 * us an invalid request.
826 host->flags &= ~SDHCI_REQ_USE_DMA;
828 WARN_ON(sg_cnt != 1);
829 sdhci_writel(host, sg_dma_address(data->sg),
836 * Always adjust the DMA selection as some controllers
837 * (e.g. JMicron) can't do PIO properly when the selection
840 if (host->version >= SDHCI_SPEC_200) {
841 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
842 ctrl &= ~SDHCI_CTRL_DMA_MASK;
843 if ((host->flags & SDHCI_REQ_USE_DMA) &&
844 (host->flags & SDHCI_USE_ADMA))
845 ctrl |= SDHCI_CTRL_ADMA32;
847 ctrl |= SDHCI_CTRL_SDMA;
848 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
851 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
854 flags = SG_MITER_ATOMIC;
855 if (host->data->flags & MMC_DATA_READ)
856 flags |= SG_MITER_TO_SG;
858 flags |= SG_MITER_FROM_SG;
859 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
860 host->blocks = data->blocks;
863 sdhci_set_transfer_irqs(host);
865 /* Set the DMA boundary value and block size */
866 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
867 data->blksz), SDHCI_BLOCK_SIZE);
868 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
871 static void sdhci_set_transfer_mode(struct sdhci_host *host,
872 struct mmc_command *cmd)
875 struct mmc_data *data = cmd->data;
878 /* clear Auto CMD settings for no data CMDs */
879 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
880 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
881 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
885 WARN_ON(!host->data);
887 mode = SDHCI_TRNS_BLK_CNT_EN;
888 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
889 mode |= SDHCI_TRNS_MULTI;
891 * If we are sending CMD23, CMD12 never gets sent
892 * on successful completion (so no Auto-CMD12).
894 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
895 mode |= SDHCI_TRNS_AUTO_CMD12;
896 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
897 mode |= SDHCI_TRNS_AUTO_CMD23;
898 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
902 if (data->flags & MMC_DATA_READ)
903 mode |= SDHCI_TRNS_READ;
904 if (host->flags & SDHCI_REQ_USE_DMA)
905 mode |= SDHCI_TRNS_DMA;
907 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
910 static void sdhci_finish_data(struct sdhci_host *host)
912 struct mmc_data *data;
919 if (host->flags & SDHCI_REQ_USE_DMA) {
920 if (host->flags & SDHCI_USE_ADMA)
921 sdhci_adma_table_post(host, data);
923 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
924 data->sg_len, (data->flags & MMC_DATA_READ) ?
925 DMA_FROM_DEVICE : DMA_TO_DEVICE);
930 * The specification states that the block count register must
931 * be updated, but it does not specify at what point in the
932 * data flow. That makes the register entirely useless to read
933 * back so we have to assume that nothing made it to the card
934 * in the event of an error.
937 data->bytes_xfered = 0;
939 data->bytes_xfered = data->blksz * data->blocks;
942 * Need to send CMD12 if -
943 * a) open-ended multiblock transfer (no CMD23)
944 * b) error in multiblock transfer
951 * The controller needs a reset of internal state machines
952 * upon error conditions.
955 sdhci_do_reset(host, SDHCI_RESET_CMD);
956 sdhci_do_reset(host, SDHCI_RESET_DATA);
959 sdhci_send_command(host, data->stop);
961 tasklet_schedule(&host->finish_tasklet);
964 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
968 unsigned long timeout;
975 mask = SDHCI_CMD_INHIBIT;
976 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
977 mask |= SDHCI_DATA_INHIBIT;
979 /* We shouldn't wait for data inihibit for stop commands, even
980 though they might use busy signaling */
981 if (host->mrq->data && (cmd == host->mrq->data->stop))
982 mask &= ~SDHCI_DATA_INHIBIT;
984 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
986 pr_err("%s: Controller never released "
987 "inhibit bit(s).\n", mmc_hostname(host->mmc));
988 sdhci_dumpregs(host);
990 tasklet_schedule(&host->finish_tasklet);
998 if (!cmd->data && cmd->busy_timeout > 9000)
999 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1002 mod_timer(&host->timer, timeout);
1006 sdhci_prepare_data(host, cmd);
1008 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1010 sdhci_set_transfer_mode(host, cmd);
1012 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1013 pr_err("%s: Unsupported response type!\n",
1014 mmc_hostname(host->mmc));
1015 cmd->error = -EINVAL;
1016 tasklet_schedule(&host->finish_tasklet);
1020 if (!(cmd->flags & MMC_RSP_PRESENT))
1021 flags = SDHCI_CMD_RESP_NONE;
1022 else if (cmd->flags & MMC_RSP_136)
1023 flags = SDHCI_CMD_RESP_LONG;
1024 else if (cmd->flags & MMC_RSP_BUSY)
1025 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1027 flags = SDHCI_CMD_RESP_SHORT;
1029 if (cmd->flags & MMC_RSP_CRC)
1030 flags |= SDHCI_CMD_CRC;
1031 if (cmd->flags & MMC_RSP_OPCODE)
1032 flags |= SDHCI_CMD_INDEX;
1034 /* CMD19 is special in that the Data Present Select should be set */
1035 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1036 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1037 flags |= SDHCI_CMD_DATA;
1039 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1041 EXPORT_SYMBOL_GPL(sdhci_send_command);
1043 static void sdhci_finish_command(struct sdhci_host *host)
1047 BUG_ON(host->cmd == NULL);
1049 if (host->cmd->flags & MMC_RSP_PRESENT) {
1050 if (host->cmd->flags & MMC_RSP_136) {
1051 /* CRC is stripped so we need to do some shifting. */
1052 for (i = 0;i < 4;i++) {
1053 host->cmd->resp[i] = sdhci_readl(host,
1054 SDHCI_RESPONSE + (3-i)*4) << 8;
1056 host->cmd->resp[i] |=
1058 SDHCI_RESPONSE + (3-i)*4-1);
1061 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1065 host->cmd->error = 0;
1067 /* Finished CMD23, now send actual command. */
1068 if (host->cmd == host->mrq->sbc) {
1070 sdhci_send_command(host, host->mrq->cmd);
1073 /* Processed actual command. */
1074 if (host->data && host->data_early)
1075 sdhci_finish_data(host);
1077 if (!host->cmd->data)
1078 tasklet_schedule(&host->finish_tasklet);
1084 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1086 u16 ctrl, preset = 0;
1088 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1090 switch (ctrl & SDHCI_CTRL_UHS_MASK) {
1091 case SDHCI_CTRL_UHS_SDR12:
1092 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1094 case SDHCI_CTRL_UHS_SDR25:
1095 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1097 case SDHCI_CTRL_UHS_SDR50:
1098 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1100 case SDHCI_CTRL_UHS_SDR104:
1101 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1103 case SDHCI_CTRL_UHS_DDR50:
1104 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1107 pr_warn("%s: Invalid UHS-I mode selected\n",
1108 mmc_hostname(host->mmc));
1109 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1115 static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1117 int div = 0; /* Initialized for compiler warning */
1118 int real_div = div, clk_mul = 1;
1120 unsigned long timeout;
1122 if (clock && clock == host->clock)
1125 host->mmc->actual_clock = 0;
1127 if (host->ops->set_clock) {
1128 host->ops->set_clock(host, clock);
1129 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1133 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1138 if (host->version >= SDHCI_SPEC_300) {
1139 if (sdhci_readw(host, SDHCI_HOST_CONTROL2) &
1140 SDHCI_CTRL_PRESET_VAL_ENABLE) {
1143 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1144 pre_val = sdhci_get_preset_value(host);
1145 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1146 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1147 if (host->clk_mul &&
1148 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1149 clk = SDHCI_PROG_CLOCK_MODE;
1151 clk_mul = host->clk_mul;
1153 real_div = max_t(int, 1, div << 1);
1159 * Check if the Host Controller supports Programmable Clock
1162 if (host->clk_mul) {
1163 for (div = 1; div <= 1024; div++) {
1164 if ((host->max_clk * host->clk_mul / div)
1169 * Set Programmable Clock Mode in the Clock
1172 clk = SDHCI_PROG_CLOCK_MODE;
1174 clk_mul = host->clk_mul;
1177 /* Version 3.00 divisors must be a multiple of 2. */
1178 if (host->max_clk <= clock)
1181 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1183 if ((host->max_clk / div) <= clock)
1191 /* Version 2.00 divisors must be a power of 2. */
1192 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1193 if ((host->max_clk / div) <= clock)
1202 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1204 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1205 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1206 << SDHCI_DIVIDER_HI_SHIFT;
1207 clk |= SDHCI_CLOCK_INT_EN;
1208 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1210 /* Wait max 20 ms */
1212 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1213 & SDHCI_CLOCK_INT_STABLE)) {
1215 pr_err("%s: Internal clock never "
1216 "stabilised.\n", mmc_hostname(host->mmc));
1217 sdhci_dumpregs(host);
1224 clk |= SDHCI_CLOCK_CARD_EN;
1225 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1228 host->clock = clock;
1231 static inline void sdhci_update_clock(struct sdhci_host *host)
1235 clock = host->clock;
1237 sdhci_set_clock(host, clock);
1240 static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
1244 if (power != (unsigned short)-1) {
1245 switch (1 << power) {
1246 case MMC_VDD_165_195:
1247 pwr = SDHCI_POWER_180;
1251 pwr = SDHCI_POWER_300;
1255 pwr = SDHCI_POWER_330;
1262 if (host->pwr == pwr)
1268 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1269 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1270 sdhci_runtime_pm_bus_off(host);
1275 * Spec says that we should clear the power reg before setting
1276 * a new value. Some controllers don't seem to like this though.
1278 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1279 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1282 * At least the Marvell CaFe chip gets confused if we set the voltage
1283 * and set turn on power at the same time, so set the voltage first.
1285 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1286 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1288 pwr |= SDHCI_POWER_ON;
1290 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1292 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1293 sdhci_runtime_pm_bus_on(host);
1296 * Some controllers need an extra 10ms delay of 10ms before they
1297 * can apply clock after applying power
1299 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1305 /*****************************************************************************\
1309 \*****************************************************************************/
1311 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1313 struct sdhci_host *host;
1315 unsigned long flags;
1318 host = mmc_priv(mmc);
1320 sdhci_runtime_pm_get(host);
1322 spin_lock_irqsave(&host->lock, flags);
1324 WARN_ON(host->mrq != NULL);
1326 #ifndef SDHCI_USE_LEDS_CLASS
1327 sdhci_activate_led(host);
1331 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1332 * requests if Auto-CMD12 is enabled.
1334 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1336 mrq->data->stop = NULL;
1344 * Firstly check card presence from cd-gpio. The return could
1345 * be one of the following possibilities:
1346 * negative: cd-gpio is not available
1347 * zero: cd-gpio is used, and card is removed
1348 * one: cd-gpio is used, and card is present
1350 present = mmc_gpio_get_cd(host->mmc);
1352 /* If polling, assume that the card is always present. */
1353 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1356 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1360 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1361 host->mrq->cmd->error = -ENOMEDIUM;
1362 tasklet_schedule(&host->finish_tasklet);
1366 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1368 * Check if the re-tuning timer has already expired and there
1369 * is no on-going data transfer. If so, we need to execute
1370 * tuning procedure before sending command.
1372 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1373 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1375 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1377 mmc->card->type == MMC_TYPE_MMC ?
1378 MMC_SEND_TUNING_BLOCK_HS200 :
1379 MMC_SEND_TUNING_BLOCK;
1381 /* Here we need to set the host->mrq to NULL,
1382 * in case the pending finish_tasklet
1383 * finishes it incorrectly.
1387 spin_unlock_irqrestore(&host->lock, flags);
1388 sdhci_execute_tuning(mmc, tuning_opcode);
1389 spin_lock_irqsave(&host->lock, flags);
1391 /* Restore original mmc_request structure */
1396 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1397 sdhci_send_command(host, mrq->sbc);
1399 sdhci_send_command(host, mrq->cmd);
1403 spin_unlock_irqrestore(&host->lock, flags);
1406 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1410 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1411 if (width == MMC_BUS_WIDTH_8) {
1412 ctrl &= ~SDHCI_CTRL_4BITBUS;
1413 if (host->version >= SDHCI_SPEC_300)
1414 ctrl |= SDHCI_CTRL_8BITBUS;
1416 if (host->version >= SDHCI_SPEC_300)
1417 ctrl &= ~SDHCI_CTRL_8BITBUS;
1418 if (width == MMC_BUS_WIDTH_4)
1419 ctrl |= SDHCI_CTRL_4BITBUS;
1421 ctrl &= ~SDHCI_CTRL_4BITBUS;
1423 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1425 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1427 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1429 unsigned long flags;
1433 spin_lock_irqsave(&host->lock, flags);
1435 if (host->flags & SDHCI_DEVICE_DEAD) {
1436 spin_unlock_irqrestore(&host->lock, flags);
1437 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1438 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1443 * Reset the chip on each power off.
1444 * Should clear out any weird states.
1446 if (ios->power_mode == MMC_POWER_OFF) {
1447 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1451 if (host->version >= SDHCI_SPEC_300 &&
1452 (ios->power_mode == MMC_POWER_UP) &&
1453 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1454 sdhci_enable_preset_value(host, false);
1456 sdhci_set_clock(host, ios->clock);
1458 if (ios->power_mode == MMC_POWER_OFF)
1459 vdd_bit = sdhci_set_power(host, -1);
1461 vdd_bit = sdhci_set_power(host, ios->vdd);
1463 if (host->vmmc && vdd_bit != -1) {
1464 spin_unlock_irqrestore(&host->lock, flags);
1465 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1466 spin_lock_irqsave(&host->lock, flags);
1469 if (host->ops->platform_send_init_74_clocks)
1470 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1472 host->ops->set_bus_width(host, ios->bus_width);
1474 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1476 if ((ios->timing == MMC_TIMING_SD_HS ||
1477 ios->timing == MMC_TIMING_MMC_HS)
1478 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1479 ctrl |= SDHCI_CTRL_HISPD;
1481 ctrl &= ~SDHCI_CTRL_HISPD;
1483 if (host->version >= SDHCI_SPEC_300) {
1486 /* In case of UHS-I modes, set High Speed Enable */
1487 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1488 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1489 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1490 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1491 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1492 (ios->timing == MMC_TIMING_UHS_SDR25))
1493 ctrl |= SDHCI_CTRL_HISPD;
1495 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1496 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1497 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1499 * We only need to set Driver Strength if the
1500 * preset value enable is not set.
1502 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1503 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1504 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1505 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1506 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1508 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1511 * According to SDHC Spec v3.00, if the Preset Value
1512 * Enable in the Host Control 2 register is set, we
1513 * need to reset SD Clock Enable before changing High
1514 * Speed Enable to avoid generating clock gliches.
1517 /* Reset SD Clock Enable */
1518 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1519 clk &= ~SDHCI_CLOCK_CARD_EN;
1520 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1522 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1524 /* Re-enable SD Clock */
1525 sdhci_update_clock(host);
1529 /* Reset SD Clock Enable */
1530 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1531 clk &= ~SDHCI_CLOCK_CARD_EN;
1532 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1534 if (host->ops->set_uhs_signaling)
1535 host->ops->set_uhs_signaling(host, ios->timing);
1537 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1538 /* Select Bus Speed Mode for host */
1539 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1540 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1541 (ios->timing == MMC_TIMING_UHS_SDR104))
1542 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1543 else if (ios->timing == MMC_TIMING_UHS_SDR12)
1544 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1545 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1546 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1547 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1548 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1549 else if ((ios->timing == MMC_TIMING_UHS_DDR50) ||
1550 (ios->timing == MMC_TIMING_MMC_DDR52))
1551 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1552 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1555 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1556 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1557 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1558 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1559 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1560 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1563 sdhci_enable_preset_value(host, true);
1564 preset = sdhci_get_preset_value(host);
1565 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1566 >> SDHCI_PRESET_DRV_SHIFT;
1569 /* Re-enable SD Clock */
1570 sdhci_update_clock(host);
1572 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1575 * Some (ENE) controllers go apeshit on some ios operation,
1576 * signalling timeout and CRC errors even on CMD0. Resetting
1577 * it on each ios seems to solve the problem.
1579 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1580 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1583 spin_unlock_irqrestore(&host->lock, flags);
1586 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1588 struct sdhci_host *host = mmc_priv(mmc);
1590 sdhci_runtime_pm_get(host);
1591 sdhci_do_set_ios(host, ios);
1592 sdhci_runtime_pm_put(host);
1595 static int sdhci_do_get_cd(struct sdhci_host *host)
1597 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1599 if (host->flags & SDHCI_DEVICE_DEAD)
1602 /* If polling/nonremovable, assume that the card is always present. */
1603 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1604 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1607 /* Try slot gpio detect */
1608 if (!IS_ERR_VALUE(gpio_cd))
1611 /* Host native card detect */
1612 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1615 static int sdhci_get_cd(struct mmc_host *mmc)
1617 struct sdhci_host *host = mmc_priv(mmc);
1620 sdhci_runtime_pm_get(host);
1621 ret = sdhci_do_get_cd(host);
1622 sdhci_runtime_pm_put(host);
1626 static int sdhci_check_ro(struct sdhci_host *host)
1628 unsigned long flags;
1631 spin_lock_irqsave(&host->lock, flags);
1633 if (host->flags & SDHCI_DEVICE_DEAD)
1635 else if (host->ops->get_ro)
1636 is_readonly = host->ops->get_ro(host);
1638 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1639 & SDHCI_WRITE_PROTECT);
1641 spin_unlock_irqrestore(&host->lock, flags);
1643 /* This quirk needs to be replaced by a callback-function later */
1644 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1645 !is_readonly : is_readonly;
1648 #define SAMPLE_COUNT 5
1650 static int sdhci_do_get_ro(struct sdhci_host *host)
1654 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1655 return sdhci_check_ro(host);
1658 for (i = 0; i < SAMPLE_COUNT; i++) {
1659 if (sdhci_check_ro(host)) {
1660 if (++ro_count > SAMPLE_COUNT / 2)
1668 static void sdhci_hw_reset(struct mmc_host *mmc)
1670 struct sdhci_host *host = mmc_priv(mmc);
1672 if (host->ops && host->ops->hw_reset)
1673 host->ops->hw_reset(host);
1676 static int sdhci_get_ro(struct mmc_host *mmc)
1678 struct sdhci_host *host = mmc_priv(mmc);
1681 sdhci_runtime_pm_get(host);
1682 ret = sdhci_do_get_ro(host);
1683 sdhci_runtime_pm_put(host);
1687 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1689 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1691 host->ier |= SDHCI_INT_CARD_INT;
1693 host->ier &= ~SDHCI_INT_CARD_INT;
1695 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1696 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1701 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1703 struct sdhci_host *host = mmc_priv(mmc);
1704 unsigned long flags;
1706 sdhci_runtime_pm_get(host);
1708 spin_lock_irqsave(&host->lock, flags);
1710 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1712 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1714 sdhci_enable_sdio_irq_nolock(host, enable);
1715 spin_unlock_irqrestore(&host->lock, flags);
1717 sdhci_runtime_pm_put(host);
1720 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1721 struct mmc_ios *ios)
1727 * Signal Voltage Switching is only applicable for Host Controllers
1730 if (host->version < SDHCI_SPEC_300)
1733 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1735 switch (ios->signal_voltage) {
1736 case MMC_SIGNAL_VOLTAGE_330:
1737 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1738 ctrl &= ~SDHCI_CTRL_VDD_180;
1739 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1742 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
1744 pr_warning("%s: Switching to 3.3V signalling voltage "
1745 " failed\n", mmc_hostname(host->mmc));
1750 usleep_range(5000, 5500);
1752 /* 3.3V regulator output should be stable within 5 ms */
1753 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1754 if (!(ctrl & SDHCI_CTRL_VDD_180))
1757 pr_warning("%s: 3.3V regulator output did not became stable\n",
1758 mmc_hostname(host->mmc));
1761 case MMC_SIGNAL_VOLTAGE_180:
1763 ret = regulator_set_voltage(host->vqmmc,
1766 pr_warning("%s: Switching to 1.8V signalling voltage "
1767 " failed\n", mmc_hostname(host->mmc));
1773 * Enable 1.8V Signal Enable in the Host Control2
1776 ctrl |= SDHCI_CTRL_VDD_180;
1777 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1780 usleep_range(5000, 5500);
1782 /* 1.8V regulator output should be stable within 5 ms */
1783 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1784 if (ctrl & SDHCI_CTRL_VDD_180)
1787 pr_warning("%s: 1.8V regulator output did not became stable\n",
1788 mmc_hostname(host->mmc));
1791 case MMC_SIGNAL_VOLTAGE_120:
1793 ret = regulator_set_voltage(host->vqmmc, 1100000, 1300000);
1795 pr_warning("%s: Switching to 1.2V signalling voltage "
1796 " failed\n", mmc_hostname(host->mmc));
1802 /* No signal voltage switch required */
1807 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1808 struct mmc_ios *ios)
1810 struct sdhci_host *host = mmc_priv(mmc);
1813 if (host->version < SDHCI_SPEC_300)
1815 sdhci_runtime_pm_get(host);
1816 err = sdhci_do_start_signal_voltage_switch(host, ios);
1817 sdhci_runtime_pm_put(host);
1821 static int sdhci_card_busy(struct mmc_host *mmc)
1823 struct sdhci_host *host = mmc_priv(mmc);
1826 sdhci_runtime_pm_get(host);
1827 /* Check whether DAT[3:0] is 0000 */
1828 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1829 sdhci_runtime_pm_put(host);
1831 return !(present_state & SDHCI_DATA_LVL_MASK);
1834 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1836 struct sdhci_host *host;
1838 int tuning_loop_counter = MAX_TUNING_LOOP;
1839 unsigned long timeout;
1841 bool requires_tuning_nonuhs = false;
1842 unsigned long flags;
1844 host = mmc_priv(mmc);
1846 sdhci_runtime_pm_get(host);
1847 spin_lock_irqsave(&host->lock, flags);
1849 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1852 * The Host Controller needs tuning only in case of SDR104 mode
1853 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1854 * Capabilities register.
1855 * If the Host Controller supports the HS200 mode then the
1856 * tuning function has to be executed.
1858 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1859 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1860 host->flags & SDHCI_SDR104_NEEDS_TUNING))
1861 requires_tuning_nonuhs = true;
1863 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1864 requires_tuning_nonuhs)
1865 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1867 spin_unlock_irqrestore(&host->lock, flags);
1868 sdhci_runtime_pm_put(host);
1872 if (host->ops->platform_execute_tuning) {
1873 spin_unlock_irqrestore(&host->lock, flags);
1874 err = host->ops->platform_execute_tuning(host, opcode);
1875 sdhci_runtime_pm_put(host);
1879 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1882 * As per the Host Controller spec v3.00, tuning command
1883 * generates Buffer Read Ready interrupt, so enable that.
1885 * Note: The spec clearly says that when tuning sequence
1886 * is being performed, the controller does not generate
1887 * interrupts other than Buffer Read Ready interrupt. But
1888 * to make sure we don't hit a controller bug, we _only_
1889 * enable Buffer Read Ready interrupt here.
1891 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1892 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1895 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1896 * of loops reaches 40 times or a timeout of 150ms occurs.
1900 struct mmc_command cmd = {0};
1901 struct mmc_request mrq = {NULL};
1903 if (!tuning_loop_counter && !timeout)
1906 cmd.opcode = opcode;
1908 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1917 * In response to CMD19, the card sends 64 bytes of tuning
1918 * block to the Host Controller. So we set the block size
1921 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1922 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1923 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1925 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1926 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1929 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1934 * The tuning block is sent by the card to the host controller.
1935 * So we set the TRNS_READ bit in the Transfer Mode register.
1936 * This also takes care of setting DMA Enable and Multi Block
1937 * Select in the same register to 0.
1939 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1941 sdhci_send_command(host, &cmd);
1946 spin_unlock_irqrestore(&host->lock, flags);
1947 /* Wait for Buffer Read Ready interrupt */
1948 wait_event_interruptible_timeout(host->buf_ready_int,
1949 (host->tuning_done == 1),
1950 msecs_to_jiffies(50));
1951 spin_lock_irqsave(&host->lock, flags);
1953 if (!host->tuning_done) {
1954 pr_info(DRIVER_NAME ": Timeout waiting for "
1955 "Buffer Read Ready interrupt during tuning "
1956 "procedure, falling back to fixed sampling "
1958 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1959 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1960 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1961 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1967 host->tuning_done = 0;
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1970 tuning_loop_counter--;
1973 /* eMMC spec does not require a delay between tuning cycles */
1974 if (opcode == MMC_SEND_TUNING_BLOCK)
1976 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1979 * The Host Driver has exhausted the maximum number of loops allowed,
1980 * so use fixed sampling frequency.
1982 if (!tuning_loop_counter || !timeout) {
1983 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1984 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1987 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1988 pr_info(DRIVER_NAME ": Tuning procedure"
1989 " failed, falling back to fixed sampling"
1997 * If this is the very first time we are here, we start the retuning
1998 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1999 * flag won't be set, we check this condition before actually starting
2002 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2003 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2004 host->flags |= SDHCI_USING_RETUNING_TIMER;
2005 mod_timer(&host->tuning_timer, jiffies +
2006 host->tuning_count * HZ);
2007 /* Tuning mode 1 limits the maximum data length to 4MB */
2008 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2009 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2010 host->flags &= ~SDHCI_NEEDS_RETUNING;
2011 /* Reload the new initial value for timer */
2012 mod_timer(&host->tuning_timer, jiffies +
2013 host->tuning_count * HZ);
2017 * In case tuning fails, host controllers which support re-tuning can
2018 * try tuning again at a later time, when the re-tuning timer expires.
2019 * So for these controllers, we return 0. Since there might be other
2020 * controllers who do not have this capability, we return error for
2021 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2022 * a retuning timer to do the retuning for the card.
2024 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2027 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2028 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2029 spin_unlock_irqrestore(&host->lock, flags);
2030 sdhci_runtime_pm_put(host);
2036 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2040 /* Host Controller v3.00 defines preset value registers */
2041 if (host->version < SDHCI_SPEC_300)
2044 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2047 * We only enable or disable Preset Value if they are not already
2048 * enabled or disabled respectively. Otherwise, we bail out.
2050 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2051 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2052 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2053 host->flags |= SDHCI_PV_ENABLED;
2054 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2055 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2056 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2057 host->flags &= ~SDHCI_PV_ENABLED;
2061 static void sdhci_card_event(struct mmc_host *mmc)
2063 struct sdhci_host *host = mmc_priv(mmc);
2064 unsigned long flags;
2066 /* First check if client has provided their own card event */
2067 if (host->ops->card_event)
2068 host->ops->card_event(host);
2070 spin_lock_irqsave(&host->lock, flags);
2072 /* Check host->mrq first in case we are runtime suspended */
2073 if (host->mrq && !sdhci_do_get_cd(host)) {
2074 pr_err("%s: Card removed during transfer!\n",
2075 mmc_hostname(host->mmc));
2076 pr_err("%s: Resetting controller.\n",
2077 mmc_hostname(host->mmc));
2079 sdhci_do_reset(host, SDHCI_RESET_CMD);
2080 sdhci_do_reset(host, SDHCI_RESET_DATA);
2082 host->mrq->cmd->error = -ENOMEDIUM;
2083 tasklet_schedule(&host->finish_tasklet);
2086 spin_unlock_irqrestore(&host->lock, flags);
2089 static const struct mmc_host_ops sdhci_ops = {
2090 .request = sdhci_request,
2091 .set_ios = sdhci_set_ios,
2092 .get_cd = sdhci_get_cd,
2093 .get_ro = sdhci_get_ro,
2094 .hw_reset = sdhci_hw_reset,
2095 .enable_sdio_irq = sdhci_enable_sdio_irq,
2096 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2097 .execute_tuning = sdhci_execute_tuning,
2098 .card_event = sdhci_card_event,
2099 .card_busy = sdhci_card_busy,
2102 /*****************************************************************************\
2106 \*****************************************************************************/
2108 static void sdhci_tasklet_finish(unsigned long param)
2110 struct sdhci_host *host;
2111 unsigned long flags;
2112 struct mmc_request *mrq;
2114 host = (struct sdhci_host*)param;
2116 spin_lock_irqsave(&host->lock, flags);
2119 * If this tasklet gets rescheduled while running, it will
2120 * be run again afterwards but without any active request.
2123 spin_unlock_irqrestore(&host->lock, flags);
2127 del_timer(&host->timer);
2132 * The controller needs a reset of internal state machines
2133 * upon error conditions.
2135 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2136 ((mrq->cmd && mrq->cmd->error) ||
2137 (mrq->data && (mrq->data->error ||
2138 (mrq->data->stop && mrq->data->stop->error))) ||
2139 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2141 /* Some controllers need this kick or reset won't work here */
2142 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2143 /* This is to force an update */
2144 sdhci_update_clock(host);
2146 /* Spec says we should do both at the same time, but Ricoh
2147 controllers do not like that. */
2148 sdhci_do_reset(host, SDHCI_RESET_CMD);
2149 sdhci_do_reset(host, SDHCI_RESET_DATA);
2156 #ifndef SDHCI_USE_LEDS_CLASS
2157 sdhci_deactivate_led(host);
2161 spin_unlock_irqrestore(&host->lock, flags);
2163 mmc_request_done(host->mmc, mrq);
2164 sdhci_runtime_pm_put(host);
2167 static void sdhci_timeout_timer(unsigned long data)
2169 struct sdhci_host *host;
2170 unsigned long flags;
2172 host = (struct sdhci_host*)data;
2174 spin_lock_irqsave(&host->lock, flags);
2177 pr_err("%s: Timeout waiting for hardware "
2178 "interrupt.\n", mmc_hostname(host->mmc));
2179 sdhci_dumpregs(host);
2182 host->data->error = -ETIMEDOUT;
2183 sdhci_finish_data(host);
2186 host->cmd->error = -ETIMEDOUT;
2188 host->mrq->cmd->error = -ETIMEDOUT;
2190 tasklet_schedule(&host->finish_tasklet);
2195 spin_unlock_irqrestore(&host->lock, flags);
2198 static void sdhci_tuning_timer(unsigned long data)
2200 struct sdhci_host *host;
2201 unsigned long flags;
2203 host = (struct sdhci_host *)data;
2205 spin_lock_irqsave(&host->lock, flags);
2207 host->flags |= SDHCI_NEEDS_RETUNING;
2209 spin_unlock_irqrestore(&host->lock, flags);
2212 /*****************************************************************************\
2214 * Interrupt handling *
2216 \*****************************************************************************/
2218 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2220 BUG_ON(intmask == 0);
2223 pr_err("%s: Got command interrupt 0x%08x even "
2224 "though no command operation was in progress.\n",
2225 mmc_hostname(host->mmc), (unsigned)intmask);
2226 sdhci_dumpregs(host);
2230 if (intmask & SDHCI_INT_TIMEOUT)
2231 host->cmd->error = -ETIMEDOUT;
2232 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2234 host->cmd->error = -EILSEQ;
2236 if (host->cmd->error) {
2237 tasklet_schedule(&host->finish_tasklet);
2242 * The host can send and interrupt when the busy state has
2243 * ended, allowing us to wait without wasting CPU cycles.
2244 * Unfortunately this is overloaded on the "data complete"
2245 * interrupt, so we need to take some care when handling
2248 * Note: The 1.0 specification is a bit ambiguous about this
2249 * feature so there might be some problems with older
2252 if (host->cmd->flags & MMC_RSP_BUSY) {
2253 if (host->cmd->data)
2254 DBG("Cannot wait for busy signal when also "
2255 "doing a data transfer");
2256 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
2259 /* The controller does not support the end-of-busy IRQ,
2260 * fall through and take the SDHCI_INT_RESPONSE */
2263 if (intmask & SDHCI_INT_RESPONSE)
2264 sdhci_finish_command(host);
2267 #ifdef CONFIG_MMC_DEBUG
2268 static void sdhci_show_adma_error(struct sdhci_host *host)
2270 const char *name = mmc_hostname(host->mmc);
2271 u8 *desc = host->adma_desc;
2276 sdhci_dumpregs(host);
2279 dma = (__le32 *)(desc + 4);
2280 len = (__le16 *)(desc + 2);
2283 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2284 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2293 static void sdhci_show_adma_error(struct sdhci_host *host) { }
2296 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2299 BUG_ON(intmask == 0);
2301 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2302 if (intmask & SDHCI_INT_DATA_AVAIL) {
2303 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2304 if (command == MMC_SEND_TUNING_BLOCK ||
2305 command == MMC_SEND_TUNING_BLOCK_HS200) {
2306 host->tuning_done = 1;
2307 wake_up(&host->buf_ready_int);
2314 * The "data complete" interrupt is also used to
2315 * indicate that a busy state has ended. See comment
2316 * above in sdhci_cmd_irq().
2318 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2319 if (intmask & SDHCI_INT_DATA_END) {
2320 sdhci_finish_command(host);
2325 pr_err("%s: Got data interrupt 0x%08x even "
2326 "though no data operation was in progress.\n",
2327 mmc_hostname(host->mmc), (unsigned)intmask);
2328 sdhci_dumpregs(host);
2333 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2334 host->data->error = -ETIMEDOUT;
2335 else if (intmask & SDHCI_INT_DATA_END_BIT)
2336 host->data->error = -EILSEQ;
2337 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2338 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2340 host->data->error = -EILSEQ;
2341 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2342 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2343 sdhci_show_adma_error(host);
2344 host->data->error = -EIO;
2345 if (host->ops->adma_workaround)
2346 host->ops->adma_workaround(host, intmask);
2349 if (host->data->error)
2350 sdhci_finish_data(host);
2352 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2353 sdhci_transfer_pio(host);
2356 * We currently don't do anything fancy with DMA
2357 * boundaries, but as we can't disable the feature
2358 * we need to at least restart the transfer.
2360 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2361 * should return a valid address to continue from, but as
2362 * some controllers are faulty, don't trust them.
2364 if (intmask & SDHCI_INT_DMA_END) {
2365 u32 dmastart, dmanow;
2366 dmastart = sg_dma_address(host->data->sg);
2367 dmanow = dmastart + host->data->bytes_xfered;
2369 * Force update to the next DMA block boundary.
2372 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2373 SDHCI_DEFAULT_BOUNDARY_SIZE;
2374 host->data->bytes_xfered = dmanow - dmastart;
2375 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2377 mmc_hostname(host->mmc), dmastart,
2378 host->data->bytes_xfered, dmanow);
2379 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2382 if (intmask & SDHCI_INT_DATA_END) {
2385 * Data managed to finish before the
2386 * command completed. Make sure we do
2387 * things in the proper order.
2389 host->data_early = 1;
2391 sdhci_finish_data(host);
2397 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2399 irqreturn_t result = IRQ_NONE;
2400 struct sdhci_host *host = dev_id;
2401 u32 intmask, mask, unexpected = 0;
2404 spin_lock(&host->lock);
2406 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2407 spin_unlock(&host->lock);
2411 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2412 if (!intmask || intmask == 0xffffffff) {
2418 /* Clear selected interrupts. */
2419 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2420 SDHCI_INT_BUS_POWER);
2421 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2423 DBG("*** %s got interrupt: 0x%08x\n",
2424 mmc_hostname(host->mmc), intmask);
2426 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2427 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2431 * There is a observation on i.mx esdhc. INSERT
2432 * bit will be immediately set again when it gets
2433 * cleared, if a card is inserted. We have to mask
2434 * the irq to prevent interrupt storm which will
2435 * freeze the system. And the REMOVE gets the
2438 * More testing are needed here to ensure it works
2439 * for other platforms though.
2441 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2442 SDHCI_INT_CARD_REMOVE);
2443 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2444 SDHCI_INT_CARD_INSERT;
2445 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2446 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2448 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2449 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2451 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2452 SDHCI_INT_CARD_REMOVE);
2453 result = IRQ_WAKE_THREAD;
2456 if (intmask & SDHCI_INT_CMD_MASK)
2457 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2459 if (intmask & SDHCI_INT_DATA_MASK)
2460 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2462 if (intmask & SDHCI_INT_BUS_POWER)
2463 pr_err("%s: Card is consuming too much power!\n",
2464 mmc_hostname(host->mmc));
2466 if (intmask & SDHCI_INT_CARD_INT) {
2467 sdhci_enable_sdio_irq_nolock(host, false);
2468 host->thread_isr |= SDHCI_INT_CARD_INT;
2469 result = IRQ_WAKE_THREAD;
2472 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2473 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2474 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2475 SDHCI_INT_CARD_INT);
2478 unexpected |= intmask;
2479 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2482 if (result == IRQ_NONE)
2483 result = IRQ_HANDLED;
2485 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2486 } while (intmask && --max_loops);
2488 spin_unlock(&host->lock);
2491 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2492 mmc_hostname(host->mmc), unexpected);
2493 sdhci_dumpregs(host);
2499 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2501 struct sdhci_host *host = dev_id;
2502 unsigned long flags;
2505 spin_lock_irqsave(&host->lock, flags);
2506 isr = host->thread_isr;
2507 host->thread_isr = 0;
2508 spin_unlock_irqrestore(&host->lock, flags);
2510 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2511 sdhci_card_event(host->mmc);
2512 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2515 if (isr & SDHCI_INT_CARD_INT) {
2516 sdio_run_irqs(host->mmc);
2518 spin_lock_irqsave(&host->lock, flags);
2519 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2520 sdhci_enable_sdio_irq_nolock(host, true);
2521 spin_unlock_irqrestore(&host->lock, flags);
2524 return isr ? IRQ_HANDLED : IRQ_NONE;
2527 /*****************************************************************************\
2531 \*****************************************************************************/
2534 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2537 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2538 | SDHCI_WAKE_ON_INT;
2540 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2542 /* Avoid fake wake up */
2543 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2544 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2545 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2547 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2549 void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2552 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2553 | SDHCI_WAKE_ON_INT;
2555 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2557 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2559 EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
2561 int sdhci_suspend_host(struct sdhci_host *host)
2563 if (host->ops->platform_suspend)
2564 host->ops->platform_suspend(host);
2566 sdhci_disable_card_detection(host);
2568 /* Disable tuning since we are suspending */
2569 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2570 del_timer_sync(&host->tuning_timer);
2571 host->flags &= ~SDHCI_NEEDS_RETUNING;
2574 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2576 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2577 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2578 free_irq(host->irq, host);
2580 sdhci_enable_irq_wakeups(host);
2581 enable_irq_wake(host->irq);
2586 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2588 int sdhci_resume_host(struct sdhci_host *host)
2592 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2593 if (host->ops->enable_dma)
2594 host->ops->enable_dma(host);
2597 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2598 ret = request_threaded_irq(host->irq, sdhci_irq,
2599 sdhci_thread_irq, IRQF_SHARED,
2600 mmc_hostname(host->mmc), host);
2604 sdhci_disable_irq_wakeups(host);
2605 disable_irq_wake(host->irq);
2608 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2609 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2610 /* Card keeps power but host controller does not */
2611 sdhci_init(host, 0);
2614 sdhci_do_set_ios(host, &host->mmc->ios);
2616 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2620 sdhci_enable_card_detection(host);
2622 if (host->ops->platform_resume)
2623 host->ops->platform_resume(host);
2625 /* Set the re-tuning expiration flag */
2626 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2627 host->flags |= SDHCI_NEEDS_RETUNING;
2632 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2633 #endif /* CONFIG_PM */
2635 #ifdef CONFIG_PM_RUNTIME
2637 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2639 return pm_runtime_get_sync(host->mmc->parent);
2642 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2644 pm_runtime_mark_last_busy(host->mmc->parent);
2645 return pm_runtime_put_autosuspend(host->mmc->parent);
2648 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2650 if (host->runtime_suspended || host->bus_on)
2652 host->bus_on = true;
2653 pm_runtime_get_noresume(host->mmc->parent);
2656 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2658 if (host->runtime_suspended || !host->bus_on)
2660 host->bus_on = false;
2661 pm_runtime_put_noidle(host->mmc->parent);
2664 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2666 unsigned long flags;
2669 /* Disable tuning since we are suspending */
2670 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2671 del_timer_sync(&host->tuning_timer);
2672 host->flags &= ~SDHCI_NEEDS_RETUNING;
2675 spin_lock_irqsave(&host->lock, flags);
2676 host->ier &= SDHCI_INT_CARD_INT;
2677 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2678 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2679 spin_unlock_irqrestore(&host->lock, flags);
2681 synchronize_hardirq(host->irq);
2683 spin_lock_irqsave(&host->lock, flags);
2684 host->runtime_suspended = true;
2685 spin_unlock_irqrestore(&host->lock, flags);
2689 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2691 int sdhci_runtime_resume_host(struct sdhci_host *host)
2693 unsigned long flags;
2694 int ret = 0, host_flags = host->flags;
2696 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2697 if (host->ops->enable_dma)
2698 host->ops->enable_dma(host);
2701 sdhci_init(host, 0);
2703 /* Force clock and power re-program */
2706 sdhci_do_set_ios(host, &host->mmc->ios);
2708 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2709 if ((host_flags & SDHCI_PV_ENABLED) &&
2710 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2711 spin_lock_irqsave(&host->lock, flags);
2712 sdhci_enable_preset_value(host, true);
2713 spin_unlock_irqrestore(&host->lock, flags);
2716 /* Set the re-tuning expiration flag */
2717 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2718 host->flags |= SDHCI_NEEDS_RETUNING;
2720 spin_lock_irqsave(&host->lock, flags);
2722 host->runtime_suspended = false;
2724 /* Enable SDIO IRQ */
2725 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2726 sdhci_enable_sdio_irq_nolock(host, true);
2728 /* Enable Card Detection */
2729 sdhci_enable_card_detection(host);
2731 spin_unlock_irqrestore(&host->lock, flags);
2735 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2739 /*****************************************************************************\
2741 * Device allocation/registration *
2743 \*****************************************************************************/
2745 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2748 struct mmc_host *mmc;
2749 struct sdhci_host *host;
2751 WARN_ON(dev == NULL);
2753 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2755 return ERR_PTR(-ENOMEM);
2757 host = mmc_priv(mmc);
2763 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2765 int sdhci_add_host(struct sdhci_host *host)
2767 struct mmc_host *mmc;
2768 u32 caps[2] = {0, 0};
2769 u32 max_current_caps;
2770 unsigned int ocr_avail;
2773 WARN_ON(host == NULL);
2780 host->quirks = debug_quirks;
2782 host->quirks2 = debug_quirks2;
2784 sdhci_do_reset(host, SDHCI_RESET_ALL);
2786 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2787 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2788 >> SDHCI_SPEC_VER_SHIFT;
2789 if (host->version > SDHCI_SPEC_300) {
2790 pr_err("%s: Unknown controller version (%d). "
2791 "You may experience problems.\n", mmc_hostname(mmc),
2795 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2796 sdhci_readl(host, SDHCI_CAPABILITIES);
2798 if (host->version >= SDHCI_SPEC_300)
2799 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2801 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2803 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2804 host->flags |= SDHCI_USE_SDMA;
2805 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2806 DBG("Controller doesn't have SDMA capability\n");
2808 host->flags |= SDHCI_USE_SDMA;
2810 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2811 (host->flags & SDHCI_USE_SDMA)) {
2812 DBG("Disabling DMA as it is marked broken\n");
2813 host->flags &= ~SDHCI_USE_SDMA;
2816 if ((host->version >= SDHCI_SPEC_200) &&
2817 (caps[0] & SDHCI_CAN_DO_ADMA2))
2818 host->flags |= SDHCI_USE_ADMA;
2820 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2821 (host->flags & SDHCI_USE_ADMA)) {
2822 DBG("Disabling ADMA as it is marked broken\n");
2823 host->flags &= ~SDHCI_USE_ADMA;
2826 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2827 if (host->ops->enable_dma) {
2828 if (host->ops->enable_dma(host)) {
2829 pr_warning("%s: No suitable DMA "
2830 "available. Falling back to PIO.\n",
2833 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2838 if (host->flags & SDHCI_USE_ADMA) {
2840 * We need to allocate descriptors for all sg entries
2841 * (128) and potentially one alignment transfer for
2842 * each of those entries.
2844 host->adma_desc = dma_alloc_coherent(mmc_dev(host->mmc),
2845 ADMA_SIZE, &host->adma_addr,
2847 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2848 if (!host->adma_desc || !host->align_buffer) {
2849 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2850 host->adma_desc, host->adma_addr);
2851 kfree(host->align_buffer);
2852 pr_warning("%s: Unable to allocate ADMA "
2853 "buffers. Falling back to standard DMA.\n",
2855 host->flags &= ~SDHCI_USE_ADMA;
2856 host->adma_desc = NULL;
2857 host->align_buffer = NULL;
2858 } else if (host->adma_addr & 3) {
2859 pr_warning("%s: unable to allocate aligned ADMA descriptor\n",
2861 host->flags &= ~SDHCI_USE_ADMA;
2862 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
2863 host->adma_desc, host->adma_addr);
2864 kfree(host->align_buffer);
2865 host->adma_desc = NULL;
2866 host->align_buffer = NULL;
2871 * If we use DMA, then it's up to the caller to set the DMA
2872 * mask, but PIO does not need the hw shim so we set a new
2873 * mask here in that case.
2875 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2876 host->dma_mask = DMA_BIT_MASK(64);
2877 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2880 if (host->version >= SDHCI_SPEC_300)
2881 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
2882 >> SDHCI_CLOCK_BASE_SHIFT;
2884 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
2885 >> SDHCI_CLOCK_BASE_SHIFT;
2887 host->max_clk *= 1000000;
2888 if (host->max_clk == 0 || host->quirks &
2889 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
2890 if (!host->ops->get_max_clock) {
2891 pr_err("%s: Hardware doesn't specify base clock "
2892 "frequency.\n", mmc_hostname(mmc));
2895 host->max_clk = host->ops->get_max_clock(host);
2899 * In case of Host Controller v3.00, find out whether clock
2900 * multiplier is supported.
2902 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2903 SDHCI_CLOCK_MUL_SHIFT;
2906 * In case the value in Clock Multiplier is 0, then programmable
2907 * clock mode is not supported, otherwise the actual clock
2908 * multiplier is one more than the value of Clock Multiplier
2909 * in the Capabilities Register.
2915 * Set host parameters.
2917 mmc->ops = &sdhci_ops;
2918 mmc->f_max = host->max_clk;
2919 if (host->ops->get_min_clock)
2920 mmc->f_min = host->ops->get_min_clock(host);
2921 else if (host->version >= SDHCI_SPEC_300) {
2922 if (host->clk_mul) {
2923 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2924 mmc->f_max = host->max_clk * host->clk_mul;
2926 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2928 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
2931 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2932 if (host->timeout_clk == 0) {
2933 if (host->ops->get_timeout_clock) {
2934 host->timeout_clk = host->ops->get_timeout_clock(host);
2935 } else if (!(host->quirks &
2936 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2937 pr_err("%s: Hardware doesn't specify timeout clock "
2938 "frequency.\n", mmc_hostname(mmc));
2942 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2943 host->timeout_clk *= 1000;
2945 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
2946 host->timeout_clk = mmc->f_max / 1000;
2948 mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
2950 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2951 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
2953 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2954 host->flags |= SDHCI_AUTO_CMD12;
2956 /* Auto-CMD23 stuff only works in ADMA or PIO. */
2957 if ((host->version >= SDHCI_SPEC_300) &&
2958 ((host->flags & SDHCI_USE_ADMA) ||
2959 !(host->flags & SDHCI_USE_SDMA))) {
2960 host->flags |= SDHCI_AUTO_CMD23;
2961 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2963 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2967 * A controller may support 8-bit width, but the board itself
2968 * might not have the pins brought out. Boards that support
2969 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2970 * their platform code before calling sdhci_add_host(), and we
2971 * won't assume 8-bit width for hosts without that CAP.
2973 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
2974 mmc->caps |= MMC_CAP_4_BIT_DATA;
2976 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2977 mmc->caps &= ~MMC_CAP_CMD23;
2979 if (caps[0] & SDHCI_CAN_DO_HISPD)
2980 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2982 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2983 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
2984 mmc->caps |= MMC_CAP_NEEDS_POLL;
2986 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2987 host->vqmmc = regulator_get_optional(mmc_dev(mmc), "vqmmc");
2988 if (IS_ERR_OR_NULL(host->vqmmc)) {
2989 if (PTR_ERR(host->vqmmc) < 0) {
2990 pr_info("%s: no vqmmc regulator found\n",
2995 ret = regulator_enable(host->vqmmc);
2996 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2998 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2999 SDHCI_SUPPORT_SDR50 |
3000 SDHCI_SUPPORT_DDR50);
3002 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3003 mmc_hostname(mmc), ret);
3008 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3009 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3010 SDHCI_SUPPORT_DDR50);
3012 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3013 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3014 SDHCI_SUPPORT_DDR50))
3015 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3017 /* SDR104 supports also implies SDR50 support */
3018 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3019 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3020 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3021 * field can be promoted to support HS200.
3023 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3024 mmc->caps2 |= MMC_CAP2_HS200;
3025 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3026 mmc->caps |= MMC_CAP_UHS_SDR50;
3028 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3029 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3030 mmc->caps |= MMC_CAP_UHS_DDR50;
3032 /* Does the host need tuning for SDR50? */
3033 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3034 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3036 /* Does the host need tuning for SDR104 / HS200? */
3037 if (mmc->caps2 & MMC_CAP2_HS200)
3038 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3040 /* Driver Type(s) (A, C, D) supported by the host */
3041 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3042 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3043 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3044 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3045 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3046 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3048 /* Initial value for re-tuning timer count */
3049 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3050 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3053 * In case Re-tuning Timer is not disabled, the actual value of
3054 * re-tuning timer will be 2 ^ (n - 1).
3056 if (host->tuning_count)
3057 host->tuning_count = 1 << (host->tuning_count - 1);
3059 /* Re-tuning mode supported by the Host Controller */
3060 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3061 SDHCI_RETUNING_MODE_SHIFT;
3065 host->vmmc = regulator_get_optional(mmc_dev(mmc), "vmmc");
3066 if (IS_ERR_OR_NULL(host->vmmc)) {
3067 if (PTR_ERR(host->vmmc) < 0) {
3068 pr_info("%s: no vmmc regulator found\n",
3074 #ifdef CONFIG_REGULATOR
3076 * Voltage range check makes sense only if regulator reports
3077 * any voltage value.
3079 if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
3080 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
3082 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
3083 caps[0] &= ~SDHCI_CAN_VDD_330;
3084 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
3085 caps[0] &= ~SDHCI_CAN_VDD_300;
3086 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
3088 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
3089 caps[0] &= ~SDHCI_CAN_VDD_180;
3091 #endif /* CONFIG_REGULATOR */
3094 * According to SD Host Controller spec v3.00, if the Host System
3095 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3096 * the value is meaningful only if Voltage Support in the Capabilities
3097 * register is set. The actual current value is 4 times the register
3100 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3101 if (!max_current_caps && host->vmmc) {
3102 u32 curr = regulator_get_current_limit(host->vmmc);
3105 /* convert to SDHCI_MAX_CURRENT format */
3106 curr = curr/1000; /* convert to mA */
3107 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3109 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3111 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3112 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3113 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3117 if (caps[0] & SDHCI_CAN_VDD_330) {
3118 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3120 mmc->max_current_330 = ((max_current_caps &
3121 SDHCI_MAX_CURRENT_330_MASK) >>
3122 SDHCI_MAX_CURRENT_330_SHIFT) *
3123 SDHCI_MAX_CURRENT_MULTIPLIER;
3125 if (caps[0] & SDHCI_CAN_VDD_300) {
3126 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3128 mmc->max_current_300 = ((max_current_caps &
3129 SDHCI_MAX_CURRENT_300_MASK) >>
3130 SDHCI_MAX_CURRENT_300_SHIFT) *
3131 SDHCI_MAX_CURRENT_MULTIPLIER;
3133 if (caps[0] & SDHCI_CAN_VDD_180) {
3134 ocr_avail |= MMC_VDD_165_195;
3136 mmc->max_current_180 = ((max_current_caps &
3137 SDHCI_MAX_CURRENT_180_MASK) >>
3138 SDHCI_MAX_CURRENT_180_SHIFT) *
3139 SDHCI_MAX_CURRENT_MULTIPLIER;
3143 ocr_avail = host->ocr_mask;
3145 mmc->ocr_avail = ocr_avail;
3146 mmc->ocr_avail_sdio = ocr_avail;
3147 if (host->ocr_avail_sdio)
3148 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3149 mmc->ocr_avail_sd = ocr_avail;
3150 if (host->ocr_avail_sd)
3151 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3152 else /* normal SD controllers don't support 1.8V */
3153 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3154 mmc->ocr_avail_mmc = ocr_avail;
3155 if (host->ocr_avail_mmc)
3156 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3158 if (mmc->ocr_avail == 0) {
3159 pr_err("%s: Hardware doesn't report any "
3160 "support voltages.\n", mmc_hostname(mmc));
3164 spin_lock_init(&host->lock);
3167 * Maximum number of segments. Depends on if the hardware
3168 * can do scatter/gather or not.
3170 if (host->flags & SDHCI_USE_ADMA)
3171 mmc->max_segs = 128;
3172 else if (host->flags & SDHCI_USE_SDMA)
3175 mmc->max_segs = 128;
3178 * Maximum number of sectors in one transfer. Limited by DMA boundary
3181 mmc->max_req_size = 524288;
3184 * Maximum segment size. Could be one segment with the maximum number
3185 * of bytes. When doing hardware scatter/gather, each entry cannot
3186 * be larger than 64 KiB though.
3188 if (host->flags & SDHCI_USE_ADMA) {
3189 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3190 mmc->max_seg_size = 65535;
3192 mmc->max_seg_size = 65536;
3194 mmc->max_seg_size = mmc->max_req_size;
3198 * Maximum block size. This varies from controller to controller and
3199 * is specified in the capabilities register.
3201 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3202 mmc->max_blk_size = 2;
3204 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3205 SDHCI_MAX_BLOCK_SHIFT;
3206 if (mmc->max_blk_size >= 3) {
3207 pr_warning("%s: Invalid maximum block size, "
3208 "assuming 512 bytes\n", mmc_hostname(mmc));
3209 mmc->max_blk_size = 0;
3213 mmc->max_blk_size = 512 << mmc->max_blk_size;
3216 * Maximum block count.
3218 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3223 tasklet_init(&host->finish_tasklet,
3224 sdhci_tasklet_finish, (unsigned long)host);
3226 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3228 if (host->version >= SDHCI_SPEC_300) {
3229 init_waitqueue_head(&host->buf_ready_int);
3231 /* Initialize re-tuning timer */
3232 init_timer(&host->tuning_timer);
3233 host->tuning_timer.data = (unsigned long)host;
3234 host->tuning_timer.function = sdhci_tuning_timer;
3237 sdhci_init(host, 0);
3239 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3240 IRQF_SHARED, mmc_hostname(mmc), host);
3242 pr_err("%s: Failed to request IRQ %d: %d\n",
3243 mmc_hostname(mmc), host->irq, ret);
3247 #ifdef CONFIG_MMC_DEBUG
3248 sdhci_dumpregs(host);
3251 #ifdef SDHCI_USE_LEDS_CLASS
3252 snprintf(host->led_name, sizeof(host->led_name),
3253 "%s::", mmc_hostname(mmc));
3254 host->led.name = host->led_name;
3255 host->led.brightness = LED_OFF;
3256 host->led.default_trigger = mmc_hostname(mmc);
3257 host->led.brightness_set = sdhci_led_control;
3259 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3261 pr_err("%s: Failed to register LED device: %d\n",
3262 mmc_hostname(mmc), ret);
3271 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3272 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3273 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3274 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3276 sdhci_enable_card_detection(host);
3280 #ifdef SDHCI_USE_LEDS_CLASS
3282 sdhci_do_reset(host, SDHCI_RESET_ALL);
3283 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3284 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3285 free_irq(host->irq, host);
3288 tasklet_kill(&host->finish_tasklet);
3293 EXPORT_SYMBOL_GPL(sdhci_add_host);
3295 void sdhci_remove_host(struct sdhci_host *host, int dead)
3297 unsigned long flags;
3300 spin_lock_irqsave(&host->lock, flags);
3302 host->flags |= SDHCI_DEVICE_DEAD;
3305 pr_err("%s: Controller removed during "
3306 " transfer!\n", mmc_hostname(host->mmc));
3308 host->mrq->cmd->error = -ENOMEDIUM;
3309 tasklet_schedule(&host->finish_tasklet);
3312 spin_unlock_irqrestore(&host->lock, flags);
3315 sdhci_disable_card_detection(host);
3317 mmc_remove_host(host->mmc);
3319 #ifdef SDHCI_USE_LEDS_CLASS
3320 led_classdev_unregister(&host->led);
3324 sdhci_do_reset(host, SDHCI_RESET_ALL);
3326 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3327 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3328 free_irq(host->irq, host);
3330 del_timer_sync(&host->timer);
3332 tasklet_kill(&host->finish_tasklet);
3335 regulator_disable(host->vmmc);
3336 regulator_put(host->vmmc);
3340 regulator_disable(host->vqmmc);
3341 regulator_put(host->vqmmc);
3344 if (host->adma_desc)
3345 dma_free_coherent(mmc_dev(host->mmc), ADMA_SIZE,
3346 host->adma_desc, host->adma_addr);
3347 kfree(host->align_buffer);
3349 host->adma_desc = NULL;
3350 host->align_buffer = NULL;
3353 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3355 void sdhci_free_host(struct sdhci_host *host)
3357 mmc_free_host(host->mmc);
3360 EXPORT_SYMBOL_GPL(sdhci_free_host);
3362 /*****************************************************************************\
3364 * Driver init/exit *
3366 \*****************************************************************************/
3368 static int __init sdhci_drv_init(void)
3371 ": Secure Digital Host Controller Interface driver\n");
3372 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3377 static void __exit sdhci_drv_exit(void)
3381 module_init(sdhci_drv_init);
3382 module_exit(sdhci_drv_exit);
3384 module_param(debug_quirks, uint, 0444);
3385 module_param(debug_quirks2, uint, 0444);
3387 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3388 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3389 MODULE_LICENSE("GPL");
3391 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3392 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");