blob: c7851c0aabce52226d118d50bbe95fe61b4094c2 [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 Hunter66fd8ad2011-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>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
52static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
53static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053055static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030057#ifdef CONFIG_PM_RUNTIME
58static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
60#else
61static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
62{
63 return 0;
64}
65static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
66{
67 return 0;
68}
69#endif
70
Pierre Ossmand129bce2006-03-24 03:18:17 -080071static void sdhci_dumpregs(struct sdhci_host *host)
72{
Girish K Sa3c76eb2011-10-11 11:44:09 +053073 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070074 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080075
Girish K Sa3c76eb2011-10-11 11:44:09 +053076 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readl(host, SDHCI_DMA_ADDRESS),
78 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053079 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readw(host, SDHCI_BLOCK_SIZE),
81 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053082 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readl(host, SDHCI_ARGUMENT),
84 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_PRESENT_STATE),
87 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readb(host, SDHCI_POWER_CONTROL),
90 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
93 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
96 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readl(host, SDHCI_INT_ENABLE),
99 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readw(host, SDHCI_ACMD12_ERR),
102 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500107 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530110 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800111
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100114 readl(host->ioaddr + SDHCI_ADMA_ERROR),
115 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
116
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800118}
119
120/*****************************************************************************\
121 * *
122 * Low level functions *
123 * *
124\*****************************************************************************/
125
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300126static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
127{
128 u32 ier;
129
130 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
131 ier &= ~clear;
132 ier |= set;
133 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
134 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
135}
136
137static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
138{
139 sdhci_clear_set_irqs(host, 0, irqs);
140}
141
142static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
143{
144 sdhci_clear_set_irqs(host, irqs, 0);
145}
146
147static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
148{
Shawn Guod25928d2011-06-21 22:41:48 +0800149 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300150
Adrian Hunterc79396c2011-12-27 15:48:42 +0200151 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100152 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300153 return;
154
Shawn Guod25928d2011-06-21 22:41:48 +0800155 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
156 SDHCI_CARD_PRESENT;
157 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
158
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300159 if (enable)
160 sdhci_unmask_irqs(host, irqs);
161 else
162 sdhci_mask_irqs(host, irqs);
163}
164
165static void sdhci_enable_card_detection(struct sdhci_host *host)
166{
167 sdhci_set_card_detection(host, true);
168}
169
170static void sdhci_disable_card_detection(struct sdhci_host *host)
171{
172 sdhci_set_card_detection(host, false);
173}
174
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175static void sdhci_reset(struct sdhci_host *host, u8 mask)
176{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700177 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300178 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100180 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700182 SDHCI_CARD_PRESENT))
183 return;
184 }
185
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300186 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
187 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
188
Philip Rakity393c1a32011-01-21 11:26:40 -0800189 if (host->ops->platform_reset_enter)
190 host->ops->platform_reset_enter(host, mask);
191
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300192 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800195 host->clock = 0;
196
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 /* Wait max 100 ms */
198 timeout = 100;
199
200 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300201 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700202 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530203 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700204 mmc_hostname(host->mmc), (int)mask);
205 sdhci_dumpregs(host);
206 return;
207 }
208 timeout--;
209 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300211
Philip Rakity393c1a32011-01-21 11:26:40 -0800212 if (host->ops->platform_reset_exit)
213 host->ops->platform_reset_exit(host, mask);
214
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300215 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
216 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800217
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
220 host->ops->enable_dma(host);
221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
225
226static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228 if (soft)
229 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
230 else
231 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300233 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
234 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700235 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
236 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300237 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800238
239 if (soft) {
240 /* force clock reconfiguration */
241 host->clock = 0;
242 sdhci_set_ios(host->mmc, &host->mmc->ios);
243 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300244}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300246static void sdhci_reinit(struct sdhci_host *host)
247{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800248 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800249 /*
250 * Retuning stuffs are affected by different cards inserted and only
251 * applicable to UHS-I cards. So reset these fields to their initial
252 * value when card is removed.
253 */
Aaron Lu973905f2012-07-04 13:29:09 +0800254 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
255 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
256
Aaron Lub67c6b42012-06-29 16:17:31 +0800257 del_timer_sync(&host->tuning_timer);
258 host->flags &= ~SDHCI_NEEDS_RETUNING;
259 host->mmc->max_blk_count =
260 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
261 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263}
264
265static void sdhci_activate_led(struct sdhci_host *host)
266{
267 u8 ctrl;
268
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300271 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272}
273
274static void sdhci_deactivate_led(struct sdhci_host *host)
275{
276 u8 ctrl;
277
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300278 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300280 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800281}
282
Pierre Ossmanf9134312008-12-21 17:01:48 +0100283#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284static void sdhci_led_control(struct led_classdev *led,
285 enum led_brightness brightness)
286{
287 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
288 unsigned long flags;
289
290 spin_lock_irqsave(&host->lock, flags);
291
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300292 if (host->runtime_suspended)
293 goto out;
294
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100295 if (brightness == LED_OFF)
296 sdhci_deactivate_led(host);
297 else
298 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300299out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100300 spin_unlock_irqrestore(&host->lock, flags);
301}
302#endif
303
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304/*****************************************************************************\
305 * *
306 * Core functions *
307 * *
308\*****************************************************************************/
309
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311{
Pierre Ossman76591502008-07-21 00:32:11 +0200312 unsigned long flags;
313 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700314 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200315 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200320 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200325 if (!sg_miter_next(&host->sg_miter))
326 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800329
Pierre Ossman76591502008-07-21 00:32:11 +0200330 blksize -= len;
331 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800334
Pierre Ossman76591502008-07-21 00:32:11 +0200335 while (len) {
336 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300337 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200338 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800339 }
Pierre Ossman76591502008-07-21 00:32:11 +0200340
341 *buf = scratch & 0xFF;
342
343 buf++;
344 scratch >>= 8;
345 chunk--;
346 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347 }
348 }
Pierre Ossman76591502008-07-21 00:32:11 +0200349
350 sg_miter_stop(&host->sg_miter);
351
352 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355static void sdhci_write_block_pio(struct sdhci_host *host)
356{
Pierre Ossman76591502008-07-21 00:32:11 +0200357 unsigned long flags;
358 size_t blksize, len, chunk;
359 u32 scratch;
360 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
362 DBG("PIO writing\n");
363
364 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200365 chunk = 0;
366 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
370 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200371 if (!sg_miter_next(&host->sg_miter))
372 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200375
Pierre Ossman76591502008-07-21 00:32:11 +0200376 blksize -= len;
377 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380
Pierre Ossman76591502008-07-21 00:32:11 +0200381 while (len) {
382 scratch |= (u32)*buf << (chunk * 8);
383
384 buf++;
385 chunk++;
386 len--;
387
388 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300389 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200390 chunk = 0;
391 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393 }
394 }
Pierre Ossman76591502008-07-21 00:32:11 +0200395
396 sg_miter_stop(&host->sg_miter);
397
398 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399}
400
401static void sdhci_transfer_pio(struct sdhci_host *host)
402{
403 u32 mask;
404
405 BUG_ON(!host->data);
406
Pierre Ossman76591502008-07-21 00:32:11 +0200407 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408 return;
409
410 if (host->data->flags & MMC_DATA_READ)
411 mask = SDHCI_DATA_AVAILABLE;
412 else
413 mask = SDHCI_SPACE_AVAILABLE;
414
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200415 /*
416 * Some controllers (JMicron JMB38x) mess up the buffer bits
417 * for transfers < 4 bytes. As long as it is just one block,
418 * we can ignore the bits.
419 */
420 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
421 (host->data->blocks == 1))
422 mask = ~0;
423
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300424 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300425 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
426 udelay(100);
427
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428 if (host->data->flags & MMC_DATA_READ)
429 sdhci_read_block_pio(host);
430 else
431 sdhci_write_block_pio(host);
432
Pierre Ossman76591502008-07-21 00:32:11 +0200433 host->blocks--;
434 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100436 }
437
438 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800439}
440
Pierre Ossman2134a922008-06-28 18:28:51 +0200441static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
442{
443 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800444 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200445}
446
447static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
448{
Cong Wang482fce92011-11-27 13:27:00 +0800449 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200450 local_irq_restore(*flags);
451}
452
Ben Dooks118cd172010-03-05 13:43:26 -0800453static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
454{
Ben Dooks9e506f32010-03-05 13:43:29 -0800455 __le32 *dataddr = (__le32 __force *)(desc + 4);
456 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800457
Ben Dooks9e506f32010-03-05 13:43:29 -0800458 /* SDHCI specification says ADMA descriptors should be 4 byte
459 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Ben Dooks9e506f32010-03-05 13:43:29 -0800461 cmdlen[0] = cpu_to_le16(cmd);
462 cmdlen[1] = cpu_to_le16(len);
463
464 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800465}
466
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200467static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200468 struct mmc_data *data)
469{
470 int direction;
471
472 u8 *desc;
473 u8 *align;
474 dma_addr_t addr;
475 dma_addr_t align_addr;
476 int len, offset;
477
478 struct scatterlist *sg;
479 int i;
480 char *buffer;
481 unsigned long flags;
482
483 /*
484 * The spec does not specify endianness of descriptor table.
485 * We currently guess that it is LE.
486 */
487
488 if (data->flags & MMC_DATA_READ)
489 direction = DMA_FROM_DEVICE;
490 else
491 direction = DMA_TO_DEVICE;
492
493 /*
494 * The ADMA descriptor table is mapped further down as we
495 * need to fill it with data first.
496 */
497
498 host->align_addr = dma_map_single(mmc_dev(host->mmc),
499 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700500 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200501 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200502 BUG_ON(host->align_addr & 0x3);
503
504 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
505 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200506 if (host->sg_count == 0)
507 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200508
509 desc = host->adma_desc;
510 align = host->align_buffer;
511
512 align_addr = host->align_addr;
513
514 for_each_sg(data->sg, sg, host->sg_count, i) {
515 addr = sg_dma_address(sg);
516 len = sg_dma_len(sg);
517
518 /*
519 * The SDHCI specification states that ADMA
520 * addresses must be 32-bit aligned. If they
521 * aren't, then we use a bounce buffer for
522 * the (up to three) bytes that screw up the
523 * alignment.
524 */
525 offset = (4 - (addr & 0x3)) & 0x3;
526 if (offset) {
527 if (data->flags & MMC_DATA_WRITE) {
528 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200529 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
532 }
533
Ben Dooks118cd172010-03-05 13:43:26 -0800534 /* tran, valid */
535 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 BUG_ON(offset > 65536);
538
Pierre Ossman2134a922008-06-28 18:28:51 +0200539 align += 4;
540 align_addr += 4;
541
542 desc += 8;
543
544 addr += offset;
545 len -= offset;
546 }
547
Pierre Ossman2134a922008-06-28 18:28:51 +0200548 BUG_ON(len > 65536);
549
Ben Dooks118cd172010-03-05 13:43:26 -0800550 /* tran, valid */
551 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 desc += 8;
553
554 /*
555 * If this triggers then we have a calculation bug
556 * somewhere. :/
557 */
558 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
559 }
560
Thomas Abraham70764a92010-05-26 14:42:04 -0700561 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
562 /*
563 * Mark the last descriptor as the terminating descriptor
564 */
565 if (desc != host->adma_desc) {
566 desc -= 8;
567 desc[0] |= 0x2; /* end */
568 }
569 } else {
570 /*
571 * Add a terminating entry.
572 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
Thomas Abraham70764a92010-05-26 14:42:04 -0700574 /* nop, end, valid */
575 sdhci_set_adma_desc(desc, 0, 0, 0x3);
576 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 /*
579 * Resync align buffer as we might have changed it.
580 */
581 if (data->flags & MMC_DATA_WRITE) {
582 dma_sync_single_for_device(mmc_dev(host->mmc),
583 host->align_addr, 128 * 4, direction);
584 }
585
586 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
587 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200588 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200589 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200590 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200591
592 return 0;
593
594unmap_entries:
595 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
596 data->sg_len, direction);
597unmap_align:
598 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
599 128 * 4, direction);
600fail:
601 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602}
603
604static void sdhci_adma_table_post(struct sdhci_host *host,
605 struct mmc_data *data)
606{
607 int direction;
608
609 struct scatterlist *sg;
610 int i, size;
611 u8 *align;
612 char *buffer;
613 unsigned long flags;
614
615 if (data->flags & MMC_DATA_READ)
616 direction = DMA_FROM_DEVICE;
617 else
618 direction = DMA_TO_DEVICE;
619
620 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
621 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
622
623 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
624 128 * 4, direction);
625
626 if (data->flags & MMC_DATA_READ) {
627 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
628 data->sg_len, direction);
629
630 align = host->align_buffer;
631
632 for_each_sg(data->sg, sg, host->sg_count, i) {
633 if (sg_dma_address(sg) & 0x3) {
634 size = 4 - (sg_dma_address(sg) & 0x3);
635
636 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200637 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200638 memcpy(buffer, align, size);
639 sdhci_kunmap_atomic(buffer, &flags);
640
641 align += 4;
642 }
643 }
644 }
645
646 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
647 data->sg_len, direction);
648}
649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800651{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500653 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800655
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200656 /*
657 * If the host controller provides us with an incorrect timeout
658 * value, just skip the check and use 0xE. The hardware may take
659 * longer to time out, but that's much better than having a too-short
660 * timeout value.
661 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200662 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200663 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200664
Andrei Warkentina3c77782011-04-11 16:13:42 -0500665 /* Unspecified timeout, assume max */
666 if (!data && !cmd->cmd_timeout_ms)
667 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 /* timeout in us */
670 if (!data)
671 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300672 else {
673 target_timeout = data->timeout_ns / 1000;
674 if (host->clock)
675 target_timeout += data->timeout_clks / host->clock;
676 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700677
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700678 /*
679 * Figure out needed cycles.
680 * We do this in steps in order to fit inside a 32 bit int.
681 * The first step is the minimum timeout, which will have a
682 * minimum resolution of 6 bits:
683 * (1) 2^13*1000 > 2^22,
684 * (2) host->timeout_clk < 2^16
685 * =>
686 * (1) / (2) > 2^6
687 */
688 count = 0;
689 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
690 while (current_timeout < target_timeout) {
691 count++;
692 current_timeout <<= 1;
693 if (count >= 0xF)
694 break;
695 }
696
697 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400698 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
699 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700700 count = 0xE;
701 }
702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 return count;
704}
705
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300706static void sdhci_set_transfer_irqs(struct sdhci_host *host)
707{
708 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
709 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
710
711 if (host->flags & SDHCI_REQ_USE_DMA)
712 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
713 else
714 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
715}
716
Andrei Warkentina3c77782011-04-11 16:13:42 -0500717static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718{
719 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200720 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500721 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200722 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200723
724 WARN_ON(host->data);
725
Andrei Warkentina3c77782011-04-11 16:13:42 -0500726 if (data || (cmd->flags & MMC_RSP_BUSY)) {
727 count = sdhci_calc_timeout(host, cmd);
728 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
729 }
730
731 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200732 return;
733
734 /* Sanity checks */
735 BUG_ON(data->blksz * data->blocks > 524288);
736 BUG_ON(data->blksz > host->mmc->max_blk_size);
737 BUG_ON(data->blocks > 65535);
738
739 host->data = data;
740 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400741 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200742
Richard Röjforsa13abc72009-09-22 16:45:30 -0700743 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100744 host->flags |= SDHCI_REQ_USE_DMA;
745
Pierre Ossman2134a922008-06-28 18:28:51 +0200746 /*
747 * FIXME: This doesn't account for merging when mapping the
748 * scatterlist.
749 */
750 if (host->flags & SDHCI_REQ_USE_DMA) {
751 int broken, i;
752 struct scatterlist *sg;
753
754 broken = 0;
755 if (host->flags & SDHCI_USE_ADMA) {
756 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
757 broken = 1;
758 } else {
759 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
760 broken = 1;
761 }
762
763 if (unlikely(broken)) {
764 for_each_sg(data->sg, sg, data->sg_len, i) {
765 if (sg->length & 0x3) {
766 DBG("Reverting to PIO because of "
767 "transfer size (%d)\n",
768 sg->length);
769 host->flags &= ~SDHCI_REQ_USE_DMA;
770 break;
771 }
772 }
773 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100774 }
775
776 /*
777 * The assumption here being that alignment is the same after
778 * translation to device address space.
779 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200780 if (host->flags & SDHCI_REQ_USE_DMA) {
781 int broken, i;
782 struct scatterlist *sg;
783
784 broken = 0;
785 if (host->flags & SDHCI_USE_ADMA) {
786 /*
787 * As we use 3 byte chunks to work around
788 * alignment problems, we need to check this
789 * quirk.
790 */
791 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
792 broken = 1;
793 } else {
794 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
795 broken = 1;
796 }
797
798 if (unlikely(broken)) {
799 for_each_sg(data->sg, sg, data->sg_len, i) {
800 if (sg->offset & 0x3) {
801 DBG("Reverting to PIO because of "
802 "bad alignment\n");
803 host->flags &= ~SDHCI_REQ_USE_DMA;
804 break;
805 }
806 }
807 }
808 }
809
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200810 if (host->flags & SDHCI_REQ_USE_DMA) {
811 if (host->flags & SDHCI_USE_ADMA) {
812 ret = sdhci_adma_table_pre(host, data);
813 if (ret) {
814 /*
815 * This only happens when someone fed
816 * us an invalid request.
817 */
818 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200819 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200820 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300821 sdhci_writel(host, host->adma_addr,
822 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200823 }
824 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300825 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200826
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300827 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200828 data->sg, data->sg_len,
829 (data->flags & MMC_DATA_READ) ?
830 DMA_FROM_DEVICE :
831 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300832 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200833 /*
834 * This only happens when someone fed
835 * us an invalid request.
836 */
837 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200838 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200839 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200840 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300841 sdhci_writel(host, sg_dma_address(data->sg),
842 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200843 }
844 }
845 }
846
Pierre Ossman2134a922008-06-28 18:28:51 +0200847 /*
848 * Always adjust the DMA selection as some controllers
849 * (e.g. JMicron) can't do PIO properly when the selection
850 * is ADMA.
851 */
852 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300853 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200854 ctrl &= ~SDHCI_CTRL_DMA_MASK;
855 if ((host->flags & SDHCI_REQ_USE_DMA) &&
856 (host->flags & SDHCI_USE_ADMA))
857 ctrl |= SDHCI_CTRL_ADMA32;
858 else
859 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100861 }
862
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200863 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200864 int flags;
865
866 flags = SG_MITER_ATOMIC;
867 if (host->data->flags & MMC_DATA_READ)
868 flags |= SG_MITER_TO_SG;
869 else
870 flags |= SG_MITER_FROM_SG;
871 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200872 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800873 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300875 sdhci_set_transfer_irqs(host);
876
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400877 /* Set the DMA boundary value and block size */
878 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
879 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300880 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881}
882
883static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500884 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700885{
886 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500887 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700888
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 if (data == NULL)
890 return;
891
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200892 WARN_ON(!host->data);
893
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700894 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500895 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
896 mode |= SDHCI_TRNS_MULTI;
897 /*
898 * If we are sending CMD23, CMD12 never gets sent
899 * on successful completion (so no Auto-CMD12).
900 */
901 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
902 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500903 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
904 mode |= SDHCI_TRNS_AUTO_CMD23;
905 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
906 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700907 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500908
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909 if (data->flags & MMC_DATA_READ)
910 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100911 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 mode |= SDHCI_TRNS_DMA;
913
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300914 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915}
916
917static void sdhci_finish_data(struct sdhci_host *host)
918{
919 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800920
921 BUG_ON(!host->data);
922
923 data = host->data;
924 host->data = NULL;
925
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100926 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200927 if (host->flags & SDHCI_USE_ADMA)
928 sdhci_adma_table_post(host, data);
929 else {
930 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
931 data->sg_len, (data->flags & MMC_DATA_READ) ?
932 DMA_FROM_DEVICE : DMA_TO_DEVICE);
933 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934 }
935
936 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200937 * The specification states that the block count register must
938 * be updated, but it does not specify at what point in the
939 * data flow. That makes the register entirely useless to read
940 * back so we have to assume that nothing made it to the card
941 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200943 if (data->error)
944 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200946 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Andrei Warkentine89d4562011-05-23 15:06:37 -0500948 /*
949 * Need to send CMD12 if -
950 * a) open-ended multiblock transfer (no CMD23)
951 * b) error in multiblock transfer
952 */
953 if (data->stop &&
954 (data->error ||
955 !host->mrq->sbc)) {
956
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 /*
958 * The controller needs a reset of internal state machines
959 * upon error conditions.
960 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200961 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 sdhci_reset(host, SDHCI_RESET_CMD);
963 sdhci_reset(host, SDHCI_RESET_DATA);
964 }
965
966 sdhci_send_command(host, data->stop);
967 } else
968 tasklet_schedule(&host->finish_tasklet);
969}
970
971static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
972{
973 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700974 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700975 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976
977 WARN_ON(host->cmd);
978
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700980 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700981
982 mask = SDHCI_CMD_INHIBIT;
983 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
984 mask |= SDHCI_DATA_INHIBIT;
985
986 /* We shouldn't wait for data inihibit for stop commands, even
987 though they might use busy signaling */
988 if (host->mrq->data && (cmd == host->mrq->data->stop))
989 mask &= ~SDHCI_DATA_INHIBIT;
990
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700992 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530993 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100994 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200996 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 tasklet_schedule(&host->finish_tasklet);
998 return;
999 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001000 timeout--;
1001 mdelay(1);
1002 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
1004 mod_timer(&host->timer, jiffies + 10 * HZ);
1005
1006 host->cmd = cmd;
1007
Andrei Warkentina3c77782011-04-11 16:13:42 -05001008 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001010 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011
Andrei Warkentine89d4562011-05-23 15:06:37 -05001012 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001013
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301015 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001017 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 tasklet_schedule(&host->finish_tasklet);
1019 return;
1020 }
1021
1022 if (!(cmd->flags & MMC_RSP_PRESENT))
1023 flags = SDHCI_CMD_RESP_NONE;
1024 else if (cmd->flags & MMC_RSP_136)
1025 flags = SDHCI_CMD_RESP_LONG;
1026 else if (cmd->flags & MMC_RSP_BUSY)
1027 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1028 else
1029 flags = SDHCI_CMD_RESP_SHORT;
1030
1031 if (cmd->flags & MMC_RSP_CRC)
1032 flags |= SDHCI_CMD_CRC;
1033 if (cmd->flags & MMC_RSP_OPCODE)
1034 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301035
1036 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301037 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1038 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 flags |= SDHCI_CMD_DATA;
1040
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001041 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042}
1043
1044static void sdhci_finish_command(struct sdhci_host *host)
1045{
1046 int i;
1047
1048 BUG_ON(host->cmd == NULL);
1049
1050 if (host->cmd->flags & MMC_RSP_PRESENT) {
1051 if (host->cmd->flags & MMC_RSP_136) {
1052 /* CRC is stripped so we need to do some shifting. */
1053 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055 SDHCI_RESPONSE + (3-i)*4) << 8;
1056 if (i != 3)
1057 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 SDHCI_RESPONSE + (3-i)*4-1);
1060 }
1061 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001062 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063 }
1064 }
1065
Pierre Ossman17b04292007-07-22 22:18:46 +02001066 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 /* Finished CMD23, now send actual command. */
1069 if (host->cmd == host->mrq->sbc) {
1070 host->cmd = NULL;
1071 sdhci_send_command(host, host->mrq->cmd);
1072 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001073
Andrei Warkentine89d4562011-05-23 15:06:37 -05001074 /* Processed actual command. */
1075 if (host->data && host->data_early)
1076 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
Andrei Warkentine89d4562011-05-23 15:06:37 -05001078 if (!host->cmd->data)
1079 tasklet_schedule(&host->finish_tasklet);
1080
1081 host->cmd = NULL;
1082 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083}
1084
1085static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1086{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301087 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001088 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301089 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001090 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
Todd Poynor30832ab2011-12-27 15:48:46 +02001092 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001093 return;
1094
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001095 host->mmc->actual_clock = 0;
1096
Anton Vorontsov81146342009-03-17 00:13:59 +03001097 if (host->ops->set_clock) {
1098 host->ops->set_clock(host, clock);
1099 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1100 return;
1101 }
1102
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001103 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104
1105 if (clock == 0)
1106 goto out;
1107
Zhangfei Gao85105c52010-08-06 07:10:01 +08001108 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301109 /*
1110 * Check if the Host Controller supports Programmable Clock
1111 * Mode.
1112 */
1113 if (host->clk_mul) {
1114 u16 ctrl;
1115
1116 /*
1117 * We need to figure out whether the Host Driver needs
1118 * to select Programmable Clock Mode, or the value can
1119 * be set automatically by the Host Controller based on
1120 * the Preset Value registers.
1121 */
1122 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1123 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1124 for (div = 1; div <= 1024; div++) {
1125 if (((host->max_clk * host->clk_mul) /
1126 div) <= clock)
1127 break;
1128 }
1129 /*
1130 * Set Programmable Clock Mode in the Clock
1131 * Control register.
1132 */
1133 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001134 real_div = div;
1135 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001137 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 } else {
1139 /* Version 3.00 divisors must be a multiple of 2. */
1140 if (host->max_clk <= clock)
1141 div = 1;
1142 else {
1143 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1144 div += 2) {
1145 if ((host->max_clk / div) <= clock)
1146 break;
1147 }
1148 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001149 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301150 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001151 }
1152 } else {
1153 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001154 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001155 if ((host->max_clk / div) <= clock)
1156 break;
1157 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001158 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001160 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001161
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001162 if (real_div)
1163 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1164
Arindam Nathc3ed3872011-05-05 12:19:06 +05301165 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001166 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1167 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001169 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
Chris Ball27f6cb12009-09-22 16:45:31 -07001171 /* Wait max 20 ms */
1172 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001173 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001174 & SDHCI_CLOCK_INT_STABLE)) {
1175 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301176 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001177 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 sdhci_dumpregs(host);
1179 return;
1180 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001181 timeout--;
1182 mdelay(1);
1183 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184
1185 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001186 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001187
1188out:
1189 host->clock = clock;
1190}
1191
Adrian Hunterceb61432011-12-27 15:48:41 +02001192static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001193{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001194 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001195
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001196 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001197 switch (1 << power) {
1198 case MMC_VDD_165_195:
1199 pwr = SDHCI_POWER_180;
1200 break;
1201 case MMC_VDD_29_30:
1202 case MMC_VDD_30_31:
1203 pwr = SDHCI_POWER_300;
1204 break;
1205 case MMC_VDD_32_33:
1206 case MMC_VDD_33_34:
1207 pwr = SDHCI_POWER_330;
1208 break;
1209 default:
1210 BUG();
1211 }
1212 }
1213
1214 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001215 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001216
Pierre Ossmanae628902009-05-03 20:45:03 +02001217 host->pwr = pwr;
1218
1219 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001221 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001222 }
1223
1224 /*
1225 * Spec says that we should clear the power reg before setting
1226 * a new value. Some controllers don't seem to like this though.
1227 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001228 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001229 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001230
Andres Salomone08c1692008-07-04 10:00:03 -07001231 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001232 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001233 * and set turn on power at the same time, so set the voltage first.
1234 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001235 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001236 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1237
1238 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001239
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001240 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001241
1242 /*
1243 * Some controllers need an extra 10ms delay of 10ms before they
1244 * can apply clock after applying power
1245 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001246 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001247 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001248
1249 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001250}
1251
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252/*****************************************************************************\
1253 * *
1254 * MMC callbacks *
1255 * *
1256\*****************************************************************************/
1257
1258static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1259{
1260 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001261 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001262 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001263 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
1265 host = mmc_priv(mmc);
1266
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001267 sdhci_runtime_pm_get(host);
1268
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269 spin_lock_irqsave(&host->lock, flags);
1270
1271 WARN_ON(host->mrq != NULL);
1272
Pierre Ossmanf9134312008-12-21 17:01:48 +01001273#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001274 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001275#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001276
1277 /*
1278 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1279 * requests if Auto-CMD12 is enabled.
1280 */
1281 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001282 if (mrq->stop) {
1283 mrq->data->stop = NULL;
1284 mrq->stop = NULL;
1285 }
1286 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287
1288 host->mrq = mrq;
1289
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001290 /* If polling, assume that the card is always present. */
1291 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1292 present = true;
1293 else
1294 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1295 SDHCI_CARD_PRESENT;
1296
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001297 /* If we're using a cd-gpio, testing the presence bit might fail. */
1298 if (!present) {
1299 int ret = mmc_gpio_get_cd(host->mmc);
1300 if (ret > 0)
1301 present = true;
1302 }
1303
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001304 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001305 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301307 } else {
1308 u32 present_state;
1309
1310 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1311 /*
1312 * Check if the re-tuning timer has already expired and there
1313 * is no on-going data transfer. If so, we need to execute
1314 * tuning procedure before sending command.
1315 */
1316 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1317 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001318 if (mmc->card) {
1319 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1320 tuning_opcode =
1321 mmc->card->type == MMC_TYPE_MMC ?
1322 MMC_SEND_TUNING_BLOCK_HS200 :
1323 MMC_SEND_TUNING_BLOCK;
1324 spin_unlock_irqrestore(&host->lock, flags);
1325 sdhci_execute_tuning(mmc, tuning_opcode);
1326 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301327
Chris Ball14efd952012-11-05 14:29:49 -05001328 /* Restore original mmc_request structure */
1329 host->mrq = mrq;
1330 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301331 }
1332
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001333 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001334 sdhci_send_command(host, mrq->sbc);
1335 else
1336 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301337 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338
Pierre Ossman5f25a662006-10-04 02:15:39 -07001339 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 spin_unlock_irqrestore(&host->lock, flags);
1341}
1342
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001343static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001346 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347 u8 ctrl;
1348
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349 spin_lock_irqsave(&host->lock, flags);
1350
Adrian Hunterceb61432011-12-27 15:48:41 +02001351 if (host->flags & SDHCI_DEVICE_DEAD) {
1352 spin_unlock_irqrestore(&host->lock, flags);
1353 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1354 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1355 return;
1356 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001357
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358 /*
1359 * Reset the chip on each power off.
1360 * Should clear out any weird states.
1361 */
1362 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001363 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001364 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365 }
1366
1367 sdhci_set_clock(host, ios->clock);
1368
1369 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001370 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001372 vdd_bit = sdhci_set_power(host, ios->vdd);
1373
1374 if (host->vmmc && vdd_bit != -1) {
1375 spin_unlock_irqrestore(&host->lock, flags);
1376 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1377 spin_lock_irqsave(&host->lock, flags);
1378 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379
Philip Rakity643a81f2010-09-23 08:24:32 -07001380 if (host->ops->platform_send_init_74_clocks)
1381 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1382
Philip Rakity15ec4462010-11-19 16:48:39 -05001383 /*
1384 * If your platform has 8-bit width support but is not a v3 controller,
1385 * or if it requires special setup code, you should implement that in
1386 * platform_8bit_width().
1387 */
1388 if (host->ops->platform_8bit_width)
1389 host->ops->platform_8bit_width(host, ios->bus_width);
1390 else {
1391 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1392 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1393 ctrl &= ~SDHCI_CTRL_4BITBUS;
1394 if (host->version >= SDHCI_SPEC_300)
1395 ctrl |= SDHCI_CTRL_8BITBUS;
1396 } else {
1397 if (host->version >= SDHCI_SPEC_300)
1398 ctrl &= ~SDHCI_CTRL_8BITBUS;
1399 if (ios->bus_width == MMC_BUS_WIDTH_4)
1400 ctrl |= SDHCI_CTRL_4BITBUS;
1401 else
1402 ctrl &= ~SDHCI_CTRL_4BITBUS;
1403 }
1404 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1405 }
1406
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001407 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001408
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001409 if ((ios->timing == MMC_TIMING_SD_HS ||
1410 ios->timing == MMC_TIMING_MMC_HS)
1411 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001412 ctrl |= SDHCI_CTRL_HISPD;
1413 else
1414 ctrl &= ~SDHCI_CTRL_HISPD;
1415
Arindam Nathd6d50a12011-05-05 12:18:59 +05301416 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301417 u16 clk, ctrl_2;
1418 unsigned int clock;
1419
1420 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301421 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1422 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301423 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1424 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001425 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301426 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301427
1428 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1429 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301430 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301431 /*
1432 * We only need to set Driver Strength if the
1433 * preset value enable is not set.
1434 */
1435 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1436 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1437 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1438 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1439 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1440
1441 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301442 } else {
1443 /*
1444 * According to SDHC Spec v3.00, if the Preset Value
1445 * Enable in the Host Control 2 register is set, we
1446 * need to reset SD Clock Enable before changing High
1447 * Speed Enable to avoid generating clock gliches.
1448 */
Arindam Nath758535c2011-05-05 12:19:00 +05301449
1450 /* Reset SD Clock Enable */
1451 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1452 clk &= ~SDHCI_CLOCK_CARD_EN;
1453 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1454
1455 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1456
1457 /* Re-enable SD Clock */
1458 clock = host->clock;
1459 host->clock = 0;
1460 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301461 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301462
Arindam Nath49c468f2011-05-05 12:19:01 +05301463
1464 /* Reset SD Clock Enable */
1465 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1466 clk &= ~SDHCI_CLOCK_CARD_EN;
1467 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1468
Philip Rakity6322cdd2011-05-13 11:17:15 +05301469 if (host->ops->set_uhs_signaling)
1470 host->ops->set_uhs_signaling(host, ios->timing);
1471 else {
1472 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1473 /* Select Bus Speed Mode for host */
1474 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301475 if (ios->timing == MMC_TIMING_MMC_HS200)
1476 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1477 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301478 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1479 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1480 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1481 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1482 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1483 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1484 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1485 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1486 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1487 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1488 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301489
1490 /* Re-enable SD Clock */
1491 clock = host->clock;
1492 host->clock = 0;
1493 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301494 } else
1495 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301496
Leandro Dorileob8352262007-07-25 23:47:04 +02001497 /*
1498 * Some (ENE) controllers go apeshit on some ios operation,
1499 * signalling timeout and CRC errors even on CMD0. Resetting
1500 * it on each ios seems to solve the problem.
1501 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001502 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001503 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1504
Pierre Ossman5f25a662006-10-04 02:15:39 -07001505 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506 spin_unlock_irqrestore(&host->lock, flags);
1507}
1508
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001509static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1510{
1511 struct sdhci_host *host = mmc_priv(mmc);
1512
1513 sdhci_runtime_pm_get(host);
1514 sdhci_do_set_ios(host, ios);
1515 sdhci_runtime_pm_put(host);
1516}
1517
1518static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001520 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001521 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523 spin_lock_irqsave(&host->lock, flags);
1524
Pierre Ossman1e728592008-04-16 19:13:13 +02001525 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001526 is_readonly = 0;
1527 else if (host->ops->get_ro)
1528 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001529 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001530 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1531 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001532
1533 spin_unlock_irqrestore(&host->lock, flags);
1534
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001535 /* This quirk needs to be replaced by a callback-function later */
1536 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1537 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538}
1539
Takashi Iwai82b0e232011-04-21 20:26:38 +02001540#define SAMPLE_COUNT 5
1541
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001542static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001543{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001544 int i, ro_count;
1545
Takashi Iwai82b0e232011-04-21 20:26:38 +02001546 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001547 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001548
1549 ro_count = 0;
1550 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001551 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001552 if (++ro_count > SAMPLE_COUNT / 2)
1553 return 1;
1554 }
1555 msleep(30);
1556 }
1557 return 0;
1558}
1559
Adrian Hunter20758b62011-08-29 16:42:12 +03001560static void sdhci_hw_reset(struct mmc_host *mmc)
1561{
1562 struct sdhci_host *host = mmc_priv(mmc);
1563
1564 if (host->ops && host->ops->hw_reset)
1565 host->ops->hw_reset(host);
1566}
1567
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001568static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001569{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001570 struct sdhci_host *host = mmc_priv(mmc);
1571 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001572
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001573 sdhci_runtime_pm_get(host);
1574 ret = sdhci_do_get_ro(host);
1575 sdhci_runtime_pm_put(host);
1576 return ret;
1577}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001578
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001579static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1580{
Pierre Ossman1e728592008-04-16 19:13:13 +02001581 if (host->flags & SDHCI_DEVICE_DEAD)
1582 goto out;
1583
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001584 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001585 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1586 else
1587 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1588
1589 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1590 if (host->runtime_suspended)
1591 goto out;
1592
1593 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001594 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1595 else
1596 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001597out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001598 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001599}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001600
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001601static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1602{
1603 struct sdhci_host *host = mmc_priv(mmc);
1604 unsigned long flags;
1605
1606 spin_lock_irqsave(&host->lock, flags);
1607 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001608 spin_unlock_irqrestore(&host->lock, flags);
1609}
1610
Philip Rakity6231f3d2012-07-23 15:56:23 -07001611static int sdhci_do_3_3v_signal_voltage_switch(struct sdhci_host *host,
1612 u16 ctrl)
Arindam Nathf2119df2011-05-05 12:18:57 +05301613{
Philip Rakity6231f3d2012-07-23 15:56:23 -07001614 int ret;
Arindam Nathf2119df2011-05-05 12:18:57 +05301615
Philip Rakity6231f3d2012-07-23 15:56:23 -07001616 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1617 ctrl &= ~SDHCI_CTRL_VDD_180;
1618 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Arindam Nathf2119df2011-05-05 12:18:57 +05301619
Philip Rakity6231f3d2012-07-23 15:56:23 -07001620 if (host->vqmmc) {
1621 ret = regulator_set_voltage(host->vqmmc, 3300000, 3300000);
1622 if (ret) {
1623 pr_warning("%s: Switching to 3.3V signalling voltage "
1624 " failed\n", mmc_hostname(host->mmc));
Arindam Nathf2119df2011-05-05 12:18:57 +05301625 return -EIO;
1626 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07001627 }
1628 /* Wait for 5ms */
1629 usleep_range(5000, 5500);
Arindam Nathf2119df2011-05-05 12:18:57 +05301630
Philip Rakity6231f3d2012-07-23 15:56:23 -07001631 /* 3.3V regulator output should be stable within 5 ms */
1632 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1633 if (!(ctrl & SDHCI_CTRL_VDD_180))
1634 return 0;
1635
1636 pr_warning("%s: 3.3V regulator output did not became stable\n",
1637 mmc_hostname(host->mmc));
1638
1639 return -EIO;
1640}
1641
1642static int sdhci_do_1_8v_signal_voltage_switch(struct sdhci_host *host,
1643 u16 ctrl)
1644{
1645 u8 pwr;
1646 u16 clk;
1647 u32 present_state;
1648 int ret;
1649
1650 /* Stop SDCLK */
1651 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1652 clk &= ~SDHCI_CLOCK_CARD_EN;
1653 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1654
1655 /* Check whether DAT[3:0] is 0000 */
1656 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1657 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1658 SDHCI_DATA_LVL_SHIFT)) {
1659 /*
1660 * Enable 1.8V Signal Enable in the Host Control2
1661 * register
1662 */
1663 if (host->vqmmc)
1664 ret = regulator_set_voltage(host->vqmmc,
1665 1800000, 1800000);
1666 else
1667 ret = 0;
1668
1669 if (!ret) {
Arindam Nathf2119df2011-05-05 12:18:57 +05301670 ctrl |= SDHCI_CTRL_VDD_180;
1671 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1672
1673 /* Wait for 5ms */
1674 usleep_range(5000, 5500);
1675
1676 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1677 if (ctrl & SDHCI_CTRL_VDD_180) {
Philip Rakity6231f3d2012-07-23 15:56:23 -07001678 /* Provide SDCLK again and wait for 1ms */
Arindam Nathf2119df2011-05-05 12:18:57 +05301679 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1680 clk |= SDHCI_CLOCK_CARD_EN;
1681 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1682 usleep_range(1000, 1500);
1683
1684 /*
1685 * If DAT[3:0] level is 1111b, then the card
1686 * was successfully switched to 1.8V signaling.
1687 */
1688 present_state = sdhci_readl(host,
1689 SDHCI_PRESENT_STATE);
1690 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1691 SDHCI_DATA_LVL_MASK)
1692 return 0;
1693 }
1694 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07001695 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301696
Philip Rakity6231f3d2012-07-23 15:56:23 -07001697 /*
1698 * If we are here, that means the switch to 1.8V signaling
1699 * failed. We power cycle the card, and retry initialization
1700 * sequence by setting S18R to 0.
1701 */
1702 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1703 pwr &= ~SDHCI_POWER_ON;
1704 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1705 if (host->vmmc)
1706 regulator_disable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301707
Philip Rakity6231f3d2012-07-23 15:56:23 -07001708 /* Wait for 1ms as per the spec */
1709 usleep_range(1000, 1500);
1710 pwr |= SDHCI_POWER_ON;
1711 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1712 if (host->vmmc)
1713 regulator_enable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301714
Philip Rakity6231f3d2012-07-23 15:56:23 -07001715 pr_warning("%s: Switching to 1.8V signalling voltage failed, "
1716 "retrying with S18R set to 0\n", mmc_hostname(host->mmc));
1717
1718 return -EAGAIN;
1719}
1720
1721static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1722 struct mmc_ios *ios)
1723{
1724 u16 ctrl;
1725
1726 /*
1727 * Signal Voltage Switching is only applicable for Host Controllers
1728 * v3.00 and above.
1729 */
1730 if (host->version < SDHCI_SPEC_300)
1731 return 0;
1732
1733 /*
1734 * We first check whether the request is to set signalling voltage
1735 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1736 */
1737 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1738 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1739 return sdhci_do_3_3v_signal_voltage_switch(host, ctrl);
1740 else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1741 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180))
1742 return sdhci_do_1_8v_signal_voltage_switch(host, ctrl);
1743 else
Arindam Nathf2119df2011-05-05 12:18:57 +05301744 /* No signal voltage switch required */
1745 return 0;
1746}
1747
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001748static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1749 struct mmc_ios *ios)
1750{
1751 struct sdhci_host *host = mmc_priv(mmc);
1752 int err;
1753
1754 if (host->version < SDHCI_SPEC_300)
1755 return 0;
1756 sdhci_runtime_pm_get(host);
1757 err = sdhci_do_start_signal_voltage_switch(host, ios);
1758 sdhci_runtime_pm_put(host);
1759 return err;
1760}
1761
Girish K S069c9f12012-01-06 09:56:39 +05301762static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301763{
1764 struct sdhci_host *host;
1765 u16 ctrl;
1766 u32 ier;
1767 int tuning_loop_counter = MAX_TUNING_LOOP;
1768 unsigned long timeout;
1769 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301770 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301771
1772 host = mmc_priv(mmc);
1773
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001774 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301775 disable_irq(host->irq);
1776 spin_lock(&host->lock);
1777
1778 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1779
1780 /*
Girish K S069c9f12012-01-06 09:56:39 +05301781 * The Host Controller needs tuning only in case of SDR104 mode
1782 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301783 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301784 * If the Host Controller supports the HS200 mode then the
1785 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301786 */
Girish K S069c9f12012-01-06 09:56:39 +05301787 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1788 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1789 host->flags & SDHCI_HS200_NEEDS_TUNING))
1790 requires_tuning_nonuhs = true;
1791
Arindam Nathb513ea22011-05-05 12:19:04 +05301792 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301793 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301794 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1795 else {
1796 spin_unlock(&host->lock);
1797 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001798 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301799 return 0;
1800 }
1801
1802 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1803
1804 /*
1805 * As per the Host Controller spec v3.00, tuning command
1806 * generates Buffer Read Ready interrupt, so enable that.
1807 *
1808 * Note: The spec clearly says that when tuning sequence
1809 * is being performed, the controller does not generate
1810 * interrupts other than Buffer Read Ready interrupt. But
1811 * to make sure we don't hit a controller bug, we _only_
1812 * enable Buffer Read Ready interrupt here.
1813 */
1814 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1815 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1816
1817 /*
1818 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1819 * of loops reaches 40 times or a timeout of 150ms occurs.
1820 */
1821 timeout = 150;
1822 do {
1823 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001824 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301825
1826 if (!tuning_loop_counter && !timeout)
1827 break;
1828
Girish K S069c9f12012-01-06 09:56:39 +05301829 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301830 cmd.arg = 0;
1831 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1832 cmd.retries = 0;
1833 cmd.data = NULL;
1834 cmd.error = 0;
1835
1836 mrq.cmd = &cmd;
1837 host->mrq = &mrq;
1838
1839 /*
1840 * In response to CMD19, the card sends 64 bytes of tuning
1841 * block to the Host Controller. So we set the block size
1842 * to 64 here.
1843 */
Girish K S069c9f12012-01-06 09:56:39 +05301844 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1845 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1846 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1847 SDHCI_BLOCK_SIZE);
1848 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1849 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1850 SDHCI_BLOCK_SIZE);
1851 } else {
1852 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1853 SDHCI_BLOCK_SIZE);
1854 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301855
1856 /*
1857 * The tuning block is sent by the card to the host controller.
1858 * So we set the TRNS_READ bit in the Transfer Mode register.
1859 * This also takes care of setting DMA Enable and Multi Block
1860 * Select in the same register to 0.
1861 */
1862 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1863
1864 sdhci_send_command(host, &cmd);
1865
1866 host->cmd = NULL;
1867 host->mrq = NULL;
1868
1869 spin_unlock(&host->lock);
1870 enable_irq(host->irq);
1871
1872 /* Wait for Buffer Read Ready interrupt */
1873 wait_event_interruptible_timeout(host->buf_ready_int,
1874 (host->tuning_done == 1),
1875 msecs_to_jiffies(50));
1876 disable_irq(host->irq);
1877 spin_lock(&host->lock);
1878
1879 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301880 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301881 "Buffer Read Ready interrupt during tuning "
1882 "procedure, falling back to fixed sampling "
1883 "clock\n");
1884 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1885 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1886 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1887 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1888
1889 err = -EIO;
1890 goto out;
1891 }
1892
1893 host->tuning_done = 0;
1894
1895 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1896 tuning_loop_counter--;
1897 timeout--;
1898 mdelay(1);
1899 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1900
1901 /*
1902 * The Host Driver has exhausted the maximum number of loops allowed,
1903 * so use fixed sampling frequency.
1904 */
1905 if (!tuning_loop_counter || !timeout) {
1906 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1907 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1908 } else {
1909 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301910 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301911 " failed, falling back to fixed sampling"
1912 " clock\n");
1913 err = -EIO;
1914 }
1915 }
1916
1917out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301918 /*
1919 * If this is the very first time we are here, we start the retuning
1920 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1921 * flag won't be set, we check this condition before actually starting
1922 * the timer.
1923 */
1924 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1925 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001926 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301927 mod_timer(&host->tuning_timer, jiffies +
1928 host->tuning_count * HZ);
1929 /* Tuning mode 1 limits the maximum data length to 4MB */
1930 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1931 } else {
1932 host->flags &= ~SDHCI_NEEDS_RETUNING;
1933 /* Reload the new initial value for timer */
1934 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1935 mod_timer(&host->tuning_timer, jiffies +
1936 host->tuning_count * HZ);
1937 }
1938
1939 /*
1940 * In case tuning fails, host controllers which support re-tuning can
1941 * try tuning again at a later time, when the re-tuning timer expires.
1942 * So for these controllers, we return 0. Since there might be other
1943 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08001944 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
1945 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301946 */
Aaron Lu973905f2012-07-04 13:29:09 +08001947 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301948 err = 0;
1949
Arindam Nathb513ea22011-05-05 12:19:04 +05301950 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1951 spin_unlock(&host->lock);
1952 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001953 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301954
1955 return err;
1956}
1957
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001958static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301959{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301960 u16 ctrl;
1961 unsigned long flags;
1962
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301963 /* Host Controller v3.00 defines preset value registers */
1964 if (host->version < SDHCI_SPEC_300)
1965 return;
1966
1967 spin_lock_irqsave(&host->lock, flags);
1968
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1970
1971 /*
1972 * We only enable or disable Preset Value if they are not already
1973 * enabled or disabled respectively. Otherwise, we bail out.
1974 */
1975 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1976 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1977 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001978 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301979 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1980 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1981 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001982 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301983 }
1984
1985 spin_unlock_irqrestore(&host->lock, flags);
1986}
1987
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001988static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1989{
1990 struct sdhci_host *host = mmc_priv(mmc);
1991
1992 sdhci_runtime_pm_get(host);
1993 sdhci_do_enable_preset_value(host, enable);
1994 sdhci_runtime_pm_put(host);
1995}
1996
David Brownellab7aefd2006-11-12 17:55:30 -08001997static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998 .request = sdhci_request,
1999 .set_ios = sdhci_set_ios,
2000 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03002001 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002002 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05302003 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05302004 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302005 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08002006};
2007
2008/*****************************************************************************\
2009 * *
2010 * Tasklets *
2011 * *
2012\*****************************************************************************/
2013
2014static void sdhci_tasklet_card(unsigned long param)
2015{
2016 struct sdhci_host *host;
2017 unsigned long flags;
2018
2019 host = (struct sdhci_host*)param;
2020
2021 spin_lock_irqsave(&host->lock, flags);
2022
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002023 /* Check host->mrq first in case we are runtime suspended */
2024 if (host->mrq &&
2025 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302026 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002027 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302028 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002029 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002030
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002031 sdhci_reset(host, SDHCI_RESET_CMD);
2032 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002033
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002034 host->mrq->cmd->error = -ENOMEDIUM;
2035 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002036 }
2037
2038 spin_unlock_irqrestore(&host->lock, flags);
2039
Pierre Ossman04cf5852008-08-18 22:18:14 +02002040 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002041}
2042
2043static void sdhci_tasklet_finish(unsigned long param)
2044{
2045 struct sdhci_host *host;
2046 unsigned long flags;
2047 struct mmc_request *mrq;
2048
2049 host = (struct sdhci_host*)param;
2050
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002051 spin_lock_irqsave(&host->lock, flags);
2052
Chris Ball0c9c99a2011-04-27 17:35:31 -04002053 /*
2054 * If this tasklet gets rescheduled while running, it will
2055 * be run again afterwards but without any active request.
2056 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002057 if (!host->mrq) {
2058 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002059 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002060 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002061
2062 del_timer(&host->timer);
2063
2064 mrq = host->mrq;
2065
Pierre Ossmand129bce2006-03-24 03:18:17 -08002066 /*
2067 * The controller needs a reset of internal state machines
2068 * upon error conditions.
2069 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002070 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002071 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002072 (mrq->data && (mrq->data->error ||
2073 (mrq->data->stop && mrq->data->stop->error))) ||
2074 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002075
2076 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002077 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002078 unsigned int clock;
2079
2080 /* This is to force an update */
2081 clock = host->clock;
2082 host->clock = 0;
2083 sdhci_set_clock(host, clock);
2084 }
2085
2086 /* Spec says we should do both at the same time, but Ricoh
2087 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002088 sdhci_reset(host, SDHCI_RESET_CMD);
2089 sdhci_reset(host, SDHCI_RESET_DATA);
2090 }
2091
2092 host->mrq = NULL;
2093 host->cmd = NULL;
2094 host->data = NULL;
2095
Pierre Ossmanf9134312008-12-21 17:01:48 +01002096#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002098#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099
Pierre Ossman5f25a662006-10-04 02:15:39 -07002100 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002101 spin_unlock_irqrestore(&host->lock, flags);
2102
2103 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002104 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002105}
2106
2107static void sdhci_timeout_timer(unsigned long data)
2108{
2109 struct sdhci_host *host;
2110 unsigned long flags;
2111
2112 host = (struct sdhci_host*)data;
2113
2114 spin_lock_irqsave(&host->lock, flags);
2115
2116 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302117 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002118 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002119 sdhci_dumpregs(host);
2120
2121 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002122 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002123 sdhci_finish_data(host);
2124 } else {
2125 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002126 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002127 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002128 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002129
2130 tasklet_schedule(&host->finish_tasklet);
2131 }
2132 }
2133
Pierre Ossman5f25a662006-10-04 02:15:39 -07002134 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135 spin_unlock_irqrestore(&host->lock, flags);
2136}
2137
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302138static void sdhci_tuning_timer(unsigned long data)
2139{
2140 struct sdhci_host *host;
2141 unsigned long flags;
2142
2143 host = (struct sdhci_host *)data;
2144
2145 spin_lock_irqsave(&host->lock, flags);
2146
2147 host->flags |= SDHCI_NEEDS_RETUNING;
2148
2149 spin_unlock_irqrestore(&host->lock, flags);
2150}
2151
Pierre Ossmand129bce2006-03-24 03:18:17 -08002152/*****************************************************************************\
2153 * *
2154 * Interrupt handling *
2155 * *
2156\*****************************************************************************/
2157
2158static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2159{
2160 BUG_ON(intmask == 0);
2161
2162 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302163 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002164 "though no command operation was in progress.\n",
2165 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 sdhci_dumpregs(host);
2167 return;
2168 }
2169
Pierre Ossman43b58b32007-07-25 23:15:27 +02002170 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002171 host->cmd->error = -ETIMEDOUT;
2172 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2173 SDHCI_INT_INDEX))
2174 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175
Pierre Ossmane8095172008-07-25 01:09:08 +02002176 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002178 return;
2179 }
2180
2181 /*
2182 * The host can send and interrupt when the busy state has
2183 * ended, allowing us to wait without wasting CPU cycles.
2184 * Unfortunately this is overloaded on the "data complete"
2185 * interrupt, so we need to take some care when handling
2186 * it.
2187 *
2188 * Note: The 1.0 specification is a bit ambiguous about this
2189 * feature so there might be some problems with older
2190 * controllers.
2191 */
2192 if (host->cmd->flags & MMC_RSP_BUSY) {
2193 if (host->cmd->data)
2194 DBG("Cannot wait for busy signal when also "
2195 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002196 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002197 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002198
2199 /* The controller does not support the end-of-busy IRQ,
2200 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002201 }
2202
2203 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002204 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205}
2206
George G. Davis0957c332010-02-18 12:32:12 -05002207#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002208static void sdhci_show_adma_error(struct sdhci_host *host)
2209{
2210 const char *name = mmc_hostname(host->mmc);
2211 u8 *desc = host->adma_desc;
2212 __le32 *dma;
2213 __le16 *len;
2214 u8 attr;
2215
2216 sdhci_dumpregs(host);
2217
2218 while (true) {
2219 dma = (__le32 *)(desc + 4);
2220 len = (__le16 *)(desc + 2);
2221 attr = *desc;
2222
2223 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2224 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2225
2226 desc += 8;
2227
2228 if (attr & 2)
2229 break;
2230 }
2231}
2232#else
2233static void sdhci_show_adma_error(struct sdhci_host *host) { }
2234#endif
2235
Pierre Ossmand129bce2006-03-24 03:18:17 -08002236static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2237{
Girish K S069c9f12012-01-06 09:56:39 +05302238 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239 BUG_ON(intmask == 0);
2240
Arindam Nathb513ea22011-05-05 12:19:04 +05302241 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2242 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302243 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2244 if (command == MMC_SEND_TUNING_BLOCK ||
2245 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302246 host->tuning_done = 1;
2247 wake_up(&host->buf_ready_int);
2248 return;
2249 }
2250 }
2251
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252 if (!host->data) {
2253 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002254 * The "data complete" interrupt is also used to
2255 * indicate that a busy state has ended. See comment
2256 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002258 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2259 if (intmask & SDHCI_INT_DATA_END) {
2260 sdhci_finish_command(host);
2261 return;
2262 }
2263 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264
Girish K Sa3c76eb2011-10-11 11:44:09 +05302265 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002266 "though no data operation was in progress.\n",
2267 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268 sdhci_dumpregs(host);
2269
2270 return;
2271 }
2272
2273 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002274 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002275 else if (intmask & SDHCI_INT_DATA_END_BIT)
2276 host->data->error = -EILSEQ;
2277 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2278 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2279 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002280 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002281 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302282 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002283 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002284 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002285 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286
Pierre Ossman17b04292007-07-22 22:18:46 +02002287 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288 sdhci_finish_data(host);
2289 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002290 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291 sdhci_transfer_pio(host);
2292
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002293 /*
2294 * We currently don't do anything fancy with DMA
2295 * boundaries, but as we can't disable the feature
2296 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002297 *
2298 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2299 * should return a valid address to continue from, but as
2300 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002301 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002302 if (intmask & SDHCI_INT_DMA_END) {
2303 u32 dmastart, dmanow;
2304 dmastart = sg_dma_address(host->data->sg);
2305 dmanow = dmastart + host->data->bytes_xfered;
2306 /*
2307 * Force update to the next DMA block boundary.
2308 */
2309 dmanow = (dmanow &
2310 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2311 SDHCI_DEFAULT_BOUNDARY_SIZE;
2312 host->data->bytes_xfered = dmanow - dmastart;
2313 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2314 " next 0x%08x\n",
2315 mmc_hostname(host->mmc), dmastart,
2316 host->data->bytes_xfered, dmanow);
2317 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2318 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002319
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002320 if (intmask & SDHCI_INT_DATA_END) {
2321 if (host->cmd) {
2322 /*
2323 * Data managed to finish before the
2324 * command completed. Make sure we do
2325 * things in the proper order.
2326 */
2327 host->data_early = 1;
2328 } else {
2329 sdhci_finish_data(host);
2330 }
2331 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 }
2333}
2334
David Howells7d12e782006-10-05 14:55:46 +01002335static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002336{
2337 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002338 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002339 u32 intmask, unexpected = 0;
2340 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002341
2342 spin_lock(&host->lock);
2343
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002344 if (host->runtime_suspended) {
2345 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302346 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002347 mmc_hostname(host->mmc));
2348 return IRQ_HANDLED;
2349 }
2350
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002351 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352
Mark Lord62df67a52007-03-06 13:30:13 +01002353 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354 result = IRQ_NONE;
2355 goto out;
2356 }
2357
Alexander Stein6379b232012-03-14 09:52:10 +01002358again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002359 DBG("*** %s got interrupt: 0x%08x\n",
2360 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361
Pierre Ossman3192a282006-06-30 02:22:26 -07002362 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002363 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2364 SDHCI_CARD_PRESENT;
2365
2366 /*
2367 * There is a observation on i.mx esdhc. INSERT bit will be
2368 * immediately set again when it gets cleared, if a card is
2369 * inserted. We have to mask the irq to prevent interrupt
2370 * storm which will freeze the system. And the REMOVE gets
2371 * the same situation.
2372 *
2373 * More testing are needed here to ensure it works for other
2374 * platforms though.
2375 */
2376 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2377 SDHCI_INT_CARD_REMOVE);
2378 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2379 SDHCI_INT_CARD_INSERT);
2380
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002381 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002382 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2383 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002385 }
2386
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002388 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2389 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002390 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391 }
2392
2393 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002394 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2395 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002396 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002397 }
2398
2399 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2400
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002401 intmask &= ~SDHCI_INT_ERROR;
2402
Pierre Ossmand129bce2006-03-24 03:18:17 -08002403 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302404 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002406 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 }
2408
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002409 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002410
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002411 if (intmask & SDHCI_INT_CARD_INT)
2412 cardint = 1;
2413
2414 intmask &= ~SDHCI_INT_CARD_INT;
2415
Pierre Ossman3192a282006-06-30 02:22:26 -07002416 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002417 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002418 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002419 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002420
2421 result = IRQ_HANDLED;
2422
Alexander Stein6379b232012-03-14 09:52:10 +01002423 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2424 if (intmask && --max_loops)
2425 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426out:
2427 spin_unlock(&host->lock);
2428
Alexander Stein6379b232012-03-14 09:52:10 +01002429 if (unexpected) {
2430 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2431 mmc_hostname(host->mmc), unexpected);
2432 sdhci_dumpregs(host);
2433 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002434 /*
2435 * We have to delay this as it calls back into the driver.
2436 */
2437 if (cardint)
2438 mmc_signal_sdio_irq(host->mmc);
2439
Pierre Ossmand129bce2006-03-24 03:18:17 -08002440 return result;
2441}
2442
2443/*****************************************************************************\
2444 * *
2445 * Suspend/resume *
2446 * *
2447\*****************************************************************************/
2448
2449#ifdef CONFIG_PM
2450
Manuel Lauss29495aa2011-11-03 11:09:45 +01002451int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002452{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002453 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454
Chris Balla1b13b42012-02-06 00:43:59 -05002455 if (host->ops->platform_suspend)
2456 host->ops->platform_suspend(host);
2457
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002458 sdhci_disable_card_detection(host);
2459
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302460 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002461 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002462 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302463 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302464 }
2465
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002466 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002467 if (ret) {
Aaron Lu973905f2012-07-04 13:29:09 +08002468 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Lu38a60ea2012-01-04 10:07:43 +08002469 host->flags |= SDHCI_NEEDS_RETUNING;
2470 mod_timer(&host->tuning_timer, jiffies +
2471 host->tuning_count * HZ);
2472 }
2473
2474 sdhci_enable_card_detection(host);
2475
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002476 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002477 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002479 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002480
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002481 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002482}
2483
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002484EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002485
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002486int sdhci_resume_host(struct sdhci_host *host)
2487{
2488 int ret;
2489
Richard Röjforsa13abc72009-09-22 16:45:30 -07002490 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002491 if (host->ops->enable_dma)
2492 host->ops->enable_dma(host);
2493 }
2494
2495 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2496 mmc_hostname(host->mmc), host);
2497 if (ret)
2498 return ret;
2499
Adrian Hunter6308d292012-02-07 14:48:54 +02002500 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2501 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2502 /* Card keeps power but host controller does not */
2503 sdhci_init(host, 0);
2504 host->pwr = 0;
2505 host->clock = 0;
2506 sdhci_do_set_ios(host, &host->mmc->ios);
2507 } else {
2508 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2509 mmiowb();
2510 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002511
2512 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002513 sdhci_enable_card_detection(host);
2514
Chris Balla1b13b42012-02-06 00:43:59 -05002515 if (host->ops->platform_resume)
2516 host->ops->platform_resume(host);
2517
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302518 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002519 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302520 host->flags |= SDHCI_NEEDS_RETUNING;
2521
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002522 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002523}
2524
2525EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526
Daniel Drake5f619702010-11-04 22:20:39 +00002527void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2528{
2529 u8 val;
2530 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2531 val |= SDHCI_WAKE_ON_INT;
2532 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2533}
2534
2535EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2536
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537#endif /* CONFIG_PM */
2538
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002539#ifdef CONFIG_PM_RUNTIME
2540
2541static int sdhci_runtime_pm_get(struct sdhci_host *host)
2542{
2543 return pm_runtime_get_sync(host->mmc->parent);
2544}
2545
2546static int sdhci_runtime_pm_put(struct sdhci_host *host)
2547{
2548 pm_runtime_mark_last_busy(host->mmc->parent);
2549 return pm_runtime_put_autosuspend(host->mmc->parent);
2550}
2551
2552int sdhci_runtime_suspend_host(struct sdhci_host *host)
2553{
2554 unsigned long flags;
2555 int ret = 0;
2556
2557 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002558 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002559 del_timer_sync(&host->tuning_timer);
2560 host->flags &= ~SDHCI_NEEDS_RETUNING;
2561 }
2562
2563 spin_lock_irqsave(&host->lock, flags);
2564 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2565 spin_unlock_irqrestore(&host->lock, flags);
2566
2567 synchronize_irq(host->irq);
2568
2569 spin_lock_irqsave(&host->lock, flags);
2570 host->runtime_suspended = true;
2571 spin_unlock_irqrestore(&host->lock, flags);
2572
2573 return ret;
2574}
2575EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2576
2577int sdhci_runtime_resume_host(struct sdhci_host *host)
2578{
2579 unsigned long flags;
2580 int ret = 0, host_flags = host->flags;
2581
2582 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2583 if (host->ops->enable_dma)
2584 host->ops->enable_dma(host);
2585 }
2586
2587 sdhci_init(host, 0);
2588
2589 /* Force clock and power re-program */
2590 host->pwr = 0;
2591 host->clock = 0;
2592 sdhci_do_set_ios(host, &host->mmc->ios);
2593
2594 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2595 if (host_flags & SDHCI_PV_ENABLED)
2596 sdhci_do_enable_preset_value(host, true);
2597
2598 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002599 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002600 host->flags |= SDHCI_NEEDS_RETUNING;
2601
2602 spin_lock_irqsave(&host->lock, flags);
2603
2604 host->runtime_suspended = false;
2605
2606 /* Enable SDIO IRQ */
2607 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2608 sdhci_enable_sdio_irq_nolock(host, true);
2609
2610 /* Enable Card Detection */
2611 sdhci_enable_card_detection(host);
2612
2613 spin_unlock_irqrestore(&host->lock, flags);
2614
2615 return ret;
2616}
2617EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2618
2619#endif
2620
Pierre Ossmand129bce2006-03-24 03:18:17 -08002621/*****************************************************************************\
2622 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002623 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002624 * *
2625\*****************************************************************************/
2626
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002627struct sdhci_host *sdhci_alloc_host(struct device *dev,
2628 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002629{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002630 struct mmc_host *mmc;
2631 struct sdhci_host *host;
2632
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002633 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002635 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002636 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002637 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002638
2639 host = mmc_priv(mmc);
2640 host->mmc = mmc;
2641
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002642 return host;
2643}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002644
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002645EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002646
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002647int sdhci_add_host(struct sdhci_host *host)
2648{
2649 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002650 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302651 u32 max_current_caps;
2652 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002653 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002655 WARN_ON(host == NULL);
2656 if (host == NULL)
2657 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002658
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002659 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002660
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002661 if (debug_quirks)
2662 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002663 if (debug_quirks2)
2664 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665
Pierre Ossmand96649e2006-06-30 02:22:30 -07002666 sdhci_reset(host, SDHCI_RESET_ALL);
2667
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002668 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002669 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2670 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002671 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302672 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002673 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002674 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002675 }
2676
Arindam Nathf2119df2011-05-05 12:18:57 +05302677 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002678 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002680 if (host->version >= SDHCI_SPEC_300)
2681 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2682 host->caps1 :
2683 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302684
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002685 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002686 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302687 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002688 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002689 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002690 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002692 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002693 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002694 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002695 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002696 }
2697
Arindam Nathf2119df2011-05-05 12:18:57 +05302698 if ((host->version >= SDHCI_SPEC_200) &&
2699 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002700 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002701
2702 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2703 (host->flags & SDHCI_USE_ADMA)) {
2704 DBG("Disabling ADMA as it is marked broken\n");
2705 host->flags &= ~SDHCI_USE_ADMA;
2706 }
2707
Richard Röjforsa13abc72009-09-22 16:45:30 -07002708 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002709 if (host->ops->enable_dma) {
2710 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302711 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002712 "available. Falling back to PIO.\n",
2713 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002714 host->flags &=
2715 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002716 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002717 }
2718 }
2719
Pierre Ossman2134a922008-06-28 18:28:51 +02002720 if (host->flags & SDHCI_USE_ADMA) {
2721 /*
2722 * We need to allocate descriptors for all sg entries
2723 * (128) and potentially one alignment transfer for
2724 * each of those entries.
2725 */
2726 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2727 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2728 if (!host->adma_desc || !host->align_buffer) {
2729 kfree(host->adma_desc);
2730 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302731 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002732 "buffers. Falling back to standard DMA.\n",
2733 mmc_hostname(mmc));
2734 host->flags &= ~SDHCI_USE_ADMA;
2735 }
2736 }
2737
Pierre Ossman76591502008-07-21 00:32:11 +02002738 /*
2739 * If we use DMA, then it's up to the caller to set the DMA
2740 * mask, but PIO does not need the hw shim so we set a new
2741 * mask here in that case.
2742 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002743 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002744 host->dma_mask = DMA_BIT_MASK(64);
2745 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2746 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002747
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002748 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302749 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002750 >> SDHCI_CLOCK_BASE_SHIFT;
2751 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302752 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002753 >> SDHCI_CLOCK_BASE_SHIFT;
2754
Pierre Ossmand129bce2006-03-24 03:18:17 -08002755 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002756 if (host->max_clk == 0 || host->quirks &
2757 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002758 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302759 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002760 "frequency.\n", mmc_hostname(mmc));
2761 return -ENODEV;
2762 }
2763 host->max_clk = host->ops->get_max_clock(host);
2764 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765
2766 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302767 * In case of Host Controller v3.00, find out whether clock
2768 * multiplier is supported.
2769 */
2770 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2771 SDHCI_CLOCK_MUL_SHIFT;
2772
2773 /*
2774 * In case the value in Clock Multiplier is 0, then programmable
2775 * clock mode is not supported, otherwise the actual clock
2776 * multiplier is one more than the value of Clock Multiplier
2777 * in the Capabilities Register.
2778 */
2779 if (host->clk_mul)
2780 host->clk_mul += 1;
2781
2782 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783 * Set host parameters.
2784 */
2785 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302786 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002787 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002788 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302789 else if (host->version >= SDHCI_SPEC_300) {
2790 if (host->clk_mul) {
2791 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2792 mmc->f_max = host->max_clk * host->clk_mul;
2793 } else
2794 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2795 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002796 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002797
Andy Shevchenko272308c2011-08-03 18:36:00 +03002798 host->timeout_clk =
2799 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2800 if (host->timeout_clk == 0) {
2801 if (host->ops->get_timeout_clock) {
2802 host->timeout_clk = host->ops->get_timeout_clock(host);
2803 } else if (!(host->quirks &
2804 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302805 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002806 "frequency.\n", mmc_hostname(mmc));
2807 return -ENODEV;
2808 }
2809 }
2810 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2811 host->timeout_clk *= 1000;
2812
2813 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002814 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002815
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002816 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002817
Andrei Warkentine89d4562011-05-23 15:06:37 -05002818 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2819
2820 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2821 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002822
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002823 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002824 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002825 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002826 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002827 host->flags |= SDHCI_AUTO_CMD23;
2828 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2829 } else {
2830 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2831 }
2832
Philip Rakity15ec4462010-11-19 16:48:39 -05002833 /*
2834 * A controller may support 8-bit width, but the board itself
2835 * might not have the pins brought out. Boards that support
2836 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2837 * their platform code before calling sdhci_add_host(), and we
2838 * won't assume 8-bit width for hosts without that CAP.
2839 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002840 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002841 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Jerry Huang63ef5d82012-10-25 13:47:19 +08002843 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2844 mmc->caps &= ~MMC_CAP_CMD23;
2845
Arindam Nathf2119df2011-05-05 12:18:57 +05302846 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002847 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002848
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002849 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002850 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002851 mmc->caps |= MMC_CAP_NEEDS_POLL;
2852
Philip Rakity6231f3d2012-07-23 15:56:23 -07002853 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2854 host->vqmmc = regulator_get(mmc_dev(mmc), "vqmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002855 if (IS_ERR_OR_NULL(host->vqmmc)) {
2856 if (PTR_ERR(host->vqmmc) < 0) {
2857 pr_info("%s: no vqmmc regulator found\n",
2858 mmc_hostname(mmc));
2859 host->vqmmc = NULL;
2860 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07002861 }
2862 else if (regulator_is_supported_voltage(host->vqmmc, 1800000, 1800000))
2863 regulator_enable(host->vqmmc);
2864 else
2865 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2866 SDHCI_SUPPORT_DDR50);
2867
Al Cooper4188bba2012-03-16 15:54:17 -04002868 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2869 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2870 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2872
2873 /* SDR104 supports also implies SDR50 support */
2874 if (caps[1] & SDHCI_SUPPORT_SDR104)
2875 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2876 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2877 mmc->caps |= MMC_CAP_UHS_SDR50;
2878
2879 if (caps[1] & SDHCI_SUPPORT_DDR50)
2880 mmc->caps |= MMC_CAP_UHS_DDR50;
2881
Girish K S069c9f12012-01-06 09:56:39 +05302882 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302883 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2884 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2885
Girish K S069c9f12012-01-06 09:56:39 +05302886 /* Does the host need tuning for HS200? */
2887 if (mmc->caps2 & MMC_CAP2_HS200)
2888 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2889
Arindam Nathd6d50a12011-05-05 12:18:59 +05302890 /* Driver Type(s) (A, C, D) supported by the host */
2891 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2892 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2893 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2894 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2895 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2896 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2897
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302898 /* Initial value for re-tuning timer count */
2899 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2900 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2901
2902 /*
2903 * In case Re-tuning Timer is not disabled, the actual value of
2904 * re-tuning timer will be 2 ^ (n - 1).
2905 */
2906 if (host->tuning_count)
2907 host->tuning_count = 1 << (host->tuning_count - 1);
2908
2909 /* Re-tuning mode supported by the Host Controller */
2910 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2911 SDHCI_RETUNING_MODE_SHIFT;
2912
Takashi Iwai8f230f42010-12-08 10:04:30 +01002913 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07002914
2915 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002916 if (IS_ERR_OR_NULL(host->vmmc)) {
2917 if (PTR_ERR(host->vmmc) < 0) {
2918 pr_info("%s: no vmmc regulator found\n",
2919 mmc_hostname(mmc));
2920 host->vmmc = NULL;
2921 }
Philip Rakity77dcb3f2012-07-23 17:25:18 -07002922 } else
2923 regulator_enable(host->vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07002924
Philip Rakity68737042012-06-08 12:26:13 -07002925#ifdef CONFIG_REGULATOR
2926 if (host->vmmc) {
2927 ret = regulator_is_supported_voltage(host->vmmc, 3300000,
2928 3300000);
2929 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
2930 caps[0] &= ~SDHCI_CAN_VDD_330;
2931 ret = regulator_is_supported_voltage(host->vmmc, 3000000,
2932 3000000);
2933 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
2934 caps[0] &= ~SDHCI_CAN_VDD_300;
2935 ret = regulator_is_supported_voltage(host->vmmc, 1800000,
2936 1800000);
2937 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
2938 caps[0] &= ~SDHCI_CAN_VDD_180;
2939 }
2940#endif /* CONFIG_REGULATOR */
2941
Arindam Nathf2119df2011-05-05 12:18:57 +05302942 /*
2943 * According to SD Host Controller spec v3.00, if the Host System
2944 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2945 * the value is meaningful only if Voltage Support in the Capabilities
2946 * register is set. The actual current value is 4 times the register
2947 * value.
2948 */
2949 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07002950 if (!max_current_caps && host->vmmc) {
2951 u32 curr = regulator_get_current_limit(host->vmmc);
2952 if (curr > 0) {
2953
2954 /* convert to SDHCI_MAX_CURRENT format */
2955 curr = curr/1000; /* convert to mA */
2956 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
2957
2958 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
2959 max_current_caps =
2960 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
2961 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
2962 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
2963 }
2964 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302965
2966 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002967 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302968
Aaron Lu55c46652012-07-04 13:31:48 +08002969 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302970 SDHCI_MAX_CURRENT_330_MASK) >>
2971 SDHCI_MAX_CURRENT_330_SHIFT) *
2972 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302973 }
2974 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002975 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302976
Aaron Lu55c46652012-07-04 13:31:48 +08002977 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302978 SDHCI_MAX_CURRENT_300_MASK) >>
2979 SDHCI_MAX_CURRENT_300_SHIFT) *
2980 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302981 }
2982 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002983 ocr_avail |= MMC_VDD_165_195;
2984
Aaron Lu55c46652012-07-04 13:31:48 +08002985 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302986 SDHCI_MAX_CURRENT_180_MASK) >>
2987 SDHCI_MAX_CURRENT_180_SHIFT) *
2988 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302989 }
2990
Takashi Iwai8f230f42010-12-08 10:04:30 +01002991 mmc->ocr_avail = ocr_avail;
2992 mmc->ocr_avail_sdio = ocr_avail;
2993 if (host->ocr_avail_sdio)
2994 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2995 mmc->ocr_avail_sd = ocr_avail;
2996 if (host->ocr_avail_sd)
2997 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2998 else /* normal SD controllers don't support 1.8V */
2999 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3000 mmc->ocr_avail_mmc = ocr_avail;
3001 if (host->ocr_avail_mmc)
3002 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003003
3004 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303005 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003006 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003007 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003008 }
3009
Pierre Ossmand129bce2006-03-24 03:18:17 -08003010 spin_lock_init(&host->lock);
3011
3012 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003013 * Maximum number of segments. Depends on if the hardware
3014 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003015 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003016 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003017 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003018 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003019 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003020 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003021 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003022
3023 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003024 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003025 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003027 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003028
3029 /*
3030 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003031 * of bytes. When doing hardware scatter/gather, each entry cannot
3032 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003033 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003034 if (host->flags & SDHCI_USE_ADMA) {
3035 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3036 mmc->max_seg_size = 65535;
3037 else
3038 mmc->max_seg_size = 65536;
3039 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003040 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003041 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003042
3043 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003044 * Maximum block size. This varies from controller to controller and
3045 * is specified in the capabilities register.
3046 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003047 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3048 mmc->max_blk_size = 2;
3049 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303050 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003051 SDHCI_MAX_BLOCK_SHIFT;
3052 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303053 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003054 "assuming 512 bytes\n", mmc_hostname(mmc));
3055 mmc->max_blk_size = 0;
3056 }
3057 }
3058
3059 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003060
3061 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003062 * Maximum block count.
3063 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003064 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003065
3066 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003067 * Init tasklets.
3068 */
3069 tasklet_init(&host->card_tasklet,
3070 sdhci_tasklet_card, (unsigned long)host);
3071 tasklet_init(&host->finish_tasklet,
3072 sdhci_tasklet_finish, (unsigned long)host);
3073
Al Viroe4cad1b2006-10-10 22:47:07 +01003074 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003075
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303076 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303077 init_waitqueue_head(&host->buf_ready_int);
3078
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303079 /* Initialize re-tuning timer */
3080 init_timer(&host->tuning_timer);
3081 host->tuning_timer.data = (unsigned long)host;
3082 host->tuning_timer.function = sdhci_tuning_timer;
3083 }
3084
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003085 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003086 mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003087 if (ret) {
3088 pr_err("%s: Failed to request IRQ %d: %d\n",
3089 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003090 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003091 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003092
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003093 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003094
3095#ifdef CONFIG_MMC_DEBUG
3096 sdhci_dumpregs(host);
3097#endif
3098
Pierre Ossmanf9134312008-12-21 17:01:48 +01003099#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003100 snprintf(host->led_name, sizeof(host->led_name),
3101 "%s::", mmc_hostname(mmc));
3102 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003103 host->led.brightness = LED_OFF;
3104 host->led.default_trigger = mmc_hostname(mmc);
3105 host->led.brightness_set = sdhci_led_control;
3106
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003107 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003108 if (ret) {
3109 pr_err("%s: Failed to register LED device: %d\n",
3110 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003111 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003112 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003113#endif
3114
Pierre Ossman5f25a662006-10-04 02:15:39 -07003115 mmiowb();
3116
Pierre Ossmand129bce2006-03-24 03:18:17 -08003117 mmc_add_host(mmc);
3118
Girish K Sa3c76eb2011-10-11 11:44:09 +05303119 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003120 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003121 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3122 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003123
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003124 sdhci_enable_card_detection(host);
3125
Pierre Ossmand129bce2006-03-24 03:18:17 -08003126 return 0;
3127
Pierre Ossmanf9134312008-12-21 17:01:48 +01003128#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003129reset:
3130 sdhci_reset(host, SDHCI_RESET_ALL);
3131 free_irq(host->irq, host);
3132#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003133untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003134 tasklet_kill(&host->card_tasklet);
3135 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003136
3137 return ret;
3138}
3139
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003140EXPORT_SYMBOL_GPL(sdhci_add_host);
3141
Pierre Ossman1e728592008-04-16 19:13:13 +02003142void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003143{
Pierre Ossman1e728592008-04-16 19:13:13 +02003144 unsigned long flags;
3145
3146 if (dead) {
3147 spin_lock_irqsave(&host->lock, flags);
3148
3149 host->flags |= SDHCI_DEVICE_DEAD;
3150
3151 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303152 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003153 " transfer!\n", mmc_hostname(host->mmc));
3154
3155 host->mrq->cmd->error = -ENOMEDIUM;
3156 tasklet_schedule(&host->finish_tasklet);
3157 }
3158
3159 spin_unlock_irqrestore(&host->lock, flags);
3160 }
3161
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003162 sdhci_disable_card_detection(host);
3163
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003164 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003165
Pierre Ossmanf9134312008-12-21 17:01:48 +01003166#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003167 led_classdev_unregister(&host->led);
3168#endif
3169
Pierre Ossman1e728592008-04-16 19:13:13 +02003170 if (!dead)
3171 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003172
3173 free_irq(host->irq, host);
3174
3175 del_timer_sync(&host->timer);
3176
3177 tasklet_kill(&host->card_tasklet);
3178 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003179
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003180 if (host->vmmc) {
3181 regulator_disable(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003182 regulator_put(host->vmmc);
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003183 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003184
Philip Rakity6231f3d2012-07-23 15:56:23 -07003185 if (host->vqmmc) {
3186 regulator_disable(host->vqmmc);
3187 regulator_put(host->vqmmc);
3188 }
3189
Pierre Ossman2134a922008-06-28 18:28:51 +02003190 kfree(host->adma_desc);
3191 kfree(host->align_buffer);
3192
3193 host->adma_desc = NULL;
3194 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003195}
3196
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003197EXPORT_SYMBOL_GPL(sdhci_remove_host);
3198
3199void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003200{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003201 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003202}
3203
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003204EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003205
3206/*****************************************************************************\
3207 * *
3208 * Driver init/exit *
3209 * *
3210\*****************************************************************************/
3211
3212static int __init sdhci_drv_init(void)
3213{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303214 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003215 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303216 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003217
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003218 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003219}
3220
3221static void __exit sdhci_drv_exit(void)
3222{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003223}
3224
3225module_init(sdhci_drv_init);
3226module_exit(sdhci_drv_exit);
3227
Pierre Ossmandf673b22006-06-30 02:22:31 -07003228module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003229module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003230
Pierre Ossman32710e82009-04-08 20:14:54 +02003231MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003232MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003234
Pierre Ossmandf673b22006-06-30 02:22:31 -07003235MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003236MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");