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