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 | 473b095a | 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> |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 33 | #include <linux/mmc/sdio.h> |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 34 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 35 | #include <trace/events/mmc.h> |
| 36 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 37 | #include "sdhci.h" |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 38 | #include "cmdq_hci.h" |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_NAME "sdhci" |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 41 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 42 | #define DBG(f, x...) \ |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 43 | pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 44 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 45 | #define MAX_TUNING_LOOP 40 |
| 46 | |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 47 | #define SDHCI_DBG_DUMP_RS_INTERVAL (10 * HZ) |
| 48 | #define SDHCI_DBG_DUMP_RS_BURST 2 |
| 49 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 50 | static unsigned int debug_quirks = 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 51 | static unsigned int debug_quirks2; |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 52 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 53 | static void sdhci_finish_data(struct sdhci_host *); |
| 54 | |
Sahitya Tummala | ea4e3aa | 2013-05-24 14:08:10 +0530 | [diff] [blame] | 55 | static bool sdhci_check_state(struct sdhci_host *); |
| 56 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 57 | static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 58 | |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 59 | #ifdef CONFIG_PM |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 60 | static int sdhci_runtime_pm_get(struct sdhci_host *host); |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 61 | static int sdhci_runtime_pm_put(struct sdhci_host *host); |
| 62 | #else |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 63 | static inline int sdhci_runtime_pm_get(struct sdhci_host *host) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 67 | static inline int sdhci_runtime_pm_put(struct sdhci_host *host) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | #endif |
| 72 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 73 | static void sdhci_dump_state(struct sdhci_host *host) |
| 74 | { |
| 75 | struct mmc_host *mmc = host->mmc; |
| 76 | |
| 77 | #ifdef CONFIG_MMC_CLKGATE |
| 78 | pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n", |
| 79 | mmc_hostname(mmc), host->clock, mmc->clk_gated, |
| 80 | mmc->claimer->comm, host->pwr); |
| 81 | #else |
| 82 | pr_info("%s: clk: %d claimer: %s pwr: %d\n", |
| 83 | mmc_hostname(mmc), host->clock, |
| 84 | mmc->claimer->comm, host->pwr); |
| 85 | #endif |
| 86 | pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n", |
| 87 | mmc_hostname(mmc), mmc->parent->power.runtime_status, |
| 88 | atomic_read(&mmc->parent->power.usage_count), |
| 89 | mmc->parent->power.disable_depth); |
| 90 | } |
| 91 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 92 | static void sdhci_dumpregs(struct sdhci_host *host) |
| 93 | { |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 94 | pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n", |
| 95 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 96 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 97 | pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 98 | sdhci_readl(host, SDHCI_DMA_ADDRESS), |
| 99 | sdhci_readw(host, SDHCI_HOST_VERSION)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 100 | pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 101 | sdhci_readw(host, SDHCI_BLOCK_SIZE), |
| 102 | sdhci_readw(host, SDHCI_BLOCK_COUNT)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 103 | pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 104 | sdhci_readl(host, SDHCI_ARGUMENT), |
| 105 | sdhci_readw(host, SDHCI_TRANSFER_MODE)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 106 | pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 107 | sdhci_readl(host, SDHCI_PRESENT_STATE), |
| 108 | sdhci_readb(host, SDHCI_HOST_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 109 | pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 110 | sdhci_readb(host, SDHCI_POWER_CONTROL), |
| 111 | sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 112 | pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 113 | sdhci_readb(host, SDHCI_WAKE_UP_CONTROL), |
| 114 | sdhci_readw(host, SDHCI_CLOCK_CONTROL)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 115 | pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 116 | sdhci_readb(host, SDHCI_TIMEOUT_CONTROL), |
| 117 | sdhci_readl(host, SDHCI_INT_STATUS)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 118 | pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 119 | sdhci_readl(host, SDHCI_INT_ENABLE), |
| 120 | sdhci_readl(host, SDHCI_SIGNAL_ENABLE)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 121 | pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n", |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 122 | host->auto_cmd_err_sts, |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 123 | sdhci_readw(host, SDHCI_SLOT_INT_STATUS)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 124 | pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 125 | sdhci_readl(host, SDHCI_CAPABILITIES), |
| 126 | sdhci_readl(host, SDHCI_CAPABILITIES_1)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 127 | pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 128 | sdhci_readw(host, SDHCI_COMMAND), |
| 129 | sdhci_readl(host, SDHCI_MAX_CURRENT)); |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 130 | pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n", |
| 131 | sdhci_readl(host, SDHCI_RESPONSE + 0x4), |
| 132 | sdhci_readl(host, SDHCI_RESPONSE)); |
| 133 | pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n", |
| 134 | sdhci_readl(host, SDHCI_RESPONSE + 0xC), |
| 135 | sdhci_readl(host, SDHCI_RESPONSE + 0x8)); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 136 | pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 137 | sdhci_readw(host, SDHCI_HOST_CONTROL2)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 138 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 139 | if (host->flags & SDHCI_USE_ADMA) { |
| 140 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 141 | pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 142 | readl(host->ioaddr + SDHCI_ADMA_ERROR), |
| 143 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI), |
| 144 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS)); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 145 | else |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 146 | pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n", |
Chuanxiao Dong | a7c5367 | 2016-06-22 14:40:01 +0300 | [diff] [blame] | 147 | readl(host->ioaddr + SDHCI_ADMA_ERROR), |
| 148 | readl(host->ioaddr + SDHCI_ADMA_ADDRESS)); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 149 | } |
Ben Dooks | be3f4ae | 2009-06-08 23:33:52 +0100 | [diff] [blame] | 150 | |
Sahitya Tummala | 91d315e | 2013-08-02 09:17:54 +0530 | [diff] [blame] | 151 | if (host->ops->dump_vendor_regs) |
| 152 | host->ops->dump_vendor_regs(host); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 153 | sdhci_dump_state(host); |
| 154 | pr_info(DRIVER_NAME ": ===========================================\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /*****************************************************************************\ |
| 158 | * * |
| 159 | * Low level functions * |
| 160 | * * |
| 161 | \*****************************************************************************/ |
| 162 | |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 163 | static inline bool sdhci_data_line_cmd(struct mmc_command *cmd) |
| 164 | { |
| 165 | return cmd->data || cmd->flags & MMC_RSP_BUSY; |
| 166 | } |
| 167 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 168 | static void sdhci_set_card_detection(struct sdhci_host *host, bool enable) |
| 169 | { |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 170 | u32 present; |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 171 | |
Adrian Hunter | c79396c | 2011-12-27 15:48:42 +0200 | [diff] [blame] | 172 | if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) || |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 173 | !mmc_card_is_removable(host->mmc)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 174 | return; |
| 175 | |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 176 | if (enable) { |
| 177 | present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 178 | SDHCI_CARD_PRESENT; |
Shawn Guo | d25928d | 2011-06-21 22:41:48 +0800 | [diff] [blame] | 179 | |
Russell King | 5b4f1f6 | 2014-04-25 12:57:02 +0100 | [diff] [blame] | 180 | host->ier |= present ? SDHCI_INT_CARD_REMOVE : |
| 181 | SDHCI_INT_CARD_INSERT; |
| 182 | } else { |
| 183 | host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT); |
| 184 | } |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 185 | |
| 186 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 187 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void sdhci_enable_card_detection(struct sdhci_host *host) |
| 191 | { |
| 192 | sdhci_set_card_detection(host, true); |
| 193 | } |
| 194 | |
| 195 | static void sdhci_disable_card_detection(struct sdhci_host *host) |
| 196 | { |
| 197 | sdhci_set_card_detection(host, false); |
| 198 | } |
| 199 | |
Ulf Hansson | 02d0b68 | 2016-04-11 15:32:41 +0200 | [diff] [blame] | 200 | static void sdhci_runtime_pm_bus_on(struct sdhci_host *host) |
| 201 | { |
| 202 | if (host->bus_on) |
| 203 | return; |
| 204 | host->bus_on = true; |
| 205 | pm_runtime_get_noresume(host->mmc->parent); |
| 206 | } |
| 207 | |
| 208 | static void sdhci_runtime_pm_bus_off(struct sdhci_host *host) |
| 209 | { |
| 210 | if (!host->bus_on) |
| 211 | return; |
| 212 | host->bus_on = false; |
| 213 | pm_runtime_put_noidle(host->mmc->parent); |
| 214 | } |
| 215 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 216 | void sdhci_reset(struct sdhci_host *host, u8 mask) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 217 | { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 218 | unsigned long timeout; |
Philip Rakity | 393c1a3 | 2011-01-21 11:26:40 -0800 | [diff] [blame] | 219 | |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 220 | retry_reset: |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 221 | sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 222 | |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 223 | if (mask & SDHCI_RESET_ALL) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 224 | host->clock = 0; |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 225 | /* Reset-all turns off SD Bus Power */ |
| 226 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 227 | sdhci_runtime_pm_bus_off(host); |
| 228 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 229 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 230 | /* Wait max 100 ms */ |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 231 | timeout = 100000; |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 232 | |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 233 | if (host->ops->check_power_status && host->pwr && |
| 234 | (mask & SDHCI_RESET_ALL)) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 235 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 236 | |
Sujit Reddy Thumma | 23dd7f8 | 2014-02-14 08:37:47 +0530 | [diff] [blame] | 237 | /* clear pending normal/error interrupt status */ |
| 238 | sdhci_writel(host, sdhci_readl(host, SDHCI_INT_STATUS), |
| 239 | SDHCI_INT_STATUS); |
| 240 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 241 | /* hw clears the bit when it's done */ |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 242 | while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) { |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 243 | if (timeout == 0) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 244 | pr_err("%s: Reset 0x%x never completed.\n", |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 245 | mmc_hostname(host->mmc), (int)mask); |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 246 | if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND) |
| 247 | && host->ops->reset_workaround) { |
| 248 | if (!host->reset_wa_applied) { |
| 249 | /* |
| 250 | * apply the workaround and issue |
| 251 | * reset again. |
| 252 | */ |
| 253 | host->ops->reset_workaround(host, 1); |
| 254 | host->reset_wa_applied = 1; |
| 255 | host->reset_wa_cnt++; |
| 256 | goto retry_reset; |
| 257 | } else { |
| 258 | pr_err("%s: Reset 0x%x failed with workaround\n", |
| 259 | mmc_hostname(host->mmc), |
| 260 | (int)mask); |
| 261 | /* clear the workaround */ |
| 262 | host->ops->reset_workaround(host, 0); |
| 263 | host->reset_wa_applied = 0; |
| 264 | } |
| 265 | } |
| 266 | |
Pierre Ossman | e16514d8 | 2006-06-30 02:22:24 -0700 | [diff] [blame] | 267 | sdhci_dumpregs(host); |
| 268 | return; |
| 269 | } |
| 270 | timeout--; |
| 271 | mdelay(1); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 272 | } |
Pavan Anamula | e1ec2a7 | 2015-08-25 15:00:25 +0530 | [diff] [blame] | 273 | |
| 274 | if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND) && |
| 275 | host->ops->reset_workaround && host->reset_wa_applied) { |
| 276 | pr_info("%s: Reset 0x%x successful with workaround\n", |
| 277 | mmc_hostname(host->mmc), (int)mask); |
| 278 | /* clear the workaround */ |
| 279 | host->ops->reset_workaround(host, 0); |
| 280 | host->reset_wa_applied = 0; |
| 281 | } |
| 282 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 283 | } |
| 284 | EXPORT_SYMBOL_GPL(sdhci_reset); |
Anton Vorontsov | 063a9db | 2009-03-17 00:14:02 +0300 | [diff] [blame] | 285 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 286 | static void sdhci_do_reset(struct sdhci_host *host, u8 mask) |
| 287 | { |
| 288 | if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 289 | struct mmc_host *mmc = host->mmc; |
| 290 | |
| 291 | if (!mmc->ops->get_cd(mmc)) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | host->ops->reset(host, mask); |
Philip Rakity | 393c1a3 | 2011-01-21 11:26:40 -0800 | [diff] [blame] | 296 | |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 297 | if (mask & SDHCI_RESET_ALL) { |
| 298 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
| 299 | if (host->ops->enable_dma) |
| 300 | host->ops->enable_dma(host); |
| 301 | } |
| 302 | |
| 303 | /* Resetting the controller clears many */ |
| 304 | host->preset_enabled = false; |
Shaohui Xie | 3abc1e80 | 2011-12-29 16:33:00 +0800 | [diff] [blame] | 305 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 308 | static void sdhci_init(struct sdhci_host *host, int soft) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 309 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 310 | struct mmc_host *mmc = host->mmc; |
| 311 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 312 | if (soft) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 313 | sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 314 | else |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 315 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 316 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 317 | host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 318 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | |
| 319 | SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC | |
| 320 | SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 321 | SDHCI_INT_RESPONSE | SDHCI_INT_AUTO_CMD_ERR; |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 322 | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 323 | if (host->tuning_mode == SDHCI_TUNING_MODE_2 || |
| 324 | host->tuning_mode == SDHCI_TUNING_MODE_3) |
| 325 | host->ier |= SDHCI_INT_RETUNE; |
| 326 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 327 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 328 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 329 | |
| 330 | if (soft) { |
| 331 | /* force clock reconfiguration */ |
| 332 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 333 | mmc->ops->set_ios(mmc, &mmc->ios); |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 334 | } |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 335 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 336 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 337 | static void sdhci_reinit(struct sdhci_host *host) |
| 338 | { |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 339 | sdhci_init(host, 0); |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 340 | sdhci_enable_card_detection(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 343 | static void __sdhci_led_activate(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 344 | { |
| 345 | u8 ctrl; |
| 346 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 347 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 348 | ctrl |= SDHCI_CTRL_LED; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 349 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 352 | static void __sdhci_led_deactivate(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 353 | { |
| 354 | u8 ctrl; |
| 355 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 356 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 357 | ctrl &= ~SDHCI_CTRL_LED; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 358 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Masahiro Yamada | 4f78230 | 2016-04-14 13:19:39 +0900 | [diff] [blame] | 361 | #if IS_REACHABLE(CONFIG_LEDS_CLASS) |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 362 | static void sdhci_led_control(struct led_classdev *led, |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 363 | enum led_brightness brightness) |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 364 | { |
| 365 | struct sdhci_host *host = container_of(led, struct sdhci_host, led); |
| 366 | unsigned long flags; |
| 367 | |
Sahitya Tummala | e8b0de9 | 2014-04-07 10:33:11 +0530 | [diff] [blame] | 368 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 369 | return; |
| 370 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 371 | spin_lock_irqsave(&host->lock, flags); |
| 372 | |
Sahitya Tummala | ea4e3aa | 2013-05-24 14:08:10 +0530 | [diff] [blame] | 373 | if (host->runtime_suspended || sdhci_check_state(host)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 374 | goto out; |
| 375 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 376 | if (brightness == LED_OFF) |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 377 | __sdhci_led_deactivate(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 378 | else |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 379 | __sdhci_led_activate(host); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 380 | out: |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 381 | spin_unlock_irqrestore(&host->lock, flags); |
| 382 | } |
Adrian Hunter | 061d17a | 2016-04-12 14:25:09 +0300 | [diff] [blame] | 383 | |
| 384 | static int sdhci_led_register(struct sdhci_host *host) |
| 385 | { |
| 386 | struct mmc_host *mmc = host->mmc; |
| 387 | |
| 388 | snprintf(host->led_name, sizeof(host->led_name), |
| 389 | "%s::", mmc_hostname(mmc)); |
| 390 | |
| 391 | host->led.name = host->led_name; |
| 392 | host->led.brightness = LED_OFF; |
| 393 | host->led.default_trigger = mmc_hostname(mmc); |
| 394 | host->led.brightness_set = sdhci_led_control; |
| 395 | |
| 396 | return led_classdev_register(mmc_dev(mmc), &host->led); |
| 397 | } |
| 398 | |
| 399 | static void sdhci_led_unregister(struct sdhci_host *host) |
| 400 | { |
| 401 | led_classdev_unregister(&host->led); |
| 402 | } |
| 403 | |
| 404 | static inline void sdhci_led_activate(struct sdhci_host *host) |
| 405 | { |
| 406 | } |
| 407 | |
| 408 | static inline void sdhci_led_deactivate(struct sdhci_host *host) |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | #else |
| 413 | |
| 414 | static inline int sdhci_led_register(struct sdhci_host *host) |
| 415 | { |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static inline void sdhci_led_unregister(struct sdhci_host *host) |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | static inline void sdhci_led_activate(struct sdhci_host *host) |
| 424 | { |
| 425 | __sdhci_led_activate(host); |
| 426 | } |
| 427 | |
| 428 | static inline void sdhci_led_deactivate(struct sdhci_host *host) |
| 429 | { |
| 430 | __sdhci_led_deactivate(host); |
| 431 | } |
| 432 | |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 433 | #endif |
| 434 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 435 | /*****************************************************************************\ |
| 436 | * * |
| 437 | * Core functions * |
| 438 | * * |
| 439 | \*****************************************************************************/ |
| 440 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 441 | static void sdhci_read_block_pio(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 442 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 443 | unsigned long flags; |
| 444 | size_t blksize, len, chunk; |
Steven Noonan | 7244b85 | 2008-10-01 01:50:25 -0700 | [diff] [blame] | 445 | u32 uninitialized_var(scratch); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 446 | u8 *buf; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 447 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 448 | DBG("PIO reading\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 449 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 450 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 451 | chunk = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 452 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 453 | local_irq_save(flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 454 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 455 | while (blksize) { |
Fabio Estevam | bf3a35a | 2015-05-09 18:44:51 -0300 | [diff] [blame] | 456 | BUG_ON(!sg_miter_next(&host->sg_miter)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 457 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 458 | len = min(host->sg_miter.length, blksize); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 459 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 460 | blksize -= len; |
| 461 | host->sg_miter.consumed = len; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 462 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 463 | buf = host->sg_miter.addr; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 464 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 465 | while (len) { |
| 466 | if (chunk == 0) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 467 | scratch = sdhci_readl(host, SDHCI_BUFFER); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 468 | chunk = 4; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 469 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 470 | |
| 471 | *buf = scratch & 0xFF; |
| 472 | |
| 473 | buf++; |
| 474 | scratch >>= 8; |
| 475 | chunk--; |
| 476 | len--; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 479 | |
| 480 | sg_miter_stop(&host->sg_miter); |
| 481 | |
| 482 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 483 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 484 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 485 | static void sdhci_write_block_pio(struct sdhci_host *host) |
| 486 | { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 487 | unsigned long flags; |
| 488 | size_t blksize, len, chunk; |
| 489 | u32 scratch; |
| 490 | u8 *buf; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 491 | |
| 492 | DBG("PIO writing\n"); |
| 493 | |
| 494 | blksize = host->data->blksz; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 495 | chunk = 0; |
| 496 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 497 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 498 | local_irq_save(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 499 | |
| 500 | while (blksize) { |
Fabio Estevam | bf3a35a | 2015-05-09 18:44:51 -0300 | [diff] [blame] | 501 | BUG_ON(!sg_miter_next(&host->sg_miter)); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 502 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 503 | len = min(host->sg_miter.length, blksize); |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 504 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 505 | blksize -= len; |
| 506 | host->sg_miter.consumed = len; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 507 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 508 | buf = host->sg_miter.addr; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 509 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 510 | while (len) { |
| 511 | scratch |= (u32)*buf << (chunk * 8); |
| 512 | |
| 513 | buf++; |
| 514 | chunk++; |
| 515 | len--; |
| 516 | |
| 517 | if ((chunk == 4) || ((len == 0) && (blksize == 0))) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 518 | sdhci_writel(host, scratch, SDHCI_BUFFER); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 519 | chunk = 0; |
| 520 | scratch = 0; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 521 | } |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 522 | } |
| 523 | } |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 524 | |
| 525 | sg_miter_stop(&host->sg_miter); |
| 526 | |
| 527 | local_irq_restore(flags); |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void sdhci_transfer_pio(struct sdhci_host *host) |
| 531 | { |
| 532 | u32 mask; |
| 533 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 534 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 535 | return; |
| 536 | |
| 537 | if (host->data->flags & MMC_DATA_READ) |
| 538 | mask = SDHCI_DATA_AVAILABLE; |
| 539 | else |
| 540 | mask = SDHCI_SPACE_AVAILABLE; |
| 541 | |
Pierre Ossman | 4a3cba3 | 2008-07-29 00:11:16 +0200 | [diff] [blame] | 542 | /* |
| 543 | * Some controllers (JMicron JMB38x) mess up the buffer bits |
| 544 | * for transfers < 4 bytes. As long as it is just one block, |
| 545 | * we can ignore the bits. |
| 546 | */ |
| 547 | if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) && |
| 548 | (host->data->blocks == 1)) |
| 549 | mask = ~0; |
| 550 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 551 | while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { |
Anton Vorontsov | 3e3bf20 | 2009-03-17 00:14:00 +0300 | [diff] [blame] | 552 | if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY) |
| 553 | udelay(100); |
| 554 | |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 555 | if (host->data->flags & MMC_DATA_READ) |
| 556 | sdhci_read_block_pio(host); |
| 557 | else |
| 558 | sdhci_write_block_pio(host); |
| 559 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 560 | host->blocks--; |
| 561 | if (host->blocks == 0) |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 562 | break; |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | DBG("PIO transfer complete.\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 568 | static int sdhci_pre_dma_transfer(struct sdhci_host *host, |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 569 | struct mmc_data *data, int cookie) |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 570 | { |
| 571 | int sg_count; |
| 572 | |
Russell King | 94538e5 | 2016-01-26 13:40:37 +0000 | [diff] [blame] | 573 | /* |
| 574 | * If the data buffers are already mapped, return the previous |
| 575 | * dma_map_sg() result. |
| 576 | */ |
| 577 | if (data->host_cookie == COOKIE_PRE_MAPPED) |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 578 | return data->sg_count; |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 579 | |
| 580 | sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 581 | data->flags & MMC_DATA_WRITE ? |
| 582 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 583 | |
| 584 | if (sg_count == 0) |
| 585 | return -ENOSPC; |
| 586 | |
| 587 | data->sg_count = sg_count; |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 588 | data->host_cookie = cookie; |
Russell King | 48857d9 | 2016-01-26 13:40:16 +0000 | [diff] [blame] | 589 | |
| 590 | return sg_count; |
| 591 | } |
| 592 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 593 | static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags) |
| 594 | { |
| 595 | local_irq_save(*flags); |
Cong Wang | 482fce9 | 2011-11-27 13:27:00 +0800 | [diff] [blame] | 596 | return kmap_atomic(sg_page(sg)) + sg->offset; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags) |
| 600 | { |
Cong Wang | 482fce9 | 2011-11-27 13:27:00 +0800 | [diff] [blame] | 601 | kunmap_atomic(buffer); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 602 | local_irq_restore(*flags); |
| 603 | } |
| 604 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 605 | static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc, |
| 606 | dma_addr_t addr, int len, unsigned cmd) |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 607 | { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 608 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 609 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 610 | /* 32-bit and 64-bit descriptors have these members in same position */ |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 611 | dma_desc->cmd = cpu_to_le16(cmd); |
| 612 | dma_desc->len = cpu_to_le16(len); |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 613 | dma_desc->addr_lo = cpu_to_le32((u32)addr); |
| 614 | |
| 615 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 616 | dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32); |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 619 | static void sdhci_adma_mark_end(void *desc) |
| 620 | { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 621 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 622 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 623 | /* 32-bit and 64-bit descriptors have 'cmd' in same position */ |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 624 | dma_desc->cmd |= cpu_to_le16(ADMA2_END); |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 627 | static void sdhci_adma_table_pre(struct sdhci_host *host, |
| 628 | struct mmc_data *data, int sg_count) |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 629 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 630 | struct scatterlist *sg; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 631 | unsigned long flags; |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 632 | dma_addr_t addr, align_addr; |
| 633 | void *desc, *align; |
| 634 | char *buffer; |
| 635 | int len, offset, i; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 636 | |
| 637 | /* |
| 638 | * The spec does not specify endianness of descriptor table. |
| 639 | * We currently guess that it is LE. |
| 640 | */ |
| 641 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 642 | host->sg_count = sg_count; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 643 | |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 644 | desc = host->adma_table; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 645 | align = host->align_buffer; |
| 646 | |
| 647 | align_addr = host->align_addr; |
| 648 | |
| 649 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 650 | addr = sg_dma_address(sg); |
| 651 | len = sg_dma_len(sg); |
| 652 | |
| 653 | /* |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 654 | * The SDHCI specification states that ADMA addresses must |
| 655 | * be 32-bit aligned. If they aren't, then we use a bounce |
| 656 | * buffer for the (up to three) bytes that screw up the |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 657 | * alignment. |
| 658 | */ |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 659 | offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) & |
| 660 | SDHCI_ADMA2_MASK; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 661 | if (offset) { |
| 662 | if (data->flags & MMC_DATA_WRITE) { |
| 663 | buffer = sdhci_kmap_atomic(sg, &flags); |
| 664 | memcpy(align, buffer, offset); |
| 665 | sdhci_kunmap_atomic(buffer, &flags); |
| 666 | } |
| 667 | |
Ben Dooks | 118cd17 | 2010-03-05 13:43:26 -0800 | [diff] [blame] | 668 | /* tran, valid */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 669 | sdhci_adma_write_desc(host, desc, align_addr, offset, |
Adrian Hunter | 739d46d | 2014-11-04 12:42:44 +0200 | [diff] [blame] | 670 | ADMA2_TRAN_VALID); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 671 | |
| 672 | BUG_ON(offset > 65536); |
| 673 | |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 674 | align += SDHCI_ADMA2_ALIGN; |
| 675 | align_addr += SDHCI_ADMA2_ALIGN; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 676 | |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 677 | desc += host->desc_sz; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 678 | |
| 679 | addr += offset; |
| 680 | len -= offset; |
| 681 | } |
| 682 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 683 | BUG_ON(len > 65536); |
| 684 | |
Adrian Hunter | 347ea32 | 2015-11-26 14:00:48 +0200 | [diff] [blame] | 685 | if (len) { |
| 686 | /* tran, valid */ |
| 687 | sdhci_adma_write_desc(host, desc, addr, len, |
| 688 | ADMA2_TRAN_VALID); |
| 689 | desc += host->desc_sz; |
| 690 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * If this triggers then we have a calculation bug |
| 694 | * somewhere. :/ |
| 695 | */ |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 696 | WARN_ON((desc - host->adma_table) >= host->adma_table_sz); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 699 | if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) { |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 700 | /* Mark the last descriptor as the terminating descriptor */ |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 701 | if (desc != host->adma_table) { |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 702 | desc -= host->desc_sz; |
Adrian Hunter | b5ffa67 | 2014-11-04 12:42:40 +0200 | [diff] [blame] | 703 | sdhci_adma_mark_end(desc); |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 704 | } |
| 705 | } else { |
Russell King | acc3ad1 | 2016-01-26 13:40:00 +0000 | [diff] [blame] | 706 | /* Add a terminating entry - nop, end, valid */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 707 | sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID); |
Thomas Abraham | 70764a9 | 2010-05-26 14:42:04 -0700 | [diff] [blame] | 708 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static void sdhci_adma_table_post(struct sdhci_host *host, |
| 712 | struct mmc_data *data) |
| 713 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 714 | struct scatterlist *sg; |
| 715 | int i, size; |
Adrian Hunter | 1c3d5f6 | 2014-11-04 12:42:41 +0200 | [diff] [blame] | 716 | void *align; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 717 | char *buffer; |
| 718 | unsigned long flags; |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 719 | u32 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)); |
| 720 | |
| 721 | trace_mmc_adma_table_post(command, data->sg_len); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 722 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 723 | if (data->flags & MMC_DATA_READ) { |
| 724 | bool has_unaligned = false; |
Russell King | de0b65a | 2014-04-25 12:58:29 +0100 | [diff] [blame] | 725 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 726 | /* Do a quick scan of the SG list for any unaligned mappings */ |
| 727 | for_each_sg(data->sg, sg, host->sg_count, i) |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 728 | if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) { |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 729 | has_unaligned = true; |
| 730 | break; |
| 731 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 732 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 733 | if (has_unaligned) { |
| 734 | dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg, |
Russell King | f55c98f | 2016-01-26 13:40:11 +0000 | [diff] [blame] | 735 | data->sg_len, DMA_FROM_DEVICE); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 736 | |
Russell King | 47fa961 | 2016-01-26 13:40:06 +0000 | [diff] [blame] | 737 | align = host->align_buffer; |
| 738 | |
| 739 | for_each_sg(data->sg, sg, host->sg_count, i) { |
| 740 | if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) { |
| 741 | size = SDHCI_ADMA2_ALIGN - |
| 742 | (sg_dma_address(sg) & SDHCI_ADMA2_MASK); |
| 743 | |
| 744 | buffer = sdhci_kmap_atomic(sg, &flags); |
| 745 | memcpy(buffer, align, size); |
| 746 | sdhci_kunmap_atomic(buffer, &flags); |
| 747 | |
| 748 | align += SDHCI_ADMA2_ALIGN; |
| 749 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 755 | 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] | 756 | { |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 757 | u8 count; |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 758 | struct mmc_data *data = cmd->data; |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 759 | unsigned target_timeout, current_timeout; |
Sahitya Tummala | a5733ab5 | 2013-06-10 16:32:51 +0530 | [diff] [blame] | 760 | u32 curr_clk = 0; /* In KHz */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 761 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 762 | /* |
| 763 | * If the host controller provides us with an incorrect timeout |
| 764 | * value, just skip the check and use 0xE. The hardware may take |
| 765 | * longer to time out, but that's much better than having a too-short |
| 766 | * timeout value. |
| 767 | */ |
Pierre Ossman | 11a2f1b | 2009-06-21 20:59:33 +0200 | [diff] [blame] | 768 | if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 769 | return 0xE; |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 770 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 771 | /* Unspecified timeout, assume max */ |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 772 | if (!data && !cmd->busy_timeout) |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 773 | return 0xE; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 774 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 775 | /* timeout in us */ |
| 776 | if (!data) |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 777 | target_timeout = cmd->busy_timeout * 1000; |
Andy Shevchenko | 78a2ca2 | 2011-08-03 18:35:59 +0300 | [diff] [blame] | 778 | else { |
Russell King | fafcfda | 2016-01-26 13:40:58 +0000 | [diff] [blame] | 779 | target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000); |
Russell King | 7f05538 | 2016-01-26 13:41:04 +0000 | [diff] [blame] | 780 | if (host->clock && data->timeout_clks) { |
| 781 | unsigned long long val; |
| 782 | |
| 783 | /* |
| 784 | * data->timeout_clks is in units of clock cycles. |
| 785 | * host->clock is in Hz. target_timeout is in us. |
| 786 | * Hence, us = 1000000 * cycles / Hz. Round up. |
| 787 | */ |
Haibo Chen | 02265cd6 | 2016-10-17 10:18:37 +0200 | [diff] [blame] | 788 | val = 1000000ULL * data->timeout_clks; |
Russell King | 7f05538 | 2016-01-26 13:41:04 +0000 | [diff] [blame] | 789 | if (do_div(val, host->clock)) |
| 790 | target_timeout++; |
| 791 | target_timeout += val; |
| 792 | } |
Andy Shevchenko | 78a2ca2 | 2011-08-03 18:35:59 +0300 | [diff] [blame] | 793 | } |
Anton Vorontsov | 81b3980 | 2009-09-22 16:45:13 -0700 | [diff] [blame] | 794 | |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 795 | /* |
| 796 | * Figure out needed cycles. |
| 797 | * We do this in steps in order to fit inside a 32 bit int. |
| 798 | * The first step is the minimum timeout, which will have a |
| 799 | * minimum resolution of 6 bits: |
| 800 | * (1) 2^13*1000 > 2^22, |
| 801 | * (2) host->timeout_clk < 2^16 |
| 802 | * => |
| 803 | * (1) / (2) > 2^6 |
| 804 | */ |
| 805 | count = 0; |
Sahitya Tummala | a5733ab5 | 2013-06-10 16:32:51 +0530 | [diff] [blame] | 806 | if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) { |
| 807 | curr_clk = host->clock / 1000; |
| 808 | if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4) |
| 809 | curr_clk /= 4; |
| 810 | current_timeout = (1 << 13) * 1000 / curr_clk; |
| 811 | } else { |
| 812 | current_timeout = (1 << 13) * 1000 / host->timeout_clk; |
| 813 | } |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 814 | while (current_timeout < target_timeout) { |
| 815 | count++; |
| 816 | current_timeout <<= 1; |
| 817 | if (count >= 0xF) |
| 818 | break; |
| 819 | } |
| 820 | |
Sahitya Tummala | 7c9780d | 2013-04-12 11:59:25 +0530 | [diff] [blame] | 821 | if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) { |
| 822 | if (count >= 0xF) { |
| 823 | DBG("%s: Too large timeout 0x%x requested for CMD%d!\n", |
| 824 | mmc_hostname(host->mmc), count, cmd->opcode); |
| 825 | count = 0xE; |
| 826 | } |
Pierre Ossman | 1c8cde9 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 829 | return count; |
| 830 | } |
| 831 | |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 832 | static void sdhci_set_transfer_irqs(struct sdhci_host *host) |
| 833 | { |
| 834 | u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL; |
| 835 | u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR; |
| 836 | |
| 837 | if (host->flags & SDHCI_REQ_USE_DMA) |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 838 | host->ier = (host->ier & ~pio_irqs) | dma_irqs; |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 839 | else |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 840 | host->ier = (host->ier & ~dma_irqs) | pio_irqs; |
| 841 | |
| 842 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 843 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 844 | } |
| 845 | |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | u8 count; |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 849 | |
| 850 | if (host->ops->set_timeout) { |
| 851 | host->ops->set_timeout(host, cmd); |
| 852 | } else { |
| 853 | count = sdhci_calc_timeout(host, cmd); |
| 854 | sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL); |
| 855 | } |
| 856 | } |
| 857 | |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 858 | static void sdhci_set_blk_size_reg(struct sdhci_host *host, unsigned int blksz, |
| 859 | unsigned int sdma_boundary) |
| 860 | { |
| 861 | if (host->flags & SDHCI_USE_ADMA) |
| 862 | sdhci_writew(host, SDHCI_MAKE_BLKSZ(0, blksz), |
| 863 | SDHCI_BLOCK_SIZE); |
| 864 | else |
| 865 | sdhci_writew(host, SDHCI_MAKE_BLKSZ(sdma_boundary, blksz), |
| 866 | SDHCI_BLOCK_SIZE); |
| 867 | } |
| 868 | |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 869 | static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd) |
| 870 | { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 871 | u8 ctrl; |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 872 | struct mmc_data *data = cmd->data; |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 873 | |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 874 | if (sdhci_data_line_cmd(cmd)) |
Aisheng Dong | b45e668 | 2014-08-27 15:26:29 +0800 | [diff] [blame] | 875 | sdhci_set_timeout(host, cmd); |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 876 | |
| 877 | if (!data) |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 878 | return; |
| 879 | |
Adrian Hunter | 43dea09 | 2016-06-29 16:24:26 +0300 | [diff] [blame] | 880 | WARN_ON(host->data); |
| 881 | |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 882 | /* Sanity checks */ |
Asutosh Das | aafcad4 | 2013-01-10 21:05:49 +0530 | [diff] [blame] | 883 | BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size); |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 884 | BUG_ON(data->blksz > host->mmc->max_blk_size); |
| 885 | BUG_ON(data->blocks > 65535); |
| 886 | |
| 887 | host->data = data; |
| 888 | host->data_early = 0; |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 889 | host->data->bytes_xfered = 0; |
Pierre Ossman | ee53ab5 | 2008-07-05 00:25:15 +0200 | [diff] [blame] | 890 | |
Russell King | fce1442 | 2016-01-26 13:41:20 +0000 | [diff] [blame] | 891 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 892 | struct scatterlist *sg; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 893 | unsigned int length_mask, offset_mask; |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 894 | int i; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 895 | |
Russell King | fce1442 | 2016-01-26 13:41:20 +0000 | [diff] [blame] | 896 | host->flags |= SDHCI_REQ_USE_DMA; |
| 897 | |
| 898 | /* |
| 899 | * FIXME: This doesn't account for merging when mapping the |
| 900 | * scatterlist. |
| 901 | * |
| 902 | * The assumption here being that alignment and lengths are |
| 903 | * the same after DMA mapping to device address space. |
| 904 | */ |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 905 | length_mask = 0; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 906 | offset_mask = 0; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 907 | if (host->flags & SDHCI_USE_ADMA) { |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 908 | if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) { |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 909 | length_mask = 3; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 910 | /* |
| 911 | * As we use up to 3 byte chunks to work |
| 912 | * around alignment problems, we need to |
| 913 | * check the offset as well. |
| 914 | */ |
| 915 | offset_mask = 3; |
| 916 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 917 | } else { |
| 918 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 919 | length_mask = 3; |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 920 | if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) |
| 921 | offset_mask = 3; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Russell King | df95392 | 2016-01-26 13:41:14 +0000 | [diff] [blame] | 924 | if (unlikely(length_mask | offset_mask)) { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 925 | for_each_sg(data->sg, sg, data->sg_len, i) { |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 926 | if (sg->length & length_mask) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 927 | DBG("Reverting to PIO because of transfer size (%d)\n", |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 928 | sg->length); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 929 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 930 | break; |
| 931 | } |
Russell King | a0eaf0f | 2016-01-26 13:41:09 +0000 | [diff] [blame] | 932 | if (sg->offset & offset_mask) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 933 | DBG("Reverting to PIO because of bad alignment\n"); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 934 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 941 | if (host->flags & SDHCI_REQ_USE_DMA) { |
Russell King | c0999b7 | 2016-01-26 13:40:27 +0000 | [diff] [blame] | 942 | int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 943 | |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 944 | if (sg_cnt <= 0) { |
| 945 | /* |
| 946 | * This only happens when someone fed |
| 947 | * us an invalid request. |
| 948 | */ |
| 949 | WARN_ON(1); |
| 950 | host->flags &= ~SDHCI_REQ_USE_DMA; |
| 951 | } else if (host->flags & SDHCI_USE_ADMA) { |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 952 | trace_mmc_adma_table_pre(cmd->opcode, data->sg_len); |
Russell King | 60c6476 | 2016-01-26 13:40:22 +0000 | [diff] [blame] | 953 | sdhci_adma_table_pre(host, data, sg_cnt); |
| 954 | |
| 955 | sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS); |
| 956 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 957 | sdhci_writel(host, |
| 958 | (u64)host->adma_addr >> 32, |
| 959 | SDHCI_ADMA_ADDRESS_HI); |
| 960 | } else { |
| 961 | WARN_ON(sg_cnt != 1); |
| 962 | sdhci_writel(host, sg_dma_address(data->sg), |
| 963 | SDHCI_DMA_ADDRESS); |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 967 | /* |
| 968 | * Always adjust the DMA selection as some controllers |
| 969 | * (e.g. JMicron) can't do PIO properly when the selection |
| 970 | * is ADMA. |
| 971 | */ |
| 972 | if (host->version >= SDHCI_SPEC_200) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 973 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 974 | ctrl &= ~SDHCI_CTRL_DMA_MASK; |
| 975 | if ((host->flags & SDHCI_REQ_USE_DMA) && |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 976 | (host->flags & SDHCI_USE_ADMA)) { |
| 977 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 978 | ctrl |= SDHCI_CTRL_ADMA64; |
| 979 | else |
| 980 | ctrl |= SDHCI_CTRL_ADMA32; |
| 981 | } else { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 982 | ctrl |= SDHCI_CTRL_SDMA; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 983 | } |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 984 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Pierre Ossman | 8f1934c | 2008-06-30 21:15:49 +0200 | [diff] [blame] | 987 | if (!(host->flags & SDHCI_REQ_USE_DMA)) { |
Sebastian Andrzej Siewior | da60a91 | 2009-06-18 09:33:32 +0200 | [diff] [blame] | 988 | int flags; |
| 989 | |
| 990 | flags = SG_MITER_ATOMIC; |
| 991 | if (host->data->flags & MMC_DATA_READ) |
| 992 | flags |= SG_MITER_TO_SG; |
| 993 | else |
| 994 | flags |= SG_MITER_FROM_SG; |
| 995 | sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 996 | host->blocks = data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 997 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 998 | |
Anton Vorontsov | 6aa943a | 2009-03-17 00:13:50 +0300 | [diff] [blame] | 999 | sdhci_set_transfer_irqs(host); |
| 1000 | |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 1001 | /* Set the DMA boundary value and block size */ |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 1002 | sdhci_set_blk_size_reg(host, data->blksz, SDHCI_DEFAULT_BOUNDARY_ARG); |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1003 | sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1006 | static inline bool sdhci_auto_cmd12(struct sdhci_host *host, |
| 1007 | struct mmc_request *mrq) |
| 1008 | { |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1009 | return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) && |
| 1010 | !mrq->cap_cmd_during_tfr; |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1011 | } |
| 1012 | |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1013 | static void sdhci_set_transfer_mode(struct sdhci_host *host, |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1014 | struct mmc_command *cmd) |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1015 | { |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1016 | u16 mode = 0; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1017 | struct mmc_data *data = cmd->data; |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1018 | |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1019 | if (data == NULL) { |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1020 | if (host->quirks2 & |
| 1021 | SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) { |
| 1022 | sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE); |
| 1023 | } else { |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1024 | /* clear Auto CMD settings for no data CMDs */ |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1025 | mode = sdhci_readw(host, SDHCI_TRANSFER_MODE); |
| 1026 | sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 | |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1027 | SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE); |
Vincent Wan | 9b8ffea | 2014-11-05 14:09:00 +0800 | [diff] [blame] | 1028 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1029 | return; |
Dong Aisheng | 2b558c1 | 2013-10-30 22:09:48 +0800 | [diff] [blame] | 1030 | } |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1031 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1032 | WARN_ON(!host->data); |
| 1033 | |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1034 | if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE)) |
| 1035 | mode = SDHCI_TRNS_BLK_CNT_EN; |
| 1036 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1037 | if (mmc_op_multi(cmd->opcode) || data->blocks > 1) { |
Vincent Yang | d3fc5d7 | 2015-01-20 16:05:17 +0800 | [diff] [blame] | 1038 | mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1039 | /* |
| 1040 | * If we are sending CMD23, CMD12 never gets sent |
| 1041 | * on successful completion (so no Auto-CMD12). |
| 1042 | */ |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1043 | if (sdhci_auto_cmd12(host, cmd->mrq) && |
Corneliu Doban | 85cc1c3 | 2015-02-09 16:06:29 -0800 | [diff] [blame] | 1044 | (cmd->opcode != SD_IO_RW_EXTENDED)) |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1045 | mode |= SDHCI_TRNS_AUTO_CMD12; |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1046 | else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) { |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1047 | mode |= SDHCI_TRNS_AUTO_CMD23; |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1048 | sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2); |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1049 | } |
Jerry Huang | c4512f7 | 2010-08-10 18:01:59 -0700 | [diff] [blame] | 1050 | } |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1051 | |
Sahitya Tummala | 9dca7d7 | 2013-02-25 15:45:32 +0530 | [diff] [blame] | 1052 | if (data->flags & MMC_DATA_READ) { |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1053 | mode |= SDHCI_TRNS_READ; |
Ritesh Harjani | 210c230 | 2014-11-14 11:06:40 +0530 | [diff] [blame] | 1054 | if (host->ops->toggle_cdr) { |
| 1055 | if ((cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) || |
| 1056 | (cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) || |
| 1057 | (cmd->opcode == MMC_SEND_TUNING_BLOCK)) |
| 1058 | host->ops->toggle_cdr(host, false); |
| 1059 | else |
| 1060 | host->ops->toggle_cdr(host, true); |
| 1061 | } |
Sahitya Tummala | 9dca7d7 | 2013-02-25 15:45:32 +0530 | [diff] [blame] | 1062 | } |
| 1063 | if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE)) |
| 1064 | host->ops->toggle_cdr(host, false); |
Pierre Ossman | c9fddbc | 2007-12-02 19:52:11 +0100 | [diff] [blame] | 1065 | if (host->flags & SDHCI_REQ_USE_DMA) |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1066 | mode |= SDHCI_TRNS_DMA; |
| 1067 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1068 | sdhci_writew(host, mode, SDHCI_TRANSFER_MODE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Adrian Hunter | 0cc563c | 2016-06-29 16:24:28 +0300 | [diff] [blame] | 1071 | static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq) |
| 1072 | { |
| 1073 | return (!(host->flags & SDHCI_DEVICE_DEAD) && |
| 1074 | ((mrq->cmd && mrq->cmd->error) || |
| 1075 | (mrq->sbc && mrq->sbc->error) || |
| 1076 | (mrq->data && ((mrq->data->error && !mrq->data->stop) || |
| 1077 | (mrq->data->stop && mrq->data->stop->error))) || |
| 1078 | (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))); |
| 1079 | } |
| 1080 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 1081 | static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq) |
| 1082 | { |
| 1083 | int i; |
| 1084 | |
| 1085 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 1086 | if (host->mrqs_done[i] == mrq) { |
| 1087 | WARN_ON(1); |
| 1088 | return; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 1093 | if (!host->mrqs_done[i]) { |
| 1094 | host->mrqs_done[i] = mrq; |
| 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | WARN_ON(i >= SDHCI_MAX_MRQS); |
| 1100 | |
| 1101 | tasklet_schedule(&host->finish_tasklet); |
| 1102 | } |
| 1103 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1104 | static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq) |
| 1105 | { |
Adrian Hunter | 5a8a3fe | 2016-06-29 16:24:30 +0300 | [diff] [blame] | 1106 | if (host->cmd && host->cmd->mrq == mrq) |
| 1107 | host->cmd = NULL; |
| 1108 | |
| 1109 | if (host->data_cmd && host->data_cmd->mrq == mrq) |
| 1110 | host->data_cmd = NULL; |
| 1111 | |
| 1112 | if (host->data && host->data->mrq == mrq) |
| 1113 | host->data = NULL; |
| 1114 | |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 1115 | if (sdhci_needs_reset(host, mrq)) |
| 1116 | host->pending_reset = true; |
| 1117 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 1118 | __sdhci_finish_mrq(host, mrq); |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1119 | } |
| 1120 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1121 | static void sdhci_finish_data(struct sdhci_host *host) |
| 1122 | { |
Adrian Hunter | 33a57ad | 2016-06-29 16:24:36 +0300 | [diff] [blame] | 1123 | struct mmc_command *data_cmd = host->data_cmd; |
| 1124 | struct mmc_data *data = host->data; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1125 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1126 | host->data = NULL; |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 1127 | host->data_cmd = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1128 | |
Russell King | add8913 | 2016-01-26 13:40:42 +0000 | [diff] [blame] | 1129 | if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) == |
| 1130 | (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) |
| 1131 | sdhci_adma_table_post(host, data); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1132 | |
| 1133 | /* |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1134 | * The specification states that the block count register must |
| 1135 | * be updated, but it does not specify at what point in the |
| 1136 | * data flow. That makes the register entirely useless to read |
| 1137 | * back so we have to assume that nothing made it to the card |
| 1138 | * in the event of an error. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1139 | */ |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1140 | if (data->error) |
| 1141 | data->bytes_xfered = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1142 | else |
Pierre Ossman | c9b74c5 | 2008-04-18 20:41:49 +0200 | [diff] [blame] | 1143 | data->bytes_xfered = data->blksz * data->blocks; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1144 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1145 | /* |
| 1146 | * Need to send CMD12 if - |
| 1147 | * a) open-ended multiblock transfer (no CMD23) |
| 1148 | * b) error in multiblock transfer |
| 1149 | */ |
| 1150 | if (data->stop && |
| 1151 | (data->error || |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1152 | !data->mrq->sbc)) { |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1153 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1154 | /* |
| 1155 | * The controller needs a reset of internal state machines |
| 1156 | * upon error conditions. |
| 1157 | */ |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1158 | if (data->error) { |
Adrian Hunter | 33a57ad | 2016-06-29 16:24:36 +0300 | [diff] [blame] | 1159 | if (!host->cmd || host->cmd == data_cmd) |
| 1160 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 1161 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1164 | /* |
| 1165 | * 'cap_cmd_during_tfr' request must not use the command line |
| 1166 | * after mmc_command_done() has been called. It is upper layer's |
| 1167 | * responsibility to send the stop command if required. |
| 1168 | */ |
| 1169 | if (data->mrq->cap_cmd_during_tfr) { |
| 1170 | sdhci_finish_mrq(host, data->mrq); |
| 1171 | } else { |
| 1172 | /* Avoid triggering warning in sdhci_send_command() */ |
| 1173 | host->cmd = NULL; |
| 1174 | sdhci_send_command(host, data->stop); |
| 1175 | } |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1176 | } else { |
| 1177 | sdhci_finish_mrq(host, data->mrq); |
| 1178 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 1181 | static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq, |
| 1182 | unsigned long timeout) |
| 1183 | { |
| 1184 | if (sdhci_data_line_cmd(mrq->cmd)) |
| 1185 | mod_timer(&host->data_timer, timeout); |
| 1186 | else |
| 1187 | mod_timer(&host->timer, timeout); |
| 1188 | } |
| 1189 | |
| 1190 | static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq) |
| 1191 | { |
| 1192 | if (sdhci_data_line_cmd(mrq->cmd)) |
| 1193 | del_timer(&host->data_timer); |
| 1194 | else |
| 1195 | del_timer(&host->timer); |
| 1196 | } |
| 1197 | |
Dong Aisheng | c0e55129 | 2013-09-13 19:11:31 +0800 | [diff] [blame] | 1198 | void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1199 | { |
| 1200 | int flags; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1201 | u32 mask; |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1202 | unsigned long timeout; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1203 | |
| 1204 | WARN_ON(host->cmd); |
| 1205 | |
Russell King | 9677620 | 2016-01-26 13:39:34 +0000 | [diff] [blame] | 1206 | /* Initially, a command has no error */ |
| 1207 | cmd->error = 0; |
| 1208 | |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 1209 | if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) && |
| 1210 | cmd->opcode == MMC_STOP_TRANSMISSION) |
| 1211 | cmd->flags |= MMC_RSP_BUSY; |
| 1212 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1213 | /* Wait max 10 ms */ |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 1214 | timeout = 10000; |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1215 | |
| 1216 | mask = SDHCI_CMD_INHIBIT; |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 1217 | if (sdhci_data_line_cmd(cmd)) |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1218 | mask |= SDHCI_DATA_INHIBIT; |
| 1219 | |
| 1220 | /* We shouldn't wait for data inihibit for stop commands, even |
| 1221 | though they might use busy signaling */ |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1222 | if (cmd->mrq->data && (cmd == cmd->mrq->data->stop)) |
Pierre Ossman | fd2208d | 2006-06-30 02:22:28 -0700 | [diff] [blame] | 1223 | mask &= ~SDHCI_DATA_INHIBIT; |
| 1224 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1225 | while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) { |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1226 | if (timeout == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 1227 | pr_err("%s: Controller never released inhibit bit(s).\n", |
| 1228 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1229 | sdhci_dumpregs(host); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1230 | cmd->error = -EIO; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1231 | sdhci_finish_mrq(host, cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1232 | return; |
| 1233 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1234 | timeout--; |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 1235 | udelay(1); |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1236 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1237 | |
Adrian Hunter | 3e1a689 | 2013-11-14 10:16:20 +0200 | [diff] [blame] | 1238 | timeout = jiffies; |
Ulf Hansson | 1d4d774 | 2014-01-08 15:06:08 +0100 | [diff] [blame] | 1239 | if (!cmd->data && cmd->busy_timeout > 9000) |
| 1240 | timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ; |
Adrian Hunter | 3e1a689 | 2013-11-14 10:16:20 +0200 | [diff] [blame] | 1241 | else |
| 1242 | timeout += 10 * HZ; |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 1243 | sdhci_mod_timer(host, cmd->mrq, timeout); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1244 | |
| 1245 | host->cmd = cmd; |
Adrian Hunter | 56a590d | 2016-06-29 16:24:32 +0300 | [diff] [blame] | 1246 | if (sdhci_data_line_cmd(cmd)) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 1247 | WARN_ON(host->data_cmd); |
| 1248 | host->data_cmd = cmd; |
| 1249 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1250 | |
Andrei Warkentin | a3c7778 | 2011-04-11 16:13:42 -0500 | [diff] [blame] | 1251 | sdhci_prepare_data(host, cmd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1252 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1253 | sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1254 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1255 | sdhci_set_transfer_mode(host, cmd); |
Pierre Ossman | c7fa996 | 2006-06-30 02:22:25 -0700 | [diff] [blame] | 1256 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1257 | if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1258 | pr_err("%s: Unsupported response type!\n", |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1259 | mmc_hostname(host->mmc)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 1260 | cmd->error = -EINVAL; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1261 | sdhci_finish_mrq(host, cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | if (!(cmd->flags & MMC_RSP_PRESENT)) |
| 1266 | flags = SDHCI_CMD_RESP_NONE; |
| 1267 | else if (cmd->flags & MMC_RSP_136) |
| 1268 | flags = SDHCI_CMD_RESP_LONG; |
| 1269 | else if (cmd->flags & MMC_RSP_BUSY) |
| 1270 | flags = SDHCI_CMD_RESP_SHORT_BUSY; |
| 1271 | else |
| 1272 | flags = SDHCI_CMD_RESP_SHORT; |
| 1273 | |
| 1274 | if (cmd->flags & MMC_RSP_CRC) |
| 1275 | flags |= SDHCI_CMD_CRC; |
| 1276 | if (cmd->flags & MMC_RSP_OPCODE) |
| 1277 | flags |= SDHCI_CMD_INDEX; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 1278 | |
| 1279 | /* 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] | 1280 | if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK || |
| 1281 | cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1282 | flags |= SDHCI_CMD_DATA; |
| 1283 | |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 1284 | if (cmd->data) |
| 1285 | host->data_start_time = ktime_get(); |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 1286 | trace_mmc_cmd_rw_start(cmd->opcode, cmd->arg, cmd->flags); |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1287 | sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1288 | } |
Dong Aisheng | c0e55129 | 2013-09-13 19:11:31 +0800 | [diff] [blame] | 1289 | EXPORT_SYMBOL_GPL(sdhci_send_command); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1290 | |
| 1291 | static void sdhci_finish_command(struct sdhci_host *host) |
| 1292 | { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1293 | struct mmc_command *cmd = host->cmd; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1294 | int i; |
| 1295 | |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1296 | host->cmd = NULL; |
| 1297 | |
| 1298 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 1299 | if (cmd->flags & MMC_RSP_136) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1300 | /* CRC is stripped so we need to do some shifting. */ |
| 1301 | for (i = 0;i < 4;i++) { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1302 | cmd->resp[i] = sdhci_readl(host, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1303 | SDHCI_RESPONSE + (3-i)*4) << 8; |
| 1304 | if (i != 3) |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1305 | cmd->resp[i] |= |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1306 | sdhci_readb(host, |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1307 | SDHCI_RESPONSE + (3-i)*4-1); |
| 1308 | } |
| 1309 | } else { |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1310 | cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
Adrian Hunter | 20845be | 2016-08-16 13:44:13 +0300 | [diff] [blame] | 1314 | if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd) |
| 1315 | mmc_command_done(host->mmc, cmd->mrq); |
| 1316 | |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1317 | /* |
| 1318 | * The host can send and interrupt when the busy state has |
| 1319 | * ended, allowing us to wait without wasting CPU cycles. |
| 1320 | * The busy signal uses DAT0 so this is similar to waiting |
| 1321 | * for data to complete. |
| 1322 | * |
| 1323 | * Note: The 1.0 specification is a bit ambiguous about this |
| 1324 | * feature so there might be some problems with older |
| 1325 | * controllers. |
| 1326 | */ |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1327 | if (cmd->flags & MMC_RSP_BUSY) { |
| 1328 | if (cmd->data) { |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1329 | DBG("Cannot wait for busy signal when also doing a data transfer"); |
| 1330 | } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) && |
Adrian Hunter | ea96802 | 2016-06-29 16:24:24 +0300 | [diff] [blame] | 1331 | cmd == host->data_cmd) { |
| 1332 | /* Command complete before busy is ended */ |
Adrian Hunter | 6bde868 | 2016-06-29 16:24:20 +0300 | [diff] [blame] | 1333 | return; |
| 1334 | } |
| 1335 | } |
| 1336 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1337 | /* Finished CMD23, now send actual command. */ |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1338 | if (cmd == cmd->mrq->sbc) { |
| 1339 | sdhci_send_command(host, cmd->mrq->cmd); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1340 | } else { |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 1341 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1342 | /* Processed actual command. */ |
| 1343 | if (host->data && host->data_early) |
| 1344 | sdhci_finish_data(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1345 | |
Adrian Hunter | e0a5640 | 2016-06-29 16:24:22 +0300 | [diff] [blame] | 1346 | if (!cmd->data) |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1347 | sdhci_finish_mrq(host, cmd->mrq); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1348 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1351 | static u16 sdhci_get_preset_value(struct sdhci_host *host) |
| 1352 | { |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1353 | u16 preset = 0; |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1354 | |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1355 | switch (host->timing) { |
| 1356 | case MMC_TIMING_UHS_SDR12: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1357 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12); |
| 1358 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1359 | case MMC_TIMING_UHS_SDR25: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1360 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25); |
| 1361 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1362 | case MMC_TIMING_UHS_SDR50: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1363 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50); |
| 1364 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1365 | case MMC_TIMING_UHS_SDR104: |
| 1366 | case MMC_TIMING_MMC_HS200: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1367 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104); |
| 1368 | break; |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 1369 | case MMC_TIMING_UHS_DDR50: |
Jisheng Zhang | 0dafa60 | 2015-08-18 16:21:39 +0800 | [diff] [blame] | 1370 | case MMC_TIMING_MMC_DDR52: |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1371 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50); |
| 1372 | break; |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1373 | case MMC_TIMING_MMC_HS400: |
| 1374 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400); |
| 1375 | break; |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1376 | default: |
| 1377 | pr_warn("%s: Invalid UHS-I mode selected\n", |
| 1378 | mmc_hostname(host->mmc)); |
| 1379 | preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12); |
| 1380 | break; |
| 1381 | } |
| 1382 | return preset; |
| 1383 | } |
| 1384 | |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1385 | u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock, |
| 1386 | unsigned int *actual_clock) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1387 | { |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1388 | int div = 0; /* Initialized for compiler warning */ |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1389 | int real_div = div, clk_mul = 1; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1390 | u16 clk = 0; |
ludovic.desroches@atmel.com | 5497159 | 2015-07-29 16:22:46 +0200 | [diff] [blame] | 1391 | bool switch_base_clk = false; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1392 | |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1393 | if (host->version >= SDHCI_SPEC_300) { |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 1394 | if (host->preset_enabled) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1395 | u16 pre_val; |
| 1396 | |
| 1397 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1398 | pre_val = sdhci_get_preset_value(host); |
| 1399 | div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK) |
| 1400 | >> SDHCI_PRESET_SDCLK_FREQ_SHIFT; |
| 1401 | if (host->clk_mul && |
| 1402 | (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) { |
| 1403 | clk = SDHCI_PROG_CLOCK_MODE; |
| 1404 | real_div = div + 1; |
| 1405 | clk_mul = host->clk_mul; |
| 1406 | } else { |
| 1407 | real_div = max_t(int, 1, div << 1); |
| 1408 | } |
| 1409 | goto clock_set; |
| 1410 | } |
| 1411 | |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1412 | /* |
| 1413 | * Check if the Host Controller supports Programmable Clock |
| 1414 | * Mode. |
| 1415 | */ |
| 1416 | if (host->clk_mul) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1417 | for (div = 1; div <= 1024; div++) { |
| 1418 | if ((host->max_clk * host->clk_mul / div) |
| 1419 | <= clock) |
| 1420 | break; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1421 | } |
ludovic.desroches@atmel.com | 5497159 | 2015-07-29 16:22:46 +0200 | [diff] [blame] | 1422 | if ((host->max_clk * host->clk_mul / div) <= clock) { |
| 1423 | /* |
| 1424 | * Set Programmable Clock Mode in the Clock |
| 1425 | * Control register. |
| 1426 | */ |
| 1427 | clk = SDHCI_PROG_CLOCK_MODE; |
| 1428 | real_div = div; |
| 1429 | clk_mul = host->clk_mul; |
| 1430 | div--; |
| 1431 | } else { |
| 1432 | /* |
| 1433 | * Divisor can be too small to reach clock |
| 1434 | * speed requirement. Then use the base clock. |
| 1435 | */ |
| 1436 | switch_base_clk = true; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | if (!host->clk_mul || switch_base_clk) { |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1441 | /* Version 3.00 divisors must be a multiple of 2. */ |
| 1442 | if (host->max_clk <= clock) |
| 1443 | div = 1; |
| 1444 | else { |
| 1445 | for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; |
| 1446 | div += 2) { |
| 1447 | if ((host->max_clk / div) <= clock) |
| 1448 | break; |
| 1449 | } |
| 1450 | } |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1451 | real_div = div; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1452 | div >>= 1; |
Suneel Garapati | d1955c3 | 2015-06-09 13:01:50 +0530 | [diff] [blame] | 1453 | if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN) |
| 1454 | && !div && host->max_clk <= 25000000) |
| 1455 | div = 1; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1456 | } |
| 1457 | } else { |
| 1458 | /* Version 2.00 divisors must be a power of 2. */ |
Zhangfei Gao | 0397526 | 2010-09-20 15:15:18 -0400 | [diff] [blame] | 1459 | for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) { |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1460 | if ((host->max_clk / div) <= clock) |
| 1461 | break; |
| 1462 | } |
Giuseppe CAVALLARO | df16219 | 2011-11-04 13:53:19 +0100 | [diff] [blame] | 1463 | real_div = div; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1464 | div >>= 1; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1465 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1466 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1467 | clock_set: |
Aisheng Dong | 03d6f5f | 2014-08-27 15:26:32 +0800 | [diff] [blame] | 1468 | if (real_div) |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1469 | *actual_clock = (host->max_clk * clk_mul) / real_div; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1470 | |
| 1471 | if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) |
| 1472 | div = 0; |
| 1473 | |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 1474 | clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT; |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 1475 | clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN) |
| 1476 | << SDHCI_DIVIDER_HI_SHIFT; |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1477 | |
| 1478 | return clk; |
| 1479 | } |
| 1480 | EXPORT_SYMBOL_GPL(sdhci_calc_clk); |
| 1481 | |
| 1482 | void sdhci_set_clock(struct sdhci_host *host, unsigned int clock) |
| 1483 | { |
| 1484 | u16 clk; |
| 1485 | unsigned long timeout; |
| 1486 | |
| 1487 | host->mmc->actual_clock = 0; |
| 1488 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1489 | if (host->clock) |
| 1490 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
Ludovic Desroches | fb9ee04 | 2016-04-07 11:13:08 +0200 | [diff] [blame] | 1491 | |
| 1492 | if (clock == 0) |
| 1493 | return; |
| 1494 | |
| 1495 | clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock); |
| 1496 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1497 | clk |= SDHCI_CLOCK_INT_EN; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1498 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1499 | |
Chris Ball | 27f6cb1 | 2009-09-22 16:45:31 -0700 | [diff] [blame] | 1500 | /* Wait max 20 ms */ |
Venkat Gopalakrishnan | c90fa96 | 2014-04-09 10:54:29 -0700 | [diff] [blame] | 1501 | timeout = 20000; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1502 | while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL)) |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1503 | & SDHCI_CLOCK_INT_STABLE)) { |
| 1504 | if (timeout == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 1505 | pr_err("%s: Internal clock never stabilised.\n", |
| 1506 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1507 | sdhci_dumpregs(host); |
| 1508 | return; |
| 1509 | } |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1510 | timeout--; |
Adrian Hunter | b43ba21 | 2017-03-20 19:50:29 +0200 | [diff] [blame] | 1511 | spin_unlock_irq(&host->lock); |
| 1512 | usleep_range(900, 1100); |
| 1513 | spin_lock_irq(&host->lock); |
Pierre Ossman | 7cb2c76 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1514 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1515 | |
| 1516 | clk |= SDHCI_CLOCK_CARD_EN; |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1517 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1518 | } |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 1519 | EXPORT_SYMBOL_GPL(sdhci_set_clock); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1520 | |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1521 | static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode, |
| 1522 | unsigned short vdd) |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1523 | { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 1524 | struct mmc_host *mmc = host->mmc; |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1525 | |
| 1526 | spin_unlock_irq(&host->lock); |
| 1527 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd); |
| 1528 | spin_lock_irq(&host->lock); |
| 1529 | |
| 1530 | if (mode != MMC_POWER_OFF) |
| 1531 | sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL); |
| 1532 | else |
| 1533 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
| 1534 | } |
| 1535 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1536 | void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode, |
| 1537 | unsigned short vdd) |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1538 | { |
Giuseppe Cavallaro | 8364248 | 2010-09-28 10:41:28 +0200 | [diff] [blame] | 1539 | u8 pwr = 0; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1540 | |
Russell King | 24fbb3c | 2014-04-25 13:00:06 +0100 | [diff] [blame] | 1541 | if (mode != MMC_POWER_OFF) { |
| 1542 | switch (1 << vdd) { |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1543 | case MMC_VDD_165_195: |
| 1544 | pwr = SDHCI_POWER_180; |
| 1545 | break; |
| 1546 | case MMC_VDD_29_30: |
| 1547 | case MMC_VDD_30_31: |
| 1548 | pwr = SDHCI_POWER_300; |
| 1549 | break; |
| 1550 | case MMC_VDD_32_33: |
| 1551 | case MMC_VDD_33_34: |
| 1552 | pwr = SDHCI_POWER_330; |
| 1553 | break; |
| 1554 | default: |
Adrian Hunter | 9d5de93 | 2015-11-26 14:00:46 +0200 | [diff] [blame] | 1555 | WARN(1, "%s: Invalid vdd %#x\n", |
| 1556 | mmc_hostname(host->mmc), vdd); |
| 1557 | break; |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | if (host->pwr == pwr) |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1562 | return; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1563 | |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1564 | host->pwr = pwr; |
| 1565 | |
| 1566 | if (pwr == 0) { |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1567 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1568 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1569 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Adrian Hunter | f0710a5 | 2013-05-06 12:17:32 +0300 | [diff] [blame] | 1570 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 1571 | sdhci_runtime_pm_bus_off(host); |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1572 | } else { |
| 1573 | /* |
| 1574 | * Spec says that we should clear the power reg before setting |
| 1575 | * a new value. Some controllers don't seem to like this though. |
| 1576 | */ |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1577 | if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) { |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1578 | sdhci_writeb(host, 0, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1579 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1580 | host->ops->check_power_status(host, REQ_BUS_OFF); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1581 | } |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1582 | /* |
| 1583 | * At least the Marvell CaFe chip gets confused if we set the |
| 1584 | * voltage and set turn on power at the same time, so set the |
| 1585 | * voltage first. |
| 1586 | */ |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1587 | if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) { |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1588 | sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1589 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1590 | host->ops->check_power_status(host, REQ_BUS_ON); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1591 | } |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1592 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1593 | pwr |= SDHCI_POWER_ON; |
| 1594 | |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1595 | sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1596 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1597 | host->ops->check_power_status(host, REQ_BUS_ON); |
Pierre Ossman | ae62890 | 2009-05-03 20:45:03 +0200 | [diff] [blame] | 1598 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1599 | if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON) |
| 1600 | sdhci_runtime_pm_bus_on(host); |
Andres Salomon | e08c169 | 2008-07-04 10:00:03 -0700 | [diff] [blame] | 1601 | |
Russell King | e921a8b | 2014-04-25 13:00:01 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * Some controllers need an extra 10ms delay of 10ms before |
| 1604 | * they can apply clock after applying power |
| 1605 | */ |
| 1606 | if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER) |
| 1607 | mdelay(10); |
| 1608 | } |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1609 | } |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1610 | EXPORT_SYMBOL_GPL(sdhci_set_power_noreg); |
Jisheng Zhang | 918f4cb | 2015-12-11 21:36:29 +0800 | [diff] [blame] | 1611 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1612 | void sdhci_set_power(struct sdhci_host *host, unsigned char mode, |
| 1613 | unsigned short vdd) |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1614 | { |
Dov Levenglick | 673a019 | 2015-07-16 11:58:38 +0300 | [diff] [blame] | 1615 | /* |
| 1616 | * Don't disable/re-enable power to the card when running a |
| 1617 | * suspend/resume sequence and the pm_flags are configured to preserve |
| 1618 | * card power during suspend. |
| 1619 | */ |
| 1620 | if (mmc_card_keep_power(host->mmc) && |
| 1621 | ((host->mmc->dev_status == DEV_SUSPENDED && mode == MMC_POWER_UP) || |
| 1622 | (host->mmc->dev_status == DEV_SUSPENDING && mode == MMC_POWER_OFF))) |
| 1623 | return; |
| 1624 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1625 | if (IS_ERR(host->mmc->supply.vmmc)) |
| 1626 | sdhci_set_power_noreg(host, mode, vdd); |
Adrian Hunter | 1dceb04 | 2016-03-29 12:45:43 +0300 | [diff] [blame] | 1627 | else |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1628 | sdhci_set_power_reg(host, mode, vdd); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1629 | } |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1630 | EXPORT_SYMBOL_GPL(sdhci_set_power); |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 1631 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1632 | /*****************************************************************************\ |
| 1633 | * * |
| 1634 | * MMC callbacks * |
| 1635 | * * |
| 1636 | \*****************************************************************************/ |
| 1637 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1638 | static int sdhci_enable(struct mmc_host *mmc) |
| 1639 | { |
| 1640 | struct sdhci_host *host = mmc_priv(mmc); |
| 1641 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1642 | if (host->ops->platform_bus_voting) |
| 1643 | host->ops->platform_bus_voting(host, 1); |
| 1644 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | static int sdhci_disable(struct mmc_host *mmc) |
| 1649 | { |
| 1650 | struct sdhci_host *host = mmc_priv(mmc); |
| 1651 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1652 | if (host->ops->platform_bus_voting) |
| 1653 | host->ops->platform_bus_voting(host, 0); |
| 1654 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1655 | return 0; |
| 1656 | } |
| 1657 | |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1658 | static void sdhci_notify_halt(struct mmc_host *mmc, bool halt) |
| 1659 | { |
| 1660 | struct sdhci_host *host = mmc_priv(mmc); |
| 1661 | |
| 1662 | pr_debug("%s: halt notification was sent, halt=%d\n", |
| 1663 | mmc_hostname(mmc), halt); |
| 1664 | if (host->flags & SDHCI_USE_ADMA_64BIT) { |
| 1665 | if (halt) |
| 1666 | host->adma_desc_line_sz = 16; |
| 1667 | else |
| 1668 | host->adma_desc_line_sz = 12; |
| 1669 | } |
| 1670 | } |
| 1671 | |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 1672 | static inline void sdhci_update_power_policy(struct sdhci_host *host, |
| 1673 | enum sdhci_power_policy policy) |
| 1674 | { |
| 1675 | host->power_policy = policy; |
| 1676 | } |
| 1677 | |
| 1678 | static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state) |
| 1679 | { |
| 1680 | int err = 0; |
| 1681 | struct sdhci_host *host = mmc_priv(mmc); |
| 1682 | |
| 1683 | switch (state) { |
| 1684 | case MMC_LOAD_HIGH: |
| 1685 | sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE); |
| 1686 | break; |
| 1687 | case MMC_LOAD_LOW: |
| 1688 | sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE); |
| 1689 | break; |
| 1690 | default: |
| 1691 | err = -EINVAL; |
| 1692 | break; |
| 1693 | } |
| 1694 | |
Sahitya Tummala | d300470 | 2015-08-06 13:58:47 +0530 | [diff] [blame] | 1695 | if (host->ops->notify_load) |
| 1696 | err = host->ops->notify_load(host, state); |
| 1697 | |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 1698 | return err; |
| 1699 | } |
| 1700 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1701 | static bool sdhci_check_state(struct sdhci_host *host) |
| 1702 | { |
| 1703 | if (!host->clock || !host->pwr) |
| 1704 | return true; |
| 1705 | else |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 1709 | static bool sdhci_check_auto_tuning(struct sdhci_host *host, |
| 1710 | struct mmc_command *cmd) |
| 1711 | { |
| 1712 | if (((cmd->opcode != MMC_READ_SINGLE_BLOCK) && |
| 1713 | (cmd->opcode != MMC_READ_MULTIPLE_BLOCK) && |
| 1714 | (cmd->opcode != SD_IO_RW_EXTENDED)) || (host->clock < 100000000)) |
| 1715 | return false; |
| 1716 | else if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1717 | host->mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
| 1718 | return true; |
| 1719 | else |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | static int sdhci_get_tuning_cmd(struct sdhci_host *host) |
| 1724 | { |
| 1725 | if (!host->mmc || !host->mmc->card) |
| 1726 | return 0; |
| 1727 | /* |
| 1728 | * If we are here, all conditions have already been true |
| 1729 | * and the card can either be an eMMC or SD/SDIO |
| 1730 | */ |
| 1731 | if (mmc_card_mmc(host->mmc->card)) |
| 1732 | return MMC_SEND_TUNING_BLOCK_HS200; |
| 1733 | else |
| 1734 | return MMC_SEND_TUNING_BLOCK; |
| 1735 | } |
| 1736 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1737 | static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1738 | { |
| 1739 | struct sdhci_host *host; |
Shawn Guo | 505a868 | 2012-12-11 15:23:42 +0800 | [diff] [blame] | 1740 | int present; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1741 | unsigned long flags; |
| 1742 | |
| 1743 | host = mmc_priv(mmc); |
| 1744 | |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1745 | if (sdhci_check_state(host)) { |
| 1746 | sdhci_dump_state(host); |
| 1747 | WARN(1, "sdhci in bad state"); |
| 1748 | mrq->cmd->error = -EIO; |
| 1749 | if (mrq->data) |
| 1750 | mrq->data->error = -EIO; |
Venkat Gopalakrishnan | 987fc41 | 2015-09-01 15:52:29 -0700 | [diff] [blame] | 1751 | host->mrq = NULL; |
| 1752 | sdhci_dumpregs(host); |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 1753 | mmc_request_done(host->mmc, mrq); |
| 1754 | sdhci_runtime_pm_put(host); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
Sahitya Tummala | 68adcae | 2013-04-16 18:06:06 +0530 | [diff] [blame] | 1758 | /* |
| 1759 | * Firstly check card presence from cd-gpio. The return could |
| 1760 | * be one of the following possibilities: |
| 1761 | * negative: cd-gpio is not available |
| 1762 | * zero: cd-gpio is used, and card is removed |
| 1763 | * one: cd-gpio is used, and card is present |
| 1764 | */ |
Adrian Hunter | 8d28b7a | 2016-02-09 16:12:36 +0200 | [diff] [blame] | 1765 | present = mmc->ops->get_cd(mmc); |
Sahitya Tummala | 68adcae | 2013-04-16 18:06:06 +0530 | [diff] [blame] | 1766 | if (present < 0) { |
| 1767 | /* If polling, assume that the card is always present. */ |
| 1768 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 1769 | present = 1; |
| 1770 | else |
| 1771 | present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 1772 | SDHCI_CARD_PRESENT; |
| 1773 | } |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 1774 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1775 | spin_lock_irqsave(&host->lock, flags); |
| 1776 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 1777 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 1778 | sdhci_led_activate(host); |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1779 | |
| 1780 | /* |
| 1781 | * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED |
| 1782 | * requests if Auto-CMD12 is enabled. |
| 1783 | */ |
Adrian Hunter | 0293d50 | 2016-06-29 16:24:35 +0300 | [diff] [blame] | 1784 | if (sdhci_auto_cmd12(host, mrq)) { |
Jerry Huang | c4512f7 | 2010-08-10 18:01:59 -0700 | [diff] [blame] | 1785 | if (mrq->stop) { |
| 1786 | mrq->data->stop = NULL; |
| 1787 | mrq->stop = NULL; |
| 1788 | } |
| 1789 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1790 | |
Anton Vorontsov | 68d1fb7 | 2009-03-17 00:13:52 +0300 | [diff] [blame] | 1791 | if (!present || host->flags & SDHCI_DEVICE_DEAD) { |
Adrian Hunter | a4c73ab | 2016-06-29 16:24:25 +0300 | [diff] [blame] | 1792 | mrq->cmd->error = -ENOMEDIUM; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 1793 | sdhci_finish_mrq(host, mrq); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 1794 | } else { |
Asutosh Das | b58499d | 2013-07-30 19:07:29 +0530 | [diff] [blame] | 1795 | if (host->ops->config_auto_tuning_cmd) { |
| 1796 | if (sdhci_check_auto_tuning(host, mrq->cmd)) |
| 1797 | host->ops->config_auto_tuning_cmd(host, true, |
| 1798 | sdhci_get_tuning_cmd(host)); |
| 1799 | else |
| 1800 | host->ops->config_auto_tuning_cmd(host, false, |
| 1801 | sdhci_get_tuning_cmd(host)); |
| 1802 | } |
| 1803 | |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 1804 | if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23)) |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 1805 | sdhci_send_command(host, mrq->sbc); |
| 1806 | else |
| 1807 | sdhci_send_command(host, mrq->cmd); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 1808 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1809 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 1810 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1811 | spin_unlock_irqrestore(&host->lock, flags); |
| 1812 | } |
| 1813 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 1814 | void sdhci_set_bus_width(struct sdhci_host *host, int width) |
| 1815 | { |
| 1816 | u8 ctrl; |
| 1817 | |
| 1818 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
| 1819 | if (width == MMC_BUS_WIDTH_8) { |
| 1820 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 1821 | if (host->version >= SDHCI_SPEC_300) |
| 1822 | ctrl |= SDHCI_CTRL_8BITBUS; |
| 1823 | } else { |
| 1824 | if (host->version >= SDHCI_SPEC_300) |
| 1825 | ctrl &= ~SDHCI_CTRL_8BITBUS; |
| 1826 | if (width == MMC_BUS_WIDTH_4) |
| 1827 | ctrl |= SDHCI_CTRL_4BITBUS; |
| 1828 | else |
| 1829 | ctrl &= ~SDHCI_CTRL_4BITBUS; |
| 1830 | } |
| 1831 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 1832 | } |
| 1833 | EXPORT_SYMBOL_GPL(sdhci_set_bus_width); |
| 1834 | |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 1835 | void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing) |
| 1836 | { |
| 1837 | u16 ctrl_2; |
| 1838 | |
| 1839 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 1840 | /* Select Bus Speed Mode for host */ |
| 1841 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
| 1842 | if ((timing == MMC_TIMING_MMC_HS200) || |
| 1843 | (timing == MMC_TIMING_UHS_SDR104)) |
| 1844 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 1845 | else if (timing == MMC_TIMING_UHS_SDR12) |
| 1846 | ctrl_2 |= SDHCI_CTRL_UHS_SDR12; |
| 1847 | else if (timing == MMC_TIMING_UHS_SDR25) |
| 1848 | ctrl_2 |= SDHCI_CTRL_UHS_SDR25; |
| 1849 | else if (timing == MMC_TIMING_UHS_SDR50) |
| 1850 | ctrl_2 |= SDHCI_CTRL_UHS_SDR50; |
| 1851 | else if ((timing == MMC_TIMING_UHS_DDR50) || |
| 1852 | (timing == MMC_TIMING_MMC_DDR52)) |
| 1853 | ctrl_2 |= SDHCI_CTRL_UHS_DDR50; |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1854 | else if (timing == MMC_TIMING_MMC_HS400) |
| 1855 | ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */ |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 1856 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 1857 | } |
| 1858 | EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling); |
| 1859 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 1860 | static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1861 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 1862 | struct sdhci_host *host = mmc_priv(mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1863 | unsigned long flags; |
| 1864 | u8 ctrl; |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1865 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1866 | |
Adrian Hunter | ceb6143 | 2011-12-27 15:48:41 +0200 | [diff] [blame] | 1867 | if (host->flags & SDHCI_DEVICE_DEAD) { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 1868 | if (!IS_ERR(mmc->supply.vmmc) && |
| 1869 | ios->power_mode == MMC_POWER_OFF) |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 1870 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
Adrian Hunter | ceb6143 | 2011-12-27 15:48:41 +0200 | [diff] [blame] | 1871 | return; |
| 1872 | } |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 1873 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1874 | if (host->version >= SDHCI_SPEC_300 && |
Dong Aisheng | 372c463 | 2013-10-18 19:48:50 +0800 | [diff] [blame] | 1875 | (ios->power_mode == MMC_POWER_UP) && |
| 1876 | !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 1877 | sdhci_enable_preset_value(host, false); |
| 1878 | |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1879 | spin_lock_irqsave(&host->lock, flags); |
Venkat Gopalakrishnan | f36166f | 2015-03-02 13:22:40 -0800 | [diff] [blame] | 1880 | if (ios->clock && |
| 1881 | ((ios->clock != host->clock) || (ios->timing != host->timing))) { |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1882 | spin_unlock_irqrestore(&host->lock, flags); |
| 1883 | host->ops->set_clock(host, ios->clock); |
| 1884 | spin_lock_irqsave(&host->lock, flags); |
| 1885 | host->clock = ios->clock; |
| 1886 | |
| 1887 | if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK && |
| 1888 | host->clock) { |
| 1889 | host->timeout_clk = host->mmc->actual_clock ? |
| 1890 | host->mmc->actual_clock / 1000 : |
| 1891 | host->clock / 1000; |
| 1892 | host->mmc->max_busy_timeout = |
| 1893 | host->ops->get_max_timeout_count ? |
| 1894 | host->ops->get_max_timeout_count(host) : |
| 1895 | 1 << 27; |
| 1896 | host->mmc->max_busy_timeout /= host->timeout_clk; |
| 1897 | } |
| 1898 | } |
| 1899 | spin_unlock_irqrestore(&host->lock, flags); |
| 1900 | |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1901 | /* |
| 1902 | * The controller clocks may be off during power-up and we may end up |
| 1903 | * enabling card clock before giving power to the card. Hence, during |
| 1904 | * MMC_POWER_UP enable the controller clock and turn-on the regulators. |
| 1905 | * The mmc_power_up would provide the necessary delay before turning on |
| 1906 | * the clocks to the card. |
| 1907 | */ |
| 1908 | if (ios->power_mode & MMC_POWER_UP) { |
| 1909 | if (host->ops->enable_controller_clock) { |
| 1910 | ret = host->ops->enable_controller_clock(host); |
| 1911 | if (ret) { |
| 1912 | pr_err("%s: enabling controller clock: failed: %d\n", |
| 1913 | mmc_hostname(host->mmc), ret); |
| 1914 | } else { |
| 1915 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1920 | spin_lock_irqsave(&host->lock, flags); |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1921 | if (!host->clock) { |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1922 | spin_unlock_irqrestore(&host->lock, flags); |
Venkat Gopalakrishnan | 0fac731 | 2015-01-23 12:58:00 -0800 | [diff] [blame] | 1923 | return; |
Russell King | 373073e | 2014-04-25 12:58:45 +0100 | [diff] [blame] | 1924 | } |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1925 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1926 | |
Adrian Hunter | 606d313 | 2016-10-05 12:11:22 +0300 | [diff] [blame] | 1927 | if (host->ops->set_power) |
| 1928 | host->ops->set_power(host, ios->power_mode, ios->vdd); |
| 1929 | else |
Asutosh Das | 30ec599 | 2013-11-08 12:31:48 +0530 | [diff] [blame] | 1930 | if (!host->ops->enable_controller_clock && (ios->power_mode & |
| 1931 | (MMC_POWER_UP | |
| 1932 | MMC_POWER_ON))) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1933 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 1934 | |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 1935 | spin_lock_irqsave(&host->lock, flags); |
| 1936 | |
Philip Rakity | 643a81f | 2010-09-23 08:24:32 -0700 | [diff] [blame] | 1937 | if (host->ops->platform_send_init_74_clocks) |
| 1938 | host->ops->platform_send_init_74_clocks(host, ios->power_mode); |
| 1939 | |
Russell King | 2317f56 | 2014-04-25 12:57:07 +0100 | [diff] [blame] | 1940 | host->ops->set_bus_width(host, ios->bus_width); |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 1941 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 1942 | ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL); |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1943 | |
Philip Rakity | 3ab9c8d | 2010-10-06 11:57:23 -0700 | [diff] [blame] | 1944 | if ((ios->timing == MMC_TIMING_SD_HS || |
| 1945 | ios->timing == MMC_TIMING_MMC_HS) |
| 1946 | && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1947 | ctrl |= SDHCI_CTRL_HISPD; |
| 1948 | else |
| 1949 | ctrl &= ~SDHCI_CTRL_HISPD; |
| 1950 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1951 | if (host->version >= SDHCI_SPEC_300) { |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1952 | u16 clk, ctrl_2; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1953 | |
| 1954 | /* In case of UHS-I modes, set High Speed Enable */ |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 1955 | if ((ios->timing == MMC_TIMING_MMC_HS400) || |
| 1956 | (ios->timing == MMC_TIMING_MMC_HS200) || |
Seungwon Jeon | bb8175a | 2014-03-14 21:12:48 +0900 | [diff] [blame] | 1957 | (ios->timing == MMC_TIMING_MMC_DDR52) || |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 1958 | (ios->timing == MMC_TIMING_UHS_SDR50) || |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1959 | (ios->timing == MMC_TIMING_UHS_SDR104) || |
| 1960 | (ios->timing == MMC_TIMING_UHS_DDR50) || |
Alexander Elbs | dd8df17 | 2012-01-03 23:26:53 -0500 | [diff] [blame] | 1961 | (ios->timing == MMC_TIMING_UHS_SDR25)) |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 1962 | ctrl |= SDHCI_CTRL_HISPD; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1963 | |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 1964 | if (!host->preset_enabled) { |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 1965 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1966 | /* |
| 1967 | * We only need to set Driver Strength if the |
| 1968 | * preset value enable is not set. |
| 1969 | */ |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 1970 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1971 | ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK; |
| 1972 | if (ios->drv_type == MMC_SET_DRIVER_TYPE_A) |
| 1973 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A; |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 1974 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B) |
| 1975 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B; |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1976 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C) |
| 1977 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C; |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 1978 | else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D) |
| 1979 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D; |
| 1980 | else { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 1981 | pr_warn("%s: invalid driver type, default to driver type B\n", |
| 1982 | mmc_hostname(mmc)); |
Petri Gynther | 43e943a | 2015-05-20 14:35:00 -0700 | [diff] [blame] | 1983 | ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B; |
| 1984 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1985 | |
| 1986 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 1987 | } else { |
| 1988 | /* |
| 1989 | * According to SDHC Spec v3.00, if the Preset Value |
| 1990 | * Enable in the Host Control 2 register is set, we |
| 1991 | * need to reset SD Clock Enable before changing High |
| 1992 | * Speed Enable to avoid generating clock gliches. |
| 1993 | */ |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 1994 | |
| 1995 | /* Reset SD Clock Enable */ |
| 1996 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 1997 | clk &= ~SDHCI_CLOCK_CARD_EN; |
| 1998 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 1999 | |
| 2000 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
| 2001 | |
| 2002 | /* Re-enable SD Clock */ |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2003 | if (ios->clock) { |
| 2004 | spin_unlock_irqrestore(&host->lock, flags); |
Sujit Reddy Thumma | fbe7d86 | 2014-01-21 17:22:05 +0530 | [diff] [blame] | 2005 | host->ops->set_clock(host, host->clock); |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2006 | spin_lock_irqsave(&host->lock, flags); |
| 2007 | } |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2008 | } |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2009 | |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2010 | /* Reset SD Clock Enable */ |
| 2011 | clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL); |
| 2012 | clk &= ~SDHCI_CLOCK_CARD_EN; |
| 2013 | sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL); |
| 2014 | |
Russell King | 96d7b78 | 2014-04-25 12:59:26 +0100 | [diff] [blame] | 2015 | host->ops->set_uhs_signaling(host, ios->timing); |
Russell King | d975f12 | 2014-04-25 12:59:31 +0100 | [diff] [blame] | 2016 | host->timing = ios->timing; |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2017 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2018 | if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) && |
| 2019 | ((ios->timing == MMC_TIMING_UHS_SDR12) || |
| 2020 | (ios->timing == MMC_TIMING_UHS_SDR25) || |
| 2021 | (ios->timing == MMC_TIMING_UHS_SDR50) || |
| 2022 | (ios->timing == MMC_TIMING_UHS_SDR104) || |
Jisheng Zhang | 0dafa60 | 2015-08-18 16:21:39 +0800 | [diff] [blame] | 2023 | (ios->timing == MMC_TIMING_UHS_DDR50) || |
| 2024 | (ios->timing == MMC_TIMING_MMC_DDR52))) { |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2025 | u16 preset; |
| 2026 | |
| 2027 | sdhci_enable_preset_value(host, true); |
| 2028 | preset = sdhci_get_preset_value(host); |
| 2029 | ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK) |
| 2030 | >> SDHCI_PRESET_DRV_SHIFT; |
| 2031 | } |
| 2032 | |
Arindam Nath | 49c468f | 2011-05-05 12:19:01 +0530 | [diff] [blame] | 2033 | /* Re-enable SD Clock */ |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2034 | if (ios->clock) { |
| 2035 | spin_unlock_irqrestore(&host->lock, flags); |
Sujit Reddy Thumma | fbe7d86 | 2014-01-21 17:22:05 +0530 | [diff] [blame] | 2036 | host->ops->set_clock(host, host->clock); |
Venkat Gopalakrishnan | 745734f | 2015-01-12 19:52:35 -0800 | [diff] [blame] | 2037 | spin_lock_irqsave(&host->lock, flags); |
| 2038 | } |
Arindam Nath | 758535c | 2011-05-05 12:19:00 +0530 | [diff] [blame] | 2039 | } else |
| 2040 | sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 2041 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2042 | spin_unlock_irqrestore(&host->lock, flags); |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 2043 | /* |
| 2044 | * Some (ENE) controllers go apeshit on some ios operation, |
| 2045 | * signalling timeout and CRC errors even on CMD0. Resetting |
| 2046 | * it on each ios seems to solve the problem. |
| 2047 | */ |
Mohammad Jamal | c63705e | 2015-01-13 20:47:24 +0530 | [diff] [blame] | 2048 | if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 2049 | sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA); |
Leandro Dorileo | b835226 | 2007-07-25 23:47:04 +0200 | [diff] [blame] | 2050 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2051 | /* |
| 2052 | * Reset the chip on each power off. |
| 2053 | * Should clear out any weird states. |
| 2054 | */ |
| 2055 | if (ios->power_mode == MMC_POWER_OFF) { |
| 2056 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
| 2057 | sdhci_reinit(host); |
| 2058 | sdhci_set_power(host, ios->power_mode, ios->vdd); |
| 2059 | } |
| 2060 | if (!ios->clock) |
Venkat Gopalakrishnan | 766b745 | 2015-03-10 15:51:23 -0700 | [diff] [blame] | 2061 | host->ops->set_clock(host, ios->clock); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2062 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2063 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2064 | } |
| 2065 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2066 | static int sdhci_get_cd(struct mmc_host *mmc) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2067 | { |
| 2068 | struct sdhci_host *host = mmc_priv(mmc); |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2069 | int gpio_cd = mmc_gpio_get_cd(mmc); |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2070 | |
| 2071 | if (host->flags & SDHCI_DEVICE_DEAD) |
| 2072 | return 0; |
| 2073 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2074 | /* If nonremovable, assume that the card is always present. */ |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 2075 | if (!mmc_card_is_removable(host->mmc)) |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2076 | return 1; |
| 2077 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2078 | /* |
| 2079 | * Try slot gpio detect, if defined it take precedence |
| 2080 | * over build in controller functionality |
| 2081 | */ |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 2082 | if (gpio_cd >= 0) |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2083 | return !!gpio_cd; |
| 2084 | |
Ivan T. Ivanov | 88af565 | 2015-07-06 15:16:19 +0300 | [diff] [blame] | 2085 | /* If polling, assume that the card is always present. */ |
| 2086 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) |
| 2087 | return 1; |
| 2088 | |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2089 | /* Host native card detect */ |
| 2090 | return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); |
| 2091 | } |
| 2092 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2093 | static int sdhci_check_ro(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2094 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2095 | unsigned long flags; |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2096 | int is_readonly; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2097 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2098 | spin_lock_irqsave(&host->lock, flags); |
| 2099 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 2100 | if (host->flags & SDHCI_DEVICE_DEAD) |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2101 | is_readonly = 0; |
| 2102 | else if (host->ops->get_ro) |
| 2103 | is_readonly = host->ops->get_ro(host); |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 2104 | else |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2105 | is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE) |
| 2106 | & SDHCI_WRITE_PROTECT); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2107 | |
| 2108 | spin_unlock_irqrestore(&host->lock, flags); |
| 2109 | |
Wolfram Sang | 2dfb579 | 2010-10-15 12:21:01 +0200 | [diff] [blame] | 2110 | /* This quirk needs to be replaced by a callback-function later */ |
| 2111 | return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ? |
| 2112 | !is_readonly : is_readonly; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2115 | #define SAMPLE_COUNT 5 |
| 2116 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2117 | static int sdhci_get_ro(struct mmc_host *mmc) |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2118 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2119 | struct sdhci_host *host = mmc_priv(mmc); |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2120 | int i, ro_count; |
| 2121 | |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2122 | if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT)) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2123 | return sdhci_check_ro(host); |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2124 | |
| 2125 | ro_count = 0; |
| 2126 | for (i = 0; i < SAMPLE_COUNT; i++) { |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2127 | if (sdhci_check_ro(host)) { |
Takashi Iwai | 82b0e23 | 2011-04-21 20:26:38 +0200 | [diff] [blame] | 2128 | if (++ro_count > SAMPLE_COUNT / 2) |
| 2129 | return 1; |
| 2130 | } |
| 2131 | msleep(30); |
| 2132 | } |
| 2133 | return 0; |
| 2134 | } |
| 2135 | |
Adrian Hunter | 20758b6 | 2011-08-29 16:42:12 +0300 | [diff] [blame] | 2136 | static void sdhci_hw_reset(struct mmc_host *mmc) |
| 2137 | { |
| 2138 | struct sdhci_host *host = mmc_priv(mmc); |
| 2139 | |
| 2140 | if (host->ops && host->ops->hw_reset) |
| 2141 | host->ops->hw_reset(host); |
| 2142 | } |
| 2143 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2144 | static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable) |
| 2145 | { |
Russell King | be13855 | 2014-04-25 12:55:56 +0100 | [diff] [blame] | 2146 | if (!(host->flags & SDHCI_DEVICE_DEAD)) { |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2147 | if (enable) |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2148 | host->ier |= SDHCI_INT_CARD_INT; |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2149 | else |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2150 | host->ier &= ~SDHCI_INT_CARD_INT; |
| 2151 | |
| 2152 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2153 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2154 | mmiowb(); |
| 2155 | } |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2156 | } |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2157 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2158 | static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 2159 | { |
| 2160 | struct sdhci_host *host = mmc_priv(mmc); |
| 2161 | unsigned long flags; |
| 2162 | |
Hans de Goede | fa3b4f4 | 2017-03-26 13:14:45 +0200 | [diff] [blame] | 2163 | if (enable) |
| 2164 | pm_runtime_get_noresume(host->mmc->parent); |
| 2165 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2166 | spin_lock_irqsave(&host->lock, flags); |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 2167 | if (enable) |
| 2168 | host->flags |= SDHCI_SDIO_IRQ_ENABLED; |
| 2169 | else |
| 2170 | host->flags &= ~SDHCI_SDIO_IRQ_ENABLED; |
| 2171 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2172 | sdhci_enable_sdio_irq_nolock(host, enable); |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2173 | spin_unlock_irqrestore(&host->lock, flags); |
Hans de Goede | fa3b4f4 | 2017-03-26 13:14:45 +0200 | [diff] [blame] | 2174 | |
| 2175 | if (!enable) |
| 2176 | pm_runtime_put_noidle(host->mmc->parent); |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2179 | static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc, |
| 2180 | struct mmc_ios *ios) |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2181 | { |
Dong Aisheng | ded97e0 | 2016-04-16 01:29:25 +0800 | [diff] [blame] | 2182 | struct sdhci_host *host = mmc_priv(mmc); |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2183 | u16 ctrl; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2184 | int ret; |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2185 | |
| 2186 | /* |
| 2187 | * Signal Voltage Switching is only applicable for Host Controllers |
| 2188 | * v3.00 and above. |
| 2189 | */ |
| 2190 | if (host->version < SDHCI_SPEC_300) |
| 2191 | return 0; |
| 2192 | |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 2193 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2194 | |
Fabio Estevam | 21f5998 | 2013-02-14 10:35:03 -0200 | [diff] [blame] | 2195 | switch (ios->signal_voltage) { |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2196 | case MMC_SIGNAL_VOLTAGE_330: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2197 | if (!(host->flags & SDHCI_SIGNALING_330)) |
| 2198 | return -EINVAL; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2199 | /* Set 1.8V Signal Enable in the Host Control2 register to 0 */ |
| 2200 | ctrl &= ~SDHCI_CTRL_VDD_180; |
| 2201 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 2202 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2203 | host->ops->check_power_status(host, REQ_IO_HIGH); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2204 | |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2205 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2206 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2207 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2208 | pr_warn("%s: Switching to 3.3V signalling voltage failed\n", |
| 2209 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2210 | return -EIO; |
| 2211 | } |
| 2212 | } |
| 2213 | /* Wait for 5ms */ |
| 2214 | usleep_range(5000, 5500); |
| 2215 | |
| 2216 | /* 3.3V regulator output should be stable within 5 ms */ |
| 2217 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2218 | if (!(ctrl & SDHCI_CTRL_VDD_180)) |
| 2219 | return 0; |
| 2220 | |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2221 | pr_warn("%s: 3.3V regulator output did not became stable\n", |
| 2222 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2223 | |
| 2224 | return -EAGAIN; |
| 2225 | case MMC_SIGNAL_VOLTAGE_180: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2226 | if (!(host->flags & SDHCI_SIGNALING_180)) |
| 2227 | return -EINVAL; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2228 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2229 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2230 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2231 | pr_warn("%s: Switching to 1.8V signalling voltage failed\n", |
| 2232 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2233 | return -EIO; |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * Enable 1.8V Signal Enable in the Host Control2 |
| 2239 | * register |
| 2240 | */ |
| 2241 | ctrl |= SDHCI_CTRL_VDD_180; |
| 2242 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Sahitya Tummala | 63d235d | 2013-02-21 10:09:49 +0530 | [diff] [blame] | 2243 | if (host->ops->check_power_status) |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2244 | host->ops->check_power_status(host, REQ_IO_LOW); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2245 | |
Vincent Yang | 9d967a6 | 2015-01-20 16:05:15 +0800 | [diff] [blame] | 2246 | /* Some controller need to do more when switching */ |
| 2247 | if (host->ops->voltage_switch) |
| 2248 | host->ops->voltage_switch(host); |
| 2249 | |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2250 | /* 1.8V regulator output should be stable within 5 ms */ |
| 2251 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2252 | if (ctrl & SDHCI_CTRL_VDD_180) |
| 2253 | return 0; |
| 2254 | |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2255 | pr_warn("%s: 1.8V regulator output did not became stable\n", |
| 2256 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2257 | |
| 2258 | return -EAGAIN; |
| 2259 | case MMC_SIGNAL_VOLTAGE_120: |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 2260 | if (!(host->flags & SDHCI_SIGNALING_120)) |
| 2261 | return -EINVAL; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 2262 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Dong Aisheng | 761daa3 | 2016-07-12 15:46:10 +0800 | [diff] [blame] | 2263 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2264 | if (ret) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2265 | pr_warn("%s: Switching to 1.2V signalling voltage failed\n", |
| 2266 | mmc_hostname(mmc)); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2267 | return -EIO; |
| 2268 | } |
| 2269 | } |
| 2270 | return 0; |
| 2271 | default: |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 2272 | /* No signal voltage switch required */ |
| 2273 | return 0; |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2274 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 2275 | } |
| 2276 | |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2277 | static int sdhci_card_busy(struct mmc_host *mmc) |
| 2278 | { |
| 2279 | struct sdhci_host *host = mmc_priv(mmc); |
| 2280 | u32 present_state; |
| 2281 | |
Adrian Hunter | e613cc4 | 2016-06-23 14:00:58 +0300 | [diff] [blame] | 2282 | /* Check whether DAT[0] is 0 */ |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2283 | present_state = sdhci_readl(host, SDHCI_PRESENT_STATE); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2284 | |
Adrian Hunter | e613cc4 | 2016-06-23 14:00:58 +0300 | [diff] [blame] | 2285 | return !(present_state & SDHCI_DATA_0_LVL_MASK); |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2286 | } |
| 2287 | |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2288 | static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) |
| 2289 | { |
| 2290 | struct sdhci_host *host = mmc_priv(mmc); |
| 2291 | unsigned long flags; |
| 2292 | |
| 2293 | spin_lock_irqsave(&host->lock, flags); |
| 2294 | host->flags |= SDHCI_HS400_TUNING; |
| 2295 | spin_unlock_irqrestore(&host->lock, flags); |
| 2296 | |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2300 | static int sdhci_enhanced_strobe(struct mmc_host *mmc) |
| 2301 | { |
| 2302 | struct sdhci_host *host = mmc_priv(mmc); |
| 2303 | int err = 0; |
| 2304 | |
| 2305 | sdhci_runtime_pm_get(host); |
| 2306 | if (host->ops->enhanced_strobe) |
| 2307 | err = host->ops->enhanced_strobe(host); |
| 2308 | sdhci_runtime_pm_put(host); |
| 2309 | |
| 2310 | return err; |
| 2311 | } |
| 2312 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2313 | static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode) |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2314 | { |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2315 | struct sdhci_host *host = mmc_priv(mmc); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2316 | u16 ctrl; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2317 | int tuning_loop_counter = MAX_TUNING_LOOP; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2318 | int err = 0; |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2319 | unsigned long flags; |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2320 | unsigned int tuning_count = 0; |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2321 | bool hs400_tuning; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2322 | |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2323 | spin_lock_irqsave(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2324 | |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2325 | hs400_tuning = host->flags & SDHCI_HS400_TUNING; |
| 2326 | host->flags &= ~SDHCI_HS400_TUNING; |
| 2327 | |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2328 | if (host->tuning_mode == SDHCI_TUNING_MODE_1) |
| 2329 | tuning_count = host->tuning_count; |
| 2330 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2331 | /* |
Weijun Yang | 9faac7b | 2015-10-04 12:04:12 +0000 | [diff] [blame] | 2332 | * The Host Controller needs tuning in case of SDR104 and DDR50 |
| 2333 | * mode, and for SDR50 mode when Use Tuning for SDR50 is set in |
| 2334 | * the Capabilities register. |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2335 | * If the Host Controller supports the HS200 mode then the |
| 2336 | * tuning function has to be executed. |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2337 | */ |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2338 | switch (host->timing) { |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2339 | /* HS400 tuning is done in HS200 mode */ |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 2340 | case MMC_TIMING_MMC_HS400: |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2341 | err = -EINVAL; |
| 2342 | goto out_unlock; |
| 2343 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2344 | case MMC_TIMING_MMC_HS200: |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2345 | /* |
| 2346 | * Periodic re-tuning for HS400 is not expected to be needed, so |
| 2347 | * disable it here. |
| 2348 | */ |
| 2349 | if (hs400_tuning) |
| 2350 | tuning_count = 0; |
| 2351 | break; |
| 2352 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2353 | case MMC_TIMING_UHS_SDR104: |
Weijun Yang | 9faac7b | 2015-10-04 12:04:12 +0000 | [diff] [blame] | 2354 | case MMC_TIMING_UHS_DDR50: |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2355 | break; |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2356 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2357 | case MMC_TIMING_UHS_SDR50: |
Adrian Hunter | 4228b21 | 2016-04-20 09:24:03 +0300 | [diff] [blame] | 2358 | if (host->flags & SDHCI_SDR50_NEEDS_TUNING) |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2359 | break; |
| 2360 | /* FALLTHROUGH */ |
| 2361 | |
| 2362 | default: |
Adrian Hunter | d519c86 | 2014-12-05 19:25:29 +0200 | [diff] [blame] | 2363 | goto out_unlock; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2364 | } |
| 2365 | |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2366 | if (host->ops->platform_execute_tuning) { |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2367 | spin_unlock_irqrestore(&host->lock, flags); |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2368 | err = host->ops->platform_execute_tuning(host, opcode); |
Dong Aisheng | 4525181 | 2013-09-13 19:11:30 +0800 | [diff] [blame] | 2369 | return err; |
| 2370 | } |
| 2371 | |
Russell King | 4b6f37d | 2014-04-25 12:59:36 +0100 | [diff] [blame] | 2372 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2373 | ctrl |= SDHCI_CTRL_EXEC_TUNING; |
Vincent Yang | 67d0d04 | 2015-01-20 16:05:16 +0800 | [diff] [blame] | 2374 | if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND) |
| 2375 | ctrl |= SDHCI_CTRL_TUNED_CLK; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2376 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 2377 | |
| 2378 | /* |
| 2379 | * As per the Host Controller spec v3.00, tuning command |
| 2380 | * generates Buffer Read Ready interrupt, so enable that. |
| 2381 | * |
| 2382 | * Note: The spec clearly says that when tuning sequence |
| 2383 | * is being performed, the controller does not generate |
| 2384 | * interrupts other than Buffer Read Ready interrupt. But |
| 2385 | * to make sure we don't hit a controller bug, we _only_ |
| 2386 | * enable Buffer Read Ready interrupt here. |
| 2387 | */ |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2388 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE); |
| 2389 | sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2390 | |
| 2391 | /* |
| 2392 | * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number |
Simon Horman | 1473bdd | 2016-05-13 13:24:31 +0900 | [diff] [blame] | 2393 | * of loops reaches 40 times. |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2394 | */ |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2395 | do { |
| 2396 | struct mmc_command cmd = {0}; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2397 | struct mmc_request mrq = {NULL}; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2398 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2399 | cmd.opcode = opcode; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2400 | cmd.arg = 0; |
| 2401 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2402 | cmd.retries = 0; |
| 2403 | cmd.data = NULL; |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2404 | cmd.mrq = &mrq; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2405 | cmd.error = 0; |
| 2406 | |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2407 | if (tuning_loop_counter-- == 0) |
| 2408 | break; |
| 2409 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2410 | mrq.cmd = &cmd; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2411 | |
| 2412 | /* |
| 2413 | * In response to CMD19, the card sends 64 bytes of tuning |
| 2414 | * block to the Host Controller. So we set the block size |
| 2415 | * to 64 here. |
| 2416 | */ |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2417 | if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) { |
| 2418 | if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2419 | sdhci_set_blk_size_reg(host, 128, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2420 | else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2421 | sdhci_set_blk_size_reg(host, 64, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2422 | } else { |
Subhash Jadavani | 3ed9771 | 2014-09-02 17:55:54 -0700 | [diff] [blame] | 2423 | sdhci_set_blk_size_reg(host, 64, 7); |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2424 | } |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2425 | |
| 2426 | /* |
| 2427 | * The tuning block is sent by the card to the host controller. |
| 2428 | * So we set the TRNS_READ bit in the Transfer Mode register. |
| 2429 | * This also takes care of setting DMA Enable and Multi Block |
| 2430 | * Select in the same register to 0. |
| 2431 | */ |
| 2432 | sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); |
| 2433 | |
| 2434 | sdhci_send_command(host, &cmd); |
| 2435 | |
| 2436 | host->cmd = NULL; |
Adrian Hunter | 07c161b | 2016-06-29 16:24:38 +0300 | [diff] [blame] | 2437 | sdhci_del_timer(host, &mrq); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2438 | |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2439 | spin_unlock_irqrestore(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2440 | /* Wait for Buffer Read Ready interrupt */ |
Christopher Freeman | 622b5f3 | 2016-08-17 13:34:27 -0400 | [diff] [blame] | 2441 | wait_event_timeout(host->buf_ready_int, |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2442 | (host->tuning_done == 1), |
| 2443 | msecs_to_jiffies(50)); |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2444 | spin_lock_irqsave(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2445 | |
| 2446 | if (!host->tuning_done) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2447 | 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] | 2448 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2449 | ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 2450 | ctrl &= ~SDHCI_CTRL_EXEC_TUNING; |
| 2451 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
| 2452 | |
Adrian Hunter | cee9358 | 2016-12-02 15:14:20 +0200 | [diff] [blame] | 2453 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2454 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
| 2455 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2456 | err = -EIO; |
Adrian Hunter | cee9358 | 2016-12-02 15:14:20 +0200 | [diff] [blame] | 2457 | |
| 2458 | if (cmd.opcode != MMC_SEND_TUNING_BLOCK_HS200) |
| 2459 | goto out; |
| 2460 | |
| 2461 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2462 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 2463 | |
| 2464 | spin_unlock_irqrestore(&host->lock, flags); |
| 2465 | |
| 2466 | memset(&cmd, 0, sizeof(cmd)); |
| 2467 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 2468 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 2469 | cmd.busy_timeout = 50; |
| 2470 | mmc_wait_for_cmd(mmc, &cmd, 0); |
| 2471 | |
| 2472 | spin_lock_irqsave(&host->lock, flags); |
| 2473 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2474 | goto out; |
| 2475 | } |
| 2476 | |
| 2477 | host->tuning_done = 0; |
| 2478 | |
| 2479 | ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
Nick Sanders | 197160d | 2014-05-06 18:52:38 -0700 | [diff] [blame] | 2480 | |
| 2481 | /* eMMC spec does not require a delay between tuning cycles */ |
| 2482 | if (opcode == MMC_SEND_TUNING_BLOCK) |
| 2483 | mdelay(1); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2484 | } while (ctrl & SDHCI_CTRL_EXEC_TUNING); |
| 2485 | |
| 2486 | /* |
| 2487 | * The Host Driver has exhausted the maximum number of loops allowed, |
| 2488 | * so use fixed sampling frequency. |
| 2489 | */ |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2490 | if (tuning_loop_counter < 0) { |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2491 | ctrl &= ~SDHCI_CTRL_TUNED_CLK; |
| 2492 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Al Cooper | 7ce45e9 | 2014-05-09 11:34:07 -0400 | [diff] [blame] | 2493 | } |
| 2494 | if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2495 | 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] | 2496 | err = -EIO; |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | out: |
Adrian Hunter | 38e40bf | 2014-12-05 19:25:30 +0200 | [diff] [blame] | 2500 | if (tuning_count) { |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 2501 | /* |
| 2502 | * In case tuning fails, host controllers which support |
| 2503 | * re-tuning can try tuning again at a later time, when the |
| 2504 | * re-tuning timer expires. So for these controllers, we |
| 2505 | * return 0. Since there might be other controllers who do not |
| 2506 | * have this capability, we return error for them. |
| 2507 | */ |
| 2508 | err = 0; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 2509 | } |
| 2510 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 2511 | host->mmc->retune_period = err ? 0 : tuning_count; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 2512 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 2513 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 2514 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Adrian Hunter | d519c86 | 2014-12-05 19:25:29 +0200 | [diff] [blame] | 2515 | out_unlock: |
Aisheng Dong | 2b35bd8 | 2013-12-23 19:13:04 +0800 | [diff] [blame] | 2516 | spin_unlock_irqrestore(&host->lock, flags); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2517 | return err; |
| 2518 | } |
| 2519 | |
Adrian Hunter | cb84964 | 2015-02-06 14:12:59 +0200 | [diff] [blame] | 2520 | static int sdhci_select_drive_strength(struct mmc_card *card, |
| 2521 | unsigned int max_dtr, int host_drv, |
| 2522 | int card_drv, int *drv_type) |
| 2523 | { |
| 2524 | struct sdhci_host *host = mmc_priv(card->host); |
| 2525 | |
| 2526 | if (!host->ops->select_drive_strength) |
| 2527 | return 0; |
| 2528 | |
| 2529 | return host->ops->select_drive_strength(host, card, max_dtr, host_drv, |
| 2530 | card_drv, drv_type); |
| 2531 | } |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 2532 | |
| 2533 | static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable) |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2534 | { |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2535 | /* Host Controller v3.00 defines preset value registers */ |
| 2536 | if (host->version < SDHCI_SPEC_300) |
| 2537 | return; |
| 2538 | |
Sahitya Tummala | 314162c | 2013-04-12 12:11:20 +0530 | [diff] [blame] | 2539 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE) |
| 2540 | return; |
| 2541 | |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2542 | /* |
| 2543 | * We only enable or disable Preset Value if they are not already |
| 2544 | * enabled or disabled respectively. Otherwise, we bail out. |
| 2545 | */ |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2546 | if (host->preset_enabled != enable) { |
| 2547 | u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2548 | |
| 2549 | if (enable) |
| 2550 | ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE; |
| 2551 | else |
| 2552 | ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE; |
| 2553 | |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2554 | sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2); |
Russell King | da91a8f | 2014-04-25 13:00:12 +0100 | [diff] [blame] | 2555 | |
| 2556 | if (enable) |
| 2557 | host->flags |= SDHCI_PV_ENABLED; |
| 2558 | else |
| 2559 | host->flags &= ~SDHCI_PV_ENABLED; |
| 2560 | |
| 2561 | host->preset_enabled = enable; |
Arindam Nath | 4d55c5a | 2011-05-05 12:19:05 +0530 | [diff] [blame] | 2562 | } |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2563 | } |
| 2564 | |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2565 | static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 2566 | int err) |
| 2567 | { |
| 2568 | struct sdhci_host *host = mmc_priv(mmc); |
| 2569 | struct mmc_data *data = mrq->data; |
| 2570 | |
Russell King | f48f039 | 2016-01-26 13:40:32 +0000 | [diff] [blame] | 2571 | if (data->host_cookie != COOKIE_UNMAPPED) |
Russell King | 771a3dc | 2016-01-26 13:40:53 +0000 | [diff] [blame] | 2572 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 2573 | data->flags & MMC_DATA_WRITE ? |
| 2574 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 2575 | |
| 2576 | data->host_cookie = COOKIE_UNMAPPED; |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2577 | |
| 2578 | if (host->ops->pre_req) |
| 2579 | host->ops->pre_req(host, mrq); |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2580 | } |
| 2581 | |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2582 | static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 2583 | bool is_first_req) |
| 2584 | { |
| 2585 | struct sdhci_host *host = mmc_priv(mmc); |
| 2586 | |
Haibo Chen | d31911b | 2015-08-25 10:02:11 +0800 | [diff] [blame] | 2587 | mrq->data->host_cookie = COOKIE_UNMAPPED; |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2588 | |
| 2589 | if (host->flags & SDHCI_REQ_USE_DMA) |
Russell King | 94538e5 | 2016-01-26 13:40:37 +0000 | [diff] [blame] | 2590 | sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED); |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2593 | static inline bool sdhci_has_requests(struct sdhci_host *host) |
| 2594 | { |
| 2595 | return host->cmd || host->data_cmd; |
| 2596 | } |
| 2597 | |
| 2598 | static void sdhci_error_out_mrqs(struct sdhci_host *host, int err) |
| 2599 | { |
| 2600 | if (host->data_cmd) { |
| 2601 | host->data_cmd->error = err; |
| 2602 | sdhci_finish_mrq(host, host->data_cmd->mrq); |
| 2603 | } |
| 2604 | |
| 2605 | if (host->cmd) { |
| 2606 | host->cmd->error = err; |
| 2607 | sdhci_finish_mrq(host, host->cmd->mrq); |
| 2608 | } |
| 2609 | } |
| 2610 | |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2611 | static void sdhci_card_event(struct mmc_host *mmc) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2612 | { |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2613 | struct sdhci_host *host = mmc_priv(mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2614 | unsigned long flags; |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 2615 | int present; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2616 | |
Christian Daudt | 722e128 | 2013-06-20 14:26:36 -0700 | [diff] [blame] | 2617 | /* First check if client has provided their own card event */ |
| 2618 | if (host->ops->card_event) |
| 2619 | host->ops->card_event(host); |
| 2620 | |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 2621 | present = mmc->ops->get_cd(mmc); |
Krzysztof Kozlowski | 2836766 | 2015-01-05 10:50:15 +0100 | [diff] [blame] | 2622 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2623 | spin_lock_irqsave(&host->lock, flags); |
| 2624 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2625 | /* Check sdhci_has_requests() first in case we are runtime suspended */ |
| 2626 | if (sdhci_has_requests(host) && !present) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2627 | pr_err("%s: Card removed during transfer!\n", |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2628 | mmc_hostname(host->mmc)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2629 | pr_err("%s: Resetting controller.\n", |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2630 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2631 | |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 2632 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2633 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2634 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 2635 | sdhci_error_out_mrqs(host, -ENOMEDIUM); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | spin_unlock_irqrestore(&host->lock, flags); |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
Dov Levenglick | 1669f8e | 2015-07-20 16:50:03 +0300 | [diff] [blame] | 2641 | static void sdhci_detect(struct mmc_host *mmc, bool detected) |
| 2642 | { |
| 2643 | struct sdhci_host *host = mmc_priv(mmc); |
| 2644 | |
| 2645 | if (host->ops->detect) |
| 2646 | host->ops->detect(host, detected); |
| 2647 | } |
| 2648 | |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2649 | static int sdhci_late_init(struct mmc_host *mmc) |
| 2650 | { |
| 2651 | struct sdhci_host *host = mmc_priv(mmc); |
| 2652 | |
| 2653 | if (host->ops->init) |
| 2654 | host->ops->init(host); |
| 2655 | |
| 2656 | return 0; |
| 2657 | } |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2658 | static const struct mmc_host_ops sdhci_ops = { |
Gilad Broner | 07d92eb | 2015-09-29 16:57:21 +0300 | [diff] [blame] | 2659 | .init = sdhci_late_init, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2660 | .request = sdhci_request, |
Haibo Chen | 348487c | 2014-12-09 17:04:05 +0800 | [diff] [blame] | 2661 | .post_req = sdhci_post_req, |
| 2662 | .pre_req = sdhci_pre_req, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2663 | .set_ios = sdhci_set_ios, |
Kevin Liu | 94144a4 | 2013-02-28 17:35:53 +0800 | [diff] [blame] | 2664 | .get_cd = sdhci_get_cd, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2665 | .get_ro = sdhci_get_ro, |
| 2666 | .hw_reset = sdhci_hw_reset, |
| 2667 | .enable_sdio_irq = sdhci_enable_sdio_irq, |
| 2668 | .start_signal_voltage_switch = sdhci_start_signal_voltage_switch, |
Adrian Hunter | b5540ce | 2014-12-05 19:25:31 +0200 | [diff] [blame] | 2669 | .prepare_hs400_tuning = sdhci_prepare_hs400_tuning, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2670 | .execute_tuning = sdhci_execute_tuning, |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 2671 | .enhanced_strobe = sdhci_enhanced_strobe, |
Adrian Hunter | cb84964 | 2015-02-06 14:12:59 +0200 | [diff] [blame] | 2672 | .select_drive_strength = sdhci_select_drive_strength, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2673 | .card_event = sdhci_card_event, |
Kevin Liu | 20b92a3 | 2012-12-17 19:29:26 +0800 | [diff] [blame] | 2674 | .card_busy = sdhci_card_busy, |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 2675 | .enable = sdhci_enable, |
| 2676 | .disable = sdhci_disable, |
Sujit Reddy Thumma | 360bbf4 | 2013-06-19 20:15:37 +0530 | [diff] [blame] | 2677 | .notify_load = sdhci_notify_load, |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 2678 | .notify_halt = sdhci_notify_halt, |
Dov Levenglick | 1669f8e | 2015-07-20 16:50:03 +0300 | [diff] [blame] | 2679 | .detect = sdhci_detect, |
Guennadi Liakhovetski | 71e6921 | 2012-12-04 16:51:40 +0100 | [diff] [blame] | 2680 | }; |
| 2681 | |
| 2682 | /*****************************************************************************\ |
| 2683 | * * |
| 2684 | * Tasklets * |
| 2685 | * * |
| 2686 | \*****************************************************************************/ |
| 2687 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2688 | static bool sdhci_request_done(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2689 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2690 | unsigned long flags; |
| 2691 | struct mmc_request *mrq; |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2692 | int i; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2693 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2694 | spin_lock_irqsave(&host->lock, flags); |
| 2695 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2696 | for (i = 0; i < SDHCI_MAX_MRQS; i++) { |
| 2697 | mrq = host->mrqs_done[i]; |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2698 | if (mrq) |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2699 | break; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 2700 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2701 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2702 | if (!mrq) { |
| 2703 | spin_unlock_irqrestore(&host->lock, flags); |
| 2704 | return true; |
| 2705 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2706 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2707 | sdhci_del_timer(host, mrq); |
| 2708 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2709 | /* |
Russell King | 054cedf | 2016-01-26 13:40:42 +0000 | [diff] [blame] | 2710 | * Always unmap the data buffers if they were mapped by |
| 2711 | * sdhci_prepare_data() whenever we finish with a request. |
| 2712 | * This avoids leaking DMA mappings on error. |
| 2713 | */ |
| 2714 | if (host->flags & SDHCI_REQ_USE_DMA) { |
| 2715 | struct mmc_data *data = mrq->data; |
| 2716 | |
| 2717 | if (data && data->host_cookie == COOKIE_MAPPED) { |
| 2718 | dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, |
| 2719 | (data->flags & MMC_DATA_READ) ? |
| 2720 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 2721 | data->host_cookie = COOKIE_UNMAPPED; |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2726 | * The controller needs a reset of internal state machines |
| 2727 | * upon error conditions. |
| 2728 | */ |
Adrian Hunter | 0cc563c | 2016-06-29 16:24:28 +0300 | [diff] [blame] | 2729 | if (sdhci_needs_reset(host, mrq)) { |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2730 | /* |
| 2731 | * Do not finish until command and data lines are available for |
| 2732 | * reset. Note there can only be one other mrq, so it cannot |
| 2733 | * also be in mrqs_done, otherwise host->cmd and host->data_cmd |
| 2734 | * would both be null. |
| 2735 | */ |
| 2736 | if (host->cmd || host->data_cmd) { |
| 2737 | spin_unlock_irqrestore(&host->lock, flags); |
| 2738 | return true; |
| 2739 | } |
| 2740 | |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2741 | /* Some controllers need this kick or reset won't work here */ |
Andy Shevchenko | 8213af3 | 2013-01-07 16:31:08 +0200 | [diff] [blame] | 2742 | if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2743 | /* This is to force an update */ |
Russell King | 1771059 | 2014-04-25 12:58:55 +0100 | [diff] [blame] | 2744 | host->ops->set_clock(host, host->clock); |
Pierre Ossman | 645289d | 2006-06-30 02:22:33 -0700 | [diff] [blame] | 2745 | |
| 2746 | /* Spec says we should do both at the same time, but Ricoh |
| 2747 | controllers do not like that. */ |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2748 | sdhci_do_reset(host, SDHCI_RESET_CMD); |
| 2749 | sdhci_do_reset(host, SDHCI_RESET_DATA); |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 2750 | |
| 2751 | host->pending_reset = false; |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 2752 | } else { |
| 2753 | if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT) |
| 2754 | sdhci_reset(host, SDHCI_RESET_DATA); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2755 | } |
| 2756 | |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2757 | if (!sdhci_has_requests(host)) |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 2758 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 2759 | sdhci_led_deactivate(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2760 | |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2761 | host->mrqs_done[i] = NULL; |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2762 | host->auto_cmd_err_sts = 0; |
Adrian Hunter | 6ebebea | 2016-11-02 15:49:08 +0200 | [diff] [blame] | 2763 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2764 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2765 | spin_unlock_irqrestore(&host->lock, flags); |
| 2766 | |
| 2767 | mmc_request_done(host->mmc, mrq); |
Adrian Hunter | 4e9f8fe | 2016-06-29 16:24:34 +0300 | [diff] [blame] | 2768 | |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
| 2772 | static void sdhci_tasklet_finish(unsigned long param) |
| 2773 | { |
| 2774 | struct sdhci_host *host = (struct sdhci_host *)param; |
| 2775 | |
| 2776 | while (!sdhci_request_done(host)) |
| 2777 | ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | static void sdhci_timeout_timer(unsigned long data) |
| 2781 | { |
| 2782 | struct sdhci_host *host; |
| 2783 | unsigned long flags; |
| 2784 | |
| 2785 | host = (struct sdhci_host*)data; |
| 2786 | |
| 2787 | spin_lock_irqsave(&host->lock, flags); |
| 2788 | |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2789 | if (host->cmd && !sdhci_data_line_cmd(host->cmd)) { |
| 2790 | pr_err("%s: Timeout waiting for hardware cmd interrupt.\n", |
| 2791 | mmc_hostname(host->mmc)); |
| 2792 | sdhci_dumpregs(host); |
| 2793 | |
| 2794 | host->cmd->error = -ETIMEDOUT; |
| 2795 | sdhci_finish_mrq(host, host->cmd->mrq); |
| 2796 | } |
| 2797 | |
| 2798 | mmiowb(); |
| 2799 | spin_unlock_irqrestore(&host->lock, flags); |
| 2800 | } |
| 2801 | |
| 2802 | static void sdhci_timeout_data_timer(unsigned long data) |
| 2803 | { |
| 2804 | struct sdhci_host *host; |
| 2805 | unsigned long flags; |
| 2806 | |
| 2807 | host = (struct sdhci_host *)data; |
| 2808 | |
| 2809 | spin_lock_irqsave(&host->lock, flags); |
| 2810 | |
| 2811 | if (host->data || host->data_cmd || |
| 2812 | (host->cmd && sdhci_data_line_cmd(host->cmd))) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2813 | pr_err("%s: Timeout waiting for hardware interrupt.\n", |
| 2814 | mmc_hostname(host->mmc)); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2815 | sdhci_dumpregs(host); |
| 2816 | |
| 2817 | if (host->data) { |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 2818 | pr_info("%s: bytes to transfer: %d transferred: %d\n", |
| 2819 | mmc_hostname(host->mmc), |
| 2820 | (host->data->blksz * host->data->blocks), |
| 2821 | (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) * |
| 2822 | sdhci_readw(host, SDHCI_BLOCK_COUNT)); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 2823 | host->data->error = -ETIMEDOUT; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2824 | sdhci_finish_data(host); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2825 | } else if (host->data_cmd) { |
| 2826 | host->data_cmd->error = -ETIMEDOUT; |
| 2827 | sdhci_finish_mrq(host, host->data_cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2828 | } else { |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 2829 | host->cmd->error = -ETIMEDOUT; |
| 2830 | sdhci_finish_mrq(host, host->cmd->mrq); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 2834 | mmiowb(); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2835 | spin_unlock_irqrestore(&host->lock, flags); |
| 2836 | } |
| 2837 | |
| 2838 | /*****************************************************************************\ |
| 2839 | * * |
| 2840 | * Interrupt handling * |
| 2841 | * * |
| 2842 | \*****************************************************************************/ |
| 2843 | |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 2844 | static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2845 | { |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2846 | u16 auto_cmd_status; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2847 | if (!host->cmd) { |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 2848 | /* |
| 2849 | * SDHCI recovers from errors by resetting the cmd and data |
| 2850 | * circuits. Until that is done, there very well might be more |
| 2851 | * interrupts, so ignore them in that case. |
| 2852 | */ |
| 2853 | if (host->pending_reset) |
| 2854 | return; |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 2855 | pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n", |
| 2856 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2857 | sdhci_dumpregs(host); |
| 2858 | return; |
| 2859 | } |
| 2860 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 2861 | trace_mmc_cmd_rw_end(host->cmd->opcode, intmask, |
| 2862 | sdhci_readl(host, SDHCI_RESPONSE)); |
| 2863 | |
Russell King | ec014cb | 2016-01-26 13:39:39 +0000 | [diff] [blame] | 2864 | if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2865 | SDHCI_INT_END_BIT | SDHCI_INT_INDEX | |
| 2866 | SDHCI_INT_AUTO_CMD_ERR)) { |
Russell King | ec014cb | 2016-01-26 13:39:39 +0000 | [diff] [blame] | 2867 | if (intmask & SDHCI_INT_TIMEOUT) |
| 2868 | host->cmd->error = -ETIMEDOUT; |
| 2869 | else |
| 2870 | host->cmd->error = -EILSEQ; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2871 | |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2872 | if (intmask & SDHCI_INT_AUTO_CMD_ERR) { |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2873 | auto_cmd_status = host->auto_cmd_err_sts; |
Konstantin Dorfman | 4b0bcd3 | 2015-06-02 17:41:53 +0300 | [diff] [blame] | 2874 | pr_err_ratelimited("%s: %s: AUTO CMD err sts 0x%08x\n", |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 2875 | mmc_hostname(host->mmc), __func__, auto_cmd_status); |
Asutosh Das | 09f36d0 | 2013-07-23 16:20:34 +0530 | [diff] [blame] | 2876 | if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC | |
| 2877 | SDHCI_AUTO_CMD_INDEX_ERR | |
| 2878 | SDHCI_AUTO_CMD_ENDBIT_ERR)) |
| 2879 | host->cmd->error = -EIO; |
| 2880 | else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR) |
| 2881 | host->cmd->error = -ETIMEDOUT; |
| 2882 | else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR) |
| 2883 | host->cmd->error = -EILSEQ; |
| 2884 | } |
| 2885 | |
Russell King | 71fcbda | 2016-01-26 13:39:45 +0000 | [diff] [blame] | 2886 | /* |
| 2887 | * If this command initiates a data phase and a response |
| 2888 | * CRC error is signalled, the card can start transferring |
| 2889 | * data - the card may have received the command without |
| 2890 | * error. We must not terminate the mmc_request early. |
| 2891 | * |
| 2892 | * If the card did not receive the command or returned an |
| 2893 | * error which prevented it sending data, the data phase |
| 2894 | * will time out. |
| 2895 | */ |
| 2896 | if (host->cmd->data && |
| 2897 | (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) == |
| 2898 | SDHCI_INT_CRC) { |
| 2899 | host->cmd = NULL; |
| 2900 | return; |
| 2901 | } |
| 2902 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 2903 | sdhci_finish_mrq(host, host->cmd->mrq); |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2904 | return; |
| 2905 | } |
| 2906 | |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2907 | if (intmask & SDHCI_INT_RESPONSE) |
Pierre Ossman | 43b58b3 | 2007-07-25 23:15:27 +0200 | [diff] [blame] | 2908 | sdhci_finish_command(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2909 | } |
| 2910 | |
George G. Davis | 0957c33 | 2010-02-18 12:32:12 -0500 | [diff] [blame] | 2911 | #ifdef CONFIG_MMC_DEBUG |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 2912 | static void sdhci_adma_show_error(struct sdhci_host *host) |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2913 | { |
| 2914 | const char *name = mmc_hostname(host->mmc); |
Adrian Hunter | 1c3d5f6 | 2014-11-04 12:42:41 +0200 | [diff] [blame] | 2915 | void *desc = host->adma_table; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2916 | |
| 2917 | sdhci_dumpregs(host); |
| 2918 | |
| 2919 | while (true) { |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 2920 | struct sdhci_adma2_64_desc *dma_desc = desc; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2921 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 2922 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 2923 | DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n", |
| 2924 | name, desc, le32_to_cpu(dma_desc->addr_hi), |
| 2925 | le32_to_cpu(dma_desc->addr_lo), |
| 2926 | le16_to_cpu(dma_desc->len), |
| 2927 | le16_to_cpu(dma_desc->cmd)); |
| 2928 | else |
| 2929 | DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n", |
| 2930 | name, desc, le32_to_cpu(dma_desc->addr_lo), |
| 2931 | le16_to_cpu(dma_desc->len), |
| 2932 | le16_to_cpu(dma_desc->cmd)); |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2933 | |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 2934 | desc += host->desc_sz; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2935 | |
Adrian Hunter | 0545230 | 2014-11-04 12:42:45 +0200 | [diff] [blame] | 2936 | if (dma_desc->cmd & cpu_to_le16(ADMA2_END)) |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2937 | break; |
| 2938 | } |
| 2939 | } |
| 2940 | #else |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 2941 | static void sdhci_adma_show_error(struct sdhci_host *host) { } |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 2942 | #endif |
| 2943 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2944 | static void sdhci_data_irq(struct sdhci_host *host, u32 intmask) |
| 2945 | { |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2946 | u32 command; |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 2947 | bool pr_msg = false; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2948 | |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 2949 | command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)); |
| 2950 | trace_mmc_data_rw_end(command, intmask); |
| 2951 | |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2952 | /* CMD19 generates _only_ Buffer Read Ready interrupt */ |
| 2953 | if (intmask & SDHCI_INT_DATA_AVAIL) { |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 2954 | if (command == MMC_SEND_TUNING_BLOCK || |
| 2955 | command == MMC_SEND_TUNING_BLOCK_HS200) { |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 2956 | host->tuning_done = 1; |
| 2957 | wake_up(&host->buf_ready_int); |
| 2958 | return; |
| 2959 | } |
| 2960 | } |
| 2961 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2962 | if (!host->data) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 2963 | struct mmc_command *data_cmd = host->data_cmd; |
| 2964 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2965 | /* |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2966 | * The "data complete" interrupt is also used to |
| 2967 | * indicate that a busy state has ended. See comment |
| 2968 | * above in sdhci_cmd_irq(). |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2969 | */ |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 2970 | if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) { |
Matthieu CASTET | c5abd5e | 2014-08-14 16:03:17 +0200 | [diff] [blame] | 2971 | if (intmask & SDHCI_INT_DATA_TIMEOUT) { |
Adrian Hunter | 69b962a | 2016-11-02 15:49:09 +0200 | [diff] [blame] | 2972 | host->data_cmd = NULL; |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 2973 | data_cmd->error = -ETIMEDOUT; |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 2974 | sdhci_finish_mrq(host, data_cmd->mrq); |
Matthieu CASTET | c5abd5e | 2014-08-14 16:03:17 +0200 | [diff] [blame] | 2975 | return; |
| 2976 | } |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2977 | if (intmask & SDHCI_INT_DATA_END) { |
Adrian Hunter | 69b962a | 2016-11-02 15:49:09 +0200 | [diff] [blame] | 2978 | host->data_cmd = NULL; |
Chanho Min | e99783a | 2014-08-30 12:40:40 +0900 | [diff] [blame] | 2979 | /* |
| 2980 | * Some cards handle busy-end interrupt |
| 2981 | * before the command completed, so make |
| 2982 | * sure we do things in the proper order. |
| 2983 | */ |
Adrian Hunter | ea96802 | 2016-06-29 16:24:24 +0300 | [diff] [blame] | 2984 | if (host->cmd == data_cmd) |
| 2985 | return; |
| 2986 | |
Adrian Hunter | a6d3bdd | 2016-06-29 16:24:27 +0300 | [diff] [blame] | 2987 | sdhci_finish_mrq(host, data_cmd->mrq); |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2988 | return; |
| 2989 | } |
Sahitya Tummala | 87d4394 | 2013-04-12 11:49:11 +0530 | [diff] [blame] | 2990 | if (host->quirks2 & |
| 2991 | SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD) |
| 2992 | return; |
Pierre Ossman | e809517 | 2008-07-25 01:09:08 +0200 | [diff] [blame] | 2993 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 2994 | |
Adrian Hunter | ed1563d | 2016-06-29 16:24:29 +0300 | [diff] [blame] | 2995 | /* |
| 2996 | * SDHCI recovers from errors by resetting the cmd and data |
| 2997 | * circuits. Until that is done, there very well might be more |
| 2998 | * interrupts, so ignore them in that case. |
| 2999 | */ |
| 3000 | if (host->pending_reset) |
| 3001 | return; |
| 3002 | |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3003 | pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n", |
| 3004 | mmc_hostname(host->mmc), (unsigned)intmask); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3005 | sdhci_dumpregs(host); |
| 3006 | |
| 3007 | return; |
| 3008 | } |
| 3009 | |
| 3010 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 3011 | host->data->error = -ETIMEDOUT; |
Aries Lee | 22113ef | 2010-12-15 08:14:24 +0100 | [diff] [blame] | 3012 | else if (intmask & SDHCI_INT_DATA_END_BIT) |
| 3013 | host->data->error = -EILSEQ; |
| 3014 | else if ((intmask & SDHCI_INT_DATA_CRC) && |
Venkat Gopalakrishnan | a71fbae | 2014-06-09 14:00:31 -0700 | [diff] [blame] | 3015 | (command != MMC_BUS_TEST_R)) |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 3016 | host->data->error = -EILSEQ; |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 3017 | else if (intmask & SDHCI_INT_ADMA_ERROR) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3018 | pr_err("%s: ADMA error\n", mmc_hostname(host->mmc)); |
Adrian Hunter | 08621b1 | 2014-11-04 12:42:38 +0200 | [diff] [blame] | 3019 | sdhci_adma_show_error(host); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3020 | host->data->error = -EIO; |
Haijun Zhang | a4071fb | 2012-12-04 10:41:28 +0800 | [diff] [blame] | 3021 | if (host->ops->adma_workaround) |
| 3022 | host->ops->adma_workaround(host, intmask); |
Ben Dooks | 6882a8c | 2009-06-14 13:52:38 +0100 | [diff] [blame] | 3023 | } |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3024 | if (host->data->error) { |
| 3025 | if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) { |
| 3026 | command = SDHCI_GET_CMD(sdhci_readw(host, |
| 3027 | SDHCI_COMMAND)); |
| 3028 | if ((command != MMC_SEND_TUNING_BLOCK_HS200) && |
| 3029 | (command != MMC_SEND_TUNING_BLOCK)) |
| 3030 | pr_msg = true; |
| 3031 | } else { |
| 3032 | pr_msg = true; |
| 3033 | } |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 3034 | if (pr_msg && __ratelimit(&host->dbg_dump_rs)) { |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 3035 | pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n", |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3036 | mmc_hostname(host->mmc), intmask, |
Sahitya Tummala | 16dabee | 2013-04-08 12:53:44 +0530 | [diff] [blame] | 3037 | host->data->error, ktime_to_ms(ktime_sub( |
| 3038 | ktime_get(), host->data_start_time))); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3039 | sdhci_dumpregs(host); |
| 3040 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3041 | sdhci_finish_data(host); |
Asutosh Das | 494003b | 2013-03-20 22:53:40 +0530 | [diff] [blame] | 3042 | } else { |
Pierre Ossman | a406f5a | 2006-07-02 16:50:59 +0100 | [diff] [blame] | 3043 | if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3044 | sdhci_transfer_pio(host); |
| 3045 | |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3046 | /* |
| 3047 | * We currently don't do anything fancy with DMA |
| 3048 | * boundaries, but as we can't disable the feature |
| 3049 | * we need to at least restart the transfer. |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 3050 | * |
| 3051 | * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS) |
| 3052 | * should return a valid address to continue from, but as |
| 3053 | * some controllers are faulty, don't trust them. |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3054 | */ |
Mikko Vinni | f6a03cb | 2011-04-12 09:36:18 -0400 | [diff] [blame] | 3055 | if (intmask & SDHCI_INT_DMA_END) { |
| 3056 | u32 dmastart, dmanow; |
| 3057 | dmastart = sg_dma_address(host->data->sg); |
| 3058 | dmanow = dmastart + host->data->bytes_xfered; |
| 3059 | /* |
| 3060 | * Force update to the next DMA block boundary. |
| 3061 | */ |
| 3062 | dmanow = (dmanow & |
| 3063 | ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) + |
| 3064 | SDHCI_DEFAULT_BOUNDARY_SIZE; |
| 3065 | host->data->bytes_xfered = dmanow - dmastart; |
| 3066 | DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes," |
| 3067 | " next 0x%08x\n", |
| 3068 | mmc_hostname(host->mmc), dmastart, |
| 3069 | host->data->bytes_xfered, dmanow); |
| 3070 | sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS); |
| 3071 | } |
Pierre Ossman | 6ba736a | 2007-05-13 22:39:23 +0200 | [diff] [blame] | 3072 | |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 3073 | if (intmask & SDHCI_INT_DATA_END) { |
Adrian Hunter | 7c89a3d | 2016-06-29 16:24:23 +0300 | [diff] [blame] | 3074 | if (host->cmd == host->data_cmd) { |
Pierre Ossman | e538fbe | 2007-08-12 16:46:32 +0200 | [diff] [blame] | 3075 | /* |
| 3076 | * Data managed to finish before the |
| 3077 | * command completed. Make sure we do |
| 3078 | * things in the proper order. |
| 3079 | */ |
| 3080 | host->data_early = 1; |
| 3081 | } else { |
| 3082 | sdhci_finish_data(host); |
| 3083 | } |
| 3084 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3088 | #ifdef CONFIG_MMC_CQ_HCI |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3089 | static int sdhci_get_cmd_err(u32 intmask) |
| 3090 | { |
| 3091 | if (intmask & SDHCI_INT_TIMEOUT) |
| 3092 | return -ETIMEDOUT; |
| 3093 | else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT | |
| 3094 | SDHCI_INT_INDEX)) |
| 3095 | return -EILSEQ; |
| 3096 | return 0; |
| 3097 | } |
| 3098 | |
| 3099 | static int sdhci_get_data_err(u32 intmask) |
| 3100 | { |
| 3101 | if (intmask & SDHCI_INT_DATA_TIMEOUT) |
| 3102 | return -ETIMEDOUT; |
| 3103 | else if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC)) |
| 3104 | return -EILSEQ; |
| 3105 | else if (intmask & SDHCI_INT_ADMA_ERROR) |
| 3106 | return -EIO; |
| 3107 | return 0; |
| 3108 | } |
| 3109 | |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3110 | static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask) |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3111 | { |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3112 | int err = 0; |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3113 | u32 mask = 0; |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3114 | |
| 3115 | if (intmask & SDHCI_INT_CMD_MASK) |
| 3116 | err = sdhci_get_cmd_err(intmask); |
| 3117 | else if (intmask & SDHCI_INT_DATA_MASK) |
| 3118 | err = sdhci_get_data_err(intmask); |
| 3119 | |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3120 | if (err) { |
| 3121 | /* Clear the error interrupts */ |
| 3122 | mask = intmask & SDHCI_INT_ERROR_MASK; |
| 3123 | sdhci_writel(host, mask, SDHCI_INT_STATUS); |
| 3124 | } |
| 3125 | |
| 3126 | return cmdq_irq(host->mmc, err); |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | #else |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3130 | static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask) |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3131 | { |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3132 | pr_err("%s: Received cmdq-irq when disabled !!!!\n", |
| 3133 | mmc_hostname(host->mmc)); |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3134 | return IRQ_NONE; |
| 3135 | } |
| 3136 | #endif |
| 3137 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 3138 | static irqreturn_t sdhci_irq(int irq, void *dev_id) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3139 | { |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3140 | irqreturn_t result = IRQ_NONE; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3141 | struct sdhci_host *host = dev_id; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3142 | u32 intmask, mask, unexpected = 0; |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3143 | int max_loops = 16; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3144 | |
| 3145 | spin_lock(&host->lock); |
| 3146 | |
Russell King | be13855 | 2014-04-25 12:55:56 +0100 | [diff] [blame] | 3147 | if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) { |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3148 | spin_unlock(&host->lock); |
Adrian Hunter | 655bca7 | 2014-03-11 10:09:36 +0200 | [diff] [blame] | 3149 | return IRQ_NONE; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3150 | } |
| 3151 | |
Anton Vorontsov | 4e4141a | 2009-03-17 00:13:46 +0300 | [diff] [blame] | 3152 | intmask = sdhci_readl(host, SDHCI_INT_STATUS); |
Mark Lord | 62df67a5 | 2007-03-06 13:30:13 +0100 | [diff] [blame] | 3153 | if (!intmask || intmask == 0xffffffff) { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3154 | result = IRQ_NONE; |
| 3155 | goto out; |
| 3156 | } |
| 3157 | |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3158 | do { |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3159 | if (host->mmc->card && mmc_card_cmdq(host->mmc->card) && |
| 3160 | !mmc_host_halt(host->mmc)) { |
| 3161 | pr_debug("*** %s: cmdq intr: 0x%08x\n", |
| 3162 | mmc_hostname(host->mmc), |
| 3163 | intmask); |
Venkat Gopalakrishnan | 7c6a56c1 | 2015-09-15 12:33:13 -0700 | [diff] [blame] | 3164 | result = sdhci_cmdq_irq(host, intmask); |
Asutosh Das | f67a83f | 2015-02-27 00:02:02 +0530 | [diff] [blame] | 3165 | if (result == IRQ_HANDLED) |
| 3166 | goto out; |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3167 | } |
| 3168 | |
Sahitya Tummala | 9e7fadb | 2013-08-07 18:40:29 +0530 | [diff] [blame] | 3169 | if (intmask & SDHCI_INT_AUTO_CMD_ERR) |
| 3170 | host->auto_cmd_err_sts = sdhci_readw(host, |
| 3171 | SDHCI_AUTO_CMD_ERR); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3172 | /* Clear selected interrupts. */ |
| 3173 | mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK | |
| 3174 | SDHCI_INT_BUS_POWER); |
| 3175 | sdhci_writel(host, mask, SDHCI_INT_STATUS); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3176 | |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3177 | DBG("*** %s got interrupt: 0x%08x\n", |
| 3178 | mmc_hostname(host->mmc), intmask); |
| 3179 | |
| 3180 | if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
| 3181 | u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) & |
| 3182 | SDHCI_CARD_PRESENT; |
| 3183 | |
| 3184 | /* |
| 3185 | * There is a observation on i.mx esdhc. INSERT |
| 3186 | * bit will be immediately set again when it gets |
| 3187 | * cleared, if a card is inserted. We have to mask |
| 3188 | * the irq to prevent interrupt storm which will |
| 3189 | * freeze the system. And the REMOVE gets the |
| 3190 | * same situation. |
| 3191 | * |
| 3192 | * More testing are needed here to ensure it works |
| 3193 | * for other platforms though. |
| 3194 | */ |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3195 | host->ier &= ~(SDHCI_INT_CARD_INSERT | |
| 3196 | SDHCI_INT_CARD_REMOVE); |
| 3197 | host->ier |= present ? SDHCI_INT_CARD_REMOVE : |
| 3198 | SDHCI_INT_CARD_INSERT; |
| 3199 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 3200 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3201 | |
| 3202 | sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT | |
| 3203 | SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS); |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3204 | |
| 3205 | host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT | |
| 3206 | SDHCI_INT_CARD_REMOVE); |
| 3207 | result = IRQ_WAKE_THREAD; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3210 | if (intmask & SDHCI_INT_CMD_MASK) { |
| 3211 | if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) && |
| 3212 | (host->clock <= 400000)) |
| 3213 | udelay(40); |
Adrian Hunter | fc605f1 | 2016-10-05 12:11:21 +0300 | [diff] [blame] | 3214 | sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3215 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3216 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3217 | if (intmask & SDHCI_INT_DATA_MASK) { |
| 3218 | if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) && |
| 3219 | (host->clock <= 400000)) |
| 3220 | udelay(40); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3221 | sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3222 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3223 | |
| 3224 | if (intmask & SDHCI_INT_BUS_POWER) |
| 3225 | pr_err("%s: Card is consuming too much power!\n", |
| 3226 | mmc_hostname(host->mmc)); |
| 3227 | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3228 | if (intmask & SDHCI_INT_RETUNE) |
| 3229 | mmc_retune_needed(host->mmc); |
| 3230 | |
Gabriel Krisman Bertazi | 04eb7db | 2017-01-16 12:23:42 -0200 | [diff] [blame] | 3231 | if ((intmask & SDHCI_INT_CARD_INT) && |
| 3232 | (host->ier & SDHCI_INT_CARD_INT)) { |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3233 | sdhci_enable_sdio_irq_nolock(host, false); |
| 3234 | host->thread_isr |= SDHCI_INT_CARD_INT; |
| 3235 | result = IRQ_WAKE_THREAD; |
| 3236 | } |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3237 | |
| 3238 | intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | |
| 3239 | SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK | |
| 3240 | SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER | |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3241 | SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3242 | |
| 3243 | if (intmask) { |
| 3244 | unexpected |= intmask; |
| 3245 | sdhci_writel(host, intmask, SDHCI_INT_STATUS); |
| 3246 | } |
| 3247 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3248 | if (result == IRQ_NONE) |
| 3249 | result = IRQ_HANDLED; |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3250 | |
| 3251 | intmask = sdhci_readl(host, SDHCI_INT_STATUS); |
Russell King | 4100500 | 2014-04-25 12:55:36 +0100 | [diff] [blame] | 3252 | } while (intmask && --max_loops); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3253 | out: |
| 3254 | spin_unlock(&host->lock); |
| 3255 | |
Alexander Stein | 6379b23 | 2012-03-14 09:52:10 +0100 | [diff] [blame] | 3256 | if (unexpected) { |
| 3257 | pr_err("%s: Unexpected interrupt 0x%08x.\n", |
| 3258 | mmc_hostname(host->mmc), unexpected); |
| 3259 | sdhci_dumpregs(host); |
| 3260 | } |
Pierre Ossman | f75979b | 2007-09-04 07:59:18 +0200 | [diff] [blame] | 3261 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3262 | return result; |
| 3263 | } |
| 3264 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3265 | static irqreturn_t sdhci_thread_irq(int irq, void *dev_id) |
| 3266 | { |
| 3267 | struct sdhci_host *host = dev_id; |
| 3268 | unsigned long flags; |
| 3269 | u32 isr; |
| 3270 | |
| 3271 | spin_lock_irqsave(&host->lock, flags); |
| 3272 | isr = host->thread_isr; |
| 3273 | host->thread_isr = 0; |
| 3274 | spin_unlock_irqrestore(&host->lock, flags); |
| 3275 | |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3276 | if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3277 | struct mmc_host *mmc = host->mmc; |
| 3278 | |
| 3279 | mmc->ops->card_event(mmc); |
| 3280 | mmc_detect_change(mmc, msecs_to_jiffies(200)); |
Russell King | 3560db8 | 2014-04-25 12:55:51 +0100 | [diff] [blame] | 3281 | } |
| 3282 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3283 | if (isr & SDHCI_INT_CARD_INT) { |
| 3284 | sdio_run_irqs(host->mmc); |
| 3285 | |
| 3286 | spin_lock_irqsave(&host->lock, flags); |
| 3287 | if (host->flags & SDHCI_SDIO_IRQ_ENABLED) |
| 3288 | sdhci_enable_sdio_irq_nolock(host, true); |
| 3289 | spin_unlock_irqrestore(&host->lock, flags); |
| 3290 | } |
| 3291 | |
| 3292 | return isr ? IRQ_HANDLED : IRQ_NONE; |
| 3293 | } |
| 3294 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3295 | /*****************************************************************************\ |
| 3296 | * * |
| 3297 | * Suspend/resume * |
| 3298 | * * |
| 3299 | \*****************************************************************************/ |
| 3300 | |
| 3301 | #ifdef CONFIG_PM |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3302 | /* |
| 3303 | * To enable wakeup events, the corresponding events have to be enabled in |
| 3304 | * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal |
| 3305 | * Table' in the SD Host Controller Standard Specification. |
| 3306 | * It is useless to restore SDHCI_INT_ENABLE state in |
| 3307 | * sdhci_disable_irq_wakeups() since it will be set by |
| 3308 | * sdhci_enable_card_detection() or sdhci_init(). |
| 3309 | */ |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3310 | void sdhci_enable_irq_wakeups(struct sdhci_host *host) |
| 3311 | { |
| 3312 | u8 val; |
| 3313 | u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
| 3314 | | SDHCI_WAKE_ON_INT; |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3315 | u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | |
| 3316 | SDHCI_INT_CARD_INT; |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3317 | |
| 3318 | val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL); |
| 3319 | val |= mask ; |
| 3320 | /* Avoid fake wake up */ |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3321 | if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) { |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3322 | val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE); |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3323 | irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); |
| 3324 | } |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3325 | sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL); |
Ludovic Desroches | 84d6260 | 2016-05-13 15:16:02 +0200 | [diff] [blame] | 3326 | sdhci_writel(host, irq_val, SDHCI_INT_ENABLE); |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3327 | } |
| 3328 | EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups); |
| 3329 | |
Fabio Estevam | 0b10f47 | 2014-08-30 14:53:13 -0300 | [diff] [blame] | 3330 | static void sdhci_disable_irq_wakeups(struct sdhci_host *host) |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3331 | { |
| 3332 | u8 val; |
| 3333 | u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
| 3334 | | SDHCI_WAKE_ON_INT; |
| 3335 | |
| 3336 | val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL); |
| 3337 | val &= ~mask; |
| 3338 | sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL); |
| 3339 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3340 | |
Manuel Lauss | 29495aa | 2011-11-03 11:09:45 +0100 | [diff] [blame] | 3341 | int sdhci_suspend_host(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3342 | { |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 3343 | sdhci_disable_card_detection(host); |
| 3344 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 3345 | mmc_retune_timer_stop(host->mmc); |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3346 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 3347 | mmc_retune_needed(host->mmc); |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 3348 | |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3349 | if (!device_may_wakeup(mmc_dev(host->mmc))) { |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3350 | host->ier = 0; |
| 3351 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 3352 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Kevin Liu | ad080d7 | 2013-01-05 17:21:33 +0800 | [diff] [blame] | 3353 | free_irq(host->irq, host); |
| 3354 | } else { |
| 3355 | sdhci_enable_irq_wakeups(host); |
| 3356 | enable_irq_wake(host->irq); |
| 3357 | } |
Ulf Hansson | 4ee14ec | 2013-09-25 14:15:24 +0200 | [diff] [blame] | 3358 | return 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3359 | } |
| 3360 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3361 | EXPORT_SYMBOL_GPL(sdhci_suspend_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3362 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3363 | int sdhci_resume_host(struct sdhci_host *host) |
| 3364 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3365 | struct mmc_host *mmc = host->mmc; |
Ulf Hansson | 4ee14ec | 2013-09-25 14:15:24 +0200 | [diff] [blame] | 3366 | int ret = 0; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3367 | |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3368 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3369 | if (host->ops->enable_dma) |
| 3370 | host->ops->enable_dma(host); |
| 3371 | } |
| 3372 | |
Adrian Hunter | 6308d29 | 2012-02-07 14:48:54 +0200 | [diff] [blame] | 3373 | if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) && |
| 3374 | (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) { |
| 3375 | /* Card keeps power but host controller does not */ |
| 3376 | sdhci_init(host, 0); |
| 3377 | host->pwr = 0; |
| 3378 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3379 | mmc->ops->set_ios(mmc, &mmc->ios); |
Adrian Hunter | 6308d29 | 2012-02-07 14:48:54 +0200 | [diff] [blame] | 3380 | } else { |
| 3381 | sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER)); |
| 3382 | mmiowb(); |
| 3383 | } |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3384 | |
Haibo Chen | 14a7b4164 | 2015-09-15 18:32:58 +0800 | [diff] [blame] | 3385 | if (!device_may_wakeup(mmc_dev(host->mmc))) { |
| 3386 | ret = request_threaded_irq(host->irq, sdhci_irq, |
| 3387 | sdhci_thread_irq, IRQF_SHARED, |
| 3388 | mmc_hostname(host->mmc), host); |
| 3389 | if (ret) |
| 3390 | return ret; |
| 3391 | } else { |
| 3392 | sdhci_disable_irq_wakeups(host); |
| 3393 | disable_irq_wake(host->irq); |
| 3394 | } |
| 3395 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 3396 | sdhci_enable_card_detection(host); |
| 3397 | |
Nicolas Pitre | 2f4cbb3 | 2010-03-05 13:43:32 -0800 | [diff] [blame] | 3398 | return ret; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | EXPORT_SYMBOL_GPL(sdhci_resume_host); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3402 | |
Ritesh Harjani | 9ce9ecf | 2015-05-27 15:32:40 +0530 | [diff] [blame] | 3403 | static int sdhci_runtime_pm_get(struct sdhci_host *host) |
| 3404 | { |
| 3405 | return pm_runtime_get_sync(host->mmc->parent); |
| 3406 | } |
| 3407 | |
Asutosh Das | 06d9f32 | 2014-02-21 11:28:36 +0530 | [diff] [blame] | 3408 | static int sdhci_runtime_pm_put(struct sdhci_host *host) |
| 3409 | { |
| 3410 | pm_runtime_mark_last_busy(host->mmc->parent); |
| 3411 | return pm_runtime_put_autosuspend(host->mmc->parent); |
| 3412 | } |
| 3413 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3414 | int sdhci_runtime_suspend_host(struct sdhci_host *host) |
| 3415 | { |
| 3416 | unsigned long flags; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3417 | |
Adrian Hunter | 66c39dfc | 2015-05-07 13:10:21 +0300 | [diff] [blame] | 3418 | mmc_retune_timer_stop(host->mmc); |
Dong Aisheng | f37b20e | 2016-07-12 15:46:17 +0800 | [diff] [blame] | 3419 | if (host->tuning_mode != SDHCI_TUNING_MODE_3) |
| 3420 | mmc_retune_needed(host->mmc); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3421 | |
| 3422 | spin_lock_irqsave(&host->lock, flags); |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 3423 | host->ier &= SDHCI_INT_CARD_INT; |
| 3424 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 3425 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3426 | spin_unlock_irqrestore(&host->lock, flags); |
| 3427 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3428 | synchronize_hardirq(host->irq); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3429 | |
| 3430 | spin_lock_irqsave(&host->lock, flags); |
| 3431 | host->runtime_suspended = true; |
| 3432 | spin_unlock_irqrestore(&host->lock, flags); |
| 3433 | |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3434 | return 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3435 | } |
| 3436 | EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host); |
| 3437 | |
| 3438 | int sdhci_runtime_resume_host(struct sdhci_host *host) |
| 3439 | { |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3440 | struct mmc_host *mmc = host->mmc; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3441 | unsigned long flags; |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3442 | int host_flags = host->flags; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3443 | |
| 3444 | if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
| 3445 | if (host->ops->enable_dma) |
| 3446 | host->ops->enable_dma(host); |
| 3447 | } |
| 3448 | |
| 3449 | sdhci_init(host, 0); |
| 3450 | |
| 3451 | /* Force clock and power re-program */ |
| 3452 | host->pwr = 0; |
| 3453 | host->clock = 0; |
Adrian Hunter | d3940f2 | 2016-06-29 16:24:14 +0300 | [diff] [blame] | 3454 | mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios); |
| 3455 | mmc->ops->set_ios(mmc, &mmc->ios); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3456 | |
Kevin Liu | 5298338 | 2013-01-31 11:31:37 +0800 | [diff] [blame] | 3457 | if ((host_flags & SDHCI_PV_ENABLED) && |
| 3458 | !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) { |
| 3459 | spin_lock_irqsave(&host->lock, flags); |
| 3460 | sdhci_enable_preset_value(host, true); |
| 3461 | spin_unlock_irqrestore(&host->lock, flags); |
| 3462 | } |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3463 | |
Adrian Hunter | 086b0dd | 2016-11-02 15:49:11 +0200 | [diff] [blame] | 3464 | if ((mmc->caps2 & MMC_CAP2_HS400_ES) && |
| 3465 | mmc->ops->hs400_enhanced_strobe) |
| 3466 | mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios); |
| 3467 | |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3468 | spin_lock_irqsave(&host->lock, flags); |
| 3469 | |
| 3470 | host->runtime_suspended = false; |
| 3471 | |
| 3472 | /* Enable SDIO IRQ */ |
Russell King | ef10433 | 2014-04-25 12:55:41 +0100 | [diff] [blame] | 3473 | if (host->flags & SDHCI_SDIO_IRQ_ENABLED) |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3474 | sdhci_enable_sdio_irq_nolock(host, true); |
| 3475 | |
| 3476 | /* Enable Card Detection */ |
| 3477 | sdhci_enable_card_detection(host); |
| 3478 | |
| 3479 | spin_unlock_irqrestore(&host->lock, flags); |
| 3480 | |
Markus Pargmann | 8a125ba | 2014-06-04 15:24:29 +0200 | [diff] [blame] | 3481 | return 0; |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3482 | } |
| 3483 | EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host); |
| 3484 | |
Rafael J. Wysocki | 162d6f9 | 2014-12-05 03:05:33 +0100 | [diff] [blame] | 3485 | #endif /* CONFIG_PM */ |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 3486 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3487 | /*****************************************************************************\ |
| 3488 | * * |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3489 | * Device allocation/registration * |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3490 | * * |
| 3491 | \*****************************************************************************/ |
| 3492 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3493 | struct sdhci_host *sdhci_alloc_host(struct device *dev, |
| 3494 | size_t priv_size) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3495 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3496 | struct mmc_host *mmc; |
| 3497 | struct sdhci_host *host; |
| 3498 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3499 | WARN_ON(dev == NULL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3500 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3501 | mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3502 | if (!mmc) |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3503 | return ERR_PTR(-ENOMEM); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3504 | |
| 3505 | host = mmc_priv(mmc); |
| 3506 | host->mmc = mmc; |
Adrian Hunter | bf60e59 | 2016-02-09 16:12:35 +0200 | [diff] [blame] | 3507 | host->mmc_host_ops = sdhci_ops; |
| 3508 | mmc->ops = &host->mmc_host_ops; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3509 | |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 3510 | host->flags = SDHCI_SIGNALING_330; |
| 3511 | |
Sahitya Tummala | ef4de6c | 2013-05-24 08:47:26 +0530 | [diff] [blame] | 3512 | spin_lock_init(&host->lock); |
Sahitya Tummala | 4c196de | 2014-10-31 14:00:12 +0530 | [diff] [blame] | 3513 | ratelimit_state_init(&host->dbg_dump_rs, SDHCI_DBG_DUMP_RS_INTERVAL, |
| 3514 | SDHCI_DBG_DUMP_RS_BURST); |
Sahitya Tummala | ef4de6c | 2013-05-24 08:47:26 +0530 | [diff] [blame] | 3515 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3516 | return host; |
| 3517 | } |
Pierre Ossman | 8a4da14 | 2006-10-04 02:15:40 -0700 | [diff] [blame] | 3518 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3519 | EXPORT_SYMBOL_GPL(sdhci_alloc_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3520 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3521 | #ifdef CONFIG_MMC_CQ_HCI |
| 3522 | static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear) |
| 3523 | { |
| 3524 | struct sdhci_host *host = mmc_priv(mmc); |
| 3525 | u32 ier = 0; |
| 3526 | |
| 3527 | ier &= ~SDHCI_INT_ALL_MASK; |
| 3528 | |
| 3529 | if (clear) { |
| 3530 | ier = SDHCI_INT_CMDQ_EN | SDHCI_INT_ERROR_MASK; |
| 3531 | sdhci_writel(host, ier, SDHCI_INT_ENABLE); |
| 3532 | sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE); |
| 3533 | } else { |
| 3534 | ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | |
| 3535 | SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | |
| 3536 | SDHCI_INT_INDEX | SDHCI_INT_END_BIT | |
| 3537 | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | |
| 3538 | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | |
| 3539 | SDHCI_INT_AUTO_CMD_ERR; |
| 3540 | sdhci_writel(host, ier, SDHCI_INT_ENABLE); |
| 3541 | sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE); |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val) |
| 3546 | { |
| 3547 | struct sdhci_host *host = mmc_priv(mmc); |
| 3548 | |
| 3549 | sdhci_writeb(host, val, SDHCI_TIMEOUT_CONTROL); |
| 3550 | } |
| 3551 | |
| 3552 | static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc) |
| 3553 | { |
| 3554 | struct sdhci_host *host = mmc_priv(mmc); |
| 3555 | |
| 3556 | sdhci_dumpregs(host); |
| 3557 | } |
| 3558 | |
| 3559 | static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc, |
| 3560 | bool dma64) |
| 3561 | { |
| 3562 | return cmdq_init(host->cq_host, mmc, dma64); |
| 3563 | } |
| 3564 | |
| 3565 | static void sdhci_cmdq_set_block_size(struct mmc_host *mmc) |
| 3566 | { |
| 3567 | struct sdhci_host *host = mmc_priv(mmc); |
| 3568 | |
| 3569 | sdhci_set_blk_size_reg(host, 512, 0); |
| 3570 | } |
| 3571 | |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3572 | static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set) |
| 3573 | { |
| 3574 | struct sdhci_host *host = mmc_priv(mmc); |
| 3575 | |
| 3576 | if (host->ops->enhanced_strobe_mask) |
| 3577 | host->ops->enhanced_strobe_mask(host, set); |
| 3578 | } |
| 3579 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3580 | static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set) |
| 3581 | { |
| 3582 | struct sdhci_host *host = mmc_priv(mmc); |
| 3583 | |
| 3584 | if (host->ops->clear_set_dumpregs) |
| 3585 | host->ops->clear_set_dumpregs(host, set); |
| 3586 | } |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3587 | |
| 3588 | static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc) |
| 3589 | { |
| 3590 | struct sdhci_host *host = mmc_priv(mmc); |
| 3591 | |
| 3592 | sdhci_writel(host, sdhci_readl(host, SDHCI_INT_ENABLE) | |
| 3593 | SDHCI_INT_RESPONSE, SDHCI_INT_ENABLE); |
| 3594 | sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS); |
| 3595 | } |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3596 | #else |
| 3597 | static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear) |
| 3598 | { |
| 3599 | |
| 3600 | } |
| 3601 | |
| 3602 | static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val) |
| 3603 | { |
| 3604 | |
| 3605 | } |
| 3606 | |
| 3607 | static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc) |
| 3608 | { |
| 3609 | |
| 3610 | } |
| 3611 | |
| 3612 | static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc, |
| 3613 | bool dma64) |
| 3614 | { |
| 3615 | return -ENOSYS; |
| 3616 | } |
| 3617 | |
| 3618 | static void sdhci_cmdq_set_block_size(struct mmc_host *mmc) |
| 3619 | { |
| 3620 | |
| 3621 | } |
| 3622 | |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3623 | static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set) |
| 3624 | { |
| 3625 | |
| 3626 | } |
| 3627 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3628 | static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set) |
| 3629 | { |
| 3630 | |
| 3631 | } |
| 3632 | |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3633 | static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc) |
| 3634 | { |
| 3635 | } |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3636 | #endif |
| 3637 | |
| 3638 | static const struct cmdq_host_ops sdhci_cmdq_ops = { |
| 3639 | .clear_set_irqs = sdhci_cmdq_clear_set_irqs, |
| 3640 | .set_data_timeout = sdhci_cmdq_set_data_timeout, |
| 3641 | .dump_vendor_regs = sdhci_cmdq_dump_vendor_regs, |
| 3642 | .set_block_size = sdhci_cmdq_set_block_size, |
| 3643 | .clear_set_dumpregs = sdhci_cmdq_clear_set_dumpregs, |
Ritesh Harjani | 1f6d762 | 2015-07-15 13:31:06 +0530 | [diff] [blame] | 3644 | .enhanced_strobe_mask = sdhci_enhanced_strobe_mask, |
Konstantin Dorfman | 26a6d9b | 2015-05-31 10:08:29 +0300 | [diff] [blame] | 3645 | .post_cqe_halt = sdhci_cmdq_post_cqe_halt, |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 3646 | }; |
| 3647 | |
Alexandre Courbot | 7b91369 | 2016-03-07 11:07:55 +0900 | [diff] [blame] | 3648 | static int sdhci_set_dma_mask(struct sdhci_host *host) |
| 3649 | { |
| 3650 | struct mmc_host *mmc = host->mmc; |
| 3651 | struct device *dev = mmc_dev(mmc); |
| 3652 | int ret = -EINVAL; |
| 3653 | |
| 3654 | if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA) |
| 3655 | host->flags &= ~SDHCI_USE_64_BIT_DMA; |
| 3656 | |
| 3657 | /* Try 64-bit mask if hardware is capable of it */ |
| 3658 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
| 3659 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); |
| 3660 | if (ret) { |
| 3661 | pr_warn("%s: Failed to set 64-bit DMA mask.\n", |
| 3662 | mmc_hostname(mmc)); |
| 3663 | host->flags &= ~SDHCI_USE_64_BIT_DMA; |
| 3664 | } |
| 3665 | } |
| 3666 | |
| 3667 | /* 32-bit mask as default & fallback */ |
| 3668 | if (ret) { |
| 3669 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 3670 | if (ret) |
| 3671 | pr_warn("%s: Failed to set 32-bit DMA mask.\n", |
| 3672 | mmc_hostname(mmc)); |
| 3673 | } |
| 3674 | |
| 3675 | return ret; |
| 3676 | } |
| 3677 | |
Adrian Hunter | 6132a3b | 2016-06-29 16:24:18 +0300 | [diff] [blame] | 3678 | void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1) |
| 3679 | { |
| 3680 | u16 v; |
| 3681 | |
| 3682 | if (host->read_caps) |
| 3683 | return; |
| 3684 | |
| 3685 | host->read_caps = true; |
| 3686 | |
| 3687 | if (debug_quirks) |
| 3688 | host->quirks = debug_quirks; |
| 3689 | |
| 3690 | if (debug_quirks2) |
| 3691 | host->quirks2 = debug_quirks2; |
| 3692 | |
| 3693 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
| 3694 | |
| 3695 | v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION); |
| 3696 | host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT; |
| 3697 | |
| 3698 | if (host->quirks & SDHCI_QUIRK_MISSING_CAPS) |
| 3699 | return; |
| 3700 | |
| 3701 | host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES); |
| 3702 | |
| 3703 | if (host->version < SDHCI_SPEC_300) |
| 3704 | return; |
| 3705 | |
| 3706 | host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1); |
| 3707 | } |
| 3708 | EXPORT_SYMBOL_GPL(__sdhci_read_caps); |
| 3709 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 3710 | int sdhci_setup_host(struct sdhci_host *host) |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3711 | { |
| 3712 | struct mmc_host *mmc; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3713 | u32 max_current_caps; |
| 3714 | unsigned int ocr_avail; |
Adrian Hunter | f5fa92e | 2014-09-24 10:27:32 +0300 | [diff] [blame] | 3715 | unsigned int override_timeout_clk; |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 3716 | u32 max_clk; |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3717 | int ret; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3718 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3719 | WARN_ON(host == NULL); |
| 3720 | if (host == NULL) |
| 3721 | return -EINVAL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3722 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3723 | mmc = host->mmc; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3724 | |
Jon Hunter | efba142 | 2016-07-12 14:53:36 +0100 | [diff] [blame] | 3725 | /* |
| 3726 | * If there are external regulators, get them. Note this must be done |
| 3727 | * early before resetting the host and reading the capabilities so that |
| 3728 | * the host can take the appropriate action if regulators are not |
| 3729 | * available. |
| 3730 | */ |
| 3731 | ret = mmc_regulator_get_supply(mmc); |
| 3732 | if (ret == -EPROBE_DEFER) |
| 3733 | return ret; |
| 3734 | |
Adrian Hunter | 6132a3b | 2016-06-29 16:24:18 +0300 | [diff] [blame] | 3735 | sdhci_read_caps(host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3736 | |
Adrian Hunter | f5fa92e | 2014-09-24 10:27:32 +0300 | [diff] [blame] | 3737 | override_timeout_clk = host->timeout_clk; |
| 3738 | |
Zhangfei Gao | 85105c5 | 2010-08-06 07:10:01 +0800 | [diff] [blame] | 3739 | if (host->version > SDHCI_SPEC_300) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3740 | pr_err("%s: Unknown controller version (%d). You may experience problems.\n", |
| 3741 | mmc_hostname(mmc), host->version); |
Pierre Ossman | 4a96550 | 2006-06-30 02:22:29 -0700 | [diff] [blame] | 3742 | } |
| 3743 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3744 | if (host->quirks & SDHCI_QUIRK_FORCE_DMA) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3745 | host->flags |= SDHCI_USE_SDMA; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3746 | else if (!(host->caps & SDHCI_CAN_DO_SDMA)) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3747 | DBG("Controller doesn't have SDMA capability\n"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 3748 | else |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3749 | host->flags |= SDHCI_USE_SDMA; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3750 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 3751 | if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) && |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3752 | (host->flags & SDHCI_USE_SDMA)) { |
Rolf Eike Beer | cee687c | 2007-11-02 15:22:30 +0100 | [diff] [blame] | 3753 | DBG("Disabling DMA as it is marked broken\n"); |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3754 | host->flags &= ~SDHCI_USE_SDMA; |
Feng Tang | 7c168e3 | 2007-09-30 12:44:18 +0200 | [diff] [blame] | 3755 | } |
| 3756 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3757 | if ((host->version >= SDHCI_SPEC_200) && |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3758 | (host->caps & SDHCI_CAN_DO_ADMA2)) |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3759 | host->flags |= SDHCI_USE_ADMA; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3760 | |
| 3761 | if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) && |
| 3762 | (host->flags & SDHCI_USE_ADMA)) { |
| 3763 | DBG("Disabling ADMA as it is marked broken\n"); |
| 3764 | host->flags &= ~SDHCI_USE_ADMA; |
| 3765 | } |
| 3766 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3767 | /* |
| 3768 | * It is assumed that a 64-bit capable device has set a 64-bit DMA mask |
| 3769 | * and *must* do 64-bit DMA. A driver has the opportunity to change |
| 3770 | * that during the first call to ->enable_dma(). Similarly |
| 3771 | * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to |
| 3772 | * implement. |
| 3773 | */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3774 | if (host->caps & SDHCI_CAN_64BIT) |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3775 | host->flags |= SDHCI_USE_64_BIT_DMA; |
| 3776 | |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3777 | if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) { |
Alexandre Courbot | 7b91369 | 2016-03-07 11:07:55 +0900 | [diff] [blame] | 3778 | ret = sdhci_set_dma_mask(host); |
| 3779 | |
| 3780 | if (!ret && host->ops->enable_dma) |
| 3781 | ret = host->ops->enable_dma(host); |
| 3782 | |
| 3783 | if (ret) { |
| 3784 | pr_warn("%s: No suitable DMA available - falling back to PIO\n", |
| 3785 | mmc_hostname(mmc)); |
| 3786 | host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA); |
| 3787 | |
| 3788 | ret = 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3789 | } |
| 3790 | } |
| 3791 | |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3792 | /* SDMA does not support 64-bit DMA */ |
| 3793 | if (host->flags & SDHCI_USE_64_BIT_DMA) |
| 3794 | host->flags &= ~SDHCI_USE_SDMA; |
| 3795 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3796 | if (host->flags & SDHCI_USE_ADMA) { |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3797 | dma_addr_t dma; |
| 3798 | void *buf; |
| 3799 | |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3800 | /* |
Adrian Hunter | 76fe379 | 2014-11-04 12:42:42 +0200 | [diff] [blame] | 3801 | * The DMA descriptor table size is calculated as the maximum |
| 3802 | * number of segments times 2, to allow for an alignment |
| 3803 | * descriptor for each segment, plus 1 for a nop end descriptor, |
| 3804 | * all multipled by the descriptor size. |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3805 | */ |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3806 | if (host->flags & SDHCI_USE_64_BIT_DMA) { |
| 3807 | host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) * |
| 3808 | SDHCI_ADMA2_64_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3809 | host->desc_sz = SDHCI_ADMA2_64_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3810 | } else { |
| 3811 | host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) * |
| 3812 | SDHCI_ADMA2_32_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3813 | host->desc_sz = SDHCI_ADMA2_32_DESC_SZ; |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 3814 | } |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3815 | |
Adrian Hunter | 04a5ae6 | 2015-11-26 14:00:49 +0200 | [diff] [blame] | 3816 | host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3817 | buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 3818 | host->adma_table_sz, &dma, GFP_KERNEL); |
| 3819 | if (!buf) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 3820 | 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] | 3821 | mmc_hostname(mmc)); |
| 3822 | host->flags &= ~SDHCI_USE_ADMA; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3823 | } else if ((dma + host->align_buffer_sz) & |
| 3824 | (SDHCI_ADMA2_DESC_ALIGN - 1)) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 3825 | pr_warn("%s: unable to allocate aligned ADMA descriptor\n", |
| 3826 | mmc_hostname(mmc)); |
Russell King | d1e49f7 | 2014-04-25 12:58:34 +0100 | [diff] [blame] | 3827 | host->flags &= ~SDHCI_USE_ADMA; |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3828 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 3829 | host->adma_table_sz, buf, dma); |
| 3830 | } else { |
| 3831 | host->align_buffer = buf; |
| 3832 | host->align_addr = dma; |
Russell King | edd63fc | 2016-01-26 13:39:50 +0000 | [diff] [blame] | 3833 | |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 3834 | host->adma_table = buf + host->align_buffer_sz; |
| 3835 | host->adma_addr = dma + host->align_buffer_sz; |
| 3836 | } |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 3837 | } |
| 3838 | |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3839 | /* |
| 3840 | * If we use DMA, then it's up to the caller to set the DMA |
| 3841 | * mask, but PIO does not need the hw shim so we set a new |
| 3842 | * mask here in that case. |
| 3843 | */ |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 3844 | if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) { |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3845 | host->dma_mask = DMA_BIT_MASK(64); |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 3846 | mmc_dev(mmc)->dma_mask = &host->dma_mask; |
Pierre Ossman | 7659150 | 2008-07-21 00:32:11 +0200 | [diff] [blame] | 3847 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3848 | |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3849 | if (host->version >= SDHCI_SPEC_300) |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3850 | host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK) |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3851 | >> SDHCI_CLOCK_BASE_SHIFT; |
| 3852 | else |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3853 | host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK) |
Zhangfei Gao | c4687d5 | 2010-08-20 14:02:36 -0400 | [diff] [blame] | 3854 | >> SDHCI_CLOCK_BASE_SHIFT; |
| 3855 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3856 | host->max_clk *= 1000000; |
Anton Vorontsov | f27f47e | 2010-05-26 14:41:53 -0700 | [diff] [blame] | 3857 | if (host->max_clk == 0 || host->quirks & |
| 3858 | SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) { |
Ben Dooks | 4240ff0 | 2009-03-17 00:13:57 +0300 | [diff] [blame] | 3859 | if (!host->ops->get_max_clock) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 3860 | pr_err("%s: Hardware doesn't specify base clock frequency.\n", |
| 3861 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 3862 | ret = -ENODEV; |
| 3863 | goto undma; |
Ben Dooks | 4240ff0 | 2009-03-17 00:13:57 +0300 | [diff] [blame] | 3864 | } |
| 3865 | host->max_clk = host->ops->get_max_clock(host); |
| 3866 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3867 | |
| 3868 | /* |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 3869 | * In case of Host Controller v3.00, find out whether clock |
| 3870 | * multiplier is supported. |
| 3871 | */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3872 | host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >> |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 3873 | SDHCI_CLOCK_MUL_SHIFT; |
| 3874 | |
| 3875 | /* |
| 3876 | * In case the value in Clock Multiplier is 0, then programmable |
| 3877 | * clock mode is not supported, otherwise the actual clock |
| 3878 | * multiplier is one more than the value of Clock Multiplier |
| 3879 | * in the Capabilities Register. |
| 3880 | */ |
| 3881 | if (host->clk_mul) |
| 3882 | host->clk_mul += 1; |
| 3883 | |
| 3884 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3885 | * Set host parameters. |
| 3886 | */ |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 3887 | max_clk = host->max_clk; |
| 3888 | |
Marek Szyprowski | ce5f036 | 2010-08-10 18:01:56 -0700 | [diff] [blame] | 3889 | if (host->ops->get_min_clock) |
Anton Vorontsov | a9e58f2 | 2009-07-29 15:04:16 -0700 | [diff] [blame] | 3890 | mmc->f_min = host->ops->get_min_clock(host); |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 3891 | else if (host->version >= SDHCI_SPEC_300) { |
| 3892 | if (host->clk_mul) { |
| 3893 | mmc->f_min = (host->max_clk * host->clk_mul) / 1024; |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 3894 | max_clk = host->max_clk * host->clk_mul; |
Arindam Nath | c3ed387 | 2011-05-05 12:19:06 +0530 | [diff] [blame] | 3895 | } else |
| 3896 | mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300; |
| 3897 | } else |
Zhangfei Gao | 0397526 | 2010-09-20 15:15:18 -0400 | [diff] [blame] | 3898 | mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200; |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 3899 | |
Adrian Hunter | d310ae4 | 2016-04-12 14:25:07 +0300 | [diff] [blame] | 3900 | if (!mmc->f_max || mmc->f_max > max_clk) |
Dong Aisheng | 5924175 | 2015-07-22 20:53:07 +0800 | [diff] [blame] | 3901 | mmc->f_max = max_clk; |
| 3902 | |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3903 | if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) { |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3904 | host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >> |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3905 | SDHCI_TIMEOUT_CLK_SHIFT; |
| 3906 | if (host->timeout_clk == 0) { |
| 3907 | if (host->ops->get_timeout_clock) { |
| 3908 | host->timeout_clk = |
| 3909 | host->ops->get_timeout_clock(host); |
| 3910 | } else { |
| 3911 | pr_err("%s: Hardware doesn't specify timeout clock frequency.\n", |
| 3912 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 3913 | ret = -ENODEV; |
| 3914 | goto undma; |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3915 | } |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 3916 | } |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 3917 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3918 | if (host->caps & SDHCI_TIMEOUT_CLK_UNIT) |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3919 | host->timeout_clk *= 1000; |
Andy Shevchenko | 272308c | 2011-08-03 18:36:00 +0300 | [diff] [blame] | 3920 | |
Adrian Hunter | 9951362 | 2016-03-07 13:33:55 +0200 | [diff] [blame] | 3921 | if (override_timeout_clk) |
| 3922 | host->timeout_clk = override_timeout_clk; |
| 3923 | |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3924 | mmc->max_busy_timeout = host->ops->get_max_timeout_count ? |
Aisheng Dong | a6ff5ae | 2014-08-27 15:26:27 +0800 | [diff] [blame] | 3925 | host->ops->get_max_timeout_count(host) : 1 << 27; |
Aisheng Dong | 28aab05 | 2014-08-27 15:26:31 +0800 | [diff] [blame] | 3926 | mmc->max_busy_timeout /= host->timeout_clk; |
| 3927 | } |
Adrian Hunter | 58d1246 | 2011-06-28 17:16:03 +0300 | [diff] [blame] | 3928 | |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 3929 | mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23; |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 3930 | mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; |
Andrei Warkentin | e89d456 | 2011-05-23 15:06:37 -0500 | [diff] [blame] | 3931 | |
| 3932 | if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) |
| 3933 | host->flags |= SDHCI_AUTO_CMD12; |
Anton Vorontsov | 5fe23c7 | 2009-06-18 00:14:08 +0400 | [diff] [blame] | 3934 | |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 3935 | /* Auto-CMD23 stuff only works in ADMA or PIO. */ |
Andrei Warkentin | 4f3d3e9 | 2011-05-25 10:42:50 -0400 | [diff] [blame] | 3936 | if ((host->version >= SDHCI_SPEC_300) && |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 3937 | ((host->flags & SDHCI_USE_ADMA) || |
Scott Branden | 3bfa6f0 | 2015-02-09 16:06:28 -0800 | [diff] [blame] | 3938 | !(host->flags & SDHCI_USE_SDMA)) && |
| 3939 | !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) { |
Andrei Warkentin | 8edf6371 | 2011-05-23 15:06:39 -0500 | [diff] [blame] | 3940 | host->flags |= SDHCI_AUTO_CMD23; |
| 3941 | DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc)); |
| 3942 | } else { |
| 3943 | DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc)); |
| 3944 | } |
| 3945 | |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 3946 | /* |
| 3947 | * A controller may support 8-bit width, but the board itself |
| 3948 | * might not have the pins brought out. Boards that support |
| 3949 | * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in |
| 3950 | * their platform code before calling sdhci_add_host(), and we |
| 3951 | * won't assume 8-bit width for hosts without that CAP. |
| 3952 | */ |
Anton Vorontsov | 5fe23c7 | 2009-06-18 00:14:08 +0400 | [diff] [blame] | 3953 | if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA)) |
Philip Rakity | 15ec446 | 2010-11-19 16:48:39 -0500 | [diff] [blame] | 3954 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 3955 | |
Jerry Huang | 63ef5d8 | 2012-10-25 13:47:19 +0800 | [diff] [blame] | 3956 | if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23) |
| 3957 | mmc->caps &= ~MMC_CAP_CMD23; |
| 3958 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3959 | if (host->caps & SDHCI_CAN_DO_HISPD) |
Zhangfei Gao | a29e7e1 | 2010-08-16 21:15:32 -0400 | [diff] [blame] | 3960 | mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 3961 | |
Jaehoon Chung | 176d1ed | 2010-09-27 09:42:20 +0100 | [diff] [blame] | 3962 | if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) && |
Jaehoon Chung | 860951c | 2016-06-21 10:13:26 +0900 | [diff] [blame] | 3963 | mmc_card_is_removable(mmc) && |
Guoping Yu | e3b17cf | 2014-08-20 16:42:55 +0800 | [diff] [blame] | 3964 | mmc_gpio_get_cd(host->mmc) < 0 && |
| 3965 | !(mmc->caps2 & MMC_CAP2_NONHOTPLUG)) |
Anton Vorontsov | 68d1fb7 | 2009-03-17 00:13:52 +0300 | [diff] [blame] | 3966 | mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 3967 | |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 3968 | /* 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] | 3969 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 3970 | ret = regulator_enable(mmc->supply.vqmmc); |
| 3971 | if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000, |
| 3972 | 1950000)) |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3973 | host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | |
| 3974 | SDHCI_SUPPORT_SDR50 | |
| 3975 | SDHCI_SUPPORT_DDR50); |
Chris Ball | a3361ab | 2013-03-11 17:51:53 -0400 | [diff] [blame] | 3976 | if (ret) { |
| 3977 | pr_warn("%s: Failed to enable vqmmc regulator: %d\n", |
| 3978 | mmc_hostname(mmc), ret); |
Adrian Hunter | 4bb7431 | 2014-11-06 15:19:04 +0200 | [diff] [blame] | 3979 | mmc->supply.vqmmc = ERR_PTR(-EINVAL); |
Chris Ball | a3361ab | 2013-03-11 17:51:53 -0400 | [diff] [blame] | 3980 | } |
Kevin Liu | 8363c37 | 2012-11-17 17:55:51 -0500 | [diff] [blame] | 3981 | } |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 3982 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3983 | if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) { |
| 3984 | host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | |
| 3985 | SDHCI_SUPPORT_DDR50); |
| 3986 | } |
Daniel Drake | 6a66180 | 2012-11-25 13:01:19 -0500 | [diff] [blame] | 3987 | |
Al Cooper | 4188bba | 2012-03-16 15:54:17 -0400 | [diff] [blame] | 3988 | /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3989 | if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 | |
| 3990 | SDHCI_SUPPORT_DDR50)) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3991 | mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; |
| 3992 | |
| 3993 | /* SDR104 supports also implies SDR50 support */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 3994 | if (host->caps1 & SDHCI_SUPPORT_SDR104) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3995 | mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; |
Giuseppe CAVALLARO | 156e14b | 2013-06-12 08:16:38 +0200 | [diff] [blame] | 3996 | /* SD3.0: SDR104 is supported so (for eMMC) the caps2 |
| 3997 | * field can be promoted to support HS200. |
| 3998 | */ |
Adrian Hunter | 549c0b1 | 2014-11-06 15:19:05 +0200 | [diff] [blame] | 3999 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200)) |
David Cohen | 13868bf | 2013-10-29 10:58:26 -0700 | [diff] [blame] | 4000 | mmc->caps2 |= MMC_CAP2_HS200; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4001 | } else if (host->caps1 & SDHCI_SUPPORT_SDR50) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4002 | mmc->caps |= MMC_CAP_UHS_SDR50; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4003 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4004 | |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 4005 | if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 && |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4006 | (host->caps1 & SDHCI_SUPPORT_HS400)) |
Adrian Hunter | e9fb05d | 2014-11-06 15:19:06 +0200 | [diff] [blame] | 4007 | mmc->caps2 |= MMC_CAP2_HS400; |
| 4008 | |
Adrian Hunter | 549c0b1 | 2014-11-06 15:19:05 +0200 | [diff] [blame] | 4009 | if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) && |
| 4010 | (IS_ERR(mmc->supply.vqmmc) || |
| 4011 | !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000, |
| 4012 | 1300000))) |
| 4013 | mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V; |
| 4014 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4015 | if ((host->caps1 & SDHCI_SUPPORT_DDR50) && |
| 4016 | !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50)) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4017 | mmc->caps |= MMC_CAP_UHS_DDR50; |
| 4018 | |
Girish K S | 069c9f1 | 2012-01-06 09:56:39 +0530 | [diff] [blame] | 4019 | /* Does the host need tuning for SDR50? */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4020 | if (host->caps1 & SDHCI_USE_SDR50_TUNING) |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 4021 | host->flags |= SDHCI_SDR50_NEEDS_TUNING; |
| 4022 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4023 | /* Driver Type(s) (A, C, D) supported by the host */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4024 | if (host->caps1 & SDHCI_DRIVER_TYPE_A) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4025 | mmc->caps |= MMC_CAP_DRIVER_TYPE_A; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4026 | if (host->caps1 & SDHCI_DRIVER_TYPE_C) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4027 | mmc->caps |= MMC_CAP_DRIVER_TYPE_C; |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4028 | if (host->caps1 & SDHCI_DRIVER_TYPE_D) |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 4029 | mmc->caps |= MMC_CAP_DRIVER_TYPE_D; |
| 4030 | |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4031 | /* Initial value for re-tuning timer count */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4032 | host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >> |
| 4033 | SDHCI_RETUNING_TIMER_COUNT_SHIFT; |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4034 | |
| 4035 | /* |
| 4036 | * In case Re-tuning Timer is not disabled, the actual value of |
| 4037 | * re-tuning timer will be 2 ^ (n - 1). |
| 4038 | */ |
| 4039 | if (host->tuning_count) |
| 4040 | host->tuning_count = 1 << (host->tuning_count - 1); |
| 4041 | |
| 4042 | /* Re-tuning mode supported by the Host Controller */ |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4043 | host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >> |
Arindam Nath | cf2b5ee | 2011-05-05 12:19:07 +0530 | [diff] [blame] | 4044 | SDHCI_RETUNING_MODE_SHIFT; |
| 4045 | |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4046 | ocr_avail = 0; |
Philip Rakity | bad37e1 | 2012-05-27 18:36:44 -0700 | [diff] [blame] | 4047 | |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4048 | /* |
| 4049 | * According to SD Host Controller spec v3.00, if the Host System |
| 4050 | * can afford more than 150mA, Host Driver should set XPC to 1. Also |
| 4051 | * the value is meaningful only if Voltage Support in the Capabilities |
| 4052 | * register is set. The actual current value is 4 times the register |
| 4053 | * value. |
| 4054 | */ |
| 4055 | max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT); |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4056 | if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) { |
Chuanxiao.Dong | ae90603 | 2014-08-01 14:00:13 +0800 | [diff] [blame] | 4057 | int curr = regulator_get_current_limit(mmc->supply.vmmc); |
Philip Rakity | bad37e1 | 2012-05-27 18:36:44 -0700 | [diff] [blame] | 4058 | if (curr > 0) { |
| 4059 | |
| 4060 | /* convert to SDHCI_MAX_CURRENT format */ |
| 4061 | curr = curr/1000; /* convert to mA */ |
| 4062 | curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER; |
| 4063 | |
| 4064 | curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT); |
| 4065 | max_current_caps = |
| 4066 | (curr << SDHCI_MAX_CURRENT_330_SHIFT) | |
| 4067 | (curr << SDHCI_MAX_CURRENT_300_SHIFT) | |
| 4068 | (curr << SDHCI_MAX_CURRENT_180_SHIFT); |
| 4069 | } |
| 4070 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4071 | |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4072 | if (host->caps & SDHCI_CAN_VDD_330) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4073 | ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4074 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4075 | mmc->max_current_330 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4076 | SDHCI_MAX_CURRENT_330_MASK) >> |
| 4077 | SDHCI_MAX_CURRENT_330_SHIFT) * |
| 4078 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4079 | } |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4080 | if (host->caps & SDHCI_CAN_VDD_300) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4081 | ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4082 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4083 | mmc->max_current_300 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4084 | SDHCI_MAX_CURRENT_300_MASK) >> |
| 4085 | SDHCI_MAX_CURRENT_300_SHIFT) * |
| 4086 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4087 | } |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4088 | if (host->caps & SDHCI_CAN_VDD_180) { |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4089 | ocr_avail |= MMC_VDD_165_195; |
| 4090 | |
Aaron Lu | 55c4665 | 2012-07-04 13:31:48 +0800 | [diff] [blame] | 4091 | mmc->max_current_180 = ((max_current_caps & |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4092 | SDHCI_MAX_CURRENT_180_MASK) >> |
| 4093 | SDHCI_MAX_CURRENT_180_SHIFT) * |
| 4094 | SDHCI_MAX_CURRENT_MULTIPLIER; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 4095 | } |
| 4096 | |
Ulf Hansson | 5fd26c7 | 2015-06-05 11:40:08 +0200 | [diff] [blame] | 4097 | /* If OCR set by host, use it instead. */ |
| 4098 | if (host->ocr_mask) |
| 4099 | ocr_avail = host->ocr_mask; |
| 4100 | |
| 4101 | /* If OCR set by external regulators, give it highest prio. */ |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4102 | if (mmc->ocr_avail) |
Tim Kryger | 5222161 | 2014-06-25 00:25:34 -0700 | [diff] [blame] | 4103 | ocr_avail = mmc->ocr_avail; |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4104 | |
Takashi Iwai | 8f230f4 | 2010-12-08 10:04:30 +0100 | [diff] [blame] | 4105 | mmc->ocr_avail = ocr_avail; |
| 4106 | mmc->ocr_avail_sdio = ocr_avail; |
| 4107 | if (host->ocr_avail_sdio) |
| 4108 | mmc->ocr_avail_sdio &= host->ocr_avail_sdio; |
| 4109 | mmc->ocr_avail_sd = ocr_avail; |
| 4110 | if (host->ocr_avail_sd) |
| 4111 | mmc->ocr_avail_sd &= host->ocr_avail_sd; |
| 4112 | else /* normal SD controllers don't support 1.8V */ |
| 4113 | mmc->ocr_avail_sd &= ~MMC_VDD_165_195; |
| 4114 | mmc->ocr_avail_mmc = ocr_avail; |
| 4115 | if (host->ocr_avail_mmc) |
| 4116 | mmc->ocr_avail_mmc &= host->ocr_avail_mmc; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 4117 | |
| 4118 | if (mmc->ocr_avail == 0) { |
Marek Vasut | 2e4456f | 2015-11-18 10:47:02 +0100 | [diff] [blame] | 4119 | pr_err("%s: Hardware doesn't report any support voltages.\n", |
| 4120 | mmc_hostname(mmc)); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4121 | ret = -ENODEV; |
| 4122 | goto unreg; |
Pierre Ossman | 146ad66 | 2006-06-30 02:22:23 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Adrian Hunter | 8cb851a | 2016-06-29 16:24:16 +0300 | [diff] [blame] | 4125 | if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | |
| 4126 | MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | |
| 4127 | MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) || |
| 4128 | (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V))) |
| 4129 | host->flags |= SDHCI_SIGNALING_180; |
| 4130 | |
| 4131 | if (mmc->caps2 & MMC_CAP2_HSX00_1_2V) |
| 4132 | host->flags |= SDHCI_SIGNALING_120; |
| 4133 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4134 | /* |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4135 | * Maximum number of segments. Depends on if the hardware |
| 4136 | * can do scatter/gather or not. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4137 | */ |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4138 | if (host->flags & SDHCI_USE_ADMA) |
Adrian Hunter | 4fb213f | 2014-11-04 12:42:43 +0200 | [diff] [blame] | 4139 | mmc->max_segs = SDHCI_MAX_SEGS; |
Richard Röjfors | a13abc7 | 2009-09-22 16:45:30 -0700 | [diff] [blame] | 4140 | else if (host->flags & SDHCI_USE_SDMA) |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 4141 | mmc->max_segs = 1; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4142 | else /* PIO */ |
Adrian Hunter | 4fb213f | 2014-11-04 12:42:43 +0200 | [diff] [blame] | 4143 | mmc->max_segs = SDHCI_MAX_SEGS; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4144 | |
| 4145 | /* |
Adrian Hunter | ac00531 | 2014-12-05 19:25:28 +0200 | [diff] [blame] | 4146 | * Maximum number of sectors in one transfer. Limited by SDMA boundary |
| 4147 | * size (512KiB). Note some tuning modes impose a 4MiB limit, but this |
| 4148 | * is less anyway. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4149 | */ |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4150 | mmc->max_req_size = 524288; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4151 | |
| 4152 | /* |
| 4153 | * Maximum segment size. Could be one segment with the maximum number |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4154 | * of bytes. When doing hardware scatter/gather, each entry cannot |
| 4155 | * be larger than 64 KiB though. |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4156 | */ |
Olof Johansson | 30652aa | 2011-01-01 18:37:32 -0600 | [diff] [blame] | 4157 | if (host->flags & SDHCI_USE_ADMA) { |
| 4158 | if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC) |
| 4159 | mmc->max_seg_size = 65535; |
| 4160 | else |
| 4161 | mmc->max_seg_size = 65536; |
| 4162 | } else { |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4163 | mmc->max_seg_size = mmc->max_req_size; |
Olof Johansson | 30652aa | 2011-01-01 18:37:32 -0600 | [diff] [blame] | 4164 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4165 | |
| 4166 | /* |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 4167 | * Maximum block size. This varies from controller to controller and |
| 4168 | * is specified in the capabilities register. |
| 4169 | */ |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4170 | if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) { |
| 4171 | mmc->max_blk_size = 2; |
| 4172 | } else { |
Adrian Hunter | 28da358 | 2016-06-29 16:24:17 +0300 | [diff] [blame] | 4173 | mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >> |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4174 | SDHCI_MAX_BLOCK_SHIFT; |
| 4175 | if (mmc->max_blk_size >= 3) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 4176 | pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n", |
| 4177 | mmc_hostname(mmc)); |
Anton Vorontsov | 0633f65 | 2009-03-17 00:14:03 +0300 | [diff] [blame] | 4178 | mmc->max_blk_size = 0; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | mmc->max_blk_size = 512 << mmc->max_blk_size; |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4185 | * Maximum block count. |
| 4186 | */ |
Ben Dooks | 1388eef | 2009-06-14 12:40:53 +0100 | [diff] [blame] | 4187 | mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4188 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4189 | return 0; |
| 4190 | |
| 4191 | unreg: |
| 4192 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4193 | regulator_disable(mmc->supply.vqmmc); |
| 4194 | undma: |
| 4195 | if (host->align_buffer) |
| 4196 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4197 | host->adma_table_sz, host->align_buffer, |
| 4198 | host->align_addr); |
| 4199 | host->adma_table = NULL; |
| 4200 | host->align_buffer = NULL; |
| 4201 | |
| 4202 | return ret; |
| 4203 | } |
| 4204 | EXPORT_SYMBOL_GPL(sdhci_setup_host); |
| 4205 | |
| 4206 | int __sdhci_add_host(struct sdhci_host *host) |
| 4207 | { |
| 4208 | struct mmc_host *mmc = host->mmc; |
| 4209 | int ret; |
| 4210 | |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 4211 | /* |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4212 | * Init tasklets. |
| 4213 | */ |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4214 | tasklet_init(&host->finish_tasklet, |
| 4215 | sdhci_tasklet_finish, (unsigned long)host); |
| 4216 | |
Al Viro | e4cad1b | 2006-10-10 22:47:07 +0100 | [diff] [blame] | 4217 | setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 4218 | setup_timer(&host->data_timer, sdhci_timeout_data_timer, |
| 4219 | (unsigned long)host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4220 | |
Adrian Hunter | 250fb7b4 | 2014-12-05 19:41:10 +0200 | [diff] [blame] | 4221 | init_waitqueue_head(&host->buf_ready_int); |
Arindam Nath | b513ea2 | 2011-05-05 12:19:04 +0530 | [diff] [blame] | 4222 | |
Shawn Guo | 2af502c | 2013-07-05 14:38:55 +0800 | [diff] [blame] | 4223 | sdhci_init(host, 0); |
| 4224 | |
Russell King | 781e989 | 2014-04-25 12:55:46 +0100 | [diff] [blame] | 4225 | ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq, |
| 4226 | IRQF_SHARED, mmc_hostname(mmc), host); |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4227 | if (ret) { |
| 4228 | pr_err("%s: Failed to request IRQ %d: %d\n", |
| 4229 | mmc_hostname(mmc), host->irq, ret); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 4230 | goto untasklet; |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4231 | } |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4232 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4233 | #ifdef CONFIG_MMC_DEBUG |
| 4234 | sdhci_dumpregs(host); |
| 4235 | #endif |
| 4236 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4237 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) { |
| 4238 | ret = sdhci_led_register(host); |
| 4239 | if (ret) { |
| 4240 | pr_err("%s: Failed to register LED device: %d\n", |
| 4241 | mmc_hostname(mmc), ret); |
| 4242 | goto unirq; |
| 4243 | } |
Mark Brown | 0fc81ee | 2012-07-02 14:26:15 +0100 | [diff] [blame] | 4244 | } |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4245 | |
Pierre Ossman | 5f25a66 | 2006-10-04 02:15:39 -0700 | [diff] [blame] | 4246 | mmiowb(); |
| 4247 | |
Asutosh Das | 214b966 | 2013-06-13 14:27:42 +0530 | [diff] [blame] | 4248 | if (host->quirks2 & SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR) { |
| 4249 | host->ier = (host->ier & ~SDHCI_INT_DATA_END_BIT); |
| 4250 | sdhci_writel(host, host->ier, SDHCI_INT_ENABLE); |
| 4251 | sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE); |
| 4252 | } |
| 4253 | |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 4254 | if (mmc->caps2 & MMC_CAP2_CMD_QUEUE) { |
| 4255 | bool dma64 = (host->flags & SDHCI_USE_ADMA_64BIT) ? |
| 4256 | true : false; |
| 4257 | ret = sdhci_cmdq_init(host, mmc, dma64); |
| 4258 | if (ret) |
| 4259 | pr_err("%s: CMDQ init: failed (%d)\n", |
| 4260 | mmc_hostname(host->mmc), ret); |
| 4261 | else |
| 4262 | host->cq_host->ops = &sdhci_cmdq_ops; |
| 4263 | } |
| 4264 | |
| 4265 | pr_info("%s: SDHCI controller on %s [%s] using %s in %s mode\n", |
| 4266 | mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)), |
Adrian Hunter | e57a5f6 | 2014-11-04 12:42:46 +0200 | [diff] [blame] | 4267 | (host->flags & SDHCI_USE_ADMA) ? |
Asutosh Das | 3621b37 | 2014-10-17 16:36:47 +0530 | [diff] [blame] | 4268 | ((host->flags & SDHCI_USE_ADMA_64BIT) ? |
| 4269 | "64-bit ADMA" : "32-bit ADMA") : |
| 4270 | ((host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO"), |
| 4271 | ((mmc->caps2 & MMC_CAP2_CMD_QUEUE) && !ret) ? |
| 4272 | "CMDQ" : "legacy"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4273 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 4274 | sdhci_enable_card_detection(host); |
| 4275 | |
Venkat Gopalakrishnan | 0c3a9e4 | 2014-12-15 17:45:03 -0800 | [diff] [blame] | 4276 | ret = mmc_add_host(mmc); |
| 4277 | if (ret) |
| 4278 | goto unled; |
| 4279 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4280 | return 0; |
| 4281 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4282 | unled: |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4283 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 4284 | sdhci_led_unregister(host); |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4285 | unirq: |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 4286 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 4287 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 4288 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4289 | free_irq(host->irq, host); |
Pierre Ossman | 8ef1a14 | 2006-06-30 02:22:21 -0700 | [diff] [blame] | 4290 | untasklet: |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4291 | tasklet_kill(&host->finish_tasklet); |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4292 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4293 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4294 | regulator_disable(mmc->supply.vqmmc); |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4295 | |
Adrian Hunter | eb5c20d | 2016-04-12 14:25:08 +0300 | [diff] [blame] | 4296 | if (host->align_buffer) |
| 4297 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4298 | host->adma_table_sz, host->align_buffer, |
| 4299 | host->align_addr); |
| 4300 | host->adma_table = NULL; |
| 4301 | host->align_buffer = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4302 | |
| 4303 | return ret; |
| 4304 | } |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4305 | EXPORT_SYMBOL_GPL(__sdhci_add_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4306 | |
Adrian Hunter | 52f5336 | 2016-06-29 16:24:15 +0300 | [diff] [blame] | 4307 | int sdhci_add_host(struct sdhci_host *host) |
| 4308 | { |
| 4309 | int ret; |
| 4310 | |
| 4311 | ret = sdhci_setup_host(host); |
| 4312 | if (ret) |
| 4313 | return ret; |
| 4314 | |
| 4315 | return __sdhci_add_host(host); |
| 4316 | } |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4317 | EXPORT_SYMBOL_GPL(sdhci_add_host); |
| 4318 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4319 | void sdhci_remove_host(struct sdhci_host *host, int dead) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4320 | { |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4321 | struct mmc_host *mmc = host->mmc; |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4322 | unsigned long flags; |
| 4323 | |
| 4324 | if (dead) { |
| 4325 | spin_lock_irqsave(&host->lock, flags); |
| 4326 | |
| 4327 | host->flags |= SDHCI_DEVICE_DEAD; |
| 4328 | |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 4329 | if (sdhci_has_requests(host)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4330 | pr_err("%s: Controller removed during " |
Markus Mayer | 4e743f1 | 2014-07-03 13:27:42 -0700 | [diff] [blame] | 4331 | " transfer!\n", mmc_hostname(mmc)); |
Adrian Hunter | 5d0d11c | 2016-06-29 16:24:31 +0300 | [diff] [blame] | 4332 | sdhci_error_out_mrqs(host, -ENOMEDIUM); |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | spin_unlock_irqrestore(&host->lock, flags); |
| 4336 | } |
| 4337 | |
Anton Vorontsov | 7260cf5 | 2009-03-17 00:13:48 +0300 | [diff] [blame] | 4338 | sdhci_disable_card_detection(host); |
| 4339 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 4340 | mmc_remove_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4341 | |
Pavan Anamula | 5b76150 | 2015-07-23 18:45:37 +0530 | [diff] [blame] | 4342 | if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) |
| 4343 | sdhci_led_unregister(host); |
Pierre Ossman | 2f730fe | 2008-03-17 10:29:38 +0100 | [diff] [blame] | 4344 | |
Pierre Ossman | 1e72859 | 2008-04-16 19:13:13 +0200 | [diff] [blame] | 4345 | if (!dead) |
Russell King | 03231f9 | 2014-04-25 12:57:12 +0100 | [diff] [blame] | 4346 | sdhci_do_reset(host, SDHCI_RESET_ALL); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4347 | |
Russell King | b537f94 | 2014-04-25 12:56:01 +0100 | [diff] [blame] | 4348 | sdhci_writel(host, 0, SDHCI_INT_ENABLE); |
| 4349 | sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4350 | free_irq(host->irq, host); |
| 4351 | |
| 4352 | del_timer_sync(&host->timer); |
Adrian Hunter | d7422fb | 2016-06-29 16:24:33 +0300 | [diff] [blame] | 4353 | del_timer_sync(&host->data_timer); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4354 | |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4355 | tasklet_kill(&host->finish_tasklet); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4356 | |
Tim Kryger | 3a48edc | 2014-06-13 10:13:56 -0700 | [diff] [blame] | 4357 | if (!IS_ERR(mmc->supply.vqmmc)) |
| 4358 | regulator_disable(mmc->supply.vqmmc); |
Philip Rakity | 6231f3d | 2012-07-23 15:56:23 -0700 | [diff] [blame] | 4359 | |
Russell King | edd63fc | 2016-01-26 13:39:50 +0000 | [diff] [blame] | 4360 | if (host->align_buffer) |
Russell King | e66e61c | 2016-01-26 13:39:55 +0000 | [diff] [blame] | 4361 | dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz + |
| 4362 | host->adma_table_sz, host->align_buffer, |
| 4363 | host->align_addr); |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4364 | |
Adrian Hunter | 4efaa6f | 2014-11-04 12:42:39 +0200 | [diff] [blame] | 4365 | host->adma_table = NULL; |
Pierre Ossman | 2134a92 | 2008-06-28 18:28:51 +0200 | [diff] [blame] | 4366 | host->align_buffer = NULL; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4367 | } |
| 4368 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4369 | EXPORT_SYMBOL_GPL(sdhci_remove_host); |
| 4370 | |
| 4371 | void sdhci_free_host(struct sdhci_host *host) |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4372 | { |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4373 | mmc_free_host(host->mmc); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4374 | } |
| 4375 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4376 | EXPORT_SYMBOL_GPL(sdhci_free_host); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4377 | |
| 4378 | /*****************************************************************************\ |
| 4379 | * * |
| 4380 | * Driver init/exit * |
| 4381 | * * |
| 4382 | \*****************************************************************************/ |
| 4383 | |
| 4384 | static int __init sdhci_drv_init(void) |
| 4385 | { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4386 | pr_info(DRIVER_NAME |
Pierre Ossman | 52fbf9c | 2007-02-09 08:23:41 +0100 | [diff] [blame] | 4387 | ": Secure Digital Host Controller Interface driver\n"); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 4388 | pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4389 | |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4390 | return 0; |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | static void __exit sdhci_drv_exit(void) |
| 4394 | { |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | module_init(sdhci_drv_init); |
| 4398 | module_exit(sdhci_drv_exit); |
| 4399 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4400 | module_param(debug_quirks, uint, 0444); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 4401 | module_param(debug_quirks2, uint, 0444); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4402 | |
Pierre Ossman | 32710e8 | 2009-04-08 20:14:54 +0200 | [diff] [blame] | 4403 | MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>"); |
Pierre Ossman | b8c86fc | 2008-03-18 17:35:49 +0100 | [diff] [blame] | 4404 | MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver"); |
Pierre Ossman | d129bce | 2006-03-24 03:18:17 -0800 | [diff] [blame] | 4405 | MODULE_LICENSE("GPL"); |
Pierre Ossman | 6743527 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4406 | |
Pierre Ossman | df673b2 | 2006-06-30 02:22:31 -0700 | [diff] [blame] | 4407 | MODULE_PARM_DESC(debug_quirks, "Force certain quirks."); |
Adrian Hunter | 66fd8ad | 2011-10-03 15:33:34 +0300 | [diff] [blame] | 4408 | MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks."); |