blob: 0cb049118b64efb23c0ef97361094df308171bd0 [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter50accb92011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Girish K S2cd06dc2012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static void sdhci_tuning_timer(unsigned long data);
Sahitya Tummala1b248c42013-05-24 14:08:10 +053054static bool sdhci_check_state(struct sdhci_host *);
Pierre Ossmand129bce2006-03-24 03:18:17 -080055
Adrian Hunter50accb92011-10-03 15:33:34 +030056#ifdef CONFIG_PM_RUNTIME
57static int sdhci_runtime_pm_get(struct sdhci_host *host);
58static int sdhci_runtime_pm_put(struct sdhci_host *host);
59#else
60static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
61{
62 return 0;
63}
64static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
65{
66 return 0;
67}
68#endif
69
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053070static void sdhci_dump_state(struct sdhci_host *host)
71{
72 struct mmc_host *mmc = host->mmc;
73
74 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
75 mmc_hostname(mmc), host->clock, mmc->clk_gated,
76 mmc->claimer->comm, host->pwr);
77 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
78 mmc_hostname(mmc), mmc->parent->power.runtime_status,
79 atomic_read(&mmc->parent->power.usage_count),
80 mmc->parent->power.disable_depth);
81}
82
Pierre Ossmand129bce2006-03-24 03:18:17 -080083static void sdhci_dumpregs(struct sdhci_host *host)
84{
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053085 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070086 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080087
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053088 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readl(host, SDHCI_DMA_ADDRESS),
90 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053091 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readw(host, SDHCI_BLOCK_SIZE),
93 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053094 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_ARGUMENT),
96 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +053097 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readl(host, SDHCI_PRESENT_STATE),
99 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530100 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readb(host, SDHCI_POWER_CONTROL),
102 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530103 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
105 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530106 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
108 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530109 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300110 sdhci_readl(host, SDHCI_INT_ENABLE),
111 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530112 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Asutosh Das80c02552013-07-23 16:20:34 +0530113 sdhci_readw(host, SDHCI_AUTO_CMD_ERR),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300114 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530115 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300116 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500117 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530118 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500119 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300120 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das80c02552013-07-23 16:20:34 +0530121 pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n",
122 sdhci_readl(host, SDHCI_RESPONSE + 0x4),
123 sdhci_readl(host, SDHCI_RESPONSE));
124 pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n",
125 sdhci_readl(host, SDHCI_RESPONSE + 0xC),
126 sdhci_readl(host, SDHCI_RESPONSE + 0x8));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530127 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530128 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800129
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100130 if (host->flags & SDHCI_USE_ADMA)
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530131 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100132 readl(host->ioaddr + SDHCI_ADMA_ERROR),
133 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
134
Asutosh Das5fa7d3b2013-03-20 22:53:40 +0530135 sdhci_dump_state(host);
136 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800137}
138
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +0530139#define MAX_PM_QOS_TIMEOUT_VALUE 100000 /* 100 ms */
140static ssize_t
141show_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
142 char *buf)
143{
144 struct sdhci_host *host = dev_get_drvdata(dev);
145
146 return snprintf(buf, PAGE_SIZE, "%d us\n", host->pm_qos_timeout_us);
147}
148
149static ssize_t
150store_sdhci_pm_qos_tout(struct device *dev, struct device_attribute *attr,
151 const char *buf, size_t count)
152{
153 struct sdhci_host *host = dev_get_drvdata(dev);
154 uint32_t value;
155 unsigned long flags;
156
157 if (!kstrtou32(buf, 0, &value)) {
158 spin_lock_irqsave(&host->lock, flags);
159 if (value <= MAX_PM_QOS_TIMEOUT_VALUE)
160 host->pm_qos_timeout_us = value;
161 spin_unlock_irqrestore(&host->lock, flags);
162 }
163 return count;
164}
165
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166/*****************************************************************************\
167 * *
168 * Low level functions *
169 * *
170\*****************************************************************************/
171
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300172static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
173{
174 u32 ier;
175
176 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
177 ier &= ~clear;
178 ier |= set;
179 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
180 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
181}
182
183static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
184{
185 sdhci_clear_set_irqs(host, 0, irqs);
186}
187
188static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
189{
190 sdhci_clear_set_irqs(host, irqs, 0);
191}
192
193static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
194{
Sahitya Tummalaca422112013-02-22 12:15:54 +0530195 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300196
Adrian Hunterc79396c2011-12-27 15:48:42 +0200197 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100198 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300199 return;
200
Sahitya Tummalaca422112013-02-22 12:15:54 +0530201 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
202 SDHCI_CARD_PRESENT;
203 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
Adrian Hunter50accb92011-10-03 15:33:34 +0300204
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300205 if (enable)
206 sdhci_unmask_irqs(host, irqs);
207 else
208 sdhci_mask_irqs(host, irqs);
209}
210
211static void sdhci_enable_card_detection(struct sdhci_host *host)
212{
213 sdhci_set_card_detection(host, true);
214}
215
216static void sdhci_disable_card_detection(struct sdhci_host *host)
217{
218 sdhci_set_card_detection(host, false);
219}
220
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221static void sdhci_reset(struct sdhci_host *host, u8 mask)
222{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700223 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300224 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700225
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100226 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300227 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700228 SDHCI_CARD_PRESENT))
229 return;
230 }
231
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300232 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
233 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
234
Philip Rakity393c1a32011-01-21 11:26:40 -0800235 if (host->ops->platform_reset_enter)
236 host->ops->platform_reset_enter(host, mask);
237
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300238 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800239
Pierre Ossmane16514d2006-06-30 02:22:24 -0700240 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241 host->clock = 0;
242
Pierre Ossmane16514d2006-06-30 02:22:24 -0700243 /* Wait max 100 ms */
244 timeout = 100;
245
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530246 if (host->ops->check_power_status && host->pwr &&
247 (mask & SDHCI_RESET_ALL))
Sahitya Tummala179e7382013-03-20 19:24:01 +0530248 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +0530249
Pierre Ossmane16514d2006-06-30 02:22:24 -0700250 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300251 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700252 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +0530253 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700254 mmc_hostname(host->mmc), (int)mask);
255 sdhci_dumpregs(host);
256 return;
257 }
258 timeout--;
259 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300261
Philip Rakity393c1a32011-01-21 11:26:40 -0800262 if (host->ops->platform_reset_exit)
263 host->ops->platform_reset_exit(host, mask);
264
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300265 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
266 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Sahitya Tummalaca422112013-02-22 12:15:54 +0530267
268 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
269 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
270 host->ops->enable_dma(host);
271 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272}
273
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800274static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
275
276static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800278 if (soft)
279 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
280 else
281 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300283 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
284 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700285 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
286 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Asutosh Das80c02552013-07-23 16:20:34 +0530287 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
288 SDHCI_INT_AUTO_CMD_ERR);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800289
290 if (soft) {
291 /* force clock reconfiguration */
292 host->clock = 0;
293 sdhci_set_ios(host->mmc, &host->mmc->ios);
294 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300295}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300297static void sdhci_reinit(struct sdhci_host *host)
298{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800299 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300300 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301}
302
303static void sdhci_activate_led(struct sdhci_host *host)
304{
305 u8 ctrl;
306
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300307 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300309 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310}
311
312static void sdhci_deactivate_led(struct sdhci_host *host)
313{
314 u8 ctrl;
315
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300316 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300318 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319}
320
Pierre Ossmanf9134312008-12-21 17:01:48 +0100321#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100322static void sdhci_led_control(struct led_classdev *led,
323 enum led_brightness brightness)
324{
325 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
326 unsigned long flags;
327
328 spin_lock_irqsave(&host->lock, flags);
329
Sahitya Tummala1b248c42013-05-24 14:08:10 +0530330 if (host->runtime_suspended || sdhci_check_state(host))
Adrian Hunter50accb92011-10-03 15:33:34 +0300331 goto out;
332
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100333 if (brightness == LED_OFF)
334 sdhci_deactivate_led(host);
335 else
336 sdhci_activate_led(host);
Adrian Hunter50accb92011-10-03 15:33:34 +0300337out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100338 spin_unlock_irqrestore(&host->lock, flags);
339}
340#endif
341
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342/*****************************************************************************\
343 * *
344 * Core functions *
345 * *
346\*****************************************************************************/
347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349{
Pierre Ossman76591502008-07-21 00:32:11 +0200350 unsigned long flags;
351 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700352 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200353 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800356
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200358 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200363 if (!sg_miter_next(&host->sg_miter))
364 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800365
Pierre Ossman76591502008-07-21 00:32:11 +0200366 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 blksize -= len;
369 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 while (len) {
374 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300375 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200376 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800377 }
Pierre Ossman76591502008-07-21 00:32:11 +0200378
379 *buf = scratch & 0xFF;
380
381 buf++;
382 scratch >>= 8;
383 chunk--;
384 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800385 }
386 }
Pierre Ossman76591502008-07-21 00:32:11 +0200387
388 sg_miter_stop(&host->sg_miter);
389
390 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100391}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800392
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393static void sdhci_write_block_pio(struct sdhci_host *host)
394{
Pierre Ossman76591502008-07-21 00:32:11 +0200395 unsigned long flags;
396 size_t blksize, len, chunk;
397 u32 scratch;
398 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399
400 DBG("PIO writing\n");
401
402 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200403 chunk = 0;
404 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100405
Pierre Ossman76591502008-07-21 00:32:11 +0200406 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407
408 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200409 if (!sg_miter_next(&host->sg_miter))
410 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411
Pierre Ossman76591502008-07-21 00:32:11 +0200412 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200413
Pierre Ossman76591502008-07-21 00:32:11 +0200414 blksize -= len;
415 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416
Pierre Ossman76591502008-07-21 00:32:11 +0200417 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418
Pierre Ossman76591502008-07-21 00:32:11 +0200419 while (len) {
420 scratch |= (u32)*buf << (chunk * 8);
421
422 buf++;
423 chunk++;
424 len--;
425
426 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300427 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200428 chunk = 0;
429 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100430 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431 }
432 }
Pierre Ossman76591502008-07-21 00:32:11 +0200433
434 sg_miter_stop(&host->sg_miter);
435
436 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437}
438
439static void sdhci_transfer_pio(struct sdhci_host *host)
440{
441 u32 mask;
442
443 BUG_ON(!host->data);
444
Pierre Ossman76591502008-07-21 00:32:11 +0200445 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100446 return;
447
448 if (host->data->flags & MMC_DATA_READ)
449 mask = SDHCI_DATA_AVAILABLE;
450 else
451 mask = SDHCI_SPACE_AVAILABLE;
452
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200453 /*
454 * Some controllers (JMicron JMB38x) mess up the buffer bits
455 * for transfers < 4 bytes. As long as it is just one block,
456 * we can ignore the bits.
457 */
458 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
459 (host->data->blocks == 1))
460 mask = ~0;
461
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300462 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300463 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
464 udelay(100);
465
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100466 if (host->data->flags & MMC_DATA_READ)
467 sdhci_read_block_pio(host);
468 else
469 sdhci_write_block_pio(host);
470
Pierre Ossman76591502008-07-21 00:32:11 +0200471 host->blocks--;
472 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100473 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100474 }
475
476 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800477}
478
Pierre Ossman2134a922008-06-28 18:28:51 +0200479static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
480{
481 local_irq_save(*flags);
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800482 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200483}
484
485static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
486{
Cong Wang9a4bf3b2011-11-27 13:27:00 +0800487 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200488 local_irq_restore(*flags);
489}
490
Ben Dooks118cd172010-03-05 13:43:26 -0800491static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
492{
Ben Dooks9e506f32010-03-05 13:43:29 -0800493 __le32 *dataddr = (__le32 __force *)(desc + 4);
494 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800495
Ben Dooks9e506f32010-03-05 13:43:29 -0800496 /* SDHCI specification says ADMA descriptors should be 4 byte
497 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800498
Ben Dooks9e506f32010-03-05 13:43:29 -0800499 cmdlen[0] = cpu_to_le16(cmd);
500 cmdlen[1] = cpu_to_le16(len);
501
502 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800503}
504
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800505static int sdhci_pre_dma_transfer(struct sdhci_host *host,
506 struct mmc_data *data,
507 struct sdhci_next *next)
508{
509 int sg_count;
510
511 if (!next && data->host_cookie &&
512 data->host_cookie != host->next_data.cookie) {
513 printk(KERN_WARNING "[%s] invalid cookie: data->host_cookie %d"
514 " host->next_data.cookie %d\n",
515 __func__, data->host_cookie, host->next_data.cookie);
516 data->host_cookie = 0;
517 }
518
519 /* Check if next job is already prepared */
520 if (next ||
521 (!next && data->host_cookie != host->next_data.cookie)) {
522 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg,
523 data->sg_len,
524 (data->flags & MMC_DATA_WRITE) ?
525 DMA_TO_DEVICE : DMA_FROM_DEVICE);
526 } else {
527 sg_count = host->next_data.sg_count;
528 host->next_data.sg_count = 0;
529 }
530
531 if (sg_count == 0)
532 return -EINVAL;
533
534 if (next) {
535 next->sg_count = sg_count;
536 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
537 } else
538 host->sg_count = sg_count;
539
540 return sg_count;
541}
542
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200543static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 struct mmc_data *data)
545{
546 int direction;
547
548 u8 *desc;
549 u8 *align;
550 dma_addr_t addr;
551 dma_addr_t align_addr;
552 int len, offset;
553
554 struct scatterlist *sg;
555 int i;
556 char *buffer;
557 unsigned long flags;
558
559 /*
560 * The spec does not specify endianness of descriptor table.
561 * We currently guess that it is LE.
562 */
563
564 if (data->flags & MMC_DATA_READ)
565 direction = DMA_FROM_DEVICE;
566 else
567 direction = DMA_TO_DEVICE;
568
569 /*
570 * The ADMA descriptor table is mapped further down as we
571 * need to fill it with data first.
572 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200573 host->align_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530574 host->align_buffer,
575 host->align_buf_sz,
576 direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700577 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200578 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200579 BUG_ON(host->align_addr & 0x3);
580
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800581 host->sg_count = sdhci_pre_dma_transfer(host, data, NULL);
582 if (host->sg_count < 0)
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200583 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200584
585 desc = host->adma_desc;
586 align = host->align_buffer;
587
588 align_addr = host->align_addr;
589
590 for_each_sg(data->sg, sg, host->sg_count, i) {
591 addr = sg_dma_address(sg);
592 len = sg_dma_len(sg);
593
594 /*
595 * The SDHCI specification states that ADMA
596 * addresses must be 32-bit aligned. If they
597 * aren't, then we use a bounce buffer for
598 * the (up to three) bytes that screw up the
599 * alignment.
600 */
601 offset = (4 - (addr & 0x3)) & 0x3;
602 if (offset) {
603 if (data->flags & MMC_DATA_WRITE) {
604 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200605 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200606 memcpy(align, buffer, offset);
607 sdhci_kunmap_atomic(buffer, &flags);
608 }
609
Ben Dooks118cd172010-03-05 13:43:26 -0800610 /* tran, valid */
611 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200612
613 BUG_ON(offset > 65536);
614
Pierre Ossman2134a922008-06-28 18:28:51 +0200615 align += 4;
616 align_addr += 4;
617
618 desc += 8;
619
620 addr += offset;
621 len -= offset;
622 }
623
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 BUG_ON(len > 65536);
625
Ben Dooks118cd172010-03-05 13:43:26 -0800626 /* tran, valid */
627 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200628 desc += 8;
629
630 /*
631 * If this triggers then we have a calculation bug
632 * somewhere. :/
633 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530634 WARN_ON((desc - host->adma_desc) > host->adma_desc_sz);
635
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 }
637
Thomas Abraham70764a92010-05-26 14:42:04 -0700638 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
639 /*
640 * Mark the last descriptor as the terminating descriptor
641 */
642 if (desc != host->adma_desc) {
643 desc -= 8;
644 desc[0] |= 0x2; /* end */
645 }
646 } else {
647 /*
648 * Add a terminating entry.
649 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200650
Thomas Abraham70764a92010-05-26 14:42:04 -0700651 /* nop, end, valid */
652 sdhci_set_adma_desc(desc, 0, 0, 0x3);
653 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200654
655 /*
656 * Resync align buffer as we might have changed it.
657 */
658 if (data->flags & MMC_DATA_WRITE) {
659 dma_sync_single_for_device(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530660 host->align_addr,
661 host->align_buf_sz,
662 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200663 }
664
665 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530666 host->adma_desc,
667 host->adma_desc_sz,
668 DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200669 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200670 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200671 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200672
673 return 0;
674
675unmap_entries:
676 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
677 data->sg_len, direction);
678unmap_align:
679 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530680 host->align_buf_sz, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200681fail:
682 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200683}
684
685static void sdhci_adma_table_post(struct sdhci_host *host,
686 struct mmc_data *data)
687{
688 int direction;
689
690 struct scatterlist *sg;
691 int i, size;
692 u8 *align;
693 char *buffer;
694 unsigned long flags;
695
696 if (data->flags & MMC_DATA_READ)
697 direction = DMA_FROM_DEVICE;
698 else
699 direction = DMA_TO_DEVICE;
700
701 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530702 host->adma_desc_sz, DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200703
704 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530705 host->align_buf_sz, direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200706
707 if (data->flags & MMC_DATA_READ) {
708 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
709 data->sg_len, direction);
710
711 align = host->align_buffer;
712
713 for_each_sg(data->sg, sg, host->sg_count, i) {
714 if (sg_dma_address(sg) & 0x3) {
715 size = 4 - (sg_dma_address(sg) & 0x3);
716
717 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200718 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200719 memcpy(buffer, align, size);
720 sdhci_kunmap_atomic(buffer, &flags);
721
722 align += 4;
723 }
724 }
725 }
726
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800727 if (!data->host_cookie)
728 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
729 direction);
Pierre Ossman2134a922008-06-28 18:28:51 +0200730}
731
Andrei Warkentina3c77782011-04-11 16:13:42 -0500732static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800733{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700734 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500735 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700736 unsigned target_timeout, current_timeout;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530737 u32 curr_clk = 0; /* In KHz */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800738
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200739 /*
740 * If the host controller provides us with an incorrect timeout
741 * value, just skip the check and use 0xE. The hardware may take
742 * longer to time out, but that's much better than having a too-short
743 * timeout value.
744 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200745 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200746 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200747
Andrei Warkentina3c77782011-04-11 16:13:42 -0500748 /* Unspecified timeout, assume max */
749 if (!data && !cmd->cmd_timeout_ms)
750 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800751
Andrei Warkentina3c77782011-04-11 16:13:42 -0500752 /* timeout in us */
753 if (!data)
754 target_timeout = cmd->cmd_timeout_ms * 1000;
Sahitya Tummalaca422112013-02-22 12:15:54 +0530755 else {
756 target_timeout = data->timeout_ns / 1000;
757 if (host->clock)
758 target_timeout += data->timeout_clks / host->clock;
759 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700760
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700761 /*
762 * Figure out needed cycles.
763 * We do this in steps in order to fit inside a 32 bit int.
764 * The first step is the minimum timeout, which will have a
765 * minimum resolution of 6 bits:
766 * (1) 2^13*1000 > 2^22,
767 * (2) host->timeout_clk < 2^16
768 * =>
769 * (1) / (2) > 2^6
770 */
771 count = 0;
Sahitya Tummalaf667cc12013-06-10 16:32:51 +0530772 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) {
773 curr_clk = host->clock / 1000;
774 if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4)
775 curr_clk /= 4;
776 current_timeout = (1 << 13) * 1000 / curr_clk;
777 } else {
778 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
779 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700780 while (current_timeout < target_timeout) {
781 count++;
782 current_timeout <<= 1;
783 if (count >= 0xF)
784 break;
785 }
786
Sahitya Tummala4d12d0b2013-04-12 11:59:25 +0530787 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
788 if (count >= 0xF) {
789 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
790 mmc_hostname(host->mmc), count, cmd->opcode);
791 count = 0xE;
792 }
Sahitya Tummalaca422112013-02-22 12:15:54 +0530793 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700794
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200795 return count;
796}
797
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300798static void sdhci_set_transfer_irqs(struct sdhci_host *host)
799{
800 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
801 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
802
803 if (host->flags & SDHCI_REQ_USE_DMA)
804 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
805 else
806 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
807}
808
Andrei Warkentina3c77782011-04-11 16:13:42 -0500809static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200810{
811 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500813 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200815
816 WARN_ON(host->data);
817
Andrei Warkentina3c77782011-04-11 16:13:42 -0500818 if (data || (cmd->flags & MMC_RSP_BUSY)) {
819 count = sdhci_calc_timeout(host, cmd);
820 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
821 }
822
823 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200824 return;
825
826 /* Sanity checks */
Asutosh Dasc8e8e562013-01-10 21:05:49 +0530827 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200828 BUG_ON(data->blksz > host->mmc->max_blk_size);
829 BUG_ON(data->blocks > 65535);
830
831 host->data = data;
832 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400833 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200834
Richard Röjforsa13abc72009-09-22 16:45:30 -0700835 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100836 host->flags |= SDHCI_REQ_USE_DMA;
837
Pierre Ossman2134a922008-06-28 18:28:51 +0200838 /*
839 * FIXME: This doesn't account for merging when mapping the
840 * scatterlist.
841 */
842 if (host->flags & SDHCI_REQ_USE_DMA) {
843 int broken, i;
844 struct scatterlist *sg;
845
846 broken = 0;
847 if (host->flags & SDHCI_USE_ADMA) {
848 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
849 broken = 1;
850 } else {
851 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
852 broken = 1;
853 }
854
855 if (unlikely(broken)) {
856 for_each_sg(data->sg, sg, data->sg_len, i) {
857 if (sg->length & 0x3) {
858 DBG("Reverting to PIO because of "
859 "transfer size (%d)\n",
860 sg->length);
861 host->flags &= ~SDHCI_REQ_USE_DMA;
862 break;
863 }
864 }
865 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100866 }
867
868 /*
869 * The assumption here being that alignment is the same after
870 * translation to device address space.
871 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200872 if (host->flags & SDHCI_REQ_USE_DMA) {
873 int broken, i;
874 struct scatterlist *sg;
875
876 broken = 0;
877 if (host->flags & SDHCI_USE_ADMA) {
878 /*
879 * As we use 3 byte chunks to work around
880 * alignment problems, we need to check this
881 * quirk.
882 */
883 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
884 broken = 1;
885 } else {
886 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
887 broken = 1;
888 }
889
890 if (unlikely(broken)) {
891 for_each_sg(data->sg, sg, data->sg_len, i) {
892 if (sg->offset & 0x3) {
893 DBG("Reverting to PIO because of "
894 "bad alignment\n");
895 host->flags &= ~SDHCI_REQ_USE_DMA;
896 break;
897 }
898 }
899 }
900 }
901
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200902 if (host->flags & SDHCI_REQ_USE_DMA) {
903 if (host->flags & SDHCI_USE_ADMA) {
904 ret = sdhci_adma_table_pre(host, data);
905 if (ret) {
906 /*
907 * This only happens when someone fed
908 * us an invalid request.
909 */
910 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200911 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200912 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300913 sdhci_writel(host, host->adma_addr,
914 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200915 }
916 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300917 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200918
Shawn Guo6f9ad6f2011-04-17 00:48:36 +0800919 sg_cnt = sdhci_pre_dma_transfer(host, data, NULL);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300920 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200921 /*
922 * This only happens when someone fed
923 * us an invalid request.
924 */
925 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200926 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200927 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200928 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300929 sdhci_writel(host, sg_dma_address(data->sg),
930 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200931 }
932 }
933 }
934
Pierre Ossman2134a922008-06-28 18:28:51 +0200935 /*
936 * Always adjust the DMA selection as some controllers
937 * (e.g. JMicron) can't do PIO properly when the selection
938 * is ADMA.
939 */
940 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300941 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200942 ctrl &= ~SDHCI_CTRL_DMA_MASK;
943 if ((host->flags & SDHCI_REQ_USE_DMA) &&
944 (host->flags & SDHCI_USE_ADMA))
945 ctrl |= SDHCI_CTRL_ADMA32;
946 else
947 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300948 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100949 }
950
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200951 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200952 int flags;
953
954 flags = SG_MITER_ATOMIC;
955 if (host->data->flags & MMC_DATA_READ)
956 flags |= SG_MITER_TO_SG;
957 else
958 flags |= SG_MITER_FROM_SG;
959 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200960 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700962
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300963 sdhci_set_transfer_irqs(host);
964
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400965 /* Set the DMA boundary value and block size */
966 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
967 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300968 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700969}
970
971static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500972 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700973{
974 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500975 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700976
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700977 if (data == NULL)
978 return;
979
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200980 WARN_ON(!host->data);
981
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700982 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500983 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
984 mode |= SDHCI_TRNS_MULTI;
985 /*
986 * If we are sending CMD23, CMD12 never gets sent
987 * on successful completion (so no Auto-CMD12).
988 */
989 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
990 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500991 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
992 mode |= SDHCI_TRNS_AUTO_CMD23;
993 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
994 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700995 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500996
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530997 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700998 mode |= SDHCI_TRNS_READ;
Sahitya Tummala239e5a82013-02-25 15:45:32 +0530999 if (host->ops->toggle_cdr)
1000 host->ops->toggle_cdr(host, true);
1001 }
1002 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
1003 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001004 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001005 mode |= SDHCI_TRNS_DMA;
1006
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001007 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008}
1009
1010static void sdhci_finish_data(struct sdhci_host *host)
1011{
1012 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
1014 BUG_ON(!host->data);
1015
1016 data = host->data;
1017 host->data = NULL;
1018
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001019 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001020 if (host->flags & SDHCI_USE_ADMA)
1021 sdhci_adma_table_post(host, data);
1022 else {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001023 if (!data->host_cookie)
1024 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
1025 data->sg_len,
1026 (data->flags & MMC_DATA_READ) ?
1027 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +02001028 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029 }
1030
1031 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001032 * The specification states that the block count register must
1033 * be updated, but it does not specify at what point in the
1034 * data flow. That makes the register entirely useless to read
1035 * back so we have to assume that nothing made it to the card
1036 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001038 if (data->error)
1039 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001041 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042
Andrei Warkentine89d4562011-05-23 15:06:37 -05001043 /*
1044 * Need to send CMD12 if -
1045 * a) open-ended multiblock transfer (no CMD23)
1046 * b) error in multiblock transfer
1047 */
1048 if (data->stop &&
1049 (data->error ||
1050 !host->mrq->sbc)) {
1051
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052 /*
1053 * The controller needs a reset of internal state machines
1054 * upon error conditions.
1055 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001056 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 sdhci_reset(host, SDHCI_RESET_CMD);
1058 sdhci_reset(host, SDHCI_RESET_DATA);
1059 }
1060
1061 sdhci_send_command(host, data->stop);
1062 } else
1063 tasklet_schedule(&host->finish_tasklet);
1064}
1065
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301066#define SDHCI_REQUEST_TIMEOUT 10 /* Default request timeout in seconds */
1067
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
1069{
1070 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001071 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001072 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073
1074 WARN_ON(host->cmd);
1075
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001077 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001078
1079 mask = SDHCI_CMD_INHIBIT;
1080 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
1081 mask |= SDHCI_DATA_INHIBIT;
1082
1083 /* We shouldn't wait for data inihibit for stop commands, even
1084 though they might use busy signaling */
1085 if (host->mrq->data && (cmd == host->mrq->data->stop))
1086 mask &= ~SDHCI_DATA_INHIBIT;
1087
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001088 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001089 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301090 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001091 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001093 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094 tasklet_schedule(&host->finish_tasklet);
1095 return;
1096 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001097 timeout--;
1098 mdelay(1);
1099 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100
Sahitya Tummalaa03d9af2013-02-11 15:59:03 +05301101 mod_timer(&host->timer, jiffies + SDHCI_REQUEST_TIMEOUT * HZ);
1102
1103 if (cmd->cmd_timeout_ms > SDHCI_REQUEST_TIMEOUT * MSEC_PER_SEC)
1104 mod_timer(&host->timer, jiffies +
1105 (msecs_to_jiffies(cmd->cmd_timeout_ms * 2)));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106
1107 host->cmd = cmd;
1108
Andrei Warkentina3c77782011-04-11 16:13:42 -05001109 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001111 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112
Andrei Warkentine89d4562011-05-23 15:06:37 -05001113 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001114
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301116 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001118 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 tasklet_schedule(&host->finish_tasklet);
1120 return;
1121 }
1122
1123 if (!(cmd->flags & MMC_RSP_PRESENT))
1124 flags = SDHCI_CMD_RESP_NONE;
1125 else if (cmd->flags & MMC_RSP_136)
1126 flags = SDHCI_CMD_RESP_LONG;
1127 else if (cmd->flags & MMC_RSP_BUSY)
1128 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1129 else
1130 flags = SDHCI_CMD_RESP_SHORT;
1131
1132 if (cmd->flags & MMC_RSP_CRC)
1133 flags |= SDHCI_CMD_CRC;
1134 if (cmd->flags & MMC_RSP_OPCODE)
1135 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301136
1137 /* CMD19 is special in that the Data Present Select should be set */
Girish K S2cd06dc2012-01-06 09:56:39 +05301138 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001139 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400 ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301140 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 flags |= SDHCI_CMD_DATA;
1142
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301143 if (cmd->data)
1144 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001145 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146}
1147
1148static void sdhci_finish_command(struct sdhci_host *host)
1149{
1150 int i;
1151
1152 BUG_ON(host->cmd == NULL);
1153
1154 if (host->cmd->flags & MMC_RSP_PRESENT) {
1155 if (host->cmd->flags & MMC_RSP_136) {
1156 /* CRC is stripped so we need to do some shifting. */
1157 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001158 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159 SDHCI_RESPONSE + (3-i)*4) << 8;
1160 if (i != 3)
1161 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001162 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 SDHCI_RESPONSE + (3-i)*4-1);
1164 }
1165 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001166 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167 }
1168 }
1169
Pierre Ossman17b04292007-07-22 22:18:46 +02001170 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171
Andrei Warkentine89d4562011-05-23 15:06:37 -05001172 /* Finished CMD23, now send actual command. */
1173 if (host->cmd == host->mrq->sbc) {
1174 host->cmd = NULL;
1175 sdhci_send_command(host, host->mrq->cmd);
1176 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001177
Andrei Warkentine89d4562011-05-23 15:06:37 -05001178 /* Processed actual command. */
1179 if (host->data && host->data_early)
1180 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001181
Andrei Warkentine89d4562011-05-23 15:06:37 -05001182 if (!host->cmd->data)
1183 tasklet_schedule(&host->finish_tasklet);
1184
1185 host->cmd = NULL;
1186 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001187}
1188
1189static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1190{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301191 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301192 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301193 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001194 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301195 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001196
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301197 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001198 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301199 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200
Sahitya Tummalaca422112013-02-22 12:15:54 +05301201 host->mmc->actual_clock = 0;
1202
Anton Vorontsov81146342009-03-17 00:13:59 +03001203 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301204 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001205 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301206 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001207 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301208 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001209 }
1210
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301211 if (host->clock)
1212 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
1214 if (clock == 0)
1215 goto out;
1216
Zhangfei Gao85105c52010-08-06 07:10:01 +08001217 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301218 /*
1219 * Check if the Host Controller supports Programmable Clock
1220 * Mode.
1221 */
1222 if (host->clk_mul) {
1223 u16 ctrl;
1224
1225 /*
1226 * We need to figure out whether the Host Driver needs
1227 * to select Programmable Clock Mode, or the value can
1228 * be set automatically by the Host Controller based on
1229 * the Preset Value registers.
1230 */
1231 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1232 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1233 for (div = 1; div <= 1024; div++) {
1234 if (((host->max_clk * host->clk_mul) /
1235 div) <= clock)
1236 break;
1237 }
1238 /*
1239 * Set Programmable Clock Mode in the Clock
1240 * Control register.
1241 */
1242 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301243 real_div = div;
1244 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301245 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001246 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301247 } else {
1248 /* Version 3.00 divisors must be a multiple of 2. */
1249 if (host->max_clk <= clock)
1250 div = 1;
1251 else {
1252 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1253 div += 2) {
1254 if ((host->max_clk / div) <= clock)
1255 break;
1256 }
1257 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301258 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301259 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001260 }
1261 } else {
1262 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001263 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001264 if ((host->max_clk / div) <= clock)
1265 break;
1266 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301267 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301268 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270
Sahitya Tummalaca422112013-02-22 12:15:54 +05301271 if (real_div)
1272 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1273
Sahitya Tummala00240122013-02-28 19:50:51 +05301274 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1275 div = 0;
1276
Arindam Nathc3ed3872011-05-05 12:19:06 +05301277 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001278 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1279 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001280 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001281 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001282
Chris Ball27f6cb12009-09-22 16:45:31 -07001283 /* Wait max 20 ms */
1284 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001285 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001286 & SDHCI_CLOCK_INT_STABLE)) {
1287 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301288 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001289 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001290 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301291 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001292 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001293 timeout--;
1294 mdelay(1);
1295 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296
1297 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001298 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299
1300out:
1301 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301302ret:
1303 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304}
1305
Sahitya Tummalaca422112013-02-22 12:15:54 +05301306static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001307{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001308 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001309
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001310 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001311 switch (1 << power) {
1312 case MMC_VDD_165_195:
1313 pwr = SDHCI_POWER_180;
1314 break;
1315 case MMC_VDD_29_30:
1316 case MMC_VDD_30_31:
1317 pwr = SDHCI_POWER_300;
1318 break;
1319 case MMC_VDD_32_33:
1320 case MMC_VDD_33_34:
1321 pwr = SDHCI_POWER_330;
1322 break;
1323 default:
1324 BUG();
1325 }
1326 }
1327
1328 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301329 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001330
Pierre Ossmanae628902009-05-03 20:45:03 +02001331 host->pwr = pwr;
1332
1333 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001334 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301335 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301336 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301337 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001338 }
1339
1340 /*
1341 * Spec says that we should clear the power reg before setting
1342 * a new value. Some controllers don't seem to like this though.
1343 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301344 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001345 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301346 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301347 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301348 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001349
Andres Salomone08c1692008-07-04 10:00:03 -07001350 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001351 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001352 * and set turn on power at the same time, so set the voltage first.
1353 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301354 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001355 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301356 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301357 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301358 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001359
1360 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001361
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001362 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);
Harald Welte557b0692009-06-18 16:53:38 +02001365
1366 /*
1367 * Some controllers need an extra 10ms delay of 10ms before they
1368 * can apply clock after applying power
1369 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001370 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001371 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301372
1373 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001374}
1375
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376/*****************************************************************************\
1377 * *
1378 * MMC callbacks *
1379 * *
1380\*****************************************************************************/
1381
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301382static int sdhci_enable(struct mmc_host *mmc)
1383{
1384 struct sdhci_host *host = mmc_priv(mmc);
1385
1386 if (host->cpu_dma_latency_us)
1387 pm_qos_update_request(&host->pm_qos_req_dma,
1388 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301389 if (host->ops->platform_bus_voting)
1390 host->ops->platform_bus_voting(host, 1);
1391
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301392 return 0;
1393}
1394
1395static int sdhci_disable(struct mmc_host *mmc)
1396{
1397 struct sdhci_host *host = mmc_priv(mmc);
1398
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301399 if (host->cpu_dma_latency_us) {
1400 /*
1401 * In performance mode, release QoS vote after a timeout to
1402 * make sure back-to-back requests don't suffer from latencies
1403 * that are involved to wake CPU from low power modes in cases
1404 * where the CPU goes into low power mode as soon as QoS vote is
1405 * released.
1406 */
1407 if (host->power_policy == SDHCI_PERFORMANCE_MODE)
1408 pm_qos_update_request_timeout(&host->pm_qos_req_dma,
1409 host->cpu_dma_latency_us,
1410 host->pm_qos_timeout_us);
1411 else
1412 pm_qos_update_request(&host->pm_qos_req_dma,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301413 PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301414 }
1415
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301416 if (host->ops->platform_bus_voting)
1417 host->ops->platform_bus_voting(host, 0);
1418
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301419 return 0;
1420}
1421
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301422static inline void sdhci_update_power_policy(struct sdhci_host *host,
1423 enum sdhci_power_policy policy)
1424{
1425 host->power_policy = policy;
1426}
1427
1428static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1429{
1430 int err = 0;
1431 struct sdhci_host *host = mmc_priv(mmc);
1432
1433 switch (state) {
1434 case MMC_LOAD_HIGH:
1435 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1436 break;
1437 case MMC_LOAD_LOW:
1438 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1439 break;
1440 default:
1441 err = -EINVAL;
1442 break;
1443 }
1444
1445 return err;
1446}
1447
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001448static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1449 bool is_first_req)
1450{
1451 struct sdhci_host *host = mmc_priv(mmc);
1452
1453 if (mrq->data->host_cookie) {
1454 mrq->data->host_cookie = 0;
1455 return;
1456 }
1457
1458 if (host->flags & SDHCI_REQ_USE_DMA)
1459 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1460 mrq->data->host_cookie = 0;
1461}
1462
1463static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1464 int err)
1465{
1466 struct sdhci_host *host = mmc_priv(mmc);
1467 struct mmc_data *data = mrq->data;
1468
1469 if (host->flags & SDHCI_REQ_USE_DMA) {
1470 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1471 (data->flags & MMC_DATA_WRITE) ?
1472 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1473 data->host_cookie = 0;
1474 }
1475}
1476
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301477static bool sdhci_check_state(struct sdhci_host *host)
1478{
1479 struct mmc_host *mmc = host->mmc;
1480
1481 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301482 (mmc_use_core_runtime_pm(mmc) ?
1483 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301484 return true;
1485 else
1486 return false;
1487}
1488
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1490{
1491 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001492 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493 unsigned long flags;
1494
1495 host = mmc_priv(mmc);
1496
Adrian Hunter50accb92011-10-03 15:33:34 +03001497 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301498 if (sdhci_check_state(host)) {
1499 sdhci_dump_state(host);
1500 WARN(1, "sdhci in bad state");
1501 mrq->cmd->error = -EIO;
1502 if (mrq->data)
1503 mrq->data->error = -EIO;
1504 tasklet_schedule(&host->finish_tasklet);
1505 return;
1506 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001507
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508 spin_lock_irqsave(&host->lock, flags);
1509
1510 WARN_ON(host->mrq != NULL);
1511
Pierre Ossmanf9134312008-12-21 17:01:48 +01001512#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001514#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001515
1516 /*
1517 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1518 * requests if Auto-CMD12 is enabled.
1519 */
1520 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001521 if (mrq->stop) {
1522 mrq->data->stop = NULL;
1523 mrq->stop = NULL;
1524 }
1525 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526
1527 host->mrq = mrq;
1528
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001529 /* If polling, assume that the card is always present. */
1530 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1531 present = true;
1532 else
1533 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1534 SDHCI_CARD_PRESENT;
1535
1536 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001537 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301539 } else {
1540 u32 present_state;
1541
1542 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1543 /*
1544 * Check if the re-tuning timer has already expired and there
1545 * is no on-going data transfer. If so, we need to execute
1546 * tuning procedure before sending command.
1547 */
1548 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1549 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1550 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301551 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301552 spin_lock_irqsave(&host->lock, flags);
1553
1554 /* Restore original mmc_request structure */
1555 host->mrq = mrq;
1556 }
1557
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001558 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001559 sdhci_send_command(host, mrq->sbc);
1560 else
1561 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301562 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001563
Pierre Ossman5f25a662006-10-04 02:15:39 -07001564 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001565 spin_unlock_irqrestore(&host->lock, flags);
1566}
1567
Adrian Hunter50accb92011-10-03 15:33:34 +03001568static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001569{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301571 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001572 u8 ctrl;
1573
Sahitya Tummala40474e42013-07-10 14:40:37 +05301574 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301575 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301576 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1577 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301578 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301579 return;
1580 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001581
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301582 if (ios->clock)
1583 sdhci_set_clock(host, ios->clock);
1584
1585 spin_lock_irqsave(&host->lock, flags);
1586 if (!host->clock) {
1587 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301588 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301589 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001590 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301591 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001592
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301593 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301594 vdd_bit = sdhci_set_power(host, ios->vdd);
1595
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301596 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301597 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301598
1599 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600
Philip Rakity643a81f2010-09-23 08:24:32 -07001601 if (host->ops->platform_send_init_74_clocks)
1602 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1603
Philip Rakity15ec4462010-11-19 16:48:39 -05001604 /*
1605 * If your platform has 8-bit width support but is not a v3 controller,
1606 * or if it requires special setup code, you should implement that in
1607 * platform_8bit_width().
1608 */
1609 if (host->ops->platform_8bit_width)
1610 host->ops->platform_8bit_width(host, ios->bus_width);
1611 else {
1612 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1613 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1614 ctrl &= ~SDHCI_CTRL_4BITBUS;
1615 if (host->version >= SDHCI_SPEC_300)
1616 ctrl |= SDHCI_CTRL_8BITBUS;
1617 } else {
1618 if (host->version >= SDHCI_SPEC_300)
1619 ctrl &= ~SDHCI_CTRL_8BITBUS;
1620 if (ios->bus_width == MMC_BUS_WIDTH_4)
1621 ctrl |= SDHCI_CTRL_4BITBUS;
1622 else
1623 ctrl &= ~SDHCI_CTRL_4BITBUS;
1624 }
1625 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1626 }
1627
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001628 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001629
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001630 if ((ios->timing == MMC_TIMING_SD_HS ||
1631 ios->timing == MMC_TIMING_MMC_HS)
1632 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001633 ctrl |= SDHCI_CTRL_HISPD;
1634 else
1635 ctrl &= ~SDHCI_CTRL_HISPD;
1636
Arindam Nathd6d50a12011-05-05 12:18:59 +05301637 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301638 u16 clk, ctrl_2;
1639 unsigned int clock;
1640
1641 /* In case of UHS-I modes, set High Speed Enable */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001642 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1643 (ios->timing == MMC_TIMING_MMC_HS200) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05301644 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301645 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1646 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001647 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301648 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301649
1650 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1651 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301652 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301653 /*
1654 * We only need to set Driver Strength if the
1655 * preset value enable is not set.
1656 */
1657 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1658 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1659 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1660 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1661 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1662
1663 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301664 } else {
1665 /*
1666 * According to SDHC Spec v3.00, if the Preset Value
1667 * Enable in the Host Control 2 register is set, we
1668 * need to reset SD Clock Enable before changing High
1669 * Speed Enable to avoid generating clock gliches.
1670 */
Arindam Nath758535c2011-05-05 12:19:00 +05301671
1672 /* Reset SD Clock Enable */
1673 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1674 clk &= ~SDHCI_CLOCK_CARD_EN;
1675 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1676
1677 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1678
1679 /* Re-enable SD Clock */
1680 clock = host->clock;
1681 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301682 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301683 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301684 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301685 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301686
Arindam Nath49c468f2011-05-05 12:19:01 +05301687 /* Reset SD Clock Enable */
1688 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1689 clk &= ~SDHCI_CLOCK_CARD_EN;
1690 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1691
Philip Rakity6322cdd2011-05-13 11:17:15 +05301692 if (host->ops->set_uhs_signaling)
1693 host->ops->set_uhs_signaling(host, ios->timing);
1694 else {
1695 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1696 /* Select Bus Speed Mode for host */
1697 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001698 if (ios->timing == MMC_TIMING_MMC_HS400)
1699 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1700 else if (ios->timing == MMC_TIMING_MMC_HS200)
Girish K S2cd06dc2012-01-06 09:56:39 +05301701 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1702 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301703 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1704 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1705 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1706 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1707 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1708 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1709 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1710 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1711 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1712 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1713 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301714
1715 /* Re-enable SD Clock */
1716 clock = host->clock;
1717 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301718 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301719 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301720 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301721 } else
1722 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301723
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301724 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001725 /*
1726 * Some (ENE) controllers go apeshit on some ios operation,
1727 * signalling timeout and CRC errors even on CMD0. Resetting
1728 * it on each ios seems to solve the problem.
1729 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001730 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001731 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1732
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301733 /*
1734 * Reset the chip on each power off.
1735 * Should clear out any weird states.
1736 */
1737 if (ios->power_mode == MMC_POWER_OFF) {
1738 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1739 sdhci_reinit(host);
1740 vdd_bit = sdhci_set_power(host, -1);
1741 if (host->vmmc && vdd_bit != -1)
1742 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1743 }
1744 if (!ios->clock)
1745 sdhci_set_clock(host, ios->clock);
1746
Pierre Ossman5f25a662006-10-04 02:15:39 -07001747 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301748 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001749}
1750
Adrian Hunter50accb92011-10-03 15:33:34 +03001751static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1752{
1753 struct sdhci_host *host = mmc_priv(mmc);
1754
1755 sdhci_runtime_pm_get(host);
1756 sdhci_do_set_ios(host, ios);
1757 sdhci_runtime_pm_put(host);
1758}
1759
1760static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001761{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001762 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001763 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765 spin_lock_irqsave(&host->lock, flags);
1766
Pierre Ossman1e728592008-04-16 19:13:13 +02001767 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001768 is_readonly = 0;
1769 else if (host->ops->get_ro)
1770 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001771 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001772 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1773 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001774
1775 spin_unlock_irqrestore(&host->lock, flags);
1776
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001777 /* This quirk needs to be replaced by a callback-function later */
1778 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1779 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001780}
1781
Takashi Iwai82b0e232011-04-21 20:26:38 +02001782#define SAMPLE_COUNT 5
1783
Adrian Hunter50accb92011-10-03 15:33:34 +03001784static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001785{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001786 int i, ro_count;
1787
Takashi Iwai82b0e232011-04-21 20:26:38 +02001788 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001789 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001790
1791 ro_count = 0;
1792 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001793 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001794 if (++ro_count > SAMPLE_COUNT / 2)
1795 return 1;
1796 }
1797 msleep(30);
1798 }
1799 return 0;
1800}
1801
Adrian Hunter50accb92011-10-03 15:33:34 +03001802static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001803{
Adrian Hunter50accb92011-10-03 15:33:34 +03001804 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001805
Adrian Hunter50accb92011-10-03 15:33:34 +03001806 if (host->ops && host->ops->hw_reset)
1807 host->ops->hw_reset(host);
1808}
Adrian Hunter20758b62011-08-29 16:42:12 +03001809
Adrian Hunter50accb92011-10-03 15:33:34 +03001810static int sdhci_get_ro(struct mmc_host *mmc)
1811{
1812 struct sdhci_host *host = mmc_priv(mmc);
1813 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001814
Adrian Hunter50accb92011-10-03 15:33:34 +03001815 sdhci_runtime_pm_get(host);
1816 ret = sdhci_do_get_ro(host);
1817 sdhci_runtime_pm_put(host);
1818 return ret;
1819}
1820
1821static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1822{
Pierre Ossman1e728592008-04-16 19:13:13 +02001823 if (host->flags & SDHCI_DEVICE_DEAD)
1824 goto out;
1825
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001826 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001827 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1828 else
1829 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1830
1831 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1832 if (host->runtime_suspended)
1833 goto out;
1834
1835 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001836 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1837 else
1838 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001839out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001840 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001841}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001842
Adrian Hunter50accb92011-10-03 15:33:34 +03001843static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1844{
1845 struct sdhci_host *host = mmc_priv(mmc);
1846 unsigned long flags;
1847
1848 spin_lock_irqsave(&host->lock, flags);
1849 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001850 spin_unlock_irqrestore(&host->lock, flags);
1851}
1852
Adrian Hunter50accb92011-10-03 15:33:34 +03001853static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1854 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301855{
Arindam Nathf2119df2011-05-05 12:18:57 +05301856 u8 pwr;
1857 u16 clk, ctrl;
1858 u32 present_state;
1859
Arindam Nathf2119df2011-05-05 12:18:57 +05301860 /*
1861 * Signal Voltage Switching is only applicable for Host Controllers
1862 * v3.00 and above.
1863 */
1864 if (host->version < SDHCI_SPEC_300)
1865 return 0;
1866
1867 /*
1868 * We first check whether the request is to set signalling voltage
1869 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1870 */
1871 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1872 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1873 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1874 ctrl &= ~SDHCI_CTRL_VDD_180;
1875 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301876 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301877 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301878
1879 /* Wait for 5ms */
1880 usleep_range(5000, 5500);
1881
1882 /* 3.3V regulator output should be stable within 5 ms */
1883 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1884 if (!(ctrl & SDHCI_CTRL_VDD_180))
1885 return 0;
1886 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301887 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301888 "signalling voltage failed\n");
1889 return -EIO;
1890 }
1891 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1892 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1893 /* Stop SDCLK */
1894 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1895 clk &= ~SDHCI_CLOCK_CARD_EN;
1896 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1897
1898 /* Check whether DAT[3:0] is 0000 */
1899 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1900 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1901 SDHCI_DATA_LVL_SHIFT)) {
1902 /*
1903 * Enable 1.8V Signal Enable in the Host Control2
1904 * register
1905 */
1906 ctrl |= SDHCI_CTRL_VDD_180;
1907 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301908 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301909 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301910
1911 /* Wait for 5ms */
1912 usleep_range(5000, 5500);
1913
1914 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1915 if (ctrl & SDHCI_CTRL_VDD_180) {
1916 /* Provide SDCLK again and wait for 1ms*/
1917 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1918 clk |= SDHCI_CLOCK_CARD_EN;
1919 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1920 usleep_range(1000, 1500);
1921
1922 /*
1923 * If DAT[3:0] level is 1111b, then the card
1924 * was successfully switched to 1.8V signaling.
1925 */
1926 present_state = sdhci_readl(host,
1927 SDHCI_PRESENT_STATE);
1928 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1929 SDHCI_DATA_LVL_MASK)
1930 return 0;
1931 }
1932 }
1933
1934 /*
1935 * If we are here, that means the switch to 1.8V signaling
1936 * failed. We power cycle the card, and retry initialization
1937 * sequence by setting S18R to 0.
1938 */
1939 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1940 pwr &= ~SDHCI_POWER_ON;
1941 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301942 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301943 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301944
1945 /* Wait for 1ms as per the spec */
1946 usleep_range(1000, 1500);
1947 pwr |= SDHCI_POWER_ON;
1948 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301949 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301950 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301951
Sahitya Tummalaca422112013-02-22 12:15:54 +05301952 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301953 "voltage failed, retrying with S18R set to 0\n");
1954 return -EAGAIN;
1955 } else
1956 /* No signal voltage switch required */
1957 return 0;
1958}
1959
Adrian Hunter50accb92011-10-03 15:33:34 +03001960static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1961 struct mmc_ios *ios)
1962{
1963 struct sdhci_host *host = mmc_priv(mmc);
1964 int err;
1965
1966 if (host->version < SDHCI_SPEC_300)
1967 return 0;
1968 sdhci_runtime_pm_get(host);
1969 err = sdhci_do_start_signal_voltage_switch(host, ios);
1970 sdhci_runtime_pm_put(host);
1971 return err;
1972}
1973
Girish K S2cd06dc2012-01-06 09:56:39 +05301974static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301975{
1976 struct sdhci_host *host;
1977 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301978 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301979 int tuning_loop_counter = MAX_TUNING_LOOP;
1980 unsigned long timeout;
1981 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301982 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301983
1984 host = mmc_priv(mmc);
1985
Adrian Hunter50accb92011-10-03 15:33:34 +03001986 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301987 disable_irq(host->irq);
1988 spin_lock(&host->lock);
1989
1990 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1991
1992 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301993 * The Host Controller needs tuning only in case of SDR104 mode
1994 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301995 * Capabilities register.
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07001996 * If the Host Controller supports the HS400/HS200 mode then the
Girish K S2cd06dc2012-01-06 09:56:39 +05301997 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301998 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001999 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
2000 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002001 (host->flags & SDHCI_HS200_NEEDS_TUNING) ||
2002 (host->flags & SDHCI_HS400_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05302003 requires_tuning_nonuhs = true;
2004
Arindam Nathb513ea22011-05-05 12:19:04 +05302005 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05302006 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2008 else {
2009 spin_unlock(&host->lock);
2010 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002011 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302012 return 0;
2013 }
2014
Asutosh Das8ddd3482013-01-04 11:45:46 +05302015 if (host->ops->execute_tuning) {
2016 spin_unlock(&host->lock);
2017 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05302018 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05302019 disable_irq(host->irq);
2020 spin_lock(&host->lock);
2021 goto out;
2022 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302023 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2024
2025 /*
2026 * As per the Host Controller spec v3.00, tuning command
2027 * generates Buffer Read Ready interrupt, so enable that.
2028 *
2029 * Note: The spec clearly says that when tuning sequence
2030 * is being performed, the controller does not generate
2031 * interrupts other than Buffer Read Ready interrupt. But
2032 * to make sure we don't hit a controller bug, we _only_
2033 * enable Buffer Read Ready interrupt here.
2034 */
2035 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
2036 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
2037
2038 /*
2039 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
2040 * of loops reaches 40 times or a timeout of 150ms occurs.
2041 */
2042 timeout = 150;
2043 do {
2044 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03002045 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302046
2047 if (!tuning_loop_counter && !timeout)
2048 break;
2049
Girish K S2cd06dc2012-01-06 09:56:39 +05302050 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302051 cmd.arg = 0;
2052 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2053 cmd.retries = 0;
2054 cmd.data = NULL;
2055 cmd.error = 0;
2056
2057 mrq.cmd = &cmd;
2058 host->mrq = &mrq;
2059
2060 /*
2061 * In response to CMD19, the card sends 64 bytes of tuning
2062 * block to the Host Controller. So we set the block size
2063 * to 64 here.
2064 */
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002065 if ((cmd.opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2066 (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200)) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302067 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2068 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2069 SDHCI_BLOCK_SIZE);
2070 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2071 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2072 SDHCI_BLOCK_SIZE);
2073 } else {
2074 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2075 SDHCI_BLOCK_SIZE);
2076 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302077
2078 /*
2079 * The tuning block is sent by the card to the host controller.
2080 * So we set the TRNS_READ bit in the Transfer Mode register.
2081 * This also takes care of setting DMA Enable and Multi Block
2082 * Select in the same register to 0.
2083 */
2084 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2085
2086 sdhci_send_command(host, &cmd);
2087
2088 host->cmd = NULL;
2089 host->mrq = NULL;
2090
2091 spin_unlock(&host->lock);
2092 enable_irq(host->irq);
2093
2094 /* Wait for Buffer Read Ready interrupt */
2095 wait_event_interruptible_timeout(host->buf_ready_int,
2096 (host->tuning_done == 1),
2097 msecs_to_jiffies(50));
2098 disable_irq(host->irq);
2099 spin_lock(&host->lock);
2100
2101 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302102 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302103 "Buffer Read Ready interrupt during tuning "
2104 "procedure, falling back to fixed sampling "
2105 "clock\n");
2106 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2107 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2108 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2109 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2110
2111 err = -EIO;
2112 goto out;
2113 }
2114
2115 host->tuning_done = 0;
2116
2117 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2118 tuning_loop_counter--;
2119 timeout--;
2120 mdelay(1);
2121 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2122
2123 /*
2124 * The Host Driver has exhausted the maximum number of loops allowed,
2125 * so use fixed sampling frequency.
2126 */
2127 if (!tuning_loop_counter || !timeout) {
2128 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2129 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2130 } else {
2131 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302132 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302133 " failed, falling back to fixed sampling"
2134 " clock\n");
2135 err = -EIO;
2136 }
2137 }
2138
2139out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302140 /*
2141 * If this is the very first time we are here, we start the retuning
2142 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2143 * flag won't be set, we check this condition before actually starting
2144 * the timer.
2145 */
2146 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2147 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2148 mod_timer(&host->tuning_timer, jiffies +
2149 host->tuning_count * HZ);
2150 /* Tuning mode 1 limits the maximum data length to 4MB */
2151 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2152 } else {
2153 host->flags &= ~SDHCI_NEEDS_RETUNING;
2154 /* Reload the new initial value for timer */
2155 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2156 mod_timer(&host->tuning_timer, jiffies +
2157 host->tuning_count * HZ);
2158 }
2159
2160 /*
2161 * In case tuning fails, host controllers which support re-tuning can
2162 * try tuning again at a later time, when the re-tuning timer expires.
2163 * So for these controllers, we return 0. Since there might be other
2164 * controllers who do not have this capability, we return error for
2165 * them.
2166 */
2167 if (err && host->tuning_count &&
2168 host->tuning_mode == SDHCI_TUNING_MODE_1)
2169 err = 0;
2170
Arindam Nathb513ea22011-05-05 12:19:04 +05302171 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2172 spin_unlock(&host->lock);
2173 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002174 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302175
2176 return err;
2177}
2178
Adrian Hunter50accb92011-10-03 15:33:34 +03002179static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302180{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302181 u16 ctrl;
2182 unsigned long flags;
2183
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302184 /* Host Controller v3.00 defines preset value registers */
2185 if (host->version < SDHCI_SPEC_300)
2186 return;
2187
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302188 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2189 return;
2190
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302191 spin_lock_irqsave(&host->lock, flags);
2192
2193 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2194
2195 /*
2196 * We only enable or disable Preset Value if they are not already
2197 * enabled or disabled respectively. Otherwise, we bail out.
2198 */
2199 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2200 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2201 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002202 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302203 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2204 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2205 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002206 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302207 }
2208
2209 spin_unlock_irqrestore(&host->lock, flags);
2210}
2211
Adrian Hunter50accb92011-10-03 15:33:34 +03002212static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2213{
2214 struct sdhci_host *host = mmc_priv(mmc);
2215
2216 sdhci_runtime_pm_get(host);
2217 sdhci_do_enable_preset_value(host, enable);
2218 sdhci_runtime_pm_put(host);
2219}
2220
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002221static int sdhci_stop_request(struct mmc_host *mmc)
2222{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002223 struct sdhci_host *host = mmc_priv(mmc);
2224 unsigned long flags;
2225 struct mmc_data *data;
2226
2227 spin_lock_irqsave(&host->lock, flags);
2228 if (!host->mrq || !host->data)
2229 goto out;
2230
2231 data = host->data;
2232
2233 if (host->ops->disable_data_xfer)
2234 host->ops->disable_data_xfer(host);
2235
2236 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2237
2238 if (host->flags & SDHCI_REQ_USE_DMA) {
2239 if (host->flags & SDHCI_USE_ADMA) {
2240 sdhci_adma_table_post(host, data);
2241 } else {
2242 if (!data->host_cookie)
2243 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2244 data->sg_len,
2245 (data->flags & MMC_DATA_READ) ?
2246 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2247 }
2248 }
2249 del_timer(&host->timer);
2250 host->mrq = NULL;
2251 host->cmd = NULL;
2252 host->data = NULL;
2253out:
2254 spin_unlock_irqrestore(&host->lock, flags);
2255 return 0;
2256}
2257
2258static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2259{
2260 struct sdhci_host *host = mmc_priv(mmc);
2261 u32 present_state = 0;
2262
2263 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2264
2265 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002266}
2267
David Brownellab7aefd2006-11-12 17:55:30 -08002268static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002269 .pre_req = sdhci_pre_req,
2270 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271 .request = sdhci_request,
2272 .set_ios = sdhci_set_ios,
2273 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002274 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002275 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302276 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302277 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302278 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302279 .enable = sdhci_enable,
2280 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002281 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002282 .get_xfer_remain = sdhci_get_xfer_remain,
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05302283 .notify_load = sdhci_notify_load,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284};
2285
2286/*****************************************************************************\
2287 * *
2288 * Tasklets *
2289 * *
2290\*****************************************************************************/
2291
2292static void sdhci_tasklet_card(unsigned long param)
2293{
2294 struct sdhci_host *host;
2295 unsigned long flags;
2296
2297 host = (struct sdhci_host*)param;
2298
2299 spin_lock_irqsave(&host->lock, flags);
2300
Adrian Hunter50accb92011-10-03 15:33:34 +03002301 /* Check host->mrq first in case we are runtime suspended */
2302 if (host->mrq &&
2303 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302304 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002305 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302306 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002307 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002308
Adrian Hunter50accb92011-10-03 15:33:34 +03002309 sdhci_reset(host, SDHCI_RESET_CMD);
2310 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311
Adrian Hunter50accb92011-10-03 15:33:34 +03002312 host->mrq->cmd->error = -ENOMEDIUM;
2313 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314 }
2315
2316 spin_unlock_irqrestore(&host->lock, flags);
2317
Pierre Ossman04cf5852008-08-18 22:18:14 +02002318 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002319}
2320
2321static void sdhci_tasklet_finish(unsigned long param)
2322{
2323 struct sdhci_host *host;
2324 unsigned long flags;
2325 struct mmc_request *mrq;
2326
2327 host = (struct sdhci_host*)param;
2328
Adrian Hunter50accb92011-10-03 15:33:34 +03002329 spin_lock_irqsave(&host->lock, flags);
2330
Chris Ball0c9c99a2011-04-27 17:35:31 -04002331 /*
2332 * If this tasklet gets rescheduled while running, it will
2333 * be run again afterwards but without any active request.
2334 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002335 if (!host->mrq) {
2336 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002337 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002338 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339
2340 del_timer(&host->timer);
2341
2342 mrq = host->mrq;
2343
Pierre Ossmand129bce2006-03-24 03:18:17 -08002344 /*
2345 * The controller needs a reset of internal state machines
2346 * upon error conditions.
2347 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002348 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002349 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002350 (mrq->data && (mrq->data->error ||
2351 (mrq->data->stop && mrq->data->stop->error))) ||
2352 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002353
2354 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002355 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002356 unsigned int clock;
2357
2358 /* This is to force an update */
2359 clock = host->clock;
2360 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302361 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002362 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302363 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002364 }
2365
2366 /* Spec says we should do both at the same time, but Ricoh
2367 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368 sdhci_reset(host, SDHCI_RESET_CMD);
2369 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002370 } else {
2371 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2372 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002373 }
2374
2375 host->mrq = NULL;
2376 host->cmd = NULL;
2377 host->data = NULL;
2378
Pierre Ossmanf9134312008-12-21 17:01:48 +01002379#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002380 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002381#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382
Pierre Ossman5f25a662006-10-04 02:15:39 -07002383 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384 spin_unlock_irqrestore(&host->lock, flags);
2385
2386 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002387 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388}
2389
2390static void sdhci_timeout_timer(unsigned long data)
2391{
2392 struct sdhci_host *host;
2393 unsigned long flags;
2394
2395 host = (struct sdhci_host*)data;
2396
2397 spin_lock_irqsave(&host->lock, flags);
2398
2399 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302400 if (!host->mrq->cmd->ignore_timeout) {
2401 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2402 mmc_hostname(host->mmc));
2403 sdhci_dumpregs(host);
2404 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405
2406 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302407 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2408 mmc_hostname(host->mmc),
2409 (host->data->blksz * host->data->blocks),
2410 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2411 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002412 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413 sdhci_finish_data(host);
2414 } else {
2415 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002416 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002418 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419
2420 tasklet_schedule(&host->finish_tasklet);
2421 }
2422 }
2423
Pierre Ossman5f25a662006-10-04 02:15:39 -07002424 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425 spin_unlock_irqrestore(&host->lock, flags);
2426}
2427
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302428static void sdhci_tuning_timer(unsigned long data)
2429{
2430 struct sdhci_host *host;
2431 unsigned long flags;
2432
2433 host = (struct sdhci_host *)data;
2434
2435 spin_lock_irqsave(&host->lock, flags);
2436
2437 host->flags |= SDHCI_NEEDS_RETUNING;
2438
2439 spin_unlock_irqrestore(&host->lock, flags);
2440}
2441
Pierre Ossmand129bce2006-03-24 03:18:17 -08002442/*****************************************************************************\
2443 * *
2444 * Interrupt handling *
2445 * *
2446\*****************************************************************************/
2447
2448static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2449{
Asutosh Das80c02552013-07-23 16:20:34 +05302450 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451 BUG_ON(intmask == 0);
2452
2453 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302454 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002455 "though no command operation was in progress.\n",
2456 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 sdhci_dumpregs(host);
2458 return;
2459 }
2460
Pierre Ossman43b58b32007-07-25 23:15:27 +02002461 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002462 host->cmd->error = -ETIMEDOUT;
2463 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2464 SDHCI_INT_INDEX))
2465 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002466
Asutosh Das80c02552013-07-23 16:20:34 +05302467 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
2468 auto_cmd_status = sdhci_readw(host, SDHCI_AUTO_CMD_ERR);
2469 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2470 SDHCI_AUTO_CMD_INDEX_ERR |
2471 SDHCI_AUTO_CMD_ENDBIT_ERR))
2472 host->cmd->error = -EIO;
2473 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2474 host->cmd->error = -ETIMEDOUT;
2475 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2476 host->cmd->error = -EILSEQ;
2477 }
2478
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302479 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002480 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2481 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302482 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2483 if (intmask & SDHCI_INT_CRC) {
2484 sdhci_reset(host, SDHCI_RESET_CMD);
2485 host->cmd->error = 0;
2486 }
2487 }
2488 }
2489
Pierre Ossmane8095172008-07-25 01:09:08 +02002490 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002491 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002492 return;
2493 }
2494
2495 /*
2496 * The host can send and interrupt when the busy state has
2497 * ended, allowing us to wait without wasting CPU cycles.
2498 * Unfortunately this is overloaded on the "data complete"
2499 * interrupt, so we need to take some care when handling
2500 * it.
2501 *
2502 * Note: The 1.0 specification is a bit ambiguous about this
2503 * feature so there might be some problems with older
2504 * controllers.
2505 */
2506 if (host->cmd->flags & MMC_RSP_BUSY) {
2507 if (host->cmd->data)
2508 DBG("Cannot wait for busy signal when also "
2509 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002510 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002511 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002512
2513 /* The controller does not support the end-of-busy IRQ,
2514 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002515 }
2516
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302517 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002518 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
2519 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302520 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2521 if (intmask & SDHCI_INT_CRC) {
2522 sdhci_finish_command(host);
2523 return;
2524 }
2525 }
2526 }
2527
Pierre Ossmane8095172008-07-25 01:09:08 +02002528 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002529 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530}
2531
Ben Dooks6882a8c2009-06-14 13:52:38 +01002532static void sdhci_show_adma_error(struct sdhci_host *host)
2533{
2534 const char *name = mmc_hostname(host->mmc);
2535 u8 *desc = host->adma_desc;
2536 __le32 *dma;
2537 __le16 *len;
2538 u8 attr;
2539
2540 sdhci_dumpregs(host);
2541
2542 while (true) {
2543 dma = (__le32 *)(desc + 4);
2544 len = (__le16 *)(desc + 2);
2545 attr = *desc;
2546
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302547 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002548 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2549
2550 desc += 8;
2551
2552 if (attr & 2)
2553 break;
2554 }
2555}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002556
Pierre Ossmand129bce2006-03-24 03:18:17 -08002557static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2558{
Girish K S2cd06dc2012-01-06 09:56:39 +05302559 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302560 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 BUG_ON(intmask == 0);
2562
Arindam Nathb513ea22011-05-05 12:19:04 +05302563 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2564 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302565 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2566 if (command == MMC_SEND_TUNING_BLOCK ||
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002567 command == MMC_SEND_TUNING_BLOCK_HS200 ||
2568 command == MMC_SEND_TUNING_BLOCK_HS400) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302569 host->tuning_done = 1;
2570 wake_up(&host->buf_ready_int);
2571 return;
2572 }
2573 }
2574
Pierre Ossmand129bce2006-03-24 03:18:17 -08002575 if (!host->data) {
2576 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002577 * The "data complete" interrupt is also used to
2578 * indicate that a busy state has ended. See comment
2579 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002580 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002581 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2582 if (intmask & SDHCI_INT_DATA_END) {
2583 sdhci_finish_command(host);
2584 return;
2585 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302586 if (host->quirks2 &
2587 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2588 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002589 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002590
Sahitya Tummalaca422112013-02-22 12:15:54 +05302591 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002592 "though no data operation was in progress.\n",
2593 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002594 sdhci_dumpregs(host);
2595
2596 return;
2597 }
2598
2599 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002600 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002601 else if (intmask & SDHCI_INT_DATA_END_BIT)
2602 host->data->error = -EILSEQ;
2603 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2604 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2605 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002606 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002607 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302608 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002609 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002610 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002611 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302612 if (host->data->error) {
2613 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2614 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2615 command = SDHCI_GET_CMD(sdhci_readw(host,
2616 SDHCI_COMMAND));
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07002617 if ((command != MMC_SEND_TUNING_BLOCK_HS400) &&
2618 (command != MMC_SEND_TUNING_BLOCK_HS200) &&
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302619 (command != MMC_SEND_TUNING_BLOCK))
2620 pr_msg = true;
2621 } else {
2622 pr_msg = true;
2623 }
2624 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302625 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302626 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302627 host->data->error, ktime_to_ms(ktime_sub(
2628 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302629 sdhci_dumpregs(host);
2630 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302632 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002633 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634 sdhci_transfer_pio(host);
2635
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002636 /*
2637 * We currently don't do anything fancy with DMA
2638 * boundaries, but as we can't disable the feature
2639 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002640 *
2641 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2642 * should return a valid address to continue from, but as
2643 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002644 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002645 if (intmask & SDHCI_INT_DMA_END) {
2646 u32 dmastart, dmanow;
2647 dmastart = sg_dma_address(host->data->sg);
2648 dmanow = dmastart + host->data->bytes_xfered;
2649 /*
2650 * Force update to the next DMA block boundary.
2651 */
2652 dmanow = (dmanow &
2653 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2654 SDHCI_DEFAULT_BOUNDARY_SIZE;
2655 host->data->bytes_xfered = dmanow - dmastart;
2656 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2657 " next 0x%08x\n",
2658 mmc_hostname(host->mmc), dmastart,
2659 host->data->bytes_xfered, dmanow);
2660 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2661 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002662
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002663 if (intmask & SDHCI_INT_DATA_END) {
2664 if (host->cmd) {
2665 /*
2666 * Data managed to finish before the
2667 * command completed. Make sure we do
2668 * things in the proper order.
2669 */
2670 host->data_early = 1;
2671 } else {
2672 sdhci_finish_data(host);
2673 }
2674 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002675 }
2676}
2677
David Howells7d12e782006-10-05 14:55:46 +01002678static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679{
2680 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002681 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002682 u32 intmask, unexpected = 0;
2683 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684
2685 spin_lock(&host->lock);
2686
Adrian Hunter50accb92011-10-03 15:33:34 +03002687 if (host->runtime_suspended) {
2688 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302689 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002690 mmc_hostname(host->mmc));
2691 return IRQ_HANDLED;
2692 }
2693
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002694 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002695
Mark Lord62df67a2007-03-06 13:30:13 +01002696 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002697 result = IRQ_NONE;
2698 goto out;
2699 }
2700
Alexander Stein6379b232012-03-14 09:52:10 +01002701again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002702 DBG("*** %s got interrupt: 0x%08x\n",
2703 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002704
Pierre Ossman3192a282006-06-30 02:22:26 -07002705 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302706 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2707 SDHCI_CARD_PRESENT;
2708
2709 /*
2710 * There is a observation on i.mx esdhc. INSERT bit will be
2711 * immediately set again when it gets cleared, if a card is
2712 * inserted. We have to mask the irq to prevent interrupt
2713 * storm which will freeze the system. And the REMOVE gets
2714 * the same situation.
2715 *
2716 * More testing are needed here to ensure it works for other
2717 * platforms though.
2718 */
2719 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2720 SDHCI_INT_CARD_REMOVE);
2721 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2722 SDHCI_INT_CARD_INSERT);
2723
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002724 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302725 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2726 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002728 }
2729
Pierre Ossmand129bce2006-03-24 03:18:17 -08002730 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002731 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2732 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002733 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2734 (host->clock <= 400000))
2735 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002736 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002737 }
2738
2739 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002740 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2741 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002742 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2743 (host->clock <= 400000))
2744 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002745 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002746 }
2747
2748 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2749
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002750 intmask &= ~SDHCI_INT_ERROR;
2751
Pierre Ossmand129bce2006-03-24 03:18:17 -08002752 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302753 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002754 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002755 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002756 }
2757
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002758 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002759
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002760 if (intmask & SDHCI_INT_CARD_INT)
2761 cardint = 1;
2762
2763 intmask &= ~SDHCI_INT_CARD_INT;
2764
Pierre Ossman3192a282006-06-30 02:22:26 -07002765 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002766 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002767 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002768 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002769
2770 result = IRQ_HANDLED;
2771
Alexander Stein6379b232012-03-14 09:52:10 +01002772 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2773 if (intmask && --max_loops)
2774 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775out:
2776 spin_unlock(&host->lock);
2777
Alexander Stein6379b232012-03-14 09:52:10 +01002778 if (unexpected) {
2779 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2780 mmc_hostname(host->mmc), unexpected);
2781 sdhci_dumpregs(host);
2782 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002783 /*
2784 * We have to delay this as it calls back into the driver.
2785 */
2786 if (cardint)
2787 mmc_signal_sdio_irq(host->mmc);
2788
Pierre Ossmand129bce2006-03-24 03:18:17 -08002789 return result;
2790}
2791
2792/*****************************************************************************\
2793 * *
2794 * Suspend/resume *
2795 * *
2796\*****************************************************************************/
2797
2798#ifdef CONFIG_PM
2799
Manuel Laussd72faa62011-11-03 11:09:45 +01002800int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002801{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002802 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302803 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002804
Chris Balla1b13b42012-02-06 00:43:59 -05002805 if (host->ops->platform_suspend)
2806 host->ops->platform_suspend(host);
2807
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002808 sdhci_disable_card_detection(host);
2809
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302810 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302811 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2812 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2813 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002814 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302815 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302816 }
2817
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002818 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302819 if (ret) {
2820 if (has_tuning_timer) {
2821 host->flags |= SDHCI_NEEDS_RETUNING;
2822 mod_timer(&host->tuning_timer, jiffies +
2823 host->tuning_count * HZ);
2824 }
2825
2826 sdhci_enable_card_detection(host);
2827
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002828 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302829 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002830
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002831 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002832
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002833 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834}
2835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002837
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002838int sdhci_resume_host(struct sdhci_host *host)
2839{
2840 int ret;
2841
Richard Röjforsa13abc72009-09-22 16:45:30 -07002842 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002843 if (host->ops->enable_dma)
2844 host->ops->enable_dma(host);
2845 }
2846
2847 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2848 mmc_hostname(host->mmc), host);
2849 if (ret)
2850 return ret;
2851
Adrian Hunter6308d292012-02-07 14:48:54 +02002852 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2853 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2854 /* Card keeps power but host controller does not */
2855 sdhci_init(host, 0);
2856 host->pwr = 0;
2857 host->clock = 0;
2858 sdhci_do_set_ios(host, &host->mmc->ios);
2859 } else {
2860 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2861 mmiowb();
2862 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863
2864 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002865 sdhci_enable_card_detection(host);
2866
Chris Balla1b13b42012-02-06 00:43:59 -05002867 if (host->ops->platform_resume)
2868 host->ops->platform_resume(host);
2869
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302870 /* Set the re-tuning expiration flag */
2871 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2872 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2873 host->flags |= SDHCI_NEEDS_RETUNING;
2874
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002875 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002876}
2877
2878EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002879
Daniel Drake5f619702010-11-04 22:20:39 +00002880void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2881{
2882 u8 val;
2883 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2884 val |= SDHCI_WAKE_ON_INT;
2885 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2886}
2887
2888EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2889
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890#endif /* CONFIG_PM */
2891
Adrian Hunter50accb92011-10-03 15:33:34 +03002892#ifdef CONFIG_PM_RUNTIME
2893
2894static int sdhci_runtime_pm_get(struct sdhci_host *host)
2895{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302896 if (!mmc_use_core_runtime_pm(host->mmc))
2897 return pm_runtime_get_sync(host->mmc->parent);
2898 else
2899 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002900}
2901
2902static int sdhci_runtime_pm_put(struct sdhci_host *host)
2903{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302904 if (!mmc_use_core_runtime_pm(host->mmc)) {
2905 pm_runtime_mark_last_busy(host->mmc->parent);
2906 return pm_runtime_put_autosuspend(host->mmc->parent);
2907 } else {
2908 return 0;
2909 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002910}
2911
2912int sdhci_runtime_suspend_host(struct sdhci_host *host)
2913{
2914 unsigned long flags;
2915 int ret = 0;
2916
2917 /* Disable tuning since we are suspending */
2918 if (host->version >= SDHCI_SPEC_300 &&
2919 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2920 del_timer_sync(&host->tuning_timer);
2921 host->flags &= ~SDHCI_NEEDS_RETUNING;
2922 }
2923
2924 spin_lock_irqsave(&host->lock, flags);
2925 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2926 spin_unlock_irqrestore(&host->lock, flags);
2927
2928 synchronize_irq(host->irq);
2929
2930 spin_lock_irqsave(&host->lock, flags);
2931 host->runtime_suspended = true;
2932 spin_unlock_irqrestore(&host->lock, flags);
2933
2934 return ret;
2935}
2936EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2937
2938int sdhci_runtime_resume_host(struct sdhci_host *host)
2939{
2940 unsigned long flags;
2941 int ret = 0, host_flags = host->flags;
2942
2943 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2944 if (host->ops->enable_dma)
2945 host->ops->enable_dma(host);
2946 }
2947
2948 sdhci_init(host, 0);
2949
2950 /* Force clock and power re-program */
2951 host->pwr = 0;
2952 host->clock = 0;
2953 sdhci_do_set_ios(host, &host->mmc->ios);
2954
2955 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2956 if (host_flags & SDHCI_PV_ENABLED)
2957 sdhci_do_enable_preset_value(host, true);
2958
2959 /* Set the re-tuning expiration flag */
2960 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2961 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2962 host->flags |= SDHCI_NEEDS_RETUNING;
2963
2964 spin_lock_irqsave(&host->lock, flags);
2965
2966 host->runtime_suspended = false;
2967
2968 /* Enable SDIO IRQ */
2969 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2970 sdhci_enable_sdio_irq_nolock(host, true);
2971
2972 /* Enable Card Detection */
2973 sdhci_enable_card_detection(host);
2974
2975 spin_unlock_irqrestore(&host->lock, flags);
2976
2977 return ret;
2978}
2979EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2980
2981#endif
2982
Pierre Ossmand129bce2006-03-24 03:18:17 -08002983/*****************************************************************************\
2984 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002985 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002986 * *
2987\*****************************************************************************/
2988
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002989struct sdhci_host *sdhci_alloc_host(struct device *dev,
2990 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002991{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002992 struct mmc_host *mmc;
2993 struct sdhci_host *host;
2994
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002995 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002996
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002997 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002998 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002999 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003000
3001 host = mmc_priv(mmc);
3002 host->mmc = mmc;
3003
Sahitya Tummala951c1202013-05-24 08:47:26 +05303004 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05303005 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05303006
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003007 return host;
3008}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003009
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003011
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003012int sdhci_add_host(struct sdhci_host *host)
3013{
3014 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303015 u32 caps[2];
3016 u32 max_current_caps;
3017 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003018 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003019
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003020 WARN_ON(host == NULL);
3021 if (host == NULL)
3022 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003023
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003024 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003025
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003026 if (debug_quirks)
3027 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03003028 if (debug_quirks2)
3029 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003030
Pierre Ossmand96649e2006-06-30 02:22:30 -07003031 sdhci_reset(host, SDHCI_RESET_ALL);
3032
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003033 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02003034 host->version = (host->version & SDHCI_SPEC_VER_MASK)
3035 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08003036 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303037 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003038 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02003039 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003040 }
3041
Arindam Nathf2119df2011-05-05 12:18:57 +05303042 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07003043 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044
Arindam Nathf2119df2011-05-05 12:18:57 +05303045 caps[1] = (host->version >= SDHCI_SPEC_300) ?
3046 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
3047
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003048 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003049 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05303050 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003051 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003052 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003053 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003054
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003055 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003056 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003057 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003058 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003059 }
3060
Arindam Nathf2119df2011-05-05 12:18:57 +05303061 if ((host->version >= SDHCI_SPEC_200) &&
3062 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003063 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003064
3065 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3066 (host->flags & SDHCI_USE_ADMA)) {
3067 DBG("Disabling ADMA as it is marked broken\n");
3068 host->flags &= ~SDHCI_USE_ADMA;
3069 }
3070
Richard Röjforsa13abc72009-09-22 16:45:30 -07003071 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003072 if (host->ops->enable_dma) {
3073 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303074 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003075 "available. Falling back to PIO.\n",
3076 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003077 host->flags &=
3078 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003079 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003080 }
3081 }
3082
Pierre Ossman2134a922008-06-28 18:28:51 +02003083 if (host->flags & SDHCI_USE_ADMA) {
3084 /*
3085 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303086 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02003087 * each of those entries.
3088 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303089 if (host->ops->get_max_segments)
3090 host->adma_max_desc = host->ops->get_max_segments();
3091 else
3092 host->adma_max_desc = 128;
3093
3094 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
3095 host->align_buf_sz = host->adma_max_desc * 4;
3096
3097 pr_debug("%s: %s: dma_desc_size: %d\n",
3098 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3099 host->adma_desc = kmalloc(host->adma_desc_sz,
3100 GFP_KERNEL);
3101 host->align_buffer = kmalloc(host->align_buf_sz,
3102 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003103 if (!host->adma_desc || !host->align_buffer) {
3104 kfree(host->adma_desc);
3105 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303106 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003107 "buffers. Falling back to standard DMA.\n",
3108 mmc_hostname(mmc));
3109 host->flags &= ~SDHCI_USE_ADMA;
3110 }
3111 }
3112
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003113 host->next_data.cookie = 1;
3114
Pierre Ossman76591502008-07-21 00:32:11 +02003115 /*
3116 * If we use DMA, then it's up to the caller to set the DMA
3117 * mask, but PIO does not need the hw shim so we set a new
3118 * mask here in that case.
3119 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003120 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003121 host->dma_mask = DMA_BIT_MASK(64);
3122 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3123 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003124
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003125 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303126 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003127 >> SDHCI_CLOCK_BASE_SHIFT;
3128 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303129 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003130 >> SDHCI_CLOCK_BASE_SHIFT;
3131
Pierre Ossmand129bce2006-03-24 03:18:17 -08003132 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003133 if (host->max_clk == 0 || host->quirks &
3134 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003135 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303136 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003137 "frequency.\n", mmc_hostname(mmc));
3138 return -ENODEV;
3139 }
3140 host->max_clk = host->ops->get_max_clock(host);
3141 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003142
3143 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303144 * In case of Host Controller v3.00, find out whether clock
3145 * multiplier is supported.
3146 */
3147 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3148 SDHCI_CLOCK_MUL_SHIFT;
3149
3150 /*
3151 * In case the value in Clock Multiplier is 0, then programmable
3152 * clock mode is not supported, otherwise the actual clock
3153 * multiplier is one more than the value of Clock Multiplier
3154 * in the Capabilities Register.
3155 */
3156 if (host->clk_mul)
3157 host->clk_mul += 1;
3158
3159 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003160 * Set host parameters.
3161 */
3162 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303163 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003164 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003165 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303166 else if (host->version >= SDHCI_SPEC_300) {
3167 if (host->clk_mul) {
3168 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3169 mmc->f_max = host->max_clk * host->clk_mul;
3170 } else
3171 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3172 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003173 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003174
Sahitya Tummalaca422112013-02-22 12:15:54 +05303175 host->timeout_clk =
3176 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3177 if (host->timeout_clk == 0) {
3178 if (host->ops->get_timeout_clock) {
3179 host->timeout_clk = host->ops->get_timeout_clock(host);
3180 } else if (!(host->quirks &
3181 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3182 pr_err("%s: Hardware doesn't specify timeout clock "
3183 "frequency.\n", mmc_hostname(mmc));
3184 return -ENODEV;
3185 }
3186 }
3187 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3188 host->timeout_clk *= 1000;
3189
3190 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3191 host->timeout_clk = mmc->f_max / 1000;
3192
Krishna Kondaa20d3362013-04-01 21:01:59 -07003193 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3194 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303195
Andrei Warkentine89d4562011-05-23 15:06:37 -05003196 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3197
3198 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3199 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003200
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003201 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003202 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003203 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003204 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003205 host->flags |= SDHCI_AUTO_CMD23;
3206 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3207 } else {
3208 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3209 }
3210
Philip Rakity15ec4462010-11-19 16:48:39 -05003211 /*
3212 * A controller may support 8-bit width, but the board itself
3213 * might not have the pins brought out. Boards that support
3214 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3215 * their platform code before calling sdhci_add_host(), and we
3216 * won't assume 8-bit width for hosts without that CAP.
3217 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003218 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003219 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003220
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003222 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003223
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003224 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3225 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003226 mmc->caps |= MMC_CAP_NEEDS_POLL;
3227
Al Cooper4188bba2012-03-16 15:54:17 -04003228 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3229 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3230 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303231 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3232
3233 /* SDR104 supports also implies SDR50 support */
3234 if (caps[1] & SDHCI_SUPPORT_SDR104)
3235 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3236 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3237 mmc->caps |= MMC_CAP_UHS_SDR50;
3238
3239 if (caps[1] & SDHCI_SUPPORT_DDR50)
3240 mmc->caps |= MMC_CAP_UHS_DDR50;
3241
Girish K S2cd06dc2012-01-06 09:56:39 +05303242 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303243 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3244 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3245
Girish K S2cd06dc2012-01-06 09:56:39 +05303246 /* Does the host need tuning for HS200? */
3247 if (mmc->caps2 & MMC_CAP2_HS200)
3248 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3249
Venkat Gopalakrishnanceca4752013-06-11 21:29:40 -07003250 /* Does the host need tuning for HS400? */
3251 if (mmc->caps2 & MMC_CAP2_HS400)
3252 host->flags |= SDHCI_HS400_NEEDS_TUNING;
3253
Arindam Nathd6d50a12011-05-05 12:18:59 +05303254 /* Driver Type(s) (A, C, D) supported by the host */
3255 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3256 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3257 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3258 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3259 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3260 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3261
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003262 /* Initial value for re-tuning timer count */
3263 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3264 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3265
3266 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303267 * In case Re-tuning Timer is not disabled, the actual value of
3268 * re-tuning timer will be 2 ^ (n - 1).
3269 */
3270 if (host->tuning_count)
3271 host->tuning_count = 1 << (host->tuning_count - 1);
3272
3273 /* Re-tuning mode supported by the Host Controller */
3274 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3275 SDHCI_RETUNING_MODE_SHIFT;
3276
Takashi Iwai8f230f42010-12-08 10:04:30 +01003277 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303278 /*
3279 * According to SD Host Controller spec v3.00, if the Host System
3280 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3281 * the value is meaningful only if Voltage Support in the Capabilities
3282 * register is set. The actual current value is 4 times the register
3283 * value.
3284 */
3285 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3286
3287 if (caps[0] & SDHCI_CAN_VDD_330) {
3288 int max_current_330;
3289
Takashi Iwai8f230f42010-12-08 10:04:30 +01003290 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303291
3292 max_current_330 = ((max_current_caps &
3293 SDHCI_MAX_CURRENT_330_MASK) >>
3294 SDHCI_MAX_CURRENT_330_SHIFT) *
3295 SDHCI_MAX_CURRENT_MULTIPLIER;
3296
3297 if (max_current_330 > 150)
3298 mmc->caps |= MMC_CAP_SET_XPC_330;
3299 }
3300 if (caps[0] & SDHCI_CAN_VDD_300) {
3301 int max_current_300;
3302
Takashi Iwai8f230f42010-12-08 10:04:30 +01003303 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303304
3305 max_current_300 = ((max_current_caps &
3306 SDHCI_MAX_CURRENT_300_MASK) >>
3307 SDHCI_MAX_CURRENT_300_SHIFT) *
3308 SDHCI_MAX_CURRENT_MULTIPLIER;
3309
3310 if (max_current_300 > 150)
3311 mmc->caps |= MMC_CAP_SET_XPC_300;
3312 }
3313 if (caps[0] & SDHCI_CAN_VDD_180) {
3314 int max_current_180;
3315
Takashi Iwai8f230f42010-12-08 10:04:30 +01003316 ocr_avail |= MMC_VDD_165_195;
3317
Arindam Nathf2119df2011-05-05 12:18:57 +05303318 max_current_180 = ((max_current_caps &
3319 SDHCI_MAX_CURRENT_180_MASK) >>
3320 SDHCI_MAX_CURRENT_180_SHIFT) *
3321 SDHCI_MAX_CURRENT_MULTIPLIER;
3322
3323 if (max_current_180 > 150)
3324 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303325
3326 /* Maximum current capabilities of the host at 1.8V */
3327 if (max_current_180 >= 800)
3328 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3329 else if (max_current_180 >= 600)
3330 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3331 else if (max_current_180 >= 400)
3332 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3333 else
3334 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303335 }
3336
Takashi Iwai8f230f42010-12-08 10:04:30 +01003337 mmc->ocr_avail = ocr_avail;
3338 mmc->ocr_avail_sdio = ocr_avail;
3339 if (host->ocr_avail_sdio)
3340 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3341 mmc->ocr_avail_sd = ocr_avail;
3342 if (host->ocr_avail_sd)
3343 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3344 else /* normal SD controllers don't support 1.8V */
3345 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3346 mmc->ocr_avail_mmc = ocr_avail;
3347 if (host->ocr_avail_mmc)
3348 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003349
3350 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303351 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003352 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003353 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003354 }
3355
Pierre Ossmand129bce2006-03-24 03:18:17 -08003356 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003357 * Maximum number of segments. Depends on if the hardware
3358 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003359 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003360 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303361 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003362 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003363 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303364 else/* PIO */
3365 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366
3367 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003368 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303369 * size (512KiB), unless specified by platform specific driver. Each
3370 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003371 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303372 if (host->ops->get_max_segments)
3373 mmc->max_req_size = (host->adma_max_desc * 65536);
3374 else
3375 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376
3377 /*
3378 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003379 * of bytes. When doing hardware scatter/gather, each entry cannot
3380 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003381 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003382 if (host->flags & SDHCI_USE_ADMA) {
3383 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3384 mmc->max_seg_size = 65535;
3385 else
3386 mmc->max_seg_size = 65536;
3387 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003388 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003389 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003390
3391 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003392 * Maximum block size. This varies from controller to controller and
3393 * is specified in the capabilities register.
3394 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003395 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3396 mmc->max_blk_size = 2;
3397 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303398 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003399 SDHCI_MAX_BLOCK_SHIFT;
3400 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303401 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003402 "assuming 512 bytes\n", mmc_hostname(mmc));
3403 mmc->max_blk_size = 0;
3404 }
3405 }
3406
3407 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003408
3409 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003410 * Maximum block count.
3411 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003412 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003413
3414 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415 * Init tasklets.
3416 */
3417 tasklet_init(&host->card_tasklet,
3418 sdhci_tasklet_card, (unsigned long)host);
3419 tasklet_init(&host->finish_tasklet,
3420 sdhci_tasklet_finish, (unsigned long)host);
3421
Al Viroe4cad1b2006-10-10 22:47:07 +01003422 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003423
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303424 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303425 init_waitqueue_head(&host->buf_ready_int);
3426
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303427 /* Initialize re-tuning timer */
3428 init_timer(&host->tuning_timer);
3429 host->tuning_timer.data = (unsigned long)host;
3430 host->tuning_timer.function = sdhci_tuning_timer;
3431 }
3432
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003433 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003434 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003435 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003436 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003437
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003438 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3439 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303440 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003441 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003442 }
3443
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003444 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003445
3446#ifdef CONFIG_MMC_DEBUG
3447 sdhci_dumpregs(host);
3448#endif
3449
Pierre Ossmanf9134312008-12-21 17:01:48 +01003450#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003451 snprintf(host->led_name, sizeof(host->led_name),
3452 "%s::", mmc_hostname(mmc));
3453 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003454 host->led.brightness = LED_OFF;
3455 host->led.default_trigger = mmc_hostname(mmc);
3456 host->led.brightness_set = sdhci_led_control;
3457
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003458 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003459 if (ret)
3460 goto reset;
3461#endif
3462
Pierre Ossman5f25a662006-10-04 02:15:39 -07003463 mmiowb();
3464
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303465 if (host->cpu_dma_latency_us) {
3466 host->pm_qos_timeout_us = 10000; /* default value */
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303467 pm_qos_add_request(&host->pm_qos_req_dma,
3468 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303469
3470 host->pm_qos_tout.show = show_sdhci_pm_qos_tout;
3471 host->pm_qos_tout.store = store_sdhci_pm_qos_tout;
3472 sysfs_attr_init(&host->pm_qos_tout.attr);
3473 host->pm_qos_tout.attr.name = "pm_qos_unvote_delay";
3474 host->pm_qos_tout.attr.mode = S_IRUGO | S_IWUSR;
3475 ret = device_create_file(mmc_dev(mmc), &host->pm_qos_tout);
3476 if (ret)
3477 pr_err("%s: cannot create pm_qos_unvote_delay %d\n",
3478 mmc_hostname(mmc), ret);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303479 }
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303480
Pierre Ossmand129bce2006-03-24 03:18:17 -08003481 mmc_add_host(mmc);
3482
Sahitya Tummalaca422112013-02-22 12:15:54 +05303483 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003484 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003485 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3486 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003487
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003488 sdhci_enable_card_detection(host);
3489
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490 return 0;
3491
Pierre Ossmanf9134312008-12-21 17:01:48 +01003492#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003493reset:
3494 sdhci_reset(host, SDHCI_RESET_ALL);
3495 free_irq(host->irq, host);
3496#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003497untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003498 tasklet_kill(&host->card_tasklet);
3499 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003500
3501 return ret;
3502}
3503
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003504EXPORT_SYMBOL_GPL(sdhci_add_host);
3505
Pierre Ossman1e728592008-04-16 19:13:13 +02003506void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003507{
Pierre Ossman1e728592008-04-16 19:13:13 +02003508 unsigned long flags;
3509
3510 if (dead) {
3511 spin_lock_irqsave(&host->lock, flags);
3512
3513 host->flags |= SDHCI_DEVICE_DEAD;
3514
3515 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303516 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003517 " transfer!\n", mmc_hostname(host->mmc));
3518
3519 host->mrq->cmd->error = -ENOMEDIUM;
3520 tasklet_schedule(&host->finish_tasklet);
3521 }
3522
3523 spin_unlock_irqrestore(&host->lock, flags);
3524 }
3525
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003526 sdhci_disable_card_detection(host);
3527
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303528 if (host->cpu_dma_latency_us)
3529 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003530 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003531
Pierre Ossmanf9134312008-12-21 17:01:48 +01003532#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003533 led_classdev_unregister(&host->led);
3534#endif
3535
Pierre Ossman1e728592008-04-16 19:13:13 +02003536 if (!dead)
3537 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003538
3539 free_irq(host->irq, host);
3540
3541 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303542 if (host->version >= SDHCI_SPEC_300)
3543 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003544
3545 tasklet_kill(&host->card_tasklet);
3546 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003547
Sahitya Tummalaca422112013-02-22 12:15:54 +05303548 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003549 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003550
Pierre Ossman2134a922008-06-28 18:28:51 +02003551 kfree(host->adma_desc);
3552 kfree(host->align_buffer);
3553
3554 host->adma_desc = NULL;
3555 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003556}
3557
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003558EXPORT_SYMBOL_GPL(sdhci_remove_host);
3559
3560void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003561{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003562 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003563}
3564
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003565EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003566
3567/*****************************************************************************\
3568 * *
3569 * Driver init/exit *
3570 * *
3571\*****************************************************************************/
3572
3573static int __init sdhci_drv_init(void)
3574{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303575 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003576 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303577 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003578
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003579 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003580}
3581
3582static void __exit sdhci_drv_exit(void)
3583{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003584}
3585
3586module_init(sdhci_drv_init);
3587module_exit(sdhci_drv_exit);
3588
Pierre Ossmandf673b22006-06-30 02:22:31 -07003589module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003590module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003591
Pierre Ossman32710e82009-04-08 20:14:54 +02003592MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003593MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003594MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003595
Pierre Ossmandf673b22006-06-30 02:22:31 -07003596MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003597MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");