blob: e06299734f5dc1a51b85da5f6ff384fb389af61d [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053052static int sdhci_execute_tuning(struct mmc_host *mmc);
53static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Pierre Ossmand129bce2006-03-24 03:18:17 -080069static void sdhci_dumpregs(struct sdhci_host *host)
70{
Girish K Sa3c76eb2011-10-11 11:44:09 +053071 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070072 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073
Girish K Sa3c76eb2011-10-11 11:44:09 +053074 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_DMA_ADDRESS),
76 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053077 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_BLOCK_SIZE),
79 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053080 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_ARGUMENT),
82 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_PRESENT_STATE),
85 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readb(host, SDHCI_POWER_CONTROL),
88 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
91 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
94 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_INT_ENABLE),
97 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_ACMD12_ERR),
100 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500103 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530108 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800109
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 readl(host->ioaddr + SDHCI_ADMA_ERROR),
113 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
114
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
125{
126 u32 ier;
127
128 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
129 ier &= ~clear;
130 ier |= set;
131 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
133}
134
135static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
136{
137 sdhci_clear_set_irqs(host, 0, irqs);
138}
139
140static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
141{
142 sdhci_clear_set_irqs(host, irqs, 0);
143}
144
145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Shawn Guod25928d2011-06-21 22:41:48 +0800147 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Adrian Hunterc79396c2011-12-27 15:48:42 +0200149 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Adrian Hunterc79396c2011-12-27 15:48:42 +0200150 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300151 return;
152
Shawn Guod25928d2011-06-21 22:41:48 +0800153 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
154 SDHCI_CARD_PRESENT;
155 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
156
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300157 if (enable)
158 sdhci_unmask_irqs(host, irqs);
159 else
160 sdhci_mask_irqs(host, irqs);
161}
162
163static void sdhci_enable_card_detection(struct sdhci_host *host)
164{
165 sdhci_set_card_detection(host, true);
166}
167
168static void sdhci_disable_card_detection(struct sdhci_host *host)
169{
170 sdhci_set_card_detection(host, false);
171}
172
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173static void sdhci_reset(struct sdhci_host *host, u8 mask)
174{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700175 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300176 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-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 Ossmane16514d82006-06-30 02:22:24 -0700192 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 host->clock = 0;
194
Pierre Ossmane16514d82006-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 Ossmane16514d82006-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 Ossmane16514d82006-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-06-30 21:15:49 +0200569 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200570 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-06-30 21:15:49 +0200803 }
804 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300805 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200806
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300807 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-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 Ossman8f1934c2008-06-30 21:15:49 +0200843 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200844 int flags;
845
846 flags = SG_MITER_ATOMIC;
847 if (host->data->flags & MMC_DATA_READ)
848 flags |= SG_MITER_TO_SG;
849 else
850 flags |= SG_MITER_FROM_SG;
851 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200852 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800853 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700854
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300855 sdhci_set_transfer_irqs(host);
856
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400857 /* Set the DMA boundary value and block size */
858 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
859 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700861}
862
863static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500864 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700865{
866 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500867 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700868
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869 if (data == NULL)
870 return;
871
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200872 WARN_ON(!host->data);
873
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500875 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
876 mode |= SDHCI_TRNS_MULTI;
877 /*
878 * If we are sending CMD23, CMD12 never gets sent
879 * on successful completion (so no Auto-CMD12).
880 */
881 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
882 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500883 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
884 mode |= SDHCI_TRNS_AUTO_CMD23;
885 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
886 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700887 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500888
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 if (data->flags & MMC_DATA_READ)
890 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100891 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700892 mode |= SDHCI_TRNS_DMA;
893
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300894 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800895}
896
897static void sdhci_finish_data(struct sdhci_host *host)
898{
899 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900
901 BUG_ON(!host->data);
902
903 data = host->data;
904 host->data = NULL;
905
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100906 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200907 if (host->flags & SDHCI_USE_ADMA)
908 sdhci_adma_table_post(host, data);
909 else {
910 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
911 data->sg_len, (data->flags & MMC_DATA_READ) ?
912 DMA_FROM_DEVICE : DMA_TO_DEVICE);
913 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914 }
915
916 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200917 * The specification states that the block count register must
918 * be updated, but it does not specify at what point in the
919 * data flow. That makes the register entirely useless to read
920 * back so we have to assume that nothing made it to the card
921 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200923 if (data->error)
924 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200926 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927
Andrei Warkentine89d4562011-05-23 15:06:37 -0500928 /*
929 * Need to send CMD12 if -
930 * a) open-ended multiblock transfer (no CMD23)
931 * b) error in multiblock transfer
932 */
933 if (data->stop &&
934 (data->error ||
935 !host->mrq->sbc)) {
936
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937 /*
938 * The controller needs a reset of internal state machines
939 * upon error conditions.
940 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200941 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 sdhci_reset(host, SDHCI_RESET_CMD);
943 sdhci_reset(host, SDHCI_RESET_DATA);
944 }
945
946 sdhci_send_command(host, data->stop);
947 } else
948 tasklet_schedule(&host->finish_tasklet);
949}
950
951static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
952{
953 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700954 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700955 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956
957 WARN_ON(host->cmd);
958
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700960 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700961
962 mask = SDHCI_CMD_INHIBIT;
963 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
964 mask |= SDHCI_DATA_INHIBIT;
965
966 /* We shouldn't wait for data inihibit for stop commands, even
967 though they might use busy signaling */
968 if (host->mrq->data && (cmd == host->mrq->data->stop))
969 mask &= ~SDHCI_DATA_INHIBIT;
970
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300971 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700972 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530973 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100974 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200976 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977 tasklet_schedule(&host->finish_tasklet);
978 return;
979 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700980 timeout--;
981 mdelay(1);
982 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
984 mod_timer(&host->timer, jiffies + 10 * HZ);
985
986 host->cmd = cmd;
987
Andrei Warkentina3c77782011-04-11 16:13:42 -0500988 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300990 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991
Andrei Warkentine89d4562011-05-23 15:06:37 -0500992 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700993
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530995 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200997 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 tasklet_schedule(&host->finish_tasklet);
999 return;
1000 }
1001
1002 if (!(cmd->flags & MMC_RSP_PRESENT))
1003 flags = SDHCI_CMD_RESP_NONE;
1004 else if (cmd->flags & MMC_RSP_136)
1005 flags = SDHCI_CMD_RESP_LONG;
1006 else if (cmd->flags & MMC_RSP_BUSY)
1007 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1008 else
1009 flags = SDHCI_CMD_RESP_SHORT;
1010
1011 if (cmd->flags & MMC_RSP_CRC)
1012 flags |= SDHCI_CMD_CRC;
1013 if (cmd->flags & MMC_RSP_OPCODE)
1014 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301015
1016 /* CMD19 is special in that the Data Present Select should be set */
1017 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 flags |= SDHCI_CMD_DATA;
1019
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001020 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021}
1022
1023static void sdhci_finish_command(struct sdhci_host *host)
1024{
1025 int i;
1026
1027 BUG_ON(host->cmd == NULL);
1028
1029 if (host->cmd->flags & MMC_RSP_PRESENT) {
1030 if (host->cmd->flags & MMC_RSP_136) {
1031 /* CRC is stripped so we need to do some shifting. */
1032 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001033 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 SDHCI_RESPONSE + (3-i)*4) << 8;
1035 if (i != 3)
1036 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001037 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 SDHCI_RESPONSE + (3-i)*4-1);
1039 }
1040 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001041 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 }
1043 }
1044
Pierre Ossman17b04292007-07-22 22:18:46 +02001045 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
Andrei Warkentine89d4562011-05-23 15:06:37 -05001047 /* Finished CMD23, now send actual command. */
1048 if (host->cmd == host->mrq->sbc) {
1049 host->cmd = NULL;
1050 sdhci_send_command(host, host->mrq->cmd);
1051 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001052
Andrei Warkentine89d4562011-05-23 15:06:37 -05001053 /* Processed actual command. */
1054 if (host->data && host->data_early)
1055 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056
Andrei Warkentine89d4562011-05-23 15:06:37 -05001057 if (!host->cmd->data)
1058 tasklet_schedule(&host->finish_tasklet);
1059
1060 host->cmd = NULL;
1061 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062}
1063
1064static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1065{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301066 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001067 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301068 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001069 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070
Todd Poynor30832ab2011-12-27 15:48:46 +02001071 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072 return;
1073
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001074 host->mmc->actual_clock = 0;
1075
Anton Vorontsov81146342009-03-17 00:13:59 +03001076 if (host->ops->set_clock) {
1077 host->ops->set_clock(host, clock);
1078 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1079 return;
1080 }
1081
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001082 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083
1084 if (clock == 0)
1085 goto out;
1086
Zhangfei Gao85105c52010-08-06 07:10:01 +08001087 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301088 /*
1089 * Check if the Host Controller supports Programmable Clock
1090 * Mode.
1091 */
1092 if (host->clk_mul) {
1093 u16 ctrl;
1094
1095 /*
1096 * We need to figure out whether the Host Driver needs
1097 * to select Programmable Clock Mode, or the value can
1098 * be set automatically by the Host Controller based on
1099 * the Preset Value registers.
1100 */
1101 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1102 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1103 for (div = 1; div <= 1024; div++) {
1104 if (((host->max_clk * host->clk_mul) /
1105 div) <= clock)
1106 break;
1107 }
1108 /*
1109 * Set Programmable Clock Mode in the Clock
1110 * Control register.
1111 */
1112 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001113 real_div = div;
1114 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301115 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001116 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301117 } else {
1118 /* Version 3.00 divisors must be a multiple of 2. */
1119 if (host->max_clk <= clock)
1120 div = 1;
1121 else {
1122 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1123 div += 2) {
1124 if ((host->max_clk / div) <= clock)
1125 break;
1126 }
1127 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001128 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301129 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001130 }
1131 } else {
1132 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001133 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001134 if ((host->max_clk / div) <= clock)
1135 break;
1136 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001141 if (real_div)
1142 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1143
Arindam Nathc3ed3872011-05-05 12:19:06 +05301144 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001145 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1146 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001148 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149
Chris Ball27f6cb12009-09-22 16:45:31 -07001150 /* Wait max 20 ms */
1151 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001152 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001153 & SDHCI_CLOCK_INT_STABLE)) {
1154 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301155 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001156 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157 sdhci_dumpregs(host);
1158 return;
1159 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001160 timeout--;
1161 mdelay(1);
1162 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163
1164 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001165 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166
1167out:
1168 host->clock = clock;
1169}
1170
Adrian Hunterceb61432011-12-27 15:48:41 +02001171static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001172{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001173 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001174
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001175 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001176 switch (1 << power) {
1177 case MMC_VDD_165_195:
1178 pwr = SDHCI_POWER_180;
1179 break;
1180 case MMC_VDD_29_30:
1181 case MMC_VDD_30_31:
1182 pwr = SDHCI_POWER_300;
1183 break;
1184 case MMC_VDD_32_33:
1185 case MMC_VDD_33_34:
1186 pwr = SDHCI_POWER_330;
1187 break;
1188 default:
1189 BUG();
1190 }
1191 }
1192
1193 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001194 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001195
Pierre Ossmanae628902009-05-03 20:45:03 +02001196 host->pwr = pwr;
1197
1198 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001199 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001200 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001201 }
1202
1203 /*
1204 * Spec says that we should clear the power reg before setting
1205 * a new value. Some controllers don't seem to like this though.
1206 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001207 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001208 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001209
Andres Salomone08c1692008-07-04 10:00:03 -07001210 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001211 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001212 * and set turn on power at the same time, so set the voltage first.
1213 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001214 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001215 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1216
1217 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001218
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001219 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001220
1221 /*
1222 * Some controllers need an extra 10ms delay of 10ms before they
1223 * can apply clock after applying power
1224 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001225 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001226 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001227
1228 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001229}
1230
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231/*****************************************************************************\
1232 * *
1233 * MMC callbacks *
1234 * *
1235\*****************************************************************************/
1236
1237static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1238{
1239 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001240 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241 unsigned long flags;
1242
1243 host = mmc_priv(mmc);
1244
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001245 sdhci_runtime_pm_get(host);
1246
Pierre Ossmand129bce2006-03-24 03:18:17 -08001247 spin_lock_irqsave(&host->lock, flags);
1248
1249 WARN_ON(host->mrq != NULL);
1250
Pierre Ossmanf9134312008-12-21 17:01:48 +01001251#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001253#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001254
1255 /*
1256 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1257 * requests if Auto-CMD12 is enabled.
1258 */
1259 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001260 if (mrq->stop) {
1261 mrq->data->stop = NULL;
1262 mrq->stop = NULL;
1263 }
1264 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265
1266 host->mrq = mrq;
1267
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001268 /* If polling, assume that the card is always present. */
1269 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1270 present = true;
1271 else
1272 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1273 SDHCI_CARD_PRESENT;
1274
1275 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001276 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301278 } else {
1279 u32 present_state;
1280
1281 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1282 /*
1283 * Check if the re-tuning timer has already expired and there
1284 * is no on-going data transfer. If so, we need to execute
1285 * tuning procedure before sending command.
1286 */
1287 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1288 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1289 spin_unlock_irqrestore(&host->lock, flags);
1290 sdhci_execute_tuning(mmc);
1291 spin_lock_irqsave(&host->lock, flags);
1292
1293 /* Restore original mmc_request structure */
1294 host->mrq = mrq;
1295 }
1296
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001297 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001298 sdhci_send_command(host, mrq->sbc);
1299 else
1300 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301301 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001302
Pierre Ossman5f25a662006-10-04 02:15:39 -07001303 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304 spin_unlock_irqrestore(&host->lock, flags);
1305}
1306
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001307static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001310 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 u8 ctrl;
1312
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 spin_lock_irqsave(&host->lock, flags);
1314
Adrian Hunterceb61432011-12-27 15:48:41 +02001315 if (host->flags & SDHCI_DEVICE_DEAD) {
1316 spin_unlock_irqrestore(&host->lock, flags);
1317 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1318 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1319 return;
1320 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001321
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 /*
1323 * Reset the chip on each power off.
1324 * Should clear out any weird states.
1325 */
1326 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001327 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001328 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 }
1330
1331 sdhci_set_clock(host, ios->clock);
1332
1333 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001334 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001336 vdd_bit = sdhci_set_power(host, ios->vdd);
1337
1338 if (host->vmmc && vdd_bit != -1) {
1339 spin_unlock_irqrestore(&host->lock, flags);
1340 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1341 spin_lock_irqsave(&host->lock, flags);
1342 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343
Philip Rakity643a81f2010-09-23 08:24:32 -07001344 if (host->ops->platform_send_init_74_clocks)
1345 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1346
Philip Rakity15ec4462010-11-19 16:48:39 -05001347 /*
1348 * If your platform has 8-bit width support but is not a v3 controller,
1349 * or if it requires special setup code, you should implement that in
1350 * platform_8bit_width().
1351 */
1352 if (host->ops->platform_8bit_width)
1353 host->ops->platform_8bit_width(host, ios->bus_width);
1354 else {
1355 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1356 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1357 ctrl &= ~SDHCI_CTRL_4BITBUS;
1358 if (host->version >= SDHCI_SPEC_300)
1359 ctrl |= SDHCI_CTRL_8BITBUS;
1360 } else {
1361 if (host->version >= SDHCI_SPEC_300)
1362 ctrl &= ~SDHCI_CTRL_8BITBUS;
1363 if (ios->bus_width == MMC_BUS_WIDTH_4)
1364 ctrl |= SDHCI_CTRL_4BITBUS;
1365 else
1366 ctrl &= ~SDHCI_CTRL_4BITBUS;
1367 }
1368 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1369 }
1370
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001371 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001372
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001373 if ((ios->timing == MMC_TIMING_SD_HS ||
1374 ios->timing == MMC_TIMING_MMC_HS)
1375 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001376 ctrl |= SDHCI_CTRL_HISPD;
1377 else
1378 ctrl &= ~SDHCI_CTRL_HISPD;
1379
Arindam Nathd6d50a12011-05-05 12:18:59 +05301380 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301381 u16 clk, ctrl_2;
1382 unsigned int clock;
1383
1384 /* In case of UHS-I modes, set High Speed Enable */
1385 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1386 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1387 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1388 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1389 (ios->timing == MMC_TIMING_UHS_SDR12))
1390 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301391
1392 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1393 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301394 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301395 /*
1396 * We only need to set Driver Strength if the
1397 * preset value enable is not set.
1398 */
1399 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1400 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1401 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1402 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1403 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1404
1405 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301406 } else {
1407 /*
1408 * According to SDHC Spec v3.00, if the Preset Value
1409 * Enable in the Host Control 2 register is set, we
1410 * need to reset SD Clock Enable before changing High
1411 * Speed Enable to avoid generating clock gliches.
1412 */
Arindam Nath758535c2011-05-05 12:19:00 +05301413
1414 /* Reset SD Clock Enable */
1415 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1416 clk &= ~SDHCI_CLOCK_CARD_EN;
1417 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1418
1419 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1420
1421 /* Re-enable SD Clock */
1422 clock = host->clock;
1423 host->clock = 0;
1424 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301425 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301426
Arindam Nath49c468f2011-05-05 12:19:01 +05301427
1428 /* Reset SD Clock Enable */
1429 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1430 clk &= ~SDHCI_CLOCK_CARD_EN;
1431 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1432
Philip Rakity6322cdd2011-05-13 11:17:15 +05301433 if (host->ops->set_uhs_signaling)
1434 host->ops->set_uhs_signaling(host, ios->timing);
1435 else {
1436 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1437 /* Select Bus Speed Mode for host */
1438 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1439 if (ios->timing == MMC_TIMING_UHS_SDR12)
1440 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1441 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1442 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1443 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1444 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1445 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1446 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1447 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1448 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1449 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1450 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301451
1452 /* Re-enable SD Clock */
1453 clock = host->clock;
1454 host->clock = 0;
1455 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301456 } else
1457 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301458
Leandro Dorileob8352262007-07-25 23:47:04 +02001459 /*
1460 * Some (ENE) controllers go apeshit on some ios operation,
1461 * signalling timeout and CRC errors even on CMD0. Resetting
1462 * it on each ios seems to solve the problem.
1463 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001464 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001465 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1466
Pierre Ossman5f25a662006-10-04 02:15:39 -07001467 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001468 spin_unlock_irqrestore(&host->lock, flags);
1469}
1470
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001471static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1472{
1473 struct sdhci_host *host = mmc_priv(mmc);
1474
1475 sdhci_runtime_pm_get(host);
1476 sdhci_do_set_ios(host, ios);
1477 sdhci_runtime_pm_put(host);
1478}
1479
1480static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001483 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 spin_lock_irqsave(&host->lock, flags);
1486
Pierre Ossman1e728592008-04-16 19:13:13 +02001487 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001488 is_readonly = 0;
1489 else if (host->ops->get_ro)
1490 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001491 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001492 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1493 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494
1495 spin_unlock_irqrestore(&host->lock, flags);
1496
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001497 /* This quirk needs to be replaced by a callback-function later */
1498 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1499 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001500}
1501
Takashi Iwai82b0e232011-04-21 20:26:38 +02001502#define SAMPLE_COUNT 5
1503
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001504static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001505{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001506 int i, ro_count;
1507
Takashi Iwai82b0e232011-04-21 20:26:38 +02001508 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001509 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001510
1511 ro_count = 0;
1512 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001513 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001514 if (++ro_count > SAMPLE_COUNT / 2)
1515 return 1;
1516 }
1517 msleep(30);
1518 }
1519 return 0;
1520}
1521
Adrian Hunter20758b62011-08-29 16:42:12 +03001522static void sdhci_hw_reset(struct mmc_host *mmc)
1523{
1524 struct sdhci_host *host = mmc_priv(mmc);
1525
1526 if (host->ops && host->ops->hw_reset)
1527 host->ops->hw_reset(host);
1528}
1529
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001530static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001531{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001532 struct sdhci_host *host = mmc_priv(mmc);
1533 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001534
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001535 sdhci_runtime_pm_get(host);
1536 ret = sdhci_do_get_ro(host);
1537 sdhci_runtime_pm_put(host);
1538 return ret;
1539}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001540
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001541static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1542{
Pierre Ossman1e728592008-04-16 19:13:13 +02001543 if (host->flags & SDHCI_DEVICE_DEAD)
1544 goto out;
1545
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001546 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001547 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1548 else
1549 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1550
1551 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1552 if (host->runtime_suspended)
1553 goto out;
1554
1555 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001556 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1557 else
1558 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001559out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001560 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001561}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001562
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001563static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1564{
1565 struct sdhci_host *host = mmc_priv(mmc);
1566 unsigned long flags;
1567
1568 spin_lock_irqsave(&host->lock, flags);
1569 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001570 spin_unlock_irqrestore(&host->lock, flags);
1571}
1572
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001573static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1574 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301575{
Arindam Nathf2119df2011-05-05 12:18:57 +05301576 u8 pwr;
1577 u16 clk, ctrl;
1578 u32 present_state;
1579
Arindam Nathf2119df2011-05-05 12:18:57 +05301580 /*
1581 * Signal Voltage Switching is only applicable for Host Controllers
1582 * v3.00 and above.
1583 */
1584 if (host->version < SDHCI_SPEC_300)
1585 return 0;
1586
1587 /*
1588 * We first check whether the request is to set signalling voltage
1589 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1590 */
1591 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1592 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1593 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1594 ctrl &= ~SDHCI_CTRL_VDD_180;
1595 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1596
1597 /* Wait for 5ms */
1598 usleep_range(5000, 5500);
1599
1600 /* 3.3V regulator output should be stable within 5 ms */
1601 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1602 if (!(ctrl & SDHCI_CTRL_VDD_180))
1603 return 0;
1604 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301605 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301606 "signalling voltage failed\n");
1607 return -EIO;
1608 }
1609 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1610 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1611 /* Stop SDCLK */
1612 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1613 clk &= ~SDHCI_CLOCK_CARD_EN;
1614 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1615
1616 /* Check whether DAT[3:0] is 0000 */
1617 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1618 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1619 SDHCI_DATA_LVL_SHIFT)) {
1620 /*
1621 * Enable 1.8V Signal Enable in the Host Control2
1622 * register
1623 */
1624 ctrl |= SDHCI_CTRL_VDD_180;
1625 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1626
1627 /* Wait for 5ms */
1628 usleep_range(5000, 5500);
1629
1630 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1631 if (ctrl & SDHCI_CTRL_VDD_180) {
1632 /* Provide SDCLK again and wait for 1ms*/
1633 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1634 clk |= SDHCI_CLOCK_CARD_EN;
1635 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1636 usleep_range(1000, 1500);
1637
1638 /*
1639 * If DAT[3:0] level is 1111b, then the card
1640 * was successfully switched to 1.8V signaling.
1641 */
1642 present_state = sdhci_readl(host,
1643 SDHCI_PRESENT_STATE);
1644 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1645 SDHCI_DATA_LVL_MASK)
1646 return 0;
1647 }
1648 }
1649
1650 /*
1651 * If we are here, that means the switch to 1.8V signaling
1652 * failed. We power cycle the card, and retry initialization
1653 * sequence by setting S18R to 0.
1654 */
1655 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1656 pwr &= ~SDHCI_POWER_ON;
1657 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1658
1659 /* Wait for 1ms as per the spec */
1660 usleep_range(1000, 1500);
1661 pwr |= SDHCI_POWER_ON;
1662 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1663
Girish K Sa3c76eb2011-10-11 11:44:09 +05301664 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301665 "voltage failed, retrying with S18R set to 0\n");
1666 return -EAGAIN;
1667 } else
1668 /* No signal voltage switch required */
1669 return 0;
1670}
1671
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1673 struct mmc_ios *ios)
1674{
1675 struct sdhci_host *host = mmc_priv(mmc);
1676 int err;
1677
1678 if (host->version < SDHCI_SPEC_300)
1679 return 0;
1680 sdhci_runtime_pm_get(host);
1681 err = sdhci_do_start_signal_voltage_switch(host, ios);
1682 sdhci_runtime_pm_put(host);
1683 return err;
1684}
1685
Arindam Nathb513ea22011-05-05 12:19:04 +05301686static int sdhci_execute_tuning(struct mmc_host *mmc)
1687{
1688 struct sdhci_host *host;
1689 u16 ctrl;
1690 u32 ier;
1691 int tuning_loop_counter = MAX_TUNING_LOOP;
1692 unsigned long timeout;
1693 int err = 0;
1694
1695 host = mmc_priv(mmc);
1696
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001697 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301698 disable_irq(host->irq);
1699 spin_lock(&host->lock);
1700
1701 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1702
1703 /*
1704 * Host Controller needs tuning only in case of SDR104 mode
1705 * and for SDR50 mode when Use Tuning for SDR50 is set in
1706 * Capabilities register.
1707 */
1708 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1709 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1710 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1711 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1712 else {
1713 spin_unlock(&host->lock);
1714 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001715 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301716 return 0;
1717 }
1718
1719 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1720
1721 /*
1722 * As per the Host Controller spec v3.00, tuning command
1723 * generates Buffer Read Ready interrupt, so enable that.
1724 *
1725 * Note: The spec clearly says that when tuning sequence
1726 * is being performed, the controller does not generate
1727 * interrupts other than Buffer Read Ready interrupt. But
1728 * to make sure we don't hit a controller bug, we _only_
1729 * enable Buffer Read Ready interrupt here.
1730 */
1731 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1732 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1733
1734 /*
1735 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1736 * of loops reaches 40 times or a timeout of 150ms occurs.
1737 */
1738 timeout = 150;
1739 do {
1740 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001741 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301742
1743 if (!tuning_loop_counter && !timeout)
1744 break;
1745
1746 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1747 cmd.arg = 0;
1748 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1749 cmd.retries = 0;
1750 cmd.data = NULL;
1751 cmd.error = 0;
1752
1753 mrq.cmd = &cmd;
1754 host->mrq = &mrq;
1755
1756 /*
1757 * In response to CMD19, the card sends 64 bytes of tuning
1758 * block to the Host Controller. So we set the block size
1759 * to 64 here.
1760 */
1761 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1762
1763 /*
1764 * The tuning block is sent by the card to the host controller.
1765 * So we set the TRNS_READ bit in the Transfer Mode register.
1766 * This also takes care of setting DMA Enable and Multi Block
1767 * Select in the same register to 0.
1768 */
1769 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1770
1771 sdhci_send_command(host, &cmd);
1772
1773 host->cmd = NULL;
1774 host->mrq = NULL;
1775
1776 spin_unlock(&host->lock);
1777 enable_irq(host->irq);
1778
1779 /* Wait for Buffer Read Ready interrupt */
1780 wait_event_interruptible_timeout(host->buf_ready_int,
1781 (host->tuning_done == 1),
1782 msecs_to_jiffies(50));
1783 disable_irq(host->irq);
1784 spin_lock(&host->lock);
1785
1786 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301787 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301788 "Buffer Read Ready interrupt during tuning "
1789 "procedure, falling back to fixed sampling "
1790 "clock\n");
1791 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1792 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1793 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1794 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1795
1796 err = -EIO;
1797 goto out;
1798 }
1799
1800 host->tuning_done = 0;
1801
1802 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1803 tuning_loop_counter--;
1804 timeout--;
1805 mdelay(1);
1806 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1807
1808 /*
1809 * The Host Driver has exhausted the maximum number of loops allowed,
1810 * so use fixed sampling frequency.
1811 */
1812 if (!tuning_loop_counter || !timeout) {
1813 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1814 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1815 } else {
1816 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301817 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301818 " failed, falling back to fixed sampling"
1819 " clock\n");
1820 err = -EIO;
1821 }
1822 }
1823
1824out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301825 /*
1826 * If this is the very first time we are here, we start the retuning
1827 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1828 * flag won't be set, we check this condition before actually starting
1829 * the timer.
1830 */
1831 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1832 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1833 mod_timer(&host->tuning_timer, jiffies +
1834 host->tuning_count * HZ);
1835 /* Tuning mode 1 limits the maximum data length to 4MB */
1836 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1837 } else {
1838 host->flags &= ~SDHCI_NEEDS_RETUNING;
1839 /* Reload the new initial value for timer */
1840 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1841 mod_timer(&host->tuning_timer, jiffies +
1842 host->tuning_count * HZ);
1843 }
1844
1845 /*
1846 * In case tuning fails, host controllers which support re-tuning can
1847 * try tuning again at a later time, when the re-tuning timer expires.
1848 * So for these controllers, we return 0. Since there might be other
1849 * controllers who do not have this capability, we return error for
1850 * them.
1851 */
1852 if (err && host->tuning_count &&
1853 host->tuning_mode == SDHCI_TUNING_MODE_1)
1854 err = 0;
1855
Arindam Nathb513ea22011-05-05 12:19:04 +05301856 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1857 spin_unlock(&host->lock);
1858 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001859 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301860
1861 return err;
1862}
1863
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001864static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301865{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301866 u16 ctrl;
1867 unsigned long flags;
1868
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301869 /* Host Controller v3.00 defines preset value registers */
1870 if (host->version < SDHCI_SPEC_300)
1871 return;
1872
1873 spin_lock_irqsave(&host->lock, flags);
1874
1875 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1876
1877 /*
1878 * We only enable or disable Preset Value if they are not already
1879 * enabled or disabled respectively. Otherwise, we bail out.
1880 */
1881 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1882 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1883 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001884 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301885 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1886 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1887 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001888 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301889 }
1890
1891 spin_unlock_irqrestore(&host->lock, flags);
1892}
1893
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001894static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1895{
1896 struct sdhci_host *host = mmc_priv(mmc);
1897
1898 sdhci_runtime_pm_get(host);
1899 sdhci_do_enable_preset_value(host, enable);
1900 sdhci_runtime_pm_put(host);
1901}
1902
David Brownellab7aefd2006-11-12 17:55:30 -08001903static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001904 .request = sdhci_request,
1905 .set_ios = sdhci_set_ios,
1906 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001907 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001908 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301909 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301910 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301911 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001912};
1913
1914/*****************************************************************************\
1915 * *
1916 * Tasklets *
1917 * *
1918\*****************************************************************************/
1919
1920static void sdhci_tasklet_card(unsigned long param)
1921{
1922 struct sdhci_host *host;
1923 unsigned long flags;
1924
1925 host = (struct sdhci_host*)param;
1926
1927 spin_lock_irqsave(&host->lock, flags);
1928
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001929 /* Check host->mrq first in case we are runtime suspended */
1930 if (host->mrq &&
1931 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301932 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001933 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301934 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001935 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001936
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001937 sdhci_reset(host, SDHCI_RESET_CMD);
1938 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001939
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001940 host->mrq->cmd->error = -ENOMEDIUM;
1941 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001942 }
1943
1944 spin_unlock_irqrestore(&host->lock, flags);
1945
Pierre Ossman04cf5852008-08-18 22:18:14 +02001946 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001947}
1948
1949static void sdhci_tasklet_finish(unsigned long param)
1950{
1951 struct sdhci_host *host;
1952 unsigned long flags;
1953 struct mmc_request *mrq;
1954
1955 host = (struct sdhci_host*)param;
1956
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001957 spin_lock_irqsave(&host->lock, flags);
1958
Chris Ball0c9c99a2011-04-27 17:35:31 -04001959 /*
1960 * If this tasklet gets rescheduled while running, it will
1961 * be run again afterwards but without any active request.
1962 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001963 if (!host->mrq) {
1964 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001965 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001966 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967
1968 del_timer(&host->timer);
1969
1970 mrq = host->mrq;
1971
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972 /*
1973 * The controller needs a reset of internal state machines
1974 * upon error conditions.
1975 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001976 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001977 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001978 (mrq->data && (mrq->data->error ||
1979 (mrq->data->stop && mrq->data->stop->error))) ||
1980 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001981
1982 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001983 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001984 unsigned int clock;
1985
1986 /* This is to force an update */
1987 clock = host->clock;
1988 host->clock = 0;
1989 sdhci_set_clock(host, clock);
1990 }
1991
1992 /* Spec says we should do both at the same time, but Ricoh
1993 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001994 sdhci_reset(host, SDHCI_RESET_CMD);
1995 sdhci_reset(host, SDHCI_RESET_DATA);
1996 }
1997
1998 host->mrq = NULL;
1999 host->cmd = NULL;
2000 host->data = NULL;
2001
Pierre Ossmanf9134312008-12-21 17:01:48 +01002002#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002003 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002004#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002005
Pierre Ossman5f25a662006-10-04 02:15:39 -07002006 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002007 spin_unlock_irqrestore(&host->lock, flags);
2008
2009 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002010 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002011}
2012
2013static void sdhci_timeout_timer(unsigned long data)
2014{
2015 struct sdhci_host *host;
2016 unsigned long flags;
2017
2018 host = (struct sdhci_host*)data;
2019
2020 spin_lock_irqsave(&host->lock, flags);
2021
2022 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302023 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002024 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002025 sdhci_dumpregs(host);
2026
2027 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002028 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002029 sdhci_finish_data(host);
2030 } else {
2031 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002032 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002033 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002034 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002035
2036 tasklet_schedule(&host->finish_tasklet);
2037 }
2038 }
2039
Pierre Ossman5f25a662006-10-04 02:15:39 -07002040 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002041 spin_unlock_irqrestore(&host->lock, flags);
2042}
2043
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302044static void sdhci_tuning_timer(unsigned long data)
2045{
2046 struct sdhci_host *host;
2047 unsigned long flags;
2048
2049 host = (struct sdhci_host *)data;
2050
2051 spin_lock_irqsave(&host->lock, flags);
2052
2053 host->flags |= SDHCI_NEEDS_RETUNING;
2054
2055 spin_unlock_irqrestore(&host->lock, flags);
2056}
2057
Pierre Ossmand129bce2006-03-24 03:18:17 -08002058/*****************************************************************************\
2059 * *
2060 * Interrupt handling *
2061 * *
2062\*****************************************************************************/
2063
2064static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2065{
2066 BUG_ON(intmask == 0);
2067
2068 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302069 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002070 "though no command operation was in progress.\n",
2071 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002072 sdhci_dumpregs(host);
2073 return;
2074 }
2075
Pierre Ossman43b58b32007-07-25 23:15:27 +02002076 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002077 host->cmd->error = -ETIMEDOUT;
2078 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2079 SDHCI_INT_INDEX))
2080 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002081
Pierre Ossmane8095172008-07-25 01:09:08 +02002082 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002084 return;
2085 }
2086
2087 /*
2088 * The host can send and interrupt when the busy state has
2089 * ended, allowing us to wait without wasting CPU cycles.
2090 * Unfortunately this is overloaded on the "data complete"
2091 * interrupt, so we need to take some care when handling
2092 * it.
2093 *
2094 * Note: The 1.0 specification is a bit ambiguous about this
2095 * feature so there might be some problems with older
2096 * controllers.
2097 */
2098 if (host->cmd->flags & MMC_RSP_BUSY) {
2099 if (host->cmd->data)
2100 DBG("Cannot wait for busy signal when also "
2101 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002102 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002103 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002104
2105 /* The controller does not support the end-of-busy IRQ,
2106 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002107 }
2108
2109 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002110 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002111}
2112
George G. Davis0957c332010-02-18 12:32:12 -05002113#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002114static void sdhci_show_adma_error(struct sdhci_host *host)
2115{
2116 const char *name = mmc_hostname(host->mmc);
2117 u8 *desc = host->adma_desc;
2118 __le32 *dma;
2119 __le16 *len;
2120 u8 attr;
2121
2122 sdhci_dumpregs(host);
2123
2124 while (true) {
2125 dma = (__le32 *)(desc + 4);
2126 len = (__le16 *)(desc + 2);
2127 attr = *desc;
2128
2129 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2130 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2131
2132 desc += 8;
2133
2134 if (attr & 2)
2135 break;
2136 }
2137}
2138#else
2139static void sdhci_show_adma_error(struct sdhci_host *host) { }
2140#endif
2141
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2143{
2144 BUG_ON(intmask == 0);
2145
Arindam Nathb513ea22011-05-05 12:19:04 +05302146 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2147 if (intmask & SDHCI_INT_DATA_AVAIL) {
2148 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2149 MMC_SEND_TUNING_BLOCK) {
2150 host->tuning_done = 1;
2151 wake_up(&host->buf_ready_int);
2152 return;
2153 }
2154 }
2155
Pierre Ossmand129bce2006-03-24 03:18:17 -08002156 if (!host->data) {
2157 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002158 * The "data complete" interrupt is also used to
2159 * indicate that a busy state has ended. See comment
2160 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002161 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002162 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2163 if (intmask & SDHCI_INT_DATA_END) {
2164 sdhci_finish_command(host);
2165 return;
2166 }
2167 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002168
Girish K Sa3c76eb2011-10-11 11:44:09 +05302169 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002170 "though no data operation was in progress.\n",
2171 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002172 sdhci_dumpregs(host);
2173
2174 return;
2175 }
2176
2177 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002178 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002179 else if (intmask & SDHCI_INT_DATA_END_BIT)
2180 host->data->error = -EILSEQ;
2181 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2182 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2183 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002184 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002185 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302186 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002187 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002188 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002189 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190
Pierre Ossman17b04292007-07-22 22:18:46 +02002191 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002192 sdhci_finish_data(host);
2193 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002194 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002195 sdhci_transfer_pio(host);
2196
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002197 /*
2198 * We currently don't do anything fancy with DMA
2199 * boundaries, but as we can't disable the feature
2200 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002201 *
2202 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2203 * should return a valid address to continue from, but as
2204 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002205 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002206 if (intmask & SDHCI_INT_DMA_END) {
2207 u32 dmastart, dmanow;
2208 dmastart = sg_dma_address(host->data->sg);
2209 dmanow = dmastart + host->data->bytes_xfered;
2210 /*
2211 * Force update to the next DMA block boundary.
2212 */
2213 dmanow = (dmanow &
2214 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2215 SDHCI_DEFAULT_BOUNDARY_SIZE;
2216 host->data->bytes_xfered = dmanow - dmastart;
2217 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2218 " next 0x%08x\n",
2219 mmc_hostname(host->mmc), dmastart,
2220 host->data->bytes_xfered, dmanow);
2221 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2222 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002223
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002224 if (intmask & SDHCI_INT_DATA_END) {
2225 if (host->cmd) {
2226 /*
2227 * Data managed to finish before the
2228 * command completed. Make sure we do
2229 * things in the proper order.
2230 */
2231 host->data_early = 1;
2232 } else {
2233 sdhci_finish_data(host);
2234 }
2235 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002236 }
2237}
2238
David Howells7d12e782006-10-05 14:55:46 +01002239static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002240{
2241 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002242 struct sdhci_host *host = dev_id;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002243 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002244 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245
2246 spin_lock(&host->lock);
2247
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002248 if (host->runtime_suspended) {
2249 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302250 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002251 mmc_hostname(host->mmc));
2252 return IRQ_HANDLED;
2253 }
2254
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002255 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256
Mark Lord62df67a52007-03-06 13:30:13 +01002257 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258 result = IRQ_NONE;
2259 goto out;
2260 }
2261
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002262 DBG("*** %s got interrupt: 0x%08x\n",
2263 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264
Pierre Ossman3192a282006-06-30 02:22:26 -07002265 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002266 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2267 SDHCI_CARD_PRESENT;
2268
2269 /*
2270 * There is a observation on i.mx esdhc. INSERT bit will be
2271 * immediately set again when it gets cleared, if a card is
2272 * inserted. We have to mask the irq to prevent interrupt
2273 * storm which will freeze the system. And the REMOVE gets
2274 * the same situation.
2275 *
2276 * More testing are needed here to ensure it works for other
2277 * platforms though.
2278 */
2279 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2280 SDHCI_INT_CARD_REMOVE);
2281 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2282 SDHCI_INT_CARD_INSERT);
2283
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002284 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002285 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2286 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002288 }
2289
Pierre Ossmand129bce2006-03-24 03:18:17 -08002290 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002291 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2292 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002293 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 }
2295
2296 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002297 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2298 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002299 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002300 }
2301
2302 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2303
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002304 intmask &= ~SDHCI_INT_ERROR;
2305
Pierre Ossmand129bce2006-03-24 03:18:17 -08002306 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302307 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002308 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002309 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310 }
2311
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002312 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002313
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002314 if (intmask & SDHCI_INT_CARD_INT)
2315 cardint = 1;
2316
2317 intmask &= ~SDHCI_INT_CARD_INT;
2318
Pierre Ossman3192a282006-06-30 02:22:26 -07002319 if (intmask) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302320 pr_err("%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002321 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 sdhci_dumpregs(host);
2323
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002324 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002325 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326
2327 result = IRQ_HANDLED;
2328
Pierre Ossman5f25a662006-10-04 02:15:39 -07002329 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330out:
2331 spin_unlock(&host->lock);
2332
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002333 /*
2334 * We have to delay this as it calls back into the driver.
2335 */
2336 if (cardint)
2337 mmc_signal_sdio_irq(host->mmc);
2338
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 return result;
2340}
2341
2342/*****************************************************************************\
2343 * *
2344 * Suspend/resume *
2345 * *
2346\*****************************************************************************/
2347
2348#ifdef CONFIG_PM
2349
Manuel Lauss29495aa2011-11-03 11:09:45 +01002350int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002352 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002353
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002354 sdhci_disable_card_detection(host);
2355
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302356 /* Disable tuning since we are suspending */
2357 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2358 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2359 host->flags &= ~SDHCI_NEEDS_RETUNING;
2360 mod_timer(&host->tuning_timer, jiffies +
2361 host->tuning_count * HZ);
2362 }
2363
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002364 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002365 if (ret)
2366 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002368 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002370 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002371}
2372
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002373EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002374
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002375int sdhci_resume_host(struct sdhci_host *host)
2376{
2377 int ret;
2378
Richard Röjforsa13abc72009-09-22 16:45:30 -07002379 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002380 if (host->ops->enable_dma)
2381 host->ops->enable_dma(host);
2382 }
2383
2384 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2385 mmc_hostname(host->mmc), host);
2386 if (ret)
2387 return ret;
2388
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002389 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002390 mmiowb();
2391
2392 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002393 sdhci_enable_card_detection(host);
2394
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302395 /* Set the re-tuning expiration flag */
2396 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2397 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2398 host->flags |= SDHCI_NEEDS_RETUNING;
2399
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002400 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002401}
2402
2403EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404
Daniel Drake5f619702010-11-04 22:20:39 +00002405void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2406{
2407 u8 val;
2408 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2409 val |= SDHCI_WAKE_ON_INT;
2410 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2411}
2412
2413EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2414
Pierre Ossmand129bce2006-03-24 03:18:17 -08002415#endif /* CONFIG_PM */
2416
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002417#ifdef CONFIG_PM_RUNTIME
2418
2419static int sdhci_runtime_pm_get(struct sdhci_host *host)
2420{
2421 return pm_runtime_get_sync(host->mmc->parent);
2422}
2423
2424static int sdhci_runtime_pm_put(struct sdhci_host *host)
2425{
2426 pm_runtime_mark_last_busy(host->mmc->parent);
2427 return pm_runtime_put_autosuspend(host->mmc->parent);
2428}
2429
2430int sdhci_runtime_suspend_host(struct sdhci_host *host)
2431{
2432 unsigned long flags;
2433 int ret = 0;
2434
2435 /* Disable tuning since we are suspending */
2436 if (host->version >= SDHCI_SPEC_300 &&
2437 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2438 del_timer_sync(&host->tuning_timer);
2439 host->flags &= ~SDHCI_NEEDS_RETUNING;
2440 }
2441
2442 spin_lock_irqsave(&host->lock, flags);
2443 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2444 spin_unlock_irqrestore(&host->lock, flags);
2445
2446 synchronize_irq(host->irq);
2447
2448 spin_lock_irqsave(&host->lock, flags);
2449 host->runtime_suspended = true;
2450 spin_unlock_irqrestore(&host->lock, flags);
2451
2452 return ret;
2453}
2454EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2455
2456int sdhci_runtime_resume_host(struct sdhci_host *host)
2457{
2458 unsigned long flags;
2459 int ret = 0, host_flags = host->flags;
2460
2461 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2462 if (host->ops->enable_dma)
2463 host->ops->enable_dma(host);
2464 }
2465
2466 sdhci_init(host, 0);
2467
2468 /* Force clock and power re-program */
2469 host->pwr = 0;
2470 host->clock = 0;
2471 sdhci_do_set_ios(host, &host->mmc->ios);
2472
2473 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2474 if (host_flags & SDHCI_PV_ENABLED)
2475 sdhci_do_enable_preset_value(host, true);
2476
2477 /* Set the re-tuning expiration flag */
2478 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2479 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2480 host->flags |= SDHCI_NEEDS_RETUNING;
2481
2482 spin_lock_irqsave(&host->lock, flags);
2483
2484 host->runtime_suspended = false;
2485
2486 /* Enable SDIO IRQ */
2487 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2488 sdhci_enable_sdio_irq_nolock(host, true);
2489
2490 /* Enable Card Detection */
2491 sdhci_enable_card_detection(host);
2492
2493 spin_unlock_irqrestore(&host->lock, flags);
2494
2495 return ret;
2496}
2497EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2498
2499#endif
2500
Pierre Ossmand129bce2006-03-24 03:18:17 -08002501/*****************************************************************************\
2502 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002503 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002504 * *
2505\*****************************************************************************/
2506
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002507struct sdhci_host *sdhci_alloc_host(struct device *dev,
2508 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002510 struct mmc_host *mmc;
2511 struct sdhci_host *host;
2512
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002513 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002515 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002516 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002517 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518
2519 host = mmc_priv(mmc);
2520 host->mmc = mmc;
2521
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002522 return host;
2523}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002524
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002525EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002527int sdhci_add_host(struct sdhci_host *host)
2528{
2529 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302530 u32 caps[2];
2531 u32 max_current_caps;
2532 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002533 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002534
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002535 WARN_ON(host == NULL);
2536 if (host == NULL)
2537 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002538
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002539 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002540
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002541 if (debug_quirks)
2542 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002543 if (debug_quirks2)
2544 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002545
Pierre Ossmand96649e2006-06-30 02:22:30 -07002546 sdhci_reset(host, SDHCI_RESET_ALL);
2547
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002548 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002549 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2550 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002551 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302552 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002553 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002554 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002555 }
2556
Arindam Nathf2119df2011-05-05 12:18:57 +05302557 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002558 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002559
Arindam Nathf2119df2011-05-05 12:18:57 +05302560 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2561 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2562
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002563 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002564 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302565 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002566 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002567 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002568 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002569
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002570 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002571 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002572 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002573 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002574 }
2575
Arindam Nathf2119df2011-05-05 12:18:57 +05302576 if ((host->version >= SDHCI_SPEC_200) &&
2577 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002578 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002579
2580 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2581 (host->flags & SDHCI_USE_ADMA)) {
2582 DBG("Disabling ADMA as it is marked broken\n");
2583 host->flags &= ~SDHCI_USE_ADMA;
2584 }
2585
Richard Röjforsa13abc72009-09-22 16:45:30 -07002586 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002587 if (host->ops->enable_dma) {
2588 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302589 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002590 "available. Falling back to PIO.\n",
2591 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002592 host->flags &=
2593 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002594 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002595 }
2596 }
2597
Pierre Ossman2134a922008-06-28 18:28:51 +02002598 if (host->flags & SDHCI_USE_ADMA) {
2599 /*
2600 * We need to allocate descriptors for all sg entries
2601 * (128) and potentially one alignment transfer for
2602 * each of those entries.
2603 */
2604 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2605 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2606 if (!host->adma_desc || !host->align_buffer) {
2607 kfree(host->adma_desc);
2608 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302609 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002610 "buffers. Falling back to standard DMA.\n",
2611 mmc_hostname(mmc));
2612 host->flags &= ~SDHCI_USE_ADMA;
2613 }
2614 }
2615
Pierre Ossman76591502008-07-21 00:32:11 +02002616 /*
2617 * If we use DMA, then it's up to the caller to set the DMA
2618 * mask, but PIO does not need the hw shim so we set a new
2619 * mask here in that case.
2620 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002621 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002622 host->dma_mask = DMA_BIT_MASK(64);
2623 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2624 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002626 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302627 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002628 >> SDHCI_CLOCK_BASE_SHIFT;
2629 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302630 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002631 >> SDHCI_CLOCK_BASE_SHIFT;
2632
Pierre Ossmand129bce2006-03-24 03:18:17 -08002633 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002634 if (host->max_clk == 0 || host->quirks &
2635 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002636 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302637 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002638 "frequency.\n", mmc_hostname(mmc));
2639 return -ENODEV;
2640 }
2641 host->max_clk = host->ops->get_max_clock(host);
2642 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002643
2644 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302645 * In case of Host Controller v3.00, find out whether clock
2646 * multiplier is supported.
2647 */
2648 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2649 SDHCI_CLOCK_MUL_SHIFT;
2650
2651 /*
2652 * In case the value in Clock Multiplier is 0, then programmable
2653 * clock mode is not supported, otherwise the actual clock
2654 * multiplier is one more than the value of Clock Multiplier
2655 * in the Capabilities Register.
2656 */
2657 if (host->clk_mul)
2658 host->clk_mul += 1;
2659
2660 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661 * Set host parameters.
2662 */
2663 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302664 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002665 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002666 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302667 else if (host->version >= SDHCI_SPEC_300) {
2668 if (host->clk_mul) {
2669 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2670 mmc->f_max = host->max_clk * host->clk_mul;
2671 } else
2672 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2673 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002674 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002675
Andy Shevchenko272308c2011-08-03 18:36:00 +03002676 host->timeout_clk =
2677 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2678 if (host->timeout_clk == 0) {
2679 if (host->ops->get_timeout_clock) {
2680 host->timeout_clk = host->ops->get_timeout_clock(host);
2681 } else if (!(host->quirks &
2682 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302683 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002684 "frequency.\n", mmc_hostname(mmc));
2685 return -ENODEV;
2686 }
2687 }
2688 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2689 host->timeout_clk *= 1000;
2690
2691 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002692 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002693
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002694 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002695
Andrei Warkentine89d4562011-05-23 15:06:37 -05002696 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2697
2698 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2699 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002700
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002701 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002702 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002703 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002704 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002705 host->flags |= SDHCI_AUTO_CMD23;
2706 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2707 } else {
2708 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2709 }
2710
Philip Rakity15ec4462010-11-19 16:48:39 -05002711 /*
2712 * A controller may support 8-bit width, but the board itself
2713 * might not have the pins brought out. Boards that support
2714 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2715 * their platform code before calling sdhci_add_host(), and we
2716 * won't assume 8-bit width for hosts without that CAP.
2717 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002718 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002719 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002720
Arindam Nathf2119df2011-05-05 12:18:57 +05302721 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002722 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002723
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002724 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2725 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002726 mmc->caps |= MMC_CAP_NEEDS_POLL;
2727
Arindam Nathf2119df2011-05-05 12:18:57 +05302728 /* UHS-I mode(s) supported by the host controller. */
2729 if (host->version >= SDHCI_SPEC_300)
2730 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2731
2732 /* SDR104 supports also implies SDR50 support */
2733 if (caps[1] & SDHCI_SUPPORT_SDR104)
2734 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2735 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2736 mmc->caps |= MMC_CAP_UHS_SDR50;
2737
2738 if (caps[1] & SDHCI_SUPPORT_DDR50)
2739 mmc->caps |= MMC_CAP_UHS_DDR50;
2740
Arindam Nathb513ea22011-05-05 12:19:04 +05302741 /* Does the host needs tuning for SDR50? */
2742 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2743 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2744
Arindam Nathd6d50a12011-05-05 12:18:59 +05302745 /* Driver Type(s) (A, C, D) supported by the host */
2746 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2747 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2748 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2749 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2750 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2751 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2752
Girish K Sbec87262011-10-13 12:04:16 +05302753 /*
2754 * If Power Off Notify capability is enabled by the host,
2755 * set notify to short power off notify timeout value.
2756 */
2757 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2758 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2759 else
2760 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2761
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302762 /* Initial value for re-tuning timer count */
2763 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2764 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2765
2766 /*
2767 * In case Re-tuning Timer is not disabled, the actual value of
2768 * re-tuning timer will be 2 ^ (n - 1).
2769 */
2770 if (host->tuning_count)
2771 host->tuning_count = 1 << (host->tuning_count - 1);
2772
2773 /* Re-tuning mode supported by the Host Controller */
2774 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2775 SDHCI_RETUNING_MODE_SHIFT;
2776
Takashi Iwai8f230f42010-12-08 10:04:30 +01002777 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302778 /*
2779 * According to SD Host Controller spec v3.00, if the Host System
2780 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2781 * the value is meaningful only if Voltage Support in the Capabilities
2782 * register is set. The actual current value is 4 times the register
2783 * value.
2784 */
2785 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2786
2787 if (caps[0] & SDHCI_CAN_VDD_330) {
2788 int max_current_330;
2789
Takashi Iwai8f230f42010-12-08 10:04:30 +01002790 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302791
2792 max_current_330 = ((max_current_caps &
2793 SDHCI_MAX_CURRENT_330_MASK) >>
2794 SDHCI_MAX_CURRENT_330_SHIFT) *
2795 SDHCI_MAX_CURRENT_MULTIPLIER;
2796
2797 if (max_current_330 > 150)
2798 mmc->caps |= MMC_CAP_SET_XPC_330;
2799 }
2800 if (caps[0] & SDHCI_CAN_VDD_300) {
2801 int max_current_300;
2802
Takashi Iwai8f230f42010-12-08 10:04:30 +01002803 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302804
2805 max_current_300 = ((max_current_caps &
2806 SDHCI_MAX_CURRENT_300_MASK) >>
2807 SDHCI_MAX_CURRENT_300_SHIFT) *
2808 SDHCI_MAX_CURRENT_MULTIPLIER;
2809
2810 if (max_current_300 > 150)
2811 mmc->caps |= MMC_CAP_SET_XPC_300;
2812 }
2813 if (caps[0] & SDHCI_CAN_VDD_180) {
2814 int max_current_180;
2815
Takashi Iwai8f230f42010-12-08 10:04:30 +01002816 ocr_avail |= MMC_VDD_165_195;
2817
Arindam Nathf2119df2011-05-05 12:18:57 +05302818 max_current_180 = ((max_current_caps &
2819 SDHCI_MAX_CURRENT_180_MASK) >>
2820 SDHCI_MAX_CURRENT_180_SHIFT) *
2821 SDHCI_MAX_CURRENT_MULTIPLIER;
2822
2823 if (max_current_180 > 150)
2824 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302825
2826 /* Maximum current capabilities of the host at 1.8V */
2827 if (max_current_180 >= 800)
2828 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2829 else if (max_current_180 >= 600)
2830 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2831 else if (max_current_180 >= 400)
2832 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2833 else
2834 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302835 }
2836
Takashi Iwai8f230f42010-12-08 10:04:30 +01002837 mmc->ocr_avail = ocr_avail;
2838 mmc->ocr_avail_sdio = ocr_avail;
2839 if (host->ocr_avail_sdio)
2840 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2841 mmc->ocr_avail_sd = ocr_avail;
2842 if (host->ocr_avail_sd)
2843 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2844 else /* normal SD controllers don't support 1.8V */
2845 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2846 mmc->ocr_avail_mmc = ocr_avail;
2847 if (host->ocr_avail_mmc)
2848 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002849
2850 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302851 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002852 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002854 }
2855
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856 spin_lock_init(&host->lock);
2857
2858 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002859 * Maximum number of segments. Depends on if the hardware
2860 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002861 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002862 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002863 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002864 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002865 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002866 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002867 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002868
2869 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002870 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002871 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002873 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874
2875 /*
2876 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002877 * of bytes. When doing hardware scatter/gather, each entry cannot
2878 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002879 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002880 if (host->flags & SDHCI_USE_ADMA) {
2881 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2882 mmc->max_seg_size = 65535;
2883 else
2884 mmc->max_seg_size = 65536;
2885 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002886 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002887 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002888
2889 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002890 * Maximum block size. This varies from controller to controller and
2891 * is specified in the capabilities register.
2892 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002893 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2894 mmc->max_blk_size = 2;
2895 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302896 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002897 SDHCI_MAX_BLOCK_SHIFT;
2898 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302899 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002900 "assuming 512 bytes\n", mmc_hostname(mmc));
2901 mmc->max_blk_size = 0;
2902 }
2903 }
2904
2905 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002906
2907 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002908 * Maximum block count.
2909 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002910 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002911
2912 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002913 * Init tasklets.
2914 */
2915 tasklet_init(&host->card_tasklet,
2916 sdhci_tasklet_card, (unsigned long)host);
2917 tasklet_init(&host->finish_tasklet,
2918 sdhci_tasklet_finish, (unsigned long)host);
2919
Al Viroe4cad1b2006-10-10 22:47:07 +01002920 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002921
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302922 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302923 init_waitqueue_head(&host->buf_ready_int);
2924
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302925 /* Initialize re-tuning timer */
2926 init_timer(&host->tuning_timer);
2927 host->tuning_timer.data = (unsigned long)host;
2928 host->tuning_timer.function = sdhci_tuning_timer;
2929 }
2930
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002931 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002932 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002933 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002934 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002935
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002936 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2937 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302938 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002939 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002940 }
2941
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002942 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002943
2944#ifdef CONFIG_MMC_DEBUG
2945 sdhci_dumpregs(host);
2946#endif
2947
Pierre Ossmanf9134312008-12-21 17:01:48 +01002948#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002949 snprintf(host->led_name, sizeof(host->led_name),
2950 "%s::", mmc_hostname(mmc));
2951 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002952 host->led.brightness = LED_OFF;
2953 host->led.default_trigger = mmc_hostname(mmc);
2954 host->led.brightness_set = sdhci_led_control;
2955
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002956 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002957 if (ret)
2958 goto reset;
2959#endif
2960
Pierre Ossman5f25a662006-10-04 02:15:39 -07002961 mmiowb();
2962
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963 mmc_add_host(mmc);
2964
Girish K Sa3c76eb2011-10-11 11:44:09 +05302965 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002966 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002967 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2968 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002969
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002970 sdhci_enable_card_detection(host);
2971
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 return 0;
2973
Pierre Ossmanf9134312008-12-21 17:01:48 +01002974#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002975reset:
2976 sdhci_reset(host, SDHCI_RESET_ALL);
2977 free_irq(host->irq, host);
2978#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002979untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002980 tasklet_kill(&host->card_tasklet);
2981 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982
2983 return ret;
2984}
2985
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002986EXPORT_SYMBOL_GPL(sdhci_add_host);
2987
Pierre Ossman1e728592008-04-16 19:13:13 +02002988void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002989{
Pierre Ossman1e728592008-04-16 19:13:13 +02002990 unsigned long flags;
2991
2992 if (dead) {
2993 spin_lock_irqsave(&host->lock, flags);
2994
2995 host->flags |= SDHCI_DEVICE_DEAD;
2996
2997 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302998 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02002999 " transfer!\n", mmc_hostname(host->mmc));
3000
3001 host->mrq->cmd->error = -ENOMEDIUM;
3002 tasklet_schedule(&host->finish_tasklet);
3003 }
3004
3005 spin_unlock_irqrestore(&host->lock, flags);
3006 }
3007
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003008 sdhci_disable_card_detection(host);
3009
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003010 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003011
Pierre Ossmanf9134312008-12-21 17:01:48 +01003012#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003013 led_classdev_unregister(&host->led);
3014#endif
3015
Pierre Ossman1e728592008-04-16 19:13:13 +02003016 if (!dead)
3017 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003018
3019 free_irq(host->irq, host);
3020
3021 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303022 if (host->version >= SDHCI_SPEC_300)
3023 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003024
3025 tasklet_kill(&host->card_tasklet);
3026 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003027
Adrian Hunterceb61432011-12-27 15:48:41 +02003028 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003029 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003030
Pierre Ossman2134a922008-06-28 18:28:51 +02003031 kfree(host->adma_desc);
3032 kfree(host->align_buffer);
3033
3034 host->adma_desc = NULL;
3035 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003036}
3037
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003038EXPORT_SYMBOL_GPL(sdhci_remove_host);
3039
3040void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003041{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003042 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003043}
3044
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003045EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003046
3047/*****************************************************************************\
3048 * *
3049 * Driver init/exit *
3050 * *
3051\*****************************************************************************/
3052
3053static int __init sdhci_drv_init(void)
3054{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303055 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003056 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303057 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003058
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003059 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060}
3061
3062static void __exit sdhci_drv_exit(void)
3063{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064}
3065
3066module_init(sdhci_drv_init);
3067module_exit(sdhci_drv_exit);
3068
Pierre Ossmandf673b22006-06-30 02:22:31 -07003069module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003070module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003071
Pierre Ossman32710e82009-04-08 20:14:54 +02003072MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003073MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003074MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003075
Pierre Ossmandf673b22006-06-30 02:22:31 -07003076MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003077MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");