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