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