blob: 3d338b4b416af47ebcdfc3c9e59bfbe6a0a42ef7 [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) ||
Adrian Hunterc79396c2011-12-27 15:48:42 +0200150 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Shawn Guod25928d2011-06-21 22:41:48 +0800153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
155 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
156
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300157 if (enable)
158 sdhci_unmask_irqs(host, irqs);
159 else
160 sdhci_mask_irqs(host, irqs);
161}
162
163static void sdhci_enable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, true);
166}
167
168static void sdhci_disable_card_detection(struct sdhci_host *host)
169{
170 sdhci_set_card_detection(host, false);
171}
172
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173static void sdhci_reset(struct sdhci_host *host, u8 mask)
174{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700175 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300176 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700177
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100178 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300179 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700180 SDHCI_CARD_PRESENT))
181 return;
182 }
183
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300184 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
185 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
186
Philip Rakity393c1a32011-01-21 11:26:40 -0800187 if (host->ops->platform_reset_enter)
188 host->ops->platform_reset_enter(host, mask);
189
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300190 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191
Pierre Ossmane16514d2006-06-30 02:22:24 -0700192 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 host->clock = 0;
194
Pierre Ossmane16514d2006-06-30 02:22:24 -0700195 /* Wait max 100 ms */
196 timeout = 100;
197
198 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300199 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700200 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530201 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700202 mmc_hostname(host->mmc), (int)mask);
203 sdhci_dumpregs(host);
204 return;
205 }
206 timeout--;
207 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300209
Philip Rakity393c1a32011-01-21 11:26:40 -0800210 if (host->ops->platform_reset_exit)
211 host->ops->platform_reset_exit(host, mask);
212
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300213 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
214 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
218
219static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800221 if (soft)
222 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
223 else
224 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300226 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
227 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700228 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
229 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300230 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800231
232 if (soft) {
233 /* force clock reconfiguration */
234 host->clock = 0;
235 sdhci_set_ios(host->mmc, &host->mmc->ios);
236 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300237}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300239static void sdhci_reinit(struct sdhci_host *host)
240{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300242 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243}
244
245static void sdhci_activate_led(struct sdhci_host *host)
246{
247 u8 ctrl;
248
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300249 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300251 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252}
253
254static void sdhci_deactivate_led(struct sdhci_host *host)
255{
256 u8 ctrl;
257
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300260 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261}
262
Pierre Ossmanf9134312008-12-21 17:01:48 +0100263#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100264static void sdhci_led_control(struct led_classdev *led,
265 enum led_brightness brightness)
266{
267 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
268 unsigned long flags;
269
270 spin_lock_irqsave(&host->lock, flags);
271
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300272 if (host->runtime_suspended)
273 goto out;
274
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100275 if (brightness == LED_OFF)
276 sdhci_deactivate_led(host);
277 else
278 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300279out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100280 spin_unlock_irqrestore(&host->lock, flags);
281}
282#endif
283
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284/*****************************************************************************\
285 * *
286 * Core functions *
287 * *
288\*****************************************************************************/
289
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100290static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291{
Pierre Ossman76591502008-07-21 00:32:11 +0200292 unsigned long flags;
293 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700294 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200295 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100297 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800298
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200300 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301
Pierre Ossman76591502008-07-21 00:32:11 +0200302 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200305 if (!sg_miter_next(&host->sg_miter))
306 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossman76591502008-07-21 00:32:11 +0200308 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309
Pierre Ossman76591502008-07-21 00:32:11 +0200310 blksize -= len;
311 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200312
Pierre Ossman76591502008-07-21 00:32:11 +0200313 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossman76591502008-07-21 00:32:11 +0200315 while (len) {
316 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300317 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200318 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319 }
Pierre Ossman76591502008-07-21 00:32:11 +0200320
321 *buf = scratch & 0xFF;
322
323 buf++;
324 scratch >>= 8;
325 chunk--;
326 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327 }
328 }
Pierre Ossman76591502008-07-21 00:32:11 +0200329
330 sg_miter_stop(&host->sg_miter);
331
332 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100333}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800334
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100335static void sdhci_write_block_pio(struct sdhci_host *host)
336{
Pierre Ossman76591502008-07-21 00:32:11 +0200337 unsigned long flags;
338 size_t blksize, len, chunk;
339 u32 scratch;
340 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100341
342 DBG("PIO writing\n");
343
344 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200345 chunk = 0;
346 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347
Pierre Ossman76591502008-07-21 00:32:11 +0200348 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349
350 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200351 if (!sg_miter_next(&host->sg_miter))
352 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353
Pierre Ossman76591502008-07-21 00:32:11 +0200354 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 blksize -= len;
357 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
Pierre Ossman76591502008-07-21 00:32:11 +0200359 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 while (len) {
362 scratch |= (u32)*buf << (chunk * 8);
363
364 buf++;
365 chunk++;
366 len--;
367
368 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300369 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200370 chunk = 0;
371 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373 }
374 }
Pierre Ossman76591502008-07-21 00:32:11 +0200375
376 sg_miter_stop(&host->sg_miter);
377
378 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379}
380
381static void sdhci_transfer_pio(struct sdhci_host *host)
382{
383 u32 mask;
384
385 BUG_ON(!host->data);
386
Pierre Ossman76591502008-07-21 00:32:11 +0200387 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 return;
389
390 if (host->data->flags & MMC_DATA_READ)
391 mask = SDHCI_DATA_AVAILABLE;
392 else
393 mask = SDHCI_SPACE_AVAILABLE;
394
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200395 /*
396 * Some controllers (JMicron JMB38x) mess up the buffer bits
397 * for transfers < 4 bytes. As long as it is just one block,
398 * we can ignore the bits.
399 */
400 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
401 (host->data->blocks == 1))
402 mask = ~0;
403
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300404 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300405 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
406 udelay(100);
407
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408 if (host->data->flags & MMC_DATA_READ)
409 sdhci_read_block_pio(host);
410 else
411 sdhci_write_block_pio(host);
412
Pierre Ossman76591502008-07-21 00:32:11 +0200413 host->blocks--;
414 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100415 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416 }
417
418 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800419}
420
Pierre Ossman2134a922008-06-28 18:28:51 +0200421static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
422{
423 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800424 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200425}
426
427static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
428{
Cong Wang482fce92011-11-27 13:27:00 +0800429 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200430 local_irq_restore(*flags);
431}
432
Ben Dooks118cd172010-03-05 13:43:26 -0800433static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
434{
Ben Dooks9e506f32010-03-05 13:43:29 -0800435 __le32 *dataddr = (__le32 __force *)(desc + 4);
436 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800437
Ben Dooks9e506f32010-03-05 13:43:29 -0800438 /* SDHCI specification says ADMA descriptors should be 4 byte
439 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800440
Ben Dooks9e506f32010-03-05 13:43:29 -0800441 cmdlen[0] = cpu_to_le16(cmd);
442 cmdlen[1] = cpu_to_le16(len);
443
444 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800445}
446
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200447static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200448 struct mmc_data *data)
449{
450 int direction;
451
452 u8 *desc;
453 u8 *align;
454 dma_addr_t addr;
455 dma_addr_t align_addr;
456 int len, offset;
457
458 struct scatterlist *sg;
459 int i;
460 char *buffer;
461 unsigned long flags;
462
463 /*
464 * The spec does not specify endianness of descriptor table.
465 * We currently guess that it is LE.
466 */
467
468 if (data->flags & MMC_DATA_READ)
469 direction = DMA_FROM_DEVICE;
470 else
471 direction = DMA_TO_DEVICE;
472
473 /*
474 * The ADMA descriptor table is mapped further down as we
475 * need to fill it with data first.
476 */
477
478 host->align_addr = dma_map_single(mmc_dev(host->mmc),
479 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700480 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200481 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200482 BUG_ON(host->align_addr & 0x3);
483
484 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
485 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200486 if (host->sg_count == 0)
487 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200488
489 desc = host->adma_desc;
490 align = host->align_buffer;
491
492 align_addr = host->align_addr;
493
494 for_each_sg(data->sg, sg, host->sg_count, i) {
495 addr = sg_dma_address(sg);
496 len = sg_dma_len(sg);
497
498 /*
499 * The SDHCI specification states that ADMA
500 * addresses must be 32-bit aligned. If they
501 * aren't, then we use a bounce buffer for
502 * the (up to three) bytes that screw up the
503 * alignment.
504 */
505 offset = (4 - (addr & 0x3)) & 0x3;
506 if (offset) {
507 if (data->flags & MMC_DATA_WRITE) {
508 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200509 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200510 memcpy(align, buffer, offset);
511 sdhci_kunmap_atomic(buffer, &flags);
512 }
513
Ben Dooks118cd172010-03-05 13:43:26 -0800514 /* tran, valid */
515 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
517 BUG_ON(offset > 65536);
518
Pierre Ossman2134a922008-06-28 18:28:51 +0200519 align += 4;
520 align_addr += 4;
521
522 desc += 8;
523
524 addr += offset;
525 len -= offset;
526 }
527
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 BUG_ON(len > 65536);
529
Ben Dooks118cd172010-03-05 13:43:26 -0800530 /* tran, valid */
531 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200532 desc += 8;
533
534 /*
535 * If this triggers then we have a calculation bug
536 * somewhere. :/
537 */
538 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
539 }
540
Thomas Abraham70764a92010-05-26 14:42:04 -0700541 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
542 /*
543 * Mark the last descriptor as the terminating descriptor
544 */
545 if (desc != host->adma_desc) {
546 desc -= 8;
547 desc[0] |= 0x2; /* end */
548 }
549 } else {
550 /*
551 * Add a terminating entry.
552 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200553
Thomas Abraham70764a92010-05-26 14:42:04 -0700554 /* nop, end, valid */
555 sdhci_set_adma_desc(desc, 0, 0, 0x3);
556 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200557
558 /*
559 * Resync align buffer as we might have changed it.
560 */
561 if (data->flags & MMC_DATA_WRITE) {
562 dma_sync_single_for_device(mmc_dev(host->mmc),
563 host->align_addr, 128 * 4, direction);
564 }
565
566 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
567 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200568 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200569 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200570 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200571
572 return 0;
573
574unmap_entries:
575 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
576 data->sg_len, direction);
577unmap_align:
578 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
579 128 * 4, direction);
580fail:
581 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200582}
583
584static void sdhci_adma_table_post(struct sdhci_host *host,
585 struct mmc_data *data)
586{
587 int direction;
588
589 struct scatterlist *sg;
590 int i, size;
591 u8 *align;
592 char *buffer;
593 unsigned long flags;
594
595 if (data->flags & MMC_DATA_READ)
596 direction = DMA_FROM_DEVICE;
597 else
598 direction = DMA_TO_DEVICE;
599
600 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
601 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
602
603 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
604 128 * 4, direction);
605
606 if (data->flags & MMC_DATA_READ) {
607 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
608 data->sg_len, direction);
609
610 align = host->align_buffer;
611
612 for_each_sg(data->sg, sg, host->sg_count, i) {
613 if (sg_dma_address(sg) & 0x3) {
614 size = 4 - (sg_dma_address(sg) & 0x3);
615
616 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200617 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200618 memcpy(buffer, align, size);
619 sdhci_kunmap_atomic(buffer, &flags);
620
621 align += 4;
622 }
623 }
624 }
625
626 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
627 data->sg_len, direction);
628}
629
Andrei Warkentina3c77782011-04-11 16:13:42 -0500630static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800631{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700632 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500633 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700634 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800635
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200636 /*
637 * If the host controller provides us with an incorrect timeout
638 * value, just skip the check and use 0xE. The hardware may take
639 * longer to time out, but that's much better than having a too-short
640 * timeout value.
641 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200642 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200643 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200644
Andrei Warkentina3c77782011-04-11 16:13:42 -0500645 /* Unspecified timeout, assume max */
646 if (!data && !cmd->cmd_timeout_ms)
647 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800648
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649 /* timeout in us */
650 if (!data)
651 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300652 else {
653 target_timeout = data->timeout_ns / 1000;
654 if (host->clock)
655 target_timeout += data->timeout_clks / host->clock;
656 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700657
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700658 /*
659 * Figure out needed cycles.
660 * We do this in steps in order to fit inside a 32 bit int.
661 * The first step is the minimum timeout, which will have a
662 * minimum resolution of 6 bits:
663 * (1) 2^13*1000 > 2^22,
664 * (2) host->timeout_clk < 2^16
665 * =>
666 * (1) / (2) > 2^6
667 */
668 count = 0;
669 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
670 while (current_timeout < target_timeout) {
671 count++;
672 current_timeout <<= 1;
673 if (count >= 0xF)
674 break;
675 }
676
677 if (count >= 0xF) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530678 pr_warning("%s: Too large timeout requested for CMD%d!\n",
Andrei Warkentina3c77782011-04-11 16:13:42 -0500679 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700680 count = 0xE;
681 }
682
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200683 return count;
684}
685
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300686static void sdhci_set_transfer_irqs(struct sdhci_host *host)
687{
688 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
689 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
690
691 if (host->flags & SDHCI_REQ_USE_DMA)
692 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
693 else
694 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
695}
696
Andrei Warkentina3c77782011-04-11 16:13:42 -0500697static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200698{
699 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200700 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500701 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200702 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703
704 WARN_ON(host->data);
705
Andrei Warkentina3c77782011-04-11 16:13:42 -0500706 if (data || (cmd->flags & MMC_RSP_BUSY)) {
707 count = sdhci_calc_timeout(host, cmd);
708 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
709 }
710
711 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200712 return;
713
714 /* Sanity checks */
715 BUG_ON(data->blksz * data->blocks > 524288);
716 BUG_ON(data->blksz > host->mmc->max_blk_size);
717 BUG_ON(data->blocks > 65535);
718
719 host->data = data;
720 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400721 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200722
Richard Röjforsa13abc72009-09-22 16:45:30 -0700723 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100724 host->flags |= SDHCI_REQ_USE_DMA;
725
Pierre Ossman2134a922008-06-28 18:28:51 +0200726 /*
727 * FIXME: This doesn't account for merging when mapping the
728 * scatterlist.
729 */
730 if (host->flags & SDHCI_REQ_USE_DMA) {
731 int broken, i;
732 struct scatterlist *sg;
733
734 broken = 0;
735 if (host->flags & SDHCI_USE_ADMA) {
736 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
737 broken = 1;
738 } else {
739 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
740 broken = 1;
741 }
742
743 if (unlikely(broken)) {
744 for_each_sg(data->sg, sg, data->sg_len, i) {
745 if (sg->length & 0x3) {
746 DBG("Reverting to PIO because of "
747 "transfer size (%d)\n",
748 sg->length);
749 host->flags &= ~SDHCI_REQ_USE_DMA;
750 break;
751 }
752 }
753 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100754 }
755
756 /*
757 * The assumption here being that alignment is the same after
758 * translation to device address space.
759 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200760 if (host->flags & SDHCI_REQ_USE_DMA) {
761 int broken, i;
762 struct scatterlist *sg;
763
764 broken = 0;
765 if (host->flags & SDHCI_USE_ADMA) {
766 /*
767 * As we use 3 byte chunks to work around
768 * alignment problems, we need to check this
769 * quirk.
770 */
771 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
772 broken = 1;
773 } else {
774 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
775 broken = 1;
776 }
777
778 if (unlikely(broken)) {
779 for_each_sg(data->sg, sg, data->sg_len, i) {
780 if (sg->offset & 0x3) {
781 DBG("Reverting to PIO because of "
782 "bad alignment\n");
783 host->flags &= ~SDHCI_REQ_USE_DMA;
784 break;
785 }
786 }
787 }
788 }
789
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200790 if (host->flags & SDHCI_REQ_USE_DMA) {
791 if (host->flags & SDHCI_USE_ADMA) {
792 ret = sdhci_adma_table_pre(host, data);
793 if (ret) {
794 /*
795 * This only happens when someone fed
796 * us an invalid request.
797 */
798 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200799 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200800 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300801 sdhci_writel(host, host->adma_addr,
802 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200803 }
804 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300805 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200806
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300807 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200808 data->sg, data->sg_len,
809 (data->flags & MMC_DATA_READ) ?
810 DMA_FROM_DEVICE :
811 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300812 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200813 /*
814 * This only happens when someone fed
815 * us an invalid request.
816 */
817 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200818 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200819 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200820 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300821 sdhci_writel(host, sg_dma_address(data->sg),
822 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200823 }
824 }
825 }
826
Pierre Ossman2134a922008-06-28 18:28:51 +0200827 /*
828 * Always adjust the DMA selection as some controllers
829 * (e.g. JMicron) can't do PIO properly when the selection
830 * is ADMA.
831 */
832 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300833 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200834 ctrl &= ~SDHCI_CTRL_DMA_MASK;
835 if ((host->flags & SDHCI_REQ_USE_DMA) &&
836 (host->flags & SDHCI_USE_ADMA))
837 ctrl |= SDHCI_CTRL_ADMA32;
838 else
839 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300840 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100841 }
842
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200843 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200844 int flags;
845
846 flags = SG_MITER_ATOMIC;
847 if (host->data->flags & MMC_DATA_READ)
848 flags |= SG_MITER_TO_SG;
849 else
850 flags |= SG_MITER_FROM_SG;
851 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200852 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800853 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700854
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300855 sdhci_set_transfer_irqs(host);
856
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400857 /* Set the DMA boundary value and block size */
858 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
859 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700861}
862
863static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500864 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700865{
866 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500867 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700868
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869 if (data == NULL)
870 return;
871
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200872 WARN_ON(!host->data);
873
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500875 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
876 mode |= SDHCI_TRNS_MULTI;
877 /*
878 * If we are sending CMD23, CMD12 never gets sent
879 * on successful completion (so no Auto-CMD12).
880 */
881 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
882 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500883 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
884 mode |= SDHCI_TRNS_AUTO_CMD23;
885 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
886 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700887 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500888
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 if (data->flags & MMC_DATA_READ)
890 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100891 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892 mode |= SDHCI_TRNS_DMA;
893
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300894 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800895}
896
897static void sdhci_finish_data(struct sdhci_host *host)
898{
899 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900
901 BUG_ON(!host->data);
902
903 data = host->data;
904 host->data = NULL;
905
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100906 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200907 if (host->flags & SDHCI_USE_ADMA)
908 sdhci_adma_table_post(host, data);
909 else {
910 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
911 data->sg_len, (data->flags & MMC_DATA_READ) ?
912 DMA_FROM_DEVICE : DMA_TO_DEVICE);
913 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914 }
915
916 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200917 * The specification states that the block count register must
918 * be updated, but it does not specify at what point in the
919 * data flow. That makes the register entirely useless to read
920 * back so we have to assume that nothing made it to the card
921 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200923 if (data->error)
924 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200926 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927
Andrei Warkentine89d4562011-05-23 15:06:37 -0500928 /*
929 * Need to send CMD12 if -
930 * a) open-ended multiblock transfer (no CMD23)
931 * b) error in multiblock transfer
932 */
933 if (data->stop &&
934 (data->error ||
935 !host->mrq->sbc)) {
936
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937 /*
938 * The controller needs a reset of internal state machines
939 * upon error conditions.
940 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200941 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 sdhci_reset(host, SDHCI_RESET_CMD);
943 sdhci_reset(host, SDHCI_RESET_DATA);
944 }
945
946 sdhci_send_command(host, data->stop);
947 } else
948 tasklet_schedule(&host->finish_tasklet);
949}
950
951static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
952{
953 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700954 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700955 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956
957 WARN_ON(host->cmd);
958
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700960 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700961
962 mask = SDHCI_CMD_INHIBIT;
963 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
964 mask |= SDHCI_DATA_INHIBIT;
965
966 /* We shouldn't wait for data inihibit for stop commands, even
967 though they might use busy signaling */
968 if (host->mrq->data && (cmd == host->mrq->data->stop))
969 mask &= ~SDHCI_DATA_INHIBIT;
970
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300971 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700972 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530973 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100974 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200976 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977 tasklet_schedule(&host->finish_tasklet);
978 return;
979 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700980 timeout--;
981 mdelay(1);
982 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
984 mod_timer(&host->timer, jiffies + 10 * HZ);
985
986 host->cmd = cmd;
987
Andrei Warkentina3c77782011-04-11 16:13:42 -0500988 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300990 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991
Andrei Warkentine89d4562011-05-23 15:06:37 -0500992 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700993
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530995 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200997 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 tasklet_schedule(&host->finish_tasklet);
999 return;
1000 }
1001
1002 if (!(cmd->flags & MMC_RSP_PRESENT))
1003 flags = SDHCI_CMD_RESP_NONE;
1004 else if (cmd->flags & MMC_RSP_136)
1005 flags = SDHCI_CMD_RESP_LONG;
1006 else if (cmd->flags & MMC_RSP_BUSY)
1007 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1008 else
1009 flags = SDHCI_CMD_RESP_SHORT;
1010
1011 if (cmd->flags & MMC_RSP_CRC)
1012 flags |= SDHCI_CMD_CRC;
1013 if (cmd->flags & MMC_RSP_OPCODE)
1014 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301015
1016 /* CMD19 is special in that the Data Present Select should be set */
1017 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 flags |= SDHCI_CMD_DATA;
1019
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001020 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021}
1022
1023static void sdhci_finish_command(struct sdhci_host *host)
1024{
1025 int i;
1026
1027 BUG_ON(host->cmd == NULL);
1028
1029 if (host->cmd->flags & MMC_RSP_PRESENT) {
1030 if (host->cmd->flags & MMC_RSP_136) {
1031 /* CRC is stripped so we need to do some shifting. */
1032 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001033 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 SDHCI_RESPONSE + (3-i)*4) << 8;
1035 if (i != 3)
1036 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001037 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 SDHCI_RESPONSE + (3-i)*4-1);
1039 }
1040 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001041 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 }
1043 }
1044
Pierre Ossman17b04292007-07-22 22:18:46 +02001045 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
Andrei Warkentine89d4562011-05-23 15:06:37 -05001047 /* Finished CMD23, now send actual command. */
1048 if (host->cmd == host->mrq->sbc) {
1049 host->cmd = NULL;
1050 sdhci_send_command(host, host->mrq->cmd);
1051 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001052
Andrei Warkentine89d4562011-05-23 15:06:37 -05001053 /* Processed actual command. */
1054 if (host->data && host->data_early)
1055 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056
Andrei Warkentine89d4562011-05-23 15:06:37 -05001057 if (!host->cmd->data)
1058 tasklet_schedule(&host->finish_tasklet);
1059
1060 host->cmd = NULL;
1061 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062}
1063
1064static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1065{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301066 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001067 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301068 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001069 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070
Todd Poynor30832ab2011-12-27 15:48:46 +02001071 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 return;
1073
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001074 host->mmc->actual_clock = 0;
1075
Anton Vorontsov81146342009-03-17 00:13:59 +03001076 if (host->ops->set_clock) {
1077 host->ops->set_clock(host, clock);
1078 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1079 return;
1080 }
1081
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001082 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083
1084 if (clock == 0)
1085 goto out;
1086
Zhangfei Gao85105c52010-08-06 07:10:01 +08001087 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301088 /*
1089 * Check if the Host Controller supports Programmable Clock
1090 * Mode.
1091 */
1092 if (host->clk_mul) {
1093 u16 ctrl;
1094
1095 /*
1096 * We need to figure out whether the Host Driver needs
1097 * to select Programmable Clock Mode, or the value can
1098 * be set automatically by the Host Controller based on
1099 * the Preset Value registers.
1100 */
1101 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1102 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1103 for (div = 1; div <= 1024; div++) {
1104 if (((host->max_clk * host->clk_mul) /
1105 div) <= clock)
1106 break;
1107 }
1108 /*
1109 * Set Programmable Clock Mode in the Clock
1110 * Control register.
1111 */
1112 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001113 real_div = div;
1114 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301115 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001116 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301117 } else {
1118 /* Version 3.00 divisors must be a multiple of 2. */
1119 if (host->max_clk <= clock)
1120 div = 1;
1121 else {
1122 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1123 div += 2) {
1124 if ((host->max_clk / div) <= clock)
1125 break;
1126 }
1127 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001128 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301129 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001130 }
1131 } else {
1132 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001133 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001134 if ((host->max_clk / div) <= clock)
1135 break;
1136 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001141 if (real_div)
1142 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1143
Arindam Nathc3ed3872011-05-05 12:19:06 +05301144 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001145 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1146 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001148 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149
Chris Ball27f6cb12009-09-22 16:45:31 -07001150 /* Wait max 20 ms */
1151 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001152 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001153 & SDHCI_CLOCK_INT_STABLE)) {
1154 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301155 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001156 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157 sdhci_dumpregs(host);
1158 return;
1159 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001160 timeout--;
1161 mdelay(1);
1162 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163
1164 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001165 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166
1167out:
1168 host->clock = clock;
1169}
1170
Adrian Hunterceb61432011-12-27 15:48:41 +02001171static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001172{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001173 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001174
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001175 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001176 switch (1 << power) {
1177 case MMC_VDD_165_195:
1178 pwr = SDHCI_POWER_180;
1179 break;
1180 case MMC_VDD_29_30:
1181 case MMC_VDD_30_31:
1182 pwr = SDHCI_POWER_300;
1183 break;
1184 case MMC_VDD_32_33:
1185 case MMC_VDD_33_34:
1186 pwr = SDHCI_POWER_330;
1187 break;
1188 default:
1189 BUG();
1190 }
1191 }
1192
1193 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001194 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001195
Pierre Ossmanae628902009-05-03 20:45:03 +02001196 host->pwr = pwr;
1197
1198 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001199 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001200 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001201 }
1202
1203 /*
1204 * Spec says that we should clear the power reg before setting
1205 * a new value. Some controllers don't seem to like this though.
1206 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001207 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001208 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001209
Andres Salomone08c1692008-07-04 10:00:03 -07001210 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001211 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001212 * and set turn on power at the same time, so set the voltage first.
1213 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001214 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001215 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1216
1217 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001218
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001220
1221 /*
1222 * Some controllers need an extra 10ms delay of 10ms before they
1223 * can apply clock after applying power
1224 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001225 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001226 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001227
1228 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001229}
1230
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231/*****************************************************************************\
1232 * *
1233 * MMC callbacks *
1234 * *
1235\*****************************************************************************/
1236
1237static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1238{
1239 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001240 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241 unsigned long flags;
1242
1243 host = mmc_priv(mmc);
1244
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001245 sdhci_runtime_pm_get(host);
1246
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247 spin_lock_irqsave(&host->lock, flags);
1248
1249 WARN_ON(host->mrq != NULL);
1250
Pierre Ossmanf9134312008-12-21 17:01:48 +01001251#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001253#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001254
1255 /*
1256 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1257 * requests if Auto-CMD12 is enabled.
1258 */
1259 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001260 if (mrq->stop) {
1261 mrq->data->stop = NULL;
1262 mrq->stop = NULL;
1263 }
1264 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265
1266 host->mrq = mrq;
1267
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001268 /* If polling, assume that the card is always present. */
1269 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1270 present = true;
1271 else
1272 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1273 SDHCI_CARD_PRESENT;
1274
1275 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001276 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301278 } else {
1279 u32 present_state;
1280
1281 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1282 /*
1283 * Check if the re-tuning timer has already expired and there
1284 * is no on-going data transfer. If so, we need to execute
1285 * tuning procedure before sending command.
1286 */
1287 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1288 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1289 spin_unlock_irqrestore(&host->lock, flags);
1290 sdhci_execute_tuning(mmc);
1291 spin_lock_irqsave(&host->lock, flags);
1292
1293 /* Restore original mmc_request structure */
1294 host->mrq = mrq;
1295 }
1296
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001297 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001298 sdhci_send_command(host, mrq->sbc);
1299 else
1300 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301301 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001302
Pierre Ossman5f25a662006-10-04 02:15:39 -07001303 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304 spin_unlock_irqrestore(&host->lock, flags);
1305}
1306
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001307static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001310 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 u8 ctrl;
1312
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 spin_lock_irqsave(&host->lock, flags);
1314
Adrian Hunterceb61432011-12-27 15:48:41 +02001315 if (host->flags & SDHCI_DEVICE_DEAD) {
1316 spin_unlock_irqrestore(&host->lock, flags);
1317 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1318 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1319 return;
1320 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001321
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 /*
1323 * Reset the chip on each power off.
1324 * Should clear out any weird states.
1325 */
1326 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001327 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001328 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 }
1330
1331 sdhci_set_clock(host, ios->clock);
1332
1333 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001334 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001336 vdd_bit = sdhci_set_power(host, ios->vdd);
1337
1338 if (host->vmmc && vdd_bit != -1) {
1339 spin_unlock_irqrestore(&host->lock, flags);
1340 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1341 spin_lock_irqsave(&host->lock, flags);
1342 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343
Philip Rakity643a81f2010-09-23 08:24:32 -07001344 if (host->ops->platform_send_init_74_clocks)
1345 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1346
Philip Rakity15ec4462010-11-19 16:48:39 -05001347 /*
1348 * If your platform has 8-bit width support but is not a v3 controller,
1349 * or if it requires special setup code, you should implement that in
1350 * platform_8bit_width().
1351 */
1352 if (host->ops->platform_8bit_width)
1353 host->ops->platform_8bit_width(host, ios->bus_width);
1354 else {
1355 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1356 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1357 ctrl &= ~SDHCI_CTRL_4BITBUS;
1358 if (host->version >= SDHCI_SPEC_300)
1359 ctrl |= SDHCI_CTRL_8BITBUS;
1360 } else {
1361 if (host->version >= SDHCI_SPEC_300)
1362 ctrl &= ~SDHCI_CTRL_8BITBUS;
1363 if (ios->bus_width == MMC_BUS_WIDTH_4)
1364 ctrl |= SDHCI_CTRL_4BITBUS;
1365 else
1366 ctrl &= ~SDHCI_CTRL_4BITBUS;
1367 }
1368 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1369 }
1370
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001371 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001372
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001373 if ((ios->timing == MMC_TIMING_SD_HS ||
1374 ios->timing == MMC_TIMING_MMC_HS)
1375 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001376 ctrl |= SDHCI_CTRL_HISPD;
1377 else
1378 ctrl &= ~SDHCI_CTRL_HISPD;
1379
Arindam Nathd6d50a12011-05-05 12:18:59 +05301380 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301381 u16 clk, ctrl_2;
1382 unsigned int clock;
1383
1384 /* In case of UHS-I modes, set High Speed Enable */
1385 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1386 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1387 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001388 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301389 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301390
1391 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1392 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301393 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301394 /*
1395 * We only need to set Driver Strength if the
1396 * preset value enable is not set.
1397 */
1398 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1399 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1400 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1401 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1402 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1403
1404 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301405 } else {
1406 /*
1407 * According to SDHC Spec v3.00, if the Preset Value
1408 * Enable in the Host Control 2 register is set, we
1409 * need to reset SD Clock Enable before changing High
1410 * Speed Enable to avoid generating clock gliches.
1411 */
Arindam Nath758535c2011-05-05 12:19:00 +05301412
1413 /* Reset SD Clock Enable */
1414 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1415 clk &= ~SDHCI_CLOCK_CARD_EN;
1416 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1417
1418 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1419
1420 /* Re-enable SD Clock */
1421 clock = host->clock;
1422 host->clock = 0;
1423 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301424 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301425
Arindam Nath49c468f2011-05-05 12:19:01 +05301426
1427 /* Reset SD Clock Enable */
1428 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1429 clk &= ~SDHCI_CLOCK_CARD_EN;
1430 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1431
Philip Rakity6322cdd2011-05-13 11:17:15 +05301432 if (host->ops->set_uhs_signaling)
1433 host->ops->set_uhs_signaling(host, ios->timing);
1434 else {
1435 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1436 /* Select Bus Speed Mode for host */
1437 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1438 if (ios->timing == MMC_TIMING_UHS_SDR12)
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1440 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1442 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1443 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1444 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1446 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1447 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1448 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1449 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301450
1451 /* Re-enable SD Clock */
1452 clock = host->clock;
1453 host->clock = 0;
1454 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301455 } else
1456 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301457
Leandro Dorileob8352262007-07-25 23:47:04 +02001458 /*
1459 * Some (ENE) controllers go apeshit on some ios operation,
1460 * signalling timeout and CRC errors even on CMD0. Resetting
1461 * it on each ios seems to solve the problem.
1462 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001463 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001464 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1465
Pierre Ossman5f25a662006-10-04 02:15:39 -07001466 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001467 spin_unlock_irqrestore(&host->lock, flags);
1468}
1469
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001470static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1471{
1472 struct sdhci_host *host = mmc_priv(mmc);
1473
1474 sdhci_runtime_pm_get(host);
1475 sdhci_do_set_ios(host, ios);
1476 sdhci_runtime_pm_put(host);
1477}
1478
1479static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001480{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001482 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484 spin_lock_irqsave(&host->lock, flags);
1485
Pierre Ossman1e728592008-04-16 19:13:13 +02001486 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001487 is_readonly = 0;
1488 else if (host->ops->get_ro)
1489 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001490 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001491 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1492 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493
1494 spin_unlock_irqrestore(&host->lock, flags);
1495
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001496 /* This quirk needs to be replaced by a callback-function later */
1497 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1498 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499}
1500
Takashi Iwai82b0e232011-04-21 20:26:38 +02001501#define SAMPLE_COUNT 5
1502
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001503static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001504{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001505 int i, ro_count;
1506
Takashi Iwai82b0e232011-04-21 20:26:38 +02001507 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001508 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001509
1510 ro_count = 0;
1511 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001512 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001513 if (++ro_count > SAMPLE_COUNT / 2)
1514 return 1;
1515 }
1516 msleep(30);
1517 }
1518 return 0;
1519}
1520
Adrian Hunter20758b62011-08-29 16:42:12 +03001521static void sdhci_hw_reset(struct mmc_host *mmc)
1522{
1523 struct sdhci_host *host = mmc_priv(mmc);
1524
1525 if (host->ops && host->ops->hw_reset)
1526 host->ops->hw_reset(host);
1527}
1528
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001529static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001530{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001531 struct sdhci_host *host = mmc_priv(mmc);
1532 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001533
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001534 sdhci_runtime_pm_get(host);
1535 ret = sdhci_do_get_ro(host);
1536 sdhci_runtime_pm_put(host);
1537 return ret;
1538}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001539
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001540static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1541{
Pierre Ossman1e728592008-04-16 19:13:13 +02001542 if (host->flags & SDHCI_DEVICE_DEAD)
1543 goto out;
1544
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001545 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001546 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1547 else
1548 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1549
1550 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1551 if (host->runtime_suspended)
1552 goto out;
1553
1554 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001555 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1556 else
1557 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001558out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001559 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001560}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001561
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001562static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1563{
1564 struct sdhci_host *host = mmc_priv(mmc);
1565 unsigned long flags;
1566
1567 spin_lock_irqsave(&host->lock, flags);
1568 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001569 spin_unlock_irqrestore(&host->lock, flags);
1570}
1571
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001572static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1573 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301574{
Arindam Nathf2119df2011-05-05 12:18:57 +05301575 u8 pwr;
1576 u16 clk, ctrl;
1577 u32 present_state;
1578
Arindam Nathf2119df2011-05-05 12:18:57 +05301579 /*
1580 * Signal Voltage Switching is only applicable for Host Controllers
1581 * v3.00 and above.
1582 */
1583 if (host->version < SDHCI_SPEC_300)
1584 return 0;
1585
1586 /*
1587 * We first check whether the request is to set signalling voltage
1588 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1589 */
1590 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1591 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1592 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1593 ctrl &= ~SDHCI_CTRL_VDD_180;
1594 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1595
1596 /* Wait for 5ms */
1597 usleep_range(5000, 5500);
1598
1599 /* 3.3V regulator output should be stable within 5 ms */
1600 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1601 if (!(ctrl & SDHCI_CTRL_VDD_180))
1602 return 0;
1603 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301604 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301605 "signalling voltage failed\n");
1606 return -EIO;
1607 }
1608 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1609 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1610 /* Stop SDCLK */
1611 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1612 clk &= ~SDHCI_CLOCK_CARD_EN;
1613 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1614
1615 /* Check whether DAT[3:0] is 0000 */
1616 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1617 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1618 SDHCI_DATA_LVL_SHIFT)) {
1619 /*
1620 * Enable 1.8V Signal Enable in the Host Control2
1621 * register
1622 */
1623 ctrl |= SDHCI_CTRL_VDD_180;
1624 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1625
1626 /* Wait for 5ms */
1627 usleep_range(5000, 5500);
1628
1629 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1630 if (ctrl & SDHCI_CTRL_VDD_180) {
1631 /* Provide SDCLK again and wait for 1ms*/
1632 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1633 clk |= SDHCI_CLOCK_CARD_EN;
1634 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1635 usleep_range(1000, 1500);
1636
1637 /*
1638 * If DAT[3:0] level is 1111b, then the card
1639 * was successfully switched to 1.8V signaling.
1640 */
1641 present_state = sdhci_readl(host,
1642 SDHCI_PRESENT_STATE);
1643 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1644 SDHCI_DATA_LVL_MASK)
1645 return 0;
1646 }
1647 }
1648
1649 /*
1650 * If we are here, that means the switch to 1.8V signaling
1651 * failed. We power cycle the card, and retry initialization
1652 * sequence by setting S18R to 0.
1653 */
1654 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1655 pwr &= ~SDHCI_POWER_ON;
1656 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1657
1658 /* Wait for 1ms as per the spec */
1659 usleep_range(1000, 1500);
1660 pwr |= SDHCI_POWER_ON;
1661 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1662
Girish K Sa3c76eb2011-10-11 11:44:09 +05301663 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301664 "voltage failed, retrying with S18R set to 0\n");
1665 return -EAGAIN;
1666 } else
1667 /* No signal voltage switch required */
1668 return 0;
1669}
1670
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001671static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1672 struct mmc_ios *ios)
1673{
1674 struct sdhci_host *host = mmc_priv(mmc);
1675 int err;
1676
1677 if (host->version < SDHCI_SPEC_300)
1678 return 0;
1679 sdhci_runtime_pm_get(host);
1680 err = sdhci_do_start_signal_voltage_switch(host, ios);
1681 sdhci_runtime_pm_put(host);
1682 return err;
1683}
1684
Arindam Nathb513ea22011-05-05 12:19:04 +05301685static int sdhci_execute_tuning(struct mmc_host *mmc)
1686{
1687 struct sdhci_host *host;
1688 u16 ctrl;
1689 u32 ier;
1690 int tuning_loop_counter = MAX_TUNING_LOOP;
1691 unsigned long timeout;
1692 int err = 0;
1693
1694 host = mmc_priv(mmc);
1695
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001696 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301697 disable_irq(host->irq);
1698 spin_lock(&host->lock);
1699
1700 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1701
1702 /*
1703 * Host Controller needs tuning only in case of SDR104 mode
1704 * and for SDR50 mode when Use Tuning for SDR50 is set in
1705 * Capabilities register.
1706 */
1707 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1708 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1709 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1710 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1711 else {
1712 spin_unlock(&host->lock);
1713 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001714 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301715 return 0;
1716 }
1717
1718 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1719
1720 /*
1721 * As per the Host Controller spec v3.00, tuning command
1722 * generates Buffer Read Ready interrupt, so enable that.
1723 *
1724 * Note: The spec clearly says that when tuning sequence
1725 * is being performed, the controller does not generate
1726 * interrupts other than Buffer Read Ready interrupt. But
1727 * to make sure we don't hit a controller bug, we _only_
1728 * enable Buffer Read Ready interrupt here.
1729 */
1730 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1731 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1732
1733 /*
1734 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1735 * of loops reaches 40 times or a timeout of 150ms occurs.
1736 */
1737 timeout = 150;
1738 do {
1739 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001740 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301741
1742 if (!tuning_loop_counter && !timeout)
1743 break;
1744
1745 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1746 cmd.arg = 0;
1747 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1748 cmd.retries = 0;
1749 cmd.data = NULL;
1750 cmd.error = 0;
1751
1752 mrq.cmd = &cmd;
1753 host->mrq = &mrq;
1754
1755 /*
1756 * In response to CMD19, the card sends 64 bytes of tuning
1757 * block to the Host Controller. So we set the block size
1758 * to 64 here.
1759 */
1760 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1761
1762 /*
1763 * The tuning block is sent by the card to the host controller.
1764 * So we set the TRNS_READ bit in the Transfer Mode register.
1765 * This also takes care of setting DMA Enable and Multi Block
1766 * Select in the same register to 0.
1767 */
1768 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1769
1770 sdhci_send_command(host, &cmd);
1771
1772 host->cmd = NULL;
1773 host->mrq = NULL;
1774
1775 spin_unlock(&host->lock);
1776 enable_irq(host->irq);
1777
1778 /* Wait for Buffer Read Ready interrupt */
1779 wait_event_interruptible_timeout(host->buf_ready_int,
1780 (host->tuning_done == 1),
1781 msecs_to_jiffies(50));
1782 disable_irq(host->irq);
1783 spin_lock(&host->lock);
1784
1785 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301786 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301787 "Buffer Read Ready interrupt during tuning "
1788 "procedure, falling back to fixed sampling "
1789 "clock\n");
1790 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1791 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1792 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1793 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1794
1795 err = -EIO;
1796 goto out;
1797 }
1798
1799 host->tuning_done = 0;
1800
1801 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1802 tuning_loop_counter--;
1803 timeout--;
1804 mdelay(1);
1805 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1806
1807 /*
1808 * The Host Driver has exhausted the maximum number of loops allowed,
1809 * so use fixed sampling frequency.
1810 */
1811 if (!tuning_loop_counter || !timeout) {
1812 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1813 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1814 } else {
1815 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301816 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301817 " failed, falling back to fixed sampling"
1818 " clock\n");
1819 err = -EIO;
1820 }
1821 }
1822
1823out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301824 /*
1825 * If this is the very first time we are here, we start the retuning
1826 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1827 * flag won't be set, we check this condition before actually starting
1828 * the timer.
1829 */
1830 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1831 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1832 mod_timer(&host->tuning_timer, jiffies +
1833 host->tuning_count * HZ);
1834 /* Tuning mode 1 limits the maximum data length to 4MB */
1835 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1836 } else {
1837 host->flags &= ~SDHCI_NEEDS_RETUNING;
1838 /* Reload the new initial value for timer */
1839 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1840 mod_timer(&host->tuning_timer, jiffies +
1841 host->tuning_count * HZ);
1842 }
1843
1844 /*
1845 * In case tuning fails, host controllers which support re-tuning can
1846 * try tuning again at a later time, when the re-tuning timer expires.
1847 * So for these controllers, we return 0. Since there might be other
1848 * controllers who do not have this capability, we return error for
1849 * them.
1850 */
1851 if (err && host->tuning_count &&
1852 host->tuning_mode == SDHCI_TUNING_MODE_1)
1853 err = 0;
1854
Arindam Nathb513ea22011-05-05 12:19:04 +05301855 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1856 spin_unlock(&host->lock);
1857 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001858 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301859
1860 return err;
1861}
1862
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001863static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301864{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301865 u16 ctrl;
1866 unsigned long flags;
1867
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301868 /* Host Controller v3.00 defines preset value registers */
1869 if (host->version < SDHCI_SPEC_300)
1870 return;
1871
1872 spin_lock_irqsave(&host->lock, flags);
1873
1874 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1875
1876 /*
1877 * We only enable or disable Preset Value if they are not already
1878 * enabled or disabled respectively. Otherwise, we bail out.
1879 */
1880 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1881 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1882 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001883 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301884 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1885 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1886 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001887 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301888 }
1889
1890 spin_unlock_irqrestore(&host->lock, flags);
1891}
1892
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001893static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1894{
1895 struct sdhci_host *host = mmc_priv(mmc);
1896
1897 sdhci_runtime_pm_get(host);
1898 sdhci_do_enable_preset_value(host, enable);
1899 sdhci_runtime_pm_put(host);
1900}
1901
David Brownellab7aefd2006-11-12 17:55:30 -08001902static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001903 .request = sdhci_request,
1904 .set_ios = sdhci_set_ios,
1905 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001906 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001907 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301908 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301909 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301910 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001911};
1912
1913/*****************************************************************************\
1914 * *
1915 * Tasklets *
1916 * *
1917\*****************************************************************************/
1918
1919static void sdhci_tasklet_card(unsigned long param)
1920{
1921 struct sdhci_host *host;
1922 unsigned long flags;
1923
1924 host = (struct sdhci_host*)param;
1925
1926 spin_lock_irqsave(&host->lock, flags);
1927
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001928 /* Check host->mrq first in case we are runtime suspended */
1929 if (host->mrq &&
1930 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301931 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001932 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301933 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001934 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001935
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001936 sdhci_reset(host, SDHCI_RESET_CMD);
1937 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001938
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001939 host->mrq->cmd->error = -ENOMEDIUM;
1940 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001941 }
1942
1943 spin_unlock_irqrestore(&host->lock, flags);
1944
Pierre Ossman04cf5852008-08-18 22:18:14 +02001945 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001946}
1947
1948static void sdhci_tasklet_finish(unsigned long param)
1949{
1950 struct sdhci_host *host;
1951 unsigned long flags;
1952 struct mmc_request *mrq;
1953
1954 host = (struct sdhci_host*)param;
1955
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001956 spin_lock_irqsave(&host->lock, flags);
1957
Chris Ball0c9c99a2011-04-27 17:35:31 -04001958 /*
1959 * If this tasklet gets rescheduled while running, it will
1960 * be run again afterwards but without any active request.
1961 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001962 if (!host->mrq) {
1963 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001964 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001965 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001966
1967 del_timer(&host->timer);
1968
1969 mrq = host->mrq;
1970
Pierre Ossmand129bce2006-03-24 03:18:17 -08001971 /*
1972 * The controller needs a reset of internal state machines
1973 * upon error conditions.
1974 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001975 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001976 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001977 (mrq->data && (mrq->data->error ||
1978 (mrq->data->stop && mrq->data->stop->error))) ||
1979 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001980
1981 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001982 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001983 unsigned int clock;
1984
1985 /* This is to force an update */
1986 clock = host->clock;
1987 host->clock = 0;
1988 sdhci_set_clock(host, clock);
1989 }
1990
1991 /* Spec says we should do both at the same time, but Ricoh
1992 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001993 sdhci_reset(host, SDHCI_RESET_CMD);
1994 sdhci_reset(host, SDHCI_RESET_DATA);
1995 }
1996
1997 host->mrq = NULL;
1998 host->cmd = NULL;
1999 host->data = NULL;
2000
Pierre Ossmanf9134312008-12-21 17:01:48 +01002001#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002002 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002003#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002004
Pierre Ossman5f25a662006-10-04 02:15:39 -07002005 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002006 spin_unlock_irqrestore(&host->lock, flags);
2007
2008 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002009 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002010}
2011
2012static void sdhci_timeout_timer(unsigned long data)
2013{
2014 struct sdhci_host *host;
2015 unsigned long flags;
2016
2017 host = (struct sdhci_host*)data;
2018
2019 spin_lock_irqsave(&host->lock, flags);
2020
2021 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302022 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002023 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002024 sdhci_dumpregs(host);
2025
2026 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002027 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002028 sdhci_finish_data(host);
2029 } else {
2030 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002031 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002032 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002033 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002034
2035 tasklet_schedule(&host->finish_tasklet);
2036 }
2037 }
2038
Pierre Ossman5f25a662006-10-04 02:15:39 -07002039 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002040 spin_unlock_irqrestore(&host->lock, flags);
2041}
2042
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302043static void sdhci_tuning_timer(unsigned long data)
2044{
2045 struct sdhci_host *host;
2046 unsigned long flags;
2047
2048 host = (struct sdhci_host *)data;
2049
2050 spin_lock_irqsave(&host->lock, flags);
2051
2052 host->flags |= SDHCI_NEEDS_RETUNING;
2053
2054 spin_unlock_irqrestore(&host->lock, flags);
2055}
2056
Pierre Ossmand129bce2006-03-24 03:18:17 -08002057/*****************************************************************************\
2058 * *
2059 * Interrupt handling *
2060 * *
2061\*****************************************************************************/
2062
2063static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2064{
2065 BUG_ON(intmask == 0);
2066
2067 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302068 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002069 "though no command operation was in progress.\n",
2070 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002071 sdhci_dumpregs(host);
2072 return;
2073 }
2074
Pierre Ossman43b58b32007-07-25 23:15:27 +02002075 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002076 host->cmd->error = -ETIMEDOUT;
2077 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2078 SDHCI_INT_INDEX))
2079 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002080
Pierre Ossmane8095172008-07-25 01:09:08 +02002081 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002083 return;
2084 }
2085
2086 /*
2087 * The host can send and interrupt when the busy state has
2088 * ended, allowing us to wait without wasting CPU cycles.
2089 * Unfortunately this is overloaded on the "data complete"
2090 * interrupt, so we need to take some care when handling
2091 * it.
2092 *
2093 * Note: The 1.0 specification is a bit ambiguous about this
2094 * feature so there might be some problems with older
2095 * controllers.
2096 */
2097 if (host->cmd->flags & MMC_RSP_BUSY) {
2098 if (host->cmd->data)
2099 DBG("Cannot wait for busy signal when also "
2100 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002101 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002102 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002103
2104 /* The controller does not support the end-of-busy IRQ,
2105 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002106 }
2107
2108 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002109 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002110}
2111
George G. Davis0957c332010-02-18 12:32:12 -05002112#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002113static void sdhci_show_adma_error(struct sdhci_host *host)
2114{
2115 const char *name = mmc_hostname(host->mmc);
2116 u8 *desc = host->adma_desc;
2117 __le32 *dma;
2118 __le16 *len;
2119 u8 attr;
2120
2121 sdhci_dumpregs(host);
2122
2123 while (true) {
2124 dma = (__le32 *)(desc + 4);
2125 len = (__le16 *)(desc + 2);
2126 attr = *desc;
2127
2128 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2129 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2130
2131 desc += 8;
2132
2133 if (attr & 2)
2134 break;
2135 }
2136}
2137#else
2138static void sdhci_show_adma_error(struct sdhci_host *host) { }
2139#endif
2140
Pierre Ossmand129bce2006-03-24 03:18:17 -08002141static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2142{
2143 BUG_ON(intmask == 0);
2144
Arindam Nathb513ea22011-05-05 12:19:04 +05302145 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2146 if (intmask & SDHCI_INT_DATA_AVAIL) {
2147 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2148 MMC_SEND_TUNING_BLOCK) {
2149 host->tuning_done = 1;
2150 wake_up(&host->buf_ready_int);
2151 return;
2152 }
2153 }
2154
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155 if (!host->data) {
2156 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002157 * The "data complete" interrupt is also used to
2158 * indicate that a busy state has ended. See comment
2159 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002161 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2162 if (intmask & SDHCI_INT_DATA_END) {
2163 sdhci_finish_command(host);
2164 return;
2165 }
2166 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167
Girish K Sa3c76eb2011-10-11 11:44:09 +05302168 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002169 "though no data operation was in progress.\n",
2170 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 sdhci_dumpregs(host);
2172
2173 return;
2174 }
2175
2176 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002177 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002178 else if (intmask & SDHCI_INT_DATA_END_BIT)
2179 host->data->error = -EILSEQ;
2180 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2181 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2182 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002183 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002184 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302185 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002186 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002187 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002189
Pierre Ossman17b04292007-07-22 22:18:46 +02002190 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191 sdhci_finish_data(host);
2192 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002193 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_transfer_pio(host);
2195
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002196 /*
2197 * We currently don't do anything fancy with DMA
2198 * boundaries, but as we can't disable the feature
2199 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002200 *
2201 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2202 * should return a valid address to continue from, but as
2203 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002204 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002205 if (intmask & SDHCI_INT_DMA_END) {
2206 u32 dmastart, dmanow;
2207 dmastart = sg_dma_address(host->data->sg);
2208 dmanow = dmastart + host->data->bytes_xfered;
2209 /*
2210 * Force update to the next DMA block boundary.
2211 */
2212 dmanow = (dmanow &
2213 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2214 SDHCI_DEFAULT_BOUNDARY_SIZE;
2215 host->data->bytes_xfered = dmanow - dmastart;
2216 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2217 " next 0x%08x\n",
2218 mmc_hostname(host->mmc), dmastart,
2219 host->data->bytes_xfered, dmanow);
2220 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2221 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002222
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002223 if (intmask & SDHCI_INT_DATA_END) {
2224 if (host->cmd) {
2225 /*
2226 * Data managed to finish before the
2227 * command completed. Make sure we do
2228 * things in the proper order.
2229 */
2230 host->data_early = 1;
2231 } else {
2232 sdhci_finish_data(host);
2233 }
2234 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235 }
2236}
2237
David Howells7d12e782006-10-05 14:55:46 +01002238static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239{
2240 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002241 struct sdhci_host *host = dev_id;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002243 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244
2245 spin_lock(&host->lock);
2246
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002247 if (host->runtime_suspended) {
2248 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302249 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002250 mmc_hostname(host->mmc));
2251 return IRQ_HANDLED;
2252 }
2253
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002254 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002255
Mark Lord62df67a52007-03-06 13:30:13 +01002256 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257 result = IRQ_NONE;
2258 goto out;
2259 }
2260
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002261 DBG("*** %s got interrupt: 0x%08x\n",
2262 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263
Pierre Ossman3192a282006-06-30 02:22:26 -07002264 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002265 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2266 SDHCI_CARD_PRESENT;
2267
2268 /*
2269 * There is a observation on i.mx esdhc. INSERT bit will be
2270 * immediately set again when it gets cleared, if a card is
2271 * inserted. We have to mask the irq to prevent interrupt
2272 * storm which will freeze the system. And the REMOVE gets
2273 * the same situation.
2274 *
2275 * More testing are needed here to ensure it works for other
2276 * platforms though.
2277 */
2278 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2279 SDHCI_INT_CARD_REMOVE);
2280 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2281 SDHCI_INT_CARD_INSERT);
2282
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002283 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002284 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2285 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002287 }
2288
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002290 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2291 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002292 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293 }
2294
2295 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002296 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2297 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002298 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002299 }
2300
2301 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2302
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002303 intmask &= ~SDHCI_INT_ERROR;
2304
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302306 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002308 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 }
2310
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002311 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002312
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002313 if (intmask & SDHCI_INT_CARD_INT)
2314 cardint = 1;
2315
2316 intmask &= ~SDHCI_INT_CARD_INT;
2317
Pierre Ossman3192a282006-06-30 02:22:26 -07002318 if (intmask) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302319 pr_err("%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002320 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321 sdhci_dumpregs(host);
2322
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002323 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002324 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325
2326 result = IRQ_HANDLED;
2327
Pierre Ossman5f25a662006-10-04 02:15:39 -07002328 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002329out:
2330 spin_unlock(&host->lock);
2331
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002332 /*
2333 * We have to delay this as it calls back into the driver.
2334 */
2335 if (cardint)
2336 mmc_signal_sdio_irq(host->mmc);
2337
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338 return result;
2339}
2340
2341/*****************************************************************************\
2342 * *
2343 * Suspend/resume *
2344 * *
2345\*****************************************************************************/
2346
2347#ifdef CONFIG_PM
2348
Manuel Lauss29495aa2011-11-03 11:09:45 +01002349int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002350{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002351 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002353 sdhci_disable_card_detection(host);
2354
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302355 /* Disable tuning since we are suspending */
2356 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2357 host->tuning_mode == SDHCI_TUNING_MODE_1) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002358 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302359 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302360 }
2361
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002362 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002363 if (ret)
2364 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002365
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002366 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002368 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369}
2370
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002371EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002373int sdhci_resume_host(struct sdhci_host *host)
2374{
2375 int ret;
2376
Richard Röjforsa13abc72009-09-22 16:45:30 -07002377 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002378 if (host->ops->enable_dma)
2379 host->ops->enable_dma(host);
2380 }
2381
2382 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2383 mmc_hostname(host->mmc), host);
2384 if (ret)
2385 return ret;
2386
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002387 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002388 mmiowb();
2389
2390 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002391 sdhci_enable_card_detection(host);
2392
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302393 /* Set the re-tuning expiration flag */
2394 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2395 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2396 host->flags |= SDHCI_NEEDS_RETUNING;
2397
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002398 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002399}
2400
2401EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002402
Daniel Drake5f619702010-11-04 22:20:39 +00002403void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2404{
2405 u8 val;
2406 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2407 val |= SDHCI_WAKE_ON_INT;
2408 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2409}
2410
2411EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2412
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413#endif /* CONFIG_PM */
2414
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002415#ifdef CONFIG_PM_RUNTIME
2416
2417static int sdhci_runtime_pm_get(struct sdhci_host *host)
2418{
2419 return pm_runtime_get_sync(host->mmc->parent);
2420}
2421
2422static int sdhci_runtime_pm_put(struct sdhci_host *host)
2423{
2424 pm_runtime_mark_last_busy(host->mmc->parent);
2425 return pm_runtime_put_autosuspend(host->mmc->parent);
2426}
2427
2428int sdhci_runtime_suspend_host(struct sdhci_host *host)
2429{
2430 unsigned long flags;
2431 int ret = 0;
2432
2433 /* Disable tuning since we are suspending */
2434 if (host->version >= SDHCI_SPEC_300 &&
2435 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2436 del_timer_sync(&host->tuning_timer);
2437 host->flags &= ~SDHCI_NEEDS_RETUNING;
2438 }
2439
2440 spin_lock_irqsave(&host->lock, flags);
2441 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2442 spin_unlock_irqrestore(&host->lock, flags);
2443
2444 synchronize_irq(host->irq);
2445
2446 spin_lock_irqsave(&host->lock, flags);
2447 host->runtime_suspended = true;
2448 spin_unlock_irqrestore(&host->lock, flags);
2449
2450 return ret;
2451}
2452EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2453
2454int sdhci_runtime_resume_host(struct sdhci_host *host)
2455{
2456 unsigned long flags;
2457 int ret = 0, host_flags = host->flags;
2458
2459 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2460 if (host->ops->enable_dma)
2461 host->ops->enable_dma(host);
2462 }
2463
2464 sdhci_init(host, 0);
2465
2466 /* Force clock and power re-program */
2467 host->pwr = 0;
2468 host->clock = 0;
2469 sdhci_do_set_ios(host, &host->mmc->ios);
2470
2471 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2472 if (host_flags & SDHCI_PV_ENABLED)
2473 sdhci_do_enable_preset_value(host, true);
2474
2475 /* Set the re-tuning expiration flag */
2476 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2477 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2478 host->flags |= SDHCI_NEEDS_RETUNING;
2479
2480 spin_lock_irqsave(&host->lock, flags);
2481
2482 host->runtime_suspended = false;
2483
2484 /* Enable SDIO IRQ */
2485 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2486 sdhci_enable_sdio_irq_nolock(host, true);
2487
2488 /* Enable Card Detection */
2489 sdhci_enable_card_detection(host);
2490
2491 spin_unlock_irqrestore(&host->lock, flags);
2492
2493 return ret;
2494}
2495EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2496
2497#endif
2498
Pierre Ossmand129bce2006-03-24 03:18:17 -08002499/*****************************************************************************\
2500 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002501 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502 * *
2503\*****************************************************************************/
2504
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002505struct sdhci_host *sdhci_alloc_host(struct device *dev,
2506 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002507{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508 struct mmc_host *mmc;
2509 struct sdhci_host *host;
2510
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002511 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002513 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002515 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002516
2517 host = mmc_priv(mmc);
2518 host->mmc = mmc;
2519
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002520 return host;
2521}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002522
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002523EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002525int sdhci_add_host(struct sdhci_host *host)
2526{
2527 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302528 u32 caps[2];
2529 u32 max_current_caps;
2530 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002531 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002532
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002533 WARN_ON(host == NULL);
2534 if (host == NULL)
2535 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002536
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002537 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002539 if (debug_quirks)
2540 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002541 if (debug_quirks2)
2542 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002543
Pierre Ossmand96649e2006-06-30 02:22:30 -07002544 sdhci_reset(host, SDHCI_RESET_ALL);
2545
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002546 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002547 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2548 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002549 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302550 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002551 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002552 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002553 }
2554
Arindam Nathf2119df2011-05-05 12:18:57 +05302555 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002556 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002557
Arindam Nathf2119df2011-05-05 12:18:57 +05302558 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2559 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2560
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002561 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002562 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302563 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002564 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002565 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002566 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002568 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002569 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002570 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002571 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002572 }
2573
Arindam Nathf2119df2011-05-05 12:18:57 +05302574 if ((host->version >= SDHCI_SPEC_200) &&
2575 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002576 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002577
2578 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2579 (host->flags & SDHCI_USE_ADMA)) {
2580 DBG("Disabling ADMA as it is marked broken\n");
2581 host->flags &= ~SDHCI_USE_ADMA;
2582 }
2583
Richard Röjforsa13abc72009-09-22 16:45:30 -07002584 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002585 if (host->ops->enable_dma) {
2586 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302587 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002588 "available. Falling back to PIO.\n",
2589 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002590 host->flags &=
2591 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002592 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593 }
2594 }
2595
Pierre Ossman2134a922008-06-28 18:28:51 +02002596 if (host->flags & SDHCI_USE_ADMA) {
2597 /*
2598 * We need to allocate descriptors for all sg entries
2599 * (128) and potentially one alignment transfer for
2600 * each of those entries.
2601 */
2602 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2603 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2604 if (!host->adma_desc || !host->align_buffer) {
2605 kfree(host->adma_desc);
2606 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302607 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002608 "buffers. Falling back to standard DMA.\n",
2609 mmc_hostname(mmc));
2610 host->flags &= ~SDHCI_USE_ADMA;
2611 }
2612 }
2613
Pierre Ossman76591502008-07-21 00:32:11 +02002614 /*
2615 * If we use DMA, then it's up to the caller to set the DMA
2616 * mask, but PIO does not need the hw shim so we set a new
2617 * mask here in that case.
2618 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002619 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002620 host->dma_mask = DMA_BIT_MASK(64);
2621 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2622 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002624 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302625 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002626 >> SDHCI_CLOCK_BASE_SHIFT;
2627 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302628 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002629 >> SDHCI_CLOCK_BASE_SHIFT;
2630
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002632 if (host->max_clk == 0 || host->quirks &
2633 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002634 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302635 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002636 "frequency.\n", mmc_hostname(mmc));
2637 return -ENODEV;
2638 }
2639 host->max_clk = host->ops->get_max_clock(host);
2640 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641
2642 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302643 * In case of Host Controller v3.00, find out whether clock
2644 * multiplier is supported.
2645 */
2646 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2647 SDHCI_CLOCK_MUL_SHIFT;
2648
2649 /*
2650 * In case the value in Clock Multiplier is 0, then programmable
2651 * clock mode is not supported, otherwise the actual clock
2652 * multiplier is one more than the value of Clock Multiplier
2653 * in the Capabilities Register.
2654 */
2655 if (host->clk_mul)
2656 host->clk_mul += 1;
2657
2658 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002659 * Set host parameters.
2660 */
2661 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302662 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002663 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002664 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302665 else if (host->version >= SDHCI_SPEC_300) {
2666 if (host->clk_mul) {
2667 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2668 mmc->f_max = host->max_clk * host->clk_mul;
2669 } else
2670 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2671 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002672 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002673
Andy Shevchenko272308c2011-08-03 18:36:00 +03002674 host->timeout_clk =
2675 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2676 if (host->timeout_clk == 0) {
2677 if (host->ops->get_timeout_clock) {
2678 host->timeout_clk = host->ops->get_timeout_clock(host);
2679 } else if (!(host->quirks &
2680 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302681 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002682 "frequency.\n", mmc_hostname(mmc));
2683 return -ENODEV;
2684 }
2685 }
2686 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2687 host->timeout_clk *= 1000;
2688
2689 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002690 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002691
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002692 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002693
Andrei Warkentine89d4562011-05-23 15:06:37 -05002694 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2695
2696 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2697 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002698
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002699 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002700 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002701 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002702 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002703 host->flags |= SDHCI_AUTO_CMD23;
2704 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2705 } else {
2706 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2707 }
2708
Philip Rakity15ec4462010-11-19 16:48:39 -05002709 /*
2710 * A controller may support 8-bit width, but the board itself
2711 * might not have the pins brought out. Boards that support
2712 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2713 * their platform code before calling sdhci_add_host(), and we
2714 * won't assume 8-bit width for hosts without that CAP.
2715 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002716 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002717 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718
Arindam Nathf2119df2011-05-05 12:18:57 +05302719 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002720 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002721
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002722 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2723 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002724 mmc->caps |= MMC_CAP_NEEDS_POLL;
2725
Arindam Nathf2119df2011-05-05 12:18:57 +05302726 /* UHS-I mode(s) supported by the host controller. */
2727 if (host->version >= SDHCI_SPEC_300)
2728 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2729
2730 /* SDR104 supports also implies SDR50 support */
2731 if (caps[1] & SDHCI_SUPPORT_SDR104)
2732 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2733 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2734 mmc->caps |= MMC_CAP_UHS_SDR50;
2735
2736 if (caps[1] & SDHCI_SUPPORT_DDR50)
2737 mmc->caps |= MMC_CAP_UHS_DDR50;
2738
Arindam Nathb513ea22011-05-05 12:19:04 +05302739 /* Does the host needs tuning for SDR50? */
2740 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2741 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2742
Arindam Nathd6d50a12011-05-05 12:18:59 +05302743 /* Driver Type(s) (A, C, D) supported by the host */
2744 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2745 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2746 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2747 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2748 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2749 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2750
Girish K Sbec87262011-10-13 12:04:16 +05302751 /*
2752 * If Power Off Notify capability is enabled by the host,
2753 * set notify to short power off notify timeout value.
2754 */
2755 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2756 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2757 else
2758 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2759
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302760 /* Initial value for re-tuning timer count */
2761 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2762 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2763
2764 /*
2765 * In case Re-tuning Timer is not disabled, the actual value of
2766 * re-tuning timer will be 2 ^ (n - 1).
2767 */
2768 if (host->tuning_count)
2769 host->tuning_count = 1 << (host->tuning_count - 1);
2770
2771 /* Re-tuning mode supported by the Host Controller */
2772 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2773 SDHCI_RETUNING_MODE_SHIFT;
2774
Takashi Iwai8f230f42010-12-08 10:04:30 +01002775 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302776 /*
2777 * According to SD Host Controller spec v3.00, if the Host System
2778 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2779 * the value is meaningful only if Voltage Support in the Capabilities
2780 * register is set. The actual current value is 4 times the register
2781 * value.
2782 */
2783 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2784
2785 if (caps[0] & SDHCI_CAN_VDD_330) {
2786 int max_current_330;
2787
Takashi Iwai8f230f42010-12-08 10:04:30 +01002788 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302789
2790 max_current_330 = ((max_current_caps &
2791 SDHCI_MAX_CURRENT_330_MASK) >>
2792 SDHCI_MAX_CURRENT_330_SHIFT) *
2793 SDHCI_MAX_CURRENT_MULTIPLIER;
2794
2795 if (max_current_330 > 150)
2796 mmc->caps |= MMC_CAP_SET_XPC_330;
2797 }
2798 if (caps[0] & SDHCI_CAN_VDD_300) {
2799 int max_current_300;
2800
Takashi Iwai8f230f42010-12-08 10:04:30 +01002801 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302802
2803 max_current_300 = ((max_current_caps &
2804 SDHCI_MAX_CURRENT_300_MASK) >>
2805 SDHCI_MAX_CURRENT_300_SHIFT) *
2806 SDHCI_MAX_CURRENT_MULTIPLIER;
2807
2808 if (max_current_300 > 150)
2809 mmc->caps |= MMC_CAP_SET_XPC_300;
2810 }
2811 if (caps[0] & SDHCI_CAN_VDD_180) {
2812 int max_current_180;
2813
Takashi Iwai8f230f42010-12-08 10:04:30 +01002814 ocr_avail |= MMC_VDD_165_195;
2815
Arindam Nathf2119df2011-05-05 12:18:57 +05302816 max_current_180 = ((max_current_caps &
2817 SDHCI_MAX_CURRENT_180_MASK) >>
2818 SDHCI_MAX_CURRENT_180_SHIFT) *
2819 SDHCI_MAX_CURRENT_MULTIPLIER;
2820
2821 if (max_current_180 > 150)
2822 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302823
2824 /* Maximum current capabilities of the host at 1.8V */
2825 if (max_current_180 >= 800)
2826 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2827 else if (max_current_180 >= 600)
2828 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2829 else if (max_current_180 >= 400)
2830 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2831 else
2832 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302833 }
2834
Takashi Iwai8f230f42010-12-08 10:04:30 +01002835 mmc->ocr_avail = ocr_avail;
2836 mmc->ocr_avail_sdio = ocr_avail;
2837 if (host->ocr_avail_sdio)
2838 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2839 mmc->ocr_avail_sd = ocr_avail;
2840 if (host->ocr_avail_sd)
2841 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2842 else /* normal SD controllers don't support 1.8V */
2843 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2844 mmc->ocr_avail_mmc = ocr_avail;
2845 if (host->ocr_avail_mmc)
2846 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002847
2848 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302849 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002850 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002852 }
2853
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854 spin_lock_init(&host->lock);
2855
2856 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002857 * Maximum number of segments. Depends on if the hardware
2858 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002859 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002860 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002861 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002862 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002863 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002864 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002865 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002866
2867 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002868 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002869 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002871 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872
2873 /*
2874 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002875 * of bytes. When doing hardware scatter/gather, each entry cannot
2876 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002877 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002878 if (host->flags & SDHCI_USE_ADMA) {
2879 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2880 mmc->max_seg_size = 65535;
2881 else
2882 mmc->max_seg_size = 65536;
2883 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002884 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002885 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886
2887 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002888 * Maximum block size. This varies from controller to controller and
2889 * is specified in the capabilities register.
2890 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002891 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2892 mmc->max_blk_size = 2;
2893 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302894 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002895 SDHCI_MAX_BLOCK_SHIFT;
2896 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302897 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002898 "assuming 512 bytes\n", mmc_hostname(mmc));
2899 mmc->max_blk_size = 0;
2900 }
2901 }
2902
2903 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002904
2905 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002906 * Maximum block count.
2907 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002908 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002909
2910 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002911 * Init tasklets.
2912 */
2913 tasklet_init(&host->card_tasklet,
2914 sdhci_tasklet_card, (unsigned long)host);
2915 tasklet_init(&host->finish_tasklet,
2916 sdhci_tasklet_finish, (unsigned long)host);
2917
Al Viroe4cad1b2006-10-10 22:47:07 +01002918 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002919
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302920 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302921 init_waitqueue_head(&host->buf_ready_int);
2922
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302923 /* Initialize re-tuning timer */
2924 init_timer(&host->tuning_timer);
2925 host->tuning_timer.data = (unsigned long)host;
2926 host->tuning_timer.function = sdhci_tuning_timer;
2927 }
2928
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002929 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002930 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002931 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002932 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002933
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002934 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2935 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302936 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002937 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002938 }
2939
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002940 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002941
2942#ifdef CONFIG_MMC_DEBUG
2943 sdhci_dumpregs(host);
2944#endif
2945
Pierre Ossmanf9134312008-12-21 17:01:48 +01002946#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002947 snprintf(host->led_name, sizeof(host->led_name),
2948 "%s::", mmc_hostname(mmc));
2949 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002950 host->led.brightness = LED_OFF;
2951 host->led.default_trigger = mmc_hostname(mmc);
2952 host->led.brightness_set = sdhci_led_control;
2953
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002954 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002955 if (ret)
2956 goto reset;
2957#endif
2958
Pierre Ossman5f25a662006-10-04 02:15:39 -07002959 mmiowb();
2960
Pierre Ossmand129bce2006-03-24 03:18:17 -08002961 mmc_add_host(mmc);
2962
Girish K Sa3c76eb2011-10-11 11:44:09 +05302963 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002964 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002965 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2966 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002967
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002968 sdhci_enable_card_detection(host);
2969
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970 return 0;
2971
Pierre Ossmanf9134312008-12-21 17:01:48 +01002972#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002973reset:
2974 sdhci_reset(host, SDHCI_RESET_ALL);
2975 free_irq(host->irq, host);
2976#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002977untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002978 tasklet_kill(&host->card_tasklet);
2979 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002980
2981 return ret;
2982}
2983
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002984EXPORT_SYMBOL_GPL(sdhci_add_host);
2985
Pierre Ossman1e728592008-04-16 19:13:13 +02002986void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002987{
Pierre Ossman1e728592008-04-16 19:13:13 +02002988 unsigned long flags;
2989
2990 if (dead) {
2991 spin_lock_irqsave(&host->lock, flags);
2992
2993 host->flags |= SDHCI_DEVICE_DEAD;
2994
2995 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302996 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02002997 " transfer!\n", mmc_hostname(host->mmc));
2998
2999 host->mrq->cmd->error = -ENOMEDIUM;
3000 tasklet_schedule(&host->finish_tasklet);
3001 }
3002
3003 spin_unlock_irqrestore(&host->lock, flags);
3004 }
3005
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003006 sdhci_disable_card_detection(host);
3007
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003008 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003009
Pierre Ossmanf9134312008-12-21 17:01:48 +01003010#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003011 led_classdev_unregister(&host->led);
3012#endif
3013
Pierre Ossman1e728592008-04-16 19:13:13 +02003014 if (!dead)
3015 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003016
3017 free_irq(host->irq, host);
3018
3019 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303020 if (host->version >= SDHCI_SPEC_300)
3021 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003022
3023 tasklet_kill(&host->card_tasklet);
3024 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003025
Adrian Hunterceb61432011-12-27 15:48:41 +02003026 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003027 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003028
Pierre Ossman2134a922008-06-28 18:28:51 +02003029 kfree(host->adma_desc);
3030 kfree(host->align_buffer);
3031
3032 host->adma_desc = NULL;
3033 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003034}
3035
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003036EXPORT_SYMBOL_GPL(sdhci_remove_host);
3037
3038void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003039{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003040 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003041}
3042
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003043EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044
3045/*****************************************************************************\
3046 * *
3047 * Driver init/exit *
3048 * *
3049\*****************************************************************************/
3050
3051static int __init sdhci_drv_init(void)
3052{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303053 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003054 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303055 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003056
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003057 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003058}
3059
3060static void __exit sdhci_drv_exit(void)
3061{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003062}
3063
3064module_init(sdhci_drv_init);
3065module_exit(sdhci_drv_exit);
3066
Pierre Ossmandf673b22006-06-30 02:22:31 -07003067module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003068module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003069
Pierre Ossman32710e82009-04-08 20:14:54 +02003070MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003071MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003072MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003073
Pierre Ossmandf673b22006-06-30 02:22:31 -07003074MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003075MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");