blob: 32f52202a694a97c23e046c7661517d81d762028 [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 Hunter50accb92011-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>
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +030030#include <linux/mmc/card.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080031
Pierre Ossmand129bce2006-03-24 03:18:17 -080032#include "sdhci.h"
33
34#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmand129bce2006-03-24 03:18:17 -080036#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010037 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080038
Pierre Ossmanf9134312008-12-21 17:01:48 +010039#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
40 defined(CONFIG_MMC_SDHCI_MODULE))
41#define SDHCI_USE_LEDS_CLASS
42#endif
43
Arindam Nathb513ea22011-05-05 12:19:04 +053044#define MAX_TUNING_LOOP 40
45
Pierre Ossmandf673b22006-06-30 02:22:31 -070046static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030047static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070048
Pierre Ossmand129bce2006-03-24 03:18:17 -080049static void sdhci_finish_data(struct sdhci_host *);
50
51static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
52static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-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);
Sahitya Tummala1b248c42013-05-24 14:08:10 +053055static bool sdhci_check_state(struct sdhci_host *);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunter50accb92011-10-03 15:33:34 +030057#ifdef CONFIG_PM_RUNTIME
58static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
60#else
61static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
62{
63 return 0;
64}
65static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
66{
67 return 0;
68}
69#endif
70
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053071static void sdhci_dump_state(struct sdhci_host *host)
72{
73 struct mmc_host *mmc = host->mmc;
74
75 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
76 mmc_hostname(mmc), host->clock, mmc->clk_gated,
77 mmc->claimer->comm, host->pwr);
78 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
79 mmc_hostname(mmc), mmc->parent->power.runtime_status,
80 atomic_read(&mmc->parent->power.usage_count),
81 mmc->parent->power.disable_depth);
82}
83
Pierre Ossmand129bce2006-03-24 03:18:17 -080084static void sdhci_dumpregs(struct sdhci_host *host)
85{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053086 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070087 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080088
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053089 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readl(host, SDHCI_DMA_ADDRESS),
91 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053092 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readw(host, SDHCI_BLOCK_SIZE),
94 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053095 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_ARGUMENT),
97 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053098 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readl(host, SDHCI_PRESENT_STATE),
100 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530101 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readb(host, SDHCI_POWER_CONTROL),
103 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530104 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
106 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530107 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
109 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530110 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300111 sdhci_readl(host, SDHCI_INT_ENABLE),
112 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530113 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Sahitya Tummala8f6c0002013-08-07 18:40:29 +0530114 host->auto_cmd_err_sts,
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300115 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530116 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300117 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500118 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530119 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500120 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300121 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das80c02552013-07-23 16:20:34 +0530122 pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n",
123 sdhci_readl(host, SDHCI_RESPONSE + 0x4),
124 sdhci_readl(host, SDHCI_RESPONSE));
125 pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n",
126 sdhci_readl(host, SDHCI_RESPONSE + 0xC),
127 sdhci_readl(host, SDHCI_RESPONSE + 0x8));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530128 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530129 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800130
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100131 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530132 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100133 readl(host->ioaddr + SDHCI_ADMA_ERROR),
134 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
135
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530136 sdhci_dump_state(host);
137 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800138}
139
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +0530140#define MAX_PM_QOS_TIMEOUT_VALUE 100000 /* 100 ms */
141static ssize_t
142show_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
143 char *buf)
144{
145 struct sdhci_host *host = dev_get_drvdata(dev);
146
147 return snprintf(buf, PAGE_SIZE, "%d us\n", host->pm_qos_timeout_us);
148}
149
150static ssize_t
151store_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
152 const char *buf, size_t count)
153{
154 struct sdhci_host *host = dev_get_drvdata(dev);
155 uint32_t value;
156 unsigned long flags;
157
158 if (!kstrtou32(buf, 0, &value)) {
159 spin_lock_irqsave(&host->lock, flags);
160 if (value <= MAX_PM_QOS_TIMEOUT_VALUE)
161 host->pm_qos_timeout_us = value;
162 spin_unlock_irqrestore(&host->lock, flags);
163 }
164 return count;
165}
166
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167/*****************************************************************************\
168 * *
169 * Low level functions *
170 * *
171\*****************************************************************************/
172
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300173static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
174{
175 u32 ier;
176
177 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
178 ier &= ~clear;
179 ier |= set;
180 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
181 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
182}
183
184static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
185{
186 sdhci_clear_set_irqs(host, 0, irqs);
187}
188
189static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
190{
191 sdhci_clear_set_irqs(host, irqs, 0);
192}
193
194static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
195{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530196 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300197
Adrian Hunterc79396c2011-12-27 15:48:42 +0200198 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100199 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300200 return;
201
Sahitya Tummalaca422112013-02-22 12:15:54 +0530202 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
203 SDHCI_CARD_PRESENT;
204 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300205
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300206 if (enable)
207 sdhci_unmask_irqs(host, irqs);
208 else
209 sdhci_mask_irqs(host, irqs);
210}
211
212static void sdhci_enable_card_detection(struct sdhci_host *host)
213{
214 sdhci_set_card_detection(host, true);
215}
216
217static void sdhci_disable_card_detection(struct sdhci_host *host)
218{
219 sdhci_set_card_detection(host, false);
220}
221
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222static void sdhci_reset(struct sdhci_host *host, u8 mask)
223{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700224 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300225 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700226
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100227 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300228 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700229 SDHCI_CARD_PRESENT))
230 return;
231 }
232
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300233 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
234 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
235
Philip Rakity393c1a32011-01-21 11:26:40 -0800236 if (host->ops->platform_reset_enter)
237 host->ops->platform_reset_enter(host, mask);
238
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300239 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800240
Pierre Ossmane16514d2006-06-30 02:22:24 -0700241 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242 host->clock = 0;
243
Pierre Ossmane16514d2006-06-30 02:22:24 -0700244 /* Wait max 100 ms */
245 timeout = 100;
246
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530247 if (host->ops->check_power_status && host->pwr &&
248 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530249 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530250
Pierre Ossmane16514d2006-06-30 02:22:24 -0700251 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300252 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700253 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530254 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700255 mmc_hostname(host->mmc), (int)mask);
256 sdhci_dumpregs(host);
257 return;
258 }
259 timeout--;
260 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300262
Philip Rakity393c1a32011-01-21 11:26:40 -0800263 if (host->ops->platform_reset_exit)
264 host->ops->platform_reset_exit(host, mask);
265
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300266 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
267 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530268
269 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
270 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
271 host->ops->enable_dma(host);
272 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273}
274
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800275static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
276
277static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800279 if (soft)
280 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
281 else
282 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300284 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
285 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700286 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
287 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Asutosh Das80c02552013-07-23 16:20:34 +0530288 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
289 SDHCI_INT_AUTO_CMD_ERR);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800290
291 if (soft) {
292 /* force clock reconfiguration */
293 host->clock = 0;
294 sdhci_set_ios(host->mmc, &host->mmc->ios);
295 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300296}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300298static void sdhci_reinit(struct sdhci_host *host)
299{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800300 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300301 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800302}
303
304static void sdhci_activate_led(struct sdhci_host *host)
305{
306 u8 ctrl;
307
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300308 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300310 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311}
312
313static void sdhci_deactivate_led(struct sdhci_host *host)
314{
315 u8 ctrl;
316
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300317 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300319 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320}
321
Pierre Ossmanf9134312008-12-21 17:01:48 +0100322#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100323static void sdhci_led_control(struct led_classdev *led,
324 enum led_brightness brightness)
325{
326 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
327 unsigned long flags;
328
329 spin_lock_irqsave(&host->lock, flags);
330
Sahitya Tummala1b248c42013-05-24 14:08:10 +0530331 if (host->runtime_suspended || sdhci_check_state(host))
Adrian Hunter50accb92011-10-03 15:33:34 +0300332 goto out;
333
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100334 if (brightness == LED_OFF)
335 sdhci_deactivate_led(host);
336 else
337 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300338out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100339 spin_unlock_irqrestore(&host->lock, flags);
340}
341#endif
342
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343/*****************************************************************************\
344 * *
345 * Core functions *
346 * *
347\*****************************************************************************/
348
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350{
Pierre Ossman76591502008-07-21 00:32:11 +0200351 unsigned long flags;
352 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700353 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200354 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800355
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200359 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800362
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100363 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200364 if (!sg_miter_next(&host->sg_miter))
365 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 blksize -= len;
370 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 while (len) {
375 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300376 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200377 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800378 }
Pierre Ossman76591502008-07-21 00:32:11 +0200379
380 *buf = scratch & 0xFF;
381
382 buf++;
383 scratch >>= 8;
384 chunk--;
385 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800386 }
387 }
Pierre Ossman76591502008-07-21 00:32:11 +0200388
389 sg_miter_stop(&host->sg_miter);
390
391 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800393
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100394static void sdhci_write_block_pio(struct sdhci_host *host)
395{
Pierre Ossman76591502008-07-21 00:32:11 +0200396 unsigned long flags;
397 size_t blksize, len, chunk;
398 u32 scratch;
399 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400
401 DBG("PIO writing\n");
402
403 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200404 chunk = 0;
405 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100406
Pierre Ossman76591502008-07-21 00:32:11 +0200407 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408
409 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200410 if (!sg_miter_next(&host->sg_miter))
411 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100412
Pierre Ossman76591502008-07-21 00:32:11 +0200413 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200414
Pierre Ossman76591502008-07-21 00:32:11 +0200415 blksize -= len;
416 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417
Pierre Ossman76591502008-07-21 00:32:11 +0200418 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100419
Pierre Ossman76591502008-07-21 00:32:11 +0200420 while (len) {
421 scratch |= (u32)*buf << (chunk * 8);
422
423 buf++;
424 chunk++;
425 len--;
426
427 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300428 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200429 chunk = 0;
430 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432 }
433 }
Pierre Ossman76591502008-07-21 00:32:11 +0200434
435 sg_miter_stop(&host->sg_miter);
436
437 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438}
439
440static void sdhci_transfer_pio(struct sdhci_host *host)
441{
442 u32 mask;
443
444 BUG_ON(!host->data);
445
Pierre Ossman76591502008-07-21 00:32:11 +0200446 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100447 return;
448
449 if (host->data->flags & MMC_DATA_READ)
450 mask = SDHCI_DATA_AVAILABLE;
451 else
452 mask = SDHCI_SPACE_AVAILABLE;
453
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200454 /*
455 * Some controllers (JMicron JMB38x) mess up the buffer bits
456 * for transfers < 4 bytes. As long as it is just one block,
457 * we can ignore the bits.
458 */
459 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
460 (host->data->blocks == 1))
461 mask = ~0;
462
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300463 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300464 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
465 udelay(100);
466
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100467 if (host->data->flags & MMC_DATA_READ)
468 sdhci_read_block_pio(host);
469 else
470 sdhci_write_block_pio(host);
471
Pierre Ossman76591502008-07-21 00:32:11 +0200472 host->blocks--;
473 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100474 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100475 }
476
477 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800478}
479
Pierre Ossman2134a922008-06-28 18:28:51 +0200480static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
481{
482 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800483 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200484}
485
486static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
487{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800488 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200489 local_irq_restore(*flags);
490}
491
Ben Dooks118cd172010-03-05 13:43:26 -0800492static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
493{
Ben Dooks9e506f32010-03-05 13:43:29 -0800494 __le32 *dataddr = (__le32 __force *)(desc + 4);
495 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800496
Ben Dooks9e506f32010-03-05 13:43:29 -0800497 /* SDHCI specification says ADMA descriptors should be 4 byte
498 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800499
Ben Dooks9e506f32010-03-05 13:43:29 -0800500 cmdlen[0] = cpu_to_le16(cmd);
501 cmdlen[1] = cpu_to_le16(len);
502
503 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800504}
505
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800506static int sdhci_pre_dma_transfer(struct sdhci_host *host,
507 struct mmc_data *data,
508 struct sdhci_next *next)
509{
510 int sg_count;
511
512 if (!next && data->host_cookie &&
513 data->host_cookie != host->next_data.cookie) {
514 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
515 " host->next_data.cookie %d\n",
516 __func__, data->host_cookie, host->next_data.cookie);
517 data->host_cookie = 0;
518 }
519
520 /* Check if next job is already prepared */
521 if (next ||
522 (!next && data->host_cookie != host->next_data.cookie)) {
523 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
524 data->sg_len,
525 (data->flags & MMC_DATA_WRITE) ?
526 DMA_TO_DEVICE : DMA_FROM_DEVICE);
527 } else {
528 sg_count = host->next_data.sg_count;
529 host->next_data.sg_count = 0;
530 }
531
532 if (sg_count == 0)
533 return -EINVAL;
534
535 if (next) {
536 next->sg_count = sg_count;
537 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
538 } else
539 host->sg_count = sg_count;
540
541 return sg_count;
542}
543
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200544static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 struct mmc_data *data)
546{
547 int direction;
548
549 u8 *desc;
550 u8 *align;
551 dma_addr_t addr;
552 dma_addr_t align_addr;
553 int len, offset;
554
555 struct scatterlist *sg;
556 int i;
557 char *buffer;
558 unsigned long flags;
559
560 /*
561 * The spec does not specify endianness of descriptor table.
562 * We currently guess that it is LE.
563 */
564
565 if (data->flags & MMC_DATA_READ)
566 direction = DMA_FROM_DEVICE;
567 else
568 direction = DMA_TO_DEVICE;
569
570 /*
571 * The ADMA descriptor table is mapped further down as we
572 * need to fill it with data first.
573 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200574 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530575 host->align_buffer,
576 host->align_buf_sz,
577 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700578 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200579 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 BUG_ON(host->align_addr & 0x3);
581
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800582 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
583 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200584 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585
586 desc = host->adma_desc;
587 align = host->align_buffer;
588
589 align_addr = host->align_addr;
590
591 for_each_sg(data->sg, sg, host->sg_count, i) {
592 addr = sg_dma_address(sg);
593 len = sg_dma_len(sg);
594
595 /*
596 * The SDHCI specification states that ADMA
597 * addresses must be 32-bit aligned. If they
598 * aren't, then we use a bounce buffer for
599 * the (up to three) bytes that screw up the
600 * alignment.
601 */
602 offset = (4 - (addr & 0x3)) & 0x3;
603 if (offset) {
604 if (data->flags & MMC_DATA_WRITE) {
605 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200606 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200607 memcpy(align, buffer, offset);
608 sdhci_kunmap_atomic(buffer, &flags);
609 }
610
Ben Dooks118cd172010-03-05 13:43:26 -0800611 /* tran, valid */
612 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200613
614 BUG_ON(offset > 65536);
615
Pierre Ossman2134a922008-06-28 18:28:51 +0200616 align += 4;
617 align_addr += 4;
618
619 desc += 8;
620
621 addr += offset;
622 len -= offset;
623 }
624
Pierre Ossman2134a922008-06-28 18:28:51 +0200625 BUG_ON(len > 65536);
626
Ben Dooks118cd172010-03-05 13:43:26 -0800627 /* tran, valid */
628 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200629 desc += 8;
630
631 /*
632 * If this triggers then we have a calculation bug
633 * somewhere. :/
634 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530635 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
636
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 }
638
Thomas Abraham70764a92010-05-26 14:42:04 -0700639 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
640 /*
641 * Mark the last descriptor as the terminating descriptor
642 */
643 if (desc != host->adma_desc) {
644 desc -= 8;
645 desc[0] |= 0x2; /* end */
646 }
647 } else {
648 /*
649 * Add a terminating entry.
650 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200651
Thomas Abraham70764a92010-05-26 14:42:04 -0700652 /* nop, end, valid */
653 sdhci_set_adma_desc(desc, 0, 0, 0x3);
654 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200655
656 /*
657 * Resync align buffer as we might have changed it.
658 */
659 if (data->flags & MMC_DATA_WRITE) {
660 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530661 host->align_addr,
662 host->align_buf_sz,
663 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200664 }
665
666 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530667 host->adma_desc,
668 host->adma_desc_sz,
669 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200670 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200671 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200672 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200673
674 return 0;
675
676unmap_entries:
677 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
678 data->sg_len, direction);
679unmap_align:
680 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530681 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200682fail:
683 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200684}
685
686static void sdhci_adma_table_post(struct sdhci_host *host,
687 struct mmc_data *data)
688{
689 int direction;
690
691 struct scatterlist *sg;
692 int i, size;
693 u8 *align;
694 char *buffer;
695 unsigned long flags;
696
697 if (data->flags & MMC_DATA_READ)
698 direction = DMA_FROM_DEVICE;
699 else
700 direction = DMA_TO_DEVICE;
701
702 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530703 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200704
705 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530706 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200707
708 if (data->flags & MMC_DATA_READ) {
709 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
710 data->sg_len, direction);
711
712 align = host->align_buffer;
713
714 for_each_sg(data->sg, sg, host->sg_count, i) {
715 if (sg_dma_address(sg) & 0x3) {
716 size = 4 - (sg_dma_address(sg) & 0x3);
717
718 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200719 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200720 memcpy(buffer, align, size);
721 sdhci_kunmap_atomic(buffer, &flags);
722
723 align += 4;
724 }
725 }
726 }
727
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800728 if (!data->host_cookie)
729 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
730 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200731}
732
Andrei Warkentina3c77782011-04-11 16:13:42 -0500733static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800734{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700735 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500736 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700737 unsigned target_timeout, current_timeout;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530738 u32 curr_clk = 0; /* In KHz */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800739
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200740 /*
741 * If the host controller provides us with an incorrect timeout
742 * value, just skip the check and use 0xE. The hardware may take
743 * longer to time out, but that's much better than having a too-short
744 * timeout value.
745 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200746 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200747 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200748
Andrei Warkentina3c77782011-04-11 16:13:42 -0500749 /* Unspecified timeout, assume max */
750 if (!data && !cmd->cmd_timeout_ms)
751 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800752
Andrei Warkentina3c77782011-04-11 16:13:42 -0500753 /* timeout in us */
754 if (!data)
755 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530756 else {
757 target_timeout = data->timeout_ns / 1000;
758 if (host->clock)
759 target_timeout += data->timeout_clks / host->clock;
760 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700761
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700762 /*
763 * Figure out needed cycles.
764 * We do this in steps in order to fit inside a 32 bit int.
765 * The first step is the minimum timeout, which will have a
766 * minimum resolution of 6 bits:
767 * (1) 2^13*1000 > 2^22,
768 * (2) host->timeout_clk < 2^16
769 * =>
770 * (1) / (2) > 2^6
771 */
772 count = 0;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530773 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) {
774 curr_clk = host->clock / 1000;
775 if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4)
776 curr_clk /= 4;
777 current_timeout = (1 << 13) * 1000 / curr_clk;
778 } else {
779 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
780 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700781 while (current_timeout < target_timeout) {
782 count++;
783 current_timeout <<= 1;
784 if (count >= 0xF)
785 break;
786 }
787
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530788 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
789 if (count >= 0xF) {
790 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
791 mmc_hostname(host->mmc), count, cmd->opcode);
792 count = 0xE;
793 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530794 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700795
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200796 return count;
797}
798
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300799static void sdhci_set_transfer_irqs(struct sdhci_host *host)
800{
801 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
802 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
803
804 if (host->flags & SDHCI_REQ_USE_DMA)
805 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
806 else
807 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
808}
809
Andrei Warkentina3c77782011-04-11 16:13:42 -0500810static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200811{
812 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200813 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500814 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200815 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200816
817 WARN_ON(host->data);
818
Andrei Warkentina3c77782011-04-11 16:13:42 -0500819 if (data || (cmd->flags & MMC_RSP_BUSY)) {
820 count = sdhci_calc_timeout(host, cmd);
821 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
822 }
823
824 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200825 return;
826
827 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530828 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200829 BUG_ON(data->blksz > host->mmc->max_blk_size);
830 BUG_ON(data->blocks > 65535);
831
832 host->data = data;
833 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400834 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200835
Richard Röjforsa13abc72009-09-22 16:45:30 -0700836 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100837 host->flags |= SDHCI_REQ_USE_DMA;
838
Pierre Ossman2134a922008-06-28 18:28:51 +0200839 /*
840 * FIXME: This doesn't account for merging when mapping the
841 * scatterlist.
842 */
843 if (host->flags & SDHCI_REQ_USE_DMA) {
844 int broken, i;
845 struct scatterlist *sg;
846
847 broken = 0;
848 if (host->flags & SDHCI_USE_ADMA) {
849 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
850 broken = 1;
851 } else {
852 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
853 broken = 1;
854 }
855
856 if (unlikely(broken)) {
857 for_each_sg(data->sg, sg, data->sg_len, i) {
858 if (sg->length & 0x3) {
859 DBG("Reverting to PIO because of "
860 "transfer size (%d)\n",
861 sg->length);
862 host->flags &= ~SDHCI_REQ_USE_DMA;
863 break;
864 }
865 }
866 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100867 }
868
869 /*
870 * The assumption here being that alignment is the same after
871 * translation to device address space.
872 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200873 if (host->flags & SDHCI_REQ_USE_DMA) {
874 int broken, i;
875 struct scatterlist *sg;
876
877 broken = 0;
878 if (host->flags & SDHCI_USE_ADMA) {
879 /*
880 * As we use 3 byte chunks to work around
881 * alignment problems, we need to check this
882 * quirk.
883 */
884 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
885 broken = 1;
886 } else {
887 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
888 broken = 1;
889 }
890
891 if (unlikely(broken)) {
892 for_each_sg(data->sg, sg, data->sg_len, i) {
893 if (sg->offset & 0x3) {
894 DBG("Reverting to PIO because of "
895 "bad alignment\n");
896 host->flags &= ~SDHCI_REQ_USE_DMA;
897 break;
898 }
899 }
900 }
901 }
902
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200903 if (host->flags & SDHCI_REQ_USE_DMA) {
904 if (host->flags & SDHCI_USE_ADMA) {
905 ret = sdhci_adma_table_pre(host, data);
906 if (ret) {
907 /*
908 * This only happens when someone fed
909 * us an invalid request.
910 */
911 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200912 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200913 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300914 sdhci_writel(host, host->adma_addr,
915 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200916 }
917 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300918 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200919
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800920 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300921 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200922 /*
923 * This only happens when someone fed
924 * us an invalid request.
925 */
926 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200927 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200928 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200929 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300930 sdhci_writel(host, sg_dma_address(data->sg),
931 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200932 }
933 }
934 }
935
Pierre Ossman2134a922008-06-28 18:28:51 +0200936 /*
937 * Always adjust the DMA selection as some controllers
938 * (e.g. JMicron) can't do PIO properly when the selection
939 * is ADMA.
940 */
941 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300942 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200943 ctrl &= ~SDHCI_CTRL_DMA_MASK;
944 if ((host->flags & SDHCI_REQ_USE_DMA) &&
945 (host->flags & SDHCI_USE_ADMA))
946 ctrl |= SDHCI_CTRL_ADMA32;
947 else
948 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100950 }
951
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200952 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200953 int flags;
954
955 flags = SG_MITER_ATOMIC;
956 if (host->data->flags & MMC_DATA_READ)
957 flags |= SG_MITER_TO_SG;
958 else
959 flags |= SG_MITER_FROM_SG;
960 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200961 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700963
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300964 sdhci_set_transfer_irqs(host);
965
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400966 /* Set the DMA boundary value and block size */
967 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
968 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300969 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700970}
971
972static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500973 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700974{
975 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500976 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700977
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700978 if (data == NULL)
979 return;
980
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200981 WARN_ON(!host->data);
982
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700983 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500984 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
985 mode |= SDHCI_TRNS_MULTI;
986 /*
987 * If we are sending CMD23, CMD12 never gets sent
988 * on successful completion (so no Auto-CMD12).
989 */
990 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
991 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500992 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
993 mode |= SDHCI_TRNS_AUTO_CMD23;
994 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
995 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700996 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500997
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530998 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700999 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +05301000 if (host->ops->toggle_cdr)
1001 host->ops->toggle_cdr(host, true);
1002 }
1003 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
1004 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001005 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001006 mode |= SDHCI_TRNS_DMA;
1007
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001008 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009}
1010
1011static void sdhci_finish_data(struct sdhci_host *host)
1012{
1013 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015 BUG_ON(!host->data);
1016
1017 data = host->data;
1018 host->data = NULL;
1019
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001020 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001021 if (host->flags & SDHCI_USE_ADMA)
1022 sdhci_adma_table_post(host, data);
1023 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001024 if (!data->host_cookie)
1025 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1026 data->sg_len,
1027 (data->flags & MMC_DATA_READ) ?
1028 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +02001029 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 }
1031
1032 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001033 * The specification states that the block count register must
1034 * be updated, but it does not specify at what point in the
1035 * data flow. That makes the register entirely useless to read
1036 * back so we have to assume that nothing made it to the card
1037 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001039 if (data->error)
1040 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001042 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043
Andrei Warkentine89d4562011-05-23 15:06:37 -05001044 /*
1045 * Need to send CMD12 if -
1046 * a) open-ended multiblock transfer (no CMD23)
1047 * b) error in multiblock transfer
1048 */
1049 if (data->stop &&
1050 (data->error ||
1051 !host->mrq->sbc)) {
1052
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053 /*
1054 * The controller needs a reset of internal state machines
1055 * upon error conditions.
1056 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001057 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 sdhci_reset(host, SDHCI_RESET_CMD);
1059 sdhci_reset(host, SDHCI_RESET_DATA);
1060 }
1061
1062 sdhci_send_command(host, data->stop);
1063 } else
1064 tasklet_schedule(&host->finish_tasklet);
1065}
1066
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301067#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1068
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1070{
1071 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001072 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001073 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001074
1075 WARN_ON(host->cmd);
1076
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001078 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001079
1080 mask = SDHCI_CMD_INHIBIT;
1081 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1082 mask |= SDHCI_DATA_INHIBIT;
1083
1084 /* We shouldn't wait for data inihibit for stop commands, even
1085 though they might use busy signaling */
1086 if (host->mrq->data && (cmd == host->mrq->data->stop))
1087 mask &= ~SDHCI_DATA_INHIBIT;
1088
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001089 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001090 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301091 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001092 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001093 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001094 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 tasklet_schedule(&host->finish_tasklet);
1096 return;
1097 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001098 timeout--;
1099 mdelay(1);
1100 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301102 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1103
1104 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1105 mod_timer(&host->timer, jiffies +
1106 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
1108 host->cmd = cmd;
1109
Andrei Warkentina3c77782011-04-11 16:13:42 -05001110 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001112 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113
Andrei Warkentine89d4562011-05-23 15:06:37 -05001114 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001115
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301117 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001119 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120 tasklet_schedule(&host->finish_tasklet);
1121 return;
1122 }
1123
1124 if (!(cmd->flags & MMC_RSP_PRESENT))
1125 flags = SDHCI_CMD_RESP_NONE;
1126 else if (cmd->flags & MMC_RSP_136)
1127 flags = SDHCI_CMD_RESP_LONG;
1128 else if (cmd->flags & MMC_RSP_BUSY)
1129 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1130 else
1131 flags = SDHCI_CMD_RESP_SHORT;
1132
1133 if (cmd->flags & MMC_RSP_CRC)
1134 flags |= SDHCI_CMD_CRC;
1135 if (cmd->flags & MMC_RSP_OPCODE)
1136 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301137
1138 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301139 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001140 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400 ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301141 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142 flags |= SDHCI_CMD_DATA;
1143
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301144 if (cmd->data)
1145 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001146 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147}
1148
1149static void sdhci_finish_command(struct sdhci_host *host)
1150{
1151 int i;
1152
1153 BUG_ON(host->cmd == NULL);
1154
1155 if (host->cmd->flags & MMC_RSP_PRESENT) {
1156 if (host->cmd->flags & MMC_RSP_136) {
1157 /* CRC is stripped so we need to do some shifting. */
1158 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001159 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001160 SDHCI_RESPONSE + (3-i)*4) << 8;
1161 if (i != 3)
1162 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001163 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164 SDHCI_RESPONSE + (3-i)*4-1);
1165 }
1166 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 }
1169 }
1170
Pierre Ossman17b04292007-07-22 22:18:46 +02001171 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172
Andrei Warkentine89d4562011-05-23 15:06:37 -05001173 /* Finished CMD23, now send actual command. */
1174 if (host->cmd == host->mrq->sbc) {
1175 host->cmd = NULL;
1176 sdhci_send_command(host, host->mrq->cmd);
1177 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001178
Andrei Warkentine89d4562011-05-23 15:06:37 -05001179 /* Processed actual command. */
1180 if (host->data && host->data_early)
1181 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001182
Andrei Warkentine89d4562011-05-23 15:06:37 -05001183 if (!host->cmd->data)
1184 tasklet_schedule(&host->finish_tasklet);
1185
1186 host->cmd = NULL;
1187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188}
1189
1190static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1191{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301192 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301193 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301194 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001195 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301196 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301198 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001199 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301200 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201
Sahitya Tummalaca422112013-02-22 12:15:54 +05301202 host->mmc->actual_clock = 0;
1203
Anton Vorontsov81146342009-03-17 00:13:59 +03001204 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301205 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001206 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301207 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001208 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301209 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001210 }
1211
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301212 if (host->clock)
1213 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214
1215 if (clock == 0)
1216 goto out;
1217
Zhangfei Gao85105c52010-08-06 07:10:01 +08001218 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301219 /*
1220 * Check if the Host Controller supports Programmable Clock
1221 * Mode.
1222 */
1223 if (host->clk_mul) {
1224 u16 ctrl;
1225
1226 /*
1227 * We need to figure out whether the Host Driver needs
1228 * to select Programmable Clock Mode, or the value can
1229 * be set automatically by the Host Controller based on
1230 * the Preset Value registers.
1231 */
1232 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1233 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1234 for (div = 1; div <= 1024; div++) {
1235 if (((host->max_clk * host->clk_mul) /
1236 div) <= clock)
1237 break;
1238 }
1239 /*
1240 * Set Programmable Clock Mode in the Clock
1241 * Control register.
1242 */
1243 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301244 real_div = div;
1245 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301246 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001247 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301248 } else {
1249 /* Version 3.00 divisors must be a multiple of 2. */
1250 if (host->max_clk <= clock)
1251 div = 1;
1252 else {
1253 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1254 div += 2) {
1255 if ((host->max_clk / div) <= clock)
1256 break;
1257 }
1258 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301259 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301260 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001261 }
1262 } else {
1263 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001264 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001265 if ((host->max_clk / div) <= clock)
1266 break;
1267 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301268 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301269 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271
Sahitya Tummalaca422112013-02-22 12:15:54 +05301272 if (real_div)
1273 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1274
Sahitya Tummala00240122013-02-28 19:50:51 +05301275 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1276 div = 0;
1277
Arindam Nathc3ed3872011-05-05 12:19:06 +05301278 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001279 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1280 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001281 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001282 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283
Chris Ball27f6cb12009-09-22 16:45:31 -07001284 /* Wait max 20 ms */
1285 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001286 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001287 & SDHCI_CLOCK_INT_STABLE)) {
1288 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301289 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001290 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001291 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301292 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001293 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001294 timeout--;
1295 mdelay(1);
1296 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297
1298 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001299 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001300
1301out:
1302 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301303ret:
1304 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001305}
1306
Sahitya Tummalaca422112013-02-22 12:15:54 +05301307static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001308{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001309 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001310
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001311 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001312 switch (1 << power) {
1313 case MMC_VDD_165_195:
1314 pwr = SDHCI_POWER_180;
1315 break;
1316 case MMC_VDD_29_30:
1317 case MMC_VDD_30_31:
1318 pwr = SDHCI_POWER_300;
1319 break;
1320 case MMC_VDD_32_33:
1321 case MMC_VDD_33_34:
1322 pwr = SDHCI_POWER_330;
1323 break;
1324 default:
1325 BUG();
1326 }
1327 }
1328
1329 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301330 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001331
Pierre Ossmanae628902009-05-03 20:45:03 +02001332 host->pwr = pwr;
1333
1334 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001335 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301336 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301337 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301338 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001339 }
1340
1341 /*
1342 * Spec says that we should clear the power reg before setting
1343 * a new value. Some controllers don't seem to like this though.
1344 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301345 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001346 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301347 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301348 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301349 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001350
Andres Salomone08c1692008-07-04 10:00:03 -07001351 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001352 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001353 * and set turn on power at the same time, so set the voltage first.
1354 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301355 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001356 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301357 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301358 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301359 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001360
1361 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001362
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001363 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301364 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301365 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001366
1367 /*
1368 * Some controllers need an extra 10ms delay of 10ms before they
1369 * can apply clock after applying power
1370 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001371 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001372 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301373
1374 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001375}
1376
Pierre Ossmand129bce2006-03-24 03:18:17 -08001377/*****************************************************************************\
1378 * *
1379 * MMC callbacks *
1380 * *
1381\*****************************************************************************/
1382
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301383static int sdhci_enable(struct mmc_host *mmc)
1384{
1385 struct sdhci_host *host = mmc_priv(mmc);
1386
1387 if (host->cpu_dma_latency_us)
1388 pm_qos_update_request(&host->pm_qos_req_dma,
1389 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301390 if (host->ops->platform_bus_voting)
1391 host->ops->platform_bus_voting(host, 1);
1392
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301393 return 0;
1394}
1395
1396static int sdhci_disable(struct mmc_host *mmc)
1397{
1398 struct sdhci_host *host = mmc_priv(mmc);
1399
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301400 if (host->cpu_dma_latency_us) {
1401 /*
1402 * In performance mode, release QoS vote after a timeout to
1403 * make sure back-to-back requests don't suffer from latencies
1404 * that are involved to wake CPU from low power modes in cases
1405 * where the CPU goes into low power mode as soon as QoS vote is
1406 * released.
1407 */
1408 if (host->power_policy == SDHCI_PERFORMANCE_MODE)
1409 pm_qos_update_request_timeout(&host->pm_qos_req_dma,
1410 host->cpu_dma_latency_us,
1411 host->pm_qos_timeout_us);
1412 else
1413 pm_qos_update_request(&host->pm_qos_req_dma,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301414 PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301415 }
1416
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301417 if (host->ops->platform_bus_voting)
1418 host->ops->platform_bus_voting(host, 0);
1419
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301420 return 0;
1421}
1422
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301423static inline void sdhci_update_power_policy(struct sdhci_host *host,
1424 enum sdhci_power_policy policy)
1425{
1426 host->power_policy = policy;
1427}
1428
1429static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1430{
1431 int err = 0;
1432 struct sdhci_host *host = mmc_priv(mmc);
1433
1434 switch (state) {
1435 case MMC_LOAD_HIGH:
1436 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1437 break;
1438 case MMC_LOAD_LOW:
1439 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1440 break;
1441 default:
1442 err = -EINVAL;
1443 break;
1444 }
1445
1446 return err;
1447}
1448
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001449static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1450 bool is_first_req)
1451{
1452 struct sdhci_host *host = mmc_priv(mmc);
1453
1454 if (mrq->data->host_cookie) {
1455 mrq->data->host_cookie = 0;
1456 return;
1457 }
1458
1459 if (host->flags & SDHCI_REQ_USE_DMA)
1460 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1461 mrq->data->host_cookie = 0;
1462}
1463
1464static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1465 int err)
1466{
1467 struct sdhci_host *host = mmc_priv(mmc);
1468 struct mmc_data *data = mrq->data;
1469
1470 if (host->flags & SDHCI_REQ_USE_DMA) {
1471 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1472 (data->flags & MMC_DATA_WRITE) ?
1473 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1474 data->host_cookie = 0;
1475 }
1476}
1477
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301478static bool sdhci_check_state(struct sdhci_host *host)
1479{
1480 struct mmc_host *mmc = host->mmc;
1481
1482 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301483 (mmc_use_core_runtime_pm(mmc) ?
1484 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301485 return true;
1486 else
1487 return false;
1488}
1489
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1491{
1492 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001493 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494 unsigned long flags;
Aaron Lu11052c72012-07-03 17:27:49 +08001495 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001496
1497 host = mmc_priv(mmc);
1498
Adrian Hunter50accb92011-10-03 15:33:34 +03001499 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301500 if (sdhci_check_state(host)) {
1501 sdhci_dump_state(host);
1502 WARN(1, "sdhci in bad state");
1503 mrq->cmd->error = -EIO;
1504 if (mrq->data)
1505 mrq->data->error = -EIO;
1506 tasklet_schedule(&host->finish_tasklet);
1507 return;
1508 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001509
Pierre Ossmand129bce2006-03-24 03:18:17 -08001510 spin_lock_irqsave(&host->lock, flags);
1511
1512 WARN_ON(host->mrq != NULL);
1513
Pierre Ossmanf9134312008-12-21 17:01:48 +01001514#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001516#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001517
1518 /*
1519 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1520 * requests if Auto-CMD12 is enabled.
1521 */
1522 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001523 if (mrq->stop) {
1524 mrq->data->stop = NULL;
1525 mrq->stop = NULL;
1526 }
1527 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528
1529 host->mrq = mrq;
1530
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001531 /* If polling, assume that the card is always present. */
1532 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1533 present = true;
1534 else
1535 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1536 SDHCI_CARD_PRESENT;
1537
1538 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001539 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001540 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301541 } else {
1542 u32 present_state;
1543
1544 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1545 /*
1546 * Check if the re-tuning timer has already expired and there
1547 * is no on-going data transfer. If so, we need to execute
1548 * tuning procedure before sending command.
1549 */
Asutosh Das6ec99eb2013-12-12 16:12:43 +05301550 if ((mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK) &&
1551 (mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK_HS400) &&
1552 (mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
1553 (host->flags & SDHCI_NEEDS_RETUNING) &&
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301554 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ballcb778082012-11-05 14:29:49 -05001555 if (mmc->card) {
1556 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1557 tuning_opcode =
1558 mmc->card->type == MMC_TYPE_MMC ?
1559 MMC_SEND_TUNING_BLOCK_HS200 :
1560 MMC_SEND_TUNING_BLOCK;
Asutosh Das6ec99eb2013-12-12 16:12:43 +05301561 host->mrq = NULL;
Chris Ballcb778082012-11-05 14:29:49 -05001562 spin_unlock_irqrestore(&host->lock, flags);
1563 sdhci_execute_tuning(mmc, tuning_opcode);
1564 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301565
Chris Ballcb778082012-11-05 14:29:49 -05001566 /* Restore original mmc_request structure */
1567 host->mrq = mrq;
1568 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301569 }
1570
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001571 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001572 sdhci_send_command(host, mrq->sbc);
1573 else
1574 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301575 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001576
Pierre Ossman5f25a662006-10-04 02:15:39 -07001577 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001578 spin_unlock_irqrestore(&host->lock, flags);
1579}
1580
Adrian Hunter50accb92011-10-03 15:33:34 +03001581static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301584 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585 u8 ctrl;
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301586 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001587
Sahitya Tummala40474e42013-07-10 14:40:37 +05301588 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301589 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301590 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1591 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301592 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301593 return;
1594 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001595
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301596 if (ios->clock)
1597 sdhci_set_clock(host, ios->clock);
1598
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301599 /*
1600 * The controller clocks may be off during power-up and we may end up
1601 * enabling card clock before giving power to the card. Hence, during
1602 * MMC_POWER_UP enable the controller clock and turn-on the regulators.
1603 * The mmc_power_up would provide the necessary delay before turning on
1604 * the clocks to the card.
1605 */
1606 if (ios->power_mode & MMC_POWER_UP) {
1607 if (host->ops->enable_controller_clock) {
1608 ret = host->ops->enable_controller_clock(host);
1609 if (ret) {
1610 pr_err("%s: enabling controller clock: failed: %d\n",
1611 mmc_hostname(host->mmc), ret);
1612 } else {
1613 vdd_bit = sdhci_set_power(host, ios->vdd);
1614
1615 if (host->vmmc && vdd_bit != -1)
1616 mmc_regulator_set_ocr(host->mmc,
1617 host->vmmc,
1618 vdd_bit);
1619 }
1620 }
1621 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301622 spin_lock_irqsave(&host->lock, flags);
1623 if (!host->clock) {
1624 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301625 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301626 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301628 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301630 if (!host->ops->enable_controller_clock && (ios->power_mode &
1631 (MMC_POWER_UP |
1632 MMC_POWER_ON))) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301633 vdd_bit = sdhci_set_power(host, ios->vdd);
1634
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301635 if (host->vmmc && vdd_bit != -1)
1636 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1637 }
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301638
1639 spin_lock_irqsave(&host->lock, flags);
Philip Rakity643a81f2010-09-23 08:24:32 -07001640 if (host->ops->platform_send_init_74_clocks)
1641 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1642
Philip Rakity15ec4462010-11-19 16:48:39 -05001643 /*
1644 * If your platform has 8-bit width support but is not a v3 controller,
1645 * or if it requires special setup code, you should implement that in
1646 * platform_8bit_width().
1647 */
1648 if (host->ops->platform_8bit_width)
1649 host->ops->platform_8bit_width(host, ios->bus_width);
1650 else {
1651 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1652 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1653 ctrl &= ~SDHCI_CTRL_4BITBUS;
1654 if (host->version >= SDHCI_SPEC_300)
1655 ctrl |= SDHCI_CTRL_8BITBUS;
1656 } else {
1657 if (host->version >= SDHCI_SPEC_300)
1658 ctrl &= ~SDHCI_CTRL_8BITBUS;
1659 if (ios->bus_width == MMC_BUS_WIDTH_4)
1660 ctrl |= SDHCI_CTRL_4BITBUS;
1661 else
1662 ctrl &= ~SDHCI_CTRL_4BITBUS;
1663 }
1664 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1665 }
1666
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001667 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001668
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001669 if ((ios->timing == MMC_TIMING_SD_HS ||
1670 ios->timing == MMC_TIMING_MMC_HS)
1671 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001672 ctrl |= SDHCI_CTRL_HISPD;
1673 else
1674 ctrl &= ~SDHCI_CTRL_HISPD;
1675
Arindam Nathd6d50a12011-05-05 12:18:59 +05301676 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301677 u16 clk, ctrl_2;
1678 unsigned int clock;
1679
1680 /* In case of UHS-I modes, set High Speed Enable */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001681 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1682 (ios->timing == MMC_TIMING_MMC_HS200) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301683 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301684 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1685 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001686 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301687 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301688
1689 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1690 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301691 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301692 /*
1693 * We only need to set Driver Strength if the
1694 * preset value enable is not set.
1695 */
1696 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1697 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1698 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1699 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1700 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1701
1702 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301703 } else {
1704 /*
1705 * According to SDHC Spec v3.00, if the Preset Value
1706 * Enable in the Host Control 2 register is set, we
1707 * need to reset SD Clock Enable before changing High
1708 * Speed Enable to avoid generating clock gliches.
1709 */
Arindam Nath758535c2011-05-05 12:19:00 +05301710
1711 /* Reset SD Clock Enable */
1712 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1713 clk &= ~SDHCI_CLOCK_CARD_EN;
1714 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1715
1716 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1717
1718 /* Re-enable SD Clock */
1719 clock = host->clock;
1720 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301721 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301722 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301723 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301724 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301725
Arindam Nath49c468f2011-05-05 12:19:01 +05301726 /* Reset SD Clock Enable */
1727 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1728 clk &= ~SDHCI_CLOCK_CARD_EN;
1729 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1730
Philip Rakity6322cdd2011-05-13 11:17:15 +05301731 if (host->ops->set_uhs_signaling)
1732 host->ops->set_uhs_signaling(host, ios->timing);
1733 else {
1734 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1735 /* Select Bus Speed Mode for host */
1736 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001737 if (ios->timing == MMC_TIMING_MMC_HS400)
1738 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1739 else if (ios->timing == MMC_TIMING_MMC_HS200)
Girish K S2cd06dc2012-01-06 09:56:39 +05301740 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1741 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301742 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1743 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1744 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1745 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1746 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1747 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1748 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1749 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1750 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1751 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1752 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301753
1754 /* Re-enable SD Clock */
1755 clock = host->clock;
1756 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301757 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301758 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301759 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301760 } else
1761 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301762
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301763 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001764 /*
1765 * Some (ENE) controllers go apeshit on some ios operation,
1766 * signalling timeout and CRC errors even on CMD0. Resetting
1767 * it on each ios seems to solve the problem.
1768 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001769 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001770 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1771
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301772 /*
1773 * Reset the chip on each power off.
1774 * Should clear out any weird states.
1775 */
1776 if (ios->power_mode == MMC_POWER_OFF) {
1777 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1778 sdhci_reinit(host);
1779 vdd_bit = sdhci_set_power(host, -1);
1780 if (host->vmmc && vdd_bit != -1)
1781 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1782 }
1783 if (!ios->clock)
1784 sdhci_set_clock(host, ios->clock);
1785
Pierre Ossman5f25a662006-10-04 02:15:39 -07001786 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301787 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001788}
1789
Adrian Hunter50accb92011-10-03 15:33:34 +03001790static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1791{
1792 struct sdhci_host *host = mmc_priv(mmc);
1793
1794 sdhci_runtime_pm_get(host);
1795 sdhci_do_set_ios(host, ios);
1796 sdhci_runtime_pm_put(host);
1797}
1798
1799static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001800{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001801 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001802 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001803
Pierre Ossmand129bce2006-03-24 03:18:17 -08001804 spin_lock_irqsave(&host->lock, flags);
1805
Pierre Ossman1e728592008-04-16 19:13:13 +02001806 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001807 is_readonly = 0;
1808 else if (host->ops->get_ro)
1809 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001810 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001811 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1812 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001813
1814 spin_unlock_irqrestore(&host->lock, flags);
1815
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001816 /* This quirk needs to be replaced by a callback-function later */
1817 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1818 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001819}
1820
Takashi Iwai82b0e232011-04-21 20:26:38 +02001821#define SAMPLE_COUNT 5
1822
Adrian Hunter50accb92011-10-03 15:33:34 +03001823static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001824{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001825 int i, ro_count;
1826
Takashi Iwai82b0e232011-04-21 20:26:38 +02001827 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001828 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001829
1830 ro_count = 0;
1831 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001832 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001833 if (++ro_count > SAMPLE_COUNT / 2)
1834 return 1;
1835 }
1836 msleep(30);
1837 }
1838 return 0;
1839}
1840
Adrian Hunter50accb92011-10-03 15:33:34 +03001841static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001842{
Adrian Hunter50accb92011-10-03 15:33:34 +03001843 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001844
Adrian Hunter50accb92011-10-03 15:33:34 +03001845 if (host->ops && host->ops->hw_reset)
1846 host->ops->hw_reset(host);
1847}
Adrian Hunter20758b62011-08-29 16:42:12 +03001848
Adrian Hunter50accb92011-10-03 15:33:34 +03001849static int sdhci_get_ro(struct mmc_host *mmc)
1850{
1851 struct sdhci_host *host = mmc_priv(mmc);
1852 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001853
Adrian Hunter50accb92011-10-03 15:33:34 +03001854 sdhci_runtime_pm_get(host);
1855 ret = sdhci_do_get_ro(host);
1856 sdhci_runtime_pm_put(host);
1857 return ret;
1858}
1859
1860static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1861{
Pierre Ossman1e728592008-04-16 19:13:13 +02001862 if (host->flags & SDHCI_DEVICE_DEAD)
1863 goto out;
1864
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001865 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001866 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1867 else
1868 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1869
1870 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1871 if (host->runtime_suspended)
1872 goto out;
1873
1874 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001875 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1876 else
1877 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001878out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001879 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001880}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001881
Adrian Hunter50accb92011-10-03 15:33:34 +03001882static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1883{
1884 struct sdhci_host *host = mmc_priv(mmc);
1885 unsigned long flags;
1886
1887 spin_lock_irqsave(&host->lock, flags);
1888 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001889 spin_unlock_irqrestore(&host->lock, flags);
1890}
1891
Adrian Hunter50accb92011-10-03 15:33:34 +03001892static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1893 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301894{
Arindam Nathf2119df2011-05-05 12:18:57 +05301895 u8 pwr;
1896 u16 clk, ctrl;
1897 u32 present_state;
1898
Arindam Nathf2119df2011-05-05 12:18:57 +05301899 /*
1900 * Signal Voltage Switching is only applicable for Host Controllers
1901 * v3.00 and above.
1902 */
1903 if (host->version < SDHCI_SPEC_300)
1904 return 0;
1905
1906 /*
1907 * We first check whether the request is to set signalling voltage
1908 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1909 */
1910 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1911 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1912 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1913 ctrl &= ~SDHCI_CTRL_VDD_180;
1914 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301915 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301916 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301917
1918 /* Wait for 5ms */
1919 usleep_range(5000, 5500);
1920
1921 /* 3.3V regulator output should be stable within 5 ms */
1922 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1923 if (!(ctrl & SDHCI_CTRL_VDD_180))
1924 return 0;
1925 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301926 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301927 "signalling voltage failed\n");
1928 return -EIO;
1929 }
1930 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1931 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1932 /* Stop SDCLK */
1933 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1934 clk &= ~SDHCI_CLOCK_CARD_EN;
1935 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1936
1937 /* Check whether DAT[3:0] is 0000 */
1938 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1939 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1940 SDHCI_DATA_LVL_SHIFT)) {
1941 /*
1942 * Enable 1.8V Signal Enable in the Host Control2
1943 * register
1944 */
1945 ctrl |= SDHCI_CTRL_VDD_180;
1946 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301947 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301948 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301949
1950 /* Wait for 5ms */
1951 usleep_range(5000, 5500);
1952
1953 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1954 if (ctrl & SDHCI_CTRL_VDD_180) {
1955 /* Provide SDCLK again and wait for 1ms*/
1956 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1957 clk |= SDHCI_CLOCK_CARD_EN;
1958 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1959 usleep_range(1000, 1500);
1960
1961 /*
1962 * If DAT[3:0] level is 1111b, then the card
1963 * was successfully switched to 1.8V signaling.
1964 */
1965 present_state = sdhci_readl(host,
1966 SDHCI_PRESENT_STATE);
1967 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1968 SDHCI_DATA_LVL_MASK)
1969 return 0;
1970 }
1971 }
1972
1973 /*
1974 * If we are here, that means the switch to 1.8V signaling
1975 * failed. We power cycle the card, and retry initialization
1976 * sequence by setting S18R to 0.
1977 */
1978 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1979 pwr &= ~SDHCI_POWER_ON;
1980 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301981 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301982 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301983
1984 /* Wait for 1ms as per the spec */
1985 usleep_range(1000, 1500);
1986 pwr |= SDHCI_POWER_ON;
1987 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301988 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301989 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301990
Sahitya Tummalaca422112013-02-22 12:15:54 +05301991 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301992 "voltage failed, retrying with S18R set to 0\n");
1993 return -EAGAIN;
1994 } else
1995 /* No signal voltage switch required */
1996 return 0;
1997}
1998
Adrian Hunter50accb92011-10-03 15:33:34 +03001999static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
2000 struct mmc_ios *ios)
2001{
2002 struct sdhci_host *host = mmc_priv(mmc);
2003 int err;
2004
2005 if (host->version < SDHCI_SPEC_300)
2006 return 0;
2007 sdhci_runtime_pm_get(host);
2008 err = sdhci_do_start_signal_voltage_switch(host, ios);
2009 sdhci_runtime_pm_put(host);
2010 return err;
2011}
2012
Girish K S2cd06dc2012-01-06 09:56:39 +05302013static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302014{
2015 struct sdhci_host *host;
2016 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05302017 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05302018 int tuning_loop_counter = MAX_TUNING_LOOP;
2019 unsigned long timeout;
2020 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05302021 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05302022
2023 host = mmc_priv(mmc);
2024
Adrian Hunter50accb92011-10-03 15:33:34 +03002025 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302026 disable_irq(host->irq);
2027 spin_lock(&host->lock);
2028
2029 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2030
2031 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05302032 * The Host Controller needs tuning only in case of SDR104 mode
2033 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05302034 * Capabilities register.
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002035 * If the Host Controller supports the HS400/HS200 mode then the
Girish K S2cd06dc2012-01-06 09:56:39 +05302036 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302037 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08002038 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
2039 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002040 (host->flags & SDHCI_HS200_NEEDS_TUNING) ||
2041 (host->flags & SDHCI_HS400_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05302042 requires_tuning_nonuhs = true;
2043
Arindam Nathb513ea22011-05-05 12:19:04 +05302044 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05302045 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05302046 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2047 else {
2048 spin_unlock(&host->lock);
2049 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002050 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302051 return 0;
2052 }
2053
Asutosh Das8ddd3482013-01-04 11:45:46 +05302054 if (host->ops->execute_tuning) {
2055 spin_unlock(&host->lock);
2056 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05302057 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05302058 disable_irq(host->irq);
2059 spin_lock(&host->lock);
2060 goto out;
2061 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302062 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2063
2064 /*
2065 * As per the Host Controller spec v3.00, tuning command
2066 * generates Buffer Read Ready interrupt, so enable that.
2067 *
2068 * Note: The spec clearly says that when tuning sequence
2069 * is being performed, the controller does not generate
2070 * interrupts other than Buffer Read Ready interrupt. But
2071 * to make sure we don't hit a controller bug, we _only_
2072 * enable Buffer Read Ready interrupt here.
2073 */
2074 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
2075 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
2076
2077 /*
2078 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
2079 * of loops reaches 40 times or a timeout of 150ms occurs.
2080 */
2081 timeout = 150;
2082 do {
2083 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03002084 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302085
2086 if (!tuning_loop_counter && !timeout)
2087 break;
2088
Girish K S2cd06dc2012-01-06 09:56:39 +05302089 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302090 cmd.arg = 0;
2091 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2092 cmd.retries = 0;
2093 cmd.data = NULL;
2094 cmd.error = 0;
2095
2096 mrq.cmd = &cmd;
2097 host->mrq = &mrq;
2098
2099 /*
2100 * In response to CMD19, the card sends 64 bytes of tuning
2101 * block to the Host Controller. So we set the block size
2102 * to 64 here.
2103 */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002104 if ((cmd.opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2105 (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200)) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302106 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2107 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2108 SDHCI_BLOCK_SIZE);
2109 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2110 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2111 SDHCI_BLOCK_SIZE);
2112 } else {
2113 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2114 SDHCI_BLOCK_SIZE);
2115 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302116
2117 /*
2118 * The tuning block is sent by the card to the host controller.
2119 * So we set the TRNS_READ bit in the Transfer Mode register.
2120 * This also takes care of setting DMA Enable and Multi Block
2121 * Select in the same register to 0.
2122 */
2123 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2124
2125 sdhci_send_command(host, &cmd);
2126
2127 host->cmd = NULL;
2128 host->mrq = NULL;
2129
2130 spin_unlock(&host->lock);
2131 enable_irq(host->irq);
2132
2133 /* Wait for Buffer Read Ready interrupt */
2134 wait_event_interruptible_timeout(host->buf_ready_int,
2135 (host->tuning_done == 1),
2136 msecs_to_jiffies(50));
2137 disable_irq(host->irq);
2138 spin_lock(&host->lock);
2139
2140 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302141 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302142 "Buffer Read Ready interrupt during tuning "
2143 "procedure, falling back to fixed sampling "
2144 "clock\n");
2145 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2146 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2147 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2148 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2149
2150 err = -EIO;
2151 goto out;
2152 }
2153
2154 host->tuning_done = 0;
2155
2156 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2157 tuning_loop_counter--;
2158 timeout--;
2159 mdelay(1);
2160 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2161
2162 /*
2163 * The Host Driver has exhausted the maximum number of loops allowed,
2164 * so use fixed sampling frequency.
2165 */
2166 if (!tuning_loop_counter || !timeout) {
2167 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2168 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2169 } else {
2170 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302171 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302172 " failed, falling back to fixed sampling"
2173 " clock\n");
2174 err = -EIO;
2175 }
2176 }
2177
2178out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302179 /*
2180 * If this is the very first time we are here, we start the retuning
2181 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2182 * flag won't be set, we check this condition before actually starting
2183 * the timer.
2184 */
2185 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2186 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2187 mod_timer(&host->tuning_timer, jiffies +
2188 host->tuning_count * HZ);
2189 /* Tuning mode 1 limits the maximum data length to 4MB */
2190 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2191 } else {
2192 host->flags &= ~SDHCI_NEEDS_RETUNING;
2193 /* Reload the new initial value for timer */
2194 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2195 mod_timer(&host->tuning_timer, jiffies +
2196 host->tuning_count * HZ);
2197 }
2198
2199 /*
2200 * In case tuning fails, host controllers which support re-tuning can
2201 * try tuning again at a later time, when the re-tuning timer expires.
2202 * So for these controllers, we return 0. Since there might be other
2203 * controllers who do not have this capability, we return error for
2204 * them.
2205 */
2206 if (err && host->tuning_count &&
2207 host->tuning_mode == SDHCI_TUNING_MODE_1)
2208 err = 0;
2209
Arindam Nathb513ea22011-05-05 12:19:04 +05302210 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2211 spin_unlock(&host->lock);
2212 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002213 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302214
2215 return err;
2216}
2217
Adrian Hunter50accb92011-10-03 15:33:34 +03002218static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302219{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302220 u16 ctrl;
2221 unsigned long flags;
2222
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302223 /* Host Controller v3.00 defines preset value registers */
2224 if (host->version < SDHCI_SPEC_300)
2225 return;
2226
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302227 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2228 return;
2229
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302230 spin_lock_irqsave(&host->lock, flags);
2231
2232 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2233
2234 /*
2235 * We only enable or disable Preset Value if they are not already
2236 * enabled or disabled respectively. Otherwise, we bail out.
2237 */
2238 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2239 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2240 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002241 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302242 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2243 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2244 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002245 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302246 }
2247
2248 spin_unlock_irqrestore(&host->lock, flags);
2249}
2250
Adrian Hunter50accb92011-10-03 15:33:34 +03002251static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2252{
2253 struct sdhci_host *host = mmc_priv(mmc);
2254
2255 sdhci_runtime_pm_get(host);
2256 sdhci_do_enable_preset_value(host, enable);
2257 sdhci_runtime_pm_put(host);
2258}
2259
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002260static int sdhci_stop_request(struct mmc_host *mmc)
2261{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002262 struct sdhci_host *host = mmc_priv(mmc);
2263 unsigned long flags;
2264 struct mmc_data *data;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002265 int ret = 0;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002266
2267 spin_lock_irqsave(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002268 if (!host->mrq || !host->data) {
2269 ret = MMC_BLK_NO_REQ_TO_STOP;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002270 goto out;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002271 }
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002272
2273 data = host->data;
2274
2275 if (host->ops->disable_data_xfer)
2276 host->ops->disable_data_xfer(host);
2277
2278 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2279
2280 if (host->flags & SDHCI_REQ_USE_DMA) {
2281 if (host->flags & SDHCI_USE_ADMA) {
2282 sdhci_adma_table_post(host, data);
2283 } else {
2284 if (!data->host_cookie)
2285 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2286 data->sg_len,
2287 (data->flags & MMC_DATA_READ) ?
2288 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2289 }
2290 }
2291 del_timer(&host->timer);
2292 host->mrq = NULL;
2293 host->cmd = NULL;
2294 host->data = NULL;
2295out:
2296 spin_unlock_irqrestore(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002297 return ret;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002298}
2299
2300static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2301{
2302 struct sdhci_host *host = mmc_priv(mmc);
2303 u32 present_state = 0;
2304
2305 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2306
2307 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002308}
2309
David Brownellab7aefd2006-11-12 17:55:30 -08002310static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002311 .pre_req = sdhci_pre_req,
2312 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313 .request = sdhci_request,
2314 .set_ios = sdhci_set_ios,
2315 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002316 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002317 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302318 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302319 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302320 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302321 .enable = sdhci_enable,
2322 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002323 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002324 .get_xfer_remain = sdhci_get_xfer_remain,
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05302325 .notify_load = sdhci_notify_load,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326};
2327
2328/*****************************************************************************\
2329 * *
2330 * Tasklets *
2331 * *
2332\*****************************************************************************/
2333
2334static void sdhci_tasklet_card(unsigned long param)
2335{
2336 struct sdhci_host *host;
2337 unsigned long flags;
2338
2339 host = (struct sdhci_host*)param;
2340
2341 spin_lock_irqsave(&host->lock, flags);
2342
Adrian Hunter50accb92011-10-03 15:33:34 +03002343 /* Check host->mrq first in case we are runtime suspended */
2344 if (host->mrq &&
2345 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302346 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002347 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302348 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002349 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002350
Adrian Hunter50accb92011-10-03 15:33:34 +03002351 sdhci_reset(host, SDHCI_RESET_CMD);
2352 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002353
Adrian Hunter50accb92011-10-03 15:33:34 +03002354 host->mrq->cmd->error = -ENOMEDIUM;
2355 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002356 }
2357
2358 spin_unlock_irqrestore(&host->lock, flags);
2359
Pierre Ossman04cf5852008-08-18 22:18:14 +02002360 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361}
2362
2363static void sdhci_tasklet_finish(unsigned long param)
2364{
2365 struct sdhci_host *host;
2366 unsigned long flags;
2367 struct mmc_request *mrq;
2368
2369 host = (struct sdhci_host*)param;
2370
Adrian Hunter50accb92011-10-03 15:33:34 +03002371 spin_lock_irqsave(&host->lock, flags);
2372
Chris Ball0c9c99a2011-04-27 17:35:31 -04002373 /*
2374 * If this tasklet gets rescheduled while running, it will
2375 * be run again afterwards but without any active request.
2376 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002377 if (!host->mrq) {
2378 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002379 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002380 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002381
2382 del_timer(&host->timer);
2383
2384 mrq = host->mrq;
2385
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386 /*
2387 * The controller needs a reset of internal state machines
2388 * upon error conditions.
2389 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002390 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002391 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002392 (mrq->data && (mrq->data->error ||
2393 (mrq->data->stop && mrq->data->stop->error))) ||
2394 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002395
2396 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002397 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002398 unsigned int clock;
2399
2400 /* This is to force an update */
2401 clock = host->clock;
2402 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302403 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002404 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302405 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002406 }
2407
2408 /* Spec says we should do both at the same time, but Ricoh
2409 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002410 sdhci_reset(host, SDHCI_RESET_CMD);
2411 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002412 } else {
2413 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2414 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002415 }
2416
2417 host->mrq = NULL;
2418 host->cmd = NULL;
2419 host->data = NULL;
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302420 host->auto_cmd_err_sts = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002421
Pierre Ossmanf9134312008-12-21 17:01:48 +01002422#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002424#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425
Pierre Ossman5f25a662006-10-04 02:15:39 -07002426 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427 spin_unlock_irqrestore(&host->lock, flags);
2428
2429 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002430 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002431}
2432
2433static void sdhci_timeout_timer(unsigned long data)
2434{
2435 struct sdhci_host *host;
2436 unsigned long flags;
2437
2438 host = (struct sdhci_host*)data;
2439
2440 spin_lock_irqsave(&host->lock, flags);
2441
2442 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302443 if (!host->mrq->cmd->ignore_timeout) {
2444 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2445 mmc_hostname(host->mmc));
2446 sdhci_dumpregs(host);
2447 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002448
2449 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302450 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2451 mmc_hostname(host->mmc),
2452 (host->data->blksz * host->data->blocks),
2453 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2454 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002455 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 sdhci_finish_data(host);
2457 } else {
2458 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002459 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002460 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002461 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462
2463 tasklet_schedule(&host->finish_tasklet);
2464 }
2465 }
2466
Pierre Ossman5f25a662006-10-04 02:15:39 -07002467 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002468 spin_unlock_irqrestore(&host->lock, flags);
2469}
2470
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302471static void sdhci_tuning_timer(unsigned long data)
2472{
2473 struct sdhci_host *host;
2474 unsigned long flags;
2475
2476 host = (struct sdhci_host *)data;
2477
2478 spin_lock_irqsave(&host->lock, flags);
2479
2480 host->flags |= SDHCI_NEEDS_RETUNING;
2481
2482 spin_unlock_irqrestore(&host->lock, flags);
2483}
2484
Pierre Ossmand129bce2006-03-24 03:18:17 -08002485/*****************************************************************************\
2486 * *
2487 * Interrupt handling *
2488 * *
2489\*****************************************************************************/
2490
2491static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2492{
Asutosh Das80c02552013-07-23 16:20:34 +05302493 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002494 BUG_ON(intmask == 0);
2495
2496 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302497 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002498 "though no command operation was in progress.\n",
2499 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500 sdhci_dumpregs(host);
2501 return;
2502 }
2503
Pierre Ossman43b58b32007-07-25 23:15:27 +02002504 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002505 host->cmd->error = -ETIMEDOUT;
2506 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2507 SDHCI_INT_INDEX))
2508 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509
Asutosh Das80c02552013-07-23 16:20:34 +05302510 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302511 auto_cmd_status = host->auto_cmd_err_sts;
2512 pr_err("%s: %s: AUTO CMD err sts 0x%08x\n",
2513 mmc_hostname(host->mmc), __func__, auto_cmd_status);
Asutosh Das80c02552013-07-23 16:20:34 +05302514 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2515 SDHCI_AUTO_CMD_INDEX_ERR |
2516 SDHCI_AUTO_CMD_ENDBIT_ERR))
2517 host->cmd->error = -EIO;
2518 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2519 host->cmd->error = -ETIMEDOUT;
2520 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2521 host->cmd->error = -EILSEQ;
2522 }
2523
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302524 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002525 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2526 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302527 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2528 if (intmask & SDHCI_INT_CRC) {
2529 sdhci_reset(host, SDHCI_RESET_CMD);
2530 host->cmd->error = 0;
2531 }
2532 }
2533 }
2534
Pierre Ossmane8095172008-07-25 01:09:08 +02002535 if (host->cmd->error) {
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302536 if (host->cmd->error == -EILSEQ)
2537 host->flags |= SDHCI_NEEDS_RETUNING;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002539 return;
2540 }
2541
2542 /*
2543 * The host can send and interrupt when the busy state has
2544 * ended, allowing us to wait without wasting CPU cycles.
2545 * Unfortunately this is overloaded on the "data complete"
2546 * interrupt, so we need to take some care when handling
2547 * it.
2548 *
2549 * Note: The 1.0 specification is a bit ambiguous about this
2550 * feature so there might be some problems with older
2551 * controllers.
2552 */
2553 if (host->cmd->flags & MMC_RSP_BUSY) {
2554 if (host->cmd->data)
2555 DBG("Cannot wait for busy signal when also "
2556 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002557 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002558 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002559
2560 /* The controller does not support the end-of-busy IRQ,
2561 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002562 }
2563
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302564 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002565 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2566 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302567 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2568 if (intmask & SDHCI_INT_CRC) {
2569 sdhci_finish_command(host);
2570 return;
2571 }
2572 }
2573 }
2574
Pierre Ossmane8095172008-07-25 01:09:08 +02002575 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002576 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002577}
2578
Ben Dooks6882a8c2009-06-14 13:52:38 +01002579static void sdhci_show_adma_error(struct sdhci_host *host)
2580{
2581 const char *name = mmc_hostname(host->mmc);
2582 u8 *desc = host->adma_desc;
2583 __le32 *dma;
2584 __le16 *len;
2585 u8 attr;
2586
2587 sdhci_dumpregs(host);
2588
2589 while (true) {
2590 dma = (__le32 *)(desc + 4);
2591 len = (__le16 *)(desc + 2);
2592 attr = *desc;
2593
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302594 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002595 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2596
2597 desc += 8;
2598
2599 if (attr & 2)
2600 break;
2601 }
2602}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002603
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2605{
Girish K S2cd06dc2012-01-06 09:56:39 +05302606 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302607 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002608 BUG_ON(intmask == 0);
2609
Arindam Nathb513ea22011-05-05 12:19:04 +05302610 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2611 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302612 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2613 if (command == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002614 command == MMC_SEND_TUNING_BLOCK_HS200 ||
2615 command == MMC_SEND_TUNING_BLOCK_HS400) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302616 host->tuning_done = 1;
2617 wake_up(&host->buf_ready_int);
2618 return;
2619 }
2620 }
2621
Pierre Ossmand129bce2006-03-24 03:18:17 -08002622 if (!host->data) {
2623 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002624 * The "data complete" interrupt is also used to
2625 * indicate that a busy state has ended. See comment
2626 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002627 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002628 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2629 if (intmask & SDHCI_INT_DATA_END) {
2630 sdhci_finish_command(host);
2631 return;
2632 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302633 if (host->quirks2 &
2634 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2635 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002636 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002637
Sahitya Tummalaca422112013-02-22 12:15:54 +05302638 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002639 "though no data operation was in progress.\n",
2640 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641 sdhci_dumpregs(host);
2642
2643 return;
2644 }
2645
2646 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002647 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002648 else if (intmask & SDHCI_INT_DATA_END_BIT)
2649 host->data->error = -EILSEQ;
2650 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2651 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2652 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002653 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002654 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302655 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002656 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002657 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002658 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302659 if (host->data->error) {
2660 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2661 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2662 command = SDHCI_GET_CMD(sdhci_readw(host,
2663 SDHCI_COMMAND));
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002664 if ((command != MMC_SEND_TUNING_BLOCK_HS400) &&
2665 (command != MMC_SEND_TUNING_BLOCK_HS200) &&
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302666 (command != MMC_SEND_TUNING_BLOCK)) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302667 pr_msg = true;
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302668 if (intmask & SDHCI_INT_DATA_CRC)
2669 host->flags |= SDHCI_NEEDS_RETUNING;
2670 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302671 } else {
2672 pr_msg = true;
2673 }
2674 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302675 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302676 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302677 host->data->error, ktime_to_ms(ktime_sub(
2678 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302679 sdhci_dumpregs(host);
2680 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002681 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302682 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002683 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684 sdhci_transfer_pio(host);
2685
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002686 /*
2687 * We currently don't do anything fancy with DMA
2688 * boundaries, but as we can't disable the feature
2689 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002690 *
2691 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2692 * should return a valid address to continue from, but as
2693 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002694 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002695 if (intmask & SDHCI_INT_DMA_END) {
2696 u32 dmastart, dmanow;
2697 dmastart = sg_dma_address(host->data->sg);
2698 dmanow = dmastart + host->data->bytes_xfered;
2699 /*
2700 * Force update to the next DMA block boundary.
2701 */
2702 dmanow = (dmanow &
2703 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2704 SDHCI_DEFAULT_BOUNDARY_SIZE;
2705 host->data->bytes_xfered = dmanow - dmastart;
2706 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2707 " next 0x%08x\n",
2708 mmc_hostname(host->mmc), dmastart,
2709 host->data->bytes_xfered, dmanow);
2710 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2711 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002712
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002713 if (intmask & SDHCI_INT_DATA_END) {
2714 if (host->cmd) {
2715 /*
2716 * Data managed to finish before the
2717 * command completed. Make sure we do
2718 * things in the proper order.
2719 */
2720 host->data_early = 1;
2721 } else {
2722 sdhci_finish_data(host);
2723 }
2724 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002725 }
2726}
2727
David Howells7d12e782006-10-05 14:55:46 +01002728static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002729{
2730 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002731 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002732 u32 intmask, unexpected = 0;
2733 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002734
2735 spin_lock(&host->lock);
2736
Adrian Hunter50accb92011-10-03 15:33:34 +03002737 if (host->runtime_suspended) {
2738 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302739 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002740 mmc_hostname(host->mmc));
2741 return IRQ_HANDLED;
2742 }
2743
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002744 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002745
Mark Lord62df67a2007-03-06 13:30:13 +01002746 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002747 result = IRQ_NONE;
2748 goto out;
2749 }
2750
Alexander Stein6379b232012-03-14 09:52:10 +01002751again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002752 DBG("*** %s got interrupt: 0x%08x\n",
2753 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002754
Pierre Ossman3192a282006-06-30 02:22:26 -07002755 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302756 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2757 SDHCI_CARD_PRESENT;
2758
2759 /*
2760 * There is a observation on i.mx esdhc. INSERT bit will be
2761 * immediately set again when it gets cleared, if a card is
2762 * inserted. We have to mask the irq to prevent interrupt
2763 * storm which will freeze the system. And the REMOVE gets
2764 * the same situation.
2765 *
2766 * More testing are needed here to ensure it works for other
2767 * platforms though.
2768 */
2769 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2770 SDHCI_INT_CARD_REMOVE);
2771 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2772 SDHCI_INT_CARD_INSERT);
2773
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002774 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302775 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2776 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002777 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002778 }
2779
Pierre Ossmand129bce2006-03-24 03:18:17 -08002780 if (intmask & SDHCI_INT_CMD_MASK) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302781 if (intmask & SDHCI_INT_AUTO_CMD_ERR)
2782 host->auto_cmd_err_sts = sdhci_readw(host,
2783 SDHCI_AUTO_CMD_ERR);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002784 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2785 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002786 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2787 (host->clock <= 400000))
2788 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002789 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002790 }
2791
2792 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002793 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2794 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002795 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2796 (host->clock <= 400000))
2797 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002798 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799 }
2800
2801 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2802
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002803 intmask &= ~SDHCI_INT_ERROR;
2804
Pierre Ossmand129bce2006-03-24 03:18:17 -08002805 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302806 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002807 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002808 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002809 }
2810
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002811 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002812
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002813 if (intmask & SDHCI_INT_CARD_INT)
2814 cardint = 1;
2815
2816 intmask &= ~SDHCI_INT_CARD_INT;
2817
Pierre Ossman3192a282006-06-30 02:22:26 -07002818 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002819 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002820 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002821 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002822
2823 result = IRQ_HANDLED;
2824
Alexander Stein6379b232012-03-14 09:52:10 +01002825 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2826 if (intmask && --max_loops)
2827 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828out:
2829 spin_unlock(&host->lock);
2830
Alexander Stein6379b232012-03-14 09:52:10 +01002831 if (unexpected) {
2832 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2833 mmc_hostname(host->mmc), unexpected);
2834 sdhci_dumpregs(host);
2835 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002836 /*
2837 * We have to delay this as it calls back into the driver.
2838 */
2839 if (cardint)
2840 mmc_signal_sdio_irq(host->mmc);
2841
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842 return result;
2843}
2844
2845/*****************************************************************************\
2846 * *
2847 * Suspend/resume *
2848 * *
2849\*****************************************************************************/
2850
2851#ifdef CONFIG_PM
2852
Manuel Laussd72faa62011-11-03 11:09:45 +01002853int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302856 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857
Chris Balla1b13b42012-02-06 00:43:59 -05002858 if (host->ops->platform_suspend)
2859 host->ops->platform_suspend(host);
2860
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002861 sdhci_disable_card_detection(host);
2862
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302863 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302864 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2865 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2866 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002867 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302868 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302869 }
2870
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002871 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302872 if (ret) {
2873 if (has_tuning_timer) {
2874 host->flags |= SDHCI_NEEDS_RETUNING;
2875 mod_timer(&host->tuning_timer, jiffies +
2876 host->tuning_count * HZ);
2877 }
2878
2879 sdhci_enable_card_detection(host);
2880
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002881 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302882 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002883
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002884 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002885
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002886 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002887}
2888
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002889EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002891int sdhci_resume_host(struct sdhci_host *host)
2892{
2893 int ret;
2894
Richard Röjforsa13abc72009-09-22 16:45:30 -07002895 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002896 if (host->ops->enable_dma)
2897 host->ops->enable_dma(host);
2898 }
2899
2900 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2901 mmc_hostname(host->mmc), host);
2902 if (ret)
2903 return ret;
2904
Adrian Hunter6308d292012-02-07 14:48:54 +02002905 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2906 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2907 /* Card keeps power but host controller does not */
2908 sdhci_init(host, 0);
2909 host->pwr = 0;
2910 host->clock = 0;
2911 sdhci_do_set_ios(host, &host->mmc->ios);
2912 } else {
2913 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2914 mmiowb();
2915 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002916
2917 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002918 sdhci_enable_card_detection(host);
2919
Chris Balla1b13b42012-02-06 00:43:59 -05002920 if (host->ops->platform_resume)
2921 host->ops->platform_resume(host);
2922
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302923 /* Set the re-tuning expiration flag */
2924 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2925 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2926 host->flags |= SDHCI_NEEDS_RETUNING;
2927
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002928 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002929}
2930
2931EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002932
Daniel Drake5f619702010-11-04 22:20:39 +00002933void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2934{
2935 u8 val;
2936 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2937 val |= SDHCI_WAKE_ON_INT;
2938 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2939}
2940
2941EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2942
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943#endif /* CONFIG_PM */
2944
Adrian Hunter50accb92011-10-03 15:33:34 +03002945#ifdef CONFIG_PM_RUNTIME
2946
2947static int sdhci_runtime_pm_get(struct sdhci_host *host)
2948{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302949 if (!mmc_use_core_runtime_pm(host->mmc))
2950 return pm_runtime_get_sync(host->mmc->parent);
2951 else
2952 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002953}
2954
2955static int sdhci_runtime_pm_put(struct sdhci_host *host)
2956{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302957 if (!mmc_use_core_runtime_pm(host->mmc)) {
2958 pm_runtime_mark_last_busy(host->mmc->parent);
2959 return pm_runtime_put_autosuspend(host->mmc->parent);
2960 } else {
2961 return 0;
2962 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002963}
2964
2965int sdhci_runtime_suspend_host(struct sdhci_host *host)
2966{
2967 unsigned long flags;
2968 int ret = 0;
2969
2970 /* Disable tuning since we are suspending */
2971 if (host->version >= SDHCI_SPEC_300 &&
2972 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2973 del_timer_sync(&host->tuning_timer);
2974 host->flags &= ~SDHCI_NEEDS_RETUNING;
2975 }
2976
2977 spin_lock_irqsave(&host->lock, flags);
2978 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2979 spin_unlock_irqrestore(&host->lock, flags);
2980
2981 synchronize_irq(host->irq);
2982
2983 spin_lock_irqsave(&host->lock, flags);
2984 host->runtime_suspended = true;
2985 spin_unlock_irqrestore(&host->lock, flags);
2986
2987 return ret;
2988}
2989EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2990
2991int sdhci_runtime_resume_host(struct sdhci_host *host)
2992{
2993 unsigned long flags;
2994 int ret = 0, host_flags = host->flags;
2995
2996 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2997 if (host->ops->enable_dma)
2998 host->ops->enable_dma(host);
2999 }
3000
3001 sdhci_init(host, 0);
3002
3003 /* Force clock and power re-program */
3004 host->pwr = 0;
3005 host->clock = 0;
3006 sdhci_do_set_ios(host, &host->mmc->ios);
3007
3008 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
3009 if (host_flags & SDHCI_PV_ENABLED)
3010 sdhci_do_enable_preset_value(host, true);
3011
3012 /* Set the re-tuning expiration flag */
3013 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
3014 (host->tuning_mode == SDHCI_TUNING_MODE_1))
3015 host->flags |= SDHCI_NEEDS_RETUNING;
3016
3017 spin_lock_irqsave(&host->lock, flags);
3018
3019 host->runtime_suspended = false;
3020
3021 /* Enable SDIO IRQ */
3022 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
3023 sdhci_enable_sdio_irq_nolock(host, true);
3024
3025 /* Enable Card Detection */
3026 sdhci_enable_card_detection(host);
3027
3028 spin_unlock_irqrestore(&host->lock, flags);
3029
3030 return ret;
3031}
3032EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
3033
3034#endif
3035
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036/*****************************************************************************\
3037 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003038 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003039 * *
3040\*****************************************************************************/
3041
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003042struct sdhci_host *sdhci_alloc_host(struct device *dev,
3043 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003045 struct mmc_host *mmc;
3046 struct sdhci_host *host;
3047
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003048 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003049
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003050 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003051 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003052 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053
3054 host = mmc_priv(mmc);
3055 host->mmc = mmc;
3056
Sahitya Tummala951c1202013-05-24 08:47:26 +05303057 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05303058 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05303059
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003060 return host;
3061}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003062
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003063EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003065int sdhci_add_host(struct sdhci_host *host)
3066{
3067 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303068 u32 caps[2];
3069 u32 max_current_caps;
3070 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003071 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003072
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003073 WARN_ON(host == NULL);
3074 if (host == NULL)
3075 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003076
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003077 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003078
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003079 if (debug_quirks)
3080 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03003081 if (debug_quirks2)
3082 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003083
Pierre Ossmand96649e2006-06-30 02:22:30 -07003084 sdhci_reset(host, SDHCI_RESET_ALL);
3085
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003086 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02003087 host->version = (host->version & SDHCI_SPEC_VER_MASK)
3088 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08003089 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303090 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003091 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02003092 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003093 }
3094
Arindam Nathf2119df2011-05-05 12:18:57 +05303095 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07003096 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003097
Arindam Nathf2119df2011-05-05 12:18:57 +05303098 caps[1] = (host->version >= SDHCI_SPEC_300) ?
3099 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
3100
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003101 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003102 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05303103 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003104 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003105 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003106 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003107
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003108 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003109 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003110 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003111 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003112 }
3113
Arindam Nathf2119df2011-05-05 12:18:57 +05303114 if ((host->version >= SDHCI_SPEC_200) &&
3115 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003116 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003117
3118 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3119 (host->flags & SDHCI_USE_ADMA)) {
3120 DBG("Disabling ADMA as it is marked broken\n");
3121 host->flags &= ~SDHCI_USE_ADMA;
3122 }
3123
Richard Röjforsa13abc72009-09-22 16:45:30 -07003124 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003125 if (host->ops->enable_dma) {
3126 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303127 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003128 "available. Falling back to PIO.\n",
3129 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003130 host->flags &=
3131 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003132 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003133 }
3134 }
3135
Pierre Ossman2134a922008-06-28 18:28:51 +02003136 if (host->flags & SDHCI_USE_ADMA) {
3137 /*
3138 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303139 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02003140 * each of those entries.
3141 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303142 if (host->ops->get_max_segments)
3143 host->adma_max_desc = host->ops->get_max_segments();
3144 else
3145 host->adma_max_desc = 128;
3146
3147 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
3148 host->align_buf_sz = host->adma_max_desc * 4;
3149
3150 pr_debug("%s: %s: dma_desc_size: %d\n",
3151 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3152 host->adma_desc = kmalloc(host->adma_desc_sz,
3153 GFP_KERNEL);
3154 host->align_buffer = kmalloc(host->align_buf_sz,
3155 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003156 if (!host->adma_desc || !host->align_buffer) {
3157 kfree(host->adma_desc);
3158 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303159 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003160 "buffers. Falling back to standard DMA.\n",
3161 mmc_hostname(mmc));
3162 host->flags &= ~SDHCI_USE_ADMA;
3163 }
3164 }
3165
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003166 host->next_data.cookie = 1;
3167
Pierre Ossman76591502008-07-21 00:32:11 +02003168 /*
3169 * If we use DMA, then it's up to the caller to set the DMA
3170 * mask, but PIO does not need the hw shim so we set a new
3171 * mask here in that case.
3172 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003173 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003174 host->dma_mask = DMA_BIT_MASK(64);
3175 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3176 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003177
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003178 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303179 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003180 >> SDHCI_CLOCK_BASE_SHIFT;
3181 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303182 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003183 >> SDHCI_CLOCK_BASE_SHIFT;
3184
Pierre Ossmand129bce2006-03-24 03:18:17 -08003185 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003186 if (host->max_clk == 0 || host->quirks &
3187 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003188 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303189 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003190 "frequency.\n", mmc_hostname(mmc));
3191 return -ENODEV;
3192 }
3193 host->max_clk = host->ops->get_max_clock(host);
3194 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003195
3196 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303197 * In case of Host Controller v3.00, find out whether clock
3198 * multiplier is supported.
3199 */
3200 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3201 SDHCI_CLOCK_MUL_SHIFT;
3202
3203 /*
3204 * In case the value in Clock Multiplier is 0, then programmable
3205 * clock mode is not supported, otherwise the actual clock
3206 * multiplier is one more than the value of Clock Multiplier
3207 * in the Capabilities Register.
3208 */
3209 if (host->clk_mul)
3210 host->clk_mul += 1;
3211
3212 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003213 * Set host parameters.
3214 */
3215 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303216 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003217 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003218 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303219 else if (host->version >= SDHCI_SPEC_300) {
3220 if (host->clk_mul) {
3221 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3222 mmc->f_max = host->max_clk * host->clk_mul;
3223 } else
3224 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3225 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003226 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003227
Sahitya Tummalaca422112013-02-22 12:15:54 +05303228 host->timeout_clk =
3229 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3230 if (host->timeout_clk == 0) {
3231 if (host->ops->get_timeout_clock) {
3232 host->timeout_clk = host->ops->get_timeout_clock(host);
3233 } else if (!(host->quirks &
3234 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3235 pr_err("%s: Hardware doesn't specify timeout clock "
3236 "frequency.\n", mmc_hostname(mmc));
3237 return -ENODEV;
3238 }
3239 }
3240 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3241 host->timeout_clk *= 1000;
3242
3243 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3244 host->timeout_clk = mmc->f_max / 1000;
3245
Krishna Kondaa20d3362013-04-01 21:01:59 -07003246 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3247 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303248
Andrei Warkentine89d4562011-05-23 15:06:37 -05003249 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3250
3251 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3252 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003253
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003254 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003255 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003256 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003257 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003258 host->flags |= SDHCI_AUTO_CMD23;
3259 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3260 } else {
3261 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3262 }
3263
Philip Rakity15ec4462010-11-19 16:48:39 -05003264 /*
3265 * A controller may support 8-bit width, but the board itself
3266 * might not have the pins brought out. Boards that support
3267 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3268 * their platform code before calling sdhci_add_host(), and we
3269 * won't assume 8-bit width for hosts without that CAP.
3270 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003271 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003272 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273
Arindam Nathf2119df2011-05-05 12:18:57 +05303274 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003275 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003276
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003277 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3278 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003279 mmc->caps |= MMC_CAP_NEEDS_POLL;
3280
Al Cooper4188bba2012-03-16 15:54:17 -04003281 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3282 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3283 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303284 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3285
3286 /* SDR104 supports also implies SDR50 support */
3287 if (caps[1] & SDHCI_SUPPORT_SDR104)
3288 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3289 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3290 mmc->caps |= MMC_CAP_UHS_SDR50;
3291
3292 if (caps[1] & SDHCI_SUPPORT_DDR50)
3293 mmc->caps |= MMC_CAP_UHS_DDR50;
3294
Girish K S2cd06dc2012-01-06 09:56:39 +05303295 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303296 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3297 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3298
Girish K S2cd06dc2012-01-06 09:56:39 +05303299 /* Does the host need tuning for HS200? */
3300 if (mmc->caps2 & MMC_CAP2_HS200)
3301 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3302
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07003303 /* Does the host need tuning for HS400? */
3304 if (mmc->caps2 & MMC_CAP2_HS400)
3305 host->flags |= SDHCI_HS400_NEEDS_TUNING;
3306
Arindam Nathd6d50a12011-05-05 12:18:59 +05303307 /* Driver Type(s) (A, C, D) supported by the host */
3308 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3309 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3310 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3311 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3312 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3313 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3314
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003315 /* Initial value for re-tuning timer count */
3316 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3317 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3318
3319 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303320 * In case Re-tuning Timer is not disabled, the actual value of
3321 * re-tuning timer will be 2 ^ (n - 1).
3322 */
3323 if (host->tuning_count)
3324 host->tuning_count = 1 << (host->tuning_count - 1);
3325
3326 /* Re-tuning mode supported by the Host Controller */
3327 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3328 SDHCI_RETUNING_MODE_SHIFT;
3329
Takashi Iwai8f230f42010-12-08 10:04:30 +01003330 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303331 /*
3332 * According to SD Host Controller spec v3.00, if the Host System
3333 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3334 * the value is meaningful only if Voltage Support in the Capabilities
3335 * register is set. The actual current value is 4 times the register
3336 * value.
3337 */
3338 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3339
3340 if (caps[0] & SDHCI_CAN_VDD_330) {
3341 int max_current_330;
3342
Takashi Iwai8f230f42010-12-08 10:04:30 +01003343 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303344
3345 max_current_330 = ((max_current_caps &
3346 SDHCI_MAX_CURRENT_330_MASK) >>
3347 SDHCI_MAX_CURRENT_330_SHIFT) *
3348 SDHCI_MAX_CURRENT_MULTIPLIER;
3349
3350 if (max_current_330 > 150)
3351 mmc->caps |= MMC_CAP_SET_XPC_330;
3352 }
3353 if (caps[0] & SDHCI_CAN_VDD_300) {
3354 int max_current_300;
3355
Takashi Iwai8f230f42010-12-08 10:04:30 +01003356 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303357
3358 max_current_300 = ((max_current_caps &
3359 SDHCI_MAX_CURRENT_300_MASK) >>
3360 SDHCI_MAX_CURRENT_300_SHIFT) *
3361 SDHCI_MAX_CURRENT_MULTIPLIER;
3362
3363 if (max_current_300 > 150)
3364 mmc->caps |= MMC_CAP_SET_XPC_300;
3365 }
3366 if (caps[0] & SDHCI_CAN_VDD_180) {
3367 int max_current_180;
3368
Takashi Iwai8f230f42010-12-08 10:04:30 +01003369 ocr_avail |= MMC_VDD_165_195;
3370
Arindam Nathf2119df2011-05-05 12:18:57 +05303371 max_current_180 = ((max_current_caps &
3372 SDHCI_MAX_CURRENT_180_MASK) >>
3373 SDHCI_MAX_CURRENT_180_SHIFT) *
3374 SDHCI_MAX_CURRENT_MULTIPLIER;
3375
3376 if (max_current_180 > 150)
3377 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303378
3379 /* Maximum current capabilities of the host at 1.8V */
3380 if (max_current_180 >= 800)
3381 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3382 else if (max_current_180 >= 600)
3383 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3384 else if (max_current_180 >= 400)
3385 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3386 else
3387 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303388 }
3389
Takashi Iwai8f230f42010-12-08 10:04:30 +01003390 mmc->ocr_avail = ocr_avail;
3391 mmc->ocr_avail_sdio = ocr_avail;
3392 if (host->ocr_avail_sdio)
3393 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3394 mmc->ocr_avail_sd = ocr_avail;
3395 if (host->ocr_avail_sd)
3396 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3397 else /* normal SD controllers don't support 1.8V */
3398 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3399 mmc->ocr_avail_mmc = ocr_avail;
3400 if (host->ocr_avail_mmc)
3401 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003402
3403 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303404 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003405 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003406 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003407 }
3408
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003410 * Maximum number of segments. Depends on if the hardware
3411 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003412 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003413 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303414 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003415 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003416 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303417 else/* PIO */
3418 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003419
3420 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003421 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303422 * size (512KiB), unless specified by platform specific driver. Each
3423 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003424 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303425 if (host->ops->get_max_segments)
3426 mmc->max_req_size = (host->adma_max_desc * 65536);
3427 else
3428 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003429
3430 /*
3431 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003432 * of bytes. When doing hardware scatter/gather, each entry cannot
3433 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003434 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003435 if (host->flags & SDHCI_USE_ADMA) {
3436 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3437 mmc->max_seg_size = 65535;
3438 else
3439 mmc->max_seg_size = 65536;
3440 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003441 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003442 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003443
3444 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003445 * Maximum block size. This varies from controller to controller and
3446 * is specified in the capabilities register.
3447 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003448 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3449 mmc->max_blk_size = 2;
3450 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303451 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003452 SDHCI_MAX_BLOCK_SHIFT;
3453 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303454 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003455 "assuming 512 bytes\n", mmc_hostname(mmc));
3456 mmc->max_blk_size = 0;
3457 }
3458 }
3459
3460 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003461
3462 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003463 * Maximum block count.
3464 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003465 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003466
3467 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003468 * Init tasklets.
3469 */
3470 tasklet_init(&host->card_tasklet,
3471 sdhci_tasklet_card, (unsigned long)host);
3472 tasklet_init(&host->finish_tasklet,
3473 sdhci_tasklet_finish, (unsigned long)host);
3474
Al Viroe4cad1b2006-10-10 22:47:07 +01003475 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003476
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303477 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303478 init_waitqueue_head(&host->buf_ready_int);
3479
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303480 /* Initialize re-tuning timer */
3481 init_timer(&host->tuning_timer);
3482 host->tuning_timer.data = (unsigned long)host;
3483 host->tuning_timer.function = sdhci_tuning_timer;
3484 }
3485
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003486 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003487 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003488 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003489 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003491 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3492 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303493 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003494 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003495 }
3496
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003497 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003498
3499#ifdef CONFIG_MMC_DEBUG
3500 sdhci_dumpregs(host);
3501#endif
3502
Pierre Ossmanf9134312008-12-21 17:01:48 +01003503#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003504 snprintf(host->led_name, sizeof(host->led_name),
3505 "%s::", mmc_hostname(mmc));
3506 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003507 host->led.brightness = LED_OFF;
3508 host->led.default_trigger = mmc_hostname(mmc);
3509 host->led.brightness_set = sdhci_led_control;
3510
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003511 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003512 if (ret)
3513 goto reset;
3514#endif
3515
Pierre Ossman5f25a662006-10-04 02:15:39 -07003516 mmiowb();
3517
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303518 if (host->cpu_dma_latency_us) {
3519 host->pm_qos_timeout_us = 10000; /* default value */
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303520 pm_qos_add_request(&host->pm_qos_req_dma,
3521 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303522
3523 host->pm_qos_tout.show = show_sdhci_pm_qos_tout;
3524 host->pm_qos_tout.store = store_sdhci_pm_qos_tout;
3525 sysfs_attr_init(&host->pm_qos_tout.attr);
3526 host->pm_qos_tout.attr.name = "pm_qos_unvote_delay";
3527 host->pm_qos_tout.attr.mode = S_IRUGO | S_IWUSR;
3528 ret = device_create_file(mmc_dev(mmc), &host->pm_qos_tout);
3529 if (ret)
3530 pr_err("%s: cannot create pm_qos_unvote_delay %d\n",
3531 mmc_hostname(mmc), ret);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303532 }
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303533
Pierre Ossmand129bce2006-03-24 03:18:17 -08003534 mmc_add_host(mmc);
3535
Sahitya Tummalaca422112013-02-22 12:15:54 +05303536 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003537 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003538 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3539 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003540
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003541 sdhci_enable_card_detection(host);
3542
Pierre Ossmand129bce2006-03-24 03:18:17 -08003543 return 0;
3544
Pierre Ossmanf9134312008-12-21 17:01:48 +01003545#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003546reset:
3547 sdhci_reset(host, SDHCI_RESET_ALL);
3548 free_irq(host->irq, host);
3549#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003550untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003551 tasklet_kill(&host->card_tasklet);
3552 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003553
3554 return ret;
3555}
3556
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003557EXPORT_SYMBOL_GPL(sdhci_add_host);
3558
Pierre Ossman1e728592008-04-16 19:13:13 +02003559void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003560{
Pierre Ossman1e728592008-04-16 19:13:13 +02003561 unsigned long flags;
3562
3563 if (dead) {
3564 spin_lock_irqsave(&host->lock, flags);
3565
3566 host->flags |= SDHCI_DEVICE_DEAD;
3567
3568 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303569 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003570 " transfer!\n", mmc_hostname(host->mmc));
3571
3572 host->mrq->cmd->error = -ENOMEDIUM;
3573 tasklet_schedule(&host->finish_tasklet);
3574 }
3575
3576 spin_unlock_irqrestore(&host->lock, flags);
3577 }
3578
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003579 sdhci_disable_card_detection(host);
3580
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303581 if (host->cpu_dma_latency_us)
3582 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003583 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003584
Pierre Ossmanf9134312008-12-21 17:01:48 +01003585#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003586 led_classdev_unregister(&host->led);
3587#endif
3588
Pierre Ossman1e728592008-04-16 19:13:13 +02003589 if (!dead)
3590 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003591
3592 free_irq(host->irq, host);
3593
3594 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303595 if (host->version >= SDHCI_SPEC_300)
3596 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003597
3598 tasklet_kill(&host->card_tasklet);
3599 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003600
Sahitya Tummalaca422112013-02-22 12:15:54 +05303601 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003602 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003603
Pierre Ossman2134a922008-06-28 18:28:51 +02003604 kfree(host->adma_desc);
3605 kfree(host->align_buffer);
3606
3607 host->adma_desc = NULL;
3608 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003609}
3610
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003611EXPORT_SYMBOL_GPL(sdhci_remove_host);
3612
3613void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003614{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003615 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003616}
3617
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003618EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003619
3620/*****************************************************************************\
3621 * *
3622 * Driver init/exit *
3623 * *
3624\*****************************************************************************/
3625
3626static int __init sdhci_drv_init(void)
3627{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303628 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003629 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303630 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003631
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003632 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003633}
3634
3635static void __exit sdhci_drv_exit(void)
3636{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003637}
3638
3639module_init(sdhci_drv_init);
3640module_exit(sdhci_drv_exit);
3641
Pierre Ossmandf673b22006-06-30 02:22:31 -07003642module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003643module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003644
Pierre Ossman32710e82009-04-08 20:14:54 +02003645MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003646MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003647MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003648
Pierre Ossmandf673b22006-06-30 02:22:31 -07003649MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003650MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");