blob: b01abe7a2319927a411df26d94656873cc39c163 [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;
1495
1496 host = mmc_priv(mmc);
1497
Adrian Hunter50accb92011-10-03 15:33:34 +03001498 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301499 if (sdhci_check_state(host)) {
1500 sdhci_dump_state(host);
1501 WARN(1, "sdhci in bad state");
1502 mrq->cmd->error = -EIO;
1503 if (mrq->data)
1504 mrq->data->error = -EIO;
1505 tasklet_schedule(&host->finish_tasklet);
1506 return;
1507 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001508
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509 spin_lock_irqsave(&host->lock, flags);
1510
1511 WARN_ON(host->mrq != NULL);
1512
Pierre Ossmanf9134312008-12-21 17:01:48 +01001513#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001515#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001516
1517 /*
1518 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1519 * requests if Auto-CMD12 is enabled.
1520 */
1521 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001522 if (mrq->stop) {
1523 mrq->data->stop = NULL;
1524 mrq->stop = NULL;
1525 }
1526 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001527
1528 host->mrq = mrq;
1529
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001530 /* If polling, assume that the card is always present. */
1531 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1532 present = true;
1533 else
1534 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1535 SDHCI_CARD_PRESENT;
1536
1537 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001538 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001539 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301540 } else {
1541 u32 present_state;
1542
1543 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1544 /*
1545 * Check if the re-tuning timer has already expired and there
1546 * is no on-going data transfer. If so, we need to execute
1547 * tuning procedure before sending command.
1548 */
1549 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1550 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1551 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301552 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301553 spin_lock_irqsave(&host->lock, flags);
1554
1555 /* Restore original mmc_request structure */
1556 host->mrq = mrq;
1557 }
1558
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001559 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001560 sdhci_send_command(host, mrq->sbc);
1561 else
1562 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301563 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001564
Pierre Ossman5f25a662006-10-04 02:15:39 -07001565 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566 spin_unlock_irqrestore(&host->lock, flags);
1567}
1568
Adrian Hunter50accb92011-10-03 15:33:34 +03001569static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301572 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001573 u8 ctrl;
1574
Sahitya Tummala40474e42013-07-10 14:40:37 +05301575 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301576 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301577 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1578 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301579 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301580 return;
1581 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001582
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301583 if (ios->clock)
1584 sdhci_set_clock(host, ios->clock);
1585
1586 spin_lock_irqsave(&host->lock, flags);
1587 if (!host->clock) {
1588 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301589 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301590 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001591 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301592 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301594 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301595 vdd_bit = sdhci_set_power(host, ios->vdd);
1596
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301597 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301598 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301599
1600 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601
Philip Rakity643a81f2010-09-23 08:24:32 -07001602 if (host->ops->platform_send_init_74_clocks)
1603 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1604
Philip Rakity15ec4462010-11-19 16:48:39 -05001605 /*
1606 * If your platform has 8-bit width support but is not a v3 controller,
1607 * or if it requires special setup code, you should implement that in
1608 * platform_8bit_width().
1609 */
1610 if (host->ops->platform_8bit_width)
1611 host->ops->platform_8bit_width(host, ios->bus_width);
1612 else {
1613 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1614 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1615 ctrl &= ~SDHCI_CTRL_4BITBUS;
1616 if (host->version >= SDHCI_SPEC_300)
1617 ctrl |= SDHCI_CTRL_8BITBUS;
1618 } else {
1619 if (host->version >= SDHCI_SPEC_300)
1620 ctrl &= ~SDHCI_CTRL_8BITBUS;
1621 if (ios->bus_width == MMC_BUS_WIDTH_4)
1622 ctrl |= SDHCI_CTRL_4BITBUS;
1623 else
1624 ctrl &= ~SDHCI_CTRL_4BITBUS;
1625 }
1626 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1627 }
1628
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001629 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001630
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001631 if ((ios->timing == MMC_TIMING_SD_HS ||
1632 ios->timing == MMC_TIMING_MMC_HS)
1633 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001634 ctrl |= SDHCI_CTRL_HISPD;
1635 else
1636 ctrl &= ~SDHCI_CTRL_HISPD;
1637
Arindam Nathd6d50a12011-05-05 12:18:59 +05301638 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301639 u16 clk, ctrl_2;
1640 unsigned int clock;
1641
1642 /* In case of UHS-I modes, set High Speed Enable */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001643 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1644 (ios->timing == MMC_TIMING_MMC_HS200) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301645 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301646 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1647 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001648 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301649 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301650
1651 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1652 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301653 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301654 /*
1655 * We only need to set Driver Strength if the
1656 * preset value enable is not set.
1657 */
1658 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1659 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1660 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1661 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1662 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1663
1664 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301665 } else {
1666 /*
1667 * According to SDHC Spec v3.00, if the Preset Value
1668 * Enable in the Host Control 2 register is set, we
1669 * need to reset SD Clock Enable before changing High
1670 * Speed Enable to avoid generating clock gliches.
1671 */
Arindam Nath758535c2011-05-05 12:19:00 +05301672
1673 /* Reset SD Clock Enable */
1674 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1675 clk &= ~SDHCI_CLOCK_CARD_EN;
1676 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1677
1678 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1679
1680 /* Re-enable SD Clock */
1681 clock = host->clock;
1682 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301683 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301684 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301685 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301686 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301687
Arindam Nath49c468f2011-05-05 12:19:01 +05301688 /* Reset SD Clock Enable */
1689 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1690 clk &= ~SDHCI_CLOCK_CARD_EN;
1691 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1692
Philip Rakity6322cdd2011-05-13 11:17:15 +05301693 if (host->ops->set_uhs_signaling)
1694 host->ops->set_uhs_signaling(host, ios->timing);
1695 else {
1696 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1697 /* Select Bus Speed Mode for host */
1698 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001699 if (ios->timing == MMC_TIMING_MMC_HS400)
1700 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1701 else if (ios->timing == MMC_TIMING_MMC_HS200)
Girish K S2cd06dc2012-01-06 09:56:39 +05301702 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1703 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301704 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1705 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1706 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1707 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1708 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1709 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1710 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1711 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1712 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1713 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1714 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301715
1716 /* Re-enable SD Clock */
1717 clock = host->clock;
1718 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301719 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301720 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301721 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301722 } else
1723 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301724
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301725 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001726 /*
1727 * Some (ENE) controllers go apeshit on some ios operation,
1728 * signalling timeout and CRC errors even on CMD0. Resetting
1729 * it on each ios seems to solve the problem.
1730 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001731 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001732 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1733
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301734 /*
1735 * Reset the chip on each power off.
1736 * Should clear out any weird states.
1737 */
1738 if (ios->power_mode == MMC_POWER_OFF) {
1739 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1740 sdhci_reinit(host);
1741 vdd_bit = sdhci_set_power(host, -1);
1742 if (host->vmmc && vdd_bit != -1)
1743 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1744 }
1745 if (!ios->clock)
1746 sdhci_set_clock(host, ios->clock);
1747
Pierre Ossman5f25a662006-10-04 02:15:39 -07001748 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301749 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750}
1751
Adrian Hunter50accb92011-10-03 15:33:34 +03001752static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1753{
1754 struct sdhci_host *host = mmc_priv(mmc);
1755
1756 sdhci_runtime_pm_get(host);
1757 sdhci_do_set_ios(host, ios);
1758 sdhci_runtime_pm_put(host);
1759}
1760
1761static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001762{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001764 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765
Pierre Ossmand129bce2006-03-24 03:18:17 -08001766 spin_lock_irqsave(&host->lock, flags);
1767
Pierre Ossman1e728592008-04-16 19:13:13 +02001768 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001769 is_readonly = 0;
1770 else if (host->ops->get_ro)
1771 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001772 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001773 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1774 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001775
1776 spin_unlock_irqrestore(&host->lock, flags);
1777
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001778 /* This quirk needs to be replaced by a callback-function later */
1779 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1780 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001781}
1782
Takashi Iwai82b0e232011-04-21 20:26:38 +02001783#define SAMPLE_COUNT 5
1784
Adrian Hunter50accb92011-10-03 15:33:34 +03001785static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001786{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001787 int i, ro_count;
1788
Takashi Iwai82b0e232011-04-21 20:26:38 +02001789 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001790 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001791
1792 ro_count = 0;
1793 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001794 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001795 if (++ro_count > SAMPLE_COUNT / 2)
1796 return 1;
1797 }
1798 msleep(30);
1799 }
1800 return 0;
1801}
1802
Adrian Hunter50accb92011-10-03 15:33:34 +03001803static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001804{
Adrian Hunter50accb92011-10-03 15:33:34 +03001805 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001806
Adrian Hunter50accb92011-10-03 15:33:34 +03001807 if (host->ops && host->ops->hw_reset)
1808 host->ops->hw_reset(host);
1809}
Adrian Hunter20758b62011-08-29 16:42:12 +03001810
Adrian Hunter50accb92011-10-03 15:33:34 +03001811static int sdhci_get_ro(struct mmc_host *mmc)
1812{
1813 struct sdhci_host *host = mmc_priv(mmc);
1814 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001815
Adrian Hunter50accb92011-10-03 15:33:34 +03001816 sdhci_runtime_pm_get(host);
1817 ret = sdhci_do_get_ro(host);
1818 sdhci_runtime_pm_put(host);
1819 return ret;
1820}
1821
1822static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1823{
Pierre Ossman1e728592008-04-16 19:13:13 +02001824 if (host->flags & SDHCI_DEVICE_DEAD)
1825 goto out;
1826
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001827 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001828 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1829 else
1830 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1831
1832 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1833 if (host->runtime_suspended)
1834 goto out;
1835
1836 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001837 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1838 else
1839 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001840out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001841 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001842}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001843
Adrian Hunter50accb92011-10-03 15:33:34 +03001844static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1845{
1846 struct sdhci_host *host = mmc_priv(mmc);
1847 unsigned long flags;
1848
1849 spin_lock_irqsave(&host->lock, flags);
1850 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001851 spin_unlock_irqrestore(&host->lock, flags);
1852}
1853
Adrian Hunter50accb92011-10-03 15:33:34 +03001854static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1855 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301856{
Arindam Nathf2119df2011-05-05 12:18:57 +05301857 u8 pwr;
1858 u16 clk, ctrl;
1859 u32 present_state;
1860
Arindam Nathf2119df2011-05-05 12:18:57 +05301861 /*
1862 * Signal Voltage Switching is only applicable for Host Controllers
1863 * v3.00 and above.
1864 */
1865 if (host->version < SDHCI_SPEC_300)
1866 return 0;
1867
1868 /*
1869 * We first check whether the request is to set signalling voltage
1870 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1871 */
1872 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1873 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1874 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1875 ctrl &= ~SDHCI_CTRL_VDD_180;
1876 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301877 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301878 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301879
1880 /* Wait for 5ms */
1881 usleep_range(5000, 5500);
1882
1883 /* 3.3V regulator output should be stable within 5 ms */
1884 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1885 if (!(ctrl & SDHCI_CTRL_VDD_180))
1886 return 0;
1887 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301888 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301889 "signalling voltage failed\n");
1890 return -EIO;
1891 }
1892 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1893 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1894 /* Stop SDCLK */
1895 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1896 clk &= ~SDHCI_CLOCK_CARD_EN;
1897 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1898
1899 /* Check whether DAT[3:0] is 0000 */
1900 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1901 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1902 SDHCI_DATA_LVL_SHIFT)) {
1903 /*
1904 * Enable 1.8V Signal Enable in the Host Control2
1905 * register
1906 */
1907 ctrl |= SDHCI_CTRL_VDD_180;
1908 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301909 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301910 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301911
1912 /* Wait for 5ms */
1913 usleep_range(5000, 5500);
1914
1915 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1916 if (ctrl & SDHCI_CTRL_VDD_180) {
1917 /* Provide SDCLK again and wait for 1ms*/
1918 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1919 clk |= SDHCI_CLOCK_CARD_EN;
1920 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1921 usleep_range(1000, 1500);
1922
1923 /*
1924 * If DAT[3:0] level is 1111b, then the card
1925 * was successfully switched to 1.8V signaling.
1926 */
1927 present_state = sdhci_readl(host,
1928 SDHCI_PRESENT_STATE);
1929 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1930 SDHCI_DATA_LVL_MASK)
1931 return 0;
1932 }
1933 }
1934
1935 /*
1936 * If we are here, that means the switch to 1.8V signaling
1937 * failed. We power cycle the card, and retry initialization
1938 * sequence by setting S18R to 0.
1939 */
1940 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1941 pwr &= ~SDHCI_POWER_ON;
1942 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301943 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301944 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301945
1946 /* Wait for 1ms as per the spec */
1947 usleep_range(1000, 1500);
1948 pwr |= SDHCI_POWER_ON;
1949 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301950 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301951 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301952
Sahitya Tummalaca422112013-02-22 12:15:54 +05301953 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301954 "voltage failed, retrying with S18R set to 0\n");
1955 return -EAGAIN;
1956 } else
1957 /* No signal voltage switch required */
1958 return 0;
1959}
1960
Adrian Hunter50accb92011-10-03 15:33:34 +03001961static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1962 struct mmc_ios *ios)
1963{
1964 struct sdhci_host *host = mmc_priv(mmc);
1965 int err;
1966
1967 if (host->version < SDHCI_SPEC_300)
1968 return 0;
1969 sdhci_runtime_pm_get(host);
1970 err = sdhci_do_start_signal_voltage_switch(host, ios);
1971 sdhci_runtime_pm_put(host);
1972 return err;
1973}
1974
Girish K S2cd06dc2012-01-06 09:56:39 +05301975static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301976{
1977 struct sdhci_host *host;
1978 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301979 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301980 int tuning_loop_counter = MAX_TUNING_LOOP;
1981 unsigned long timeout;
1982 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301983 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301984
1985 host = mmc_priv(mmc);
1986
Adrian Hunter50accb92011-10-03 15:33:34 +03001987 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301988 disable_irq(host->irq);
1989 spin_lock(&host->lock);
1990
1991 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1992
1993 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301994 * The Host Controller needs tuning only in case of SDR104 mode
1995 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301996 * Capabilities register.
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001997 * If the Host Controller supports the HS400/HS200 mode then the
Girish K S2cd06dc2012-01-06 09:56:39 +05301998 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301999 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08002000 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
2001 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002002 (host->flags & SDHCI_HS200_NEEDS_TUNING) ||
2003 (host->flags & SDHCI_HS400_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05302004 requires_tuning_nonuhs = true;
2005
Arindam Nathb513ea22011-05-05 12:19:04 +05302006 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05302007 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05302008 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2009 else {
2010 spin_unlock(&host->lock);
2011 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002012 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302013 return 0;
2014 }
2015
Asutosh Das8ddd3482013-01-04 11:45:46 +05302016 if (host->ops->execute_tuning) {
2017 spin_unlock(&host->lock);
2018 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05302019 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05302020 disable_irq(host->irq);
2021 spin_lock(&host->lock);
2022 goto out;
2023 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302024 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2025
2026 /*
2027 * As per the Host Controller spec v3.00, tuning command
2028 * generates Buffer Read Ready interrupt, so enable that.
2029 *
2030 * Note: The spec clearly says that when tuning sequence
2031 * is being performed, the controller does not generate
2032 * interrupts other than Buffer Read Ready interrupt. But
2033 * to make sure we don't hit a controller bug, we _only_
2034 * enable Buffer Read Ready interrupt here.
2035 */
2036 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
2037 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
2038
2039 /*
2040 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
2041 * of loops reaches 40 times or a timeout of 150ms occurs.
2042 */
2043 timeout = 150;
2044 do {
2045 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03002046 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302047
2048 if (!tuning_loop_counter && !timeout)
2049 break;
2050
Girish K S2cd06dc2012-01-06 09:56:39 +05302051 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302052 cmd.arg = 0;
2053 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2054 cmd.retries = 0;
2055 cmd.data = NULL;
2056 cmd.error = 0;
2057
2058 mrq.cmd = &cmd;
2059 host->mrq = &mrq;
2060
2061 /*
2062 * In response to CMD19, the card sends 64 bytes of tuning
2063 * block to the Host Controller. So we set the block size
2064 * to 64 here.
2065 */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002066 if ((cmd.opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2067 (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200)) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302068 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2069 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2070 SDHCI_BLOCK_SIZE);
2071 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2072 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2073 SDHCI_BLOCK_SIZE);
2074 } else {
2075 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2076 SDHCI_BLOCK_SIZE);
2077 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302078
2079 /*
2080 * The tuning block is sent by the card to the host controller.
2081 * So we set the TRNS_READ bit in the Transfer Mode register.
2082 * This also takes care of setting DMA Enable and Multi Block
2083 * Select in the same register to 0.
2084 */
2085 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2086
2087 sdhci_send_command(host, &cmd);
2088
2089 host->cmd = NULL;
2090 host->mrq = NULL;
2091
2092 spin_unlock(&host->lock);
2093 enable_irq(host->irq);
2094
2095 /* Wait for Buffer Read Ready interrupt */
2096 wait_event_interruptible_timeout(host->buf_ready_int,
2097 (host->tuning_done == 1),
2098 msecs_to_jiffies(50));
2099 disable_irq(host->irq);
2100 spin_lock(&host->lock);
2101
2102 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302103 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302104 "Buffer Read Ready interrupt during tuning "
2105 "procedure, falling back to fixed sampling "
2106 "clock\n");
2107 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2108 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2109 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2110 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2111
2112 err = -EIO;
2113 goto out;
2114 }
2115
2116 host->tuning_done = 0;
2117
2118 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2119 tuning_loop_counter--;
2120 timeout--;
2121 mdelay(1);
2122 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2123
2124 /*
2125 * The Host Driver has exhausted the maximum number of loops allowed,
2126 * so use fixed sampling frequency.
2127 */
2128 if (!tuning_loop_counter || !timeout) {
2129 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2130 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2131 } else {
2132 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302133 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302134 " failed, falling back to fixed sampling"
2135 " clock\n");
2136 err = -EIO;
2137 }
2138 }
2139
2140out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302141 /*
2142 * If this is the very first time we are here, we start the retuning
2143 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2144 * flag won't be set, we check this condition before actually starting
2145 * the timer.
2146 */
2147 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2148 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2149 mod_timer(&host->tuning_timer, jiffies +
2150 host->tuning_count * HZ);
2151 /* Tuning mode 1 limits the maximum data length to 4MB */
2152 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2153 } else {
2154 host->flags &= ~SDHCI_NEEDS_RETUNING;
2155 /* Reload the new initial value for timer */
2156 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2157 mod_timer(&host->tuning_timer, jiffies +
2158 host->tuning_count * HZ);
2159 }
2160
2161 /*
2162 * In case tuning fails, host controllers which support re-tuning can
2163 * try tuning again at a later time, when the re-tuning timer expires.
2164 * So for these controllers, we return 0. Since there might be other
2165 * controllers who do not have this capability, we return error for
2166 * them.
2167 */
2168 if (err && host->tuning_count &&
2169 host->tuning_mode == SDHCI_TUNING_MODE_1)
2170 err = 0;
2171
Arindam Nathb513ea22011-05-05 12:19:04 +05302172 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2173 spin_unlock(&host->lock);
2174 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002175 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302176
2177 return err;
2178}
2179
Adrian Hunter50accb92011-10-03 15:33:34 +03002180static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302181{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302182 u16 ctrl;
2183 unsigned long flags;
2184
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302185 /* Host Controller v3.00 defines preset value registers */
2186 if (host->version < SDHCI_SPEC_300)
2187 return;
2188
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302189 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2190 return;
2191
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302192 spin_lock_irqsave(&host->lock, flags);
2193
2194 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2195
2196 /*
2197 * We only enable or disable Preset Value if they are not already
2198 * enabled or disabled respectively. Otherwise, we bail out.
2199 */
2200 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2201 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2202 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002203 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302204 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2205 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2206 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002207 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302208 }
2209
2210 spin_unlock_irqrestore(&host->lock, flags);
2211}
2212
Adrian Hunter50accb92011-10-03 15:33:34 +03002213static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2214{
2215 struct sdhci_host *host = mmc_priv(mmc);
2216
2217 sdhci_runtime_pm_get(host);
2218 sdhci_do_enable_preset_value(host, enable);
2219 sdhci_runtime_pm_put(host);
2220}
2221
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002222static int sdhci_stop_request(struct mmc_host *mmc)
2223{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002224 struct sdhci_host *host = mmc_priv(mmc);
2225 unsigned long flags;
2226 struct mmc_data *data;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002227 int ret = 0;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002228
2229 spin_lock_irqsave(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002230 if (!host->mrq || !host->data) {
2231 ret = MMC_BLK_NO_REQ_TO_STOP;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002232 goto out;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002233 }
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002234
2235 data = host->data;
2236
2237 if (host->ops->disable_data_xfer)
2238 host->ops->disable_data_xfer(host);
2239
2240 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2241
2242 if (host->flags & SDHCI_REQ_USE_DMA) {
2243 if (host->flags & SDHCI_USE_ADMA) {
2244 sdhci_adma_table_post(host, data);
2245 } else {
2246 if (!data->host_cookie)
2247 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2248 data->sg_len,
2249 (data->flags & MMC_DATA_READ) ?
2250 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2251 }
2252 }
2253 del_timer(&host->timer);
2254 host->mrq = NULL;
2255 host->cmd = NULL;
2256 host->data = NULL;
2257out:
2258 spin_unlock_irqrestore(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002259 return ret;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002260}
2261
2262static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2263{
2264 struct sdhci_host *host = mmc_priv(mmc);
2265 u32 present_state = 0;
2266
2267 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2268
2269 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002270}
2271
David Brownellab7aefd2006-11-12 17:55:30 -08002272static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002273 .pre_req = sdhci_pre_req,
2274 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 .request = sdhci_request,
2276 .set_ios = sdhci_set_ios,
2277 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002278 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002279 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302280 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302281 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302282 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302283 .enable = sdhci_enable,
2284 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002285 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002286 .get_xfer_remain = sdhci_get_xfer_remain,
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05302287 .notify_load = sdhci_notify_load,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288};
2289
2290/*****************************************************************************\
2291 * *
2292 * Tasklets *
2293 * *
2294\*****************************************************************************/
2295
2296static void sdhci_tasklet_card(unsigned long param)
2297{
2298 struct sdhci_host *host;
2299 unsigned long flags;
2300
2301 host = (struct sdhci_host*)param;
2302
2303 spin_lock_irqsave(&host->lock, flags);
2304
Adrian Hunter50accb92011-10-03 15:33:34 +03002305 /* Check host->mrq first in case we are runtime suspended */
2306 if (host->mrq &&
2307 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302308 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002309 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302310 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002311 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312
Adrian Hunter50accb92011-10-03 15:33:34 +03002313 sdhci_reset(host, SDHCI_RESET_CMD);
2314 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002315
Adrian Hunter50accb92011-10-03 15:33:34 +03002316 host->mrq->cmd->error = -ENOMEDIUM;
2317 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318 }
2319
2320 spin_unlock_irqrestore(&host->lock, flags);
2321
Pierre Ossman04cf5852008-08-18 22:18:14 +02002322 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323}
2324
2325static void sdhci_tasklet_finish(unsigned long param)
2326{
2327 struct sdhci_host *host;
2328 unsigned long flags;
2329 struct mmc_request *mrq;
2330
2331 host = (struct sdhci_host*)param;
2332
Adrian Hunter50accb92011-10-03 15:33:34 +03002333 spin_lock_irqsave(&host->lock, flags);
2334
Chris Ball0c9c99a2011-04-27 17:35:31 -04002335 /*
2336 * If this tasklet gets rescheduled while running, it will
2337 * be run again afterwards but without any active request.
2338 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002339 if (!host->mrq) {
2340 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002341 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002342 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343
2344 del_timer(&host->timer);
2345
2346 mrq = host->mrq;
2347
Pierre Ossmand129bce2006-03-24 03:18:17 -08002348 /*
2349 * The controller needs a reset of internal state machines
2350 * upon error conditions.
2351 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002352 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002353 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002354 (mrq->data && (mrq->data->error ||
2355 (mrq->data->stop && mrq->data->stop->error))) ||
2356 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002357
2358 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002359 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002360 unsigned int clock;
2361
2362 /* This is to force an update */
2363 clock = host->clock;
2364 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302365 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002366 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302367 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002368 }
2369
2370 /* Spec says we should do both at the same time, but Ricoh
2371 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 sdhci_reset(host, SDHCI_RESET_CMD);
2373 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002374 } else {
2375 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2376 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002377 }
2378
2379 host->mrq = NULL;
2380 host->cmd = NULL;
2381 host->data = NULL;
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302382 host->auto_cmd_err_sts = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383
Pierre Ossmanf9134312008-12-21 17:01:48 +01002384#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002385 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002386#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387
Pierre Ossman5f25a662006-10-04 02:15:39 -07002388 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389 spin_unlock_irqrestore(&host->lock, flags);
2390
2391 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002392 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393}
2394
2395static void sdhci_timeout_timer(unsigned long data)
2396{
2397 struct sdhci_host *host;
2398 unsigned long flags;
2399
2400 host = (struct sdhci_host*)data;
2401
2402 spin_lock_irqsave(&host->lock, flags);
2403
2404 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302405 if (!host->mrq->cmd->ignore_timeout) {
2406 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2407 mmc_hostname(host->mmc));
2408 sdhci_dumpregs(host);
2409 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002410
2411 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302412 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2413 mmc_hostname(host->mmc),
2414 (host->data->blksz * host->data->blocks),
2415 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2416 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002417 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002418 sdhci_finish_data(host);
2419 } else {
2420 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002421 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002422 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002423 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002424
2425 tasklet_schedule(&host->finish_tasklet);
2426 }
2427 }
2428
Pierre Ossman5f25a662006-10-04 02:15:39 -07002429 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 spin_unlock_irqrestore(&host->lock, flags);
2431}
2432
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302433static void sdhci_tuning_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 host->flags |= SDHCI_NEEDS_RETUNING;
2443
2444 spin_unlock_irqrestore(&host->lock, flags);
2445}
2446
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447/*****************************************************************************\
2448 * *
2449 * Interrupt handling *
2450 * *
2451\*****************************************************************************/
2452
2453static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2454{
Asutosh Das80c02552013-07-23 16:20:34 +05302455 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 BUG_ON(intmask == 0);
2457
2458 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302459 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002460 "though no command operation was in progress.\n",
2461 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462 sdhci_dumpregs(host);
2463 return;
2464 }
2465
Pierre Ossman43b58b32007-07-25 23:15:27 +02002466 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002467 host->cmd->error = -ETIMEDOUT;
2468 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2469 SDHCI_INT_INDEX))
2470 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002471
Asutosh Das80c02552013-07-23 16:20:34 +05302472 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302473 auto_cmd_status = host->auto_cmd_err_sts;
2474 pr_err("%s: %s: AUTO CMD err sts 0x%08x\n",
2475 mmc_hostname(host->mmc), __func__, auto_cmd_status);
Asutosh Das80c02552013-07-23 16:20:34 +05302476 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2477 SDHCI_AUTO_CMD_INDEX_ERR |
2478 SDHCI_AUTO_CMD_ENDBIT_ERR))
2479 host->cmd->error = -EIO;
2480 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2481 host->cmd->error = -ETIMEDOUT;
2482 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2483 host->cmd->error = -EILSEQ;
2484 }
2485
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302486 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002487 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2488 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302489 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2490 if (intmask & SDHCI_INT_CRC) {
2491 sdhci_reset(host, SDHCI_RESET_CMD);
2492 host->cmd->error = 0;
2493 }
2494 }
2495 }
2496
Pierre Ossmane8095172008-07-25 01:09:08 +02002497 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002498 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002499 return;
2500 }
2501
2502 /*
2503 * The host can send and interrupt when the busy state has
2504 * ended, allowing us to wait without wasting CPU cycles.
2505 * Unfortunately this is overloaded on the "data complete"
2506 * interrupt, so we need to take some care when handling
2507 * it.
2508 *
2509 * Note: The 1.0 specification is a bit ambiguous about this
2510 * feature so there might be some problems with older
2511 * controllers.
2512 */
2513 if (host->cmd->flags & MMC_RSP_BUSY) {
2514 if (host->cmd->data)
2515 DBG("Cannot wait for busy signal when also "
2516 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002517 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002518 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002519
2520 /* The controller does not support the end-of-busy IRQ,
2521 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002522 }
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_finish_command(host);
2530 return;
2531 }
2532 }
2533 }
2534
Pierre Ossmane8095172008-07-25 01:09:08 +02002535 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002536 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537}
2538
Ben Dooks6882a8c2009-06-14 13:52:38 +01002539static void sdhci_show_adma_error(struct sdhci_host *host)
2540{
2541 const char *name = mmc_hostname(host->mmc);
2542 u8 *desc = host->adma_desc;
2543 __le32 *dma;
2544 __le16 *len;
2545 u8 attr;
2546
2547 sdhci_dumpregs(host);
2548
2549 while (true) {
2550 dma = (__le32 *)(desc + 4);
2551 len = (__le16 *)(desc + 2);
2552 attr = *desc;
2553
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302554 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002555 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2556
2557 desc += 8;
2558
2559 if (attr & 2)
2560 break;
2561 }
2562}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002563
Pierre Ossmand129bce2006-03-24 03:18:17 -08002564static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2565{
Girish K S2cd06dc2012-01-06 09:56:39 +05302566 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302567 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002568 BUG_ON(intmask == 0);
2569
Arindam Nathb513ea22011-05-05 12:19:04 +05302570 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2571 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302572 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2573 if (command == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002574 command == MMC_SEND_TUNING_BLOCK_HS200 ||
2575 command == MMC_SEND_TUNING_BLOCK_HS400) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302576 host->tuning_done = 1;
2577 wake_up(&host->buf_ready_int);
2578 return;
2579 }
2580 }
2581
Pierre Ossmand129bce2006-03-24 03:18:17 -08002582 if (!host->data) {
2583 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002584 * The "data complete" interrupt is also used to
2585 * indicate that a busy state has ended. See comment
2586 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002587 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002588 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2589 if (intmask & SDHCI_INT_DATA_END) {
2590 sdhci_finish_command(host);
2591 return;
2592 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302593 if (host->quirks2 &
2594 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2595 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002596 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002597
Sahitya Tummalaca422112013-02-22 12:15:54 +05302598 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002599 "though no data operation was in progress.\n",
2600 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601 sdhci_dumpregs(host);
2602
2603 return;
2604 }
2605
2606 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002607 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002608 else if (intmask & SDHCI_INT_DATA_END_BIT)
2609 host->data->error = -EILSEQ;
2610 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2611 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2612 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002613 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002614 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302615 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002616 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002617 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002618 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302619 if (host->data->error) {
2620 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2621 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2622 command = SDHCI_GET_CMD(sdhci_readw(host,
2623 SDHCI_COMMAND));
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002624 if ((command != MMC_SEND_TUNING_BLOCK_HS400) &&
2625 (command != MMC_SEND_TUNING_BLOCK_HS200) &&
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302626 (command != MMC_SEND_TUNING_BLOCK))
2627 pr_msg = true;
2628 } else {
2629 pr_msg = true;
2630 }
2631 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302632 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302633 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302634 host->data->error, ktime_to_ms(ktime_sub(
2635 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302636 sdhci_dumpregs(host);
2637 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002638 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302639 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002640 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641 sdhci_transfer_pio(host);
2642
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002643 /*
2644 * We currently don't do anything fancy with DMA
2645 * boundaries, but as we can't disable the feature
2646 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002647 *
2648 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2649 * should return a valid address to continue from, but as
2650 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002651 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002652 if (intmask & SDHCI_INT_DMA_END) {
2653 u32 dmastart, dmanow;
2654 dmastart = sg_dma_address(host->data->sg);
2655 dmanow = dmastart + host->data->bytes_xfered;
2656 /*
2657 * Force update to the next DMA block boundary.
2658 */
2659 dmanow = (dmanow &
2660 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2661 SDHCI_DEFAULT_BOUNDARY_SIZE;
2662 host->data->bytes_xfered = dmanow - dmastart;
2663 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2664 " next 0x%08x\n",
2665 mmc_hostname(host->mmc), dmastart,
2666 host->data->bytes_xfered, dmanow);
2667 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2668 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002669
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002670 if (intmask & SDHCI_INT_DATA_END) {
2671 if (host->cmd) {
2672 /*
2673 * Data managed to finish before the
2674 * command completed. Make sure we do
2675 * things in the proper order.
2676 */
2677 host->data_early = 1;
2678 } else {
2679 sdhci_finish_data(host);
2680 }
2681 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002682 }
2683}
2684
David Howells7d12e782006-10-05 14:55:46 +01002685static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002686{
2687 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002688 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002689 u32 intmask, unexpected = 0;
2690 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691
2692 spin_lock(&host->lock);
2693
Adrian Hunter50accb92011-10-03 15:33:34 +03002694 if (host->runtime_suspended) {
2695 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302696 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002697 mmc_hostname(host->mmc));
2698 return IRQ_HANDLED;
2699 }
2700
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002701 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002702
Mark Lord62df67a2007-03-06 13:30:13 +01002703 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002704 result = IRQ_NONE;
2705 goto out;
2706 }
2707
Alexander Stein6379b232012-03-14 09:52:10 +01002708again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002709 DBG("*** %s got interrupt: 0x%08x\n",
2710 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002711
Pierre Ossman3192a282006-06-30 02:22:26 -07002712 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302713 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2714 SDHCI_CARD_PRESENT;
2715
2716 /*
2717 * There is a observation on i.mx esdhc. INSERT bit will be
2718 * immediately set again when it gets cleared, if a card is
2719 * inserted. We have to mask the irq to prevent interrupt
2720 * storm which will freeze the system. And the REMOVE gets
2721 * the same situation.
2722 *
2723 * More testing are needed here to ensure it works for other
2724 * platforms though.
2725 */
2726 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2727 SDHCI_INT_CARD_REMOVE);
2728 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2729 SDHCI_INT_CARD_INSERT);
2730
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002731 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302732 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2733 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002734 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002735 }
2736
Pierre Ossmand129bce2006-03-24 03:18:17 -08002737 if (intmask & SDHCI_INT_CMD_MASK) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302738 if (intmask & SDHCI_INT_AUTO_CMD_ERR)
2739 host->auto_cmd_err_sts = sdhci_readw(host,
2740 SDHCI_AUTO_CMD_ERR);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002741 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2742 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002743 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2744 (host->clock <= 400000))
2745 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002746 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002747 }
2748
2749 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002750 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2751 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002752 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2753 (host->clock <= 400000))
2754 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002755 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002756 }
2757
2758 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2759
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002760 intmask &= ~SDHCI_INT_ERROR;
2761
Pierre Ossmand129bce2006-03-24 03:18:17 -08002762 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302763 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002765 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002766 }
2767
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002768 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002769
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002770 if (intmask & SDHCI_INT_CARD_INT)
2771 cardint = 1;
2772
2773 intmask &= ~SDHCI_INT_CARD_INT;
2774
Pierre Ossman3192a282006-06-30 02:22:26 -07002775 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002776 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002777 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002778 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002779
2780 result = IRQ_HANDLED;
2781
Alexander Stein6379b232012-03-14 09:52:10 +01002782 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2783 if (intmask && --max_loops)
2784 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002785out:
2786 spin_unlock(&host->lock);
2787
Alexander Stein6379b232012-03-14 09:52:10 +01002788 if (unexpected) {
2789 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2790 mmc_hostname(host->mmc), unexpected);
2791 sdhci_dumpregs(host);
2792 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002793 /*
2794 * We have to delay this as it calls back into the driver.
2795 */
2796 if (cardint)
2797 mmc_signal_sdio_irq(host->mmc);
2798
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799 return result;
2800}
2801
2802/*****************************************************************************\
2803 * *
2804 * Suspend/resume *
2805 * *
2806\*****************************************************************************/
2807
2808#ifdef CONFIG_PM
2809
Manuel Laussd72faa62011-11-03 11:09:45 +01002810int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002811{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002812 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302813 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002814
Chris Balla1b13b42012-02-06 00:43:59 -05002815 if (host->ops->platform_suspend)
2816 host->ops->platform_suspend(host);
2817
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002818 sdhci_disable_card_detection(host);
2819
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302820 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302821 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2822 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2823 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002824 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302825 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302826 }
2827
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002828 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302829 if (ret) {
2830 if (has_tuning_timer) {
2831 host->flags |= SDHCI_NEEDS_RETUNING;
2832 mod_timer(&host->tuning_timer, jiffies +
2833 host->tuning_count * HZ);
2834 }
2835
2836 sdhci_enable_card_detection(host);
2837
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002838 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302839 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002840
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002841 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002843 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844}
2845
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002846EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848int sdhci_resume_host(struct sdhci_host *host)
2849{
2850 int ret;
2851
Richard Röjforsa13abc72009-09-22 16:45:30 -07002852 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853 if (host->ops->enable_dma)
2854 host->ops->enable_dma(host);
2855 }
2856
2857 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2858 mmc_hostname(host->mmc), host);
2859 if (ret)
2860 return ret;
2861
Adrian Hunter6308d292012-02-07 14:48:54 +02002862 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2863 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2864 /* Card keeps power but host controller does not */
2865 sdhci_init(host, 0);
2866 host->pwr = 0;
2867 host->clock = 0;
2868 sdhci_do_set_ios(host, &host->mmc->ios);
2869 } else {
2870 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2871 mmiowb();
2872 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002873
2874 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002875 sdhci_enable_card_detection(host);
2876
Chris Balla1b13b42012-02-06 00:43:59 -05002877 if (host->ops->platform_resume)
2878 host->ops->platform_resume(host);
2879
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302880 /* Set the re-tuning expiration flag */
2881 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2882 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2883 host->flags |= SDHCI_NEEDS_RETUNING;
2884
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002885 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002886}
2887
2888EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002889
Daniel Drake5f619702010-11-04 22:20:39 +00002890void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2891{
2892 u8 val;
2893 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2894 val |= SDHCI_WAKE_ON_INT;
2895 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2896}
2897
2898EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2899
Pierre Ossmand129bce2006-03-24 03:18:17 -08002900#endif /* CONFIG_PM */
2901
Adrian Hunter50accb92011-10-03 15:33:34 +03002902#ifdef CONFIG_PM_RUNTIME
2903
2904static int sdhci_runtime_pm_get(struct sdhci_host *host)
2905{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302906 if (!mmc_use_core_runtime_pm(host->mmc))
2907 return pm_runtime_get_sync(host->mmc->parent);
2908 else
2909 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002910}
2911
2912static int sdhci_runtime_pm_put(struct sdhci_host *host)
2913{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302914 if (!mmc_use_core_runtime_pm(host->mmc)) {
2915 pm_runtime_mark_last_busy(host->mmc->parent);
2916 return pm_runtime_put_autosuspend(host->mmc->parent);
2917 } else {
2918 return 0;
2919 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002920}
2921
2922int sdhci_runtime_suspend_host(struct sdhci_host *host)
2923{
2924 unsigned long flags;
2925 int ret = 0;
2926
2927 /* Disable tuning since we are suspending */
2928 if (host->version >= SDHCI_SPEC_300 &&
2929 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2930 del_timer_sync(&host->tuning_timer);
2931 host->flags &= ~SDHCI_NEEDS_RETUNING;
2932 }
2933
2934 spin_lock_irqsave(&host->lock, flags);
2935 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2936 spin_unlock_irqrestore(&host->lock, flags);
2937
2938 synchronize_irq(host->irq);
2939
2940 spin_lock_irqsave(&host->lock, flags);
2941 host->runtime_suspended = true;
2942 spin_unlock_irqrestore(&host->lock, flags);
2943
2944 return ret;
2945}
2946EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2947
2948int sdhci_runtime_resume_host(struct sdhci_host *host)
2949{
2950 unsigned long flags;
2951 int ret = 0, host_flags = host->flags;
2952
2953 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2954 if (host->ops->enable_dma)
2955 host->ops->enable_dma(host);
2956 }
2957
2958 sdhci_init(host, 0);
2959
2960 /* Force clock and power re-program */
2961 host->pwr = 0;
2962 host->clock = 0;
2963 sdhci_do_set_ios(host, &host->mmc->ios);
2964
2965 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2966 if (host_flags & SDHCI_PV_ENABLED)
2967 sdhci_do_enable_preset_value(host, true);
2968
2969 /* Set the re-tuning expiration flag */
2970 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2971 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2972 host->flags |= SDHCI_NEEDS_RETUNING;
2973
2974 spin_lock_irqsave(&host->lock, flags);
2975
2976 host->runtime_suspended = false;
2977
2978 /* Enable SDIO IRQ */
2979 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2980 sdhci_enable_sdio_irq_nolock(host, true);
2981
2982 /* Enable Card Detection */
2983 sdhci_enable_card_detection(host);
2984
2985 spin_unlock_irqrestore(&host->lock, flags);
2986
2987 return ret;
2988}
2989EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2990
2991#endif
2992
Pierre Ossmand129bce2006-03-24 03:18:17 -08002993/*****************************************************************************\
2994 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002995 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002996 * *
2997\*****************************************************************************/
2998
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002999struct sdhci_host *sdhci_alloc_host(struct device *dev,
3000 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003001{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003002 struct mmc_host *mmc;
3003 struct sdhci_host *host;
3004
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003005 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003006
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003007 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003008 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003009 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003010
3011 host = mmc_priv(mmc);
3012 host->mmc = mmc;
3013
Sahitya Tummala951c1202013-05-24 08:47:26 +05303014 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05303015 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05303016
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003017 return host;
3018}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003019
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003020EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003021
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003022int sdhci_add_host(struct sdhci_host *host)
3023{
3024 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303025 u32 caps[2];
3026 u32 max_current_caps;
3027 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003028 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003029
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003030 WARN_ON(host == NULL);
3031 if (host == NULL)
3032 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003033
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003034 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003035
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003036 if (debug_quirks)
3037 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03003038 if (debug_quirks2)
3039 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003040
Pierre Ossmand96649e2006-06-30 02:22:30 -07003041 sdhci_reset(host, SDHCI_RESET_ALL);
3042
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003043 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02003044 host->version = (host->version & SDHCI_SPEC_VER_MASK)
3045 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08003046 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303047 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003048 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02003049 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003050 }
3051
Arindam Nathf2119df2011-05-05 12:18:57 +05303052 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07003053 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003054
Arindam Nathf2119df2011-05-05 12:18:57 +05303055 caps[1] = (host->version >= SDHCI_SPEC_300) ?
3056 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
3057
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003058 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003059 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05303060 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003061 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003062 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003063 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003065 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003066 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003067 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003068 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003069 }
3070
Arindam Nathf2119df2011-05-05 12:18:57 +05303071 if ((host->version >= SDHCI_SPEC_200) &&
3072 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003073 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003074
3075 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3076 (host->flags & SDHCI_USE_ADMA)) {
3077 DBG("Disabling ADMA as it is marked broken\n");
3078 host->flags &= ~SDHCI_USE_ADMA;
3079 }
3080
Richard Röjforsa13abc72009-09-22 16:45:30 -07003081 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003082 if (host->ops->enable_dma) {
3083 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303084 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003085 "available. Falling back to PIO.\n",
3086 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003087 host->flags &=
3088 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003089 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003090 }
3091 }
3092
Pierre Ossman2134a922008-06-28 18:28:51 +02003093 if (host->flags & SDHCI_USE_ADMA) {
3094 /*
3095 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303096 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02003097 * each of those entries.
3098 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303099 if (host->ops->get_max_segments)
3100 host->adma_max_desc = host->ops->get_max_segments();
3101 else
3102 host->adma_max_desc = 128;
3103
3104 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
3105 host->align_buf_sz = host->adma_max_desc * 4;
3106
3107 pr_debug("%s: %s: dma_desc_size: %d\n",
3108 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3109 host->adma_desc = kmalloc(host->adma_desc_sz,
3110 GFP_KERNEL);
3111 host->align_buffer = kmalloc(host->align_buf_sz,
3112 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003113 if (!host->adma_desc || !host->align_buffer) {
3114 kfree(host->adma_desc);
3115 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303116 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003117 "buffers. Falling back to standard DMA.\n",
3118 mmc_hostname(mmc));
3119 host->flags &= ~SDHCI_USE_ADMA;
3120 }
3121 }
3122
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003123 host->next_data.cookie = 1;
3124
Pierre Ossman76591502008-07-21 00:32:11 +02003125 /*
3126 * If we use DMA, then it's up to the caller to set the DMA
3127 * mask, but PIO does not need the hw shim so we set a new
3128 * mask here in that case.
3129 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003130 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003131 host->dma_mask = DMA_BIT_MASK(64);
3132 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3133 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003134
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003135 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303136 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003137 >> SDHCI_CLOCK_BASE_SHIFT;
3138 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303139 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003140 >> SDHCI_CLOCK_BASE_SHIFT;
3141
Pierre Ossmand129bce2006-03-24 03:18:17 -08003142 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003143 if (host->max_clk == 0 || host->quirks &
3144 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003145 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303146 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003147 "frequency.\n", mmc_hostname(mmc));
3148 return -ENODEV;
3149 }
3150 host->max_clk = host->ops->get_max_clock(host);
3151 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003152
3153 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303154 * In case of Host Controller v3.00, find out whether clock
3155 * multiplier is supported.
3156 */
3157 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3158 SDHCI_CLOCK_MUL_SHIFT;
3159
3160 /*
3161 * In case the value in Clock Multiplier is 0, then programmable
3162 * clock mode is not supported, otherwise the actual clock
3163 * multiplier is one more than the value of Clock Multiplier
3164 * in the Capabilities Register.
3165 */
3166 if (host->clk_mul)
3167 host->clk_mul += 1;
3168
3169 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003170 * Set host parameters.
3171 */
3172 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303173 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003174 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003175 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303176 else if (host->version >= SDHCI_SPEC_300) {
3177 if (host->clk_mul) {
3178 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3179 mmc->f_max = host->max_clk * host->clk_mul;
3180 } else
3181 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3182 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003183 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003184
Sahitya Tummalaca422112013-02-22 12:15:54 +05303185 host->timeout_clk =
3186 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3187 if (host->timeout_clk == 0) {
3188 if (host->ops->get_timeout_clock) {
3189 host->timeout_clk = host->ops->get_timeout_clock(host);
3190 } else if (!(host->quirks &
3191 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3192 pr_err("%s: Hardware doesn't specify timeout clock "
3193 "frequency.\n", mmc_hostname(mmc));
3194 return -ENODEV;
3195 }
3196 }
3197 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3198 host->timeout_clk *= 1000;
3199
3200 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3201 host->timeout_clk = mmc->f_max / 1000;
3202
Krishna Kondaa20d3362013-04-01 21:01:59 -07003203 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3204 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303205
Andrei Warkentine89d4562011-05-23 15:06:37 -05003206 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3207
3208 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3209 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003210
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003211 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003212 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003213 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003214 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003215 host->flags |= SDHCI_AUTO_CMD23;
3216 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3217 } else {
3218 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3219 }
3220
Philip Rakity15ec4462010-11-19 16:48:39 -05003221 /*
3222 * A controller may support 8-bit width, but the board itself
3223 * might not have the pins brought out. Boards that support
3224 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3225 * their platform code before calling sdhci_add_host(), and we
3226 * won't assume 8-bit width for hosts without that CAP.
3227 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003228 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003229 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003230
Arindam Nathf2119df2011-05-05 12:18:57 +05303231 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003232 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003233
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003234 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3235 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003236 mmc->caps |= MMC_CAP_NEEDS_POLL;
3237
Al Cooper4188bba2012-03-16 15:54:17 -04003238 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3239 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3240 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303241 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3242
3243 /* SDR104 supports also implies SDR50 support */
3244 if (caps[1] & SDHCI_SUPPORT_SDR104)
3245 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3246 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3247 mmc->caps |= MMC_CAP_UHS_SDR50;
3248
3249 if (caps[1] & SDHCI_SUPPORT_DDR50)
3250 mmc->caps |= MMC_CAP_UHS_DDR50;
3251
Girish K S2cd06dc2012-01-06 09:56:39 +05303252 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303253 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3254 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3255
Girish K S2cd06dc2012-01-06 09:56:39 +05303256 /* Does the host need tuning for HS200? */
3257 if (mmc->caps2 & MMC_CAP2_HS200)
3258 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3259
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07003260 /* Does the host need tuning for HS400? */
3261 if (mmc->caps2 & MMC_CAP2_HS400)
3262 host->flags |= SDHCI_HS400_NEEDS_TUNING;
3263
Arindam Nathd6d50a12011-05-05 12:18:59 +05303264 /* Driver Type(s) (A, C, D) supported by the host */
3265 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3266 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3267 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3268 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3269 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3270 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3271
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003272 /* Initial value for re-tuning timer count */
3273 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3274 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3275
3276 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303277 * In case Re-tuning Timer is not disabled, the actual value of
3278 * re-tuning timer will be 2 ^ (n - 1).
3279 */
3280 if (host->tuning_count)
3281 host->tuning_count = 1 << (host->tuning_count - 1);
3282
3283 /* Re-tuning mode supported by the Host Controller */
3284 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3285 SDHCI_RETUNING_MODE_SHIFT;
3286
Takashi Iwai8f230f42010-12-08 10:04:30 +01003287 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303288 /*
3289 * According to SD Host Controller spec v3.00, if the Host System
3290 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3291 * the value is meaningful only if Voltage Support in the Capabilities
3292 * register is set. The actual current value is 4 times the register
3293 * value.
3294 */
3295 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3296
3297 if (caps[0] & SDHCI_CAN_VDD_330) {
3298 int max_current_330;
3299
Takashi Iwai8f230f42010-12-08 10:04:30 +01003300 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303301
3302 max_current_330 = ((max_current_caps &
3303 SDHCI_MAX_CURRENT_330_MASK) >>
3304 SDHCI_MAX_CURRENT_330_SHIFT) *
3305 SDHCI_MAX_CURRENT_MULTIPLIER;
3306
3307 if (max_current_330 > 150)
3308 mmc->caps |= MMC_CAP_SET_XPC_330;
3309 }
3310 if (caps[0] & SDHCI_CAN_VDD_300) {
3311 int max_current_300;
3312
Takashi Iwai8f230f42010-12-08 10:04:30 +01003313 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303314
3315 max_current_300 = ((max_current_caps &
3316 SDHCI_MAX_CURRENT_300_MASK) >>
3317 SDHCI_MAX_CURRENT_300_SHIFT) *
3318 SDHCI_MAX_CURRENT_MULTIPLIER;
3319
3320 if (max_current_300 > 150)
3321 mmc->caps |= MMC_CAP_SET_XPC_300;
3322 }
3323 if (caps[0] & SDHCI_CAN_VDD_180) {
3324 int max_current_180;
3325
Takashi Iwai8f230f42010-12-08 10:04:30 +01003326 ocr_avail |= MMC_VDD_165_195;
3327
Arindam Nathf2119df2011-05-05 12:18:57 +05303328 max_current_180 = ((max_current_caps &
3329 SDHCI_MAX_CURRENT_180_MASK) >>
3330 SDHCI_MAX_CURRENT_180_SHIFT) *
3331 SDHCI_MAX_CURRENT_MULTIPLIER;
3332
3333 if (max_current_180 > 150)
3334 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303335
3336 /* Maximum current capabilities of the host at 1.8V */
3337 if (max_current_180 >= 800)
3338 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3339 else if (max_current_180 >= 600)
3340 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3341 else if (max_current_180 >= 400)
3342 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3343 else
3344 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303345 }
3346
Takashi Iwai8f230f42010-12-08 10:04:30 +01003347 mmc->ocr_avail = ocr_avail;
3348 mmc->ocr_avail_sdio = ocr_avail;
3349 if (host->ocr_avail_sdio)
3350 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3351 mmc->ocr_avail_sd = ocr_avail;
3352 if (host->ocr_avail_sd)
3353 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3354 else /* normal SD controllers don't support 1.8V */
3355 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3356 mmc->ocr_avail_mmc = ocr_avail;
3357 if (host->ocr_avail_mmc)
3358 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003359
3360 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303361 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003362 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003363 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003364 }
3365
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003367 * Maximum number of segments. Depends on if the hardware
3368 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003369 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003370 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303371 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003372 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003373 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303374 else/* PIO */
3375 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376
3377 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003378 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303379 * size (512KiB), unless specified by platform specific driver. Each
3380 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003381 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303382 if (host->ops->get_max_segments)
3383 mmc->max_req_size = (host->adma_max_desc * 65536);
3384 else
3385 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003386
3387 /*
3388 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003389 * of bytes. When doing hardware scatter/gather, each entry cannot
3390 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003392 if (host->flags & SDHCI_USE_ADMA) {
3393 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3394 mmc->max_seg_size = 65535;
3395 else
3396 mmc->max_seg_size = 65536;
3397 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003398 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003399 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003400
3401 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003402 * Maximum block size. This varies from controller to controller and
3403 * is specified in the capabilities register.
3404 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003405 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3406 mmc->max_blk_size = 2;
3407 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303408 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003409 SDHCI_MAX_BLOCK_SHIFT;
3410 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303411 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003412 "assuming 512 bytes\n", mmc_hostname(mmc));
3413 mmc->max_blk_size = 0;
3414 }
3415 }
3416
3417 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003418
3419 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003420 * Maximum block count.
3421 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003422 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003423
3424 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003425 * Init tasklets.
3426 */
3427 tasklet_init(&host->card_tasklet,
3428 sdhci_tasklet_card, (unsigned long)host);
3429 tasklet_init(&host->finish_tasklet,
3430 sdhci_tasklet_finish, (unsigned long)host);
3431
Al Viroe4cad1b2006-10-10 22:47:07 +01003432 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303434 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303435 init_waitqueue_head(&host->buf_ready_int);
3436
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303437 /* Initialize re-tuning timer */
3438 init_timer(&host->tuning_timer);
3439 host->tuning_timer.data = (unsigned long)host;
3440 host->tuning_timer.function = sdhci_tuning_timer;
3441 }
3442
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003443 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003444 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003445 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003446 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003447
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003448 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3449 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303450 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003451 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003452 }
3453
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003454 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003455
3456#ifdef CONFIG_MMC_DEBUG
3457 sdhci_dumpregs(host);
3458#endif
3459
Pierre Ossmanf9134312008-12-21 17:01:48 +01003460#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003461 snprintf(host->led_name, sizeof(host->led_name),
3462 "%s::", mmc_hostname(mmc));
3463 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003464 host->led.brightness = LED_OFF;
3465 host->led.default_trigger = mmc_hostname(mmc);
3466 host->led.brightness_set = sdhci_led_control;
3467
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003468 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003469 if (ret)
3470 goto reset;
3471#endif
3472
Pierre Ossman5f25a662006-10-04 02:15:39 -07003473 mmiowb();
3474
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303475 if (host->cpu_dma_latency_us) {
3476 host->pm_qos_timeout_us = 10000; /* default value */
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303477 pm_qos_add_request(&host->pm_qos_req_dma,
3478 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303479
3480 host->pm_qos_tout.show = show_sdhci_pm_qos_tout;
3481 host->pm_qos_tout.store = store_sdhci_pm_qos_tout;
3482 sysfs_attr_init(&host->pm_qos_tout.attr);
3483 host->pm_qos_tout.attr.name = "pm_qos_unvote_delay";
3484 host->pm_qos_tout.attr.mode = S_IRUGO | S_IWUSR;
3485 ret = device_create_file(mmc_dev(mmc), &host->pm_qos_tout);
3486 if (ret)
3487 pr_err("%s: cannot create pm_qos_unvote_delay %d\n",
3488 mmc_hostname(mmc), ret);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303489 }
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303490
Pierre Ossmand129bce2006-03-24 03:18:17 -08003491 mmc_add_host(mmc);
3492
Sahitya Tummalaca422112013-02-22 12:15:54 +05303493 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003494 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003495 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3496 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003497
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003498 sdhci_enable_card_detection(host);
3499
Pierre Ossmand129bce2006-03-24 03:18:17 -08003500 return 0;
3501
Pierre Ossmanf9134312008-12-21 17:01:48 +01003502#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003503reset:
3504 sdhci_reset(host, SDHCI_RESET_ALL);
3505 free_irq(host->irq, host);
3506#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003507untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003508 tasklet_kill(&host->card_tasklet);
3509 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003510
3511 return ret;
3512}
3513
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003514EXPORT_SYMBOL_GPL(sdhci_add_host);
3515
Pierre Ossman1e728592008-04-16 19:13:13 +02003516void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003517{
Pierre Ossman1e728592008-04-16 19:13:13 +02003518 unsigned long flags;
3519
3520 if (dead) {
3521 spin_lock_irqsave(&host->lock, flags);
3522
3523 host->flags |= SDHCI_DEVICE_DEAD;
3524
3525 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303526 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003527 " transfer!\n", mmc_hostname(host->mmc));
3528
3529 host->mrq->cmd->error = -ENOMEDIUM;
3530 tasklet_schedule(&host->finish_tasklet);
3531 }
3532
3533 spin_unlock_irqrestore(&host->lock, flags);
3534 }
3535
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003536 sdhci_disable_card_detection(host);
3537
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303538 if (host->cpu_dma_latency_us)
3539 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003540 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003541
Pierre Ossmanf9134312008-12-21 17:01:48 +01003542#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003543 led_classdev_unregister(&host->led);
3544#endif
3545
Pierre Ossman1e728592008-04-16 19:13:13 +02003546 if (!dead)
3547 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003548
3549 free_irq(host->irq, host);
3550
3551 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303552 if (host->version >= SDHCI_SPEC_300)
3553 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003554
3555 tasklet_kill(&host->card_tasklet);
3556 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003557
Sahitya Tummalaca422112013-02-22 12:15:54 +05303558 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003559 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003560
Pierre Ossman2134a922008-06-28 18:28:51 +02003561 kfree(host->adma_desc);
3562 kfree(host->align_buffer);
3563
3564 host->adma_desc = NULL;
3565 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003566}
3567
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003568EXPORT_SYMBOL_GPL(sdhci_remove_host);
3569
3570void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003571{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003572 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003573}
3574
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003575EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003576
3577/*****************************************************************************\
3578 * *
3579 * Driver init/exit *
3580 * *
3581\*****************************************************************************/
3582
3583static int __init sdhci_drv_init(void)
3584{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303585 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003586 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303587 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003588
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003589 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003590}
3591
3592static void __exit sdhci_drv_exit(void)
3593{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003594}
3595
3596module_init(sdhci_drv_init);
3597module_exit(sdhci_drv_exit);
3598
Pierre Ossmandf673b22006-06-30 02:22:31 -07003599module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003600module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003601
Pierre Ossman32710e82009-04-08 20:14:54 +02003602MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003603MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003604MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003605
Pierre Ossmandf673b22006-06-30 02:22:31 -07003606MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003607MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");