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