19 #include <linux/module.h>
21 #include <linux/slab.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 *);
53 static void sdhci_finish_command(
struct sdhci_host *);
55 static void sdhci_tuning_timer(
unsigned long data);
57 #ifdef CONFIG_PM_RUNTIME
137 static void sdhci_unmask_irqs(
struct sdhci_host *host,
u32 irqs)
139 sdhci_clear_set_irqs(host, 0, irqs);
142 static void sdhci_mask_irqs(
struct sdhci_host *host,
u32 irqs)
144 sdhci_clear_set_irqs(host, irqs, 0);
160 sdhci_unmask_irqs(host, irqs);
162 sdhci_mask_irqs(host, irqs);
165 static void sdhci_enable_card_detection(
struct sdhci_host *host)
167 sdhci_set_card_detection(host,
true);
170 static void sdhci_disable_card_detection(
struct sdhci_host *host)
172 sdhci_set_card_detection(host,
false);
189 if (host->
ops->platform_reset_enter)
190 host->
ops->platform_reset_enter(host, mask);
203 pr_err(
"%s: Reset 0x%x never completed.\n",
205 sdhci_dumpregs(host);
212 if (host->
ops->platform_reset_exit)
213 host->
ops->platform_reset_exit(host, mask);
219 if ((host->
ops->enable_dma) && (mask & SDHCI_RESET_ALL))
220 host->
ops->enable_dma(host);
231 sdhci_reset(host, SDHCI_RESET_ALL);
242 sdhci_set_ios(host->
mmc, &host->
mmc->ios);
246 static void sdhci_reinit(
struct sdhci_host *host)
259 host->
mmc->max_blk_count =
262 sdhci_enable_card_detection(host);
265 static void sdhci_activate_led(
struct sdhci_host *host)
274 static void sdhci_deactivate_led(
struct sdhci_host *host)
283 #ifdef SDHCI_USE_LEDS_CLASS
296 sdhci_deactivate_led(host);
298 sdhci_activate_led(host);
300 spin_unlock_irqrestore(&host->
lock, flags);
310 static void sdhci_read_block_pio(
struct sdhci_host *host)
317 DBG(
"PIO reading\n");
319 blksize = host->
data->blksz;
355 static void sdhci_write_block_pio(
struct sdhci_host *host)
358 size_t blksize, len,
chunk;
362 DBG(
"PIO writing\n");
364 blksize = host->
data->blksz;
382 scratch |= (
u32)*buf << (chunk * 8);
388 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
401 static void sdhci_transfer_pio(
struct sdhci_host *host)
421 (host->
data->blocks == 1))
429 sdhci_read_block_pio(host);
431 sdhci_write_block_pio(host);
438 DBG(
"PIO transfer complete.\n");
441 static char *sdhci_kmap_atomic(
struct scatterlist *
sg,
unsigned long *flags)
447 static void sdhci_kunmap_atomic(
void *
buffer,
unsigned long *flags)
467 static int sdhci_adma_table_pre(
struct sdhci_host *host,
525 offset = (4 - (addr & 0x3)) & 0x3;
528 buffer = sdhci_kmap_atomic(sg, &flags);
530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
535 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
551 sdhci_set_adma_desc(desc, addr, len, 0x21);
575 sdhci_set_adma_desc(desc, 0, 0, 0x3);
604 static void sdhci_adma_table_post(
struct sdhci_host *host,
636 buffer = sdhci_kmap_atomic(sg, &flags);
638 memcpy(buffer, align, size);
639 sdhci_kunmap_atomic(buffer, &flags);
654 unsigned target_timeout, current_timeout;
689 current_timeout = (1 << 13) * 1000 / host->
timeout_clk;
690 while (current_timeout < target_timeout) {
692 current_timeout <<= 1;
698 DBG(
"%s: Too large timeout 0x%x requested for CMD%d!\n",
706 static void sdhci_set_transfer_irqs(
struct sdhci_host *host)
712 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
714 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
727 count = sdhci_calc_timeout(host, cmd);
741 host->
data->bytes_xfered = 0;
766 DBG(
"Reverting to PIO because of "
767 "transfer size (%d)\n",
801 DBG(
"Reverting to PIO because of "
812 ret = sdhci_adma_table_pre(host, data);
875 sdhci_set_transfer_irqs(host);
883 static void sdhci_set_transfer_mode(
struct sdhci_host *host,
917 static void sdhci_finish_data(
struct sdhci_host *host)
928 sdhci_adma_table_post(host, data);
966 sdhci_send_command(host, data->
stop);
975 unsigned long timeout;
988 if (host->
mrq->data && (cmd == host->
mrq->data->stop))
993 pr_err(
"%s: Controller never released "
995 sdhci_dumpregs(host);
1008 sdhci_prepare_data(host, cmd);
1012 sdhci_set_transfer_mode(host, cmd);
1015 pr_err(
"%s: Unsupported response type!\n",
1044 static void sdhci_finish_command(
struct sdhci_host *host)
1053 for (i = 0;i < 4;i++) {
1054 host->
cmd->resp[
i] = sdhci_readl(host,
1057 host->
cmd->resp[
i] |=
1066 host->
cmd->error = 0;
1069 if (host->
cmd == host->
mrq->sbc) {
1071 sdhci_send_command(host, host->
mrq->cmd);
1076 sdhci_finish_data(host);
1078 if (!host->
cmd->data)
1085 static void sdhci_set_clock(
struct sdhci_host *host,
unsigned int clock)
1088 int real_div =
div, clk_mul = 1;
1090 unsigned long timeout;
1092 if (clock && clock == host->
clock)
1095 host->
mmc->actual_clock = 0;
1097 if (host->
ops->set_clock) {
1098 host->
ops->set_clock(host, clock);
1124 for (div = 1; div <= 1024; div++) {
1145 if ((host->
max_clk / div) <= clock)
1155 if ((host->
max_clk / div) <= clock)
1163 host->
mmc->actual_clock = (host->
max_clk * clk_mul) / real_div;
1176 pr_err(
"%s: Internal clock never "
1178 sdhci_dumpregs(host);
1192 static int sdhci_set_power(
struct sdhci_host *host,
unsigned short power)
1196 if (power != (
unsigned short)-1) {
1197 switch (1 << power) {
1214 if (host->
pwr == pwr)
1262 unsigned long flags;
1265 host = mmc_priv(mmc);
1267 sdhci_runtime_pm_get(host);
1273 #ifndef SDHCI_USE_LEDS_CLASS
1274 sdhci_activate_led(host);
1324 spin_unlock_irqrestore(&host->
lock, flags);
1325 sdhci_execute_tuning(mmc, tuning_opcode);
1334 sdhci_send_command(host, mrq->
sbc);
1336 sdhci_send_command(host, mrq->
cmd);
1340 spin_unlock_irqrestore(&host->
lock, flags);
1345 unsigned long flags;
1352 spin_unlock_irqrestore(&host->
lock, flags);
1354 mmc_regulator_set_ocr(host->
mmc, host->
vmmc, 0);
1367 sdhci_set_clock(host, ios->
clock);
1370 vdd_bit = sdhci_set_power(host, -1);
1372 vdd_bit = sdhci_set_power(host, ios->
vdd);
1374 if (host->
vmmc && vdd_bit != -1) {
1375 spin_unlock_irqrestore(&host->
lock, flags);
1376 mmc_regulator_set_ocr(host->
mmc, host->
vmmc, vdd_bit);
1380 if (host->
ops->platform_send_init_74_clocks)
1381 host->
ops->platform_send_init_74_clocks(host, ios->
power_mode);
1388 if (host->
ops->platform_8bit_width)
1429 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1458 clock = host->
clock;
1460 sdhci_set_clock(host, clock);
1469 if (host->
ops->set_uhs_signaling)
1470 host->
ops->set_uhs_signaling(host, ios->
timing);
1491 clock = host->
clock;
1493 sdhci_set_clock(host, clock);
1506 spin_unlock_irqrestore(&host->
lock, flags);
1513 sdhci_runtime_pm_get(host);
1514 sdhci_do_set_ios(host, ios);
1515 sdhci_runtime_pm_put(host);
1518 static int sdhci_check_ro(
struct sdhci_host *host)
1520 unsigned long flags;
1527 else if (host->
ops->get_ro)
1528 is_readonly = host->
ops->get_ro(host);
1533 spin_unlock_irqrestore(&host->
lock, flags);
1537 !is_readonly : is_readonly;
1540 #define SAMPLE_COUNT 5
1542 static int sdhci_do_get_ro(
struct sdhci_host *host)
1547 return sdhci_check_ro(host);
1551 if (sdhci_check_ro(host)) {
1552 if (++ro_count > SAMPLE_COUNT / 2)
1560 static void sdhci_hw_reset(
struct mmc_host *mmc)
1564 if (host->
ops && host->
ops->hw_reset)
1565 host->
ops->hw_reset(host);
1568 static int sdhci_get_ro(
struct mmc_host *mmc)
1573 sdhci_runtime_pm_get(host);
1574 ret = sdhci_do_get_ro(host);
1575 sdhci_runtime_pm_put(host);
1579 static void sdhci_enable_sdio_irq_nolock(
struct sdhci_host *host,
int enable)
1601 static void sdhci_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
1604 unsigned long flags;
1607 sdhci_enable_sdio_irq_nolock(host, enable);
1608 spin_unlock_irqrestore(&host->
lock, flags);
1611 static int sdhci_do_3_3v_signal_voltage_switch(
struct sdhci_host *host,
1623 pr_warning(
"%s: Switching to 3.3V signalling voltage "
1636 pr_warning(
"%s: 3.3V regulator output did not became stable\n",
1642 static int sdhci_do_1_8v_signal_voltage_switch(
struct sdhci_host *host,
1677 if (ctrl & SDHCI_CTRL_VDD_180) {
1688 present_state = sdhci_readl(host,
1690 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1691 SDHCI_DATA_LVL_MASK)
1715 pr_warning(
"%s: Switching to 1.8V signalling voltage failed, "
1721 static int sdhci_do_start_signal_voltage_switch(
struct sdhci_host *host,
1739 return sdhci_do_3_3v_signal_voltage_switch(host, ctrl);
1740 else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1742 return sdhci_do_1_8v_signal_voltage_switch(host, ctrl);
1748 static int sdhci_start_signal_voltage_switch(
struct mmc_host *mmc,
1756 sdhci_runtime_pm_get(host);
1757 err = sdhci_do_start_signal_voltage_switch(host, ios);
1758 sdhci_runtime_pm_put(host);
1768 unsigned long timeout;
1770 bool requires_tuning_nonuhs =
false;
1772 host = mmc_priv(mmc);
1774 sdhci_runtime_pm_get(host);
1776 spin_lock(&host->
lock);
1790 requires_tuning_nonuhs =
true;
1793 requires_tuning_nonuhs)
1796 spin_unlock(&host->
lock);
1798 sdhci_runtime_pm_put(host);
1826 if (!tuning_loop_counter && !timeout)
1864 sdhci_send_command(host, &cmd);
1869 spin_unlock(&host->
lock);
1877 spin_lock(&host->
lock);
1881 "Buffer Read Ready interrupt during tuning "
1882 "procedure, falling back to fixed sampling "
1896 tuning_loop_counter--;
1905 if (!tuning_loop_counter || !timeout) {
1911 " failed, falling back to fixed sampling"
1951 spin_unlock(&host->
lock);
1953 sdhci_runtime_pm_put(host);
1958 static void sdhci_do_enable_preset_value(
struct sdhci_host *host,
bool enable)
1961 unsigned long flags;
1975 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1979 }
else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1980 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1985 spin_unlock_irqrestore(&host->
lock, flags);
1988 static void sdhci_enable_preset_value(
struct mmc_host *mmc,
bool enable)
1992 sdhci_runtime_pm_get(host);
1993 sdhci_do_enable_preset_value(host, enable);
1994 sdhci_runtime_pm_put(host);
1998 .request = sdhci_request,
1999 .set_ios = sdhci_set_ios,
2000 .get_ro = sdhci_get_ro,
2001 .hw_reset = sdhci_hw_reset,
2002 .enable_sdio_irq = sdhci_enable_sdio_irq,
2003 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2004 .execute_tuning = sdhci_execute_tuning,
2005 .enable_preset_value = sdhci_enable_preset_value,
2014 static void sdhci_tasklet_card(
unsigned long param)
2017 unsigned long flags;
2026 pr_err(
"%s: Card removed during transfer!\n",
2028 pr_err(
"%s: Resetting controller.\n",
2038 spin_unlock_irqrestore(&host->
lock, flags);
2043 static void sdhci_tasklet_finish(
unsigned long param)
2046 unsigned long flags;
2058 spin_unlock_irqrestore(&host->
lock, flags);
2071 ((mrq->
cmd && mrq->
cmd->error) ||
2073 (mrq->
data->stop && mrq->
data->stop->error))) ||
2081 clock = host->
clock;
2083 sdhci_set_clock(host, clock);
2096 #ifndef SDHCI_USE_LEDS_CLASS
2097 sdhci_deactivate_led(host);
2101 spin_unlock_irqrestore(&host->
lock, flags);
2104 sdhci_runtime_pm_put(host);
2107 static void sdhci_timeout_timer(
unsigned long data)
2110 unsigned long flags;
2117 pr_err(
"%s: Timeout waiting for hardware "
2119 sdhci_dumpregs(host);
2123 sdhci_finish_data(host);
2135 spin_unlock_irqrestore(&host->
lock, flags);
2138 static void sdhci_tuning_timer(
unsigned long data)
2141 unsigned long flags;
2149 spin_unlock_irqrestore(&host->
lock, flags);
2158 static void sdhci_cmd_irq(
struct sdhci_host *host,
u32 intmask)
2163 pr_err(
"%s: Got command interrupt 0x%08x even "
2164 "though no command operation was in progress.\n",
2166 sdhci_dumpregs(host);
2176 if (host->
cmd->error) {
2193 if (host->
cmd->data)
2194 DBG(
"Cannot wait for busy signal when also "
2195 "doing a data transfer");
2204 sdhci_finish_command(host);
2207 #ifdef CONFIG_MMC_DEBUG
2208 static void sdhci_show_adma_error(
struct sdhci_host *host)
2216 sdhci_dumpregs(host);
2219 dma = (
__le32 *)(desc + 4);
2220 len = (
__le16 *)(desc + 2);
2223 DBG(
"%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2233 static void sdhci_show_adma_error(
struct sdhci_host *host) { }
2236 static void sdhci_data_irq(
struct sdhci_host *host,
u32 intmask)
2260 sdhci_finish_command(host);
2265 pr_err(
"%s: Got data interrupt 0x%08x even "
2266 "though no data operation was in progress.\n",
2268 sdhci_dumpregs(host);
2283 sdhci_show_adma_error(host);
2287 if (host->
data->error)
2288 sdhci_finish_data(host);
2291 sdhci_transfer_pio(host);
2303 u32 dmastart, dmanow;
2305 dmanow = dmastart + host->
data->bytes_xfered;
2312 host->
data->bytes_xfered = dmanow - dmastart;
2313 DBG(
"%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2316 host->
data->bytes_xfered, dmanow);
2329 sdhci_finish_data(host);
2339 u32 intmask, unexpected = 0;
2340 int cardint = 0, max_loops = 16;
2342 spin_lock(&host->
lock);
2345 spin_unlock(&host->
lock);
2346 pr_warning(
"%s: got irq while runtime suspended\n",
2353 if (!intmask || intmask == 0xffffffff) {
2359 DBG(
"*** %s got interrupt: 0x%08x\n",
2388 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2390 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
2394 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2396 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2404 pr_err(
"%s: Card is consuming too much power!\n",
2409 intmask &= ~SDHCI_INT_BUS_POWER;
2414 intmask &= ~SDHCI_INT_CARD_INT;
2417 unexpected |= intmask;
2424 if (intmask && --max_loops)
2427 spin_unlock(&host->
lock);
2430 pr_err(
"%s: Unexpected interrupt 0x%08x.\n",
2432 sdhci_dumpregs(host);
2438 mmc_signal_sdio_irq(host->
mmc);
2451 int sdhci_suspend_host(
struct sdhci_host *host)
2455 if (host->
ops->platform_suspend)
2456 host->
ops->platform_suspend(host);
2458 sdhci_disable_card_detection(host);
2474 sdhci_enable_card_detection(host);
2486 int sdhci_resume_host(
struct sdhci_host *host)
2491 if (host->
ops->enable_dma)
2492 host->
ops->enable_dma(host);
2503 sdhci_init(host, 0);
2506 sdhci_do_set_ios(host, &host->
mmc->ios);
2513 sdhci_enable_card_detection(host);
2515 if (host->
ops->platform_resume)
2516 host->
ops->platform_resume(host);
2527 void sdhci_enable_irq_wakeups(
struct sdhci_host *host)
2539 #ifdef CONFIG_PM_RUNTIME
2541 static int sdhci_runtime_pm_get(
struct sdhci_host *host)
2543 return pm_runtime_get_sync(host->
mmc->parent);
2546 static int sdhci_runtime_pm_put(
struct sdhci_host *host)
2548 pm_runtime_mark_last_busy(host->
mmc->parent);
2549 return pm_runtime_put_autosuspend(host->
mmc->parent);
2552 int sdhci_runtime_suspend_host(
struct sdhci_host *host)
2554 unsigned long flags;
2565 spin_unlock_irqrestore(&host->
lock, flags);
2571 spin_unlock_irqrestore(&host->
lock, flags);
2577 int sdhci_runtime_resume_host(
struct sdhci_host *host)
2579 unsigned long flags;
2583 if (host->
ops->enable_dma)
2584 host->
ops->enable_dma(host);
2587 sdhci_init(host, 0);
2592 sdhci_do_set_ios(host, &host->
mmc->ios);
2594 sdhci_do_start_signal_voltage_switch(host, &host->
mmc->ios);
2596 sdhci_do_enable_preset_value(host,
true);
2608 sdhci_enable_sdio_irq_nolock(host,
true);
2611 sdhci_enable_card_detection(host);
2613 spin_unlock_irqrestore(&host->
lock, flags);
2639 host = mmc_priv(mmc);
2651 u32 max_current_caps;
2662 host->
quirks = debug_quirks;
2664 host->
quirks2 = debug_quirks2;
2666 sdhci_reset(host, SDHCI_RESET_ALL);
2672 pr_err(
"%s: Unknown controller version (%d). "
2688 DBG(
"Controller doesn't have SDMA capability\n");
2694 DBG(
"Disabling DMA as it is marked broken\n");
2704 DBG(
"Disabling ADMA as it is marked broken\n");
2709 if (host->
ops->enable_dma) {
2710 if (host->
ops->enable_dma(host)) {
2712 "available. Falling back to PIO.\n",
2732 "buffers. Falling back to standard DMA.\n",
2758 if (!host->
ops->get_max_clock) {
2759 pr_err(
"%s: Hardware doesn't specify base clock "
2763 host->
max_clk = host->
ops->get_max_clock(host);
2785 mmc->
ops = &sdhci_ops;
2787 if (host->
ops->get_min_clock)
2788 mmc->
f_min = host->
ops->get_min_clock(host);
2801 if (host->
ops->get_timeout_clock) {
2803 }
else if (!(host->
quirks &
2805 pr_err(
"%s: Hardware doesn't specify timeout clock "
2855 if (IS_ERR_OR_NULL(host->
vqmmc)) {
2856 if (PTR_ERR(host->
vqmmc) < 0) {
2857 pr_info(
"%s: no vqmmc regulator found\n",
2916 if (IS_ERR_OR_NULL(host->
vmmc)) {
2917 if (PTR_ERR(host->
vmmc) < 0) {
2918 pr_info(
"%s: no vmmc regulator found\n",
2925 #ifdef CONFIG_REGULATOR
2930 caps[0] &= ~SDHCI_CAN_VDD_330;
2934 caps[0] &= ~SDHCI_CAN_VDD_300;
2938 caps[0] &= ~SDHCI_CAN_VDD_180;
2950 if (!max_current_caps && host->
vmmc) {
3005 pr_err(
"%s: Hardware doesn't report any "
3053 pr_warning(
"%s: Invalid maximum block size, "
3070 sdhci_tasklet_card, (
unsigned long)host);
3072 sdhci_tasklet_finish, (
unsigned long)host);
3074 setup_timer(&host->timer, sdhci_timeout_timer, (
unsigned long)host);
3081 host->tuning_timer.data = (
unsigned long)host;
3082 host->tuning_timer.function = sdhci_tuning_timer;
3088 pr_err(
"%s: Failed to request IRQ %d: %d\n",
3093 sdhci_init(host, 0);
3095 #ifdef CONFIG_MMC_DEBUG
3096 sdhci_dumpregs(host);
3099 #ifdef SDHCI_USE_LEDS_CLASS
3100 snprintf(host->led_name,
sizeof(host->led_name),
3102 host->led.name = host->led_name;
3103 host->led.brightness =
LED_OFF;
3105 host->led.brightness_set = sdhci_led_control;
3109 pr_err(
"%s: Failed to register LED device: %d\n",
3119 pr_info(
"%s: SDHCI controller on %s [%s] using %s\n",
3124 sdhci_enable_card_detection(host);
3128 #ifdef SDHCI_USE_LEDS_CLASS
3130 sdhci_reset(host, SDHCI_RESET_ALL);
3144 unsigned long flags;
3152 pr_err(
"%s: Controller removed during "
3159 spin_unlock_irqrestore(&host->
lock, flags);
3162 sdhci_disable_card_detection(host);
3166 #ifdef SDHCI_USE_LEDS_CLASS
3171 sdhci_reset(host, SDHCI_RESET_ALL);
3212 static int __init sdhci_drv_init(
void)
3215 ": Secure Digital Host Controller Interface driver\n");
3221 static void __exit sdhci_drv_exit(
void)