blob: 17b7251e4e18b46d112bdb3a7cf68f9b5ca81ffb [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 ||
1139 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 flags |= SDHCI_CMD_DATA;
1141
Sahitya Tummala48b458e2013-04-08 12:53:44 +05301142 if (cmd->data)
1143 host->data_start_time = ktime_get();
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001144 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145}
1146
1147static void sdhci_finish_command(struct sdhci_host *host)
1148{
1149 int i;
1150
1151 BUG_ON(host->cmd == NULL);
1152
1153 if (host->cmd->flags & MMC_RSP_PRESENT) {
1154 if (host->cmd->flags & MMC_RSP_136) {
1155 /* CRC is stripped so we need to do some shifting. */
1156 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001157 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158 SDHCI_RESPONSE + (3-i)*4) << 8;
1159 if (i != 3)
1160 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001161 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 SDHCI_RESPONSE + (3-i)*4-1);
1163 }
1164 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001165 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166 }
1167 }
1168
Pierre Ossman17b04292007-07-22 22:18:46 +02001169 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
Andrei Warkentine89d4562011-05-23 15:06:37 -05001171 /* Finished CMD23, now send actual command. */
1172 if (host->cmd == host->mrq->sbc) {
1173 host->cmd = NULL;
1174 sdhci_send_command(host, host->mrq->cmd);
1175 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001176
Andrei Warkentine89d4562011-05-23 15:06:37 -05001177 /* Processed actual command. */
1178 if (host->data && host->data_early)
1179 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180
Andrei Warkentine89d4562011-05-23 15:06:37 -05001181 if (!host->cmd->data)
1182 tasklet_schedule(&host->finish_tasklet);
1183
1184 host->cmd = NULL;
1185 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186}
1187
1188static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1189{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301190 int div = 0; /* Initialized for compiler warning */
Sahitya Tummalaca422112013-02-22 12:15:54 +05301191 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301192 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001193 unsigned long timeout;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301194 unsigned long flags;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001195
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301196 spin_lock_irqsave(&host->lock, flags);
Todd Poynor30832ab2011-12-27 15:48:46 +02001197 if (clock && clock == host->clock)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301198 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199
Sahitya Tummalaca422112013-02-22 12:15:54 +05301200 host->mmc->actual_clock = 0;
1201
Anton Vorontsov81146342009-03-17 00:13:59 +03001202 if (host->ops->set_clock) {
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301203 spin_unlock_irqrestore(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001204 host->ops->set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301205 spin_lock_irqsave(&host->lock, flags);
Anton Vorontsov81146342009-03-17 00:13:59 +03001206 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301207 goto ret;
Anton Vorontsov81146342009-03-17 00:13:59 +03001208 }
1209
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301210 if (host->clock)
1211 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212
1213 if (clock == 0)
1214 goto out;
1215
Zhangfei Gao85105c52010-08-06 07:10:01 +08001216 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301217 /*
1218 * Check if the Host Controller supports Programmable Clock
1219 * Mode.
1220 */
1221 if (host->clk_mul) {
1222 u16 ctrl;
1223
1224 /*
1225 * We need to figure out whether the Host Driver needs
1226 * to select Programmable Clock Mode, or the value can
1227 * be set automatically by the Host Controller based on
1228 * the Preset Value registers.
1229 */
1230 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1231 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1232 for (div = 1; div <= 1024; div++) {
1233 if (((host->max_clk * host->clk_mul) /
1234 div) <= clock)
1235 break;
1236 }
1237 /*
1238 * Set Programmable Clock Mode in the Clock
1239 * Control register.
1240 */
1241 clk = SDHCI_PROG_CLOCK_MODE;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301242 real_div = div;
1243 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301244 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001245 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301246 } else {
1247 /* Version 3.00 divisors must be a multiple of 2. */
1248 if (host->max_clk <= clock)
1249 div = 1;
1250 else {
1251 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1252 div += 2) {
1253 if ((host->max_clk / div) <= clock)
1254 break;
1255 }
1256 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301257 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301258 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001259 }
1260 } else {
1261 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001262 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001263 if ((host->max_clk / div) <= clock)
1264 break;
1265 }
Sahitya Tummalaca422112013-02-22 12:15:54 +05301266 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301267 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269
Sahitya Tummalaca422112013-02-22 12:15:54 +05301270 if (real_div)
1271 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1272
Sahitya Tummala00240122013-02-28 19:50:51 +05301273 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1274 div = 0;
1275
Arindam Nathc3ed3872011-05-05 12:19:06 +05301276 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001277 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1278 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001279 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001280 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001281
Chris Ball27f6cb12009-09-22 16:45:31 -07001282 /* Wait max 20 ms */
1283 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001284 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001285 & SDHCI_CLOCK_INT_STABLE)) {
1286 if (timeout == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301287 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001288 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001289 sdhci_dumpregs(host);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301290 goto ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001291 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001292 timeout--;
1293 mdelay(1);
1294 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001295
1296 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001297 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001298
1299out:
1300 host->clock = clock;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301301ret:
1302 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303}
1304
Sahitya Tummalaca422112013-02-22 12:15:54 +05301305static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001306{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001307 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001308
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001309 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001310 switch (1 << power) {
1311 case MMC_VDD_165_195:
1312 pwr = SDHCI_POWER_180;
1313 break;
1314 case MMC_VDD_29_30:
1315 case MMC_VDD_30_31:
1316 pwr = SDHCI_POWER_300;
1317 break;
1318 case MMC_VDD_32_33:
1319 case MMC_VDD_33_34:
1320 pwr = SDHCI_POWER_330;
1321 break;
1322 default:
1323 BUG();
1324 }
1325 }
1326
1327 if (host->pwr == pwr)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301328 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001329
Pierre Ossmanae628902009-05-03 20:45:03 +02001330 host->pwr = pwr;
1331
1332 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001333 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301334 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301335 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301336 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001337 }
1338
1339 /*
1340 * Spec says that we should clear the power reg before setting
1341 * a new value. Some controllers don't seem to like this though.
1342 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301343 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001344 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301345 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301346 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301347 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001348
Andres Salomone08c1692008-07-04 10:00:03 -07001349 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001350 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001351 * and set turn on power at the same time, so set the voltage first.
1352 */
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301353 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001354 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301355 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301356 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301357 }
Pierre Ossmanae628902009-05-03 20:45:03 +02001358
1359 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001360
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001361 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301362 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301363 host->ops->check_power_status(host, REQ_BUS_ON);
Harald Welte557b0692009-06-18 16:53:38 +02001364
1365 /*
1366 * Some controllers need an extra 10ms delay of 10ms before they
1367 * can apply clock after applying power
1368 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001369 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001370 mdelay(10);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301371
1372 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001373}
1374
Pierre Ossmand129bce2006-03-24 03:18:17 -08001375/*****************************************************************************\
1376 * *
1377 * MMC callbacks *
1378 * *
1379\*****************************************************************************/
1380
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301381static int sdhci_enable(struct mmc_host *mmc)
1382{
1383 struct sdhci_host *host = mmc_priv(mmc);
1384
1385 if (host->cpu_dma_latency_us)
1386 pm_qos_update_request(&host->pm_qos_req_dma,
1387 host->cpu_dma_latency_us);
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301388 if (host->ops->platform_bus_voting)
1389 host->ops->platform_bus_voting(host, 1);
1390
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301391 return 0;
1392}
1393
1394static int sdhci_disable(struct mmc_host *mmc)
1395{
1396 struct sdhci_host *host = mmc_priv(mmc);
1397
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301398 if (host->cpu_dma_latency_us) {
1399 /*
1400 * In performance mode, release QoS vote after a timeout to
1401 * make sure back-to-back requests don't suffer from latencies
1402 * that are involved to wake CPU from low power modes in cases
1403 * where the CPU goes into low power mode as soon as QoS vote is
1404 * released.
1405 */
1406 if (host->power_policy == SDHCI_PERFORMANCE_MODE)
1407 pm_qos_update_request_timeout(&host->pm_qos_req_dma,
1408 host->cpu_dma_latency_us,
1409 host->pm_qos_timeout_us);
1410 else
1411 pm_qos_update_request(&host->pm_qos_req_dma,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301412 PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301413 }
1414
Sahitya Tummala9f5cbb82013-03-10 14:12:52 +05301415 if (host->ops->platform_bus_voting)
1416 host->ops->platform_bus_voting(host, 0);
1417
Sahitya Tummalab4e84042013-03-10 07:03:17 +05301418 return 0;
1419}
1420
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05301421static inline void sdhci_update_power_policy(struct sdhci_host *host,
1422 enum sdhci_power_policy policy)
1423{
1424 host->power_policy = policy;
1425}
1426
1427static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1428{
1429 int err = 0;
1430 struct sdhci_host *host = mmc_priv(mmc);
1431
1432 switch (state) {
1433 case MMC_LOAD_HIGH:
1434 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1435 break;
1436 case MMC_LOAD_LOW:
1437 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1438 break;
1439 default:
1440 err = -EINVAL;
1441 break;
1442 }
1443
1444 return err;
1445}
1446
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08001447static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1448 bool is_first_req)
1449{
1450 struct sdhci_host *host = mmc_priv(mmc);
1451
1452 if (mrq->data->host_cookie) {
1453 mrq->data->host_cookie = 0;
1454 return;
1455 }
1456
1457 if (host->flags & SDHCI_REQ_USE_DMA)
1458 if (sdhci_pre_dma_transfer(host, mrq->data, &host->next_data) < 0)
1459 mrq->data->host_cookie = 0;
1460}
1461
1462static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1463 int err)
1464{
1465 struct sdhci_host *host = mmc_priv(mmc);
1466 struct mmc_data *data = mrq->data;
1467
1468 if (host->flags & SDHCI_REQ_USE_DMA) {
1469 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1470 (data->flags & MMC_DATA_WRITE) ?
1471 DMA_TO_DEVICE : DMA_FROM_DEVICE);
1472 data->host_cookie = 0;
1473 }
1474}
1475
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301476static bool sdhci_check_state(struct sdhci_host *host)
1477{
1478 struct mmc_host *mmc = host->mmc;
1479
1480 if (!host->clock || !host->pwr ||
Sahitya Tummala1b248c42013-05-24 14:08:10 +05301481 (mmc_use_core_runtime_pm(mmc) ?
1482 pm_runtime_suspended(mmc->parent) : 0))
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301483 return true;
1484 else
1485 return false;
1486}
1487
Pierre Ossmand129bce2006-03-24 03:18:17 -08001488static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1489{
1490 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001491 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492 unsigned long flags;
1493
1494 host = mmc_priv(mmc);
1495
Adrian Hunter50accb92011-10-03 15:33:34 +03001496 sdhci_runtime_pm_get(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05301497 if (sdhci_check_state(host)) {
1498 sdhci_dump_state(host);
1499 WARN(1, "sdhci in bad state");
1500 mrq->cmd->error = -EIO;
1501 if (mrq->data)
1502 mrq->data->error = -EIO;
1503 tasklet_schedule(&host->finish_tasklet);
1504 return;
1505 }
Adrian Hunter50accb92011-10-03 15:33:34 +03001506
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507 spin_lock_irqsave(&host->lock, flags);
1508
1509 WARN_ON(host->mrq != NULL);
1510
Pierre Ossmanf9134312008-12-21 17:01:48 +01001511#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001513#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001514
1515 /*
1516 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1517 * requests if Auto-CMD12 is enabled.
1518 */
1519 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001520 if (mrq->stop) {
1521 mrq->data->stop = NULL;
1522 mrq->stop = NULL;
1523 }
1524 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001525
1526 host->mrq = mrq;
1527
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001528 /* If polling, assume that the card is always present. */
1529 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1530 present = true;
1531 else
1532 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1533 SDHCI_CARD_PRESENT;
1534
1535 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001536 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301538 } else {
1539 u32 present_state;
1540
1541 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1542 /*
1543 * Check if the re-tuning timer has already expired and there
1544 * is no on-going data transfer. If so, we need to execute
1545 * tuning procedure before sending command.
1546 */
1547 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1548 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1549 spin_unlock_irqrestore(&host->lock, flags);
Girish K S2cd06dc2012-01-06 09:56:39 +05301550 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301551 spin_lock_irqsave(&host->lock, flags);
1552
1553 /* Restore original mmc_request structure */
1554 host->mrq = mrq;
1555 }
1556
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001557 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001558 sdhci_send_command(host, mrq->sbc);
1559 else
1560 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301561 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001562
Pierre Ossman5f25a662006-10-04 02:15:39 -07001563 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001564 spin_unlock_irqrestore(&host->lock, flags);
1565}
1566
Adrian Hunter50accb92011-10-03 15:33:34 +03001567static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001568{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001569 unsigned long flags;
Sahitya Tummalaca422112013-02-22 12:15:54 +05301570 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 u8 ctrl;
1572
Sahitya Tummala40474e42013-07-10 14:40:37 +05301573 mutex_lock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301574 if (host->flags & SDHCI_DEVICE_DEAD) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301575 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1576 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301577 mutex_unlock(&host->ios_mutex);
Sahitya Tummalaca422112013-02-22 12:15:54 +05301578 return;
1579 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001580
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301581 if (ios->clock)
1582 sdhci_set_clock(host, ios->clock);
1583
1584 spin_lock_irqsave(&host->lock, flags);
1585 if (!host->clock) {
1586 spin_unlock_irqrestore(&host->lock, flags);
Sahitya Tummala40474e42013-07-10 14:40:37 +05301587 mutex_unlock(&host->ios_mutex);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301588 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001589 }
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301590 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001591
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301592 if (ios->power_mode & (MMC_POWER_UP | MMC_POWER_ON))
Sahitya Tummalaca422112013-02-22 12:15:54 +05301593 vdd_bit = sdhci_set_power(host, ios->vdd);
1594
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301595 if (host->vmmc && vdd_bit != -1)
Sahitya Tummalaca422112013-02-22 12:15:54 +05301596 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301597
1598 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599
Philip Rakity643a81f2010-09-23 08:24:32 -07001600 if (host->ops->platform_send_init_74_clocks)
1601 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1602
Philip Rakity15ec4462010-11-19 16:48:39 -05001603 /*
1604 * If your platform has 8-bit width support but is not a v3 controller,
1605 * or if it requires special setup code, you should implement that in
1606 * platform_8bit_width().
1607 */
1608 if (host->ops->platform_8bit_width)
1609 host->ops->platform_8bit_width(host, ios->bus_width);
1610 else {
1611 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1612 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1613 ctrl &= ~SDHCI_CTRL_4BITBUS;
1614 if (host->version >= SDHCI_SPEC_300)
1615 ctrl |= SDHCI_CTRL_8BITBUS;
1616 } else {
1617 if (host->version >= SDHCI_SPEC_300)
1618 ctrl &= ~SDHCI_CTRL_8BITBUS;
1619 if (ios->bus_width == MMC_BUS_WIDTH_4)
1620 ctrl |= SDHCI_CTRL_4BITBUS;
1621 else
1622 ctrl &= ~SDHCI_CTRL_4BITBUS;
1623 }
1624 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1625 }
1626
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001627 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001628
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001629 if ((ios->timing == MMC_TIMING_SD_HS ||
1630 ios->timing == MMC_TIMING_MMC_HS)
1631 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001632 ctrl |= SDHCI_CTRL_HISPD;
1633 else
1634 ctrl &= ~SDHCI_CTRL_HISPD;
1635
Arindam Nathd6d50a12011-05-05 12:18:59 +05301636 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301637 u16 clk, ctrl_2;
1638 unsigned int clock;
1639
1640 /* In case of UHS-I modes, set High Speed Enable */
Girish K S2cd06dc2012-01-06 09:56:39 +05301641 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1642 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301643 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1644 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001645 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301646 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301647
1648 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1649 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301650 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301651 /*
1652 * We only need to set Driver Strength if the
1653 * preset value enable is not set.
1654 */
1655 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1656 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1657 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1658 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1659 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1660
1661 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301662 } else {
1663 /*
1664 * According to SDHC Spec v3.00, if the Preset Value
1665 * Enable in the Host Control 2 register is set, we
1666 * need to reset SD Clock Enable before changing High
1667 * Speed Enable to avoid generating clock gliches.
1668 */
Arindam Nath758535c2011-05-05 12:19:00 +05301669
1670 /* Reset SD Clock Enable */
1671 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1672 clk &= ~SDHCI_CLOCK_CARD_EN;
1673 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1674
1675 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1676
1677 /* Re-enable SD Clock */
1678 clock = host->clock;
1679 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301680 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301681 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301682 spin_lock_irqsave(&host->lock, flags);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301683 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301684
Arindam Nath49c468f2011-05-05 12:19:01 +05301685 /* Reset SD Clock Enable */
1686 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1687 clk &= ~SDHCI_CLOCK_CARD_EN;
1688 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1689
Philip Rakity6322cdd2011-05-13 11:17:15 +05301690 if (host->ops->set_uhs_signaling)
1691 host->ops->set_uhs_signaling(host, ios->timing);
1692 else {
1693 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1694 /* Select Bus Speed Mode for host */
1695 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S2cd06dc2012-01-06 09:56:39 +05301696 if (ios->timing == MMC_TIMING_MMC_HS200)
1697 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1698 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301699 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1700 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1701 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1702 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1703 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1704 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1705 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1706 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1707 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1708 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1709 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301710
1711 /* Re-enable SD Clock */
1712 clock = host->clock;
1713 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301714 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nath49c468f2011-05-05 12:19:01 +05301715 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301716 spin_lock_irqsave(&host->lock, flags);
Arindam Nath758535c2011-05-05 12:19:00 +05301717 } else
1718 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301719
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301720 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02001721 /*
1722 * Some (ENE) controllers go apeshit on some ios operation,
1723 * signalling timeout and CRC errors even on CMD0. Resetting
1724 * it on each ios seems to solve the problem.
1725 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001726 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001727 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1728
Sahitya Tummala04c3a462013-01-11 11:30:45 +05301729 /*
1730 * Reset the chip on each power off.
1731 * Should clear out any weird states.
1732 */
1733 if (ios->power_mode == MMC_POWER_OFF) {
1734 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
1735 sdhci_reinit(host);
1736 vdd_bit = sdhci_set_power(host, -1);
1737 if (host->vmmc && vdd_bit != -1)
1738 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1739 }
1740 if (!ios->clock)
1741 sdhci_set_clock(host, ios->clock);
1742
Pierre Ossman5f25a662006-10-04 02:15:39 -07001743 mmiowb();
Sahitya Tummala40474e42013-07-10 14:40:37 +05301744 mutex_unlock(&host->ios_mutex);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001745}
1746
Adrian Hunter50accb92011-10-03 15:33:34 +03001747static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1748{
1749 struct sdhci_host *host = mmc_priv(mmc);
1750
1751 sdhci_runtime_pm_get(host);
1752 sdhci_do_set_ios(host, ios);
1753 sdhci_runtime_pm_put(host);
1754}
1755
1756static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001757{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001758 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001759 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001760
Pierre Ossmand129bce2006-03-24 03:18:17 -08001761 spin_lock_irqsave(&host->lock, flags);
1762
Pierre Ossman1e728592008-04-16 19:13:13 +02001763 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001764 is_readonly = 0;
1765 else if (host->ops->get_ro)
1766 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001767 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001768 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1769 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001770
1771 spin_unlock_irqrestore(&host->lock, flags);
1772
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001773 /* This quirk needs to be replaced by a callback-function later */
1774 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1775 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001776}
1777
Takashi Iwai82b0e232011-04-21 20:26:38 +02001778#define SAMPLE_COUNT 5
1779
Adrian Hunter50accb92011-10-03 15:33:34 +03001780static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001781{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001782 int i, ro_count;
1783
Takashi Iwai82b0e232011-04-21 20:26:38 +02001784 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter50accb92011-10-03 15:33:34 +03001785 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001786
1787 ro_count = 0;
1788 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter50accb92011-10-03 15:33:34 +03001789 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001790 if (++ro_count > SAMPLE_COUNT / 2)
1791 return 1;
1792 }
1793 msleep(30);
1794 }
1795 return 0;
1796}
1797
Adrian Hunter50accb92011-10-03 15:33:34 +03001798static void sdhci_hw_reset(struct mmc_host *mmc)
Adrian Hunter20758b62011-08-29 16:42:12 +03001799{
Adrian Hunter50accb92011-10-03 15:33:34 +03001800 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001801
Adrian Hunter50accb92011-10-03 15:33:34 +03001802 if (host->ops && host->ops->hw_reset)
1803 host->ops->hw_reset(host);
1804}
Adrian Hunter20758b62011-08-29 16:42:12 +03001805
Adrian Hunter50accb92011-10-03 15:33:34 +03001806static int sdhci_get_ro(struct mmc_host *mmc)
1807{
1808 struct sdhci_host *host = mmc_priv(mmc);
1809 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001810
Adrian Hunter50accb92011-10-03 15:33:34 +03001811 sdhci_runtime_pm_get(host);
1812 ret = sdhci_do_get_ro(host);
1813 sdhci_runtime_pm_put(host);
1814 return ret;
1815}
1816
1817static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1818{
Pierre Ossman1e728592008-04-16 19:13:13 +02001819 if (host->flags & SDHCI_DEVICE_DEAD)
1820 goto out;
1821
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001822 if (enable)
Adrian Hunter50accb92011-10-03 15:33:34 +03001823 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1824 else
1825 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1826
1827 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1828 if (host->runtime_suspended)
1829 goto out;
1830
1831 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001832 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1833 else
1834 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001835out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001836 mmiowb();
Adrian Hunter50accb92011-10-03 15:33:34 +03001837}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001838
Adrian Hunter50accb92011-10-03 15:33:34 +03001839static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1840{
1841 struct sdhci_host *host = mmc_priv(mmc);
1842 unsigned long flags;
1843
1844 spin_lock_irqsave(&host->lock, flags);
1845 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001846 spin_unlock_irqrestore(&host->lock, flags);
1847}
1848
Adrian Hunter50accb92011-10-03 15:33:34 +03001849static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1850 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301851{
Arindam Nathf2119df2011-05-05 12:18:57 +05301852 u8 pwr;
1853 u16 clk, ctrl;
1854 u32 present_state;
1855
Arindam Nathf2119df2011-05-05 12:18:57 +05301856 /*
1857 * Signal Voltage Switching is only applicable for Host Controllers
1858 * v3.00 and above.
1859 */
1860 if (host->version < SDHCI_SPEC_300)
1861 return 0;
1862
1863 /*
1864 * We first check whether the request is to set signalling voltage
1865 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1866 */
1867 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1868 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1869 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1870 ctrl &= ~SDHCI_CTRL_VDD_180;
1871 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301872 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301873 host->ops->check_power_status(host, REQ_IO_HIGH);
Arindam Nathf2119df2011-05-05 12:18:57 +05301874
1875 /* Wait for 5ms */
1876 usleep_range(5000, 5500);
1877
1878 /* 3.3V regulator output should be stable within 5 ms */
1879 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1880 if (!(ctrl & SDHCI_CTRL_VDD_180))
1881 return 0;
1882 else {
Sahitya Tummalaca422112013-02-22 12:15:54 +05301883 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301884 "signalling voltage failed\n");
1885 return -EIO;
1886 }
1887 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1888 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1889 /* Stop SDCLK */
1890 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1891 clk &= ~SDHCI_CLOCK_CARD_EN;
1892 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1893
1894 /* Check whether DAT[3:0] is 0000 */
1895 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1896 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1897 SDHCI_DATA_LVL_SHIFT)) {
1898 /*
1899 * Enable 1.8V Signal Enable in the Host Control2
1900 * register
1901 */
1902 ctrl |= SDHCI_CTRL_VDD_180;
1903 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301904 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301905 host->ops->check_power_status(host, REQ_IO_LOW);
Arindam Nathf2119df2011-05-05 12:18:57 +05301906
1907 /* Wait for 5ms */
1908 usleep_range(5000, 5500);
1909
1910 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1911 if (ctrl & SDHCI_CTRL_VDD_180) {
1912 /* Provide SDCLK again and wait for 1ms*/
1913 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1914 clk |= SDHCI_CLOCK_CARD_EN;
1915 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1916 usleep_range(1000, 1500);
1917
1918 /*
1919 * If DAT[3:0] level is 1111b, then the card
1920 * was successfully switched to 1.8V signaling.
1921 */
1922 present_state = sdhci_readl(host,
1923 SDHCI_PRESENT_STATE);
1924 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1925 SDHCI_DATA_LVL_MASK)
1926 return 0;
1927 }
1928 }
1929
1930 /*
1931 * If we are here, that means the switch to 1.8V signaling
1932 * failed. We power cycle the card, and retry initialization
1933 * sequence by setting S18R to 0.
1934 */
1935 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1936 pwr &= ~SDHCI_POWER_ON;
1937 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301938 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301939 host->ops->check_power_status(host, REQ_BUS_OFF);
Arindam Nathf2119df2011-05-05 12:18:57 +05301940
1941 /* Wait for 1ms as per the spec */
1942 usleep_range(1000, 1500);
1943 pwr |= SDHCI_POWER_ON;
1944 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala66a6aa62013-02-21 10:09:49 +05301945 if (host->ops->check_power_status)
Sahitya Tummala179e7382013-03-20 19:24:01 +05301946 host->ops->check_power_status(host, REQ_BUS_ON);
Arindam Nathf2119df2011-05-05 12:18:57 +05301947
Sahitya Tummalaca422112013-02-22 12:15:54 +05301948 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301949 "voltage failed, retrying with S18R set to 0\n");
1950 return -EAGAIN;
1951 } else
1952 /* No signal voltage switch required */
1953 return 0;
1954}
1955
Adrian Hunter50accb92011-10-03 15:33:34 +03001956static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1957 struct mmc_ios *ios)
1958{
1959 struct sdhci_host *host = mmc_priv(mmc);
1960 int err;
1961
1962 if (host->version < SDHCI_SPEC_300)
1963 return 0;
1964 sdhci_runtime_pm_get(host);
1965 err = sdhci_do_start_signal_voltage_switch(host, ios);
1966 sdhci_runtime_pm_put(host);
1967 return err;
1968}
1969
Girish K S2cd06dc2012-01-06 09:56:39 +05301970static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301971{
1972 struct sdhci_host *host;
1973 u16 ctrl;
Asutosh Das8ddd3482013-01-04 11:45:46 +05301974 u32 ier = 0;
Arindam Nathb513ea22011-05-05 12:19:04 +05301975 int tuning_loop_counter = MAX_TUNING_LOOP;
1976 unsigned long timeout;
1977 int err = 0;
Girish K S2cd06dc2012-01-06 09:56:39 +05301978 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301979
1980 host = mmc_priv(mmc);
1981
Adrian Hunter50accb92011-10-03 15:33:34 +03001982 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301983 disable_irq(host->irq);
1984 spin_lock(&host->lock);
1985
1986 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1987
1988 /*
Girish K S2cd06dc2012-01-06 09:56:39 +05301989 * The Host Controller needs tuning only in case of SDR104 mode
1990 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301991 * Capabilities register.
Girish K S2cd06dc2012-01-06 09:56:39 +05301992 * If the Host Controller supports the HS200 mode then the
1993 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301994 */
Venkat Gopalakrishnana2a8df92012-11-18 20:59:33 -08001995 if ((((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1996 (host->flags & SDHCI_SDR50_NEEDS_TUNING)) ||
1997 (host->flags & SDHCI_HS200_NEEDS_TUNING))
Girish K S2cd06dc2012-01-06 09:56:39 +05301998 requires_tuning_nonuhs = true;
1999
Arindam Nathb513ea22011-05-05 12:19:04 +05302000 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S2cd06dc2012-01-06 09:56:39 +05302001 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05302002 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2003 else {
2004 spin_unlock(&host->lock);
2005 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002006 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302007 return 0;
2008 }
2009
Asutosh Das8ddd3482013-01-04 11:45:46 +05302010 if (host->ops->execute_tuning) {
2011 spin_unlock(&host->lock);
2012 enable_irq(host->irq);
Sahitya Tummaladda88d62013-06-13 10:36:11 +05302013 err = host->ops->execute_tuning(host, opcode);
Asutosh Das8ddd3482013-01-04 11:45:46 +05302014 disable_irq(host->irq);
2015 spin_lock(&host->lock);
2016 goto out;
2017 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302018 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2019
2020 /*
2021 * As per the Host Controller spec v3.00, tuning command
2022 * generates Buffer Read Ready interrupt, so enable that.
2023 *
2024 * Note: The spec clearly says that when tuning sequence
2025 * is being performed, the controller does not generate
2026 * interrupts other than Buffer Read Ready interrupt. But
2027 * to make sure we don't hit a controller bug, we _only_
2028 * enable Buffer Read Ready interrupt here.
2029 */
2030 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
2031 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
2032
2033 /*
2034 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
2035 * of loops reaches 40 times or a timeout of 150ms occurs.
2036 */
2037 timeout = 150;
2038 do {
2039 struct mmc_command cmd = {0};
Adrian Hunter50accb92011-10-03 15:33:34 +03002040 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302041
2042 if (!tuning_loop_counter && !timeout)
2043 break;
2044
Girish K S2cd06dc2012-01-06 09:56:39 +05302045 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302046 cmd.arg = 0;
2047 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2048 cmd.retries = 0;
2049 cmd.data = NULL;
2050 cmd.error = 0;
2051
2052 mrq.cmd = &cmd;
2053 host->mrq = &mrq;
2054
2055 /*
2056 * In response to CMD19, the card sends 64 bytes of tuning
2057 * block to the Host Controller. So we set the block size
2058 * to 64 here.
2059 */
Girish K S2cd06dc2012-01-06 09:56:39 +05302060 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2061 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2062 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2063 SDHCI_BLOCK_SIZE);
2064 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2065 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2066 SDHCI_BLOCK_SIZE);
2067 } else {
2068 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2069 SDHCI_BLOCK_SIZE);
2070 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302071
2072 /*
2073 * The tuning block is sent by the card to the host controller.
2074 * So we set the TRNS_READ bit in the Transfer Mode register.
2075 * This also takes care of setting DMA Enable and Multi Block
2076 * Select in the same register to 0.
2077 */
2078 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2079
2080 sdhci_send_command(host, &cmd);
2081
2082 host->cmd = NULL;
2083 host->mrq = NULL;
2084
2085 spin_unlock(&host->lock);
2086 enable_irq(host->irq);
2087
2088 /* Wait for Buffer Read Ready interrupt */
2089 wait_event_interruptible_timeout(host->buf_ready_int,
2090 (host->tuning_done == 1),
2091 msecs_to_jiffies(50));
2092 disable_irq(host->irq);
2093 spin_lock(&host->lock);
2094
2095 if (!host->tuning_done) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302096 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05302097 "Buffer Read Ready interrupt during tuning "
2098 "procedure, falling back to fixed sampling "
2099 "clock\n");
2100 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2101 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2102 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2103 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2104
2105 err = -EIO;
2106 goto out;
2107 }
2108
2109 host->tuning_done = 0;
2110
2111 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2112 tuning_loop_counter--;
2113 timeout--;
2114 mdelay(1);
2115 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2116
2117 /*
2118 * The Host Driver has exhausted the maximum number of loops allowed,
2119 * so use fixed sampling frequency.
2120 */
2121 if (!tuning_loop_counter || !timeout) {
2122 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2123 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2124 } else {
2125 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302126 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05302127 " failed, falling back to fixed sampling"
2128 " clock\n");
2129 err = -EIO;
2130 }
2131 }
2132
2133out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302134 /*
2135 * If this is the very first time we are here, we start the retuning
2136 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
2137 * flag won't be set, we check this condition before actually starting
2138 * the timer.
2139 */
2140 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
2141 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
2142 mod_timer(&host->tuning_timer, jiffies +
2143 host->tuning_count * HZ);
2144 /* Tuning mode 1 limits the maximum data length to 4MB */
2145 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
2146 } else {
2147 host->flags &= ~SDHCI_NEEDS_RETUNING;
2148 /* Reload the new initial value for timer */
2149 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2150 mod_timer(&host->tuning_timer, jiffies +
2151 host->tuning_count * HZ);
2152 }
2153
2154 /*
2155 * In case tuning fails, host controllers which support re-tuning can
2156 * try tuning again at a later time, when the re-tuning timer expires.
2157 * So for these controllers, we return 0. Since there might be other
2158 * controllers who do not have this capability, we return error for
2159 * them.
2160 */
2161 if (err && host->tuning_count &&
2162 host->tuning_mode == SDHCI_TUNING_MODE_1)
2163 err = 0;
2164
Arindam Nathb513ea22011-05-05 12:19:04 +05302165 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2166 spin_unlock(&host->lock);
2167 enable_irq(host->irq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002168 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302169
2170 return err;
2171}
2172
Adrian Hunter50accb92011-10-03 15:33:34 +03002173static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302174{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302175 u16 ctrl;
2176 unsigned long flags;
2177
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302178 /* Host Controller v3.00 defines preset value registers */
2179 if (host->version < SDHCI_SPEC_300)
2180 return;
2181
Sahitya Tummalae6886bd2013-04-12 12:11:20 +05302182 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2183 return;
2184
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302185 spin_lock_irqsave(&host->lock, flags);
2186
2187 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2188
2189 /*
2190 * We only enable or disable Preset Value if they are not already
2191 * enabled or disabled respectively. Otherwise, we bail out.
2192 */
2193 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2194 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2195 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002196 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302197 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2198 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2199 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter50accb92011-10-03 15:33:34 +03002200 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302201 }
2202
2203 spin_unlock_irqrestore(&host->lock, flags);
2204}
2205
Adrian Hunter50accb92011-10-03 15:33:34 +03002206static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
2207{
2208 struct sdhci_host *host = mmc_priv(mmc);
2209
2210 sdhci_runtime_pm_get(host);
2211 sdhci_do_enable_preset_value(host, enable);
2212 sdhci_runtime_pm_put(host);
2213}
2214
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002215static int sdhci_stop_request(struct mmc_host *mmc)
2216{
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002217 struct sdhci_host *host = mmc_priv(mmc);
2218 unsigned long flags;
2219 struct mmc_data *data;
2220
2221 spin_lock_irqsave(&host->lock, flags);
2222 if (!host->mrq || !host->data)
2223 goto out;
2224
2225 data = host->data;
2226
2227 if (host->ops->disable_data_xfer)
2228 host->ops->disable_data_xfer(host);
2229
2230 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
2231
2232 if (host->flags & SDHCI_REQ_USE_DMA) {
2233 if (host->flags & SDHCI_USE_ADMA) {
2234 sdhci_adma_table_post(host, data);
2235 } else {
2236 if (!data->host_cookie)
2237 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2238 data->sg_len,
2239 (data->flags & MMC_DATA_READ) ?
2240 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2241 }
2242 }
2243 del_timer(&host->timer);
2244 host->mrq = NULL;
2245 host->cmd = NULL;
2246 host->data = NULL;
2247out:
2248 spin_unlock_irqrestore(&host->lock, flags);
2249 return 0;
2250}
2251
2252static unsigned int sdhci_get_xfer_remain(struct mmc_host *mmc)
2253{
2254 struct sdhci_host *host = mmc_priv(mmc);
2255 u32 present_state = 0;
2256
2257 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
2258
2259 return present_state & SDHCI_DOING_WRITE;
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002260}
2261
David Brownellab7aefd2006-11-12 17:55:30 -08002262static const struct mmc_host_ops sdhci_ops = {
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08002263 .pre_req = sdhci_pre_req,
2264 .post_req = sdhci_post_req,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265 .request = sdhci_request,
2266 .set_ios = sdhci_set_ios,
2267 .get_ro = sdhci_get_ro,
Adrian Hunter50accb92011-10-03 15:33:34 +03002268 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002269 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302270 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302271 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302272 .enable_preset_value = sdhci_enable_preset_value,
Sahitya Tummalab4e84042013-03-10 07:03:17 +05302273 .enable = sdhci_enable,
2274 .disable = sdhci_disable,
Konstantin Dorfman29b89ca2013-04-15 12:15:26 +03002275 .stop_request = sdhci_stop_request,
Konstantin Dorfmancceca8d2013-04-24 15:51:31 +03002276 .get_xfer_remain = sdhci_get_xfer_remain,
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05302277 .notify_load = sdhci_notify_load,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278};
2279
2280/*****************************************************************************\
2281 * *
2282 * Tasklets *
2283 * *
2284\*****************************************************************************/
2285
2286static void sdhci_tasklet_card(unsigned long param)
2287{
2288 struct sdhci_host *host;
2289 unsigned long flags;
2290
2291 host = (struct sdhci_host*)param;
2292
2293 spin_lock_irqsave(&host->lock, flags);
2294
Adrian Hunter50accb92011-10-03 15:33:34 +03002295 /* Check host->mrq first in case we are runtime suspended */
2296 if (host->mrq &&
2297 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302298 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002299 mmc_hostname(host->mmc));
Sahitya Tummalaca422112013-02-22 12:15:54 +05302300 pr_err("%s: Resetting controller.\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002301 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302
Adrian Hunter50accb92011-10-03 15:33:34 +03002303 sdhci_reset(host, SDHCI_RESET_CMD);
2304 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305
Adrian Hunter50accb92011-10-03 15:33:34 +03002306 host->mrq->cmd->error = -ENOMEDIUM;
2307 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002308 }
2309
2310 spin_unlock_irqrestore(&host->lock, flags);
2311
Pierre Ossman04cf5852008-08-18 22:18:14 +02002312 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313}
2314
2315static void sdhci_tasklet_finish(unsigned long param)
2316{
2317 struct sdhci_host *host;
2318 unsigned long flags;
2319 struct mmc_request *mrq;
2320
2321 host = (struct sdhci_host*)param;
2322
Adrian Hunter50accb92011-10-03 15:33:34 +03002323 spin_lock_irqsave(&host->lock, flags);
2324
Chris Ball0c9c99a2011-04-27 17:35:31 -04002325 /*
2326 * If this tasklet gets rescheduled while running, it will
2327 * be run again afterwards but without any active request.
2328 */
Adrian Hunter50accb92011-10-03 15:33:34 +03002329 if (!host->mrq) {
2330 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002331 return;
Adrian Hunter50accb92011-10-03 15:33:34 +03002332 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002333
2334 del_timer(&host->timer);
2335
2336 mrq = host->mrq;
2337
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338 /*
2339 * The controller needs a reset of internal state machines
2340 * upon error conditions.
2341 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002342 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002343 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002344 (mrq->data && (mrq->data->error ||
2345 (mrq->data->stop && mrq->data->stop->error))) ||
2346 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002347
2348 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002349 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002350 unsigned int clock;
2351
2352 /* This is to force an update */
2353 clock = host->clock;
2354 host->clock = 0;
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302355 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002356 sdhci_set_clock(host, clock);
Sahitya Tummala04c3a462013-01-11 11:30:45 +05302357 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman645289d2006-06-30 02:22:33 -07002358 }
2359
2360 /* Spec says we should do both at the same time, but Ricoh
2361 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362 sdhci_reset(host, SDHCI_RESET_CMD);
2363 sdhci_reset(host, SDHCI_RESET_DATA);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002364 } else {
2365 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2366 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367 }
2368
2369 host->mrq = NULL;
2370 host->cmd = NULL;
2371 host->data = NULL;
2372
Pierre Ossmanf9134312008-12-21 17:01:48 +01002373#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002374 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002375#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376
Pierre Ossman5f25a662006-10-04 02:15:39 -07002377 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002378 spin_unlock_irqrestore(&host->lock, flags);
2379
2380 mmc_request_done(host->mmc, mrq);
Adrian Hunter50accb92011-10-03 15:33:34 +03002381 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002382}
2383
2384static void sdhci_timeout_timer(unsigned long data)
2385{
2386 struct sdhci_host *host;
2387 unsigned long flags;
2388
2389 host = (struct sdhci_host*)data;
2390
2391 spin_lock_irqsave(&host->lock, flags);
2392
2393 if (host->mrq) {
Subhash Jadavani5b254e42013-06-27 18:16:02 +05302394 if (!host->mrq->cmd->ignore_timeout) {
2395 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2396 mmc_hostname(host->mmc));
2397 sdhci_dumpregs(host);
2398 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002399
2400 if (host->data) {
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302401 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2402 mmc_hostname(host->mmc),
2403 (host->data->blksz * host->data->blocks),
2404 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2405 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002406 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 sdhci_finish_data(host);
2408 } else {
2409 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002410 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002412 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413
2414 tasklet_schedule(&host->finish_tasklet);
2415 }
2416 }
2417
Pierre Ossman5f25a662006-10-04 02:15:39 -07002418 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419 spin_unlock_irqrestore(&host->lock, flags);
2420}
2421
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302422static void sdhci_tuning_timer(unsigned long data)
2423{
2424 struct sdhci_host *host;
2425 unsigned long flags;
2426
2427 host = (struct sdhci_host *)data;
2428
2429 spin_lock_irqsave(&host->lock, flags);
2430
2431 host->flags |= SDHCI_NEEDS_RETUNING;
2432
2433 spin_unlock_irqrestore(&host->lock, flags);
2434}
2435
Pierre Ossmand129bce2006-03-24 03:18:17 -08002436/*****************************************************************************\
2437 * *
2438 * Interrupt handling *
2439 * *
2440\*****************************************************************************/
2441
2442static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2443{
Asutosh Das80c02552013-07-23 16:20:34 +05302444 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445 BUG_ON(intmask == 0);
2446
2447 if (!host->cmd) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302448 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002449 "though no command operation was in progress.\n",
2450 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002451 sdhci_dumpregs(host);
2452 return;
2453 }
2454
Pierre Ossman43b58b32007-07-25 23:15:27 +02002455 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002456 host->cmd->error = -ETIMEDOUT;
2457 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2458 SDHCI_INT_INDEX))
2459 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002460
Asutosh Das80c02552013-07-23 16:20:34 +05302461 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
2462 auto_cmd_status = sdhci_readw(host, SDHCI_AUTO_CMD_ERR);
2463 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2464 SDHCI_AUTO_CMD_INDEX_ERR |
2465 SDHCI_AUTO_CMD_ENDBIT_ERR))
2466 host->cmd->error = -EIO;
2467 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2468 host->cmd->error = -ETIMEDOUT;
2469 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2470 host->cmd->error = -EILSEQ;
2471 }
2472
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302473 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2474 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2475 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2476 if (intmask & SDHCI_INT_CRC) {
2477 sdhci_reset(host, SDHCI_RESET_CMD);
2478 host->cmd->error = 0;
2479 }
2480 }
2481 }
2482
Pierre Ossmane8095172008-07-25 01:09:08 +02002483 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002484 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002485 return;
2486 }
2487
2488 /*
2489 * The host can send and interrupt when the busy state has
2490 * ended, allowing us to wait without wasting CPU cycles.
2491 * Unfortunately this is overloaded on the "data complete"
2492 * interrupt, so we need to take some care when handling
2493 * it.
2494 *
2495 * Note: The 1.0 specification is a bit ambiguous about this
2496 * feature so there might be some problems with older
2497 * controllers.
2498 */
2499 if (host->cmd->flags & MMC_RSP_BUSY) {
2500 if (host->cmd->data)
2501 DBG("Cannot wait for busy signal when also "
2502 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002503 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002504 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002505
2506 /* The controller does not support the end-of-busy IRQ,
2507 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002508 }
2509
Sahitya Tummalad6a74b02013-02-25 15:50:08 +05302510 if (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING) {
2511 if ((host->cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
2512 (host->cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
2513 if (intmask & SDHCI_INT_CRC) {
2514 sdhci_finish_command(host);
2515 return;
2516 }
2517 }
2518 }
2519
Pierre Ossmane8095172008-07-25 01:09:08 +02002520 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002521 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002522}
2523
Ben Dooks6882a8c2009-06-14 13:52:38 +01002524static void sdhci_show_adma_error(struct sdhci_host *host)
2525{
2526 const char *name = mmc_hostname(host->mmc);
2527 u8 *desc = host->adma_desc;
2528 __le32 *dma;
2529 __le16 *len;
2530 u8 attr;
2531
2532 sdhci_dumpregs(host);
2533
2534 while (true) {
2535 dma = (__le32 *)(desc + 4);
2536 len = (__le16 *)(desc + 2);
2537 attr = *desc;
2538
Sahitya Tummala419b6c82013-04-12 12:28:29 +05302539 pr_info("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
Ben Dooks6882a8c2009-06-14 13:52:38 +01002540 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2541
2542 desc += 8;
2543
2544 if (attr & 2)
2545 break;
2546 }
2547}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002548
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2550{
Girish K S2cd06dc2012-01-06 09:56:39 +05302551 u32 command;
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302552 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002553 BUG_ON(intmask == 0);
2554
Arindam Nathb513ea22011-05-05 12:19:04 +05302555 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2556 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S2cd06dc2012-01-06 09:56:39 +05302557 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2558 if (command == MMC_SEND_TUNING_BLOCK ||
2559 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302560 host->tuning_done = 1;
2561 wake_up(&host->buf_ready_int);
2562 return;
2563 }
2564 }
2565
Pierre Ossmand129bce2006-03-24 03:18:17 -08002566 if (!host->data) {
2567 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002568 * The "data complete" interrupt is also used to
2569 * indicate that a busy state has ended. See comment
2570 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002572 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2573 if (intmask & SDHCI_INT_DATA_END) {
2574 sdhci_finish_command(host);
2575 return;
2576 }
Sahitya Tummalad2ae8832013-04-12 11:49:11 +05302577 if (host->quirks2 &
2578 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2579 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002580 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581
Sahitya Tummalaca422112013-02-22 12:15:54 +05302582 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002583 "though no data operation was in progress.\n",
2584 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002585 sdhci_dumpregs(host);
2586
2587 return;
2588 }
2589
2590 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002591 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002592 else if (intmask & SDHCI_INT_DATA_END_BIT)
2593 host->data->error = -EILSEQ;
2594 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2595 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2596 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002597 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002598 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302599 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002600 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002601 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002602 }
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302603 if (host->data->error) {
2604 if ((intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) &&
2605 (host->quirks2 & SDHCI_QUIRK2_IGNORE_CMDCRC_FOR_TUNING)) {
2606 command = SDHCI_GET_CMD(sdhci_readw(host,
2607 SDHCI_COMMAND));
2608 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
2609 (command != MMC_SEND_TUNING_BLOCK))
2610 pr_msg = true;
2611 } else {
2612 pr_msg = true;
2613 }
2614 if (pr_msg) {
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302615 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302616 mmc_hostname(host->mmc), intmask,
Sahitya Tummala48b458e2013-04-08 12:53:44 +05302617 host->data->error, ktime_to_ms(ktime_sub(
2618 ktime_get(), host->data_start_time)));
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302619 sdhci_dumpregs(host);
2620 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002621 sdhci_finish_data(host);
Asutosh Das5fa7d3b2013-03-20 22:53:40 +05302622 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002623 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002624 sdhci_transfer_pio(host);
2625
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002626 /*
2627 * We currently don't do anything fancy with DMA
2628 * boundaries, but as we can't disable the feature
2629 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002630 *
2631 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2632 * should return a valid address to continue from, but as
2633 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002634 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002635 if (intmask & SDHCI_INT_DMA_END) {
2636 u32 dmastart, dmanow;
2637 dmastart = sg_dma_address(host->data->sg);
2638 dmanow = dmastart + host->data->bytes_xfered;
2639 /*
2640 * Force update to the next DMA block boundary.
2641 */
2642 dmanow = (dmanow &
2643 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2644 SDHCI_DEFAULT_BOUNDARY_SIZE;
2645 host->data->bytes_xfered = dmanow - dmastart;
2646 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2647 " next 0x%08x\n",
2648 mmc_hostname(host->mmc), dmastart,
2649 host->data->bytes_xfered, dmanow);
2650 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2651 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002652
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002653 if (intmask & SDHCI_INT_DATA_END) {
2654 if (host->cmd) {
2655 /*
2656 * Data managed to finish before the
2657 * command completed. Make sure we do
2658 * things in the proper order.
2659 */
2660 host->data_early = 1;
2661 } else {
2662 sdhci_finish_data(host);
2663 }
2664 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665 }
2666}
2667
David Howells7d12e782006-10-05 14:55:46 +01002668static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002669{
2670 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002671 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002672 u32 intmask, unexpected = 0;
2673 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002674
2675 spin_lock(&host->lock);
2676
Adrian Hunter50accb92011-10-03 15:33:34 +03002677 if (host->runtime_suspended) {
2678 spin_unlock(&host->lock);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302679 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter50accb92011-10-03 15:33:34 +03002680 mmc_hostname(host->mmc));
2681 return IRQ_HANDLED;
2682 }
2683
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002684 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002685
Mark Lord62df67a2007-03-06 13:30:13 +01002686 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002687 result = IRQ_NONE;
2688 goto out;
2689 }
2690
Alexander Stein6379b232012-03-14 09:52:10 +01002691again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002692 DBG("*** %s got interrupt: 0x%08x\n",
2693 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002694
Pierre Ossman3192a282006-06-30 02:22:26 -07002695 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302696 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2697 SDHCI_CARD_PRESENT;
2698
2699 /*
2700 * There is a observation on i.mx esdhc. INSERT bit will be
2701 * immediately set again when it gets cleared, if a card is
2702 * inserted. We have to mask the irq to prevent interrupt
2703 * storm which will freeze the system. And the REMOVE gets
2704 * the same situation.
2705 *
2706 * More testing are needed here to ensure it works for other
2707 * platforms though.
2708 */
2709 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2710 SDHCI_INT_CARD_REMOVE);
2711 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2712 SDHCI_INT_CARD_INSERT);
2713
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002714 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Sahitya Tummalaca422112013-02-22 12:15:54 +05302715 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2716 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002717 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002718 }
2719
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002721 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2722 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002723 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2724 (host->clock <= 400000))
2725 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002726 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727 }
2728
2729 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002730 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2731 SDHCI_INT_STATUS);
Venkat Gopalakrishnane9beaa22012-09-17 16:00:15 -07002732 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
2733 (host->clock <= 400000))
2734 udelay(40);
Pierre Ossman3192a282006-06-30 02:22:26 -07002735 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002736 }
2737
2738 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2739
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002740 intmask &= ~SDHCI_INT_ERROR;
2741
Pierre Ossmand129bce2006-03-24 03:18:17 -08002742 if (intmask & SDHCI_INT_BUS_POWER) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05302743 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002744 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002745 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002746 }
2747
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002748 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002749
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002750 if (intmask & SDHCI_INT_CARD_INT)
2751 cardint = 1;
2752
2753 intmask &= ~SDHCI_INT_CARD_INT;
2754
Pierre Ossman3192a282006-06-30 02:22:26 -07002755 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002756 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002757 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002758 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759
2760 result = IRQ_HANDLED;
2761
Alexander Stein6379b232012-03-14 09:52:10 +01002762 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2763 if (intmask && --max_loops)
2764 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765out:
2766 spin_unlock(&host->lock);
2767
Alexander Stein6379b232012-03-14 09:52:10 +01002768 if (unexpected) {
2769 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2770 mmc_hostname(host->mmc), unexpected);
2771 sdhci_dumpregs(host);
2772 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002773 /*
2774 * We have to delay this as it calls back into the driver.
2775 */
2776 if (cardint)
2777 mmc_signal_sdio_irq(host->mmc);
2778
Pierre Ossmand129bce2006-03-24 03:18:17 -08002779 return result;
2780}
2781
2782/*****************************************************************************\
2783 * *
2784 * Suspend/resume *
2785 * *
2786\*****************************************************************************/
2787
2788#ifdef CONFIG_PM
2789
Manuel Laussd72faa62011-11-03 11:09:45 +01002790int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002791{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002792 int ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302793 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794
Chris Balla1b13b42012-02-06 00:43:59 -05002795 if (host->ops->platform_suspend)
2796 host->ops->platform_suspend(host);
2797
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002798 sdhci_disable_card_detection(host);
2799
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302800 /* Disable tuning since we are suspending */
Sahitya Tummalaca422112013-02-22 12:15:54 +05302801 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2802 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2803 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002804 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302805 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302806 }
2807
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002808 ret = mmc_suspend_host(host->mmc);
Sahitya Tummalaca422112013-02-22 12:15:54 +05302809 if (ret) {
2810 if (has_tuning_timer) {
2811 host->flags |= SDHCI_NEEDS_RETUNING;
2812 mod_timer(&host->tuning_timer, jiffies +
2813 host->tuning_count * HZ);
2814 }
2815
2816 sdhci_enable_card_detection(host);
2817
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002818 return ret;
Sahitya Tummalaca422112013-02-22 12:15:54 +05302819 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002821 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002822
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002823 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824}
2825
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002826EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002827
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002828int sdhci_resume_host(struct sdhci_host *host)
2829{
2830 int ret;
2831
Richard Röjforsa13abc72009-09-22 16:45:30 -07002832 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002833 if (host->ops->enable_dma)
2834 host->ops->enable_dma(host);
2835 }
2836
2837 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2838 mmc_hostname(host->mmc), host);
2839 if (ret)
2840 return ret;
2841
Adrian Hunter6308d292012-02-07 14:48:54 +02002842 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2843 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2844 /* Card keeps power but host controller does not */
2845 sdhci_init(host, 0);
2846 host->pwr = 0;
2847 host->clock = 0;
2848 sdhci_do_set_ios(host, &host->mmc->ios);
2849 } else {
2850 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2851 mmiowb();
2852 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853
2854 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002855 sdhci_enable_card_detection(host);
2856
Chris Balla1b13b42012-02-06 00:43:59 -05002857 if (host->ops->platform_resume)
2858 host->ops->platform_resume(host);
2859
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302860 /* Set the re-tuning expiration flag */
2861 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2862 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2863 host->flags |= SDHCI_NEEDS_RETUNING;
2864
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002865 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002866}
2867
2868EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869
Daniel Drake5f619702010-11-04 22:20:39 +00002870void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2871{
2872 u8 val;
2873 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2874 val |= SDHCI_WAKE_ON_INT;
2875 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2876}
2877
2878EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2879
Pierre Ossmand129bce2006-03-24 03:18:17 -08002880#endif /* CONFIG_PM */
2881
Adrian Hunter50accb92011-10-03 15:33:34 +03002882#ifdef CONFIG_PM_RUNTIME
2883
2884static int sdhci_runtime_pm_get(struct sdhci_host *host)
2885{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302886 if (!mmc_use_core_runtime_pm(host->mmc))
2887 return pm_runtime_get_sync(host->mmc->parent);
2888 else
2889 return 0;
Adrian Hunter50accb92011-10-03 15:33:34 +03002890}
2891
2892static int sdhci_runtime_pm_put(struct sdhci_host *host)
2893{
Asutosh Dasbbc84782013-02-11 15:31:35 +05302894 if (!mmc_use_core_runtime_pm(host->mmc)) {
2895 pm_runtime_mark_last_busy(host->mmc->parent);
2896 return pm_runtime_put_autosuspend(host->mmc->parent);
2897 } else {
2898 return 0;
2899 }
Adrian Hunter50accb92011-10-03 15:33:34 +03002900}
2901
2902int sdhci_runtime_suspend_host(struct sdhci_host *host)
2903{
2904 unsigned long flags;
2905 int ret = 0;
2906
2907 /* Disable tuning since we are suspending */
2908 if (host->version >= SDHCI_SPEC_300 &&
2909 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2910 del_timer_sync(&host->tuning_timer);
2911 host->flags &= ~SDHCI_NEEDS_RETUNING;
2912 }
2913
2914 spin_lock_irqsave(&host->lock, flags);
2915 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2916 spin_unlock_irqrestore(&host->lock, flags);
2917
2918 synchronize_irq(host->irq);
2919
2920 spin_lock_irqsave(&host->lock, flags);
2921 host->runtime_suspended = true;
2922 spin_unlock_irqrestore(&host->lock, flags);
2923
2924 return ret;
2925}
2926EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2927
2928int sdhci_runtime_resume_host(struct sdhci_host *host)
2929{
2930 unsigned long flags;
2931 int ret = 0, host_flags = host->flags;
2932
2933 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2934 if (host->ops->enable_dma)
2935 host->ops->enable_dma(host);
2936 }
2937
2938 sdhci_init(host, 0);
2939
2940 /* Force clock and power re-program */
2941 host->pwr = 0;
2942 host->clock = 0;
2943 sdhci_do_set_ios(host, &host->mmc->ios);
2944
2945 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2946 if (host_flags & SDHCI_PV_ENABLED)
2947 sdhci_do_enable_preset_value(host, true);
2948
2949 /* Set the re-tuning expiration flag */
2950 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2951 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2952 host->flags |= SDHCI_NEEDS_RETUNING;
2953
2954 spin_lock_irqsave(&host->lock, flags);
2955
2956 host->runtime_suspended = false;
2957
2958 /* Enable SDIO IRQ */
2959 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2960 sdhci_enable_sdio_irq_nolock(host, true);
2961
2962 /* Enable Card Detection */
2963 sdhci_enable_card_detection(host);
2964
2965 spin_unlock_irqrestore(&host->lock, flags);
2966
2967 return ret;
2968}
2969EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2970
2971#endif
2972
Pierre Ossmand129bce2006-03-24 03:18:17 -08002973/*****************************************************************************\
2974 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002975 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002976 * *
2977\*****************************************************************************/
2978
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002979struct sdhci_host *sdhci_alloc_host(struct device *dev,
2980 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002981{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982 struct mmc_host *mmc;
2983 struct sdhci_host *host;
2984
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002985 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002986
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002987 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002988 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002989 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990
2991 host = mmc_priv(mmc);
2992 host->mmc = mmc;
2993
Sahitya Tummala951c1202013-05-24 08:47:26 +05302994 spin_lock_init(&host->lock);
Sahitya Tummala40474e42013-07-10 14:40:37 +05302995 mutex_init(&host->ios_mutex);
Sahitya Tummala951c1202013-05-24 08:47:26 +05302996
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002997 return host;
2998}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002999
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003000EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003001
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003002int sdhci_add_host(struct sdhci_host *host)
3003{
3004 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303005 u32 caps[2];
3006 u32 max_current_caps;
3007 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003008 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003009
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010 WARN_ON(host == NULL);
3011 if (host == NULL)
3012 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003013
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003014 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003015
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003016 if (debug_quirks)
3017 host->quirks = debug_quirks;
Adrian Hunter50accb92011-10-03 15:33:34 +03003018 if (debug_quirks2)
3019 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003020
Pierre Ossmand96649e2006-06-30 02:22:30 -07003021 sdhci_reset(host, SDHCI_RESET_ALL);
3022
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003023 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02003024 host->version = (host->version & SDHCI_SPEC_VER_MASK)
3025 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08003026 if (host->version > SDHCI_SPEC_300) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303027 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003028 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02003029 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003030 }
3031
Arindam Nathf2119df2011-05-05 12:18:57 +05303032 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07003033 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003034
Arindam Nathf2119df2011-05-05 12:18:57 +05303035 caps[1] = (host->version >= SDHCI_SPEC_300) ?
3036 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
3037
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003038 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003039 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05303040 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003041 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003042 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003043 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003045 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003046 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003047 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003048 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003049 }
3050
Arindam Nathf2119df2011-05-05 12:18:57 +05303051 if ((host->version >= SDHCI_SPEC_200) &&
3052 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003053 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003054
3055 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3056 (host->flags & SDHCI_USE_ADMA)) {
3057 DBG("Disabling ADMA as it is marked broken\n");
3058 host->flags &= ~SDHCI_USE_ADMA;
3059 }
3060
Richard Röjforsa13abc72009-09-22 16:45:30 -07003061 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003062 if (host->ops->enable_dma) {
3063 if (host->ops->enable_dma(host)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303064 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003065 "available. Falling back to PIO.\n",
3066 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07003067 host->flags &=
3068 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003069 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003070 }
3071 }
3072
Pierre Ossman2134a922008-06-28 18:28:51 +02003073 if (host->flags & SDHCI_USE_ADMA) {
3074 /*
3075 * We need to allocate descriptors for all sg entries
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303076 * (128/max_segments) and potentially one alignment transfer for
Pierre Ossman2134a922008-06-28 18:28:51 +02003077 * each of those entries.
3078 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303079 if (host->ops->get_max_segments)
3080 host->adma_max_desc = host->ops->get_max_segments();
3081 else
3082 host->adma_max_desc = 128;
3083
3084 host->adma_desc_sz = (host->adma_max_desc * 2 + 1) * 4;
3085 host->align_buf_sz = host->adma_max_desc * 4;
3086
3087 pr_debug("%s: %s: dma_desc_size: %d\n",
3088 mmc_hostname(host->mmc), __func__, host->adma_desc_sz);
3089 host->adma_desc = kmalloc(host->adma_desc_sz,
3090 GFP_KERNEL);
3091 host->align_buffer = kmalloc(host->align_buf_sz,
3092 GFP_KERNEL);
Pierre Ossman2134a922008-06-28 18:28:51 +02003093 if (!host->adma_desc || !host->align_buffer) {
3094 kfree(host->adma_desc);
3095 kfree(host->align_buffer);
Sahitya Tummalaca422112013-02-22 12:15:54 +05303096 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02003097 "buffers. Falling back to standard DMA.\n",
3098 mmc_hostname(mmc));
3099 host->flags &= ~SDHCI_USE_ADMA;
3100 }
3101 }
3102
Shawn Guo6f9ad6f2011-04-17 00:48:36 +08003103 host->next_data.cookie = 1;
3104
Pierre Ossman76591502008-07-21 00:32:11 +02003105 /*
3106 * If we use DMA, then it's up to the caller to set the DMA
3107 * mask, but PIO does not need the hw shim so we set a new
3108 * mask here in that case.
3109 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003110 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003111 host->dma_mask = DMA_BIT_MASK(64);
3112 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
3113 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003115 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05303116 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003117 >> SDHCI_CLOCK_BASE_SHIFT;
3118 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303119 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003120 >> SDHCI_CLOCK_BASE_SHIFT;
3121
Pierre Ossmand129bce2006-03-24 03:18:17 -08003122 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003123 if (host->max_clk == 0 || host->quirks &
3124 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003125 if (!host->ops->get_max_clock) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303126 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03003127 "frequency.\n", mmc_hostname(mmc));
3128 return -ENODEV;
3129 }
3130 host->max_clk = host->ops->get_max_clock(host);
3131 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003132
3133 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303134 * In case of Host Controller v3.00, find out whether clock
3135 * multiplier is supported.
3136 */
3137 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3138 SDHCI_CLOCK_MUL_SHIFT;
3139
3140 /*
3141 * In case the value in Clock Multiplier is 0, then programmable
3142 * clock mode is not supported, otherwise the actual clock
3143 * multiplier is one more than the value of Clock Multiplier
3144 * in the Capabilities Register.
3145 */
3146 if (host->clk_mul)
3147 host->clk_mul += 1;
3148
3149 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003150 * Set host parameters.
3151 */
3152 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303153 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003154 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003155 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303156 else if (host->version >= SDHCI_SPEC_300) {
3157 if (host->clk_mul) {
3158 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
3159 mmc->f_max = host->max_clk * host->clk_mul;
3160 } else
3161 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3162 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003163 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003164
Sahitya Tummalaca422112013-02-22 12:15:54 +05303165 host->timeout_clk =
3166 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
3167 if (host->timeout_clk == 0) {
3168 if (host->ops->get_timeout_clock) {
3169 host->timeout_clk = host->ops->get_timeout_clock(host);
3170 } else if (!(host->quirks &
3171 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3172 pr_err("%s: Hardware doesn't specify timeout clock "
3173 "frequency.\n", mmc_hostname(mmc));
3174 return -ENODEV;
3175 }
3176 }
3177 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3178 host->timeout_clk *= 1000;
3179
3180 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
3181 host->timeout_clk = mmc->f_max / 1000;
3182
Krishna Kondaa20d3362013-04-01 21:01:59 -07003183 if (!(host->quirks2 & SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE))
3184 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Sahitya Tummalaca422112013-02-22 12:15:54 +05303185
Andrei Warkentine89d4562011-05-23 15:06:37 -05003186 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
3187
3188 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3189 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003190
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003191 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003192 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003193 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003194 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003195 host->flags |= SDHCI_AUTO_CMD23;
3196 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3197 } else {
3198 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3199 }
3200
Philip Rakity15ec4462010-11-19 16:48:39 -05003201 /*
3202 * A controller may support 8-bit width, but the board itself
3203 * might not have the pins brought out. Boards that support
3204 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3205 * their platform code before calling sdhci_add_host(), and we
3206 * won't assume 8-bit width for hosts without that CAP.
3207 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003208 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003209 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003210
Arindam Nathf2119df2011-05-05 12:18:57 +05303211 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003212 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003213
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003214 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
3215 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003216 mmc->caps |= MMC_CAP_NEEDS_POLL;
3217
Al Cooper4188bba2012-03-16 15:54:17 -04003218 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3219 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3220 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303221 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3222
3223 /* SDR104 supports also implies SDR50 support */
3224 if (caps[1] & SDHCI_SUPPORT_SDR104)
3225 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
3226 else if (caps[1] & SDHCI_SUPPORT_SDR50)
3227 mmc->caps |= MMC_CAP_UHS_SDR50;
3228
3229 if (caps[1] & SDHCI_SUPPORT_DDR50)
3230 mmc->caps |= MMC_CAP_UHS_DDR50;
3231
Girish K S2cd06dc2012-01-06 09:56:39 +05303232 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303233 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3234 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3235
Girish K S2cd06dc2012-01-06 09:56:39 +05303236 /* Does the host need tuning for HS200? */
3237 if (mmc->caps2 & MMC_CAP2_HS200)
3238 host->flags |= SDHCI_HS200_NEEDS_TUNING;
3239
Arindam Nathd6d50a12011-05-05 12:18:59 +05303240 /* Driver Type(s) (A, C, D) supported by the host */
3241 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3242 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3243 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3244 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3245 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3246 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3247
Tatyana Brokhman8b458cf2012-10-16 08:26:18 +02003248 /* Initial value for re-tuning timer count */
3249 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3250 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3251
3252 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303253 * In case Re-tuning Timer is not disabled, the actual value of
3254 * re-tuning timer will be 2 ^ (n - 1).
3255 */
3256 if (host->tuning_count)
3257 host->tuning_count = 1 << (host->tuning_count - 1);
3258
3259 /* Re-tuning mode supported by the Host Controller */
3260 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3261 SDHCI_RETUNING_MODE_SHIFT;
3262
Takashi Iwai8f230f42010-12-08 10:04:30 +01003263 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05303264 /*
3265 * According to SD Host Controller spec v3.00, if the Host System
3266 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3267 * the value is meaningful only if Voltage Support in the Capabilities
3268 * register is set. The actual current value is 4 times the register
3269 * value.
3270 */
3271 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
3272
3273 if (caps[0] & SDHCI_CAN_VDD_330) {
3274 int max_current_330;
3275
Takashi Iwai8f230f42010-12-08 10:04:30 +01003276 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303277
3278 max_current_330 = ((max_current_caps &
3279 SDHCI_MAX_CURRENT_330_MASK) >>
3280 SDHCI_MAX_CURRENT_330_SHIFT) *
3281 SDHCI_MAX_CURRENT_MULTIPLIER;
3282
3283 if (max_current_330 > 150)
3284 mmc->caps |= MMC_CAP_SET_XPC_330;
3285 }
3286 if (caps[0] & SDHCI_CAN_VDD_300) {
3287 int max_current_300;
3288
Takashi Iwai8f230f42010-12-08 10:04:30 +01003289 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303290
3291 max_current_300 = ((max_current_caps &
3292 SDHCI_MAX_CURRENT_300_MASK) >>
3293 SDHCI_MAX_CURRENT_300_SHIFT) *
3294 SDHCI_MAX_CURRENT_MULTIPLIER;
3295
3296 if (max_current_300 > 150)
3297 mmc->caps |= MMC_CAP_SET_XPC_300;
3298 }
3299 if (caps[0] & SDHCI_CAN_VDD_180) {
3300 int max_current_180;
3301
Takashi Iwai8f230f42010-12-08 10:04:30 +01003302 ocr_avail |= MMC_VDD_165_195;
3303
Arindam Nathf2119df2011-05-05 12:18:57 +05303304 max_current_180 = ((max_current_caps &
3305 SDHCI_MAX_CURRENT_180_MASK) >>
3306 SDHCI_MAX_CURRENT_180_SHIFT) *
3307 SDHCI_MAX_CURRENT_MULTIPLIER;
3308
3309 if (max_current_180 > 150)
3310 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05303311
3312 /* Maximum current capabilities of the host at 1.8V */
3313 if (max_current_180 >= 800)
3314 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
3315 else if (max_current_180 >= 600)
3316 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
3317 else if (max_current_180 >= 400)
3318 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
3319 else
3320 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05303321 }
3322
Takashi Iwai8f230f42010-12-08 10:04:30 +01003323 mmc->ocr_avail = ocr_avail;
3324 mmc->ocr_avail_sdio = ocr_avail;
3325 if (host->ocr_avail_sdio)
3326 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3327 mmc->ocr_avail_sd = ocr_avail;
3328 if (host->ocr_avail_sd)
3329 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3330 else /* normal SD controllers don't support 1.8V */
3331 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3332 mmc->ocr_avail_mmc = ocr_avail;
3333 if (host->ocr_avail_mmc)
3334 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003335
3336 if (mmc->ocr_avail == 0) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303337 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003338 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003339 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003340 }
3341
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003343 * Maximum number of segments. Depends on if the hardware
3344 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003346 if (host->flags & SDHCI_USE_ADMA)
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303347 mmc->max_segs = host->adma_max_desc;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003348 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003349 mmc->max_segs = 1;
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303350 else/* PIO */
3351 mmc->max_segs = host->adma_max_desc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352
3353 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003354 * Maximum number of sectors in one transfer. Limited by DMA boundary
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303355 * size (512KiB), unless specified by platform specific driver. Each
3356 * descriptor can transfer a maximum of 64KB.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003357 */
Asutosh Dasc8e8e562013-01-10 21:05:49 +05303358 if (host->ops->get_max_segments)
3359 mmc->max_req_size = (host->adma_max_desc * 65536);
3360 else
3361 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003362
3363 /*
3364 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003365 * of bytes. When doing hardware scatter/gather, each entry cannot
3366 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003367 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003368 if (host->flags & SDHCI_USE_ADMA) {
3369 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3370 mmc->max_seg_size = 65535;
3371 else
3372 mmc->max_seg_size = 65536;
3373 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003374 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003375 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376
3377 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003378 * Maximum block size. This varies from controller to controller and
3379 * is specified in the capabilities register.
3380 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003381 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3382 mmc->max_blk_size = 2;
3383 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303384 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003385 SDHCI_MAX_BLOCK_SHIFT;
3386 if (mmc->max_blk_size >= 3) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303387 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003388 "assuming 512 bytes\n", mmc_hostname(mmc));
3389 mmc->max_blk_size = 0;
3390 }
3391 }
3392
3393 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003394
3395 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003396 * Maximum block count.
3397 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003398 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003399
3400 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401 * Init tasklets.
3402 */
3403 tasklet_init(&host->card_tasklet,
3404 sdhci_tasklet_card, (unsigned long)host);
3405 tasklet_init(&host->finish_tasklet,
3406 sdhci_tasklet_finish, (unsigned long)host);
3407
Al Viroe4cad1b2006-10-10 22:47:07 +01003408 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303410 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303411 init_waitqueue_head(&host->buf_ready_int);
3412
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303413 /* Initialize re-tuning timer */
3414 init_timer(&host->tuning_timer);
3415 host->tuning_timer.data = (unsigned long)host;
3416 host->tuning_timer.function = sdhci_tuning_timer;
3417 }
3418
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003419 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003420 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003421 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003422 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003423
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003424 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3425 if (IS_ERR(host->vmmc)) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303426 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003427 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003428 }
3429
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003430 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431
3432#ifdef CONFIG_MMC_DEBUG
3433 sdhci_dumpregs(host);
3434#endif
3435
Pierre Ossmanf9134312008-12-21 17:01:48 +01003436#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003437 snprintf(host->led_name, sizeof(host->led_name),
3438 "%s::", mmc_hostname(mmc));
3439 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003440 host->led.brightness = LED_OFF;
3441 host->led.default_trigger = mmc_hostname(mmc);
3442 host->led.brightness_set = sdhci_led_control;
3443
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003444 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003445 if (ret)
3446 goto reset;
3447#endif
3448
Pierre Ossman5f25a662006-10-04 02:15:39 -07003449 mmiowb();
3450
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303451 if (host->cpu_dma_latency_us) {
3452 host->pm_qos_timeout_us = 10000; /* default value */
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303453 pm_qos_add_request(&host->pm_qos_req_dma,
3454 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303455
3456 host->pm_qos_tout.show = show_sdhci_pm_qos_tout;
3457 host->pm_qos_tout.store = store_sdhci_pm_qos_tout;
3458 sysfs_attr_init(&host->pm_qos_tout.attr);
3459 host->pm_qos_tout.attr.name = "pm_qos_unvote_delay";
3460 host->pm_qos_tout.attr.mode = S_IRUGO | S_IWUSR;
3461 ret = device_create_file(mmc_dev(mmc), &host->pm_qos_tout);
3462 if (ret)
3463 pr_err("%s: cannot create pm_qos_unvote_delay %d\n",
3464 mmc_hostname(mmc), ret);
Sujit Reddy Thummadeb1ada2013-06-19 20:15:37 +05303465 }
Sujit Reddy Thumma693af8d2013-06-19 20:25:38 +05303466
Pierre Ossmand129bce2006-03-24 03:18:17 -08003467 mmc_add_host(mmc);
3468
Sahitya Tummalaca422112013-02-22 12:15:54 +05303469 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003470 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003471 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3472 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003473
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003474 sdhci_enable_card_detection(host);
3475
Pierre Ossmand129bce2006-03-24 03:18:17 -08003476 return 0;
3477
Pierre Ossmanf9134312008-12-21 17:01:48 +01003478#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003479reset:
3480 sdhci_reset(host, SDHCI_RESET_ALL);
3481 free_irq(host->irq, host);
3482#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003483untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003484 tasklet_kill(&host->card_tasklet);
3485 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003486
3487 return ret;
3488}
3489
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003490EXPORT_SYMBOL_GPL(sdhci_add_host);
3491
Pierre Ossman1e728592008-04-16 19:13:13 +02003492void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003493{
Pierre Ossman1e728592008-04-16 19:13:13 +02003494 unsigned long flags;
3495
3496 if (dead) {
3497 spin_lock_irqsave(&host->lock, flags);
3498
3499 host->flags |= SDHCI_DEVICE_DEAD;
3500
3501 if (host->mrq) {
Sahitya Tummalaca422112013-02-22 12:15:54 +05303502 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003503 " transfer!\n", mmc_hostname(host->mmc));
3504
3505 host->mrq->cmd->error = -ENOMEDIUM;
3506 tasklet_schedule(&host->finish_tasklet);
3507 }
3508
3509 spin_unlock_irqrestore(&host->lock, flags);
3510 }
3511
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003512 sdhci_disable_card_detection(host);
3513
Sahitya Tummalab4e84042013-03-10 07:03:17 +05303514 if (host->cpu_dma_latency_us)
3515 pm_qos_remove_request(&host->pm_qos_req_dma);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003516 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003517
Pierre Ossmanf9134312008-12-21 17:01:48 +01003518#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003519 led_classdev_unregister(&host->led);
3520#endif
3521
Pierre Ossman1e728592008-04-16 19:13:13 +02003522 if (!dead)
3523 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003524
3525 free_irq(host->irq, host);
3526
3527 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303528 if (host->version >= SDHCI_SPEC_300)
3529 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003530
3531 tasklet_kill(&host->card_tasklet);
3532 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003533
Sahitya Tummalaca422112013-02-22 12:15:54 +05303534 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003535 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003536
Pierre Ossman2134a922008-06-28 18:28:51 +02003537 kfree(host->adma_desc);
3538 kfree(host->align_buffer);
3539
3540 host->adma_desc = NULL;
3541 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003542}
3543
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003544EXPORT_SYMBOL_GPL(sdhci_remove_host);
3545
3546void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003547{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003548 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003549}
3550
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003551EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003552
3553/*****************************************************************************\
3554 * *
3555 * Driver init/exit *
3556 * *
3557\*****************************************************************************/
3558
3559static int __init sdhci_drv_init(void)
3560{
Sahitya Tummalaca422112013-02-22 12:15:54 +05303561 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003562 ": Secure Digital Host Controller Interface driver\n");
Sahitya Tummalaca422112013-02-22 12:15:54 +05303563 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003564
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003565 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003566}
3567
3568static void __exit sdhci_drv_exit(void)
3569{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003570}
3571
3572module_init(sdhci_drv_init);
3573module_exit(sdhci_drv_exit);
3574
Pierre Ossmandf673b22006-06-30 02:22:31 -07003575module_param(debug_quirks, uint, 0444);
Adrian Hunter50accb92011-10-03 15:33:34 +03003576module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003577
Pierre Ossman32710e82009-04-08 20:14:54 +02003578MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003579MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003580MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003581
Pierre Ossmandf673b22006-06-30 02:22:31 -07003582MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter50accb92011-10-03 15:33:34 +03003583MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");