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