blob: cbb245b5853873cbddc2f1a0967c8bcc989ea4e1 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * 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 Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b095a2012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010057#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030058static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030060static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
61static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030062#else
63static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
64{
65 return 0;
66}
67static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
68{
69 return 0;
70}
Adrian Hunterf0710a52013-05-06 12:17:32 +030071static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
72{
73}
74static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
75{
76}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030077#endif
78
Pierre Ossmand129bce2006-03-24 03:18:17 -080079static void sdhci_dumpregs(struct sdhci_host *host)
80{
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070082 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080083
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_DMA_ADDRESS),
86 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readw(host, SDHCI_BLOCK_SIZE),
89 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readl(host, SDHCI_ARGUMENT),
92 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readl(host, SDHCI_PRESENT_STATE),
95 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readb(host, SDHCI_POWER_CONTROL),
98 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
101 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
104 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_INT_ENABLE),
107 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readw(host, SDHCI_ACMD12_ERR),
110 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300112 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500113 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500115 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530118 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119
Adrian Huntere57a5f62014-11-04 12:42:46 +0200120 if (host->flags & SDHCI_USE_ADMA) {
121 if (host->flags & SDHCI_USE_64_BIT_DMA)
122 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
123 readl(host->ioaddr + SDHCI_ADMA_ERROR),
124 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
126 else
127 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
128 readl(host->ioaddr + SDHCI_ADMA_ERROR),
129 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
130 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100131
Girish K Sa3c76eb2011-10-11 11:44:09 +0530132 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800133}
134
135/*****************************************************************************\
136 * *
137 * Low level functions *
138 * *
139\*****************************************************************************/
140
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300141static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
142{
Russell King5b4f1f62014-04-25 12:57:02 +0100143 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300144
Adrian Hunterc79396c2011-12-27 15:48:42 +0200145 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100146 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300147 return;
148
Russell King5b4f1f62014-04-25 12:57:02 +0100149 if (enable) {
150 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
151 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800152
Russell King5b4f1f62014-04-25 12:57:02 +0100153 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
154 SDHCI_INT_CARD_INSERT;
155 } else {
156 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
157 }
Russell Kingb537f942014-04-25 12:56:01 +0100158
159 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
160 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300161}
162
163static void sdhci_enable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, true);
166}
167
168static void sdhci_disable_card_detection(struct sdhci_host *host)
169{
170 sdhci_set_card_detection(host, false);
171}
172
Russell King03231f92014-04-25 12:57:12 +0100173void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700175 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800176
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300177 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178
Adrian Hunterf0710a52013-05-06 12:17:32 +0300179 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300181 /* Reset-all turns off SD Bus Power */
182 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
183 sdhci_runtime_pm_bus_off(host);
184 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800185
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 /* Wait max 100 ms */
187 timeout = 100;
188
189 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300190 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700191 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530192 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700193 mmc_hostname(host->mmc), (int)mask);
194 sdhci_dumpregs(host);
195 return;
196 }
197 timeout--;
198 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800199 }
Russell King03231f92014-04-25 12:57:12 +0100200}
201EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300202
Russell King03231f92014-04-25 12:57:12 +0100203static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
204{
205 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
206 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
207 SDHCI_CARD_PRESENT))
208 return;
209 }
210
211 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800212
Russell Kingda91a8f2014-04-25 13:00:12 +0100213 if (mask & SDHCI_RESET_ALL) {
214 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
215 if (host->ops->enable_dma)
216 host->ops->enable_dma(host);
217 }
218
219 /* Resetting the controller clears many */
220 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
225
226static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100229 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 else
Russell King03231f92014-04-25 12:57:12 +0100231 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Russell Kingb537f942014-04-25 12:56:01 +0100233 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
234 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
235 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
236 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
237 SDHCI_INT_RESPONSE;
238
239 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
240 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241
242 if (soft) {
243 /* force clock reconfiguration */
244 host->clock = 0;
245 sdhci_set_ios(host->mmc, &host->mmc->ios);
246 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249static void sdhci_reinit(struct sdhci_host *host)
250{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800251 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800252 /*
253 * Retuning stuffs are affected by different cards inserted and only
254 * applicable to UHS-I cards. So reset these fields to their initial
255 * value when card is removed.
256 */
Aaron Lu973905f2012-07-04 13:29:09 +0800257 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
258 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
259
Aaron Lub67c6b42012-06-29 16:17:31 +0800260 del_timer_sync(&host->tuning_timer);
261 host->flags &= ~SDHCI_NEEDS_RETUNING;
262 host->mmc->max_blk_count =
263 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
264 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300265 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266}
267
268static void sdhci_activate_led(struct sdhci_host *host)
269{
270 u8 ctrl;
271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275}
276
277static void sdhci_deactivate_led(struct sdhci_host *host)
278{
279 u8 ctrl;
280
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284}
285
Pierre Ossmanf9134312008-12-21 17:01:48 +0100286#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100287static void sdhci_led_control(struct led_classdev *led,
288 enum led_brightness brightness)
289{
290 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
291 unsigned long flags;
292
293 spin_lock_irqsave(&host->lock, flags);
294
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300295 if (host->runtime_suspended)
296 goto out;
297
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 if (brightness == LED_OFF)
299 sdhci_deactivate_led(host);
300 else
301 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300302out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100303 spin_unlock_irqrestore(&host->lock, flags);
304}
305#endif
306
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307/*****************************************************************************\
308 * *
309 * Core functions *
310 * *
311\*****************************************************************************/
312
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314{
Pierre Ossman76591502008-07-21 00:32:11 +0200315 unsigned long flags;
316 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700317 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200318 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200323 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (!sg_miter_next(&host->sg_miter))
329 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 blksize -= len;
334 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 while (len) {
339 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300340 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200341 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342 }
Pierre Ossman76591502008-07-21 00:32:11 +0200343
344 *buf = scratch & 0xFF;
345
346 buf++;
347 scratch >>= 8;
348 chunk--;
349 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350 }
351 }
Pierre Ossman76591502008-07-21 00:32:11 +0200352
353 sg_miter_stop(&host->sg_miter);
354
355 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358static void sdhci_write_block_pio(struct sdhci_host *host)
359{
Pierre Ossman76591502008-07-21 00:32:11 +0200360 unsigned long flags;
361 size_t blksize, len, chunk;
362 u32 scratch;
363 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
365 DBG("PIO writing\n");
366
367 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 0;
369 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
373 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200374 if (!sg_miter_next(&host->sg_miter))
375 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 blksize -= len;
380 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 while (len) {
385 scratch |= (u32)*buf << (chunk * 8);
386
387 buf++;
388 chunk++;
389 len--;
390
391 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300392 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
397 }
Pierre Ossman76591502008-07-21 00:32:11 +0200398
399 sg_miter_stop(&host->sg_miter);
400
401 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402}
403
404static void sdhci_transfer_pio(struct sdhci_host *host)
405{
406 u32 mask;
407
408 BUG_ON(!host->data);
409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411 return;
412
413 if (host->data->flags & MMC_DATA_READ)
414 mask = SDHCI_DATA_AVAILABLE;
415 else
416 mask = SDHCI_SPACE_AVAILABLE;
417
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200418 /*
419 * Some controllers (JMicron JMB38x) mess up the buffer bits
420 * for transfers < 4 bytes. As long as it is just one block,
421 * we can ignore the bits.
422 */
423 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
424 (host->data->blocks == 1))
425 mask = ~0;
426
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300427 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300428 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
429 udelay(100);
430
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 if (host->data->flags & MMC_DATA_READ)
432 sdhci_read_block_pio(host);
433 else
434 sdhci_write_block_pio(host);
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 host->blocks--;
437 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 }
440
441 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800442}
443
Pierre Ossman2134a922008-06-28 18:28:51 +0200444static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
445{
446 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800447 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200448}
449
450static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
451{
Cong Wang482fce92011-11-27 13:27:00 +0800452 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 local_irq_restore(*flags);
454}
455
Adrian Huntere57a5f62014-11-04 12:42:46 +0200456static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
457 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800458{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200459 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Adrian Huntere57a5f62014-11-04 12:42:46 +0200461 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200462 dma_desc->cmd = cpu_to_le16(cmd);
463 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200464 dma_desc->addr_lo = cpu_to_le32((u32)addr);
465
466 if (host->flags & SDHCI_USE_64_BIT_DMA)
467 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800468}
469
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200470static void sdhci_adma_mark_end(void *desc)
471{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200472 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200473
Adrian Huntere57a5f62014-11-04 12:42:46 +0200474 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200475 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200476}
477
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200478static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200479 struct mmc_data *data)
480{
481 int direction;
482
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200483 void *desc;
484 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200485 dma_addr_t addr;
486 dma_addr_t align_addr;
487 int len, offset;
488
489 struct scatterlist *sg;
490 int i;
491 char *buffer;
492 unsigned long flags;
493
494 /*
495 * The spec does not specify endianness of descriptor table.
496 * We currently guess that it is LE.
497 */
498
499 if (data->flags & MMC_DATA_READ)
500 direction = DMA_FROM_DEVICE;
501 else
502 direction = DMA_TO_DEVICE;
503
Pierre Ossman2134a922008-06-28 18:28:51 +0200504 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200505 host->align_buffer, host->align_buffer_sz, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700506 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200507 goto fail;
Adrian Hunter76fe3792014-11-04 12:42:42 +0200508 BUG_ON(host->align_addr & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200509
510 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
511 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200512 if (host->sg_count == 0)
513 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200514
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200515 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516 align = host->align_buffer;
517
518 align_addr = host->align_addr;
519
520 for_each_sg(data->sg, sg, host->sg_count, i) {
521 addr = sg_dma_address(sg);
522 len = sg_dma_len(sg);
523
524 /*
525 * The SDHCI specification states that ADMA
526 * addresses must be 32-bit aligned. If they
527 * aren't, then we use a bounce buffer for
528 * the (up to three) bytes that screw up the
529 * alignment.
530 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200531 offset = (host->align_sz - (addr & host->align_mask)) &
532 host->align_mask;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 if (offset) {
534 if (data->flags & MMC_DATA_WRITE) {
535 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200536 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
537 (PAGE_SIZE - offset));
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 memcpy(align, buffer, offset);
539 sdhci_kunmap_atomic(buffer, &flags);
540 }
541
Ben Dooks118cd172010-03-05 13:43:26 -0800542 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200543 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200544 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200545
546 BUG_ON(offset > 65536);
547
Adrian Hunter76fe3792014-11-04 12:42:42 +0200548 align += host->align_sz;
549 align_addr += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550
Adrian Hunter76fe3792014-11-04 12:42:42 +0200551 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200552
553 addr += offset;
554 len -= offset;
555 }
556
Pierre Ossman2134a922008-06-28 18:28:51 +0200557 BUG_ON(len > 65536);
558
Ben Dooks118cd172010-03-05 13:43:26 -0800559 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200560 sdhci_adma_write_desc(host, desc, addr, len, ADMA2_TRAN_VALID);
Adrian Hunter76fe3792014-11-04 12:42:42 +0200561 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200562
563 /*
564 * If this triggers then we have a calculation bug
565 * somewhere. :/
566 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200567 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200568 }
569
Thomas Abraham70764a92010-05-26 14:42:04 -0700570 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
571 /*
572 * Mark the last descriptor as the terminating descriptor
573 */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200574 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200575 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200576 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700577 }
578 } else {
579 /*
580 * Add a terminating entry.
581 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200582
Thomas Abraham70764a92010-05-26 14:42:04 -0700583 /* nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200584 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700585 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200586
587 /*
588 * Resync align buffer as we might have changed it.
589 */
590 if (data->flags & MMC_DATA_WRITE) {
591 dma_sync_single_for_device(mmc_dev(host->mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +0200592 host->align_addr, host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200593 }
594
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200595 return 0;
596
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200597unmap_align:
598 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200599 host->align_buffer_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200600fail:
601 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602}
603
604static void sdhci_adma_table_post(struct sdhci_host *host,
605 struct mmc_data *data)
606{
607 int direction;
608
609 struct scatterlist *sg;
610 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200611 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200612 char *buffer;
613 unsigned long flags;
Russell Kingde0b65a2014-04-25 12:58:29 +0100614 bool has_unaligned;
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
616 if (data->flags & MMC_DATA_READ)
617 direction = DMA_FROM_DEVICE;
618 else
619 direction = DMA_TO_DEVICE;
620
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Adrian Hunter76fe3792014-11-04 12:42:42 +0200622 host->align_buffer_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200623
Russell Kingde0b65a2014-04-25 12:58:29 +0100624 /* Do a quick scan of the SG list for any unaligned mappings */
625 has_unaligned = false;
626 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter76fe3792014-11-04 12:42:42 +0200627 if (sg_dma_address(sg) & host->align_mask) {
Russell Kingde0b65a2014-04-25 12:58:29 +0100628 has_unaligned = true;
629 break;
630 }
631
632 if (has_unaligned && data->flags & MMC_DATA_READ) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200633 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
634 data->sg_len, direction);
635
636 align = host->align_buffer;
637
638 for_each_sg(data->sg, sg, host->sg_count, i) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200639 if (sg_dma_address(sg) & host->align_mask) {
640 size = host->align_sz -
641 (sg_dma_address(sg) & host->align_mask);
Pierre Ossman2134a922008-06-28 18:28:51 +0200642
643 buffer = sdhci_kmap_atomic(sg, &flags);
Adrian Hunter8be78c62014-11-04 12:42:36 +0200644 WARN_ON(((long)buffer & (PAGE_SIZE - 1)) >
645 (PAGE_SIZE - size));
Pierre Ossman2134a922008-06-28 18:28:51 +0200646 memcpy(buffer, align, size);
647 sdhci_kunmap_atomic(buffer, &flags);
648
Adrian Hunter76fe3792014-11-04 12:42:42 +0200649 align += host->align_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200650 }
651 }
652 }
653
654 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
655 data->sg_len, direction);
656}
657
Andrei Warkentina3c77782011-04-11 16:13:42 -0500658static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800659{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700660 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500661 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700662 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800663
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200664 /*
665 * If the host controller provides us with an incorrect timeout
666 * value, just skip the check and use 0xE. The hardware may take
667 * longer to time out, but that's much better than having a too-short
668 * timeout value.
669 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200670 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200671 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200672
Andrei Warkentina3c77782011-04-11 16:13:42 -0500673 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100674 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500675 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800676
Andrei Warkentina3c77782011-04-11 16:13:42 -0500677 /* timeout in us */
678 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100679 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300680 else {
681 target_timeout = data->timeout_ns / 1000;
682 if (host->clock)
683 target_timeout += data->timeout_clks / host->clock;
684 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700685
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700686 /*
687 * Figure out needed cycles.
688 * We do this in steps in order to fit inside a 32 bit int.
689 * The first step is the minimum timeout, which will have a
690 * minimum resolution of 6 bits:
691 * (1) 2^13*1000 > 2^22,
692 * (2) host->timeout_clk < 2^16
693 * =>
694 * (1) / (2) > 2^6
695 */
696 count = 0;
697 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
698 while (current_timeout < target_timeout) {
699 count++;
700 current_timeout <<= 1;
701 if (count >= 0xF)
702 break;
703 }
704
705 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400706 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
707 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700708 count = 0xE;
709 }
710
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200711 return count;
712}
713
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300714static void sdhci_set_transfer_irqs(struct sdhci_host *host)
715{
716 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
717 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
718
719 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100720 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300721 else
Russell Kingb537f942014-04-25 12:56:01 +0100722 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
723
724 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
725 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300726}
727
Aisheng Dongb45e6682014-08-27 15:26:29 +0800728static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200729{
730 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800731
732 if (host->ops->set_timeout) {
733 host->ops->set_timeout(host, cmd);
734 } else {
735 count = sdhci_calc_timeout(host, cmd);
736 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
737 }
738}
739
740static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
741{
Pierre Ossman2134a922008-06-28 18:28:51 +0200742 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500743 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200744 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200745
746 WARN_ON(host->data);
747
Aisheng Dongb45e6682014-08-27 15:26:29 +0800748 if (data || (cmd->flags & MMC_RSP_BUSY))
749 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500750
751 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200752 return;
753
754 /* Sanity checks */
755 BUG_ON(data->blksz * data->blocks > 524288);
756 BUG_ON(data->blksz > host->mmc->max_blk_size);
757 BUG_ON(data->blocks > 65535);
758
759 host->data = data;
760 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400761 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200762
Richard Röjforsa13abc72009-09-22 16:45:30 -0700763 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100764 host->flags |= SDHCI_REQ_USE_DMA;
765
Pierre Ossman2134a922008-06-28 18:28:51 +0200766 /*
767 * FIXME: This doesn't account for merging when mapping the
768 * scatterlist.
769 */
770 if (host->flags & SDHCI_REQ_USE_DMA) {
771 int broken, i;
772 struct scatterlist *sg;
773
774 broken = 0;
775 if (host->flags & SDHCI_USE_ADMA) {
776 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
777 broken = 1;
778 } else {
779 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
780 broken = 1;
781 }
782
783 if (unlikely(broken)) {
784 for_each_sg(data->sg, sg, data->sg_len, i) {
785 if (sg->length & 0x3) {
786 DBG("Reverting to PIO because of "
787 "transfer size (%d)\n",
788 sg->length);
789 host->flags &= ~SDHCI_REQ_USE_DMA;
790 break;
791 }
792 }
793 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100794 }
795
796 /*
797 * The assumption here being that alignment is the same after
798 * translation to device address space.
799 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 if (host->flags & SDHCI_REQ_USE_DMA) {
801 int broken, i;
802 struct scatterlist *sg;
803
804 broken = 0;
805 if (host->flags & SDHCI_USE_ADMA) {
806 /*
807 * As we use 3 byte chunks to work around
808 * alignment problems, we need to check this
809 * quirk.
810 */
811 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
812 broken = 1;
813 } else {
814 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
815 broken = 1;
816 }
817
818 if (unlikely(broken)) {
819 for_each_sg(data->sg, sg, data->sg_len, i) {
820 if (sg->offset & 0x3) {
821 DBG("Reverting to PIO because of "
822 "bad alignment\n");
823 host->flags &= ~SDHCI_REQ_USE_DMA;
824 break;
825 }
826 }
827 }
828 }
829
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200830 if (host->flags & SDHCI_REQ_USE_DMA) {
831 if (host->flags & SDHCI_USE_ADMA) {
832 ret = sdhci_adma_table_pre(host, data);
833 if (ret) {
834 /*
835 * This only happens when someone fed
836 * us an invalid request.
837 */
838 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200839 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200840 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300841 sdhci_writel(host, host->adma_addr,
842 SDHCI_ADMA_ADDRESS);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200843 if (host->flags & SDHCI_USE_64_BIT_DMA)
844 sdhci_writel(host,
845 (u64)host->adma_addr >> 32,
846 SDHCI_ADMA_ADDRESS_HI);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200847 }
848 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300849 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200850
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300851 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200852 data->sg, data->sg_len,
853 (data->flags & MMC_DATA_READ) ?
854 DMA_FROM_DEVICE :
855 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300856 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200857 /*
858 * This only happens when someone fed
859 * us an invalid request.
860 */
861 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200862 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200863 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200864 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300865 sdhci_writel(host, sg_dma_address(data->sg),
866 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200867 }
868 }
869 }
870
Pierre Ossman2134a922008-06-28 18:28:51 +0200871 /*
872 * Always adjust the DMA selection as some controllers
873 * (e.g. JMicron) can't do PIO properly when the selection
874 * is ADMA.
875 */
876 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300877 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200878 ctrl &= ~SDHCI_CTRL_DMA_MASK;
879 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200880 (host->flags & SDHCI_USE_ADMA)) {
881 if (host->flags & SDHCI_USE_64_BIT_DMA)
882 ctrl |= SDHCI_CTRL_ADMA64;
883 else
884 ctrl |= SDHCI_CTRL_ADMA32;
885 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200886 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200887 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300888 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100889 }
890
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200891 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200892 int flags;
893
894 flags = SG_MITER_ATOMIC;
895 if (host->data->flags & MMC_DATA_READ)
896 flags |= SG_MITER_TO_SG;
897 else
898 flags |= SG_MITER_FROM_SG;
899 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200900 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300903 sdhci_set_transfer_irqs(host);
904
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400905 /* Set the DMA boundary value and block size */
906 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
907 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300908 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909}
910
911static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500912 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700913{
914 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500915 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700916
Dong Aisheng2b558c12013-10-30 22:09:48 +0800917 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800918 if (host->quirks2 &
919 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
920 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
921 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800922 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800923 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
924 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800925 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800926 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700927 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800928 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700929
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200930 WARN_ON(!host->data);
931
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700932 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500933 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
934 mode |= SDHCI_TRNS_MULTI;
935 /*
936 * If we are sending CMD23, CMD12 never gets sent
937 * on successful completion (so no Auto-CMD12).
938 */
939 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
940 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500941 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
942 mode |= SDHCI_TRNS_AUTO_CMD23;
943 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
944 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700945 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500946
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700947 if (data->flags & MMC_DATA_READ)
948 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100949 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700950 mode |= SDHCI_TRNS_DMA;
951
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300952 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953}
954
955static void sdhci_finish_data(struct sdhci_host *host)
956{
957 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958
959 BUG_ON(!host->data);
960
961 data = host->data;
962 host->data = NULL;
963
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100964 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200965 if (host->flags & SDHCI_USE_ADMA)
966 sdhci_adma_table_post(host, data);
967 else {
968 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
969 data->sg_len, (data->flags & MMC_DATA_READ) ?
970 DMA_FROM_DEVICE : DMA_TO_DEVICE);
971 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 }
973
974 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200975 * The specification states that the block count register must
976 * be updated, but it does not specify at what point in the
977 * data flow. That makes the register entirely useless to read
978 * back so we have to assume that nothing made it to the card
979 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200981 if (data->error)
982 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200984 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800985
Andrei Warkentine89d4562011-05-23 15:06:37 -0500986 /*
987 * Need to send CMD12 if -
988 * a) open-ended multiblock transfer (no CMD23)
989 * b) error in multiblock transfer
990 */
991 if (data->stop &&
992 (data->error ||
993 !host->mrq->sbc)) {
994
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 /*
996 * The controller needs a reset of internal state machines
997 * upon error conditions.
998 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200999 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +01001000 sdhci_do_reset(host, SDHCI_RESET_CMD);
1001 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002 }
1003
1004 sdhci_send_command(host, data->stop);
1005 } else
1006 tasklet_schedule(&host->finish_tasklet);
1007}
1008
Dong Aishengc0e551292013-09-13 19:11:31 +08001009void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010{
1011 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001012 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001013 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015 WARN_ON(host->cmd);
1016
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001018 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001019
1020 mask = SDHCI_CMD_INHIBIT;
1021 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1022 mask |= SDHCI_DATA_INHIBIT;
1023
1024 /* We shouldn't wait for data inihibit for stop commands, even
1025 though they might use busy signaling */
1026 if (host->mrq->data && (cmd == host->mrq->data->stop))
1027 mask &= ~SDHCI_DATA_INHIBIT;
1028
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001030 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301031 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001032 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001034 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 tasklet_schedule(&host->finish_tasklet);
1036 return;
1037 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001038 timeout--;
1039 mdelay(1);
1040 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001042 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001043 if (!cmd->data && cmd->busy_timeout > 9000)
1044 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001045 else
1046 timeout += 10 * HZ;
1047 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048
1049 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001050 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051
Andrei Warkentina3c77782011-04-11 16:13:42 -05001052 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055
Andrei Warkentine89d4562011-05-23 15:06:37 -05001056 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001057
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301059 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001061 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 tasklet_schedule(&host->finish_tasklet);
1063 return;
1064 }
1065
1066 if (!(cmd->flags & MMC_RSP_PRESENT))
1067 flags = SDHCI_CMD_RESP_NONE;
1068 else if (cmd->flags & MMC_RSP_136)
1069 flags = SDHCI_CMD_RESP_LONG;
1070 else if (cmd->flags & MMC_RSP_BUSY)
1071 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1072 else
1073 flags = SDHCI_CMD_RESP_SHORT;
1074
1075 if (cmd->flags & MMC_RSP_CRC)
1076 flags |= SDHCI_CMD_CRC;
1077 if (cmd->flags & MMC_RSP_OPCODE)
1078 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301079
1080 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301081 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1082 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083 flags |= SDHCI_CMD_DATA;
1084
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001085 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001086}
Dong Aishengc0e551292013-09-13 19:11:31 +08001087EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001088
1089static void sdhci_finish_command(struct sdhci_host *host)
1090{
1091 int i;
1092
1093 BUG_ON(host->cmd == NULL);
1094
1095 if (host->cmd->flags & MMC_RSP_PRESENT) {
1096 if (host->cmd->flags & MMC_RSP_136) {
1097 /* CRC is stripped so we need to do some shifting. */
1098 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001099 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100 SDHCI_RESPONSE + (3-i)*4) << 8;
1101 if (i != 3)
1102 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001103 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104 SDHCI_RESPONSE + (3-i)*4-1);
1105 }
1106 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001107 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001108 }
1109 }
1110
Pierre Ossman17b04292007-07-22 22:18:46 +02001111 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112
Andrei Warkentine89d4562011-05-23 15:06:37 -05001113 /* Finished CMD23, now send actual command. */
1114 if (host->cmd == host->mrq->sbc) {
1115 host->cmd = NULL;
1116 sdhci_send_command(host, host->mrq->cmd);
1117 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001118
Andrei Warkentine89d4562011-05-23 15:06:37 -05001119 /* Processed actual command. */
1120 if (host->data && host->data_early)
1121 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122
Andrei Warkentine89d4562011-05-23 15:06:37 -05001123 if (!host->cmd->data)
1124 tasklet_schedule(&host->finish_tasklet);
1125
1126 host->cmd = NULL;
1127 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128}
1129
Kevin Liu52983382013-01-31 11:31:37 +08001130static u16 sdhci_get_preset_value(struct sdhci_host *host)
1131{
Russell Kingd975f122014-04-25 12:59:31 +01001132 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001133
Russell Kingd975f122014-04-25 12:59:31 +01001134 switch (host->timing) {
1135 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001136 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1137 break;
Russell Kingd975f122014-04-25 12:59:31 +01001138 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001139 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1140 break;
Russell Kingd975f122014-04-25 12:59:31 +01001141 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001142 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1143 break;
Russell Kingd975f122014-04-25 12:59:31 +01001144 case MMC_TIMING_UHS_SDR104:
1145 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001146 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1147 break;
Russell Kingd975f122014-04-25 12:59:31 +01001148 case MMC_TIMING_UHS_DDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001149 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1150 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001151 case MMC_TIMING_MMC_HS400:
1152 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1153 break;
Kevin Liu52983382013-01-31 11:31:37 +08001154 default:
1155 pr_warn("%s: Invalid UHS-I mode selected\n",
1156 mmc_hostname(host->mmc));
1157 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1158 break;
1159 }
1160 return preset;
1161}
1162
Russell King17710592014-04-25 12:58:55 +01001163void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301165 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001166 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301167 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001168 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
Russell King1650d0c2014-04-25 12:58:50 +01001170 host->mmc->actual_clock = 0;
1171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001172 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173
1174 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001175 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176
Zhangfei Gao85105c52010-08-06 07:10:01 +08001177 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001178 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001179 u16 pre_val;
1180
1181 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1182 pre_val = sdhci_get_preset_value(host);
1183 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1184 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1185 if (host->clk_mul &&
1186 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1187 clk = SDHCI_PROG_CLOCK_MODE;
1188 real_div = div + 1;
1189 clk_mul = host->clk_mul;
1190 } else {
1191 real_div = max_t(int, 1, div << 1);
1192 }
1193 goto clock_set;
1194 }
1195
Arindam Nathc3ed3872011-05-05 12:19:06 +05301196 /*
1197 * Check if the Host Controller supports Programmable Clock
1198 * Mode.
1199 */
1200 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001201 for (div = 1; div <= 1024; div++) {
1202 if ((host->max_clk * host->clk_mul / div)
1203 <= clock)
1204 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001205 }
Kevin Liu52983382013-01-31 11:31:37 +08001206 /*
1207 * Set Programmable Clock Mode in the Clock
1208 * Control register.
1209 */
1210 clk = SDHCI_PROG_CLOCK_MODE;
1211 real_div = div;
1212 clk_mul = host->clk_mul;
1213 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301214 } else {
1215 /* Version 3.00 divisors must be a multiple of 2. */
1216 if (host->max_clk <= clock)
1217 div = 1;
1218 else {
1219 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1220 div += 2) {
1221 if ((host->max_clk / div) <= clock)
1222 break;
1223 }
1224 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001225 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301226 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001227 }
1228 } else {
1229 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001230 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001231 if ((host->max_clk / div) <= clock)
1232 break;
1233 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001234 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301235 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237
Kevin Liu52983382013-01-31 11:31:37 +08001238clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001239 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001240 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301241 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001242 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1243 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001245 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246
Chris Ball27f6cb12009-09-22 16:45:31 -07001247 /* Wait max 20 ms */
1248 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001249 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001250 & SDHCI_CLOCK_INT_STABLE)) {
1251 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301252 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001253 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 sdhci_dumpregs(host);
1255 return;
1256 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001257 timeout--;
1258 mdelay(1);
1259 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260
1261 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001262 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263}
Russell King17710592014-04-25 12:58:55 +01001264EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265
Russell King24fbb3c2014-04-25 13:00:06 +01001266static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1267 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001268{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001269 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001270 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001271
Tim Kryger52221612014-06-25 00:25:34 -07001272 if (!IS_ERR(mmc->supply.vmmc)) {
1273 spin_unlock_irq(&host->lock);
Markus Mayer4e743f12014-07-03 13:27:42 -07001274 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Tim Kryger52221612014-06-25 00:25:34 -07001275 spin_lock_irq(&host->lock);
1276 return;
1277 }
1278
Russell King24fbb3c2014-04-25 13:00:06 +01001279 if (mode != MMC_POWER_OFF) {
1280 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001281 case MMC_VDD_165_195:
1282 pwr = SDHCI_POWER_180;
1283 break;
1284 case MMC_VDD_29_30:
1285 case MMC_VDD_30_31:
1286 pwr = SDHCI_POWER_300;
1287 break;
1288 case MMC_VDD_32_33:
1289 case MMC_VDD_33_34:
1290 pwr = SDHCI_POWER_330;
1291 break;
1292 default:
1293 BUG();
1294 }
1295 }
1296
1297 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001298 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001299
Pierre Ossmanae628902009-05-03 20:45:03 +02001300 host->pwr = pwr;
1301
1302 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001303 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001304 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1305 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001306 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001307 } else {
1308 /*
1309 * Spec says that we should clear the power reg before setting
1310 * a new value. Some controllers don't seem to like this though.
1311 */
1312 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1313 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001314
Russell Kinge921a8b2014-04-25 13:00:01 +01001315 /*
1316 * At least the Marvell CaFe chip gets confused if we set the
1317 * voltage and set turn on power at the same time, so set the
1318 * voltage first.
1319 */
1320 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1321 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001322
Russell Kinge921a8b2014-04-25 13:00:01 +01001323 pwr |= SDHCI_POWER_ON;
1324
Pierre Ossmanae628902009-05-03 20:45:03 +02001325 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1326
Russell Kinge921a8b2014-04-25 13:00:01 +01001327 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1328 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001329
Russell Kinge921a8b2014-04-25 13:00:01 +01001330 /*
1331 * Some controllers need an extra 10ms delay of 10ms before
1332 * they can apply clock after applying power
1333 */
1334 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1335 mdelay(10);
1336 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001337}
1338
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339/*****************************************************************************\
1340 * *
1341 * MMC callbacks *
1342 * *
1343\*****************************************************************************/
1344
1345static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1346{
1347 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001348 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349 unsigned long flags;
Aaron Lu473b095a2012-07-03 17:27:49 +08001350 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351
1352 host = mmc_priv(mmc);
1353
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001354 sdhci_runtime_pm_get(host);
1355
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356 spin_lock_irqsave(&host->lock, flags);
1357
1358 WARN_ON(host->mrq != NULL);
1359
Pierre Ossmanf9134312008-12-21 17:01:48 +01001360#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001362#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001363
1364 /*
1365 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1366 * requests if Auto-CMD12 is enabled.
1367 */
1368 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001369 if (mrq->stop) {
1370 mrq->data->stop = NULL;
1371 mrq->stop = NULL;
1372 }
1373 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374
1375 host->mrq = mrq;
1376
Shawn Guo505a8682012-12-11 15:23:42 +08001377 /*
1378 * Firstly check card presence from cd-gpio. The return could
1379 * be one of the following possibilities:
1380 * negative: cd-gpio is not available
1381 * zero: cd-gpio is used, and card is removed
1382 * one: cd-gpio is used, and card is present
1383 */
1384 present = mmc_gpio_get_cd(host->mmc);
1385 if (present < 0) {
1386 /* If polling, assume that the card is always present. */
1387 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1388 present = 1;
1389 else
1390 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1391 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001392 }
1393
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001394 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001395 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301397 } else {
1398 u32 present_state;
1399
1400 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1401 /*
1402 * Check if the re-tuning timer has already expired and there
Yi Sun7756a96d2014-09-09 02:13:59 +00001403 * is no on-going data transfer and DAT0 is not busy. If so,
1404 * we need to execute tuning procedure before sending command.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301405 */
1406 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
Yi Sun7756a96d2014-09-09 02:13:59 +00001407 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ)) &&
1408 (present_state & SDHCI_DATA_0_LVL_MASK)) {
Chris Ball14efd952012-11-05 14:29:49 -05001409 if (mmc->card) {
1410 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1411 tuning_opcode =
1412 mmc->card->type == MMC_TYPE_MMC ?
1413 MMC_SEND_TUNING_BLOCK_HS200 :
1414 MMC_SEND_TUNING_BLOCK;
Chuansheng Liu63c21182013-11-05 14:52:45 +08001415
1416 /* Here we need to set the host->mrq to NULL,
1417 * in case the pending finish_tasklet
1418 * finishes it incorrectly.
1419 */
1420 host->mrq = NULL;
1421
Chris Ball14efd952012-11-05 14:29:49 -05001422 spin_unlock_irqrestore(&host->lock, flags);
1423 sdhci_execute_tuning(mmc, tuning_opcode);
1424 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301425
Chris Ball14efd952012-11-05 14:29:49 -05001426 /* Restore original mmc_request structure */
1427 host->mrq = mrq;
1428 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301429 }
1430
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001431 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001432 sdhci_send_command(host, mrq->sbc);
1433 else
1434 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301435 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436
Pierre Ossman5f25a662006-10-04 02:15:39 -07001437 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438 spin_unlock_irqrestore(&host->lock, flags);
1439}
1440
Russell King2317f562014-04-25 12:57:07 +01001441void sdhci_set_bus_width(struct sdhci_host *host, int width)
1442{
1443 u8 ctrl;
1444
1445 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1446 if (width == MMC_BUS_WIDTH_8) {
1447 ctrl &= ~SDHCI_CTRL_4BITBUS;
1448 if (host->version >= SDHCI_SPEC_300)
1449 ctrl |= SDHCI_CTRL_8BITBUS;
1450 } else {
1451 if (host->version >= SDHCI_SPEC_300)
1452 ctrl &= ~SDHCI_CTRL_8BITBUS;
1453 if (width == MMC_BUS_WIDTH_4)
1454 ctrl |= SDHCI_CTRL_4BITBUS;
1455 else
1456 ctrl &= ~SDHCI_CTRL_4BITBUS;
1457 }
1458 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1459}
1460EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1461
Russell King96d7b782014-04-25 12:59:26 +01001462void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1463{
1464 u16 ctrl_2;
1465
1466 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1467 /* Select Bus Speed Mode for host */
1468 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1469 if ((timing == MMC_TIMING_MMC_HS200) ||
1470 (timing == MMC_TIMING_UHS_SDR104))
1471 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1472 else if (timing == MMC_TIMING_UHS_SDR12)
1473 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1474 else if (timing == MMC_TIMING_UHS_SDR25)
1475 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1476 else if (timing == MMC_TIMING_UHS_SDR50)
1477 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1478 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1479 (timing == MMC_TIMING_MMC_DDR52))
1480 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001481 else if (timing == MMC_TIMING_MMC_HS400)
1482 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001483 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1484}
1485EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1486
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001487static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001488{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489 unsigned long flags;
1490 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001491 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493 spin_lock_irqsave(&host->lock, flags);
1494
Adrian Hunterceb61432011-12-27 15:48:41 +02001495 if (host->flags & SDHCI_DEVICE_DEAD) {
1496 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001497 if (!IS_ERR(mmc->supply.vmmc) &&
1498 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001499 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001500 return;
1501 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001502
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503 /*
1504 * Reset the chip on each power off.
1505 * Should clear out any weird states.
1506 */
1507 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001508 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001509 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001510 }
1511
Kevin Liu52983382013-01-31 11:31:37 +08001512 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001513 (ios->power_mode == MMC_POWER_UP) &&
1514 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001515 sdhci_enable_preset_value(host, false);
1516
Russell King373073e2014-04-25 12:58:45 +01001517 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001518 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001519 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001520
1521 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1522 host->clock) {
1523 host->timeout_clk = host->mmc->actual_clock ?
1524 host->mmc->actual_clock / 1000 :
1525 host->clock / 1000;
1526 host->mmc->max_busy_timeout =
1527 host->ops->get_max_timeout_count ?
1528 host->ops->get_max_timeout_count(host) :
1529 1 << 27;
1530 host->mmc->max_busy_timeout /= host->timeout_clk;
1531 }
Russell King373073e2014-04-25 12:58:45 +01001532 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001533
Russell King24fbb3c2014-04-25 13:00:06 +01001534 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535
Philip Rakity643a81f2010-09-23 08:24:32 -07001536 if (host->ops->platform_send_init_74_clocks)
1537 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1538
Russell King2317f562014-04-25 12:57:07 +01001539 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001540
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001541 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001542
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001543 if ((ios->timing == MMC_TIMING_SD_HS ||
1544 ios->timing == MMC_TIMING_MMC_HS)
1545 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001546 ctrl |= SDHCI_CTRL_HISPD;
1547 else
1548 ctrl &= ~SDHCI_CTRL_HISPD;
1549
Arindam Nathd6d50a12011-05-05 12:18:59 +05301550 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301551 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301552
1553 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001554 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1555 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001556 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301557 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301558 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1559 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001560 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301561 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301562
Russell Kingda91a8f2014-04-25 13:00:12 +01001563 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301564 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301565 /*
1566 * We only need to set Driver Strength if the
1567 * preset value enable is not set.
1568 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001569 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301570 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1571 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1572 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1573 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1574 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1575
1576 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301577 } else {
1578 /*
1579 * According to SDHC Spec v3.00, if the Preset Value
1580 * Enable in the Host Control 2 register is set, we
1581 * need to reset SD Clock Enable before changing High
1582 * Speed Enable to avoid generating clock gliches.
1583 */
Arindam Nath758535c2011-05-05 12:19:00 +05301584
1585 /* Reset SD Clock Enable */
1586 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1587 clk &= ~SDHCI_CLOCK_CARD_EN;
1588 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1589
1590 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1591
1592 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001593 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301594 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301595
Arindam Nath49c468f2011-05-05 12:19:01 +05301596 /* Reset SD Clock Enable */
1597 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1598 clk &= ~SDHCI_CLOCK_CARD_EN;
1599 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1600
Russell King96d7b782014-04-25 12:59:26 +01001601 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001602 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301603
Kevin Liu52983382013-01-31 11:31:37 +08001604 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1605 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1606 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1607 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1608 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1609 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1610 u16 preset;
1611
1612 sdhci_enable_preset_value(host, true);
1613 preset = sdhci_get_preset_value(host);
1614 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1615 >> SDHCI_PRESET_DRV_SHIFT;
1616 }
1617
Arindam Nath49c468f2011-05-05 12:19:01 +05301618 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001619 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301620 } else
1621 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301622
Leandro Dorileob8352262007-07-25 23:47:04 +02001623 /*
1624 * Some (ENE) controllers go apeshit on some ios operation,
1625 * signalling timeout and CRC errors even on CMD0. Resetting
1626 * it on each ios seems to solve the problem.
1627 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001628 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001629 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001630
Pierre Ossman5f25a662006-10-04 02:15:39 -07001631 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632 spin_unlock_irqrestore(&host->lock, flags);
1633}
1634
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001635static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1636{
1637 struct sdhci_host *host = mmc_priv(mmc);
1638
1639 sdhci_runtime_pm_get(host);
1640 sdhci_do_set_ios(host, ios);
1641 sdhci_runtime_pm_put(host);
1642}
1643
Kevin Liu94144a42013-02-28 17:35:53 +08001644static int sdhci_do_get_cd(struct sdhci_host *host)
1645{
1646 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1647
1648 if (host->flags & SDHCI_DEVICE_DEAD)
1649 return 0;
1650
1651 /* If polling/nonremovable, assume that the card is always present. */
1652 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1653 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1654 return 1;
1655
1656 /* Try slot gpio detect */
1657 if (!IS_ERR_VALUE(gpio_cd))
1658 return !!gpio_cd;
1659
1660 /* Host native card detect */
1661 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1662}
1663
1664static int sdhci_get_cd(struct mmc_host *mmc)
1665{
1666 struct sdhci_host *host = mmc_priv(mmc);
1667 int ret;
1668
1669 sdhci_runtime_pm_get(host);
1670 ret = sdhci_do_get_cd(host);
1671 sdhci_runtime_pm_put(host);
1672 return ret;
1673}
1674
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001675static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001676{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001677 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001678 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679
Pierre Ossmand129bce2006-03-24 03:18:17 -08001680 spin_lock_irqsave(&host->lock, flags);
1681
Pierre Ossman1e728592008-04-16 19:13:13 +02001682 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001683 is_readonly = 0;
1684 else if (host->ops->get_ro)
1685 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001686 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001687 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1688 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001689
1690 spin_unlock_irqrestore(&host->lock, flags);
1691
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001692 /* This quirk needs to be replaced by a callback-function later */
1693 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1694 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001695}
1696
Takashi Iwai82b0e232011-04-21 20:26:38 +02001697#define SAMPLE_COUNT 5
1698
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001699static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001700{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001701 int i, ro_count;
1702
Takashi Iwai82b0e232011-04-21 20:26:38 +02001703 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001704 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001705
1706 ro_count = 0;
1707 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001709 if (++ro_count > SAMPLE_COUNT / 2)
1710 return 1;
1711 }
1712 msleep(30);
1713 }
1714 return 0;
1715}
1716
Adrian Hunter20758b62011-08-29 16:42:12 +03001717static void sdhci_hw_reset(struct mmc_host *mmc)
1718{
1719 struct sdhci_host *host = mmc_priv(mmc);
1720
1721 if (host->ops && host->ops->hw_reset)
1722 host->ops->hw_reset(host);
1723}
1724
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001726{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001727 struct sdhci_host *host = mmc_priv(mmc);
1728 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001729
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001730 sdhci_runtime_pm_get(host);
1731 ret = sdhci_do_get_ro(host);
1732 sdhci_runtime_pm_put(host);
1733 return ret;
1734}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001735
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001736static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1737{
Russell Kingbe138552014-04-25 12:55:56 +01001738 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001739 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001740 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001741 else
Russell Kingb537f942014-04-25 12:56:01 +01001742 host->ier &= ~SDHCI_INT_CARD_INT;
1743
1744 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1745 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001746 mmiowb();
1747 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001748}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001749
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001750static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1751{
1752 struct sdhci_host *host = mmc_priv(mmc);
1753 unsigned long flags;
1754
Russell Kingef104332014-04-25 12:55:41 +01001755 sdhci_runtime_pm_get(host);
1756
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001757 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001758 if (enable)
1759 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1760 else
1761 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1762
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001763 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001764 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001765
1766 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001767}
1768
Philip Rakity6231f3d2012-07-23 15:56:23 -07001769static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001770 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001771{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001772 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001773 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001774 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001775
1776 /*
1777 * Signal Voltage Switching is only applicable for Host Controllers
1778 * v3.00 and above.
1779 */
1780 if (host->version < SDHCI_SPEC_300)
1781 return 0;
1782
Philip Rakity6231f3d2012-07-23 15:56:23 -07001783 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001784
Fabio Estevam21f59982013-02-14 10:35:03 -02001785 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001786 case MMC_SIGNAL_VOLTAGE_330:
1787 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1788 ctrl &= ~SDHCI_CTRL_VDD_180;
1789 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1790
Tim Kryger3a48edc2014-06-13 10:13:56 -07001791 if (!IS_ERR(mmc->supply.vqmmc)) {
1792 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1793 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001794 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001795 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1796 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001797 return -EIO;
1798 }
1799 }
1800 /* Wait for 5ms */
1801 usleep_range(5000, 5500);
1802
1803 /* 3.3V regulator output should be stable within 5 ms */
1804 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1805 if (!(ctrl & SDHCI_CTRL_VDD_180))
1806 return 0;
1807
Joe Perches66061102014-09-12 14:56:56 -07001808 pr_warn("%s: 3.3V regulator output did not became stable\n",
1809 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001810
1811 return -EAGAIN;
1812 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001813 if (!IS_ERR(mmc->supply.vqmmc)) {
1814 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001815 1700000, 1950000);
1816 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001817 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1818 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001819 return -EIO;
1820 }
1821 }
1822
1823 /*
1824 * Enable 1.8V Signal Enable in the Host Control2
1825 * register
1826 */
1827 ctrl |= SDHCI_CTRL_VDD_180;
1828 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1829
Kevin Liu20b92a32012-12-17 19:29:26 +08001830 /* 1.8V regulator output should be stable within 5 ms */
1831 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1832 if (ctrl & SDHCI_CTRL_VDD_180)
1833 return 0;
1834
Joe Perches66061102014-09-12 14:56:56 -07001835 pr_warn("%s: 1.8V regulator output did not became stable\n",
1836 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001837
1838 return -EAGAIN;
1839 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001840 if (!IS_ERR(mmc->supply.vqmmc)) {
1841 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1842 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001843 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001844 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1845 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001846 return -EIO;
1847 }
1848 }
1849 return 0;
1850 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301851 /* No signal voltage switch required */
1852 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001853 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301854}
1855
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001856static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001857 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001858{
1859 struct sdhci_host *host = mmc_priv(mmc);
1860 int err;
1861
1862 if (host->version < SDHCI_SPEC_300)
1863 return 0;
1864 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001865 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001866 sdhci_runtime_pm_put(host);
1867 return err;
1868}
1869
Kevin Liu20b92a32012-12-17 19:29:26 +08001870static int sdhci_card_busy(struct mmc_host *mmc)
1871{
1872 struct sdhci_host *host = mmc_priv(mmc);
1873 u32 present_state;
1874
1875 sdhci_runtime_pm_get(host);
1876 /* Check whether DAT[3:0] is 0000 */
1877 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1878 sdhci_runtime_pm_put(host);
1879
1880 return !(present_state & SDHCI_DATA_LVL_MASK);
1881}
1882
Girish K S069c9f12012-01-06 09:56:39 +05301883static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301884{
Russell King4b6f37d2014-04-25 12:59:36 +01001885 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301886 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301887 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301888 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001889 unsigned long flags;
Arindam Nathb513ea22011-05-05 12:19:04 +05301890
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001891 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001892 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301893
Arindam Nathb513ea22011-05-05 12:19:04 +05301894 /*
Girish K S069c9f12012-01-06 09:56:39 +05301895 * The Host Controller needs tuning only in case of SDR104 mode
1896 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301897 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301898 * If the Host Controller supports the HS200 mode then the
1899 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301900 */
Russell King4b6f37d2014-04-25 12:59:36 +01001901 switch (host->timing) {
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001902 case MMC_TIMING_MMC_HS400:
Russell King4b6f37d2014-04-25 12:59:36 +01001903 case MMC_TIMING_MMC_HS200:
1904 case MMC_TIMING_UHS_SDR104:
1905 break;
Girish K S069c9f12012-01-06 09:56:39 +05301906
Russell King4b6f37d2014-04-25 12:59:36 +01001907 case MMC_TIMING_UHS_SDR50:
1908 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1909 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1910 break;
1911 /* FALLTHROUGH */
1912
1913 default:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001914 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001915 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301916 return 0;
1917 }
1918
Dong Aisheng45251812013-09-13 19:11:30 +08001919 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001920 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001921 err = host->ops->platform_execute_tuning(host, opcode);
1922 sdhci_runtime_pm_put(host);
1923 return err;
1924 }
1925
Russell King4b6f37d2014-04-25 12:59:36 +01001926 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1927 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Arindam Nathb513ea22011-05-05 12:19:04 +05301928 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1929
1930 /*
1931 * As per the Host Controller spec v3.00, tuning command
1932 * generates Buffer Read Ready interrupt, so enable that.
1933 *
1934 * Note: The spec clearly says that when tuning sequence
1935 * is being performed, the controller does not generate
1936 * interrupts other than Buffer Read Ready interrupt. But
1937 * to make sure we don't hit a controller bug, we _only_
1938 * enable Buffer Read Ready interrupt here.
1939 */
Russell Kingb537f942014-04-25 12:56:01 +01001940 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1941 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301942
1943 /*
1944 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1945 * of loops reaches 40 times or a timeout of 150ms occurs.
1946 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301947 do {
1948 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001949 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301950
Girish K S069c9f12012-01-06 09:56:39 +05301951 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301952 cmd.arg = 0;
1953 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1954 cmd.retries = 0;
1955 cmd.data = NULL;
1956 cmd.error = 0;
1957
Al Cooper7ce45e92014-05-09 11:34:07 -04001958 if (tuning_loop_counter-- == 0)
1959 break;
1960
Arindam Nathb513ea22011-05-05 12:19:04 +05301961 mrq.cmd = &cmd;
1962 host->mrq = &mrq;
1963
1964 /*
1965 * In response to CMD19, the card sends 64 bytes of tuning
1966 * block to the Host Controller. So we set the block size
1967 * to 64 here.
1968 */
Girish K S069c9f12012-01-06 09:56:39 +05301969 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1970 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1971 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1972 SDHCI_BLOCK_SIZE);
1973 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1974 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1975 SDHCI_BLOCK_SIZE);
1976 } else {
1977 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1978 SDHCI_BLOCK_SIZE);
1979 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301980
1981 /*
1982 * The tuning block is sent by the card to the host controller.
1983 * So we set the TRNS_READ bit in the Transfer Mode register.
1984 * This also takes care of setting DMA Enable and Multi Block
1985 * Select in the same register to 0.
1986 */
1987 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1988
1989 sdhci_send_command(host, &cmd);
1990
1991 host->cmd = NULL;
1992 host->mrq = NULL;
1993
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001994 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301995 /* Wait for Buffer Read Ready interrupt */
1996 wait_event_interruptible_timeout(host->buf_ready_int,
1997 (host->tuning_done == 1),
1998 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001999 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302000
2001 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302002 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302003 "Buffer Read Ready interrupt during tuning "
2004 "procedure, falling back to fixed sampling "
2005 "clock\n");
2006 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2007 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2008 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2009 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2010
2011 err = -EIO;
2012 goto out;
2013 }
2014
2015 host->tuning_done = 0;
2016
2017 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002018
2019 /* eMMC spec does not require a delay between tuning cycles */
2020 if (opcode == MMC_SEND_TUNING_BLOCK)
2021 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302022 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2023
2024 /*
2025 * The Host Driver has exhausted the maximum number of loops allowed,
2026 * so use fixed sampling frequency.
2027 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002028 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302029 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2030 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002031 }
2032 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
2033 pr_info(DRIVER_NAME ": Tuning procedure"
2034 " failed, falling back to fixed sampling"
2035 " clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002036 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302037 }
2038
2039out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302040 /*
2041 * If this is the very first time we are here, we start the retuning
2042 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2043 * flag won't be set, we check this condition before actually starting
2044 * the timer.
2045 */
2046 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2047 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08002048 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302049 mod_timer(&host->tuning_timer, jiffies +
2050 host->tuning_count * HZ);
2051 /* Tuning mode 1 limits the maximum data length to 4MB */
2052 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
Arend van Spriel2bc02482014-01-04 13:51:26 +01002053 } else if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302054 host->flags &= ~SDHCI_NEEDS_RETUNING;
2055 /* Reload the new initial value for timer */
Arend van Spriel2bc02482014-01-04 13:51:26 +01002056 mod_timer(&host->tuning_timer, jiffies +
2057 host->tuning_count * HZ);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302058 }
2059
2060 /*
2061 * In case tuning fails, host controllers which support re-tuning can
2062 * try tuning again at a later time, when the re-tuning timer expires.
2063 * So for these controllers, we return 0. Since there might be other
2064 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002065 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2066 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302067 */
Aaron Lu973905f2012-07-04 13:29:09 +08002068 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302069 err = 0;
2070
Russell Kingb537f942014-04-25 12:56:01 +01002071 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2072 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002073 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002074 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302075
2076 return err;
2077}
2078
Kevin Liu52983382013-01-31 11:31:37 +08002079
2080static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302081{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302082 /* Host Controller v3.00 defines preset value registers */
2083 if (host->version < SDHCI_SPEC_300)
2084 return;
2085
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302086 /*
2087 * We only enable or disable Preset Value if they are not already
2088 * enabled or disabled respectively. Otherwise, we bail out.
2089 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002090 if (host->preset_enabled != enable) {
2091 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2092
2093 if (enable)
2094 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2095 else
2096 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2097
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302098 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002099
2100 if (enable)
2101 host->flags |= SDHCI_PV_ENABLED;
2102 else
2103 host->flags &= ~SDHCI_PV_ENABLED;
2104
2105 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302106 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002107}
2108
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002109static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002110{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002111 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002112 unsigned long flags;
2113
Christian Daudt722e1282013-06-20 14:26:36 -07002114 /* First check if client has provided their own card event */
2115 if (host->ops->card_event)
2116 host->ops->card_event(host);
2117
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118 spin_lock_irqsave(&host->lock, flags);
2119
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002120 /* Check host->mrq first in case we are runtime suspended */
Shawn Guo9668d762013-06-09 19:49:24 +08002121 if (host->mrq && !sdhci_do_get_cd(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302122 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002123 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302124 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002125 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002126
Russell King03231f92014-04-25 12:57:12 +01002127 sdhci_do_reset(host, SDHCI_RESET_CMD);
2128 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002129
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002130 host->mrq->cmd->error = -ENOMEDIUM;
2131 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002132 }
2133
2134 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002135}
2136
2137static const struct mmc_host_ops sdhci_ops = {
2138 .request = sdhci_request,
2139 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002140 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002141 .get_ro = sdhci_get_ro,
2142 .hw_reset = sdhci_hw_reset,
2143 .enable_sdio_irq = sdhci_enable_sdio_irq,
2144 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2145 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002146 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002147 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002148};
2149
2150/*****************************************************************************\
2151 * *
2152 * Tasklets *
2153 * *
2154\*****************************************************************************/
2155
Pierre Ossmand129bce2006-03-24 03:18:17 -08002156static void sdhci_tasklet_finish(unsigned long param)
2157{
2158 struct sdhci_host *host;
2159 unsigned long flags;
2160 struct mmc_request *mrq;
2161
2162 host = (struct sdhci_host*)param;
2163
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002164 spin_lock_irqsave(&host->lock, flags);
2165
Chris Ball0c9c99a2011-04-27 17:35:31 -04002166 /*
2167 * If this tasklet gets rescheduled while running, it will
2168 * be run again afterwards but without any active request.
2169 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002170 if (!host->mrq) {
2171 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002172 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002173 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002174
2175 del_timer(&host->timer);
2176
2177 mrq = host->mrq;
2178
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179 /*
2180 * The controller needs a reset of internal state machines
2181 * upon error conditions.
2182 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002183 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002184 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002185 (mrq->sbc && mrq->sbc->error) ||
2186 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2187 (mrq->data->stop && mrq->data->stop->error))) ||
2188 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002189
2190 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002191 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002192 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002193 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002194
2195 /* Spec says we should do both at the same time, but Ricoh
2196 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002197 sdhci_do_reset(host, SDHCI_RESET_CMD);
2198 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199 }
2200
2201 host->mrq = NULL;
2202 host->cmd = NULL;
2203 host->data = NULL;
2204
Pierre Ossmanf9134312008-12-21 17:01:48 +01002205#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002206 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002207#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208
Pierre Ossman5f25a662006-10-04 02:15:39 -07002209 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002210 spin_unlock_irqrestore(&host->lock, flags);
2211
2212 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002213 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002214}
2215
2216static void sdhci_timeout_timer(unsigned long data)
2217{
2218 struct sdhci_host *host;
2219 unsigned long flags;
2220
2221 host = (struct sdhci_host*)data;
2222
2223 spin_lock_irqsave(&host->lock, flags);
2224
2225 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302226 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002227 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002228 sdhci_dumpregs(host);
2229
2230 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002231 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232 sdhci_finish_data(host);
2233 } else {
2234 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002235 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002236 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002237 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238
2239 tasklet_schedule(&host->finish_tasklet);
2240 }
2241 }
2242
Pierre Ossman5f25a662006-10-04 02:15:39 -07002243 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244 spin_unlock_irqrestore(&host->lock, flags);
2245}
2246
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302247static void sdhci_tuning_timer(unsigned long data)
2248{
2249 struct sdhci_host *host;
2250 unsigned long flags;
2251
2252 host = (struct sdhci_host *)data;
2253
2254 spin_lock_irqsave(&host->lock, flags);
2255
2256 host->flags |= SDHCI_NEEDS_RETUNING;
2257
2258 spin_unlock_irqrestore(&host->lock, flags);
2259}
2260
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261/*****************************************************************************\
2262 * *
2263 * Interrupt handling *
2264 * *
2265\*****************************************************************************/
2266
Adrian Hunter61541392014-09-24 10:27:27 +03002267static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268{
2269 BUG_ON(intmask == 0);
2270
2271 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302272 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002273 "though no command operation was in progress.\n",
2274 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 sdhci_dumpregs(host);
2276 return;
2277 }
2278
Pierre Ossman43b58b32007-07-25 23:15:27 +02002279 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002280 host->cmd->error = -ETIMEDOUT;
2281 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2282 SDHCI_INT_INDEX))
2283 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284
Pierre Ossmane8095172008-07-25 01:09:08 +02002285 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002287 return;
2288 }
2289
2290 /*
2291 * The host can send and interrupt when the busy state has
2292 * ended, allowing us to wait without wasting CPU cycles.
2293 * Unfortunately this is overloaded on the "data complete"
2294 * interrupt, so we need to take some care when handling
2295 * it.
2296 *
2297 * Note: The 1.0 specification is a bit ambiguous about this
2298 * feature so there might be some problems with older
2299 * controllers.
2300 */
2301 if (host->cmd->flags & MMC_RSP_BUSY) {
2302 if (host->cmd->data)
2303 DBG("Cannot wait for busy signal when also "
2304 "doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002305 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2306 && !host->busy_handle) {
2307 /* Mark that command complete before busy is ended */
2308 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002309 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002310 }
Ben Dooksf9454052009-02-20 20:33:08 +03002311
2312 /* The controller does not support the end-of-busy IRQ,
2313 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002314 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2315 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2316 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002317 }
2318
2319 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002320 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321}
2322
George G. Davis0957c332010-02-18 12:32:12 -05002323#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002324static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002325{
2326 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002327 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002328
2329 sdhci_dumpregs(host);
2330
2331 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002332 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002333
Adrian Huntere57a5f62014-11-04 12:42:46 +02002334 if (host->flags & SDHCI_USE_64_BIT_DMA)
2335 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2336 name, desc, le32_to_cpu(dma_desc->addr_hi),
2337 le32_to_cpu(dma_desc->addr_lo),
2338 le16_to_cpu(dma_desc->len),
2339 le16_to_cpu(dma_desc->cmd));
2340 else
2341 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2342 name, desc, le32_to_cpu(dma_desc->addr_lo),
2343 le16_to_cpu(dma_desc->len),
2344 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002345
Adrian Hunter76fe3792014-11-04 12:42:42 +02002346 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002347
Adrian Hunter05452302014-11-04 12:42:45 +02002348 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002349 break;
2350 }
2351}
2352#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002353static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002354#endif
2355
Pierre Ossmand129bce2006-03-24 03:18:17 -08002356static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2357{
Girish K S069c9f12012-01-06 09:56:39 +05302358 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359 BUG_ON(intmask == 0);
2360
Arindam Nathb513ea22011-05-05 12:19:04 +05302361 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2362 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302363 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2364 if (command == MMC_SEND_TUNING_BLOCK ||
2365 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302366 host->tuning_done = 1;
2367 wake_up(&host->buf_ready_int);
2368 return;
2369 }
2370 }
2371
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 if (!host->data) {
2373 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002374 * The "data complete" interrupt is also used to
2375 * indicate that a busy state has ended. See comment
2376 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002377 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002378 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002379 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2380 host->cmd->error = -ETIMEDOUT;
2381 tasklet_schedule(&host->finish_tasklet);
2382 return;
2383 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002384 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002385 /*
2386 * Some cards handle busy-end interrupt
2387 * before the command completed, so make
2388 * sure we do things in the proper order.
2389 */
2390 if (host->busy_handle)
2391 sdhci_finish_command(host);
2392 else
2393 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002394 return;
2395 }
2396 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002397
Girish K Sa3c76eb2011-10-11 11:44:09 +05302398 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002399 "though no data operation was in progress.\n",
2400 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401 sdhci_dumpregs(host);
2402
2403 return;
2404 }
2405
2406 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002407 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002408 else if (intmask & SDHCI_INT_DATA_END_BIT)
2409 host->data->error = -EILSEQ;
2410 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2411 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2412 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002413 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002414 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302415 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002416 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002417 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002418 if (host->ops->adma_workaround)
2419 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002420 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002421
Pierre Ossman17b04292007-07-22 22:18:46 +02002422 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423 sdhci_finish_data(host);
2424 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002425 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426 sdhci_transfer_pio(host);
2427
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002428 /*
2429 * We currently don't do anything fancy with DMA
2430 * boundaries, but as we can't disable the feature
2431 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002432 *
2433 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2434 * should return a valid address to continue from, but as
2435 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002436 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002437 if (intmask & SDHCI_INT_DMA_END) {
2438 u32 dmastart, dmanow;
2439 dmastart = sg_dma_address(host->data->sg);
2440 dmanow = dmastart + host->data->bytes_xfered;
2441 /*
2442 * Force update to the next DMA block boundary.
2443 */
2444 dmanow = (dmanow &
2445 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2446 SDHCI_DEFAULT_BOUNDARY_SIZE;
2447 host->data->bytes_xfered = dmanow - dmastart;
2448 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2449 " next 0x%08x\n",
2450 mmc_hostname(host->mmc), dmastart,
2451 host->data->bytes_xfered, dmanow);
2452 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2453 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002454
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002455 if (intmask & SDHCI_INT_DATA_END) {
2456 if (host->cmd) {
2457 /*
2458 * Data managed to finish before the
2459 * command completed. Make sure we do
2460 * things in the proper order.
2461 */
2462 host->data_early = 1;
2463 } else {
2464 sdhci_finish_data(host);
2465 }
2466 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467 }
2468}
2469
David Howells7d12e782006-10-05 14:55:46 +01002470static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002471{
Russell King781e9892014-04-25 12:55:46 +01002472 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002473 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002474 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002475 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002476
2477 spin_lock(&host->lock);
2478
Russell Kingbe138552014-04-25 12:55:56 +01002479 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002480 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002481 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002482 }
2483
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002484 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002485 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002486 result = IRQ_NONE;
2487 goto out;
2488 }
2489
Russell King41005002014-04-25 12:55:36 +01002490 do {
2491 /* Clear selected interrupts. */
2492 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2493 SDHCI_INT_BUS_POWER);
2494 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002495
Russell King41005002014-04-25 12:55:36 +01002496 DBG("*** %s got interrupt: 0x%08x\n",
2497 mmc_hostname(host->mmc), intmask);
2498
2499 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2500 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2501 SDHCI_CARD_PRESENT;
2502
2503 /*
2504 * There is a observation on i.mx esdhc. INSERT
2505 * bit will be immediately set again when it gets
2506 * cleared, if a card is inserted. We have to mask
2507 * the irq to prevent interrupt storm which will
2508 * freeze the system. And the REMOVE gets the
2509 * same situation.
2510 *
2511 * More testing are needed here to ensure it works
2512 * for other platforms though.
2513 */
Russell Kingb537f942014-04-25 12:56:01 +01002514 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2515 SDHCI_INT_CARD_REMOVE);
2516 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2517 SDHCI_INT_CARD_INSERT;
2518 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2519 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002520
2521 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2522 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002523
2524 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2525 SDHCI_INT_CARD_REMOVE);
2526 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002527 }
2528
2529 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002530 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2531 &intmask);
Russell King41005002014-04-25 12:55:36 +01002532
2533 if (intmask & SDHCI_INT_DATA_MASK)
2534 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2535
2536 if (intmask & SDHCI_INT_BUS_POWER)
2537 pr_err("%s: Card is consuming too much power!\n",
2538 mmc_hostname(host->mmc));
2539
Russell King781e9892014-04-25 12:55:46 +01002540 if (intmask & SDHCI_INT_CARD_INT) {
2541 sdhci_enable_sdio_irq_nolock(host, false);
2542 host->thread_isr |= SDHCI_INT_CARD_INT;
2543 result = IRQ_WAKE_THREAD;
2544 }
Russell King41005002014-04-25 12:55:36 +01002545
2546 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2547 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2548 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2549 SDHCI_INT_CARD_INT);
2550
2551 if (intmask) {
2552 unexpected |= intmask;
2553 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2554 }
2555
Russell King781e9892014-04-25 12:55:46 +01002556 if (result == IRQ_NONE)
2557 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002558
2559 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002560 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561out:
2562 spin_unlock(&host->lock);
2563
Alexander Stein6379b232012-03-14 09:52:10 +01002564 if (unexpected) {
2565 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2566 mmc_hostname(host->mmc), unexpected);
2567 sdhci_dumpregs(host);
2568 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002569
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570 return result;
2571}
2572
Russell King781e9892014-04-25 12:55:46 +01002573static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2574{
2575 struct sdhci_host *host = dev_id;
2576 unsigned long flags;
2577 u32 isr;
2578
2579 spin_lock_irqsave(&host->lock, flags);
2580 isr = host->thread_isr;
2581 host->thread_isr = 0;
2582 spin_unlock_irqrestore(&host->lock, flags);
2583
Russell King3560db82014-04-25 12:55:51 +01002584 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2585 sdhci_card_event(host->mmc);
2586 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2587 }
2588
Russell King781e9892014-04-25 12:55:46 +01002589 if (isr & SDHCI_INT_CARD_INT) {
2590 sdio_run_irqs(host->mmc);
2591
2592 spin_lock_irqsave(&host->lock, flags);
2593 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2594 sdhci_enable_sdio_irq_nolock(host, true);
2595 spin_unlock_irqrestore(&host->lock, flags);
2596 }
2597
2598 return isr ? IRQ_HANDLED : IRQ_NONE;
2599}
2600
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601/*****************************************************************************\
2602 * *
2603 * Suspend/resume *
2604 * *
2605\*****************************************************************************/
2606
2607#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002608void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2609{
2610 u8 val;
2611 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2612 | SDHCI_WAKE_ON_INT;
2613
2614 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2615 val |= mask ;
2616 /* Avoid fake wake up */
2617 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2618 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2619 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2620}
2621EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2622
Fabio Estevam0b10f472014-08-30 14:53:13 -03002623static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002624{
2625 u8 val;
2626 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2627 | SDHCI_WAKE_ON_INT;
2628
2629 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2630 val &= ~mask;
2631 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2632}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002633
Manuel Lauss29495aa2011-11-03 11:09:45 +01002634int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002635{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002636 sdhci_disable_card_detection(host);
2637
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302638 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002639 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002640 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302641 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302642 }
2643
Kevin Liuad080d72013-01-05 17:21:33 +08002644 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002645 host->ier = 0;
2646 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2647 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002648 free_irq(host->irq, host);
2649 } else {
2650 sdhci_enable_irq_wakeups(host);
2651 enable_irq_wake(host->irq);
2652 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002653 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654}
2655
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002656EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002657
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002658int sdhci_resume_host(struct sdhci_host *host)
2659{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002660 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002661
Richard Röjforsa13abc72009-09-22 16:45:30 -07002662 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002663 if (host->ops->enable_dma)
2664 host->ops->enable_dma(host);
2665 }
2666
Kevin Liuad080d72013-01-05 17:21:33 +08002667 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell King781e9892014-04-25 12:55:46 +01002668 ret = request_threaded_irq(host->irq, sdhci_irq,
2669 sdhci_thread_irq, IRQF_SHARED,
2670 mmc_hostname(host->mmc), host);
Kevin Liuad080d72013-01-05 17:21:33 +08002671 if (ret)
2672 return ret;
2673 } else {
2674 sdhci_disable_irq_wakeups(host);
2675 disable_irq_wake(host->irq);
2676 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002677
Adrian Hunter6308d292012-02-07 14:48:54 +02002678 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2679 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2680 /* Card keeps power but host controller does not */
2681 sdhci_init(host, 0);
2682 host->pwr = 0;
2683 host->clock = 0;
2684 sdhci_do_set_ios(host, &host->mmc->ios);
2685 } else {
2686 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2687 mmiowb();
2688 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002689
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002690 sdhci_enable_card_detection(host);
2691
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302692 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002693 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302694 host->flags |= SDHCI_NEEDS_RETUNING;
2695
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002696 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002697}
2698
2699EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700
2701static int sdhci_runtime_pm_get(struct sdhci_host *host)
2702{
2703 return pm_runtime_get_sync(host->mmc->parent);
2704}
2705
2706static int sdhci_runtime_pm_put(struct sdhci_host *host)
2707{
2708 pm_runtime_mark_last_busy(host->mmc->parent);
2709 return pm_runtime_put_autosuspend(host->mmc->parent);
2710}
2711
Adrian Hunterf0710a52013-05-06 12:17:32 +03002712static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2713{
2714 if (host->runtime_suspended || host->bus_on)
2715 return;
2716 host->bus_on = true;
2717 pm_runtime_get_noresume(host->mmc->parent);
2718}
2719
2720static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2721{
2722 if (host->runtime_suspended || !host->bus_on)
2723 return;
2724 host->bus_on = false;
2725 pm_runtime_put_noidle(host->mmc->parent);
2726}
2727
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002728int sdhci_runtime_suspend_host(struct sdhci_host *host)
2729{
2730 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002731
2732 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002733 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002734 del_timer_sync(&host->tuning_timer);
2735 host->flags &= ~SDHCI_NEEDS_RETUNING;
2736 }
2737
2738 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002739 host->ier &= SDHCI_INT_CARD_INT;
2740 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2741 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002742 spin_unlock_irqrestore(&host->lock, flags);
2743
Russell King781e9892014-04-25 12:55:46 +01002744 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002745
2746 spin_lock_irqsave(&host->lock, flags);
2747 host->runtime_suspended = true;
2748 spin_unlock_irqrestore(&host->lock, flags);
2749
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002750 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002751}
2752EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2753
2754int sdhci_runtime_resume_host(struct sdhci_host *host)
2755{
2756 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002757 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002758
2759 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2760 if (host->ops->enable_dma)
2761 host->ops->enable_dma(host);
2762 }
2763
2764 sdhci_init(host, 0);
2765
2766 /* Force clock and power re-program */
2767 host->pwr = 0;
2768 host->clock = 0;
2769 sdhci_do_set_ios(host, &host->mmc->ios);
2770
2771 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002772 if ((host_flags & SDHCI_PV_ENABLED) &&
2773 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2774 spin_lock_irqsave(&host->lock, flags);
2775 sdhci_enable_preset_value(host, true);
2776 spin_unlock_irqrestore(&host->lock, flags);
2777 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002778
2779 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002780 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002781 host->flags |= SDHCI_NEEDS_RETUNING;
2782
2783 spin_lock_irqsave(&host->lock, flags);
2784
2785 host->runtime_suspended = false;
2786
2787 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002788 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002789 sdhci_enable_sdio_irq_nolock(host, true);
2790
2791 /* Enable Card Detection */
2792 sdhci_enable_card_detection(host);
2793
2794 spin_unlock_irqrestore(&host->lock, flags);
2795
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002796 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002797}
2798EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2799
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002800#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002801
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802/*****************************************************************************\
2803 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002804 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002805 * *
2806\*****************************************************************************/
2807
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002808struct sdhci_host *sdhci_alloc_host(struct device *dev,
2809 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002810{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811 struct mmc_host *mmc;
2812 struct sdhci_host *host;
2813
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002814 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002816 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002817 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002818 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002819
2820 host = mmc_priv(mmc);
2821 host->mmc = mmc;
2822
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002823 return host;
2824}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002825
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002826EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002827
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828int sdhci_add_host(struct sdhci_host *host)
2829{
2830 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002831 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302832 u32 max_current_caps;
2833 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002834 unsigned int override_timeout_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002835 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002836
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002837 WARN_ON(host == NULL);
2838 if (host == NULL)
2839 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002840
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002843 if (debug_quirks)
2844 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002845 if (debug_quirks2)
2846 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002848 override_timeout_clk = host->timeout_clk;
2849
Russell King03231f92014-04-25 12:57:12 +01002850 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002851
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002852 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002853 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2854 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002855 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302856 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002857 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002858 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002859 }
2860
Arindam Nathf2119df2011-05-05 12:18:57 +05302861 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002862 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002863
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002864 if (host->version >= SDHCI_SPEC_300)
2865 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2866 host->caps1 :
2867 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302868
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002869 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002870 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002872 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002873 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002874 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002875
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002876 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002877 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002878 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002879 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002880 }
2881
Arindam Nathf2119df2011-05-05 12:18:57 +05302882 if ((host->version >= SDHCI_SPEC_200) &&
2883 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002884 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002885
2886 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2887 (host->flags & SDHCI_USE_ADMA)) {
2888 DBG("Disabling ADMA as it is marked broken\n");
2889 host->flags &= ~SDHCI_USE_ADMA;
2890 }
2891
Adrian Huntere57a5f62014-11-04 12:42:46 +02002892 /*
2893 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2894 * and *must* do 64-bit DMA. A driver has the opportunity to change
2895 * that during the first call to ->enable_dma(). Similarly
2896 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2897 * implement.
2898 */
2899 if (sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT)
2900 host->flags |= SDHCI_USE_64_BIT_DMA;
2901
Richard Röjforsa13abc72009-09-22 16:45:30 -07002902 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002903 if (host->ops->enable_dma) {
2904 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002905 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002906 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002907 host->flags &=
2908 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002909 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002910 }
2911 }
2912
Adrian Huntere57a5f62014-11-04 12:42:46 +02002913 /* SDMA does not support 64-bit DMA */
2914 if (host->flags & SDHCI_USE_64_BIT_DMA)
2915 host->flags &= ~SDHCI_USE_SDMA;
2916
Pierre Ossman2134a922008-06-28 18:28:51 +02002917 if (host->flags & SDHCI_USE_ADMA) {
2918 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002919 * The DMA descriptor table size is calculated as the maximum
2920 * number of segments times 2, to allow for an alignment
2921 * descriptor for each segment, plus 1 for a nop end descriptor,
2922 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002923 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002924 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2925 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2926 SDHCI_ADMA2_64_DESC_SZ;
2927 host->align_buffer_sz = SDHCI_MAX_SEGS *
2928 SDHCI_ADMA2_64_ALIGN;
2929 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
2930 host->align_sz = SDHCI_ADMA2_64_ALIGN;
2931 host->align_mask = SDHCI_ADMA2_64_ALIGN - 1;
2932 } else {
2933 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2934 SDHCI_ADMA2_32_DESC_SZ;
2935 host->align_buffer_sz = SDHCI_MAX_SEGS *
2936 SDHCI_ADMA2_32_ALIGN;
2937 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
2938 host->align_sz = SDHCI_ADMA2_32_ALIGN;
2939 host->align_mask = SDHCI_ADMA2_32_ALIGN - 1;
2940 }
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002941 host->adma_table = dma_alloc_coherent(mmc_dev(mmc),
Adrian Hunter76fe3792014-11-04 12:42:42 +02002942 host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002943 &host->adma_addr,
2944 GFP_KERNEL);
Adrian Hunter76fe3792014-11-04 12:42:42 +02002945 host->align_buffer = kmalloc(host->align_buffer_sz, GFP_KERNEL);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002946 if (!host->adma_table || !host->align_buffer) {
Adrian Hunter76fe3792014-11-04 12:42:42 +02002947 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002948 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02002949 kfree(host->align_buffer);
Joe Perches66061102014-09-12 14:56:56 -07002950 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002951 mmc_hostname(mmc));
2952 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002953 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002954 host->align_buffer = NULL;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002955 } else if (host->adma_addr & host->align_mask) {
Joe Perches66061102014-09-12 14:56:56 -07002956 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2957 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002958 host->flags &= ~SDHCI_USE_ADMA;
Adrian Hunter76fe3792014-11-04 12:42:42 +02002959 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002960 host->adma_table, host->adma_addr);
Russell Kingd1e49f72014-04-25 12:58:34 +01002961 kfree(host->align_buffer);
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02002962 host->adma_table = NULL;
Russell Kingd1e49f72014-04-25 12:58:34 +01002963 host->align_buffer = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02002964 }
2965 }
2966
Pierre Ossman76591502008-07-21 00:32:11 +02002967 /*
2968 * If we use DMA, then it's up to the caller to set the DMA
2969 * mask, but PIO does not need the hw shim so we set a new
2970 * mask here in that case.
2971 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002972 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002973 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002974 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002975 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002976
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002977 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302978 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002979 >> SDHCI_CLOCK_BASE_SHIFT;
2980 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302981 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002982 >> SDHCI_CLOCK_BASE_SHIFT;
2983
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002985 if (host->max_clk == 0 || host->quirks &
2986 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002987 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302988 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002989 "frequency.\n", mmc_hostname(mmc));
2990 return -ENODEV;
2991 }
2992 host->max_clk = host->ops->get_max_clock(host);
2993 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002994
2995 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302996 * In case of Host Controller v3.00, find out whether clock
2997 * multiplier is supported.
2998 */
2999 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3000 SDHCI_CLOCK_MUL_SHIFT;
3001
3002 /*
3003 * In case the value in Clock Multiplier is 0, then programmable
3004 * clock mode is not supported, otherwise the actual clock
3005 * multiplier is one more than the value of Clock Multiplier
3006 * in the Capabilities Register.
3007 */
3008 if (host->clk_mul)
3009 host->clk_mul += 1;
3010
3011 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003012 * Set host parameters.
3013 */
3014 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303015 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003016 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003017 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303018 else if (host->version >= SDHCI_SPEC_300) {
3019 if (host->clk_mul) {
3020 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3021 mmc->f_max = host->max_clk * host->clk_mul;
3022 } else
3023 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3024 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003025 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003026
Aisheng Dong28aab052014-08-27 15:26:31 +08003027 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3028 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3029 SDHCI_TIMEOUT_CLK_SHIFT;
3030 if (host->timeout_clk == 0) {
3031 if (host->ops->get_timeout_clock) {
3032 host->timeout_clk =
3033 host->ops->get_timeout_clock(host);
3034 } else {
3035 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3036 mmc_hostname(mmc));
3037 return -ENODEV;
3038 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003039 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003040
Aisheng Dong28aab052014-08-27 15:26:31 +08003041 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3042 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003043
Aisheng Dong28aab052014-08-27 15:26:31 +08003044 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003045 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003046 mmc->max_busy_timeout /= host->timeout_clk;
3047 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003048
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003049 if (override_timeout_clk)
3050 host->timeout_clk = override_timeout_clk;
3051
Andrei Warkentine89d4562011-05-23 15:06:37 -05003052 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003053 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003054
3055 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3056 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003057
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003058 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003059 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003060 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003061 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003062 host->flags |= SDHCI_AUTO_CMD23;
3063 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3064 } else {
3065 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3066 }
3067
Philip Rakity15ec4462010-11-19 16:48:39 -05003068 /*
3069 * A controller may support 8-bit width, but the board itself
3070 * might not have the pins brought out. Boards that support
3071 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3072 * their platform code before calling sdhci_add_host(), and we
3073 * won't assume 8-bit width for hosts without that CAP.
3074 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003075 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003076 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003077
Jerry Huang63ef5d82012-10-25 13:47:19 +08003078 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3079 mmc->caps &= ~MMC_CAP_CMD23;
3080
Arindam Nathf2119df2011-05-05 12:18:57 +05303081 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003082 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003083
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003084 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Markus Mayer4e743f12014-07-03 13:27:42 -07003085 !(mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003086 mmc->caps |= MMC_CAP_NEEDS_POLL;
3087
Tim Kryger3a48edc2014-06-13 10:13:56 -07003088 /* If there are external regulators, get them */
3089 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3090 return -EPROBE_DEFER;
3091
Philip Rakity6231f3d2012-07-23 15:56:23 -07003092 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003093 if (!IS_ERR(mmc->supply.vqmmc)) {
3094 ret = regulator_enable(mmc->supply.vqmmc);
3095 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3096 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003097 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3098 SDHCI_SUPPORT_SDR50 |
3099 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003100 if (ret) {
3101 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3102 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003103 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003104 }
Kevin Liu8363c372012-11-17 17:55:51 -05003105 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003106
Daniel Drake6a661802012-11-25 13:01:19 -05003107 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3108 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3109 SDHCI_SUPPORT_DDR50);
3110
Al Cooper4188bba2012-03-16 15:54:17 -04003111 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3112 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3113 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303114 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3115
3116 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003117 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303118 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003119 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3120 * field can be promoted to support HS200.
3121 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003122 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003123 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003124 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303125 mmc->caps |= MMC_CAP_UHS_SDR50;
3126
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003127 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3128 (caps[1] & SDHCI_SUPPORT_HS400))
3129 mmc->caps2 |= MMC_CAP2_HS400;
3130
Adrian Hunter549c0b12014-11-06 15:19:05 +02003131 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3132 (IS_ERR(mmc->supply.vqmmc) ||
3133 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3134 1300000)))
3135 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3136
Micky Ching9107ebb2014-02-21 18:40:35 +08003137 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3138 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303139 mmc->caps |= MMC_CAP_UHS_DDR50;
3140
Girish K S069c9f12012-01-06 09:56:39 +05303141 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303142 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3143 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3144
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003145 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303146 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003147 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303148
Arindam Nathd6d50a12011-05-05 12:18:59 +05303149 /* Driver Type(s) (A, C, D) supported by the host */
3150 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3151 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3152 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3153 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3154 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3155 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3156
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303157 /* Initial value for re-tuning timer count */
3158 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3159 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3160
3161 /*
3162 * In case Re-tuning Timer is not disabled, the actual value of
3163 * re-tuning timer will be 2 ^ (n - 1).
3164 */
3165 if (host->tuning_count)
3166 host->tuning_count = 1 << (host->tuning_count - 1);
3167
3168 /* Re-tuning mode supported by the Host Controller */
3169 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3170 SDHCI_RETUNING_MODE_SHIFT;
3171
Takashi Iwai8f230f42010-12-08 10:04:30 +01003172 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003173
Arindam Nathf2119df2011-05-05 12:18:57 +05303174 /*
3175 * According to SD Host Controller spec v3.00, if the Host System
3176 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3177 * the value is meaningful only if Voltage Support in the Capabilities
3178 * register is set. The actual current value is 4 times the register
3179 * value.
3180 */
3181 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003182 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003183 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003184 if (curr > 0) {
3185
3186 /* convert to SDHCI_MAX_CURRENT format */
3187 curr = curr/1000; /* convert to mA */
3188 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3189
3190 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3191 max_current_caps =
3192 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3193 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3194 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3195 }
3196 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303197
3198 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003199 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303200
Aaron Lu55c46652012-07-04 13:31:48 +08003201 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303202 SDHCI_MAX_CURRENT_330_MASK) >>
3203 SDHCI_MAX_CURRENT_330_SHIFT) *
3204 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303205 }
3206 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003207 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303208
Aaron Lu55c46652012-07-04 13:31:48 +08003209 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303210 SDHCI_MAX_CURRENT_300_MASK) >>
3211 SDHCI_MAX_CURRENT_300_SHIFT) *
3212 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303213 }
3214 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003215 ocr_avail |= MMC_VDD_165_195;
3216
Aaron Lu55c46652012-07-04 13:31:48 +08003217 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303218 SDHCI_MAX_CURRENT_180_MASK) >>
3219 SDHCI_MAX_CURRENT_180_SHIFT) *
3220 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 }
3222
Tim Kryger52221612014-06-25 00:25:34 -07003223 /* If OCR set by external regulators, use it instead */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003224 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003225 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003226
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003227 if (host->ocr_mask)
Tim Kryger3a48edc2014-06-13 10:13:56 -07003228 ocr_avail &= host->ocr_mask;
Haijun Zhangc0b887b2013-08-26 09:19:23 +08003229
Takashi Iwai8f230f42010-12-08 10:04:30 +01003230 mmc->ocr_avail = ocr_avail;
3231 mmc->ocr_avail_sdio = ocr_avail;
3232 if (host->ocr_avail_sdio)
3233 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3234 mmc->ocr_avail_sd = ocr_avail;
3235 if (host->ocr_avail_sd)
3236 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3237 else /* normal SD controllers don't support 1.8V */
3238 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3239 mmc->ocr_avail_mmc = ocr_avail;
3240 if (host->ocr_avail_mmc)
3241 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003242
3243 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303244 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003245 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003246 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003247 }
3248
Pierre Ossmand129bce2006-03-24 03:18:17 -08003249 spin_lock_init(&host->lock);
3250
3251 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003252 * Maximum number of segments. Depends on if the hardware
3253 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003254 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003255 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003256 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003257 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003258 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003259 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003260 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003261
3262 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003263 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003264 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003266 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003267
3268 /*
3269 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003270 * of bytes. When doing hardware scatter/gather, each entry cannot
3271 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003272 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003273 if (host->flags & SDHCI_USE_ADMA) {
3274 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3275 mmc->max_seg_size = 65535;
3276 else
3277 mmc->max_seg_size = 65536;
3278 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003279 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003280 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003281
3282 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003283 * Maximum block size. This varies from controller to controller and
3284 * is specified in the capabilities register.
3285 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003286 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3287 mmc->max_blk_size = 2;
3288 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303289 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003290 SDHCI_MAX_BLOCK_SHIFT;
3291 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003292 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3293 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003294 mmc->max_blk_size = 0;
3295 }
3296 }
3297
3298 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003299
3300 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003301 * Maximum block count.
3302 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003303 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003304
3305 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306 * Init tasklets.
3307 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003308 tasklet_init(&host->finish_tasklet,
3309 sdhci_tasklet_finish, (unsigned long)host);
3310
Al Viroe4cad1b2006-10-10 22:47:07 +01003311 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303313 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303314 init_waitqueue_head(&host->buf_ready_int);
3315
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303316 /* Initialize re-tuning timer */
3317 init_timer(&host->tuning_timer);
3318 host->tuning_timer.data = (unsigned long)host;
3319 host->tuning_timer.function = sdhci_tuning_timer;
3320 }
3321
Shawn Guo2af502c2013-07-05 14:38:55 +08003322 sdhci_init(host, 0);
3323
Russell King781e9892014-04-25 12:55:46 +01003324 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3325 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003326 if (ret) {
3327 pr_err("%s: Failed to request IRQ %d: %d\n",
3328 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003329 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003330 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003331
Pierre Ossmand129bce2006-03-24 03:18:17 -08003332#ifdef CONFIG_MMC_DEBUG
3333 sdhci_dumpregs(host);
3334#endif
3335
Pierre Ossmanf9134312008-12-21 17:01:48 +01003336#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003337 snprintf(host->led_name, sizeof(host->led_name),
3338 "%s::", mmc_hostname(mmc));
3339 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003340 host->led.brightness = LED_OFF;
3341 host->led.default_trigger = mmc_hostname(mmc);
3342 host->led.brightness_set = sdhci_led_control;
3343
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003344 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003345 if (ret) {
3346 pr_err("%s: Failed to register LED device: %d\n",
3347 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003348 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003349 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003350#endif
3351
Pierre Ossman5f25a662006-10-04 02:15:39 -07003352 mmiowb();
3353
Pierre Ossmand129bce2006-03-24 03:18:17 -08003354 mmc_add_host(mmc);
3355
Girish K Sa3c76eb2011-10-11 11:44:09 +05303356 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003357 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003358 (host->flags & SDHCI_USE_ADMA) ?
3359 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003360 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003361
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003362 sdhci_enable_card_detection(host);
3363
Pierre Ossmand129bce2006-03-24 03:18:17 -08003364 return 0;
3365
Pierre Ossmanf9134312008-12-21 17:01:48 +01003366#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003367reset:
Russell King03231f92014-04-25 12:57:12 +01003368 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003369 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3370 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003371 free_irq(host->irq, host);
3372#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003373untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003375
3376 return ret;
3377}
3378
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003379EXPORT_SYMBOL_GPL(sdhci_add_host);
3380
Pierre Ossman1e728592008-04-16 19:13:13 +02003381void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003382{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003383 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003384 unsigned long flags;
3385
3386 if (dead) {
3387 spin_lock_irqsave(&host->lock, flags);
3388
3389 host->flags |= SDHCI_DEVICE_DEAD;
3390
3391 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303392 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003393 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003394
3395 host->mrq->cmd->error = -ENOMEDIUM;
3396 tasklet_schedule(&host->finish_tasklet);
3397 }
3398
3399 spin_unlock_irqrestore(&host->lock, flags);
3400 }
3401
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003402 sdhci_disable_card_detection(host);
3403
Markus Mayer4e743f12014-07-03 13:27:42 -07003404 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003405
Pierre Ossmanf9134312008-12-21 17:01:48 +01003406#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003407 led_classdev_unregister(&host->led);
3408#endif
3409
Pierre Ossman1e728592008-04-16 19:13:13 +02003410 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003411 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003412
Russell Kingb537f942014-04-25 12:56:01 +01003413 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3414 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415 free_irq(host->irq, host);
3416
3417 del_timer_sync(&host->timer);
3418
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003420
Tim Kryger3a48edc2014-06-13 10:13:56 -07003421 if (!IS_ERR(mmc->supply.vqmmc))
3422 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003423
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003424 if (host->adma_table)
Adrian Hunter76fe3792014-11-04 12:42:42 +02003425 dma_free_coherent(mmc_dev(mmc), host->adma_table_sz,
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003426 host->adma_table, host->adma_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003427 kfree(host->align_buffer);
3428
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003429 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003430 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431}
3432
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003433EXPORT_SYMBOL_GPL(sdhci_remove_host);
3434
3435void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003436{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003437 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003438}
3439
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003440EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003441
3442/*****************************************************************************\
3443 * *
3444 * Driver init/exit *
3445 * *
3446\*****************************************************************************/
3447
3448static int __init sdhci_drv_init(void)
3449{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303450 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003451 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303452 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003453
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003455}
3456
3457static void __exit sdhci_drv_exit(void)
3458{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003459}
3460
3461module_init(sdhci_drv_init);
3462module_exit(sdhci_drv_exit);
3463
Pierre Ossmandf673b22006-06-30 02:22:31 -07003464module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003465module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003466
Pierre Ossman32710e82009-04-08 20:14:54 +02003467MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003468MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003469MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003470
Pierre Ossmandf673b22006-06-30 02:22:31 -07003471MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003472MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");