blob: b69a67830d3131b498536f7009d3a5ab17e789f2 [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 Das08a00a32013-04-24 14:48:48 +053071static inline int sdhci_get_async_int_status(struct sdhci_host *host)
72{
73 return (sdhci_readl(host, SDHCI_HOST_CONTROL2) &
74 SDHCI_CTRL_ASYNC_INT_ENABLE) >> 14;
75}
76
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053077static void sdhci_dump_state(struct sdhci_host *host)
78{
79 struct mmc_host *mmc = host->mmc;
80
81 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
82 mmc_hostname(mmc), host->clock, mmc->clk_gated,
83 mmc->claimer->comm, host->pwr);
84 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
85 mmc_hostname(mmc), mmc->parent->power.runtime_status,
86 atomic_read(&mmc->parent->power.usage_count),
87 mmc->parent->power.disable_depth);
88}
89
Pierre Ossmand129bce2006-03-24 03:18:17 -080090static void sdhci_dumpregs(struct sdhci_host *host)
91{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053092 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070093 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080094
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053095 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_DMA_ADDRESS),
97 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053098 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_BLOCK_SIZE),
100 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530101 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_ARGUMENT),
103 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530104 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readl(host, SDHCI_PRESENT_STATE),
106 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530107 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readb(host, SDHCI_POWER_CONTROL),
109 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530110 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300111 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
112 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530113 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300114 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
115 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530116 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300117 sdhci_readl(host, SDHCI_INT_ENABLE),
118 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530119 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Sahitya Tummala8f6c0002013-08-07 18:40:29 +0530120 host->auto_cmd_err_sts,
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300121 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530122 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300123 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500124 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530125 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500126 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300127 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das80c02552013-07-23 16:20:34 +0530128 pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n",
129 sdhci_readl(host, SDHCI_RESPONSE + 0x4),
130 sdhci_readl(host, SDHCI_RESPONSE));
131 pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n",
132 sdhci_readl(host, SDHCI_RESPONSE + 0xC),
133 sdhci_readl(host, SDHCI_RESPONSE + 0x8));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530134 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530135 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800136
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100137 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530138 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100139 readl(host->ioaddr + SDHCI_ADMA_ERROR),
140 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
141
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530142 sdhci_dump_state(host);
143 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800144}
145
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +0530146#define MAX_PM_QOS_TIMEOUT_VALUE 100000 /* 100 ms */
147static ssize_t
148show_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
149 char *buf)
150{
151 struct sdhci_host *host = dev_get_drvdata(dev);
152
153 return snprintf(buf, PAGE_SIZE, "%d us\n", host->pm_qos_timeout_us);
154}
155
156static ssize_t
157store_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
158 const char *buf, size_t count)
159{
160 struct sdhci_host *host = dev_get_drvdata(dev);
161 uint32_t value;
162 unsigned long flags;
163
164 if (!kstrtou32(buf, 0, &value)) {
165 spin_lock_irqsave(&host->lock, flags);
166 if (value <= MAX_PM_QOS_TIMEOUT_VALUE)
167 host->pm_qos_timeout_us = value;
168 spin_unlock_irqrestore(&host->lock, flags);
169 }
170 return count;
171}
172
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173/*****************************************************************************\
174 * *
175 * Low level functions *
176 * *
177\*****************************************************************************/
178
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300179static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
180{
181 u32 ier;
182
183 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
184 ier &= ~clear;
185 ier |= set;
186 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
187 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
188}
189
190static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
191{
192 sdhci_clear_set_irqs(host, 0, irqs);
193}
194
195static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
196{
197 sdhci_clear_set_irqs(host, irqs, 0);
198}
199
200static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
201{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530202 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300203
Adrian Hunterc79396c2011-12-27 15:48:42 +0200204 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100205 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300206 return;
207
Sahitya Tummalaca422112013-02-22 12:15:54 +0530208 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
209 SDHCI_CARD_PRESENT;
210 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300211
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300212 if (enable)
213 sdhci_unmask_irqs(host, irqs);
214 else
215 sdhci_mask_irqs(host, irqs);
216}
217
218static void sdhci_enable_card_detection(struct sdhci_host *host)
219{
220 sdhci_set_card_detection(host, true);
221}
222
223static void sdhci_disable_card_detection(struct sdhci_host *host)
224{
225 sdhci_set_card_detection(host, false);
226}
227
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228static void sdhci_reset(struct sdhci_host *host, u8 mask)
229{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700230 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300231 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700232
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100233 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300234 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700235 SDHCI_CARD_PRESENT))
236 return;
237 }
238
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300239 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
240 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
241
Philip Rakity393c1a32011-01-21 11:26:40 -0800242 if (host->ops->platform_reset_enter)
243 host->ops->platform_reset_enter(host, mask);
244
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300245 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800246
Pierre Ossmane16514d2006-06-30 02:22:24 -0700247 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248 host->clock = 0;
249
Pierre Ossmane16514d2006-06-30 02:22:24 -0700250 /* Wait max 100 ms */
251 timeout = 100;
252
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530253 if (host->ops->check_power_status && host->pwr &&
254 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530255 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530256
Pierre Ossmane16514d2006-06-30 02:22:24 -0700257 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700259 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530260 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700261 mmc_hostname(host->mmc), (int)mask);
262 sdhci_dumpregs(host);
263 return;
264 }
265 timeout--;
266 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300268
Philip Rakity393c1a32011-01-21 11:26:40 -0800269 if (host->ops->platform_reset_exit)
270 host->ops->platform_reset_exit(host, mask);
271
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300272 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
273 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530274
275 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
276 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
277 host->ops->enable_dma(host);
278 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279}
280
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800281static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
282
283static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800285 if (soft)
286 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
287 else
288 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800289
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300290 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
291 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700292 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
293 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Asutosh Das80c02552013-07-23 16:20:34 +0530294 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
295 SDHCI_INT_AUTO_CMD_ERR);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800296
297 if (soft) {
298 /* force clock reconfiguration */
299 host->clock = 0;
300 sdhci_set_ios(host->mmc, &host->mmc->ios);
301 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300302}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300304static void sdhci_reinit(struct sdhci_host *host)
305{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800306 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300307 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308}
309
310static void sdhci_activate_led(struct sdhci_host *host)
311{
312 u8 ctrl;
313
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300314 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300316 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317}
318
319static void sdhci_deactivate_led(struct sdhci_host *host)
320{
321 u8 ctrl;
322
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300323 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300325 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326}
327
Pierre Ossmanf9134312008-12-21 17:01:48 +0100328#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100329static void sdhci_led_control(struct led_classdev *led,
330 enum led_brightness brightness)
331{
332 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
333 unsigned long flags;
334
335 spin_lock_irqsave(&host->lock, flags);
336
Sahitya Tummala1b248c42013-05-24 14:08:10 +0530337 if (host->runtime_suspended || sdhci_check_state(host))
Adrian Hunter50accb92011-10-03 15:33:34 +0300338 goto out;
339
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100340 if (brightness == LED_OFF)
341 sdhci_deactivate_led(host);
342 else
343 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300344out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100345 spin_unlock_irqrestore(&host->lock, flags);
346}
347#endif
348
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349/*****************************************************************************\
350 * *
351 * Core functions *
352 * *
353\*****************************************************************************/
354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800356{
Pierre Ossman76591502008-07-21 00:32:11 +0200357 unsigned long flags;
358 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700359 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200360 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800363
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200365 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800368
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200370 if (!sg_miter_next(&host->sg_miter))
371 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 blksize -= len;
376 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 while (len) {
381 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300382 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200383 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800384 }
Pierre Ossman76591502008-07-21 00:32:11 +0200385
386 *buf = scratch & 0xFF;
387
388 buf++;
389 scratch >>= 8;
390 chunk--;
391 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800392 }
393 }
Pierre Ossman76591502008-07-21 00:32:11 +0200394
395 sg_miter_stop(&host->sg_miter);
396
397 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800399
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400static void sdhci_write_block_pio(struct sdhci_host *host)
401{
Pierre Ossman76591502008-07-21 00:32:11 +0200402 unsigned long flags;
403 size_t blksize, len, chunk;
404 u32 scratch;
405 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100406
407 DBG("PIO writing\n");
408
409 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200410 chunk = 0;
411 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100412
Pierre Ossman76591502008-07-21 00:32:11 +0200413 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100414
415 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200416 if (!sg_miter_next(&host->sg_miter))
417 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418
Pierre Ossman76591502008-07-21 00:32:11 +0200419 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200420
Pierre Ossman76591502008-07-21 00:32:11 +0200421 blksize -= len;
422 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423
Pierre Ossman76591502008-07-21 00:32:11 +0200424 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425
Pierre Ossman76591502008-07-21 00:32:11 +0200426 while (len) {
427 scratch |= (u32)*buf << (chunk * 8);
428
429 buf++;
430 chunk++;
431 len--;
432
433 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300434 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200435 chunk = 0;
436 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 }
439 }
Pierre Ossman76591502008-07-21 00:32:11 +0200440
441 sg_miter_stop(&host->sg_miter);
442
443 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100444}
445
446static void sdhci_transfer_pio(struct sdhci_host *host)
447{
448 u32 mask;
449
450 BUG_ON(!host->data);
451
Pierre Ossman76591502008-07-21 00:32:11 +0200452 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100453 return;
454
455 if (host->data->flags & MMC_DATA_READ)
456 mask = SDHCI_DATA_AVAILABLE;
457 else
458 mask = SDHCI_SPACE_AVAILABLE;
459
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200460 /*
461 * Some controllers (JMicron JMB38x) mess up the buffer bits
462 * for transfers < 4 bytes. As long as it is just one block,
463 * we can ignore the bits.
464 */
465 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
466 (host->data->blocks == 1))
467 mask = ~0;
468
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300469 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300470 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
471 udelay(100);
472
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100473 if (host->data->flags & MMC_DATA_READ)
474 sdhci_read_block_pio(host);
475 else
476 sdhci_write_block_pio(host);
477
Pierre Ossman76591502008-07-21 00:32:11 +0200478 host->blocks--;
479 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100480 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100481 }
482
483 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800484}
485
Pierre Ossman2134a922008-06-28 18:28:51 +0200486static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
487{
488 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800489 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200490}
491
492static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
493{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800494 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200495 local_irq_restore(*flags);
496}
497
Ben Dooks118cd172010-03-05 13:43:26 -0800498static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
499{
Ben Dooks9e506f32010-03-05 13:43:29 -0800500 __le32 *dataddr = (__le32 __force *)(desc + 4);
501 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800502
Ben Dooks9e506f32010-03-05 13:43:29 -0800503 /* SDHCI specification says ADMA descriptors should be 4 byte
504 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800505
Ben Dooks9e506f32010-03-05 13:43:29 -0800506 cmdlen[0] = cpu_to_le16(cmd);
507 cmdlen[1] = cpu_to_le16(len);
508
509 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800510}
511
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800512static int sdhci_pre_dma_transfer(struct sdhci_host *host,
513 struct mmc_data *data,
514 struct sdhci_next *next)
515{
516 int sg_count;
517
518 if (!next && data->host_cookie &&
519 data->host_cookie != host->next_data.cookie) {
520 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
521 " host->next_data.cookie %d\n",
522 __func__, data->host_cookie, host->next_data.cookie);
523 data->host_cookie = 0;
524 }
525
526 /* Check if next job is already prepared */
527 if (next ||
528 (!next && data->host_cookie != host->next_data.cookie)) {
529 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
530 data->sg_len,
531 (data->flags & MMC_DATA_WRITE) ?
532 DMA_TO_DEVICE : DMA_FROM_DEVICE);
533 } else {
534 sg_count = host->next_data.sg_count;
535 host->next_data.sg_count = 0;
536 }
537
538 if (sg_count == 0)
539 return -EINVAL;
540
541 if (next) {
542 next->sg_count = sg_count;
543 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
544 } else
545 host->sg_count = sg_count;
546
547 return sg_count;
548}
549
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200550static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200551 struct mmc_data *data)
552{
553 int direction;
554
555 u8 *desc;
556 u8 *align;
557 dma_addr_t addr;
558 dma_addr_t align_addr;
559 int len, offset;
560
561 struct scatterlist *sg;
562 int i;
563 char *buffer;
564 unsigned long flags;
565
566 /*
567 * The spec does not specify endianness of descriptor table.
568 * We currently guess that it is LE.
569 */
570
571 if (data->flags & MMC_DATA_READ)
572 direction = DMA_FROM_DEVICE;
573 else
574 direction = DMA_TO_DEVICE;
575
576 /*
577 * The ADMA descriptor table is mapped further down as we
578 * need to fill it with data first.
579 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530581 host->align_buffer,
582 host->align_buf_sz,
583 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700584 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200585 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200586 BUG_ON(host->align_addr & 0x3);
587
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800588 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
589 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200590 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200591
592 desc = host->adma_desc;
593 align = host->align_buffer;
594
595 align_addr = host->align_addr;
596
597 for_each_sg(data->sg, sg, host->sg_count, i) {
598 addr = sg_dma_address(sg);
599 len = sg_dma_len(sg);
600
601 /*
602 * The SDHCI specification states that ADMA
603 * addresses must be 32-bit aligned. If they
604 * aren't, then we use a bounce buffer for
605 * the (up to three) bytes that screw up the
606 * alignment.
607 */
608 offset = (4 - (addr & 0x3)) & 0x3;
609 if (offset) {
610 if (data->flags & MMC_DATA_WRITE) {
611 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200612 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200613 memcpy(align, buffer, offset);
614 sdhci_kunmap_atomic(buffer, &flags);
615 }
616
Ben Dooks118cd172010-03-05 13:43:26 -0800617 /* tran, valid */
618 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200619
620 BUG_ON(offset > 65536);
621
Pierre Ossman2134a922008-06-28 18:28:51 +0200622 align += 4;
623 align_addr += 4;
624
625 desc += 8;
626
627 addr += offset;
628 len -= offset;
629 }
630
Pierre Ossman2134a922008-06-28 18:28:51 +0200631 BUG_ON(len > 65536);
632
Ben Dooks118cd172010-03-05 13:43:26 -0800633 /* tran, valid */
634 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200635 desc += 8;
636
637 /*
638 * If this triggers then we have a calculation bug
639 * somewhere. :/
640 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530641 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
642
Pierre Ossman2134a922008-06-28 18:28:51 +0200643 }
644
Thomas Abraham70764a92010-05-26 14:42:04 -0700645 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
646 /*
647 * Mark the last descriptor as the terminating descriptor
648 */
649 if (desc != host->adma_desc) {
650 desc -= 8;
651 desc[0] |= 0x2; /* end */
652 }
653 } else {
654 /*
655 * Add a terminating entry.
656 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200657
Thomas Abraham70764a92010-05-26 14:42:04 -0700658 /* nop, end, valid */
659 sdhci_set_adma_desc(desc, 0, 0, 0x3);
660 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200661
662 /*
663 * Resync align buffer as we might have changed it.
664 */
665 if (data->flags & MMC_DATA_WRITE) {
666 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530667 host->align_addr,
668 host->align_buf_sz,
669 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200670 }
671
672 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530673 host->adma_desc,
674 host->adma_desc_sz,
675 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200676 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200677 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200678 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200679
680 return 0;
681
682unmap_entries:
683 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
684 data->sg_len, direction);
685unmap_align:
686 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530687 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200688fail:
689 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200690}
691
692static void sdhci_adma_table_post(struct sdhci_host *host,
693 struct mmc_data *data)
694{
695 int direction;
696
697 struct scatterlist *sg;
698 int i, size;
699 u8 *align;
700 char *buffer;
701 unsigned long flags;
702
703 if (data->flags & MMC_DATA_READ)
704 direction = DMA_FROM_DEVICE;
705 else
706 direction = DMA_TO_DEVICE;
707
708 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530709 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200710
711 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530712 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200713
714 if (data->flags & MMC_DATA_READ) {
715 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
716 data->sg_len, direction);
717
718 align = host->align_buffer;
719
720 for_each_sg(data->sg, sg, host->sg_count, i) {
721 if (sg_dma_address(sg) & 0x3) {
722 size = 4 - (sg_dma_address(sg) & 0x3);
723
724 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200725 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200726 memcpy(buffer, align, size);
727 sdhci_kunmap_atomic(buffer, &flags);
728
729 align += 4;
730 }
731 }
732 }
733
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800734 if (!data->host_cookie)
735 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
736 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200737}
738
Andrei Warkentina3c77782011-04-11 16:13:42 -0500739static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800740{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700741 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500742 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700743 unsigned target_timeout, current_timeout;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530744 u32 curr_clk = 0; /* In KHz */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800745
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200746 /*
747 * If the host controller provides us with an incorrect timeout
748 * value, just skip the check and use 0xE. The hardware may take
749 * longer to time out, but that's much better than having a too-short
750 * timeout value.
751 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200752 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200753 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200754
Andrei Warkentina3c77782011-04-11 16:13:42 -0500755 /* Unspecified timeout, assume max */
756 if (!data && !cmd->cmd_timeout_ms)
757 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800758
Andrei Warkentina3c77782011-04-11 16:13:42 -0500759 /* timeout in us */
760 if (!data)
761 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530762 else {
763 target_timeout = data->timeout_ns / 1000;
764 if (host->clock)
765 target_timeout += data->timeout_clks / host->clock;
766 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700767
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700768 /*
769 * Figure out needed cycles.
770 * We do this in steps in order to fit inside a 32 bit int.
771 * The first step is the minimum timeout, which will have a
772 * minimum resolution of 6 bits:
773 * (1) 2^13*1000 > 2^22,
774 * (2) host->timeout_clk < 2^16
775 * =>
776 * (1) / (2) > 2^6
777 */
778 count = 0;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530779 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) {
780 curr_clk = host->clock / 1000;
781 if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4)
782 curr_clk /= 4;
783 current_timeout = (1 << 13) * 1000 / curr_clk;
784 } else {
785 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
786 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700787 while (current_timeout < target_timeout) {
788 count++;
789 current_timeout <<= 1;
790 if (count >= 0xF)
791 break;
792 }
793
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530794 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
795 if (count >= 0xF) {
796 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
797 mmc_hostname(host->mmc), count, cmd->opcode);
798 count = 0xE;
799 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530800 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700801
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200802 return count;
803}
804
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300805static void sdhci_set_transfer_irqs(struct sdhci_host *host)
806{
807 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
808 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
809
810 if (host->flags & SDHCI_REQ_USE_DMA)
811 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
812 else
813 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
814}
815
Andrei Warkentina3c77782011-04-11 16:13:42 -0500816static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200817{
818 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200819 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500820 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200822
823 WARN_ON(host->data);
824
Andrei Warkentina3c77782011-04-11 16:13:42 -0500825 if (data || (cmd->flags & MMC_RSP_BUSY)) {
826 count = sdhci_calc_timeout(host, cmd);
827 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
828 }
829
830 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200831 return;
832
833 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530834 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200835 BUG_ON(data->blksz > host->mmc->max_blk_size);
836 BUG_ON(data->blocks > 65535);
837
838 host->data = data;
839 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400840 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200841
Richard Röjforsa13abc72009-09-22 16:45:30 -0700842 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100843 host->flags |= SDHCI_REQ_USE_DMA;
844
Pierre Ossman2134a922008-06-28 18:28:51 +0200845 /*
846 * FIXME: This doesn't account for merging when mapping the
847 * scatterlist.
848 */
849 if (host->flags & SDHCI_REQ_USE_DMA) {
850 int broken, i;
851 struct scatterlist *sg;
852
853 broken = 0;
854 if (host->flags & SDHCI_USE_ADMA) {
855 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
856 broken = 1;
857 } else {
858 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
859 broken = 1;
860 }
861
862 if (unlikely(broken)) {
863 for_each_sg(data->sg, sg, data->sg_len, i) {
864 if (sg->length & 0x3) {
865 DBG("Reverting to PIO because of "
866 "transfer size (%d)\n",
867 sg->length);
868 host->flags &= ~SDHCI_REQ_USE_DMA;
869 break;
870 }
871 }
872 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100873 }
874
875 /*
876 * The assumption here being that alignment is the same after
877 * translation to device address space.
878 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200879 if (host->flags & SDHCI_REQ_USE_DMA) {
880 int broken, i;
881 struct scatterlist *sg;
882
883 broken = 0;
884 if (host->flags & SDHCI_USE_ADMA) {
885 /*
886 * As we use 3 byte chunks to work around
887 * alignment problems, we need to check this
888 * quirk.
889 */
890 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
891 broken = 1;
892 } else {
893 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
894 broken = 1;
895 }
896
897 if (unlikely(broken)) {
898 for_each_sg(data->sg, sg, data->sg_len, i) {
899 if (sg->offset & 0x3) {
900 DBG("Reverting to PIO because of "
901 "bad alignment\n");
902 host->flags &= ~SDHCI_REQ_USE_DMA;
903 break;
904 }
905 }
906 }
907 }
908
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200909 if (host->flags & SDHCI_REQ_USE_DMA) {
910 if (host->flags & SDHCI_USE_ADMA) {
911 ret = sdhci_adma_table_pre(host, data);
912 if (ret) {
913 /*
914 * This only happens when someone fed
915 * us an invalid request.
916 */
917 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200918 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200919 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300920 sdhci_writel(host, host->adma_addr,
921 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200922 }
923 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300924 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200925
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800926 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300927 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200928 /*
929 * This only happens when someone fed
930 * us an invalid request.
931 */
932 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200933 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200934 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200935 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300936 sdhci_writel(host, sg_dma_address(data->sg),
937 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200938 }
939 }
940 }
941
Pierre Ossman2134a922008-06-28 18:28:51 +0200942 /*
943 * Always adjust the DMA selection as some controllers
944 * (e.g. JMicron) can't do PIO properly when the selection
945 * is ADMA.
946 */
947 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300948 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200949 ctrl &= ~SDHCI_CTRL_DMA_MASK;
950 if ((host->flags & SDHCI_REQ_USE_DMA) &&
951 (host->flags & SDHCI_USE_ADMA))
952 ctrl |= SDHCI_CTRL_ADMA32;
953 else
954 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300955 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100956 }
957
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200958 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200959 int flags;
960
961 flags = SG_MITER_ATOMIC;
962 if (host->data->flags & MMC_DATA_READ)
963 flags |= SG_MITER_TO_SG;
964 else
965 flags |= SG_MITER_FROM_SG;
966 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200967 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800968 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700969
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300970 sdhci_set_transfer_irqs(host);
971
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400972 /* Set the DMA boundary value and block size */
973 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
974 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300975 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700976}
977
978static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500979 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700980{
981 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500982 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700983
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700984 if (data == NULL)
985 return;
986
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200987 WARN_ON(!host->data);
988
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700989 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500990 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
991 mode |= SDHCI_TRNS_MULTI;
992 /*
993 * If we are sending CMD23, CMD12 never gets sent
994 * on successful completion (so no Auto-CMD12).
995 */
996 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
997 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500998 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
999 mode |= SDHCI_TRNS_AUTO_CMD23;
1000 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
1001 }
Jerry Huangc4512f72010-08-10 18:01:59 -07001002 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001003
Sahitya Tummala239e5a82013-02-25 15:45:32 +05301004 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001005 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +05301006 if (host->ops->toggle_cdr)
1007 host->ops->toggle_cdr(host, true);
1008 }
1009 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
1010 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001011 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001012 mode |= SDHCI_TRNS_DMA;
1013
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001014 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015}
1016
1017static void sdhci_finish_data(struct sdhci_host *host)
1018{
1019 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020
1021 BUG_ON(!host->data);
1022
1023 data = host->data;
1024 host->data = NULL;
1025
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001026 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001027 if (host->flags & SDHCI_USE_ADMA)
1028 sdhci_adma_table_post(host, data);
1029 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001030 if (!data->host_cookie)
1031 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1032 data->sg_len,
1033 (data->flags & MMC_DATA_READ) ?
1034 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +02001035 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 }
1037
1038 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001039 * The specification states that the block count register must
1040 * be updated, but it does not specify at what point in the
1041 * data flow. That makes the register entirely useless to read
1042 * back so we have to assume that nothing made it to the card
1043 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001045 if (data->error)
1046 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001048 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049
Andrei Warkentine89d4562011-05-23 15:06:37 -05001050 /*
1051 * Need to send CMD12 if -
1052 * a) open-ended multiblock transfer (no CMD23)
1053 * b) error in multiblock transfer
1054 */
1055 if (data->stop &&
1056 (data->error ||
1057 !host->mrq->sbc)) {
1058
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 /*
1060 * The controller needs a reset of internal state machines
1061 * upon error conditions.
1062 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001063 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064 sdhci_reset(host, SDHCI_RESET_CMD);
1065 sdhci_reset(host, SDHCI_RESET_DATA);
1066 }
1067
1068 sdhci_send_command(host, data->stop);
1069 } else
1070 tasklet_schedule(&host->finish_tasklet);
1071}
1072
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301073#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1074
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1076{
1077 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001078 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001079 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080
1081 WARN_ON(host->cmd);
1082
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001084 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001085
1086 mask = SDHCI_CMD_INHIBIT;
1087 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1088 mask |= SDHCI_DATA_INHIBIT;
1089
1090 /* We shouldn't wait for data inihibit for stop commands, even
1091 though they might use busy signaling */
1092 if (host->mrq->data && (cmd == host->mrq->data->stop))
1093 mask &= ~SDHCI_DATA_INHIBIT;
1094
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001095 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001096 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301097 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001098 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001100 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 tasklet_schedule(&host->finish_tasklet);
1102 return;
1103 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001104 timeout--;
1105 mdelay(1);
1106 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301108 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1109
1110 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1111 mod_timer(&host->timer, jiffies +
1112 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113
1114 host->cmd = cmd;
1115
Andrei Warkentina3c77782011-04-11 16:13:42 -05001116 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001118 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119
Andrei Warkentine89d4562011-05-23 15:06:37 -05001120 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001121
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301123 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001125 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126 tasklet_schedule(&host->finish_tasklet);
1127 return;
1128 }
1129
1130 if (!(cmd->flags & MMC_RSP_PRESENT))
1131 flags = SDHCI_CMD_RESP_NONE;
1132 else if (cmd->flags & MMC_RSP_136)
1133 flags = SDHCI_CMD_RESP_LONG;
1134 else if (cmd->flags & MMC_RSP_BUSY)
1135 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1136 else
1137 flags = SDHCI_CMD_RESP_SHORT;
1138
1139 if (cmd->flags & MMC_RSP_CRC)
1140 flags |= SDHCI_CMD_CRC;
1141 if (cmd->flags & MMC_RSP_OPCODE)
1142 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301143
1144 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301145 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001146 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400 ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301147 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 flags |= SDHCI_CMD_DATA;
1149
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301150 if (cmd->data)
1151 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001152 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153}
1154
1155static void sdhci_finish_command(struct sdhci_host *host)
1156{
1157 int i;
1158
1159 BUG_ON(host->cmd == NULL);
1160
1161 if (host->cmd->flags & MMC_RSP_PRESENT) {
1162 if (host->cmd->flags & MMC_RSP_136) {
1163 /* CRC is stripped so we need to do some shifting. */
1164 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001165 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166 SDHCI_RESPONSE + (3-i)*4) << 8;
1167 if (i != 3)
1168 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001169 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170 SDHCI_RESPONSE + (3-i)*4-1);
1171 }
1172 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001173 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001174 }
1175 }
1176
Pierre Ossman17b04292007-07-22 22:18:46 +02001177 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178
Andrei Warkentine89d4562011-05-23 15:06:37 -05001179 /* Finished CMD23, now send actual command. */
1180 if (host->cmd == host->mrq->sbc) {
1181 host->cmd = NULL;
1182 sdhci_send_command(host, host->mrq->cmd);
1183 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001184
Andrei Warkentine89d4562011-05-23 15:06:37 -05001185 /* Processed actual command. */
1186 if (host->data && host->data_early)
1187 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188
Andrei Warkentine89d4562011-05-23 15:06:37 -05001189 if (!host->cmd->data)
1190 tasklet_schedule(&host->finish_tasklet);
1191
1192 host->cmd = NULL;
1193 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001194}
1195
1196static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1197{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301198 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301199 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301200 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001201 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301202 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301204 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001205 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301206 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207
Sahitya Tummalaca422112013-02-22 12:15:54 +05301208 host->mmc->actual_clock = 0;
1209
Anton Vorontsov81146342009-03-17 00:13:59 +03001210 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301211 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001212 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301213 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001214 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301215 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001216 }
1217
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301218 if (host->clock)
1219 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001220
1221 if (clock == 0)
1222 goto out;
1223
Zhangfei Gao85105c52010-08-06 07:10:01 +08001224 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301225 /*
1226 * Check if the Host Controller supports Programmable Clock
1227 * Mode.
1228 */
1229 if (host->clk_mul) {
1230 u16 ctrl;
1231
1232 /*
1233 * We need to figure out whether the Host Driver needs
1234 * to select Programmable Clock Mode, or the value can
1235 * be set automatically by the Host Controller based on
1236 * the Preset Value registers.
1237 */
1238 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1239 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1240 for (div = 1; div <= 1024; div++) {
1241 if (((host->max_clk * host->clk_mul) /
1242 div) <= clock)
1243 break;
1244 }
1245 /*
1246 * Set Programmable Clock Mode in the Clock
1247 * Control register.
1248 */
1249 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301250 real_div = div;
1251 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301252 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001253 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301254 } else {
1255 /* Version 3.00 divisors must be a multiple of 2. */
1256 if (host->max_clk <= clock)
1257 div = 1;
1258 else {
1259 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1260 div += 2) {
1261 if ((host->max_clk / div) <= clock)
1262 break;
1263 }
1264 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301265 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301266 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001267 }
1268 } else {
1269 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001270 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001271 if ((host->max_clk / div) <= clock)
1272 break;
1273 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301274 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301275 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277
Sahitya Tummalaca422112013-02-22 12:15:54 +05301278 if (real_div)
1279 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1280
Sahitya Tummala00240122013-02-28 19:50:51 +05301281 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1282 div = 0;
1283
Arindam Nathc3ed3872011-05-05 12:19:06 +05301284 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001285 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1286 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001288 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001289
Chris Ball27f6cb12009-09-22 16:45:31 -07001290 /* Wait max 20 ms */
1291 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001292 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001293 & SDHCI_CLOCK_INT_STABLE)) {
1294 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301295 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001296 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301298 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001300 timeout--;
1301 mdelay(1);
1302 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303
1304 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001305 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306
1307out:
1308 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301309ret:
1310 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311}
1312
Sahitya Tummalaca422112013-02-22 12:15:54 +05301313static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001314{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001315 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001316
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001317 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001318 switch (1 << power) {
1319 case MMC_VDD_165_195:
1320 pwr = SDHCI_POWER_180;
1321 break;
1322 case MMC_VDD_29_30:
1323 case MMC_VDD_30_31:
1324 pwr = SDHCI_POWER_300;
1325 break;
1326 case MMC_VDD_32_33:
1327 case MMC_VDD_33_34:
1328 pwr = SDHCI_POWER_330;
1329 break;
1330 default:
1331 BUG();
1332 }
1333 }
1334
1335 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301336 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001337
Pierre Ossmanae628902009-05-03 20:45:03 +02001338 host->pwr = pwr;
1339
1340 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001341 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301342 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301343 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301344 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001345 }
1346
1347 /*
1348 * Spec says that we should clear the power reg before setting
1349 * a new value. Some controllers don't seem to like this though.
1350 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301351 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001352 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301353 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301354 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301355 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001356
Andres Salomone08c1692008-07-04 10:00:03 -07001357 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001358 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001359 * and set turn on power at the same time, so set the voltage first.
1360 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301361 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001362 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301363 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301364 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301365 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001366
1367 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001368
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001369 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301370 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301371 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001372
1373 /*
1374 * Some controllers need an extra 10ms delay of 10ms before they
1375 * can apply clock after applying power
1376 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001377 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001378 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301379
1380 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001381}
1382
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383/*****************************************************************************\
1384 * *
1385 * MMC callbacks *
1386 * *
1387\*****************************************************************************/
1388
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301389static int sdhci_enable(struct mmc_host *mmc)
1390{
1391 struct sdhci_host *host = mmc_priv(mmc);
1392
1393 if (host->cpu_dma_latency_us)
1394 pm_qos_update_request(&host->pm_qos_req_dma,
1395 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301396 if (host->ops->platform_bus_voting)
1397 host->ops->platform_bus_voting(host, 1);
1398
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301399 return 0;
1400}
1401
1402static int sdhci_disable(struct mmc_host *mmc)
1403{
1404 struct sdhci_host *host = mmc_priv(mmc);
1405
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301406 if (host->cpu_dma_latency_us) {
1407 /*
1408 * In performance mode, release QoS vote after a timeout to
1409 * make sure back-to-back requests don't suffer from latencies
1410 * that are involved to wake CPU from low power modes in cases
1411 * where the CPU goes into low power mode as soon as QoS vote is
1412 * released.
1413 */
1414 if (host->power_policy == SDHCI_PERFORMANCE_MODE)
1415 pm_qos_update_request_timeout(&host->pm_qos_req_dma,
1416 host->cpu_dma_latency_us,
1417 host->pm_qos_timeout_us);
1418 else
1419 pm_qos_update_request(&host->pm_qos_req_dma,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301420 PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301421 }
1422
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301423 if (host->ops->platform_bus_voting)
1424 host->ops->platform_bus_voting(host, 0);
1425
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301426 return 0;
1427}
1428
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301429static inline void sdhci_update_power_policy(struct sdhci_host *host,
1430 enum sdhci_power_policy policy)
1431{
1432 host->power_policy = policy;
1433}
1434
1435static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1436{
1437 int err = 0;
1438 struct sdhci_host *host = mmc_priv(mmc);
1439
1440 switch (state) {
1441 case MMC_LOAD_HIGH:
1442 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1443 break;
1444 case MMC_LOAD_LOW:
1445 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1446 break;
1447 default:
1448 err = -EINVAL;
1449 break;
1450 }
1451
1452 return err;
1453}
1454
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001455static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1456 bool is_first_req)
1457{
1458 struct sdhci_host *host = mmc_priv(mmc);
1459
1460 if (mrq->data->host_cookie) {
1461 mrq->data->host_cookie = 0;
1462 return;
1463 }
1464
1465 if (host->flags & SDHCI_REQ_USE_DMA)
1466 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1467 mrq->data->host_cookie = 0;
1468}
1469
1470static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1471 int err)
1472{
1473 struct sdhci_host *host = mmc_priv(mmc);
1474 struct mmc_data *data = mrq->data;
1475
1476 if (host->flags & SDHCI_REQ_USE_DMA) {
1477 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1478 (data->flags & MMC_DATA_WRITE) ?
1479 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1480 data->host_cookie = 0;
1481 }
1482}
1483
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301484static bool sdhci_check_state(struct sdhci_host *host)
1485{
1486 struct mmc_host *mmc = host->mmc;
1487
1488 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301489 (mmc_use_core_runtime_pm(mmc) ?
1490 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301491 return true;
1492 else
1493 return false;
1494}
1495
Pierre Ossmand129bce2006-03-24 03:18:17 -08001496static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1497{
1498 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001499 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001500 unsigned long flags;
Aaron Lu11052c72012-07-03 17:27:49 +08001501 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
1503 host = mmc_priv(mmc);
1504
Adrian Hunter50accb92011-10-03 15:33:34 +03001505 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301506 if (sdhci_check_state(host)) {
1507 sdhci_dump_state(host);
1508 WARN(1, "sdhci in bad state");
1509 mrq->cmd->error = -EIO;
1510 if (mrq->data)
1511 mrq->data->error = -EIO;
Asutosh Dasd9264b02014-02-21 11:28:36 +05301512 mmc_request_done(host->mmc, mrq);
1513 sdhci_runtime_pm_put(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301514 return;
1515 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001516
Pierre Ossmand129bce2006-03-24 03:18:17 -08001517 spin_lock_irqsave(&host->lock, flags);
1518
1519 WARN_ON(host->mrq != NULL);
1520
Pierre Ossmanf9134312008-12-21 17:01:48 +01001521#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001523#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001524
1525 /*
1526 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1527 * requests if Auto-CMD12 is enabled.
1528 */
1529 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001530 if (mrq->stop) {
1531 mrq->data->stop = NULL;
1532 mrq->stop = NULL;
1533 }
1534 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535
1536 host->mrq = mrq;
1537
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001538 /* If polling, assume that the card is always present. */
1539 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1540 present = true;
1541 else
1542 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1543 SDHCI_CARD_PRESENT;
1544
1545 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001546 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001547 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301548 } else {
1549 u32 present_state;
1550
1551 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1552 /*
1553 * Check if the re-tuning timer has already expired and there
1554 * is no on-going data transfer. If so, we need to execute
1555 * tuning procedure before sending command.
1556 */
Asutosh Das6ec99eb2013-12-12 16:12:43 +05301557 if ((mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK) &&
1558 (mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK_HS400) &&
1559 (mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
1560 (host->flags & SDHCI_NEEDS_RETUNING) &&
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301561 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ballcb778082012-11-05 14:29:49 -05001562 if (mmc->card) {
1563 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1564 tuning_opcode =
1565 mmc->card->type == MMC_TYPE_MMC ?
1566 MMC_SEND_TUNING_BLOCK_HS200 :
1567 MMC_SEND_TUNING_BLOCK;
Asutosh Das6ec99eb2013-12-12 16:12:43 +05301568 host->mrq = NULL;
Chris Ballcb778082012-11-05 14:29:49 -05001569 spin_unlock_irqrestore(&host->lock, flags);
1570 sdhci_execute_tuning(mmc, tuning_opcode);
1571 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301572
Chris Ballcb778082012-11-05 14:29:49 -05001573 /* Restore original mmc_request structure */
1574 host->mrq = mrq;
1575 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301576 }
1577
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001578 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001579 sdhci_send_command(host, mrq->sbc);
1580 else
1581 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301582 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583
Pierre Ossman5f25a662006-10-04 02:15:39 -07001584 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585 spin_unlock_irqrestore(&host->lock, flags);
1586}
1587
Asutosh Das08a00a32013-04-24 14:48:48 +05301588static void sdhci_cfg_async_intr(struct sdhci_host *host, bool enable)
1589{
1590 if (!host->async_int_supp)
1591 return;
1592
1593 if (enable)
1594 sdhci_writel(host,
1595 sdhci_readl(host, SDHCI_HOST_CONTROL2) |
1596 SDHCI_CTRL_ASYNC_INT_ENABLE,
1597 SDHCI_HOST_CONTROL2);
1598 else
1599 sdhci_writel(host, sdhci_readl(host, SDHCI_HOST_CONTROL2) &
1600 ~SDHCI_CTRL_ASYNC_INT_ENABLE,
1601 SDHCI_HOST_CONTROL2);
1602}
1603
1604static void sdhci_cfg_irq(struct sdhci_host *host, bool enable)
1605{
1606 if (enable && !host->irq_enabled) {
1607 enable_irq(host->irq);
1608 host->irq_enabled = true;
1609 } else if (!enable && host->irq_enabled) {
1610 disable_irq_nosync(host->irq);
1611 host->irq_enabled = false;
1612 }
1613}
1614
Adrian Hunter50accb92011-10-03 15:33:34 +03001615static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301618 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619 u8 ctrl;
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301620 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621
Sahitya Tummala40474e42013-07-10 14:40:37 +05301622 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301623 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301624 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1625 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301626 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301627 return;
1628 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001629
Asutosh Das08a00a32013-04-24 14:48:48 +05301630 spin_lock_irqsave(&host->lock, flags);
1631 /* lock is being released intermittently below, hence disable irq */
1632 sdhci_cfg_irq(host, false);
1633 spin_unlock_irqrestore(&host->lock, flags);
1634 if (ios->clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301635 sdhci_set_clock(host, ios->clock);
Asutosh Das08a00a32013-04-24 14:48:48 +05301636 if (host->async_int_supp && sdhci_get_async_int_status(host)) {
1637 if (host->disable_sdio_irq_deferred) {
1638 pr_debug("%s: %s: disable sdio irq\n",
1639 mmc_hostname(host->mmc), __func__);
1640 host->mmc->ops->enable_sdio_irq(host->mmc, 0);
1641 host->disable_sdio_irq_deferred = false;
1642 }
1643 spin_lock_irqsave(&host->lock, flags);
1644 sdhci_cfg_async_intr(host, false);
1645 spin_unlock_irqrestore(&host->lock, flags);
1646 pr_debug("%s: %s: unconfig async intr\n",
1647 mmc_hostname(host->mmc), __func__);
1648 }
1649 }
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301650 /*
1651 * The controller clocks may be off during power-up and we may end up
1652 * enabling card clock before giving power to the card. Hence, during
1653 * MMC_POWER_UP enable the controller clock and turn-on the regulators.
1654 * The mmc_power_up would provide the necessary delay before turning on
1655 * the clocks to the card.
1656 */
1657 if (ios->power_mode & MMC_POWER_UP) {
1658 if (host->ops->enable_controller_clock) {
1659 ret = host->ops->enable_controller_clock(host);
1660 if (ret) {
1661 pr_err("%s: enabling controller clock: failed: %d\n",
1662 mmc_hostname(host->mmc), ret);
1663 } else {
1664 vdd_bit = sdhci_set_power(host, ios->vdd);
1665
1666 if (host->vmmc && vdd_bit != -1)
1667 mmc_regulator_set_ocr(host->mmc,
1668 host->vmmc,
1669 vdd_bit);
1670 }
1671 }
1672 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301673 spin_lock_irqsave(&host->lock, flags);
1674 if (!host->clock) {
Asutosh Das08a00a32013-04-24 14:48:48 +05301675 sdhci_cfg_irq(host, true);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301676 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301677 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301678 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301680 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301682 if (!host->ops->enable_controller_clock && (ios->power_mode &
1683 (MMC_POWER_UP |
1684 MMC_POWER_ON))) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301685 vdd_bit = sdhci_set_power(host, ios->vdd);
1686
Asutosh Dasd02c09a2013-11-08 12:31:48 +05301687 if (host->vmmc && vdd_bit != -1)
1688 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1689 }
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301690
1691 spin_lock_irqsave(&host->lock, flags);
Philip Rakity643a81f2010-09-23 08:24:32 -07001692 if (host->ops->platform_send_init_74_clocks)
1693 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1694
Philip Rakity15ec4462010-11-19 16:48:39 -05001695 /*
1696 * If your platform has 8-bit width support but is not a v3 controller,
1697 * or if it requires special setup code, you should implement that in
1698 * platform_8bit_width().
1699 */
1700 if (host->ops->platform_8bit_width)
1701 host->ops->platform_8bit_width(host, ios->bus_width);
1702 else {
1703 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1704 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1705 ctrl &= ~SDHCI_CTRL_4BITBUS;
1706 if (host->version >= SDHCI_SPEC_300)
1707 ctrl |= SDHCI_CTRL_8BITBUS;
1708 } else {
1709 if (host->version >= SDHCI_SPEC_300)
1710 ctrl &= ~SDHCI_CTRL_8BITBUS;
1711 if (ios->bus_width == MMC_BUS_WIDTH_4)
1712 ctrl |= SDHCI_CTRL_4BITBUS;
1713 else
1714 ctrl &= ~SDHCI_CTRL_4BITBUS;
1715 }
1716 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1717 }
1718
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001719 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001720
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001721 if ((ios->timing == MMC_TIMING_SD_HS ||
1722 ios->timing == MMC_TIMING_MMC_HS)
1723 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001724 ctrl |= SDHCI_CTRL_HISPD;
1725 else
1726 ctrl &= ~SDHCI_CTRL_HISPD;
1727
Arindam Nathd6d50a12011-05-05 12:18:59 +05301728 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301729 u16 clk, ctrl_2;
1730 unsigned int clock;
1731
1732 /* In case of UHS-I modes, set High Speed Enable */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001733 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1734 (ios->timing == MMC_TIMING_MMC_HS200) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301735 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301736 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1737 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001738 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301739 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301740
1741 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1742 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301743 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301744 /*
1745 * We only need to set Driver Strength if the
1746 * preset value enable is not set.
1747 */
1748 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1749 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1750 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1751 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1752 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1753
1754 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301755 } else {
1756 /*
1757 * According to SDHC Spec v3.00, if the Preset Value
1758 * Enable in the Host Control 2 register is set, we
1759 * need to reset SD Clock Enable before changing High
1760 * Speed Enable to avoid generating clock gliches.
1761 */
Arindam Nath758535c2011-05-05 12:19:00 +05301762
1763 /* Reset SD Clock Enable */
1764 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1765 clk &= ~SDHCI_CLOCK_CARD_EN;
1766 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1767
1768 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1769
1770 /* Re-enable SD Clock */
1771 clock = host->clock;
1772 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301773 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301774 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301775 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301776 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301777
Arindam Nath49c468f2011-05-05 12:19:01 +05301778 /* Reset SD Clock Enable */
1779 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1780 clk &= ~SDHCI_CLOCK_CARD_EN;
1781 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1782
Philip Rakity6322cdd2011-05-13 11:17:15 +05301783 if (host->ops->set_uhs_signaling)
1784 host->ops->set_uhs_signaling(host, ios->timing);
1785 else {
1786 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1787 /* Select Bus Speed Mode for host */
1788 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001789 if (ios->timing == MMC_TIMING_MMC_HS400)
1790 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1791 else if (ios->timing == MMC_TIMING_MMC_HS200)
Girish K S2cd06dc2012-01-06 09:56:39 +05301792 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1793 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301794 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1795 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1796 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1797 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1798 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1799 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1800 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1801 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1802 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1803 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1804 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301805
1806 /* Re-enable SD Clock */
1807 clock = host->clock;
1808 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301809 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301810 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301811 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301812 } else
1813 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301814
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301815 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001816 /*
1817 * Some (ENE) controllers go apeshit on some ios operation,
1818 * signalling timeout and CRC errors even on CMD0. Resetting
1819 * it on each ios seems to solve the problem.
1820 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001821 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001822 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1823
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301824 /*
1825 * Reset the chip on each power off.
1826 * Should clear out any weird states.
1827 */
1828 if (ios->power_mode == MMC_POWER_OFF) {
1829 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1830 sdhci_reinit(host);
1831 vdd_bit = sdhci_set_power(host, -1);
1832 if (host->vmmc && vdd_bit != -1)
1833 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1834 }
Asutosh Das08a00a32013-04-24 14:48:48 +05301835 if (!ios->clock) {
1836 if (host->async_int_supp && host->mmc->card &&
1837 mmc_card_sdio(host->mmc->card)) {
1838 sdhci_cfg_async_intr(host, true);
1839 pr_debug("%s: %s: config async intr\n",
1840 mmc_hostname(host->mmc), __func__);
1841 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301842 sdhci_set_clock(host, ios->clock);
Asutosh Das08a00a32013-04-24 14:48:48 +05301843 }
1844 spin_lock_irqsave(&host->lock, flags);
1845 sdhci_cfg_irq(host, true);
1846 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman5f25a662006-10-04 02:15:39 -07001847 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301848 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001849}
1850
Adrian Hunter50accb92011-10-03 15:33:34 +03001851static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1852{
1853 struct sdhci_host *host = mmc_priv(mmc);
1854
1855 sdhci_runtime_pm_get(host);
1856 sdhci_do_set_ios(host, ios);
1857 sdhci_runtime_pm_put(host);
1858}
1859
1860static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001861{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001862 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001863 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001864
Pierre Ossmand129bce2006-03-24 03:18:17 -08001865 spin_lock_irqsave(&host->lock, flags);
1866
Pierre Ossman1e728592008-04-16 19:13:13 +02001867 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001868 is_readonly = 0;
1869 else if (host->ops->get_ro)
1870 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001871 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001872 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1873 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874
1875 spin_unlock_irqrestore(&host->lock, flags);
1876
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001877 /* This quirk needs to be replaced by a callback-function later */
1878 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1879 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001880}
1881
Takashi Iwai82b0e232011-04-21 20:26:38 +02001882#define SAMPLE_COUNT 5
1883
Adrian Hunter50accb92011-10-03 15:33:34 +03001884static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001885{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001886 int i, ro_count;
1887
Takashi Iwai82b0e232011-04-21 20:26:38 +02001888 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001889 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001890
1891 ro_count = 0;
1892 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001893 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001894 if (++ro_count > SAMPLE_COUNT / 2)
1895 return 1;
1896 }
1897 msleep(30);
1898 }
1899 return 0;
1900}
1901
Adrian Hunter50accb92011-10-03 15:33:34 +03001902static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001903{
Adrian Hunter50accb92011-10-03 15:33:34 +03001904 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001905
Adrian Hunter50accb92011-10-03 15:33:34 +03001906 if (host->ops && host->ops->hw_reset)
1907 host->ops->hw_reset(host);
1908}
Adrian Hunter20758b62011-08-29 16:42:12 +03001909
Adrian Hunter50accb92011-10-03 15:33:34 +03001910static int sdhci_get_ro(struct mmc_host *mmc)
1911{
1912 struct sdhci_host *host = mmc_priv(mmc);
1913 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001914
Adrian Hunter50accb92011-10-03 15:33:34 +03001915 sdhci_runtime_pm_get(host);
1916 ret = sdhci_do_get_ro(host);
1917 sdhci_runtime_pm_put(host);
1918 return ret;
1919}
1920
1921static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1922{
Pierre Ossman1e728592008-04-16 19:13:13 +02001923 if (host->flags & SDHCI_DEVICE_DEAD)
1924 goto out;
1925
Asutosh Das08a00a32013-04-24 14:48:48 +05301926 if (!enable && !host->clock) {
1927 pr_debug("%s: %s: defered disabling card intr\n",
1928 host->mmc ? mmc_hostname(host->mmc) : "null",
1929 __func__);
1930 host->disable_sdio_irq_deferred = true;
1931 return;
1932 }
1933
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001934 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001935 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1936 else
1937 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1938
1939 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1940 if (host->runtime_suspended)
1941 goto out;
1942
1943 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001944 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1945 else
1946 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001947out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001948 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001949}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001950
Adrian Hunter50accb92011-10-03 15:33:34 +03001951static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1952{
1953 struct sdhci_host *host = mmc_priv(mmc);
1954 unsigned long flags;
1955
1956 spin_lock_irqsave(&host->lock, flags);
1957 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001958 spin_unlock_irqrestore(&host->lock, flags);
1959}
1960
Adrian Hunter50accb92011-10-03 15:33:34 +03001961static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1962 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301963{
Arindam Nathf2119df2011-05-05 12:18:57 +05301964 u8 pwr;
1965 u16 clk, ctrl;
1966 u32 present_state;
1967
Arindam Nathf2119df2011-05-05 12:18:57 +05301968 /*
1969 * Signal Voltage Switching is only applicable for Host Controllers
1970 * v3.00 and above.
1971 */
1972 if (host->version < SDHCI_SPEC_300)
1973 return 0;
1974
1975 /*
1976 * We first check whether the request is to set signalling voltage
1977 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1978 */
1979 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1980 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1981 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1982 ctrl &= ~SDHCI_CTRL_VDD_180;
1983 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301984 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301985 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301986
1987 /* Wait for 5ms */
1988 usleep_range(5000, 5500);
1989
1990 /* 3.3V regulator output should be stable within 5 ms */
1991 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1992 if (!(ctrl & SDHCI_CTRL_VDD_180))
1993 return 0;
1994 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301995 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301996 "signalling voltage failed\n");
1997 return -EIO;
1998 }
1999 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
2000 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
2001 /* Stop SDCLK */
2002 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2003 clk &= ~SDHCI_CLOCK_CARD_EN;
2004 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2005
2006 /* Check whether DAT[3:0] is 0000 */
2007 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2008 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
2009 SDHCI_DATA_LVL_SHIFT)) {
2010 /*
2011 * Enable 1.8V Signal Enable in the Host Control2
2012 * register
2013 */
2014 ctrl |= SDHCI_CTRL_VDD_180;
2015 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05302016 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05302017 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05302018
2019 /* Wait for 5ms */
2020 usleep_range(5000, 5500);
2021
2022 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2023 if (ctrl & SDHCI_CTRL_VDD_180) {
2024 /* Provide SDCLK again and wait for 1ms*/
2025 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2026 clk |= SDHCI_CLOCK_CARD_EN;
2027 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2028 usleep_range(1000, 1500);
2029
2030 /*
2031 * If DAT[3:0] level is 1111b, then the card
2032 * was successfully switched to 1.8V signaling.
2033 */
2034 present_state = sdhci_readl(host,
2035 SDHCI_PRESENT_STATE);
2036 if ((present_state & SDHCI_DATA_LVL_MASK) ==
2037 SDHCI_DATA_LVL_MASK)
2038 return 0;
2039 }
2040 }
2041
2042 /*
2043 * If we are here, that means the switch to 1.8V signaling
2044 * failed. We power cycle the card, and retry initialization
2045 * sequence by setting S18R to 0.
2046 */
2047 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
2048 pwr &= ~SDHCI_POWER_ON;
2049 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05302050 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05302051 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05302052
2053 /* Wait for 1ms as per the spec */
2054 usleep_range(1000, 1500);
2055 pwr |= SDHCI_POWER_ON;
2056 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05302057 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05302058 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05302059
Sahitya Tummalaca422112013-02-22 12:15:54 +05302060 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05302061 "voltage failed, retrying with S18R set to 0\n");
2062 return -EAGAIN;
2063 } else
2064 /* No signal voltage switch required */
2065 return 0;
2066}
2067
Adrian Hunter50accb92011-10-03 15:33:34 +03002068static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
2069 struct mmc_ios *ios)
2070{
2071 struct sdhci_host *host = mmc_priv(mmc);
2072 int err;
2073
2074 if (host->version < SDHCI_SPEC_300)
2075 return 0;
2076 sdhci_runtime_pm_get(host);
2077 err = sdhci_do_start_signal_voltage_switch(host, ios);
2078 sdhci_runtime_pm_put(host);
2079 return err;
2080}
2081
Girish K S2cd06dc2012-01-06 09:56:39 +05302082static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302083{
2084 struct sdhci_host *host;
2085 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05302086 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05302087 int tuning_loop_counter = MAX_TUNING_LOOP;
2088 unsigned long timeout;
2089 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05302090 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05302091
2092 host = mmc_priv(mmc);
2093
Adrian Hunter50accb92011-10-03 15:33:34 +03002094 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302095 disable_irq(host->irq);
2096 spin_lock(&host->lock);
2097
2098 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2099
2100 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05302101 * The Host Controller needs tuning only in case of SDR104 mode
2102 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05302103 * Capabilities register.
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002104 * If the Host Controller supports the HS400/HS200 mode then the
Girish K S2cd06dc2012-01-06 09:56:39 +05302105 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302106 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08002107 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
2108 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002109 (host->flags & SDHCI_HS200_NEEDS_TUNING) ||
2110 (host->flags & SDHCI_HS400_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05302111 requires_tuning_nonuhs = true;
2112
Arindam Nathb513ea22011-05-05 12:19:04 +05302113 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05302114 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05302115 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2116 else {
2117 spin_unlock(&host->lock);
2118 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002119 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302120 return 0;
2121 }
2122
Asutosh Das8ddd3482013-01-04 11:45:46 +05302123 if (host->ops->execute_tuning) {
2124 spin_unlock(&host->lock);
2125 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05302126 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05302127 disable_irq(host->irq);
2128 spin_lock(&host->lock);
2129 goto out;
2130 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302131 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2132
2133 /*
2134 * As per the Host Controller spec v3.00, tuning command
2135 * generates Buffer Read Ready interrupt, so enable that.
2136 *
2137 * Note: The spec clearly says that when tuning sequence
2138 * is being performed, the controller does not generate
2139 * interrupts other than Buffer Read Ready interrupt. But
2140 * to make sure we don't hit a controller bug, we _only_
2141 * enable Buffer Read Ready interrupt here.
2142 */
2143 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
2144 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
2145
2146 /*
2147 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
2148 * of loops reaches 40 times or a timeout of 150ms occurs.
2149 */
2150 timeout = 150;
2151 do {
2152 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03002153 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302154
2155 if (!tuning_loop_counter && !timeout)
2156 break;
2157
Girish K S2cd06dc2012-01-06 09:56:39 +05302158 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302159 cmd.arg = 0;
2160 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2161 cmd.retries = 0;
2162 cmd.data = NULL;
2163 cmd.error = 0;
2164
2165 mrq.cmd = &cmd;
2166 host->mrq = &mrq;
2167
2168 /*
2169 * In response to CMD19, the card sends 64 bytes of tuning
2170 * block to the Host Controller. So we set the block size
2171 * to 64 here.
2172 */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002173 if ((cmd.opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2174 (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200)) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302175 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2176 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2177 SDHCI_BLOCK_SIZE);
2178 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2179 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2180 SDHCI_BLOCK_SIZE);
2181 } else {
2182 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2183 SDHCI_BLOCK_SIZE);
2184 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302185
2186 /*
2187 * The tuning block is sent by the card to the host controller.
2188 * So we set the TRNS_READ bit in the Transfer Mode register.
2189 * This also takes care of setting DMA Enable and Multi Block
2190 * Select in the same register to 0.
2191 */
2192 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2193
2194 sdhci_send_command(host, &cmd);
2195
2196 host->cmd = NULL;
2197 host->mrq = NULL;
2198
2199 spin_unlock(&host->lock);
2200 enable_irq(host->irq);
2201
2202 /* Wait for Buffer Read Ready interrupt */
2203 wait_event_interruptible_timeout(host->buf_ready_int,
2204 (host->tuning_done == 1),
2205 msecs_to_jiffies(50));
2206 disable_irq(host->irq);
2207 spin_lock(&host->lock);
2208
2209 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302210 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302211 "Buffer Read Ready interrupt during tuning "
2212 "procedure, falling back to fixed sampling "
2213 "clock\n");
2214 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2215 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2216 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2217 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2218
2219 err = -EIO;
2220 goto out;
2221 }
2222
2223 host->tuning_done = 0;
2224
2225 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2226 tuning_loop_counter--;
2227 timeout--;
2228 mdelay(1);
2229 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2230
2231 /*
2232 * The Host Driver has exhausted the maximum number of loops allowed,
2233 * so use fixed sampling frequency.
2234 */
2235 if (!tuning_loop_counter || !timeout) {
2236 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2237 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2238 } else {
2239 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302240 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302241 " failed, falling back to fixed sampling"
2242 " clock\n");
2243 err = -EIO;
2244 }
2245 }
2246
2247out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302248 /*
2249 * If this is the very first time we are here, we start the retuning
2250 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2251 * flag won't be set, we check this condition before actually starting
2252 * the timer.
2253 */
2254 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2255 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2256 mod_timer(&host->tuning_timer, jiffies +
2257 host->tuning_count * HZ);
2258 /* Tuning mode 1 limits the maximum data length to 4MB */
2259 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2260 } else {
2261 host->flags &= ~SDHCI_NEEDS_RETUNING;
2262 /* Reload the new initial value for timer */
2263 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2264 mod_timer(&host->tuning_timer, jiffies +
2265 host->tuning_count * HZ);
2266 }
2267
2268 /*
2269 * In case tuning fails, host controllers which support re-tuning can
2270 * try tuning again at a later time, when the re-tuning timer expires.
2271 * So for these controllers, we return 0. Since there might be other
2272 * controllers who do not have this capability, we return error for
2273 * them.
2274 */
2275 if (err && host->tuning_count &&
2276 host->tuning_mode == SDHCI_TUNING_MODE_1)
2277 err = 0;
2278
Arindam Nathb513ea22011-05-05 12:19:04 +05302279 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2280 spin_unlock(&host->lock);
2281 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002282 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302283
2284 return err;
2285}
2286
Adrian Hunter50accb92011-10-03 15:33:34 +03002287static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302288{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302289 u16 ctrl;
2290 unsigned long flags;
2291
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302292 /* Host Controller v3.00 defines preset value registers */
2293 if (host->version < SDHCI_SPEC_300)
2294 return;
2295
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302296 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2297 return;
2298
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302299 spin_lock_irqsave(&host->lock, flags);
2300
2301 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2302
2303 /*
2304 * We only enable or disable Preset Value if they are not already
2305 * enabled or disabled respectively. Otherwise, we bail out.
2306 */
2307 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2308 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2309 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002310 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302311 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2312 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2313 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002314 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302315 }
2316
2317 spin_unlock_irqrestore(&host->lock, flags);
2318}
2319
Adrian Hunter50accb92011-10-03 15:33:34 +03002320static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2321{
2322 struct sdhci_host *host = mmc_priv(mmc);
2323
2324 sdhci_runtime_pm_get(host);
2325 sdhci_do_enable_preset_value(host, enable);
2326 sdhci_runtime_pm_put(host);
2327}
2328
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002329static int sdhci_stop_request(struct mmc_host *mmc)
2330{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002331 struct sdhci_host *host = mmc_priv(mmc);
2332 unsigned long flags;
2333 struct mmc_data *data;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002334 int ret = 0;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002335
2336 spin_lock_irqsave(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002337 if (!host->mrq || !host->data) {
2338 ret = MMC_BLK_NO_REQ_TO_STOP;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002339 goto out;
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002340 }
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002341
2342 data = host->data;
2343
2344 if (host->ops->disable_data_xfer)
2345 host->ops->disable_data_xfer(host);
2346
2347 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2348
2349 if (host->flags & SDHCI_REQ_USE_DMA) {
2350 if (host->flags & SDHCI_USE_ADMA) {
2351 sdhci_adma_table_post(host, data);
2352 } else {
2353 if (!data->host_cookie)
2354 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2355 data->sg_len,
2356 (data->flags & MMC_DATA_READ) ?
2357 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2358 }
2359 }
2360 del_timer(&host->timer);
2361 host->mrq = NULL;
2362 host->cmd = NULL;
2363 host->data = NULL;
2364out:
2365 spin_unlock_irqrestore(&host->lock, flags);
Konstantin Dorfman66e7ce72013-09-22 15:43:24 +03002366 return ret;
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002367}
2368
2369static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2370{
2371 struct sdhci_host *host = mmc_priv(mmc);
2372 u32 present_state = 0;
2373
2374 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2375
2376 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002377}
2378
David Brownellab7aefd2006-11-12 17:55:30 -08002379static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002380 .pre_req = sdhci_pre_req,
2381 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382 .request = sdhci_request,
2383 .set_ios = sdhci_set_ios,
2384 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002385 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002386 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302387 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302388 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302389 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302390 .enable = sdhci_enable,
2391 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002392 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002393 .get_xfer_remain = sdhci_get_xfer_remain,
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05302394 .notify_load = sdhci_notify_load,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395};
2396
2397/*****************************************************************************\
2398 * *
2399 * Tasklets *
2400 * *
2401\*****************************************************************************/
2402
2403static void sdhci_tasklet_card(unsigned long param)
2404{
2405 struct sdhci_host *host;
2406 unsigned long flags;
2407
2408 host = (struct sdhci_host*)param;
2409
2410 spin_lock_irqsave(&host->lock, flags);
2411
Adrian Hunter50accb92011-10-03 15:33:34 +03002412 /* Check host->mrq first in case we are runtime suspended */
2413 if (host->mrq &&
2414 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302415 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002416 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302417 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002418 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419
Adrian Hunter50accb92011-10-03 15:33:34 +03002420 sdhci_reset(host, SDHCI_RESET_CMD);
2421 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002422
Adrian Hunter50accb92011-10-03 15:33:34 +03002423 host->mrq->cmd->error = -ENOMEDIUM;
2424 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425 }
2426
2427 spin_unlock_irqrestore(&host->lock, flags);
2428
Pierre Ossman04cf5852008-08-18 22:18:14 +02002429 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430}
2431
2432static void sdhci_tasklet_finish(unsigned long param)
2433{
2434 struct sdhci_host *host;
2435 unsigned long flags;
2436 struct mmc_request *mrq;
2437
2438 host = (struct sdhci_host*)param;
2439
Adrian Hunter50accb92011-10-03 15:33:34 +03002440 spin_lock_irqsave(&host->lock, flags);
2441
Chris Ball0c9c99a2011-04-27 17:35:31 -04002442 /*
2443 * If this tasklet gets rescheduled while running, it will
2444 * be run again afterwards but without any active request.
2445 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002446 if (!host->mrq) {
2447 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002448 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002449 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450
2451 del_timer(&host->timer);
2452
2453 mrq = host->mrq;
2454
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455 /*
2456 * The controller needs a reset of internal state machines
2457 * upon error conditions.
2458 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002459 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002460 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002461 (mrq->data && (mrq->data->error ||
2462 (mrq->data->stop && mrq->data->stop->error))) ||
2463 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002464
2465 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002466 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002467 unsigned int clock;
2468
2469 /* This is to force an update */
2470 clock = host->clock;
2471 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302472 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002473 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302474 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002475 }
2476
2477 /* Spec says we should do both at the same time, but Ricoh
2478 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002479 sdhci_reset(host, SDHCI_RESET_CMD);
2480 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002481 } else {
2482 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2483 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002484 }
2485
2486 host->mrq = NULL;
2487 host->cmd = NULL;
2488 host->data = NULL;
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302489 host->auto_cmd_err_sts = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002490
Pierre Ossmanf9134312008-12-21 17:01:48 +01002491#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002493#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002494
Pierre Ossman5f25a662006-10-04 02:15:39 -07002495 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002496 spin_unlock_irqrestore(&host->lock, flags);
2497
2498 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002499 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500}
2501
2502static void sdhci_timeout_timer(unsigned long data)
2503{
2504 struct sdhci_host *host;
2505 unsigned long flags;
2506
2507 host = (struct sdhci_host*)data;
2508
2509 spin_lock_irqsave(&host->lock, flags);
2510
2511 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302512 if (!host->mrq->cmd->ignore_timeout) {
2513 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2514 mmc_hostname(host->mmc));
2515 sdhci_dumpregs(host);
2516 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002517
2518 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302519 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2520 mmc_hostname(host->mmc),
2521 (host->data->blksz * host->data->blocks),
2522 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2523 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002524 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002525 sdhci_finish_data(host);
2526 } else {
2527 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002528 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002529 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002530 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002531
2532 tasklet_schedule(&host->finish_tasklet);
2533 }
2534 }
2535
Pierre Ossman5f25a662006-10-04 02:15:39 -07002536 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537 spin_unlock_irqrestore(&host->lock, flags);
2538}
2539
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302540static void sdhci_tuning_timer(unsigned long data)
2541{
2542 struct sdhci_host *host;
2543 unsigned long flags;
2544
2545 host = (struct sdhci_host *)data;
2546
2547 spin_lock_irqsave(&host->lock, flags);
2548
2549 host->flags |= SDHCI_NEEDS_RETUNING;
2550
2551 spin_unlock_irqrestore(&host->lock, flags);
2552}
2553
Pierre Ossmand129bce2006-03-24 03:18:17 -08002554/*****************************************************************************\
2555 * *
2556 * Interrupt handling *
2557 * *
2558\*****************************************************************************/
2559
2560static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2561{
Asutosh Das80c02552013-07-23 16:20:34 +05302562 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002563 BUG_ON(intmask == 0);
2564
2565 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302566 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002567 "though no command operation was in progress.\n",
2568 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002569 sdhci_dumpregs(host);
2570 return;
2571 }
2572
Pierre Ossman43b58b32007-07-25 23:15:27 +02002573 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002574 host->cmd->error = -ETIMEDOUT;
2575 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2576 SDHCI_INT_INDEX))
2577 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002578
Asutosh Das80c02552013-07-23 16:20:34 +05302579 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302580 auto_cmd_status = host->auto_cmd_err_sts;
2581 pr_err("%s: %s: AUTO CMD err sts 0x%08x\n",
2582 mmc_hostname(host->mmc), __func__, auto_cmd_status);
Asutosh Das80c02552013-07-23 16:20:34 +05302583 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2584 SDHCI_AUTO_CMD_INDEX_ERR |
2585 SDHCI_AUTO_CMD_ENDBIT_ERR))
2586 host->cmd->error = -EIO;
2587 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2588 host->cmd->error = -ETIMEDOUT;
2589 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2590 host->cmd->error = -EILSEQ;
2591 }
2592
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302593 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002594 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2595 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302596 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2597 if (intmask & SDHCI_INT_CRC) {
2598 sdhci_reset(host, SDHCI_RESET_CMD);
2599 host->cmd->error = 0;
2600 }
2601 }
2602 }
2603
Pierre Ossmane8095172008-07-25 01:09:08 +02002604 if (host->cmd->error) {
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302605 if (host->cmd->error == -EILSEQ)
2606 host->flags |= SDHCI_NEEDS_RETUNING;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002608 return;
2609 }
2610
2611 /*
2612 * The host can send and interrupt when the busy state has
2613 * ended, allowing us to wait without wasting CPU cycles.
2614 * Unfortunately this is overloaded on the "data complete"
2615 * interrupt, so we need to take some care when handling
2616 * it.
2617 *
2618 * Note: The 1.0 specification is a bit ambiguous about this
2619 * feature so there might be some problems with older
2620 * controllers.
2621 */
2622 if (host->cmd->flags & MMC_RSP_BUSY) {
2623 if (host->cmd->data)
2624 DBG("Cannot wait for busy signal when also "
2625 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002626 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002627 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002628
2629 /* The controller does not support the end-of-busy IRQ,
2630 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002631 }
2632
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302633 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002634 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2635 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302636 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2637 if (intmask & SDHCI_INT_CRC) {
2638 sdhci_finish_command(host);
2639 return;
2640 }
2641 }
2642 }
2643
Pierre Ossmane8095172008-07-25 01:09:08 +02002644 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002645 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002646}
2647
Ben Dooks6882a8c2009-06-14 13:52:38 +01002648static void sdhci_show_adma_error(struct sdhci_host *host)
2649{
2650 const char *name = mmc_hostname(host->mmc);
2651 u8 *desc = host->adma_desc;
2652 __le32 *dma;
2653 __le16 *len;
2654 u8 attr;
2655
2656 sdhci_dumpregs(host);
2657
2658 while (true) {
2659 dma = (__le32 *)(desc + 4);
2660 len = (__le16 *)(desc + 2);
2661 attr = *desc;
2662
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302663 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002664 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2665
2666 desc += 8;
2667
2668 if (attr & 2)
2669 break;
2670 }
2671}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002672
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2674{
Girish K S2cd06dc2012-01-06 09:56:39 +05302675 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302676 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677 BUG_ON(intmask == 0);
2678
Arindam Nathb513ea22011-05-05 12:19:04 +05302679 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2680 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302681 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2682 if (command == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002683 command == MMC_SEND_TUNING_BLOCK_HS200 ||
2684 command == MMC_SEND_TUNING_BLOCK_HS400) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302685 host->tuning_done = 1;
2686 wake_up(&host->buf_ready_int);
2687 return;
2688 }
2689 }
2690
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691 if (!host->data) {
2692 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002693 * The "data complete" interrupt is also used to
2694 * indicate that a busy state has ended. See comment
2695 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002696 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002697 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2698 if (intmask & SDHCI_INT_DATA_END) {
2699 sdhci_finish_command(host);
2700 return;
2701 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302702 if (host->quirks2 &
2703 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2704 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002705 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002706
Sahitya Tummalaca422112013-02-22 12:15:54 +05302707 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002708 "though no data operation was in progress.\n",
2709 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002710 sdhci_dumpregs(host);
2711
2712 return;
2713 }
2714
2715 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002716 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002717 else if (intmask & SDHCI_INT_DATA_END_BIT)
2718 host->data->error = -EILSEQ;
2719 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2720 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2721 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002722 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002723 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302724 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002725 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002726 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002727 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302728 if (host->data->error) {
2729 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2730 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2731 command = SDHCI_GET_CMD(sdhci_readw(host,
2732 SDHCI_COMMAND));
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002733 if ((command != MMC_SEND_TUNING_BLOCK_HS400) &&
2734 (command != MMC_SEND_TUNING_BLOCK_HS200) &&
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302735 (command != MMC_SEND_TUNING_BLOCK)) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302736 pr_msg = true;
Asutosh Das6ec99eb2013-12-12 16:12:43 +05302737 if (intmask & SDHCI_INT_DATA_CRC)
2738 host->flags |= SDHCI_NEEDS_RETUNING;
2739 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302740 } else {
2741 pr_msg = true;
2742 }
2743 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302744 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302745 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302746 host->data->error, ktime_to_ms(ktime_sub(
2747 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302748 sdhci_dumpregs(host);
2749 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002750 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302751 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002752 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002753 sdhci_transfer_pio(host);
2754
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002755 /*
2756 * We currently don't do anything fancy with DMA
2757 * boundaries, but as we can't disable the feature
2758 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002759 *
2760 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2761 * should return a valid address to continue from, but as
2762 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002763 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002764 if (intmask & SDHCI_INT_DMA_END) {
2765 u32 dmastart, dmanow;
2766 dmastart = sg_dma_address(host->data->sg);
2767 dmanow = dmastart + host->data->bytes_xfered;
2768 /*
2769 * Force update to the next DMA block boundary.
2770 */
2771 dmanow = (dmanow &
2772 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2773 SDHCI_DEFAULT_BOUNDARY_SIZE;
2774 host->data->bytes_xfered = dmanow - dmastart;
2775 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2776 " next 0x%08x\n",
2777 mmc_hostname(host->mmc), dmastart,
2778 host->data->bytes_xfered, dmanow);
2779 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2780 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002781
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002782 if (intmask & SDHCI_INT_DATA_END) {
2783 if (host->cmd) {
2784 /*
2785 * Data managed to finish before the
2786 * command completed. Make sure we do
2787 * things in the proper order.
2788 */
2789 host->data_early = 1;
2790 } else {
2791 sdhci_finish_data(host);
2792 }
2793 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794 }
2795}
2796
David Howells7d12e782006-10-05 14:55:46 +01002797static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002798{
2799 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002800 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002801 u32 intmask, unexpected = 0;
2802 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002803
2804 spin_lock(&host->lock);
2805
Adrian Hunter50accb92011-10-03 15:33:34 +03002806 if (host->runtime_suspended) {
2807 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302808 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002809 mmc_hostname(host->mmc));
2810 return IRQ_HANDLED;
2811 }
2812
Asutosh Das08a00a32013-04-24 14:48:48 +05302813 if (!host->clock && host->mmc->card &&
2814 mmc_card_sdio(host->mmc->card)) {
2815 /* SDIO async. interrupt is level-sensitive */
2816 sdhci_cfg_irq(host, false);
2817 pr_debug("%s: got async-irq: clocks: %d gated: %d host-irq[en:1/dis:0]: %d\n",
2818 mmc_hostname(host->mmc), host->clock,
2819 host->mmc->clk_gated, host->irq_enabled);
2820 spin_unlock(&host->lock);
2821 mmc_signal_sdio_irq(host->mmc);
2822 return IRQ_HANDLED;
2823 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002824 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002825
Mark Lord62df67a2007-03-06 13:30:13 +01002826 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002827 result = IRQ_NONE;
2828 goto out;
2829 }
2830
Alexander Stein6379b232012-03-14 09:52:10 +01002831again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002832 DBG("*** %s got interrupt: 0x%08x\n",
2833 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834
Pierre Ossman3192a282006-06-30 02:22:26 -07002835 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302836 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2837 SDHCI_CARD_PRESENT;
2838
2839 /*
2840 * There is a observation on i.mx esdhc. INSERT bit will be
2841 * immediately set again when it gets cleared, if a card is
2842 * inserted. We have to mask the irq to prevent interrupt
2843 * storm which will freeze the system. And the REMOVE gets
2844 * the same situation.
2845 *
2846 * More testing are needed here to ensure it works for other
2847 * platforms though.
2848 */
2849 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2850 SDHCI_INT_CARD_REMOVE);
2851 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2852 SDHCI_INT_CARD_INSERT);
2853
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002854 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302855 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2856 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002858 }
2859
Pierre Ossmand129bce2006-03-24 03:18:17 -08002860 if (intmask & SDHCI_INT_CMD_MASK) {
Sahitya Tummala8f6c0002013-08-07 18:40:29 +05302861 if (intmask & SDHCI_INT_AUTO_CMD_ERR)
2862 host->auto_cmd_err_sts = sdhci_readw(host,
2863 SDHCI_AUTO_CMD_ERR);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002864 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2865 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002866 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2867 (host->clock <= 400000))
2868 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002869 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870 }
2871
2872 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002873 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2874 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002875 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2876 (host->clock <= 400000))
2877 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002878 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002879 }
2880
2881 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2882
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002883 intmask &= ~SDHCI_INT_ERROR;
2884
Pierre Ossmand129bce2006-03-24 03:18:17 -08002885 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302886 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002887 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002888 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002889 }
2890
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002891 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002892
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002893 if (intmask & SDHCI_INT_CARD_INT)
2894 cardint = 1;
2895
2896 intmask &= ~SDHCI_INT_CARD_INT;
2897
Pierre Ossman3192a282006-06-30 02:22:26 -07002898 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002899 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002900 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002901 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902
2903 result = IRQ_HANDLED;
2904
Alexander Stein6379b232012-03-14 09:52:10 +01002905 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2906 if (intmask && --max_loops)
2907 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002908out:
2909 spin_unlock(&host->lock);
2910
Alexander Stein6379b232012-03-14 09:52:10 +01002911 if (unexpected) {
2912 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2913 mmc_hostname(host->mmc), unexpected);
2914 sdhci_dumpregs(host);
2915 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002916 /*
2917 * We have to delay this as it calls back into the driver.
2918 */
2919 if (cardint)
2920 mmc_signal_sdio_irq(host->mmc);
2921
Pierre Ossmand129bce2006-03-24 03:18:17 -08002922 return result;
2923}
2924
2925/*****************************************************************************\
2926 * *
2927 * Suspend/resume *
2928 * *
2929\*****************************************************************************/
2930
2931#ifdef CONFIG_PM
2932
Manuel Laussd72faa62011-11-03 11:09:45 +01002933int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002934{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002935 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302936 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937
Chris Balla1b13b42012-02-06 00:43:59 -05002938 if (host->ops->platform_suspend)
2939 host->ops->platform_suspend(host);
2940
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002941 sdhci_disable_card_detection(host);
2942
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302943 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302944 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2945 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2946 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002947 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302948 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302949 }
2950
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002951 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302952 if (ret) {
2953 if (has_tuning_timer) {
2954 host->flags |= SDHCI_NEEDS_RETUNING;
2955 mod_timer(&host->tuning_timer, jiffies +
2956 host->tuning_count * HZ);
2957 }
2958
2959 sdhci_enable_card_detection(host);
2960
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002961 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302962 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002964 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002965
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002966 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002967}
2968
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002969EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002971int sdhci_resume_host(struct sdhci_host *host)
2972{
2973 int ret;
2974
Richard Röjforsa13abc72009-09-22 16:45:30 -07002975 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002976 if (host->ops->enable_dma)
2977 host->ops->enable_dma(host);
2978 }
2979
2980 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2981 mmc_hostname(host->mmc), host);
2982 if (ret)
2983 return ret;
2984
Adrian Hunter6308d292012-02-07 14:48:54 +02002985 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2986 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2987 /* Card keeps power but host controller does not */
2988 sdhci_init(host, 0);
2989 host->pwr = 0;
2990 host->clock = 0;
2991 sdhci_do_set_ios(host, &host->mmc->ios);
2992 } else {
2993 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2994 mmiowb();
2995 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002996
2997 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002998 sdhci_enable_card_detection(host);
2999
Chris Balla1b13b42012-02-06 00:43:59 -05003000 if (host->ops->platform_resume)
3001 host->ops->platform_resume(host);
3002
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303003 /* Set the re-tuning expiration flag */
3004 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
3005 (host->tuning_mode == SDHCI_TUNING_MODE_1))
3006 host->flags |= SDHCI_NEEDS_RETUNING;
3007
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003008 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003009}
3010
3011EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003012
Daniel Drake5f619702010-11-04 22:20:39 +00003013void sdhci_enable_irq_wakeups(struct sdhci_host *host)
3014{
3015 u8 val;
3016 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3017 val |= SDHCI_WAKE_ON_INT;
3018 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
3019}
3020
3021EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
3022
Pierre Ossmand129bce2006-03-24 03:18:17 -08003023#endif /* CONFIG_PM */
3024
Adrian Hunter50accb92011-10-03 15:33:34 +03003025#ifdef CONFIG_PM_RUNTIME
3026
3027static int sdhci_runtime_pm_get(struct sdhci_host *host)
3028{
Asutosh Dasbbc84782013-02-11 15:31:35 +05303029 if (!mmc_use_core_runtime_pm(host->mmc))
3030 return pm_runtime_get_sync(host->mmc->parent);
3031 else
3032 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03003033}
3034
3035static int sdhci_runtime_pm_put(struct sdhci_host *host)
3036{
Asutosh Dasbbc84782013-02-11 15:31:35 +05303037 if (!mmc_use_core_runtime_pm(host->mmc)) {
3038 pm_runtime_mark_last_busy(host->mmc->parent);
3039 return pm_runtime_put_autosuspend(host->mmc->parent);
3040 } else {
3041 return 0;
3042 }
Adrian Hunter50accb92011-10-03 15:33:34 +03003043}
3044
3045int sdhci_runtime_suspend_host(struct sdhci_host *host)
3046{
3047 unsigned long flags;
3048 int ret = 0;
3049
3050 /* Disable tuning since we are suspending */
3051 if (host->version >= SDHCI_SPEC_300 &&
3052 host->tuning_mode == SDHCI_TUNING_MODE_1) {
3053 del_timer_sync(&host->tuning_timer);
3054 host->flags &= ~SDHCI_NEEDS_RETUNING;
3055 }
3056
3057 spin_lock_irqsave(&host->lock, flags);
3058 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
3059 spin_unlock_irqrestore(&host->lock, flags);
3060
3061 synchronize_irq(host->irq);
3062
3063 spin_lock_irqsave(&host->lock, flags);
3064 host->runtime_suspended = true;
3065 spin_unlock_irqrestore(&host->lock, flags);
3066
3067 return ret;
3068}
3069EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
3070
3071int sdhci_runtime_resume_host(struct sdhci_host *host)
3072{
3073 unsigned long flags;
3074 int ret = 0, host_flags = host->flags;
3075
3076 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
3077 if (host->ops->enable_dma)
3078 host->ops->enable_dma(host);
3079 }
3080
3081 sdhci_init(host, 0);
3082
3083 /* Force clock and power re-program */
3084 host->pwr = 0;
3085 host->clock = 0;
3086 sdhci_do_set_ios(host, &host->mmc->ios);
3087
3088 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
3089 if (host_flags & SDHCI_PV_ENABLED)
3090 sdhci_do_enable_preset_value(host, true);
3091
3092 /* Set the re-tuning expiration flag */
3093 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
3094 (host->tuning_mode == SDHCI_TUNING_MODE_1))
3095 host->flags |= SDHCI_NEEDS_RETUNING;
3096
3097 spin_lock_irqsave(&host->lock, flags);
3098
3099 host->runtime_suspended = false;
3100
3101 /* Enable SDIO IRQ */
3102 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
3103 sdhci_enable_sdio_irq_nolock(host, true);
3104
3105 /* Enable Card Detection */
3106 sdhci_enable_card_detection(host);
3107
3108 spin_unlock_irqrestore(&host->lock, flags);
3109
3110 return ret;
3111}
3112EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
3113
3114#endif
3115
Pierre Ossmand129bce2006-03-24 03:18:17 -08003116/*****************************************************************************\
3117 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003118 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003119 * *
3120\*****************************************************************************/
3121
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003122struct sdhci_host *sdhci_alloc_host(struct device *dev,
3123 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003124{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003125 struct mmc_host *mmc;
3126 struct sdhci_host *host;
3127
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003128 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003129
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003130 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003131 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003132 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003133
3134 host = mmc_priv(mmc);
3135 host->mmc = mmc;
3136
Sahitya Tummala951c1202013-05-24 08:47:26 +05303137 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05303138 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05303139
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003140 return host;
3141}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003142
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003143EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003144
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003145int sdhci_add_host(struct sdhci_host *host)
3146{
3147 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303148 u32 caps[2];
3149 u32 max_current_caps;
3150 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003151 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003152
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003153 WARN_ON(host == NULL);
3154 if (host == NULL)
3155 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003156
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003157 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003158
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003159 if (debug_quirks)
3160 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03003161 if (debug_quirks2)
3162 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003163
Pierre Ossmand96649e2006-06-30 02:22:30 -07003164 sdhci_reset(host, SDHCI_RESET_ALL);
3165
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003166 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02003167 host->version = (host->version & SDHCI_SPEC_VER_MASK)
3168 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08003169 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303170 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003171 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02003172 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003173 }
3174
Arindam Nathf2119df2011-05-05 12:18:57 +05303175 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07003176 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003177
Arindam Nathf2119df2011-05-05 12:18:57 +05303178 caps[1] = (host->version >= SDHCI_SPEC_300) ?
3179 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
3180
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003181 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003182 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05303183 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003184 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003185 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003186 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003187
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003188 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003189 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003190 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003191 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003192 }
3193
Arindam Nathf2119df2011-05-05 12:18:57 +05303194 if ((host->version >= SDHCI_SPEC_200) &&
3195 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003196 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003197
3198 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3199 (host->flags & SDHCI_USE_ADMA)) {
3200 DBG("Disabling ADMA as it is marked broken\n");
3201 host->flags &= ~SDHCI_USE_ADMA;
3202 }
3203
Richard Röjforsa13abc72009-09-22 16:45:30 -07003204 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003205 if (host->ops->enable_dma) {
3206 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303207 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003208 "available. Falling back to PIO.\n",
3209 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003210 host->flags &=
3211 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003212 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003213 }
3214 }
3215
Pierre Ossman2134a922008-06-28 18:28:51 +02003216 if (host->flags & SDHCI_USE_ADMA) {
3217 /*
3218 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303219 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02003220 * each of those entries.
3221 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303222 if (host->ops->get_max_segments)
3223 host->adma_max_desc = host->ops->get_max_segments();
3224 else
3225 host->adma_max_desc = 128;
3226
3227 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
3228 host->align_buf_sz = host->adma_max_desc * 4;
3229
3230 pr_debug("%s: %s: dma_desc_size: %d\n",
3231 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3232 host->adma_desc = kmalloc(host->adma_desc_sz,
3233 GFP_KERNEL);
3234 host->align_buffer = kmalloc(host->align_buf_sz,
3235 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003236 if (!host->adma_desc || !host->align_buffer) {
3237 kfree(host->adma_desc);
3238 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303239 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003240 "buffers. Falling back to standard DMA.\n",
3241 mmc_hostname(mmc));
3242 host->flags &= ~SDHCI_USE_ADMA;
3243 }
3244 }
3245
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003246 host->next_data.cookie = 1;
3247
Pierre Ossman76591502008-07-21 00:32:11 +02003248 /*
3249 * If we use DMA, then it's up to the caller to set the DMA
3250 * mask, but PIO does not need the hw shim so we set a new
3251 * mask here in that case.
3252 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003253 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003254 host->dma_mask = DMA_BIT_MASK(64);
3255 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3256 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003258 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303259 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003260 >> SDHCI_CLOCK_BASE_SHIFT;
3261 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303262 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003263 >> SDHCI_CLOCK_BASE_SHIFT;
3264
Pierre Ossmand129bce2006-03-24 03:18:17 -08003265 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003266 if (host->max_clk == 0 || host->quirks &
3267 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003268 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303269 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003270 "frequency.\n", mmc_hostname(mmc));
3271 return -ENODEV;
3272 }
3273 host->max_clk = host->ops->get_max_clock(host);
3274 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275
3276 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303277 * In case of Host Controller v3.00, find out whether clock
3278 * multiplier is supported.
3279 */
3280 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3281 SDHCI_CLOCK_MUL_SHIFT;
3282
3283 /*
3284 * In case the value in Clock Multiplier is 0, then programmable
3285 * clock mode is not supported, otherwise the actual clock
3286 * multiplier is one more than the value of Clock Multiplier
3287 * in the Capabilities Register.
3288 */
3289 if (host->clk_mul)
3290 host->clk_mul += 1;
3291
3292 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003293 * Set host parameters.
3294 */
3295 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303296 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003297 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003298 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303299 else if (host->version >= SDHCI_SPEC_300) {
3300 if (host->clk_mul) {
3301 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3302 mmc->f_max = host->max_clk * host->clk_mul;
3303 } else
3304 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3305 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003306 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003307
Sahitya Tummalaca422112013-02-22 12:15:54 +05303308 host->timeout_clk =
3309 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3310 if (host->timeout_clk == 0) {
3311 if (host->ops->get_timeout_clock) {
3312 host->timeout_clk = host->ops->get_timeout_clock(host);
3313 } else if (!(host->quirks &
3314 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3315 pr_err("%s: Hardware doesn't specify timeout clock "
3316 "frequency.\n", mmc_hostname(mmc));
3317 return -ENODEV;
3318 }
3319 }
3320 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3321 host->timeout_clk *= 1000;
3322
3323 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3324 host->timeout_clk = mmc->f_max / 1000;
3325
Krishna Kondaa20d3362013-04-01 21:01:59 -07003326 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3327 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303328
Andrei Warkentine89d4562011-05-23 15:06:37 -05003329 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3330
3331 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3332 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003333
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003334 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003335 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003336 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003337 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003338 host->flags |= SDHCI_AUTO_CMD23;
3339 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3340 } else {
3341 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3342 }
3343
Philip Rakity15ec4462010-11-19 16:48:39 -05003344 /*
3345 * A controller may support 8-bit width, but the board itself
3346 * might not have the pins brought out. Boards that support
3347 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3348 * their platform code before calling sdhci_add_host(), and we
3349 * won't assume 8-bit width for hosts without that CAP.
3350 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003351 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003352 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003353
Arindam Nathf2119df2011-05-05 12:18:57 +05303354 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003355 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003356
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003357 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3358 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003359 mmc->caps |= MMC_CAP_NEEDS_POLL;
3360
Al Cooper4188bba2012-03-16 15:54:17 -04003361 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3362 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3363 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303364 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3365
3366 /* SDR104 supports also implies SDR50 support */
3367 if (caps[1] & SDHCI_SUPPORT_SDR104)
3368 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3369 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3370 mmc->caps |= MMC_CAP_UHS_SDR50;
3371
3372 if (caps[1] & SDHCI_SUPPORT_DDR50)
3373 mmc->caps |= MMC_CAP_UHS_DDR50;
3374
Girish K S2cd06dc2012-01-06 09:56:39 +05303375 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303376 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3377 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3378
Girish K S2cd06dc2012-01-06 09:56:39 +05303379 /* Does the host need tuning for HS200? */
3380 if (mmc->caps2 & MMC_CAP2_HS200)
3381 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3382
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07003383 /* Does the host need tuning for HS400? */
3384 if (mmc->caps2 & MMC_CAP2_HS400)
3385 host->flags |= SDHCI_HS400_NEEDS_TUNING;
3386
Arindam Nathd6d50a12011-05-05 12:18:59 +05303387 /* Driver Type(s) (A, C, D) supported by the host */
3388 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3389 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3390 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3391 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3392 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3393 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3394
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003395 /* Initial value for re-tuning timer count */
3396 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3397 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3398
3399 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303400 * In case Re-tuning Timer is not disabled, the actual value of
3401 * re-tuning timer will be 2 ^ (n - 1).
3402 */
3403 if (host->tuning_count)
3404 host->tuning_count = 1 << (host->tuning_count - 1);
3405
3406 /* Re-tuning mode supported by the Host Controller */
3407 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3408 SDHCI_RETUNING_MODE_SHIFT;
3409
Takashi Iwai8f230f42010-12-08 10:04:30 +01003410 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303411 /*
3412 * According to SD Host Controller spec v3.00, if the Host System
3413 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3414 * the value is meaningful only if Voltage Support in the Capabilities
3415 * register is set. The actual current value is 4 times the register
3416 * value.
3417 */
3418 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3419
3420 if (caps[0] & SDHCI_CAN_VDD_330) {
3421 int max_current_330;
3422
Takashi Iwai8f230f42010-12-08 10:04:30 +01003423 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303424
3425 max_current_330 = ((max_current_caps &
3426 SDHCI_MAX_CURRENT_330_MASK) >>
3427 SDHCI_MAX_CURRENT_330_SHIFT) *
3428 SDHCI_MAX_CURRENT_MULTIPLIER;
3429
3430 if (max_current_330 > 150)
3431 mmc->caps |= MMC_CAP_SET_XPC_330;
3432 }
3433 if (caps[0] & SDHCI_CAN_VDD_300) {
3434 int max_current_300;
3435
Takashi Iwai8f230f42010-12-08 10:04:30 +01003436 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303437
3438 max_current_300 = ((max_current_caps &
3439 SDHCI_MAX_CURRENT_300_MASK) >>
3440 SDHCI_MAX_CURRENT_300_SHIFT) *
3441 SDHCI_MAX_CURRENT_MULTIPLIER;
3442
3443 if (max_current_300 > 150)
3444 mmc->caps |= MMC_CAP_SET_XPC_300;
3445 }
3446 if (caps[0] & SDHCI_CAN_VDD_180) {
3447 int max_current_180;
3448
Takashi Iwai8f230f42010-12-08 10:04:30 +01003449 ocr_avail |= MMC_VDD_165_195;
3450
Arindam Nathf2119df2011-05-05 12:18:57 +05303451 max_current_180 = ((max_current_caps &
3452 SDHCI_MAX_CURRENT_180_MASK) >>
3453 SDHCI_MAX_CURRENT_180_SHIFT) *
3454 SDHCI_MAX_CURRENT_MULTIPLIER;
3455
3456 if (max_current_180 > 150)
3457 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303458
3459 /* Maximum current capabilities of the host at 1.8V */
3460 if (max_current_180 >= 800)
3461 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3462 else if (max_current_180 >= 600)
3463 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3464 else if (max_current_180 >= 400)
3465 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3466 else
3467 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303468 }
3469
Takashi Iwai8f230f42010-12-08 10:04:30 +01003470 mmc->ocr_avail = ocr_avail;
3471 mmc->ocr_avail_sdio = ocr_avail;
3472 if (host->ocr_avail_sdio)
3473 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3474 mmc->ocr_avail_sd = ocr_avail;
3475 if (host->ocr_avail_sd)
3476 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3477 else /* normal SD controllers don't support 1.8V */
3478 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3479 mmc->ocr_avail_mmc = ocr_avail;
3480 if (host->ocr_avail_mmc)
3481 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003482
3483 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303484 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003485 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003486 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003487 }
3488
Pierre Ossmand129bce2006-03-24 03:18:17 -08003489 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003490 * Maximum number of segments. Depends on if the hardware
3491 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003492 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003493 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303494 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003495 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003496 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303497 else/* PIO */
3498 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003499
3500 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003501 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303502 * size (512KiB), unless specified by platform specific driver. Each
3503 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003504 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303505 if (host->ops->get_max_segments)
3506 mmc->max_req_size = (host->adma_max_desc * 65536);
3507 else
3508 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509
3510 /*
3511 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003512 * of bytes. When doing hardware scatter/gather, each entry cannot
3513 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003514 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003515 if (host->flags & SDHCI_USE_ADMA) {
3516 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3517 mmc->max_seg_size = 65535;
3518 else
3519 mmc->max_seg_size = 65536;
3520 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003521 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003522 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003523
3524 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003525 * Maximum block size. This varies from controller to controller and
3526 * is specified in the capabilities register.
3527 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003528 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3529 mmc->max_blk_size = 2;
3530 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303531 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003532 SDHCI_MAX_BLOCK_SHIFT;
3533 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303534 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003535 "assuming 512 bytes\n", mmc_hostname(mmc));
3536 mmc->max_blk_size = 0;
3537 }
3538 }
3539
3540 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003541
3542 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003543 * Maximum block count.
3544 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003545 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003546
3547 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003548 * Init tasklets.
3549 */
3550 tasklet_init(&host->card_tasklet,
3551 sdhci_tasklet_card, (unsigned long)host);
3552 tasklet_init(&host->finish_tasklet,
3553 sdhci_tasklet_finish, (unsigned long)host);
3554
Al Viroe4cad1b2006-10-10 22:47:07 +01003555 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003556
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303557 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303558 init_waitqueue_head(&host->buf_ready_int);
3559
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303560 /* Initialize re-tuning timer */
3561 init_timer(&host->tuning_timer);
3562 host->tuning_timer.data = (unsigned long)host;
3563 host->tuning_timer.function = sdhci_tuning_timer;
3564 }
3565
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003566 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003567 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003568 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003569 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003570
Asutosh Das08a00a32013-04-24 14:48:48 +05303571 host->irq_enabled = true;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003572 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3573 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303574 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003575 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003576 }
3577
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003578 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003579
3580#ifdef CONFIG_MMC_DEBUG
3581 sdhci_dumpregs(host);
3582#endif
3583
Pierre Ossmanf9134312008-12-21 17:01:48 +01003584#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003585 snprintf(host->led_name, sizeof(host->led_name),
3586 "%s::", mmc_hostname(mmc));
3587 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003588 host->led.brightness = LED_OFF;
3589 host->led.default_trigger = mmc_hostname(mmc);
3590 host->led.brightness_set = sdhci_led_control;
3591
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003592 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003593 if (ret)
3594 goto reset;
3595#endif
3596
Pierre Ossman5f25a662006-10-04 02:15:39 -07003597 mmiowb();
3598
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303599 if (host->cpu_dma_latency_us) {
3600 host->pm_qos_timeout_us = 10000; /* default value */
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303601 pm_qos_add_request(&host->pm_qos_req_dma,
3602 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303603
3604 host->pm_qos_tout.show = show_sdhci_pm_qos_tout;
3605 host->pm_qos_tout.store = store_sdhci_pm_qos_tout;
3606 sysfs_attr_init(&host->pm_qos_tout.attr);
3607 host->pm_qos_tout.attr.name = "pm_qos_unvote_delay";
3608 host->pm_qos_tout.attr.mode = S_IRUGO | S_IWUSR;
3609 ret = device_create_file(mmc_dev(mmc), &host->pm_qos_tout);
3610 if (ret)
3611 pr_err("%s: cannot create pm_qos_unvote_delay %d\n",
3612 mmc_hostname(mmc), ret);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303613 }
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303614
Asutosh Das08a00a32013-04-24 14:48:48 +05303615 if (caps[0] & SDHCI_ASYNC_INTR)
3616 host->async_int_supp = true;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003617 mmc_add_host(mmc);
3618
Sahitya Tummalaca422112013-02-22 12:15:54 +05303619 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003620 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003621 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3622 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003623
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003624 sdhci_enable_card_detection(host);
3625
Pierre Ossmand129bce2006-03-24 03:18:17 -08003626 return 0;
3627
Pierre Ossmanf9134312008-12-21 17:01:48 +01003628#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003629reset:
3630 sdhci_reset(host, SDHCI_RESET_ALL);
3631 free_irq(host->irq, host);
3632#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003633untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003634 tasklet_kill(&host->card_tasklet);
3635 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003636
3637 return ret;
3638}
3639
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003640EXPORT_SYMBOL_GPL(sdhci_add_host);
3641
Pierre Ossman1e728592008-04-16 19:13:13 +02003642void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003643{
Pierre Ossman1e728592008-04-16 19:13:13 +02003644 unsigned long flags;
3645
3646 if (dead) {
3647 spin_lock_irqsave(&host->lock, flags);
3648
3649 host->flags |= SDHCI_DEVICE_DEAD;
3650
3651 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303652 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003653 " transfer!\n", mmc_hostname(host->mmc));
3654
3655 host->mrq->cmd->error = -ENOMEDIUM;
3656 tasklet_schedule(&host->finish_tasklet);
3657 }
3658
3659 spin_unlock_irqrestore(&host->lock, flags);
3660 }
3661
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003662 sdhci_disable_card_detection(host);
3663
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303664 if (host->cpu_dma_latency_us)
3665 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003666 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003667
Pierre Ossmanf9134312008-12-21 17:01:48 +01003668#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003669 led_classdev_unregister(&host->led);
3670#endif
3671
Pierre Ossman1e728592008-04-16 19:13:13 +02003672 if (!dead)
3673 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003674
3675 free_irq(host->irq, host);
3676
3677 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303678 if (host->version >= SDHCI_SPEC_300)
3679 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003680
3681 tasklet_kill(&host->card_tasklet);
3682 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003683
Sahitya Tummalaca422112013-02-22 12:15:54 +05303684 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003685 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003686
Pierre Ossman2134a922008-06-28 18:28:51 +02003687 kfree(host->adma_desc);
3688 kfree(host->align_buffer);
3689
3690 host->adma_desc = NULL;
3691 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003692}
3693
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003694EXPORT_SYMBOL_GPL(sdhci_remove_host);
3695
3696void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003697{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003698 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003699}
3700
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003701EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003702
3703/*****************************************************************************\
3704 * *
3705 * Driver init/exit *
3706 * *
3707\*****************************************************************************/
3708
3709static int __init sdhci_drv_init(void)
3710{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303711 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003712 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303713 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003714
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003715 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003716}
3717
3718static void __exit sdhci_drv_exit(void)
3719{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003720}
3721
3722module_init(sdhci_drv_init);
3723module_exit(sdhci_drv_exit);
3724
Pierre Ossmandf673b22006-06-30 02:22:31 -07003725module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003726module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003727
Pierre Ossman32710e82009-04-08 20:14:54 +02003728MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003729MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003730MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003731
Pierre Ossmandf673b22006-06-30 02:22:31 -07003732MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003733MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");