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