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