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