blob: 9a11dc39921c06ebdf67a3ed602fbba79cc38b89 [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080031
Pierre Ossmand129bce2006-03-24 03:18:17 -080032#include "sdhci.h"
33
34#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmand129bce2006-03-24 03:18:17 -080036#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010037 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080038
Pierre Ossmanf9134312008-12-21 17:01:48 +010039#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
40 defined(CONFIG_MMC_SDHCI_MODULE))
41#define SDHCI_USE_LEDS_CLASS
42#endif
43
Arindam Nathb513ea22011-05-05 12:19:04 +053044#define MAX_TUNING_LOOP 40
45
Pierre Ossmandf673b22006-06-30 02:22:31 -070046static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030047static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070048
Pierre Ossmand129bce2006-03-24 03:18:17 -080049static void sdhci_finish_data(struct sdhci_host *);
50
51static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
52static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053053static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053054static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080055
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030056#ifdef CONFIG_PM_RUNTIME
57static int sdhci_runtime_pm_get(struct sdhci_host *host);
58static int sdhci_runtime_pm_put(struct sdhci_host *host);
59#else
60static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
61{
62 return 0;
63}
64static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
65{
66 return 0;
67}
68#endif
69
Pierre Ossmand129bce2006-03-24 03:18:17 -080070static void sdhci_dumpregs(struct sdhci_host *host)
71{
Girish K Sa3c76eb2011-10-11 11:44:09 +053072 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070073 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074
Girish K Sa3c76eb2011-10-11 11:44:09 +053075 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030076 sdhci_readl(host, SDHCI_DMA_ADDRESS),
77 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053078 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030079 sdhci_readw(host, SDHCI_BLOCK_SIZE),
80 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053081 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030082 sdhci_readl(host, SDHCI_ARGUMENT),
83 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053084 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_PRESENT_STATE),
86 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053087 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030088 sdhci_readb(host, SDHCI_POWER_CONTROL),
89 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053090 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030091 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
92 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053093 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030094 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
95 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053096 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030097 sdhci_readl(host, SDHCI_INT_ENABLE),
98 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053099 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300100 sdhci_readw(host, SDHCI_ACMD12_ERR),
101 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530102 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300103 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500104 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530105 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500106 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530108 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530109 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800110
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100111 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530112 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100113 readl(host->ioaddr + SDHCI_ADMA_ERROR),
114 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
115
Girish K Sa3c76eb2011-10-11 11:44:09 +0530116 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800117}
118
119/*****************************************************************************\
120 * *
121 * Low level functions *
122 * *
123\*****************************************************************************/
124
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300125static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
126{
127 u32 ier;
128
129 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
130 ier &= ~clear;
131 ier |= set;
132 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
133 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
134}
135
136static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
137{
138 sdhci_clear_set_irqs(host, 0, irqs);
139}
140
141static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
142{
143 sdhci_clear_set_irqs(host, irqs, 0);
144}
145
146static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
147{
Shawn Guod25928d2011-06-21 22:41:48 +0800148 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300149
Adrian Hunterc79396c2011-12-27 15:48:42 +0200150 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100151 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300152 return;
153
Shawn Guod25928d2011-06-21 22:41:48 +0800154 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
155 SDHCI_CARD_PRESENT;
156 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
157
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300158 if (enable)
159 sdhci_unmask_irqs(host, irqs);
160 else
161 sdhci_mask_irqs(host, irqs);
162}
163
164static void sdhci_enable_card_detection(struct sdhci_host *host)
165{
166 sdhci_set_card_detection(host, true);
167}
168
169static void sdhci_disable_card_detection(struct sdhci_host *host)
170{
171 sdhci_set_card_detection(host, false);
172}
173
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174static void sdhci_reset(struct sdhci_host *host, u8 mask)
175{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300177 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100179 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700181 SDHCI_CARD_PRESENT))
182 return;
183 }
184
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300185 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
186 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
187
Philip Rakity393c1a32011-01-21 11:26:40 -0800188 if (host->ops->platform_reset_enter)
189 host->ops->platform_reset_enter(host, mask);
190
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300191 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192
Pierre Ossmane16514d82006-06-30 02:22:24 -0700193 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 host->clock = 0;
195
Pierre Ossmane16514d82006-06-30 02:22:24 -0700196 /* Wait max 100 ms */
197 timeout = 100;
198
199 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300200 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700201 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530202 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700203 mmc_hostname(host->mmc), (int)mask);
204 sdhci_dumpregs(host);
205 return;
206 }
207 timeout--;
208 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800209 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300210
Philip Rakity393c1a32011-01-21 11:26:40 -0800211 if (host->ops->platform_reset_exit)
212 host->ops->platform_reset_exit(host, mask);
213
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300214 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
215 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800216
217 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
218 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
219 host->ops->enable_dma(host);
220 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221}
222
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
224
225static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800227 if (soft)
228 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
229 else
230 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800231
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300232 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
233 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700234 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
235 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300236 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800237
238 if (soft) {
239 /* force clock reconfiguration */
240 host->clock = 0;
241 sdhci_set_ios(host->mmc, &host->mmc->ios);
242 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300243}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300245static void sdhci_reinit(struct sdhci_host *host)
246{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800247 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800248 /*
249 * Retuning stuffs are affected by different cards inserted and only
250 * applicable to UHS-I cards. So reset these fields to their initial
251 * value when card is removed.
252 */
Aaron Lu973905f2012-07-04 13:29:09 +0800253 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
254 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
255
Aaron Lub67c6b42012-06-29 16:17:31 +0800256 del_timer_sync(&host->tuning_timer);
257 host->flags &= ~SDHCI_NEEDS_RETUNING;
258 host->mmc->max_blk_count =
259 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
260 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300261 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262}
263
264static void sdhci_activate_led(struct sdhci_host *host)
265{
266 u8 ctrl;
267
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300268 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271}
272
273static void sdhci_deactivate_led(struct sdhci_host *host)
274{
275 u8 ctrl;
276
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300277 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300279 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280}
281
Pierre Ossmanf9134312008-12-21 17:01:48 +0100282#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100283static void sdhci_led_control(struct led_classdev *led,
284 enum led_brightness brightness)
285{
286 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
287 unsigned long flags;
288
289 spin_lock_irqsave(&host->lock, flags);
290
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300291 if (host->runtime_suspended)
292 goto out;
293
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100294 if (brightness == LED_OFF)
295 sdhci_deactivate_led(host);
296 else
297 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300298out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100299 spin_unlock_irqrestore(&host->lock, flags);
300}
301#endif
302
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303/*****************************************************************************\
304 * *
305 * Core functions *
306 * *
307\*****************************************************************************/
308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310{
Pierre Ossman76591502008-07-21 00:32:11 +0200311 unsigned long flags;
312 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700313 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200314 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200319 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100323 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200324 if (!sg_miter_next(&host->sg_miter))
325 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800326
Pierre Ossman76591502008-07-21 00:32:11 +0200327 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 blksize -= len;
330 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 while (len) {
335 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300336 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200337 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800338 }
Pierre Ossman76591502008-07-21 00:32:11 +0200339
340 *buf = scratch & 0xFF;
341
342 buf++;
343 scratch >>= 8;
344 chunk--;
345 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800346 }
347 }
Pierre Ossman76591502008-07-21 00:32:11 +0200348
349 sg_miter_stop(&host->sg_miter);
350
351 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800353
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354static void sdhci_write_block_pio(struct sdhci_host *host)
355{
Pierre Ossman76591502008-07-21 00:32:11 +0200356 unsigned long flags;
357 size_t blksize, len, chunk;
358 u32 scratch;
359 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
361 DBG("PIO writing\n");
362
363 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200364 chunk = 0;
365 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
369 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200370 if (!sg_miter_next(&host->sg_miter))
371 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 blksize -= len;
376 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 while (len) {
381 scratch |= (u32)*buf << (chunk * 8);
382
383 buf++;
384 chunk++;
385 len--;
386
387 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300388 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200389 chunk = 0;
390 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100391 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392 }
393 }
Pierre Ossman76591502008-07-21 00:32:11 +0200394
395 sg_miter_stop(&host->sg_miter);
396
397 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398}
399
400static void sdhci_transfer_pio(struct sdhci_host *host)
401{
402 u32 mask;
403
404 BUG_ON(!host->data);
405
Pierre Ossman76591502008-07-21 00:32:11 +0200406 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407 return;
408
409 if (host->data->flags & MMC_DATA_READ)
410 mask = SDHCI_DATA_AVAILABLE;
411 else
412 mask = SDHCI_SPACE_AVAILABLE;
413
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200414 /*
415 * Some controllers (JMicron JMB38x) mess up the buffer bits
416 * for transfers < 4 bytes. As long as it is just one block,
417 * we can ignore the bits.
418 */
419 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
420 (host->data->blocks == 1))
421 mask = ~0;
422
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300423 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300424 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
425 udelay(100);
426
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100427 if (host->data->flags & MMC_DATA_READ)
428 sdhci_read_block_pio(host);
429 else
430 sdhci_write_block_pio(host);
431
Pierre Ossman76591502008-07-21 00:32:11 +0200432 host->blocks--;
433 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100434 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435 }
436
437 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800438}
439
Pierre Ossman2134a922008-06-28 18:28:51 +0200440static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
441{
442 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800443 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200444}
445
446static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
447{
Cong Wang482fce92011-11-27 13:27:00 +0800448 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200449 local_irq_restore(*flags);
450}
451
Ben Dooks118cd172010-03-05 13:43:26 -0800452static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
453{
Ben Dooks9e506f32010-03-05 13:43:29 -0800454 __le32 *dataddr = (__le32 __force *)(desc + 4);
455 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800456
Ben Dooks9e506f32010-03-05 13:43:29 -0800457 /* SDHCI specification says ADMA descriptors should be 4 byte
458 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800459
Ben Dooks9e506f32010-03-05 13:43:29 -0800460 cmdlen[0] = cpu_to_le16(cmd);
461 cmdlen[1] = cpu_to_le16(len);
462
463 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800464}
465
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200466static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200467 struct mmc_data *data)
468{
469 int direction;
470
471 u8 *desc;
472 u8 *align;
473 dma_addr_t addr;
474 dma_addr_t align_addr;
475 int len, offset;
476
477 struct scatterlist *sg;
478 int i;
479 char *buffer;
480 unsigned long flags;
481
482 /*
483 * The spec does not specify endianness of descriptor table.
484 * We currently guess that it is LE.
485 */
486
487 if (data->flags & MMC_DATA_READ)
488 direction = DMA_FROM_DEVICE;
489 else
490 direction = DMA_TO_DEVICE;
491
492 /*
493 * The ADMA descriptor table is mapped further down as we
494 * need to fill it with data first.
495 */
496
497 host->align_addr = dma_map_single(mmc_dev(host->mmc),
498 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700499 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200500 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 BUG_ON(host->align_addr & 0x3);
502
503 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
504 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200505 if (host->sg_count == 0)
506 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200507
508 desc = host->adma_desc;
509 align = host->align_buffer;
510
511 align_addr = host->align_addr;
512
513 for_each_sg(data->sg, sg, host->sg_count, i) {
514 addr = sg_dma_address(sg);
515 len = sg_dma_len(sg);
516
517 /*
518 * The SDHCI specification states that ADMA
519 * addresses must be 32-bit aligned. If they
520 * aren't, then we use a bounce buffer for
521 * the (up to three) bytes that screw up the
522 * alignment.
523 */
524 offset = (4 - (addr & 0x3)) & 0x3;
525 if (offset) {
526 if (data->flags & MMC_DATA_WRITE) {
527 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200528 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200529 memcpy(align, buffer, offset);
530 sdhci_kunmap_atomic(buffer, &flags);
531 }
532
Ben Dooks118cd172010-03-05 13:43:26 -0800533 /* tran, valid */
534 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200535
536 BUG_ON(offset > 65536);
537
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 align += 4;
539 align_addr += 4;
540
541 desc += 8;
542
543 addr += offset;
544 len -= offset;
545 }
546
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 BUG_ON(len > 65536);
548
Ben Dooks118cd172010-03-05 13:43:26 -0800549 /* tran, valid */
550 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200551 desc += 8;
552
553 /*
554 * If this triggers then we have a calculation bug
555 * somewhere. :/
556 */
557 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
558 }
559
Thomas Abraham70764a92010-05-26 14:42:04 -0700560 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
561 /*
562 * Mark the last descriptor as the terminating descriptor
563 */
564 if (desc != host->adma_desc) {
565 desc -= 8;
566 desc[0] |= 0x2; /* end */
567 }
568 } else {
569 /*
570 * Add a terminating entry.
571 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200572
Thomas Abraham70764a92010-05-26 14:42:04 -0700573 /* nop, end, valid */
574 sdhci_set_adma_desc(desc, 0, 0, 0x3);
575 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200576
577 /*
578 * Resync align buffer as we might have changed it.
579 */
580 if (data->flags & MMC_DATA_WRITE) {
581 dma_sync_single_for_device(mmc_dev(host->mmc),
582 host->align_addr, 128 * 4, direction);
583 }
584
585 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
586 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200587 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200588 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200590
591 return 0;
592
593unmap_entries:
594 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
595 data->sg_len, direction);
596unmap_align:
597 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
598 128 * 4, direction);
599fail:
600 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200601}
602
603static void sdhci_adma_table_post(struct sdhci_host *host,
604 struct mmc_data *data)
605{
606 int direction;
607
608 struct scatterlist *sg;
609 int i, size;
610 u8 *align;
611 char *buffer;
612 unsigned long flags;
613
614 if (data->flags & MMC_DATA_READ)
615 direction = DMA_FROM_DEVICE;
616 else
617 direction = DMA_TO_DEVICE;
618
619 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
620 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
621
622 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
623 128 * 4, direction);
624
625 if (data->flags & MMC_DATA_READ) {
626 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
627 data->sg_len, direction);
628
629 align = host->align_buffer;
630
631 for_each_sg(data->sg, sg, host->sg_count, i) {
632 if (sg_dma_address(sg) & 0x3) {
633 size = 4 - (sg_dma_address(sg) & 0x3);
634
635 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200636 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 memcpy(buffer, align, size);
638 sdhci_kunmap_atomic(buffer, &flags);
639
640 align += 4;
641 }
642 }
643 }
644
645 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
646 data->sg_len, direction);
647}
648
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700651 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700653 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800654
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200655 /*
656 * If the host controller provides us with an incorrect timeout
657 * value, just skip the check and use 0xE. The hardware may take
658 * longer to time out, but that's much better than having a too-short
659 * timeout value.
660 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200661 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200662 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200663
Andrei Warkentina3c77782011-04-11 16:13:42 -0500664 /* Unspecified timeout, assume max */
665 if (!data && !cmd->cmd_timeout_ms)
666 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800667
Andrei Warkentina3c77782011-04-11 16:13:42 -0500668 /* timeout in us */
669 if (!data)
670 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300671 else {
672 target_timeout = data->timeout_ns / 1000;
673 if (host->clock)
674 target_timeout += data->timeout_clks / host->clock;
675 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700676
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700677 /*
678 * Figure out needed cycles.
679 * We do this in steps in order to fit inside a 32 bit int.
680 * The first step is the minimum timeout, which will have a
681 * minimum resolution of 6 bits:
682 * (1) 2^13*1000 > 2^22,
683 * (2) host->timeout_clk < 2^16
684 * =>
685 * (1) / (2) > 2^6
686 */
687 count = 0;
688 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
689 while (current_timeout < target_timeout) {
690 count++;
691 current_timeout <<= 1;
692 if (count >= 0xF)
693 break;
694 }
695
696 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400697 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
698 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700699 count = 0xE;
700 }
701
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200702 return count;
703}
704
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300705static void sdhci_set_transfer_irqs(struct sdhci_host *host)
706{
707 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
708 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
709
710 if (host->flags & SDHCI_REQ_USE_DMA)
711 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
712 else
713 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
714}
715
Andrei Warkentina3c77782011-04-11 16:13:42 -0500716static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200717{
718 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200719 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500720 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200721 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200722
723 WARN_ON(host->data);
724
Andrei Warkentina3c77782011-04-11 16:13:42 -0500725 if (data || (cmd->flags & MMC_RSP_BUSY)) {
726 count = sdhci_calc_timeout(host, cmd);
727 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
728 }
729
730 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200731 return;
732
733 /* Sanity checks */
734 BUG_ON(data->blksz * data->blocks > 524288);
735 BUG_ON(data->blksz > host->mmc->max_blk_size);
736 BUG_ON(data->blocks > 65535);
737
738 host->data = data;
739 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400740 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200741
Richard Röjforsa13abc72009-09-22 16:45:30 -0700742 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100743 host->flags |= SDHCI_REQ_USE_DMA;
744
Pierre Ossman2134a922008-06-28 18:28:51 +0200745 /*
746 * FIXME: This doesn't account for merging when mapping the
747 * scatterlist.
748 */
749 if (host->flags & SDHCI_REQ_USE_DMA) {
750 int broken, i;
751 struct scatterlist *sg;
752
753 broken = 0;
754 if (host->flags & SDHCI_USE_ADMA) {
755 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
756 broken = 1;
757 } else {
758 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
759 broken = 1;
760 }
761
762 if (unlikely(broken)) {
763 for_each_sg(data->sg, sg, data->sg_len, i) {
764 if (sg->length & 0x3) {
765 DBG("Reverting to PIO because of "
766 "transfer size (%d)\n",
767 sg->length);
768 host->flags &= ~SDHCI_REQ_USE_DMA;
769 break;
770 }
771 }
772 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100773 }
774
775 /*
776 * The assumption here being that alignment is the same after
777 * translation to device address space.
778 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200779 if (host->flags & SDHCI_REQ_USE_DMA) {
780 int broken, i;
781 struct scatterlist *sg;
782
783 broken = 0;
784 if (host->flags & SDHCI_USE_ADMA) {
785 /*
786 * As we use 3 byte chunks to work around
787 * alignment problems, we need to check this
788 * quirk.
789 */
790 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
791 broken = 1;
792 } else {
793 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
794 broken = 1;
795 }
796
797 if (unlikely(broken)) {
798 for_each_sg(data->sg, sg, data->sg_len, i) {
799 if (sg->offset & 0x3) {
800 DBG("Reverting to PIO because of "
801 "bad alignment\n");
802 host->flags &= ~SDHCI_REQ_USE_DMA;
803 break;
804 }
805 }
806 }
807 }
808
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200809 if (host->flags & SDHCI_REQ_USE_DMA) {
810 if (host->flags & SDHCI_USE_ADMA) {
811 ret = sdhci_adma_table_pre(host, data);
812 if (ret) {
813 /*
814 * This only happens when someone fed
815 * us an invalid request.
816 */
817 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200818 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300820 sdhci_writel(host, host->adma_addr,
821 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200822 }
823 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300824 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200825
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300826 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200827 data->sg, data->sg_len,
828 (data->flags & MMC_DATA_READ) ?
829 DMA_FROM_DEVICE :
830 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300831 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200832 /*
833 * This only happens when someone fed
834 * us an invalid request.
835 */
836 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200837 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200838 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200839 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300840 sdhci_writel(host, sg_dma_address(data->sg),
841 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200842 }
843 }
844 }
845
Pierre Ossman2134a922008-06-28 18:28:51 +0200846 /*
847 * Always adjust the DMA selection as some controllers
848 * (e.g. JMicron) can't do PIO properly when the selection
849 * is ADMA.
850 */
851 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300852 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200853 ctrl &= ~SDHCI_CTRL_DMA_MASK;
854 if ((host->flags & SDHCI_REQ_USE_DMA) &&
855 (host->flags & SDHCI_USE_ADMA))
856 ctrl |= SDHCI_CTRL_ADMA32;
857 else
858 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100860 }
861
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200862 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200863 int flags;
864
865 flags = SG_MITER_ATOMIC;
866 if (host->data->flags & MMC_DATA_READ)
867 flags |= SG_MITER_TO_SG;
868 else
869 flags |= SG_MITER_FROM_SG;
870 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200871 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700873
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300874 sdhci_set_transfer_irqs(host);
875
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400876 /* Set the DMA boundary value and block size */
877 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
878 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300879 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880}
881
882static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500883 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884{
885 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700888 if (data == NULL)
889 return;
890
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200891 WARN_ON(!host->data);
892
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700893 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500894 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
895 mode |= SDHCI_TRNS_MULTI;
896 /*
897 * If we are sending CMD23, CMD12 never gets sent
898 * on successful completion (so no Auto-CMD12).
899 */
900 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
901 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500902 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
903 mode |= SDHCI_TRNS_AUTO_CMD23;
904 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
905 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700906 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500907
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700908 if (data->flags & MMC_DATA_READ)
909 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100910 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700911 mode |= SDHCI_TRNS_DMA;
912
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300913 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914}
915
916static void sdhci_finish_data(struct sdhci_host *host)
917{
918 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800919
920 BUG_ON(!host->data);
921
922 data = host->data;
923 host->data = NULL;
924
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100925 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200926 if (host->flags & SDHCI_USE_ADMA)
927 sdhci_adma_table_post(host, data);
928 else {
929 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
930 data->sg_len, (data->flags & MMC_DATA_READ) ?
931 DMA_FROM_DEVICE : DMA_TO_DEVICE);
932 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933 }
934
935 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200936 * The specification states that the block count register must
937 * be updated, but it does not specify at what point in the
938 * data flow. That makes the register entirely useless to read
939 * back so we have to assume that nothing made it to the card
940 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200942 if (data->error)
943 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200945 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946
Andrei Warkentine89d4562011-05-23 15:06:37 -0500947 /*
948 * Need to send CMD12 if -
949 * a) open-ended multiblock transfer (no CMD23)
950 * b) error in multiblock transfer
951 */
952 if (data->stop &&
953 (data->error ||
954 !host->mrq->sbc)) {
955
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956 /*
957 * The controller needs a reset of internal state machines
958 * upon error conditions.
959 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200960 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 sdhci_reset(host, SDHCI_RESET_CMD);
962 sdhci_reset(host, SDHCI_RESET_DATA);
963 }
964
965 sdhci_send_command(host, data->stop);
966 } else
967 tasklet_schedule(&host->finish_tasklet);
968}
969
970static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
971{
972 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700973 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700974 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975
976 WARN_ON(host->cmd);
977
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700979 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700980
981 mask = SDHCI_CMD_INHIBIT;
982 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
983 mask |= SDHCI_DATA_INHIBIT;
984
985 /* We shouldn't wait for data inihibit for stop commands, even
986 though they might use busy signaling */
987 if (host->mrq->data && (cmd == host->mrq->data->stop))
988 mask &= ~SDHCI_DATA_INHIBIT;
989
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300990 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700991 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530992 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100993 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200995 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 tasklet_schedule(&host->finish_tasklet);
997 return;
998 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700999 timeout--;
1000 mdelay(1);
1001 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002
1003 mod_timer(&host->timer, jiffies + 10 * HZ);
1004
1005 host->cmd = cmd;
1006
Andrei Warkentina3c77782011-04-11 16:13:42 -05001007 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001009 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
Andrei Warkentine89d4562011-05-23 15:06:37 -05001011 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001012
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301014 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001016 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 tasklet_schedule(&host->finish_tasklet);
1018 return;
1019 }
1020
1021 if (!(cmd->flags & MMC_RSP_PRESENT))
1022 flags = SDHCI_CMD_RESP_NONE;
1023 else if (cmd->flags & MMC_RSP_136)
1024 flags = SDHCI_CMD_RESP_LONG;
1025 else if (cmd->flags & MMC_RSP_BUSY)
1026 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1027 else
1028 flags = SDHCI_CMD_RESP_SHORT;
1029
1030 if (cmd->flags & MMC_RSP_CRC)
1031 flags |= SDHCI_CMD_CRC;
1032 if (cmd->flags & MMC_RSP_OPCODE)
1033 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301034
1035 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301036 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1037 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 flags |= SDHCI_CMD_DATA;
1039
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001040 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041}
1042
1043static void sdhci_finish_command(struct sdhci_host *host)
1044{
1045 int i;
1046
1047 BUG_ON(host->cmd == NULL);
1048
1049 if (host->cmd->flags & MMC_RSP_PRESENT) {
1050 if (host->cmd->flags & MMC_RSP_136) {
1051 /* CRC is stripped so we need to do some shifting. */
1052 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001053 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 SDHCI_RESPONSE + (3-i)*4) << 8;
1055 if (i != 3)
1056 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058 SDHCI_RESPONSE + (3-i)*4-1);
1059 }
1060 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001061 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 }
1063 }
1064
Pierre Ossman17b04292007-07-22 22:18:46 +02001065 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066
Andrei Warkentine89d4562011-05-23 15:06:37 -05001067 /* Finished CMD23, now send actual command. */
1068 if (host->cmd == host->mrq->sbc) {
1069 host->cmd = NULL;
1070 sdhci_send_command(host, host->mrq->cmd);
1071 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001072
Andrei Warkentine89d4562011-05-23 15:06:37 -05001073 /* Processed actual command. */
1074 if (host->data && host->data_early)
1075 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076
Andrei Warkentine89d4562011-05-23 15:06:37 -05001077 if (!host->cmd->data)
1078 tasklet_schedule(&host->finish_tasklet);
1079
1080 host->cmd = NULL;
1081 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082}
1083
1084static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1085{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301086 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001087 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301088 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001089 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090
Todd Poynor30832ab2011-12-27 15:48:46 +02001091 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092 return;
1093
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001094 host->mmc->actual_clock = 0;
1095
Anton Vorontsov81146342009-03-17 00:13:59 +03001096 if (host->ops->set_clock) {
1097 host->ops->set_clock(host, clock);
1098 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1099 return;
1100 }
1101
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001102 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103
1104 if (clock == 0)
1105 goto out;
1106
Zhangfei Gao85105c52010-08-06 07:10:01 +08001107 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301108 /*
1109 * Check if the Host Controller supports Programmable Clock
1110 * Mode.
1111 */
1112 if (host->clk_mul) {
1113 u16 ctrl;
1114
1115 /*
1116 * We need to figure out whether the Host Driver needs
1117 * to select Programmable Clock Mode, or the value can
1118 * be set automatically by the Host Controller based on
1119 * the Preset Value registers.
1120 */
1121 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1122 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1123 for (div = 1; div <= 1024; div++) {
1124 if (((host->max_clk * host->clk_mul) /
1125 div) <= clock)
1126 break;
1127 }
1128 /*
1129 * Set Programmable Clock Mode in the Clock
1130 * Control register.
1131 */
1132 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001133 real_div = div;
1134 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301135 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001136 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301137 } else {
1138 /* Version 3.00 divisors must be a multiple of 2. */
1139 if (host->max_clk <= clock)
1140 div = 1;
1141 else {
1142 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1143 div += 2) {
1144 if ((host->max_clk / div) <= clock)
1145 break;
1146 }
1147 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001148 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301149 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001150 }
1151 } else {
1152 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001153 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001154 if ((host->max_clk / div) <= clock)
1155 break;
1156 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001157 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301158 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001160
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001161 if (real_div)
1162 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1163
Arindam Nathc3ed3872011-05-05 12:19:06 +05301164 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001165 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1166 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001168 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
Chris Ball27f6cb12009-09-22 16:45:31 -07001170 /* Wait max 20 ms */
1171 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001172 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001173 & SDHCI_CLOCK_INT_STABLE)) {
1174 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301175 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001176 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001177 sdhci_dumpregs(host);
1178 return;
1179 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001180 timeout--;
1181 mdelay(1);
1182 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001183
1184 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001185 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186
1187out:
1188 host->clock = clock;
1189}
1190
Adrian Hunterceb61432011-12-27 15:48:41 +02001191static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001192{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001193 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001194
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001195 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001196 switch (1 << power) {
1197 case MMC_VDD_165_195:
1198 pwr = SDHCI_POWER_180;
1199 break;
1200 case MMC_VDD_29_30:
1201 case MMC_VDD_30_31:
1202 pwr = SDHCI_POWER_300;
1203 break;
1204 case MMC_VDD_32_33:
1205 case MMC_VDD_33_34:
1206 pwr = SDHCI_POWER_330;
1207 break;
1208 default:
1209 BUG();
1210 }
1211 }
1212
1213 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001214 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001215
Pierre Ossmanae628902009-05-03 20:45:03 +02001216 host->pwr = pwr;
1217
1218 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001220 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001221 }
1222
1223 /*
1224 * Spec says that we should clear the power reg before setting
1225 * a new value. Some controllers don't seem to like this though.
1226 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001227 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001228 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001229
Andres Salomone08c1692008-07-04 10:00:03 -07001230 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001231 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001232 * and set turn on power at the same time, so set the voltage first.
1233 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001234 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001235 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1236
1237 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001238
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001239 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001240
1241 /*
1242 * Some controllers need an extra 10ms delay of 10ms before they
1243 * can apply clock after applying power
1244 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001245 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001246 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001247
1248 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001249}
1250
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251/*****************************************************************************\
1252 * *
1253 * MMC callbacks *
1254 * *
1255\*****************************************************************************/
1256
1257static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1258{
1259 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001260 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001262 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263
1264 host = mmc_priv(mmc);
1265
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001266 sdhci_runtime_pm_get(host);
1267
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268 spin_lock_irqsave(&host->lock, flags);
1269
1270 WARN_ON(host->mrq != NULL);
1271
Pierre Ossmanf9134312008-12-21 17:01:48 +01001272#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001274#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001275
1276 /*
1277 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1278 * requests if Auto-CMD12 is enabled.
1279 */
1280 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001281 if (mrq->stop) {
1282 mrq->data->stop = NULL;
1283 mrq->stop = NULL;
1284 }
1285 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001286
1287 host->mrq = mrq;
1288
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001289 /* If polling, assume that the card is always present. */
1290 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1291 present = true;
1292 else
1293 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1294 SDHCI_CARD_PRESENT;
1295
1296 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001297 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001298 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301299 } else {
1300 u32 present_state;
1301
1302 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1303 /*
1304 * Check if the re-tuning timer has already expired and there
1305 * is no on-going data transfer. If so, we need to execute
1306 * tuning procedure before sending command.
1307 */
1308 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1309 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Aaron Lu473b0952012-07-03 17:27:49 +08001310 /* eMMC uses cmd21 while sd and sdio use cmd19 */
1311 tuning_opcode = mmc->card->type == MMC_TYPE_MMC ?
1312 MMC_SEND_TUNING_BLOCK_HS200 :
1313 MMC_SEND_TUNING_BLOCK;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301314 spin_unlock_irqrestore(&host->lock, flags);
Aaron Lu473b0952012-07-03 17:27:49 +08001315 sdhci_execute_tuning(mmc, tuning_opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301316 spin_lock_irqsave(&host->lock, flags);
1317
1318 /* Restore original mmc_request structure */
1319 host->mrq = mrq;
1320 }
1321
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001322 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001323 sdhci_send_command(host, mrq->sbc);
1324 else
1325 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301326 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
Pierre Ossman5f25a662006-10-04 02:15:39 -07001328 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 spin_unlock_irqrestore(&host->lock, flags);
1330}
1331
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001332static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001335 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 u8 ctrl;
1337
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338 spin_lock_irqsave(&host->lock, flags);
1339
Adrian Hunterceb61432011-12-27 15:48:41 +02001340 if (host->flags & SDHCI_DEVICE_DEAD) {
1341 spin_unlock_irqrestore(&host->lock, flags);
1342 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1343 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1344 return;
1345 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001346
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347 /*
1348 * Reset the chip on each power off.
1349 * Should clear out any weird states.
1350 */
1351 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001352 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001353 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001354 }
1355
1356 sdhci_set_clock(host, ios->clock);
1357
1358 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001359 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001360 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001361 vdd_bit = sdhci_set_power(host, ios->vdd);
1362
1363 if (host->vmmc && vdd_bit != -1) {
1364 spin_unlock_irqrestore(&host->lock, flags);
1365 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1366 spin_lock_irqsave(&host->lock, flags);
1367 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368
Philip Rakity643a81f2010-09-23 08:24:32 -07001369 if (host->ops->platform_send_init_74_clocks)
1370 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1371
Philip Rakity15ec4462010-11-19 16:48:39 -05001372 /*
1373 * If your platform has 8-bit width support but is not a v3 controller,
1374 * or if it requires special setup code, you should implement that in
1375 * platform_8bit_width().
1376 */
1377 if (host->ops->platform_8bit_width)
1378 host->ops->platform_8bit_width(host, ios->bus_width);
1379 else {
1380 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1381 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1382 ctrl &= ~SDHCI_CTRL_4BITBUS;
1383 if (host->version >= SDHCI_SPEC_300)
1384 ctrl |= SDHCI_CTRL_8BITBUS;
1385 } else {
1386 if (host->version >= SDHCI_SPEC_300)
1387 ctrl &= ~SDHCI_CTRL_8BITBUS;
1388 if (ios->bus_width == MMC_BUS_WIDTH_4)
1389 ctrl |= SDHCI_CTRL_4BITBUS;
1390 else
1391 ctrl &= ~SDHCI_CTRL_4BITBUS;
1392 }
1393 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1394 }
1395
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001396 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001397
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001398 if ((ios->timing == MMC_TIMING_SD_HS ||
1399 ios->timing == MMC_TIMING_MMC_HS)
1400 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001401 ctrl |= SDHCI_CTRL_HISPD;
1402 else
1403 ctrl &= ~SDHCI_CTRL_HISPD;
1404
Arindam Nathd6d50a12011-05-05 12:18:59 +05301405 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301406 u16 clk, ctrl_2;
1407 unsigned int clock;
1408
1409 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301410 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1411 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301412 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1413 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001414 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301415 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301416
1417 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1418 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301419 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301420 /*
1421 * We only need to set Driver Strength if the
1422 * preset value enable is not set.
1423 */
1424 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1425 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1426 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1427 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1428 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1429
1430 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301431 } else {
1432 /*
1433 * According to SDHC Spec v3.00, if the Preset Value
1434 * Enable in the Host Control 2 register is set, we
1435 * need to reset SD Clock Enable before changing High
1436 * Speed Enable to avoid generating clock gliches.
1437 */
Arindam Nath758535c2011-05-05 12:19:00 +05301438
1439 /* Reset SD Clock Enable */
1440 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1441 clk &= ~SDHCI_CLOCK_CARD_EN;
1442 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1443
1444 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1445
1446 /* Re-enable SD Clock */
1447 clock = host->clock;
1448 host->clock = 0;
1449 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301450 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301451
Arindam Nath49c468f2011-05-05 12:19:01 +05301452
1453 /* Reset SD Clock Enable */
1454 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1455 clk &= ~SDHCI_CLOCK_CARD_EN;
1456 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1457
Philip Rakity6322cdd2011-05-13 11:17:15 +05301458 if (host->ops->set_uhs_signaling)
1459 host->ops->set_uhs_signaling(host, ios->timing);
1460 else {
1461 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1462 /* Select Bus Speed Mode for host */
1463 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301464 if (ios->timing == MMC_TIMING_MMC_HS200)
1465 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1466 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301467 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1468 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1469 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1470 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1471 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1472 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1473 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1474 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1475 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1476 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1477 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301478
1479 /* Re-enable SD Clock */
1480 clock = host->clock;
1481 host->clock = 0;
1482 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301483 } else
1484 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301485
Leandro Dorileob8352262007-07-25 23:47:04 +02001486 /*
1487 * Some (ENE) controllers go apeshit on some ios operation,
1488 * signalling timeout and CRC errors even on CMD0. Resetting
1489 * it on each ios seems to solve the problem.
1490 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001491 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001492 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1493
Pierre Ossman5f25a662006-10-04 02:15:39 -07001494 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 spin_unlock_irqrestore(&host->lock, flags);
1496}
1497
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001498static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1499{
1500 struct sdhci_host *host = mmc_priv(mmc);
1501
1502 sdhci_runtime_pm_get(host);
1503 sdhci_do_set_ios(host, ios);
1504 sdhci_runtime_pm_put(host);
1505}
1506
1507static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001510 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512 spin_lock_irqsave(&host->lock, flags);
1513
Pierre Ossman1e728592008-04-16 19:13:13 +02001514 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001515 is_readonly = 0;
1516 else if (host->ops->get_ro)
1517 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001518 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001519 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1520 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001521
1522 spin_unlock_irqrestore(&host->lock, flags);
1523
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001524 /* This quirk needs to be replaced by a callback-function later */
1525 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1526 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001527}
1528
Takashi Iwai82b0e232011-04-21 20:26:38 +02001529#define SAMPLE_COUNT 5
1530
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001531static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001532{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001533 int i, ro_count;
1534
Takashi Iwai82b0e232011-04-21 20:26:38 +02001535 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001536 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001537
1538 ro_count = 0;
1539 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001540 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001541 if (++ro_count > SAMPLE_COUNT / 2)
1542 return 1;
1543 }
1544 msleep(30);
1545 }
1546 return 0;
1547}
1548
Adrian Hunter20758b62011-08-29 16:42:12 +03001549static void sdhci_hw_reset(struct mmc_host *mmc)
1550{
1551 struct sdhci_host *host = mmc_priv(mmc);
1552
1553 if (host->ops && host->ops->hw_reset)
1554 host->ops->hw_reset(host);
1555}
1556
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001557static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001558{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001559 struct sdhci_host *host = mmc_priv(mmc);
1560 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001561
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001562 sdhci_runtime_pm_get(host);
1563 ret = sdhci_do_get_ro(host);
1564 sdhci_runtime_pm_put(host);
1565 return ret;
1566}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001567
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001568static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1569{
Pierre Ossman1e728592008-04-16 19:13:13 +02001570 if (host->flags & SDHCI_DEVICE_DEAD)
1571 goto out;
1572
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001573 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001574 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1575 else
1576 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1577
1578 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1579 if (host->runtime_suspended)
1580 goto out;
1581
1582 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001583 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1584 else
1585 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001586out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001587 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001588}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001589
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001590static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1591{
1592 struct sdhci_host *host = mmc_priv(mmc);
1593 unsigned long flags;
1594
1595 spin_lock_irqsave(&host->lock, flags);
1596 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001597 spin_unlock_irqrestore(&host->lock, flags);
1598}
1599
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001600static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1601 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301602{
Arindam Nathf2119df2011-05-05 12:18:57 +05301603 u8 pwr;
1604 u16 clk, ctrl;
1605 u32 present_state;
1606
Arindam Nathf2119df2011-05-05 12:18:57 +05301607 /*
1608 * Signal Voltage Switching is only applicable for Host Controllers
1609 * v3.00 and above.
1610 */
1611 if (host->version < SDHCI_SPEC_300)
1612 return 0;
1613
1614 /*
1615 * We first check whether the request is to set signalling voltage
1616 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1617 */
1618 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1619 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1620 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1621 ctrl &= ~SDHCI_CTRL_VDD_180;
1622 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1623
1624 /* Wait for 5ms */
1625 usleep_range(5000, 5500);
1626
1627 /* 3.3V regulator output should be stable within 5 ms */
1628 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1629 if (!(ctrl & SDHCI_CTRL_VDD_180))
1630 return 0;
1631 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301632 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301633 "signalling voltage failed\n");
1634 return -EIO;
1635 }
1636 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1637 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1638 /* Stop SDCLK */
1639 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1640 clk &= ~SDHCI_CLOCK_CARD_EN;
1641 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1642
1643 /* Check whether DAT[3:0] is 0000 */
1644 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1645 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1646 SDHCI_DATA_LVL_SHIFT)) {
1647 /*
1648 * Enable 1.8V Signal Enable in the Host Control2
1649 * register
1650 */
1651 ctrl |= SDHCI_CTRL_VDD_180;
1652 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1653
1654 /* Wait for 5ms */
1655 usleep_range(5000, 5500);
1656
1657 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1658 if (ctrl & SDHCI_CTRL_VDD_180) {
1659 /* Provide SDCLK again and wait for 1ms*/
1660 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1661 clk |= SDHCI_CLOCK_CARD_EN;
1662 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1663 usleep_range(1000, 1500);
1664
1665 /*
1666 * If DAT[3:0] level is 1111b, then the card
1667 * was successfully switched to 1.8V signaling.
1668 */
1669 present_state = sdhci_readl(host,
1670 SDHCI_PRESENT_STATE);
1671 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1672 SDHCI_DATA_LVL_MASK)
1673 return 0;
1674 }
1675 }
1676
1677 /*
1678 * If we are here, that means the switch to 1.8V signaling
1679 * failed. We power cycle the card, and retry initialization
1680 * sequence by setting S18R to 0.
1681 */
1682 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1683 pwr &= ~SDHCI_POWER_ON;
1684 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Philip Rakity38cfc2f2012-06-27 09:15:00 -07001685 if (host->vmmc)
1686 regulator_disable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301687
1688 /* Wait for 1ms as per the spec */
1689 usleep_range(1000, 1500);
1690 pwr |= SDHCI_POWER_ON;
1691 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Philip Rakity38cfc2f2012-06-27 09:15:00 -07001692 if (host->vmmc)
1693 regulator_enable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301694
Girish K Sa3c76eb2011-10-11 11:44:09 +05301695 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301696 "voltage failed, retrying with S18R set to 0\n");
1697 return -EAGAIN;
1698 } else
1699 /* No signal voltage switch required */
1700 return 0;
1701}
1702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1704 struct mmc_ios *ios)
1705{
1706 struct sdhci_host *host = mmc_priv(mmc);
1707 int err;
1708
1709 if (host->version < SDHCI_SPEC_300)
1710 return 0;
1711 sdhci_runtime_pm_get(host);
1712 err = sdhci_do_start_signal_voltage_switch(host, ios);
1713 sdhci_runtime_pm_put(host);
1714 return err;
1715}
1716
Girish K S069c9f12012-01-06 09:56:39 +05301717static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301718{
1719 struct sdhci_host *host;
1720 u16 ctrl;
1721 u32 ier;
1722 int tuning_loop_counter = MAX_TUNING_LOOP;
1723 unsigned long timeout;
1724 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301725 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301726
1727 host = mmc_priv(mmc);
1728
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001729 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301730 disable_irq(host->irq);
1731 spin_lock(&host->lock);
1732
1733 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1734
1735 /*
Girish K S069c9f12012-01-06 09:56:39 +05301736 * The Host Controller needs tuning only in case of SDR104 mode
1737 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301738 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301739 * If the Host Controller supports the HS200 mode then the
1740 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301741 */
Girish K S069c9f12012-01-06 09:56:39 +05301742 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1743 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1744 host->flags & SDHCI_HS200_NEEDS_TUNING))
1745 requires_tuning_nonuhs = true;
1746
Arindam Nathb513ea22011-05-05 12:19:04 +05301747 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301748 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301749 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1750 else {
1751 spin_unlock(&host->lock);
1752 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001753 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301754 return 0;
1755 }
1756
1757 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1758
1759 /*
1760 * As per the Host Controller spec v3.00, tuning command
1761 * generates Buffer Read Ready interrupt, so enable that.
1762 *
1763 * Note: The spec clearly says that when tuning sequence
1764 * is being performed, the controller does not generate
1765 * interrupts other than Buffer Read Ready interrupt. But
1766 * to make sure we don't hit a controller bug, we _only_
1767 * enable Buffer Read Ready interrupt here.
1768 */
1769 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1770 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1771
1772 /*
1773 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1774 * of loops reaches 40 times or a timeout of 150ms occurs.
1775 */
1776 timeout = 150;
1777 do {
1778 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001779 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301780
1781 if (!tuning_loop_counter && !timeout)
1782 break;
1783
Girish K S069c9f12012-01-06 09:56:39 +05301784 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301785 cmd.arg = 0;
1786 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1787 cmd.retries = 0;
1788 cmd.data = NULL;
1789 cmd.error = 0;
1790
1791 mrq.cmd = &cmd;
1792 host->mrq = &mrq;
1793
1794 /*
1795 * In response to CMD19, the card sends 64 bytes of tuning
1796 * block to the Host Controller. So we set the block size
1797 * to 64 here.
1798 */
Girish K S069c9f12012-01-06 09:56:39 +05301799 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1800 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1801 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1802 SDHCI_BLOCK_SIZE);
1803 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1804 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1805 SDHCI_BLOCK_SIZE);
1806 } else {
1807 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1808 SDHCI_BLOCK_SIZE);
1809 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301810
1811 /*
1812 * The tuning block is sent by the card to the host controller.
1813 * So we set the TRNS_READ bit in the Transfer Mode register.
1814 * This also takes care of setting DMA Enable and Multi Block
1815 * Select in the same register to 0.
1816 */
1817 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1818
1819 sdhci_send_command(host, &cmd);
1820
1821 host->cmd = NULL;
1822 host->mrq = NULL;
1823
1824 spin_unlock(&host->lock);
1825 enable_irq(host->irq);
1826
1827 /* Wait for Buffer Read Ready interrupt */
1828 wait_event_interruptible_timeout(host->buf_ready_int,
1829 (host->tuning_done == 1),
1830 msecs_to_jiffies(50));
1831 disable_irq(host->irq);
1832 spin_lock(&host->lock);
1833
1834 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301835 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301836 "Buffer Read Ready interrupt during tuning "
1837 "procedure, falling back to fixed sampling "
1838 "clock\n");
1839 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1840 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1841 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1842 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1843
1844 err = -EIO;
1845 goto out;
1846 }
1847
1848 host->tuning_done = 0;
1849
1850 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1851 tuning_loop_counter--;
1852 timeout--;
1853 mdelay(1);
1854 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1855
1856 /*
1857 * The Host Driver has exhausted the maximum number of loops allowed,
1858 * so use fixed sampling frequency.
1859 */
1860 if (!tuning_loop_counter || !timeout) {
1861 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1862 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1863 } else {
1864 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301865 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301866 " failed, falling back to fixed sampling"
1867 " clock\n");
1868 err = -EIO;
1869 }
1870 }
1871
1872out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301873 /*
1874 * If this is the very first time we are here, we start the retuning
1875 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1876 * flag won't be set, we check this condition before actually starting
1877 * the timer.
1878 */
1879 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1880 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001881 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301882 mod_timer(&host->tuning_timer, jiffies +
1883 host->tuning_count * HZ);
1884 /* Tuning mode 1 limits the maximum data length to 4MB */
1885 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1886 } else {
1887 host->flags &= ~SDHCI_NEEDS_RETUNING;
1888 /* Reload the new initial value for timer */
1889 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1890 mod_timer(&host->tuning_timer, jiffies +
1891 host->tuning_count * HZ);
1892 }
1893
1894 /*
1895 * In case tuning fails, host controllers which support re-tuning can
1896 * try tuning again at a later time, when the re-tuning timer expires.
1897 * So for these controllers, we return 0. Since there might be other
1898 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08001899 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
1900 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301901 */
Aaron Lu973905f2012-07-04 13:29:09 +08001902 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301903 err = 0;
1904
Arindam Nathb513ea22011-05-05 12:19:04 +05301905 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1906 spin_unlock(&host->lock);
1907 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001908 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301909
1910 return err;
1911}
1912
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001913static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301914{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301915 u16 ctrl;
1916 unsigned long flags;
1917
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301918 /* Host Controller v3.00 defines preset value registers */
1919 if (host->version < SDHCI_SPEC_300)
1920 return;
1921
1922 spin_lock_irqsave(&host->lock, flags);
1923
1924 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1925
1926 /*
1927 * We only enable or disable Preset Value if they are not already
1928 * enabled or disabled respectively. Otherwise, we bail out.
1929 */
1930 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1931 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1932 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001933 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301934 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1935 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1936 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001937 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301938 }
1939
1940 spin_unlock_irqrestore(&host->lock, flags);
1941}
1942
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001943static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1944{
1945 struct sdhci_host *host = mmc_priv(mmc);
1946
1947 sdhci_runtime_pm_get(host);
1948 sdhci_do_enable_preset_value(host, enable);
1949 sdhci_runtime_pm_put(host);
1950}
1951
David Brownellab7aefd2006-11-12 17:55:30 -08001952static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001953 .request = sdhci_request,
1954 .set_ios = sdhci_set_ios,
1955 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001956 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001957 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301958 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301959 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301960 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001961};
1962
1963/*****************************************************************************\
1964 * *
1965 * Tasklets *
1966 * *
1967\*****************************************************************************/
1968
1969static void sdhci_tasklet_card(unsigned long param)
1970{
1971 struct sdhci_host *host;
1972 unsigned long flags;
1973
1974 host = (struct sdhci_host*)param;
1975
1976 spin_lock_irqsave(&host->lock, flags);
1977
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001978 /* Check host->mrq first in case we are runtime suspended */
1979 if (host->mrq &&
1980 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301981 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001982 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301983 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001984 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001985
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001986 sdhci_reset(host, SDHCI_RESET_CMD);
1987 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001988
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001989 host->mrq->cmd->error = -ENOMEDIUM;
1990 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001991 }
1992
1993 spin_unlock_irqrestore(&host->lock, flags);
1994
Pierre Ossman04cf5852008-08-18 22:18:14 +02001995 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001996}
1997
1998static void sdhci_tasklet_finish(unsigned long param)
1999{
2000 struct sdhci_host *host;
2001 unsigned long flags;
2002 struct mmc_request *mrq;
2003
2004 host = (struct sdhci_host*)param;
2005
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002006 spin_lock_irqsave(&host->lock, flags);
2007
Chris Ball0c9c99a2011-04-27 17:35:31 -04002008 /*
2009 * If this tasklet gets rescheduled while running, it will
2010 * be run again afterwards but without any active request.
2011 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002012 if (!host->mrq) {
2013 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002014 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002015 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002016
2017 del_timer(&host->timer);
2018
2019 mrq = host->mrq;
2020
Pierre Ossmand129bce2006-03-24 03:18:17 -08002021 /*
2022 * The controller needs a reset of internal state machines
2023 * upon error conditions.
2024 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002025 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002026 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002027 (mrq->data && (mrq->data->error ||
2028 (mrq->data->stop && mrq->data->stop->error))) ||
2029 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002030
2031 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002032 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002033 unsigned int clock;
2034
2035 /* This is to force an update */
2036 clock = host->clock;
2037 host->clock = 0;
2038 sdhci_set_clock(host, clock);
2039 }
2040
2041 /* Spec says we should do both at the same time, but Ricoh
2042 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002043 sdhci_reset(host, SDHCI_RESET_CMD);
2044 sdhci_reset(host, SDHCI_RESET_DATA);
2045 }
2046
2047 host->mrq = NULL;
2048 host->cmd = NULL;
2049 host->data = NULL;
2050
Pierre Ossmanf9134312008-12-21 17:01:48 +01002051#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002052 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002053#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054
Pierre Ossman5f25a662006-10-04 02:15:39 -07002055 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002056 spin_unlock_irqrestore(&host->lock, flags);
2057
2058 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002059 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002060}
2061
2062static void sdhci_timeout_timer(unsigned long data)
2063{
2064 struct sdhci_host *host;
2065 unsigned long flags;
2066
2067 host = (struct sdhci_host*)data;
2068
2069 spin_lock_irqsave(&host->lock, flags);
2070
2071 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302072 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002073 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002074 sdhci_dumpregs(host);
2075
2076 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002077 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078 sdhci_finish_data(host);
2079 } else {
2080 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002081 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002083 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002084
2085 tasklet_schedule(&host->finish_tasklet);
2086 }
2087 }
2088
Pierre Ossman5f25a662006-10-04 02:15:39 -07002089 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002090 spin_unlock_irqrestore(&host->lock, flags);
2091}
2092
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302093static void sdhci_tuning_timer(unsigned long data)
2094{
2095 struct sdhci_host *host;
2096 unsigned long flags;
2097
2098 host = (struct sdhci_host *)data;
2099
2100 spin_lock_irqsave(&host->lock, flags);
2101
2102 host->flags |= SDHCI_NEEDS_RETUNING;
2103
2104 spin_unlock_irqrestore(&host->lock, flags);
2105}
2106
Pierre Ossmand129bce2006-03-24 03:18:17 -08002107/*****************************************************************************\
2108 * *
2109 * Interrupt handling *
2110 * *
2111\*****************************************************************************/
2112
2113static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2114{
2115 BUG_ON(intmask == 0);
2116
2117 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302118 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002119 "though no command operation was in progress.\n",
2120 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002121 sdhci_dumpregs(host);
2122 return;
2123 }
2124
Pierre Ossman43b58b32007-07-25 23:15:27 +02002125 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002126 host->cmd->error = -ETIMEDOUT;
2127 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2128 SDHCI_INT_INDEX))
2129 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002130
Pierre Ossmane8095172008-07-25 01:09:08 +02002131 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002132 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002133 return;
2134 }
2135
2136 /*
2137 * The host can send and interrupt when the busy state has
2138 * ended, allowing us to wait without wasting CPU cycles.
2139 * Unfortunately this is overloaded on the "data complete"
2140 * interrupt, so we need to take some care when handling
2141 * it.
2142 *
2143 * Note: The 1.0 specification is a bit ambiguous about this
2144 * feature so there might be some problems with older
2145 * controllers.
2146 */
2147 if (host->cmd->flags & MMC_RSP_BUSY) {
2148 if (host->cmd->data)
2149 DBG("Cannot wait for busy signal when also "
2150 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002151 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002152 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002153
2154 /* The controller does not support the end-of-busy IRQ,
2155 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002156 }
2157
2158 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002159 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160}
2161
George G. Davis0957c332010-02-18 12:32:12 -05002162#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002163static void sdhci_show_adma_error(struct sdhci_host *host)
2164{
2165 const char *name = mmc_hostname(host->mmc);
2166 u8 *desc = host->adma_desc;
2167 __le32 *dma;
2168 __le16 *len;
2169 u8 attr;
2170
2171 sdhci_dumpregs(host);
2172
2173 while (true) {
2174 dma = (__le32 *)(desc + 4);
2175 len = (__le16 *)(desc + 2);
2176 attr = *desc;
2177
2178 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2179 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2180
2181 desc += 8;
2182
2183 if (attr & 2)
2184 break;
2185 }
2186}
2187#else
2188static void sdhci_show_adma_error(struct sdhci_host *host) { }
2189#endif
2190
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2192{
Girish K S069c9f12012-01-06 09:56:39 +05302193 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 BUG_ON(intmask == 0);
2195
Arindam Nathb513ea22011-05-05 12:19:04 +05302196 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2197 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302198 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2199 if (command == MMC_SEND_TUNING_BLOCK ||
2200 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302201 host->tuning_done = 1;
2202 wake_up(&host->buf_ready_int);
2203 return;
2204 }
2205 }
2206
Pierre Ossmand129bce2006-03-24 03:18:17 -08002207 if (!host->data) {
2208 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002209 * The "data complete" interrupt is also used to
2210 * indicate that a busy state has ended. See comment
2211 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002212 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002213 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2214 if (intmask & SDHCI_INT_DATA_END) {
2215 sdhci_finish_command(host);
2216 return;
2217 }
2218 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002219
Girish K Sa3c76eb2011-10-11 11:44:09 +05302220 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002221 "though no data operation was in progress.\n",
2222 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002223 sdhci_dumpregs(host);
2224
2225 return;
2226 }
2227
2228 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002229 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002230 else if (intmask & SDHCI_INT_DATA_END_BIT)
2231 host->data->error = -EILSEQ;
2232 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2233 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2234 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002235 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002236 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302237 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002238 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002239 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002240 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241
Pierre Ossman17b04292007-07-22 22:18:46 +02002242 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002243 sdhci_finish_data(host);
2244 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002245 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246 sdhci_transfer_pio(host);
2247
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002248 /*
2249 * We currently don't do anything fancy with DMA
2250 * boundaries, but as we can't disable the feature
2251 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002252 *
2253 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2254 * should return a valid address to continue from, but as
2255 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002256 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002257 if (intmask & SDHCI_INT_DMA_END) {
2258 u32 dmastart, dmanow;
2259 dmastart = sg_dma_address(host->data->sg);
2260 dmanow = dmastart + host->data->bytes_xfered;
2261 /*
2262 * Force update to the next DMA block boundary.
2263 */
2264 dmanow = (dmanow &
2265 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2266 SDHCI_DEFAULT_BOUNDARY_SIZE;
2267 host->data->bytes_xfered = dmanow - dmastart;
2268 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2269 " next 0x%08x\n",
2270 mmc_hostname(host->mmc), dmastart,
2271 host->data->bytes_xfered, dmanow);
2272 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2273 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002274
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002275 if (intmask & SDHCI_INT_DATA_END) {
2276 if (host->cmd) {
2277 /*
2278 * Data managed to finish before the
2279 * command completed. Make sure we do
2280 * things in the proper order.
2281 */
2282 host->data_early = 1;
2283 } else {
2284 sdhci_finish_data(host);
2285 }
2286 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287 }
2288}
2289
David Howells7d12e782006-10-05 14:55:46 +01002290static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291{
2292 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002293 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002294 u32 intmask, unexpected = 0;
2295 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002296
2297 spin_lock(&host->lock);
2298
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002299 if (host->runtime_suspended) {
2300 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302301 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002302 mmc_hostname(host->mmc));
2303 return IRQ_HANDLED;
2304 }
2305
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002306 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307
Mark Lord62df67a52007-03-06 13:30:13 +01002308 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 result = IRQ_NONE;
2310 goto out;
2311 }
2312
Alexander Stein6379b232012-03-14 09:52:10 +01002313again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002314 DBG("*** %s got interrupt: 0x%08x\n",
2315 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316
Pierre Ossman3192a282006-06-30 02:22:26 -07002317 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002318 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2319 SDHCI_CARD_PRESENT;
2320
2321 /*
2322 * There is a observation on i.mx esdhc. INSERT bit will be
2323 * immediately set again when it gets cleared, if a card is
2324 * inserted. We have to mask the irq to prevent interrupt
2325 * storm which will freeze the system. And the REMOVE gets
2326 * the same situation.
2327 *
2328 * More testing are needed here to ensure it works for other
2329 * platforms though.
2330 */
2331 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2332 SDHCI_INT_CARD_REMOVE);
2333 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2334 SDHCI_INT_CARD_INSERT);
2335
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002336 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002337 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2338 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002340 }
2341
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002343 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2344 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002345 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346 }
2347
2348 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002349 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2350 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002351 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352 }
2353
2354 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2355
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002356 intmask &= ~SDHCI_INT_ERROR;
2357
Pierre Ossmand129bce2006-03-24 03:18:17 -08002358 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302359 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002360 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002361 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362 }
2363
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002364 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002365
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002366 if (intmask & SDHCI_INT_CARD_INT)
2367 cardint = 1;
2368
2369 intmask &= ~SDHCI_INT_CARD_INT;
2370
Pierre Ossman3192a282006-06-30 02:22:26 -07002371 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002372 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002373 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002374 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375
2376 result = IRQ_HANDLED;
2377
Alexander Stein6379b232012-03-14 09:52:10 +01002378 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2379 if (intmask && --max_loops)
2380 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002381out:
2382 spin_unlock(&host->lock);
2383
Alexander Stein6379b232012-03-14 09:52:10 +01002384 if (unexpected) {
2385 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2386 mmc_hostname(host->mmc), unexpected);
2387 sdhci_dumpregs(host);
2388 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002389 /*
2390 * We have to delay this as it calls back into the driver.
2391 */
2392 if (cardint)
2393 mmc_signal_sdio_irq(host->mmc);
2394
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395 return result;
2396}
2397
2398/*****************************************************************************\
2399 * *
2400 * Suspend/resume *
2401 * *
2402\*****************************************************************************/
2403
2404#ifdef CONFIG_PM
2405
Manuel Lauss29495aa2011-11-03 11:09:45 +01002406int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002408 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409
Chris Balla1b13b42012-02-06 00:43:59 -05002410 if (host->ops->platform_suspend)
2411 host->ops->platform_suspend(host);
2412
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002413 sdhci_disable_card_detection(host);
2414
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302415 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002416 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002417 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302418 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302419 }
2420
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002421 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002422 if (ret) {
Aaron Lu973905f2012-07-04 13:29:09 +08002423 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Lu38a60ea2012-01-04 10:07:43 +08002424 host->flags |= SDHCI_NEEDS_RETUNING;
2425 mod_timer(&host->tuning_timer, jiffies +
2426 host->tuning_count * HZ);
2427 }
2428
2429 sdhci_enable_card_detection(host);
2430
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002431 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002432 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002433
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002434 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002435
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002436 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002437}
2438
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002439EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002440
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002441int sdhci_resume_host(struct sdhci_host *host)
2442{
2443 int ret;
2444
Richard Röjforsa13abc72009-09-22 16:45:30 -07002445 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002446 if (host->ops->enable_dma)
2447 host->ops->enable_dma(host);
2448 }
2449
2450 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2451 mmc_hostname(host->mmc), host);
2452 if (ret)
2453 return ret;
2454
Adrian Hunter6308d292012-02-07 14:48:54 +02002455 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2456 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2457 /* Card keeps power but host controller does not */
2458 sdhci_init(host, 0);
2459 host->pwr = 0;
2460 host->clock = 0;
2461 sdhci_do_set_ios(host, &host->mmc->ios);
2462 } else {
2463 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2464 mmiowb();
2465 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002466
2467 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002468 sdhci_enable_card_detection(host);
2469
Chris Balla1b13b42012-02-06 00:43:59 -05002470 if (host->ops->platform_resume)
2471 host->ops->platform_resume(host);
2472
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302473 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002474 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302475 host->flags |= SDHCI_NEEDS_RETUNING;
2476
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002477 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002478}
2479
2480EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002481
Daniel Drake5f619702010-11-04 22:20:39 +00002482void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2483{
2484 u8 val;
2485 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2486 val |= SDHCI_WAKE_ON_INT;
2487 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2488}
2489
2490EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2491
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492#endif /* CONFIG_PM */
2493
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002494#ifdef CONFIG_PM_RUNTIME
2495
2496static int sdhci_runtime_pm_get(struct sdhci_host *host)
2497{
2498 return pm_runtime_get_sync(host->mmc->parent);
2499}
2500
2501static int sdhci_runtime_pm_put(struct sdhci_host *host)
2502{
2503 pm_runtime_mark_last_busy(host->mmc->parent);
2504 return pm_runtime_put_autosuspend(host->mmc->parent);
2505}
2506
2507int sdhci_runtime_suspend_host(struct sdhci_host *host)
2508{
2509 unsigned long flags;
2510 int ret = 0;
2511
2512 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002513 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002514 del_timer_sync(&host->tuning_timer);
2515 host->flags &= ~SDHCI_NEEDS_RETUNING;
2516 }
2517
2518 spin_lock_irqsave(&host->lock, flags);
2519 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2520 spin_unlock_irqrestore(&host->lock, flags);
2521
2522 synchronize_irq(host->irq);
2523
2524 spin_lock_irqsave(&host->lock, flags);
2525 host->runtime_suspended = true;
2526 spin_unlock_irqrestore(&host->lock, flags);
2527
2528 return ret;
2529}
2530EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2531
2532int sdhci_runtime_resume_host(struct sdhci_host *host)
2533{
2534 unsigned long flags;
2535 int ret = 0, host_flags = host->flags;
2536
2537 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2538 if (host->ops->enable_dma)
2539 host->ops->enable_dma(host);
2540 }
2541
2542 sdhci_init(host, 0);
2543
2544 /* Force clock and power re-program */
2545 host->pwr = 0;
2546 host->clock = 0;
2547 sdhci_do_set_ios(host, &host->mmc->ios);
2548
2549 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2550 if (host_flags & SDHCI_PV_ENABLED)
2551 sdhci_do_enable_preset_value(host, true);
2552
2553 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002554 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002555 host->flags |= SDHCI_NEEDS_RETUNING;
2556
2557 spin_lock_irqsave(&host->lock, flags);
2558
2559 host->runtime_suspended = false;
2560
2561 /* Enable SDIO IRQ */
2562 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2563 sdhci_enable_sdio_irq_nolock(host, true);
2564
2565 /* Enable Card Detection */
2566 sdhci_enable_card_detection(host);
2567
2568 spin_unlock_irqrestore(&host->lock, flags);
2569
2570 return ret;
2571}
2572EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2573
2574#endif
2575
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576/*****************************************************************************\
2577 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002578 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002579 * *
2580\*****************************************************************************/
2581
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002582struct sdhci_host *sdhci_alloc_host(struct device *dev,
2583 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002584{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002585 struct mmc_host *mmc;
2586 struct sdhci_host *host;
2587
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002588 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002590 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002591 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002592 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593
2594 host = mmc_priv(mmc);
2595 host->mmc = mmc;
2596
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002597 return host;
2598}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002599
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002600EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002602int sdhci_add_host(struct sdhci_host *host)
2603{
2604 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002605 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302606 u32 max_current_caps;
2607 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002608 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002610 WARN_ON(host == NULL);
2611 if (host == NULL)
2612 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002613
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002614 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002615
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002616 if (debug_quirks)
2617 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002618 if (debug_quirks2)
2619 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002620
Pierre Ossmand96649e2006-06-30 02:22:30 -07002621 sdhci_reset(host, SDHCI_RESET_ALL);
2622
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002623 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002624 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2625 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002626 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302627 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002628 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002629 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002630 }
2631
Arindam Nathf2119df2011-05-05 12:18:57 +05302632 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002633 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002635 if (host->version >= SDHCI_SPEC_300)
2636 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2637 host->caps1 :
2638 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302639
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002640 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002641 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302642 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002643 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002644 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002645 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002646
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002647 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002648 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002649 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002650 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002651 }
2652
Arindam Nathf2119df2011-05-05 12:18:57 +05302653 if ((host->version >= SDHCI_SPEC_200) &&
2654 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002655 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002656
2657 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2658 (host->flags & SDHCI_USE_ADMA)) {
2659 DBG("Disabling ADMA as it is marked broken\n");
2660 host->flags &= ~SDHCI_USE_ADMA;
2661 }
2662
Richard Röjforsa13abc72009-09-22 16:45:30 -07002663 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002664 if (host->ops->enable_dma) {
2665 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302666 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002667 "available. Falling back to PIO.\n",
2668 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002669 host->flags &=
2670 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002671 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002672 }
2673 }
2674
Pierre Ossman2134a922008-06-28 18:28:51 +02002675 if (host->flags & SDHCI_USE_ADMA) {
2676 /*
2677 * We need to allocate descriptors for all sg entries
2678 * (128) and potentially one alignment transfer for
2679 * each of those entries.
2680 */
2681 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2682 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2683 if (!host->adma_desc || !host->align_buffer) {
2684 kfree(host->adma_desc);
2685 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302686 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002687 "buffers. Falling back to standard DMA.\n",
2688 mmc_hostname(mmc));
2689 host->flags &= ~SDHCI_USE_ADMA;
2690 }
2691 }
2692
Pierre Ossman76591502008-07-21 00:32:11 +02002693 /*
2694 * If we use DMA, then it's up to the caller to set the DMA
2695 * mask, but PIO does not need the hw shim so we set a new
2696 * mask here in that case.
2697 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002698 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002699 host->dma_mask = DMA_BIT_MASK(64);
2700 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2701 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002702
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002703 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302704 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002705 >> SDHCI_CLOCK_BASE_SHIFT;
2706 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302707 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002708 >> SDHCI_CLOCK_BASE_SHIFT;
2709
Pierre Ossmand129bce2006-03-24 03:18:17 -08002710 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002711 if (host->max_clk == 0 || host->quirks &
2712 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002713 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302714 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002715 "frequency.\n", mmc_hostname(mmc));
2716 return -ENODEV;
2717 }
2718 host->max_clk = host->ops->get_max_clock(host);
2719 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720
2721 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302722 * In case of Host Controller v3.00, find out whether clock
2723 * multiplier is supported.
2724 */
2725 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2726 SDHCI_CLOCK_MUL_SHIFT;
2727
2728 /*
2729 * In case the value in Clock Multiplier is 0, then programmable
2730 * clock mode is not supported, otherwise the actual clock
2731 * multiplier is one more than the value of Clock Multiplier
2732 * in the Capabilities Register.
2733 */
2734 if (host->clk_mul)
2735 host->clk_mul += 1;
2736
2737 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002738 * Set host parameters.
2739 */
2740 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302741 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002742 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002743 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302744 else if (host->version >= SDHCI_SPEC_300) {
2745 if (host->clk_mul) {
2746 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2747 mmc->f_max = host->max_clk * host->clk_mul;
2748 } else
2749 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2750 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002751 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002752
Andy Shevchenko272308c2011-08-03 18:36:00 +03002753 host->timeout_clk =
2754 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2755 if (host->timeout_clk == 0) {
2756 if (host->ops->get_timeout_clock) {
2757 host->timeout_clk = host->ops->get_timeout_clock(host);
2758 } else if (!(host->quirks &
2759 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302760 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002761 "frequency.\n", mmc_hostname(mmc));
2762 return -ENODEV;
2763 }
2764 }
2765 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2766 host->timeout_clk *= 1000;
2767
2768 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002769 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002770
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002771 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002772
Andrei Warkentine89d4562011-05-23 15:06:37 -05002773 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2774
2775 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2776 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002777
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002778 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002779 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002780 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002781 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002782 host->flags |= SDHCI_AUTO_CMD23;
2783 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2784 } else {
2785 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2786 }
2787
Philip Rakity15ec4462010-11-19 16:48:39 -05002788 /*
2789 * A controller may support 8-bit width, but the board itself
2790 * might not have the pins brought out. Boards that support
2791 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2792 * their platform code before calling sdhci_add_host(), and we
2793 * won't assume 8-bit width for hosts without that CAP.
2794 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002795 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002796 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797
Arindam Nathf2119df2011-05-05 12:18:57 +05302798 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002799 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002800
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002801 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002802 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002803 mmc->caps |= MMC_CAP_NEEDS_POLL;
2804
Al Cooper4188bba2012-03-16 15:54:17 -04002805 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2806 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2807 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302808 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2809
2810 /* SDR104 supports also implies SDR50 support */
2811 if (caps[1] & SDHCI_SUPPORT_SDR104)
2812 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2813 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2814 mmc->caps |= MMC_CAP_UHS_SDR50;
2815
2816 if (caps[1] & SDHCI_SUPPORT_DDR50)
2817 mmc->caps |= MMC_CAP_UHS_DDR50;
2818
Girish K S069c9f12012-01-06 09:56:39 +05302819 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302820 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2821 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2822
Girish K S069c9f12012-01-06 09:56:39 +05302823 /* Does the host need tuning for HS200? */
2824 if (mmc->caps2 & MMC_CAP2_HS200)
2825 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2826
Arindam Nathd6d50a12011-05-05 12:18:59 +05302827 /* Driver Type(s) (A, C, D) supported by the host */
2828 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2829 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2830 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2831 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2832 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2833 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2834
Girish K Sbec87262011-10-13 12:04:16 +05302835 /*
2836 * If Power Off Notify capability is enabled by the host,
2837 * set notify to short power off notify timeout value.
2838 */
2839 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2840 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2841 else
2842 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2843
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302844 /* Initial value for re-tuning timer count */
2845 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2846 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2847
2848 /*
2849 * In case Re-tuning Timer is not disabled, the actual value of
2850 * re-tuning timer will be 2 ^ (n - 1).
2851 */
2852 if (host->tuning_count)
2853 host->tuning_count = 1 << (host->tuning_count - 1);
2854
2855 /* Re-tuning mode supported by the Host Controller */
2856 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2857 SDHCI_RETUNING_MODE_SHIFT;
2858
Takashi Iwai8f230f42010-12-08 10:04:30 +01002859 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07002860
2861 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2862 if (IS_ERR(host->vmmc)) {
2863 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
2864 host->vmmc = NULL;
2865 }
2866
Philip Rakity68737042012-06-08 12:26:13 -07002867#ifdef CONFIG_REGULATOR
2868 if (host->vmmc) {
2869 ret = regulator_is_supported_voltage(host->vmmc, 3300000,
2870 3300000);
2871 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
2872 caps[0] &= ~SDHCI_CAN_VDD_330;
2873 ret = regulator_is_supported_voltage(host->vmmc, 3000000,
2874 3000000);
2875 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
2876 caps[0] &= ~SDHCI_CAN_VDD_300;
2877 ret = regulator_is_supported_voltage(host->vmmc, 1800000,
2878 1800000);
2879 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
2880 caps[0] &= ~SDHCI_CAN_VDD_180;
2881 }
2882#endif /* CONFIG_REGULATOR */
2883
Arindam Nathf2119df2011-05-05 12:18:57 +05302884 /*
2885 * According to SD Host Controller spec v3.00, if the Host System
2886 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2887 * the value is meaningful only if Voltage Support in the Capabilities
2888 * register is set. The actual current value is 4 times the register
2889 * value.
2890 */
2891 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07002892 if (!max_current_caps && host->vmmc) {
2893 u32 curr = regulator_get_current_limit(host->vmmc);
2894 if (curr > 0) {
2895
2896 /* convert to SDHCI_MAX_CURRENT format */
2897 curr = curr/1000; /* convert to mA */
2898 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
2899
2900 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
2901 max_current_caps =
2902 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
2903 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
2904 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
2905 }
2906 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302907
2908 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002909 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302910
Aaron Lu55c46652012-07-04 13:31:48 +08002911 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302912 SDHCI_MAX_CURRENT_330_MASK) >>
2913 SDHCI_MAX_CURRENT_330_SHIFT) *
2914 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302915 }
2916 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002917 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302918
Aaron Lu55c46652012-07-04 13:31:48 +08002919 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302920 SDHCI_MAX_CURRENT_300_MASK) >>
2921 SDHCI_MAX_CURRENT_300_SHIFT) *
2922 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302923 }
2924 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01002925 ocr_avail |= MMC_VDD_165_195;
2926
Aaron Lu55c46652012-07-04 13:31:48 +08002927 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05302928 SDHCI_MAX_CURRENT_180_MASK) >>
2929 SDHCI_MAX_CURRENT_180_SHIFT) *
2930 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05302931 }
2932
Takashi Iwai8f230f42010-12-08 10:04:30 +01002933 mmc->ocr_avail = ocr_avail;
2934 mmc->ocr_avail_sdio = ocr_avail;
2935 if (host->ocr_avail_sdio)
2936 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2937 mmc->ocr_avail_sd = ocr_avail;
2938 if (host->ocr_avail_sd)
2939 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2940 else /* normal SD controllers don't support 1.8V */
2941 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2942 mmc->ocr_avail_mmc = ocr_avail;
2943 if (host->ocr_avail_mmc)
2944 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002945
2946 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302947 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002948 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002949 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002950 }
2951
Pierre Ossmand129bce2006-03-24 03:18:17 -08002952 spin_lock_init(&host->lock);
2953
2954 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002955 * Maximum number of segments. Depends on if the hardware
2956 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002957 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002958 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002959 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002961 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002962 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002963 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964
2965 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002966 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002967 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002968 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002969 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970
2971 /*
2972 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002973 * of bytes. When doing hardware scatter/gather, each entry cannot
2974 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002975 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002976 if (host->flags & SDHCI_USE_ADMA) {
2977 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2978 mmc->max_seg_size = 65535;
2979 else
2980 mmc->max_seg_size = 65536;
2981 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002982 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002983 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984
2985 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002986 * Maximum block size. This varies from controller to controller and
2987 * is specified in the capabilities register.
2988 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002989 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2990 mmc->max_blk_size = 2;
2991 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302992 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002993 SDHCI_MAX_BLOCK_SHIFT;
2994 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302995 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002996 "assuming 512 bytes\n", mmc_hostname(mmc));
2997 mmc->max_blk_size = 0;
2998 }
2999 }
3000
3001 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003002
3003 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003004 * Maximum block count.
3005 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003006 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003007
3008 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003009 * Init tasklets.
3010 */
3011 tasklet_init(&host->card_tasklet,
3012 sdhci_tasklet_card, (unsigned long)host);
3013 tasklet_init(&host->finish_tasklet,
3014 sdhci_tasklet_finish, (unsigned long)host);
3015
Al Viroe4cad1b2006-10-10 22:47:07 +01003016 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003017
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303018 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303019 init_waitqueue_head(&host->buf_ready_int);
3020
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303021 /* Initialize re-tuning timer */
3022 init_timer(&host->tuning_timer);
3023 host->tuning_timer.data = (unsigned long)host;
3024 host->tuning_timer.function = sdhci_tuning_timer;
3025 }
3026
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003027 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003028 mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003029 if (ret) {
3030 pr_err("%s: Failed to request IRQ %d: %d\n",
3031 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003032 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003033 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003034
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003035 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036
3037#ifdef CONFIG_MMC_DEBUG
3038 sdhci_dumpregs(host);
3039#endif
3040
Pierre Ossmanf9134312008-12-21 17:01:48 +01003041#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003042 snprintf(host->led_name, sizeof(host->led_name),
3043 "%s::", mmc_hostname(mmc));
3044 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003045 host->led.brightness = LED_OFF;
3046 host->led.default_trigger = mmc_hostname(mmc);
3047 host->led.brightness_set = sdhci_led_control;
3048
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003049 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003050 if (ret) {
3051 pr_err("%s: Failed to register LED device: %d\n",
3052 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003053 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003054 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003055#endif
3056
Pierre Ossman5f25a662006-10-04 02:15:39 -07003057 mmiowb();
3058
Pierre Ossmand129bce2006-03-24 03:18:17 -08003059 mmc_add_host(mmc);
3060
Girish K Sa3c76eb2011-10-11 11:44:09 +05303061 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003062 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003063 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3064 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003065
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003066 sdhci_enable_card_detection(host);
3067
Pierre Ossmand129bce2006-03-24 03:18:17 -08003068 return 0;
3069
Pierre Ossmanf9134312008-12-21 17:01:48 +01003070#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003071reset:
3072 sdhci_reset(host, SDHCI_RESET_ALL);
3073 free_irq(host->irq, host);
3074#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003075untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003076 tasklet_kill(&host->card_tasklet);
3077 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003078
3079 return ret;
3080}
3081
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003082EXPORT_SYMBOL_GPL(sdhci_add_host);
3083
Pierre Ossman1e728592008-04-16 19:13:13 +02003084void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003085{
Pierre Ossman1e728592008-04-16 19:13:13 +02003086 unsigned long flags;
3087
3088 if (dead) {
3089 spin_lock_irqsave(&host->lock, flags);
3090
3091 host->flags |= SDHCI_DEVICE_DEAD;
3092
3093 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303094 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003095 " transfer!\n", mmc_hostname(host->mmc));
3096
3097 host->mrq->cmd->error = -ENOMEDIUM;
3098 tasklet_schedule(&host->finish_tasklet);
3099 }
3100
3101 spin_unlock_irqrestore(&host->lock, flags);
3102 }
3103
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003104 sdhci_disable_card_detection(host);
3105
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003106 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003107
Pierre Ossmanf9134312008-12-21 17:01:48 +01003108#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003109 led_classdev_unregister(&host->led);
3110#endif
3111
Pierre Ossman1e728592008-04-16 19:13:13 +02003112 if (!dead)
3113 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114
3115 free_irq(host->irq, host);
3116
3117 del_timer_sync(&host->timer);
3118
3119 tasklet_kill(&host->card_tasklet);
3120 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003121
Adrian Hunterceb61432011-12-27 15:48:41 +02003122 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003123 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003124
Pierre Ossman2134a922008-06-28 18:28:51 +02003125 kfree(host->adma_desc);
3126 kfree(host->align_buffer);
3127
3128 host->adma_desc = NULL;
3129 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003130}
3131
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003132EXPORT_SYMBOL_GPL(sdhci_remove_host);
3133
3134void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003136 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003137}
3138
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003139EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003140
3141/*****************************************************************************\
3142 * *
3143 * Driver init/exit *
3144 * *
3145\*****************************************************************************/
3146
3147static int __init sdhci_drv_init(void)
3148{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303149 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003150 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303151 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003152
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003153 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003154}
3155
3156static void __exit sdhci_drv_exit(void)
3157{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003158}
3159
3160module_init(sdhci_drv_init);
3161module_exit(sdhci_drv_exit);
3162
Pierre Ossmandf673b22006-06-30 02:22:31 -07003163module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003164module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003165
Pierre Ossman32710e82009-04-08 20:14:54 +02003166MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003167MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003168MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003169
Pierre Ossmandf673b22006-06-30 02:22:31 -07003170MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003171MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");