blob: 96f4e548ba22154b102f328043e62f1b2e0594d4 [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 *);
Girish K S069c9f12012-01-06 09:56:39 +053052static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053053static 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 */
Girish K S069c9f12012-01-06 09:56:39 +05301017 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1018 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 flags |= SDHCI_CMD_DATA;
1020
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001021 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022}
1023
1024static void sdhci_finish_command(struct sdhci_host *host)
1025{
1026 int i;
1027
1028 BUG_ON(host->cmd == NULL);
1029
1030 if (host->cmd->flags & MMC_RSP_PRESENT) {
1031 if (host->cmd->flags & MMC_RSP_136) {
1032 /* CRC is stripped so we need to do some shifting. */
1033 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001034 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 SDHCI_RESPONSE + (3-i)*4) << 8;
1036 if (i != 3)
1037 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001038 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 SDHCI_RESPONSE + (3-i)*4-1);
1040 }
1041 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001042 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043 }
1044 }
1045
Pierre Ossman17b04292007-07-22 22:18:46 +02001046 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
Andrei Warkentine89d4562011-05-23 15:06:37 -05001048 /* Finished CMD23, now send actual command. */
1049 if (host->cmd == host->mrq->sbc) {
1050 host->cmd = NULL;
1051 sdhci_send_command(host, host->mrq->cmd);
1052 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001053
Andrei Warkentine89d4562011-05-23 15:06:37 -05001054 /* Processed actual command. */
1055 if (host->data && host->data_early)
1056 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057
Andrei Warkentine89d4562011-05-23 15:06:37 -05001058 if (!host->cmd->data)
1059 tasklet_schedule(&host->finish_tasklet);
1060
1061 host->cmd = NULL;
1062 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063}
1064
1065static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1066{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301067 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001068 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301069 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001070 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071
Todd Poynor30832ab2011-12-27 15:48:46 +02001072 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073 return;
1074
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001075 host->mmc->actual_clock = 0;
1076
Anton Vorontsov81146342009-03-17 00:13:59 +03001077 if (host->ops->set_clock) {
1078 host->ops->set_clock(host, clock);
1079 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1080 return;
1081 }
1082
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001083 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001084
1085 if (clock == 0)
1086 goto out;
1087
Zhangfei Gao85105c52010-08-06 07:10:01 +08001088 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301089 /*
1090 * Check if the Host Controller supports Programmable Clock
1091 * Mode.
1092 */
1093 if (host->clk_mul) {
1094 u16 ctrl;
1095
1096 /*
1097 * We need to figure out whether the Host Driver needs
1098 * to select Programmable Clock Mode, or the value can
1099 * be set automatically by the Host Controller based on
1100 * the Preset Value registers.
1101 */
1102 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1103 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1104 for (div = 1; div <= 1024; div++) {
1105 if (((host->max_clk * host->clk_mul) /
1106 div) <= clock)
1107 break;
1108 }
1109 /*
1110 * Set Programmable Clock Mode in the Clock
1111 * Control register.
1112 */
1113 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001114 real_div = div;
1115 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301116 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001117 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301118 } else {
1119 /* Version 3.00 divisors must be a multiple of 2. */
1120 if (host->max_clk <= clock)
1121 div = 1;
1122 else {
1123 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1124 div += 2) {
1125 if ((host->max_clk / div) <= clock)
1126 break;
1127 }
1128 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001129 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301130 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001131 }
1132 } else {
1133 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001134 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001135 if ((host->max_clk / div) <= clock)
1136 break;
1137 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001138 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301139 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001142 if (real_div)
1143 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1144
Arindam Nathc3ed3872011-05-05 12:19:06 +05301145 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001146 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1147 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001149 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150
Chris Ball27f6cb12009-09-22 16:45:31 -07001151 /* Wait max 20 ms */
1152 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001153 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001154 & SDHCI_CLOCK_INT_STABLE)) {
1155 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301156 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001157 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158 sdhci_dumpregs(host);
1159 return;
1160 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001161 timeout--;
1162 mdelay(1);
1163 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164
1165 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001166 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167
1168out:
1169 host->clock = clock;
1170}
1171
Adrian Hunterceb61432011-12-27 15:48:41 +02001172static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001173{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001174 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001175
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001176 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001177 switch (1 << power) {
1178 case MMC_VDD_165_195:
1179 pwr = SDHCI_POWER_180;
1180 break;
1181 case MMC_VDD_29_30:
1182 case MMC_VDD_30_31:
1183 pwr = SDHCI_POWER_300;
1184 break;
1185 case MMC_VDD_32_33:
1186 case MMC_VDD_33_34:
1187 pwr = SDHCI_POWER_330;
1188 break;
1189 default:
1190 BUG();
1191 }
1192 }
1193
1194 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001195 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001196
Pierre Ossmanae628902009-05-03 20:45:03 +02001197 host->pwr = pwr;
1198
1199 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001200 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001201 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001202 }
1203
1204 /*
1205 * Spec says that we should clear the power reg before setting
1206 * a new value. Some controllers don't seem to like this though.
1207 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001208 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001209 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001210
Andres Salomone08c1692008-07-04 10:00:03 -07001211 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001212 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001213 * and set turn on power at the same time, so set the voltage first.
1214 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001215 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001216 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1217
1218 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001219
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001221
1222 /*
1223 * Some controllers need an extra 10ms delay of 10ms before they
1224 * can apply clock after applying power
1225 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001226 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001227 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001228
1229 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001230}
1231
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232/*****************************************************************************\
1233 * *
1234 * MMC callbacks *
1235 * *
1236\*****************************************************************************/
1237
1238static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1239{
1240 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001241 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 unsigned long flags;
1243
1244 host = mmc_priv(mmc);
1245
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001246 sdhci_runtime_pm_get(host);
1247
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 spin_lock_irqsave(&host->lock, flags);
1249
1250 WARN_ON(host->mrq != NULL);
1251
Pierre Ossmanf9134312008-12-21 17:01:48 +01001252#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001254#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001255
1256 /*
1257 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1258 * requests if Auto-CMD12 is enabled.
1259 */
1260 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001261 if (mrq->stop) {
1262 mrq->data->stop = NULL;
1263 mrq->stop = NULL;
1264 }
1265 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266
1267 host->mrq = mrq;
1268
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001269 /* If polling, assume that the card is always present. */
1270 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1271 present = true;
1272 else
1273 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1274 SDHCI_CARD_PRESENT;
1275
1276 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001277 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301279 } else {
1280 u32 present_state;
1281
1282 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1283 /*
1284 * Check if the re-tuning timer has already expired and there
1285 * is no on-going data transfer. If so, we need to execute
1286 * tuning procedure before sending command.
1287 */
1288 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1289 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1290 spin_unlock_irqrestore(&host->lock, flags);
Girish K S069c9f12012-01-06 09:56:39 +05301291 sdhci_execute_tuning(mmc, mrq->cmd->opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301292 spin_lock_irqsave(&host->lock, flags);
1293
1294 /* Restore original mmc_request structure */
1295 host->mrq = mrq;
1296 }
1297
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001298 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001299 sdhci_send_command(host, mrq->sbc);
1300 else
1301 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301302 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303
Pierre Ossman5f25a662006-10-04 02:15:39 -07001304 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001305 spin_unlock_irqrestore(&host->lock, flags);
1306}
1307
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001308static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001311 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 u8 ctrl;
1313
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314 spin_lock_irqsave(&host->lock, flags);
1315
Adrian Hunterceb61432011-12-27 15:48:41 +02001316 if (host->flags & SDHCI_DEVICE_DEAD) {
1317 spin_unlock_irqrestore(&host->lock, flags);
1318 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1319 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1320 return;
1321 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001322
Pierre Ossmand129bce2006-03-24 03:18:17 -08001323 /*
1324 * Reset the chip on each power off.
1325 * Should clear out any weird states.
1326 */
1327 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001328 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001329 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001330 }
1331
1332 sdhci_set_clock(host, ios->clock);
1333
1334 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001335 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001337 vdd_bit = sdhci_set_power(host, ios->vdd);
1338
1339 if (host->vmmc && vdd_bit != -1) {
1340 spin_unlock_irqrestore(&host->lock, flags);
1341 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1342 spin_lock_irqsave(&host->lock, flags);
1343 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344
Philip Rakity643a81f2010-09-23 08:24:32 -07001345 if (host->ops->platform_send_init_74_clocks)
1346 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1347
Philip Rakity15ec4462010-11-19 16:48:39 -05001348 /*
1349 * If your platform has 8-bit width support but is not a v3 controller,
1350 * or if it requires special setup code, you should implement that in
1351 * platform_8bit_width().
1352 */
1353 if (host->ops->platform_8bit_width)
1354 host->ops->platform_8bit_width(host, ios->bus_width);
1355 else {
1356 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1357 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1358 ctrl &= ~SDHCI_CTRL_4BITBUS;
1359 if (host->version >= SDHCI_SPEC_300)
1360 ctrl |= SDHCI_CTRL_8BITBUS;
1361 } else {
1362 if (host->version >= SDHCI_SPEC_300)
1363 ctrl &= ~SDHCI_CTRL_8BITBUS;
1364 if (ios->bus_width == MMC_BUS_WIDTH_4)
1365 ctrl |= SDHCI_CTRL_4BITBUS;
1366 else
1367 ctrl &= ~SDHCI_CTRL_4BITBUS;
1368 }
1369 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1370 }
1371
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001372 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001373
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001374 if ((ios->timing == MMC_TIMING_SD_HS ||
1375 ios->timing == MMC_TIMING_MMC_HS)
1376 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001377 ctrl |= SDHCI_CTRL_HISPD;
1378 else
1379 ctrl &= ~SDHCI_CTRL_HISPD;
1380
Arindam Nathd6d50a12011-05-05 12:18:59 +05301381 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301382 u16 clk, ctrl_2;
1383 unsigned int clock;
1384
1385 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301386 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1387 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301388 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1389 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001390 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301391 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301392
1393 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1394 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301395 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301396 /*
1397 * We only need to set Driver Strength if the
1398 * preset value enable is not set.
1399 */
1400 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1401 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1402 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1403 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1404 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1405
1406 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301407 } else {
1408 /*
1409 * According to SDHC Spec v3.00, if the Preset Value
1410 * Enable in the Host Control 2 register is set, we
1411 * need to reset SD Clock Enable before changing High
1412 * Speed Enable to avoid generating clock gliches.
1413 */
Arindam Nath758535c2011-05-05 12:19:00 +05301414
1415 /* Reset SD Clock Enable */
1416 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1417 clk &= ~SDHCI_CLOCK_CARD_EN;
1418 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1419
1420 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1421
1422 /* Re-enable SD Clock */
1423 clock = host->clock;
1424 host->clock = 0;
1425 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301426 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301427
Arindam Nath49c468f2011-05-05 12:19:01 +05301428
1429 /* Reset SD Clock Enable */
1430 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1431 clk &= ~SDHCI_CLOCK_CARD_EN;
1432 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1433
Philip Rakity6322cdd2011-05-13 11:17:15 +05301434 if (host->ops->set_uhs_signaling)
1435 host->ops->set_uhs_signaling(host, ios->timing);
1436 else {
1437 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1438 /* Select Bus Speed Mode for host */
1439 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301440 if (ios->timing == MMC_TIMING_MMC_HS200)
1441 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1442 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301443 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1444 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1446 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1447 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1448 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1449 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1450 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1451 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1452 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1453 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301454
1455 /* Re-enable SD Clock */
1456 clock = host->clock;
1457 host->clock = 0;
1458 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301459 } else
1460 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301461
Leandro Dorileob8352262007-07-25 23:47:04 +02001462 /*
1463 * Some (ENE) controllers go apeshit on some ios operation,
1464 * signalling timeout and CRC errors even on CMD0. Resetting
1465 * it on each ios seems to solve the problem.
1466 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001467 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001468 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1469
Pierre Ossman5f25a662006-10-04 02:15:39 -07001470 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001471 spin_unlock_irqrestore(&host->lock, flags);
1472}
1473
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001474static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1475{
1476 struct sdhci_host *host = mmc_priv(mmc);
1477
1478 sdhci_runtime_pm_get(host);
1479 sdhci_do_set_ios(host, ios);
1480 sdhci_runtime_pm_put(host);
1481}
1482
1483static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001486 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487
Pierre Ossmand129bce2006-03-24 03:18:17 -08001488 spin_lock_irqsave(&host->lock, flags);
1489
Pierre Ossman1e728592008-04-16 19:13:13 +02001490 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001491 is_readonly = 0;
1492 else if (host->ops->get_ro)
1493 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001494 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001495 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1496 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497
1498 spin_unlock_irqrestore(&host->lock, flags);
1499
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001500 /* This quirk needs to be replaced by a callback-function later */
1501 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1502 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503}
1504
Takashi Iwai82b0e232011-04-21 20:26:38 +02001505#define SAMPLE_COUNT 5
1506
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001507static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001508{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001509 int i, ro_count;
1510
Takashi Iwai82b0e232011-04-21 20:26:38 +02001511 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001512 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001513
1514 ro_count = 0;
1515 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001516 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001517 if (++ro_count > SAMPLE_COUNT / 2)
1518 return 1;
1519 }
1520 msleep(30);
1521 }
1522 return 0;
1523}
1524
Adrian Hunter20758b62011-08-29 16:42:12 +03001525static void sdhci_hw_reset(struct mmc_host *mmc)
1526{
1527 struct sdhci_host *host = mmc_priv(mmc);
1528
1529 if (host->ops && host->ops->hw_reset)
1530 host->ops->hw_reset(host);
1531}
1532
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001533static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001534{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001535 struct sdhci_host *host = mmc_priv(mmc);
1536 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001537
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001538 sdhci_runtime_pm_get(host);
1539 ret = sdhci_do_get_ro(host);
1540 sdhci_runtime_pm_put(host);
1541 return ret;
1542}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001543
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001544static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1545{
Pierre Ossman1e728592008-04-16 19:13:13 +02001546 if (host->flags & SDHCI_DEVICE_DEAD)
1547 goto out;
1548
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001549 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001550 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1551 else
1552 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1553
1554 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1555 if (host->runtime_suspended)
1556 goto out;
1557
1558 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001559 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1560 else
1561 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001562out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001563 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001564}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001565
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001566static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1567{
1568 struct sdhci_host *host = mmc_priv(mmc);
1569 unsigned long flags;
1570
1571 spin_lock_irqsave(&host->lock, flags);
1572 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001573 spin_unlock_irqrestore(&host->lock, flags);
1574}
1575
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001576static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1577 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301578{
Arindam Nathf2119df2011-05-05 12:18:57 +05301579 u8 pwr;
1580 u16 clk, ctrl;
1581 u32 present_state;
1582
Arindam Nathf2119df2011-05-05 12:18:57 +05301583 /*
1584 * Signal Voltage Switching is only applicable for Host Controllers
1585 * v3.00 and above.
1586 */
1587 if (host->version < SDHCI_SPEC_300)
1588 return 0;
1589
1590 /*
1591 * We first check whether the request is to set signalling voltage
1592 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1593 */
1594 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1595 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1596 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1597 ctrl &= ~SDHCI_CTRL_VDD_180;
1598 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1599
1600 /* Wait for 5ms */
1601 usleep_range(5000, 5500);
1602
1603 /* 3.3V regulator output should be stable within 5 ms */
1604 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1605 if (!(ctrl & SDHCI_CTRL_VDD_180))
1606 return 0;
1607 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301608 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301609 "signalling voltage failed\n");
1610 return -EIO;
1611 }
1612 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1613 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1614 /* Stop SDCLK */
1615 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1616 clk &= ~SDHCI_CLOCK_CARD_EN;
1617 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1618
1619 /* Check whether DAT[3:0] is 0000 */
1620 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1621 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1622 SDHCI_DATA_LVL_SHIFT)) {
1623 /*
1624 * Enable 1.8V Signal Enable in the Host Control2
1625 * register
1626 */
1627 ctrl |= SDHCI_CTRL_VDD_180;
1628 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1629
1630 /* Wait for 5ms */
1631 usleep_range(5000, 5500);
1632
1633 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1634 if (ctrl & SDHCI_CTRL_VDD_180) {
1635 /* Provide SDCLK again and wait for 1ms*/
1636 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1637 clk |= SDHCI_CLOCK_CARD_EN;
1638 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1639 usleep_range(1000, 1500);
1640
1641 /*
1642 * If DAT[3:0] level is 1111b, then the card
1643 * was successfully switched to 1.8V signaling.
1644 */
1645 present_state = sdhci_readl(host,
1646 SDHCI_PRESENT_STATE);
1647 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1648 SDHCI_DATA_LVL_MASK)
1649 return 0;
1650 }
1651 }
1652
1653 /*
1654 * If we are here, that means the switch to 1.8V signaling
1655 * failed. We power cycle the card, and retry initialization
1656 * sequence by setting S18R to 0.
1657 */
1658 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1659 pwr &= ~SDHCI_POWER_ON;
1660 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1661
1662 /* Wait for 1ms as per the spec */
1663 usleep_range(1000, 1500);
1664 pwr |= SDHCI_POWER_ON;
1665 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1666
Girish K Sa3c76eb2011-10-11 11:44:09 +05301667 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301668 "voltage failed, retrying with S18R set to 0\n");
1669 return -EAGAIN;
1670 } else
1671 /* No signal voltage switch required */
1672 return 0;
1673}
1674
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001675static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1676 struct mmc_ios *ios)
1677{
1678 struct sdhci_host *host = mmc_priv(mmc);
1679 int err;
1680
1681 if (host->version < SDHCI_SPEC_300)
1682 return 0;
1683 sdhci_runtime_pm_get(host);
1684 err = sdhci_do_start_signal_voltage_switch(host, ios);
1685 sdhci_runtime_pm_put(host);
1686 return err;
1687}
1688
Girish K S069c9f12012-01-06 09:56:39 +05301689static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301690{
1691 struct sdhci_host *host;
1692 u16 ctrl;
1693 u32 ier;
1694 int tuning_loop_counter = MAX_TUNING_LOOP;
1695 unsigned long timeout;
1696 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301697 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301698
1699 host = mmc_priv(mmc);
1700
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301702 disable_irq(host->irq);
1703 spin_lock(&host->lock);
1704
1705 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1706
1707 /*
Girish K S069c9f12012-01-06 09:56:39 +05301708 * The Host Controller needs tuning only in case of SDR104 mode
1709 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301710 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301711 * If the Host Controller supports the HS200 mode then the
1712 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301713 */
Girish K S069c9f12012-01-06 09:56:39 +05301714 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1715 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1716 host->flags & SDHCI_HS200_NEEDS_TUNING))
1717 requires_tuning_nonuhs = true;
1718
Arindam Nathb513ea22011-05-05 12:19:04 +05301719 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301720 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301721 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1722 else {
1723 spin_unlock(&host->lock);
1724 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001725 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301726 return 0;
1727 }
1728
1729 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1730
1731 /*
1732 * As per the Host Controller spec v3.00, tuning command
1733 * generates Buffer Read Ready interrupt, so enable that.
1734 *
1735 * Note: The spec clearly says that when tuning sequence
1736 * is being performed, the controller does not generate
1737 * interrupts other than Buffer Read Ready interrupt. But
1738 * to make sure we don't hit a controller bug, we _only_
1739 * enable Buffer Read Ready interrupt here.
1740 */
1741 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1742 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1743
1744 /*
1745 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1746 * of loops reaches 40 times or a timeout of 150ms occurs.
1747 */
1748 timeout = 150;
1749 do {
1750 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001751 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301752
1753 if (!tuning_loop_counter && !timeout)
1754 break;
1755
Girish K S069c9f12012-01-06 09:56:39 +05301756 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301757 cmd.arg = 0;
1758 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1759 cmd.retries = 0;
1760 cmd.data = NULL;
1761 cmd.error = 0;
1762
1763 mrq.cmd = &cmd;
1764 host->mrq = &mrq;
1765
1766 /*
1767 * In response to CMD19, the card sends 64 bytes of tuning
1768 * block to the Host Controller. So we set the block size
1769 * to 64 here.
1770 */
Girish K S069c9f12012-01-06 09:56:39 +05301771 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1772 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1773 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1774 SDHCI_BLOCK_SIZE);
1775 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1776 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1777 SDHCI_BLOCK_SIZE);
1778 } else {
1779 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1780 SDHCI_BLOCK_SIZE);
1781 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301782
1783 /*
1784 * The tuning block is sent by the card to the host controller.
1785 * So we set the TRNS_READ bit in the Transfer Mode register.
1786 * This also takes care of setting DMA Enable and Multi Block
1787 * Select in the same register to 0.
1788 */
1789 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1790
1791 sdhci_send_command(host, &cmd);
1792
1793 host->cmd = NULL;
1794 host->mrq = NULL;
1795
1796 spin_unlock(&host->lock);
1797 enable_irq(host->irq);
1798
1799 /* Wait for Buffer Read Ready interrupt */
1800 wait_event_interruptible_timeout(host->buf_ready_int,
1801 (host->tuning_done == 1),
1802 msecs_to_jiffies(50));
1803 disable_irq(host->irq);
1804 spin_lock(&host->lock);
1805
1806 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301807 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301808 "Buffer Read Ready interrupt during tuning "
1809 "procedure, falling back to fixed sampling "
1810 "clock\n");
1811 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1812 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1813 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1814 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1815
1816 err = -EIO;
1817 goto out;
1818 }
1819
1820 host->tuning_done = 0;
1821
1822 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1823 tuning_loop_counter--;
1824 timeout--;
1825 mdelay(1);
1826 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1827
1828 /*
1829 * The Host Driver has exhausted the maximum number of loops allowed,
1830 * so use fixed sampling frequency.
1831 */
1832 if (!tuning_loop_counter || !timeout) {
1833 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1834 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1835 } else {
1836 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301837 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301838 " failed, falling back to fixed sampling"
1839 " clock\n");
1840 err = -EIO;
1841 }
1842 }
1843
1844out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301845 /*
1846 * If this is the very first time we are here, we start the retuning
1847 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1848 * flag won't be set, we check this condition before actually starting
1849 * the timer.
1850 */
1851 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1852 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1853 mod_timer(&host->tuning_timer, jiffies +
1854 host->tuning_count * HZ);
1855 /* Tuning mode 1 limits the maximum data length to 4MB */
1856 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1857 } else {
1858 host->flags &= ~SDHCI_NEEDS_RETUNING;
1859 /* Reload the new initial value for timer */
1860 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1861 mod_timer(&host->tuning_timer, jiffies +
1862 host->tuning_count * HZ);
1863 }
1864
1865 /*
1866 * In case tuning fails, host controllers which support re-tuning can
1867 * try tuning again at a later time, when the re-tuning timer expires.
1868 * So for these controllers, we return 0. Since there might be other
1869 * controllers who do not have this capability, we return error for
1870 * them.
1871 */
1872 if (err && host->tuning_count &&
1873 host->tuning_mode == SDHCI_TUNING_MODE_1)
1874 err = 0;
1875
Arindam Nathb513ea22011-05-05 12:19:04 +05301876 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1877 spin_unlock(&host->lock);
1878 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001879 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301880
1881 return err;
1882}
1883
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001884static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301885{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301886 u16 ctrl;
1887 unsigned long flags;
1888
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301889 /* Host Controller v3.00 defines preset value registers */
1890 if (host->version < SDHCI_SPEC_300)
1891 return;
1892
1893 spin_lock_irqsave(&host->lock, flags);
1894
1895 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1896
1897 /*
1898 * We only enable or disable Preset Value if they are not already
1899 * enabled or disabled respectively. Otherwise, we bail out.
1900 */
1901 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1902 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1903 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001904 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301905 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1906 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1907 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001908 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301909 }
1910
1911 spin_unlock_irqrestore(&host->lock, flags);
1912}
1913
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001914static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1915{
1916 struct sdhci_host *host = mmc_priv(mmc);
1917
1918 sdhci_runtime_pm_get(host);
1919 sdhci_do_enable_preset_value(host, enable);
1920 sdhci_runtime_pm_put(host);
1921}
1922
David Brownellab7aefd2006-11-12 17:55:30 -08001923static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001924 .request = sdhci_request,
1925 .set_ios = sdhci_set_ios,
1926 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001927 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001928 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301929 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301930 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301931 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001932};
1933
1934/*****************************************************************************\
1935 * *
1936 * Tasklets *
1937 * *
1938\*****************************************************************************/
1939
1940static void sdhci_tasklet_card(unsigned long param)
1941{
1942 struct sdhci_host *host;
1943 unsigned long flags;
1944
1945 host = (struct sdhci_host*)param;
1946
1947 spin_lock_irqsave(&host->lock, flags);
1948
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001949 /* Check host->mrq first in case we are runtime suspended */
1950 if (host->mrq &&
1951 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301952 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001953 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301954 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001955 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001956
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001957 sdhci_reset(host, SDHCI_RESET_CMD);
1958 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001959
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001960 host->mrq->cmd->error = -ENOMEDIUM;
1961 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001962 }
1963
1964 spin_unlock_irqrestore(&host->lock, flags);
1965
Pierre Ossman04cf5852008-08-18 22:18:14 +02001966 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967}
1968
1969static void sdhci_tasklet_finish(unsigned long param)
1970{
1971 struct sdhci_host *host;
1972 unsigned long flags;
1973 struct mmc_request *mrq;
1974
1975 host = (struct sdhci_host*)param;
1976
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001977 spin_lock_irqsave(&host->lock, flags);
1978
Chris Ball0c9c99a2011-04-27 17:35:31 -04001979 /*
1980 * If this tasklet gets rescheduled while running, it will
1981 * be run again afterwards but without any active request.
1982 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001983 if (!host->mrq) {
1984 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001985 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001986 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001987
1988 del_timer(&host->timer);
1989
1990 mrq = host->mrq;
1991
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992 /*
1993 * The controller needs a reset of internal state machines
1994 * upon error conditions.
1995 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001996 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001997 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001998 (mrq->data && (mrq->data->error ||
1999 (mrq->data->stop && mrq->data->stop->error))) ||
2000 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002001
2002 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002003 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002004 unsigned int clock;
2005
2006 /* This is to force an update */
2007 clock = host->clock;
2008 host->clock = 0;
2009 sdhci_set_clock(host, clock);
2010 }
2011
2012 /* Spec says we should do both at the same time, but Ricoh
2013 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002014 sdhci_reset(host, SDHCI_RESET_CMD);
2015 sdhci_reset(host, SDHCI_RESET_DATA);
2016 }
2017
2018 host->mrq = NULL;
2019 host->cmd = NULL;
2020 host->data = NULL;
2021
Pierre Ossmanf9134312008-12-21 17:01:48 +01002022#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002023 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002024#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002025
Pierre Ossman5f25a662006-10-04 02:15:39 -07002026 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002027 spin_unlock_irqrestore(&host->lock, flags);
2028
2029 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002030 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002031}
2032
2033static void sdhci_timeout_timer(unsigned long data)
2034{
2035 struct sdhci_host *host;
2036 unsigned long flags;
2037
2038 host = (struct sdhci_host*)data;
2039
2040 spin_lock_irqsave(&host->lock, flags);
2041
2042 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302043 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002044 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002045 sdhci_dumpregs(host);
2046
2047 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002048 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002049 sdhci_finish_data(host);
2050 } else {
2051 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002052 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002053 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002054 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002055
2056 tasklet_schedule(&host->finish_tasklet);
2057 }
2058 }
2059
Pierre Ossman5f25a662006-10-04 02:15:39 -07002060 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002061 spin_unlock_irqrestore(&host->lock, flags);
2062}
2063
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302064static void sdhci_tuning_timer(unsigned long data)
2065{
2066 struct sdhci_host *host;
2067 unsigned long flags;
2068
2069 host = (struct sdhci_host *)data;
2070
2071 spin_lock_irqsave(&host->lock, flags);
2072
2073 host->flags |= SDHCI_NEEDS_RETUNING;
2074
2075 spin_unlock_irqrestore(&host->lock, flags);
2076}
2077
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078/*****************************************************************************\
2079 * *
2080 * Interrupt handling *
2081 * *
2082\*****************************************************************************/
2083
2084static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2085{
2086 BUG_ON(intmask == 0);
2087
2088 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302089 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002090 "though no command operation was in progress.\n",
2091 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092 sdhci_dumpregs(host);
2093 return;
2094 }
2095
Pierre Ossman43b58b32007-07-25 23:15:27 +02002096 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002097 host->cmd->error = -ETIMEDOUT;
2098 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2099 SDHCI_INT_INDEX))
2100 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002101
Pierre Ossmane8095172008-07-25 01:09:08 +02002102 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002103 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002104 return;
2105 }
2106
2107 /*
2108 * The host can send and interrupt when the busy state has
2109 * ended, allowing us to wait without wasting CPU cycles.
2110 * Unfortunately this is overloaded on the "data complete"
2111 * interrupt, so we need to take some care when handling
2112 * it.
2113 *
2114 * Note: The 1.0 specification is a bit ambiguous about this
2115 * feature so there might be some problems with older
2116 * controllers.
2117 */
2118 if (host->cmd->flags & MMC_RSP_BUSY) {
2119 if (host->cmd->data)
2120 DBG("Cannot wait for busy signal when also "
2121 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002122 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002123 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002124
2125 /* The controller does not support the end-of-busy IRQ,
2126 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002127 }
2128
2129 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002130 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131}
2132
George G. Davis0957c332010-02-18 12:32:12 -05002133#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002134static void sdhci_show_adma_error(struct sdhci_host *host)
2135{
2136 const char *name = mmc_hostname(host->mmc);
2137 u8 *desc = host->adma_desc;
2138 __le32 *dma;
2139 __le16 *len;
2140 u8 attr;
2141
2142 sdhci_dumpregs(host);
2143
2144 while (true) {
2145 dma = (__le32 *)(desc + 4);
2146 len = (__le16 *)(desc + 2);
2147 attr = *desc;
2148
2149 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2150 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2151
2152 desc += 8;
2153
2154 if (attr & 2)
2155 break;
2156 }
2157}
2158#else
2159static void sdhci_show_adma_error(struct sdhci_host *host) { }
2160#endif
2161
Pierre Ossmand129bce2006-03-24 03:18:17 -08002162static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2163{
Girish K S069c9f12012-01-06 09:56:39 +05302164 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002165 BUG_ON(intmask == 0);
2166
Arindam Nathb513ea22011-05-05 12:19:04 +05302167 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2168 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302169 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2170 if (command == MMC_SEND_TUNING_BLOCK ||
2171 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302172 host->tuning_done = 1;
2173 wake_up(&host->buf_ready_int);
2174 return;
2175 }
2176 }
2177
Pierre Ossmand129bce2006-03-24 03:18:17 -08002178 if (!host->data) {
2179 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002180 * The "data complete" interrupt is also used to
2181 * indicate that a busy state has ended. See comment
2182 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002184 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2185 if (intmask & SDHCI_INT_DATA_END) {
2186 sdhci_finish_command(host);
2187 return;
2188 }
2189 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190
Girish K Sa3c76eb2011-10-11 11:44:09 +05302191 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002192 "though no data operation was in progress.\n",
2193 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_dumpregs(host);
2195
2196 return;
2197 }
2198
2199 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002200 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002201 else if (intmask & SDHCI_INT_DATA_END_BIT)
2202 host->data->error = -EILSEQ;
2203 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2204 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2205 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002206 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002207 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302208 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002209 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002210 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002211 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002212
Pierre Ossman17b04292007-07-22 22:18:46 +02002213 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002214 sdhci_finish_data(host);
2215 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002216 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217 sdhci_transfer_pio(host);
2218
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002219 /*
2220 * We currently don't do anything fancy with DMA
2221 * boundaries, but as we can't disable the feature
2222 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002223 *
2224 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2225 * should return a valid address to continue from, but as
2226 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002227 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002228 if (intmask & SDHCI_INT_DMA_END) {
2229 u32 dmastart, dmanow;
2230 dmastart = sg_dma_address(host->data->sg);
2231 dmanow = dmastart + host->data->bytes_xfered;
2232 /*
2233 * Force update to the next DMA block boundary.
2234 */
2235 dmanow = (dmanow &
2236 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2237 SDHCI_DEFAULT_BOUNDARY_SIZE;
2238 host->data->bytes_xfered = dmanow - dmastart;
2239 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2240 " next 0x%08x\n",
2241 mmc_hostname(host->mmc), dmastart,
2242 host->data->bytes_xfered, dmanow);
2243 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2244 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002245
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002246 if (intmask & SDHCI_INT_DATA_END) {
2247 if (host->cmd) {
2248 /*
2249 * Data managed to finish before the
2250 * command completed. Make sure we do
2251 * things in the proper order.
2252 */
2253 host->data_early = 1;
2254 } else {
2255 sdhci_finish_data(host);
2256 }
2257 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258 }
2259}
2260
David Howells7d12e782006-10-05 14:55:46 +01002261static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002262{
2263 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002264 struct sdhci_host *host = dev_id;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002266 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267
2268 spin_lock(&host->lock);
2269
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002270 if (host->runtime_suspended) {
2271 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302272 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002273 mmc_hostname(host->mmc));
2274 return IRQ_HANDLED;
2275 }
2276
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002277 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278
Mark Lord62df67a52007-03-06 13:30:13 +01002279 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002280 result = IRQ_NONE;
2281 goto out;
2282 }
2283
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002284 DBG("*** %s got interrupt: 0x%08x\n",
2285 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286
Pierre Ossman3192a282006-06-30 02:22:26 -07002287 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002288 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2289 SDHCI_CARD_PRESENT;
2290
2291 /*
2292 * There is a observation on i.mx esdhc. INSERT bit will be
2293 * immediately set again when it gets cleared, if a card is
2294 * inserted. We have to mask the irq to prevent interrupt
2295 * storm which will freeze the system. And the REMOVE gets
2296 * the same situation.
2297 *
2298 * More testing are needed here to ensure it works for other
2299 * platforms though.
2300 */
2301 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2302 SDHCI_INT_CARD_REMOVE);
2303 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2304 SDHCI_INT_CARD_INSERT);
2305
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002306 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002307 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2308 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002310 }
2311
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002313 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2314 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002315 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002316 }
2317
2318 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002319 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2320 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002321 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 }
2323
2324 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2325
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002326 intmask &= ~SDHCI_INT_ERROR;
2327
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302329 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002331 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332 }
2333
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002334 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002335
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002336 if (intmask & SDHCI_INT_CARD_INT)
2337 cardint = 1;
2338
2339 intmask &= ~SDHCI_INT_CARD_INT;
2340
Pierre Ossman3192a282006-06-30 02:22:26 -07002341 if (intmask) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302342 pr_err("%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002343 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002344 sdhci_dumpregs(host);
2345
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002346 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002347 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002348
2349 result = IRQ_HANDLED;
2350
Pierre Ossman5f25a662006-10-04 02:15:39 -07002351 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352out:
2353 spin_unlock(&host->lock);
2354
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002355 /*
2356 * We have to delay this as it calls back into the driver.
2357 */
2358 if (cardint)
2359 mmc_signal_sdio_irq(host->mmc);
2360
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361 return result;
2362}
2363
2364/*****************************************************************************\
2365 * *
2366 * Suspend/resume *
2367 * *
2368\*****************************************************************************/
2369
2370#ifdef CONFIG_PM
2371
Manuel Lauss29495aa2011-11-03 11:09:45 +01002372int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002373{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002374 int ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002375 bool has_tuning_timer;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002377 sdhci_disable_card_detection(host);
2378
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302379 /* Disable tuning since we are suspending */
Aaron Lu38a60ea2012-01-04 10:07:43 +08002380 has_tuning_timer = host->version >= SDHCI_SPEC_300 &&
2381 host->tuning_count && host->tuning_mode == SDHCI_TUNING_MODE_1;
2382 if (has_tuning_timer) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002383 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302384 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302385 }
2386
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002387 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002388 if (ret) {
2389 if (has_tuning_timer) {
2390 host->flags |= SDHCI_NEEDS_RETUNING;
2391 mod_timer(&host->tuning_timer, jiffies +
2392 host->tuning_count * HZ);
2393 }
2394
2395 sdhci_enable_card_detection(host);
2396
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002397 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002398 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002399
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002400 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002402 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002403}
2404
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002405EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002407int sdhci_resume_host(struct sdhci_host *host)
2408{
2409 int ret;
2410
Richard Röjforsa13abc72009-09-22 16:45:30 -07002411 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002412 if (host->ops->enable_dma)
2413 host->ops->enable_dma(host);
2414 }
2415
2416 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2417 mmc_hostname(host->mmc), host);
2418 if (ret)
2419 return ret;
2420
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002421 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002422 mmiowb();
2423
2424 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002425 sdhci_enable_card_detection(host);
2426
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302427 /* Set the re-tuning expiration flag */
2428 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2429 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2430 host->flags |= SDHCI_NEEDS_RETUNING;
2431
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002432 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002433}
2434
2435EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002436
Daniel Drake5f619702010-11-04 22:20:39 +00002437void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2438{
2439 u8 val;
2440 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2441 val |= SDHCI_WAKE_ON_INT;
2442 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2443}
2444
2445EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2446
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447#endif /* CONFIG_PM */
2448
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002449#ifdef CONFIG_PM_RUNTIME
2450
2451static int sdhci_runtime_pm_get(struct sdhci_host *host)
2452{
2453 return pm_runtime_get_sync(host->mmc->parent);
2454}
2455
2456static int sdhci_runtime_pm_put(struct sdhci_host *host)
2457{
2458 pm_runtime_mark_last_busy(host->mmc->parent);
2459 return pm_runtime_put_autosuspend(host->mmc->parent);
2460}
2461
2462int sdhci_runtime_suspend_host(struct sdhci_host *host)
2463{
2464 unsigned long flags;
2465 int ret = 0;
2466
2467 /* Disable tuning since we are suspending */
2468 if (host->version >= SDHCI_SPEC_300 &&
2469 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2470 del_timer_sync(&host->tuning_timer);
2471 host->flags &= ~SDHCI_NEEDS_RETUNING;
2472 }
2473
2474 spin_lock_irqsave(&host->lock, flags);
2475 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2476 spin_unlock_irqrestore(&host->lock, flags);
2477
2478 synchronize_irq(host->irq);
2479
2480 spin_lock_irqsave(&host->lock, flags);
2481 host->runtime_suspended = true;
2482 spin_unlock_irqrestore(&host->lock, flags);
2483
2484 return ret;
2485}
2486EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2487
2488int sdhci_runtime_resume_host(struct sdhci_host *host)
2489{
2490 unsigned long flags;
2491 int ret = 0, host_flags = host->flags;
2492
2493 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2494 if (host->ops->enable_dma)
2495 host->ops->enable_dma(host);
2496 }
2497
2498 sdhci_init(host, 0);
2499
2500 /* Force clock and power re-program */
2501 host->pwr = 0;
2502 host->clock = 0;
2503 sdhci_do_set_ios(host, &host->mmc->ios);
2504
2505 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2506 if (host_flags & SDHCI_PV_ENABLED)
2507 sdhci_do_enable_preset_value(host, true);
2508
2509 /* Set the re-tuning expiration flag */
2510 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2511 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2512 host->flags |= SDHCI_NEEDS_RETUNING;
2513
2514 spin_lock_irqsave(&host->lock, flags);
2515
2516 host->runtime_suspended = false;
2517
2518 /* Enable SDIO IRQ */
2519 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2520 sdhci_enable_sdio_irq_nolock(host, true);
2521
2522 /* Enable Card Detection */
2523 sdhci_enable_card_detection(host);
2524
2525 spin_unlock_irqrestore(&host->lock, flags);
2526
2527 return ret;
2528}
2529EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2530
2531#endif
2532
Pierre Ossmand129bce2006-03-24 03:18:17 -08002533/*****************************************************************************\
2534 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002535 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002536 * *
2537\*****************************************************************************/
2538
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002539struct sdhci_host *sdhci_alloc_host(struct device *dev,
2540 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002541{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002542 struct mmc_host *mmc;
2543 struct sdhci_host *host;
2544
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002545 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002546
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002547 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002548 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002549 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002550
2551 host = mmc_priv(mmc);
2552 host->mmc = mmc;
2553
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002554 return host;
2555}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002556
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002557EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002558
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002559int sdhci_add_host(struct sdhci_host *host)
2560{
2561 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302562 u32 caps[2];
2563 u32 max_current_caps;
2564 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002565 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002566
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002567 WARN_ON(host == NULL);
2568 if (host == NULL)
2569 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002571 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002572
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002573 if (debug_quirks)
2574 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002575 if (debug_quirks2)
2576 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002577
Pierre Ossmand96649e2006-06-30 02:22:30 -07002578 sdhci_reset(host, SDHCI_RESET_ALL);
2579
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002580 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002581 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2582 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002583 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302584 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002585 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002586 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002587 }
2588
Arindam Nathf2119df2011-05-05 12:18:57 +05302589 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002590 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002591
Arindam Nathf2119df2011-05-05 12:18:57 +05302592 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2593 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2594
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002595 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002596 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302597 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002598 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002599 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002600 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002602 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002603 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002604 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002605 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002606 }
2607
Arindam Nathf2119df2011-05-05 12:18:57 +05302608 if ((host->version >= SDHCI_SPEC_200) &&
2609 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002610 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002611
2612 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2613 (host->flags & SDHCI_USE_ADMA)) {
2614 DBG("Disabling ADMA as it is marked broken\n");
2615 host->flags &= ~SDHCI_USE_ADMA;
2616 }
2617
Richard Röjforsa13abc72009-09-22 16:45:30 -07002618 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002619 if (host->ops->enable_dma) {
2620 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302621 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002622 "available. Falling back to PIO.\n",
2623 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002624 host->flags &=
2625 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002626 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002627 }
2628 }
2629
Pierre Ossman2134a922008-06-28 18:28:51 +02002630 if (host->flags & SDHCI_USE_ADMA) {
2631 /*
2632 * We need to allocate descriptors for all sg entries
2633 * (128) and potentially one alignment transfer for
2634 * each of those entries.
2635 */
2636 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2637 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2638 if (!host->adma_desc || !host->align_buffer) {
2639 kfree(host->adma_desc);
2640 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302641 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002642 "buffers. Falling back to standard DMA.\n",
2643 mmc_hostname(mmc));
2644 host->flags &= ~SDHCI_USE_ADMA;
2645 }
2646 }
2647
Pierre Ossman76591502008-07-21 00:32:11 +02002648 /*
2649 * If we use DMA, then it's up to the caller to set the DMA
2650 * mask, but PIO does not need the hw shim so we set a new
2651 * mask here in that case.
2652 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002653 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002654 host->dma_mask = DMA_BIT_MASK(64);
2655 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2656 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002657
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002658 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302659 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002660 >> SDHCI_CLOCK_BASE_SHIFT;
2661 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302662 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002663 >> SDHCI_CLOCK_BASE_SHIFT;
2664
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002666 if (host->max_clk == 0 || host->quirks &
2667 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002668 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302669 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002670 "frequency.\n", mmc_hostname(mmc));
2671 return -ENODEV;
2672 }
2673 host->max_clk = host->ops->get_max_clock(host);
2674 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002675
2676 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302677 * In case of Host Controller v3.00, find out whether clock
2678 * multiplier is supported.
2679 */
2680 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2681 SDHCI_CLOCK_MUL_SHIFT;
2682
2683 /*
2684 * In case the value in Clock Multiplier is 0, then programmable
2685 * clock mode is not supported, otherwise the actual clock
2686 * multiplier is one more than the value of Clock Multiplier
2687 * in the Capabilities Register.
2688 */
2689 if (host->clk_mul)
2690 host->clk_mul += 1;
2691
2692 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002693 * Set host parameters.
2694 */
2695 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302696 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002697 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002698 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302699 else if (host->version >= SDHCI_SPEC_300) {
2700 if (host->clk_mul) {
2701 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2702 mmc->f_max = host->max_clk * host->clk_mul;
2703 } else
2704 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2705 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002706 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002707
Andy Shevchenko272308c2011-08-03 18:36:00 +03002708 host->timeout_clk =
2709 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2710 if (host->timeout_clk == 0) {
2711 if (host->ops->get_timeout_clock) {
2712 host->timeout_clk = host->ops->get_timeout_clock(host);
2713 } else if (!(host->quirks &
2714 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302715 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002716 "frequency.\n", mmc_hostname(mmc));
2717 return -ENODEV;
2718 }
2719 }
2720 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2721 host->timeout_clk *= 1000;
2722
2723 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002724 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002725
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002726 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002727
Andrei Warkentine89d4562011-05-23 15:06:37 -05002728 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2729
2730 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2731 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002732
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002733 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002734 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002735 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002736 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002737 host->flags |= SDHCI_AUTO_CMD23;
2738 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2739 } else {
2740 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2741 }
2742
Philip Rakity15ec4462010-11-19 16:48:39 -05002743 /*
2744 * A controller may support 8-bit width, but the board itself
2745 * might not have the pins brought out. Boards that support
2746 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2747 * their platform code before calling sdhci_add_host(), and we
2748 * won't assume 8-bit width for hosts without that CAP.
2749 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002750 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002751 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002752
Arindam Nathf2119df2011-05-05 12:18:57 +05302753 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002754 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002755
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002756 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2757 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002758 mmc->caps |= MMC_CAP_NEEDS_POLL;
2759
Arindam Nathf2119df2011-05-05 12:18:57 +05302760 /* UHS-I mode(s) supported by the host controller. */
2761 if (host->version >= SDHCI_SPEC_300)
2762 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2763
2764 /* SDR104 supports also implies SDR50 support */
2765 if (caps[1] & SDHCI_SUPPORT_SDR104)
2766 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2767 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2768 mmc->caps |= MMC_CAP_UHS_SDR50;
2769
2770 if (caps[1] & SDHCI_SUPPORT_DDR50)
2771 mmc->caps |= MMC_CAP_UHS_DDR50;
2772
Girish K S069c9f12012-01-06 09:56:39 +05302773 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302774 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2775 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2776
Girish K S069c9f12012-01-06 09:56:39 +05302777 /* Does the host need tuning for HS200? */
2778 if (mmc->caps2 & MMC_CAP2_HS200)
2779 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2780
Arindam Nathd6d50a12011-05-05 12:18:59 +05302781 /* Driver Type(s) (A, C, D) supported by the host */
2782 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2783 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2784 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2785 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2786 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2787 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2788
Girish K Sbec87262011-10-13 12:04:16 +05302789 /*
2790 * If Power Off Notify capability is enabled by the host,
2791 * set notify to short power off notify timeout value.
2792 */
2793 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2794 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2795 else
2796 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2797
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302798 /* Initial value for re-tuning timer count */
2799 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2800 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2801
2802 /*
2803 * In case Re-tuning Timer is not disabled, the actual value of
2804 * re-tuning timer will be 2 ^ (n - 1).
2805 */
2806 if (host->tuning_count)
2807 host->tuning_count = 1 << (host->tuning_count - 1);
2808
2809 /* Re-tuning mode supported by the Host Controller */
2810 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2811 SDHCI_RETUNING_MODE_SHIFT;
2812
Takashi Iwai8f230f42010-12-08 10:04:30 +01002813 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302814 /*
2815 * According to SD Host Controller spec v3.00, if the Host System
2816 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2817 * the value is meaningful only if Voltage Support in the Capabilities
2818 * register is set. The actual current value is 4 times the register
2819 * value.
2820 */
2821 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2822
2823 if (caps[0] & SDHCI_CAN_VDD_330) {
2824 int max_current_330;
2825
Takashi Iwai8f230f42010-12-08 10:04:30 +01002826 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302827
2828 max_current_330 = ((max_current_caps &
2829 SDHCI_MAX_CURRENT_330_MASK) >>
2830 SDHCI_MAX_CURRENT_330_SHIFT) *
2831 SDHCI_MAX_CURRENT_MULTIPLIER;
2832
2833 if (max_current_330 > 150)
2834 mmc->caps |= MMC_CAP_SET_XPC_330;
2835 }
2836 if (caps[0] & SDHCI_CAN_VDD_300) {
2837 int max_current_300;
2838
Takashi Iwai8f230f42010-12-08 10:04:30 +01002839 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302840
2841 max_current_300 = ((max_current_caps &
2842 SDHCI_MAX_CURRENT_300_MASK) >>
2843 SDHCI_MAX_CURRENT_300_SHIFT) *
2844 SDHCI_MAX_CURRENT_MULTIPLIER;
2845
2846 if (max_current_300 > 150)
2847 mmc->caps |= MMC_CAP_SET_XPC_300;
2848 }
2849 if (caps[0] & SDHCI_CAN_VDD_180) {
2850 int max_current_180;
2851
Takashi Iwai8f230f42010-12-08 10:04:30 +01002852 ocr_avail |= MMC_VDD_165_195;
2853
Arindam Nathf2119df2011-05-05 12:18:57 +05302854 max_current_180 = ((max_current_caps &
2855 SDHCI_MAX_CURRENT_180_MASK) >>
2856 SDHCI_MAX_CURRENT_180_SHIFT) *
2857 SDHCI_MAX_CURRENT_MULTIPLIER;
2858
2859 if (max_current_180 > 150)
2860 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302861
2862 /* Maximum current capabilities of the host at 1.8V */
2863 if (max_current_180 >= 800)
2864 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2865 else if (max_current_180 >= 600)
2866 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2867 else if (max_current_180 >= 400)
2868 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2869 else
2870 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302871 }
2872
Takashi Iwai8f230f42010-12-08 10:04:30 +01002873 mmc->ocr_avail = ocr_avail;
2874 mmc->ocr_avail_sdio = ocr_avail;
2875 if (host->ocr_avail_sdio)
2876 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2877 mmc->ocr_avail_sd = ocr_avail;
2878 if (host->ocr_avail_sd)
2879 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2880 else /* normal SD controllers don't support 1.8V */
2881 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2882 mmc->ocr_avail_mmc = ocr_avail;
2883 if (host->ocr_avail_mmc)
2884 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002885
2886 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302887 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002888 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002889 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002890 }
2891
Pierre Ossmand129bce2006-03-24 03:18:17 -08002892 spin_lock_init(&host->lock);
2893
2894 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002895 * Maximum number of segments. Depends on if the hardware
2896 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002897 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002898 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002899 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002900 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002901 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002902 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002903 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002904
2905 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002906 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002907 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002908 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002909 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002910
2911 /*
2912 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002913 * of bytes. When doing hardware scatter/gather, each entry cannot
2914 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002915 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002916 if (host->flags & SDHCI_USE_ADMA) {
2917 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2918 mmc->max_seg_size = 65535;
2919 else
2920 mmc->max_seg_size = 65536;
2921 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002922 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002923 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002924
2925 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002926 * Maximum block size. This varies from controller to controller and
2927 * is specified in the capabilities register.
2928 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002929 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2930 mmc->max_blk_size = 2;
2931 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302932 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002933 SDHCI_MAX_BLOCK_SHIFT;
2934 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302935 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002936 "assuming 512 bytes\n", mmc_hostname(mmc));
2937 mmc->max_blk_size = 0;
2938 }
2939 }
2940
2941 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002942
2943 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002944 * Maximum block count.
2945 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002946 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002947
2948 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002949 * Init tasklets.
2950 */
2951 tasklet_init(&host->card_tasklet,
2952 sdhci_tasklet_card, (unsigned long)host);
2953 tasklet_init(&host->finish_tasklet,
2954 sdhci_tasklet_finish, (unsigned long)host);
2955
Al Viroe4cad1b2006-10-10 22:47:07 +01002956 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002957
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302958 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302959 init_waitqueue_head(&host->buf_ready_int);
2960
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302961 /* Initialize re-tuning timer */
2962 init_timer(&host->tuning_timer);
2963 host->tuning_timer.data = (unsigned long)host;
2964 host->tuning_timer.function = sdhci_tuning_timer;
2965 }
2966
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002967 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002968 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002969 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002970 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002972 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2973 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302974 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002975 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002976 }
2977
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002978 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002979
2980#ifdef CONFIG_MMC_DEBUG
2981 sdhci_dumpregs(host);
2982#endif
2983
Pierre Ossmanf9134312008-12-21 17:01:48 +01002984#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002985 snprintf(host->led_name, sizeof(host->led_name),
2986 "%s::", mmc_hostname(mmc));
2987 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002988 host->led.brightness = LED_OFF;
2989 host->led.default_trigger = mmc_hostname(mmc);
2990 host->led.brightness_set = sdhci_led_control;
2991
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002992 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002993 if (ret)
2994 goto reset;
2995#endif
2996
Pierre Ossman5f25a662006-10-04 02:15:39 -07002997 mmiowb();
2998
Pierre Ossmand129bce2006-03-24 03:18:17 -08002999 mmc_add_host(mmc);
3000
Girish K Sa3c76eb2011-10-11 11:44:09 +05303001 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003002 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003003 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3004 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003005
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003006 sdhci_enable_card_detection(host);
3007
Pierre Ossmand129bce2006-03-24 03:18:17 -08003008 return 0;
3009
Pierre Ossmanf9134312008-12-21 17:01:48 +01003010#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003011reset:
3012 sdhci_reset(host, SDHCI_RESET_ALL);
3013 free_irq(host->irq, host);
3014#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003015untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003016 tasklet_kill(&host->card_tasklet);
3017 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003018
3019 return ret;
3020}
3021
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003022EXPORT_SYMBOL_GPL(sdhci_add_host);
3023
Pierre Ossman1e728592008-04-16 19:13:13 +02003024void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003025{
Pierre Ossman1e728592008-04-16 19:13:13 +02003026 unsigned long flags;
3027
3028 if (dead) {
3029 spin_lock_irqsave(&host->lock, flags);
3030
3031 host->flags |= SDHCI_DEVICE_DEAD;
3032
3033 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303034 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003035 " transfer!\n", mmc_hostname(host->mmc));
3036
3037 host->mrq->cmd->error = -ENOMEDIUM;
3038 tasklet_schedule(&host->finish_tasklet);
3039 }
3040
3041 spin_unlock_irqrestore(&host->lock, flags);
3042 }
3043
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003044 sdhci_disable_card_detection(host);
3045
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003046 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003047
Pierre Ossmanf9134312008-12-21 17:01:48 +01003048#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003049 led_classdev_unregister(&host->led);
3050#endif
3051
Pierre Ossman1e728592008-04-16 19:13:13 +02003052 if (!dead)
3053 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003054
3055 free_irq(host->irq, host);
3056
3057 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303058 if (host->version >= SDHCI_SPEC_300)
3059 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060
3061 tasklet_kill(&host->card_tasklet);
3062 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003063
Adrian Hunterceb61432011-12-27 15:48:41 +02003064 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003065 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003066
Pierre Ossman2134a922008-06-28 18:28:51 +02003067 kfree(host->adma_desc);
3068 kfree(host->align_buffer);
3069
3070 host->adma_desc = NULL;
3071 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003072}
3073
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003074EXPORT_SYMBOL_GPL(sdhci_remove_host);
3075
3076void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003077{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003078 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003079}
3080
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003081EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003082
3083/*****************************************************************************\
3084 * *
3085 * Driver init/exit *
3086 * *
3087\*****************************************************************************/
3088
3089static int __init sdhci_drv_init(void)
3090{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303091 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003092 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303093 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003094
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003095 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003096}
3097
3098static void __exit sdhci_drv_exit(void)
3099{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003100}
3101
3102module_init(sdhci_drv_init);
3103module_exit(sdhci_drv_exit);
3104
Pierre Ossmandf673b22006-06-30 02:22:31 -07003105module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003106module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003107
Pierre Ossman32710e82009-04-08 20:14:54 +02003108MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003109MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003110MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003111
Pierre Ossmandf673b22006-06-30 02:22:31 -07003112MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003113MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");