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