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