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);
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 if (host->flags & SDHCI_USE_64_BIT_DMA)
122 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
123 readl(host->ioaddr + SDHCI_ADMA_ERROR),
124 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
128 readl(host->ioaddr + SDHCI_ADMA_ERROR),
129 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
132 pr_debug(DRIVER_NAME ": ===========================================\n");
135 /*****************************************************************************\
137 * Low level functions *
139 \*****************************************************************************/
141 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
145 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
146 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
150 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
153 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
154 SDHCI_INT_CARD_INSERT;
156 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
159 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
160 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
163 static void sdhci_enable_card_detection(struct sdhci_host *host)
165 sdhci_set_card_detection(host, true);
168 static void sdhci_disable_card_detection(struct sdhci_host *host)
170 sdhci_set_card_detection(host, false);
173 void sdhci_reset(struct sdhci_host *host, u8 mask)
175 unsigned long timeout;
177 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
179 if (mask & SDHCI_RESET_ALL) {
181 /* Reset-all turns off SD Bus Power */
182 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
183 sdhci_runtime_pm_bus_off(host);
186 /* Wait max 100 ms */
189 /* hw clears the bit when it's done */
190 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
192 pr_err("%s: Reset 0x%x never completed.\n",
193 mmc_hostname(host->mmc), (int)mask);
194 sdhci_dumpregs(host);
201 EXPORT_SYMBOL_GPL(sdhci_reset);
203 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
205 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
206 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
211 host->ops->reset(host, mask);
213 if (mask & SDHCI_RESET_ALL) {
214 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
215 if (host->ops->enable_dma)
216 host->ops->enable_dma(host);
219 /* Resetting the controller clears many */
220 host->preset_enabled = false;
224 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
226 static void sdhci_init(struct sdhci_host *host, int soft)
229 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
231 sdhci_do_reset(host, SDHCI_RESET_ALL);
233 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
234 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
235 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
236 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
239 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
240 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
243 /* force clock reconfiguration */
245 sdhci_set_ios(host->mmc, &host->mmc->ios);
249 static void sdhci_reinit(struct sdhci_host *host)
253 * Retuning stuffs are affected by different cards inserted and only
254 * applicable to UHS-I cards. So reset these fields to their initial
255 * value when card is removed.
257 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
258 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
260 del_timer_sync(&host->tuning_timer);
261 host->flags &= ~SDHCI_NEEDS_RETUNING;
263 sdhci_enable_card_detection(host);
266 static void sdhci_activate_led(struct sdhci_host *host)
270 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
271 ctrl |= SDHCI_CTRL_LED;
272 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
275 static void sdhci_deactivate_led(struct sdhci_host *host)
279 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
280 ctrl &= ~SDHCI_CTRL_LED;
281 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
284 #ifdef SDHCI_USE_LEDS_CLASS
285 static void sdhci_led_control(struct led_classdev *led,
286 enum led_brightness brightness)
288 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
291 spin_lock_irqsave(&host->lock, flags);
293 if (host->runtime_suspended)
296 if (brightness == LED_OFF)
297 sdhci_deactivate_led(host);
299 sdhci_activate_led(host);
301 spin_unlock_irqrestore(&host->lock, flags);
305 /*****************************************************************************\
309 \*****************************************************************************/
311 static void sdhci_read_block_pio(struct sdhci_host *host)
314 size_t blksize, len, chunk;
315 u32 uninitialized_var(scratch);
318 DBG("PIO reading\n");
320 blksize = host->data->blksz;
323 local_irq_save(flags);
326 if (!sg_miter_next(&host->sg_miter))
329 len = min(host->sg_miter.length, blksize);
332 host->sg_miter.consumed = len;
334 buf = host->sg_miter.addr;
338 scratch = sdhci_readl(host, SDHCI_BUFFER);
342 *buf = scratch & 0xFF;
351 sg_miter_stop(&host->sg_miter);
353 local_irq_restore(flags);
356 static void sdhci_write_block_pio(struct sdhci_host *host)
359 size_t blksize, len, chunk;
363 DBG("PIO writing\n");
365 blksize = host->data->blksz;
369 local_irq_save(flags);
372 if (!sg_miter_next(&host->sg_miter))
375 len = min(host->sg_miter.length, blksize);
378 host->sg_miter.consumed = len;
380 buf = host->sg_miter.addr;
383 scratch |= (u32)*buf << (chunk * 8);
389 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
390 sdhci_writel(host, scratch, SDHCI_BUFFER);
397 sg_miter_stop(&host->sg_miter);
399 local_irq_restore(flags);
402 static void sdhci_transfer_pio(struct sdhci_host *host)
408 if (host->blocks == 0)
411 if (host->data->flags & MMC_DATA_READ)
412 mask = SDHCI_DATA_AVAILABLE;
414 mask = SDHCI_SPACE_AVAILABLE;
417 * Some controllers (JMicron JMB38x) mess up the buffer bits
418 * for transfers < 4 bytes. As long as it is just one block,
419 * we can ignore the bits.
421 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
422 (host->data->blocks == 1))
425 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
426 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
429 if (host->data->flags & MMC_DATA_READ)
430 sdhci_read_block_pio(host);
432 sdhci_write_block_pio(host);
435 if (host->blocks == 0)
439 DBG("PIO transfer complete.\n");
442 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
444 local_irq_save(*flags);
445 return kmap_atomic(sg_page(sg)) + sg->offset;
448 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
450 kunmap_atomic(buffer);
451 local_irq_restore(*flags);
454 static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
455 dma_addr_t addr, int len, unsigned cmd)
457 struct sdhci_adma2_64_desc *dma_desc = desc;
459 /* 32-bit and 64-bit descriptors have these members in same position */
460 dma_desc->cmd = cpu_to_le16(cmd);
461 dma_desc->len = cpu_to_le16(len);
462 dma_desc->addr_lo = cpu_to_le32((u32)addr);
464 if (host->flags & SDHCI_USE_64_BIT_DMA)
465 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
468 static void sdhci_adma_mark_end(void *desc)
470 struct sdhci_adma2_64_desc *dma_desc = desc;
472 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
473 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
476 static int sdhci_adma_table_pre(struct sdhci_host *host,
477 struct mmc_data *data)
484 dma_addr_t align_addr;
487 struct scatterlist *sg;
493 * The spec does not specify endianness of descriptor table.
494 * We currently guess that it is LE.
497 if (data->flags & MMC_DATA_READ)
498 direction = DMA_FROM_DEVICE;
500 direction = DMA_TO_DEVICE;
502 host->align_addr = dma_map_single(mmc_dev(host->mmc),
503 host->align_buffer, host->align_buffer_sz, direction);
504 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
506 BUG_ON(host->align_addr & host->align_mask);
508 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
509 data->sg, data->sg_len, direction);
510 if (host->sg_count == 0)
513 desc = host->adma_table;
514 align = host->align_buffer;
516 align_addr = host->align_addr;
518 for_each_sg(data->sg, sg, host->sg_count, i) {
519 addr = sg_dma_address(sg);
520 len = sg_dma_len(sg);
523 * The SDHCI specification states that ADMA
524 * addresses must be 32-bit aligned. If they
525 * aren't, then we use a bounce buffer for
526 * the (up to three) bytes that screw up the
529 offset = (host->align_sz - (addr & host->align_mask)) &
532 if (data->flags & MMC_DATA_WRITE) {
533 buffer = sdhci_kmap_atomic(sg, &flags);
534 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
535 (PAGE_SIZE - offset));
536 memcpy(align, buffer, offset);
537 sdhci_kunmap_atomic(buffer, &flags);
541 sdhci_adma_write_desc(host, desc, align_addr, offset,
544 BUG_ON(offset > 65536);
546 align += host->align_sz;
547 align_addr += host->align_sz;
549 desc += host->desc_sz;
558 sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
559 desc += host->desc_sz;
562 * If this triggers then we have a calculation bug
565 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
568 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
570 * Mark the last descriptor as the terminating descriptor
572 if (desc != host->adma_table) {
573 desc -= host->desc_sz;
574 sdhci_adma_mark_end(desc);
578 * Add a terminating entry.
581 /* nop, end, valid */
582 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
586 * Resync align buffer as we might have changed it.
588 if (data->flags & MMC_DATA_WRITE) {
589 dma_sync_single_for_device(mmc_dev(host->mmc),
590 host->align_addr, host->align_buffer_sz, direction);
596 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
597 host->align_buffer_sz, direction);
602 static void sdhci_adma_table_post(struct sdhci_host *host,
603 struct mmc_data *data)
607 struct scatterlist *sg;
614 if (data->flags & MMC_DATA_READ)
615 direction = DMA_FROM_DEVICE;
617 direction = DMA_TO_DEVICE;
619 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
620 host->align_buffer_sz, direction);
622 /* Do a quick scan of the SG list for any unaligned mappings */
623 has_unaligned = false;
624 for_each_sg(data->sg, sg, host->sg_count, i)
625 if (sg_dma_address(sg) & host->align_mask) {
626 has_unaligned = true;
630 if (has_unaligned && data->flags & MMC_DATA_READ) {
631 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
632 data->sg_len, direction);
634 align = host->align_buffer;
636 for_each_sg(data->sg, sg, host->sg_count, i) {
637 if (sg_dma_address(sg) & host->align_mask) {
638 size = host->align_sz -
639 (sg_dma_address(sg) & host->align_mask);
641 buffer = sdhci_kmap_atomic(sg, &flags);
642 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
644 memcpy(buffer, align, size);
645 sdhci_kunmap_atomic(buffer, &flags);
647 align += host->align_sz;
652 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
653 data->sg_len, direction);
656 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
659 struct mmc_data *data = cmd->data;
660 unsigned target_timeout, current_timeout;
663 * If the host controller provides us with an incorrect timeout
664 * value, just skip the check and use 0xE. The hardware may take
665 * longer to time out, but that's much better than having a too-short
668 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
671 /* Unspecified timeout, assume max */
672 if (!data && !cmd->busy_timeout)
677 target_timeout = cmd->busy_timeout * 1000;
679 target_timeout = data->timeout_ns / 1000;
681 target_timeout += data->timeout_clks / host->clock;
685 * Figure out needed cycles.
686 * We do this in steps in order to fit inside a 32 bit int.
687 * The first step is the minimum timeout, which will have a
688 * minimum resolution of 6 bits:
689 * (1) 2^13*1000 > 2^22,
690 * (2) host->timeout_clk < 2^16
695 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
696 while (current_timeout < target_timeout) {
698 current_timeout <<= 1;
704 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
705 mmc_hostname(host->mmc), count, cmd->opcode);
712 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
714 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
715 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
717 if (host->flags & SDHCI_REQ_USE_DMA)
718 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
720 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
722 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
723 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
726 static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
730 if (host->ops->set_timeout) {
731 host->ops->set_timeout(host, cmd);
733 count = sdhci_calc_timeout(host, cmd);
734 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
738 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
741 struct mmc_data *data = cmd->data;
746 if (data || (cmd->flags & MMC_RSP_BUSY))
747 sdhci_set_timeout(host, cmd);
753 BUG_ON(data->blksz * data->blocks > 524288);
754 BUG_ON(data->blksz > host->mmc->max_blk_size);
755 BUG_ON(data->blocks > 65535);
758 host->data_early = 0;
759 host->data->bytes_xfered = 0;
761 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
762 host->flags |= SDHCI_REQ_USE_DMA;
765 * FIXME: This doesn't account for merging when mapping the
768 if (host->flags & SDHCI_REQ_USE_DMA) {
770 struct scatterlist *sg;
773 if (host->flags & SDHCI_USE_ADMA) {
774 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
777 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
781 if (unlikely(broken)) {
782 for_each_sg(data->sg, sg, data->sg_len, i) {
783 if (sg->length & 0x3) {
784 DBG("Reverting to PIO because of "
785 "transfer size (%d)\n",
787 host->flags &= ~SDHCI_REQ_USE_DMA;
795 * The assumption here being that alignment is the same after
796 * translation to device address space.
798 if (host->flags & SDHCI_REQ_USE_DMA) {
800 struct scatterlist *sg;
803 if (host->flags & SDHCI_USE_ADMA) {
805 * As we use 3 byte chunks to work around
806 * alignment problems, we need to check this
809 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
812 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
816 if (unlikely(broken)) {
817 for_each_sg(data->sg, sg, data->sg_len, i) {
818 if (sg->offset & 0x3) {
819 DBG("Reverting to PIO because of "
821 host->flags &= ~SDHCI_REQ_USE_DMA;
828 if (host->flags & SDHCI_REQ_USE_DMA) {
829 if (host->flags & SDHCI_USE_ADMA) {
830 ret = sdhci_adma_table_pre(host, data);
833 * This only happens when someone fed
834 * us an invalid request.
837 host->flags &= ~SDHCI_REQ_USE_DMA;
839 sdhci_writel(host, host->adma_addr,
841 if (host->flags & SDHCI_USE_64_BIT_DMA)
843 (u64)host->adma_addr >> 32,
844 SDHCI_ADMA_ADDRESS_HI);
849 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
850 data->sg, data->sg_len,
851 (data->flags & MMC_DATA_READ) ?
856 * This only happens when someone fed
857 * us an invalid request.
860 host->flags &= ~SDHCI_REQ_USE_DMA;
862 WARN_ON(sg_cnt != 1);
863 sdhci_writel(host, sg_dma_address(data->sg),
870 * Always adjust the DMA selection as some controllers
871 * (e.g. JMicron) can't do PIO properly when the selection
874 if (host->version >= SDHCI_SPEC_200) {
875 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
876 ctrl &= ~SDHCI_CTRL_DMA_MASK;
877 if ((host->flags & SDHCI_REQ_USE_DMA) &&
878 (host->flags & SDHCI_USE_ADMA)) {
879 if (host->flags & SDHCI_USE_64_BIT_DMA)
880 ctrl |= SDHCI_CTRL_ADMA64;
882 ctrl |= SDHCI_CTRL_ADMA32;
884 ctrl |= SDHCI_CTRL_SDMA;
886 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
889 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
892 flags = SG_MITER_ATOMIC;
893 if (host->data->flags & MMC_DATA_READ)
894 flags |= SG_MITER_TO_SG;
896 flags |= SG_MITER_FROM_SG;
897 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
898 host->blocks = data->blocks;
901 sdhci_set_transfer_irqs(host);
903 /* Set the DMA boundary value and block size */
904 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
905 data->blksz), SDHCI_BLOCK_SIZE);
906 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
909 static void sdhci_set_transfer_mode(struct sdhci_host *host,
910 struct mmc_command *cmd)
913 struct mmc_data *data = cmd->data;
917 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
918 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
920 /* clear Auto CMD settings for no data CMDs */
921 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
922 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
923 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
928 WARN_ON(!host->data);
930 mode = SDHCI_TRNS_BLK_CNT_EN;
931 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
932 mode |= SDHCI_TRNS_MULTI;
934 * If we are sending CMD23, CMD12 never gets sent
935 * on successful completion (so no Auto-CMD12).
937 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
938 mode |= SDHCI_TRNS_AUTO_CMD12;
939 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
940 mode |= SDHCI_TRNS_AUTO_CMD23;
941 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
945 if (data->flags & MMC_DATA_READ)
946 mode |= SDHCI_TRNS_READ;
947 if (host->flags & SDHCI_REQ_USE_DMA)
948 mode |= SDHCI_TRNS_DMA;
950 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
953 static void sdhci_finish_data(struct sdhci_host *host)
955 struct mmc_data *data;
962 if (host->flags & SDHCI_REQ_USE_DMA) {
963 if (host->flags & SDHCI_USE_ADMA)
964 sdhci_adma_table_post(host, data);
966 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
967 data->sg_len, (data->flags & MMC_DATA_READ) ?
968 DMA_FROM_DEVICE : DMA_TO_DEVICE);
973 * The specification states that the block count register must
974 * be updated, but it does not specify at what point in the
975 * data flow. That makes the register entirely useless to read
976 * back so we have to assume that nothing made it to the card
977 * in the event of an error.
980 data->bytes_xfered = 0;
982 data->bytes_xfered = data->blksz * data->blocks;
985 * Need to send CMD12 if -
986 * a) open-ended multiblock transfer (no CMD23)
987 * b) error in multiblock transfer
994 * The controller needs a reset of internal state machines
995 * upon error conditions.
998 sdhci_do_reset(host, SDHCI_RESET_CMD);
999 sdhci_do_reset(host, SDHCI_RESET_DATA);
1002 sdhci_send_command(host, data->stop);
1004 tasklet_schedule(&host->finish_tasklet);
1007 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1011 unsigned long timeout;
1015 /* Wait max 10 ms */
1018 mask = SDHCI_CMD_INHIBIT;
1019 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1020 mask |= SDHCI_DATA_INHIBIT;
1022 /* We shouldn't wait for data inihibit for stop commands, even
1023 though they might use busy signaling */
1024 if (host->mrq->data && (cmd == host->mrq->data->stop))
1025 mask &= ~SDHCI_DATA_INHIBIT;
1027 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1029 pr_err("%s: Controller never released "
1030 "inhibit bit(s).\n", mmc_hostname(host->mmc));
1031 sdhci_dumpregs(host);
1033 tasklet_schedule(&host->finish_tasklet);
1041 if (!cmd->data && cmd->busy_timeout > 9000)
1042 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1045 mod_timer(&host->timer, timeout);
1048 host->busy_handle = 0;
1050 sdhci_prepare_data(host, cmd);
1052 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1054 sdhci_set_transfer_mode(host, cmd);
1056 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1057 pr_err("%s: Unsupported response type!\n",
1058 mmc_hostname(host->mmc));
1059 cmd->error = -EINVAL;
1060 tasklet_schedule(&host->finish_tasklet);
1064 if (!(cmd->flags & MMC_RSP_PRESENT))
1065 flags = SDHCI_CMD_RESP_NONE;
1066 else if (cmd->flags & MMC_RSP_136)
1067 flags = SDHCI_CMD_RESP_LONG;
1068 else if (cmd->flags & MMC_RSP_BUSY)
1069 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1071 flags = SDHCI_CMD_RESP_SHORT;
1073 if (cmd->flags & MMC_RSP_CRC)
1074 flags |= SDHCI_CMD_CRC;
1075 if (cmd->flags & MMC_RSP_OPCODE)
1076 flags |= SDHCI_CMD_INDEX;
1078 /* CMD19 is special in that the Data Present Select should be set */
1079 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1080 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1081 flags |= SDHCI_CMD_DATA;
1083 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1085 EXPORT_SYMBOL_GPL(sdhci_send_command);
1087 static void sdhci_finish_command(struct sdhci_host *host)
1091 BUG_ON(host->cmd == NULL);
1093 if (host->cmd->flags & MMC_RSP_PRESENT) {
1094 if (host->cmd->flags & MMC_RSP_136) {
1095 /* CRC is stripped so we need to do some shifting. */
1096 for (i = 0;i < 4;i++) {
1097 host->cmd->resp[i] = sdhci_readl(host,
1098 SDHCI_RESPONSE + (3-i)*4) << 8;
1100 host->cmd->resp[i] |=
1102 SDHCI_RESPONSE + (3-i)*4-1);
1105 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1109 host->cmd->error = 0;
1111 /* Finished CMD23, now send actual command. */
1112 if (host->cmd == host->mrq->sbc) {
1114 sdhci_send_command(host, host->mrq->cmd);
1117 /* Processed actual command. */
1118 if (host->data && host->data_early)
1119 sdhci_finish_data(host);
1121 if (!host->cmd->data)
1122 tasklet_schedule(&host->finish_tasklet);
1128 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1132 switch (host->timing) {
1133 case MMC_TIMING_UHS_SDR12:
1134 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1136 case MMC_TIMING_UHS_SDR25:
1137 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1139 case MMC_TIMING_UHS_SDR50:
1140 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1142 case MMC_TIMING_UHS_SDR104:
1143 case MMC_TIMING_MMC_HS200:
1144 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1146 case MMC_TIMING_UHS_DDR50:
1147 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1149 case MMC_TIMING_MMC_HS400:
1150 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1153 pr_warn("%s: Invalid UHS-I mode selected\n",
1154 mmc_hostname(host->mmc));
1155 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1161 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1163 int div = 0; /* Initialized for compiler warning */
1164 int real_div = div, clk_mul = 1;
1166 unsigned long timeout;
1168 host->mmc->actual_clock = 0;
1170 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1175 if (host->version >= SDHCI_SPEC_300) {
1176 if (host->preset_enabled) {
1179 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1180 pre_val = sdhci_get_preset_value(host);
1181 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1182 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1183 if (host->clk_mul &&
1184 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1185 clk = SDHCI_PROG_CLOCK_MODE;
1187 clk_mul = host->clk_mul;
1189 real_div = max_t(int, 1, div << 1);
1195 * Check if the Host Controller supports Programmable Clock
1198 if (host->clk_mul) {
1199 for (div = 1; div <= 1024; div++) {
1200 if ((host->max_clk * host->clk_mul / div)
1205 * Set Programmable Clock Mode in the Clock
1208 clk = SDHCI_PROG_CLOCK_MODE;
1210 clk_mul = host->clk_mul;
1213 /* Version 3.00 divisors must be a multiple of 2. */
1214 if (host->max_clk <= clock)
1217 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1219 if ((host->max_clk / div) <= clock)
1227 /* Version 2.00 divisors must be a power of 2. */
1228 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1229 if ((host->max_clk / div) <= clock)
1238 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1239 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1240 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1241 << SDHCI_DIVIDER_HI_SHIFT;
1242 clk |= SDHCI_CLOCK_INT_EN;
1243 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1245 /* Wait max 20 ms */
1247 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1248 & SDHCI_CLOCK_INT_STABLE)) {
1250 pr_err("%s: Internal clock never "
1251 "stabilised.\n", mmc_hostname(host->mmc));
1252 sdhci_dumpregs(host);
1259 clk |= SDHCI_CLOCK_CARD_EN;
1260 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1262 EXPORT_SYMBOL_GPL(sdhci_set_clock);
1264 static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1267 struct mmc_host *mmc = host->mmc;
1270 if (!IS_ERR(mmc->supply.vmmc)) {
1271 spin_unlock_irq(&host->lock);
1272 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1273 spin_lock_irq(&host->lock);
1277 if (mode != MMC_POWER_OFF) {
1279 case MMC_VDD_165_195:
1280 pwr = SDHCI_POWER_180;
1284 pwr = SDHCI_POWER_300;
1288 pwr = SDHCI_POWER_330;
1295 if (host->pwr == pwr)
1301 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1302 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1303 sdhci_runtime_pm_bus_off(host);
1307 * Spec says that we should clear the power reg before setting
1308 * a new value. Some controllers don't seem to like this though.
1310 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1311 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1314 * At least the Marvell CaFe chip gets confused if we set the
1315 * voltage and set turn on power at the same time, so set the
1318 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1319 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1321 pwr |= SDHCI_POWER_ON;
1323 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1325 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1326 sdhci_runtime_pm_bus_on(host);
1329 * Some controllers need an extra 10ms delay of 10ms before
1330 * they can apply clock after applying power
1332 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1337 /*****************************************************************************\
1341 \*****************************************************************************/
1343 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1345 struct sdhci_host *host;
1347 unsigned long flags;
1350 host = mmc_priv(mmc);
1352 sdhci_runtime_pm_get(host);
1354 present = mmc_gpio_get_cd(host->mmc);
1356 spin_lock_irqsave(&host->lock, flags);
1358 WARN_ON(host->mrq != NULL);
1360 #ifndef SDHCI_USE_LEDS_CLASS
1361 sdhci_activate_led(host);
1365 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1366 * requests if Auto-CMD12 is enabled.
1368 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1370 mrq->data->stop = NULL;
1378 * Firstly check card presence from cd-gpio. The return could
1379 * be one of the following possibilities:
1380 * negative: cd-gpio is not available
1381 * zero: cd-gpio is used, and card is removed
1382 * one: cd-gpio is used, and card is present
1385 /* If polling, assume that the card is always present. */
1386 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1389 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1393 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1394 host->mrq->cmd->error = -ENOMEDIUM;
1395 tasklet_schedule(&host->finish_tasklet);
1399 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1401 * Check if the re-tuning timer has already expired and there
1402 * is no on-going data transfer and DAT0 is not busy. If so,
1403 * we need to execute tuning procedure before sending command.
1405 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1406 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1407 (present_state & SDHCI_DATA_0_LVL_MASK)) {
1409 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1411 mmc->card->type == MMC_TYPE_MMC ?
1412 MMC_SEND_TUNING_BLOCK_HS200 :
1413 MMC_SEND_TUNING_BLOCK;
1415 /* Here we need to set the host->mrq to NULL,
1416 * in case the pending finish_tasklet
1417 * finishes it incorrectly.
1421 spin_unlock_irqrestore(&host->lock, flags);
1422 sdhci_execute_tuning(mmc, tuning_opcode);
1423 spin_lock_irqsave(&host->lock, flags);
1425 /* Restore original mmc_request structure */
1430 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1431 sdhci_send_command(host, mrq->sbc);
1433 sdhci_send_command(host, mrq->cmd);
1437 spin_unlock_irqrestore(&host->lock, flags);
1440 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1444 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1445 if (width == MMC_BUS_WIDTH_8) {
1446 ctrl &= ~SDHCI_CTRL_4BITBUS;
1447 if (host->version >= SDHCI_SPEC_300)
1448 ctrl |= SDHCI_CTRL_8BITBUS;
1450 if (host->version >= SDHCI_SPEC_300)
1451 ctrl &= ~SDHCI_CTRL_8BITBUS;
1452 if (width == MMC_BUS_WIDTH_4)
1453 ctrl |= SDHCI_CTRL_4BITBUS;
1455 ctrl &= ~SDHCI_CTRL_4BITBUS;
1457 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1459 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1461 void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1465 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1466 /* Select Bus Speed Mode for host */
1467 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1468 if ((timing == MMC_TIMING_MMC_HS200) ||
1469 (timing == MMC_TIMING_UHS_SDR104))
1470 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1471 else if (timing == MMC_TIMING_UHS_SDR12)
1472 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1473 else if (timing == MMC_TIMING_UHS_SDR25)
1474 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1475 else if (timing == MMC_TIMING_UHS_SDR50)
1476 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1477 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1478 (timing == MMC_TIMING_MMC_DDR52))
1479 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1480 else if (timing == MMC_TIMING_MMC_HS400)
1481 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
1482 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1484 EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1486 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1488 unsigned long flags;
1490 struct mmc_host *mmc = host->mmc;
1492 spin_lock_irqsave(&host->lock, flags);
1494 if (host->flags & SDHCI_DEVICE_DEAD) {
1495 spin_unlock_irqrestore(&host->lock, flags);
1496 if (!IS_ERR(mmc->supply.vmmc) &&
1497 ios->power_mode == MMC_POWER_OFF)
1498 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1503 * Reset the chip on each power off.
1504 * Should clear out any weird states.
1506 if (ios->power_mode == MMC_POWER_OFF) {
1507 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1511 if (host->version >= SDHCI_SPEC_300 &&
1512 (ios->power_mode == MMC_POWER_UP) &&
1513 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1514 sdhci_enable_preset_value(host, false);
1516 if (!ios->clock || ios->clock != host->clock) {
1517 host->ops->set_clock(host, ios->clock);
1518 host->clock = ios->clock;
1520 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1522 host->timeout_clk = host->mmc->actual_clock ?
1523 host->mmc->actual_clock / 1000 :
1525 host->mmc->max_busy_timeout =
1526 host->ops->get_max_timeout_count ?
1527 host->ops->get_max_timeout_count(host) :
1529 host->mmc->max_busy_timeout /= host->timeout_clk;
1533 sdhci_set_power(host, ios->power_mode, ios->vdd);
1535 if (host->ops->platform_send_init_74_clocks)
1536 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1538 host->ops->set_bus_width(host, ios->bus_width);
1540 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1542 if ((ios->timing == MMC_TIMING_SD_HS ||
1543 ios->timing == MMC_TIMING_MMC_HS)
1544 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1545 ctrl |= SDHCI_CTRL_HISPD;
1547 ctrl &= ~SDHCI_CTRL_HISPD;
1549 if (host->version >= SDHCI_SPEC_300) {
1552 /* In case of UHS-I modes, set High Speed Enable */
1553 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1554 (ios->timing == MMC_TIMING_MMC_HS200) ||
1555 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1556 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1557 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1558 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1559 (ios->timing == MMC_TIMING_UHS_SDR25))
1560 ctrl |= SDHCI_CTRL_HISPD;
1562 if (!host->preset_enabled) {
1563 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1565 * We only need to set Driver Strength if the
1566 * preset value enable is not set.
1568 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1569 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1570 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1571 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1572 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1573 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1575 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1578 * According to SDHC Spec v3.00, if the Preset Value
1579 * Enable in the Host Control 2 register is set, we
1580 * need to reset SD Clock Enable before changing High
1581 * Speed Enable to avoid generating clock gliches.
1584 /* Reset SD Clock Enable */
1585 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1586 clk &= ~SDHCI_CLOCK_CARD_EN;
1587 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1589 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1591 /* Re-enable SD Clock */
1592 host->ops->set_clock(host, host->clock);
1595 /* Reset SD Clock Enable */
1596 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1597 clk &= ~SDHCI_CLOCK_CARD_EN;
1598 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1600 host->ops->set_uhs_signaling(host, ios->timing);
1601 host->timing = ios->timing;
1603 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1604 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1605 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1606 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1607 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1608 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1611 sdhci_enable_preset_value(host, true);
1612 preset = sdhci_get_preset_value(host);
1613 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1614 >> SDHCI_PRESET_DRV_SHIFT;
1617 /* Re-enable SD Clock */
1618 host->ops->set_clock(host, host->clock);
1620 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1623 * Some (ENE) controllers go apeshit on some ios operation,
1624 * signalling timeout and CRC errors even on CMD0. Resetting
1625 * it on each ios seems to solve the problem.
1627 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1628 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1631 spin_unlock_irqrestore(&host->lock, flags);
1634 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1636 struct sdhci_host *host = mmc_priv(mmc);
1638 sdhci_runtime_pm_get(host);
1639 sdhci_do_set_ios(host, ios);
1640 sdhci_runtime_pm_put(host);
1643 static int sdhci_do_get_cd(struct sdhci_host *host)
1645 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1647 if (host->flags & SDHCI_DEVICE_DEAD)
1650 /* If polling/nonremovable, assume that the card is always present. */
1651 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1652 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1655 /* Try slot gpio detect */
1656 if (!IS_ERR_VALUE(gpio_cd))
1659 /* Host native card detect */
1660 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1663 static int sdhci_get_cd(struct mmc_host *mmc)
1665 struct sdhci_host *host = mmc_priv(mmc);
1668 sdhci_runtime_pm_get(host);
1669 ret = sdhci_do_get_cd(host);
1670 sdhci_runtime_pm_put(host);
1674 static int sdhci_check_ro(struct sdhci_host *host)
1676 unsigned long flags;
1679 spin_lock_irqsave(&host->lock, flags);
1681 if (host->flags & SDHCI_DEVICE_DEAD)
1683 else if (host->ops->get_ro)
1684 is_readonly = host->ops->get_ro(host);
1686 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1687 & SDHCI_WRITE_PROTECT);
1689 spin_unlock_irqrestore(&host->lock, flags);
1691 /* This quirk needs to be replaced by a callback-function later */
1692 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1693 !is_readonly : is_readonly;
1696 #define SAMPLE_COUNT 5
1698 static int sdhci_do_get_ro(struct sdhci_host *host)
1702 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1703 return sdhci_check_ro(host);
1706 for (i = 0; i < SAMPLE_COUNT; i++) {
1707 if (sdhci_check_ro(host)) {
1708 if (++ro_count > SAMPLE_COUNT / 2)
1716 static void sdhci_hw_reset(struct mmc_host *mmc)
1718 struct sdhci_host *host = mmc_priv(mmc);
1720 if (host->ops && host->ops->hw_reset)
1721 host->ops->hw_reset(host);
1724 static int sdhci_get_ro(struct mmc_host *mmc)
1726 struct sdhci_host *host = mmc_priv(mmc);
1729 sdhci_runtime_pm_get(host);
1730 ret = sdhci_do_get_ro(host);
1731 sdhci_runtime_pm_put(host);
1735 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1737 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1739 host->ier |= SDHCI_INT_CARD_INT;
1741 host->ier &= ~SDHCI_INT_CARD_INT;
1743 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1744 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1749 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1751 struct sdhci_host *host = mmc_priv(mmc);
1752 unsigned long flags;
1754 sdhci_runtime_pm_get(host);
1756 spin_lock_irqsave(&host->lock, flags);
1758 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1760 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1762 sdhci_enable_sdio_irq_nolock(host, enable);
1763 spin_unlock_irqrestore(&host->lock, flags);
1765 sdhci_runtime_pm_put(host);
1768 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1769 struct mmc_ios *ios)
1771 struct mmc_host *mmc = host->mmc;
1776 * Signal Voltage Switching is only applicable for Host Controllers
1779 if (host->version < SDHCI_SPEC_300)
1782 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1784 switch (ios->signal_voltage) {
1785 case MMC_SIGNAL_VOLTAGE_330:
1786 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1787 ctrl &= ~SDHCI_CTRL_VDD_180;
1788 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1790 if (!IS_ERR(mmc->supply.vqmmc)) {
1791 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1794 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1800 usleep_range(5000, 5500);
1802 /* 3.3V regulator output should be stable within 5 ms */
1803 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1804 if (!(ctrl & SDHCI_CTRL_VDD_180))
1807 pr_warn("%s: 3.3V regulator output did not became stable\n",
1811 case MMC_SIGNAL_VOLTAGE_180:
1812 if (!IS_ERR(mmc->supply.vqmmc)) {
1813 ret = regulator_set_voltage(mmc->supply.vqmmc,
1816 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1823 * Enable 1.8V Signal Enable in the Host Control2
1826 ctrl |= SDHCI_CTRL_VDD_180;
1827 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1829 /* 1.8V regulator output should be stable within 5 ms */
1830 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1831 if (ctrl & SDHCI_CTRL_VDD_180)
1834 pr_warn("%s: 1.8V regulator output did not became stable\n",
1838 case MMC_SIGNAL_VOLTAGE_120:
1839 if (!IS_ERR(mmc->supply.vqmmc)) {
1840 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1843 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1850 /* No signal voltage switch required */
1855 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1856 struct mmc_ios *ios)
1858 struct sdhci_host *host = mmc_priv(mmc);
1861 if (host->version < SDHCI_SPEC_300)
1863 sdhci_runtime_pm_get(host);
1864 err = sdhci_do_start_signal_voltage_switch(host, ios);
1865 sdhci_runtime_pm_put(host);
1869 static int sdhci_card_busy(struct mmc_host *mmc)
1871 struct sdhci_host *host = mmc_priv(mmc);
1874 sdhci_runtime_pm_get(host);
1875 /* Check whether DAT[3:0] is 0000 */
1876 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1877 sdhci_runtime_pm_put(host);
1879 return !(present_state & SDHCI_DATA_LVL_MASK);
1882 static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1884 struct sdhci_host *host = mmc_priv(mmc);
1885 unsigned long flags;
1887 spin_lock_irqsave(&host->lock, flags);
1888 host->flags |= SDHCI_HS400_TUNING;
1889 spin_unlock_irqrestore(&host->lock, flags);
1894 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1896 struct sdhci_host *host = mmc_priv(mmc);
1898 int tuning_loop_counter = MAX_TUNING_LOOP;
1900 unsigned long flags;
1901 unsigned int tuning_count = 0;
1904 sdhci_runtime_pm_get(host);
1905 spin_lock_irqsave(&host->lock, flags);
1907 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1908 host->flags &= ~SDHCI_HS400_TUNING;
1910 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1911 tuning_count = host->tuning_count;
1914 * The Host Controller needs tuning only in case of SDR104 mode
1915 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1916 * Capabilities register.
1917 * If the Host Controller supports the HS200 mode then the
1918 * tuning function has to be executed.
1920 switch (host->timing) {
1921 /* HS400 tuning is done in HS200 mode */
1922 case MMC_TIMING_MMC_HS400:
1926 case MMC_TIMING_MMC_HS200:
1928 * Periodic re-tuning for HS400 is not expected to be needed, so
1935 case MMC_TIMING_UHS_SDR104:
1938 case MMC_TIMING_UHS_SDR50:
1939 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1940 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1948 if (host->ops->platform_execute_tuning) {
1949 spin_unlock_irqrestore(&host->lock, flags);
1950 err = host->ops->platform_execute_tuning(host, opcode);
1951 sdhci_runtime_pm_put(host);
1955 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1956 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1957 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1960 * As per the Host Controller spec v3.00, tuning command
1961 * generates Buffer Read Ready interrupt, so enable that.
1963 * Note: The spec clearly says that when tuning sequence
1964 * is being performed, the controller does not generate
1965 * interrupts other than Buffer Read Ready interrupt. But
1966 * to make sure we don't hit a controller bug, we _only_
1967 * enable Buffer Read Ready interrupt here.
1969 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1970 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1973 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1974 * of loops reaches 40 times or a timeout of 150ms occurs.
1977 struct mmc_command cmd = {0};
1978 struct mmc_request mrq = {NULL};
1980 cmd.opcode = opcode;
1982 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1987 if (tuning_loop_counter-- == 0)
1994 * In response to CMD19, the card sends 64 bytes of tuning
1995 * block to the Host Controller. So we set the block size
1998 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1999 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2000 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2002 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2003 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2006 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2011 * The tuning block is sent by the card to the host controller.
2012 * So we set the TRNS_READ bit in the Transfer Mode register.
2013 * This also takes care of setting DMA Enable and Multi Block
2014 * Select in the same register to 0.
2016 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2018 sdhci_send_command(host, &cmd);
2023 spin_unlock_irqrestore(&host->lock, flags);
2024 /* Wait for Buffer Read Ready interrupt */
2025 wait_event_interruptible_timeout(host->buf_ready_int,
2026 (host->tuning_done == 1),
2027 msecs_to_jiffies(50));
2028 spin_lock_irqsave(&host->lock, flags);
2030 if (!host->tuning_done) {
2031 pr_info(DRIVER_NAME ": Timeout waiting for "
2032 "Buffer Read Ready interrupt during tuning "
2033 "procedure, falling back to fixed sampling "
2035 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2036 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2037 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2038 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2044 host->tuning_done = 0;
2046 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2048 /* eMMC spec does not require a delay between tuning cycles */
2049 if (opcode == MMC_SEND_TUNING_BLOCK)
2051 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2054 * The Host Driver has exhausted the maximum number of loops allowed,
2055 * so use fixed sampling frequency.
2057 if (tuning_loop_counter < 0) {
2058 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2059 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2061 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2062 pr_info(DRIVER_NAME ": Tuning procedure"
2063 " failed, falling back to fixed sampling"
2069 host->flags &= ~SDHCI_NEEDS_RETUNING;
2072 host->flags |= SDHCI_USING_RETUNING_TIMER;
2073 mod_timer(&host->tuning_timer, jiffies + tuning_count * HZ);
2077 * In case tuning fails, host controllers which support re-tuning can
2078 * try tuning again at a later time, when the re-tuning timer expires.
2079 * So for these controllers, we return 0. Since there might be other
2080 * controllers who do not have this capability, we return error for
2081 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2082 * a retuning timer to do the retuning for the card.
2084 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2087 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2088 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2090 spin_unlock_irqrestore(&host->lock, flags);
2091 sdhci_runtime_pm_put(host);
2097 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2099 /* Host Controller v3.00 defines preset value registers */
2100 if (host->version < SDHCI_SPEC_300)
2104 * We only enable or disable Preset Value if they are not already
2105 * enabled or disabled respectively. Otherwise, we bail out.
2107 if (host->preset_enabled != enable) {
2108 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2111 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2113 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2115 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2118 host->flags |= SDHCI_PV_ENABLED;
2120 host->flags &= ~SDHCI_PV_ENABLED;
2122 host->preset_enabled = enable;
2126 static void sdhci_card_event(struct mmc_host *mmc)
2128 struct sdhci_host *host = mmc_priv(mmc);
2129 unsigned long flags;
2132 /* First check if client has provided their own card event */
2133 if (host->ops->card_event)
2134 host->ops->card_event(host);
2136 present = sdhci_do_get_cd(host);
2138 spin_lock_irqsave(&host->lock, flags);
2140 /* Check host->mrq first in case we are runtime suspended */
2141 if (host->mrq && !present) {
2142 pr_err("%s: Card removed during transfer!\n",
2143 mmc_hostname(host->mmc));
2144 pr_err("%s: Resetting controller.\n",
2145 mmc_hostname(host->mmc));
2147 sdhci_do_reset(host, SDHCI_RESET_CMD);
2148 sdhci_do_reset(host, SDHCI_RESET_DATA);
2150 host->mrq->cmd->error = -ENOMEDIUM;
2151 tasklet_schedule(&host->finish_tasklet);
2154 spin_unlock_irqrestore(&host->lock, flags);
2157 static const struct mmc_host_ops sdhci_ops = {
2158 .request = sdhci_request,
2159 .set_ios = sdhci_set_ios,
2160 .get_cd = sdhci_get_cd,
2161 .get_ro = sdhci_get_ro,
2162 .hw_reset = sdhci_hw_reset,
2163 .enable_sdio_irq = sdhci_enable_sdio_irq,
2164 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2165 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
2166 .execute_tuning = sdhci_execute_tuning,
2167 .card_event = sdhci_card_event,
2168 .card_busy = sdhci_card_busy,
2171 /*****************************************************************************\
2175 \*****************************************************************************/
2177 static void sdhci_tasklet_finish(unsigned long param)
2179 struct sdhci_host *host;
2180 unsigned long flags;
2181 struct mmc_request *mrq;
2183 host = (struct sdhci_host*)param;
2185 spin_lock_irqsave(&host->lock, flags);
2188 * If this tasklet gets rescheduled while running, it will
2189 * be run again afterwards but without any active request.
2192 spin_unlock_irqrestore(&host->lock, flags);
2196 del_timer(&host->timer);
2201 * The controller needs a reset of internal state machines
2202 * upon error conditions.
2204 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2205 ((mrq->cmd && mrq->cmd->error) ||
2206 (mrq->sbc && mrq->sbc->error) ||
2207 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2208 (mrq->data->stop && mrq->data->stop->error))) ||
2209 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2211 /* Some controllers need this kick or reset won't work here */
2212 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2213 /* This is to force an update */
2214 host->ops->set_clock(host, host->clock);
2216 /* Spec says we should do both at the same time, but Ricoh
2217 controllers do not like that. */
2218 sdhci_do_reset(host, SDHCI_RESET_CMD);
2219 sdhci_do_reset(host, SDHCI_RESET_DATA);
2226 #ifndef SDHCI_USE_LEDS_CLASS
2227 sdhci_deactivate_led(host);
2231 spin_unlock_irqrestore(&host->lock, flags);
2233 mmc_request_done(host->mmc, mrq);
2234 sdhci_runtime_pm_put(host);
2237 static void sdhci_timeout_timer(unsigned long data)
2239 struct sdhci_host *host;
2240 unsigned long flags;
2242 host = (struct sdhci_host*)data;
2244 spin_lock_irqsave(&host->lock, flags);
2247 pr_err("%s: Timeout waiting for hardware "
2248 "interrupt.\n", mmc_hostname(host->mmc));
2249 sdhci_dumpregs(host);
2252 host->data->error = -ETIMEDOUT;
2253 sdhci_finish_data(host);
2256 host->cmd->error = -ETIMEDOUT;
2258 host->mrq->cmd->error = -ETIMEDOUT;
2260 tasklet_schedule(&host->finish_tasklet);
2265 spin_unlock_irqrestore(&host->lock, flags);
2268 static void sdhci_tuning_timer(unsigned long data)
2270 struct sdhci_host *host;
2271 unsigned long flags;
2273 host = (struct sdhci_host *)data;
2275 spin_lock_irqsave(&host->lock, flags);
2277 host->flags |= SDHCI_NEEDS_RETUNING;
2279 spin_unlock_irqrestore(&host->lock, flags);
2282 /*****************************************************************************\
2284 * Interrupt handling *
2286 \*****************************************************************************/
2288 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
2290 BUG_ON(intmask == 0);
2293 pr_err("%s: Got command interrupt 0x%08x even "
2294 "though no command operation was in progress.\n",
2295 mmc_hostname(host->mmc), (unsigned)intmask);
2296 sdhci_dumpregs(host);
2300 if (intmask & SDHCI_INT_TIMEOUT)
2301 host->cmd->error = -ETIMEDOUT;
2302 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2304 host->cmd->error = -EILSEQ;
2306 if (host->cmd->error) {
2307 tasklet_schedule(&host->finish_tasklet);
2312 * The host can send and interrupt when the busy state has
2313 * ended, allowing us to wait without wasting CPU cycles.
2314 * Unfortunately this is overloaded on the "data complete"
2315 * interrupt, so we need to take some care when handling
2318 * Note: The 1.0 specification is a bit ambiguous about this
2319 * feature so there might be some problems with older
2322 if (host->cmd->flags & MMC_RSP_BUSY) {
2323 if (host->cmd->data)
2324 DBG("Cannot wait for busy signal when also "
2325 "doing a data transfer");
2326 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2327 && !host->busy_handle) {
2328 /* Mark that command complete before busy is ended */
2329 host->busy_handle = 1;
2333 /* The controller does not support the end-of-busy IRQ,
2334 * fall through and take the SDHCI_INT_RESPONSE */
2335 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2336 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2337 *mask &= ~SDHCI_INT_DATA_END;
2340 if (intmask & SDHCI_INT_RESPONSE)
2341 sdhci_finish_command(host);
2344 #ifdef CONFIG_MMC_DEBUG
2345 static void sdhci_adma_show_error(struct sdhci_host *host)
2347 const char *name = mmc_hostname(host->mmc);
2348 void *desc = host->adma_table;
2350 sdhci_dumpregs(host);
2353 struct sdhci_adma2_64_desc *dma_desc = desc;
2355 if (host->flags & SDHCI_USE_64_BIT_DMA)
2356 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2357 name, desc, le32_to_cpu(dma_desc->addr_hi),
2358 le32_to_cpu(dma_desc->addr_lo),
2359 le16_to_cpu(dma_desc->len),
2360 le16_to_cpu(dma_desc->cmd));
2362 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2363 name, desc, le32_to_cpu(dma_desc->addr_lo),
2364 le16_to_cpu(dma_desc->len),
2365 le16_to_cpu(dma_desc->cmd));
2367 desc += host->desc_sz;
2369 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
2374 static void sdhci_adma_show_error(struct sdhci_host *host) { }
2377 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2380 BUG_ON(intmask == 0);
2382 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2383 if (intmask & SDHCI_INT_DATA_AVAIL) {
2384 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2385 if (command == MMC_SEND_TUNING_BLOCK ||
2386 command == MMC_SEND_TUNING_BLOCK_HS200) {
2387 host->tuning_done = 1;
2388 wake_up(&host->buf_ready_int);
2395 * The "data complete" interrupt is also used to
2396 * indicate that a busy state has ended. See comment
2397 * above in sdhci_cmd_irq().
2399 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2400 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2401 host->cmd->error = -ETIMEDOUT;
2402 tasklet_schedule(&host->finish_tasklet);
2405 if (intmask & SDHCI_INT_DATA_END) {
2407 * Some cards handle busy-end interrupt
2408 * before the command completed, so make
2409 * sure we do things in the proper order.
2411 if (host->busy_handle)
2412 sdhci_finish_command(host);
2414 host->busy_handle = 1;
2419 pr_err("%s: Got data interrupt 0x%08x even "
2420 "though no data operation was in progress.\n",
2421 mmc_hostname(host->mmc), (unsigned)intmask);
2422 sdhci_dumpregs(host);
2427 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2428 host->data->error = -ETIMEDOUT;
2429 else if (intmask & SDHCI_INT_DATA_END_BIT)
2430 host->data->error = -EILSEQ;
2431 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2432 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2434 host->data->error = -EILSEQ;
2435 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2436 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2437 sdhci_adma_show_error(host);
2438 host->data->error = -EIO;
2439 if (host->ops->adma_workaround)
2440 host->ops->adma_workaround(host, intmask);
2443 if (host->data->error)
2444 sdhci_finish_data(host);
2446 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2447 sdhci_transfer_pio(host);
2450 * We currently don't do anything fancy with DMA
2451 * boundaries, but as we can't disable the feature
2452 * we need to at least restart the transfer.
2454 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2455 * should return a valid address to continue from, but as
2456 * some controllers are faulty, don't trust them.
2458 if (intmask & SDHCI_INT_DMA_END) {
2459 u32 dmastart, dmanow;
2460 dmastart = sg_dma_address(host->data->sg);
2461 dmanow = dmastart + host->data->bytes_xfered;
2463 * Force update to the next DMA block boundary.
2466 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2467 SDHCI_DEFAULT_BOUNDARY_SIZE;
2468 host->data->bytes_xfered = dmanow - dmastart;
2469 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2471 mmc_hostname(host->mmc), dmastart,
2472 host->data->bytes_xfered, dmanow);
2473 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2476 if (intmask & SDHCI_INT_DATA_END) {
2479 * Data managed to finish before the
2480 * command completed. Make sure we do
2481 * things in the proper order.
2483 host->data_early = 1;
2485 sdhci_finish_data(host);
2491 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2493 irqreturn_t result = IRQ_NONE;
2494 struct sdhci_host *host = dev_id;
2495 u32 intmask, mask, unexpected = 0;
2498 spin_lock(&host->lock);
2500 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2501 spin_unlock(&host->lock);
2505 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2506 if (!intmask || intmask == 0xffffffff) {
2512 /* Clear selected interrupts. */
2513 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2514 SDHCI_INT_BUS_POWER);
2515 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2517 DBG("*** %s got interrupt: 0x%08x\n",
2518 mmc_hostname(host->mmc), intmask);
2520 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2521 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2525 * There is a observation on i.mx esdhc. INSERT
2526 * bit will be immediately set again when it gets
2527 * cleared, if a card is inserted. We have to mask
2528 * the irq to prevent interrupt storm which will
2529 * freeze the system. And the REMOVE gets the
2532 * More testing are needed here to ensure it works
2533 * for other platforms though.
2535 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2536 SDHCI_INT_CARD_REMOVE);
2537 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2538 SDHCI_INT_CARD_INSERT;
2539 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2540 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2542 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2543 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2545 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2546 SDHCI_INT_CARD_REMOVE);
2547 result = IRQ_WAKE_THREAD;
2550 if (intmask & SDHCI_INT_CMD_MASK)
2551 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2554 if (intmask & SDHCI_INT_DATA_MASK)
2555 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2557 if (intmask & SDHCI_INT_BUS_POWER)
2558 pr_err("%s: Card is consuming too much power!\n",
2559 mmc_hostname(host->mmc));
2561 if (intmask & SDHCI_INT_CARD_INT) {
2562 sdhci_enable_sdio_irq_nolock(host, false);
2563 host->thread_isr |= SDHCI_INT_CARD_INT;
2564 result = IRQ_WAKE_THREAD;
2567 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2568 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2569 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2570 SDHCI_INT_CARD_INT);
2573 unexpected |= intmask;
2574 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2577 if (result == IRQ_NONE)
2578 result = IRQ_HANDLED;
2580 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2581 } while (intmask && --max_loops);
2583 spin_unlock(&host->lock);
2586 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2587 mmc_hostname(host->mmc), unexpected);
2588 sdhci_dumpregs(host);
2594 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2596 struct sdhci_host *host = dev_id;
2597 unsigned long flags;
2600 spin_lock_irqsave(&host->lock, flags);
2601 isr = host->thread_isr;
2602 host->thread_isr = 0;
2603 spin_unlock_irqrestore(&host->lock, flags);
2605 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2606 sdhci_card_event(host->mmc);
2607 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2610 if (isr & SDHCI_INT_CARD_INT) {
2611 sdio_run_irqs(host->mmc);
2613 spin_lock_irqsave(&host->lock, flags);
2614 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2615 sdhci_enable_sdio_irq_nolock(host, true);
2616 spin_unlock_irqrestore(&host->lock, flags);
2619 return isr ? IRQ_HANDLED : IRQ_NONE;
2622 /*****************************************************************************\
2626 \*****************************************************************************/
2629 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2632 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2633 | SDHCI_WAKE_ON_INT;
2635 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2637 /* Avoid fake wake up */
2638 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2639 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2640 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2642 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2644 static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2647 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2648 | SDHCI_WAKE_ON_INT;
2650 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2652 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2655 int sdhci_suspend_host(struct sdhci_host *host)
2657 sdhci_disable_card_detection(host);
2659 /* Disable tuning since we are suspending */
2660 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2661 del_timer_sync(&host->tuning_timer);
2662 host->flags &= ~SDHCI_NEEDS_RETUNING;
2665 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2667 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2668 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2669 free_irq(host->irq, host);
2671 sdhci_enable_irq_wakeups(host);
2672 enable_irq_wake(host->irq);
2677 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2679 int sdhci_resume_host(struct sdhci_host *host)
2683 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2684 if (host->ops->enable_dma)
2685 host->ops->enable_dma(host);
2688 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2689 ret = request_threaded_irq(host->irq, sdhci_irq,
2690 sdhci_thread_irq, IRQF_SHARED,
2691 mmc_hostname(host->mmc), host);
2695 sdhci_disable_irq_wakeups(host);
2696 disable_irq_wake(host->irq);
2699 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2700 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2701 /* Card keeps power but host controller does not */
2702 sdhci_init(host, 0);
2705 sdhci_do_set_ios(host, &host->mmc->ios);
2707 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2711 sdhci_enable_card_detection(host);
2713 /* Set the re-tuning expiration flag */
2714 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2715 host->flags |= SDHCI_NEEDS_RETUNING;
2720 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2722 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2724 return pm_runtime_get_sync(host->mmc->parent);
2727 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2729 pm_runtime_mark_last_busy(host->mmc->parent);
2730 return pm_runtime_put_autosuspend(host->mmc->parent);
2733 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2735 if (host->runtime_suspended || host->bus_on)
2737 host->bus_on = true;
2738 pm_runtime_get_noresume(host->mmc->parent);
2741 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2743 if (host->runtime_suspended || !host->bus_on)
2745 host->bus_on = false;
2746 pm_runtime_put_noidle(host->mmc->parent);
2749 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2751 unsigned long flags;
2753 /* Disable tuning since we are suspending */
2754 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2755 del_timer_sync(&host->tuning_timer);
2756 host->flags &= ~SDHCI_NEEDS_RETUNING;
2759 spin_lock_irqsave(&host->lock, flags);
2760 host->ier &= SDHCI_INT_CARD_INT;
2761 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2762 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2763 spin_unlock_irqrestore(&host->lock, flags);
2765 synchronize_hardirq(host->irq);
2767 spin_lock_irqsave(&host->lock, flags);
2768 host->runtime_suspended = true;
2769 spin_unlock_irqrestore(&host->lock, flags);
2773 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2775 int sdhci_runtime_resume_host(struct sdhci_host *host)
2777 unsigned long flags;
2778 int host_flags = host->flags;
2780 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2781 if (host->ops->enable_dma)
2782 host->ops->enable_dma(host);
2785 sdhci_init(host, 0);
2787 /* Force clock and power re-program */
2790 sdhci_do_set_ios(host, &host->mmc->ios);
2792 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2793 if ((host_flags & SDHCI_PV_ENABLED) &&
2794 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2795 spin_lock_irqsave(&host->lock, flags);
2796 sdhci_enable_preset_value(host, true);
2797 spin_unlock_irqrestore(&host->lock, flags);
2800 /* Set the re-tuning expiration flag */
2801 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2802 host->flags |= SDHCI_NEEDS_RETUNING;
2804 spin_lock_irqsave(&host->lock, flags);
2806 host->runtime_suspended = false;
2808 /* Enable SDIO IRQ */
2809 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2810 sdhci_enable_sdio_irq_nolock(host, true);
2812 /* Enable Card Detection */
2813 sdhci_enable_card_detection(host);
2815 spin_unlock_irqrestore(&host->lock, flags);
2819 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2821 #endif /* CONFIG_PM */
2823 /*****************************************************************************\
2825 * Device allocation/registration *
2827 \*****************************************************************************/
2829 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2832 struct mmc_host *mmc;
2833 struct sdhci_host *host;
2835 WARN_ON(dev == NULL);
2837 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2839 return ERR_PTR(-ENOMEM);
2841 host = mmc_priv(mmc);
2847 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2849 int sdhci_add_host(struct sdhci_host *host)
2851 struct mmc_host *mmc;
2852 u32 caps[2] = {0, 0};
2853 u32 max_current_caps;
2854 unsigned int ocr_avail;
2855 unsigned int override_timeout_clk;
2858 WARN_ON(host == NULL);
2865 host->quirks = debug_quirks;
2867 host->quirks2 = debug_quirks2;
2869 override_timeout_clk = host->timeout_clk;
2871 sdhci_do_reset(host, SDHCI_RESET_ALL);
2873 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2874 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2875 >> SDHCI_SPEC_VER_SHIFT;
2876 if (host->version > SDHCI_SPEC_300) {
2877 pr_err("%s: Unknown controller version (%d). "
2878 "You may experience problems.\n", mmc_hostname(mmc),
2882 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2883 sdhci_readl(host, SDHCI_CAPABILITIES);
2885 if (host->version >= SDHCI_SPEC_300)
2886 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2888 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2890 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2891 host->flags |= SDHCI_USE_SDMA;
2892 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2893 DBG("Controller doesn't have SDMA capability\n");
2895 host->flags |= SDHCI_USE_SDMA;
2897 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2898 (host->flags & SDHCI_USE_SDMA)) {
2899 DBG("Disabling DMA as it is marked broken\n");
2900 host->flags &= ~SDHCI_USE_SDMA;
2903 if ((host->version >= SDHCI_SPEC_200) &&
2904 (caps[0] & SDHCI_CAN_DO_ADMA2))
2905 host->flags |= SDHCI_USE_ADMA;
2907 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2908 (host->flags & SDHCI_USE_ADMA)) {
2909 DBG("Disabling ADMA as it is marked broken\n");
2910 host->flags &= ~SDHCI_USE_ADMA;
2914 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2915 * and *must* do 64-bit DMA. A driver has the opportunity to change
2916 * that during the first call to ->enable_dma(). Similarly
2917 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2920 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
2921 host->flags |= SDHCI_USE_64_BIT_DMA;
2923 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2924 if (host->ops->enable_dma) {
2925 if (host->ops->enable_dma(host)) {
2926 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2929 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2934 /* SDMA does not support 64-bit DMA */
2935 if (host->flags & SDHCI_USE_64_BIT_DMA)
2936 host->flags &= ~SDHCI_USE_SDMA;
2938 if (host->flags & SDHCI_USE_ADMA) {
2940 * The DMA descriptor table size is calculated as the maximum
2941 * number of segments times 2, to allow for an alignment
2942 * descriptor for each segment, plus 1 for a nop end descriptor,
2943 * all multipled by the descriptor size.
2945 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2946 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2947 SDHCI_ADMA2_64_DESC_SZ;
2948 host->align_buffer_sz = SDHCI_MAX_SEGS *
2949 SDHCI_ADMA2_64_ALIGN;
2950 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
2951 host->align_sz = SDHCI_ADMA2_64_ALIGN;
2952 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
2954 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2955 SDHCI_ADMA2_32_DESC_SZ;
2956 host->align_buffer_sz = SDHCI_MAX_SEGS *
2957 SDHCI_ADMA2_32_ALIGN;
2958 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2959 host->align_sz = SDHCI_ADMA2_32_ALIGN;
2960 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
2962 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
2963 host->adma_table_sz,
2966 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
2967 if (!host->adma_table || !host->align_buffer) {
2968 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
2969 host->adma_table, host->adma_addr);
2970 kfree(host->align_buffer);
2971 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
2973 host->flags &= ~SDHCI_USE_ADMA;
2974 host->adma_table = NULL;
2975 host->align_buffer = NULL;
2976 } else if (host->adma_addr & host->align_mask) {
2977 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2979 host->flags &= ~SDHCI_USE_ADMA;
2980 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
2981 host->adma_table, host->adma_addr);
2982 kfree(host->align_buffer);
2983 host->adma_table = NULL;
2984 host->align_buffer = NULL;
2989 * If we use DMA, then it's up to the caller to set the DMA
2990 * mask, but PIO does not need the hw shim so we set a new
2991 * mask here in that case.
2993 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
2994 host->dma_mask = DMA_BIT_MASK(64);
2995 mmc_dev(mmc)->dma_mask = &host->dma_mask;
2998 if (host->version >= SDHCI_SPEC_300)
2999 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
3000 >> SDHCI_CLOCK_BASE_SHIFT;
3002 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
3003 >> SDHCI_CLOCK_BASE_SHIFT;
3005 host->max_clk *= 1000000;
3006 if (host->max_clk == 0 || host->quirks &
3007 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
3008 if (!host->ops->get_max_clock) {
3009 pr_err("%s: Hardware doesn't specify base clock "
3010 "frequency.\n", mmc_hostname(mmc));
3013 host->max_clk = host->ops->get_max_clock(host);
3017 * In case of Host Controller v3.00, find out whether clock
3018 * multiplier is supported.
3020 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3021 SDHCI_CLOCK_MUL_SHIFT;
3024 * In case the value in Clock Multiplier is 0, then programmable
3025 * clock mode is not supported, otherwise the actual clock
3026 * multiplier is one more than the value of Clock Multiplier
3027 * in the Capabilities Register.
3033 * Set host parameters.
3035 mmc->ops = &sdhci_ops;
3036 mmc->f_max = host->max_clk;
3037 if (host->ops->get_min_clock)
3038 mmc->f_min = host->ops->get_min_clock(host);
3039 else if (host->version >= SDHCI_SPEC_300) {
3040 if (host->clk_mul) {
3041 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3042 mmc->f_max = host->max_clk * host->clk_mul;
3044 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3046 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
3048 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3049 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3050 SDHCI_TIMEOUT_CLK_SHIFT;
3051 if (host->timeout_clk == 0) {
3052 if (host->ops->get_timeout_clock) {
3054 host->ops->get_timeout_clock(host);
3056 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3062 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3063 host->timeout_clk *= 1000;
3065 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
3066 host->ops->get_max_timeout_count(host) : 1 << 27;
3067 mmc->max_busy_timeout /= host->timeout_clk;
3070 if (override_timeout_clk)
3071 host->timeout_clk = override_timeout_clk;
3073 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3074 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
3076 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3077 host->flags |= SDHCI_AUTO_CMD12;
3079 /* Auto-CMD23 stuff only works in ADMA or PIO. */
3080 if ((host->version >= SDHCI_SPEC_300) &&
3081 ((host->flags & SDHCI_USE_ADMA) ||
3082 !(host->flags & SDHCI_USE_SDMA))) {
3083 host->flags |= SDHCI_AUTO_CMD23;
3084 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3086 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3090 * A controller may support 8-bit width, but the board itself
3091 * might not have the pins brought out. Boards that support
3092 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3093 * their platform code before calling sdhci_add_host(), and we
3094 * won't assume 8-bit width for hosts without that CAP.
3096 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
3097 mmc->caps |= MMC_CAP_4_BIT_DATA;
3099 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3100 mmc->caps &= ~MMC_CAP_CMD23;
3102 if (caps[0] & SDHCI_CAN_DO_HISPD)
3103 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3105 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3106 !(mmc->caps & MMC_CAP_NONREMOVABLE))
3107 mmc->caps |= MMC_CAP_NEEDS_POLL;
3109 /* If there are external regulators, get them */
3110 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3111 return -EPROBE_DEFER;
3113 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
3114 if (!IS_ERR(mmc->supply.vqmmc)) {
3115 ret = regulator_enable(mmc->supply.vqmmc);
3116 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3118 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3119 SDHCI_SUPPORT_SDR50 |
3120 SDHCI_SUPPORT_DDR50);
3122 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3123 mmc_hostname(mmc), ret);
3124 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
3128 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3129 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3130 SDHCI_SUPPORT_DDR50);
3132 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3133 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3134 SDHCI_SUPPORT_DDR50))
3135 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3137 /* SDR104 supports also implies SDR50 support */
3138 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3139 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3140 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3141 * field can be promoted to support HS200.
3143 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3144 mmc->caps2 |= MMC_CAP2_HS200;
3145 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3146 mmc->caps |= MMC_CAP_UHS_SDR50;
3148 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3149 (caps[1] & SDHCI_SUPPORT_HS400))
3150 mmc->caps2 |= MMC_CAP2_HS400;
3152 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3153 (IS_ERR(mmc->supply.vqmmc) ||
3154 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3156 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3158 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3159 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3160 mmc->caps |= MMC_CAP_UHS_DDR50;
3162 /* Does the host need tuning for SDR50? */
3163 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3164 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3166 /* Does the host need tuning for SDR104 / HS200? */
3167 if (mmc->caps2 & MMC_CAP2_HS200)
3168 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3170 /* Driver Type(s) (A, C, D) supported by the host */
3171 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3172 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3173 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3174 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3175 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3176 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3178 /* Initial value for re-tuning timer count */
3179 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3180 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3183 * In case Re-tuning Timer is not disabled, the actual value of
3184 * re-tuning timer will be 2 ^ (n - 1).
3186 if (host->tuning_count)
3187 host->tuning_count = 1 << (host->tuning_count - 1);
3189 /* Re-tuning mode supported by the Host Controller */
3190 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3191 SDHCI_RETUNING_MODE_SHIFT;
3196 * According to SD Host Controller spec v3.00, if the Host System
3197 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3198 * the value is meaningful only if Voltage Support in the Capabilities
3199 * register is set. The actual current value is 4 times the register
3202 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3203 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3204 int curr = regulator_get_current_limit(mmc->supply.vmmc);
3207 /* convert to SDHCI_MAX_CURRENT format */
3208 curr = curr/1000; /* convert to mA */
3209 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3211 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3213 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3214 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3215 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3219 if (caps[0] & SDHCI_CAN_VDD_330) {
3220 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3222 mmc->max_current_330 = ((max_current_caps &
3223 SDHCI_MAX_CURRENT_330_MASK) >>
3224 SDHCI_MAX_CURRENT_330_SHIFT) *
3225 SDHCI_MAX_CURRENT_MULTIPLIER;
3227 if (caps[0] & SDHCI_CAN_VDD_300) {
3228 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3230 mmc->max_current_300 = ((max_current_caps &
3231 SDHCI_MAX_CURRENT_300_MASK) >>
3232 SDHCI_MAX_CURRENT_300_SHIFT) *
3233 SDHCI_MAX_CURRENT_MULTIPLIER;
3235 if (caps[0] & SDHCI_CAN_VDD_180) {
3236 ocr_avail |= MMC_VDD_165_195;
3238 mmc->max_current_180 = ((max_current_caps &
3239 SDHCI_MAX_CURRENT_180_MASK) >>
3240 SDHCI_MAX_CURRENT_180_SHIFT) *
3241 SDHCI_MAX_CURRENT_MULTIPLIER;
3244 /* If OCR set by external regulators, use it instead */
3246 ocr_avail = mmc->ocr_avail;
3249 ocr_avail &= host->ocr_mask;
3251 mmc->ocr_avail = ocr_avail;
3252 mmc->ocr_avail_sdio = ocr_avail;
3253 if (host->ocr_avail_sdio)
3254 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3255 mmc->ocr_avail_sd = ocr_avail;
3256 if (host->ocr_avail_sd)
3257 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3258 else /* normal SD controllers don't support 1.8V */
3259 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3260 mmc->ocr_avail_mmc = ocr_avail;
3261 if (host->ocr_avail_mmc)
3262 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3264 if (mmc->ocr_avail == 0) {
3265 pr_err("%s: Hardware doesn't report any "
3266 "support voltages.\n", mmc_hostname(mmc));
3270 spin_lock_init(&host->lock);
3273 * Maximum number of segments. Depends on if the hardware
3274 * can do scatter/gather or not.
3276 if (host->flags & SDHCI_USE_ADMA)
3277 mmc->max_segs = SDHCI_MAX_SEGS;
3278 else if (host->flags & SDHCI_USE_SDMA)
3281 mmc->max_segs = SDHCI_MAX_SEGS;
3284 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3285 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3288 mmc->max_req_size = 524288;
3291 * Maximum segment size. Could be one segment with the maximum number
3292 * of bytes. When doing hardware scatter/gather, each entry cannot
3293 * be larger than 64 KiB though.
3295 if (host->flags & SDHCI_USE_ADMA) {
3296 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3297 mmc->max_seg_size = 65535;
3299 mmc->max_seg_size = 65536;
3301 mmc->max_seg_size = mmc->max_req_size;
3305 * Maximum block size. This varies from controller to controller and
3306 * is specified in the capabilities register.
3308 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3309 mmc->max_blk_size = 2;
3311 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3312 SDHCI_MAX_BLOCK_SHIFT;
3313 if (mmc->max_blk_size >= 3) {
3314 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3316 mmc->max_blk_size = 0;
3320 mmc->max_blk_size = 512 << mmc->max_blk_size;
3323 * Maximum block count.
3325 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3330 tasklet_init(&host->finish_tasklet,
3331 sdhci_tasklet_finish, (unsigned long)host);
3333 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3335 if (host->version >= SDHCI_SPEC_300) {
3336 init_waitqueue_head(&host->buf_ready_int);
3338 /* Initialize re-tuning timer */
3339 init_timer(&host->tuning_timer);
3340 host->tuning_timer.data = (unsigned long)host;
3341 host->tuning_timer.function = sdhci_tuning_timer;
3344 sdhci_init(host, 0);
3346 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3347 IRQF_SHARED, mmc_hostname(mmc), host);
3349 pr_err("%s: Failed to request IRQ %d: %d\n",
3350 mmc_hostname(mmc), host->irq, ret);
3354 #ifdef CONFIG_MMC_DEBUG
3355 sdhci_dumpregs(host);
3358 #ifdef SDHCI_USE_LEDS_CLASS
3359 snprintf(host->led_name, sizeof(host->led_name),
3360 "%s::", mmc_hostname(mmc));
3361 host->led.name = host->led_name;
3362 host->led.brightness = LED_OFF;
3363 host->led.default_trigger = mmc_hostname(mmc);
3364 host->led.brightness_set = sdhci_led_control;
3366 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3368 pr_err("%s: Failed to register LED device: %d\n",
3369 mmc_hostname(mmc), ret);
3378 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3379 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3380 (host->flags & SDHCI_USE_ADMA) ?
3381 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
3382 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3384 sdhci_enable_card_detection(host);
3388 #ifdef SDHCI_USE_LEDS_CLASS
3390 sdhci_do_reset(host, SDHCI_RESET_ALL);
3391 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3392 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3393 free_irq(host->irq, host);
3396 tasklet_kill(&host->finish_tasklet);
3401 EXPORT_SYMBOL_GPL(sdhci_add_host);
3403 void sdhci_remove_host(struct sdhci_host *host, int dead)
3405 struct mmc_host *mmc = host->mmc;
3406 unsigned long flags;
3409 spin_lock_irqsave(&host->lock, flags);
3411 host->flags |= SDHCI_DEVICE_DEAD;
3414 pr_err("%s: Controller removed during "
3415 " transfer!\n", mmc_hostname(mmc));
3417 host->mrq->cmd->error = -ENOMEDIUM;
3418 tasklet_schedule(&host->finish_tasklet);
3421 spin_unlock_irqrestore(&host->lock, flags);
3424 sdhci_disable_card_detection(host);
3426 mmc_remove_host(mmc);
3428 #ifdef SDHCI_USE_LEDS_CLASS
3429 led_classdev_unregister(&host->led);
3433 sdhci_do_reset(host, SDHCI_RESET_ALL);
3435 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3436 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3437 free_irq(host->irq, host);
3439 del_timer_sync(&host->timer);
3441 tasklet_kill(&host->finish_tasklet);
3443 if (!IS_ERR(mmc->supply.vqmmc))
3444 regulator_disable(mmc->supply.vqmmc);
3446 if (host->adma_table)
3447 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3448 host->adma_table, host->adma_addr);
3449 kfree(host->align_buffer);
3451 host->adma_table = NULL;
3452 host->align_buffer = NULL;
3455 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3457 void sdhci_free_host(struct sdhci_host *host)
3459 mmc_free_host(host->mmc);
3462 EXPORT_SYMBOL_GPL(sdhci_free_host);
3464 /*****************************************************************************\
3466 * Driver init/exit *
3468 \*****************************************************************************/
3470 static int __init sdhci_drv_init(void)
3473 ": Secure Digital Host Controller Interface driver\n");
3474 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3479 static void __exit sdhci_drv_exit(void)
3483 module_init(sdhci_drv_init);
3484 module_exit(sdhci_drv_exit);
3486 module_param(debug_quirks, uint, 0444);
3487 module_param(debug_quirks2, uint, 0444);
3489 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3490 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3491 MODULE_LICENSE("GPL");
3493 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3494 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");