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