Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3 | * |
Pierre Ossman | b69c905 | 2008-03-08 23:44:25 +0100 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Pierre Ossman | 643f720 | 2006-09-30 23:27:52 -0700 | [diff] [blame] | 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. |
Pierre Ossman | 84c46a5 | 2007-12-02 19:58:16 +0100 | [diff] [blame] | 10 | * |
| 11 | * Thanks to the following companies for their support: |
| 12 | * |
| 13 | * - JMicron (hardware and technical support) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/highmem.h> |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Ralf Baechle | 1176360 | 2007-10-23 20:42:11 +0200 | [diff] [blame] | 22 | #include <linux/scatterlist.h> |
Marek Szyprowski | 9bea3c8 | 2010-08-10 18:01:59 -0700 | [diff] [blame] | 23 | #include <linux/regulator/consumer.h> |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 25 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 26 | #include <linux/leds.h> |
| 27 | |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 28 | #include <linux/mmc/mmc.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 29 | #include <linux/mmc/host.h> |
Aaron Lu | 473b095a | 2012-07-03 17:27:49 +0800 | [diff] [blame] | 30 | #include <linux/mmc/card.h> |
Corneliu Doban | 85cc1c3 | 2015-02-09 16:06:29 -0800 | [diff] [blame] | 31 | #include <linux/mmc/sdio.h> |
Guennadi Liakhovetski | bec9d4e | 2012-09-17 16:45:10 +0800 | [diff] [blame] | 32 | #include <linux/mmc/slot-gpio.h> |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 33 | #include <linux/mmc/sdio.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 34 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 35 | #include <trace/events/mmc.h> |
| 36 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 37 | #include "sdhci.h" |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 38 | #include "cmdq_hci.h" |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_NAME "sdhci" |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 41 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 42 | #define DBG(f, x...) \ |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 43 | pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 44 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 45 | #define MAX_TUNING_LOOP 40 |
| 46 | |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 47 | #define SDHCI_DBG_DUMP_RS_INTERVAL (10 * HZ) |
| 48 | #define SDHCI_DBG_DUMP_RS_BURST 2 |
| 49 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 50 | static unsigned int debug_quirks = 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 51 | static unsigned int debug_quirks2; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 52 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 53 | static void sdhci_finish_data(struct sdhci_host *); |
| 54 | |
Sahitya Tummala | ea4e3aa | 2013-05-24 14:08:10 +0530 | [diff] [blame] | 55 | static bool sdhci_check_state(struct sdhci_host *); |
| 56 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 57 | static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable); |
| 58 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 59 | static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 60 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 61 | static void sdhci_dump_state(struct sdhci_host *host) |
| 62 | { |
| 63 | struct mmc_host *mmc = host->mmc; |
| 64 | |
| 65 | #ifdef CONFIG_MMC_CLKGATE |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 66 | pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d host->irq = %d\n", |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 67 | mmc_hostname(mmc), host->clock, mmc->clk_gated, |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 68 | mmc->claimer->comm, host->pwr, |
| 69 | (host->flags & SDHCI_HOST_IRQ_STATUS)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 70 | #else |
| 71 | pr_info("%s: clk: %d claimer: %s pwr: %d\n", |
| 72 | mmc_hostname(mmc), host->clock, |
| 73 | mmc->claimer->comm, host->pwr); |
| 74 | #endif |
| 75 | pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n", |
| 76 | mmc_hostname(mmc), mmc->parent->power.runtime_status, |
| 77 | atomic_read(&mmc->parent->power.usage_count), |
| 78 | mmc->parent->power.disable_depth); |
| 79 | } |
| 80 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 81 | static void sdhci_dumpregs(struct sdhci_host *host) |
| 82 | { |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 83 | MMC_TRACE(host->mmc, |
| 84 | "%s: 0x04=0x%08x 0x06=0x%08x 0x0E=0x%08x 0x30=0x%08x 0x34=0x%08x 0x38=0x%08x\n", |
| 85 | __func__, |
| 86 | sdhci_readw(host, SDHCI_BLOCK_SIZE), |
| 87 | sdhci_readw(host, SDHCI_BLOCK_COUNT), |
| 88 | sdhci_readw(host, SDHCI_COMMAND), |
| 89 | sdhci_readl(host, SDHCI_INT_STATUS), |
| 90 | sdhci_readl(host, SDHCI_INT_ENABLE), |
| 91 | sdhci_readl(host, SDHCI_SIGNAL_ENABLE)); |
| 92 | mmc_stop_tracing(host->mmc); |
| 93 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 94 | pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n", |
| 95 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 96 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 97 | pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 98 | sdhci_readl(host, SDHCI_DMA_ADDRESS), |
| 99 | sdhci_readw(host, SDHCI_HOST_VERSION)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 100 | pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 101 | sdhci_readw(host, SDHCI_BLOCK_SIZE), |
| 102 | sdhci_readw(host, SDHCI_BLOCK_COUNT)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 103 | pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 104 | sdhci_readl(host, SDHCI_ARGUMENT), |
| 105 | sdhci_readw(host, SDHCI_TRANSFER_MODE)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 106 | pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 107 | sdhci_readl(host, SDHCI_PRESENT_STATE), |
| 108 | sdhci_readb(host, SDHCI_HOST_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 109 | pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 110 | sdhci_readb(host, SDHCI_POWER_CONTROL), |
| 111 | sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 112 | pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 113 | sdhci_readb(host, SDHCI_WAKE_UP_CONTROL), |
| 114 | sdhci_readw(host, SDHCI_CLOCK_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 115 | pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 116 | sdhci_readb(host, SDHCI_TIMEOUT_CONTROL), |
| 117 | sdhci_readl(host, SDHCI_INT_STATUS)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 118 | pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 119 | sdhci_readl(host, SDHCI_INT_ENABLE), |
| 120 | sdhci_readl(host, SDHCI_SIGNAL_ENABLE)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 121 | pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n", |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 122 | host->auto_cmd_err_sts, |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 123 | sdhci_readw(host, SDHCI_SLOT_INT_STATUS)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 124 | pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 125 | sdhci_readl(host, SDHCI_CAPABILITIES), |
| 126 | sdhci_readl(host, SDHCI_CAPABILITIES_1)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 127 | pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 128 | sdhci_readw(host, SDHCI_COMMAND), |
| 129 | sdhci_readl(host, SDHCI_MAX_CURRENT)); |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 130 | pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n", |
| 131 | sdhci_readl(host, SDHCI_RESPONSE + 0x4), |
| 132 | sdhci_readl(host, SDHCI_RESPONSE)); |
| 133 | pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n", |
| 134 | sdhci_readl(host, SDHCI_RESPONSE + 0xC), |
| 135 | sdhci_readl(host, SDHCI_RESPONSE + 0x8)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 136 | pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 137 | sdhci_readw(host, SDHCI_HOST_CONTROL2)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 138 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 139 | if (host->flags & SDHCI_USE_ADMA) { |
| 140 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 141 | pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 142 | readl(host->ioaddr + SDHCI_ADMA_ERROR), |
| 143 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI), |
| 144 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS)); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 145 | else |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 146 | pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 147 | readl(host->ioaddr + SDHCI_ADMA_ERROR), |
| 148 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS)); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 149 | } |
Ben Dooks | be3f4ae | 2009-06-08 23:33:52 +0100 | [diff] [blame] | 150 | |
Sahitya Tummala | 91d315e | 2013-08-02 09:17:54 +0530 | [diff] [blame] | 151 | if (host->ops->dump_vendor_regs) |
| 152 | host->ops->dump_vendor_regs(host); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 153 | sdhci_dump_state(host); |
| 154 | pr_info(DRIVER_NAME ": ===========================================\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /*****************************************************************************\ |
| 158 | * * |
| 159 | * Low level functions * |
| 160 | * * |
| 161 | \*****************************************************************************/ |
| 162 | |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 163 | static inline bool sdhci_data_line_cmd(struct mmc_command *cmd) |
| 164 | { |
| 165 | return cmd->data || cmd->flags & MMC_RSP_BUSY; |
| 166 | } |
| 167 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 168 | static void sdhci_set_card_detection(struct sdhci_host *host, bool enable) |
| 169 | { |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 170 | u32 present; |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 171 | |
Adrian Hunter | c79396c | 2011-12-27 15:48:42 +0200 | [diff] [blame] | 172 | if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) || |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 173 | !mmc_card_is_removable(host->mmc)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 174 | return; |
| 175 | |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 176 | if (enable) { |
| 177 | present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 178 | SDHCI_CARD_PRESENT; |
Shawn Guo | d25928d | 2011-06-21 22:41:48 +0800 | [diff] [blame] | 179 | |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 180 | host->ier |= present ? SDHCI_INT_CARD_REMOVE : |
| 181 | SDHCI_INT_CARD_INSERT; |
| 182 | } else { |
| 183 | host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); |
| 184 | } |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 185 | |
| 186 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 187 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void sdhci_enable_card_detection(struct sdhci_host *host) |
| 191 | { |
| 192 | sdhci_set_card_detection(host, true); |
| 193 | } |
| 194 | |
| 195 | static void sdhci_disable_card_detection(struct sdhci_host *host) |
| 196 | { |
| 197 | sdhci_set_card_detection(host, false); |
| 198 | } |
| 199 | |
Ulf Hansson | 02d0b68 | 2016-04-11 15:32:41 +0200 | [diff] [blame] | 200 | static void sdhci_runtime_pm_bus_on(struct sdhci_host *host) |
| 201 | { |
| 202 | if (host->bus_on) |
| 203 | return; |
| 204 | host->bus_on = true; |
| 205 | pm_runtime_get_noresume(host->mmc->parent); |
| 206 | } |
| 207 | |
| 208 | static void sdhci_runtime_pm_bus_off(struct sdhci_host *host) |
| 209 | { |
| 210 | if (!host->bus_on) |
| 211 | return; |
| 212 | host->bus_on = false; |
| 213 | pm_runtime_put_noidle(host->mmc->parent); |
| 214 | } |
| 215 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 216 | void sdhci_reset(struct sdhci_host *host, u8 mask) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 217 | { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 218 | unsigned long timeout; |
Philip Rakity | 393c1a3 | 2011-01-21 11:26:40 -0800 | [diff] [blame] | 219 | |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 220 | retry_reset: |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 221 | sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 222 | |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 223 | if (mask & SDHCI_RESET_ALL) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 224 | host->clock = 0; |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 225 | /* Reset-all turns off SD Bus Power */ |
| 226 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 227 | sdhci_runtime_pm_bus_off(host); |
| 228 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 229 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 230 | /* Wait max 100 ms */ |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 231 | timeout = 100000; |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 232 | |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 233 | if (host->ops->check_power_status && host->pwr && |
| 234 | (mask & SDHCI_RESET_ALL)) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 235 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 236 | |
Sujit Reddy Thumma | 23dd7f8 | 2014-02-14 08:37:47 +0530 | [diff] [blame] | 237 | /* clear pending normal/error interrupt status */ |
| 238 | sdhci_writel(host, sdhci_readl(host, SDHCI_INT_STATUS), |
| 239 | SDHCI_INT_STATUS); |
| 240 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 241 | /* hw clears the bit when it's done */ |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 242 | while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 243 | if (timeout == 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 244 | pr_err("%s: Reset 0x%x never completed.\n", |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 245 | mmc_hostname(host->mmc), (int)mask); |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 246 | if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND) |
| 247 | && host->ops->reset_workaround) { |
| 248 | if (!host->reset_wa_applied) { |
| 249 | /* |
| 250 | * apply the workaround and issue |
| 251 | * reset again. |
| 252 | */ |
| 253 | host->ops->reset_workaround(host, 1); |
| 254 | host->reset_wa_applied = 1; |
| 255 | host->reset_wa_cnt++; |
| 256 | goto retry_reset; |
| 257 | } else { |
| 258 | pr_err("%s: Reset 0x%x failed with workaround\n", |
| 259 | mmc_hostname(host->mmc), |
| 260 | (int)mask); |
| 261 | /* clear the workaround */ |
| 262 | host->ops->reset_workaround(host, 0); |
| 263 | host->reset_wa_applied = 0; |
| 264 | } |
| 265 | } |
| 266 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 267 | sdhci_dumpregs(host); |
| 268 | return; |
| 269 | } |
| 270 | timeout--; |
Ritesh Harjani | 16cf788 | 2015-12-23 13:21:51 +0530 | [diff] [blame] | 271 | udelay(1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 272 | } |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 273 | |
| 274 | if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND) && |
| 275 | host->ops->reset_workaround && host->reset_wa_applied) { |
| 276 | pr_info("%s: Reset 0x%x successful with workaround\n", |
| 277 | mmc_hostname(host->mmc), (int)mask); |
| 278 | /* clear the workaround */ |
| 279 | host->ops->reset_workaround(host, 0); |
| 280 | host->reset_wa_applied = 0; |
| 281 | } |
| 282 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 283 | } |
| 284 | EXPORT_SYMBOL_GPL(sdhci_reset); |
Anton Vorontsov | 063a9db | 2009-03-17 00:14:02 +0300 | [diff] [blame] | 285 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 286 | static void sdhci_do_reset(struct sdhci_host *host, u8 mask) |
| 287 | { |
| 288 | if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 289 | struct mmc_host *mmc = host->mmc; |
| 290 | |
| 291 | if (!mmc->ops->get_cd(mmc)) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | host->ops->reset(host, mask); |
Philip Rakity | 393c1a3 | 2011-01-21 11:26:40 -0800 | [diff] [blame] | 296 | |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 297 | if (mask & SDHCI_RESET_ALL) { |
| 298 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
| 299 | if (host->ops->enable_dma) |
| 300 | host->ops->enable_dma(host); |
| 301 | } |
| 302 | |
| 303 | /* Resetting the controller clears many */ |
| 304 | host->preset_enabled = false; |
Shaohui Xie | 3abc1e80 | 2011-12-29 16:33:00 +0800 | [diff] [blame] | 305 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 308 | static void sdhci_init(struct sdhci_host *host, int soft) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 309 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 310 | struct mmc_host *mmc = host->mmc; |
| 311 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 312 | if (soft) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 313 | sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 314 | else |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 315 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 316 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 317 | host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 318 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | |
| 319 | SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC | |
| 320 | SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 321 | SDHCI_INT_RESPONSE | SDHCI_INT_AUTO_CMD_ERR; |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 322 | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 323 | if (host->tuning_mode == SDHCI_TUNING_MODE_2 || |
| 324 | host->tuning_mode == SDHCI_TUNING_MODE_3) |
| 325 | host->ier |= SDHCI_INT_RETUNE; |
| 326 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 327 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 328 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 329 | |
| 330 | if (soft) { |
| 331 | /* force clock reconfiguration */ |
| 332 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 333 | mmc->ops->set_ios(mmc, &mmc->ios); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 334 | } |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 335 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 336 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 337 | static void sdhci_reinit(struct sdhci_host *host) |
| 338 | { |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 339 | sdhci_init(host, 0); |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 340 | sdhci_enable_card_detection(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 343 | static void __sdhci_led_activate(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 344 | { |
| 345 | u8 ctrl; |
| 346 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 347 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 348 | ctrl |= SDHCI_CTRL_LED; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 349 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 352 | static void __sdhci_led_deactivate(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 353 | { |
| 354 | u8 ctrl; |
| 355 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 356 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 357 | ctrl &= ~SDHCI_CTRL_LED; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 358 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Masahiro Yamada | 4f78230 | 2016-04-14 13:19:39 +0900 | [diff] [blame] | 361 | #if IS_REACHABLE(CONFIG_LEDS_CLASS) |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 362 | static void sdhci_led_control(struct led_classdev *led, |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 363 | enum led_brightness brightness) |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 364 | { |
| 365 | struct sdhci_host *host = container_of(led, struct sdhci_host, led); |
| 366 | unsigned long flags; |
| 367 | |
Sahitya Tummala | e8b0de9 | 2014-04-07 10:33:11 +0530 | [diff] [blame] | 368 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 369 | return; |
| 370 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 371 | spin_lock_irqsave(&host->lock, flags); |
| 372 | |
Sahitya Tummala | ea4e3aa | 2013-05-24 14:08:10 +0530 | [diff] [blame] | 373 | if (host->runtime_suspended || sdhci_check_state(host)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 374 | goto out; |
| 375 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 376 | if (brightness == LED_OFF) |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 377 | __sdhci_led_deactivate(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 378 | else |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 379 | __sdhci_led_activate(host); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 380 | out: |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 381 | spin_unlock_irqrestore(&host->lock, flags); |
| 382 | } |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 383 | |
| 384 | static int sdhci_led_register(struct sdhci_host *host) |
| 385 | { |
| 386 | struct mmc_host *mmc = host->mmc; |
| 387 | |
| 388 | snprintf(host->led_name, sizeof(host->led_name), |
| 389 | "%s::", mmc_hostname(mmc)); |
| 390 | |
| 391 | host->led.name = host->led_name; |
| 392 | host->led.brightness = LED_OFF; |
| 393 | host->led.default_trigger = mmc_hostname(mmc); |
| 394 | host->led.brightness_set = sdhci_led_control; |
| 395 | |
| 396 | return led_classdev_register(mmc_dev(mmc), &host->led); |
| 397 | } |
| 398 | |
| 399 | static void sdhci_led_unregister(struct sdhci_host *host) |
| 400 | { |
| 401 | led_classdev_unregister(&host->led); |
| 402 | } |
| 403 | |
| 404 | static inline void sdhci_led_activate(struct sdhci_host *host) |
| 405 | { |
| 406 | } |
| 407 | |
| 408 | static inline void sdhci_led_deactivate(struct sdhci_host *host) |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | #else |
| 413 | |
| 414 | static inline int sdhci_led_register(struct sdhci_host *host) |
| 415 | { |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static inline void sdhci_led_unregister(struct sdhci_host *host) |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | static inline void sdhci_led_activate(struct sdhci_host *host) |
| 424 | { |
| 425 | __sdhci_led_activate(host); |
| 426 | } |
| 427 | |
| 428 | static inline void sdhci_led_deactivate(struct sdhci_host *host) |
| 429 | { |
| 430 | __sdhci_led_deactivate(host); |
| 431 | } |
| 432 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 433 | #endif |
| 434 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 435 | /*****************************************************************************\ |
| 436 | * * |
| 437 | * Core functions * |
| 438 | * * |
| 439 | \*****************************************************************************/ |
| 440 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 441 | static void sdhci_read_block_pio(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 442 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 443 | unsigned long flags; |
| 444 | size_t blksize, len, chunk; |
Steven Noonan | 7244b85 | 2008-10-01 01:50:25 -0700 | [diff] [blame] | 445 | u32 uninitialized_var(scratch); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 446 | u8 *buf; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 447 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 448 | DBG("PIO reading\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 449 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 450 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 451 | chunk = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 452 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 453 | local_irq_save(flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 454 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 455 | while (blksize) { |
Fabio Estevam | bf3a35a | 2015-05-09 18:44:51 -0300 | [diff] [blame] | 456 | BUG_ON(!sg_miter_next(&host->sg_miter)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 457 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 458 | len = min(host->sg_miter.length, blksize); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 459 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 460 | blksize -= len; |
| 461 | host->sg_miter.consumed = len; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 462 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 463 | buf = host->sg_miter.addr; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 464 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 465 | while (len) { |
| 466 | if (chunk == 0) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 467 | scratch = sdhci_readl(host, SDHCI_BUFFER); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 468 | chunk = 4; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 469 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 470 | |
| 471 | *buf = scratch & 0xFF; |
| 472 | |
| 473 | buf++; |
| 474 | scratch >>= 8; |
| 475 | chunk--; |
| 476 | len--; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 479 | |
| 480 | sg_miter_stop(&host->sg_miter); |
| 481 | |
| 482 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 483 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 484 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 485 | static void sdhci_write_block_pio(struct sdhci_host *host) |
| 486 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 487 | unsigned long flags; |
| 488 | size_t blksize, len, chunk; |
| 489 | u32 scratch; |
| 490 | u8 *buf; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 491 | |
| 492 | DBG("PIO writing\n"); |
| 493 | |
| 494 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 495 | chunk = 0; |
| 496 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 497 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 498 | local_irq_save(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 499 | |
| 500 | while (blksize) { |
Fabio Estevam | bf3a35a | 2015-05-09 18:44:51 -0300 | [diff] [blame] | 501 | BUG_ON(!sg_miter_next(&host->sg_miter)); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 502 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 503 | len = min(host->sg_miter.length, blksize); |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 504 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 505 | blksize -= len; |
| 506 | host->sg_miter.consumed = len; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 507 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 508 | buf = host->sg_miter.addr; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 509 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 510 | while (len) { |
| 511 | scratch |= (u32)*buf << (chunk * 8); |
| 512 | |
| 513 | buf++; |
| 514 | chunk++; |
| 515 | len--; |
| 516 | |
| 517 | if ((chunk == 4) || ((len == 0) && (blksize == 0))) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 518 | sdhci_writel(host, scratch, SDHCI_BUFFER); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 519 | chunk = 0; |
| 520 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 521 | } |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 522 | } |
| 523 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 524 | |
| 525 | sg_miter_stop(&host->sg_miter); |
| 526 | |
| 527 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void sdhci_transfer_pio(struct sdhci_host *host) |
| 531 | { |
| 532 | u32 mask; |
| 533 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 534 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 535 | return; |
| 536 | |
| 537 | if (host->data->flags & MMC_DATA_READ) |
| 538 | mask = SDHCI_DATA_AVAILABLE; |
| 539 | else |
| 540 | mask = SDHCI_SPACE_AVAILABLE; |
| 541 | |
Pierre Ossman | 4a3cba3 | 2008-07-29 00:11:16 +0200 | [diff] [blame] | 542 | /* |
| 543 | * Some controllers (JMicron JMB38x) mess up the buffer bits |
| 544 | * for transfers < 4 bytes. As long as it is just one block, |
| 545 | * we can ignore the bits. |
| 546 | */ |
| 547 | if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) && |
| 548 | (host->data->blocks == 1)) |
| 549 | mask = ~0; |
| 550 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 551 | while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { |
Anton Vorontsov | 3e3bf20 | 2009-03-17 00:14:00 +0300 | [diff] [blame] | 552 | if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY) |
| 553 | udelay(100); |
| 554 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 555 | if (host->data->flags & MMC_DATA_READ) |
| 556 | sdhci_read_block_pio(host); |
| 557 | else |
| 558 | sdhci_write_block_pio(host); |
| 559 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 560 | host->blocks--; |
| 561 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 562 | break; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | DBG("PIO transfer complete.\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 568 | static int sdhci_pre_dma_transfer(struct sdhci_host *host, |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 569 | struct mmc_data *data, int cookie) |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 570 | { |
| 571 | int sg_count; |
| 572 | |
Russell King | 94538e5 | 2016-01-26 13:40:37 +0000 | [diff] [blame] | 573 | /* |
| 574 | * If the data buffers are already mapped, return the previous |
| 575 | * dma_map_sg() result. |
| 576 | */ |
| 577 | if (data->host_cookie == COOKIE_PRE_MAPPED) |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 578 | return data->sg_count; |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 579 | |
| 580 | sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 581 | data->flags & MMC_DATA_WRITE ? |
| 582 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 583 | |
| 584 | if (sg_count == 0) |
| 585 | return -ENOSPC; |
| 586 | |
| 587 | data->sg_count = sg_count; |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 588 | data->host_cookie = cookie; |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 589 | |
| 590 | return sg_count; |
| 591 | } |
| 592 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 593 | static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags) |
| 594 | { |
| 595 | local_irq_save(*flags); |
Cong Wang | 482fce9 | 2011-11-27 13:27:00 +0800 | [diff] [blame] | 596 | return kmap_atomic(sg_page(sg)) + sg->offset; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags) |
| 600 | { |
Cong Wang | 482fce9 | 2011-11-27 13:27:00 +0800 | [diff] [blame] | 601 | kunmap_atomic(buffer); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 602 | local_irq_restore(*flags); |
| 603 | } |
| 604 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 605 | static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc, |
| 606 | dma_addr_t addr, int len, unsigned cmd) |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 607 | { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 608 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 609 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 610 | /* 32-bit and 64-bit descriptors have these members in same position */ |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 611 | dma_desc->cmd = cpu_to_le16(cmd); |
| 612 | dma_desc->len = cpu_to_le16(len); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 613 | dma_desc->addr_lo = cpu_to_le32((u32)addr); |
| 614 | |
| 615 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 616 | dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32); |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 619 | static void sdhci_adma_mark_end(void *desc) |
| 620 | { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 621 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 622 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 623 | /* 32-bit and 64-bit descriptors have 'cmd' in same position */ |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 624 | dma_desc->cmd |= cpu_to_le16(ADMA2_END); |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 627 | static void sdhci_adma_table_pre(struct sdhci_host *host, |
| 628 | struct mmc_data *data, int sg_count) |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 629 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 630 | struct scatterlist *sg; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 631 | unsigned long flags; |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 632 | dma_addr_t addr, align_addr; |
| 633 | void *desc, *align; |
| 634 | char *buffer; |
| 635 | int len, offset, i; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * The spec does not specify endianness of descriptor table. |
| 639 | * We currently guess that it is LE. |
| 640 | */ |
| 641 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 642 | host->sg_count = sg_count; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 643 | |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 644 | desc = host->adma_table; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 645 | align = host->align_buffer; |
| 646 | |
| 647 | align_addr = host->align_addr; |
| 648 | |
| 649 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 650 | addr = sg_dma_address(sg); |
| 651 | len = sg_dma_len(sg); |
| 652 | |
| 653 | /* |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 654 | * The SDHCI specification states that ADMA addresses must |
| 655 | * be 32-bit aligned. If they aren't, then we use a bounce |
| 656 | * buffer for the (up to three) bytes that screw up the |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 657 | * alignment. |
| 658 | */ |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 659 | offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) & |
| 660 | SDHCI_ADMA2_MASK; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 661 | if (offset) { |
| 662 | if (data->flags & MMC_DATA_WRITE) { |
| 663 | buffer = sdhci_kmap_atomic(sg, &flags); |
| 664 | memcpy(align, buffer, offset); |
| 665 | sdhci_kunmap_atomic(buffer, &flags); |
| 666 | } |
| 667 | |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 668 | /* tran, valid */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 669 | sdhci_adma_write_desc(host, desc, align_addr, offset, |
Adrian Hunter | 739d46d | 2014-11-04 12:42:44 +0200 | [diff] [blame] | 670 | ADMA2_TRAN_VALID); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 671 | |
| 672 | BUG_ON(offset > 65536); |
| 673 | |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 674 | align += SDHCI_ADMA2_ALIGN; |
| 675 | align_addr += SDHCI_ADMA2_ALIGN; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 676 | |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 677 | desc += host->desc_sz; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 678 | |
| 679 | addr += offset; |
| 680 | len -= offset; |
| 681 | } |
| 682 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 683 | BUG_ON(len > 65536); |
| 684 | |
Adrian Hunter | 347ea32 | 2015-11-26 14:00:48 +0200 | [diff] [blame] | 685 | if (len) { |
| 686 | /* tran, valid */ |
| 687 | sdhci_adma_write_desc(host, desc, addr, len, |
| 688 | ADMA2_TRAN_VALID); |
| 689 | desc += host->desc_sz; |
| 690 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * If this triggers then we have a calculation bug |
| 694 | * somewhere. :/ |
| 695 | */ |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 696 | WARN_ON((desc - host->adma_table) >= host->adma_table_sz); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 699 | if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) { |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 700 | /* Mark the last descriptor as the terminating descriptor */ |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 701 | if (desc != host->adma_table) { |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 702 | desc -= host->desc_sz; |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 703 | sdhci_adma_mark_end(desc); |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 704 | } |
| 705 | } else { |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 706 | /* Add a terminating entry - nop, end, valid */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 707 | sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID); |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 708 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static void sdhci_adma_table_post(struct sdhci_host *host, |
| 712 | struct mmc_data *data) |
| 713 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 714 | struct scatterlist *sg; |
| 715 | int i, size; |
Adrian Hunter | 1c3d5f6 | 2014-11-04 12:42:41 +0200 | [diff] [blame] | 716 | void *align; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 717 | char *buffer; |
| 718 | unsigned long flags; |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 719 | u32 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)); |
| 720 | |
| 721 | trace_mmc_adma_table_post(command, data->sg_len); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 722 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 723 | if (data->flags & MMC_DATA_READ) { |
| 724 | bool has_unaligned = false; |
Russell King | de0b65a | 2014-04-25 12:58:29 +0100 | [diff] [blame] | 725 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 726 | /* Do a quick scan of the SG list for any unaligned mappings */ |
| 727 | for_each_sg(data->sg, sg, host->sg_count, i) |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 728 | if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) { |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 729 | has_unaligned = true; |
| 730 | break; |
| 731 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 732 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 733 | if (has_unaligned) { |
| 734 | dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg, |
Russell King | f55c98f | 2016-01-26 13:40:11 +0000 | [diff] [blame] | 735 | data->sg_len, DMA_FROM_DEVICE); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 736 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 737 | align = host->align_buffer; |
| 738 | |
| 739 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 740 | if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) { |
| 741 | size = SDHCI_ADMA2_ALIGN - |
| 742 | (sg_dma_address(sg) & SDHCI_ADMA2_MASK); |
| 743 | |
| 744 | buffer = sdhci_kmap_atomic(sg, &flags); |
| 745 | memcpy(buffer, align, size); |
| 746 | sdhci_kunmap_atomic(buffer, &flags); |
| 747 | |
| 748 | align += SDHCI_ADMA2_ALIGN; |
| 749 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 755 | static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 756 | { |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 757 | u8 count; |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 758 | struct mmc_data *data = cmd->data; |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 759 | unsigned target_timeout, current_timeout; |
Sahitya Tummala | a5733ab5 | 2013-06-10 16:32:51 +0530 | [diff] [blame] | 760 | u32 curr_clk = 0; /* In KHz */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 761 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 762 | /* |
| 763 | * If the host controller provides us with an incorrect timeout |
| 764 | * value, just skip the check and use 0xE. The hardware may take |
| 765 | * longer to time out, but that's much better than having a too-short |
| 766 | * timeout value. |
| 767 | */ |
Pierre Ossman | 11a2f1b | 2009-06-21 20:59:33 +0200 | [diff] [blame] | 768 | if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 769 | return 0xE; |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 770 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 771 | /* Unspecified timeout, assume max */ |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 772 | if (!data && !cmd->busy_timeout) |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 773 | return 0xE; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 774 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 775 | /* timeout in us */ |
| 776 | if (!data) |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 777 | target_timeout = cmd->busy_timeout * 1000; |
Andy Shevchenko | 78a2ca2 | 2011-08-03 18:35:59 +0300 | [diff] [blame] | 778 | else { |
Russell King | fafcfda | 2016-01-26 13:40:58 +0000 | [diff] [blame] | 779 | target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000); |
Russell King | 7f05538 | 2016-01-26 13:41:04 +0000 | [diff] [blame] | 780 | if (host->clock && data->timeout_clks) { |
| 781 | unsigned long long val; |
| 782 | |
| 783 | /* |
| 784 | * data->timeout_clks is in units of clock cycles. |
| 785 | * host->clock is in Hz. target_timeout is in us. |
| 786 | * Hence, us = 1000000 * cycles / Hz. Round up. |
| 787 | */ |
Haibo Chen | 02265cd6 | 2016-10-17 10:18:37 +0200 | [diff] [blame] | 788 | val = 1000000ULL * data->timeout_clks; |
Russell King | 7f05538 | 2016-01-26 13:41:04 +0000 | [diff] [blame] | 789 | if (do_div(val, host->clock)) |
| 790 | target_timeout++; |
| 791 | target_timeout += val; |
| 792 | } |
Andy Shevchenko | 78a2ca2 | 2011-08-03 18:35:59 +0300 | [diff] [blame] | 793 | } |
Anton Vorontsov | 81b3980 | 2009-09-22 16:45:13 -0700 | [diff] [blame] | 794 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 795 | /* |
| 796 | * Figure out needed cycles. |
| 797 | * We do this in steps in order to fit inside a 32 bit int. |
| 798 | * The first step is the minimum timeout, which will have a |
| 799 | * minimum resolution of 6 bits: |
| 800 | * (1) 2^13*1000 > 2^22, |
| 801 | * (2) host->timeout_clk < 2^16 |
| 802 | * => |
| 803 | * (1) / (2) > 2^6 |
| 804 | */ |
| 805 | count = 0; |
Sahitya Tummala | a5733ab5 | 2013-06-10 16:32:51 +0530 | [diff] [blame] | 806 | if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) { |
| 807 | curr_clk = host->clock / 1000; |
| 808 | if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4) |
| 809 | curr_clk /= 4; |
| 810 | current_timeout = (1 << 13) * 1000 / curr_clk; |
| 811 | } else { |
| 812 | current_timeout = (1 << 13) * 1000 / host->timeout_clk; |
| 813 | } |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 814 | while (current_timeout < target_timeout) { |
| 815 | count++; |
| 816 | current_timeout <<= 1; |
| 817 | if (count >= 0xF) |
| 818 | break; |
| 819 | } |
| 820 | |
Sahitya Tummala | 7c9780d | 2013-04-12 11:59:25 +0530 | [diff] [blame] | 821 | if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) { |
| 822 | if (count >= 0xF) { |
| 823 | DBG("%s: Too large timeout 0x%x requested for CMD%d!\n", |
| 824 | mmc_hostname(host->mmc), count, cmd->opcode); |
| 825 | count = 0xE; |
| 826 | } |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 829 | return count; |
| 830 | } |
| 831 | |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 832 | static void sdhci_set_transfer_irqs(struct sdhci_host *host) |
| 833 | { |
| 834 | u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL; |
| 835 | u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR; |
| 836 | |
| 837 | if (host->flags & SDHCI_REQ_USE_DMA) |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 838 | host->ier = (host->ier & ~pio_irqs) | dma_irqs; |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 839 | else |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 840 | host->ier = (host->ier & ~dma_irqs) | pio_irqs; |
| 841 | |
| 842 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 843 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 844 | } |
| 845 | |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 846 | static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd) |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 847 | { |
| 848 | u8 count; |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 849 | |
| 850 | if (host->ops->set_timeout) { |
| 851 | host->ops->set_timeout(host, cmd); |
| 852 | } else { |
| 853 | count = sdhci_calc_timeout(host, cmd); |
| 854 | sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL); |
| 855 | } |
| 856 | } |
| 857 | |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 858 | static void sdhci_set_blk_size_reg(struct sdhci_host *host, unsigned int blksz, |
| 859 | unsigned int sdma_boundary) |
| 860 | { |
| 861 | if (host->flags & SDHCI_USE_ADMA) |
| 862 | sdhci_writew(host, SDHCI_MAKE_BLKSZ(0, blksz), |
| 863 | SDHCI_BLOCK_SIZE); |
| 864 | else |
| 865 | sdhci_writew(host, SDHCI_MAKE_BLKSZ(sdma_boundary, blksz), |
| 866 | SDHCI_BLOCK_SIZE); |
| 867 | } |
| 868 | |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 869 | static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd) |
| 870 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 871 | u8 ctrl; |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 872 | struct mmc_data *data = cmd->data; |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 873 | |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 874 | if (sdhci_data_line_cmd(cmd)) |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 875 | sdhci_set_timeout(host, cmd); |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 876 | |
| 877 | if (!data) |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 878 | return; |
| 879 | |
Adrian Hunter | 43dea09 | 2016-06-29 16:24:26 +0300 | [diff] [blame] | 880 | WARN_ON(host->data); |
| 881 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 882 | /* Sanity checks */ |
Asutosh Das | aafcad4 | 2013-01-10 21:05:49 +0530 | [diff] [blame] | 883 | BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size); |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 884 | BUG_ON(data->blksz > host->mmc->max_blk_size); |
| 885 | BUG_ON(data->blocks > 65535); |
| 886 | |
| 887 | host->data = data; |
| 888 | host->data_early = 0; |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 889 | host->data->bytes_xfered = 0; |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 890 | |
Russell King | fce1442 | 2016-01-26 13:41:20 +0000 | [diff] [blame] | 891 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 892 | struct scatterlist *sg; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 893 | unsigned int length_mask, offset_mask; |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 894 | int i; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 895 | |
Russell King | fce1442 | 2016-01-26 13:41:20 +0000 | [diff] [blame] | 896 | host->flags |= SDHCI_REQ_USE_DMA; |
| 897 | |
Sahitya Tummala | 01b9a92 | 2016-04-05 15:27:36 +0530 | [diff] [blame] | 898 | if ((host->quirks2 & SDHCI_QUIRK2_USE_PIO_FOR_EMMC_TUNING) && |
| 899 | cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) |
| 900 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 901 | |
Russell King | fce1442 | 2016-01-26 13:41:20 +0000 | [diff] [blame] | 902 | /* |
| 903 | * FIXME: This doesn't account for merging when mapping the |
| 904 | * scatterlist. |
| 905 | * |
| 906 | * The assumption here being that alignment and lengths are |
| 907 | * the same after DMA mapping to device address space. |
| 908 | */ |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 909 | length_mask = 0; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 910 | offset_mask = 0; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 911 | if (host->flags & SDHCI_USE_ADMA) { |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 912 | if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) { |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 913 | length_mask = 3; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 914 | /* |
| 915 | * As we use up to 3 byte chunks to work |
| 916 | * around alignment problems, we need to |
| 917 | * check the offset as well. |
| 918 | */ |
| 919 | offset_mask = 3; |
| 920 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 921 | } else { |
| 922 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 923 | length_mask = 3; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 924 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) |
| 925 | offset_mask = 3; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 928 | if (unlikely(length_mask | offset_mask)) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 929 | for_each_sg(data->sg, sg, data->sg_len, i) { |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 930 | if (sg->length & length_mask) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 931 | DBG("Reverting to PIO because of transfer size (%d)\n", |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 932 | sg->length); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 933 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 934 | break; |
| 935 | } |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 936 | if (sg->offset & offset_mask) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 937 | DBG("Reverting to PIO because of bad alignment\n"); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 938 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 945 | if (host->flags & SDHCI_REQ_USE_DMA) { |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 946 | int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 947 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 948 | if (sg_cnt <= 0) { |
| 949 | /* |
| 950 | * This only happens when someone fed |
| 951 | * us an invalid request. |
| 952 | */ |
| 953 | WARN_ON(1); |
| 954 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 955 | } else if (host->flags & SDHCI_USE_ADMA) { |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 956 | trace_mmc_adma_table_pre(cmd->opcode, data->sg_len); |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 957 | sdhci_adma_table_pre(host, data, sg_cnt); |
| 958 | |
| 959 | sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS); |
| 960 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 961 | sdhci_writel(host, |
| 962 | (u64)host->adma_addr >> 32, |
| 963 | SDHCI_ADMA_ADDRESS_HI); |
| 964 | } else { |
| 965 | WARN_ON(sg_cnt != 1); |
| 966 | sdhci_writel(host, sg_dma_address(data->sg), |
| 967 | SDHCI_DMA_ADDRESS); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 971 | /* |
| 972 | * Always adjust the DMA selection as some controllers |
| 973 | * (e.g. JMicron) can't do PIO properly when the selection |
| 974 | * is ADMA. |
| 975 | */ |
| 976 | if (host->version >= SDHCI_SPEC_200) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 977 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 978 | ctrl &= ~SDHCI_CTRL_DMA_MASK; |
| 979 | if ((host->flags & SDHCI_REQ_USE_DMA) && |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 980 | (host->flags & SDHCI_USE_ADMA)) { |
| 981 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 982 | ctrl |= SDHCI_CTRL_ADMA64; |
| 983 | else |
| 984 | ctrl |= SDHCI_CTRL_ADMA32; |
| 985 | } else { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 986 | ctrl |= SDHCI_CTRL_SDMA; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 987 | } |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 988 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 991 | if (!(host->flags & SDHCI_REQ_USE_DMA)) { |
Sebastian Andrzej Siewior | da60a91 | 2009-06-18 09:33:32 +0200 | [diff] [blame] | 992 | int flags; |
| 993 | |
| 994 | flags = SG_MITER_ATOMIC; |
| 995 | if (host->data->flags & MMC_DATA_READ) |
| 996 | flags |= SG_MITER_TO_SG; |
| 997 | else |
| 998 | flags |= SG_MITER_FROM_SG; |
| 999 | sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 1000 | host->blocks = data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1001 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1002 | |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 1003 | sdhci_set_transfer_irqs(host); |
| 1004 | |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 1005 | /* Set the DMA boundary value and block size */ |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 1006 | sdhci_set_blk_size_reg(host, data->blksz, SDHCI_DEFAULT_BOUNDARY_ARG); |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1007 | sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1008 | MMC_TRACE(host->mmc, |
| 1009 | "%s: 0x28=0x%08x 0x3E=0x%08x 0x06=0x%08x\n", __func__, |
| 1010 | sdhci_readb(host, SDHCI_HOST_CONTROL), |
| 1011 | sdhci_readw(host, SDHCI_HOST_CONTROL2), |
| 1012 | sdhci_readw(host, SDHCI_BLOCK_COUNT)); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1015 | static inline bool sdhci_auto_cmd12(struct sdhci_host *host, |
| 1016 | struct mmc_request *mrq) |
| 1017 | { |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1018 | return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) && |
| 1019 | !mrq->cap_cmd_during_tfr; |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1020 | } |
| 1021 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1022 | static void sdhci_set_transfer_mode(struct sdhci_host *host, |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1023 | struct mmc_command *cmd) |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1024 | { |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1025 | u16 mode = 0; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1026 | struct mmc_data *data = cmd->data; |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1027 | |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1028 | if (data == NULL) { |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1029 | if (host->quirks2 & |
| 1030 | SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) { |
| 1031 | sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE); |
| 1032 | } else { |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1033 | /* clear Auto CMD settings for no data CMDs */ |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1034 | mode = sdhci_readw(host, SDHCI_TRANSFER_MODE); |
| 1035 | sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 | |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1036 | SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE); |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1037 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1038 | return; |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1039 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1040 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1041 | WARN_ON(!host->data); |
| 1042 | |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1043 | if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE)) |
| 1044 | mode = SDHCI_TRNS_BLK_CNT_EN; |
| 1045 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1046 | if (mmc_op_multi(cmd->opcode) || data->blocks > 1) { |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1047 | mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1048 | /* |
| 1049 | * If we are sending CMD23, CMD12 never gets sent |
| 1050 | * on successful completion (so no Auto-CMD12). |
| 1051 | */ |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1052 | if (sdhci_auto_cmd12(host, cmd->mrq) && |
Corneliu Doban | 85cc1c3 | 2015-02-09 16:06:29 -0800 | [diff] [blame] | 1053 | (cmd->opcode != SD_IO_RW_EXTENDED)) |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1054 | mode |= SDHCI_TRNS_AUTO_CMD12; |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1055 | else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) { |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1056 | mode |= SDHCI_TRNS_AUTO_CMD23; |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1057 | sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2); |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1058 | } |
Jerry Huang | c4512f7 | 2010-08-10 18:01:59 -0700 | [diff] [blame] | 1059 | } |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1060 | |
Sahitya Tummala | 9dca7d7 | 2013-02-25 15:45:32 +0530 | [diff] [blame] | 1061 | if (data->flags & MMC_DATA_READ) { |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1062 | mode |= SDHCI_TRNS_READ; |
Ritesh Harjani | 210c230 | 2014-11-14 11:06:40 +0530 | [diff] [blame] | 1063 | if (host->ops->toggle_cdr) { |
| 1064 | if ((cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) || |
| 1065 | (cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) || |
| 1066 | (cmd->opcode == MMC_SEND_TUNING_BLOCK)) |
| 1067 | host->ops->toggle_cdr(host, false); |
| 1068 | else |
| 1069 | host->ops->toggle_cdr(host, true); |
| 1070 | } |
Sahitya Tummala | 9dca7d7 | 2013-02-25 15:45:32 +0530 | [diff] [blame] | 1071 | } |
| 1072 | if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE)) |
| 1073 | host->ops->toggle_cdr(host, false); |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 1074 | if (host->flags & SDHCI_REQ_USE_DMA) |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1075 | mode |= SDHCI_TRNS_DMA; |
| 1076 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1077 | sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1078 | MMC_TRACE(host->mmc, "%s: 0x00=0x%08x 0x0C=0x%08x\n", __func__, |
| 1079 | sdhci_readw(host, SDHCI_ARGUMENT2), |
| 1080 | sdhci_readw(host, SDHCI_TRANSFER_MODE)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Adrian Hunter | 0cc563c | 2016-06-29 16:24:28 +0300 | [diff] [blame] | 1083 | static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq) |
| 1084 | { |
| 1085 | return (!(host->flags & SDHCI_DEVICE_DEAD) && |
| 1086 | ((mrq->cmd && mrq->cmd->error) || |
| 1087 | (mrq->sbc && mrq->sbc->error) || |
| 1088 | (mrq->data && ((mrq->data->error && !mrq->data->stop) || |
| 1089 | (mrq->data->stop && mrq->data->stop->error))) || |
| 1090 | (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))); |
| 1091 | } |
| 1092 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 1093 | static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq) |
| 1094 | { |
| 1095 | int i; |
| 1096 | |
| 1097 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 1098 | if (host->mrqs_done[i] == mrq) { |
| 1099 | WARN_ON(1); |
| 1100 | return; |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 1105 | if (!host->mrqs_done[i]) { |
| 1106 | host->mrqs_done[i] = mrq; |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | WARN_ON(i >= SDHCI_MAX_MRQS); |
| 1112 | |
| 1113 | tasklet_schedule(&host->finish_tasklet); |
| 1114 | } |
| 1115 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1116 | static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq) |
| 1117 | { |
Adrian Hunter | 5a8a3fe | 2016-06-29 16:24:30 +0300 | [diff] [blame] | 1118 | if (host->cmd && host->cmd->mrq == mrq) |
| 1119 | host->cmd = NULL; |
| 1120 | |
| 1121 | if (host->data_cmd && host->data_cmd->mrq == mrq) |
| 1122 | host->data_cmd = NULL; |
| 1123 | |
| 1124 | if (host->data && host->data->mrq == mrq) |
| 1125 | host->data = NULL; |
| 1126 | |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 1127 | if (sdhci_needs_reset(host, mrq)) |
| 1128 | host->pending_reset = true; |
| 1129 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 1130 | __sdhci_finish_mrq(host, mrq); |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1131 | } |
| 1132 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1133 | static void sdhci_finish_data(struct sdhci_host *host) |
| 1134 | { |
Adrian Hunter | 33a57ad | 2016-06-29 16:24:36 +0300 | [diff] [blame] | 1135 | struct mmc_command *data_cmd = host->data_cmd; |
| 1136 | struct mmc_data *data = host->data; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1137 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1138 | host->data = NULL; |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 1139 | host->data_cmd = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1140 | |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1141 | MMC_TRACE(host->mmc, "%s: 0x24=0x%08x\n", __func__, |
| 1142 | sdhci_readl(host, SDHCI_PRESENT_STATE)); |
Russell King | add8913 | 2016-01-26 13:40:42 +0000 | [diff] [blame] | 1143 | if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) == |
| 1144 | (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) |
| 1145 | sdhci_adma_table_post(host, data); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1146 | |
| 1147 | /* |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1148 | * The specification states that the block count register must |
| 1149 | * be updated, but it does not specify at what point in the |
| 1150 | * data flow. That makes the register entirely useless to read |
| 1151 | * back so we have to assume that nothing made it to the card |
| 1152 | * in the event of an error. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1153 | */ |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1154 | if (data->error) |
| 1155 | data->bytes_xfered = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1156 | else |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1157 | data->bytes_xfered = data->blksz * data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1158 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1159 | /* |
| 1160 | * Need to send CMD12 if - |
| 1161 | * a) open-ended multiblock transfer (no CMD23) |
| 1162 | * b) error in multiblock transfer |
| 1163 | */ |
| 1164 | if (data->stop && |
| 1165 | (data->error || |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1166 | !data->mrq->sbc)) { |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1167 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1168 | /* |
| 1169 | * The controller needs a reset of internal state machines |
| 1170 | * upon error conditions. |
| 1171 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1172 | if (data->error) { |
Adrian Hunter | 33a57ad | 2016-06-29 16:24:36 +0300 | [diff] [blame] | 1173 | if (!host->cmd || host->cmd == data_cmd) |
| 1174 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 1175 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1176 | } |
| 1177 | |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1178 | /* |
| 1179 | * 'cap_cmd_during_tfr' request must not use the command line |
| 1180 | * after mmc_command_done() has been called. It is upper layer's |
| 1181 | * responsibility to send the stop command if required. |
| 1182 | */ |
| 1183 | if (data->mrq->cap_cmd_during_tfr) { |
| 1184 | sdhci_finish_mrq(host, data->mrq); |
| 1185 | } else { |
| 1186 | /* Avoid triggering warning in sdhci_send_command() */ |
| 1187 | host->cmd = NULL; |
| 1188 | sdhci_send_command(host, data->stop); |
| 1189 | } |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1190 | } else { |
| 1191 | sdhci_finish_mrq(host, data->mrq); |
| 1192 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 1195 | static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq, |
| 1196 | unsigned long timeout) |
| 1197 | { |
| 1198 | if (sdhci_data_line_cmd(mrq->cmd)) |
| 1199 | mod_timer(&host->data_timer, timeout); |
| 1200 | else |
| 1201 | mod_timer(&host->timer, timeout); |
| 1202 | } |
| 1203 | |
| 1204 | static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq) |
| 1205 | { |
| 1206 | if (sdhci_data_line_cmd(mrq->cmd)) |
| 1207 | del_timer(&host->data_timer); |
| 1208 | else |
| 1209 | del_timer(&host->timer); |
| 1210 | } |
| 1211 | |
Dong Aisheng | c0e55129 | 2013-09-13 19:11:31 +0800 | [diff] [blame] | 1212 | void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1213 | { |
| 1214 | int flags; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1215 | u32 mask; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1216 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1217 | |
| 1218 | WARN_ON(host->cmd); |
| 1219 | |
Russell King | 9677620 | 2016-01-26 13:39:34 +0000 | [diff] [blame] | 1220 | /* Initially, a command has no error */ |
| 1221 | cmd->error = 0; |
| 1222 | |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 1223 | if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) && |
| 1224 | cmd->opcode == MMC_STOP_TRANSMISSION) |
| 1225 | cmd->flags |= MMC_RSP_BUSY; |
| 1226 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1227 | /* Wait max 10 ms */ |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 1228 | timeout = 10000; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1229 | |
| 1230 | mask = SDHCI_CMD_INHIBIT; |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 1231 | if (sdhci_data_line_cmd(cmd)) |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1232 | mask |= SDHCI_DATA_INHIBIT; |
| 1233 | |
| 1234 | /* We shouldn't wait for data inihibit for stop commands, even |
| 1235 | though they might use busy signaling */ |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1236 | if (cmd->mrq->data && (cmd == cmd->mrq->data->stop)) |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1237 | mask &= ~SDHCI_DATA_INHIBIT; |
| 1238 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1239 | while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1240 | if (timeout == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 1241 | pr_err("%s: Controller never released inhibit bit(s).\n", |
| 1242 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1243 | sdhci_dumpregs(host); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1244 | cmd->error = -EIO; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1245 | sdhci_finish_mrq(host, cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1246 | return; |
| 1247 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1248 | timeout--; |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 1249 | udelay(1); |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1250 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1251 | |
Adrian Hunter | 3e1a689 | 2013-11-14 10:16:20 +0200 | [diff] [blame] | 1252 | timeout = jiffies; |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 1253 | if (!cmd->data && cmd->busy_timeout > 9000) |
| 1254 | timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ; |
Adrian Hunter | 3e1a689 | 2013-11-14 10:16:20 +0200 | [diff] [blame] | 1255 | else |
| 1256 | timeout += 10 * HZ; |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 1257 | sdhci_mod_timer(host, cmd->mrq, timeout); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1258 | |
| 1259 | host->cmd = cmd; |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 1260 | if (sdhci_data_line_cmd(cmd)) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 1261 | WARN_ON(host->data_cmd); |
| 1262 | host->data_cmd = cmd; |
| 1263 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1264 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 1265 | sdhci_prepare_data(host, cmd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1266 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1267 | sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1268 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1269 | sdhci_set_transfer_mode(host, cmd); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1270 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1271 | if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1272 | pr_err("%s: Unsupported response type!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1273 | mmc_hostname(host->mmc)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1274 | cmd->error = -EINVAL; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1275 | sdhci_finish_mrq(host, cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | if (!(cmd->flags & MMC_RSP_PRESENT)) |
| 1280 | flags = SDHCI_CMD_RESP_NONE; |
| 1281 | else if (cmd->flags & MMC_RSP_136) |
| 1282 | flags = SDHCI_CMD_RESP_LONG; |
| 1283 | else if (cmd->flags & MMC_RSP_BUSY) |
| 1284 | flags = SDHCI_CMD_RESP_SHORT_BUSY; |
| 1285 | else |
| 1286 | flags = SDHCI_CMD_RESP_SHORT; |
| 1287 | |
| 1288 | if (cmd->flags & MMC_RSP_CRC) |
| 1289 | flags |= SDHCI_CMD_CRC; |
| 1290 | if (cmd->flags & MMC_RSP_OPCODE) |
| 1291 | flags |= SDHCI_CMD_INDEX; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 1292 | |
| 1293 | /* CMD19 is special in that the Data Present Select should be set */ |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 1294 | if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK || |
| 1295 | cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1296 | flags |= SDHCI_CMD_DATA; |
| 1297 | |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 1298 | if (cmd->data) |
| 1299 | host->data_start_time = ktime_get(); |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 1300 | trace_mmc_cmd_rw_start(cmd->opcode, cmd->arg, cmd->flags); |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1301 | MMC_TRACE(host->mmc, |
| 1302 | "%s: updated 0x8=0x%08x 0xC=0x%08x 0xE=0x%08x\n", __func__, |
| 1303 | sdhci_readl(host, SDHCI_ARGUMENT), |
| 1304 | sdhci_readw(host, SDHCI_TRANSFER_MODE), |
| 1305 | sdhci_readw(host, SDHCI_COMMAND)); |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1306 | sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1307 | } |
Dong Aisheng | c0e55129 | 2013-09-13 19:11:31 +0800 | [diff] [blame] | 1308 | EXPORT_SYMBOL_GPL(sdhci_send_command); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1309 | |
| 1310 | static void sdhci_finish_command(struct sdhci_host *host) |
| 1311 | { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1312 | struct mmc_command *cmd = host->cmd; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1313 | int i; |
| 1314 | |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1315 | host->cmd = NULL; |
| 1316 | |
| 1317 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 1318 | if (cmd->flags & MMC_RSP_136) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1319 | /* CRC is stripped so we need to do some shifting. */ |
| 1320 | for (i = 0;i < 4;i++) { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1321 | cmd->resp[i] = sdhci_readl(host, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1322 | SDHCI_RESPONSE + (3-i)*4) << 8; |
| 1323 | if (i != 3) |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1324 | cmd->resp[i] |= |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1325 | sdhci_readb(host, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1326 | SDHCI_RESPONSE + (3-i)*4-1); |
| 1327 | } |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1328 | MMC_TRACE(host->mmc, |
| 1329 | "%s: resp 0: 0x%08x resp 1: 0x%08x resp 2: 0x%08x resp 3: 0x%08x\n", |
| 1330 | __func__, cmd->resp[0], cmd->resp[1], |
| 1331 | cmd->resp[2], cmd->resp[3]); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1332 | } else { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1333 | cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE); |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 1334 | MMC_TRACE(host->mmc, "%s: resp 0: 0x%08x\n", |
| 1335 | __func__, cmd->resp[0]); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1339 | if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd) |
| 1340 | mmc_command_done(host->mmc, cmd->mrq); |
| 1341 | |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1342 | /* |
| 1343 | * The host can send and interrupt when the busy state has |
| 1344 | * ended, allowing us to wait without wasting CPU cycles. |
| 1345 | * The busy signal uses DAT0 so this is similar to waiting |
| 1346 | * for data to complete. |
| 1347 | * |
| 1348 | * Note: The 1.0 specification is a bit ambiguous about this |
| 1349 | * feature so there might be some problems with older |
| 1350 | * controllers. |
| 1351 | */ |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1352 | if (cmd->flags & MMC_RSP_BUSY) { |
| 1353 | if (cmd->data) { |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1354 | DBG("Cannot wait for busy signal when also doing a data transfer"); |
| 1355 | } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) && |
Adrian Hunter | ea96802 | 2016-06-29 16:24:24 +0300 | [diff] [blame] | 1356 | cmd == host->data_cmd) { |
| 1357 | /* Command complete before busy is ended */ |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1358 | return; |
| 1359 | } |
| 1360 | } |
| 1361 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1362 | /* Finished CMD23, now send actual command. */ |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1363 | if (cmd == cmd->mrq->sbc) { |
| 1364 | sdhci_send_command(host, cmd->mrq->cmd); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1365 | } else { |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1366 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1367 | /* Processed actual command. */ |
| 1368 | if (host->data && host->data_early) |
| 1369 | sdhci_finish_data(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1370 | |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1371 | if (!cmd->data) |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1372 | sdhci_finish_mrq(host, cmd->mrq); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1373 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1376 | static u16 sdhci_get_preset_value(struct sdhci_host *host) |
| 1377 | { |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1378 | u16 preset = 0; |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1379 | |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1380 | switch (host->timing) { |
| 1381 | case MMC_TIMING_UHS_SDR12: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1382 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12); |
| 1383 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1384 | case MMC_TIMING_UHS_SDR25: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1385 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25); |
| 1386 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1387 | case MMC_TIMING_UHS_SDR50: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1388 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50); |
| 1389 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1390 | case MMC_TIMING_UHS_SDR104: |
| 1391 | case MMC_TIMING_MMC_HS200: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1392 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104); |
| 1393 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1394 | case MMC_TIMING_UHS_DDR50: |
Jisheng Zhang | 0dafa60 | 2015-08-18 16:21:39 +0800 | [diff] [blame] | 1395 | case MMC_TIMING_MMC_DDR52: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1396 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50); |
| 1397 | break; |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1398 | case MMC_TIMING_MMC_HS400: |
| 1399 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400); |
| 1400 | break; |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1401 | default: |
| 1402 | pr_warn("%s: Invalid UHS-I mode selected\n", |
| 1403 | mmc_hostname(host->mmc)); |
| 1404 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12); |
| 1405 | break; |
| 1406 | } |
| 1407 | return preset; |
| 1408 | } |
| 1409 | |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1410 | u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock, |
| 1411 | unsigned int *actual_clock) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1412 | { |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1413 | int div = 0; /* Initialized for compiler warning */ |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1414 | int real_div = div, clk_mul = 1; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1415 | u16 clk = 0; |
ludovic.desroches@atmel.com | 5497159 | 2015-07-29 16:22:46 +0200 | [diff] [blame] | 1416 | bool switch_base_clk = false; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1417 | |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1418 | if (host->version >= SDHCI_SPEC_300) { |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 1419 | if (host->preset_enabled) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1420 | u16 pre_val; |
| 1421 | |
| 1422 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1423 | pre_val = sdhci_get_preset_value(host); |
| 1424 | div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK) |
| 1425 | >> SDHCI_PRESET_SDCLK_FREQ_SHIFT; |
| 1426 | if (host->clk_mul && |
| 1427 | (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) { |
| 1428 | clk = SDHCI_PROG_CLOCK_MODE; |
| 1429 | real_div = div + 1; |
| 1430 | clk_mul = host->clk_mul; |
| 1431 | } else { |
| 1432 | real_div = max_t(int, 1, div << 1); |
| 1433 | } |
| 1434 | goto clock_set; |
| 1435 | } |
| 1436 | |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1437 | /* |
| 1438 | * Check if the Host Controller supports Programmable Clock |
| 1439 | * Mode. |
| 1440 | */ |
| 1441 | if (host->clk_mul) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1442 | for (div = 1; div <= 1024; div++) { |
| 1443 | if ((host->max_clk * host->clk_mul / div) |
| 1444 | <= clock) |
| 1445 | break; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1446 | } |
ludovic.desroches@atmel.com | 5497159 | 2015-07-29 16:22:46 +0200 | [diff] [blame] | 1447 | if ((host->max_clk * host->clk_mul / div) <= clock) { |
| 1448 | /* |
| 1449 | * Set Programmable Clock Mode in the Clock |
| 1450 | * Control register. |
| 1451 | */ |
| 1452 | clk = SDHCI_PROG_CLOCK_MODE; |
| 1453 | real_div = div; |
| 1454 | clk_mul = host->clk_mul; |
| 1455 | div--; |
| 1456 | } else { |
| 1457 | /* |
| 1458 | * Divisor can be too small to reach clock |
| 1459 | * speed requirement. Then use the base clock. |
| 1460 | */ |
| 1461 | switch_base_clk = true; |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | if (!host->clk_mul || switch_base_clk) { |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1466 | /* Version 3.00 divisors must be a multiple of 2. */ |
| 1467 | if (host->max_clk <= clock) |
| 1468 | div = 1; |
| 1469 | else { |
| 1470 | for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; |
| 1471 | div += 2) { |
| 1472 | if ((host->max_clk / div) <= clock) |
| 1473 | break; |
| 1474 | } |
| 1475 | } |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1476 | real_div = div; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1477 | div >>= 1; |
Suneel Garapati | d1955c3 | 2015-06-09 13:01:50 +0530 | [diff] [blame] | 1478 | if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN) |
| 1479 | && !div && host->max_clk <= 25000000) |
| 1480 | div = 1; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1481 | } |
| 1482 | } else { |
| 1483 | /* Version 2.00 divisors must be a power of 2. */ |
Zhangfei Gao | 0397526 | 2010-09-20 15:15:18 -0400 | [diff] [blame] | 1484 | for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) { |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1485 | if ((host->max_clk / div) <= clock) |
| 1486 | break; |
| 1487 | } |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1488 | real_div = div; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1489 | div >>= 1; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1490 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1491 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1492 | clock_set: |
Aisheng Dong | 03d6f5f | 2014-08-27 15:26:32 +0800 | [diff] [blame] | 1493 | if (real_div) |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1494 | *actual_clock = (host->max_clk * clk_mul) / real_div; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1495 | |
| 1496 | if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) |
| 1497 | div = 0; |
| 1498 | |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1499 | clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1500 | clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) |
| 1501 | << SDHCI_DIVIDER_HI_SHIFT; |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1502 | |
| 1503 | return clk; |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(sdhci_calc_clk); |
| 1506 | |
| 1507 | void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 1508 | { |
| 1509 | u16 clk; |
| 1510 | unsigned long timeout; |
| 1511 | |
| 1512 | host->mmc->actual_clock = 0; |
| 1513 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1514 | if (host->clock) |
| 1515 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1516 | |
| 1517 | if (clock == 0) |
| 1518 | return; |
| 1519 | |
| 1520 | clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock); |
| 1521 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1522 | clk |= SDHCI_CLOCK_INT_EN; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1523 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1524 | |
Chris Ball | 27f6cb1 | 2009-09-22 16:45:31 -0700 | [diff] [blame] | 1525 | /* Wait max 20 ms */ |
Subhash Jadavani | ac6de6d | 2017-04-11 19:12:39 -0700 | [diff] [blame] | 1526 | timeout = 20; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1527 | while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1528 | & SDHCI_CLOCK_INT_STABLE)) { |
| 1529 | if (timeout == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 1530 | pr_err("%s: Internal clock never stabilised.\n", |
| 1531 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1532 | sdhci_dumpregs(host); |
| 1533 | return; |
| 1534 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1535 | timeout--; |
Adrian Hunter | b43ba21 | 2017-03-20 19:50:29 +0200 | [diff] [blame] | 1536 | usleep_range(900, 1100); |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1537 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1538 | |
| 1539 | clk |= SDHCI_CLOCK_CARD_EN; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1540 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1541 | } |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 1542 | EXPORT_SYMBOL_GPL(sdhci_set_clock); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1543 | |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1544 | static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode, |
| 1545 | unsigned short vdd) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1546 | { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 1547 | struct mmc_host *mmc = host->mmc; |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1548 | |
| 1549 | spin_unlock_irq(&host->lock); |
| 1550 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); |
| 1551 | spin_lock_irq(&host->lock); |
| 1552 | |
| 1553 | if (mode != MMC_POWER_OFF) |
| 1554 | sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL); |
| 1555 | else |
| 1556 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
| 1557 | } |
| 1558 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1559 | void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode, |
| 1560 | unsigned short vdd) |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1561 | { |
Giuseppe Cavallaro | 8364248 | 2010-09-28 10:41:28 +0200 | [diff] [blame] | 1562 | u8 pwr = 0; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1563 | |
Russell King | 24fbb3c | 2014-04-25 13:00:06 +0100 | [diff] [blame] | 1564 | if (mode != MMC_POWER_OFF) { |
| 1565 | switch (1 << vdd) { |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1566 | case MMC_VDD_165_195: |
| 1567 | pwr = SDHCI_POWER_180; |
| 1568 | break; |
| 1569 | case MMC_VDD_29_30: |
| 1570 | case MMC_VDD_30_31: |
| 1571 | pwr = SDHCI_POWER_300; |
| 1572 | break; |
| 1573 | case MMC_VDD_32_33: |
| 1574 | case MMC_VDD_33_34: |
| 1575 | pwr = SDHCI_POWER_330; |
| 1576 | break; |
| 1577 | default: |
Adrian Hunter | 9d5de93 | 2015-11-26 14:00:46 +0200 | [diff] [blame] | 1578 | WARN(1, "%s: Invalid vdd %#x\n", |
| 1579 | mmc_hostname(host->mmc), vdd); |
| 1580 | break; |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | if (host->pwr == pwr) |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1585 | return; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1586 | |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1587 | host->pwr = pwr; |
| 1588 | |
| 1589 | if (pwr == 0) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1590 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1591 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1592 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 1593 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 1594 | sdhci_runtime_pm_bus_off(host); |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1595 | } else { |
| 1596 | /* |
| 1597 | * Spec says that we should clear the power reg before setting |
| 1598 | * a new value. Some controllers don't seem to like this though. |
| 1599 | */ |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1600 | if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) { |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1601 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1602 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1603 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1604 | } |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1605 | /* |
| 1606 | * At least the Marvell CaFe chip gets confused if we set the |
| 1607 | * voltage and set turn on power at the same time, so set the |
| 1608 | * voltage first. |
| 1609 | */ |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1610 | if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) { |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1611 | sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1612 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1613 | host->ops->check_power_status(host, REQ_BUS_ON); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1614 | } |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1615 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1616 | pwr |= SDHCI_POWER_ON; |
| 1617 | |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1618 | sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1619 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1620 | host->ops->check_power_status(host, REQ_BUS_ON); |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1621 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1622 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 1623 | sdhci_runtime_pm_bus_on(host); |
Andres Salomon | e08c169 | 2008-07-04 10:00:03 -0700 | [diff] [blame] | 1624 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1625 | /* |
| 1626 | * Some controllers need an extra 10ms delay of 10ms before |
| 1627 | * they can apply clock after applying power |
| 1628 | */ |
| 1629 | if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER) |
| 1630 | mdelay(10); |
| 1631 | } |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1632 | } |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1633 | EXPORT_SYMBOL_GPL(sdhci_set_power_noreg); |
Jisheng Zhang | 918f4cb | 2015-12-11 21:36:29 +0800 | [diff] [blame] | 1634 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1635 | void sdhci_set_power(struct sdhci_host *host, unsigned char mode, |
| 1636 | unsigned short vdd) |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1637 | { |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1638 | if (IS_ERR(host->mmc->supply.vmmc)) |
| 1639 | sdhci_set_power_noreg(host, mode, vdd); |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1640 | else |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1641 | sdhci_set_power_reg(host, mode, vdd); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1642 | } |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1643 | EXPORT_SYMBOL_GPL(sdhci_set_power); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1644 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1645 | /*****************************************************************************\ |
| 1646 | * * |
| 1647 | * MMC callbacks * |
| 1648 | * * |
| 1649 | \*****************************************************************************/ |
| 1650 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1651 | static int sdhci_enable(struct mmc_host *mmc) |
| 1652 | { |
| 1653 | struct sdhci_host *host = mmc_priv(mmc); |
| 1654 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1655 | if (host->ops->platform_bus_voting) |
| 1656 | host->ops->platform_bus_voting(host, 1); |
| 1657 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | static int sdhci_disable(struct mmc_host *mmc) |
| 1662 | { |
| 1663 | struct sdhci_host *host = mmc_priv(mmc); |
| 1664 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1665 | if (host->ops->platform_bus_voting) |
| 1666 | host->ops->platform_bus_voting(host, 0); |
| 1667 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1668 | return 0; |
| 1669 | } |
| 1670 | |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1671 | static void sdhci_notify_halt(struct mmc_host *mmc, bool halt) |
| 1672 | { |
| 1673 | struct sdhci_host *host = mmc_priv(mmc); |
| 1674 | |
| 1675 | pr_debug("%s: halt notification was sent, halt=%d\n", |
| 1676 | mmc_hostname(mmc), halt); |
Subhash Jadavani | 2147ba9 | 2016-05-11 16:35:47 -0700 | [diff] [blame] | 1677 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1678 | if (halt) |
Subhash Jadavani | ce55294 | 2016-05-11 17:01:06 -0700 | [diff] [blame] | 1679 | host->desc_sz = 16; |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1680 | else |
Subhash Jadavani | ce55294 | 2016-05-11 17:01:06 -0700 | [diff] [blame] | 1681 | host->desc_sz = SDHCI_ADMA2_64_DESC_SZ; |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 1685 | static inline void sdhci_update_power_policy(struct sdhci_host *host, |
| 1686 | enum sdhci_power_policy policy) |
| 1687 | { |
| 1688 | host->power_policy = policy; |
| 1689 | } |
| 1690 | |
| 1691 | static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state) |
| 1692 | { |
| 1693 | int err = 0; |
| 1694 | struct sdhci_host *host = mmc_priv(mmc); |
| 1695 | |
| 1696 | switch (state) { |
| 1697 | case MMC_LOAD_HIGH: |
| 1698 | sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE); |
| 1699 | break; |
| 1700 | case MMC_LOAD_LOW: |
| 1701 | sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE); |
| 1702 | break; |
| 1703 | default: |
| 1704 | err = -EINVAL; |
| 1705 | break; |
| 1706 | } |
| 1707 | |
Sahitya Tummala | d300470 | 2015-08-06 13:58:47 +0530 | [diff] [blame] | 1708 | if (host->ops->notify_load) |
| 1709 | err = host->ops->notify_load(host, state); |
| 1710 | |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 1711 | return err; |
| 1712 | } |
| 1713 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1714 | static bool sdhci_check_state(struct sdhci_host *host) |
| 1715 | { |
| 1716 | if (!host->clock || !host->pwr) |
| 1717 | return true; |
| 1718 | else |
| 1719 | return false; |
| 1720 | } |
| 1721 | |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 1722 | static bool sdhci_check_auto_tuning(struct sdhci_host *host, |
| 1723 | struct mmc_command *cmd) |
| 1724 | { |
| 1725 | if (((cmd->opcode != MMC_READ_SINGLE_BLOCK) && |
| 1726 | (cmd->opcode != MMC_READ_MULTIPLE_BLOCK) && |
| 1727 | (cmd->opcode != SD_IO_RW_EXTENDED)) || (host->clock < 100000000)) |
| 1728 | return false; |
| 1729 | else if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1730 | host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
| 1731 | return true; |
| 1732 | else |
| 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | static int sdhci_get_tuning_cmd(struct sdhci_host *host) |
| 1737 | { |
| 1738 | if (!host->mmc || !host->mmc->card) |
| 1739 | return 0; |
| 1740 | /* |
| 1741 | * If we are here, all conditions have already been true |
| 1742 | * and the card can either be an eMMC or SD/SDIO |
| 1743 | */ |
| 1744 | if (mmc_card_mmc(host->mmc->card)) |
| 1745 | return MMC_SEND_TUNING_BLOCK_HS200; |
| 1746 | else |
| 1747 | return MMC_SEND_TUNING_BLOCK; |
| 1748 | } |
| 1749 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1750 | static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1751 | { |
| 1752 | struct sdhci_host *host; |
Shawn Guo | 505a868 | 2012-12-11 15:23:42 +0800 | [diff] [blame] | 1753 | int present; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1754 | unsigned long flags; |
| 1755 | |
| 1756 | host = mmc_priv(mmc); |
| 1757 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1758 | if (sdhci_check_state(host)) { |
| 1759 | sdhci_dump_state(host); |
| 1760 | WARN(1, "sdhci in bad state"); |
| 1761 | mrq->cmd->error = -EIO; |
| 1762 | if (mrq->data) |
| 1763 | mrq->data->error = -EIO; |
Venkat Gopalakrishnan | 987fc41 | 2015-09-01 15:52:29 -0700 | [diff] [blame] | 1764 | host->mrq = NULL; |
| 1765 | sdhci_dumpregs(host); |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 1766 | mmc_request_done(host->mmc, mrq); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1767 | return; |
| 1768 | } |
| 1769 | |
Sahitya Tummala | 68adcae | 2013-04-16 18:06:06 +0530 | [diff] [blame] | 1770 | /* |
| 1771 | * Firstly check card presence from cd-gpio. The return could |
| 1772 | * be one of the following possibilities: |
| 1773 | * negative: cd-gpio is not available |
| 1774 | * zero: cd-gpio is used, and card is removed |
| 1775 | * one: cd-gpio is used, and card is present |
| 1776 | */ |
Adrian Hunter | 8d28b7a | 2016-02-09 16:12:36 +0200 | [diff] [blame] | 1777 | present = mmc->ops->get_cd(mmc); |
Sahitya Tummala | 68adcae | 2013-04-16 18:06:06 +0530 | [diff] [blame] | 1778 | if (present < 0) { |
| 1779 | /* If polling, assume that the card is always present. */ |
| 1780 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 1781 | present = 1; |
| 1782 | else |
| 1783 | present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 1784 | SDHCI_CARD_PRESENT; |
| 1785 | } |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 1786 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1787 | spin_lock_irqsave(&host->lock, flags); |
| 1788 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 1789 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 1790 | sdhci_led_activate(host); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1791 | |
| 1792 | /* |
| 1793 | * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED |
| 1794 | * requests if Auto-CMD12 is enabled. |
| 1795 | */ |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1796 | if (sdhci_auto_cmd12(host, mrq)) { |
Jerry Huang | c4512f7 | 2010-08-10 18:01:59 -0700 | [diff] [blame] | 1797 | if (mrq->stop) { |
| 1798 | mrq->data->stop = NULL; |
| 1799 | mrq->stop = NULL; |
| 1800 | } |
| 1801 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1802 | |
Anton Vorontsov | 68d1fb7 | 2009-03-17 00:13:52 +0300 | [diff] [blame] | 1803 | if (!present || host->flags & SDHCI_DEVICE_DEAD) { |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1804 | mrq->cmd->error = -ENOMEDIUM; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1805 | sdhci_finish_mrq(host, mrq); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 1806 | } else { |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 1807 | if (host->ops->config_auto_tuning_cmd) { |
| 1808 | if (sdhci_check_auto_tuning(host, mrq->cmd)) |
| 1809 | host->ops->config_auto_tuning_cmd(host, true, |
| 1810 | sdhci_get_tuning_cmd(host)); |
| 1811 | else |
| 1812 | host->ops->config_auto_tuning_cmd(host, false, |
| 1813 | sdhci_get_tuning_cmd(host)); |
| 1814 | } |
| 1815 | |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1816 | if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1817 | sdhci_send_command(host, mrq->sbc); |
| 1818 | else |
| 1819 | sdhci_send_command(host, mrq->cmd); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 1820 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1821 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1822 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1823 | spin_unlock_irqrestore(&host->lock, flags); |
| 1824 | } |
| 1825 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 1826 | void sdhci_set_bus_width(struct sdhci_host *host, int width) |
| 1827 | { |
| 1828 | u8 ctrl; |
| 1829 | |
| 1830 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 1831 | if (width == MMC_BUS_WIDTH_8) { |
| 1832 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 1833 | if (host->version >= SDHCI_SPEC_300) |
| 1834 | ctrl |= SDHCI_CTRL_8BITBUS; |
| 1835 | } else { |
| 1836 | if (host->version >= SDHCI_SPEC_300) |
| 1837 | ctrl &= ~SDHCI_CTRL_8BITBUS; |
| 1838 | if (width == MMC_BUS_WIDTH_4) |
| 1839 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 1840 | else |
| 1841 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 1842 | } |
| 1843 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 1844 | } |
| 1845 | EXPORT_SYMBOL_GPL(sdhci_set_bus_width); |
| 1846 | |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 1847 | void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing) |
| 1848 | { |
| 1849 | u16 ctrl_2; |
| 1850 | |
| 1851 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 1852 | /* Select Bus Speed Mode for host */ |
| 1853 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
| 1854 | if ((timing == MMC_TIMING_MMC_HS200) || |
| 1855 | (timing == MMC_TIMING_UHS_SDR104)) |
| 1856 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 1857 | else if (timing == MMC_TIMING_UHS_SDR12) |
| 1858 | ctrl_2 |= SDHCI_CTRL_UHS_SDR12; |
| 1859 | else if (timing == MMC_TIMING_UHS_SDR25) |
| 1860 | ctrl_2 |= SDHCI_CTRL_UHS_SDR25; |
| 1861 | else if (timing == MMC_TIMING_UHS_SDR50) |
| 1862 | ctrl_2 |= SDHCI_CTRL_UHS_SDR50; |
| 1863 | else if ((timing == MMC_TIMING_UHS_DDR50) || |
| 1864 | (timing == MMC_TIMING_MMC_DDR52)) |
| 1865 | ctrl_2 |= SDHCI_CTRL_UHS_DDR50; |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1866 | else if (timing == MMC_TIMING_MMC_HS400) |
| 1867 | ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */ |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 1868 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 1869 | } |
| 1870 | EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling); |
| 1871 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 1872 | void sdhci_cfg_irq(struct sdhci_host *host, bool enable, bool sync) |
| 1873 | { |
| 1874 | if (enable && !(host->flags & SDHCI_HOST_IRQ_STATUS)) { |
| 1875 | enable_irq(host->irq); |
| 1876 | host->flags |= SDHCI_HOST_IRQ_STATUS; |
| 1877 | } else if (!enable && (host->flags & SDHCI_HOST_IRQ_STATUS)) { |
| 1878 | if (sync) |
| 1879 | disable_irq(host->irq); |
| 1880 | else |
| 1881 | disable_irq_nosync(host->irq); |
| 1882 | host->flags &= ~SDHCI_HOST_IRQ_STATUS; |
| 1883 | } |
| 1884 | } |
| 1885 | EXPORT_SYMBOL(sdhci_cfg_irq); |
| 1886 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 1887 | static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1888 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 1889 | struct sdhci_host *host = mmc_priv(mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1890 | unsigned long flags; |
| 1891 | u8 ctrl; |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1892 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1893 | |
Adrian Hunter | ceb6143 | 2011-12-27 15:48:41 +0200 | [diff] [blame] | 1894 | if (host->flags & SDHCI_DEVICE_DEAD) { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 1895 | if (!IS_ERR(mmc->supply.vmmc) && |
| 1896 | ios->power_mode == MMC_POWER_OFF) |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 1897 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
Adrian Hunter | ceb6143 | 2011-12-27 15:48:41 +0200 | [diff] [blame] | 1898 | return; |
| 1899 | } |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1900 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1901 | if (host->version >= SDHCI_SPEC_300 && |
Dong Aisheng | 372c463 | 2013-10-18 19:48:50 +0800 | [diff] [blame] | 1902 | (ios->power_mode == MMC_POWER_UP) && |
| 1903 | !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1904 | sdhci_enable_preset_value(host, false); |
| 1905 | |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1906 | spin_lock_irqsave(&host->lock, flags); |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 1907 | if (host->mmc && host->mmc->card && |
| 1908 | mmc_card_sdio(host->mmc->card)) |
| 1909 | sdhci_cfg_irq(host, false, false); |
| 1910 | |
Venkat Gopalakrishnan | f36166f | 2015-03-02 13:22:40 -0800 | [diff] [blame] | 1911 | if (ios->clock && |
| 1912 | ((ios->clock != host->clock) || (ios->timing != host->timing))) { |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1913 | spin_unlock_irqrestore(&host->lock, flags); |
| 1914 | host->ops->set_clock(host, ios->clock); |
| 1915 | spin_lock_irqsave(&host->lock, flags); |
| 1916 | host->clock = ios->clock; |
| 1917 | |
| 1918 | if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK && |
| 1919 | host->clock) { |
| 1920 | host->timeout_clk = host->mmc->actual_clock ? |
| 1921 | host->mmc->actual_clock / 1000 : |
| 1922 | host->clock / 1000; |
| 1923 | host->mmc->max_busy_timeout = |
| 1924 | host->ops->get_max_timeout_count ? |
| 1925 | host->ops->get_max_timeout_count(host) : |
| 1926 | 1 << 27; |
| 1927 | host->mmc->max_busy_timeout /= host->timeout_clk; |
| 1928 | } |
| 1929 | } |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 1930 | if (ios->clock && host->sdio_irq_async_status) |
| 1931 | sdhci_enable_sdio_irq_nolock(host, false); |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1932 | spin_unlock_irqrestore(&host->lock, flags); |
| 1933 | |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1934 | /* |
| 1935 | * The controller clocks may be off during power-up and we may end up |
| 1936 | * enabling card clock before giving power to the card. Hence, during |
| 1937 | * MMC_POWER_UP enable the controller clock and turn-on the regulators. |
| 1938 | * The mmc_power_up would provide the necessary delay before turning on |
| 1939 | * the clocks to the card. |
| 1940 | */ |
| 1941 | if (ios->power_mode & MMC_POWER_UP) { |
| 1942 | if (host->ops->enable_controller_clock) { |
| 1943 | ret = host->ops->enable_controller_clock(host); |
| 1944 | if (ret) { |
| 1945 | pr_err("%s: enabling controller clock: failed: %d\n", |
| 1946 | mmc_hostname(host->mmc), ret); |
| 1947 | } else { |
| 1948 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1953 | spin_lock_irqsave(&host->lock, flags); |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1954 | if (!host->clock) { |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 1955 | if (host->mmc && host->mmc->card && |
| 1956 | mmc_card_sdio(host->mmc->card)) |
| 1957 | sdhci_cfg_irq(host, true, false); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1958 | spin_unlock_irqrestore(&host->lock, flags); |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1959 | return; |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 1960 | } |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1961 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1962 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1963 | if (host->ops->set_power) |
| 1964 | host->ops->set_power(host, ios->power_mode, ios->vdd); |
| 1965 | else |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1966 | if (!host->ops->enable_controller_clock && (ios->power_mode & |
| 1967 | (MMC_POWER_UP | |
| 1968 | MMC_POWER_ON))) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1969 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1970 | |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1971 | spin_lock_irqsave(&host->lock, flags); |
| 1972 | |
Philip Rakity | 643a81f | 2010-09-23 08:24:32 -0700 | [diff] [blame] | 1973 | if (host->ops->platform_send_init_74_clocks) |
| 1974 | host->ops->platform_send_init_74_clocks(host, ios->power_mode); |
| 1975 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 1976 | host->ops->set_bus_width(host, ios->bus_width); |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 1977 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1978 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1979 | |
Philip Rakity | 3ab9c8d | 2010-10-06 11:57:23 -0700 | [diff] [blame] | 1980 | if ((ios->timing == MMC_TIMING_SD_HS || |
| 1981 | ios->timing == MMC_TIMING_MMC_HS) |
| 1982 | && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1983 | ctrl |= SDHCI_CTRL_HISPD; |
| 1984 | else |
| 1985 | ctrl &= ~SDHCI_CTRL_HISPD; |
| 1986 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1987 | if (host->version >= SDHCI_SPEC_300) { |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1988 | u16 clk, ctrl_2; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1989 | |
| 1990 | /* In case of UHS-I modes, set High Speed Enable */ |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1991 | if ((ios->timing == MMC_TIMING_MMC_HS400) || |
| 1992 | (ios->timing == MMC_TIMING_MMC_HS200) || |
Seungwon Jeon | bb8175a | 2014-03-14 21:12:48 +0900 | [diff] [blame] | 1993 | (ios->timing == MMC_TIMING_MMC_DDR52) || |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 1994 | (ios->timing == MMC_TIMING_UHS_SDR50) || |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1995 | (ios->timing == MMC_TIMING_UHS_SDR104) || |
| 1996 | (ios->timing == MMC_TIMING_UHS_DDR50) || |
Alexander Elbs | dd8df17 | 2012-01-03 23:26:53 -0500 | [diff] [blame] | 1997 | (ios->timing == MMC_TIMING_UHS_SDR25)) |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1998 | ctrl |= SDHCI_CTRL_HISPD; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1999 | |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2000 | if (!host->preset_enabled) { |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 2001 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2002 | /* |
| 2003 | * We only need to set Driver Strength if the |
| 2004 | * preset value enable is not set. |
| 2005 | */ |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2006 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2007 | ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK; |
| 2008 | if (ios->drv_type == MMC_SET_DRIVER_TYPE_A) |
| 2009 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A; |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 2010 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B) |
| 2011 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2012 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C) |
| 2013 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C; |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 2014 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D) |
| 2015 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D; |
| 2016 | else { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2017 | pr_warn("%s: invalid driver type, default to driver type B\n", |
| 2018 | mmc_hostname(mmc)); |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 2019 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B; |
| 2020 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2021 | |
| 2022 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 2023 | } else { |
| 2024 | /* |
| 2025 | * According to SDHC Spec v3.00, if the Preset Value |
| 2026 | * Enable in the Host Control 2 register is set, we |
| 2027 | * need to reset SD Clock Enable before changing High |
| 2028 | * Speed Enable to avoid generating clock gliches. |
| 2029 | */ |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 2030 | |
| 2031 | /* Reset SD Clock Enable */ |
| 2032 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 2033 | clk &= ~SDHCI_CLOCK_CARD_EN; |
| 2034 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 2035 | |
| 2036 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 2037 | |
| 2038 | /* Re-enable SD Clock */ |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2039 | if (ios->clock) { |
| 2040 | spin_unlock_irqrestore(&host->lock, flags); |
Sujit Reddy Thumma | fbe7d86 | 2014-01-21 17:22:05 +0530 | [diff] [blame] | 2041 | host->ops->set_clock(host, host->clock); |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2042 | spin_lock_irqsave(&host->lock, flags); |
| 2043 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2044 | } |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2045 | |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2046 | /* Reset SD Clock Enable */ |
| 2047 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 2048 | clk &= ~SDHCI_CLOCK_CARD_EN; |
| 2049 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 2050 | |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 2051 | host->ops->set_uhs_signaling(host, ios->timing); |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 2052 | host->timing = ios->timing; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2053 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2054 | if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) && |
| 2055 | ((ios->timing == MMC_TIMING_UHS_SDR12) || |
| 2056 | (ios->timing == MMC_TIMING_UHS_SDR25) || |
| 2057 | (ios->timing == MMC_TIMING_UHS_SDR50) || |
| 2058 | (ios->timing == MMC_TIMING_UHS_SDR104) || |
Jisheng Zhang | 0dafa60 | 2015-08-18 16:21:39 +0800 | [diff] [blame] | 2059 | (ios->timing == MMC_TIMING_UHS_DDR50) || |
| 2060 | (ios->timing == MMC_TIMING_MMC_DDR52))) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2061 | u16 preset; |
| 2062 | |
| 2063 | sdhci_enable_preset_value(host, true); |
| 2064 | preset = sdhci_get_preset_value(host); |
| 2065 | ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK) |
| 2066 | >> SDHCI_PRESET_DRV_SHIFT; |
| 2067 | } |
| 2068 | |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2069 | /* Re-enable SD Clock */ |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2070 | if (ios->clock) { |
| 2071 | spin_unlock_irqrestore(&host->lock, flags); |
Sujit Reddy Thumma | fbe7d86 | 2014-01-21 17:22:05 +0530 | [diff] [blame] | 2072 | host->ops->set_clock(host, host->clock); |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2073 | spin_lock_irqsave(&host->lock, flags); |
| 2074 | } |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 2075 | } else |
| 2076 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2077 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2078 | spin_unlock_irqrestore(&host->lock, flags); |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 2079 | /* |
| 2080 | * Some (ENE) controllers go apeshit on some ios operation, |
| 2081 | * signalling timeout and CRC errors even on CMD0. Resetting |
| 2082 | * it on each ios seems to solve the problem. |
| 2083 | */ |
Mohammad Jamal | c63705e | 2015-01-13 20:47:24 +0530 | [diff] [blame] | 2084 | if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 2085 | sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 2086 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2087 | /* |
| 2088 | * Reset the chip on each power off. |
| 2089 | * Should clear out any weird states. |
| 2090 | */ |
| 2091 | if (ios->power_mode == MMC_POWER_OFF) { |
| 2092 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
| 2093 | sdhci_reinit(host); |
| 2094 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
| 2095 | } |
| 2096 | if (!ios->clock) |
Venkat Gopalakrishnan | 766b745 | 2015-03-10 15:51:23 -0700 | [diff] [blame] | 2097 | host->ops->set_clock(host, ios->clock); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2098 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 2099 | spin_lock_irqsave(&host->lock, flags); |
| 2100 | if (host->mmc && host->mmc->card && |
| 2101 | mmc_card_sdio(host->mmc->card)) |
| 2102 | sdhci_cfg_irq(host, true, false); |
| 2103 | spin_unlock_irqrestore(&host->lock, flags); |
| 2104 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2105 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2108 | static int sdhci_get_cd(struct mmc_host *mmc) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2109 | { |
| 2110 | struct sdhci_host *host = mmc_priv(mmc); |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2111 | int gpio_cd = mmc_gpio_get_cd(mmc); |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2112 | |
| 2113 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 2114 | return 0; |
| 2115 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2116 | /* If nonremovable, assume that the card is always present. */ |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 2117 | if (!mmc_card_is_removable(host->mmc)) |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2118 | return 1; |
| 2119 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2120 | /* |
| 2121 | * Try slot gpio detect, if defined it take precedence |
| 2122 | * over build in controller functionality |
| 2123 | */ |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 2124 | if (gpio_cd >= 0) |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2125 | return !!gpio_cd; |
| 2126 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2127 | /* If polling, assume that the card is always present. */ |
| 2128 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 2129 | return 1; |
| 2130 | |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2131 | /* Host native card detect */ |
| 2132 | return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); |
| 2133 | } |
| 2134 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2135 | static int sdhci_check_ro(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2136 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2137 | unsigned long flags; |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2138 | int is_readonly; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2139 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2140 | spin_lock_irqsave(&host->lock, flags); |
| 2141 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 2142 | if (host->flags & SDHCI_DEVICE_DEAD) |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2143 | is_readonly = 0; |
| 2144 | else if (host->ops->get_ro) |
| 2145 | is_readonly = host->ops->get_ro(host); |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 2146 | else |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2147 | is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE) |
| 2148 | & SDHCI_WRITE_PROTECT); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2149 | |
| 2150 | spin_unlock_irqrestore(&host->lock, flags); |
| 2151 | |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2152 | /* This quirk needs to be replaced by a callback-function later */ |
| 2153 | return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ? |
| 2154 | !is_readonly : is_readonly; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2155 | } |
| 2156 | |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2157 | #define SAMPLE_COUNT 5 |
| 2158 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2159 | static int sdhci_get_ro(struct mmc_host *mmc) |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2160 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2161 | struct sdhci_host *host = mmc_priv(mmc); |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2162 | int i, ro_count; |
| 2163 | |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2164 | if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2165 | return sdhci_check_ro(host); |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2166 | |
| 2167 | ro_count = 0; |
| 2168 | for (i = 0; i < SAMPLE_COUNT; i++) { |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2169 | if (sdhci_check_ro(host)) { |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2170 | if (++ro_count > SAMPLE_COUNT / 2) |
| 2171 | return 1; |
| 2172 | } |
| 2173 | msleep(30); |
| 2174 | } |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
Adrian Hunter | 20758b6 | 2011-08-29 16:42:12 +0300 | [diff] [blame] | 2178 | static void sdhci_hw_reset(struct mmc_host *mmc) |
| 2179 | { |
| 2180 | struct sdhci_host *host = mmc_priv(mmc); |
| 2181 | |
| 2182 | if (host->ops && host->ops->hw_reset) |
| 2183 | host->ops->hw_reset(host); |
| 2184 | } |
| 2185 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2186 | static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable) |
| 2187 | { |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 2188 | u16 ctrl = 0; |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2189 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 2190 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 2191 | return; |
| 2192 | |
| 2193 | if (mmc_card_and_host_support_async_int(host->mmc)) { |
| 2194 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2195 | if (enable) |
| 2196 | ctrl |= SDHCI_CTRL_ASYNC_INT_ENABLE; |
| 2197 | else |
| 2198 | ctrl &= ~SDHCI_CTRL_ASYNC_INT_ENABLE; |
| 2199 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2200 | } |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 2201 | |
| 2202 | if (enable) |
| 2203 | host->ier |= SDHCI_INT_CARD_INT; |
| 2204 | else |
| 2205 | host->ier &= ~SDHCI_INT_CARD_INT; |
| 2206 | |
| 2207 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2208 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 2209 | mmiowb(); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2210 | } |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2211 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2212 | static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 2213 | { |
| 2214 | struct sdhci_host *host = mmc_priv(mmc); |
| 2215 | unsigned long flags; |
| 2216 | |
Hans de Goede | fa3b4f4 | 2017-03-26 13:14:45 +0200 | [diff] [blame] | 2217 | if (enable) |
| 2218 | pm_runtime_get_noresume(host->mmc->parent); |
| 2219 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2220 | spin_lock_irqsave(&host->lock, flags); |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2221 | if (enable) |
| 2222 | host->flags |= SDHCI_SDIO_IRQ_ENABLED; |
| 2223 | else |
| 2224 | host->flags &= ~SDHCI_SDIO_IRQ_ENABLED; |
| 2225 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2226 | sdhci_enable_sdio_irq_nolock(host, enable); |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2227 | spin_unlock_irqrestore(&host->lock, flags); |
Hans de Goede | fa3b4f4 | 2017-03-26 13:14:45 +0200 | [diff] [blame] | 2228 | |
| 2229 | if (!enable) |
| 2230 | pm_runtime_put_noidle(host->mmc->parent); |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2231 | } |
| 2232 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2233 | static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc, |
| 2234 | struct mmc_ios *ios) |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2235 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2236 | struct sdhci_host *host = mmc_priv(mmc); |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2237 | u16 ctrl; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2238 | int ret; |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2239 | |
| 2240 | /* |
| 2241 | * Signal Voltage Switching is only applicable for Host Controllers |
| 2242 | * v3.00 and above. |
| 2243 | */ |
| 2244 | if (host->version < SDHCI_SPEC_300) |
| 2245 | return 0; |
| 2246 | |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2247 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2248 | |
Fabio Estevam | 21f5998 | 2013-02-14 10:35:03 -0200 | [diff] [blame] | 2249 | switch (ios->signal_voltage) { |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2250 | case MMC_SIGNAL_VOLTAGE_330: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2251 | if (!(host->flags & SDHCI_SIGNALING_330)) |
| 2252 | return -EINVAL; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2253 | /* Set 1.8V Signal Enable in the Host Control2 register to 0 */ |
| 2254 | ctrl &= ~SDHCI_CTRL_VDD_180; |
| 2255 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 2256 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2257 | host->ops->check_power_status(host, REQ_IO_HIGH); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2258 | |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2259 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2260 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2261 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2262 | pr_warn("%s: Switching to 3.3V signalling voltage failed\n", |
| 2263 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2264 | return -EIO; |
| 2265 | } |
| 2266 | } |
| 2267 | /* Wait for 5ms */ |
| 2268 | usleep_range(5000, 5500); |
| 2269 | |
| 2270 | /* 3.3V regulator output should be stable within 5 ms */ |
| 2271 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2272 | if (!(ctrl & SDHCI_CTRL_VDD_180)) |
| 2273 | return 0; |
| 2274 | |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2275 | pr_warn("%s: 3.3V regulator output did not became stable\n", |
| 2276 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2277 | |
| 2278 | return -EAGAIN; |
| 2279 | case MMC_SIGNAL_VOLTAGE_180: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2280 | if (!(host->flags & SDHCI_SIGNALING_180)) |
| 2281 | return -EINVAL; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2282 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2283 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2284 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2285 | pr_warn("%s: Switching to 1.8V signalling voltage failed\n", |
| 2286 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2287 | return -EIO; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Enable 1.8V Signal Enable in the Host Control2 |
| 2293 | * register |
| 2294 | */ |
| 2295 | ctrl |= SDHCI_CTRL_VDD_180; |
| 2296 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 2297 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2298 | host->ops->check_power_status(host, REQ_IO_LOW); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2299 | |
Vincent Yang | 9d967a6 | 2015-01-20 16:05:15 +0800 | [diff] [blame] | 2300 | /* Some controller need to do more when switching */ |
| 2301 | if (host->ops->voltage_switch) |
| 2302 | host->ops->voltage_switch(host); |
| 2303 | |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2304 | /* 1.8V regulator output should be stable within 5 ms */ |
| 2305 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2306 | if (ctrl & SDHCI_CTRL_VDD_180) |
| 2307 | return 0; |
| 2308 | |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2309 | pr_warn("%s: 1.8V regulator output did not became stable\n", |
| 2310 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2311 | |
| 2312 | return -EAGAIN; |
| 2313 | case MMC_SIGNAL_VOLTAGE_120: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2314 | if (!(host->flags & SDHCI_SIGNALING_120)) |
| 2315 | return -EINVAL; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2316 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2317 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2318 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2319 | pr_warn("%s: Switching to 1.2V signalling voltage failed\n", |
| 2320 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2321 | return -EIO; |
| 2322 | } |
| 2323 | } |
| 2324 | return 0; |
| 2325 | default: |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 2326 | /* No signal voltage switch required */ |
| 2327 | return 0; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2328 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 2329 | } |
| 2330 | |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2331 | static int sdhci_card_busy(struct mmc_host *mmc) |
| 2332 | { |
| 2333 | struct sdhci_host *host = mmc_priv(mmc); |
| 2334 | u32 present_state; |
| 2335 | |
Adrian Hunter | e613cc4 | 2016-06-23 14:00:58 +0300 | [diff] [blame] | 2336 | /* Check whether DAT[0] is 0 */ |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2337 | present_state = sdhci_readl(host, SDHCI_PRESENT_STATE); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2338 | |
Adrian Hunter | e613cc4 | 2016-06-23 14:00:58 +0300 | [diff] [blame] | 2339 | return !(present_state & SDHCI_DATA_0_LVL_MASK); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2340 | } |
| 2341 | |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2342 | static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) |
| 2343 | { |
| 2344 | struct sdhci_host *host = mmc_priv(mmc); |
| 2345 | unsigned long flags; |
| 2346 | |
| 2347 | spin_lock_irqsave(&host->lock, flags); |
| 2348 | host->flags |= SDHCI_HS400_TUNING; |
| 2349 | spin_unlock_irqrestore(&host->lock, flags); |
| 2350 | |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2354 | static int sdhci_enhanced_strobe(struct mmc_host *mmc) |
| 2355 | { |
| 2356 | struct sdhci_host *host = mmc_priv(mmc); |
| 2357 | int err = 0; |
| 2358 | |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2359 | if (host->ops->enhanced_strobe) |
| 2360 | err = host->ops->enhanced_strobe(host); |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2361 | |
| 2362 | return err; |
| 2363 | } |
| 2364 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2365 | static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode) |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2366 | { |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2367 | struct sdhci_host *host = mmc_priv(mmc); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2368 | u16 ctrl; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2369 | int tuning_loop_counter = MAX_TUNING_LOOP; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2370 | int err = 0; |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2371 | unsigned long flags; |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2372 | unsigned int tuning_count = 0; |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2373 | bool hs400_tuning; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2374 | |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2375 | spin_lock_irqsave(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2376 | |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2377 | hs400_tuning = host->flags & SDHCI_HS400_TUNING; |
| 2378 | host->flags &= ~SDHCI_HS400_TUNING; |
| 2379 | |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2380 | if (host->tuning_mode == SDHCI_TUNING_MODE_1) |
| 2381 | tuning_count = host->tuning_count; |
| 2382 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2383 | /* |
Weijun Yang | 9faac7b | 2015-10-04 12:04:12 +0000 | [diff] [blame] | 2384 | * The Host Controller needs tuning in case of SDR104 and DDR50 |
| 2385 | * mode, and for SDR50 mode when Use Tuning for SDR50 is set in |
| 2386 | * the Capabilities register. |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2387 | * If the Host Controller supports the HS200 mode then the |
| 2388 | * tuning function has to be executed. |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2389 | */ |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2390 | switch (host->timing) { |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2391 | /* HS400 tuning is done in HS200 mode */ |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 2392 | case MMC_TIMING_MMC_HS400: |
Sayali Lokhande | 27d29e2 | 2016-07-21 05:58:43 -0700 | [diff] [blame] | 2393 | if (!(mmc->caps2 & MMC_CAP2_HS400_POST_TUNING)) { |
| 2394 | err = -EINVAL; |
| 2395 | goto out_unlock; |
| 2396 | } |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2397 | case MMC_TIMING_MMC_HS200: |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2398 | /* |
| 2399 | * Periodic re-tuning for HS400 is not expected to be needed, so |
| 2400 | * disable it here. |
| 2401 | */ |
| 2402 | if (hs400_tuning) |
| 2403 | tuning_count = 0; |
| 2404 | break; |
| 2405 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2406 | case MMC_TIMING_UHS_SDR104: |
Weijun Yang | 9faac7b | 2015-10-04 12:04:12 +0000 | [diff] [blame] | 2407 | case MMC_TIMING_UHS_DDR50: |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2408 | break; |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2409 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2410 | case MMC_TIMING_UHS_SDR50: |
Adrian Hunter | 4228b21 | 2016-04-20 09:24:03 +0300 | [diff] [blame] | 2411 | if (host->flags & SDHCI_SDR50_NEEDS_TUNING) |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2412 | break; |
| 2413 | /* FALLTHROUGH */ |
| 2414 | |
| 2415 | default: |
Adrian Hunter | d519c86 | 2014-12-05 19:25:29 +0200 | [diff] [blame] | 2416 | goto out_unlock; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2417 | } |
| 2418 | |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2419 | if (host->ops->platform_execute_tuning) { |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2420 | spin_unlock_irqrestore(&host->lock, flags); |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2421 | err = host->ops->platform_execute_tuning(host, opcode); |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2422 | return err; |
| 2423 | } |
| 2424 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2425 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2426 | ctrl |= SDHCI_CTRL_EXEC_TUNING; |
Vincent Yang | 67d0d04 | 2015-01-20 16:05:16 +0800 | [diff] [blame] | 2427 | if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND) |
| 2428 | ctrl |= SDHCI_CTRL_TUNED_CLK; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2429 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 2430 | |
| 2431 | /* |
| 2432 | * As per the Host Controller spec v3.00, tuning command |
| 2433 | * generates Buffer Read Ready interrupt, so enable that. |
| 2434 | * |
| 2435 | * Note: The spec clearly says that when tuning sequence |
| 2436 | * is being performed, the controller does not generate |
| 2437 | * interrupts other than Buffer Read Ready interrupt. But |
| 2438 | * to make sure we don't hit a controller bug, we _only_ |
| 2439 | * enable Buffer Read Ready interrupt here. |
| 2440 | */ |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2441 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE); |
| 2442 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2443 | |
| 2444 | /* |
| 2445 | * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number |
Simon Horman | 1473bdd | 2016-05-13 13:24:31 +0900 | [diff] [blame] | 2446 | * of loops reaches 40 times. |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2447 | */ |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2448 | do { |
| 2449 | struct mmc_command cmd = {0}; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2450 | struct mmc_request mrq = {NULL}; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2451 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2452 | cmd.opcode = opcode; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2453 | cmd.arg = 0; |
| 2454 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2455 | cmd.retries = 0; |
| 2456 | cmd.data = NULL; |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2457 | cmd.mrq = &mrq; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2458 | cmd.error = 0; |
| 2459 | |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2460 | if (tuning_loop_counter-- == 0) |
| 2461 | break; |
| 2462 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2463 | mrq.cmd = &cmd; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2464 | |
| 2465 | /* |
| 2466 | * In response to CMD19, the card sends 64 bytes of tuning |
| 2467 | * block to the Host Controller. So we set the block size |
| 2468 | * to 64 here. |
| 2469 | */ |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2470 | if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) { |
| 2471 | if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2472 | sdhci_set_blk_size_reg(host, 128, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2473 | else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2474 | sdhci_set_blk_size_reg(host, 64, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2475 | } else { |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2476 | sdhci_set_blk_size_reg(host, 64, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2477 | } |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2478 | |
| 2479 | /* |
| 2480 | * The tuning block is sent by the card to the host controller. |
| 2481 | * So we set the TRNS_READ bit in the Transfer Mode register. |
| 2482 | * This also takes care of setting DMA Enable and Multi Block |
| 2483 | * Select in the same register to 0. |
| 2484 | */ |
| 2485 | sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); |
| 2486 | |
| 2487 | sdhci_send_command(host, &cmd); |
| 2488 | |
| 2489 | host->cmd = NULL; |
Adrian Hunter | 07c161b | 2016-06-29 16:24:38 +0300 | [diff] [blame] | 2490 | sdhci_del_timer(host, &mrq); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2491 | |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2492 | spin_unlock_irqrestore(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2493 | /* Wait for Buffer Read Ready interrupt */ |
Christopher Freeman | 622b5f3 | 2016-08-17 13:34:27 -0400 | [diff] [blame] | 2494 | wait_event_timeout(host->buf_ready_int, |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2495 | (host->tuning_done == 1), |
| 2496 | msecs_to_jiffies(50)); |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2497 | spin_lock_irqsave(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2498 | |
| 2499 | if (!host->tuning_done) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2500 | pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n"); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2501 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2502 | ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 2503 | ctrl &= ~SDHCI_CTRL_EXEC_TUNING; |
| 2504 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 2505 | |
Adrian Hunter | cee9358 | 2016-12-02 15:14:20 +0200 | [diff] [blame] | 2506 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2507 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
| 2508 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2509 | err = -EIO; |
Adrian Hunter | cee9358 | 2016-12-02 15:14:20 +0200 | [diff] [blame] | 2510 | |
| 2511 | if (cmd.opcode != MMC_SEND_TUNING_BLOCK_HS200) |
| 2512 | goto out; |
| 2513 | |
| 2514 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2515 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 2516 | |
| 2517 | spin_unlock_irqrestore(&host->lock, flags); |
| 2518 | |
| 2519 | memset(&cmd, 0, sizeof(cmd)); |
| 2520 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 2521 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 2522 | cmd.busy_timeout = 50; |
| 2523 | mmc_wait_for_cmd(mmc, &cmd, 0); |
| 2524 | |
| 2525 | spin_lock_irqsave(&host->lock, flags); |
| 2526 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2527 | goto out; |
| 2528 | } |
| 2529 | |
| 2530 | host->tuning_done = 0; |
| 2531 | |
| 2532 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Nick Sanders | 197160d | 2014-05-06 18:52:38 -0700 | [diff] [blame] | 2533 | |
| 2534 | /* eMMC spec does not require a delay between tuning cycles */ |
| 2535 | if (opcode == MMC_SEND_TUNING_BLOCK) |
| 2536 | mdelay(1); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2537 | } while (ctrl & SDHCI_CTRL_EXEC_TUNING); |
| 2538 | |
| 2539 | /* |
| 2540 | * The Host Driver has exhausted the maximum number of loops allowed, |
| 2541 | * so use fixed sampling frequency. |
| 2542 | */ |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2543 | if (tuning_loop_counter < 0) { |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2544 | ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 2545 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2546 | } |
| 2547 | if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2548 | pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n"); |
Dong Aisheng | 114f2bf | 2013-10-18 19:48:45 +0800 | [diff] [blame] | 2549 | err = -EIO; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | out: |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2553 | if (tuning_count) { |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 2554 | /* |
| 2555 | * In case tuning fails, host controllers which support |
| 2556 | * re-tuning can try tuning again at a later time, when the |
| 2557 | * re-tuning timer expires. So for these controllers, we |
| 2558 | * return 0. Since there might be other controllers who do not |
| 2559 | * have this capability, we return error for them. |
| 2560 | */ |
| 2561 | err = 0; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 2562 | } |
| 2563 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 2564 | host->mmc->retune_period = err ? 0 : tuning_count; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 2565 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2566 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2567 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Adrian Hunter | d519c86 | 2014-12-05 19:25:29 +0200 | [diff] [blame] | 2568 | out_unlock: |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2569 | spin_unlock_irqrestore(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2570 | return err; |
| 2571 | } |
| 2572 | |
Adrian Hunter | cb84964 | 2015-02-06 14:12:59 +0200 | [diff] [blame] | 2573 | static int sdhci_select_drive_strength(struct mmc_card *card, |
| 2574 | unsigned int max_dtr, int host_drv, |
| 2575 | int card_drv, int *drv_type) |
| 2576 | { |
| 2577 | struct sdhci_host *host = mmc_priv(card->host); |
| 2578 | |
| 2579 | if (!host->ops->select_drive_strength) |
| 2580 | return 0; |
| 2581 | |
| 2582 | return host->ops->select_drive_strength(host, card, max_dtr, host_drv, |
| 2583 | card_drv, drv_type); |
| 2584 | } |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2585 | |
| 2586 | static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable) |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2587 | { |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2588 | /* Host Controller v3.00 defines preset value registers */ |
| 2589 | if (host->version < SDHCI_SPEC_300) |
| 2590 | return; |
| 2591 | |
Sahitya Tummala | 314162c | 2013-04-12 12:11:20 +0530 | [diff] [blame] | 2592 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE) |
| 2593 | return; |
| 2594 | |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2595 | /* |
| 2596 | * We only enable or disable Preset Value if they are not already |
| 2597 | * enabled or disabled respectively. Otherwise, we bail out. |
| 2598 | */ |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2599 | if (host->preset_enabled != enable) { |
| 2600 | u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2601 | |
| 2602 | if (enable) |
| 2603 | ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE; |
| 2604 | else |
| 2605 | ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE; |
| 2606 | |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2607 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2608 | |
| 2609 | if (enable) |
| 2610 | host->flags |= SDHCI_PV_ENABLED; |
| 2611 | else |
| 2612 | host->flags &= ~SDHCI_PV_ENABLED; |
| 2613 | |
| 2614 | host->preset_enabled = enable; |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2615 | } |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2616 | } |
| 2617 | |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2618 | static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 2619 | int err) |
| 2620 | { |
| 2621 | struct sdhci_host *host = mmc_priv(mmc); |
| 2622 | struct mmc_data *data = mrq->data; |
| 2623 | |
Russell King | f48f039 | 2016-01-26 13:40:32 +0000 | [diff] [blame] | 2624 | if (data->host_cookie != COOKIE_UNMAPPED) |
Russell King | 771a3dc | 2016-01-26 13:40:53 +0000 | [diff] [blame] | 2625 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 2626 | data->flags & MMC_DATA_WRITE ? |
| 2627 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 2628 | |
| 2629 | data->host_cookie = COOKIE_UNMAPPED; |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2630 | |
| 2631 | if (host->ops->pre_req) |
| 2632 | host->ops->pre_req(host, mrq); |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2633 | } |
| 2634 | |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2635 | static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 2636 | bool is_first_req) |
| 2637 | { |
| 2638 | struct sdhci_host *host = mmc_priv(mmc); |
| 2639 | |
Haibo Chen | d31911b | 2015-08-25 10:02:11 +0800 | [diff] [blame] | 2640 | mrq->data->host_cookie = COOKIE_UNMAPPED; |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2641 | |
| 2642 | if (host->flags & SDHCI_REQ_USE_DMA) |
Russell King | 94538e5 | 2016-01-26 13:40:37 +0000 | [diff] [blame] | 2643 | sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED); |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2644 | } |
| 2645 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2646 | static inline bool sdhci_has_requests(struct sdhci_host *host) |
| 2647 | { |
| 2648 | return host->cmd || host->data_cmd; |
| 2649 | } |
| 2650 | |
| 2651 | static void sdhci_error_out_mrqs(struct sdhci_host *host, int err) |
| 2652 | { |
| 2653 | if (host->data_cmd) { |
| 2654 | host->data_cmd->error = err; |
| 2655 | sdhci_finish_mrq(host, host->data_cmd->mrq); |
| 2656 | } |
| 2657 | |
| 2658 | if (host->cmd) { |
| 2659 | host->cmd->error = err; |
| 2660 | sdhci_finish_mrq(host, host->cmd->mrq); |
| 2661 | } |
| 2662 | } |
| 2663 | |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2664 | static void sdhci_card_event(struct mmc_host *mmc) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2665 | { |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2666 | struct sdhci_host *host = mmc_priv(mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2667 | unsigned long flags; |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 2668 | int present; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2669 | |
Christian Daudt | 722e128 | 2013-06-20 14:26:36 -0700 | [diff] [blame] | 2670 | /* First check if client has provided their own card event */ |
| 2671 | if (host->ops->card_event) |
| 2672 | host->ops->card_event(host); |
| 2673 | |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 2674 | present = mmc->ops->get_cd(mmc); |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 2675 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2676 | spin_lock_irqsave(&host->lock, flags); |
| 2677 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2678 | /* Check sdhci_has_requests() first in case we are runtime suspended */ |
| 2679 | if (sdhci_has_requests(host) && !present) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2680 | pr_err("%s: Card removed during transfer!\n", |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2681 | mmc_hostname(host->mmc)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2682 | pr_err("%s: Resetting controller.\n", |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2683 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2684 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 2685 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2686 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2687 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2688 | sdhci_error_out_mrqs(host, -ENOMEDIUM); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | spin_unlock_irqrestore(&host->lock, flags); |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2694 | static int sdhci_late_init(struct mmc_host *mmc) |
| 2695 | { |
| 2696 | struct sdhci_host *host = mmc_priv(mmc); |
| 2697 | |
| 2698 | if (host->ops->init) |
| 2699 | host->ops->init(host); |
| 2700 | |
| 2701 | return 0; |
| 2702 | } |
Venkat Gopalakrishnan | 28cfcee | 2015-09-15 15:57:35 -0700 | [diff] [blame] | 2703 | |
| 2704 | static void sdhci_force_err_irq(struct mmc_host *mmc, u64 errmask) |
| 2705 | { |
| 2706 | struct sdhci_host *host = mmc_priv(mmc); |
| 2707 | u16 mask = errmask & 0xFFFF; |
| 2708 | |
| 2709 | pr_err("%s: Force raise error mask:0x%04x\n", __func__, mask); |
Venkat Gopalakrishnan | 28cfcee | 2015-09-15 15:57:35 -0700 | [diff] [blame] | 2710 | sdhci_writew(host, mask, SDHCI_SET_INT_ERROR); |
Venkat Gopalakrishnan | 28cfcee | 2015-09-15 15:57:35 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2713 | static const struct mmc_host_ops sdhci_ops = { |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2714 | .init = sdhci_late_init, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2715 | .request = sdhci_request, |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2716 | .post_req = sdhci_post_req, |
| 2717 | .pre_req = sdhci_pre_req, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2718 | .set_ios = sdhci_set_ios, |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2719 | .get_cd = sdhci_get_cd, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2720 | .get_ro = sdhci_get_ro, |
| 2721 | .hw_reset = sdhci_hw_reset, |
| 2722 | .enable_sdio_irq = sdhci_enable_sdio_irq, |
| 2723 | .start_signal_voltage_switch = sdhci_start_signal_voltage_switch, |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2724 | .prepare_hs400_tuning = sdhci_prepare_hs400_tuning, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2725 | .execute_tuning = sdhci_execute_tuning, |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2726 | .enhanced_strobe = sdhci_enhanced_strobe, |
Adrian Hunter | cb84964 | 2015-02-06 14:12:59 +0200 | [diff] [blame] | 2727 | .select_drive_strength = sdhci_select_drive_strength, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2728 | .card_event = sdhci_card_event, |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2729 | .card_busy = sdhci_card_busy, |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 2730 | .enable = sdhci_enable, |
| 2731 | .disable = sdhci_disable, |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 2732 | .notify_load = sdhci_notify_load, |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 2733 | .notify_halt = sdhci_notify_halt, |
Venkat Gopalakrishnan | 28cfcee | 2015-09-15 15:57:35 -0700 | [diff] [blame] | 2734 | .force_err_irq = sdhci_force_err_irq, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2735 | }; |
| 2736 | |
| 2737 | /*****************************************************************************\ |
| 2738 | * * |
| 2739 | * Tasklets * |
| 2740 | * * |
| 2741 | \*****************************************************************************/ |
| 2742 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2743 | static bool sdhci_request_done(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2744 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2745 | unsigned long flags; |
| 2746 | struct mmc_request *mrq; |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2747 | int i; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2748 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2749 | spin_lock_irqsave(&host->lock, flags); |
| 2750 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2751 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 2752 | mrq = host->mrqs_done[i]; |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2753 | if (mrq) |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2754 | break; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2755 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2756 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2757 | if (!mrq) { |
| 2758 | spin_unlock_irqrestore(&host->lock, flags); |
| 2759 | return true; |
| 2760 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2761 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2762 | sdhci_del_timer(host, mrq); |
| 2763 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2764 | /* |
Russell King | 054cedf | 2016-01-26 13:40:42 +0000 | [diff] [blame] | 2765 | * Always unmap the data buffers if they were mapped by |
| 2766 | * sdhci_prepare_data() whenever we finish with a request. |
| 2767 | * This avoids leaking DMA mappings on error. |
| 2768 | */ |
| 2769 | if (host->flags & SDHCI_REQ_USE_DMA) { |
| 2770 | struct mmc_data *data = mrq->data; |
| 2771 | |
| 2772 | if (data && data->host_cookie == COOKIE_MAPPED) { |
| 2773 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 2774 | (data->flags & MMC_DATA_READ) ? |
| 2775 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 2776 | data->host_cookie = COOKIE_UNMAPPED; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2781 | * The controller needs a reset of internal state machines |
| 2782 | * upon error conditions. |
| 2783 | */ |
Adrian Hunter | 0cc563c | 2016-06-29 16:24:28 +0300 | [diff] [blame] | 2784 | if (sdhci_needs_reset(host, mrq)) { |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2785 | /* |
| 2786 | * Do not finish until command and data lines are available for |
| 2787 | * reset. Note there can only be one other mrq, so it cannot |
| 2788 | * also be in mrqs_done, otherwise host->cmd and host->data_cmd |
| 2789 | * would both be null. |
| 2790 | */ |
| 2791 | if (host->cmd || host->data_cmd) { |
| 2792 | spin_unlock_irqrestore(&host->lock, flags); |
| 2793 | return true; |
| 2794 | } |
| 2795 | |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2796 | /* Some controllers need this kick or reset won't work here */ |
Andy Shevchenko | 8213af3 | 2013-01-07 16:31:08 +0200 | [diff] [blame] | 2797 | if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2798 | /* This is to force an update */ |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 2799 | host->ops->set_clock(host, host->clock); |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2800 | |
| 2801 | /* Spec says we should do both at the same time, but Ricoh |
| 2802 | controllers do not like that. */ |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2803 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2804 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 2805 | |
| 2806 | host->pending_reset = false; |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 2807 | } else { |
| 2808 | if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT) |
| 2809 | sdhci_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2812 | if (!sdhci_has_requests(host)) |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 2813 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 2814 | sdhci_led_deactivate(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2815 | |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2816 | host->mrqs_done[i] = NULL; |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2817 | host->auto_cmd_err_sts = 0; |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2818 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2819 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2820 | spin_unlock_irqrestore(&host->lock, flags); |
| 2821 | |
| 2822 | mmc_request_done(host->mmc, mrq); |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2823 | |
| 2824 | return false; |
| 2825 | } |
| 2826 | |
| 2827 | static void sdhci_tasklet_finish(unsigned long param) |
| 2828 | { |
| 2829 | struct sdhci_host *host = (struct sdhci_host *)param; |
| 2830 | |
| 2831 | while (!sdhci_request_done(host)) |
| 2832 | ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static void sdhci_timeout_timer(unsigned long data) |
| 2836 | { |
| 2837 | struct sdhci_host *host; |
| 2838 | unsigned long flags; |
| 2839 | |
| 2840 | host = (struct sdhci_host*)data; |
| 2841 | |
| 2842 | spin_lock_irqsave(&host->lock, flags); |
| 2843 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2844 | if (host->cmd && !sdhci_data_line_cmd(host->cmd)) { |
| 2845 | pr_err("%s: Timeout waiting for hardware cmd interrupt.\n", |
| 2846 | mmc_hostname(host->mmc)); |
| 2847 | sdhci_dumpregs(host); |
| 2848 | |
| 2849 | host->cmd->error = -ETIMEDOUT; |
| 2850 | sdhci_finish_mrq(host, host->cmd->mrq); |
| 2851 | } |
| 2852 | |
| 2853 | mmiowb(); |
| 2854 | spin_unlock_irqrestore(&host->lock, flags); |
| 2855 | } |
| 2856 | |
| 2857 | static void sdhci_timeout_data_timer(unsigned long data) |
| 2858 | { |
| 2859 | struct sdhci_host *host; |
| 2860 | unsigned long flags; |
| 2861 | |
| 2862 | host = (struct sdhci_host *)data; |
| 2863 | |
| 2864 | spin_lock_irqsave(&host->lock, flags); |
| 2865 | |
| 2866 | if (host->data || host->data_cmd || |
| 2867 | (host->cmd && sdhci_data_line_cmd(host->cmd))) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2868 | pr_err("%s: Timeout waiting for hardware interrupt.\n", |
| 2869 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2870 | sdhci_dumpregs(host); |
| 2871 | |
| 2872 | if (host->data) { |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 2873 | pr_info("%s: bytes to transfer: %d transferred: %d\n", |
| 2874 | mmc_hostname(host->mmc), |
| 2875 | (host->data->blksz * host->data->blocks), |
| 2876 | (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) * |
| 2877 | sdhci_readw(host, SDHCI_BLOCK_COUNT)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 2878 | host->data->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2879 | sdhci_finish_data(host); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2880 | } else if (host->data_cmd) { |
| 2881 | host->data_cmd->error = -ETIMEDOUT; |
| 2882 | sdhci_finish_mrq(host, host->data_cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2883 | } else { |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2884 | host->cmd->error = -ETIMEDOUT; |
| 2885 | sdhci_finish_mrq(host, host->cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2889 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2890 | spin_unlock_irqrestore(&host->lock, flags); |
| 2891 | } |
| 2892 | |
| 2893 | /*****************************************************************************\ |
| 2894 | * * |
| 2895 | * Interrupt handling * |
| 2896 | * * |
| 2897 | \*****************************************************************************/ |
| 2898 | |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 2899 | static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2900 | { |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2901 | u16 auto_cmd_status; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2902 | if (!host->cmd) { |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 2903 | /* |
| 2904 | * SDHCI recovers from errors by resetting the cmd and data |
| 2905 | * circuits. Until that is done, there very well might be more |
| 2906 | * interrupts, so ignore them in that case. |
| 2907 | */ |
| 2908 | if (host->pending_reset) |
| 2909 | return; |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2910 | pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n", |
| 2911 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2912 | sdhci_dumpregs(host); |
| 2913 | return; |
| 2914 | } |
| 2915 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 2916 | trace_mmc_cmd_rw_end(host->cmd->opcode, intmask, |
| 2917 | sdhci_readl(host, SDHCI_RESPONSE)); |
| 2918 | |
Russell King | ec014cb | 2016-01-26 13:39:39 +0000 | [diff] [blame] | 2919 | if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2920 | SDHCI_INT_END_BIT | SDHCI_INT_INDEX | |
| 2921 | SDHCI_INT_AUTO_CMD_ERR)) { |
Russell King | ec014cb | 2016-01-26 13:39:39 +0000 | [diff] [blame] | 2922 | if (intmask & SDHCI_INT_TIMEOUT) |
| 2923 | host->cmd->error = -ETIMEDOUT; |
| 2924 | else |
| 2925 | host->cmd->error = -EILSEQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2926 | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2927 | if (intmask & SDHCI_INT_AUTO_CMD_ERR) { |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2928 | auto_cmd_status = host->auto_cmd_err_sts; |
Konstantin Dorfman | 4b0bcd3 | 2015-06-02 17:41:53 +0300 | [diff] [blame] | 2929 | pr_err_ratelimited("%s: %s: AUTO CMD err sts 0x%08x\n", |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2930 | mmc_hostname(host->mmc), __func__, auto_cmd_status); |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2931 | if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC | |
| 2932 | SDHCI_AUTO_CMD_INDEX_ERR | |
| 2933 | SDHCI_AUTO_CMD_ENDBIT_ERR)) |
| 2934 | host->cmd->error = -EIO; |
| 2935 | else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR) |
| 2936 | host->cmd->error = -ETIMEDOUT; |
| 2937 | else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR) |
| 2938 | host->cmd->error = -EILSEQ; |
| 2939 | } |
| 2940 | |
Russell King | 71fcbda | 2016-01-26 13:39:45 +0000 | [diff] [blame] | 2941 | /* |
| 2942 | * If this command initiates a data phase and a response |
| 2943 | * CRC error is signalled, the card can start transferring |
| 2944 | * data - the card may have received the command without |
| 2945 | * error. We must not terminate the mmc_request early. |
| 2946 | * |
| 2947 | * If the card did not receive the command or returned an |
| 2948 | * error which prevented it sending data, the data phase |
| 2949 | * will time out. |
Ritesh Harjani | 8c0c937 | 2016-05-19 14:04:34 +0530 | [diff] [blame] | 2950 | * |
| 2951 | * Even in case of cmd INDEX OR ENDBIT error we |
| 2952 | * handle it the same way. |
Russell King | 71fcbda | 2016-01-26 13:39:45 +0000 | [diff] [blame] | 2953 | */ |
| 2954 | if (host->cmd->data && |
Ritesh Harjani | 8c0c937 | 2016-05-19 14:04:34 +0530 | [diff] [blame] | 2955 | (((intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) == |
| 2956 | SDHCI_INT_CRC) || (host->cmd->error == -EILSEQ))) { |
Russell King | 71fcbda | 2016-01-26 13:39:45 +0000 | [diff] [blame] | 2957 | host->cmd = NULL; |
| 2958 | return; |
| 2959 | } |
| 2960 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 2961 | sdhci_finish_mrq(host, host->cmd->mrq); |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2962 | return; |
| 2963 | } |
| 2964 | |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2965 | if (intmask & SDHCI_INT_RESPONSE) |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 2966 | sdhci_finish_command(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2967 | } |
| 2968 | |
George G. Davis | 0957c33 | 2010-02-18 12:32:12 -0500 | [diff] [blame] | 2969 | #ifdef CONFIG_MMC_DEBUG |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 2970 | static void sdhci_adma_show_error(struct sdhci_host *host) |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2971 | { |
| 2972 | const char *name = mmc_hostname(host->mmc); |
Adrian Hunter | 1c3d5f6 | 2014-11-04 12:42:41 +0200 | [diff] [blame] | 2973 | void *desc = host->adma_table; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2974 | |
| 2975 | sdhci_dumpregs(host); |
| 2976 | |
| 2977 | while (true) { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 2978 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2979 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 2980 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 2981 | DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n", |
| 2982 | name, desc, le32_to_cpu(dma_desc->addr_hi), |
| 2983 | le32_to_cpu(dma_desc->addr_lo), |
| 2984 | le16_to_cpu(dma_desc->len), |
| 2985 | le16_to_cpu(dma_desc->cmd)); |
| 2986 | else |
| 2987 | DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n", |
| 2988 | name, desc, le32_to_cpu(dma_desc->addr_lo), |
| 2989 | le16_to_cpu(dma_desc->len), |
| 2990 | le16_to_cpu(dma_desc->cmd)); |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2991 | |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 2992 | desc += host->desc_sz; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2993 | |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 2994 | if (dma_desc->cmd & cpu_to_le16(ADMA2_END)) |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2995 | break; |
| 2996 | } |
| 2997 | } |
| 2998 | #else |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 2999 | static void sdhci_adma_show_error(struct sdhci_host *host) { } |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 3000 | #endif |
| 3001 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3002 | static void sdhci_data_irq(struct sdhci_host *host, u32 intmask) |
| 3003 | { |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 3004 | u32 command; |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3005 | bool pr_msg = false; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3006 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 3007 | command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)); |
| 3008 | trace_mmc_data_rw_end(command, intmask); |
| 3009 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 3010 | /* CMD19 generates _only_ Buffer Read Ready interrupt */ |
| 3011 | if (intmask & SDHCI_INT_DATA_AVAIL) { |
Sahitya Tummala | 05709df | 2016-04-01 14:29:22 +0530 | [diff] [blame] | 3012 | if (!(host->quirks2 & SDHCI_QUIRK2_NON_STANDARD_TUNING) && |
| 3013 | (command == MMC_SEND_TUNING_BLOCK || |
| 3014 | command == MMC_SEND_TUNING_BLOCK_HS200)) { |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 3015 | host->tuning_done = 1; |
| 3016 | wake_up(&host->buf_ready_int); |
| 3017 | return; |
| 3018 | } |
| 3019 | } |
| 3020 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3021 | if (!host->data) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 3022 | struct mmc_command *data_cmd = host->data_cmd; |
| 3023 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3024 | /* |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 3025 | * The "data complete" interrupt is also used to |
| 3026 | * indicate that a busy state has ended. See comment |
| 3027 | * above in sdhci_cmd_irq(). |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3028 | */ |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 3029 | if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) { |
Matthieu CASTET | c5abd5e | 2014-08-14 16:03:17 +0200 | [diff] [blame] | 3030 | if (intmask & SDHCI_INT_DATA_TIMEOUT) { |
Adrian Hunter | 69b962a | 2016-11-02 15:49:09 +0200 | [diff] [blame] | 3031 | host->data_cmd = NULL; |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 3032 | data_cmd->error = -ETIMEDOUT; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 3033 | sdhci_finish_mrq(host, data_cmd->mrq); |
Matthieu CASTET | c5abd5e | 2014-08-14 16:03:17 +0200 | [diff] [blame] | 3034 | return; |
| 3035 | } |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 3036 | if (intmask & SDHCI_INT_DATA_END) { |
Adrian Hunter | 69b962a | 2016-11-02 15:49:09 +0200 | [diff] [blame] | 3037 | host->data_cmd = NULL; |
Chanho Min | e99783a | 2014-08-30 12:40:40 +0900 | [diff] [blame] | 3038 | /* |
| 3039 | * Some cards handle busy-end interrupt |
| 3040 | * before the command completed, so make |
| 3041 | * sure we do things in the proper order. |
| 3042 | */ |
Adrian Hunter | ea96802 | 2016-06-29 16:24:24 +0300 | [diff] [blame] | 3043 | if (host->cmd == data_cmd) |
| 3044 | return; |
| 3045 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 3046 | sdhci_finish_mrq(host, data_cmd->mrq); |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 3047 | return; |
| 3048 | } |
Sahitya Tummala | 87d4394 | 2013-04-12 11:49:11 +0530 | [diff] [blame] | 3049 | if (host->quirks2 & |
| 3050 | SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD) |
| 3051 | return; |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 3052 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3053 | |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 3054 | /* |
| 3055 | * SDHCI recovers from errors by resetting the cmd and data |
| 3056 | * circuits. Until that is done, there very well might be more |
| 3057 | * interrupts, so ignore them in that case. |
| 3058 | */ |
| 3059 | if (host->pending_reset) |
| 3060 | return; |
| 3061 | |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3062 | pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n", |
| 3063 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3064 | sdhci_dumpregs(host); |
| 3065 | |
| 3066 | return; |
| 3067 | } |
| 3068 | |
| 3069 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 3070 | host->data->error = -ETIMEDOUT; |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 3071 | else if (intmask & SDHCI_INT_DATA_END_BIT) |
| 3072 | host->data->error = -EILSEQ; |
| 3073 | else if ((intmask & SDHCI_INT_DATA_CRC) && |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 3074 | (command != MMC_BUS_TEST_R)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 3075 | host->data->error = -EILSEQ; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 3076 | else if (intmask & SDHCI_INT_ADMA_ERROR) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3077 | pr_err("%s: ADMA error\n", mmc_hostname(host->mmc)); |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 3078 | sdhci_adma_show_error(host); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3079 | host->data->error = -EIO; |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 3080 | if (host->ops->adma_workaround) |
| 3081 | host->ops->adma_workaround(host, intmask); |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 3082 | } |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3083 | if (host->data->error) { |
Pavan Anamula | 716d8c2 | 2016-04-29 11:37:23 +0530 | [diff] [blame] | 3084 | if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
| 3085 | | SDHCI_INT_DATA_END_BIT)) { |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3086 | command = SDHCI_GET_CMD(sdhci_readw(host, |
| 3087 | SDHCI_COMMAND)); |
| 3088 | if ((command != MMC_SEND_TUNING_BLOCK_HS200) && |
| 3089 | (command != MMC_SEND_TUNING_BLOCK)) |
| 3090 | pr_msg = true; |
| 3091 | } else { |
| 3092 | pr_msg = true; |
| 3093 | } |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 3094 | if (pr_msg && __ratelimit(&host->dbg_dump_rs)) { |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 3095 | pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n", |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3096 | mmc_hostname(host->mmc), intmask, |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 3097 | host->data->error, ktime_to_ms(ktime_sub( |
| 3098 | ktime_get(), host->data_start_time))); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3099 | sdhci_dumpregs(host); |
| 3100 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3101 | sdhci_finish_data(host); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3102 | } else { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 3103 | if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3104 | sdhci_transfer_pio(host); |
| 3105 | |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3106 | /* |
| 3107 | * We currently don't do anything fancy with DMA |
| 3108 | * boundaries, but as we can't disable the feature |
| 3109 | * we need to at least restart the transfer. |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 3110 | * |
| 3111 | * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS) |
| 3112 | * should return a valid address to continue from, but as |
| 3113 | * some controllers are faulty, don't trust them. |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3114 | */ |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 3115 | if (intmask & SDHCI_INT_DMA_END) { |
| 3116 | u32 dmastart, dmanow; |
| 3117 | dmastart = sg_dma_address(host->data->sg); |
| 3118 | dmanow = dmastart + host->data->bytes_xfered; |
| 3119 | /* |
| 3120 | * Force update to the next DMA block boundary. |
| 3121 | */ |
| 3122 | dmanow = (dmanow & |
| 3123 | ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + |
| 3124 | SDHCI_DEFAULT_BOUNDARY_SIZE; |
| 3125 | host->data->bytes_xfered = dmanow - dmastart; |
| 3126 | DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes," |
| 3127 | " next 0x%08x\n", |
| 3128 | mmc_hostname(host->mmc), dmastart, |
| 3129 | host->data->bytes_xfered, dmanow); |
| 3130 | sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); |
| 3131 | } |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3132 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 3133 | if (intmask & SDHCI_INT_DATA_END) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 3134 | if (host->cmd == host->data_cmd) { |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 3135 | /* |
| 3136 | * Data managed to finish before the |
| 3137 | * command completed. Make sure we do |
| 3138 | * things in the proper order. |
| 3139 | */ |
| 3140 | host->data_early = 1; |
| 3141 | } else { |
| 3142 | sdhci_finish_data(host); |
| 3143 | } |
| 3144 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3148 | #ifdef CONFIG_MMC_CQ_HCI |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3149 | static int sdhci_get_cmd_err(u32 intmask) |
| 3150 | { |
| 3151 | if (intmask & SDHCI_INT_TIMEOUT) |
| 3152 | return -ETIMEDOUT; |
| 3153 | else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT | |
| 3154 | SDHCI_INT_INDEX)) |
| 3155 | return -EILSEQ; |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | static int sdhci_get_data_err(u32 intmask) |
| 3160 | { |
| 3161 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
| 3162 | return -ETIMEDOUT; |
| 3163 | else if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC)) |
| 3164 | return -EILSEQ; |
| 3165 | else if (intmask & SDHCI_INT_ADMA_ERROR) |
| 3166 | return -EIO; |
| 3167 | return 0; |
| 3168 | } |
| 3169 | |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3170 | static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask) |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3171 | { |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3172 | int err = 0; |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3173 | u32 mask = 0; |
Ritesh Harjani | af18da9 | 2015-10-01 11:41:29 +0530 | [diff] [blame] | 3174 | irqreturn_t ret; |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3175 | |
| 3176 | if (intmask & SDHCI_INT_CMD_MASK) |
| 3177 | err = sdhci_get_cmd_err(intmask); |
| 3178 | else if (intmask & SDHCI_INT_DATA_MASK) |
| 3179 | err = sdhci_get_data_err(intmask); |
| 3180 | |
Ritesh Harjani | af18da9 | 2015-10-01 11:41:29 +0530 | [diff] [blame] | 3181 | ret = cmdq_irq(host->mmc, err); |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3182 | if (err) { |
| 3183 | /* Clear the error interrupts */ |
| 3184 | mask = intmask & SDHCI_INT_ERROR_MASK; |
| 3185 | sdhci_writel(host, mask, SDHCI_INT_STATUS); |
| 3186 | } |
Ritesh Harjani | af18da9 | 2015-10-01 11:41:29 +0530 | [diff] [blame] | 3187 | return ret; |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3188 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | #else |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3192 | static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask) |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3193 | { |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3194 | pr_err("%s: Received cmdq-irq when disabled !!!!\n", |
| 3195 | mmc_hostname(host->mmc)); |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3196 | return IRQ_NONE; |
| 3197 | } |
| 3198 | #endif |
| 3199 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 3200 | static irqreturn_t sdhci_irq(int irq, void *dev_id) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3201 | { |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3202 | irqreturn_t result = IRQ_NONE; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3203 | struct sdhci_host *host = dev_id; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3204 | u32 intmask, mask, unexpected = 0; |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3205 | int max_loops = 16; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3206 | |
| 3207 | spin_lock(&host->lock); |
| 3208 | |
Russell King | be13855 | 2014-04-25 12:55:56 +0100 | [diff] [blame] | 3209 | if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) { |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3210 | spin_unlock(&host->lock); |
Adrian Hunter | 655bca7 | 2014-03-11 10:09:36 +0200 | [diff] [blame] | 3211 | return IRQ_NONE; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3212 | } |
| 3213 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3214 | if (!host->clock && host->mmc->card && |
| 3215 | mmc_card_sdio(host->mmc->card)) { |
Pavan Anamula | f2dda06 | 2016-03-30 22:07:56 +0530 | [diff] [blame] | 3216 | if (!mmc_card_and_host_support_async_int(host->mmc)) { |
| 3217 | spin_unlock(&host->lock); |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3218 | return IRQ_NONE; |
Pavan Anamula | f2dda06 | 2016-03-30 22:07:56 +0530 | [diff] [blame] | 3219 | } |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3220 | /* |
| 3221 | * async card interrupt is level sensitive and received |
| 3222 | * when clocks are off. |
| 3223 | * If sdio card has asserted async interrupt, in that |
| 3224 | * case we need to disable host->irq. |
| 3225 | * Later we can disable card interrupt and re-enable |
| 3226 | * host->irq. |
| 3227 | */ |
| 3228 | |
| 3229 | pr_debug("%s: %s: sdio_async intr. received\n", |
| 3230 | mmc_hostname(host->mmc), __func__); |
| 3231 | sdhci_cfg_irq(host, false, false); |
| 3232 | host->sdio_irq_async_status = true; |
| 3233 | host->thread_isr |= SDHCI_INT_CARD_INT; |
| 3234 | result = IRQ_WAKE_THREAD; |
| 3235 | spin_unlock(&host->lock); |
| 3236 | return result; |
| 3237 | } |
| 3238 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 3239 | intmask = sdhci_readl(host, SDHCI_INT_STATUS); |
Mark Lord | 62df67a5 | 2007-03-06 13:30:13 +0100 | [diff] [blame] | 3240 | if (!intmask || intmask == 0xffffffff) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3241 | result = IRQ_NONE; |
| 3242 | goto out; |
| 3243 | } |
| 3244 | |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3245 | do { |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3246 | if (host->mmc->card && mmc_card_cmdq(host->mmc->card) && |
Sahitya Tummala | 94f4508 | 2016-04-26 16:31:03 +0530 | [diff] [blame] | 3247 | !mmc_host_halt(host->mmc) && !mmc_host_cq_disable(host->mmc)) { |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3248 | pr_debug("*** %s: cmdq intr: 0x%08x\n", |
| 3249 | mmc_hostname(host->mmc), |
| 3250 | intmask); |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3251 | result = sdhci_cmdq_irq(host, intmask); |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3252 | if (result == IRQ_HANDLED) |
| 3253 | goto out; |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3254 | } |
| 3255 | |
Sayali Lokhande | bff771e | 2016-11-30 11:35:22 +0530 | [diff] [blame] | 3256 | MMC_TRACE(host->mmc, |
| 3257 | "%s: intmask: 0x%x\n", __func__, intmask); |
| 3258 | |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 3259 | if (intmask & SDHCI_INT_AUTO_CMD_ERR) |
| 3260 | host->auto_cmd_err_sts = sdhci_readw(host, |
| 3261 | SDHCI_AUTO_CMD_ERR); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3262 | /* Clear selected interrupts. */ |
| 3263 | mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK | |
| 3264 | SDHCI_INT_BUS_POWER); |
| 3265 | sdhci_writel(host, mask, SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3266 | |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3267 | DBG("*** %s got interrupt: 0x%08x\n", |
| 3268 | mmc_hostname(host->mmc), intmask); |
| 3269 | |
| 3270 | if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
| 3271 | u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 3272 | SDHCI_CARD_PRESENT; |
| 3273 | |
| 3274 | /* |
| 3275 | * There is a observation on i.mx esdhc. INSERT |
| 3276 | * bit will be immediately set again when it gets |
| 3277 | * cleared, if a card is inserted. We have to mask |
| 3278 | * the irq to prevent interrupt storm which will |
| 3279 | * freeze the system. And the REMOVE gets the |
| 3280 | * same situation. |
| 3281 | * |
| 3282 | * More testing are needed here to ensure it works |
| 3283 | * for other platforms though. |
| 3284 | */ |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3285 | host->ier &= ~(SDHCI_INT_CARD_INSERT | |
| 3286 | SDHCI_INT_CARD_REMOVE); |
| 3287 | host->ier |= present ? SDHCI_INT_CARD_REMOVE : |
| 3288 | SDHCI_INT_CARD_INSERT; |
| 3289 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 3290 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3291 | |
| 3292 | sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT | |
| 3293 | SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS); |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3294 | |
| 3295 | host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT | |
| 3296 | SDHCI_INT_CARD_REMOVE); |
| 3297 | result = IRQ_WAKE_THREAD; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3298 | } |
| 3299 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3300 | if (intmask & SDHCI_INT_CMD_MASK) { |
| 3301 | if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) && |
| 3302 | (host->clock <= 400000)) |
| 3303 | udelay(40); |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 3304 | sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3305 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3306 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3307 | if (intmask & SDHCI_INT_DATA_MASK) { |
| 3308 | if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) && |
| 3309 | (host->clock <= 400000)) |
| 3310 | udelay(40); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3311 | sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3312 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3313 | |
| 3314 | if (intmask & SDHCI_INT_BUS_POWER) |
| 3315 | pr_err("%s: Card is consuming too much power!\n", |
| 3316 | mmc_hostname(host->mmc)); |
| 3317 | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3318 | if (intmask & SDHCI_INT_RETUNE) |
| 3319 | mmc_retune_needed(host->mmc); |
| 3320 | |
Gabriel Krisman Bertazi | 04eb7db | 2017-01-16 12:23:42 -0200 | [diff] [blame] | 3321 | if ((intmask & SDHCI_INT_CARD_INT) && |
| 3322 | (host->ier & SDHCI_INT_CARD_INT)) { |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3323 | sdhci_enable_sdio_irq_nolock(host, false); |
| 3324 | host->thread_isr |= SDHCI_INT_CARD_INT; |
| 3325 | result = IRQ_WAKE_THREAD; |
| 3326 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3327 | |
| 3328 | intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | |
| 3329 | SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK | |
| 3330 | SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3331 | SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3332 | |
| 3333 | if (intmask) { |
| 3334 | unexpected |= intmask; |
| 3335 | sdhci_writel(host, intmask, SDHCI_INT_STATUS); |
| 3336 | } |
| 3337 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3338 | if (result == IRQ_NONE) |
| 3339 | result = IRQ_HANDLED; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3340 | |
| 3341 | intmask = sdhci_readl(host, SDHCI_INT_STATUS); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3342 | } while (intmask && --max_loops); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3343 | out: |
| 3344 | spin_unlock(&host->lock); |
| 3345 | |
Alexander Stein | 6379b23 | 2012-03-14 09:52:10 +0100 | [diff] [blame] | 3346 | if (unexpected) { |
| 3347 | pr_err("%s: Unexpected interrupt 0x%08x.\n", |
| 3348 | mmc_hostname(host->mmc), unexpected); |
| 3349 | sdhci_dumpregs(host); |
| 3350 | } |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 3351 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3352 | return result; |
| 3353 | } |
| 3354 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3355 | static irqreturn_t sdhci_thread_irq(int irq, void *dev_id) |
| 3356 | { |
| 3357 | struct sdhci_host *host = dev_id; |
| 3358 | unsigned long flags; |
| 3359 | u32 isr; |
| 3360 | |
| 3361 | spin_lock_irqsave(&host->lock, flags); |
| 3362 | isr = host->thread_isr; |
| 3363 | host->thread_isr = 0; |
| 3364 | spin_unlock_irqrestore(&host->lock, flags); |
| 3365 | |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3366 | if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3367 | struct mmc_host *mmc = host->mmc; |
| 3368 | |
| 3369 | mmc->ops->card_event(mmc); |
| 3370 | mmc_detect_change(mmc, msecs_to_jiffies(200)); |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3371 | } |
| 3372 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3373 | if (isr & SDHCI_INT_CARD_INT) { |
| 3374 | sdio_run_irqs(host->mmc); |
| 3375 | |
| 3376 | spin_lock_irqsave(&host->lock, flags); |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3377 | if (host->flags & SDHCI_SDIO_IRQ_ENABLED) { |
| 3378 | if (host->sdio_irq_async_status) |
| 3379 | host->sdio_irq_async_status = false; |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3380 | sdhci_enable_sdio_irq_nolock(host, true); |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3381 | } |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3382 | spin_unlock_irqrestore(&host->lock, flags); |
| 3383 | } |
| 3384 | |
| 3385 | return isr ? IRQ_HANDLED : IRQ_NONE; |
| 3386 | } |
| 3387 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3388 | /*****************************************************************************\ |
| 3389 | * * |
| 3390 | * Suspend/resume * |
| 3391 | * * |
| 3392 | \*****************************************************************************/ |
| 3393 | |
| 3394 | #ifdef CONFIG_PM |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3395 | /* |
| 3396 | * To enable wakeup events, the corresponding events have to be enabled in |
| 3397 | * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal |
| 3398 | * Table' in the SD Host Controller Standard Specification. |
| 3399 | * It is useless to restore SDHCI_INT_ENABLE state in |
| 3400 | * sdhci_disable_irq_wakeups() since it will be set by |
| 3401 | * sdhci_enable_card_detection() or sdhci_init(). |
| 3402 | */ |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3403 | void sdhci_enable_irq_wakeups(struct sdhci_host *host) |
| 3404 | { |
| 3405 | u8 val; |
| 3406 | u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
| 3407 | | SDHCI_WAKE_ON_INT; |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3408 | u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | |
| 3409 | SDHCI_INT_CARD_INT; |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3410 | |
| 3411 | val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL); |
| 3412 | val |= mask ; |
| 3413 | /* Avoid fake wake up */ |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3414 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) { |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3415 | val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE); |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3416 | irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); |
| 3417 | } |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3418 | sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL); |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3419 | sdhci_writel(host, irq_val, SDHCI_INT_ENABLE); |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3420 | } |
| 3421 | EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups); |
| 3422 | |
Fabio Estevam | 0b10f47 | 2014-08-30 14:53:13 -0300 | [diff] [blame] | 3423 | static void sdhci_disable_irq_wakeups(struct sdhci_host *host) |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3424 | { |
| 3425 | u8 val; |
| 3426 | u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
| 3427 | | SDHCI_WAKE_ON_INT; |
| 3428 | |
| 3429 | val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL); |
| 3430 | val &= ~mask; |
| 3431 | sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL); |
| 3432 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3433 | |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 3434 | int sdhci_suspend_host(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3435 | { |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 3436 | sdhci_disable_card_detection(host); |
| 3437 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 3438 | mmc_retune_timer_stop(host->mmc); |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3439 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 3440 | mmc_retune_needed(host->mmc); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 3441 | |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3442 | if (!device_may_wakeup(mmc_dev(host->mmc))) { |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3443 | host->ier = 0; |
| 3444 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 3445 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3446 | free_irq(host->irq, host); |
| 3447 | } else { |
| 3448 | sdhci_enable_irq_wakeups(host); |
| 3449 | enable_irq_wake(host->irq); |
| 3450 | } |
Ulf Hansson | 4ee14ec | 2013-09-25 14:15:24 +0200 | [diff] [blame] | 3451 | return 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3452 | } |
| 3453 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3454 | EXPORT_SYMBOL_GPL(sdhci_suspend_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3455 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3456 | int sdhci_resume_host(struct sdhci_host *host) |
| 3457 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3458 | struct mmc_host *mmc = host->mmc; |
Ulf Hansson | 4ee14ec | 2013-09-25 14:15:24 +0200 | [diff] [blame] | 3459 | int ret = 0; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3460 | |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3461 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3462 | if (host->ops->enable_dma) |
| 3463 | host->ops->enable_dma(host); |
| 3464 | } |
| 3465 | |
Adrian Hunter | 6308d29 | 2012-02-07 14:48:54 +0200 | [diff] [blame] | 3466 | if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) && |
| 3467 | (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) { |
| 3468 | /* Card keeps power but host controller does not */ |
| 3469 | sdhci_init(host, 0); |
| 3470 | host->pwr = 0; |
| 3471 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3472 | mmc->ops->set_ios(mmc, &mmc->ios); |
Adrian Hunter | 6308d29 | 2012-02-07 14:48:54 +0200 | [diff] [blame] | 3473 | } else { |
| 3474 | sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER)); |
| 3475 | mmiowb(); |
| 3476 | } |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3477 | |
Haibo Chen | 14a7b4164 | 2015-09-15 18:32:58 +0800 | [diff] [blame] | 3478 | if (!device_may_wakeup(mmc_dev(host->mmc))) { |
| 3479 | ret = request_threaded_irq(host->irq, sdhci_irq, |
| 3480 | sdhci_thread_irq, IRQF_SHARED, |
| 3481 | mmc_hostname(host->mmc), host); |
| 3482 | if (ret) |
| 3483 | return ret; |
| 3484 | } else { |
| 3485 | sdhci_disable_irq_wakeups(host); |
| 3486 | disable_irq_wake(host->irq); |
| 3487 | } |
| 3488 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 3489 | sdhci_enable_card_detection(host); |
| 3490 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 3491 | return ret; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | EXPORT_SYMBOL_GPL(sdhci_resume_host); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3495 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3496 | int sdhci_runtime_suspend_host(struct sdhci_host *host) |
| 3497 | { |
| 3498 | unsigned long flags; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3499 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 3500 | mmc_retune_timer_stop(host->mmc); |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3501 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 3502 | mmc_retune_needed(host->mmc); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3503 | |
| 3504 | spin_lock_irqsave(&host->lock, flags); |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3505 | host->ier &= SDHCI_INT_CARD_INT; |
| 3506 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 3507 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3508 | spin_unlock_irqrestore(&host->lock, flags); |
| 3509 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3510 | synchronize_hardirq(host->irq); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3511 | |
| 3512 | spin_lock_irqsave(&host->lock, flags); |
| 3513 | host->runtime_suspended = true; |
| 3514 | spin_unlock_irqrestore(&host->lock, flags); |
| 3515 | |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3516 | return 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3517 | } |
| 3518 | EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host); |
| 3519 | |
| 3520 | int sdhci_runtime_resume_host(struct sdhci_host *host) |
| 3521 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3522 | struct mmc_host *mmc = host->mmc; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3523 | unsigned long flags; |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3524 | int host_flags = host->flags; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3525 | |
| 3526 | if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
| 3527 | if (host->ops->enable_dma) |
| 3528 | host->ops->enable_dma(host); |
| 3529 | } |
| 3530 | |
| 3531 | sdhci_init(host, 0); |
| 3532 | |
| 3533 | /* Force clock and power re-program */ |
| 3534 | host->pwr = 0; |
| 3535 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3536 | mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios); |
| 3537 | mmc->ops->set_ios(mmc, &mmc->ios); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3538 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 3539 | if ((host_flags & SDHCI_PV_ENABLED) && |
| 3540 | !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) { |
| 3541 | spin_lock_irqsave(&host->lock, flags); |
| 3542 | sdhci_enable_preset_value(host, true); |
| 3543 | spin_unlock_irqrestore(&host->lock, flags); |
| 3544 | } |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3545 | |
Adrian Hunter | 086b0dd | 2016-11-02 15:49:11 +0200 | [diff] [blame] | 3546 | if ((mmc->caps2 & MMC_CAP2_HS400_ES) && |
| 3547 | mmc->ops->hs400_enhanced_strobe) |
| 3548 | mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios); |
| 3549 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3550 | spin_lock_irqsave(&host->lock, flags); |
| 3551 | |
| 3552 | host->runtime_suspended = false; |
| 3553 | |
| 3554 | /* Enable SDIO IRQ */ |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 3555 | if (host->flags & SDHCI_SDIO_IRQ_ENABLED) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3556 | sdhci_enable_sdio_irq_nolock(host, true); |
| 3557 | |
| 3558 | /* Enable Card Detection */ |
| 3559 | sdhci_enable_card_detection(host); |
| 3560 | |
| 3561 | spin_unlock_irqrestore(&host->lock, flags); |
| 3562 | |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3563 | return 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3564 | } |
| 3565 | EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host); |
| 3566 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 3567 | #endif /* CONFIG_PM */ |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3568 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3569 | /*****************************************************************************\ |
| 3570 | * * |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3571 | * Device allocation/registration * |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3572 | * * |
| 3573 | \*****************************************************************************/ |
| 3574 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3575 | struct sdhci_host *sdhci_alloc_host(struct device *dev, |
| 3576 | size_t priv_size) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3577 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3578 | struct mmc_host *mmc; |
| 3579 | struct sdhci_host *host; |
| 3580 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3581 | WARN_ON(dev == NULL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3582 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3583 | mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3584 | if (!mmc) |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3585 | return ERR_PTR(-ENOMEM); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3586 | |
| 3587 | host = mmc_priv(mmc); |
| 3588 | host->mmc = mmc; |
Adrian Hunter | bf60e59 | 2016-02-09 16:12:35 +0200 | [diff] [blame] | 3589 | host->mmc_host_ops = sdhci_ops; |
| 3590 | mmc->ops = &host->mmc_host_ops; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3591 | |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 3592 | host->flags = SDHCI_SIGNALING_330; |
| 3593 | |
Sahitya Tummala | ef4de6c | 2013-05-24 08:47:26 +0530 | [diff] [blame] | 3594 | spin_lock_init(&host->lock); |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 3595 | ratelimit_state_init(&host->dbg_dump_rs, SDHCI_DBG_DUMP_RS_INTERVAL, |
| 3596 | SDHCI_DBG_DUMP_RS_BURST); |
Sahitya Tummala | ef4de6c | 2013-05-24 08:47:26 +0530 | [diff] [blame] | 3597 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3598 | return host; |
| 3599 | } |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 3600 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3601 | EXPORT_SYMBOL_GPL(sdhci_alloc_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3602 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3603 | #ifdef CONFIG_MMC_CQ_HCI |
Sahitya Tummala | 738b74c | 2016-04-12 13:25:41 +0530 | [diff] [blame] | 3604 | static void sdhci_cmdq_set_transfer_params(struct mmc_host *mmc) |
| 3605 | { |
| 3606 | struct sdhci_host *host = mmc_priv(mmc); |
| 3607 | u8 ctrl; |
| 3608 | |
| 3609 | if (host->version >= SDHCI_SPEC_200) { |
| 3610 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 3611 | ctrl &= ~SDHCI_CTRL_DMA_MASK; |
Sahitya Tummala | 05709df | 2016-04-01 14:29:22 +0530 | [diff] [blame] | 3612 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
Sahitya Tummala | 738b74c | 2016-04-12 13:25:41 +0530 | [diff] [blame] | 3613 | ctrl |= SDHCI_CTRL_ADMA64; |
| 3614 | else |
| 3615 | ctrl |= SDHCI_CTRL_ADMA32; |
| 3616 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 3617 | } |
Sahitya Tummala | 7e70930 | 2016-04-25 13:21:42 +0530 | [diff] [blame] | 3618 | if (host->ops->toggle_cdr) |
| 3619 | host->ops->toggle_cdr(host, false); |
Sahitya Tummala | 738b74c | 2016-04-12 13:25:41 +0530 | [diff] [blame] | 3620 | } |
| 3621 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3622 | static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear) |
| 3623 | { |
| 3624 | struct sdhci_host *host = mmc_priv(mmc); |
| 3625 | u32 ier = 0; |
| 3626 | |
| 3627 | ier &= ~SDHCI_INT_ALL_MASK; |
| 3628 | |
| 3629 | if (clear) { |
| 3630 | ier = SDHCI_INT_CMDQ_EN | SDHCI_INT_ERROR_MASK; |
| 3631 | sdhci_writel(host, ier, SDHCI_INT_ENABLE); |
| 3632 | sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE); |
| 3633 | } else { |
| 3634 | ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 3635 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | |
| 3636 | SDHCI_INT_INDEX | SDHCI_INT_END_BIT | |
| 3637 | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | |
| 3638 | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | |
| 3639 | SDHCI_INT_AUTO_CMD_ERR; |
| 3640 | sdhci_writel(host, ier, SDHCI_INT_ENABLE); |
| 3641 | sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE); |
| 3642 | } |
| 3643 | } |
| 3644 | |
| 3645 | static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val) |
| 3646 | { |
| 3647 | struct sdhci_host *host = mmc_priv(mmc); |
| 3648 | |
| 3649 | sdhci_writeb(host, val, SDHCI_TIMEOUT_CONTROL); |
| 3650 | } |
| 3651 | |
| 3652 | static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc) |
| 3653 | { |
| 3654 | struct sdhci_host *host = mmc_priv(mmc); |
| 3655 | |
| 3656 | sdhci_dumpregs(host); |
| 3657 | } |
| 3658 | |
| 3659 | static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc, |
| 3660 | bool dma64) |
| 3661 | { |
| 3662 | return cmdq_init(host->cq_host, mmc, dma64); |
| 3663 | } |
| 3664 | |
| 3665 | static void sdhci_cmdq_set_block_size(struct mmc_host *mmc) |
| 3666 | { |
| 3667 | struct sdhci_host *host = mmc_priv(mmc); |
| 3668 | |
| 3669 | sdhci_set_blk_size_reg(host, 512, 0); |
| 3670 | } |
| 3671 | |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3672 | static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set) |
| 3673 | { |
| 3674 | struct sdhci_host *host = mmc_priv(mmc); |
| 3675 | |
| 3676 | if (host->ops->enhanced_strobe_mask) |
| 3677 | host->ops->enhanced_strobe_mask(host, set); |
| 3678 | } |
| 3679 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3680 | static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set) |
| 3681 | { |
| 3682 | struct sdhci_host *host = mmc_priv(mmc); |
| 3683 | |
| 3684 | if (host->ops->clear_set_dumpregs) |
| 3685 | host->ops->clear_set_dumpregs(host, set); |
| 3686 | } |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3687 | |
| 3688 | static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc) |
| 3689 | { |
| 3690 | struct sdhci_host *host = mmc_priv(mmc); |
| 3691 | |
| 3692 | sdhci_writel(host, sdhci_readl(host, SDHCI_INT_ENABLE) | |
| 3693 | SDHCI_INT_RESPONSE, SDHCI_INT_ENABLE); |
| 3694 | sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS); |
| 3695 | } |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3696 | #else |
Sahitya Tummala | 738b74c | 2016-04-12 13:25:41 +0530 | [diff] [blame] | 3697 | static void sdhci_cmdq_set_transfer_params(struct mmc_host *mmc) |
| 3698 | { |
| 3699 | |
| 3700 | } |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3701 | static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear) |
| 3702 | { |
| 3703 | |
| 3704 | } |
| 3705 | |
| 3706 | static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val) |
| 3707 | { |
| 3708 | |
| 3709 | } |
| 3710 | |
| 3711 | static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc) |
| 3712 | { |
| 3713 | |
| 3714 | } |
| 3715 | |
| 3716 | static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc, |
| 3717 | bool dma64) |
| 3718 | { |
| 3719 | return -ENOSYS; |
| 3720 | } |
| 3721 | |
| 3722 | static void sdhci_cmdq_set_block_size(struct mmc_host *mmc) |
| 3723 | { |
| 3724 | |
| 3725 | } |
| 3726 | |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3727 | static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set) |
| 3728 | { |
| 3729 | |
| 3730 | } |
| 3731 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3732 | static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set) |
| 3733 | { |
| 3734 | |
| 3735 | } |
| 3736 | |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3737 | static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc) |
| 3738 | { |
| 3739 | } |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3740 | #endif |
| 3741 | |
| 3742 | static const struct cmdq_host_ops sdhci_cmdq_ops = { |
| 3743 | .clear_set_irqs = sdhci_cmdq_clear_set_irqs, |
| 3744 | .set_data_timeout = sdhci_cmdq_set_data_timeout, |
| 3745 | .dump_vendor_regs = sdhci_cmdq_dump_vendor_regs, |
| 3746 | .set_block_size = sdhci_cmdq_set_block_size, |
| 3747 | .clear_set_dumpregs = sdhci_cmdq_clear_set_dumpregs, |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3748 | .enhanced_strobe_mask = sdhci_enhanced_strobe_mask, |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3749 | .post_cqe_halt = sdhci_cmdq_post_cqe_halt, |
Sahitya Tummala | 738b74c | 2016-04-12 13:25:41 +0530 | [diff] [blame] | 3750 | .set_transfer_params = sdhci_cmdq_set_transfer_params, |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3751 | }; |
| 3752 | |
Subhash Jadavani | 7a9b007 | 2016-05-11 16:47:20 -0700 | [diff] [blame] | 3753 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 3754 | static int sdhci_is_adma2_64bit(struct sdhci_host *host) |
| 3755 | { |
| 3756 | u32 caps; |
| 3757 | |
| 3758 | caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps : |
| 3759 | sdhci_readl(host, SDHCI_CAPABILITIES); |
| 3760 | |
| 3761 | if (caps & SDHCI_CAN_64BIT) |
| 3762 | return 1; |
| 3763 | return 0; |
| 3764 | } |
| 3765 | #else |
| 3766 | static int sdhci_is_adma2_64bit(struct sdhci_host *host) |
| 3767 | { |
| 3768 | return 0; |
| 3769 | } |
| 3770 | #endif |
| 3771 | |
Alexandre Courbot | 7b91369 | 2016-03-07 11:07:55 +0900 | [diff] [blame] | 3772 | static int sdhci_set_dma_mask(struct sdhci_host *host) |
| 3773 | { |
| 3774 | struct mmc_host *mmc = host->mmc; |
| 3775 | struct device *dev = mmc_dev(mmc); |
| 3776 | int ret = -EINVAL; |
| 3777 | |
| 3778 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) |
| 3779 | host->flags &= ~SDHCI_USE_64_BIT_DMA; |
| 3780 | |
| 3781 | /* Try 64-bit mask if hardware is capable of it */ |
| 3782 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
| 3783 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); |
| 3784 | if (ret) { |
| 3785 | pr_warn("%s: Failed to set 64-bit DMA mask.\n", |
| 3786 | mmc_hostname(mmc)); |
| 3787 | host->flags &= ~SDHCI_USE_64_BIT_DMA; |
| 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | /* 32-bit mask as default & fallback */ |
| 3792 | if (ret) { |
| 3793 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 3794 | if (ret) |
| 3795 | pr_warn("%s: Failed to set 32-bit DMA mask.\n", |
| 3796 | mmc_hostname(mmc)); |
| 3797 | } |
| 3798 | |
| 3799 | return ret; |
| 3800 | } |
| 3801 | |
Adrian Hunter | 6132a3b | 2016-06-29 16:24:18 +0300 | [diff] [blame] | 3802 | void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1) |
| 3803 | { |
| 3804 | u16 v; |
| 3805 | |
| 3806 | if (host->read_caps) |
| 3807 | return; |
| 3808 | |
| 3809 | host->read_caps = true; |
| 3810 | |
| 3811 | if (debug_quirks) |
| 3812 | host->quirks = debug_quirks; |
| 3813 | |
| 3814 | if (debug_quirks2) |
| 3815 | host->quirks2 = debug_quirks2; |
| 3816 | |
| 3817 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
| 3818 | |
| 3819 | v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION); |
| 3820 | host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT; |
| 3821 | |
| 3822 | if (host->quirks & SDHCI_QUIRK_MISSING_CAPS) |
| 3823 | return; |
| 3824 | |
| 3825 | host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES); |
| 3826 | |
| 3827 | if (host->version < SDHCI_SPEC_300) |
| 3828 | return; |
| 3829 | |
| 3830 | host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1); |
| 3831 | } |
| 3832 | EXPORT_SYMBOL_GPL(__sdhci_read_caps); |
| 3833 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 3834 | int sdhci_setup_host(struct sdhci_host *host) |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3835 | { |
| 3836 | struct mmc_host *mmc; |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3837 | u32 caps[2] = {0, 0}; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3838 | u32 max_current_caps; |
| 3839 | unsigned int ocr_avail; |
Adrian Hunter | f5fa92e | 2014-09-24 10:27:32 +0300 | [diff] [blame] | 3840 | unsigned int override_timeout_clk; |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 3841 | u32 max_clk; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3842 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3843 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3844 | WARN_ON(host == NULL); |
| 3845 | if (host == NULL) |
| 3846 | return -EINVAL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3847 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3848 | mmc = host->mmc; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3849 | |
Jon Hunter | efba142 | 2016-07-12 14:53:36 +0100 | [diff] [blame] | 3850 | /* |
| 3851 | * If there are external regulators, get them. Note this must be done |
| 3852 | * early before resetting the host and reading the capabilities so that |
| 3853 | * the host can take the appropriate action if regulators are not |
| 3854 | * available. |
| 3855 | */ |
| 3856 | ret = mmc_regulator_get_supply(mmc); |
| 3857 | if (ret == -EPROBE_DEFER) |
| 3858 | return ret; |
| 3859 | |
Adrian Hunter | 6132a3b | 2016-06-29 16:24:18 +0300 | [diff] [blame] | 3860 | sdhci_read_caps(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3861 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 3862 | caps[0] = host->caps; |
| 3863 | |
Adrian Hunter | f5fa92e | 2014-09-24 10:27:32 +0300 | [diff] [blame] | 3864 | override_timeout_clk = host->timeout_clk; |
| 3865 | |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 3866 | if (host->version > SDHCI_SPEC_300) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3867 | pr_err("%s: Unknown controller version (%d). You may experience problems.\n", |
| 3868 | mmc_hostname(mmc), host->version); |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3871 | if (host->quirks & SDHCI_QUIRK_FORCE_DMA) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3872 | host->flags |= SDHCI_USE_SDMA; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3873 | else if (!(host->caps & SDHCI_CAN_DO_SDMA)) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3874 | DBG("Controller doesn't have SDMA capability\n"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 3875 | else |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3876 | host->flags |= SDHCI_USE_SDMA; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3877 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3878 | if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) && |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3879 | (host->flags & SDHCI_USE_SDMA)) { |
Rolf Eike Beer | cee687c | 2007-11-02 15:22:30 +0100 | [diff] [blame] | 3880 | DBG("Disabling DMA as it is marked broken\n"); |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3881 | host->flags &= ~SDHCI_USE_SDMA; |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 3882 | } |
| 3883 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3884 | if ((host->version >= SDHCI_SPEC_200) && |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3885 | (host->caps & SDHCI_CAN_DO_ADMA2)) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3886 | host->flags |= SDHCI_USE_ADMA; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3887 | |
| 3888 | if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) && |
| 3889 | (host->flags & SDHCI_USE_ADMA)) { |
| 3890 | DBG("Disabling ADMA as it is marked broken\n"); |
| 3891 | host->flags &= ~SDHCI_USE_ADMA; |
| 3892 | } |
| 3893 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3894 | /* |
| 3895 | * It is assumed that a 64-bit capable device has set a 64-bit DMA mask |
| 3896 | * and *must* do 64-bit DMA. A driver has the opportunity to change |
| 3897 | * that during the first call to ->enable_dma(). Similarly |
| 3898 | * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to |
| 3899 | * implement. |
| 3900 | */ |
Subhash Jadavani | 7a9b007 | 2016-05-11 16:47:20 -0700 | [diff] [blame] | 3901 | if (sdhci_is_adma2_64bit(host)) |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3902 | host->flags |= SDHCI_USE_64_BIT_DMA; |
| 3903 | |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3904 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Alexandre Courbot | 7b91369 | 2016-03-07 11:07:55 +0900 | [diff] [blame] | 3905 | ret = sdhci_set_dma_mask(host); |
| 3906 | |
| 3907 | if (!ret && host->ops->enable_dma) |
| 3908 | ret = host->ops->enable_dma(host); |
| 3909 | |
| 3910 | if (ret) { |
| 3911 | pr_warn("%s: No suitable DMA available - falling back to PIO\n", |
| 3912 | mmc_hostname(mmc)); |
| 3913 | host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA); |
| 3914 | |
| 3915 | ret = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3919 | /* SDMA does not support 64-bit DMA */ |
| 3920 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 3921 | host->flags &= ~SDHCI_USE_SDMA; |
| 3922 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3923 | if (host->flags & SDHCI_USE_ADMA) { |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3924 | dma_addr_t dma; |
| 3925 | void *buf; |
| 3926 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3927 | /* |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 3928 | * The DMA descriptor table size is calculated as the maximum |
| 3929 | * number of segments times 2, to allow for an alignment |
| 3930 | * descriptor for each segment, plus 1 for a nop end descriptor, |
| 3931 | * all multipled by the descriptor size. |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3932 | */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3933 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
| 3934 | host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) * |
| 3935 | SDHCI_ADMA2_64_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3936 | host->desc_sz = SDHCI_ADMA2_64_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3937 | } else { |
| 3938 | host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) * |
| 3939 | SDHCI_ADMA2_32_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3940 | host->desc_sz = SDHCI_ADMA2_32_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3941 | } |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3942 | |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 3943 | host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3944 | buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 3945 | host->adma_table_sz, &dma, GFP_KERNEL); |
| 3946 | if (!buf) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 3947 | pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n", |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3948 | mmc_hostname(mmc)); |
| 3949 | host->flags &= ~SDHCI_USE_ADMA; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3950 | } else if ((dma + host->align_buffer_sz) & |
| 3951 | (SDHCI_ADMA2_DESC_ALIGN - 1)) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 3952 | pr_warn("%s: unable to allocate aligned ADMA descriptor\n", |
| 3953 | mmc_hostname(mmc)); |
Russell King | d1e49f7 | 2014-04-25 12:58:34 +0100 | [diff] [blame] | 3954 | host->flags &= ~SDHCI_USE_ADMA; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3955 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 3956 | host->adma_table_sz, buf, dma); |
| 3957 | } else { |
| 3958 | host->align_buffer = buf; |
| 3959 | host->align_addr = dma; |
Russell King | edd63fc | 2016-01-26 13:39:50 +0000 | [diff] [blame] | 3960 | |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3961 | host->adma_table = buf + host->align_buffer_sz; |
| 3962 | host->adma_addr = dma + host->align_buffer_sz; |
| 3963 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3964 | } |
| 3965 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3966 | /* |
| 3967 | * If we use DMA, then it's up to the caller to set the DMA |
| 3968 | * mask, but PIO does not need the hw shim so we set a new |
| 3969 | * mask here in that case. |
| 3970 | */ |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3971 | if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3972 | host->dma_mask = DMA_BIT_MASK(64); |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 3973 | mmc_dev(mmc)->dma_mask = &host->dma_mask; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3974 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3975 | |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3976 | if (host->version >= SDHCI_SPEC_300) |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3977 | host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK) |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3978 | >> SDHCI_CLOCK_BASE_SHIFT; |
| 3979 | else |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3980 | host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK) |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3981 | >> SDHCI_CLOCK_BASE_SHIFT; |
| 3982 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3983 | host->max_clk *= 1000000; |
Anton Vorontsov | f27f47e | 2010-05-26 14:41:53 -0700 | [diff] [blame] | 3984 | if (host->max_clk == 0 || host->quirks & |
| 3985 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) { |
Ben Dooks | 4240ff0 | 2009-03-17 00:13:57 +0300 | [diff] [blame] | 3986 | if (!host->ops->get_max_clock) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3987 | pr_err("%s: Hardware doesn't specify base clock frequency.\n", |
| 3988 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 3989 | ret = -ENODEV; |
| 3990 | goto undma; |
Ben Dooks | 4240ff0 | 2009-03-17 00:13:57 +0300 | [diff] [blame] | 3991 | } |
| 3992 | host->max_clk = host->ops->get_max_clock(host); |
| 3993 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3994 | |
| 3995 | /* |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 3996 | * In case of Host Controller v3.00, find out whether clock |
| 3997 | * multiplier is supported. |
| 3998 | */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3999 | host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >> |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 4000 | SDHCI_CLOCK_MUL_SHIFT; |
| 4001 | |
| 4002 | /* |
| 4003 | * In case the value in Clock Multiplier is 0, then programmable |
| 4004 | * clock mode is not supported, otherwise the actual clock |
| 4005 | * multiplier is one more than the value of Clock Multiplier |
| 4006 | * in the Capabilities Register. |
| 4007 | */ |
| 4008 | if (host->clk_mul) |
| 4009 | host->clk_mul += 1; |
| 4010 | |
| 4011 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4012 | * Set host parameters. |
| 4013 | */ |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 4014 | max_clk = host->max_clk; |
| 4015 | |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 4016 | if (host->ops->get_min_clock) |
Anton Vorontsov | a9e58f2 | 2009-07-29 15:04:16 -0700 | [diff] [blame] | 4017 | mmc->f_min = host->ops->get_min_clock(host); |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 4018 | else if (host->version >= SDHCI_SPEC_300) { |
| 4019 | if (host->clk_mul) { |
| 4020 | mmc->f_min = (host->max_clk * host->clk_mul) / 1024; |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 4021 | max_clk = host->max_clk * host->clk_mul; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 4022 | } else |
| 4023 | mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300; |
| 4024 | } else |
Zhangfei Gao | 0397526 | 2010-09-20 15:15:18 -0400 | [diff] [blame] | 4025 | mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200; |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 4026 | |
Adrian Hunter | d310ae4 | 2016-04-12 14:25:07 +0300 | [diff] [blame] | 4027 | if (!mmc->f_max || mmc->f_max > max_clk) |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 4028 | mmc->f_max = max_clk; |
| 4029 | |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4030 | if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) { |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4031 | host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >> |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4032 | SDHCI_TIMEOUT_CLK_SHIFT; |
| 4033 | if (host->timeout_clk == 0) { |
| 4034 | if (host->ops->get_timeout_clock) { |
| 4035 | host->timeout_clk = |
| 4036 | host->ops->get_timeout_clock(host); |
| 4037 | } else { |
| 4038 | pr_err("%s: Hardware doesn't specify timeout clock frequency.\n", |
| 4039 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4040 | ret = -ENODEV; |
| 4041 | goto undma; |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4042 | } |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 4043 | } |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 4044 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4045 | if (host->caps & SDHCI_TIMEOUT_CLK_UNIT) |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4046 | host->timeout_clk *= 1000; |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 4047 | |
Adrian Hunter | 9951362 | 2016-03-07 13:33:55 +0200 | [diff] [blame] | 4048 | if (override_timeout_clk) |
| 4049 | host->timeout_clk = override_timeout_clk; |
| 4050 | |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4051 | mmc->max_busy_timeout = host->ops->get_max_timeout_count ? |
Aisheng Dong | a6ff5ae | 2014-08-27 15:26:27 +0800 | [diff] [blame] | 4052 | host->ops->get_max_timeout_count(host) : 1 << 27; |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 4053 | mmc->max_busy_timeout /= host->timeout_clk; |
| 4054 | } |
Adrian Hunter | 58d1246 | 2011-06-28 17:16:03 +0300 | [diff] [blame] | 4055 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 4056 | mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23; |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 4057 | mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 4058 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 4059 | if (caps[0] & SDHCI_CAN_ASYNC_INT) |
| 4060 | mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE; |
| 4061 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 4062 | if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) |
| 4063 | host->flags |= SDHCI_AUTO_CMD12; |
Anton Vorontsov | 5fe23c7 | 2009-06-18 00:14:08 +0400 | [diff] [blame] | 4064 | |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 4065 | /* Auto-CMD23 stuff only works in ADMA or PIO. */ |
Andrei Warkentin | 4f3d3e9 | 2011-05-25 10:42:50 -0400 | [diff] [blame] | 4066 | if ((host->version >= SDHCI_SPEC_300) && |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 4067 | ((host->flags & SDHCI_USE_ADMA) || |
Scott Branden | 3bfa6f0 | 2015-02-09 16:06:28 -0800 | [diff] [blame] | 4068 | !(host->flags & SDHCI_USE_SDMA)) && |
| 4069 | !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) { |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 4070 | host->flags |= SDHCI_AUTO_CMD23; |
| 4071 | DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc)); |
| 4072 | } else { |
| 4073 | DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc)); |
| 4074 | } |
| 4075 | |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 4076 | /* |
| 4077 | * A controller may support 8-bit width, but the board itself |
| 4078 | * might not have the pins brought out. Boards that support |
| 4079 | * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in |
| 4080 | * their platform code before calling sdhci_add_host(), and we |
| 4081 | * won't assume 8-bit width for hosts without that CAP. |
| 4082 | */ |
Anton Vorontsov | 5fe23c7 | 2009-06-18 00:14:08 +0400 | [diff] [blame] | 4083 | if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA)) |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 4084 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4085 | |
Jerry Huang | 63ef5d8 | 2012-10-25 13:47:19 +0800 | [diff] [blame] | 4086 | if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23) |
| 4087 | mmc->caps &= ~MMC_CAP_CMD23; |
| 4088 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4089 | if (host->caps & SDHCI_CAN_DO_HISPD) |
Zhangfei Gao | a29e7e1 | 2010-08-16 21:15:32 -0400 | [diff] [blame] | 4090 | mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 4091 | |
Jaehoon Chung | 176d1ed | 2010-09-27 09:42:20 +0100 | [diff] [blame] | 4092 | if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) && |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 4093 | mmc_card_is_removable(mmc) && |
Guoping Yu | e3b17cf | 2014-08-20 16:42:55 +0800 | [diff] [blame] | 4094 | mmc_gpio_get_cd(host->mmc) < 0 && |
| 4095 | !(mmc->caps2 & MMC_CAP2_NONHOTPLUG)) |
Anton Vorontsov | 68d1fb7 | 2009-03-17 00:13:52 +0300 | [diff] [blame] | 4096 | mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 4097 | |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 4098 | /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */ |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4099 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 4100 | ret = regulator_enable(mmc->supply.vqmmc); |
| 4101 | if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000, |
| 4102 | 1950000)) |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4103 | host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | |
| 4104 | SDHCI_SUPPORT_SDR50 | |
| 4105 | SDHCI_SUPPORT_DDR50); |
Chris Ball | a3361ab | 2013-03-11 17:51:53 -0400 | [diff] [blame] | 4106 | if (ret) { |
| 4107 | pr_warn("%s: Failed to enable vqmmc regulator: %d\n", |
| 4108 | mmc_hostname(mmc), ret); |
Adrian Hunter | 4bb7431 | 2014-11-06 15:19:04 +0200 | [diff] [blame] | 4109 | mmc->supply.vqmmc = ERR_PTR(-EINVAL); |
Chris Ball | a3361ab | 2013-03-11 17:51:53 -0400 | [diff] [blame] | 4110 | } |
Kevin Liu | 8363c37 | 2012-11-17 17:55:51 -0500 | [diff] [blame] | 4111 | } |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 4112 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4113 | if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) { |
| 4114 | host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | |
| 4115 | SDHCI_SUPPORT_DDR50); |
| 4116 | } |
Daniel Drake | 6a66180 | 2012-11-25 13:01:19 -0500 | [diff] [blame] | 4117 | |
Al Cooper | 4188bba | 2012-03-16 15:54:17 -0400 | [diff] [blame] | 4118 | /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4119 | if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | |
| 4120 | SDHCI_SUPPORT_DDR50)) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4121 | mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; |
| 4122 | |
| 4123 | /* SDR104 supports also implies SDR50 support */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4124 | if (host->caps1 & SDHCI_SUPPORT_SDR104) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4125 | mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; |
Giuseppe CAVALLARO | 156e14b | 2013-06-12 08:16:38 +0200 | [diff] [blame] | 4126 | /* SD3.0: SDR104 is supported so (for eMMC) the caps2 |
| 4127 | * field can be promoted to support HS200. |
| 4128 | */ |
Adrian Hunter | 549c0b1 | 2014-11-06 15:19:05 +0200 | [diff] [blame] | 4129 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) |
David Cohen | 13868bf | 2013-10-29 10:58:26 -0700 | [diff] [blame] | 4130 | mmc->caps2 |= MMC_CAP2_HS200; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4131 | } else if (host->caps1 & SDHCI_SUPPORT_SDR50) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4132 | mmc->caps |= MMC_CAP_UHS_SDR50; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4133 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4134 | |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 4135 | if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 && |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4136 | (host->caps1 & SDHCI_SUPPORT_HS400)) |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 4137 | mmc->caps2 |= MMC_CAP2_HS400; |
| 4138 | |
Adrian Hunter | 549c0b1 | 2014-11-06 15:19:05 +0200 | [diff] [blame] | 4139 | if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) && |
| 4140 | (IS_ERR(mmc->supply.vqmmc) || |
| 4141 | !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000, |
| 4142 | 1300000))) |
| 4143 | mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V; |
| 4144 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4145 | if ((host->caps1 & SDHCI_SUPPORT_DDR50) && |
| 4146 | !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50)) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4147 | mmc->caps |= MMC_CAP_UHS_DDR50; |
| 4148 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 4149 | /* Does the host need tuning for SDR50? */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4150 | if (host->caps1 & SDHCI_USE_SDR50_TUNING) |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 4151 | host->flags |= SDHCI_SDR50_NEEDS_TUNING; |
| 4152 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4153 | /* Driver Type(s) (A, C, D) supported by the host */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4154 | if (host->caps1 & SDHCI_DRIVER_TYPE_A) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4155 | mmc->caps |= MMC_CAP_DRIVER_TYPE_A; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4156 | if (host->caps1 & SDHCI_DRIVER_TYPE_C) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4157 | mmc->caps |= MMC_CAP_DRIVER_TYPE_C; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4158 | if (host->caps1 & SDHCI_DRIVER_TYPE_D) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4159 | mmc->caps |= MMC_CAP_DRIVER_TYPE_D; |
| 4160 | |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4161 | /* Initial value for re-tuning timer count */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4162 | host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >> |
| 4163 | SDHCI_RETUNING_TIMER_COUNT_SHIFT; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4164 | |
| 4165 | /* |
| 4166 | * In case Re-tuning Timer is not disabled, the actual value of |
| 4167 | * re-tuning timer will be 2 ^ (n - 1). |
| 4168 | */ |
| 4169 | if (host->tuning_count) |
| 4170 | host->tuning_count = 1 << (host->tuning_count - 1); |
| 4171 | |
| 4172 | /* Re-tuning mode supported by the Host Controller */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4173 | host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >> |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4174 | SDHCI_RETUNING_MODE_SHIFT; |
| 4175 | |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4176 | ocr_avail = 0; |
Philip Rakity | bad37e1 | 2012-05-27 18:36:44 -0700 | [diff] [blame] | 4177 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4178 | /* |
| 4179 | * According to SD Host Controller spec v3.00, if the Host System |
| 4180 | * can afford more than 150mA, Host Driver should set XPC to 1. Also |
| 4181 | * the value is meaningful only if Voltage Support in the Capabilities |
| 4182 | * register is set. The actual current value is 4 times the register |
| 4183 | * value. |
| 4184 | */ |
| 4185 | max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT); |
Sahitya Tummala | cba7e83 | 2014-09-19 15:43:37 +0530 | [diff] [blame] | 4186 | if (!max_current_caps) { |
| 4187 | u32 curr = 0; |
Philip Rakity | bad37e1 | 2012-05-27 18:36:44 -0700 | [diff] [blame] | 4188 | |
Sahitya Tummala | cba7e83 | 2014-09-19 15:43:37 +0530 | [diff] [blame] | 4189 | if (!IS_ERR(mmc->supply.vmmc)) |
| 4190 | curr = regulator_get_current_limit(mmc->supply.vmmc); |
| 4191 | else if (host->ops->get_current_limit) |
| 4192 | curr = host->ops->get_current_limit(host); |
| 4193 | |
| 4194 | if (curr > 0) { |
Philip Rakity | bad37e1 | 2012-05-27 18:36:44 -0700 | [diff] [blame] | 4195 | /* convert to SDHCI_MAX_CURRENT format */ |
| 4196 | curr = curr/1000; /* convert to mA */ |
| 4197 | curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER; |
| 4198 | |
| 4199 | curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT); |
| 4200 | max_current_caps = |
| 4201 | (curr << SDHCI_MAX_CURRENT_330_SHIFT) | |
| 4202 | (curr << SDHCI_MAX_CURRENT_300_SHIFT) | |
| 4203 | (curr << SDHCI_MAX_CURRENT_180_SHIFT); |
| 4204 | } |
| 4205 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4206 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4207 | if (host->caps & SDHCI_CAN_VDD_330) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4208 | ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4209 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4210 | mmc->max_current_330 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4211 | SDHCI_MAX_CURRENT_330_MASK) >> |
| 4212 | SDHCI_MAX_CURRENT_330_SHIFT) * |
| 4213 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4214 | } |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4215 | if (host->caps & SDHCI_CAN_VDD_300) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4216 | ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4217 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4218 | mmc->max_current_300 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4219 | SDHCI_MAX_CURRENT_300_MASK) >> |
| 4220 | SDHCI_MAX_CURRENT_300_SHIFT) * |
| 4221 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4222 | } |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4223 | if (host->caps & SDHCI_CAN_VDD_180) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4224 | ocr_avail |= MMC_VDD_165_195; |
| 4225 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4226 | mmc->max_current_180 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4227 | SDHCI_MAX_CURRENT_180_MASK) >> |
| 4228 | SDHCI_MAX_CURRENT_180_SHIFT) * |
| 4229 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4230 | } |
| 4231 | |
Ulf Hansson | 5fd26c7 | 2015-06-05 11:40:08 +0200 | [diff] [blame] | 4232 | /* If OCR set by host, use it instead. */ |
| 4233 | if (host->ocr_mask) |
| 4234 | ocr_avail = host->ocr_mask; |
| 4235 | |
| 4236 | /* If OCR set by external regulators, give it highest prio. */ |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4237 | if (mmc->ocr_avail) |
Tim Kryger | 5222161 | 2014-06-25 00:25:34 -0700 | [diff] [blame] | 4238 | ocr_avail = mmc->ocr_avail; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4239 | |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4240 | mmc->ocr_avail = ocr_avail; |
| 4241 | mmc->ocr_avail_sdio = ocr_avail; |
| 4242 | if (host->ocr_avail_sdio) |
| 4243 | mmc->ocr_avail_sdio &= host->ocr_avail_sdio; |
| 4244 | mmc->ocr_avail_sd = ocr_avail; |
| 4245 | if (host->ocr_avail_sd) |
| 4246 | mmc->ocr_avail_sd &= host->ocr_avail_sd; |
| 4247 | else /* normal SD controllers don't support 1.8V */ |
| 4248 | mmc->ocr_avail_sd &= ~MMC_VDD_165_195; |
| 4249 | mmc->ocr_avail_mmc = ocr_avail; |
| 4250 | if (host->ocr_avail_mmc) |
| 4251 | mmc->ocr_avail_mmc &= host->ocr_avail_mmc; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 4252 | |
| 4253 | if (mmc->ocr_avail == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 4254 | pr_err("%s: Hardware doesn't report any support voltages.\n", |
| 4255 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4256 | ret = -ENODEV; |
| 4257 | goto unreg; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 4260 | if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | |
| 4261 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | |
| 4262 | MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) || |
| 4263 | (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V))) |
| 4264 | host->flags |= SDHCI_SIGNALING_180; |
| 4265 | |
| 4266 | if (mmc->caps2 & MMC_CAP2_HSX00_1_2V) |
| 4267 | host->flags |= SDHCI_SIGNALING_120; |
| 4268 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4269 | /* |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4270 | * Maximum number of segments. Depends on if the hardware |
| 4271 | * can do scatter/gather or not. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4272 | */ |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4273 | if (host->flags & SDHCI_USE_ADMA) |
Adrian Hunter | 4fb213f | 2014-11-04 12:42:43 +0200 | [diff] [blame] | 4274 | mmc->max_segs = SDHCI_MAX_SEGS; |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 4275 | else if (host->flags & SDHCI_USE_SDMA) |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 4276 | mmc->max_segs = 1; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4277 | else /* PIO */ |
Adrian Hunter | 4fb213f | 2014-11-04 12:42:43 +0200 | [diff] [blame] | 4278 | mmc->max_segs = SDHCI_MAX_SEGS; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4279 | |
| 4280 | /* |
Adrian Hunter | ac00531 | 2014-12-05 19:25:28 +0200 | [diff] [blame] | 4281 | * Maximum number of sectors in one transfer. Limited by SDMA boundary |
| 4282 | * size (512KiB). Note some tuning modes impose a 4MiB limit, but this |
| 4283 | * is less anyway. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4284 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4285 | mmc->max_req_size = 524288; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4286 | |
| 4287 | /* |
| 4288 | * Maximum segment size. Could be one segment with the maximum number |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4289 | * of bytes. When doing hardware scatter/gather, each entry cannot |
| 4290 | * be larger than 64 KiB though. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4291 | */ |
Olof Johansson | 30652aa | 2011-01-01 18:37:32 -0600 | [diff] [blame] | 4292 | if (host->flags & SDHCI_USE_ADMA) { |
| 4293 | if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC) |
| 4294 | mmc->max_seg_size = 65535; |
| 4295 | else |
| 4296 | mmc->max_seg_size = 65536; |
| 4297 | } else { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4298 | mmc->max_seg_size = mmc->max_req_size; |
Olof Johansson | 30652aa | 2011-01-01 18:37:32 -0600 | [diff] [blame] | 4299 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4300 | |
| 4301 | /* |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 4302 | * Maximum block size. This varies from controller to controller and |
| 4303 | * is specified in the capabilities register. |
| 4304 | */ |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4305 | if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) { |
| 4306 | mmc->max_blk_size = 2; |
| 4307 | } else { |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4308 | mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >> |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4309 | SDHCI_MAX_BLOCK_SHIFT; |
| 4310 | if (mmc->max_blk_size >= 3) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 4311 | pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n", |
| 4312 | mmc_hostname(mmc)); |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4313 | mmc->max_blk_size = 0; |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | mmc->max_blk_size = 512 << mmc->max_blk_size; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 4318 | |
| 4319 | /* |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4320 | * Maximum block count. |
| 4321 | */ |
Ben Dooks | 1388eef | 2009-06-14 12:40:53 +0100 | [diff] [blame] | 4322 | mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4323 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4324 | return 0; |
| 4325 | |
| 4326 | unreg: |
| 4327 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4328 | regulator_disable(mmc->supply.vqmmc); |
| 4329 | undma: |
| 4330 | if (host->align_buffer) |
| 4331 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4332 | host->adma_table_sz, host->align_buffer, |
| 4333 | host->align_addr); |
| 4334 | host->adma_table = NULL; |
| 4335 | host->align_buffer = NULL; |
| 4336 | |
| 4337 | return ret; |
| 4338 | } |
| 4339 | EXPORT_SYMBOL_GPL(sdhci_setup_host); |
| 4340 | |
| 4341 | int __sdhci_add_host(struct sdhci_host *host) |
| 4342 | { |
| 4343 | struct mmc_host *mmc = host->mmc; |
| 4344 | int ret; |
| 4345 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4346 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4347 | * Init tasklets. |
| 4348 | */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4349 | tasklet_init(&host->finish_tasklet, |
| 4350 | sdhci_tasklet_finish, (unsigned long)host); |
| 4351 | |
Al Viro | e4cad1b | 2006-10-10 22:47:07 +0100 | [diff] [blame] | 4352 | setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 4353 | setup_timer(&host->data_timer, sdhci_timeout_data_timer, |
| 4354 | (unsigned long)host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4355 | |
Adrian Hunter | 250fb7b4 | 2014-12-05 19:41:10 +0200 | [diff] [blame] | 4356 | init_waitqueue_head(&host->buf_ready_int); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 4357 | |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 4358 | host->flags |= SDHCI_HOST_IRQ_STATUS; |
| 4359 | |
Shawn Guo | 2af502c | 2013-07-05 14:38:55 +0800 | [diff] [blame] | 4360 | sdhci_init(host, 0); |
| 4361 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 4362 | ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq, |
| 4363 | IRQF_SHARED, mmc_hostname(mmc), host); |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4364 | if (ret) { |
| 4365 | pr_err("%s: Failed to request IRQ %d: %d\n", |
| 4366 | mmc_hostname(mmc), host->irq, ret); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 4367 | goto untasklet; |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4368 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4369 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4370 | #ifdef CONFIG_MMC_DEBUG |
| 4371 | sdhci_dumpregs(host); |
| 4372 | #endif |
| 4373 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4374 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) { |
| 4375 | ret = sdhci_led_register(host); |
| 4376 | if (ret) { |
| 4377 | pr_err("%s: Failed to register LED device: %d\n", |
| 4378 | mmc_hostname(mmc), ret); |
| 4379 | goto unirq; |
| 4380 | } |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4381 | } |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4382 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 4383 | mmiowb(); |
| 4384 | |
Asutosh Das | 214b966 | 2013-06-13 14:27:42 +0530 | [diff] [blame] | 4385 | if (host->quirks2 & SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR) { |
| 4386 | host->ier = (host->ier & ~SDHCI_INT_DATA_END_BIT); |
| 4387 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 4388 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 4389 | } |
| 4390 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 4391 | if (mmc->caps2 & MMC_CAP2_CMD_QUEUE) { |
Subhash Jadavani | 2147ba9 | 2016-05-11 16:35:47 -0700 | [diff] [blame] | 4392 | bool dma64 = (host->flags & SDHCI_USE_64_BIT_DMA) ? |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 4393 | true : false; |
| 4394 | ret = sdhci_cmdq_init(host, mmc, dma64); |
| 4395 | if (ret) |
| 4396 | pr_err("%s: CMDQ init: failed (%d)\n", |
| 4397 | mmc_hostname(host->mmc), ret); |
| 4398 | else |
| 4399 | host->cq_host->ops = &sdhci_cmdq_ops; |
| 4400 | } |
| 4401 | |
| 4402 | pr_info("%s: SDHCI controller on %s [%s] using %s in %s mode\n", |
| 4403 | mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)), |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 4404 | (host->flags & SDHCI_USE_ADMA) ? |
Subhash Jadavani | 2147ba9 | 2016-05-11 16:35:47 -0700 | [diff] [blame] | 4405 | ((host->flags & SDHCI_USE_64_BIT_DMA) ? |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 4406 | "64-bit ADMA" : "32-bit ADMA") : |
| 4407 | ((host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO"), |
| 4408 | ((mmc->caps2 & MMC_CAP2_CMD_QUEUE) && !ret) ? |
| 4409 | "CMDQ" : "legacy"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4410 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 4411 | sdhci_enable_card_detection(host); |
| 4412 | |
Venkat Gopalakrishnan | 0c3a9e4 | 2014-12-15 17:45:03 -0800 | [diff] [blame] | 4413 | ret = mmc_add_host(mmc); |
| 4414 | if (ret) |
| 4415 | goto unled; |
| 4416 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4417 | return 0; |
| 4418 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4419 | unled: |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4420 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 4421 | sdhci_led_unregister(host); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4422 | unirq: |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 4423 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 4424 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 4425 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4426 | free_irq(host->irq, host); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 4427 | untasklet: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4428 | tasklet_kill(&host->finish_tasklet); |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4429 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4430 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4431 | regulator_disable(mmc->supply.vqmmc); |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4432 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4433 | if (host->align_buffer) |
| 4434 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4435 | host->adma_table_sz, host->align_buffer, |
| 4436 | host->align_addr); |
| 4437 | host->adma_table = NULL; |
| 4438 | host->align_buffer = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4439 | |
| 4440 | return ret; |
| 4441 | } |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4442 | EXPORT_SYMBOL_GPL(__sdhci_add_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4443 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4444 | int sdhci_add_host(struct sdhci_host *host) |
| 4445 | { |
| 4446 | int ret; |
| 4447 | |
| 4448 | ret = sdhci_setup_host(host); |
| 4449 | if (ret) |
| 4450 | return ret; |
| 4451 | |
| 4452 | return __sdhci_add_host(host); |
| 4453 | } |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4454 | EXPORT_SYMBOL_GPL(sdhci_add_host); |
| 4455 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4456 | void sdhci_remove_host(struct sdhci_host *host, int dead) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4457 | { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4458 | struct mmc_host *mmc = host->mmc; |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4459 | unsigned long flags; |
| 4460 | |
| 4461 | if (dead) { |
| 4462 | spin_lock_irqsave(&host->lock, flags); |
| 4463 | |
| 4464 | host->flags |= SDHCI_DEVICE_DEAD; |
| 4465 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 4466 | if (sdhci_has_requests(host)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4467 | pr_err("%s: Controller removed during " |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 4468 | " transfer!\n", mmc_hostname(mmc)); |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 4469 | sdhci_error_out_mrqs(host, -ENOMEDIUM); |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | spin_unlock_irqrestore(&host->lock, flags); |
| 4473 | } |
| 4474 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 4475 | sdhci_disable_card_detection(host); |
| 4476 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 4477 | mmc_remove_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4478 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4479 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 4480 | sdhci_led_unregister(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4481 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4482 | if (!dead) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 4483 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4484 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 4485 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 4486 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4487 | free_irq(host->irq, host); |
| 4488 | |
| 4489 | del_timer_sync(&host->timer); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 4490 | del_timer_sync(&host->data_timer); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4491 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4492 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4493 | |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4494 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4495 | regulator_disable(mmc->supply.vqmmc); |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 4496 | |
Russell King | edd63fc | 2016-01-26 13:39:50 +0000 | [diff] [blame] | 4497 | if (host->align_buffer) |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 4498 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4499 | host->adma_table_sz, host->align_buffer, |
| 4500 | host->align_addr); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4501 | |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 4502 | host->adma_table = NULL; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4503 | host->align_buffer = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4504 | } |
| 4505 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4506 | EXPORT_SYMBOL_GPL(sdhci_remove_host); |
| 4507 | |
| 4508 | void sdhci_free_host(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4509 | { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4510 | mmc_free_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4513 | EXPORT_SYMBOL_GPL(sdhci_free_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4514 | |
| 4515 | /*****************************************************************************\ |
| 4516 | * * |
| 4517 | * Driver init/exit * |
| 4518 | * * |
| 4519 | \*****************************************************************************/ |
| 4520 | |
| 4521 | static int __init sdhci_drv_init(void) |
| 4522 | { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4523 | pr_info(DRIVER_NAME |
Pierre Ossman | 52fbf9c | 2007-02-09 08:23:41 +0100 | [diff] [blame] | 4524 | ": Secure Digital Host Controller Interface driver\n"); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4525 | pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4526 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4527 | return 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | static void __exit sdhci_drv_exit(void) |
| 4531 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | module_init(sdhci_drv_init); |
| 4535 | module_exit(sdhci_drv_exit); |
| 4536 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4537 | module_param(debug_quirks, uint, 0444); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 4538 | module_param(debug_quirks2, uint, 0444); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4539 | |
Pierre Ossman | 32710e8 | 2009-04-08 20:14:54 +0200 | [diff] [blame] | 4540 | MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>"); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4541 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4542 | MODULE_LICENSE("GPL"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4543 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4544 | MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 4545 | MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks."); |