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);
56 static int sdhci_pre_dma_transfer(struct sdhci_host *host,
57 struct mmc_data *data,
58 struct sdhci_host_next *next);
61 static int sdhci_runtime_pm_get(struct sdhci_host *host);
62 static int sdhci_runtime_pm_put(struct sdhci_host *host);
63 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
64 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
66 static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
70 static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
74 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
77 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
82 static void sdhci_dumpregs(struct sdhci_host *host)
84 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
85 mmc_hostname(host->mmc));
87 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
88 sdhci_readl(host, SDHCI_DMA_ADDRESS),
89 sdhci_readw(host, SDHCI_HOST_VERSION));
90 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
91 sdhci_readw(host, SDHCI_BLOCK_SIZE),
92 sdhci_readw(host, SDHCI_BLOCK_COUNT));
93 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
94 sdhci_readl(host, SDHCI_ARGUMENT),
95 sdhci_readw(host, SDHCI_TRANSFER_MODE));
96 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
97 sdhci_readl(host, SDHCI_PRESENT_STATE),
98 sdhci_readb(host, SDHCI_HOST_CONTROL));
99 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
100 sdhci_readb(host, SDHCI_POWER_CONTROL),
101 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
102 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
103 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
104 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
105 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
106 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
107 sdhci_readl(host, SDHCI_INT_STATUS));
108 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
109 sdhci_readl(host, SDHCI_INT_ENABLE),
110 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
111 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
112 sdhci_readw(host, SDHCI_ACMD12_ERR),
113 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
114 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
115 sdhci_readl(host, SDHCI_CAPABILITIES),
116 sdhci_readl(host, SDHCI_CAPABILITIES_1));
117 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
118 sdhci_readw(host, SDHCI_COMMAND),
119 sdhci_readl(host, SDHCI_MAX_CURRENT));
120 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
121 sdhci_readw(host, SDHCI_HOST_CONTROL2));
123 if (host->flags & SDHCI_USE_ADMA) {
124 if (host->flags & SDHCI_USE_64_BIT_DMA)
125 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
126 readl(host->ioaddr + SDHCI_ADMA_ERROR),
127 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
128 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
130 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
131 readl(host->ioaddr + SDHCI_ADMA_ERROR),
132 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
135 pr_debug(DRIVER_NAME ": ===========================================\n");
138 /*****************************************************************************\
140 * Low level functions *
142 \*****************************************************************************/
144 static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
148 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
149 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
156 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
157 SDHCI_INT_CARD_INSERT;
159 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
162 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
163 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
166 static void sdhci_enable_card_detection(struct sdhci_host *host)
168 sdhci_set_card_detection(host, true);
171 static void sdhci_disable_card_detection(struct sdhci_host *host)
173 sdhci_set_card_detection(host, false);
176 void sdhci_reset(struct sdhci_host *host, u8 mask)
178 unsigned long timeout;
180 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
182 if (mask & SDHCI_RESET_ALL) {
184 /* Reset-all turns off SD Bus Power */
185 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186 sdhci_runtime_pm_bus_off(host);
189 /* Wait max 100 ms */
192 /* hw clears the bit when it's done */
193 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
195 pr_err("%s: Reset 0x%x never completed.\n",
196 mmc_hostname(host->mmc), (int)mask);
197 sdhci_dumpregs(host);
204 EXPORT_SYMBOL_GPL(sdhci_reset);
206 static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
209 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
214 host->ops->reset(host, mask);
216 if (mask & SDHCI_RESET_ALL) {
217 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
218 if (host->ops->enable_dma)
219 host->ops->enable_dma(host);
222 /* Resetting the controller clears many */
223 host->preset_enabled = false;
227 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
229 static void sdhci_init(struct sdhci_host *host, int soft)
232 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
234 sdhci_do_reset(host, SDHCI_RESET_ALL);
236 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
237 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
238 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
239 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
242 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
243 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
246 /* force clock reconfiguration */
248 sdhci_set_ios(host->mmc, &host->mmc->ios);
252 static void sdhci_reinit(struct sdhci_host *host)
256 * Retuning stuffs are affected by different cards inserted and only
257 * applicable to UHS-I cards. So reset these fields to their initial
258 * value when card is removed.
260 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
261 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
263 del_timer_sync(&host->tuning_timer);
264 host->flags &= ~SDHCI_NEEDS_RETUNING;
266 sdhci_enable_card_detection(host);
269 static void sdhci_activate_led(struct sdhci_host *host)
273 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
274 ctrl |= SDHCI_CTRL_LED;
275 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
278 static void sdhci_deactivate_led(struct sdhci_host *host)
282 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
283 ctrl &= ~SDHCI_CTRL_LED;
284 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
287 #ifdef SDHCI_USE_LEDS_CLASS
288 static void sdhci_led_control(struct led_classdev *led,
289 enum led_brightness brightness)
291 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
294 spin_lock_irqsave(&host->lock, flags);
296 if (host->runtime_suspended)
299 if (brightness == LED_OFF)
300 sdhci_deactivate_led(host);
302 sdhci_activate_led(host);
304 spin_unlock_irqrestore(&host->lock, flags);
308 /*****************************************************************************\
312 \*****************************************************************************/
314 static void sdhci_read_block_pio(struct sdhci_host *host)
317 size_t blksize, len, chunk;
318 u32 uninitialized_var(scratch);
321 DBG("PIO reading\n");
323 blksize = host->data->blksz;
326 local_irq_save(flags);
329 if (!sg_miter_next(&host->sg_miter))
332 len = min(host->sg_miter.length, blksize);
335 host->sg_miter.consumed = len;
337 buf = host->sg_miter.addr;
341 scratch = sdhci_readl(host, SDHCI_BUFFER);
345 *buf = scratch & 0xFF;
354 sg_miter_stop(&host->sg_miter);
356 local_irq_restore(flags);
359 static void sdhci_write_block_pio(struct sdhci_host *host)
362 size_t blksize, len, chunk;
366 DBG("PIO writing\n");
368 blksize = host->data->blksz;
372 local_irq_save(flags);
375 if (!sg_miter_next(&host->sg_miter))
378 len = min(host->sg_miter.length, blksize);
381 host->sg_miter.consumed = len;
383 buf = host->sg_miter.addr;
386 scratch |= (u32)*buf << (chunk * 8);
392 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
393 sdhci_writel(host, scratch, SDHCI_BUFFER);
400 sg_miter_stop(&host->sg_miter);
402 local_irq_restore(flags);
405 static void sdhci_transfer_pio(struct sdhci_host *host)
411 if (host->blocks == 0)
414 if (host->data->flags & MMC_DATA_READ)
415 mask = SDHCI_DATA_AVAILABLE;
417 mask = SDHCI_SPACE_AVAILABLE;
420 * Some controllers (JMicron JMB38x) mess up the buffer bits
421 * for transfers < 4 bytes. As long as it is just one block,
422 * we can ignore the bits.
424 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
425 (host->data->blocks == 1))
428 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
429 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
432 if (host->data->flags & MMC_DATA_READ)
433 sdhci_read_block_pio(host);
435 sdhci_write_block_pio(host);
438 if (host->blocks == 0)
442 DBG("PIO transfer complete.\n");
445 static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
447 local_irq_save(*flags);
448 return kmap_atomic(sg_page(sg)) + sg->offset;
451 static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
453 kunmap_atomic(buffer);
454 local_irq_restore(*flags);
457 static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
458 dma_addr_t addr, int len, unsigned cmd)
460 struct sdhci_adma2_64_desc *dma_desc = desc;
462 /* 32-bit and 64-bit descriptors have these members in same position */
463 dma_desc->cmd = cpu_to_le16(cmd);
464 dma_desc->len = cpu_to_le16(len);
465 dma_desc->addr_lo = cpu_to_le32((u32)addr);
467 if (host->flags & SDHCI_USE_64_BIT_DMA)
468 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
471 static void sdhci_adma_mark_end(void *desc)
473 struct sdhci_adma2_64_desc *dma_desc = desc;
475 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
476 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
479 static int sdhci_adma_table_pre(struct sdhci_host *host,
480 struct mmc_data *data)
487 dma_addr_t align_addr;
490 struct scatterlist *sg;
496 * The spec does not specify endianness of descriptor table.
497 * We currently guess that it is LE.
500 if (data->flags & MMC_DATA_READ)
501 direction = DMA_FROM_DEVICE;
503 direction = DMA_TO_DEVICE;
505 host->align_addr = dma_map_single(mmc_dev(host->mmc),
506 host->align_buffer, host->align_buffer_sz, direction);
507 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
509 BUG_ON(host->align_addr & host->align_mask);
511 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
512 if (host->sg_count < 0)
515 desc = host->adma_table;
516 align = host->align_buffer;
518 align_addr = host->align_addr;
520 for_each_sg(data->sg, sg, host->sg_count, i) {
521 addr = sg_dma_address(sg);
522 len = sg_dma_len(sg);
525 * The SDHCI specification states that ADMA
526 * addresses must be 32-bit aligned. If they
527 * aren't, then we use a bounce buffer for
528 * the (up to three) bytes that screw up the
531 offset = (host->align_sz - (addr & host->align_mask)) &
534 if (data->flags & MMC_DATA_WRITE) {
535 buffer = sdhci_kmap_atomic(sg, &flags);
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 memcpy(buffer, align, size);
643 sdhci_kunmap_atomic(buffer, &flags);
645 align += host->align_sz;
650 if (!data->host_cookie)
651 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
652 data->sg_len, direction);
655 static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
658 struct mmc_data *data = cmd->data;
659 unsigned target_timeout, current_timeout;
662 * If the host controller provides us with an incorrect timeout
663 * value, just skip the check and use 0xE. The hardware may take
664 * longer to time out, but that's much better than having a too-short
667 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
670 /* Unspecified timeout, assume max */
671 if (!data && !cmd->busy_timeout)
676 target_timeout = cmd->busy_timeout * 1000;
678 target_timeout = data->timeout_ns / 1000;
680 target_timeout += data->timeout_clks / host->clock;
684 * Figure out needed cycles.
685 * We do this in steps in order to fit inside a 32 bit int.
686 * The first step is the minimum timeout, which will have a
687 * minimum resolution of 6 bits:
688 * (1) 2^13*1000 > 2^22,
689 * (2) host->timeout_clk < 2^16
694 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
695 while (current_timeout < target_timeout) {
697 current_timeout <<= 1;
703 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
704 mmc_hostname(host->mmc), count, cmd->opcode);
711 static void sdhci_set_transfer_irqs(struct sdhci_host *host)
713 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
714 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
716 if (host->flags & SDHCI_REQ_USE_DMA)
717 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
719 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
721 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
722 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
725 static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
729 if (host->ops->set_timeout) {
730 host->ops->set_timeout(host, cmd);
732 count = sdhci_calc_timeout(host, cmd);
733 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
737 static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
740 struct mmc_data *data = cmd->data;
745 if (data || (cmd->flags & MMC_RSP_BUSY))
746 sdhci_set_timeout(host, cmd);
752 BUG_ON(data->blksz * data->blocks > 524288);
753 BUG_ON(data->blksz > host->mmc->max_blk_size);
754 BUG_ON(data->blocks > 65535);
757 host->data_early = 0;
758 host->data->bytes_xfered = 0;
760 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
761 host->flags |= SDHCI_REQ_USE_DMA;
764 * FIXME: This doesn't account for merging when mapping the
767 if (host->flags & SDHCI_REQ_USE_DMA) {
769 struct scatterlist *sg;
772 if (host->flags & SDHCI_USE_ADMA) {
773 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
776 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
780 if (unlikely(broken)) {
781 for_each_sg(data->sg, sg, data->sg_len, i) {
782 if (sg->length & 0x3) {
783 DBG("Reverting to PIO because of "
784 "transfer size (%d)\n",
786 host->flags &= ~SDHCI_REQ_USE_DMA;
794 * The assumption here being that alignment is the same after
795 * translation to device address space.
797 if (host->flags & SDHCI_REQ_USE_DMA) {
799 struct scatterlist *sg;
802 if (host->flags & SDHCI_USE_ADMA) {
804 * As we use 3 byte chunks to work around
805 * alignment problems, we need to check this
808 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
811 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
815 if (unlikely(broken)) {
816 for_each_sg(data->sg, sg, data->sg_len, i) {
817 if (sg->offset & 0x3) {
818 DBG("Reverting to PIO because of "
820 host->flags &= ~SDHCI_REQ_USE_DMA;
827 if (host->flags & SDHCI_REQ_USE_DMA) {
828 if (host->flags & SDHCI_USE_ADMA) {
829 ret = sdhci_adma_table_pre(host, data);
832 * This only happens when someone fed
833 * us an invalid request.
836 host->flags &= ~SDHCI_REQ_USE_DMA;
838 sdhci_writel(host, host->adma_addr,
840 if (host->flags & SDHCI_USE_64_BIT_DMA)
842 (u64)host->adma_addr >> 32,
843 SDHCI_ADMA_ADDRESS_HI);
848 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
851 * This only happens when someone fed
852 * us an invalid request.
855 host->flags &= ~SDHCI_REQ_USE_DMA;
857 WARN_ON(sg_cnt != 1);
858 sdhci_writel(host, sg_dma_address(data->sg),
865 * Always adjust the DMA selection as some controllers
866 * (e.g. JMicron) can't do PIO properly when the selection
869 if (host->version >= SDHCI_SPEC_200) {
870 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
871 ctrl &= ~SDHCI_CTRL_DMA_MASK;
872 if ((host->flags & SDHCI_REQ_USE_DMA) &&
873 (host->flags & SDHCI_USE_ADMA)) {
874 if (host->flags & SDHCI_USE_64_BIT_DMA)
875 ctrl |= SDHCI_CTRL_ADMA64;
877 ctrl |= SDHCI_CTRL_ADMA32;
879 ctrl |= SDHCI_CTRL_SDMA;
881 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
884 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
887 flags = SG_MITER_ATOMIC;
888 if (host->data->flags & MMC_DATA_READ)
889 flags |= SG_MITER_TO_SG;
891 flags |= SG_MITER_FROM_SG;
892 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
893 host->blocks = data->blocks;
896 sdhci_set_transfer_irqs(host);
898 /* Set the DMA boundary value and block size */
899 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
900 data->blksz), SDHCI_BLOCK_SIZE);
901 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
904 static void sdhci_set_transfer_mode(struct sdhci_host *host,
905 struct mmc_command *cmd)
908 struct mmc_data *data = cmd->data;
912 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
913 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
915 /* clear Auto CMD settings for no data CMDs */
916 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
917 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
918 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
923 WARN_ON(!host->data);
925 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
926 mode = SDHCI_TRNS_BLK_CNT_EN;
928 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
929 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
931 * If we are sending CMD23, CMD12 never gets sent
932 * on successful completion (so no Auto-CMD12).
934 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
935 mode |= SDHCI_TRNS_AUTO_CMD12;
936 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
937 mode |= SDHCI_TRNS_AUTO_CMD23;
938 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
942 if (data->flags & MMC_DATA_READ)
943 mode |= SDHCI_TRNS_READ;
944 if (host->flags & SDHCI_REQ_USE_DMA)
945 mode |= SDHCI_TRNS_DMA;
947 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
950 static void sdhci_finish_data(struct sdhci_host *host)
952 struct mmc_data *data;
959 if (host->flags & SDHCI_REQ_USE_DMA) {
960 if (host->flags & SDHCI_USE_ADMA)
961 sdhci_adma_table_post(host, data);
963 if (!data->host_cookie)
964 dma_unmap_sg(mmc_dev(host->mmc),
965 data->sg, data->sg_len,
966 (data->flags & MMC_DATA_READ) ?
967 DMA_FROM_DEVICE : DMA_TO_DEVICE);
972 * The specification states that the block count register must
973 * be updated, but it does not specify at what point in the
974 * data flow. That makes the register entirely useless to read
975 * back so we have to assume that nothing made it to the card
976 * in the event of an error.
979 data->bytes_xfered = 0;
981 data->bytes_xfered = data->blksz * data->blocks;
984 * Need to send CMD12 if -
985 * a) open-ended multiblock transfer (no CMD23)
986 * b) error in multiblock transfer
993 * The controller needs a reset of internal state machines
994 * upon error conditions.
997 sdhci_do_reset(host, SDHCI_RESET_CMD);
998 sdhci_do_reset(host, SDHCI_RESET_DATA);
1001 sdhci_send_command(host, data->stop);
1003 tasklet_schedule(&host->finish_tasklet);
1006 void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1010 unsigned long timeout;
1014 /* Wait max 10 ms */
1017 mask = SDHCI_CMD_INHIBIT;
1018 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1019 mask |= SDHCI_DATA_INHIBIT;
1021 /* We shouldn't wait for data inihibit for stop commands, even
1022 though they might use busy signaling */
1023 if (host->mrq->data && (cmd == host->mrq->data->stop))
1024 mask &= ~SDHCI_DATA_INHIBIT;
1026 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
1028 pr_err("%s: Controller never released "
1029 "inhibit bit(s).\n", mmc_hostname(host->mmc));
1030 sdhci_dumpregs(host);
1032 tasklet_schedule(&host->finish_tasklet);
1040 if (!cmd->data && cmd->busy_timeout > 9000)
1041 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1044 mod_timer(&host->timer, timeout);
1047 host->busy_handle = 0;
1049 sdhci_prepare_data(host, cmd);
1051 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
1053 sdhci_set_transfer_mode(host, cmd);
1055 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
1056 pr_err("%s: Unsupported response type!\n",
1057 mmc_hostname(host->mmc));
1058 cmd->error = -EINVAL;
1059 tasklet_schedule(&host->finish_tasklet);
1063 if (!(cmd->flags & MMC_RSP_PRESENT))
1064 flags = SDHCI_CMD_RESP_NONE;
1065 else if (cmd->flags & MMC_RSP_136)
1066 flags = SDHCI_CMD_RESP_LONG;
1067 else if (cmd->flags & MMC_RSP_BUSY)
1068 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1070 flags = SDHCI_CMD_RESP_SHORT;
1072 if (cmd->flags & MMC_RSP_CRC)
1073 flags |= SDHCI_CMD_CRC;
1074 if (cmd->flags & MMC_RSP_OPCODE)
1075 flags |= SDHCI_CMD_INDEX;
1077 /* CMD19 is special in that the Data Present Select should be set */
1078 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1079 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
1080 flags |= SDHCI_CMD_DATA;
1082 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
1084 EXPORT_SYMBOL_GPL(sdhci_send_command);
1086 static void sdhci_finish_command(struct sdhci_host *host)
1090 BUG_ON(host->cmd == NULL);
1092 if (host->cmd->flags & MMC_RSP_PRESENT) {
1093 if (host->cmd->flags & MMC_RSP_136) {
1094 /* CRC is stripped so we need to do some shifting. */
1095 for (i = 0;i < 4;i++) {
1096 host->cmd->resp[i] = sdhci_readl(host,
1097 SDHCI_RESPONSE + (3-i)*4) << 8;
1099 host->cmd->resp[i] |=
1101 SDHCI_RESPONSE + (3-i)*4-1);
1104 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
1108 host->cmd->error = 0;
1110 /* Finished CMD23, now send actual command. */
1111 if (host->cmd == host->mrq->sbc) {
1113 sdhci_send_command(host, host->mrq->cmd);
1116 /* Processed actual command. */
1117 if (host->data && host->data_early)
1118 sdhci_finish_data(host);
1120 if (!host->cmd->data)
1121 tasklet_schedule(&host->finish_tasklet);
1127 static u16 sdhci_get_preset_value(struct sdhci_host *host)
1131 switch (host->timing) {
1132 case MMC_TIMING_UHS_SDR12:
1133 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1135 case MMC_TIMING_UHS_SDR25:
1136 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1138 case MMC_TIMING_UHS_SDR50:
1139 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1141 case MMC_TIMING_UHS_SDR104:
1142 case MMC_TIMING_MMC_HS200:
1143 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1145 case MMC_TIMING_UHS_DDR50:
1146 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1148 case MMC_TIMING_MMC_HS400:
1149 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1152 pr_warn("%s: Invalid UHS-I mode selected\n",
1153 mmc_hostname(host->mmc));
1154 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1160 void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1162 int div = 0; /* Initialized for compiler warning */
1163 int real_div = div, clk_mul = 1;
1165 unsigned long timeout;
1167 host->mmc->actual_clock = 0;
1169 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1174 if (host->version >= SDHCI_SPEC_300) {
1175 if (host->preset_enabled) {
1178 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1179 pre_val = sdhci_get_preset_value(host);
1180 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1181 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1182 if (host->clk_mul &&
1183 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1184 clk = SDHCI_PROG_CLOCK_MODE;
1186 clk_mul = host->clk_mul;
1188 real_div = max_t(int, 1, div << 1);
1194 * Check if the Host Controller supports Programmable Clock
1197 if (host->clk_mul) {
1198 for (div = 1; div <= 1024; div++) {
1199 if ((host->max_clk * host->clk_mul / div)
1204 * Set Programmable Clock Mode in the Clock
1207 clk = SDHCI_PROG_CLOCK_MODE;
1209 clk_mul = host->clk_mul;
1212 /* Version 3.00 divisors must be a multiple of 2. */
1213 if (host->max_clk <= clock)
1216 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1218 if ((host->max_clk / div) <= clock)
1226 /* Version 2.00 divisors must be a power of 2. */
1227 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
1228 if ((host->max_clk / div) <= clock)
1237 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1238 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1239 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1240 << SDHCI_DIVIDER_HI_SHIFT;
1241 clk |= SDHCI_CLOCK_INT_EN;
1242 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1244 /* Wait max 20 ms */
1246 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
1247 & SDHCI_CLOCK_INT_STABLE)) {
1249 pr_err("%s: Internal clock never "
1250 "stabilised.\n", mmc_hostname(host->mmc));
1251 sdhci_dumpregs(host);
1258 clk |= SDHCI_CLOCK_CARD_EN;
1259 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1261 EXPORT_SYMBOL_GPL(sdhci_set_clock);
1263 static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1266 struct mmc_host *mmc = host->mmc;
1269 if (!IS_ERR(mmc->supply.vmmc)) {
1270 spin_unlock_irq(&host->lock);
1271 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1272 spin_lock_irq(&host->lock);
1274 if (mode != MMC_POWER_OFF)
1275 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1277 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1282 if (mode != MMC_POWER_OFF) {
1284 case MMC_VDD_165_195:
1285 pwr = SDHCI_POWER_180;
1289 pwr = SDHCI_POWER_300;
1293 pwr = SDHCI_POWER_330;
1300 if (host->pwr == pwr)
1306 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1307 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1308 sdhci_runtime_pm_bus_off(host);
1312 * Spec says that we should clear the power reg before setting
1313 * a new value. Some controllers don't seem to like this though.
1315 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1316 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1319 * At least the Marvell CaFe chip gets confused if we set the
1320 * voltage and set turn on power at the same time, so set the
1323 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1324 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1326 pwr |= SDHCI_POWER_ON;
1328 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1330 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1331 sdhci_runtime_pm_bus_on(host);
1334 * Some controllers need an extra 10ms delay of 10ms before
1335 * they can apply clock after applying power
1337 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1342 /*****************************************************************************\
1346 \*****************************************************************************/
1348 static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1350 struct sdhci_host *host;
1352 unsigned long flags;
1355 host = mmc_priv(mmc);
1357 sdhci_runtime_pm_get(host);
1359 present = mmc_gpio_get_cd(host->mmc);
1361 spin_lock_irqsave(&host->lock, flags);
1363 WARN_ON(host->mrq != NULL);
1365 #ifndef SDHCI_USE_LEDS_CLASS
1366 sdhci_activate_led(host);
1370 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1371 * requests if Auto-CMD12 is enabled.
1373 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
1375 mrq->data->stop = NULL;
1383 * Firstly check card presence from cd-gpio. The return could
1384 * be one of the following possibilities:
1385 * negative: cd-gpio is not available
1386 * zero: cd-gpio is used, and card is removed
1387 * one: cd-gpio is used, and card is present
1390 /* If polling, assume that the card is always present. */
1391 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1394 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1398 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
1399 host->mrq->cmd->error = -ENOMEDIUM;
1400 tasklet_schedule(&host->finish_tasklet);
1404 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1406 * Check if the re-tuning timer has already expired and there
1407 * is no on-going data transfer and DAT0 is not busy. If so,
1408 * we need to execute tuning procedure before sending command.
1410 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1411 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1412 (present_state & SDHCI_DATA_0_LVL_MASK)) {
1414 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1416 mmc->card->type == MMC_TYPE_MMC ?
1417 MMC_SEND_TUNING_BLOCK_HS200 :
1418 MMC_SEND_TUNING_BLOCK;
1420 /* Here we need to set the host->mrq to NULL,
1421 * in case the pending finish_tasklet
1422 * finishes it incorrectly.
1426 spin_unlock_irqrestore(&host->lock, flags);
1427 sdhci_execute_tuning(mmc, tuning_opcode);
1428 spin_lock_irqsave(&host->lock, flags);
1430 /* Restore original mmc_request structure */
1435 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
1436 sdhci_send_command(host, mrq->sbc);
1438 sdhci_send_command(host, mrq->cmd);
1442 spin_unlock_irqrestore(&host->lock, flags);
1445 void sdhci_set_bus_width(struct sdhci_host *host, int width)
1449 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1450 if (width == MMC_BUS_WIDTH_8) {
1451 ctrl &= ~SDHCI_CTRL_4BITBUS;
1452 if (host->version >= SDHCI_SPEC_300)
1453 ctrl |= SDHCI_CTRL_8BITBUS;
1455 if (host->version >= SDHCI_SPEC_300)
1456 ctrl &= ~SDHCI_CTRL_8BITBUS;
1457 if (width == MMC_BUS_WIDTH_4)
1458 ctrl |= SDHCI_CTRL_4BITBUS;
1460 ctrl &= ~SDHCI_CTRL_4BITBUS;
1462 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1464 EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1466 void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1470 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1471 /* Select Bus Speed Mode for host */
1472 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1473 if ((timing == MMC_TIMING_MMC_HS200) ||
1474 (timing == MMC_TIMING_UHS_SDR104))
1475 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1476 else if (timing == MMC_TIMING_UHS_SDR12)
1477 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1478 else if (timing == MMC_TIMING_UHS_SDR25)
1479 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1480 else if (timing == MMC_TIMING_UHS_SDR50)
1481 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1482 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1483 (timing == MMC_TIMING_MMC_DDR52))
1484 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1485 else if (timing == MMC_TIMING_MMC_HS400)
1486 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
1487 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1489 EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1491 static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
1493 unsigned long flags;
1495 struct mmc_host *mmc = host->mmc;
1497 spin_lock_irqsave(&host->lock, flags);
1499 if (host->flags & SDHCI_DEVICE_DEAD) {
1500 spin_unlock_irqrestore(&host->lock, flags);
1501 if (!IS_ERR(mmc->supply.vmmc) &&
1502 ios->power_mode == MMC_POWER_OFF)
1503 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1508 * Reset the chip on each power off.
1509 * Should clear out any weird states.
1511 if (ios->power_mode == MMC_POWER_OFF) {
1512 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1516 if (host->version >= SDHCI_SPEC_300 &&
1517 (ios->power_mode == MMC_POWER_UP) &&
1518 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
1519 sdhci_enable_preset_value(host, false);
1521 if (!ios->clock || ios->clock != host->clock) {
1522 host->ops->set_clock(host, ios->clock);
1523 host->clock = ios->clock;
1525 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1527 host->timeout_clk = host->mmc->actual_clock ?
1528 host->mmc->actual_clock / 1000 :
1530 host->mmc->max_busy_timeout =
1531 host->ops->get_max_timeout_count ?
1532 host->ops->get_max_timeout_count(host) :
1534 host->mmc->max_busy_timeout /= host->timeout_clk;
1538 sdhci_set_power(host, ios->power_mode, ios->vdd);
1540 if (host->ops->platform_send_init_74_clocks)
1541 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1543 host->ops->set_bus_width(host, ios->bus_width);
1545 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1547 if ((ios->timing == MMC_TIMING_SD_HS ||
1548 ios->timing == MMC_TIMING_MMC_HS)
1549 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
1550 ctrl |= SDHCI_CTRL_HISPD;
1552 ctrl &= ~SDHCI_CTRL_HISPD;
1554 if (host->version >= SDHCI_SPEC_300) {
1557 /* In case of UHS-I modes, set High Speed Enable */
1558 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1559 (ios->timing == MMC_TIMING_MMC_HS200) ||
1560 (ios->timing == MMC_TIMING_MMC_DDR52) ||
1561 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1562 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1563 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1564 (ios->timing == MMC_TIMING_UHS_SDR25))
1565 ctrl |= SDHCI_CTRL_HISPD;
1567 if (!host->preset_enabled) {
1568 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1570 * We only need to set Driver Strength if the
1571 * preset value enable is not set.
1573 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1574 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1575 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1576 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1577 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1578 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1580 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1583 * According to SDHC Spec v3.00, if the Preset Value
1584 * Enable in the Host Control 2 register is set, we
1585 * need to reset SD Clock Enable before changing High
1586 * Speed Enable to avoid generating clock gliches.
1589 /* Reset SD Clock Enable */
1590 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1591 clk &= ~SDHCI_CLOCK_CARD_EN;
1592 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1594 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1596 /* Re-enable SD Clock */
1597 host->ops->set_clock(host, host->clock);
1600 /* Reset SD Clock Enable */
1601 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1602 clk &= ~SDHCI_CLOCK_CARD_EN;
1603 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1605 host->ops->set_uhs_signaling(host, ios->timing);
1606 host->timing = ios->timing;
1608 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1609 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1610 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1611 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1612 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1613 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1616 sdhci_enable_preset_value(host, true);
1617 preset = sdhci_get_preset_value(host);
1618 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1619 >> SDHCI_PRESET_DRV_SHIFT;
1622 /* Re-enable SD Clock */
1623 host->ops->set_clock(host, host->clock);
1625 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1628 * Some (ENE) controllers go apeshit on some ios operation,
1629 * signalling timeout and CRC errors even on CMD0. Resetting
1630 * it on each ios seems to solve the problem.
1632 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
1633 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1636 spin_unlock_irqrestore(&host->lock, flags);
1639 static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1641 struct sdhci_host *host = mmc_priv(mmc);
1643 sdhci_runtime_pm_get(host);
1644 sdhci_do_set_ios(host, ios);
1645 sdhci_runtime_pm_put(host);
1648 static int sdhci_do_get_cd(struct sdhci_host *host)
1650 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1652 if (host->flags & SDHCI_DEVICE_DEAD)
1655 /* If polling/nonremovable, assume that the card is always present. */
1656 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1657 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1660 /* Try slot gpio detect */
1661 if (!IS_ERR_VALUE(gpio_cd))
1664 /* Host native card detect */
1665 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1668 static int sdhci_get_cd(struct mmc_host *mmc)
1670 struct sdhci_host *host = mmc_priv(mmc);
1673 sdhci_runtime_pm_get(host);
1674 ret = sdhci_do_get_cd(host);
1675 sdhci_runtime_pm_put(host);
1679 static int sdhci_check_ro(struct sdhci_host *host)
1681 unsigned long flags;
1684 spin_lock_irqsave(&host->lock, flags);
1686 if (host->flags & SDHCI_DEVICE_DEAD)
1688 else if (host->ops->get_ro)
1689 is_readonly = host->ops->get_ro(host);
1691 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1692 & SDHCI_WRITE_PROTECT);
1694 spin_unlock_irqrestore(&host->lock, flags);
1696 /* This quirk needs to be replaced by a callback-function later */
1697 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1698 !is_readonly : is_readonly;
1701 #define SAMPLE_COUNT 5
1703 static int sdhci_do_get_ro(struct sdhci_host *host)
1707 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1708 return sdhci_check_ro(host);
1711 for (i = 0; i < SAMPLE_COUNT; i++) {
1712 if (sdhci_check_ro(host)) {
1713 if (++ro_count > SAMPLE_COUNT / 2)
1721 static void sdhci_hw_reset(struct mmc_host *mmc)
1723 struct sdhci_host *host = mmc_priv(mmc);
1725 if (host->ops && host->ops->hw_reset)
1726 host->ops->hw_reset(host);
1729 static int sdhci_get_ro(struct mmc_host *mmc)
1731 struct sdhci_host *host = mmc_priv(mmc);
1734 sdhci_runtime_pm_get(host);
1735 ret = sdhci_do_get_ro(host);
1736 sdhci_runtime_pm_put(host);
1740 static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1742 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
1744 host->ier |= SDHCI_INT_CARD_INT;
1746 host->ier &= ~SDHCI_INT_CARD_INT;
1748 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1749 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1754 static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1756 struct sdhci_host *host = mmc_priv(mmc);
1757 unsigned long flags;
1759 sdhci_runtime_pm_get(host);
1761 spin_lock_irqsave(&host->lock, flags);
1763 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1765 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1767 sdhci_enable_sdio_irq_nolock(host, enable);
1768 spin_unlock_irqrestore(&host->lock, flags);
1770 sdhci_runtime_pm_put(host);
1773 static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1774 struct mmc_ios *ios)
1776 struct mmc_host *mmc = host->mmc;
1781 * Signal Voltage Switching is only applicable for Host Controllers
1784 if (host->version < SDHCI_SPEC_300)
1787 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1789 switch (ios->signal_voltage) {
1790 case MMC_SIGNAL_VOLTAGE_330:
1791 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1792 ctrl &= ~SDHCI_CTRL_VDD_180;
1793 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1795 if (!IS_ERR(mmc->supply.vqmmc)) {
1796 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1799 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1805 usleep_range(5000, 5500);
1807 /* 3.3V regulator output should be stable within 5 ms */
1808 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1809 if (!(ctrl & SDHCI_CTRL_VDD_180))
1812 pr_warn("%s: 3.3V regulator output did not became stable\n",
1816 case MMC_SIGNAL_VOLTAGE_180:
1817 if (!IS_ERR(mmc->supply.vqmmc)) {
1818 ret = regulator_set_voltage(mmc->supply.vqmmc,
1821 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1828 * Enable 1.8V Signal Enable in the Host Control2
1831 ctrl |= SDHCI_CTRL_VDD_180;
1832 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1834 /* Some controller need to do more when switching */
1835 if (host->ops->voltage_switch)
1836 host->ops->voltage_switch(host);
1838 /* 1.8V regulator output should be stable within 5 ms */
1839 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1840 if (ctrl & SDHCI_CTRL_VDD_180)
1843 pr_warn("%s: 1.8V regulator output did not became stable\n",
1847 case MMC_SIGNAL_VOLTAGE_120:
1848 if (!IS_ERR(mmc->supply.vqmmc)) {
1849 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1852 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1859 /* No signal voltage switch required */
1864 static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1865 struct mmc_ios *ios)
1867 struct sdhci_host *host = mmc_priv(mmc);
1870 if (host->version < SDHCI_SPEC_300)
1872 sdhci_runtime_pm_get(host);
1873 err = sdhci_do_start_signal_voltage_switch(host, ios);
1874 sdhci_runtime_pm_put(host);
1878 static int sdhci_card_busy(struct mmc_host *mmc)
1880 struct sdhci_host *host = mmc_priv(mmc);
1883 sdhci_runtime_pm_get(host);
1884 /* Check whether DAT[3:0] is 0000 */
1885 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1886 sdhci_runtime_pm_put(host);
1888 return !(present_state & SDHCI_DATA_LVL_MASK);
1891 static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1893 struct sdhci_host *host = mmc_priv(mmc);
1894 unsigned long flags;
1896 spin_lock_irqsave(&host->lock, flags);
1897 host->flags |= SDHCI_HS400_TUNING;
1898 spin_unlock_irqrestore(&host->lock, flags);
1903 static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1905 struct sdhci_host *host = mmc_priv(mmc);
1907 int tuning_loop_counter = MAX_TUNING_LOOP;
1909 unsigned long flags;
1910 unsigned int tuning_count = 0;
1913 sdhci_runtime_pm_get(host);
1914 spin_lock_irqsave(&host->lock, flags);
1916 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1917 host->flags &= ~SDHCI_HS400_TUNING;
1919 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1920 tuning_count = host->tuning_count;
1923 * The Host Controller needs tuning only in case of SDR104 mode
1924 * and for SDR50 mode when Use Tuning for SDR50 is set in the
1925 * Capabilities register.
1926 * If the Host Controller supports the HS200 mode then the
1927 * tuning function has to be executed.
1929 switch (host->timing) {
1930 /* HS400 tuning is done in HS200 mode */
1931 case MMC_TIMING_MMC_HS400:
1935 case MMC_TIMING_MMC_HS200:
1937 * Periodic re-tuning for HS400 is not expected to be needed, so
1944 case MMC_TIMING_UHS_SDR104:
1947 case MMC_TIMING_UHS_SDR50:
1948 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1949 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1957 if (host->ops->platform_execute_tuning) {
1958 spin_unlock_irqrestore(&host->lock, flags);
1959 err = host->ops->platform_execute_tuning(host, opcode);
1960 sdhci_runtime_pm_put(host);
1964 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1965 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1966 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1967 ctrl |= SDHCI_CTRL_TUNED_CLK;
1968 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1971 * As per the Host Controller spec v3.00, tuning command
1972 * generates Buffer Read Ready interrupt, so enable that.
1974 * Note: The spec clearly says that when tuning sequence
1975 * is being performed, the controller does not generate
1976 * interrupts other than Buffer Read Ready interrupt. But
1977 * to make sure we don't hit a controller bug, we _only_
1978 * enable Buffer Read Ready interrupt here.
1980 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1981 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1984 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1985 * of loops reaches 40 times or a timeout of 150ms occurs.
1988 struct mmc_command cmd = {0};
1989 struct mmc_request mrq = {NULL};
1991 cmd.opcode = opcode;
1993 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1998 if (tuning_loop_counter-- == 0)
2005 * In response to CMD19, the card sends 64 bytes of tuning
2006 * block to the Host Controller. So we set the block size
2009 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2010 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2011 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2013 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2014 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2017 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2022 * The tuning block is sent by the card to the host controller.
2023 * So we set the TRNS_READ bit in the Transfer Mode register.
2024 * This also takes care of setting DMA Enable and Multi Block
2025 * Select in the same register to 0.
2027 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2029 sdhci_send_command(host, &cmd);
2034 spin_unlock_irqrestore(&host->lock, flags);
2035 /* Wait for Buffer Read Ready interrupt */
2036 wait_event_interruptible_timeout(host->buf_ready_int,
2037 (host->tuning_done == 1),
2038 msecs_to_jiffies(50));
2039 spin_lock_irqsave(&host->lock, flags);
2041 if (!host->tuning_done) {
2042 pr_info(DRIVER_NAME ": Timeout waiting for "
2043 "Buffer Read Ready interrupt during tuning "
2044 "procedure, falling back to fixed sampling "
2046 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2047 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2048 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2049 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2055 host->tuning_done = 0;
2057 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2059 /* eMMC spec does not require a delay between tuning cycles */
2060 if (opcode == MMC_SEND_TUNING_BLOCK)
2062 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2065 * The Host Driver has exhausted the maximum number of loops allowed,
2066 * so use fixed sampling frequency.
2068 if (tuning_loop_counter < 0) {
2069 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2070 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2072 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2073 pr_info(DRIVER_NAME ": Tuning procedure"
2074 " failed, falling back to fixed sampling"
2080 host->flags &= ~SDHCI_NEEDS_RETUNING;
2083 host->flags |= SDHCI_USING_RETUNING_TIMER;
2084 mod_timer(&host->tuning_timer, jiffies + tuning_count * HZ);
2088 * In case tuning fails, host controllers which support re-tuning can
2089 * try tuning again at a later time, when the re-tuning timer expires.
2090 * So for these controllers, we return 0. Since there might be other
2091 * controllers who do not have this capability, we return error for
2092 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2093 * a retuning timer to do the retuning for the card.
2095 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
2098 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2099 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2101 spin_unlock_irqrestore(&host->lock, flags);
2102 sdhci_runtime_pm_put(host);
2108 static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
2110 /* Host Controller v3.00 defines preset value registers */
2111 if (host->version < SDHCI_SPEC_300)
2115 * We only enable or disable Preset Value if they are not already
2116 * enabled or disabled respectively. Otherwise, we bail out.
2118 if (host->preset_enabled != enable) {
2119 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2122 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2124 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2126 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2129 host->flags |= SDHCI_PV_ENABLED;
2131 host->flags &= ~SDHCI_PV_ENABLED;
2133 host->preset_enabled = enable;
2137 static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2140 struct sdhci_host *host = mmc_priv(mmc);
2141 struct mmc_data *data = mrq->data;
2143 if (host->flags & SDHCI_REQ_USE_DMA) {
2144 if (data->host_cookie)
2145 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2146 data->flags & MMC_DATA_WRITE ?
2147 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2148 mrq->data->host_cookie = 0;
2152 static int sdhci_pre_dma_transfer(struct sdhci_host *host,
2153 struct mmc_data *data,
2154 struct sdhci_host_next *next)
2158 if (!next && data->host_cookie &&
2159 data->host_cookie != host->next_data.cookie) {
2160 pr_debug(DRIVER_NAME "[%s] invalid cookie: %d, next-cookie %d\n",
2161 __func__, data->host_cookie, host->next_data.cookie);
2162 data->host_cookie = 0;
2165 /* Check if next job is already prepared */
2167 (!next && data->host_cookie != host->next_data.cookie)) {
2168 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
2170 data->flags & MMC_DATA_WRITE ?
2171 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2174 sg_count = host->next_data.sg_count;
2175 host->next_data.sg_count = 0;
2183 next->sg_count = sg_count;
2184 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
2186 host->sg_count = sg_count;
2191 static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2194 struct sdhci_host *host = mmc_priv(mmc);
2196 if (mrq->data->host_cookie) {
2197 mrq->data->host_cookie = 0;
2201 if (host->flags & SDHCI_REQ_USE_DMA)
2202 if (sdhci_pre_dma_transfer(host,
2204 &host->next_data) < 0)
2205 mrq->data->host_cookie = 0;
2208 static void sdhci_card_event(struct mmc_host *mmc)
2210 struct sdhci_host *host = mmc_priv(mmc);
2211 unsigned long flags;
2214 /* First check if client has provided their own card event */
2215 if (host->ops->card_event)
2216 host->ops->card_event(host);
2218 present = sdhci_do_get_cd(host);
2220 spin_lock_irqsave(&host->lock, flags);
2222 /* Check host->mrq first in case we are runtime suspended */
2223 if (host->mrq && !present) {
2224 pr_err("%s: Card removed during transfer!\n",
2225 mmc_hostname(host->mmc));
2226 pr_err("%s: Resetting controller.\n",
2227 mmc_hostname(host->mmc));
2229 sdhci_do_reset(host, SDHCI_RESET_CMD);
2230 sdhci_do_reset(host, SDHCI_RESET_DATA);
2232 host->mrq->cmd->error = -ENOMEDIUM;
2233 tasklet_schedule(&host->finish_tasklet);
2236 spin_unlock_irqrestore(&host->lock, flags);
2239 static const struct mmc_host_ops sdhci_ops = {
2240 .request = sdhci_request,
2241 .post_req = sdhci_post_req,
2242 .pre_req = sdhci_pre_req,
2243 .set_ios = sdhci_set_ios,
2244 .get_cd = sdhci_get_cd,
2245 .get_ro = sdhci_get_ro,
2246 .hw_reset = sdhci_hw_reset,
2247 .enable_sdio_irq = sdhci_enable_sdio_irq,
2248 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2249 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
2250 .execute_tuning = sdhci_execute_tuning,
2251 .card_event = sdhci_card_event,
2252 .card_busy = sdhci_card_busy,
2255 /*****************************************************************************\
2259 \*****************************************************************************/
2261 static void sdhci_tasklet_finish(unsigned long param)
2263 struct sdhci_host *host;
2264 unsigned long flags;
2265 struct mmc_request *mrq;
2267 host = (struct sdhci_host*)param;
2269 spin_lock_irqsave(&host->lock, flags);
2272 * If this tasklet gets rescheduled while running, it will
2273 * be run again afterwards but without any active request.
2276 spin_unlock_irqrestore(&host->lock, flags);
2280 del_timer(&host->timer);
2285 * The controller needs a reset of internal state machines
2286 * upon error conditions.
2288 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
2289 ((mrq->cmd && mrq->cmd->error) ||
2290 (mrq->sbc && mrq->sbc->error) ||
2291 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2292 (mrq->data->stop && mrq->data->stop->error))) ||
2293 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
2295 /* Some controllers need this kick or reset won't work here */
2296 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
2297 /* This is to force an update */
2298 host->ops->set_clock(host, host->clock);
2300 /* Spec says we should do both at the same time, but Ricoh
2301 controllers do not like that. */
2302 sdhci_do_reset(host, SDHCI_RESET_CMD);
2303 sdhci_do_reset(host, SDHCI_RESET_DATA);
2310 #ifndef SDHCI_USE_LEDS_CLASS
2311 sdhci_deactivate_led(host);
2315 spin_unlock_irqrestore(&host->lock, flags);
2317 mmc_request_done(host->mmc, mrq);
2318 sdhci_runtime_pm_put(host);
2321 static void sdhci_timeout_timer(unsigned long data)
2323 struct sdhci_host *host;
2324 unsigned long flags;
2326 host = (struct sdhci_host*)data;
2328 spin_lock_irqsave(&host->lock, flags);
2331 pr_err("%s: Timeout waiting for hardware "
2332 "interrupt.\n", mmc_hostname(host->mmc));
2333 sdhci_dumpregs(host);
2336 host->data->error = -ETIMEDOUT;
2337 sdhci_finish_data(host);
2340 host->cmd->error = -ETIMEDOUT;
2342 host->mrq->cmd->error = -ETIMEDOUT;
2344 tasklet_schedule(&host->finish_tasklet);
2349 spin_unlock_irqrestore(&host->lock, flags);
2352 static void sdhci_tuning_timer(unsigned long data)
2354 struct sdhci_host *host;
2355 unsigned long flags;
2357 host = (struct sdhci_host *)data;
2359 spin_lock_irqsave(&host->lock, flags);
2361 host->flags |= SDHCI_NEEDS_RETUNING;
2363 spin_unlock_irqrestore(&host->lock, flags);
2366 /*****************************************************************************\
2368 * Interrupt handling *
2370 \*****************************************************************************/
2372 static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
2374 BUG_ON(intmask == 0);
2377 pr_err("%s: Got command interrupt 0x%08x even "
2378 "though no command operation was in progress.\n",
2379 mmc_hostname(host->mmc), (unsigned)intmask);
2380 sdhci_dumpregs(host);
2384 if (intmask & SDHCI_INT_TIMEOUT)
2385 host->cmd->error = -ETIMEDOUT;
2386 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2388 host->cmd->error = -EILSEQ;
2390 if (host->cmd->error) {
2391 tasklet_schedule(&host->finish_tasklet);
2396 * The host can send and interrupt when the busy state has
2397 * ended, allowing us to wait without wasting CPU cycles.
2398 * Unfortunately this is overloaded on the "data complete"
2399 * interrupt, so we need to take some care when handling
2402 * Note: The 1.0 specification is a bit ambiguous about this
2403 * feature so there might be some problems with older
2406 if (host->cmd->flags & MMC_RSP_BUSY) {
2407 if (host->cmd->data)
2408 DBG("Cannot wait for busy signal when also "
2409 "doing a data transfer");
2410 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2411 && !host->busy_handle) {
2412 /* Mark that command complete before busy is ended */
2413 host->busy_handle = 1;
2417 /* The controller does not support the end-of-busy IRQ,
2418 * fall through and take the SDHCI_INT_RESPONSE */
2419 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2420 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2421 *mask &= ~SDHCI_INT_DATA_END;
2424 if (intmask & SDHCI_INT_RESPONSE)
2425 sdhci_finish_command(host);
2428 #ifdef CONFIG_MMC_DEBUG
2429 static void sdhci_adma_show_error(struct sdhci_host *host)
2431 const char *name = mmc_hostname(host->mmc);
2432 void *desc = host->adma_table;
2434 sdhci_dumpregs(host);
2437 struct sdhci_adma2_64_desc *dma_desc = desc;
2439 if (host->flags & SDHCI_USE_64_BIT_DMA)
2440 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2441 name, desc, le32_to_cpu(dma_desc->addr_hi),
2442 le32_to_cpu(dma_desc->addr_lo),
2443 le16_to_cpu(dma_desc->len),
2444 le16_to_cpu(dma_desc->cmd));
2446 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2447 name, desc, le32_to_cpu(dma_desc->addr_lo),
2448 le16_to_cpu(dma_desc->len),
2449 le16_to_cpu(dma_desc->cmd));
2451 desc += host->desc_sz;
2453 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
2458 static void sdhci_adma_show_error(struct sdhci_host *host) { }
2461 static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2464 BUG_ON(intmask == 0);
2466 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2467 if (intmask & SDHCI_INT_DATA_AVAIL) {
2468 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2469 if (command == MMC_SEND_TUNING_BLOCK ||
2470 command == MMC_SEND_TUNING_BLOCK_HS200) {
2471 host->tuning_done = 1;
2472 wake_up(&host->buf_ready_int);
2479 * The "data complete" interrupt is also used to
2480 * indicate that a busy state has ended. See comment
2481 * above in sdhci_cmd_irq().
2483 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2484 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2485 host->cmd->error = -ETIMEDOUT;
2486 tasklet_schedule(&host->finish_tasklet);
2489 if (intmask & SDHCI_INT_DATA_END) {
2491 * Some cards handle busy-end interrupt
2492 * before the command completed, so make
2493 * sure we do things in the proper order.
2495 if (host->busy_handle)
2496 sdhci_finish_command(host);
2498 host->busy_handle = 1;
2503 pr_err("%s: Got data interrupt 0x%08x even "
2504 "though no data operation was in progress.\n",
2505 mmc_hostname(host->mmc), (unsigned)intmask);
2506 sdhci_dumpregs(host);
2511 if (intmask & SDHCI_INT_DATA_TIMEOUT)
2512 host->data->error = -ETIMEDOUT;
2513 else if (intmask & SDHCI_INT_DATA_END_BIT)
2514 host->data->error = -EILSEQ;
2515 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2516 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2518 host->data->error = -EILSEQ;
2519 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2520 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
2521 sdhci_adma_show_error(host);
2522 host->data->error = -EIO;
2523 if (host->ops->adma_workaround)
2524 host->ops->adma_workaround(host, intmask);
2527 if (host->data->error)
2528 sdhci_finish_data(host);
2530 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
2531 sdhci_transfer_pio(host);
2534 * We currently don't do anything fancy with DMA
2535 * boundaries, but as we can't disable the feature
2536 * we need to at least restart the transfer.
2538 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2539 * should return a valid address to continue from, but as
2540 * some controllers are faulty, don't trust them.
2542 if (intmask & SDHCI_INT_DMA_END) {
2543 u32 dmastart, dmanow;
2544 dmastart = sg_dma_address(host->data->sg);
2545 dmanow = dmastart + host->data->bytes_xfered;
2547 * Force update to the next DMA block boundary.
2550 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2551 SDHCI_DEFAULT_BOUNDARY_SIZE;
2552 host->data->bytes_xfered = dmanow - dmastart;
2553 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2555 mmc_hostname(host->mmc), dmastart,
2556 host->data->bytes_xfered, dmanow);
2557 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2560 if (intmask & SDHCI_INT_DATA_END) {
2563 * Data managed to finish before the
2564 * command completed. Make sure we do
2565 * things in the proper order.
2567 host->data_early = 1;
2569 sdhci_finish_data(host);
2575 static irqreturn_t sdhci_irq(int irq, void *dev_id)
2577 irqreturn_t result = IRQ_NONE;
2578 struct sdhci_host *host = dev_id;
2579 u32 intmask, mask, unexpected = 0;
2582 spin_lock(&host->lock);
2584 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
2585 spin_unlock(&host->lock);
2589 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2590 if (!intmask || intmask == 0xffffffff) {
2596 /* Clear selected interrupts. */
2597 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2598 SDHCI_INT_BUS_POWER);
2599 sdhci_writel(host, mask, SDHCI_INT_STATUS);
2601 DBG("*** %s got interrupt: 0x%08x\n",
2602 mmc_hostname(host->mmc), intmask);
2604 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2605 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2609 * There is a observation on i.mx esdhc. INSERT
2610 * bit will be immediately set again when it gets
2611 * cleared, if a card is inserted. We have to mask
2612 * the irq to prevent interrupt storm which will
2613 * freeze the system. And the REMOVE gets the
2616 * More testing are needed here to ensure it works
2617 * for other platforms though.
2619 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2620 SDHCI_INT_CARD_REMOVE);
2621 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2622 SDHCI_INT_CARD_INSERT;
2623 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2624 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2626 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2627 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2629 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2630 SDHCI_INT_CARD_REMOVE);
2631 result = IRQ_WAKE_THREAD;
2634 if (intmask & SDHCI_INT_CMD_MASK)
2635 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2638 if (intmask & SDHCI_INT_DATA_MASK)
2639 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2641 if (intmask & SDHCI_INT_BUS_POWER)
2642 pr_err("%s: Card is consuming too much power!\n",
2643 mmc_hostname(host->mmc));
2645 if (intmask & SDHCI_INT_CARD_INT) {
2646 sdhci_enable_sdio_irq_nolock(host, false);
2647 host->thread_isr |= SDHCI_INT_CARD_INT;
2648 result = IRQ_WAKE_THREAD;
2651 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2652 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2653 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2654 SDHCI_INT_CARD_INT);
2657 unexpected |= intmask;
2658 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2661 if (result == IRQ_NONE)
2662 result = IRQ_HANDLED;
2664 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2665 } while (intmask && --max_loops);
2667 spin_unlock(&host->lock);
2670 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2671 mmc_hostname(host->mmc), unexpected);
2672 sdhci_dumpregs(host);
2678 static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2680 struct sdhci_host *host = dev_id;
2681 unsigned long flags;
2684 spin_lock_irqsave(&host->lock, flags);
2685 isr = host->thread_isr;
2686 host->thread_isr = 0;
2687 spin_unlock_irqrestore(&host->lock, flags);
2689 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2690 sdhci_card_event(host->mmc);
2691 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2694 if (isr & SDHCI_INT_CARD_INT) {
2695 sdio_run_irqs(host->mmc);
2697 spin_lock_irqsave(&host->lock, flags);
2698 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2699 sdhci_enable_sdio_irq_nolock(host, true);
2700 spin_unlock_irqrestore(&host->lock, flags);
2703 return isr ? IRQ_HANDLED : IRQ_NONE;
2706 /*****************************************************************************\
2710 \*****************************************************************************/
2713 void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2716 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2717 | SDHCI_WAKE_ON_INT;
2719 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2721 /* Avoid fake wake up */
2722 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2723 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2724 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2726 EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2728 static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2731 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2732 | SDHCI_WAKE_ON_INT;
2734 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2736 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2739 int sdhci_suspend_host(struct sdhci_host *host)
2741 sdhci_disable_card_detection(host);
2743 /* Disable tuning since we are suspending */
2744 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2745 del_timer_sync(&host->tuning_timer);
2746 host->flags &= ~SDHCI_NEEDS_RETUNING;
2749 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2751 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2752 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2753 free_irq(host->irq, host);
2755 sdhci_enable_irq_wakeups(host);
2756 enable_irq_wake(host->irq);
2761 EXPORT_SYMBOL_GPL(sdhci_suspend_host);
2763 int sdhci_resume_host(struct sdhci_host *host)
2767 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2768 if (host->ops->enable_dma)
2769 host->ops->enable_dma(host);
2772 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2773 ret = request_threaded_irq(host->irq, sdhci_irq,
2774 sdhci_thread_irq, IRQF_SHARED,
2775 mmc_hostname(host->mmc), host);
2779 sdhci_disable_irq_wakeups(host);
2780 disable_irq_wake(host->irq);
2783 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2784 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2785 /* Card keeps power but host controller does not */
2786 sdhci_init(host, 0);
2789 sdhci_do_set_ios(host, &host->mmc->ios);
2791 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2795 sdhci_enable_card_detection(host);
2797 /* Set the re-tuning expiration flag */
2798 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2799 host->flags |= SDHCI_NEEDS_RETUNING;
2804 EXPORT_SYMBOL_GPL(sdhci_resume_host);
2806 static int sdhci_runtime_pm_get(struct sdhci_host *host)
2808 return pm_runtime_get_sync(host->mmc->parent);
2811 static int sdhci_runtime_pm_put(struct sdhci_host *host)
2813 pm_runtime_mark_last_busy(host->mmc->parent);
2814 return pm_runtime_put_autosuspend(host->mmc->parent);
2817 static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2819 if (host->runtime_suspended || host->bus_on)
2821 host->bus_on = true;
2822 pm_runtime_get_noresume(host->mmc->parent);
2825 static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2827 if (host->runtime_suspended || !host->bus_on)
2829 host->bus_on = false;
2830 pm_runtime_put_noidle(host->mmc->parent);
2833 int sdhci_runtime_suspend_host(struct sdhci_host *host)
2835 unsigned long flags;
2837 /* Disable tuning since we are suspending */
2838 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
2839 del_timer_sync(&host->tuning_timer);
2840 host->flags &= ~SDHCI_NEEDS_RETUNING;
2843 spin_lock_irqsave(&host->lock, flags);
2844 host->ier &= SDHCI_INT_CARD_INT;
2845 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2846 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2847 spin_unlock_irqrestore(&host->lock, flags);
2849 synchronize_hardirq(host->irq);
2851 spin_lock_irqsave(&host->lock, flags);
2852 host->runtime_suspended = true;
2853 spin_unlock_irqrestore(&host->lock, flags);
2857 EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2859 int sdhci_runtime_resume_host(struct sdhci_host *host)
2861 unsigned long flags;
2862 int host_flags = host->flags;
2864 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2865 if (host->ops->enable_dma)
2866 host->ops->enable_dma(host);
2869 sdhci_init(host, 0);
2871 /* Force clock and power re-program */
2874 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2875 sdhci_do_set_ios(host, &host->mmc->ios);
2877 if ((host_flags & SDHCI_PV_ENABLED) &&
2878 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2879 spin_lock_irqsave(&host->lock, flags);
2880 sdhci_enable_preset_value(host, true);
2881 spin_unlock_irqrestore(&host->lock, flags);
2884 /* Set the re-tuning expiration flag */
2885 if (host->flags & SDHCI_USING_RETUNING_TIMER)
2886 host->flags |= SDHCI_NEEDS_RETUNING;
2888 spin_lock_irqsave(&host->lock, flags);
2890 host->runtime_suspended = false;
2892 /* Enable SDIO IRQ */
2893 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2894 sdhci_enable_sdio_irq_nolock(host, true);
2896 /* Enable Card Detection */
2897 sdhci_enable_card_detection(host);
2899 spin_unlock_irqrestore(&host->lock, flags);
2903 EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2905 #endif /* CONFIG_PM */
2907 /*****************************************************************************\
2909 * Device allocation/registration *
2911 \*****************************************************************************/
2913 struct sdhci_host *sdhci_alloc_host(struct device *dev,
2916 struct mmc_host *mmc;
2917 struct sdhci_host *host;
2919 WARN_ON(dev == NULL);
2921 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
2923 return ERR_PTR(-ENOMEM);
2925 host = mmc_priv(mmc);
2931 EXPORT_SYMBOL_GPL(sdhci_alloc_host);
2933 int sdhci_add_host(struct sdhci_host *host)
2935 struct mmc_host *mmc;
2936 u32 caps[2] = {0, 0};
2937 u32 max_current_caps;
2938 unsigned int ocr_avail;
2939 unsigned int override_timeout_clk;
2942 WARN_ON(host == NULL);
2949 host->quirks = debug_quirks;
2951 host->quirks2 = debug_quirks2;
2953 override_timeout_clk = host->timeout_clk;
2955 sdhci_do_reset(host, SDHCI_RESET_ALL);
2957 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
2958 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2959 >> SDHCI_SPEC_VER_SHIFT;
2960 if (host->version > SDHCI_SPEC_300) {
2961 pr_err("%s: Unknown controller version (%d). "
2962 "You may experience problems.\n", mmc_hostname(mmc),
2966 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
2967 sdhci_readl(host, SDHCI_CAPABILITIES);
2969 if (host->version >= SDHCI_SPEC_300)
2970 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2972 sdhci_readl(host, SDHCI_CAPABILITIES_1);
2974 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
2975 host->flags |= SDHCI_USE_SDMA;
2976 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
2977 DBG("Controller doesn't have SDMA capability\n");
2979 host->flags |= SDHCI_USE_SDMA;
2981 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
2982 (host->flags & SDHCI_USE_SDMA)) {
2983 DBG("Disabling DMA as it is marked broken\n");
2984 host->flags &= ~SDHCI_USE_SDMA;
2987 if ((host->version >= SDHCI_SPEC_200) &&
2988 (caps[0] & SDHCI_CAN_DO_ADMA2))
2989 host->flags |= SDHCI_USE_ADMA;
2991 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2992 (host->flags & SDHCI_USE_ADMA)) {
2993 DBG("Disabling ADMA as it is marked broken\n");
2994 host->flags &= ~SDHCI_USE_ADMA;
2998 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2999 * and *must* do 64-bit DMA. A driver has the opportunity to change
3000 * that during the first call to ->enable_dma(). Similarly
3001 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3004 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
3005 host->flags |= SDHCI_USE_64_BIT_DMA;
3007 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
3008 if (host->ops->enable_dma) {
3009 if (host->ops->enable_dma(host)) {
3010 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3013 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3018 /* SDMA does not support 64-bit DMA */
3019 if (host->flags & SDHCI_USE_64_BIT_DMA)
3020 host->flags &= ~SDHCI_USE_SDMA;
3022 if (host->flags & SDHCI_USE_ADMA) {
3024 * The DMA descriptor table size is calculated as the maximum
3025 * number of segments times 2, to allow for an alignment
3026 * descriptor for each segment, plus 1 for a nop end descriptor,
3027 * all multipled by the descriptor size.
3029 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3030 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3031 SDHCI_ADMA2_64_DESC_SZ;
3032 host->align_buffer_sz = SDHCI_MAX_SEGS *
3033 SDHCI_ADMA2_64_ALIGN;
3034 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
3035 host->align_sz = SDHCI_ADMA2_64_ALIGN;
3036 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
3038 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3039 SDHCI_ADMA2_32_DESC_SZ;
3040 host->align_buffer_sz = SDHCI_MAX_SEGS *
3041 SDHCI_ADMA2_32_ALIGN;
3042 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
3043 host->align_sz = SDHCI_ADMA2_32_ALIGN;
3044 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
3046 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
3047 host->adma_table_sz,
3050 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
3051 if (!host->adma_table || !host->align_buffer) {
3052 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3053 host->adma_table, host->adma_addr);
3054 kfree(host->align_buffer);
3055 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
3057 host->flags &= ~SDHCI_USE_ADMA;
3058 host->adma_table = NULL;
3059 host->align_buffer = NULL;
3060 } else if (host->adma_addr & host->align_mask) {
3061 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3063 host->flags &= ~SDHCI_USE_ADMA;
3064 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3065 host->adma_table, host->adma_addr);
3066 kfree(host->align_buffer);
3067 host->adma_table = NULL;
3068 host->align_buffer = NULL;
3073 * If we use DMA, then it's up to the caller to set the DMA
3074 * mask, but PIO does not need the hw shim so we set a new
3075 * mask here in that case.
3077 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
3078 host->dma_mask = DMA_BIT_MASK(64);
3079 mmc_dev(mmc)->dma_mask = &host->dma_mask;
3082 if (host->version >= SDHCI_SPEC_300)
3083 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
3084 >> SDHCI_CLOCK_BASE_SHIFT;
3086 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
3087 >> SDHCI_CLOCK_BASE_SHIFT;
3089 host->max_clk *= 1000000;
3090 if (host->max_clk == 0 || host->quirks &
3091 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
3092 if (!host->ops->get_max_clock) {
3093 pr_err("%s: Hardware doesn't specify base clock "
3094 "frequency.\n", mmc_hostname(mmc));
3097 host->max_clk = host->ops->get_max_clock(host);
3100 host->next_data.cookie = 1;
3102 * In case of Host Controller v3.00, find out whether clock
3103 * multiplier is supported.
3105 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3106 SDHCI_CLOCK_MUL_SHIFT;
3109 * In case the value in Clock Multiplier is 0, then programmable
3110 * clock mode is not supported, otherwise the actual clock
3111 * multiplier is one more than the value of Clock Multiplier
3112 * in the Capabilities Register.
3118 * Set host parameters.
3120 mmc->ops = &sdhci_ops;
3121 mmc->f_max = host->max_clk;
3122 if (host->ops->get_min_clock)
3123 mmc->f_min = host->ops->get_min_clock(host);
3124 else if (host->version >= SDHCI_SPEC_300) {
3125 if (host->clk_mul) {
3126 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3127 mmc->f_max = host->max_clk * host->clk_mul;
3129 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3131 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
3133 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3134 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3135 SDHCI_TIMEOUT_CLK_SHIFT;
3136 if (host->timeout_clk == 0) {
3137 if (host->ops->get_timeout_clock) {
3139 host->ops->get_timeout_clock(host);
3141 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3147 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3148 host->timeout_clk *= 1000;
3150 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
3151 host->ops->get_max_timeout_count(host) : 1 << 27;
3152 mmc->max_busy_timeout /= host->timeout_clk;
3155 if (override_timeout_clk)
3156 host->timeout_clk = override_timeout_clk;
3158 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3159 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
3161 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3162 host->flags |= SDHCI_AUTO_CMD12;
3164 /* Auto-CMD23 stuff only works in ADMA or PIO. */
3165 if ((host->version >= SDHCI_SPEC_300) &&
3166 ((host->flags & SDHCI_USE_ADMA) ||
3167 !(host->flags & SDHCI_USE_SDMA)) &&
3168 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
3169 host->flags |= SDHCI_AUTO_CMD23;
3170 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3172 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3176 * A controller may support 8-bit width, but the board itself
3177 * might not have the pins brought out. Boards that support
3178 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3179 * their platform code before calling sdhci_add_host(), and we
3180 * won't assume 8-bit width for hosts without that CAP.
3182 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
3183 mmc->caps |= MMC_CAP_4_BIT_DATA;
3185 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3186 mmc->caps &= ~MMC_CAP_CMD23;
3188 if (caps[0] & SDHCI_CAN_DO_HISPD)
3189 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
3191 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3192 !(mmc->caps & MMC_CAP_NONREMOVABLE))
3193 mmc->caps |= MMC_CAP_NEEDS_POLL;
3195 /* If there are external regulators, get them */
3196 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3197 return -EPROBE_DEFER;
3199 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
3200 if (!IS_ERR(mmc->supply.vqmmc)) {
3201 ret = regulator_enable(mmc->supply.vqmmc);
3202 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3204 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3205 SDHCI_SUPPORT_SDR50 |
3206 SDHCI_SUPPORT_DDR50);
3208 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3209 mmc_hostname(mmc), ret);
3210 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
3214 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3215 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3216 SDHCI_SUPPORT_DDR50);
3218 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3219 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3220 SDHCI_SUPPORT_DDR50))
3221 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3223 /* SDR104 supports also implies SDR50 support */
3224 if (caps[1] & SDHCI_SUPPORT_SDR104) {
3225 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3226 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3227 * field can be promoted to support HS200.
3229 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
3230 mmc->caps2 |= MMC_CAP2_HS200;
3231 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
3232 mmc->caps |= MMC_CAP_UHS_SDR50;
3234 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3235 (caps[1] & SDHCI_SUPPORT_HS400))
3236 mmc->caps2 |= MMC_CAP2_HS400;
3238 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3239 (IS_ERR(mmc->supply.vqmmc) ||
3240 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3242 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3244 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3245 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
3246 mmc->caps |= MMC_CAP_UHS_DDR50;
3248 /* Does the host need tuning for SDR50? */
3249 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3250 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3252 /* Does the host need tuning for SDR104 / HS200? */
3253 if (mmc->caps2 & MMC_CAP2_HS200)
3254 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
3256 /* Driver Type(s) (A, C, D) supported by the host */
3257 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3258 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3259 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3260 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3261 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3262 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3264 /* Initial value for re-tuning timer count */
3265 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3266 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3269 * In case Re-tuning Timer is not disabled, the actual value of
3270 * re-tuning timer will be 2 ^ (n - 1).
3272 if (host->tuning_count)
3273 host->tuning_count = 1 << (host->tuning_count - 1);
3275 /* Re-tuning mode supported by the Host Controller */
3276 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3277 SDHCI_RETUNING_MODE_SHIFT;
3282 * According to SD Host Controller spec v3.00, if the Host System
3283 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3284 * the value is meaningful only if Voltage Support in the Capabilities
3285 * register is set. The actual current value is 4 times the register
3288 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3289 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
3290 int curr = regulator_get_current_limit(mmc->supply.vmmc);
3293 /* convert to SDHCI_MAX_CURRENT format */
3294 curr = curr/1000; /* convert to mA */
3295 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3297 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3299 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3300 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3301 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3305 if (caps[0] & SDHCI_CAN_VDD_330) {
3306 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
3308 mmc->max_current_330 = ((max_current_caps &
3309 SDHCI_MAX_CURRENT_330_MASK) >>
3310 SDHCI_MAX_CURRENT_330_SHIFT) *
3311 SDHCI_MAX_CURRENT_MULTIPLIER;
3313 if (caps[0] & SDHCI_CAN_VDD_300) {
3314 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
3316 mmc->max_current_300 = ((max_current_caps &
3317 SDHCI_MAX_CURRENT_300_MASK) >>
3318 SDHCI_MAX_CURRENT_300_SHIFT) *
3319 SDHCI_MAX_CURRENT_MULTIPLIER;
3321 if (caps[0] & SDHCI_CAN_VDD_180) {
3322 ocr_avail |= MMC_VDD_165_195;
3324 mmc->max_current_180 = ((max_current_caps &
3325 SDHCI_MAX_CURRENT_180_MASK) >>
3326 SDHCI_MAX_CURRENT_180_SHIFT) *
3327 SDHCI_MAX_CURRENT_MULTIPLIER;
3330 /* If OCR set by external regulators, use it instead */
3332 ocr_avail = mmc->ocr_avail;
3335 ocr_avail &= host->ocr_mask;
3337 mmc->ocr_avail = ocr_avail;
3338 mmc->ocr_avail_sdio = ocr_avail;
3339 if (host->ocr_avail_sdio)
3340 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3341 mmc->ocr_avail_sd = ocr_avail;
3342 if (host->ocr_avail_sd)
3343 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3344 else /* normal SD controllers don't support 1.8V */
3345 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3346 mmc->ocr_avail_mmc = ocr_avail;
3347 if (host->ocr_avail_mmc)
3348 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
3350 if (mmc->ocr_avail == 0) {
3351 pr_err("%s: Hardware doesn't report any "
3352 "support voltages.\n", mmc_hostname(mmc));
3356 spin_lock_init(&host->lock);
3359 * Maximum number of segments. Depends on if the hardware
3360 * can do scatter/gather or not.
3362 if (host->flags & SDHCI_USE_ADMA)
3363 mmc->max_segs = SDHCI_MAX_SEGS;
3364 else if (host->flags & SDHCI_USE_SDMA)
3367 mmc->max_segs = SDHCI_MAX_SEGS;
3370 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3371 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3374 mmc->max_req_size = 524288;
3377 * Maximum segment size. Could be one segment with the maximum number
3378 * of bytes. When doing hardware scatter/gather, each entry cannot
3379 * be larger than 64 KiB though.
3381 if (host->flags & SDHCI_USE_ADMA) {
3382 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3383 mmc->max_seg_size = 65535;
3385 mmc->max_seg_size = 65536;
3387 mmc->max_seg_size = mmc->max_req_size;
3391 * Maximum block size. This varies from controller to controller and
3392 * is specified in the capabilities register.
3394 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3395 mmc->max_blk_size = 2;
3397 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
3398 SDHCI_MAX_BLOCK_SHIFT;
3399 if (mmc->max_blk_size >= 3) {
3400 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3402 mmc->max_blk_size = 0;
3406 mmc->max_blk_size = 512 << mmc->max_blk_size;
3409 * Maximum block count.
3411 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
3416 tasklet_init(&host->finish_tasklet,
3417 sdhci_tasklet_finish, (unsigned long)host);
3419 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
3421 init_waitqueue_head(&host->buf_ready_int);
3423 if (host->version >= SDHCI_SPEC_300) {
3424 /* Initialize re-tuning timer */
3425 init_timer(&host->tuning_timer);
3426 host->tuning_timer.data = (unsigned long)host;
3427 host->tuning_timer.function = sdhci_tuning_timer;
3430 sdhci_init(host, 0);
3432 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3433 IRQF_SHARED, mmc_hostname(mmc), host);
3435 pr_err("%s: Failed to request IRQ %d: %d\n",
3436 mmc_hostname(mmc), host->irq, ret);
3440 #ifdef CONFIG_MMC_DEBUG
3441 sdhci_dumpregs(host);
3444 #ifdef SDHCI_USE_LEDS_CLASS
3445 snprintf(host->led_name, sizeof(host->led_name),
3446 "%s::", mmc_hostname(mmc));
3447 host->led.name = host->led_name;
3448 host->led.brightness = LED_OFF;
3449 host->led.default_trigger = mmc_hostname(mmc);
3450 host->led.brightness_set = sdhci_led_control;
3452 ret = led_classdev_register(mmc_dev(mmc), &host->led);
3454 pr_err("%s: Failed to register LED device: %d\n",
3455 mmc_hostname(mmc), ret);
3464 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
3465 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
3466 (host->flags & SDHCI_USE_ADMA) ?
3467 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
3468 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
3470 sdhci_enable_card_detection(host);
3474 #ifdef SDHCI_USE_LEDS_CLASS
3476 sdhci_do_reset(host, SDHCI_RESET_ALL);
3477 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3478 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3479 free_irq(host->irq, host);
3482 tasklet_kill(&host->finish_tasklet);
3487 EXPORT_SYMBOL_GPL(sdhci_add_host);
3489 void sdhci_remove_host(struct sdhci_host *host, int dead)
3491 struct mmc_host *mmc = host->mmc;
3492 unsigned long flags;
3495 spin_lock_irqsave(&host->lock, flags);
3497 host->flags |= SDHCI_DEVICE_DEAD;
3500 pr_err("%s: Controller removed during "
3501 " transfer!\n", mmc_hostname(mmc));
3503 host->mrq->cmd->error = -ENOMEDIUM;
3504 tasklet_schedule(&host->finish_tasklet);
3507 spin_unlock_irqrestore(&host->lock, flags);
3510 sdhci_disable_card_detection(host);
3512 mmc_remove_host(mmc);
3514 #ifdef SDHCI_USE_LEDS_CLASS
3515 led_classdev_unregister(&host->led);
3519 sdhci_do_reset(host, SDHCI_RESET_ALL);
3521 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3522 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
3523 free_irq(host->irq, host);
3525 del_timer_sync(&host->timer);
3527 tasklet_kill(&host->finish_tasklet);
3529 if (!IS_ERR(mmc->supply.vqmmc))
3530 regulator_disable(mmc->supply.vqmmc);
3532 if (host->adma_table)
3533 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
3534 host->adma_table, host->adma_addr);
3535 kfree(host->align_buffer);
3537 host->adma_table = NULL;
3538 host->align_buffer = NULL;
3541 EXPORT_SYMBOL_GPL(sdhci_remove_host);
3543 void sdhci_free_host(struct sdhci_host *host)
3545 mmc_free_host(host->mmc);
3548 EXPORT_SYMBOL_GPL(sdhci_free_host);
3550 /*****************************************************************************\
3552 * Driver init/exit *
3554 \*****************************************************************************/
3556 static int __init sdhci_drv_init(void)
3559 ": Secure Digital Host Controller Interface driver\n");
3560 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
3565 static void __exit sdhci_drv_exit(void)
3569 module_init(sdhci_drv_init);
3570 module_exit(sdhci_drv_exit);
3572 module_param(debug_quirks, uint, 0444);
3573 module_param(debug_quirks2, uint, 0444);
3575 MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
3576 MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
3577 MODULE_LICENSE("GPL");
3579 MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
3580 MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");