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