blob: 1fe0ca9c10a3685772d832f082b775c60c831424 [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 Lu94c73bb2012-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 Ossmane16514d2006-06-30 02:22:24 -0700176 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300177 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-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 Ossmane16514d2006-06-30 02:22:24 -0700193 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 host->clock = 0;
195
Pierre Ossmane16514d2006-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 Ossmane16514d2006-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 Ossmane16514d2006-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 Xie3abc1e82011-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);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300248 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249}
250
251static void sdhci_activate_led(struct sdhci_host *host)
252{
253 u8 ctrl;
254
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300255 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300257 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258}
259
260static void sdhci_deactivate_led(struct sdhci_host *host)
261{
262 u8 ctrl;
263
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300264 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300266 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267}
268
Pierre Ossmanf9134312008-12-21 17:01:48 +0100269#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100270static void sdhci_led_control(struct led_classdev *led,
271 enum led_brightness brightness)
272{
273 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
274 unsigned long flags;
275
276 spin_lock_irqsave(&host->lock, flags);
277
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300278 if (host->runtime_suspended)
279 goto out;
280
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100281 if (brightness == LED_OFF)
282 sdhci_deactivate_led(host);
283 else
284 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300285out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100286 spin_unlock_irqrestore(&host->lock, flags);
287}
288#endif
289
Pierre Ossmand129bce2006-03-24 03:18:17 -0800290/*****************************************************************************\
291 * *
292 * Core functions *
293 * *
294\*****************************************************************************/
295
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100296static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297{
Pierre Ossman76591502008-07-21 00:32:11 +0200298 unsigned long flags;
299 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700300 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200301 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800302
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100303 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100305 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200306 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossman76591502008-07-21 00:32:11 +0200308 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200311 if (!sg_miter_next(&host->sg_miter))
312 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 blksize -= len;
317 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 while (len) {
322 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300323 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200324 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800325 }
Pierre Ossman76591502008-07-21 00:32:11 +0200326
327 *buf = scratch & 0xFF;
328
329 buf++;
330 scratch >>= 8;
331 chunk--;
332 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333 }
334 }
Pierre Ossman76591502008-07-21 00:32:11 +0200335
336 sg_miter_stop(&host->sg_miter);
337
338 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100339}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800340
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100341static void sdhci_write_block_pio(struct sdhci_host *host)
342{
Pierre Ossman76591502008-07-21 00:32:11 +0200343 unsigned long flags;
344 size_t blksize, len, chunk;
345 u32 scratch;
346 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347
348 DBG("PIO writing\n");
349
350 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200351 chunk = 0;
352 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353
Pierre Ossman76591502008-07-21 00:32:11 +0200354 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355
356 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200357 if (!sg_miter_next(&host->sg_miter))
358 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 blksize -= len;
363 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 while (len) {
368 scratch |= (u32)*buf << (chunk * 8);
369
370 buf++;
371 chunk++;
372 len--;
373
374 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300375 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200376 chunk = 0;
377 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 }
380 }
Pierre Ossman76591502008-07-21 00:32:11 +0200381
382 sg_miter_stop(&host->sg_miter);
383
384 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385}
386
387static void sdhci_transfer_pio(struct sdhci_host *host)
388{
389 u32 mask;
390
391 BUG_ON(!host->data);
392
Pierre Ossman76591502008-07-21 00:32:11 +0200393 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100394 return;
395
396 if (host->data->flags & MMC_DATA_READ)
397 mask = SDHCI_DATA_AVAILABLE;
398 else
399 mask = SDHCI_SPACE_AVAILABLE;
400
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200401 /*
402 * Some controllers (JMicron JMB38x) mess up the buffer bits
403 * for transfers < 4 bytes. As long as it is just one block,
404 * we can ignore the bits.
405 */
406 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
407 (host->data->blocks == 1))
408 mask = ~0;
409
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300410 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300411 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
412 udelay(100);
413
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100414 if (host->data->flags & MMC_DATA_READ)
415 sdhci_read_block_pio(host);
416 else
417 sdhci_write_block_pio(host);
418
Pierre Ossman76591502008-07-21 00:32:11 +0200419 host->blocks--;
420 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100422 }
423
424 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800425}
426
Pierre Ossman2134a922008-06-28 18:28:51 +0200427static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
428{
429 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800430 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200431}
432
433static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
434{
Cong Wang482fce92011-11-27 13:27:00 +0800435 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200436 local_irq_restore(*flags);
437}
438
Ben Dooks118cd172010-03-05 13:43:26 -0800439static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
440{
Ben Dooks9e506f32010-03-05 13:43:29 -0800441 __le32 *dataddr = (__le32 __force *)(desc + 4);
442 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800443
Ben Dooks9e506f32010-03-05 13:43:29 -0800444 /* SDHCI specification says ADMA descriptors should be 4 byte
445 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800446
Ben Dooks9e506f32010-03-05 13:43:29 -0800447 cmdlen[0] = cpu_to_le16(cmd);
448 cmdlen[1] = cpu_to_le16(len);
449
450 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800451}
452
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200453static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200454 struct mmc_data *data)
455{
456 int direction;
457
458 u8 *desc;
459 u8 *align;
460 dma_addr_t addr;
461 dma_addr_t align_addr;
462 int len, offset;
463
464 struct scatterlist *sg;
465 int i;
466 char *buffer;
467 unsigned long flags;
468
469 /*
470 * The spec does not specify endianness of descriptor table.
471 * We currently guess that it is LE.
472 */
473
474 if (data->flags & MMC_DATA_READ)
475 direction = DMA_FROM_DEVICE;
476 else
477 direction = DMA_TO_DEVICE;
478
479 /*
480 * The ADMA descriptor table is mapped further down as we
481 * need to fill it with data first.
482 */
483
484 host->align_addr = dma_map_single(mmc_dev(host->mmc),
485 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700486 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200487 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200488 BUG_ON(host->align_addr & 0x3);
489
490 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
491 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200492 if (host->sg_count == 0)
493 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200494
495 desc = host->adma_desc;
496 align = host->align_buffer;
497
498 align_addr = host->align_addr;
499
500 for_each_sg(data->sg, sg, host->sg_count, i) {
501 addr = sg_dma_address(sg);
502 len = sg_dma_len(sg);
503
504 /*
505 * The SDHCI specification states that ADMA
506 * addresses must be 32-bit aligned. If they
507 * aren't, then we use a bounce buffer for
508 * the (up to three) bytes that screw up the
509 * alignment.
510 */
511 offset = (4 - (addr & 0x3)) & 0x3;
512 if (offset) {
513 if (data->flags & MMC_DATA_WRITE) {
514 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200515 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200516 memcpy(align, buffer, offset);
517 sdhci_kunmap_atomic(buffer, &flags);
518 }
519
Ben Dooks118cd172010-03-05 13:43:26 -0800520 /* tran, valid */
521 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200522
523 BUG_ON(offset > 65536);
524
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 align += 4;
526 align_addr += 4;
527
528 desc += 8;
529
530 addr += offset;
531 len -= offset;
532 }
533
Pierre Ossman2134a922008-06-28 18:28:51 +0200534 BUG_ON(len > 65536);
535
Ben Dooks118cd172010-03-05 13:43:26 -0800536 /* tran, valid */
537 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 desc += 8;
539
540 /*
541 * If this triggers then we have a calculation bug
542 * somewhere. :/
543 */
544 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
545 }
546
Thomas Abraham70764a92010-05-26 14:42:04 -0700547 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
548 /*
549 * Mark the last descriptor as the terminating descriptor
550 */
551 if (desc != host->adma_desc) {
552 desc -= 8;
553 desc[0] |= 0x2; /* end */
554 }
555 } else {
556 /*
557 * Add a terminating entry.
558 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200559
Thomas Abraham70764a92010-05-26 14:42:04 -0700560 /* nop, end, valid */
561 sdhci_set_adma_desc(desc, 0, 0, 0x3);
562 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200563
564 /*
565 * Resync align buffer as we might have changed it.
566 */
567 if (data->flags & MMC_DATA_WRITE) {
568 dma_sync_single_for_device(mmc_dev(host->mmc),
569 host->align_addr, 128 * 4, direction);
570 }
571
572 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
573 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200574 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200575 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200576 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200577
578 return 0;
579
580unmap_entries:
581 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
582 data->sg_len, direction);
583unmap_align:
584 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
585 128 * 4, direction);
586fail:
587 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200588}
589
590static void sdhci_adma_table_post(struct sdhci_host *host,
591 struct mmc_data *data)
592{
593 int direction;
594
595 struct scatterlist *sg;
596 int i, size;
597 u8 *align;
598 char *buffer;
599 unsigned long flags;
600
601 if (data->flags & MMC_DATA_READ)
602 direction = DMA_FROM_DEVICE;
603 else
604 direction = DMA_TO_DEVICE;
605
606 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
607 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
608
609 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
610 128 * 4, direction);
611
612 if (data->flags & MMC_DATA_READ) {
613 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
614 data->sg_len, direction);
615
616 align = host->align_buffer;
617
618 for_each_sg(data->sg, sg, host->sg_count, i) {
619 if (sg_dma_address(sg) & 0x3) {
620 size = 4 - (sg_dma_address(sg) & 0x3);
621
622 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200623 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 memcpy(buffer, align, size);
625 sdhci_kunmap_atomic(buffer, &flags);
626
627 align += 4;
628 }
629 }
630 }
631
632 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
633 data->sg_len, direction);
634}
635
Andrei Warkentina3c77782011-04-11 16:13:42 -0500636static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800637{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700638 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500639 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700640 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800641
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200642 /*
643 * If the host controller provides us with an incorrect timeout
644 * value, just skip the check and use 0xE. The hardware may take
645 * longer to time out, but that's much better than having a too-short
646 * timeout value.
647 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200648 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200649 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200650
Andrei Warkentina3c77782011-04-11 16:13:42 -0500651 /* Unspecified timeout, assume max */
652 if (!data && !cmd->cmd_timeout_ms)
653 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800654
Andrei Warkentina3c77782011-04-11 16:13:42 -0500655 /* timeout in us */
656 if (!data)
657 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300658 else {
659 target_timeout = data->timeout_ns / 1000;
660 if (host->clock)
661 target_timeout += data->timeout_clks / host->clock;
662 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700663
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700664 /*
665 * Figure out needed cycles.
666 * We do this in steps in order to fit inside a 32 bit int.
667 * The first step is the minimum timeout, which will have a
668 * minimum resolution of 6 bits:
669 * (1) 2^13*1000 > 2^22,
670 * (2) host->timeout_clk < 2^16
671 * =>
672 * (1) / (2) > 2^6
673 */
674 count = 0;
675 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
676 while (current_timeout < target_timeout) {
677 count++;
678 current_timeout <<= 1;
679 if (count >= 0xF)
680 break;
681 }
682
683 if (count >= 0xF) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530684 pr_warning("%s: Too large timeout requested for CMD%d!\n",
Andrei Warkentina3c77782011-04-11 16:13:42 -0500685 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700686 count = 0xE;
687 }
688
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200689 return count;
690}
691
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300692static void sdhci_set_transfer_irqs(struct sdhci_host *host)
693{
694 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
695 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
696
697 if (host->flags & SDHCI_REQ_USE_DMA)
698 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
699 else
700 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
701}
702
Andrei Warkentina3c77782011-04-11 16:13:42 -0500703static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200704{
705 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200706 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500707 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200708 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200709
710 WARN_ON(host->data);
711
Andrei Warkentina3c77782011-04-11 16:13:42 -0500712 if (data || (cmd->flags & MMC_RSP_BUSY)) {
713 count = sdhci_calc_timeout(host, cmd);
714 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
715 }
716
717 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718 return;
719
720 /* Sanity checks */
721 BUG_ON(data->blksz * data->blocks > 524288);
722 BUG_ON(data->blksz > host->mmc->max_blk_size);
723 BUG_ON(data->blocks > 65535);
724
725 host->data = data;
726 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400727 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200728
Richard Röjforsa13abc72009-09-22 16:45:30 -0700729 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100730 host->flags |= SDHCI_REQ_USE_DMA;
731
Pierre Ossman2134a922008-06-28 18:28:51 +0200732 /*
733 * FIXME: This doesn't account for merging when mapping the
734 * scatterlist.
735 */
736 if (host->flags & SDHCI_REQ_USE_DMA) {
737 int broken, i;
738 struct scatterlist *sg;
739
740 broken = 0;
741 if (host->flags & SDHCI_USE_ADMA) {
742 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
743 broken = 1;
744 } else {
745 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
746 broken = 1;
747 }
748
749 if (unlikely(broken)) {
750 for_each_sg(data->sg, sg, data->sg_len, i) {
751 if (sg->length & 0x3) {
752 DBG("Reverting to PIO because of "
753 "transfer size (%d)\n",
754 sg->length);
755 host->flags &= ~SDHCI_REQ_USE_DMA;
756 break;
757 }
758 }
759 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100760 }
761
762 /*
763 * The assumption here being that alignment is the same after
764 * translation to device address space.
765 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200766 if (host->flags & SDHCI_REQ_USE_DMA) {
767 int broken, i;
768 struct scatterlist *sg;
769
770 broken = 0;
771 if (host->flags & SDHCI_USE_ADMA) {
772 /*
773 * As we use 3 byte chunks to work around
774 * alignment problems, we need to check this
775 * quirk.
776 */
777 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
778 broken = 1;
779 } else {
780 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
781 broken = 1;
782 }
783
784 if (unlikely(broken)) {
785 for_each_sg(data->sg, sg, data->sg_len, i) {
786 if (sg->offset & 0x3) {
787 DBG("Reverting to PIO because of "
788 "bad alignment\n");
789 host->flags &= ~SDHCI_REQ_USE_DMA;
790 break;
791 }
792 }
793 }
794 }
795
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200796 if (host->flags & SDHCI_REQ_USE_DMA) {
797 if (host->flags & SDHCI_USE_ADMA) {
798 ret = sdhci_adma_table_pre(host, data);
799 if (ret) {
800 /*
801 * This only happens when someone fed
802 * us an invalid request.
803 */
804 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200805 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200806 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300807 sdhci_writel(host, host->adma_addr,
808 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200809 }
810 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300811 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200812
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300813 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 data->sg, data->sg_len,
815 (data->flags & MMC_DATA_READ) ?
816 DMA_FROM_DEVICE :
817 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300818 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819 /*
820 * This only happens when someone fed
821 * us an invalid request.
822 */
823 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200824 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200825 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200826 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300827 sdhci_writel(host, sg_dma_address(data->sg),
828 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200829 }
830 }
831 }
832
Pierre Ossman2134a922008-06-28 18:28:51 +0200833 /*
834 * Always adjust the DMA selection as some controllers
835 * (e.g. JMicron) can't do PIO properly when the selection
836 * is ADMA.
837 */
838 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300839 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200840 ctrl &= ~SDHCI_CTRL_DMA_MASK;
841 if ((host->flags & SDHCI_REQ_USE_DMA) &&
842 (host->flags & SDHCI_USE_ADMA))
843 ctrl |= SDHCI_CTRL_ADMA32;
844 else
845 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300846 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100847 }
848
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200849 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200850 int flags;
851
852 flags = SG_MITER_ATOMIC;
853 if (host->data->flags & MMC_DATA_READ)
854 flags |= SG_MITER_TO_SG;
855 else
856 flags |= SG_MITER_FROM_SG;
857 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200858 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800859 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700860
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300861 sdhci_set_transfer_irqs(host);
862
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400863 /* Set the DMA boundary value and block size */
864 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
865 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300866 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867}
868
869static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500870 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700871{
872 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500873 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875 if (data == NULL)
876 return;
877
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200878 WARN_ON(!host->data);
879
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500881 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
882 mode |= SDHCI_TRNS_MULTI;
883 /*
884 * If we are sending CMD23, CMD12 never gets sent
885 * on successful completion (so no Auto-CMD12).
886 */
887 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
888 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500889 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
890 mode |= SDHCI_TRNS_AUTO_CMD23;
891 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
892 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700893 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500894
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895 if (data->flags & MMC_DATA_READ)
896 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100897 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898 mode |= SDHCI_TRNS_DMA;
899
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300900 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901}
902
903static void sdhci_finish_data(struct sdhci_host *host)
904{
905 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800906
907 BUG_ON(!host->data);
908
909 data = host->data;
910 host->data = NULL;
911
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100912 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200913 if (host->flags & SDHCI_USE_ADMA)
914 sdhci_adma_table_post(host, data);
915 else {
916 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
917 data->sg_len, (data->flags & MMC_DATA_READ) ?
918 DMA_FROM_DEVICE : DMA_TO_DEVICE);
919 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800920 }
921
922 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200923 * The specification states that the block count register must
924 * be updated, but it does not specify at what point in the
925 * data flow. That makes the register entirely useless to read
926 * back so we have to assume that nothing made it to the card
927 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200929 if (data->error)
930 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200932 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933
Andrei Warkentine89d4562011-05-23 15:06:37 -0500934 /*
935 * Need to send CMD12 if -
936 * a) open-ended multiblock transfer (no CMD23)
937 * b) error in multiblock transfer
938 */
939 if (data->stop &&
940 (data->error ||
941 !host->mrq->sbc)) {
942
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 /*
944 * The controller needs a reset of internal state machines
945 * upon error conditions.
946 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200947 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 sdhci_reset(host, SDHCI_RESET_CMD);
949 sdhci_reset(host, SDHCI_RESET_DATA);
950 }
951
952 sdhci_send_command(host, data->stop);
953 } else
954 tasklet_schedule(&host->finish_tasklet);
955}
956
957static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
958{
959 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700960 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700961 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962
963 WARN_ON(host->cmd);
964
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700966 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700967
968 mask = SDHCI_CMD_INHIBIT;
969 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
970 mask |= SDHCI_DATA_INHIBIT;
971
972 /* We shouldn't wait for data inihibit for stop commands, even
973 though they might use busy signaling */
974 if (host->mrq->data && (cmd == host->mrq->data->stop))
975 mask &= ~SDHCI_DATA_INHIBIT;
976
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300977 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700978 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530979 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100980 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800981 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200982 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 tasklet_schedule(&host->finish_tasklet);
984 return;
985 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700986 timeout--;
987 mdelay(1);
988 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
990 mod_timer(&host->timer, jiffies + 10 * HZ);
991
992 host->cmd = cmd;
993
Andrei Warkentina3c77782011-04-11 16:13:42 -0500994 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300996 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
Andrei Warkentine89d4562011-05-23 15:06:37 -0500998 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700999
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301001 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001003 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 tasklet_schedule(&host->finish_tasklet);
1005 return;
1006 }
1007
1008 if (!(cmd->flags & MMC_RSP_PRESENT))
1009 flags = SDHCI_CMD_RESP_NONE;
1010 else if (cmd->flags & MMC_RSP_136)
1011 flags = SDHCI_CMD_RESP_LONG;
1012 else if (cmd->flags & MMC_RSP_BUSY)
1013 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1014 else
1015 flags = SDHCI_CMD_RESP_SHORT;
1016
1017 if (cmd->flags & MMC_RSP_CRC)
1018 flags |= SDHCI_CMD_CRC;
1019 if (cmd->flags & MMC_RSP_OPCODE)
1020 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301021
1022 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301023 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1024 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025 flags |= SDHCI_CMD_DATA;
1026
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001027 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028}
1029
1030static void sdhci_finish_command(struct sdhci_host *host)
1031{
1032 int i;
1033
1034 BUG_ON(host->cmd == NULL);
1035
1036 if (host->cmd->flags & MMC_RSP_PRESENT) {
1037 if (host->cmd->flags & MMC_RSP_136) {
1038 /* CRC is stripped so we need to do some shifting. */
1039 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001040 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041 SDHCI_RESPONSE + (3-i)*4) << 8;
1042 if (i != 3)
1043 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001044 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001045 SDHCI_RESPONSE + (3-i)*4-1);
1046 }
1047 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001048 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 }
1050 }
1051
Pierre Ossman17b04292007-07-22 22:18:46 +02001052 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053
Andrei Warkentine89d4562011-05-23 15:06:37 -05001054 /* Finished CMD23, now send actual command. */
1055 if (host->cmd == host->mrq->sbc) {
1056 host->cmd = NULL;
1057 sdhci_send_command(host, host->mrq->cmd);
1058 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001059
Andrei Warkentine89d4562011-05-23 15:06:37 -05001060 /* Processed actual command. */
1061 if (host->data && host->data_early)
1062 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063
Andrei Warkentine89d4562011-05-23 15:06:37 -05001064 if (!host->cmd->data)
1065 tasklet_schedule(&host->finish_tasklet);
1066
1067 host->cmd = NULL;
1068 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069}
1070
1071static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1072{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301073 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001074 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301075 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001076 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
Todd Poynor30832ab2011-12-27 15:48:46 +02001078 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079 return;
1080
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001081 host->mmc->actual_clock = 0;
1082
Anton Vorontsov81146342009-03-17 00:13:59 +03001083 if (host->ops->set_clock) {
1084 host->ops->set_clock(host, clock);
1085 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1086 return;
1087 }
1088
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001089 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090
1091 if (clock == 0)
1092 goto out;
1093
Zhangfei Gao85105c52010-08-06 07:10:01 +08001094 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301095 /*
1096 * Check if the Host Controller supports Programmable Clock
1097 * Mode.
1098 */
1099 if (host->clk_mul) {
1100 u16 ctrl;
1101
1102 /*
1103 * We need to figure out whether the Host Driver needs
1104 * to select Programmable Clock Mode, or the value can
1105 * be set automatically by the Host Controller based on
1106 * the Preset Value registers.
1107 */
1108 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1109 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1110 for (div = 1; div <= 1024; div++) {
1111 if (((host->max_clk * host->clk_mul) /
1112 div) <= clock)
1113 break;
1114 }
1115 /*
1116 * Set Programmable Clock Mode in the Clock
1117 * Control register.
1118 */
1119 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001120 real_div = div;
1121 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301122 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001123 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301124 } else {
1125 /* Version 3.00 divisors must be a multiple of 2. */
1126 if (host->max_clk <= clock)
1127 div = 1;
1128 else {
1129 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1130 div += 2) {
1131 if ((host->max_clk / div) <= clock)
1132 break;
1133 }
1134 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001135 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001137 }
1138 } else {
1139 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001140 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001141 if ((host->max_clk / div) <= clock)
1142 break;
1143 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001144 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001148 if (real_div)
1149 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1150
Arindam Nathc3ed3872011-05-05 12:19:06 +05301151 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001152 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1153 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001155 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001156
Chris Ball27f6cb12009-09-22 16:45:31 -07001157 /* Wait max 20 ms */
1158 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001159 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001160 & SDHCI_CLOCK_INT_STABLE)) {
1161 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301162 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001163 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164 sdhci_dumpregs(host);
1165 return;
1166 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001167 timeout--;
1168 mdelay(1);
1169 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
1171 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001172 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173
1174out:
1175 host->clock = clock;
1176}
1177
Adrian Hunterceb61432011-12-27 15:48:41 +02001178static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001179{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001180 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001181
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001182 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001183 switch (1 << power) {
1184 case MMC_VDD_165_195:
1185 pwr = SDHCI_POWER_180;
1186 break;
1187 case MMC_VDD_29_30:
1188 case MMC_VDD_30_31:
1189 pwr = SDHCI_POWER_300;
1190 break;
1191 case MMC_VDD_32_33:
1192 case MMC_VDD_33_34:
1193 pwr = SDHCI_POWER_330;
1194 break;
1195 default:
1196 BUG();
1197 }
1198 }
1199
1200 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001201 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001202
Pierre Ossmanae628902009-05-03 20:45:03 +02001203 host->pwr = pwr;
1204
1205 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001206 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001207 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001208 }
1209
1210 /*
1211 * Spec says that we should clear the power reg before setting
1212 * a new value. Some controllers don't seem to like this though.
1213 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001214 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001215 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001216
Andres Salomone08c1692008-07-04 10:00:03 -07001217 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001218 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001219 * and set turn on power at the same time, so set the voltage first.
1220 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001221 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001222 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1223
1224 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001225
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001226 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001227
1228 /*
1229 * Some controllers need an extra 10ms delay of 10ms before they
1230 * can apply clock after applying power
1231 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001232 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001233 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001234
1235 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001236}
1237
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238/*****************************************************************************\
1239 * *
1240 * MMC callbacks *
1241 * *
1242\*****************************************************************************/
1243
1244static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1245{
1246 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001247 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 unsigned long flags;
Aaron Lu94c73bb2012-07-03 17:27:49 +08001249 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250
1251 host = mmc_priv(mmc);
1252
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001253 sdhci_runtime_pm_get(host);
1254
Pierre Ossmand129bce2006-03-24 03:18:17 -08001255 spin_lock_irqsave(&host->lock, flags);
1256
1257 WARN_ON(host->mrq != NULL);
1258
Pierre Ossmanf9134312008-12-21 17:01:48 +01001259#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001261#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001262
1263 /*
1264 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1265 * requests if Auto-CMD12 is enabled.
1266 */
1267 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001268 if (mrq->stop) {
1269 mrq->data->stop = NULL;
1270 mrq->stop = NULL;
1271 }
1272 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273
1274 host->mrq = mrq;
1275
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001276 /* If polling, assume that the card is always present. */
1277 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1278 present = true;
1279 else
1280 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1281 SDHCI_CARD_PRESENT;
1282
1283 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001284 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001285 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301286 } else {
1287 u32 present_state;
1288
1289 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1290 /*
1291 * Check if the re-tuning timer has already expired and there
1292 * is no on-going data transfer. If so, we need to execute
1293 * tuning procedure before sending command.
1294 */
1295 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1296 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ballf1e729c2012-11-05 14:29:49 -05001297 if (mmc->card) {
1298 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1299 tuning_opcode =
1300 mmc->card->type == MMC_TYPE_MMC ?
1301 MMC_SEND_TUNING_BLOCK_HS200 :
1302 MMC_SEND_TUNING_BLOCK;
1303 spin_unlock_irqrestore(&host->lock, flags);
1304 sdhci_execute_tuning(mmc, tuning_opcode);
1305 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301306
Chris Ballf1e729c2012-11-05 14:29:49 -05001307 /* Restore original mmc_request structure */
1308 host->mrq = mrq;
1309 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301310 }
1311
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001312 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001313 sdhci_send_command(host, mrq->sbc);
1314 else
1315 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301316 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317
Pierre Ossman5f25a662006-10-04 02:15:39 -07001318 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001319 spin_unlock_irqrestore(&host->lock, flags);
1320}
1321
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001322static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001323{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001324 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001325 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 u8 ctrl;
1327
Pierre Ossmand129bce2006-03-24 03:18:17 -08001328 spin_lock_irqsave(&host->lock, flags);
1329
Adrian Hunterceb61432011-12-27 15:48:41 +02001330 if (host->flags & SDHCI_DEVICE_DEAD) {
1331 spin_unlock_irqrestore(&host->lock, flags);
1332 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1333 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1334 return;
1335 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001336
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 /*
1338 * Reset the chip on each power off.
1339 * Should clear out any weird states.
1340 */
1341 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001342 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001343 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 }
1345
1346 sdhci_set_clock(host, ios->clock);
1347
1348 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001349 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001351 vdd_bit = sdhci_set_power(host, ios->vdd);
1352
1353 if (host->vmmc && vdd_bit != -1) {
1354 spin_unlock_irqrestore(&host->lock, flags);
1355 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1356 spin_lock_irqsave(&host->lock, flags);
1357 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358
Philip Rakity643a81f2010-09-23 08:24:32 -07001359 if (host->ops->platform_send_init_74_clocks)
1360 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1361
Philip Rakity15ec4462010-11-19 16:48:39 -05001362 /*
1363 * If your platform has 8-bit width support but is not a v3 controller,
1364 * or if it requires special setup code, you should implement that in
1365 * platform_8bit_width().
1366 */
1367 if (host->ops->platform_8bit_width)
1368 host->ops->platform_8bit_width(host, ios->bus_width);
1369 else {
1370 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1371 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1372 ctrl &= ~SDHCI_CTRL_4BITBUS;
1373 if (host->version >= SDHCI_SPEC_300)
1374 ctrl |= SDHCI_CTRL_8BITBUS;
1375 } else {
1376 if (host->version >= SDHCI_SPEC_300)
1377 ctrl &= ~SDHCI_CTRL_8BITBUS;
1378 if (ios->bus_width == MMC_BUS_WIDTH_4)
1379 ctrl |= SDHCI_CTRL_4BITBUS;
1380 else
1381 ctrl &= ~SDHCI_CTRL_4BITBUS;
1382 }
1383 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1384 }
1385
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001386 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001387
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001388 if ((ios->timing == MMC_TIMING_SD_HS ||
1389 ios->timing == MMC_TIMING_MMC_HS)
1390 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001391 ctrl |= SDHCI_CTRL_HISPD;
1392 else
1393 ctrl &= ~SDHCI_CTRL_HISPD;
1394
Arindam Nathd6d50a12011-05-05 12:18:59 +05301395 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301396 u16 clk, ctrl_2;
1397 unsigned int clock;
1398
1399 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301400 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1401 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301402 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1403 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001404 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301405 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301406
1407 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1408 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301409 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301410 /*
1411 * We only need to set Driver Strength if the
1412 * preset value enable is not set.
1413 */
1414 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1415 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1416 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1417 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1418 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1419
1420 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301421 } else {
1422 /*
1423 * According to SDHC Spec v3.00, if the Preset Value
1424 * Enable in the Host Control 2 register is set, we
1425 * need to reset SD Clock Enable before changing High
1426 * Speed Enable to avoid generating clock gliches.
1427 */
Arindam Nath758535c2011-05-05 12:19:00 +05301428
1429 /* Reset SD Clock Enable */
1430 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1431 clk &= ~SDHCI_CLOCK_CARD_EN;
1432 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1433
1434 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1435
1436 /* Re-enable SD Clock */
1437 clock = host->clock;
1438 host->clock = 0;
1439 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301440 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301441
Arindam Nath49c468f2011-05-05 12:19:01 +05301442
1443 /* Reset SD Clock Enable */
1444 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1445 clk &= ~SDHCI_CLOCK_CARD_EN;
1446 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1447
Philip Rakity6322cdd2011-05-13 11:17:15 +05301448 if (host->ops->set_uhs_signaling)
1449 host->ops->set_uhs_signaling(host, ios->timing);
1450 else {
1451 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1452 /* Select Bus Speed Mode for host */
1453 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301454 if (ios->timing == MMC_TIMING_MMC_HS200)
1455 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1456 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301457 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1458 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1459 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1460 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1461 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1462 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1463 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1464 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1465 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1466 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1467 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301468
1469 /* Re-enable SD Clock */
1470 clock = host->clock;
1471 host->clock = 0;
1472 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301473 } else
1474 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301475
Leandro Dorileob8352262007-07-25 23:47:04 +02001476 /*
1477 * Some (ENE) controllers go apeshit on some ios operation,
1478 * signalling timeout and CRC errors even on CMD0. Resetting
1479 * it on each ios seems to solve the problem.
1480 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001481 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001482 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1483
Pierre Ossman5f25a662006-10-04 02:15:39 -07001484 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 spin_unlock_irqrestore(&host->lock, flags);
1486}
1487
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001488static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1489{
1490 struct sdhci_host *host = mmc_priv(mmc);
1491
1492 sdhci_runtime_pm_get(host);
1493 sdhci_do_set_ios(host, ios);
1494 sdhci_runtime_pm_put(host);
1495}
1496
1497static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001500 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502 spin_lock_irqsave(&host->lock, flags);
1503
Pierre Ossman1e728592008-04-16 19:13:13 +02001504 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001505 is_readonly = 0;
1506 else if (host->ops->get_ro)
1507 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001508 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001509 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1510 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511
1512 spin_unlock_irqrestore(&host->lock, flags);
1513
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001514 /* This quirk needs to be replaced by a callback-function later */
1515 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1516 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001517}
1518
Takashi Iwai82b0e232011-04-21 20:26:38 +02001519#define SAMPLE_COUNT 5
1520
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001521static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001522{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001523 int i, ro_count;
1524
Takashi Iwai82b0e232011-04-21 20:26:38 +02001525 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001526 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001527
1528 ro_count = 0;
1529 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001530 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001531 if (++ro_count > SAMPLE_COUNT / 2)
1532 return 1;
1533 }
1534 msleep(30);
1535 }
1536 return 0;
1537}
1538
Adrian Hunter20758b62011-08-29 16:42:12 +03001539static void sdhci_hw_reset(struct mmc_host *mmc)
1540{
1541 struct sdhci_host *host = mmc_priv(mmc);
1542
1543 if (host->ops && host->ops->hw_reset)
1544 host->ops->hw_reset(host);
1545}
1546
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001547static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001548{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001549 struct sdhci_host *host = mmc_priv(mmc);
1550 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001551
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001552 sdhci_runtime_pm_get(host);
1553 ret = sdhci_do_get_ro(host);
1554 sdhci_runtime_pm_put(host);
1555 return ret;
1556}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001557
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001558static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1559{
Pierre Ossman1e728592008-04-16 19:13:13 +02001560 if (host->flags & SDHCI_DEVICE_DEAD)
1561 goto out;
1562
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001563 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001564 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1565 else
1566 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1567
1568 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1569 if (host->runtime_suspended)
1570 goto out;
1571
1572 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001573 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1574 else
1575 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001576out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001577 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001578}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001579
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001580static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1581{
1582 struct sdhci_host *host = mmc_priv(mmc);
1583 unsigned long flags;
1584
1585 spin_lock_irqsave(&host->lock, flags);
1586 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001587 spin_unlock_irqrestore(&host->lock, flags);
1588}
1589
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001590static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1591 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301592{
Arindam Nathf2119df2011-05-05 12:18:57 +05301593 u8 pwr;
1594 u16 clk, ctrl;
1595 u32 present_state;
1596
Arindam Nathf2119df2011-05-05 12:18:57 +05301597 /*
1598 * Signal Voltage Switching is only applicable for Host Controllers
1599 * v3.00 and above.
1600 */
1601 if (host->version < SDHCI_SPEC_300)
1602 return 0;
1603
1604 /*
1605 * We first check whether the request is to set signalling voltage
1606 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1607 */
1608 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1609 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1610 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1611 ctrl &= ~SDHCI_CTRL_VDD_180;
1612 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1613
1614 /* Wait for 5ms */
1615 usleep_range(5000, 5500);
1616
1617 /* 3.3V regulator output should be stable within 5 ms */
1618 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1619 if (!(ctrl & SDHCI_CTRL_VDD_180))
1620 return 0;
1621 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301622 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301623 "signalling voltage failed\n");
1624 return -EIO;
1625 }
1626 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1627 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1628 /* Stop SDCLK */
1629 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1630 clk &= ~SDHCI_CLOCK_CARD_EN;
1631 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1632
1633 /* Check whether DAT[3:0] is 0000 */
1634 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1635 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1636 SDHCI_DATA_LVL_SHIFT)) {
1637 /*
1638 * Enable 1.8V Signal Enable in the Host Control2
1639 * register
1640 */
1641 ctrl |= SDHCI_CTRL_VDD_180;
1642 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1643
1644 /* Wait for 5ms */
1645 usleep_range(5000, 5500);
1646
1647 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1648 if (ctrl & SDHCI_CTRL_VDD_180) {
1649 /* Provide SDCLK again and wait for 1ms*/
1650 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1651 clk |= SDHCI_CLOCK_CARD_EN;
1652 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1653 usleep_range(1000, 1500);
1654
1655 /*
1656 * If DAT[3:0] level is 1111b, then the card
1657 * was successfully switched to 1.8V signaling.
1658 */
1659 present_state = sdhci_readl(host,
1660 SDHCI_PRESENT_STATE);
1661 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1662 SDHCI_DATA_LVL_MASK)
1663 return 0;
1664 }
1665 }
1666
1667 /*
1668 * If we are here, that means the switch to 1.8V signaling
1669 * failed. We power cycle the card, and retry initialization
1670 * sequence by setting S18R to 0.
1671 */
1672 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1673 pwr &= ~SDHCI_POWER_ON;
1674 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1675
1676 /* Wait for 1ms as per the spec */
1677 usleep_range(1000, 1500);
1678 pwr |= SDHCI_POWER_ON;
1679 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1680
Girish K Sa3c76eb2011-10-11 11:44:09 +05301681 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301682 "voltage failed, retrying with S18R set to 0\n");
1683 return -EAGAIN;
1684 } else
1685 /* No signal voltage switch required */
1686 return 0;
1687}
1688
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001689static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1690 struct mmc_ios *ios)
1691{
1692 struct sdhci_host *host = mmc_priv(mmc);
1693 int err;
1694
1695 if (host->version < SDHCI_SPEC_300)
1696 return 0;
1697 sdhci_runtime_pm_get(host);
1698 err = sdhci_do_start_signal_voltage_switch(host, ios);
1699 sdhci_runtime_pm_put(host);
1700 return err;
1701}
1702
Girish K S069c9f12012-01-06 09:56:39 +05301703static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301704{
1705 struct sdhci_host *host;
1706 u16 ctrl;
1707 u32 ier;
1708 int tuning_loop_counter = MAX_TUNING_LOOP;
1709 unsigned long timeout;
1710 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301711 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301712
1713 host = mmc_priv(mmc);
1714
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001715 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301716 disable_irq(host->irq);
1717 spin_lock(&host->lock);
1718
1719 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1720
1721 /*
Girish K S069c9f12012-01-06 09:56:39 +05301722 * The Host Controller needs tuning only in case of SDR104 mode
1723 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301724 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301725 * If the Host Controller supports the HS200 mode then the
1726 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301727 */
Girish K S069c9f12012-01-06 09:56:39 +05301728 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1729 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1730 host->flags & SDHCI_HS200_NEEDS_TUNING))
1731 requires_tuning_nonuhs = true;
1732
Arindam Nathb513ea22011-05-05 12:19:04 +05301733 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301734 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301735 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1736 else {
1737 spin_unlock(&host->lock);
1738 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001739 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301740 return 0;
1741 }
1742
1743 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1744
1745 /*
1746 * As per the Host Controller spec v3.00, tuning command
1747 * generates Buffer Read Ready interrupt, so enable that.
1748 *
1749 * Note: The spec clearly says that when tuning sequence
1750 * is being performed, the controller does not generate
1751 * interrupts other than Buffer Read Ready interrupt. But
1752 * to make sure we don't hit a controller bug, we _only_
1753 * enable Buffer Read Ready interrupt here.
1754 */
1755 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1756 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1757
1758 /*
1759 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1760 * of loops reaches 40 times or a timeout of 150ms occurs.
1761 */
1762 timeout = 150;
1763 do {
1764 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001765 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301766
1767 if (!tuning_loop_counter && !timeout)
1768 break;
1769
Girish K S069c9f12012-01-06 09:56:39 +05301770 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301771 cmd.arg = 0;
1772 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1773 cmd.retries = 0;
1774 cmd.data = NULL;
1775 cmd.error = 0;
1776
1777 mrq.cmd = &cmd;
1778 host->mrq = &mrq;
1779
1780 /*
1781 * In response to CMD19, the card sends 64 bytes of tuning
1782 * block to the Host Controller. So we set the block size
1783 * to 64 here.
1784 */
Girish K S069c9f12012-01-06 09:56:39 +05301785 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1786 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1787 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1788 SDHCI_BLOCK_SIZE);
1789 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1790 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1791 SDHCI_BLOCK_SIZE);
1792 } else {
1793 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1794 SDHCI_BLOCK_SIZE);
1795 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301796
1797 /*
1798 * The tuning block is sent by the card to the host controller.
1799 * So we set the TRNS_READ bit in the Transfer Mode register.
1800 * This also takes care of setting DMA Enable and Multi Block
1801 * Select in the same register to 0.
1802 */
1803 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1804
1805 sdhci_send_command(host, &cmd);
1806
1807 host->cmd = NULL;
1808 host->mrq = NULL;
1809
1810 spin_unlock(&host->lock);
1811 enable_irq(host->irq);
1812
1813 /* Wait for Buffer Read Ready interrupt */
1814 wait_event_interruptible_timeout(host->buf_ready_int,
1815 (host->tuning_done == 1),
1816 msecs_to_jiffies(50));
1817 disable_irq(host->irq);
1818 spin_lock(&host->lock);
1819
1820 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301821 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301822 "Buffer Read Ready interrupt during tuning "
1823 "procedure, falling back to fixed sampling "
1824 "clock\n");
1825 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1826 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1827 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1828 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1829
1830 err = -EIO;
1831 goto out;
1832 }
1833
1834 host->tuning_done = 0;
1835
1836 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1837 tuning_loop_counter--;
1838 timeout--;
1839 mdelay(1);
1840 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1841
1842 /*
1843 * The Host Driver has exhausted the maximum number of loops allowed,
1844 * so use fixed sampling frequency.
1845 */
1846 if (!tuning_loop_counter || !timeout) {
1847 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1848 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1849 } else {
1850 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301851 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301852 " failed, falling back to fixed sampling"
1853 " clock\n");
1854 err = -EIO;
1855 }
1856 }
1857
1858out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301859 /*
1860 * If this is the very first time we are here, we start the retuning
1861 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1862 * flag won't be set, we check this condition before actually starting
1863 * the timer.
1864 */
1865 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1866 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1867 mod_timer(&host->tuning_timer, jiffies +
1868 host->tuning_count * HZ);
1869 /* Tuning mode 1 limits the maximum data length to 4MB */
1870 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1871 } else {
1872 host->flags &= ~SDHCI_NEEDS_RETUNING;
1873 /* Reload the new initial value for timer */
1874 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1875 mod_timer(&host->tuning_timer, jiffies +
1876 host->tuning_count * HZ);
1877 }
1878
1879 /*
1880 * In case tuning fails, host controllers which support re-tuning can
1881 * try tuning again at a later time, when the re-tuning timer expires.
1882 * So for these controllers, we return 0. Since there might be other
1883 * controllers who do not have this capability, we return error for
1884 * them.
1885 */
1886 if (err && host->tuning_count &&
1887 host->tuning_mode == SDHCI_TUNING_MODE_1)
1888 err = 0;
1889
Arindam Nathb513ea22011-05-05 12:19:04 +05301890 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1891 spin_unlock(&host->lock);
1892 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001893 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301894
1895 return err;
1896}
1897
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001898static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301899{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301900 u16 ctrl;
1901 unsigned long flags;
1902
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301903 /* Host Controller v3.00 defines preset value registers */
1904 if (host->version < SDHCI_SPEC_300)
1905 return;
1906
1907 spin_lock_irqsave(&host->lock, flags);
1908
1909 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1910
1911 /*
1912 * We only enable or disable Preset Value if they are not already
1913 * enabled or disabled respectively. Otherwise, we bail out.
1914 */
1915 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1916 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1917 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001918 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301919 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1920 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1921 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001922 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301923 }
1924
1925 spin_unlock_irqrestore(&host->lock, flags);
1926}
1927
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001928static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1929{
1930 struct sdhci_host *host = mmc_priv(mmc);
1931
1932 sdhci_runtime_pm_get(host);
1933 sdhci_do_enable_preset_value(host, enable);
1934 sdhci_runtime_pm_put(host);
1935}
1936
David Brownellab7aefd2006-11-12 17:55:30 -08001937static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001938 .request = sdhci_request,
1939 .set_ios = sdhci_set_ios,
1940 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001941 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001942 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301943 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301944 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301945 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001946};
1947
1948/*****************************************************************************\
1949 * *
1950 * Tasklets *
1951 * *
1952\*****************************************************************************/
1953
1954static void sdhci_tasklet_card(unsigned long param)
1955{
1956 struct sdhci_host *host;
1957 unsigned long flags;
1958
1959 host = (struct sdhci_host*)param;
1960
1961 spin_lock_irqsave(&host->lock, flags);
1962
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001963 /* Check host->mrq first in case we are runtime suspended */
1964 if (host->mrq &&
1965 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301966 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001967 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301968 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001969 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001970
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001971 sdhci_reset(host, SDHCI_RESET_CMD);
1972 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001973
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001974 host->mrq->cmd->error = -ENOMEDIUM;
1975 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001976 }
1977
1978 spin_unlock_irqrestore(&host->lock, flags);
1979
Pierre Ossman04cf5852008-08-18 22:18:14 +02001980 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001981}
1982
1983static void sdhci_tasklet_finish(unsigned long param)
1984{
1985 struct sdhci_host *host;
1986 unsigned long flags;
1987 struct mmc_request *mrq;
1988
1989 host = (struct sdhci_host*)param;
1990
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001991 spin_lock_irqsave(&host->lock, flags);
1992
Chris Ball0c9c99a2011-04-27 17:35:31 -04001993 /*
1994 * If this tasklet gets rescheduled while running, it will
1995 * be run again afterwards but without any active request.
1996 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001997 if (!host->mrq) {
1998 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001999 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002000 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002001
2002 del_timer(&host->timer);
2003
2004 mrq = host->mrq;
2005
Pierre Ossmand129bce2006-03-24 03:18:17 -08002006 /*
2007 * The controller needs a reset of internal state machines
2008 * upon error conditions.
2009 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002010 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002011 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002012 (mrq->data && (mrq->data->error ||
2013 (mrq->data->stop && mrq->data->stop->error))) ||
2014 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002015
2016 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002017 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002018 unsigned int clock;
2019
2020 /* This is to force an update */
2021 clock = host->clock;
2022 host->clock = 0;
2023 sdhci_set_clock(host, clock);
2024 }
2025
2026 /* Spec says we should do both at the same time, but Ricoh
2027 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002028 sdhci_reset(host, SDHCI_RESET_CMD);
2029 sdhci_reset(host, SDHCI_RESET_DATA);
2030 }
2031
2032 host->mrq = NULL;
2033 host->cmd = NULL;
2034 host->data = NULL;
2035
Pierre Ossmanf9134312008-12-21 17:01:48 +01002036#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002037 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002038#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002039
Pierre Ossman5f25a662006-10-04 02:15:39 -07002040 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002041 spin_unlock_irqrestore(&host->lock, flags);
2042
2043 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002044 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002045}
2046
2047static void sdhci_timeout_timer(unsigned long data)
2048{
2049 struct sdhci_host *host;
2050 unsigned long flags;
2051
2052 host = (struct sdhci_host*)data;
2053
2054 spin_lock_irqsave(&host->lock, flags);
2055
2056 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302057 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002058 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002059 sdhci_dumpregs(host);
2060
2061 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002062 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002063 sdhci_finish_data(host);
2064 } else {
2065 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002066 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002067 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002068 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002069
2070 tasklet_schedule(&host->finish_tasklet);
2071 }
2072 }
2073
Pierre Ossman5f25a662006-10-04 02:15:39 -07002074 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002075 spin_unlock_irqrestore(&host->lock, flags);
2076}
2077
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302078static void sdhci_tuning_timer(unsigned long data)
2079{
2080 struct sdhci_host *host;
2081 unsigned long flags;
2082
2083 host = (struct sdhci_host *)data;
2084
2085 spin_lock_irqsave(&host->lock, flags);
2086
2087 host->flags |= SDHCI_NEEDS_RETUNING;
2088
2089 spin_unlock_irqrestore(&host->lock, flags);
2090}
2091
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092/*****************************************************************************\
2093 * *
2094 * Interrupt handling *
2095 * *
2096\*****************************************************************************/
2097
2098static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2099{
2100 BUG_ON(intmask == 0);
2101
2102 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302103 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002104 "though no command operation was in progress.\n",
2105 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002106 sdhci_dumpregs(host);
2107 return;
2108 }
2109
Pierre Ossman43b58b32007-07-25 23:15:27 +02002110 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002111 host->cmd->error = -ETIMEDOUT;
2112 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2113 SDHCI_INT_INDEX))
2114 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002115
Pierre Ossmane8095172008-07-25 01:09:08 +02002116 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002117 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002118 return;
2119 }
2120
2121 /*
2122 * The host can send and interrupt when the busy state has
2123 * ended, allowing us to wait without wasting CPU cycles.
2124 * Unfortunately this is overloaded on the "data complete"
2125 * interrupt, so we need to take some care when handling
2126 * it.
2127 *
2128 * Note: The 1.0 specification is a bit ambiguous about this
2129 * feature so there might be some problems with older
2130 * controllers.
2131 */
2132 if (host->cmd->flags & MMC_RSP_BUSY) {
2133 if (host->cmd->data)
2134 DBG("Cannot wait for busy signal when also "
2135 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002136 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002137 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002138
2139 /* The controller does not support the end-of-busy IRQ,
2140 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002141 }
2142
2143 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002144 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145}
2146
George G. Davis0957c332010-02-18 12:32:12 -05002147#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002148static void sdhci_show_adma_error(struct sdhci_host *host)
2149{
2150 const char *name = mmc_hostname(host->mmc);
2151 u8 *desc = host->adma_desc;
2152 __le32 *dma;
2153 __le16 *len;
2154 u8 attr;
2155
2156 sdhci_dumpregs(host);
2157
2158 while (true) {
2159 dma = (__le32 *)(desc + 4);
2160 len = (__le16 *)(desc + 2);
2161 attr = *desc;
2162
2163 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2164 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2165
2166 desc += 8;
2167
2168 if (attr & 2)
2169 break;
2170 }
2171}
2172#else
2173static void sdhci_show_adma_error(struct sdhci_host *host) { }
2174#endif
2175
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2177{
Girish K S069c9f12012-01-06 09:56:39 +05302178 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179 BUG_ON(intmask == 0);
2180
Arindam Nathb513ea22011-05-05 12:19:04 +05302181 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2182 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302183 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2184 if (command == MMC_SEND_TUNING_BLOCK ||
2185 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302186 host->tuning_done = 1;
2187 wake_up(&host->buf_ready_int);
2188 return;
2189 }
2190 }
2191
Pierre Ossmand129bce2006-03-24 03:18:17 -08002192 if (!host->data) {
2193 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002194 * The "data complete" interrupt is also used to
2195 * indicate that a busy state has ended. See comment
2196 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002198 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2199 if (intmask & SDHCI_INT_DATA_END) {
2200 sdhci_finish_command(host);
2201 return;
2202 }
2203 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204
Girish K Sa3c76eb2011-10-11 11:44:09 +05302205 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002206 "though no data operation was in progress.\n",
2207 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208 sdhci_dumpregs(host);
2209
2210 return;
2211 }
2212
2213 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002214 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002215 else if (intmask & SDHCI_INT_DATA_END_BIT)
2216 host->data->error = -EILSEQ;
2217 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2218 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2219 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002220 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002221 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302222 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002223 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002224 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002226
Pierre Ossman17b04292007-07-22 22:18:46 +02002227 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002228 sdhci_finish_data(host);
2229 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002230 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002231 sdhci_transfer_pio(host);
2232
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002233 /*
2234 * We currently don't do anything fancy with DMA
2235 * boundaries, but as we can't disable the feature
2236 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002237 *
2238 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2239 * should return a valid address to continue from, but as
2240 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002241 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002242 if (intmask & SDHCI_INT_DMA_END) {
2243 u32 dmastart, dmanow;
2244 dmastart = sg_dma_address(host->data->sg);
2245 dmanow = dmastart + host->data->bytes_xfered;
2246 /*
2247 * Force update to the next DMA block boundary.
2248 */
2249 dmanow = (dmanow &
2250 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2251 SDHCI_DEFAULT_BOUNDARY_SIZE;
2252 host->data->bytes_xfered = dmanow - dmastart;
2253 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2254 " next 0x%08x\n",
2255 mmc_hostname(host->mmc), dmastart,
2256 host->data->bytes_xfered, dmanow);
2257 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2258 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002259
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002260 if (intmask & SDHCI_INT_DATA_END) {
2261 if (host->cmd) {
2262 /*
2263 * Data managed to finish before the
2264 * command completed. Make sure we do
2265 * things in the proper order.
2266 */
2267 host->data_early = 1;
2268 } else {
2269 sdhci_finish_data(host);
2270 }
2271 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272 }
2273}
2274
David Howells7d12e782006-10-05 14:55:46 +01002275static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276{
2277 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002278 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002279 u32 intmask, unexpected = 0;
2280 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002281
2282 spin_lock(&host->lock);
2283
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002284 if (host->runtime_suspended) {
2285 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302286 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002287 mmc_hostname(host->mmc));
2288 return IRQ_HANDLED;
2289 }
2290
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002291 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292
Mark Lord62df67a2007-03-06 13:30:13 +01002293 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 result = IRQ_NONE;
2295 goto out;
2296 }
2297
Alexander Stein6379b232012-03-14 09:52:10 +01002298again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002299 DBG("*** %s got interrupt: 0x%08x\n",
2300 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301
Pierre Ossman3192a282006-06-30 02:22:26 -07002302 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002303 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2304 SDHCI_CARD_PRESENT;
2305
2306 /*
2307 * There is a observation on i.mx esdhc. INSERT bit will be
2308 * immediately set again when it gets cleared, if a card is
2309 * inserted. We have to mask the irq to prevent interrupt
2310 * storm which will freeze the system. And the REMOVE gets
2311 * the same situation.
2312 *
2313 * More testing are needed here to ensure it works for other
2314 * platforms though.
2315 */
2316 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2317 SDHCI_INT_CARD_REMOVE);
2318 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2319 SDHCI_INT_CARD_INSERT);
2320
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002321 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002322 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2323 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002325 }
2326
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002328 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2329 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002330 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002331 }
2332
2333 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002334 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2335 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002336 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337 }
2338
2339 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2340
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002341 intmask &= ~SDHCI_INT_ERROR;
2342
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302344 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002346 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002347 }
2348
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002349 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002350
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002351 if (intmask & SDHCI_INT_CARD_INT)
2352 cardint = 1;
2353
2354 intmask &= ~SDHCI_INT_CARD_INT;
2355
Pierre Ossman3192a282006-06-30 02:22:26 -07002356 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002357 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002358 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002359 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002360
2361 result = IRQ_HANDLED;
2362
Alexander Stein6379b232012-03-14 09:52:10 +01002363 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2364 if (intmask && --max_loops)
2365 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366out:
2367 spin_unlock(&host->lock);
2368
Alexander Stein6379b232012-03-14 09:52:10 +01002369 if (unexpected) {
2370 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2371 mmc_hostname(host->mmc), unexpected);
2372 sdhci_dumpregs(host);
2373 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002374 /*
2375 * We have to delay this as it calls back into the driver.
2376 */
2377 if (cardint)
2378 mmc_signal_sdio_irq(host->mmc);
2379
Pierre Ossmand129bce2006-03-24 03:18:17 -08002380 return result;
2381}
2382
2383/*****************************************************************************\
2384 * *
2385 * Suspend/resume *
2386 * *
2387\*****************************************************************************/
2388
2389#ifdef CONFIG_PM
2390
Manuel Lauss29495aa2011-11-03 11:09:45 +01002391int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002392{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002393 int ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002394 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395
Chris Balla1b13b42012-02-06 00:43:59 -05002396 if (host->ops->platform_suspend)
2397 host->ops->platform_suspend(host);
2398
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002399 sdhci_disable_card_detection(host);
2400
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302401 /* Disable tuning since we are suspending */
Aaron Lu38a60ea2012-01-04 10:07:43 +08002402 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2403 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2404 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002405 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302406 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302407 }
2408
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002409 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002410 if (ret) {
2411 if (has_tuning_timer) {
2412 host->flags |= SDHCI_NEEDS_RETUNING;
2413 mod_timer(&host->tuning_timer, jiffies +
2414 host->tuning_count * HZ);
2415 }
2416
2417 sdhci_enable_card_detection(host);
2418
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002419 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002420 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002421
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002422 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002424 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425}
2426
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002427EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002428
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002429int sdhci_resume_host(struct sdhci_host *host)
2430{
2431 int ret;
2432
Richard Röjforsa13abc72009-09-22 16:45:30 -07002433 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002434 if (host->ops->enable_dma)
2435 host->ops->enable_dma(host);
2436 }
2437
2438 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2439 mmc_hostname(host->mmc), host);
2440 if (ret)
2441 return ret;
2442
Adrian Hunter6308d292012-02-07 14:48:54 +02002443 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2444 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2445 /* Card keeps power but host controller does not */
2446 sdhci_init(host, 0);
2447 host->pwr = 0;
2448 host->clock = 0;
2449 sdhci_do_set_ios(host, &host->mmc->ios);
2450 } else {
2451 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2452 mmiowb();
2453 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002454
2455 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002456 sdhci_enable_card_detection(host);
2457
Chris Balla1b13b42012-02-06 00:43:59 -05002458 if (host->ops->platform_resume)
2459 host->ops->platform_resume(host);
2460
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302461 /* Set the re-tuning expiration flag */
2462 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2463 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2464 host->flags |= SDHCI_NEEDS_RETUNING;
2465
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002466 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002467}
2468
2469EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470
Daniel Drake5f619702010-11-04 22:20:39 +00002471void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2472{
2473 u8 val;
2474 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2475 val |= SDHCI_WAKE_ON_INT;
2476 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2477}
2478
2479EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2480
Pierre Ossmand129bce2006-03-24 03:18:17 -08002481#endif /* CONFIG_PM */
2482
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002483#ifdef CONFIG_PM_RUNTIME
2484
2485static int sdhci_runtime_pm_get(struct sdhci_host *host)
2486{
2487 return pm_runtime_get_sync(host->mmc->parent);
2488}
2489
2490static int sdhci_runtime_pm_put(struct sdhci_host *host)
2491{
2492 pm_runtime_mark_last_busy(host->mmc->parent);
2493 return pm_runtime_put_autosuspend(host->mmc->parent);
2494}
2495
2496int sdhci_runtime_suspend_host(struct sdhci_host *host)
2497{
2498 unsigned long flags;
2499 int ret = 0;
2500
2501 /* Disable tuning since we are suspending */
2502 if (host->version >= SDHCI_SPEC_300 &&
2503 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2504 del_timer_sync(&host->tuning_timer);
2505 host->flags &= ~SDHCI_NEEDS_RETUNING;
2506 }
2507
2508 spin_lock_irqsave(&host->lock, flags);
2509 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2510 spin_unlock_irqrestore(&host->lock, flags);
2511
2512 synchronize_irq(host->irq);
2513
2514 spin_lock_irqsave(&host->lock, flags);
2515 host->runtime_suspended = true;
2516 spin_unlock_irqrestore(&host->lock, flags);
2517
2518 return ret;
2519}
2520EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2521
2522int sdhci_runtime_resume_host(struct sdhci_host *host)
2523{
2524 unsigned long flags;
2525 int ret = 0, host_flags = host->flags;
2526
2527 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2528 if (host->ops->enable_dma)
2529 host->ops->enable_dma(host);
2530 }
2531
2532 sdhci_init(host, 0);
2533
2534 /* Force clock and power re-program */
2535 host->pwr = 0;
2536 host->clock = 0;
2537 sdhci_do_set_ios(host, &host->mmc->ios);
2538
2539 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2540 if (host_flags & SDHCI_PV_ENABLED)
2541 sdhci_do_enable_preset_value(host, true);
2542
2543 /* Set the re-tuning expiration flag */
2544 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2545 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2546 host->flags |= SDHCI_NEEDS_RETUNING;
2547
2548 spin_lock_irqsave(&host->lock, flags);
2549
2550 host->runtime_suspended = false;
2551
2552 /* Enable SDIO IRQ */
2553 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2554 sdhci_enable_sdio_irq_nolock(host, true);
2555
2556 /* Enable Card Detection */
2557 sdhci_enable_card_detection(host);
2558
2559 spin_unlock_irqrestore(&host->lock, flags);
2560
2561 return ret;
2562}
2563EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2564
2565#endif
2566
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567/*****************************************************************************\
2568 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002569 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570 * *
2571\*****************************************************************************/
2572
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002573struct sdhci_host *sdhci_alloc_host(struct device *dev,
2574 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002575{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576 struct mmc_host *mmc;
2577 struct sdhci_host *host;
2578
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002579 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002580
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002581 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002582 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002583 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002584
2585 host = mmc_priv(mmc);
2586 host->mmc = mmc;
2587
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002588 return host;
2589}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002590
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002591EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002592
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002593int sdhci_add_host(struct sdhci_host *host)
2594{
2595 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302596 u32 caps[2];
2597 u32 max_current_caps;
2598 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002599 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002600
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002601 WARN_ON(host == NULL);
2602 if (host == NULL)
2603 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002605 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002606
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002607 if (debug_quirks)
2608 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002609 if (debug_quirks2)
2610 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002611
Pierre Ossmand96649e2006-06-30 02:22:30 -07002612 sdhci_reset(host, SDHCI_RESET_ALL);
2613
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002614 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002615 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2616 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002617 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302618 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002619 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002620 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002621 }
2622
Arindam Nathf2119df2011-05-05 12:18:57 +05302623 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002624 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625
Arindam Nathf2119df2011-05-05 12:18:57 +05302626 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2627 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2628
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002629 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002630 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302631 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002632 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002633 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002634 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002635
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002636 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002637 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002638 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002639 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002640 }
2641
Arindam Nathf2119df2011-05-05 12:18:57 +05302642 if ((host->version >= SDHCI_SPEC_200) &&
2643 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002644 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002645
2646 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2647 (host->flags & SDHCI_USE_ADMA)) {
2648 DBG("Disabling ADMA as it is marked broken\n");
2649 host->flags &= ~SDHCI_USE_ADMA;
2650 }
2651
Richard Röjforsa13abc72009-09-22 16:45:30 -07002652 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002653 if (host->ops->enable_dma) {
2654 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302655 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002656 "available. Falling back to PIO.\n",
2657 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002658 host->flags &=
2659 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002660 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661 }
2662 }
2663
Pierre Ossman2134a922008-06-28 18:28:51 +02002664 if (host->flags & SDHCI_USE_ADMA) {
2665 /*
2666 * We need to allocate descriptors for all sg entries
2667 * (128) and potentially one alignment transfer for
2668 * each of those entries.
2669 */
2670 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2671 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2672 if (!host->adma_desc || !host->align_buffer) {
2673 kfree(host->adma_desc);
2674 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302675 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002676 "buffers. Falling back to standard DMA.\n",
2677 mmc_hostname(mmc));
2678 host->flags &= ~SDHCI_USE_ADMA;
2679 }
2680 }
2681
Pierre Ossman76591502008-07-21 00:32:11 +02002682 /*
2683 * If we use DMA, then it's up to the caller to set the DMA
2684 * mask, but PIO does not need the hw shim so we set a new
2685 * mask here in that case.
2686 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002687 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002688 host->dma_mask = DMA_BIT_MASK(64);
2689 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2690 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002692 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302693 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002694 >> SDHCI_CLOCK_BASE_SHIFT;
2695 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302696 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002697 >> SDHCI_CLOCK_BASE_SHIFT;
2698
Pierre Ossmand129bce2006-03-24 03:18:17 -08002699 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002700 if (host->max_clk == 0 || host->quirks &
2701 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002702 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302703 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002704 "frequency.\n", mmc_hostname(mmc));
2705 return -ENODEV;
2706 }
2707 host->max_clk = host->ops->get_max_clock(host);
2708 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002709
2710 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302711 * In case of Host Controller v3.00, find out whether clock
2712 * multiplier is supported.
2713 */
2714 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2715 SDHCI_CLOCK_MUL_SHIFT;
2716
2717 /*
2718 * In case the value in Clock Multiplier is 0, then programmable
2719 * clock mode is not supported, otherwise the actual clock
2720 * multiplier is one more than the value of Clock Multiplier
2721 * in the Capabilities Register.
2722 */
2723 if (host->clk_mul)
2724 host->clk_mul += 1;
2725
2726 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727 * Set host parameters.
2728 */
2729 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302730 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002731 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002732 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302733 else if (host->version >= SDHCI_SPEC_300) {
2734 if (host->clk_mul) {
2735 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2736 mmc->f_max = host->max_clk * host->clk_mul;
2737 } else
2738 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2739 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002740 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002741
Andy Shevchenko272308c2011-08-03 18:36:00 +03002742 host->timeout_clk =
2743 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2744 if (host->timeout_clk == 0) {
2745 if (host->ops->get_timeout_clock) {
2746 host->timeout_clk = host->ops->get_timeout_clock(host);
2747 } else if (!(host->quirks &
2748 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302749 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002750 "frequency.\n", mmc_hostname(mmc));
2751 return -ENODEV;
2752 }
2753 }
2754 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2755 host->timeout_clk *= 1000;
2756
2757 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002758 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002759
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002760 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002761
Andrei Warkentine89d4562011-05-23 15:06:37 -05002762 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2763
2764 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2765 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002766
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002767 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002768 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002769 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002770 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002771 host->flags |= SDHCI_AUTO_CMD23;
2772 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2773 } else {
2774 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2775 }
2776
Philip Rakity15ec4462010-11-19 16:48:39 -05002777 /*
2778 * A controller may support 8-bit width, but the board itself
2779 * might not have the pins brought out. Boards that support
2780 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2781 * their platform code before calling sdhci_add_host(), and we
2782 * won't assume 8-bit width for hosts without that CAP.
2783 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002784 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002785 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002786
Arindam Nathf2119df2011-05-05 12:18:57 +05302787 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002788 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002789
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002790 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2791 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002792 mmc->caps |= MMC_CAP_NEEDS_POLL;
2793
Al Cooper4188bba2012-03-16 15:54:17 -04002794 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2795 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2796 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302797 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2798
2799 /* SDR104 supports also implies SDR50 support */
2800 if (caps[1] & SDHCI_SUPPORT_SDR104)
2801 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2802 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2803 mmc->caps |= MMC_CAP_UHS_SDR50;
2804
2805 if (caps[1] & SDHCI_SUPPORT_DDR50)
2806 mmc->caps |= MMC_CAP_UHS_DDR50;
2807
Girish K S069c9f12012-01-06 09:56:39 +05302808 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302809 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2810 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2811
Girish K S069c9f12012-01-06 09:56:39 +05302812 /* Does the host need tuning for HS200? */
2813 if (mmc->caps2 & MMC_CAP2_HS200)
2814 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2815
Arindam Nathd6d50a12011-05-05 12:18:59 +05302816 /* Driver Type(s) (A, C, D) supported by the host */
2817 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2818 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2819 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2820 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2821 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2822 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2823
Girish K Sbec87262011-10-13 12:04:16 +05302824 /*
2825 * If Power Off Notify capability is enabled by the host,
2826 * set notify to short power off notify timeout value.
2827 */
2828 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2829 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2830 else
2831 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2832
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302833 /* Initial value for re-tuning timer count */
2834 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2835 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2836
2837 /*
2838 * In case Re-tuning Timer is not disabled, the actual value of
2839 * re-tuning timer will be 2 ^ (n - 1).
2840 */
2841 if (host->tuning_count)
2842 host->tuning_count = 1 << (host->tuning_count - 1);
2843
2844 /* Re-tuning mode supported by the Host Controller */
2845 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2846 SDHCI_RETUNING_MODE_SHIFT;
2847
Takashi Iwai8f230f42010-12-08 10:04:30 +01002848 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302849 /*
2850 * According to SD Host Controller spec v3.00, if the Host System
2851 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2852 * the value is meaningful only if Voltage Support in the Capabilities
2853 * register is set. The actual current value is 4 times the register
2854 * value.
2855 */
2856 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2857
2858 if (caps[0] & SDHCI_CAN_VDD_330) {
2859 int max_current_330;
2860
Takashi Iwai8f230f42010-12-08 10:04:30 +01002861 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302862
2863 max_current_330 = ((max_current_caps &
2864 SDHCI_MAX_CURRENT_330_MASK) >>
2865 SDHCI_MAX_CURRENT_330_SHIFT) *
2866 SDHCI_MAX_CURRENT_MULTIPLIER;
2867
2868 if (max_current_330 > 150)
2869 mmc->caps |= MMC_CAP_SET_XPC_330;
2870 }
2871 if (caps[0] & SDHCI_CAN_VDD_300) {
2872 int max_current_300;
2873
Takashi Iwai8f230f42010-12-08 10:04:30 +01002874 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302875
2876 max_current_300 = ((max_current_caps &
2877 SDHCI_MAX_CURRENT_300_MASK) >>
2878 SDHCI_MAX_CURRENT_300_SHIFT) *
2879 SDHCI_MAX_CURRENT_MULTIPLIER;
2880
2881 if (max_current_300 > 150)
2882 mmc->caps |= MMC_CAP_SET_XPC_300;
2883 }
2884 if (caps[0] & SDHCI_CAN_VDD_180) {
2885 int max_current_180;
2886
Takashi Iwai8f230f42010-12-08 10:04:30 +01002887 ocr_avail |= MMC_VDD_165_195;
2888
Arindam Nathf2119df2011-05-05 12:18:57 +05302889 max_current_180 = ((max_current_caps &
2890 SDHCI_MAX_CURRENT_180_MASK) >>
2891 SDHCI_MAX_CURRENT_180_SHIFT) *
2892 SDHCI_MAX_CURRENT_MULTIPLIER;
2893
2894 if (max_current_180 > 150)
2895 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302896
2897 /* Maximum current capabilities of the host at 1.8V */
2898 if (max_current_180 >= 800)
2899 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2900 else if (max_current_180 >= 600)
2901 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2902 else if (max_current_180 >= 400)
2903 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2904 else
2905 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302906 }
2907
Takashi Iwai8f230f42010-12-08 10:04:30 +01002908 mmc->ocr_avail = ocr_avail;
2909 mmc->ocr_avail_sdio = ocr_avail;
2910 if (host->ocr_avail_sdio)
2911 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2912 mmc->ocr_avail_sd = ocr_avail;
2913 if (host->ocr_avail_sd)
2914 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2915 else /* normal SD controllers don't support 1.8V */
2916 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2917 mmc->ocr_avail_mmc = ocr_avail;
2918 if (host->ocr_avail_mmc)
2919 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002920
2921 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302922 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002923 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002924 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002925 }
2926
Pierre Ossmand129bce2006-03-24 03:18:17 -08002927 spin_lock_init(&host->lock);
2928
2929 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002930 * Maximum number of segments. Depends on if the hardware
2931 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002932 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002933 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002934 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002935 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002936 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002937 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002938 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002939
2940 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002941 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002942 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002944 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945
2946 /*
2947 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002948 * of bytes. When doing hardware scatter/gather, each entry cannot
2949 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002950 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002951 if (host->flags & SDHCI_USE_ADMA) {
2952 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2953 mmc->max_seg_size = 65535;
2954 else
2955 mmc->max_seg_size = 65536;
2956 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002957 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002958 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002959
2960 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002961 * Maximum block size. This varies from controller to controller and
2962 * is specified in the capabilities register.
2963 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002964 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2965 mmc->max_blk_size = 2;
2966 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302967 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002968 SDHCI_MAX_BLOCK_SHIFT;
2969 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302970 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002971 "assuming 512 bytes\n", mmc_hostname(mmc));
2972 mmc->max_blk_size = 0;
2973 }
2974 }
2975
2976 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002977
2978 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002979 * Maximum block count.
2980 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002981 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002982
2983 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984 * Init tasklets.
2985 */
2986 tasklet_init(&host->card_tasklet,
2987 sdhci_tasklet_card, (unsigned long)host);
2988 tasklet_init(&host->finish_tasklet,
2989 sdhci_tasklet_finish, (unsigned long)host);
2990
Al Viroe4cad1b2006-10-10 22:47:07 +01002991 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002992
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302993 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302994 init_waitqueue_head(&host->buf_ready_int);
2995
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302996 /* Initialize re-tuning timer */
2997 init_timer(&host->tuning_timer);
2998 host->tuning_timer.data = (unsigned long)host;
2999 host->tuning_timer.function = sdhci_tuning_timer;
3000 }
3001
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003002 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003003 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003004 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003005 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003006
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003007 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
3008 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303009 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003010 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003011 }
3012
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003013 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003014
3015#ifdef CONFIG_MMC_DEBUG
3016 sdhci_dumpregs(host);
3017#endif
3018
Pierre Ossmanf9134312008-12-21 17:01:48 +01003019#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003020 snprintf(host->led_name, sizeof(host->led_name),
3021 "%s::", mmc_hostname(mmc));
3022 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003023 host->led.brightness = LED_OFF;
3024 host->led.default_trigger = mmc_hostname(mmc);
3025 host->led.brightness_set = sdhci_led_control;
3026
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003027 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003028 if (ret)
3029 goto reset;
3030#endif
3031
Pierre Ossman5f25a662006-10-04 02:15:39 -07003032 mmiowb();
3033
Pierre Ossmand129bce2006-03-24 03:18:17 -08003034 mmc_add_host(mmc);
3035
Girish K Sa3c76eb2011-10-11 11:44:09 +05303036 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003037 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003038 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3039 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003040
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003041 sdhci_enable_card_detection(host);
3042
Pierre Ossmand129bce2006-03-24 03:18:17 -08003043 return 0;
3044
Pierre Ossmanf9134312008-12-21 17:01:48 +01003045#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003046reset:
3047 sdhci_reset(host, SDHCI_RESET_ALL);
3048 free_irq(host->irq, host);
3049#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003050untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003051 tasklet_kill(&host->card_tasklet);
3052 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053
3054 return ret;
3055}
3056
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003057EXPORT_SYMBOL_GPL(sdhci_add_host);
3058
Pierre Ossman1e728592008-04-16 19:13:13 +02003059void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060{
Pierre Ossman1e728592008-04-16 19:13:13 +02003061 unsigned long flags;
3062
3063 if (dead) {
3064 spin_lock_irqsave(&host->lock, flags);
3065
3066 host->flags |= SDHCI_DEVICE_DEAD;
3067
3068 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303069 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003070 " transfer!\n", mmc_hostname(host->mmc));
3071
3072 host->mrq->cmd->error = -ENOMEDIUM;
3073 tasklet_schedule(&host->finish_tasklet);
3074 }
3075
3076 spin_unlock_irqrestore(&host->lock, flags);
3077 }
3078
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003079 sdhci_disable_card_detection(host);
3080
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003081 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003082
Pierre Ossmanf9134312008-12-21 17:01:48 +01003083#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003084 led_classdev_unregister(&host->led);
3085#endif
3086
Pierre Ossman1e728592008-04-16 19:13:13 +02003087 if (!dead)
3088 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003089
3090 free_irq(host->irq, host);
3091
3092 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303093 if (host->version >= SDHCI_SPEC_300)
3094 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003095
3096 tasklet_kill(&host->card_tasklet);
3097 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003098
Adrian Hunterceb61432011-12-27 15:48:41 +02003099 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003100 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003101
Pierre Ossman2134a922008-06-28 18:28:51 +02003102 kfree(host->adma_desc);
3103 kfree(host->align_buffer);
3104
3105 host->adma_desc = NULL;
3106 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003107}
3108
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003109EXPORT_SYMBOL_GPL(sdhci_remove_host);
3110
3111void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003112{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003113 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114}
3115
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003116EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003117
3118/*****************************************************************************\
3119 * *
3120 * Driver init/exit *
3121 * *
3122\*****************************************************************************/
3123
3124static int __init sdhci_drv_init(void)
3125{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303126 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003127 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303128 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003129
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003130 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003131}
3132
3133static void __exit sdhci_drv_exit(void)
3134{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135}
3136
3137module_init(sdhci_drv_init);
3138module_exit(sdhci_drv_exit);
3139
Pierre Ossmandf673b22006-06-30 02:22:31 -07003140module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003141module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003142
Pierre Ossman32710e82009-04-08 20:14:54 +02003143MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003144MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003145MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003146
Pierre Ossmandf673b22006-06-30 02:22:31 -07003147MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003148MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");