blob: e6c6cd6e95f2303f8c43515ea2b0c001c5b4c211 [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053052static int sdhci_execute_tuning(struct mmc_host *mmc);
53static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Pierre Ossmand129bce2006-03-24 03:18:17 -080069static void sdhci_dumpregs(struct sdhci_host *host)
70{
Girish K Sa3c76eb2011-10-11 11:44:09 +053071 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070072 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073
Girish K Sa3c76eb2011-10-11 11:44:09 +053074 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_DMA_ADDRESS),
76 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053077 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_BLOCK_SIZE),
79 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053080 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_ARGUMENT),
82 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_PRESENT_STATE),
85 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readb(host, SDHCI_POWER_CONTROL),
88 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
91 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
94 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_INT_ENABLE),
97 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_ACMD12_ERR),
100 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500103 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530108 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800109
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 readl(host->ioaddr + SDHCI_ADMA_ERROR),
113 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
114
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
125{
126 u32 ier;
127
128 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
129 ier &= ~clear;
130 ier |= set;
131 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
133}
134
135static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
136{
137 sdhci_clear_set_irqs(host, 0, irqs);
138}
139
140static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
141{
142 sdhci_clear_set_irqs(host, irqs, 0);
143}
144
145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Shawn Guod25928d2011-06-21 22:41:48 +0800147 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
150 (host->quirks2 & SDHCI_QUIRK2_OWN_CARD_DETECTION) ||
151 !mmc_card_is_removable(host->mmc))
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);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216}
217
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800218static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
219
220static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800222 if (soft)
223 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
224 else
225 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300227 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
228 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700229 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
230 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300231 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232
233 if (soft) {
234 /* force clock reconfiguration */
235 host->clock = 0;
236 sdhci_set_ios(host->mmc, &host->mmc->ios);
237 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300238}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800239
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300240static void sdhci_reinit(struct sdhci_host *host)
241{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800242 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300243 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244}
245
246static void sdhci_activate_led(struct sdhci_host *host)
247{
248 u8 ctrl;
249
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300250 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300252 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253}
254
255static void sdhci_deactivate_led(struct sdhci_host *host)
256{
257 u8 ctrl;
258
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300259 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262}
263
Pierre Ossmanf9134312008-12-21 17:01:48 +0100264#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100265static void sdhci_led_control(struct led_classdev *led,
266 enum led_brightness brightness)
267{
268 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
269 unsigned long flags;
270
271 spin_lock_irqsave(&host->lock, flags);
272
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300273 if (host->runtime_suspended)
274 goto out;
275
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276 if (brightness == LED_OFF)
277 sdhci_deactivate_led(host);
278 else
279 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300280out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100281 spin_unlock_irqrestore(&host->lock, flags);
282}
283#endif
284
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285/*****************************************************************************\
286 * *
287 * Core functions *
288 * *
289\*****************************************************************************/
290
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100291static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800292{
Pierre Ossman76591502008-07-21 00:32:11 +0200293 unsigned long flags;
294 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700295 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200296 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100298 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800299
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100300 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200301 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800302
Pierre Ossman76591502008-07-21 00:32:11 +0200303 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100305 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200306 if (!sg_miter_next(&host->sg_miter))
307 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossman76591502008-07-21 00:32:11 +0200309 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 blksize -= len;
312 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 while (len) {
317 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300318 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200319 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800320 }
Pierre Ossman76591502008-07-21 00:32:11 +0200321
322 *buf = scratch & 0xFF;
323
324 buf++;
325 scratch >>= 8;
326 chunk--;
327 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328 }
329 }
Pierre Ossman76591502008-07-21 00:32:11 +0200330
331 sg_miter_stop(&host->sg_miter);
332
333 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100334}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800335
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336static void sdhci_write_block_pio(struct sdhci_host *host)
337{
Pierre Ossman76591502008-07-21 00:32:11 +0200338 unsigned long flags;
339 size_t blksize, len, chunk;
340 u32 scratch;
341 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342
343 DBG("PIO writing\n");
344
345 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200346 chunk = 0;
347 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348
Pierre Ossman76591502008-07-21 00:32:11 +0200349 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
351 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200352 if (!sg_miter_next(&host->sg_miter))
353 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354
Pierre Ossman76591502008-07-21 00:32:11 +0200355 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 blksize -= len;
358 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 while (len) {
363 scratch |= (u32)*buf << (chunk * 8);
364
365 buf++;
366 chunk++;
367 len--;
368
369 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300370 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200371 chunk = 0;
372 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 }
375 }
Pierre Ossman76591502008-07-21 00:32:11 +0200376
377 sg_miter_stop(&host->sg_miter);
378
379 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380}
381
382static void sdhci_transfer_pio(struct sdhci_host *host)
383{
384 u32 mask;
385
386 BUG_ON(!host->data);
387
Pierre Ossman76591502008-07-21 00:32:11 +0200388 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389 return;
390
391 if (host->data->flags & MMC_DATA_READ)
392 mask = SDHCI_DATA_AVAILABLE;
393 else
394 mask = SDHCI_SPACE_AVAILABLE;
395
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200396 /*
397 * Some controllers (JMicron JMB38x) mess up the buffer bits
398 * for transfers < 4 bytes. As long as it is just one block,
399 * we can ignore the bits.
400 */
401 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
402 (host->data->blocks == 1))
403 mask = ~0;
404
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300405 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300406 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
407 udelay(100);
408
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409 if (host->data->flags & MMC_DATA_READ)
410 sdhci_read_block_pio(host);
411 else
412 sdhci_write_block_pio(host);
413
Pierre Ossman76591502008-07-21 00:32:11 +0200414 host->blocks--;
415 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417 }
418
419 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800420}
421
Pierre Ossman2134a922008-06-28 18:28:51 +0200422static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
423{
424 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800425 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200426}
427
428static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
429{
Cong Wang482fce92011-11-27 13:27:00 +0800430 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200431 local_irq_restore(*flags);
432}
433
Ben Dooks118cd172010-03-05 13:43:26 -0800434static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
435{
Ben Dooks9e506f32010-03-05 13:43:29 -0800436 __le32 *dataddr = (__le32 __force *)(desc + 4);
437 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800438
Ben Dooks9e506f32010-03-05 13:43:29 -0800439 /* SDHCI specification says ADMA descriptors should be 4 byte
440 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800441
Ben Dooks9e506f32010-03-05 13:43:29 -0800442 cmdlen[0] = cpu_to_le16(cmd);
443 cmdlen[1] = cpu_to_le16(len);
444
445 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800446}
447
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200448static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200449 struct mmc_data *data)
450{
451 int direction;
452
453 u8 *desc;
454 u8 *align;
455 dma_addr_t addr;
456 dma_addr_t align_addr;
457 int len, offset;
458
459 struct scatterlist *sg;
460 int i;
461 char *buffer;
462 unsigned long flags;
463
464 /*
465 * The spec does not specify endianness of descriptor table.
466 * We currently guess that it is LE.
467 */
468
469 if (data->flags & MMC_DATA_READ)
470 direction = DMA_FROM_DEVICE;
471 else
472 direction = DMA_TO_DEVICE;
473
474 /*
475 * The ADMA descriptor table is mapped further down as we
476 * need to fill it with data first.
477 */
478
479 host->align_addr = dma_map_single(mmc_dev(host->mmc),
480 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700481 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200482 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200483 BUG_ON(host->align_addr & 0x3);
484
485 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
486 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200487 if (host->sg_count == 0)
488 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200489
490 desc = host->adma_desc;
491 align = host->align_buffer;
492
493 align_addr = host->align_addr;
494
495 for_each_sg(data->sg, sg, host->sg_count, i) {
496 addr = sg_dma_address(sg);
497 len = sg_dma_len(sg);
498
499 /*
500 * The SDHCI specification states that ADMA
501 * addresses must be 32-bit aligned. If they
502 * aren't, then we use a bounce buffer for
503 * the (up to three) bytes that screw up the
504 * alignment.
505 */
506 offset = (4 - (addr & 0x3)) & 0x3;
507 if (offset) {
508 if (data->flags & MMC_DATA_WRITE) {
509 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200510 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200511 memcpy(align, buffer, offset);
512 sdhci_kunmap_atomic(buffer, &flags);
513 }
514
Ben Dooks118cd172010-03-05 13:43:26 -0800515 /* tran, valid */
516 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200517
518 BUG_ON(offset > 65536);
519
Pierre Ossman2134a922008-06-28 18:28:51 +0200520 align += 4;
521 align_addr += 4;
522
523 desc += 8;
524
525 addr += offset;
526 len -= offset;
527 }
528
Pierre Ossman2134a922008-06-28 18:28:51 +0200529 BUG_ON(len > 65536);
530
Ben Dooks118cd172010-03-05 13:43:26 -0800531 /* tran, valid */
532 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 desc += 8;
534
535 /*
536 * If this triggers then we have a calculation bug
537 * somewhere. :/
538 */
539 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
540 }
541
Thomas Abraham70764a92010-05-26 14:42:04 -0700542 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
543 /*
544 * Mark the last descriptor as the terminating descriptor
545 */
546 if (desc != host->adma_desc) {
547 desc -= 8;
548 desc[0] |= 0x2; /* end */
549 }
550 } else {
551 /*
552 * Add a terminating entry.
553 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200554
Thomas Abraham70764a92010-05-26 14:42:04 -0700555 /* nop, end, valid */
556 sdhci_set_adma_desc(desc, 0, 0, 0x3);
557 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200558
559 /*
560 * Resync align buffer as we might have changed it.
561 */
562 if (data->flags & MMC_DATA_WRITE) {
563 dma_sync_single_for_device(mmc_dev(host->mmc),
564 host->align_addr, 128 * 4, direction);
565 }
566
567 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
568 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200569 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200570 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200571 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200572
573 return 0;
574
575unmap_entries:
576 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
577 data->sg_len, direction);
578unmap_align:
579 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
580 128 * 4, direction);
581fail:
582 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200583}
584
585static void sdhci_adma_table_post(struct sdhci_host *host,
586 struct mmc_data *data)
587{
588 int direction;
589
590 struct scatterlist *sg;
591 int i, size;
592 u8 *align;
593 char *buffer;
594 unsigned long flags;
595
596 if (data->flags & MMC_DATA_READ)
597 direction = DMA_FROM_DEVICE;
598 else
599 direction = DMA_TO_DEVICE;
600
601 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
602 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
603
604 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
605 128 * 4, direction);
606
607 if (data->flags & MMC_DATA_READ) {
608 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
609 data->sg_len, direction);
610
611 align = host->align_buffer;
612
613 for_each_sg(data->sg, sg, host->sg_count, i) {
614 if (sg_dma_address(sg) & 0x3) {
615 size = 4 - (sg_dma_address(sg) & 0x3);
616
617 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200618 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200619 memcpy(buffer, align, size);
620 sdhci_kunmap_atomic(buffer, &flags);
621
622 align += 4;
623 }
624 }
625 }
626
627 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
628 data->sg_len, direction);
629}
630
Andrei Warkentina3c77782011-04-11 16:13:42 -0500631static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800632{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700633 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500634 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700635 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800636
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200637 /*
638 * If the host controller provides us with an incorrect timeout
639 * value, just skip the check and use 0xE. The hardware may take
640 * longer to time out, but that's much better than having a too-short
641 * timeout value.
642 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200643 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200644 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200645
Andrei Warkentina3c77782011-04-11 16:13:42 -0500646 /* Unspecified timeout, assume max */
647 if (!data && !cmd->cmd_timeout_ms)
648 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650 /* timeout in us */
651 if (!data)
652 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300653 else {
654 target_timeout = data->timeout_ns / 1000;
655 if (host->clock)
656 target_timeout += data->timeout_clks / host->clock;
657 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700658
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700659 /*
660 * Figure out needed cycles.
661 * We do this in steps in order to fit inside a 32 bit int.
662 * The first step is the minimum timeout, which will have a
663 * minimum resolution of 6 bits:
664 * (1) 2^13*1000 > 2^22,
665 * (2) host->timeout_clk < 2^16
666 * =>
667 * (1) / (2) > 2^6
668 */
669 count = 0;
670 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
671 while (current_timeout < target_timeout) {
672 count++;
673 current_timeout <<= 1;
674 if (count >= 0xF)
675 break;
676 }
677
678 if (count >= 0xF) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530679 pr_warning("%s: Too large timeout requested for CMD%d!\n",
Andrei Warkentina3c77782011-04-11 16:13:42 -0500680 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700681 count = 0xE;
682 }
683
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200684 return count;
685}
686
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300687static void sdhci_set_transfer_irqs(struct sdhci_host *host)
688{
689 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
690 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
691
692 if (host->flags & SDHCI_REQ_USE_DMA)
693 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
694 else
695 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
696}
697
Andrei Warkentina3c77782011-04-11 16:13:42 -0500698static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200699{
700 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200701 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500702 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200703 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200704
705 WARN_ON(host->data);
706
Andrei Warkentina3c77782011-04-11 16:13:42 -0500707 if (data || (cmd->flags & MMC_RSP_BUSY)) {
708 count = sdhci_calc_timeout(host, cmd);
709 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
710 }
711
712 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200713 return;
714
715 /* Sanity checks */
716 BUG_ON(data->blksz * data->blocks > 524288);
717 BUG_ON(data->blksz > host->mmc->max_blk_size);
718 BUG_ON(data->blocks > 65535);
719
720 host->data = data;
721 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400722 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200723
Richard Röjforsa13abc72009-09-22 16:45:30 -0700724 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100725 host->flags |= SDHCI_REQ_USE_DMA;
726
Pierre Ossman2134a922008-06-28 18:28:51 +0200727 /*
728 * FIXME: This doesn't account for merging when mapping the
729 * scatterlist.
730 */
731 if (host->flags & SDHCI_REQ_USE_DMA) {
732 int broken, i;
733 struct scatterlist *sg;
734
735 broken = 0;
736 if (host->flags & SDHCI_USE_ADMA) {
737 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
738 broken = 1;
739 } else {
740 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
741 broken = 1;
742 }
743
744 if (unlikely(broken)) {
745 for_each_sg(data->sg, sg, data->sg_len, i) {
746 if (sg->length & 0x3) {
747 DBG("Reverting to PIO because of "
748 "transfer size (%d)\n",
749 sg->length);
750 host->flags &= ~SDHCI_REQ_USE_DMA;
751 break;
752 }
753 }
754 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100755 }
756
757 /*
758 * The assumption here being that alignment is the same after
759 * translation to device address space.
760 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200761 if (host->flags & SDHCI_REQ_USE_DMA) {
762 int broken, i;
763 struct scatterlist *sg;
764
765 broken = 0;
766 if (host->flags & SDHCI_USE_ADMA) {
767 /*
768 * As we use 3 byte chunks to work around
769 * alignment problems, we need to check this
770 * quirk.
771 */
772 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
773 broken = 1;
774 } else {
775 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
776 broken = 1;
777 }
778
779 if (unlikely(broken)) {
780 for_each_sg(data->sg, sg, data->sg_len, i) {
781 if (sg->offset & 0x3) {
782 DBG("Reverting to PIO because of "
783 "bad alignment\n");
784 host->flags &= ~SDHCI_REQ_USE_DMA;
785 break;
786 }
787 }
788 }
789 }
790
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200791 if (host->flags & SDHCI_REQ_USE_DMA) {
792 if (host->flags & SDHCI_USE_ADMA) {
793 ret = sdhci_adma_table_pre(host, data);
794 if (ret) {
795 /*
796 * This only happens when someone fed
797 * us an invalid request.
798 */
799 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200800 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200801 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300802 sdhci_writel(host, host->adma_addr,
803 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200804 }
805 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300806 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200807
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300808 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200809 data->sg, data->sg_len,
810 (data->flags & MMC_DATA_READ) ?
811 DMA_FROM_DEVICE :
812 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300813 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200814 /*
815 * This only happens when someone fed
816 * us an invalid request.
817 */
818 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200819 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200820 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200821 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300822 sdhci_writel(host, sg_dma_address(data->sg),
823 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200824 }
825 }
826 }
827
Pierre Ossman2134a922008-06-28 18:28:51 +0200828 /*
829 * Always adjust the DMA selection as some controllers
830 * (e.g. JMicron) can't do PIO properly when the selection
831 * is ADMA.
832 */
833 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300834 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200835 ctrl &= ~SDHCI_CTRL_DMA_MASK;
836 if ((host->flags & SDHCI_REQ_USE_DMA) &&
837 (host->flags & SDHCI_USE_ADMA))
838 ctrl |= SDHCI_CTRL_ADMA32;
839 else
840 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300841 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100842 }
843
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200844 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200845 int flags;
846
847 flags = SG_MITER_ATOMIC;
848 if (host->data->flags & MMC_DATA_READ)
849 flags |= SG_MITER_TO_SG;
850 else
851 flags |= SG_MITER_FROM_SG;
852 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200853 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800854 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700855
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300856 sdhci_set_transfer_irqs(host);
857
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400858 /* Set the DMA boundary value and block size */
859 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
860 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300861 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700862}
863
864static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500865 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700866{
867 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500868 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870 if (data == NULL)
871 return;
872
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200873 WARN_ON(!host->data);
874
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500876 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
877 mode |= SDHCI_TRNS_MULTI;
878 /*
879 * If we are sending CMD23, CMD12 never gets sent
880 * on successful completion (so no Auto-CMD12).
881 */
882 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
883 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500884 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
885 mode |= SDHCI_TRNS_AUTO_CMD23;
886 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
887 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700888 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500889
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890 if (data->flags & MMC_DATA_READ)
891 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100892 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700893 mode |= SDHCI_TRNS_DMA;
894
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300895 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896}
897
898static void sdhci_finish_data(struct sdhci_host *host)
899{
900 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901
902 BUG_ON(!host->data);
903
904 data = host->data;
905 host->data = NULL;
906
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100907 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200908 if (host->flags & SDHCI_USE_ADMA)
909 sdhci_adma_table_post(host, data);
910 else {
911 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
912 data->sg_len, (data->flags & MMC_DATA_READ) ?
913 DMA_FROM_DEVICE : DMA_TO_DEVICE);
914 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915 }
916
917 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200918 * The specification states that the block count register must
919 * be updated, but it does not specify at what point in the
920 * data flow. That makes the register entirely useless to read
921 * back so we have to assume that nothing made it to the card
922 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800923 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200924 if (data->error)
925 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800926 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200927 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928
Andrei Warkentine89d4562011-05-23 15:06:37 -0500929 /*
930 * Need to send CMD12 if -
931 * a) open-ended multiblock transfer (no CMD23)
932 * b) error in multiblock transfer
933 */
934 if (data->stop &&
935 (data->error ||
936 !host->mrq->sbc)) {
937
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938 /*
939 * The controller needs a reset of internal state machines
940 * upon error conditions.
941 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200942 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 sdhci_reset(host, SDHCI_RESET_CMD);
944 sdhci_reset(host, SDHCI_RESET_DATA);
945 }
946
947 sdhci_send_command(host, data->stop);
948 } else
949 tasklet_schedule(&host->finish_tasklet);
950}
951
952static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
953{
954 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700955 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700956 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957
958 WARN_ON(host->cmd);
959
Pierre Ossmand129bce2006-03-24 03:18:17 -0800960 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700961 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700962
963 mask = SDHCI_CMD_INHIBIT;
964 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
965 mask |= SDHCI_DATA_INHIBIT;
966
967 /* We shouldn't wait for data inihibit for stop commands, even
968 though they might use busy signaling */
969 if (host->mrq->data && (cmd == host->mrq->data->stop))
970 mask &= ~SDHCI_DATA_INHIBIT;
971
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300972 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700973 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530974 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100975 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200977 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 tasklet_schedule(&host->finish_tasklet);
979 return;
980 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700981 timeout--;
982 mdelay(1);
983 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984
985 mod_timer(&host->timer, jiffies + 10 * HZ);
986
987 host->cmd = cmd;
988
Andrei Warkentina3c77782011-04-11 16:13:42 -0500989 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992
Andrei Warkentine89d4562011-05-23 15:06:37 -0500993 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700994
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530996 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200998 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999 tasklet_schedule(&host->finish_tasklet);
1000 return;
1001 }
1002
1003 if (!(cmd->flags & MMC_RSP_PRESENT))
1004 flags = SDHCI_CMD_RESP_NONE;
1005 else if (cmd->flags & MMC_RSP_136)
1006 flags = SDHCI_CMD_RESP_LONG;
1007 else if (cmd->flags & MMC_RSP_BUSY)
1008 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1009 else
1010 flags = SDHCI_CMD_RESP_SHORT;
1011
1012 if (cmd->flags & MMC_RSP_CRC)
1013 flags |= SDHCI_CMD_CRC;
1014 if (cmd->flags & MMC_RSP_OPCODE)
1015 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301016
1017 /* CMD19 is special in that the Data Present Select should be set */
1018 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 flags |= SDHCI_CMD_DATA;
1020
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001021 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022}
1023
1024static void sdhci_finish_command(struct sdhci_host *host)
1025{
1026 int i;
1027
1028 BUG_ON(host->cmd == NULL);
1029
1030 if (host->cmd->flags & MMC_RSP_PRESENT) {
1031 if (host->cmd->flags & MMC_RSP_136) {
1032 /* CRC is stripped so we need to do some shifting. */
1033 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001034 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 SDHCI_RESPONSE + (3-i)*4) << 8;
1036 if (i != 3)
1037 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001038 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 SDHCI_RESPONSE + (3-i)*4-1);
1040 }
1041 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001042 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043 }
1044 }
1045
Pierre Ossman17b04292007-07-22 22:18:46 +02001046 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
Andrei Warkentine89d4562011-05-23 15:06:37 -05001048 /* Finished CMD23, now send actual command. */
1049 if (host->cmd == host->mrq->sbc) {
1050 host->cmd = NULL;
1051 sdhci_send_command(host, host->mrq->cmd);
1052 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001053
Andrei Warkentine89d4562011-05-23 15:06:37 -05001054 /* Processed actual command. */
1055 if (host->data && host->data_early)
1056 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
Andrei Warkentine89d4562011-05-23 15:06:37 -05001058 if (!host->cmd->data)
1059 tasklet_schedule(&host->finish_tasklet);
1060
1061 host->cmd = NULL;
1062 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063}
1064
1065static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1066{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301067 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001068 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301069 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001070 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071
1072 if (clock == host->clock)
1073 return;
1074
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001075 host->mmc->actual_clock = 0;
1076
Anton Vorontsov81146342009-03-17 00:13:59 +03001077 if (host->ops->set_clock) {
1078 host->ops->set_clock(host, clock);
1079 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1080 return;
1081 }
1082
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001083 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001084
1085 if (clock == 0)
1086 goto out;
1087
Zhangfei Gao85105c52010-08-06 07:10:01 +08001088 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301089 /*
1090 * Check if the Host Controller supports Programmable Clock
1091 * Mode.
1092 */
1093 if (host->clk_mul) {
1094 u16 ctrl;
1095
1096 /*
1097 * We need to figure out whether the Host Driver needs
1098 * to select Programmable Clock Mode, or the value can
1099 * be set automatically by the Host Controller based on
1100 * the Preset Value registers.
1101 */
1102 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1103 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1104 for (div = 1; div <= 1024; div++) {
1105 if (((host->max_clk * host->clk_mul) /
1106 div) <= clock)
1107 break;
1108 }
1109 /*
1110 * Set Programmable Clock Mode in the Clock
1111 * Control register.
1112 */
1113 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001114 real_div = div;
1115 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301116 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001117 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301118 } else {
1119 /* Version 3.00 divisors must be a multiple of 2. */
1120 if (host->max_clk <= clock)
1121 div = 1;
1122 else {
1123 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1124 div += 2) {
1125 if ((host->max_clk / div) <= clock)
1126 break;
1127 }
1128 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001129 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301130 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001131 }
1132 } else {
1133 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001134 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001135 if ((host->max_clk / div) <= clock)
1136 break;
1137 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001138 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301139 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001142 if (real_div)
1143 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1144
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001146 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1147 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001149 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150
Chris Ball27f6cb12009-09-22 16:45:31 -07001151 /* Wait max 20 ms */
1152 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001153 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001154 & SDHCI_CLOCK_INT_STABLE)) {
1155 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301156 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001157 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158 sdhci_dumpregs(host);
1159 return;
1160 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001161 timeout--;
1162 mdelay(1);
1163 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164
1165 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001166 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167
1168out:
1169 host->clock = clock;
1170}
1171
Adrian Hunterceb61432011-12-27 15:48:41 +02001172static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001173{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001174 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001175
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001176 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001177 switch (1 << power) {
1178 case MMC_VDD_165_195:
1179 pwr = SDHCI_POWER_180;
1180 break;
1181 case MMC_VDD_29_30:
1182 case MMC_VDD_30_31:
1183 pwr = SDHCI_POWER_300;
1184 break;
1185 case MMC_VDD_32_33:
1186 case MMC_VDD_33_34:
1187 pwr = SDHCI_POWER_330;
1188 break;
1189 default:
1190 BUG();
1191 }
1192 }
1193
1194 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001195 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001196
Pierre Ossmanae628902009-05-03 20:45:03 +02001197 host->pwr = pwr;
1198
1199 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001200 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001201 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001202 }
1203
1204 /*
1205 * Spec says that we should clear the power reg before setting
1206 * a new value. Some controllers don't seem to like this though.
1207 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001208 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001209 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001210
Andres Salomone08c1692008-07-04 10:00:03 -07001211 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001212 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001213 * and set turn on power at the same time, so set the voltage first.
1214 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001215 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001216 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1217
1218 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001219
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001221
1222 /*
1223 * Some controllers need an extra 10ms delay of 10ms before they
1224 * can apply clock after applying power
1225 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001226 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001227 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001228
1229 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001230}
1231
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232/*****************************************************************************\
1233 * *
1234 * MMC callbacks *
1235 * *
1236\*****************************************************************************/
1237
1238static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1239{
1240 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001241 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 unsigned long flags;
1243
1244 host = mmc_priv(mmc);
1245
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001246 sdhci_runtime_pm_get(host);
1247
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 spin_lock_irqsave(&host->lock, flags);
1249
1250 WARN_ON(host->mrq != NULL);
1251
Pierre Ossmanf9134312008-12-21 17:01:48 +01001252#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001254#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001255
1256 /*
1257 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1258 * requests if Auto-CMD12 is enabled.
1259 */
1260 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001261 if (mrq->stop) {
1262 mrq->data->stop = NULL;
1263 mrq->stop = NULL;
1264 }
1265 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266
1267 host->mrq = mrq;
1268
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001269 /* If polling, assume that the card is always present. */
1270 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1271 present = true;
1272 else
1273 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1274 SDHCI_CARD_PRESENT;
1275
1276 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001277 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301279 } else {
1280 u32 present_state;
1281
1282 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1283 /*
1284 * Check if the re-tuning timer has already expired and there
1285 * is no on-going data transfer. If so, we need to execute
1286 * tuning procedure before sending command.
1287 */
1288 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1289 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1290 spin_unlock_irqrestore(&host->lock, flags);
1291 sdhci_execute_tuning(mmc);
1292 spin_lock_irqsave(&host->lock, flags);
1293
1294 /* Restore original mmc_request structure */
1295 host->mrq = mrq;
1296 }
1297
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001298 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001299 sdhci_send_command(host, mrq->sbc);
1300 else
1301 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301302 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303
Pierre Ossman5f25a662006-10-04 02:15:39 -07001304 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001305 spin_unlock_irqrestore(&host->lock, flags);
1306}
1307
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001308static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001311 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 u8 ctrl;
1313
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314 spin_lock_irqsave(&host->lock, flags);
1315
Adrian Hunterceb61432011-12-27 15:48:41 +02001316 if (host->flags & SDHCI_DEVICE_DEAD) {
1317 spin_unlock_irqrestore(&host->lock, flags);
1318 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1319 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1320 return;
1321 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001322
Pierre Ossmand129bce2006-03-24 03:18:17 -08001323 /*
1324 * Reset the chip on each power off.
1325 * Should clear out any weird states.
1326 */
1327 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001328 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001329 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001330 }
1331
1332 sdhci_set_clock(host, ios->clock);
1333
1334 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001335 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001337 vdd_bit = sdhci_set_power(host, ios->vdd);
1338
1339 if (host->vmmc && vdd_bit != -1) {
1340 spin_unlock_irqrestore(&host->lock, flags);
1341 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1342 spin_lock_irqsave(&host->lock, flags);
1343 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344
Philip Rakity643a81f2010-09-23 08:24:32 -07001345 if (host->ops->platform_send_init_74_clocks)
1346 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1347
Philip Rakity15ec4462010-11-19 16:48:39 -05001348 /*
1349 * If your platform has 8-bit width support but is not a v3 controller,
1350 * or if it requires special setup code, you should implement that in
1351 * platform_8bit_width().
1352 */
1353 if (host->ops->platform_8bit_width)
1354 host->ops->platform_8bit_width(host, ios->bus_width);
1355 else {
1356 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1357 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1358 ctrl &= ~SDHCI_CTRL_4BITBUS;
1359 if (host->version >= SDHCI_SPEC_300)
1360 ctrl |= SDHCI_CTRL_8BITBUS;
1361 } else {
1362 if (host->version >= SDHCI_SPEC_300)
1363 ctrl &= ~SDHCI_CTRL_8BITBUS;
1364 if (ios->bus_width == MMC_BUS_WIDTH_4)
1365 ctrl |= SDHCI_CTRL_4BITBUS;
1366 else
1367 ctrl &= ~SDHCI_CTRL_4BITBUS;
1368 }
1369 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1370 }
1371
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001372 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001373
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001374 if ((ios->timing == MMC_TIMING_SD_HS ||
1375 ios->timing == MMC_TIMING_MMC_HS)
1376 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001377 ctrl |= SDHCI_CTRL_HISPD;
1378 else
1379 ctrl &= ~SDHCI_CTRL_HISPD;
1380
Arindam Nathd6d50a12011-05-05 12:18:59 +05301381 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301382 u16 clk, ctrl_2;
1383 unsigned int clock;
1384
1385 /* In case of UHS-I modes, set High Speed Enable */
1386 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1387 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1388 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1389 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1390 (ios->timing == MMC_TIMING_UHS_SDR12))
1391 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301392
1393 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1394 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301395 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301396 /*
1397 * We only need to set Driver Strength if the
1398 * preset value enable is not set.
1399 */
1400 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1401 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1402 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1403 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1404 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1405
1406 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301407 } else {
1408 /*
1409 * According to SDHC Spec v3.00, if the Preset Value
1410 * Enable in the Host Control 2 register is set, we
1411 * need to reset SD Clock Enable before changing High
1412 * Speed Enable to avoid generating clock gliches.
1413 */
Arindam Nath758535c2011-05-05 12:19:00 +05301414
1415 /* Reset SD Clock Enable */
1416 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1417 clk &= ~SDHCI_CLOCK_CARD_EN;
1418 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1419
1420 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1421
1422 /* Re-enable SD Clock */
1423 clock = host->clock;
1424 host->clock = 0;
1425 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301426 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301427
Arindam Nath49c468f2011-05-05 12:19:01 +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
Philip Rakity6322cdd2011-05-13 11:17:15 +05301434 if (host->ops->set_uhs_signaling)
1435 host->ops->set_uhs_signaling(host, ios->timing);
1436 else {
1437 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1438 /* Select Bus Speed Mode for host */
1439 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1440 if (ios->timing == MMC_TIMING_UHS_SDR12)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1442 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1443 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1444 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1446 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1447 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1448 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1449 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1450 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1451 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301452
1453 /* Re-enable SD Clock */
1454 clock = host->clock;
1455 host->clock = 0;
1456 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301457 } else
1458 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301459
Leandro Dorileob8352262007-07-25 23:47:04 +02001460 /*
1461 * Some (ENE) controllers go apeshit on some ios operation,
1462 * signalling timeout and CRC errors even on CMD0. Resetting
1463 * it on each ios seems to solve the problem.
1464 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001465 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001466 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1467
Pierre Ossman5f25a662006-10-04 02:15:39 -07001468 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001469 spin_unlock_irqrestore(&host->lock, flags);
1470}
1471
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001472static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1473{
1474 struct sdhci_host *host = mmc_priv(mmc);
1475
1476 sdhci_runtime_pm_get(host);
1477 sdhci_do_set_ios(host, ios);
1478 sdhci_runtime_pm_put(host);
1479}
1480
1481static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001484 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486 spin_lock_irqsave(&host->lock, flags);
1487
Pierre Ossman1e728592008-04-16 19:13:13 +02001488 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001489 is_readonly = 0;
1490 else if (host->ops->get_ro)
1491 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001492 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001493 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1494 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495
1496 spin_unlock_irqrestore(&host->lock, flags);
1497
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001498 /* This quirk needs to be replaced by a callback-function later */
1499 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1500 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501}
1502
Takashi Iwai82b0e232011-04-21 20:26:38 +02001503#define SAMPLE_COUNT 5
1504
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001505static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001506{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001507 int i, ro_count;
1508
Takashi Iwai82b0e232011-04-21 20:26:38 +02001509 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001510 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001511
1512 ro_count = 0;
1513 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001514 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001515 if (++ro_count > SAMPLE_COUNT / 2)
1516 return 1;
1517 }
1518 msleep(30);
1519 }
1520 return 0;
1521}
1522
Adrian Hunter20758b62011-08-29 16:42:12 +03001523static void sdhci_hw_reset(struct mmc_host *mmc)
1524{
1525 struct sdhci_host *host = mmc_priv(mmc);
1526
1527 if (host->ops && host->ops->hw_reset)
1528 host->ops->hw_reset(host);
1529}
1530
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001531static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001532{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001533 struct sdhci_host *host = mmc_priv(mmc);
1534 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001535
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001536 sdhci_runtime_pm_get(host);
1537 ret = sdhci_do_get_ro(host);
1538 sdhci_runtime_pm_put(host);
1539 return ret;
1540}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001541
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001542static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1543{
Pierre Ossman1e728592008-04-16 19:13:13 +02001544 if (host->flags & SDHCI_DEVICE_DEAD)
1545 goto out;
1546
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001547 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001548 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1549 else
1550 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1551
1552 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1553 if (host->runtime_suspended)
1554 goto out;
1555
1556 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001557 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1558 else
1559 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001560out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001561 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001562}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001563
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001564static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1565{
1566 struct sdhci_host *host = mmc_priv(mmc);
1567 unsigned long flags;
1568
1569 spin_lock_irqsave(&host->lock, flags);
1570 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001571 spin_unlock_irqrestore(&host->lock, flags);
1572}
1573
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001574static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1575 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301576{
Arindam Nathf2119df2011-05-05 12:18:57 +05301577 u8 pwr;
1578 u16 clk, ctrl;
1579 u32 present_state;
1580
Arindam Nathf2119df2011-05-05 12:18:57 +05301581 /*
1582 * Signal Voltage Switching is only applicable for Host Controllers
1583 * v3.00 and above.
1584 */
1585 if (host->version < SDHCI_SPEC_300)
1586 return 0;
1587
1588 /*
1589 * We first check whether the request is to set signalling voltage
1590 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1591 */
1592 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1593 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1594 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1595 ctrl &= ~SDHCI_CTRL_VDD_180;
1596 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1597
1598 /* Wait for 5ms */
1599 usleep_range(5000, 5500);
1600
1601 /* 3.3V regulator output should be stable within 5 ms */
1602 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1603 if (!(ctrl & SDHCI_CTRL_VDD_180))
1604 return 0;
1605 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301606 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301607 "signalling voltage failed\n");
1608 return -EIO;
1609 }
1610 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1611 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1612 /* Stop SDCLK */
1613 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1614 clk &= ~SDHCI_CLOCK_CARD_EN;
1615 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1616
1617 /* Check whether DAT[3:0] is 0000 */
1618 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1619 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1620 SDHCI_DATA_LVL_SHIFT)) {
1621 /*
1622 * Enable 1.8V Signal Enable in the Host Control2
1623 * register
1624 */
1625 ctrl |= SDHCI_CTRL_VDD_180;
1626 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1627
1628 /* Wait for 5ms */
1629 usleep_range(5000, 5500);
1630
1631 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1632 if (ctrl & SDHCI_CTRL_VDD_180) {
1633 /* Provide SDCLK again and wait for 1ms*/
1634 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1635 clk |= SDHCI_CLOCK_CARD_EN;
1636 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1637 usleep_range(1000, 1500);
1638
1639 /*
1640 * If DAT[3:0] level is 1111b, then the card
1641 * was successfully switched to 1.8V signaling.
1642 */
1643 present_state = sdhci_readl(host,
1644 SDHCI_PRESENT_STATE);
1645 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1646 SDHCI_DATA_LVL_MASK)
1647 return 0;
1648 }
1649 }
1650
1651 /*
1652 * If we are here, that means the switch to 1.8V signaling
1653 * failed. We power cycle the card, and retry initialization
1654 * sequence by setting S18R to 0.
1655 */
1656 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1657 pwr &= ~SDHCI_POWER_ON;
1658 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1659
1660 /* Wait for 1ms as per the spec */
1661 usleep_range(1000, 1500);
1662 pwr |= SDHCI_POWER_ON;
1663 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1664
Girish K Sa3c76eb2011-10-11 11:44:09 +05301665 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301666 "voltage failed, retrying with S18R set to 0\n");
1667 return -EAGAIN;
1668 } else
1669 /* No signal voltage switch required */
1670 return 0;
1671}
1672
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001673static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1674 struct mmc_ios *ios)
1675{
1676 struct sdhci_host *host = mmc_priv(mmc);
1677 int err;
1678
1679 if (host->version < SDHCI_SPEC_300)
1680 return 0;
1681 sdhci_runtime_pm_get(host);
1682 err = sdhci_do_start_signal_voltage_switch(host, ios);
1683 sdhci_runtime_pm_put(host);
1684 return err;
1685}
1686
Arindam Nathb513ea22011-05-05 12:19:04 +05301687static int sdhci_execute_tuning(struct mmc_host *mmc)
1688{
1689 struct sdhci_host *host;
1690 u16 ctrl;
1691 u32 ier;
1692 int tuning_loop_counter = MAX_TUNING_LOOP;
1693 unsigned long timeout;
1694 int err = 0;
1695
1696 host = mmc_priv(mmc);
1697
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001698 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301699 disable_irq(host->irq);
1700 spin_lock(&host->lock);
1701
1702 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1703
1704 /*
1705 * Host Controller needs tuning only in case of SDR104 mode
1706 * and for SDR50 mode when Use Tuning for SDR50 is set in
1707 * Capabilities register.
1708 */
1709 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1710 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1711 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1712 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1713 else {
1714 spin_unlock(&host->lock);
1715 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001716 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301717 return 0;
1718 }
1719
1720 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1721
1722 /*
1723 * As per the Host Controller spec v3.00, tuning command
1724 * generates Buffer Read Ready interrupt, so enable that.
1725 *
1726 * Note: The spec clearly says that when tuning sequence
1727 * is being performed, the controller does not generate
1728 * interrupts other than Buffer Read Ready interrupt. But
1729 * to make sure we don't hit a controller bug, we _only_
1730 * enable Buffer Read Ready interrupt here.
1731 */
1732 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1733 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1734
1735 /*
1736 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1737 * of loops reaches 40 times or a timeout of 150ms occurs.
1738 */
1739 timeout = 150;
1740 do {
1741 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001742 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301743
1744 if (!tuning_loop_counter && !timeout)
1745 break;
1746
1747 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1748 cmd.arg = 0;
1749 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1750 cmd.retries = 0;
1751 cmd.data = NULL;
1752 cmd.error = 0;
1753
1754 mrq.cmd = &cmd;
1755 host->mrq = &mrq;
1756
1757 /*
1758 * In response to CMD19, the card sends 64 bytes of tuning
1759 * block to the Host Controller. So we set the block size
1760 * to 64 here.
1761 */
1762 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1763
1764 /*
1765 * The tuning block is sent by the card to the host controller.
1766 * So we set the TRNS_READ bit in the Transfer Mode register.
1767 * This also takes care of setting DMA Enable and Multi Block
1768 * Select in the same register to 0.
1769 */
1770 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1771
1772 sdhci_send_command(host, &cmd);
1773
1774 host->cmd = NULL;
1775 host->mrq = NULL;
1776
1777 spin_unlock(&host->lock);
1778 enable_irq(host->irq);
1779
1780 /* Wait for Buffer Read Ready interrupt */
1781 wait_event_interruptible_timeout(host->buf_ready_int,
1782 (host->tuning_done == 1),
1783 msecs_to_jiffies(50));
1784 disable_irq(host->irq);
1785 spin_lock(&host->lock);
1786
1787 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301788 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301789 "Buffer Read Ready interrupt during tuning "
1790 "procedure, falling back to fixed sampling "
1791 "clock\n");
1792 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1793 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1794 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1795 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1796
1797 err = -EIO;
1798 goto out;
1799 }
1800
1801 host->tuning_done = 0;
1802
1803 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1804 tuning_loop_counter--;
1805 timeout--;
1806 mdelay(1);
1807 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1808
1809 /*
1810 * The Host Driver has exhausted the maximum number of loops allowed,
1811 * so use fixed sampling frequency.
1812 */
1813 if (!tuning_loop_counter || !timeout) {
1814 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1815 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1816 } else {
1817 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301818 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301819 " failed, falling back to fixed sampling"
1820 " clock\n");
1821 err = -EIO;
1822 }
1823 }
1824
1825out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301826 /*
1827 * If this is the very first time we are here, we start the retuning
1828 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1829 * flag won't be set, we check this condition before actually starting
1830 * the timer.
1831 */
1832 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1833 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1834 mod_timer(&host->tuning_timer, jiffies +
1835 host->tuning_count * HZ);
1836 /* Tuning mode 1 limits the maximum data length to 4MB */
1837 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1838 } else {
1839 host->flags &= ~SDHCI_NEEDS_RETUNING;
1840 /* Reload the new initial value for timer */
1841 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1842 mod_timer(&host->tuning_timer, jiffies +
1843 host->tuning_count * HZ);
1844 }
1845
1846 /*
1847 * In case tuning fails, host controllers which support re-tuning can
1848 * try tuning again at a later time, when the re-tuning timer expires.
1849 * So for these controllers, we return 0. Since there might be other
1850 * controllers who do not have this capability, we return error for
1851 * them.
1852 */
1853 if (err && host->tuning_count &&
1854 host->tuning_mode == SDHCI_TUNING_MODE_1)
1855 err = 0;
1856
Arindam Nathb513ea22011-05-05 12:19:04 +05301857 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1858 spin_unlock(&host->lock);
1859 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001860 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301861
1862 return err;
1863}
1864
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001865static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301866{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301867 u16 ctrl;
1868 unsigned long flags;
1869
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301870 /* Host Controller v3.00 defines preset value registers */
1871 if (host->version < SDHCI_SPEC_300)
1872 return;
1873
1874 spin_lock_irqsave(&host->lock, flags);
1875
1876 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1877
1878 /*
1879 * We only enable or disable Preset Value if they are not already
1880 * enabled or disabled respectively. Otherwise, we bail out.
1881 */
1882 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1883 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1884 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001885 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301886 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1887 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1888 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001889 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301890 }
1891
1892 spin_unlock_irqrestore(&host->lock, flags);
1893}
1894
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001895static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1896{
1897 struct sdhci_host *host = mmc_priv(mmc);
1898
1899 sdhci_runtime_pm_get(host);
1900 sdhci_do_enable_preset_value(host, enable);
1901 sdhci_runtime_pm_put(host);
1902}
1903
David Brownellab7aefd2006-11-12 17:55:30 -08001904static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001905 .request = sdhci_request,
1906 .set_ios = sdhci_set_ios,
1907 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001908 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001909 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301910 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301911 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301912 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913};
1914
1915/*****************************************************************************\
1916 * *
1917 * Tasklets *
1918 * *
1919\*****************************************************************************/
1920
1921static void sdhci_tasklet_card(unsigned long param)
1922{
1923 struct sdhci_host *host;
1924 unsigned long flags;
1925
1926 host = (struct sdhci_host*)param;
1927
1928 spin_lock_irqsave(&host->lock, flags);
1929
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001930 /* Check host->mrq first in case we are runtime suspended */
1931 if (host->mrq &&
1932 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301933 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001934 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301935 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001936 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001937
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001938 sdhci_reset(host, SDHCI_RESET_CMD);
1939 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001940
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001941 host->mrq->cmd->error = -ENOMEDIUM;
1942 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001943 }
1944
1945 spin_unlock_irqrestore(&host->lock, flags);
1946
Pierre Ossman04cf5852008-08-18 22:18:14 +02001947 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001948}
1949
1950static void sdhci_tasklet_finish(unsigned long param)
1951{
1952 struct sdhci_host *host;
1953 unsigned long flags;
1954 struct mmc_request *mrq;
1955
1956 host = (struct sdhci_host*)param;
1957
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001958 spin_lock_irqsave(&host->lock, flags);
1959
Chris Ball0c9c99a2011-04-27 17:35:31 -04001960 /*
1961 * If this tasklet gets rescheduled while running, it will
1962 * be run again afterwards but without any active request.
1963 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001964 if (!host->mrq) {
1965 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001966 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001967 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001968
1969 del_timer(&host->timer);
1970
1971 mrq = host->mrq;
1972
Pierre Ossmand129bce2006-03-24 03:18:17 -08001973 /*
1974 * The controller needs a reset of internal state machines
1975 * upon error conditions.
1976 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001977 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001978 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001979 (mrq->data && (mrq->data->error ||
1980 (mrq->data->stop && mrq->data->stop->error))) ||
1981 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001982
1983 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001984 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001985 unsigned int clock;
1986
1987 /* This is to force an update */
1988 clock = host->clock;
1989 host->clock = 0;
1990 sdhci_set_clock(host, clock);
1991 }
1992
1993 /* Spec says we should do both at the same time, but Ricoh
1994 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001995 sdhci_reset(host, SDHCI_RESET_CMD);
1996 sdhci_reset(host, SDHCI_RESET_DATA);
1997 }
1998
1999 host->mrq = NULL;
2000 host->cmd = NULL;
2001 host->data = NULL;
2002
Pierre Ossmanf9134312008-12-21 17:01:48 +01002003#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002004 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002005#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002006
Pierre Ossman5f25a662006-10-04 02:15:39 -07002007 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002008 spin_unlock_irqrestore(&host->lock, flags);
2009
2010 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002011 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002012}
2013
2014static void sdhci_timeout_timer(unsigned long data)
2015{
2016 struct sdhci_host *host;
2017 unsigned long flags;
2018
2019 host = (struct sdhci_host*)data;
2020
2021 spin_lock_irqsave(&host->lock, flags);
2022
2023 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302024 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002025 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002026 sdhci_dumpregs(host);
2027
2028 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002029 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002030 sdhci_finish_data(host);
2031 } else {
2032 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002033 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002034 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002035 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002036
2037 tasklet_schedule(&host->finish_tasklet);
2038 }
2039 }
2040
Pierre Ossman5f25a662006-10-04 02:15:39 -07002041 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002042 spin_unlock_irqrestore(&host->lock, flags);
2043}
2044
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302045static void sdhci_tuning_timer(unsigned long data)
2046{
2047 struct sdhci_host *host;
2048 unsigned long flags;
2049
2050 host = (struct sdhci_host *)data;
2051
2052 spin_lock_irqsave(&host->lock, flags);
2053
2054 host->flags |= SDHCI_NEEDS_RETUNING;
2055
2056 spin_unlock_irqrestore(&host->lock, flags);
2057}
2058
Pierre Ossmand129bce2006-03-24 03:18:17 -08002059/*****************************************************************************\
2060 * *
2061 * Interrupt handling *
2062 * *
2063\*****************************************************************************/
2064
2065static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2066{
2067 BUG_ON(intmask == 0);
2068
2069 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302070 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002071 "though no command operation was in progress.\n",
2072 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002073 sdhci_dumpregs(host);
2074 return;
2075 }
2076
Pierre Ossman43b58b32007-07-25 23:15:27 +02002077 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002078 host->cmd->error = -ETIMEDOUT;
2079 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2080 SDHCI_INT_INDEX))
2081 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082
Pierre Ossmane8095172008-07-25 01:09:08 +02002083 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002084 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002085 return;
2086 }
2087
2088 /*
2089 * The host can send and interrupt when the busy state has
2090 * ended, allowing us to wait without wasting CPU cycles.
2091 * Unfortunately this is overloaded on the "data complete"
2092 * interrupt, so we need to take some care when handling
2093 * it.
2094 *
2095 * Note: The 1.0 specification is a bit ambiguous about this
2096 * feature so there might be some problems with older
2097 * controllers.
2098 */
2099 if (host->cmd->flags & MMC_RSP_BUSY) {
2100 if (host->cmd->data)
2101 DBG("Cannot wait for busy signal when also "
2102 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002103 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002104 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002105
2106 /* The controller does not support the end-of-busy IRQ,
2107 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002108 }
2109
2110 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002111 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002112}
2113
George G. Davis0957c332010-02-18 12:32:12 -05002114#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002115static void sdhci_show_adma_error(struct sdhci_host *host)
2116{
2117 const char *name = mmc_hostname(host->mmc);
2118 u8 *desc = host->adma_desc;
2119 __le32 *dma;
2120 __le16 *len;
2121 u8 attr;
2122
2123 sdhci_dumpregs(host);
2124
2125 while (true) {
2126 dma = (__le32 *)(desc + 4);
2127 len = (__le16 *)(desc + 2);
2128 attr = *desc;
2129
2130 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2131 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2132
2133 desc += 8;
2134
2135 if (attr & 2)
2136 break;
2137 }
2138}
2139#else
2140static void sdhci_show_adma_error(struct sdhci_host *host) { }
2141#endif
2142
Pierre Ossmand129bce2006-03-24 03:18:17 -08002143static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2144{
2145 BUG_ON(intmask == 0);
2146
Arindam Nathb513ea22011-05-05 12:19:04 +05302147 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2148 if (intmask & SDHCI_INT_DATA_AVAIL) {
2149 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2150 MMC_SEND_TUNING_BLOCK) {
2151 host->tuning_done = 1;
2152 wake_up(&host->buf_ready_int);
2153 return;
2154 }
2155 }
2156
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157 if (!host->data) {
2158 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002159 * The "data complete" interrupt is also used to
2160 * indicate that a busy state has ended. See comment
2161 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002162 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002163 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2164 if (intmask & SDHCI_INT_DATA_END) {
2165 sdhci_finish_command(host);
2166 return;
2167 }
2168 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169
Girish K Sa3c76eb2011-10-11 11:44:09 +05302170 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002171 "though no data operation was in progress.\n",
2172 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173 sdhci_dumpregs(host);
2174
2175 return;
2176 }
2177
2178 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002179 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002180 else if (intmask & SDHCI_INT_DATA_END_BIT)
2181 host->data->error = -EILSEQ;
2182 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2183 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2184 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002185 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002186 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302187 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002188 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002189 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002190 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191
Pierre Ossman17b04292007-07-22 22:18:46 +02002192 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002193 sdhci_finish_data(host);
2194 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002195 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002196 sdhci_transfer_pio(host);
2197
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002198 /*
2199 * We currently don't do anything fancy with DMA
2200 * boundaries, but as we can't disable the feature
2201 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002202 *
2203 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2204 * should return a valid address to continue from, but as
2205 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002206 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002207 if (intmask & SDHCI_INT_DMA_END) {
2208 u32 dmastart, dmanow;
2209 dmastart = sg_dma_address(host->data->sg);
2210 dmanow = dmastart + host->data->bytes_xfered;
2211 /*
2212 * Force update to the next DMA block boundary.
2213 */
2214 dmanow = (dmanow &
2215 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2216 SDHCI_DEFAULT_BOUNDARY_SIZE;
2217 host->data->bytes_xfered = dmanow - dmastart;
2218 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2219 " next 0x%08x\n",
2220 mmc_hostname(host->mmc), dmastart,
2221 host->data->bytes_xfered, dmanow);
2222 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2223 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002224
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002225 if (intmask & SDHCI_INT_DATA_END) {
2226 if (host->cmd) {
2227 /*
2228 * Data managed to finish before the
2229 * command completed. Make sure we do
2230 * things in the proper order.
2231 */
2232 host->data_early = 1;
2233 } else {
2234 sdhci_finish_data(host);
2235 }
2236 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002237 }
2238}
2239
David Howells7d12e782006-10-05 14:55:46 +01002240static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241{
2242 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002243 struct sdhci_host *host = dev_id;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002245 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002246
2247 spin_lock(&host->lock);
2248
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002249 if (host->runtime_suspended) {
2250 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302251 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002252 mmc_hostname(host->mmc));
2253 return IRQ_HANDLED;
2254 }
2255
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002256 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257
Mark Lord62df67a52007-03-06 13:30:13 +01002258 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002259 result = IRQ_NONE;
2260 goto out;
2261 }
2262
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002263 DBG("*** %s got interrupt: 0x%08x\n",
2264 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265
Pierre Ossman3192a282006-06-30 02:22:26 -07002266 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002267 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2268 SDHCI_CARD_PRESENT;
2269
2270 /*
2271 * There is a observation on i.mx esdhc. INSERT bit will be
2272 * immediately set again when it gets cleared, if a card is
2273 * inserted. We have to mask the irq to prevent interrupt
2274 * storm which will freeze the system. And the REMOVE gets
2275 * the same situation.
2276 *
2277 * More testing are needed here to ensure it works for other
2278 * platforms though.
2279 */
2280 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2281 SDHCI_INT_CARD_REMOVE);
2282 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2283 SDHCI_INT_CARD_INSERT);
2284
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002285 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002286 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2287 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002289 }
2290
Pierre Ossmand129bce2006-03-24 03:18:17 -08002291 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002292 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2293 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002294 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295 }
2296
2297 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002298 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2299 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002300 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301 }
2302
2303 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2304
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002305 intmask &= ~SDHCI_INT_ERROR;
2306
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302308 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002310 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002311 }
2312
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002313 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002314
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002315 if (intmask & SDHCI_INT_CARD_INT)
2316 cardint = 1;
2317
2318 intmask &= ~SDHCI_INT_CARD_INT;
2319
Pierre Ossman3192a282006-06-30 02:22:26 -07002320 if (intmask) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302321 pr_err("%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002322 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323 sdhci_dumpregs(host);
2324
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002325 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002326 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327
2328 result = IRQ_HANDLED;
2329
Pierre Ossman5f25a662006-10-04 02:15:39 -07002330 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002331out:
2332 spin_unlock(&host->lock);
2333
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002334 /*
2335 * We have to delay this as it calls back into the driver.
2336 */
2337 if (cardint)
2338 mmc_signal_sdio_irq(host->mmc);
2339
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340 return result;
2341}
2342
2343/*****************************************************************************\
2344 * *
2345 * Suspend/resume *
2346 * *
2347\*****************************************************************************/
2348
2349#ifdef CONFIG_PM
2350
Manuel Lauss29495aa2011-11-03 11:09:45 +01002351int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002353 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002355 sdhci_disable_card_detection(host);
2356
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302357 /* Disable tuning since we are suspending */
2358 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2359 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2360 host->flags &= ~SDHCI_NEEDS_RETUNING;
2361 mod_timer(&host->tuning_timer, jiffies +
2362 host->tuning_count * HZ);
2363 }
2364
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002365 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002366 if (ret)
2367 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002369 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002370
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002371 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372}
2373
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002374EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002375
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002376int sdhci_resume_host(struct sdhci_host *host)
2377{
2378 int ret;
2379
Richard Röjforsa13abc72009-09-22 16:45:30 -07002380 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002381 if (host->ops->enable_dma)
2382 host->ops->enable_dma(host);
2383 }
2384
2385 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2386 mmc_hostname(host->mmc), host);
2387 if (ret)
2388 return ret;
2389
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002390 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002391 mmiowb();
2392
2393 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002394 sdhci_enable_card_detection(host);
2395
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302396 /* Set the re-tuning expiration flag */
2397 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2398 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2399 host->flags |= SDHCI_NEEDS_RETUNING;
2400
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002401 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002402}
2403
2404EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405
Daniel Drake5f619702010-11-04 22:20:39 +00002406void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2407{
2408 u8 val;
2409 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2410 val |= SDHCI_WAKE_ON_INT;
2411 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2412}
2413
2414EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2415
Pierre Ossmand129bce2006-03-24 03:18:17 -08002416#endif /* CONFIG_PM */
2417
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002418#ifdef CONFIG_PM_RUNTIME
2419
2420static int sdhci_runtime_pm_get(struct sdhci_host *host)
2421{
2422 return pm_runtime_get_sync(host->mmc->parent);
2423}
2424
2425static int sdhci_runtime_pm_put(struct sdhci_host *host)
2426{
2427 pm_runtime_mark_last_busy(host->mmc->parent);
2428 return pm_runtime_put_autosuspend(host->mmc->parent);
2429}
2430
2431int sdhci_runtime_suspend_host(struct sdhci_host *host)
2432{
2433 unsigned long flags;
2434 int ret = 0;
2435
2436 /* Disable tuning since we are suspending */
2437 if (host->version >= SDHCI_SPEC_300 &&
2438 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2439 del_timer_sync(&host->tuning_timer);
2440 host->flags &= ~SDHCI_NEEDS_RETUNING;
2441 }
2442
2443 spin_lock_irqsave(&host->lock, flags);
2444 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2445 spin_unlock_irqrestore(&host->lock, flags);
2446
2447 synchronize_irq(host->irq);
2448
2449 spin_lock_irqsave(&host->lock, flags);
2450 host->runtime_suspended = true;
2451 spin_unlock_irqrestore(&host->lock, flags);
2452
2453 return ret;
2454}
2455EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2456
2457int sdhci_runtime_resume_host(struct sdhci_host *host)
2458{
2459 unsigned long flags;
2460 int ret = 0, host_flags = host->flags;
2461
2462 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2463 if (host->ops->enable_dma)
2464 host->ops->enable_dma(host);
2465 }
2466
2467 sdhci_init(host, 0);
2468
2469 /* Force clock and power re-program */
2470 host->pwr = 0;
2471 host->clock = 0;
2472 sdhci_do_set_ios(host, &host->mmc->ios);
2473
2474 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2475 if (host_flags & SDHCI_PV_ENABLED)
2476 sdhci_do_enable_preset_value(host, true);
2477
2478 /* Set the re-tuning expiration flag */
2479 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2480 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2481 host->flags |= SDHCI_NEEDS_RETUNING;
2482
2483 spin_lock_irqsave(&host->lock, flags);
2484
2485 host->runtime_suspended = false;
2486
2487 /* Enable SDIO IRQ */
2488 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2489 sdhci_enable_sdio_irq_nolock(host, true);
2490
2491 /* Enable Card Detection */
2492 sdhci_enable_card_detection(host);
2493
2494 spin_unlock_irqrestore(&host->lock, flags);
2495
2496 return ret;
2497}
2498EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2499
2500#endif
2501
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502/*****************************************************************************\
2503 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002504 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002505 * *
2506\*****************************************************************************/
2507
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002508struct sdhci_host *sdhci_alloc_host(struct device *dev,
2509 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002510{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002511 struct mmc_host *mmc;
2512 struct sdhci_host *host;
2513
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002514 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002515
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002516 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002517 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002518 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519
2520 host = mmc_priv(mmc);
2521 host->mmc = mmc;
2522
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002523 return host;
2524}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002525
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002526EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002528int sdhci_add_host(struct sdhci_host *host)
2529{
2530 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302531 u32 caps[2];
2532 u32 max_current_caps;
2533 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002534 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002535
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002536 WARN_ON(host == NULL);
2537 if (host == NULL)
2538 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002539
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002540 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002541
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002542 if (debug_quirks)
2543 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002544 if (debug_quirks2)
2545 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002546
Pierre Ossmand96649e2006-06-30 02:22:30 -07002547 sdhci_reset(host, SDHCI_RESET_ALL);
2548
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002549 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002550 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2551 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002552 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302553 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002554 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002555 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002556 }
2557
Arindam Nathf2119df2011-05-05 12:18:57 +05302558 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002559 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002560
Arindam Nathf2119df2011-05-05 12:18:57 +05302561 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2562 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2563
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002564 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002565 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302566 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002567 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002568 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002569 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002571 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002572 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002573 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002574 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002575 }
2576
Arindam Nathf2119df2011-05-05 12:18:57 +05302577 if ((host->version >= SDHCI_SPEC_200) &&
2578 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002579 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002580
2581 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2582 (host->flags & SDHCI_USE_ADMA)) {
2583 DBG("Disabling ADMA as it is marked broken\n");
2584 host->flags &= ~SDHCI_USE_ADMA;
2585 }
2586
Richard Röjforsa13abc72009-09-22 16:45:30 -07002587 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002588 if (host->ops->enable_dma) {
2589 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302590 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002591 "available. Falling back to PIO.\n",
2592 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002593 host->flags &=
2594 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002595 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002596 }
2597 }
2598
Pierre Ossman2134a922008-06-28 18:28:51 +02002599 if (host->flags & SDHCI_USE_ADMA) {
2600 /*
2601 * We need to allocate descriptors for all sg entries
2602 * (128) and potentially one alignment transfer for
2603 * each of those entries.
2604 */
2605 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2606 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2607 if (!host->adma_desc || !host->align_buffer) {
2608 kfree(host->adma_desc);
2609 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302610 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002611 "buffers. Falling back to standard DMA.\n",
2612 mmc_hostname(mmc));
2613 host->flags &= ~SDHCI_USE_ADMA;
2614 }
2615 }
2616
Pierre Ossman76591502008-07-21 00:32:11 +02002617 /*
2618 * If we use DMA, then it's up to the caller to set the DMA
2619 * mask, but PIO does not need the hw shim so we set a new
2620 * mask here in that case.
2621 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002622 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002623 host->dma_mask = DMA_BIT_MASK(64);
2624 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2625 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002627 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302628 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002629 >> SDHCI_CLOCK_BASE_SHIFT;
2630 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302631 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002632 >> SDHCI_CLOCK_BASE_SHIFT;
2633
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002635 if (host->max_clk == 0 || host->quirks &
2636 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002637 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302638 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002639 "frequency.\n", mmc_hostname(mmc));
2640 return -ENODEV;
2641 }
2642 host->max_clk = host->ops->get_max_clock(host);
2643 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002644
2645 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302646 * In case of Host Controller v3.00, find out whether clock
2647 * multiplier is supported.
2648 */
2649 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2650 SDHCI_CLOCK_MUL_SHIFT;
2651
2652 /*
2653 * In case the value in Clock Multiplier is 0, then programmable
2654 * clock mode is not supported, otherwise the actual clock
2655 * multiplier is one more than the value of Clock Multiplier
2656 * in the Capabilities Register.
2657 */
2658 if (host->clk_mul)
2659 host->clk_mul += 1;
2660
2661 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002662 * Set host parameters.
2663 */
2664 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302665 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002666 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002667 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302668 else if (host->version >= SDHCI_SPEC_300) {
2669 if (host->clk_mul) {
2670 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2671 mmc->f_max = host->max_clk * host->clk_mul;
2672 } else
2673 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2674 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002675 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002676
Andy Shevchenko272308c2011-08-03 18:36:00 +03002677 host->timeout_clk =
2678 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2679 if (host->timeout_clk == 0) {
2680 if (host->ops->get_timeout_clock) {
2681 host->timeout_clk = host->ops->get_timeout_clock(host);
2682 } else if (!(host->quirks &
2683 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302684 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002685 "frequency.\n", mmc_hostname(mmc));
2686 return -ENODEV;
2687 }
2688 }
2689 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2690 host->timeout_clk *= 1000;
2691
2692 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002693 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002694
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002695 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002696
Andrei Warkentine89d4562011-05-23 15:06:37 -05002697 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2698
2699 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2700 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002701
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002702 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002703 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002704 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002705 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002706 host->flags |= SDHCI_AUTO_CMD23;
2707 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2708 } else {
2709 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2710 }
2711
Philip Rakity15ec4462010-11-19 16:48:39 -05002712 /*
2713 * A controller may support 8-bit width, but the board itself
2714 * might not have the pins brought out. Boards that support
2715 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2716 * their platform code before calling sdhci_add_host(), and we
2717 * won't assume 8-bit width for hosts without that CAP.
2718 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002719 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002720 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002721
Arindam Nathf2119df2011-05-05 12:18:57 +05302722 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002723 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002724
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002725 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2726 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002727 mmc->caps |= MMC_CAP_NEEDS_POLL;
2728
Arindam Nathf2119df2011-05-05 12:18:57 +05302729 /* UHS-I mode(s) supported by the host controller. */
2730 if (host->version >= SDHCI_SPEC_300)
2731 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2732
2733 /* SDR104 supports also implies SDR50 support */
2734 if (caps[1] & SDHCI_SUPPORT_SDR104)
2735 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2736 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2737 mmc->caps |= MMC_CAP_UHS_SDR50;
2738
2739 if (caps[1] & SDHCI_SUPPORT_DDR50)
2740 mmc->caps |= MMC_CAP_UHS_DDR50;
2741
Arindam Nathb513ea22011-05-05 12:19:04 +05302742 /* Does the host needs tuning for SDR50? */
2743 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2744 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2745
Arindam Nathd6d50a12011-05-05 12:18:59 +05302746 /* Driver Type(s) (A, C, D) supported by the host */
2747 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2748 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2749 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2750 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2751 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2752 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2753
Girish K Sbec87262011-10-13 12:04:16 +05302754 /*
2755 * If Power Off Notify capability is enabled by the host,
2756 * set notify to short power off notify timeout value.
2757 */
2758 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2759 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2760 else
2761 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2762
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302763 /* Initial value for re-tuning timer count */
2764 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2765 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2766
2767 /*
2768 * In case Re-tuning Timer is not disabled, the actual value of
2769 * re-tuning timer will be 2 ^ (n - 1).
2770 */
2771 if (host->tuning_count)
2772 host->tuning_count = 1 << (host->tuning_count - 1);
2773
2774 /* Re-tuning mode supported by the Host Controller */
2775 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2776 SDHCI_RETUNING_MODE_SHIFT;
2777
Takashi Iwai8f230f42010-12-08 10:04:30 +01002778 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302779 /*
2780 * According to SD Host Controller spec v3.00, if the Host System
2781 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2782 * the value is meaningful only if Voltage Support in the Capabilities
2783 * register is set. The actual current value is 4 times the register
2784 * value.
2785 */
2786 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2787
2788 if (caps[0] & SDHCI_CAN_VDD_330) {
2789 int max_current_330;
2790
Takashi Iwai8f230f42010-12-08 10:04:30 +01002791 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302792
2793 max_current_330 = ((max_current_caps &
2794 SDHCI_MAX_CURRENT_330_MASK) >>
2795 SDHCI_MAX_CURRENT_330_SHIFT) *
2796 SDHCI_MAX_CURRENT_MULTIPLIER;
2797
2798 if (max_current_330 > 150)
2799 mmc->caps |= MMC_CAP_SET_XPC_330;
2800 }
2801 if (caps[0] & SDHCI_CAN_VDD_300) {
2802 int max_current_300;
2803
Takashi Iwai8f230f42010-12-08 10:04:30 +01002804 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302805
2806 max_current_300 = ((max_current_caps &
2807 SDHCI_MAX_CURRENT_300_MASK) >>
2808 SDHCI_MAX_CURRENT_300_SHIFT) *
2809 SDHCI_MAX_CURRENT_MULTIPLIER;
2810
2811 if (max_current_300 > 150)
2812 mmc->caps |= MMC_CAP_SET_XPC_300;
2813 }
2814 if (caps[0] & SDHCI_CAN_VDD_180) {
2815 int max_current_180;
2816
Takashi Iwai8f230f42010-12-08 10:04:30 +01002817 ocr_avail |= MMC_VDD_165_195;
2818
Arindam Nathf2119df2011-05-05 12:18:57 +05302819 max_current_180 = ((max_current_caps &
2820 SDHCI_MAX_CURRENT_180_MASK) >>
2821 SDHCI_MAX_CURRENT_180_SHIFT) *
2822 SDHCI_MAX_CURRENT_MULTIPLIER;
2823
2824 if (max_current_180 > 150)
2825 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302826
2827 /* Maximum current capabilities of the host at 1.8V */
2828 if (max_current_180 >= 800)
2829 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2830 else if (max_current_180 >= 600)
2831 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2832 else if (max_current_180 >= 400)
2833 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2834 else
2835 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302836 }
2837
Takashi Iwai8f230f42010-12-08 10:04:30 +01002838 mmc->ocr_avail = ocr_avail;
2839 mmc->ocr_avail_sdio = ocr_avail;
2840 if (host->ocr_avail_sdio)
2841 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2842 mmc->ocr_avail_sd = ocr_avail;
2843 if (host->ocr_avail_sd)
2844 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2845 else /* normal SD controllers don't support 1.8V */
2846 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2847 mmc->ocr_avail_mmc = ocr_avail;
2848 if (host->ocr_avail_mmc)
2849 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002850
2851 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302852 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002853 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002854 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002855 }
2856
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857 spin_lock_init(&host->lock);
2858
2859 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002860 * Maximum number of segments. Depends on if the hardware
2861 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002863 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002864 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002865 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002866 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002867 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002868 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869
2870 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002871 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002872 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002873 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002874 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002875
2876 /*
2877 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002878 * of bytes. When doing hardware scatter/gather, each entry cannot
2879 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002880 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002881 if (host->flags & SDHCI_USE_ADMA) {
2882 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2883 mmc->max_seg_size = 65535;
2884 else
2885 mmc->max_seg_size = 65536;
2886 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002887 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002888 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002889
2890 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002891 * Maximum block size. This varies from controller to controller and
2892 * is specified in the capabilities register.
2893 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002894 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2895 mmc->max_blk_size = 2;
2896 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302897 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002898 SDHCI_MAX_BLOCK_SHIFT;
2899 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302900 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002901 "assuming 512 bytes\n", mmc_hostname(mmc));
2902 mmc->max_blk_size = 0;
2903 }
2904 }
2905
2906 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002907
2908 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002909 * Maximum block count.
2910 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002911 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002912
2913 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002914 * Init tasklets.
2915 */
2916 tasklet_init(&host->card_tasklet,
2917 sdhci_tasklet_card, (unsigned long)host);
2918 tasklet_init(&host->finish_tasklet,
2919 sdhci_tasklet_finish, (unsigned long)host);
2920
Al Viroe4cad1b2006-10-10 22:47:07 +01002921 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002922
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302923 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302924 init_waitqueue_head(&host->buf_ready_int);
2925
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302926 /* Initialize re-tuning timer */
2927 init_timer(&host->tuning_timer);
2928 host->tuning_timer.data = (unsigned long)host;
2929 host->tuning_timer.function = sdhci_tuning_timer;
2930 }
2931
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002932 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002933 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002934 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002935 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002936
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002937 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2938 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302939 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002940 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002941 }
2942
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002943 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002944
2945#ifdef CONFIG_MMC_DEBUG
2946 sdhci_dumpregs(host);
2947#endif
2948
Pierre Ossmanf9134312008-12-21 17:01:48 +01002949#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002950 snprintf(host->led_name, sizeof(host->led_name),
2951 "%s::", mmc_hostname(mmc));
2952 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002953 host->led.brightness = LED_OFF;
2954 host->led.default_trigger = mmc_hostname(mmc);
2955 host->led.brightness_set = sdhci_led_control;
2956
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002957 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002958 if (ret)
2959 goto reset;
2960#endif
2961
Pierre Ossman5f25a662006-10-04 02:15:39 -07002962 mmiowb();
2963
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964 mmc_add_host(mmc);
2965
Girish K Sa3c76eb2011-10-11 11:44:09 +05302966 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002967 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002968 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2969 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002971 sdhci_enable_card_detection(host);
2972
Pierre Ossmand129bce2006-03-24 03:18:17 -08002973 return 0;
2974
Pierre Ossmanf9134312008-12-21 17:01:48 +01002975#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002976reset:
2977 sdhci_reset(host, SDHCI_RESET_ALL);
2978 free_irq(host->irq, host);
2979#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002980untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002981 tasklet_kill(&host->card_tasklet);
2982 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002983
2984 return ret;
2985}
2986
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002987EXPORT_SYMBOL_GPL(sdhci_add_host);
2988
Pierre Ossman1e728592008-04-16 19:13:13 +02002989void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002990{
Pierre Ossman1e728592008-04-16 19:13:13 +02002991 unsigned long flags;
2992
2993 if (dead) {
2994 spin_lock_irqsave(&host->lock, flags);
2995
2996 host->flags |= SDHCI_DEVICE_DEAD;
2997
2998 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302999 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003000 " transfer!\n", mmc_hostname(host->mmc));
3001
3002 host->mrq->cmd->error = -ENOMEDIUM;
3003 tasklet_schedule(&host->finish_tasklet);
3004 }
3005
3006 spin_unlock_irqrestore(&host->lock, flags);
3007 }
3008
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003009 sdhci_disable_card_detection(host);
3010
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003011 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003012
Pierre Ossmanf9134312008-12-21 17:01:48 +01003013#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003014 led_classdev_unregister(&host->led);
3015#endif
3016
Pierre Ossman1e728592008-04-16 19:13:13 +02003017 if (!dead)
3018 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003019
3020 free_irq(host->irq, host);
3021
3022 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303023 if (host->version >= SDHCI_SPEC_300)
3024 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003025
3026 tasklet_kill(&host->card_tasklet);
3027 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003028
Adrian Hunterceb61432011-12-27 15:48:41 +02003029 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003030 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003031
Pierre Ossman2134a922008-06-28 18:28:51 +02003032 kfree(host->adma_desc);
3033 kfree(host->align_buffer);
3034
3035 host->adma_desc = NULL;
3036 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003037}
3038
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003039EXPORT_SYMBOL_GPL(sdhci_remove_host);
3040
3041void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003042{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003043 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044}
3045
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003046EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003047
3048/*****************************************************************************\
3049 * *
3050 * Driver init/exit *
3051 * *
3052\*****************************************************************************/
3053
3054static int __init sdhci_drv_init(void)
3055{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303056 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003057 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303058 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003059
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003060 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003061}
3062
3063static void __exit sdhci_drv_exit(void)
3064{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003065}
3066
3067module_init(sdhci_drv_init);
3068module_exit(sdhci_drv_exit);
3069
Pierre Ossmandf673b22006-06-30 02:22:31 -07003070module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003071module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003072
Pierre Ossman32710e82009-04-08 20:14:54 +02003073MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003074MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003075MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003076
Pierre Ossmandf673b22006-06-30 02:22:31 -07003077MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003078MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");