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