blob: 43f7e773487fac585f08f5f0a06e24c4d3ee734b [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080024
Pierre Ossman2f730fe2008-03-17 10:29:38 +010025#include <linux/leds.h>
26
Aries Lee22113ef2010-12-15 08:14:24 +010027#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080028#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#include "sdhci.h"
31
32#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010035 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmanf9134312008-12-21 17:01:48 +010037#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
38 defined(CONFIG_MMC_SDHCI_MODULE))
39#define SDHCI_USE_LEDS_CLASS
40#endif
41
Arindam Nathb513ea22011-05-05 12:19:04 +053042#define MAX_TUNING_LOOP 40
43
Pierre Ossmandf673b22006-06-30 02:22:31 -070044static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070045
Pierre Ossmand129bce2006-03-24 03:18:17 -080046static void sdhci_finish_data(struct sdhci_host *);
47
48static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49static void sdhci_finish_command(struct sdhci_host *);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053050static int sdhci_execute_tuning(struct mmc_host *mmc);
51static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53static void sdhci_dumpregs(struct sdhci_host *host)
54{
Philip Rakity412ab652010-09-22 15:25:13 -070055 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
56 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
58 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030059 sdhci_readl(host, SDHCI_DMA_ADDRESS),
60 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080061 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030062 sdhci_readw(host, SDHCI_BLOCK_SIZE),
63 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080064 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030065 sdhci_readl(host, SDHCI_ARGUMENT),
66 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080067 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030068 sdhci_readl(host, SDHCI_PRESENT_STATE),
69 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080070 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030071 sdhci_readb(host, SDHCI_POWER_CONTROL),
72 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030074 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
75 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080076 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
78 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080079 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readl(host, SDHCI_INT_ENABLE),
81 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080082 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readw(host, SDHCI_ACMD12_ERR),
84 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050085 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050087 sdhci_readl(host, SDHCI_CAPABILITIES_1));
88 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
89 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readl(host, SDHCI_MAX_CURRENT));
Arindam Nathf2119df2011-05-05 12:18:57 +053091 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
92 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080093
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010094 if (host->flags & SDHCI_USE_ADMA)
95 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
96 readl(host->ioaddr + SDHCI_ADMA_ERROR),
97 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
98
Pierre Ossmand129bce2006-03-24 03:18:17 -080099 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
100}
101
102/*****************************************************************************\
103 * *
104 * Low level functions *
105 * *
106\*****************************************************************************/
107
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300108static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
109{
110 u32 ier;
111
112 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
113 ier &= ~clear;
114 ier |= set;
115 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
116 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
117}
118
119static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
120{
121 sdhci_clear_set_irqs(host, 0, irqs);
122}
123
124static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
125{
126 sdhci_clear_set_irqs(host, irqs, 0);
127}
128
129static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
130{
131 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
132
Adrian Hunterc79396c2011-12-27 15:48:42 +0200133 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100134 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300135 return;
136
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300137 if (enable)
138 sdhci_unmask_irqs(host, irqs);
139 else
140 sdhci_mask_irqs(host, irqs);
141}
142
143static void sdhci_enable_card_detection(struct sdhci_host *host)
144{
145 sdhci_set_card_detection(host, true);
146}
147
148static void sdhci_disable_card_detection(struct sdhci_host *host)
149{
150 sdhci_set_card_detection(host, false);
151}
152
Pierre Ossmand129bce2006-03-24 03:18:17 -0800153static void sdhci_reset(struct sdhci_host *host, u8 mask)
154{
Pierre Ossmane16514d2006-06-30 02:22:24 -0700155 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300156 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-06-30 02:22:24 -0700157
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100158 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300159 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700160 SDHCI_CARD_PRESENT))
161 return;
162 }
163
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300164 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
165 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
166
Philip Rakity393c1a32011-01-21 11:26:40 -0800167 if (host->ops->platform_reset_enter)
168 host->ops->platform_reset_enter(host, mask);
169
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300170 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171
Pierre Ossmane16514d2006-06-30 02:22:24 -0700172 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173 host->clock = 0;
174
Pierre Ossmane16514d2006-06-30 02:22:24 -0700175 /* Wait max 100 ms */
176 timeout = 100;
177
178 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300179 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d2006-06-30 02:22:24 -0700180 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100181 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d2006-06-30 02:22:24 -0700182 mmc_hostname(host->mmc), (int)mask);
183 sdhci_dumpregs(host);
184 return;
185 }
186 timeout--;
187 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300189
Philip Rakity393c1a32011-01-21 11:26:40 -0800190 if (host->ops->platform_reset_exit)
191 host->ops->platform_reset_exit(host, mask);
192
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300193 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
194 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800195}
196
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800197static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
198
199static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800200{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800201 if (soft)
202 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
203 else
204 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300206 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
207 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700208 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
209 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300210 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800211
212 if (soft) {
213 /* force clock reconfiguration */
214 host->clock = 0;
215 sdhci_set_ios(host->mmc, &host->mmc->ios);
216 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300217}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800218
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300219static void sdhci_reinit(struct sdhci_host *host)
220{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800221 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300222 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800223}
224
225static void sdhci_activate_led(struct sdhci_host *host)
226{
227 u8 ctrl;
228
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300229 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300231 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232}
233
234static void sdhci_deactivate_led(struct sdhci_host *host)
235{
236 u8 ctrl;
237
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300238 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800239 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300240 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241}
242
Pierre Ossmanf9134312008-12-21 17:01:48 +0100243#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100244static void sdhci_led_control(struct led_classdev *led,
245 enum led_brightness brightness)
246{
247 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
248 unsigned long flags;
249
250 spin_lock_irqsave(&host->lock, flags);
251
252 if (brightness == LED_OFF)
253 sdhci_deactivate_led(host);
254 else
255 sdhci_activate_led(host);
256
257 spin_unlock_irqrestore(&host->lock, flags);
258}
259#endif
260
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261/*****************************************************************************\
262 * *
263 * Core functions *
264 * *
265\*****************************************************************************/
266
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100267static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268{
Pierre Ossman76591502008-07-21 00:32:11 +0200269 unsigned long flags;
270 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700271 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200272 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100274 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100276 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200277 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278
Pierre Ossman76591502008-07-21 00:32:11 +0200279 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100281 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200282 if (!sg_miter_next(&host->sg_miter))
283 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284
Pierre Ossman76591502008-07-21 00:32:11 +0200285 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286
Pierre Ossman76591502008-07-21 00:32:11 +0200287 blksize -= len;
288 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200289
Pierre Ossman76591502008-07-21 00:32:11 +0200290 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291
Pierre Ossman76591502008-07-21 00:32:11 +0200292 while (len) {
293 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300294 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200295 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296 }
Pierre Ossman76591502008-07-21 00:32:11 +0200297
298 *buf = scratch & 0xFF;
299
300 buf++;
301 scratch >>= 8;
302 chunk--;
303 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304 }
305 }
Pierre Ossman76591502008-07-21 00:32:11 +0200306
307 sg_miter_stop(&host->sg_miter);
308
309 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312static void sdhci_write_block_pio(struct sdhci_host *host)
313{
Pierre Ossman76591502008-07-21 00:32:11 +0200314 unsigned long flags;
315 size_t blksize, len, chunk;
316 u32 scratch;
317 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318
319 DBG("PIO writing\n");
320
321 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200322 chunk = 0;
323 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100326
327 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (!sg_miter_next(&host->sg_miter))
329 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 blksize -= len;
334 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100337
Pierre Ossman76591502008-07-21 00:32:11 +0200338 while (len) {
339 scratch |= (u32)*buf << (chunk * 8);
340
341 buf++;
342 chunk++;
343 len--;
344
345 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300346 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200347 chunk = 0;
348 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350 }
351 }
Pierre Ossman76591502008-07-21 00:32:11 +0200352
353 sg_miter_stop(&host->sg_miter);
354
355 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356}
357
358static void sdhci_transfer_pio(struct sdhci_host *host)
359{
360 u32 mask;
361
362 BUG_ON(!host->data);
363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365 return;
366
367 if (host->data->flags & MMC_DATA_READ)
368 mask = SDHCI_DATA_AVAILABLE;
369 else
370 mask = SDHCI_SPACE_AVAILABLE;
371
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200372 /*
373 * Some controllers (JMicron JMB38x) mess up the buffer bits
374 * for transfers < 4 bytes. As long as it is just one block,
375 * we can ignore the bits.
376 */
377 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
378 (host->data->blocks == 1))
379 mask = ~0;
380
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300381 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300382 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
383 udelay(100);
384
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385 if (host->data->flags & MMC_DATA_READ)
386 sdhci_read_block_pio(host);
387 else
388 sdhci_write_block_pio(host);
389
Pierre Ossman76591502008-07-21 00:32:11 +0200390 host->blocks--;
391 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393 }
394
395 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800396}
397
Pierre Ossman2134a922008-06-28 18:28:51 +0200398static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
399{
400 local_irq_save(*flags);
401 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
402}
403
404static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
405{
406 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
407 local_irq_restore(*flags);
408}
409
Ben Dooks118cd172010-03-05 13:43:26 -0800410static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
411{
Ben Dooks9e506f32010-03-05 13:43:29 -0800412 __le32 *dataddr = (__le32 __force *)(desc + 4);
413 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800414
Ben Dooks9e506f32010-03-05 13:43:29 -0800415 /* SDHCI specification says ADMA descriptors should be 4 byte
416 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800417
Ben Dooks9e506f32010-03-05 13:43:29 -0800418 cmdlen[0] = cpu_to_le16(cmd);
419 cmdlen[1] = cpu_to_le16(len);
420
421 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800422}
423
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200424static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200425 struct mmc_data *data)
426{
427 int direction;
428
429 u8 *desc;
430 u8 *align;
431 dma_addr_t addr;
432 dma_addr_t align_addr;
433 int len, offset;
434
435 struct scatterlist *sg;
436 int i;
437 char *buffer;
438 unsigned long flags;
439
440 /*
441 * The spec does not specify endianness of descriptor table.
442 * We currently guess that it is LE.
443 */
444
445 if (data->flags & MMC_DATA_READ)
446 direction = DMA_FROM_DEVICE;
447 else
448 direction = DMA_TO_DEVICE;
449
450 /*
451 * The ADMA descriptor table is mapped further down as we
452 * need to fill it with data first.
453 */
454
455 host->align_addr = dma_map_single(mmc_dev(host->mmc),
456 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700457 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200458 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200459 BUG_ON(host->align_addr & 0x3);
460
461 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
462 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200463 if (host->sg_count == 0)
464 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200465
466 desc = host->adma_desc;
467 align = host->align_buffer;
468
469 align_addr = host->align_addr;
470
471 for_each_sg(data->sg, sg, host->sg_count, i) {
472 addr = sg_dma_address(sg);
473 len = sg_dma_len(sg);
474
475 /*
476 * The SDHCI specification states that ADMA
477 * addresses must be 32-bit aligned. If they
478 * aren't, then we use a bounce buffer for
479 * the (up to three) bytes that screw up the
480 * alignment.
481 */
482 offset = (4 - (addr & 0x3)) & 0x3;
483 if (offset) {
484 if (data->flags & MMC_DATA_WRITE) {
485 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200486 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200487 memcpy(align, buffer, offset);
488 sdhci_kunmap_atomic(buffer, &flags);
489 }
490
Ben Dooks118cd172010-03-05 13:43:26 -0800491 /* tran, valid */
492 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200493
494 BUG_ON(offset > 65536);
495
Pierre Ossman2134a922008-06-28 18:28:51 +0200496 align += 4;
497 align_addr += 4;
498
499 desc += 8;
500
501 addr += offset;
502 len -= offset;
503 }
504
Pierre Ossman2134a922008-06-28 18:28:51 +0200505 BUG_ON(len > 65536);
506
Ben Dooks118cd172010-03-05 13:43:26 -0800507 /* tran, valid */
508 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200509 desc += 8;
510
511 /*
512 * If this triggers then we have a calculation bug
513 * somewhere. :/
514 */
515 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
516 }
517
Thomas Abraham70764a92010-05-26 14:42:04 -0700518 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
519 /*
520 * Mark the last descriptor as the terminating descriptor
521 */
522 if (desc != host->adma_desc) {
523 desc -= 8;
524 desc[0] |= 0x2; /* end */
525 }
526 } else {
527 /*
528 * Add a terminating entry.
529 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200530
Thomas Abraham70764a92010-05-26 14:42:04 -0700531 /* nop, end, valid */
532 sdhci_set_adma_desc(desc, 0, 0, 0x3);
533 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200534
535 /*
536 * Resync align buffer as we might have changed it.
537 */
538 if (data->flags & MMC_DATA_WRITE) {
539 dma_sync_single_for_device(mmc_dev(host->mmc),
540 host->align_addr, 128 * 4, direction);
541 }
542
543 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
544 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200545 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200546 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200548
549 return 0;
550
551unmap_entries:
552 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
553 data->sg_len, direction);
554unmap_align:
555 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
556 128 * 4, direction);
557fail:
558 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200559}
560
561static void sdhci_adma_table_post(struct sdhci_host *host,
562 struct mmc_data *data)
563{
564 int direction;
565
566 struct scatterlist *sg;
567 int i, size;
568 u8 *align;
569 char *buffer;
570 unsigned long flags;
571
572 if (data->flags & MMC_DATA_READ)
573 direction = DMA_FROM_DEVICE;
574 else
575 direction = DMA_TO_DEVICE;
576
577 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
578 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
579
580 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
581 128 * 4, direction);
582
583 if (data->flags & MMC_DATA_READ) {
584 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
585 data->sg_len, direction);
586
587 align = host->align_buffer;
588
589 for_each_sg(data->sg, sg, host->sg_count, i) {
590 if (sg_dma_address(sg) & 0x3) {
591 size = 4 - (sg_dma_address(sg) & 0x3);
592
593 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200594 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200595 memcpy(buffer, align, size);
596 sdhci_kunmap_atomic(buffer, &flags);
597
598 align += 4;
599 }
600 }
601 }
602
603 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
604 data->sg_len, direction);
605}
606
Andrei Warkentina3c77782011-04-11 16:13:42 -0500607static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800608{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700609 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500610 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700611 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800612
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200613 /*
614 * If the host controller provides us with an incorrect timeout
615 * value, just skip the check and use 0xE. The hardware may take
616 * longer to time out, but that's much better than having a too-short
617 * timeout value.
618 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200619 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200620 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200621
Andrei Warkentina3c77782011-04-11 16:13:42 -0500622 /* Unspecified timeout, assume max */
623 if (!data && !cmd->cmd_timeout_ms)
624 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800625
Andrei Warkentina3c77782011-04-11 16:13:42 -0500626 /* timeout in us */
627 if (!data)
628 target_timeout = cmd->cmd_timeout_ms * 1000;
629 else
630 target_timeout = data->timeout_ns / 1000 +
631 data->timeout_clks / host->clock;
Anton Vorontsov81b39802009-09-22 16:45:13 -0700632
Mark Brown4b016812011-04-19 18:44:17 +0100633 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
634 host->timeout_clk = host->clock / 1000;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800635
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700636 /*
637 * Figure out needed cycles.
638 * We do this in steps in order to fit inside a 32 bit int.
639 * The first step is the minimum timeout, which will have a
640 * minimum resolution of 6 bits:
641 * (1) 2^13*1000 > 2^22,
642 * (2) host->timeout_clk < 2^16
643 * =>
644 * (1) / (2) > 2^6
645 */
Mark Brown4b016812011-04-19 18:44:17 +0100646 BUG_ON(!host->timeout_clk);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700647 count = 0;
648 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
649 while (current_timeout < target_timeout) {
650 count++;
651 current_timeout <<= 1;
652 if (count >= 0xF)
653 break;
654 }
655
656 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500657 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
658 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700659 count = 0xE;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700660
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200661 return count;
662}
663
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300664static void sdhci_set_transfer_irqs(struct sdhci_host *host)
665{
666 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
667 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
668
669 if (host->flags & SDHCI_REQ_USE_DMA)
670 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
671 else
672 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
673}
674
Andrei Warkentina3c77782011-04-11 16:13:42 -0500675static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200676{
677 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200678 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500679 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200680 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200681
682 WARN_ON(host->data);
683
Andrei Warkentina3c77782011-04-11 16:13:42 -0500684 if (data || (cmd->flags & MMC_RSP_BUSY)) {
685 count = sdhci_calc_timeout(host, cmd);
686 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
687 }
688
689 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200690 return;
691
692 /* Sanity checks */
693 BUG_ON(data->blksz * data->blocks > 524288);
694 BUG_ON(data->blksz > host->mmc->max_blk_size);
695 BUG_ON(data->blocks > 65535);
696
697 host->data = data;
698 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400699 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200700
Richard Röjforsa13abc72009-09-22 16:45:30 -0700701 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100702 host->flags |= SDHCI_REQ_USE_DMA;
703
Pierre Ossman2134a922008-06-28 18:28:51 +0200704 /*
705 * FIXME: This doesn't account for merging when mapping the
706 * scatterlist.
707 */
708 if (host->flags & SDHCI_REQ_USE_DMA) {
709 int broken, i;
710 struct scatterlist *sg;
711
712 broken = 0;
713 if (host->flags & SDHCI_USE_ADMA) {
714 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
715 broken = 1;
716 } else {
717 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
718 broken = 1;
719 }
720
721 if (unlikely(broken)) {
722 for_each_sg(data->sg, sg, data->sg_len, i) {
723 if (sg->length & 0x3) {
724 DBG("Reverting to PIO because of "
725 "transfer size (%d)\n",
726 sg->length);
727 host->flags &= ~SDHCI_REQ_USE_DMA;
728 break;
729 }
730 }
731 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100732 }
733
734 /*
735 * The assumption here being that alignment is the same after
736 * translation to device address space.
737 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200738 if (host->flags & SDHCI_REQ_USE_DMA) {
739 int broken, i;
740 struct scatterlist *sg;
741
742 broken = 0;
743 if (host->flags & SDHCI_USE_ADMA) {
744 /*
745 * As we use 3 byte chunks to work around
746 * alignment problems, we need to check this
747 * quirk.
748 */
749 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
750 broken = 1;
751 } else {
752 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
753 broken = 1;
754 }
755
756 if (unlikely(broken)) {
757 for_each_sg(data->sg, sg, data->sg_len, i) {
758 if (sg->offset & 0x3) {
759 DBG("Reverting to PIO because of "
760 "bad alignment\n");
761 host->flags &= ~SDHCI_REQ_USE_DMA;
762 break;
763 }
764 }
765 }
766 }
767
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200768 if (host->flags & SDHCI_REQ_USE_DMA) {
769 if (host->flags & SDHCI_USE_ADMA) {
770 ret = sdhci_adma_table_pre(host, data);
771 if (ret) {
772 /*
773 * This only happens when someone fed
774 * us an invalid request.
775 */
776 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200777 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200778 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300779 sdhci_writel(host, host->adma_addr,
780 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200781 }
782 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300783 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200784
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300785 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200786 data->sg, data->sg_len,
787 (data->flags & MMC_DATA_READ) ?
788 DMA_FROM_DEVICE :
789 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300790 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200791 /*
792 * This only happens when someone fed
793 * us an invalid request.
794 */
795 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200796 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200797 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200798 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300799 sdhci_writel(host, sg_dma_address(data->sg),
800 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200801 }
802 }
803 }
804
Pierre Ossman2134a922008-06-28 18:28:51 +0200805 /*
806 * Always adjust the DMA selection as some controllers
807 * (e.g. JMicron) can't do PIO properly when the selection
808 * is ADMA.
809 */
810 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300811 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 ctrl &= ~SDHCI_CTRL_DMA_MASK;
813 if ((host->flags & SDHCI_REQ_USE_DMA) &&
814 (host->flags & SDHCI_USE_ADMA))
815 ctrl |= SDHCI_CTRL_ADMA32;
816 else
817 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300818 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100819 }
820
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200822 int flags;
823
824 flags = SG_MITER_ATOMIC;
825 if (host->data->flags & MMC_DATA_READ)
826 flags |= SG_MITER_TO_SG;
827 else
828 flags |= SG_MITER_FROM_SG;
829 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200830 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800831 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700832
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300833 sdhci_set_transfer_irqs(host);
834
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400835 /* Set the DMA boundary value and block size */
836 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
837 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300838 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700839}
840
841static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500842 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700843{
844 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500845 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700846
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700847 if (data == NULL)
848 return;
849
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200850 WARN_ON(!host->data);
851
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700852 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500853 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
854 mode |= SDHCI_TRNS_MULTI;
855 /*
856 * If we are sending CMD23, CMD12 never gets sent
857 * on successful completion (so no Auto-CMD12).
858 */
859 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
860 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500861 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
862 mode |= SDHCI_TRNS_AUTO_CMD23;
863 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
864 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700865 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500866
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867 if (data->flags & MMC_DATA_READ)
868 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100869 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870 mode |= SDHCI_TRNS_DMA;
871
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300872 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800873}
874
875static void sdhci_finish_data(struct sdhci_host *host)
876{
877 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800878
879 BUG_ON(!host->data);
880
881 data = host->data;
882 host->data = NULL;
883
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100884 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200885 if (host->flags & SDHCI_USE_ADMA)
886 sdhci_adma_table_post(host, data);
887 else {
888 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
889 data->sg_len, (data->flags & MMC_DATA_READ) ?
890 DMA_FROM_DEVICE : DMA_TO_DEVICE);
891 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800892 }
893
894 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200895 * The specification states that the block count register must
896 * be updated, but it does not specify at what point in the
897 * data flow. That makes the register entirely useless to read
898 * back so we have to assume that nothing made it to the card
899 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200901 if (data->error)
902 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800903 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200904 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905
Andrei Warkentine89d4562011-05-23 15:06:37 -0500906 /*
907 * Need to send CMD12 if -
908 * a) open-ended multiblock transfer (no CMD23)
909 * b) error in multiblock transfer
910 */
911 if (data->stop &&
912 (data->error ||
913 !host->mrq->sbc)) {
914
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915 /*
916 * The controller needs a reset of internal state machines
917 * upon error conditions.
918 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200919 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800920 sdhci_reset(host, SDHCI_RESET_CMD);
921 sdhci_reset(host, SDHCI_RESET_DATA);
922 }
923
924 sdhci_send_command(host, data->stop);
925 } else
926 tasklet_schedule(&host->finish_tasklet);
927}
928
929static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
930{
931 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700932 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700933 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934
935 WARN_ON(host->cmd);
936
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700938 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700939
940 mask = SDHCI_CMD_INHIBIT;
941 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
942 mask |= SDHCI_DATA_INHIBIT;
943
944 /* We shouldn't wait for data inihibit for stop commands, even
945 though they might use busy signaling */
946 if (host->mrq->data && (cmd == host->mrq->data->stop))
947 mask &= ~SDHCI_DATA_INHIBIT;
948
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700950 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100952 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200954 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955 tasklet_schedule(&host->finish_tasklet);
956 return;
957 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700958 timeout--;
959 mdelay(1);
960 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961
962 mod_timer(&host->timer, jiffies + 10 * HZ);
963
964 host->cmd = cmd;
965
Andrei Warkentina3c77782011-04-11 16:13:42 -0500966 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300968 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
Andrei Warkentine89d4562011-05-23 15:06:37 -0500970 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700971
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100973 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200975 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 tasklet_schedule(&host->finish_tasklet);
977 return;
978 }
979
980 if (!(cmd->flags & MMC_RSP_PRESENT))
981 flags = SDHCI_CMD_RESP_NONE;
982 else if (cmd->flags & MMC_RSP_136)
983 flags = SDHCI_CMD_RESP_LONG;
984 else if (cmd->flags & MMC_RSP_BUSY)
985 flags = SDHCI_CMD_RESP_SHORT_BUSY;
986 else
987 flags = SDHCI_CMD_RESP_SHORT;
988
989 if (cmd->flags & MMC_RSP_CRC)
990 flags |= SDHCI_CMD_CRC;
991 if (cmd->flags & MMC_RSP_OPCODE)
992 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +0530993
994 /* CMD19 is special in that the Data Present Select should be set */
995 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 flags |= SDHCI_CMD_DATA;
997
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300998 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999}
1000
1001static void sdhci_finish_command(struct sdhci_host *host)
1002{
1003 int i;
1004
1005 BUG_ON(host->cmd == NULL);
1006
1007 if (host->cmd->flags & MMC_RSP_PRESENT) {
1008 if (host->cmd->flags & MMC_RSP_136) {
1009 /* CRC is stripped so we need to do some shifting. */
1010 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001011 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012 SDHCI_RESPONSE + (3-i)*4) << 8;
1013 if (i != 3)
1014 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001015 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 SDHCI_RESPONSE + (3-i)*4-1);
1017 }
1018 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001019 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 }
1021 }
1022
Pierre Ossman17b04292007-07-22 22:18:46 +02001023 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024
Andrei Warkentine89d4562011-05-23 15:06:37 -05001025 /* Finished CMD23, now send actual command. */
1026 if (host->cmd == host->mrq->sbc) {
1027 host->cmd = NULL;
1028 sdhci_send_command(host, host->mrq->cmd);
1029 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001030
Andrei Warkentine89d4562011-05-23 15:06:37 -05001031 /* Processed actual command. */
1032 if (host->data && host->data_early)
1033 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034
Andrei Warkentine89d4562011-05-23 15:06:37 -05001035 if (!host->cmd->data)
1036 tasklet_schedule(&host->finish_tasklet);
1037
1038 host->cmd = NULL;
1039 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040}
1041
1042static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1043{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301044 int div = 0; /* Initialized for compiler warning */
1045 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001046 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
Todd Poynor30832ab2011-12-27 15:48:46 +02001048 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049 return;
1050
Anton Vorontsov81146342009-03-17 00:13:59 +03001051 if (host->ops->set_clock) {
1052 host->ops->set_clock(host, clock);
1053 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1054 return;
1055 }
1056
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058
1059 if (clock == 0)
1060 goto out;
1061
Zhangfei Gao85105c52010-08-06 07:10:01 +08001062 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301063 /*
1064 * Check if the Host Controller supports Programmable Clock
1065 * Mode.
1066 */
1067 if (host->clk_mul) {
1068 u16 ctrl;
1069
1070 /*
1071 * We need to figure out whether the Host Driver needs
1072 * to select Programmable Clock Mode, or the value can
1073 * be set automatically by the Host Controller based on
1074 * the Preset Value registers.
1075 */
1076 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1077 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1078 for (div = 1; div <= 1024; div++) {
1079 if (((host->max_clk * host->clk_mul) /
1080 div) <= clock)
1081 break;
1082 }
1083 /*
1084 * Set Programmable Clock Mode in the Clock
1085 * Control register.
1086 */
1087 clk = SDHCI_PROG_CLOCK_MODE;
1088 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001089 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301090 } else {
1091 /* Version 3.00 divisors must be a multiple of 2. */
1092 if (host->max_clk <= clock)
1093 div = 1;
1094 else {
1095 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1096 div += 2) {
1097 if ((host->max_clk / div) <= clock)
1098 break;
1099 }
1100 }
1101 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001102 }
1103 } else {
1104 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001105 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001106 if ((host->max_clk / div) <= clock)
1107 break;
1108 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301109 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
Arindam Nathc3ed3872011-05-05 12:19:06 +05301112 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001113 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1114 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001115 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001116 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117
Chris Ball27f6cb12009-09-22 16:45:31 -07001118 /* Wait max 20 ms */
1119 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001120 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001121 & SDHCI_CLOCK_INT_STABLE)) {
1122 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001123 printk(KERN_ERR "%s: Internal clock never "
1124 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125 sdhci_dumpregs(host);
1126 return;
1127 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001128 timeout--;
1129 mdelay(1);
1130 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131
1132 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001133 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134
1135out:
1136 host->clock = clock;
1137}
1138
Pierre Ossman146ad662006-06-30 02:22:23 -07001139static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1140{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001141 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001142
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001143 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001144 switch (1 << power) {
1145 case MMC_VDD_165_195:
1146 pwr = SDHCI_POWER_180;
1147 break;
1148 case MMC_VDD_29_30:
1149 case MMC_VDD_30_31:
1150 pwr = SDHCI_POWER_300;
1151 break;
1152 case MMC_VDD_32_33:
1153 case MMC_VDD_33_34:
1154 pwr = SDHCI_POWER_330;
1155 break;
1156 default:
1157 BUG();
1158 }
1159 }
1160
1161 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001162 return;
1163
Pierre Ossmanae628902009-05-03 20:45:03 +02001164 host->pwr = pwr;
1165
1166 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001168 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001169 }
1170
1171 /*
1172 * Spec says that we should clear the power reg before setting
1173 * a new value. Some controllers don't seem to like this though.
1174 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001175 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001176 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001177
Andres Salomone08c1692008-07-04 10:00:03 -07001178 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001179 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001180 * and set turn on power at the same time, so set the voltage first.
1181 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001182 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001183 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1184
1185 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001186
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001187 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001188
1189 /*
1190 * Some controllers need an extra 10ms delay of 10ms before they
1191 * can apply clock after applying power
1192 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001193 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001194 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001195}
1196
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197/*****************************************************************************\
1198 * *
1199 * MMC callbacks *
1200 * *
1201\*****************************************************************************/
1202
1203static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1204{
1205 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001206 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 unsigned long flags;
1208
1209 host = mmc_priv(mmc);
1210
1211 spin_lock_irqsave(&host->lock, flags);
1212
1213 WARN_ON(host->mrq != NULL);
1214
Pierre Ossmanf9134312008-12-21 17:01:48 +01001215#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001217#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001218
1219 /*
1220 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1221 * requests if Auto-CMD12 is enabled.
1222 */
1223 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001224 if (mrq->stop) {
1225 mrq->data->stop = NULL;
1226 mrq->stop = NULL;
1227 }
1228 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001229
1230 host->mrq = mrq;
1231
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001232 /* If polling, assume that the card is always present. */
1233 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1234 present = true;
1235 else
1236 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1237 SDHCI_CARD_PRESENT;
1238
1239 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001240 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301242 } else {
1243 u32 present_state;
1244
1245 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1246 /*
1247 * Check if the re-tuning timer has already expired and there
1248 * is no on-going data transfer. If so, we need to execute
1249 * tuning procedure before sending command.
1250 */
1251 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1252 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1253 spin_unlock_irqrestore(&host->lock, flags);
1254 sdhci_execute_tuning(mmc);
1255 spin_lock_irqsave(&host->lock, flags);
1256
1257 /* Restore original mmc_request structure */
1258 host->mrq = mrq;
1259 }
1260
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001261 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001262 sdhci_send_command(host, mrq->sbc);
1263 else
1264 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301265 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266
Pierre Ossman5f25a662006-10-04 02:15:39 -07001267 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268 spin_unlock_irqrestore(&host->lock, flags);
1269}
1270
1271static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1272{
1273 struct sdhci_host *host;
1274 unsigned long flags;
1275 u8 ctrl;
1276
1277 host = mmc_priv(mmc);
1278
1279 spin_lock_irqsave(&host->lock, flags);
1280
Pierre Ossman1e728592008-04-16 19:13:13 +02001281 if (host->flags & SDHCI_DEVICE_DEAD)
1282 goto out;
1283
Pierre Ossmand129bce2006-03-24 03:18:17 -08001284 /*
1285 * Reset the chip on each power off.
1286 * Should clear out any weird states.
1287 */
1288 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001289 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001290 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001291 }
1292
1293 sdhci_set_clock(host, ios->clock);
1294
1295 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001296 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001298 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299
Philip Rakity643a81f2010-09-23 08:24:32 -07001300 if (host->ops->platform_send_init_74_clocks)
1301 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1302
Philip Rakity15ec4462010-11-19 16:48:39 -05001303 /*
1304 * If your platform has 8-bit width support but is not a v3 controller,
1305 * or if it requires special setup code, you should implement that in
1306 * platform_8bit_width().
1307 */
1308 if (host->ops->platform_8bit_width)
1309 host->ops->platform_8bit_width(host, ios->bus_width);
1310 else {
1311 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1312 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1313 ctrl &= ~SDHCI_CTRL_4BITBUS;
1314 if (host->version >= SDHCI_SPEC_300)
1315 ctrl |= SDHCI_CTRL_8BITBUS;
1316 } else {
1317 if (host->version >= SDHCI_SPEC_300)
1318 ctrl &= ~SDHCI_CTRL_8BITBUS;
1319 if (ios->bus_width == MMC_BUS_WIDTH_4)
1320 ctrl |= SDHCI_CTRL_4BITBUS;
1321 else
1322 ctrl &= ~SDHCI_CTRL_4BITBUS;
1323 }
1324 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1325 }
1326
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001327 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001328
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001329 if ((ios->timing == MMC_TIMING_SD_HS ||
1330 ios->timing == MMC_TIMING_MMC_HS)
1331 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001332 ctrl |= SDHCI_CTRL_HISPD;
1333 else
1334 ctrl &= ~SDHCI_CTRL_HISPD;
1335
Arindam Nathd6d50a12011-05-05 12:18:59 +05301336 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301337 u16 clk, ctrl_2;
1338 unsigned int clock;
1339
1340 /* In case of UHS-I modes, set High Speed Enable */
1341 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1342 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1343 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001344 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301345 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301346
1347 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1348 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301349 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301350 /*
1351 * We only need to set Driver Strength if the
1352 * preset value enable is not set.
1353 */
1354 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1355 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1356 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1357 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1358 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1359
1360 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301361 } else {
1362 /*
1363 * According to SDHC Spec v3.00, if the Preset Value
1364 * Enable in the Host Control 2 register is set, we
1365 * need to reset SD Clock Enable before changing High
1366 * Speed Enable to avoid generating clock gliches.
1367 */
Arindam Nath758535c2011-05-05 12:19:00 +05301368
1369 /* Reset SD Clock Enable */
1370 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1371 clk &= ~SDHCI_CLOCK_CARD_EN;
1372 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1373
1374 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1375
1376 /* Re-enable SD Clock */
1377 clock = host->clock;
1378 host->clock = 0;
1379 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301380 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301381
Arindam Nath49c468f2011-05-05 12:19:01 +05301382
1383 /* Reset SD Clock Enable */
1384 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1385 clk &= ~SDHCI_CLOCK_CARD_EN;
1386 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1387
Philip Rakity6322cdd2011-05-13 11:17:15 +05301388 if (host->ops->set_uhs_signaling)
1389 host->ops->set_uhs_signaling(host, ios->timing);
1390 else {
1391 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1392 /* Select Bus Speed Mode for host */
1393 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1394 if (ios->timing == MMC_TIMING_UHS_SDR12)
1395 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1396 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1397 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1398 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1399 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1400 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1401 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1402 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1403 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1404 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1405 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301406
1407 /* Re-enable SD Clock */
1408 clock = host->clock;
1409 host->clock = 0;
1410 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301411 } else
1412 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301413
Leandro Dorileob8352262007-07-25 23:47:04 +02001414 /*
1415 * Some (ENE) controllers go apeshit on some ios operation,
1416 * signalling timeout and CRC errors even on CMD0. Resetting
1417 * it on each ios seems to solve the problem.
1418 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001419 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001420 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1421
Pierre Ossman1e728592008-04-16 19:13:13 +02001422out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001423 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001424 spin_unlock_irqrestore(&host->lock, flags);
1425}
1426
Takashi Iwai82b0e232011-04-21 20:26:38 +02001427static int check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001428{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001430 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432 spin_lock_irqsave(&host->lock, flags);
1433
Pierre Ossman1e728592008-04-16 19:13:13 +02001434 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001435 is_readonly = 0;
1436 else if (host->ops->get_ro)
1437 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001438 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001439 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1440 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441
1442 spin_unlock_irqrestore(&host->lock, flags);
1443
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001444 /* This quirk needs to be replaced by a callback-function later */
1445 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1446 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447}
1448
Takashi Iwai82b0e232011-04-21 20:26:38 +02001449#define SAMPLE_COUNT 5
1450
1451static int sdhci_get_ro(struct mmc_host *mmc)
1452{
1453 struct sdhci_host *host;
1454 int i, ro_count;
1455
1456 host = mmc_priv(mmc);
1457
1458 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1459 return check_ro(host);
1460
1461 ro_count = 0;
1462 for (i = 0; i < SAMPLE_COUNT; i++) {
1463 if (check_ro(host)) {
1464 if (++ro_count > SAMPLE_COUNT / 2)
1465 return 1;
1466 }
1467 msleep(30);
1468 }
1469 return 0;
1470}
1471
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001472static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
Adrian Hunter20758b62011-08-29 16:42:12 +03001473{
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001474 struct sdhci_host *host;
1475 unsigned long flags;
Adrian Hunter20758b62011-08-29 16:42:12 +03001476
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001477 host = mmc_priv(mmc);
Adrian Hunter20758b62011-08-29 16:42:12 +03001478
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001479 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001480
Pierre Ossman1e728592008-04-16 19:13:13 +02001481 if (host->flags & SDHCI_DEVICE_DEAD)
1482 goto out;
1483
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001484 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001485 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1486 else
1487 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001488out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001489 mmiowb();
1490
1491 spin_unlock_irqrestore(&host->lock, flags);
1492}
1493
Arindam Nathf2119df2011-05-05 12:18:57 +05301494static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1495 struct mmc_ios *ios)
1496{
1497 struct sdhci_host *host;
1498 u8 pwr;
1499 u16 clk, ctrl;
1500 u32 present_state;
1501
1502 host = mmc_priv(mmc);
1503
1504 /*
1505 * Signal Voltage Switching is only applicable for Host Controllers
1506 * v3.00 and above.
1507 */
1508 if (host->version < SDHCI_SPEC_300)
1509 return 0;
1510
1511 /*
1512 * We first check whether the request is to set signalling voltage
1513 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1514 */
1515 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1516 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1517 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1518 ctrl &= ~SDHCI_CTRL_VDD_180;
1519 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1520
1521 /* Wait for 5ms */
1522 usleep_range(5000, 5500);
1523
1524 /* 3.3V regulator output should be stable within 5 ms */
1525 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1526 if (!(ctrl & SDHCI_CTRL_VDD_180))
1527 return 0;
1528 else {
1529 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1530 "signalling voltage failed\n");
1531 return -EIO;
1532 }
1533 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1534 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1535 /* Stop SDCLK */
1536 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1537 clk &= ~SDHCI_CLOCK_CARD_EN;
1538 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1539
1540 /* Check whether DAT[3:0] is 0000 */
1541 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1542 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1543 SDHCI_DATA_LVL_SHIFT)) {
1544 /*
1545 * Enable 1.8V Signal Enable in the Host Control2
1546 * register
1547 */
1548 ctrl |= SDHCI_CTRL_VDD_180;
1549 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1550
1551 /* Wait for 5ms */
1552 usleep_range(5000, 5500);
1553
1554 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1555 if (ctrl & SDHCI_CTRL_VDD_180) {
1556 /* Provide SDCLK again and wait for 1ms*/
1557 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1558 clk |= SDHCI_CLOCK_CARD_EN;
1559 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1560 usleep_range(1000, 1500);
1561
1562 /*
1563 * If DAT[3:0] level is 1111b, then the card
1564 * was successfully switched to 1.8V signaling.
1565 */
1566 present_state = sdhci_readl(host,
1567 SDHCI_PRESENT_STATE);
1568 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1569 SDHCI_DATA_LVL_MASK)
1570 return 0;
1571 }
1572 }
1573
1574 /*
1575 * If we are here, that means the switch to 1.8V signaling
1576 * failed. We power cycle the card, and retry initialization
1577 * sequence by setting S18R to 0.
1578 */
1579 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1580 pwr &= ~SDHCI_POWER_ON;
1581 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1582
1583 /* Wait for 1ms as per the spec */
1584 usleep_range(1000, 1500);
1585 pwr |= SDHCI_POWER_ON;
1586 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1587
1588 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1589 "voltage failed, retrying with S18R set to 0\n");
1590 return -EAGAIN;
1591 } else
1592 /* No signal voltage switch required */
1593 return 0;
1594}
1595
Arindam Nathb513ea22011-05-05 12:19:04 +05301596static int sdhci_execute_tuning(struct mmc_host *mmc)
1597{
1598 struct sdhci_host *host;
1599 u16 ctrl;
1600 u32 ier;
1601 int tuning_loop_counter = MAX_TUNING_LOOP;
1602 unsigned long timeout;
1603 int err = 0;
1604
1605 host = mmc_priv(mmc);
1606
1607 disable_irq(host->irq);
1608 spin_lock(&host->lock);
1609
1610 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1611
1612 /*
1613 * Host Controller needs tuning only in case of SDR104 mode
1614 * and for SDR50 mode when Use Tuning for SDR50 is set in
1615 * Capabilities register.
1616 */
1617 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1618 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1619 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1620 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1621 else {
1622 spin_unlock(&host->lock);
1623 enable_irq(host->irq);
1624 return 0;
1625 }
1626
1627 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1628
1629 /*
1630 * As per the Host Controller spec v3.00, tuning command
1631 * generates Buffer Read Ready interrupt, so enable that.
1632 *
1633 * Note: The spec clearly says that when tuning sequence
1634 * is being performed, the controller does not generate
1635 * interrupts other than Buffer Read Ready interrupt. But
1636 * to make sure we don't hit a controller bug, we _only_
1637 * enable Buffer Read Ready interrupt here.
1638 */
1639 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1640 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1641
1642 /*
1643 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1644 * of loops reaches 40 times or a timeout of 150ms occurs.
1645 */
1646 timeout = 150;
1647 do {
1648 struct mmc_command cmd = {0};
1649 struct mmc_request mrq = {0};
1650
1651 if (!tuning_loop_counter && !timeout)
1652 break;
1653
1654 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1655 cmd.arg = 0;
1656 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1657 cmd.retries = 0;
1658 cmd.data = NULL;
1659 cmd.error = 0;
1660
1661 mrq.cmd = &cmd;
1662 host->mrq = &mrq;
1663
1664 /*
1665 * In response to CMD19, the card sends 64 bytes of tuning
1666 * block to the Host Controller. So we set the block size
1667 * to 64 here.
1668 */
1669 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1670
1671 /*
1672 * The tuning block is sent by the card to the host controller.
1673 * So we set the TRNS_READ bit in the Transfer Mode register.
1674 * This also takes care of setting DMA Enable and Multi Block
1675 * Select in the same register to 0.
1676 */
1677 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1678
1679 sdhci_send_command(host, &cmd);
1680
1681 host->cmd = NULL;
1682 host->mrq = NULL;
1683
1684 spin_unlock(&host->lock);
1685 enable_irq(host->irq);
1686
1687 /* Wait for Buffer Read Ready interrupt */
1688 wait_event_interruptible_timeout(host->buf_ready_int,
1689 (host->tuning_done == 1),
1690 msecs_to_jiffies(50));
1691 disable_irq(host->irq);
1692 spin_lock(&host->lock);
1693
1694 if (!host->tuning_done) {
1695 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1696 "Buffer Read Ready interrupt during tuning "
1697 "procedure, falling back to fixed sampling "
1698 "clock\n");
1699 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1700 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1701 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1702 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1703
1704 err = -EIO;
1705 goto out;
1706 }
1707
1708 host->tuning_done = 0;
1709
1710 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1711 tuning_loop_counter--;
1712 timeout--;
1713 mdelay(1);
1714 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1715
1716 /*
1717 * The Host Driver has exhausted the maximum number of loops allowed,
1718 * so use fixed sampling frequency.
1719 */
1720 if (!tuning_loop_counter || !timeout) {
1721 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1722 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1723 } else {
1724 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1725 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1726 " failed, falling back to fixed sampling"
1727 " clock\n");
1728 err = -EIO;
1729 }
1730 }
1731
1732out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301733 /*
1734 * If this is the very first time we are here, we start the retuning
1735 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1736 * flag won't be set, we check this condition before actually starting
1737 * the timer.
1738 */
1739 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1740 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1741 mod_timer(&host->tuning_timer, jiffies +
1742 host->tuning_count * HZ);
1743 /* Tuning mode 1 limits the maximum data length to 4MB */
1744 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1745 } else {
1746 host->flags &= ~SDHCI_NEEDS_RETUNING;
1747 /* Reload the new initial value for timer */
1748 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1749 mod_timer(&host->tuning_timer, jiffies +
1750 host->tuning_count * HZ);
1751 }
1752
1753 /*
1754 * In case tuning fails, host controllers which support re-tuning can
1755 * try tuning again at a later time, when the re-tuning timer expires.
1756 * So for these controllers, we return 0. Since there might be other
1757 * controllers who do not have this capability, we return error for
1758 * them.
1759 */
1760 if (err && host->tuning_count &&
1761 host->tuning_mode == SDHCI_TUNING_MODE_1)
1762 err = 0;
1763
Arindam Nathb513ea22011-05-05 12:19:04 +05301764 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1765 spin_unlock(&host->lock);
1766 enable_irq(host->irq);
1767
1768 return err;
1769}
1770
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301771static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1772{
1773 struct sdhci_host *host;
1774 u16 ctrl;
1775 unsigned long flags;
1776
1777 host = mmc_priv(mmc);
1778
1779 /* Host Controller v3.00 defines preset value registers */
1780 if (host->version < SDHCI_SPEC_300)
1781 return;
1782
1783 spin_lock_irqsave(&host->lock, flags);
1784
1785 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1786
1787 /*
1788 * We only enable or disable Preset Value if they are not already
1789 * enabled or disabled respectively. Otherwise, we bail out.
1790 */
1791 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1792 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1793 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1794 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1795 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1796 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1797 }
1798
1799 spin_unlock_irqrestore(&host->lock, flags);
1800}
1801
David Brownellab7aefd2006-11-12 17:55:30 -08001802static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001803 .request = sdhci_request,
1804 .set_ios = sdhci_set_ios,
1805 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001806 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301807 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301808 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301809 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001810};
1811
1812/*****************************************************************************\
1813 * *
1814 * Tasklets *
1815 * *
1816\*****************************************************************************/
1817
1818static void sdhci_tasklet_card(unsigned long param)
1819{
1820 struct sdhci_host *host;
1821 unsigned long flags;
1822
1823 host = (struct sdhci_host*)param;
1824
1825 spin_lock_irqsave(&host->lock, flags);
1826
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001827 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001828 if (host->mrq) {
1829 printk(KERN_ERR "%s: Card removed during transfer!\n",
1830 mmc_hostname(host->mmc));
1831 printk(KERN_ERR "%s: Resetting controller.\n",
1832 mmc_hostname(host->mmc));
1833
1834 sdhci_reset(host, SDHCI_RESET_CMD);
1835 sdhci_reset(host, SDHCI_RESET_DATA);
1836
Pierre Ossman17b04292007-07-22 22:18:46 +02001837 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001838 tasklet_schedule(&host->finish_tasklet);
1839 }
1840 }
1841
1842 spin_unlock_irqrestore(&host->lock, flags);
1843
Pierre Ossman04cf5852008-08-18 22:18:14 +02001844 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001845}
1846
1847static void sdhci_tasklet_finish(unsigned long param)
1848{
1849 struct sdhci_host *host;
1850 unsigned long flags;
1851 struct mmc_request *mrq;
1852
1853 host = (struct sdhci_host*)param;
1854
Chris Ball0c9c99a2011-04-27 17:35:31 -04001855 /*
1856 * If this tasklet gets rescheduled while running, it will
1857 * be run again afterwards but without any active request.
1858 */
1859 if (!host->mrq)
1860 return;
1861
Pierre Ossmand129bce2006-03-24 03:18:17 -08001862 spin_lock_irqsave(&host->lock, flags);
1863
1864 del_timer(&host->timer);
1865
1866 mrq = host->mrq;
1867
Pierre Ossmand129bce2006-03-24 03:18:17 -08001868 /*
1869 * The controller needs a reset of internal state machines
1870 * upon error conditions.
1871 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001872 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001873 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001874 (mrq->data && (mrq->data->error ||
1875 (mrq->data->stop && mrq->data->stop->error))) ||
1876 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001877
1878 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001879 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001880 unsigned int clock;
1881
1882 /* This is to force an update */
1883 clock = host->clock;
1884 host->clock = 0;
1885 sdhci_set_clock(host, clock);
1886 }
1887
1888 /* Spec says we should do both at the same time, but Ricoh
1889 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001890 sdhci_reset(host, SDHCI_RESET_CMD);
1891 sdhci_reset(host, SDHCI_RESET_DATA);
1892 }
1893
1894 host->mrq = NULL;
1895 host->cmd = NULL;
1896 host->data = NULL;
1897
Pierre Ossmanf9134312008-12-21 17:01:48 +01001898#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001899 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001900#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001901
Pierre Ossman5f25a662006-10-04 02:15:39 -07001902 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001903 spin_unlock_irqrestore(&host->lock, flags);
1904
1905 mmc_request_done(host->mmc, mrq);
1906}
1907
1908static void sdhci_timeout_timer(unsigned long data)
1909{
1910 struct sdhci_host *host;
1911 unsigned long flags;
1912
1913 host = (struct sdhci_host*)data;
1914
1915 spin_lock_irqsave(&host->lock, flags);
1916
1917 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001918 printk(KERN_ERR "%s: Timeout waiting for hardware "
1919 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001920 sdhci_dumpregs(host);
1921
1922 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001923 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001924 sdhci_finish_data(host);
1925 } else {
1926 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001927 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001928 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001929 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001930
1931 tasklet_schedule(&host->finish_tasklet);
1932 }
1933 }
1934
Pierre Ossman5f25a662006-10-04 02:15:39 -07001935 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001936 spin_unlock_irqrestore(&host->lock, flags);
1937}
1938
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301939static void sdhci_tuning_timer(unsigned long data)
1940{
1941 struct sdhci_host *host;
1942 unsigned long flags;
1943
1944 host = (struct sdhci_host *)data;
1945
1946 spin_lock_irqsave(&host->lock, flags);
1947
1948 host->flags |= SDHCI_NEEDS_RETUNING;
1949
1950 spin_unlock_irqrestore(&host->lock, flags);
1951}
1952
Pierre Ossmand129bce2006-03-24 03:18:17 -08001953/*****************************************************************************\
1954 * *
1955 * Interrupt handling *
1956 * *
1957\*****************************************************************************/
1958
1959static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1960{
1961 BUG_ON(intmask == 0);
1962
1963 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001964 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1965 "though no command operation was in progress.\n",
1966 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967 sdhci_dumpregs(host);
1968 return;
1969 }
1970
Pierre Ossman43b58b32007-07-25 23:15:27 +02001971 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001972 host->cmd->error = -ETIMEDOUT;
1973 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1974 SDHCI_INT_INDEX))
1975 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001976
Pierre Ossmane8095172008-07-25 01:09:08 +02001977 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001978 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001979 return;
1980 }
1981
1982 /*
1983 * The host can send and interrupt when the busy state has
1984 * ended, allowing us to wait without wasting CPU cycles.
1985 * Unfortunately this is overloaded on the "data complete"
1986 * interrupt, so we need to take some care when handling
1987 * it.
1988 *
1989 * Note: The 1.0 specification is a bit ambiguous about this
1990 * feature so there might be some problems with older
1991 * controllers.
1992 */
1993 if (host->cmd->flags & MMC_RSP_BUSY) {
1994 if (host->cmd->data)
1995 DBG("Cannot wait for busy signal when also "
1996 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001997 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001998 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001999
2000 /* The controller does not support the end-of-busy IRQ,
2001 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002002 }
2003
2004 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002005 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002006}
2007
George G. Davis0957c332010-02-18 12:32:12 -05002008#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002009static void sdhci_show_adma_error(struct sdhci_host *host)
2010{
2011 const char *name = mmc_hostname(host->mmc);
2012 u8 *desc = host->adma_desc;
2013 __le32 *dma;
2014 __le16 *len;
2015 u8 attr;
2016
2017 sdhci_dumpregs(host);
2018
2019 while (true) {
2020 dma = (__le32 *)(desc + 4);
2021 len = (__le16 *)(desc + 2);
2022 attr = *desc;
2023
2024 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2025 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2026
2027 desc += 8;
2028
2029 if (attr & 2)
2030 break;
2031 }
2032}
2033#else
2034static void sdhci_show_adma_error(struct sdhci_host *host) { }
2035#endif
2036
Pierre Ossmand129bce2006-03-24 03:18:17 -08002037static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2038{
2039 BUG_ON(intmask == 0);
2040
Arindam Nathb513ea22011-05-05 12:19:04 +05302041 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2042 if (intmask & SDHCI_INT_DATA_AVAIL) {
2043 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2044 MMC_SEND_TUNING_BLOCK) {
2045 host->tuning_done = 1;
2046 wake_up(&host->buf_ready_int);
2047 return;
2048 }
2049 }
2050
Pierre Ossmand129bce2006-03-24 03:18:17 -08002051 if (!host->data) {
2052 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002053 * The "data complete" interrupt is also used to
2054 * indicate that a busy state has ended. See comment
2055 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002056 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002057 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2058 if (intmask & SDHCI_INT_DATA_END) {
2059 sdhci_finish_command(host);
2060 return;
2061 }
2062 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002063
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002064 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
2065 "though no data operation was in progress.\n",
2066 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002067 sdhci_dumpregs(host);
2068
2069 return;
2070 }
2071
2072 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002073 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002074 else if (intmask & SDHCI_INT_DATA_END_BIT)
2075 host->data->error = -EILSEQ;
2076 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2077 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2078 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002079 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002080 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2081 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
2082 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002083 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002084 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002085
Pierre Ossman17b04292007-07-22 22:18:46 +02002086 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002087 sdhci_finish_data(host);
2088 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002089 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002090 sdhci_transfer_pio(host);
2091
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002092 /*
2093 * We currently don't do anything fancy with DMA
2094 * boundaries, but as we can't disable the feature
2095 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002096 *
2097 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2098 * should return a valid address to continue from, but as
2099 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002100 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002101 if (intmask & SDHCI_INT_DMA_END) {
2102 u32 dmastart, dmanow;
2103 dmastart = sg_dma_address(host->data->sg);
2104 dmanow = dmastart + host->data->bytes_xfered;
2105 /*
2106 * Force update to the next DMA block boundary.
2107 */
2108 dmanow = (dmanow &
2109 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2110 SDHCI_DEFAULT_BOUNDARY_SIZE;
2111 host->data->bytes_xfered = dmanow - dmastart;
2112 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2113 " next 0x%08x\n",
2114 mmc_hostname(host->mmc), dmastart,
2115 host->data->bytes_xfered, dmanow);
2116 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2117 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002118
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002119 if (intmask & SDHCI_INT_DATA_END) {
2120 if (host->cmd) {
2121 /*
2122 * Data managed to finish before the
2123 * command completed. Make sure we do
2124 * things in the proper order.
2125 */
2126 host->data_early = 1;
2127 } else {
2128 sdhci_finish_data(host);
2129 }
2130 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131 }
2132}
2133
David Howells7d12e782006-10-05 14:55:46 +01002134static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135{
2136 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002137 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002138 u32 intmask, unexpected = 0;
2139 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002140
2141 spin_lock(&host->lock);
2142
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002143 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144
Mark Lord62df67a2007-03-06 13:30:13 +01002145 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146 result = IRQ_NONE;
2147 goto out;
2148 }
2149
Alexander Stein6379b232012-03-14 09:52:10 +01002150again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002151 DBG("*** %s got interrupt: 0x%08x\n",
2152 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002153
Pierre Ossman3192a282006-06-30 02:22:26 -07002154 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002155 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2156 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002157 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002158 }
2159
2160 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002161
2162 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002163 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2164 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002165 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 }
2167
2168 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002169 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2170 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002171 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002172 }
2173
2174 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2175
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002176 intmask &= ~SDHCI_INT_ERROR;
2177
Pierre Ossmand129bce2006-03-24 03:18:17 -08002178 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07002179 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002180 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002181 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002182 }
2183
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002184 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002185
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002186 if (intmask & SDHCI_INT_CARD_INT)
2187 cardint = 1;
2188
2189 intmask &= ~SDHCI_INT_CARD_INT;
2190
Pierre Ossman3192a282006-06-30 02:22:26 -07002191 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002192 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002193 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_dumpregs(host);
Alexander Stein6379b232012-03-14 09:52:10 +01002195 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002196 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002197 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198
2199 result = IRQ_HANDLED;
2200
Alexander Stein6379b232012-03-14 09:52:10 +01002201 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2202 if (intmask && --max_loops)
2203 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002204out:
2205 spin_unlock(&host->lock);
2206
Alexander Stein6379b232012-03-14 09:52:10 +01002207 if (unexpected) {
2208 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2209 mmc_hostname(host->mmc), unexpected);
2210 sdhci_dumpregs(host);
2211 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002212 /*
2213 * We have to delay this as it calls back into the driver.
2214 */
2215 if (cardint)
2216 mmc_signal_sdio_irq(host->mmc);
2217
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218 return result;
2219}
2220
2221/*****************************************************************************\
2222 * *
2223 * Suspend/resume *
2224 * *
2225\*****************************************************************************/
2226
2227#ifdef CONFIG_PM
2228
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002229int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002231 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232
Chris Balla1b13b42012-02-06 00:43:59 -05002233 if (host->ops->platform_suspend)
2234 host->ops->platform_suspend(host);
2235
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002236 sdhci_disable_card_detection(host);
2237
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302238 /* Disable tuning since we are suspending */
2239 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2240 host->tuning_mode == SDHCI_TUNING_MODE_1) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002241 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302242 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302243 }
2244
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002245 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002246 if (ret)
2247 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002248
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002249 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002251 if (host->vmmc)
2252 ret = regulator_disable(host->vmmc);
2253
2254 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002255}
2256
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002257EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002259int sdhci_resume_host(struct sdhci_host *host)
2260{
2261 int ret;
2262
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002263 if (host->vmmc) {
2264 int ret = regulator_enable(host->vmmc);
2265 if (ret)
2266 return ret;
2267 }
2268
2269
Richard Röjforsa13abc72009-09-22 16:45:30 -07002270 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002271 if (host->ops->enable_dma)
2272 host->ops->enable_dma(host);
2273 }
2274
2275 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2276 mmc_hostname(host->mmc), host);
2277 if (ret)
2278 return ret;
2279
Adrian Hunter6308d292012-02-07 14:48:54 +02002280 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2281 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2282 /* Card keeps power but host controller does not */
2283 sdhci_init(host, 0);
2284 host->pwr = 0;
2285 host->clock = 0;
2286 sdhci_do_set_ios(host, &host->mmc->ios);
2287 } else {
2288 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2289 mmiowb();
2290 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002291
2292 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002293 sdhci_enable_card_detection(host);
2294
Chris Balla1b13b42012-02-06 00:43:59 -05002295 if (host->ops->platform_resume)
2296 host->ops->platform_resume(host);
2297
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302298 /* Set the re-tuning expiration flag */
2299 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2300 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2301 host->flags |= SDHCI_NEEDS_RETUNING;
2302
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002303 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002304}
2305
2306EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307
Daniel Drake5f619702010-11-04 22:20:39 +00002308void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2309{
2310 u8 val;
2311 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2312 val |= SDHCI_WAKE_ON_INT;
2313 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2314}
2315
2316EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2317
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318#endif /* CONFIG_PM */
2319
2320/*****************************************************************************\
2321 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002322 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323 * *
2324\*****************************************************************************/
2325
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002326struct sdhci_host *sdhci_alloc_host(struct device *dev,
2327 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002329 struct mmc_host *mmc;
2330 struct sdhci_host *host;
2331
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002332 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002333
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002334 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002336 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337
2338 host = mmc_priv(mmc);
2339 host->mmc = mmc;
2340
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002341 return host;
2342}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002343
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002344EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002346int sdhci_add_host(struct sdhci_host *host)
2347{
2348 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302349 u32 caps[2];
2350 u32 max_current_caps;
2351 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002352 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002353
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002354 WARN_ON(host == NULL);
2355 if (host == NULL)
2356 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002358 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002360 if (debug_quirks)
2361 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362
Pierre Ossmand96649e2006-06-30 02:22:30 -07002363 sdhci_reset(host, SDHCI_RESET_ALL);
2364
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002365 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002366 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2367 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002368 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07002369 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002370 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002371 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002372 }
2373
Arindam Nathf2119df2011-05-05 12:18:57 +05302374 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002375 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376
Arindam Nathf2119df2011-05-05 12:18:57 +05302377 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2378 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2379
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002380 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002381 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302382 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002383 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002384 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002385 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002387 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002388 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002389 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002390 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002391 }
2392
Arindam Nathf2119df2011-05-05 12:18:57 +05302393 if ((host->version >= SDHCI_SPEC_200) &&
2394 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002395 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002396
2397 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2398 (host->flags & SDHCI_USE_ADMA)) {
2399 DBG("Disabling ADMA as it is marked broken\n");
2400 host->flags &= ~SDHCI_USE_ADMA;
2401 }
2402
Richard Röjforsa13abc72009-09-22 16:45:30 -07002403 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002404 if (host->ops->enable_dma) {
2405 if (host->ops->enable_dma(host)) {
2406 printk(KERN_WARNING "%s: No suitable DMA "
2407 "available. Falling back to PIO.\n",
2408 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002409 host->flags &=
2410 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002411 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002412 }
2413 }
2414
Pierre Ossman2134a922008-06-28 18:28:51 +02002415 if (host->flags & SDHCI_USE_ADMA) {
2416 /*
2417 * We need to allocate descriptors for all sg entries
2418 * (128) and potentially one alignment transfer for
2419 * each of those entries.
2420 */
2421 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2422 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2423 if (!host->adma_desc || !host->align_buffer) {
2424 kfree(host->adma_desc);
2425 kfree(host->align_buffer);
2426 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2427 "buffers. Falling back to standard DMA.\n",
2428 mmc_hostname(mmc));
2429 host->flags &= ~SDHCI_USE_ADMA;
2430 }
2431 }
2432
Pierre Ossman76591502008-07-21 00:32:11 +02002433 /*
2434 * If we use DMA, then it's up to the caller to set the DMA
2435 * mask, but PIO does not need the hw shim so we set a new
2436 * mask here in that case.
2437 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002438 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002439 host->dma_mask = DMA_BIT_MASK(64);
2440 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2441 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002442
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002443 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302444 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002445 >> SDHCI_CLOCK_BASE_SHIFT;
2446 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302447 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002448 >> SDHCI_CLOCK_BASE_SHIFT;
2449
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002451 if (host->max_clk == 0 || host->quirks &
2452 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002453 if (!host->ops->get_max_clock) {
2454 printk(KERN_ERR
2455 "%s: Hardware doesn't specify base clock "
2456 "frequency.\n", mmc_hostname(mmc));
2457 return -ENODEV;
2458 }
2459 host->max_clk = host->ops->get_max_clock(host);
2460 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002462 host->timeout_clk =
Arindam Nathf2119df2011-05-05 12:18:57 +05302463 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002464 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07002465 if (host->ops->get_timeout_clock) {
2466 host->timeout_clk = host->ops->get_timeout_clock(host);
2467 } else if (!(host->quirks &
2468 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002469 printk(KERN_ERR
2470 "%s: Hardware doesn't specify timeout clock "
2471 "frequency.\n", mmc_hostname(mmc));
2472 return -ENODEV;
2473 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002474 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302475 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002476 host->timeout_clk *= 1000;
2477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002478 /*
2479 * In case of Host Controller v3.00, find out whether clock
2480 * multiplier is supported.
2481 */
2482 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2483 SDHCI_CLOCK_MUL_SHIFT;
2484
2485 /*
2486 * In case the value in Clock Multiplier is 0, then programmable
2487 * clock mode is not supported, otherwise the actual clock
2488 * multiplier is one more than the value of Clock Multiplier
2489 * in the Capabilities Register.
2490 */
2491 if (host->clk_mul)
2492 host->clk_mul += 1;
2493
Pierre Ossmand129bce2006-03-24 03:18:17 -08002494 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302495 * In case of Host Controller v3.00, find out whether clock
2496 * multiplier is supported.
2497 */
2498 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2499 SDHCI_CLOCK_MUL_SHIFT;
2500
2501 /*
2502 * In case the value in Clock Multiplier is 0, then programmable
2503 * clock mode is not supported, otherwise the actual clock
2504 * multiplier is one more than the value of Clock Multiplier
2505 * in the Capabilities Register.
2506 */
2507 if (host->clk_mul)
2508 host->clk_mul += 1;
2509
2510 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002511 * Set host parameters.
2512 */
2513 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302514 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002515 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002516 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302517 else if (host->version >= SDHCI_SPEC_300) {
2518 if (host->clk_mul) {
2519 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2520 mmc->f_max = host->max_clk * host->clk_mul;
2521 } else
2522 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2523 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002524 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002525
Andrei Warkentine89d4562011-05-23 15:06:37 -05002526 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2527
2528 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2529 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002530
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002531 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002532 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002533 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002534 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002535 host->flags |= SDHCI_AUTO_CMD23;
2536 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2537 } else {
2538 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2539 }
2540
Philip Rakity15ec4462010-11-19 16:48:39 -05002541 /*
2542 * A controller may support 8-bit width, but the board itself
2543 * might not have the pins brought out. Boards that support
2544 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2545 * their platform code before calling sdhci_add_host(), and we
2546 * won't assume 8-bit width for hosts without that CAP.
2547 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002548 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002549 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002550
Arindam Nathf2119df2011-05-05 12:18:57 +05302551 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002552 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002553
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002554 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2555 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002556 mmc->caps |= MMC_CAP_NEEDS_POLL;
2557
Al Cooper4188bba2012-03-16 15:54:17 -04002558 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2559 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2560 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302561 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2562
2563 /* SDR104 supports also implies SDR50 support */
2564 if (caps[1] & SDHCI_SUPPORT_SDR104)
2565 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2566 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2567 mmc->caps |= MMC_CAP_UHS_SDR50;
2568
2569 if (caps[1] & SDHCI_SUPPORT_DDR50)
2570 mmc->caps |= MMC_CAP_UHS_DDR50;
2571
Arindam Nathb513ea22011-05-05 12:19:04 +05302572 /* Does the host needs tuning for SDR50? */
2573 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2574 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2575
Arindam Nathd6d50a12011-05-05 12:18:59 +05302576 /* Driver Type(s) (A, C, D) supported by the host */
2577 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2578 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2579 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2580 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2581 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2582 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2583
Tatyana Brokhman9a2adff2012-10-16 08:26:18 +02002584 /* Initial value for re-tuning timer count */
2585 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2586 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2587
2588 /*
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302589 * In case Re-tuning Timer is not disabled, the actual value of
2590 * re-tuning timer will be 2 ^ (n - 1).
2591 */
2592 if (host->tuning_count)
2593 host->tuning_count = 1 << (host->tuning_count - 1);
2594
2595 /* Re-tuning mode supported by the Host Controller */
2596 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2597 SDHCI_RETUNING_MODE_SHIFT;
2598
Takashi Iwai8f230f42010-12-08 10:04:30 +01002599 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302600 /*
2601 * According to SD Host Controller spec v3.00, if the Host System
2602 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2603 * the value is meaningful only if Voltage Support in the Capabilities
2604 * register is set. The actual current value is 4 times the register
2605 * value.
2606 */
2607 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2608
2609 if (caps[0] & SDHCI_CAN_VDD_330) {
2610 int max_current_330;
2611
Takashi Iwai8f230f42010-12-08 10:04:30 +01002612 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302613
2614 max_current_330 = ((max_current_caps &
2615 SDHCI_MAX_CURRENT_330_MASK) >>
2616 SDHCI_MAX_CURRENT_330_SHIFT) *
2617 SDHCI_MAX_CURRENT_MULTIPLIER;
2618
2619 if (max_current_330 > 150)
2620 mmc->caps |= MMC_CAP_SET_XPC_330;
2621 }
2622 if (caps[0] & SDHCI_CAN_VDD_300) {
2623 int max_current_300;
2624
Takashi Iwai8f230f42010-12-08 10:04:30 +01002625 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302626
2627 max_current_300 = ((max_current_caps &
2628 SDHCI_MAX_CURRENT_300_MASK) >>
2629 SDHCI_MAX_CURRENT_300_SHIFT) *
2630 SDHCI_MAX_CURRENT_MULTIPLIER;
2631
2632 if (max_current_300 > 150)
2633 mmc->caps |= MMC_CAP_SET_XPC_300;
2634 }
2635 if (caps[0] & SDHCI_CAN_VDD_180) {
2636 int max_current_180;
2637
Takashi Iwai8f230f42010-12-08 10:04:30 +01002638 ocr_avail |= MMC_VDD_165_195;
2639
Arindam Nathf2119df2011-05-05 12:18:57 +05302640 max_current_180 = ((max_current_caps &
2641 SDHCI_MAX_CURRENT_180_MASK) >>
2642 SDHCI_MAX_CURRENT_180_SHIFT) *
2643 SDHCI_MAX_CURRENT_MULTIPLIER;
2644
2645 if (max_current_180 > 150)
2646 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302647
2648 /* Maximum current capabilities of the host at 1.8V */
2649 if (max_current_180 >= 800)
2650 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2651 else if (max_current_180 >= 600)
2652 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2653 else if (max_current_180 >= 400)
2654 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2655 else
2656 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302657 }
2658
Takashi Iwai8f230f42010-12-08 10:04:30 +01002659 mmc->ocr_avail = ocr_avail;
2660 mmc->ocr_avail_sdio = ocr_avail;
2661 if (host->ocr_avail_sdio)
2662 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2663 mmc->ocr_avail_sd = ocr_avail;
2664 if (host->ocr_avail_sd)
2665 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2666 else /* normal SD controllers don't support 1.8V */
2667 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2668 mmc->ocr_avail_mmc = ocr_avail;
2669 if (host->ocr_avail_mmc)
2670 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002671
2672 if (mmc->ocr_avail == 0) {
2673 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002674 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002675 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002676 }
2677
Pierre Ossmand129bce2006-03-24 03:18:17 -08002678 spin_lock_init(&host->lock);
2679
2680 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002681 * Maximum number of segments. Depends on if the hardware
2682 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002683 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002684 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002685 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002686 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002687 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002688 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002689 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690
2691 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002692 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002693 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002694 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002695 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002696
2697 /*
2698 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002699 * of bytes. When doing hardware scatter/gather, each entry cannot
2700 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002701 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002702 if (host->flags & SDHCI_USE_ADMA) {
2703 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2704 mmc->max_seg_size = 65535;
2705 else
2706 mmc->max_seg_size = 65536;
2707 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002708 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002709 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002710
2711 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002712 * Maximum block size. This varies from controller to controller and
2713 * is specified in the capabilities register.
2714 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002715 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2716 mmc->max_blk_size = 2;
2717 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302718 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002719 SDHCI_MAX_BLOCK_SHIFT;
2720 if (mmc->max_blk_size >= 3) {
2721 printk(KERN_WARNING "%s: Invalid maximum block size, "
2722 "assuming 512 bytes\n", mmc_hostname(mmc));
2723 mmc->max_blk_size = 0;
2724 }
2725 }
2726
2727 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002728
2729 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002730 * Maximum block count.
2731 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002732 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002733
2734 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002735 * Init tasklets.
2736 */
2737 tasklet_init(&host->card_tasklet,
2738 sdhci_tasklet_card, (unsigned long)host);
2739 tasklet_init(&host->finish_tasklet,
2740 sdhci_tasklet_finish, (unsigned long)host);
2741
Al Viroe4cad1b2006-10-10 22:47:07 +01002742 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002743
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302744 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302745 init_waitqueue_head(&host->buf_ready_int);
2746
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302747 /* Initialize re-tuning timer */
2748 init_timer(&host->tuning_timer);
2749 host->tuning_timer.data = (unsigned long)host;
2750 host->tuning_timer.function = sdhci_tuning_timer;
2751 }
2752
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002753 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002754 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002755 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002756 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002757
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002758 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2759 if (IS_ERR(host->vmmc)) {
2760 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2761 host->vmmc = NULL;
2762 } else {
2763 regulator_enable(host->vmmc);
2764 }
2765
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002766 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767
2768#ifdef CONFIG_MMC_DEBUG
2769 sdhci_dumpregs(host);
2770#endif
2771
Pierre Ossmanf9134312008-12-21 17:01:48 +01002772#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002773 snprintf(host->led_name, sizeof(host->led_name),
2774 "%s::", mmc_hostname(mmc));
2775 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002776 host->led.brightness = LED_OFF;
2777 host->led.default_trigger = mmc_hostname(mmc);
2778 host->led.brightness_set = sdhci_led_control;
2779
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002780 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002781 if (ret)
2782 goto reset;
2783#endif
2784
Pierre Ossman5f25a662006-10-04 02:15:39 -07002785 mmiowb();
2786
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787 mmc_add_host(mmc);
2788
Richard Röjforsa13abc72009-09-22 16:45:30 -07002789 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002790 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002791 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2792 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002793
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002794 sdhci_enable_card_detection(host);
2795
Pierre Ossmand129bce2006-03-24 03:18:17 -08002796 return 0;
2797
Pierre Ossmanf9134312008-12-21 17:01:48 +01002798#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002799reset:
2800 sdhci_reset(host, SDHCI_RESET_ALL);
2801 free_irq(host->irq, host);
2802#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002803untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002804 tasklet_kill(&host->card_tasklet);
2805 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
2807 return ret;
2808}
2809
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002810EXPORT_SYMBOL_GPL(sdhci_add_host);
2811
Pierre Ossman1e728592008-04-16 19:13:13 +02002812void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002813{
Pierre Ossman1e728592008-04-16 19:13:13 +02002814 unsigned long flags;
2815
2816 if (dead) {
2817 spin_lock_irqsave(&host->lock, flags);
2818
2819 host->flags |= SDHCI_DEVICE_DEAD;
2820
2821 if (host->mrq) {
2822 printk(KERN_ERR "%s: Controller removed during "
2823 " transfer!\n", mmc_hostname(host->mmc));
2824
2825 host->mrq->cmd->error = -ENOMEDIUM;
2826 tasklet_schedule(&host->finish_tasklet);
2827 }
2828
2829 spin_unlock_irqrestore(&host->lock, flags);
2830 }
2831
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002832 sdhci_disable_card_detection(host);
2833
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002834 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835
Pierre Ossmanf9134312008-12-21 17:01:48 +01002836#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002837 led_classdev_unregister(&host->led);
2838#endif
2839
Pierre Ossman1e728592008-04-16 19:13:13 +02002840 if (!dead)
2841 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
2843 free_irq(host->irq, host);
2844
2845 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302846 if (host->version >= SDHCI_SPEC_300)
2847 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848
2849 tasklet_kill(&host->card_tasklet);
2850 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002851
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002852 if (host->vmmc) {
2853 regulator_disable(host->vmmc);
2854 regulator_put(host->vmmc);
2855 }
2856
Pierre Ossman2134a922008-06-28 18:28:51 +02002857 kfree(host->adma_desc);
2858 kfree(host->align_buffer);
2859
2860 host->adma_desc = NULL;
2861 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862}
2863
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002864EXPORT_SYMBOL_GPL(sdhci_remove_host);
2865
2866void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002867{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002868 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002869}
2870
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002871EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002872
2873/*****************************************************************************\
2874 * *
2875 * Driver init/exit *
2876 * *
2877\*****************************************************************************/
2878
2879static int __init sdhci_drv_init(void)
2880{
2881 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002882 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002883 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2884
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886}
2887
2888static void __exit sdhci_drv_exit(void)
2889{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890}
2891
2892module_init(sdhci_drv_init);
2893module_exit(sdhci_drv_exit);
2894
Pierre Ossmandf673b22006-06-30 02:22:31 -07002895module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002896
Pierre Ossman32710e82009-04-08 20:14:54 +02002897MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002898MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002899MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002900
Pierre Ossmandf673b22006-06-30 02:22:31 -07002901MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");