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