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