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