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