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