blob: 4b97d52655262776eec047769b3cb12a65d7788f [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020021#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070022#include <linux/regulator/consumer.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080023
Pierre Ossman2f730fe2008-03-17 10:29:38 +010024#include <linux/leds.h>
25
Aries Lee22113ef2010-12-15 08:14:24 +010026#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080028
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include "sdhci.h"
30
31#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010034 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmanf9134312008-12-21 17:01:48 +010036#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
37 defined(CONFIG_MMC_SDHCI_MODULE))
38#define SDHCI_USE_LEDS_CLASS
39#endif
40
Pierre Ossmandf673b22006-06-30 02:22:31 -070041static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070042
Pierre Ossmand129bce2006-03-24 03:18:17 -080043static void sdhci_finish_data(struct sdhci_host *);
44
45static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46static void sdhci_finish_command(struct sdhci_host *);
47
48static void sdhci_dumpregs(struct sdhci_host *host)
49{
Philip Rakity412ab652010-09-22 15:25:13 -070050 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
51 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readl(host, SDHCI_DMA_ADDRESS),
55 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readw(host, SDHCI_BLOCK_SIZE),
58 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_ARGUMENT),
61 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readl(host, SDHCI_PRESENT_STATE),
64 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_POWER_CONTROL),
67 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
70 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
73 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_INT_ENABLE),
76 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_ACMD12_ERR),
79 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050080 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050082 sdhci_readl(host, SDHCI_CAPABILITIES_1));
83 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
84 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010087 if (host->flags & SDHCI_USE_ADMA)
88 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
89 readl(host->ioaddr + SDHCI_ADMA_ERROR),
90 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
91
Pierre Ossmand129bce2006-03-24 03:18:17 -080092 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
93}
94
95/*****************************************************************************\
96 * *
97 * Low level functions *
98 * *
99\*****************************************************************************/
100
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300101static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
102{
103 u32 ier;
104
105 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
106 ier &= ~clear;
107 ier |= set;
108 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
109 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
110}
111
112static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
113{
114 sdhci_clear_set_irqs(host, 0, irqs);
115}
116
117static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
118{
119 sdhci_clear_set_irqs(host, irqs, 0);
120}
121
122static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
123{
124 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
125
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300126 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
127 return;
128
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300129 if (enable)
130 sdhci_unmask_irqs(host, irqs);
131 else
132 sdhci_mask_irqs(host, irqs);
133}
134
135static void sdhci_enable_card_detection(struct sdhci_host *host)
136{
137 sdhci_set_card_detection(host, true);
138}
139
140static void sdhci_disable_card_detection(struct sdhci_host *host)
141{
142 sdhci_set_card_detection(host, false);
143}
144
Pierre Ossmand129bce2006-03-24 03:18:17 -0800145static void sdhci_reset(struct sdhci_host *host, u8 mask)
146{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700147 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300148 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700149
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100150 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300151 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700152 SDHCI_CARD_PRESENT))
153 return;
154 }
155
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300156 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
157 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
158
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300159 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800160
Pierre Ossmane16514d82006-06-30 02:22:24 -0700161 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800162 host->clock = 0;
163
Pierre Ossmane16514d82006-06-30 02:22:24 -0700164 /* Wait max 100 ms */
165 timeout = 100;
166
167 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300168 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700169 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100170 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700171 mmc_hostname(host->mmc), (int)mask);
172 sdhci_dumpregs(host);
173 return;
174 }
175 timeout--;
176 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300178
179 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
180 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181}
182
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800183static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
184
185static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800187 if (soft)
188 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
189 else
190 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300192 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
193 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700194 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
195 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300196 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800197
198 if (soft) {
199 /* force clock reconfiguration */
200 host->clock = 0;
201 sdhci_set_ios(host->mmc, &host->mmc->ios);
202 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300203}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800204
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300205static void sdhci_reinit(struct sdhci_host *host)
206{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800207 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300208 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800209}
210
211static void sdhci_activate_led(struct sdhci_host *host)
212{
213 u8 ctrl;
214
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300215 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300217 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800218}
219
220static void sdhci_deactivate_led(struct sdhci_host *host)
221{
222 u8 ctrl;
223
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300224 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300226 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227}
228
Pierre Ossmanf9134312008-12-21 17:01:48 +0100229#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100230static void sdhci_led_control(struct led_classdev *led,
231 enum led_brightness brightness)
232{
233 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
234 unsigned long flags;
235
236 spin_lock_irqsave(&host->lock, flags);
237
238 if (brightness == LED_OFF)
239 sdhci_deactivate_led(host);
240 else
241 sdhci_activate_led(host);
242
243 spin_unlock_irqrestore(&host->lock, flags);
244}
245#endif
246
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247/*****************************************************************************\
248 * *
249 * Core functions *
250 * *
251\*****************************************************************************/
252
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100253static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254{
Pierre Ossman76591502008-07-21 00:32:11 +0200255 unsigned long flags;
256 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700257 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200258 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100260 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100262 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200263 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264
Pierre Ossman76591502008-07-21 00:32:11 +0200265 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100267 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200268 if (!sg_miter_next(&host->sg_miter))
269 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270
Pierre Ossman76591502008-07-21 00:32:11 +0200271 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272
Pierre Ossman76591502008-07-21 00:32:11 +0200273 blksize -= len;
274 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200275
Pierre Ossman76591502008-07-21 00:32:11 +0200276 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277
Pierre Ossman76591502008-07-21 00:32:11 +0200278 while (len) {
279 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300280 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200281 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282 }
Pierre Ossman76591502008-07-21 00:32:11 +0200283
284 *buf = scratch & 0xFF;
285
286 buf++;
287 scratch >>= 8;
288 chunk--;
289 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800290 }
291 }
Pierre Ossman76591502008-07-21 00:32:11 +0200292
293 sg_miter_stop(&host->sg_miter);
294
295 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100296}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800297
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100298static void sdhci_write_block_pio(struct sdhci_host *host)
299{
Pierre Ossman76591502008-07-21 00:32:11 +0200300 unsigned long flags;
301 size_t blksize, len, chunk;
302 u32 scratch;
303 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304
305 DBG("PIO writing\n");
306
307 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200308 chunk = 0;
309 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312
313 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200314 if (!sg_miter_next(&host->sg_miter))
315 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 blksize -= len;
320 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 while (len) {
325 scratch |= (u32)*buf << (chunk * 8);
326
327 buf++;
328 chunk++;
329 len--;
330
331 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300332 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200333 chunk = 0;
334 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100335 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336 }
337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 sg_miter_stop(&host->sg_miter);
340
341 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342}
343
344static void sdhci_transfer_pio(struct sdhci_host *host)
345{
346 u32 mask;
347
348 BUG_ON(!host->data);
349
Pierre Ossman76591502008-07-21 00:32:11 +0200350 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351 return;
352
353 if (host->data->flags & MMC_DATA_READ)
354 mask = SDHCI_DATA_AVAILABLE;
355 else
356 mask = SDHCI_SPACE_AVAILABLE;
357
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200358 /*
359 * Some controllers (JMicron JMB38x) mess up the buffer bits
360 * for transfers < 4 bytes. As long as it is just one block,
361 * we can ignore the bits.
362 */
363 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
364 (host->data->blocks == 1))
365 mask = ~0;
366
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300367 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300368 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
369 udelay(100);
370
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371 if (host->data->flags & MMC_DATA_READ)
372 sdhci_read_block_pio(host);
373 else
374 sdhci_write_block_pio(host);
375
Pierre Ossman76591502008-07-21 00:32:11 +0200376 host->blocks--;
377 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 }
380
381 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800382}
383
Pierre Ossman2134a922008-06-28 18:28:51 +0200384static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
385{
386 local_irq_save(*flags);
387 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
388}
389
390static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
391{
392 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
393 local_irq_restore(*flags);
394}
395
Ben Dooks118cd172010-03-05 13:43:26 -0800396static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
397{
Ben Dooks9e506f32010-03-05 13:43:29 -0800398 __le32 *dataddr = (__le32 __force *)(desc + 4);
399 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800400
Ben Dooks9e506f32010-03-05 13:43:29 -0800401 /* SDHCI specification says ADMA descriptors should be 4 byte
402 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800403
Ben Dooks9e506f32010-03-05 13:43:29 -0800404 cmdlen[0] = cpu_to_le16(cmd);
405 cmdlen[1] = cpu_to_le16(len);
406
407 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800408}
409
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200410static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200411 struct mmc_data *data)
412{
413 int direction;
414
415 u8 *desc;
416 u8 *align;
417 dma_addr_t addr;
418 dma_addr_t align_addr;
419 int len, offset;
420
421 struct scatterlist *sg;
422 int i;
423 char *buffer;
424 unsigned long flags;
425
426 /*
427 * The spec does not specify endianness of descriptor table.
428 * We currently guess that it is LE.
429 */
430
431 if (data->flags & MMC_DATA_READ)
432 direction = DMA_FROM_DEVICE;
433 else
434 direction = DMA_TO_DEVICE;
435
436 /*
437 * The ADMA descriptor table is mapped further down as we
438 * need to fill it with data first.
439 */
440
441 host->align_addr = dma_map_single(mmc_dev(host->mmc),
442 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700443 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200444 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200445 BUG_ON(host->align_addr & 0x3);
446
447 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
448 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200449 if (host->sg_count == 0)
450 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200451
452 desc = host->adma_desc;
453 align = host->align_buffer;
454
455 align_addr = host->align_addr;
456
457 for_each_sg(data->sg, sg, host->sg_count, i) {
458 addr = sg_dma_address(sg);
459 len = sg_dma_len(sg);
460
461 /*
462 * The SDHCI specification states that ADMA
463 * addresses must be 32-bit aligned. If they
464 * aren't, then we use a bounce buffer for
465 * the (up to three) bytes that screw up the
466 * alignment.
467 */
468 offset = (4 - (addr & 0x3)) & 0x3;
469 if (offset) {
470 if (data->flags & MMC_DATA_WRITE) {
471 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200472 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200473 memcpy(align, buffer, offset);
474 sdhci_kunmap_atomic(buffer, &flags);
475 }
476
Ben Dooks118cd172010-03-05 13:43:26 -0800477 /* tran, valid */
478 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200479
480 BUG_ON(offset > 65536);
481
Pierre Ossman2134a922008-06-28 18:28:51 +0200482 align += 4;
483 align_addr += 4;
484
485 desc += 8;
486
487 addr += offset;
488 len -= offset;
489 }
490
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 BUG_ON(len > 65536);
492
Ben Dooks118cd172010-03-05 13:43:26 -0800493 /* tran, valid */
494 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200495 desc += 8;
496
497 /*
498 * If this triggers then we have a calculation bug
499 * somewhere. :/
500 */
501 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
502 }
503
Thomas Abraham70764a92010-05-26 14:42:04 -0700504 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
505 /*
506 * Mark the last descriptor as the terminating descriptor
507 */
508 if (desc != host->adma_desc) {
509 desc -= 8;
510 desc[0] |= 0x2; /* end */
511 }
512 } else {
513 /*
514 * Add a terminating entry.
515 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
Thomas Abraham70764a92010-05-26 14:42:04 -0700517 /* nop, end, valid */
518 sdhci_set_adma_desc(desc, 0, 0, 0x3);
519 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200520
521 /*
522 * Resync align buffer as we might have changed it.
523 */
524 if (data->flags & MMC_DATA_WRITE) {
525 dma_sync_single_for_device(mmc_dev(host->mmc),
526 host->align_addr, 128 * 4, direction);
527 }
528
529 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
530 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200531 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200532 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200534
535 return 0;
536
537unmap_entries:
538 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
539 data->sg_len, direction);
540unmap_align:
541 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
542 128 * 4, direction);
543fail:
544 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545}
546
547static void sdhci_adma_table_post(struct sdhci_host *host,
548 struct mmc_data *data)
549{
550 int direction;
551
552 struct scatterlist *sg;
553 int i, size;
554 u8 *align;
555 char *buffer;
556 unsigned long flags;
557
558 if (data->flags & MMC_DATA_READ)
559 direction = DMA_FROM_DEVICE;
560 else
561 direction = DMA_TO_DEVICE;
562
563 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
564 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
565
566 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
567 128 * 4, direction);
568
569 if (data->flags & MMC_DATA_READ) {
570 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
571 data->sg_len, direction);
572
573 align = host->align_buffer;
574
575 for_each_sg(data->sg, sg, host->sg_count, i) {
576 if (sg_dma_address(sg) & 0x3) {
577 size = 4 - (sg_dma_address(sg) & 0x3);
578
579 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200580 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200581 memcpy(buffer, align, size);
582 sdhci_kunmap_atomic(buffer, &flags);
583
584 align += 4;
585 }
586 }
587 }
588
589 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
590 data->sg_len, direction);
591}
592
Andrei Warkentina3c77782011-04-11 16:13:42 -0500593static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800594{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700595 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500596 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700597 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800598
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200599 /*
600 * If the host controller provides us with an incorrect timeout
601 * value, just skip the check and use 0xE. The hardware may take
602 * longer to time out, but that's much better than having a too-short
603 * timeout value.
604 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200605 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200606 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200607
Andrei Warkentina3c77782011-04-11 16:13:42 -0500608 /* Unspecified timeout, assume max */
609 if (!data && !cmd->cmd_timeout_ms)
610 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800611
Andrei Warkentina3c77782011-04-11 16:13:42 -0500612 /* timeout in us */
613 if (!data)
614 target_timeout = cmd->cmd_timeout_ms * 1000;
615 else
616 target_timeout = data->timeout_ns / 1000 +
617 data->timeout_clks / host->clock;
Anton Vorontsov81b39802009-09-22 16:45:13 -0700618
Mark Brown4b016812011-04-19 18:44:17 +0100619 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
620 host->timeout_clk = host->clock / 1000;
621
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700622 /*
623 * Figure out needed cycles.
624 * We do this in steps in order to fit inside a 32 bit int.
625 * The first step is the minimum timeout, which will have a
626 * minimum resolution of 6 bits:
627 * (1) 2^13*1000 > 2^22,
628 * (2) host->timeout_clk < 2^16
629 * =>
630 * (1) / (2) > 2^6
631 */
Mark Brown4b016812011-04-19 18:44:17 +0100632 BUG_ON(!host->timeout_clk);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700633 count = 0;
634 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
635 while (current_timeout < target_timeout) {
636 count++;
637 current_timeout <<= 1;
638 if (count >= 0xF)
639 break;
640 }
641
642 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500643 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
644 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700645 count = 0xE;
646 }
647
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200648 return count;
649}
650
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300651static void sdhci_set_transfer_irqs(struct sdhci_host *host)
652{
653 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
654 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
655
656 if (host->flags & SDHCI_REQ_USE_DMA)
657 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
658 else
659 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
660}
661
Andrei Warkentina3c77782011-04-11 16:13:42 -0500662static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200663{
664 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200665 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500666 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200667 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200668
669 WARN_ON(host->data);
670
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 if (data || (cmd->flags & MMC_RSP_BUSY)) {
672 count = sdhci_calc_timeout(host, cmd);
673 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
674 }
675
676 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200677 return;
678
679 /* Sanity checks */
680 BUG_ON(data->blksz * data->blocks > 524288);
681 BUG_ON(data->blksz > host->mmc->max_blk_size);
682 BUG_ON(data->blocks > 65535);
683
684 host->data = data;
685 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400686 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200687
Richard Röjforsa13abc72009-09-22 16:45:30 -0700688 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100689 host->flags |= SDHCI_REQ_USE_DMA;
690
Pierre Ossman2134a922008-06-28 18:28:51 +0200691 /*
692 * FIXME: This doesn't account for merging when mapping the
693 * scatterlist.
694 */
695 if (host->flags & SDHCI_REQ_USE_DMA) {
696 int broken, i;
697 struct scatterlist *sg;
698
699 broken = 0;
700 if (host->flags & SDHCI_USE_ADMA) {
701 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
702 broken = 1;
703 } else {
704 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
705 broken = 1;
706 }
707
708 if (unlikely(broken)) {
709 for_each_sg(data->sg, sg, data->sg_len, i) {
710 if (sg->length & 0x3) {
711 DBG("Reverting to PIO because of "
712 "transfer size (%d)\n",
713 sg->length);
714 host->flags &= ~SDHCI_REQ_USE_DMA;
715 break;
716 }
717 }
718 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100719 }
720
721 /*
722 * The assumption here being that alignment is the same after
723 * translation to device address space.
724 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200725 if (host->flags & SDHCI_REQ_USE_DMA) {
726 int broken, i;
727 struct scatterlist *sg;
728
729 broken = 0;
730 if (host->flags & SDHCI_USE_ADMA) {
731 /*
732 * As we use 3 byte chunks to work around
733 * alignment problems, we need to check this
734 * quirk.
735 */
736 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
737 broken = 1;
738 } else {
739 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
740 broken = 1;
741 }
742
743 if (unlikely(broken)) {
744 for_each_sg(data->sg, sg, data->sg_len, i) {
745 if (sg->offset & 0x3) {
746 DBG("Reverting to PIO because of "
747 "bad alignment\n");
748 host->flags &= ~SDHCI_REQ_USE_DMA;
749 break;
750 }
751 }
752 }
753 }
754
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755 if (host->flags & SDHCI_REQ_USE_DMA) {
756 if (host->flags & SDHCI_USE_ADMA) {
757 ret = sdhci_adma_table_pre(host, data);
758 if (ret) {
759 /*
760 * This only happens when someone fed
761 * us an invalid request.
762 */
763 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200764 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200765 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300766 sdhci_writel(host, host->adma_addr,
767 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200768 }
769 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300770 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200771
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300772 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200773 data->sg, data->sg_len,
774 (data->flags & MMC_DATA_READ) ?
775 DMA_FROM_DEVICE :
776 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300777 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200778 /*
779 * This only happens when someone fed
780 * us an invalid request.
781 */
782 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200783 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200784 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200785 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300786 sdhci_writel(host, sg_dma_address(data->sg),
787 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200788 }
789 }
790 }
791
Pierre Ossman2134a922008-06-28 18:28:51 +0200792 /*
793 * Always adjust the DMA selection as some controllers
794 * (e.g. JMicron) can't do PIO properly when the selection
795 * is ADMA.
796 */
797 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300798 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200799 ctrl &= ~SDHCI_CTRL_DMA_MASK;
800 if ((host->flags & SDHCI_REQ_USE_DMA) &&
801 (host->flags & SDHCI_USE_ADMA))
802 ctrl |= SDHCI_CTRL_ADMA32;
803 else
804 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300805 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100806 }
807
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200808 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200809 int flags;
810
811 flags = SG_MITER_ATOMIC;
812 if (host->data->flags & MMC_DATA_READ)
813 flags |= SG_MITER_TO_SG;
814 else
815 flags |= SG_MITER_FROM_SG;
816 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200817 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800818 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700819
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300820 sdhci_set_transfer_irqs(host);
821
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400822 /* Set the DMA boundary value and block size */
823 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
824 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300825 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700826}
827
828static void sdhci_set_transfer_mode(struct sdhci_host *host,
829 struct mmc_data *data)
830{
831 u16 mode;
832
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700833 if (data == NULL)
834 return;
835
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200836 WARN_ON(!host->data);
837
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700838 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700839 if (data->blocks > 1) {
840 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
841 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
842 else
843 mode |= SDHCI_TRNS_MULTI;
844 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700845 if (data->flags & MMC_DATA_READ)
846 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100847 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700848 mode |= SDHCI_TRNS_DMA;
849
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300850 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800851}
852
853static void sdhci_finish_data(struct sdhci_host *host)
854{
855 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800856
857 BUG_ON(!host->data);
858
859 data = host->data;
860 host->data = NULL;
861
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100862 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200863 if (host->flags & SDHCI_USE_ADMA)
864 sdhci_adma_table_post(host, data);
865 else {
866 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
867 data->sg_len, (data->flags & MMC_DATA_READ) ?
868 DMA_FROM_DEVICE : DMA_TO_DEVICE);
869 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800870 }
871
872 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200873 * The specification states that the block count register must
874 * be updated, but it does not specify at what point in the
875 * data flow. That makes the register entirely useless to read
876 * back so we have to assume that nothing made it to the card
877 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800878 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200879 if (data->error)
880 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800881 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200882 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 if (data->stop) {
885 /*
886 * The controller needs a reset of internal state machines
887 * upon error conditions.
888 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200889 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890 sdhci_reset(host, SDHCI_RESET_CMD);
891 sdhci_reset(host, SDHCI_RESET_DATA);
892 }
893
894 sdhci_send_command(host, data->stop);
895 } else
896 tasklet_schedule(&host->finish_tasklet);
897}
898
899static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
900{
901 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700902 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700903 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800904
905 WARN_ON(host->cmd);
906
Pierre Ossmand129bce2006-03-24 03:18:17 -0800907 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700908 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700909
910 mask = SDHCI_CMD_INHIBIT;
911 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
912 mask |= SDHCI_DATA_INHIBIT;
913
914 /* We shouldn't wait for data inihibit for stop commands, even
915 though they might use busy signaling */
916 if (host->mrq->data && (cmd == host->mrq->data->stop))
917 mask &= ~SDHCI_DATA_INHIBIT;
918
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300919 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700920 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800921 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100922 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800923 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200924 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 tasklet_schedule(&host->finish_tasklet);
926 return;
927 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700928 timeout--;
929 mdelay(1);
930 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931
932 mod_timer(&host->timer, jiffies + 10 * HZ);
933
934 host->cmd = cmd;
935
Andrei Warkentina3c77782011-04-11 16:13:42 -0500936 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300938 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700940 sdhci_set_transfer_mode(host, cmd->data);
941
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100943 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200945 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 tasklet_schedule(&host->finish_tasklet);
947 return;
948 }
949
950 if (!(cmd->flags & MMC_RSP_PRESENT))
951 flags = SDHCI_CMD_RESP_NONE;
952 else if (cmd->flags & MMC_RSP_136)
953 flags = SDHCI_CMD_RESP_LONG;
954 else if (cmd->flags & MMC_RSP_BUSY)
955 flags = SDHCI_CMD_RESP_SHORT_BUSY;
956 else
957 flags = SDHCI_CMD_RESP_SHORT;
958
959 if (cmd->flags & MMC_RSP_CRC)
960 flags |= SDHCI_CMD_CRC;
961 if (cmd->flags & MMC_RSP_OPCODE)
962 flags |= SDHCI_CMD_INDEX;
963 if (cmd->data)
964 flags |= SDHCI_CMD_DATA;
965
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300966 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967}
968
969static void sdhci_finish_command(struct sdhci_host *host)
970{
971 int i;
972
973 BUG_ON(host->cmd == NULL);
974
975 if (host->cmd->flags & MMC_RSP_PRESENT) {
976 if (host->cmd->flags & MMC_RSP_136) {
977 /* CRC is stripped so we need to do some shifting. */
978 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300979 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 SDHCI_RESPONSE + (3-i)*4) << 8;
981 if (i != 3)
982 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300983 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 SDHCI_RESPONSE + (3-i)*4-1);
985 }
986 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300987 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 }
989 }
990
Pierre Ossman17b04292007-07-22 22:18:46 +0200991 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200993 if (host->data && host->data_early)
994 sdhci_finish_data(host);
995
996 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 tasklet_schedule(&host->finish_tasklet);
998
999 host->cmd = NULL;
1000}
1001
1002static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1003{
1004 int div;
1005 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001006 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007
1008 if (clock == host->clock)
1009 return;
1010
Anton Vorontsov81146342009-03-17 00:13:59 +03001011 if (host->ops->set_clock) {
1012 host->ops->set_clock(host, clock);
1013 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1014 return;
1015 }
1016
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001017 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018
1019 if (clock == 0)
1020 goto out;
1021
Zhangfei Gao85105c52010-08-06 07:10:01 +08001022 if (host->version >= SDHCI_SPEC_300) {
1023 /* Version 3.00 divisors must be a multiple of 2. */
1024 if (host->max_clk <= clock)
1025 div = 1;
1026 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001027 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001028 if ((host->max_clk / div) <= clock)
1029 break;
1030 }
1031 }
1032 } else {
1033 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001034 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001035 if ((host->max_clk / div) <= clock)
1036 break;
1037 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038 }
1039 div >>= 1;
1040
Zhangfei Gao85105c52010-08-06 07:10:01 +08001041 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1042 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1043 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001045 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
Chris Ball27f6cb12009-09-22 16:45:31 -07001047 /* Wait max 20 ms */
1048 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001049 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001050 & SDHCI_CLOCK_INT_STABLE)) {
1051 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001052 printk(KERN_ERR "%s: Internal clock never "
1053 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 sdhci_dumpregs(host);
1055 return;
1056 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001057 timeout--;
1058 mdelay(1);
1059 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060
1061 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001062 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063
1064out:
1065 host->clock = clock;
1066}
1067
Pierre Ossman146ad662006-06-30 02:22:23 -07001068static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1069{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001070 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001071
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001072 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001073 switch (1 << power) {
1074 case MMC_VDD_165_195:
1075 pwr = SDHCI_POWER_180;
1076 break;
1077 case MMC_VDD_29_30:
1078 case MMC_VDD_30_31:
1079 pwr = SDHCI_POWER_300;
1080 break;
1081 case MMC_VDD_32_33:
1082 case MMC_VDD_33_34:
1083 pwr = SDHCI_POWER_330;
1084 break;
1085 default:
1086 BUG();
1087 }
1088 }
1089
1090 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001091 return;
1092
Pierre Ossmanae628902009-05-03 20:45:03 +02001093 host->pwr = pwr;
1094
1095 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001096 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001097 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001098 }
1099
1100 /*
1101 * Spec says that we should clear the power reg before setting
1102 * a new value. Some controllers don't seem to like this though.
1103 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001104 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001105 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001106
Andres Salomone08c1692008-07-04 10:00:03 -07001107 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001108 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001109 * and set turn on power at the same time, so set the voltage first.
1110 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001111 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001112 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1113
1114 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001115
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001116 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001117
1118 /*
1119 * Some controllers need an extra 10ms delay of 10ms before they
1120 * can apply clock after applying power
1121 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001122 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001123 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001124}
1125
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126/*****************************************************************************\
1127 * *
1128 * MMC callbacks *
1129 * *
1130\*****************************************************************************/
1131
1132static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1133{
1134 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001135 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136 unsigned long flags;
1137
1138 host = mmc_priv(mmc);
1139
1140 spin_lock_irqsave(&host->lock, flags);
1141
1142 WARN_ON(host->mrq != NULL);
1143
Pierre Ossmanf9134312008-12-21 17:01:48 +01001144#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001146#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001147 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1148 if (mrq->stop) {
1149 mrq->data->stop = NULL;
1150 mrq->stop = NULL;
1151 }
1152 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153
1154 host->mrq = mrq;
1155
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001156 /* If polling, assume that the card is always present. */
1157 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1158 present = true;
1159 else
1160 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1161 SDHCI_CARD_PRESENT;
1162
1163 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001164 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165 tasklet_schedule(&host->finish_tasklet);
1166 } else
1167 sdhci_send_command(host, mrq->cmd);
1168
Pierre Ossman5f25a662006-10-04 02:15:39 -07001169 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170 spin_unlock_irqrestore(&host->lock, flags);
1171}
1172
1173static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1174{
1175 struct sdhci_host *host;
1176 unsigned long flags;
1177 u8 ctrl;
1178
1179 host = mmc_priv(mmc);
1180
1181 spin_lock_irqsave(&host->lock, flags);
1182
Pierre Ossman1e728592008-04-16 19:13:13 +02001183 if (host->flags & SDHCI_DEVICE_DEAD)
1184 goto out;
1185
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 /*
1187 * Reset the chip on each power off.
1188 * Should clear out any weird states.
1189 */
1190 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001191 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001192 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193 }
1194
1195 sdhci_set_clock(host, ios->clock);
1196
1197 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001198 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001200 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201
Philip Rakity643a81f2010-09-23 08:24:32 -07001202 if (host->ops->platform_send_init_74_clocks)
1203 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1204
Philip Rakity15ec4462010-11-19 16:48:39 -05001205 /*
1206 * If your platform has 8-bit width support but is not a v3 controller,
1207 * or if it requires special setup code, you should implement that in
1208 * platform_8bit_width().
1209 */
1210 if (host->ops->platform_8bit_width)
1211 host->ops->platform_8bit_width(host, ios->bus_width);
1212 else {
1213 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1214 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1215 ctrl &= ~SDHCI_CTRL_4BITBUS;
1216 if (host->version >= SDHCI_SPEC_300)
1217 ctrl |= SDHCI_CTRL_8BITBUS;
1218 } else {
1219 if (host->version >= SDHCI_SPEC_300)
1220 ctrl &= ~SDHCI_CTRL_8BITBUS;
1221 if (ios->bus_width == MMC_BUS_WIDTH_4)
1222 ctrl |= SDHCI_CTRL_4BITBUS;
1223 else
1224 ctrl &= ~SDHCI_CTRL_4BITBUS;
1225 }
1226 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1227 }
1228
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001229 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001230
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001231 if ((ios->timing == MMC_TIMING_SD_HS ||
1232 ios->timing == MMC_TIMING_MMC_HS)
1233 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001234 ctrl |= SDHCI_CTRL_HISPD;
1235 else
1236 ctrl &= ~SDHCI_CTRL_HISPD;
1237
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239
Leandro Dorileob8352262007-07-25 23:47:04 +02001240 /*
1241 * Some (ENE) controllers go apeshit on some ios operation,
1242 * signalling timeout and CRC errors even on CMD0. Resetting
1243 * it on each ios seems to solve the problem.
1244 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001245 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001246 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1247
Pierre Ossman1e728592008-04-16 19:13:13 +02001248out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001249 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250 spin_unlock_irqrestore(&host->lock, flags);
1251}
1252
1253static int sdhci_get_ro(struct mmc_host *mmc)
1254{
1255 struct sdhci_host *host;
1256 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001257 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258
1259 host = mmc_priv(mmc);
1260
1261 spin_lock_irqsave(&host->lock, flags);
1262
Pierre Ossman1e728592008-04-16 19:13:13 +02001263 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001264 is_readonly = 0;
1265 else if (host->ops->get_ro)
1266 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001267 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001268 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1269 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270
1271 spin_unlock_irqrestore(&host->lock, flags);
1272
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001273 /* This quirk needs to be replaced by a callback-function later */
1274 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1275 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276}
1277
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001278static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1279{
1280 struct sdhci_host *host;
1281 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001282
1283 host = mmc_priv(mmc);
1284
1285 spin_lock_irqsave(&host->lock, flags);
1286
Pierre Ossman1e728592008-04-16 19:13:13 +02001287 if (host->flags & SDHCI_DEVICE_DEAD)
1288 goto out;
1289
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001290 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001291 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1292 else
1293 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001294out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001295 mmiowb();
1296
1297 spin_unlock_irqrestore(&host->lock, flags);
1298}
1299
David Brownellab7aefd2006-11-12 17:55:30 -08001300static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001301 .request = sdhci_request,
1302 .set_ios = sdhci_set_ios,
1303 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001304 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001305};
1306
1307/*****************************************************************************\
1308 * *
1309 * Tasklets *
1310 * *
1311\*****************************************************************************/
1312
1313static void sdhci_tasklet_card(unsigned long param)
1314{
1315 struct sdhci_host *host;
1316 unsigned long flags;
1317
1318 host = (struct sdhci_host*)param;
1319
1320 spin_lock_irqsave(&host->lock, flags);
1321
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001322 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001323 if (host->mrq) {
1324 printk(KERN_ERR "%s: Card removed during transfer!\n",
1325 mmc_hostname(host->mmc));
1326 printk(KERN_ERR "%s: Resetting controller.\n",
1327 mmc_hostname(host->mmc));
1328
1329 sdhci_reset(host, SDHCI_RESET_CMD);
1330 sdhci_reset(host, SDHCI_RESET_DATA);
1331
Pierre Ossman17b04292007-07-22 22:18:46 +02001332 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333 tasklet_schedule(&host->finish_tasklet);
1334 }
1335 }
1336
1337 spin_unlock_irqrestore(&host->lock, flags);
1338
Pierre Ossman04cf5852008-08-18 22:18:14 +02001339 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340}
1341
1342static void sdhci_tasklet_finish(unsigned long param)
1343{
1344 struct sdhci_host *host;
1345 unsigned long flags;
1346 struct mmc_request *mrq;
1347
1348 host = (struct sdhci_host*)param;
1349
Chris Ball0c9c99a2011-04-27 17:35:31 -04001350 /*
1351 * If this tasklet gets rescheduled while running, it will
1352 * be run again afterwards but without any active request.
1353 */
1354 if (!host->mrq)
1355 return;
1356
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357 spin_lock_irqsave(&host->lock, flags);
1358
1359 del_timer(&host->timer);
1360
1361 mrq = host->mrq;
1362
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363 /*
1364 * The controller needs a reset of internal state machines
1365 * upon error conditions.
1366 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001367 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001368 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001369 (mrq->data && (mrq->data->error ||
1370 (mrq->data->stop && mrq->data->stop->error))) ||
1371 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001372
1373 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001374 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001375 unsigned int clock;
1376
1377 /* This is to force an update */
1378 clock = host->clock;
1379 host->clock = 0;
1380 sdhci_set_clock(host, clock);
1381 }
1382
1383 /* Spec says we should do both at the same time, but Ricoh
1384 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 sdhci_reset(host, SDHCI_RESET_CMD);
1386 sdhci_reset(host, SDHCI_RESET_DATA);
1387 }
1388
1389 host->mrq = NULL;
1390 host->cmd = NULL;
1391 host->data = NULL;
1392
Pierre Ossmanf9134312008-12-21 17:01:48 +01001393#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001394 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001395#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396
Pierre Ossman5f25a662006-10-04 02:15:39 -07001397 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001398 spin_unlock_irqrestore(&host->lock, flags);
1399
1400 mmc_request_done(host->mmc, mrq);
1401}
1402
1403static void sdhci_timeout_timer(unsigned long data)
1404{
1405 struct sdhci_host *host;
1406 unsigned long flags;
1407
1408 host = (struct sdhci_host*)data;
1409
1410 spin_lock_irqsave(&host->lock, flags);
1411
1412 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001413 printk(KERN_ERR "%s: Timeout waiting for hardware "
1414 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001415 sdhci_dumpregs(host);
1416
1417 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001418 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001419 sdhci_finish_data(host);
1420 } else {
1421 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001422 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001423 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001424 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425
1426 tasklet_schedule(&host->finish_tasklet);
1427 }
1428 }
1429
Pierre Ossman5f25a662006-10-04 02:15:39 -07001430 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431 spin_unlock_irqrestore(&host->lock, flags);
1432}
1433
1434/*****************************************************************************\
1435 * *
1436 * Interrupt handling *
1437 * *
1438\*****************************************************************************/
1439
1440static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1441{
1442 BUG_ON(intmask == 0);
1443
1444 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001445 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1446 "though no command operation was in progress.\n",
1447 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448 sdhci_dumpregs(host);
1449 return;
1450 }
1451
Pierre Ossman43b58b32007-07-25 23:15:27 +02001452 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001453 host->cmd->error = -ETIMEDOUT;
1454 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1455 SDHCI_INT_INDEX))
1456 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457
Pierre Ossmane8095172008-07-25 01:09:08 +02001458 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001459 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001460 return;
1461 }
1462
1463 /*
1464 * The host can send and interrupt when the busy state has
1465 * ended, allowing us to wait without wasting CPU cycles.
1466 * Unfortunately this is overloaded on the "data complete"
1467 * interrupt, so we need to take some care when handling
1468 * it.
1469 *
1470 * Note: The 1.0 specification is a bit ambiguous about this
1471 * feature so there might be some problems with older
1472 * controllers.
1473 */
1474 if (host->cmd->flags & MMC_RSP_BUSY) {
1475 if (host->cmd->data)
1476 DBG("Cannot wait for busy signal when also "
1477 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001478 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001479 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001480
1481 /* The controller does not support the end-of-busy IRQ,
1482 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001483 }
1484
1485 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001486 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487}
1488
George G. Davis0957c332010-02-18 12:32:12 -05001489#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001490static void sdhci_show_adma_error(struct sdhci_host *host)
1491{
1492 const char *name = mmc_hostname(host->mmc);
1493 u8 *desc = host->adma_desc;
1494 __le32 *dma;
1495 __le16 *len;
1496 u8 attr;
1497
1498 sdhci_dumpregs(host);
1499
1500 while (true) {
1501 dma = (__le32 *)(desc + 4);
1502 len = (__le16 *)(desc + 2);
1503 attr = *desc;
1504
1505 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1506 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1507
1508 desc += 8;
1509
1510 if (attr & 2)
1511 break;
1512 }
1513}
1514#else
1515static void sdhci_show_adma_error(struct sdhci_host *host) { }
1516#endif
1517
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1519{
1520 BUG_ON(intmask == 0);
1521
1522 if (!host->data) {
1523 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001524 * The "data complete" interrupt is also used to
1525 * indicate that a busy state has ended. See comment
1526 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001527 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001528 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1529 if (intmask & SDHCI_INT_DATA_END) {
1530 sdhci_finish_command(host);
1531 return;
1532 }
1533 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001534
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001535 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1536 "though no data operation was in progress.\n",
1537 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538 sdhci_dumpregs(host);
1539
1540 return;
1541 }
1542
1543 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001544 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001545 else if (intmask & SDHCI_INT_DATA_END_BIT)
1546 host->data->error = -EILSEQ;
1547 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1548 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1549 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001550 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001551 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1552 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1553 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001554 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001555 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001556
Pierre Ossman17b04292007-07-22 22:18:46 +02001557 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001558 sdhci_finish_data(host);
1559 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001560 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001561 sdhci_transfer_pio(host);
1562
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001563 /*
1564 * We currently don't do anything fancy with DMA
1565 * boundaries, but as we can't disable the feature
1566 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001567 *
1568 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
1569 * should return a valid address to continue from, but as
1570 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001571 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001572 if (intmask & SDHCI_INT_DMA_END) {
1573 u32 dmastart, dmanow;
1574 dmastart = sg_dma_address(host->data->sg);
1575 dmanow = dmastart + host->data->bytes_xfered;
1576 /*
1577 * Force update to the next DMA block boundary.
1578 */
1579 dmanow = (dmanow &
1580 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
1581 SDHCI_DEFAULT_BOUNDARY_SIZE;
1582 host->data->bytes_xfered = dmanow - dmastart;
1583 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
1584 " next 0x%08x\n",
1585 mmc_hostname(host->mmc), dmastart,
1586 host->data->bytes_xfered, dmanow);
1587 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
1588 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001589
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001590 if (intmask & SDHCI_INT_DATA_END) {
1591 if (host->cmd) {
1592 /*
1593 * Data managed to finish before the
1594 * command completed. Make sure we do
1595 * things in the proper order.
1596 */
1597 host->data_early = 1;
1598 } else {
1599 sdhci_finish_data(host);
1600 }
1601 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001602 }
1603}
1604
David Howells7d12e782006-10-05 14:55:46 +01001605static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001606{
1607 irqreturn_t result;
1608 struct sdhci_host* host = dev_id;
1609 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001610 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611
1612 spin_lock(&host->lock);
1613
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001614 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001615
Mark Lord62df67a52007-03-06 13:30:13 +01001616 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617 result = IRQ_NONE;
1618 goto out;
1619 }
1620
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001621 DBG("*** %s got interrupt: 0x%08x\n",
1622 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001623
Pierre Ossman3192a282006-06-30 02:22:26 -07001624 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001625 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1626 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001628 }
1629
1630 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001631
1632 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001633 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1634 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001635 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636 }
1637
1638 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001639 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1640 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001641 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642 }
1643
1644 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1645
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001646 intmask &= ~SDHCI_INT_ERROR;
1647
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001649 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001651 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652 }
1653
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001654 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001655
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001656 if (intmask & SDHCI_INT_CARD_INT)
1657 cardint = 1;
1658
1659 intmask &= ~SDHCI_INT_CARD_INT;
1660
Pierre Ossman3192a282006-06-30 02:22:26 -07001661 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001662 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001663 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664 sdhci_dumpregs(host);
1665
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001666 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001667 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668
1669 result = IRQ_HANDLED;
1670
Pierre Ossman5f25a662006-10-04 02:15:39 -07001671 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672out:
1673 spin_unlock(&host->lock);
1674
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001675 /*
1676 * We have to delay this as it calls back into the driver.
1677 */
1678 if (cardint)
1679 mmc_signal_sdio_irq(host->mmc);
1680
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681 return result;
1682}
1683
1684/*****************************************************************************\
1685 * *
1686 * Suspend/resume *
1687 * *
1688\*****************************************************************************/
1689
1690#ifdef CONFIG_PM
1691
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001692int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001694 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001695
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001696 sdhci_disable_card_detection(host);
1697
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001698 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001699 if (ret)
1700 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001701
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001702 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001703
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001704 if (host->vmmc)
1705 ret = regulator_disable(host->vmmc);
1706
1707 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001708}
1709
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001710EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001711
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001712int sdhci_resume_host(struct sdhci_host *host)
1713{
1714 int ret;
1715
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001716 if (host->vmmc) {
1717 int ret = regulator_enable(host->vmmc);
1718 if (ret)
1719 return ret;
1720 }
1721
1722
Richard Röjforsa13abc72009-09-22 16:45:30 -07001723 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001724 if (host->ops->enable_dma)
1725 host->ops->enable_dma(host);
1726 }
1727
1728 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1729 mmc_hostname(host->mmc), host);
1730 if (ret)
1731 return ret;
1732
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001733 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001734 mmiowb();
1735
1736 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001737 sdhci_enable_card_detection(host);
1738
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001739 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001740}
1741
1742EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743
Daniel Drake5f619702010-11-04 22:20:39 +00001744void sdhci_enable_irq_wakeups(struct sdhci_host *host)
1745{
1746 u8 val;
1747 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
1748 val |= SDHCI_WAKE_ON_INT;
1749 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
1750}
1751
1752EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
1753
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754#endif /* CONFIG_PM */
1755
1756/*****************************************************************************\
1757 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001758 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001759 * *
1760\*****************************************************************************/
1761
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001762struct sdhci_host *sdhci_alloc_host(struct device *dev,
1763 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765 struct mmc_host *mmc;
1766 struct sdhci_host *host;
1767
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001768 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001769
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001770 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001771 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001772 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001773
1774 host = mmc_priv(mmc);
1775 host->mmc = mmc;
1776
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001777 return host;
1778}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001779
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001780EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001781
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001782int sdhci_add_host(struct sdhci_host *host)
1783{
1784 struct mmc_host *mmc;
Takashi Iwai8f230f42010-12-08 10:04:30 +01001785 unsigned int caps, ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001786 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001788 WARN_ON(host == NULL);
1789 if (host == NULL)
1790 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001791
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001792 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001793
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001794 if (debug_quirks)
1795 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001796
Pierre Ossmand96649e2006-06-30 02:22:30 -07001797 sdhci_reset(host, SDHCI_RESET_ALL);
1798
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001799 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001800 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1801 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001802 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001803 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001804 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001805 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001806 }
1807
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001808 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1809 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001810
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001811 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001812 host->flags |= SDHCI_USE_SDMA;
1813 else if (!(caps & SDHCI_CAN_DO_SDMA))
1814 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001815 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001816 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001817
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001818 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001819 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001820 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001821 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001822 }
1823
Richard Röjforsa13abc72009-09-22 16:45:30 -07001824 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1825 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001826
1827 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1828 (host->flags & SDHCI_USE_ADMA)) {
1829 DBG("Disabling ADMA as it is marked broken\n");
1830 host->flags &= ~SDHCI_USE_ADMA;
1831 }
1832
Richard Röjforsa13abc72009-09-22 16:45:30 -07001833 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001834 if (host->ops->enable_dma) {
1835 if (host->ops->enable_dma(host)) {
1836 printk(KERN_WARNING "%s: No suitable DMA "
1837 "available. Falling back to PIO.\n",
1838 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001839 host->flags &=
1840 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001841 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001842 }
1843 }
1844
Pierre Ossman2134a922008-06-28 18:28:51 +02001845 if (host->flags & SDHCI_USE_ADMA) {
1846 /*
1847 * We need to allocate descriptors for all sg entries
1848 * (128) and potentially one alignment transfer for
1849 * each of those entries.
1850 */
1851 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1852 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1853 if (!host->adma_desc || !host->align_buffer) {
1854 kfree(host->adma_desc);
1855 kfree(host->align_buffer);
1856 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1857 "buffers. Falling back to standard DMA.\n",
1858 mmc_hostname(mmc));
1859 host->flags &= ~SDHCI_USE_ADMA;
1860 }
1861 }
1862
Pierre Ossman76591502008-07-21 00:32:11 +02001863 /*
1864 * If we use DMA, then it's up to the caller to set the DMA
1865 * mask, but PIO does not need the hw shim so we set a new
1866 * mask here in that case.
1867 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001868 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001869 host->dma_mask = DMA_BIT_MASK(64);
1870 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1871 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001872
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04001873 if (host->version >= SDHCI_SPEC_300)
1874 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
1875 >> SDHCI_CLOCK_BASE_SHIFT;
1876 else
1877 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
1878 >> SDHCI_CLOCK_BASE_SHIFT;
1879
Pierre Ossmand129bce2006-03-24 03:18:17 -08001880 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001881 if (host->max_clk == 0 || host->quirks &
1882 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001883 if (!host->ops->get_max_clock) {
1884 printk(KERN_ERR
1885 "%s: Hardware doesn't specify base clock "
1886 "frequency.\n", mmc_hostname(mmc));
1887 return -ENODEV;
1888 }
1889 host->max_clk = host->ops->get_max_clock(host);
1890 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001891
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001892 host->timeout_clk =
1893 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1894 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001895 if (host->ops->get_timeout_clock) {
1896 host->timeout_clk = host->ops->get_timeout_clock(host);
1897 } else if (!(host->quirks &
1898 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001899 printk(KERN_ERR
1900 "%s: Hardware doesn't specify timeout clock "
1901 "frequency.\n", mmc_hostname(mmc));
1902 return -ENODEV;
1903 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001904 }
1905 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1906 host->timeout_clk *= 1000;
1907
Pierre Ossmand129bce2006-03-24 03:18:17 -08001908 /*
1909 * Set host parameters.
1910 */
1911 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07001912 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001913 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04001914 else if (host->version >= SDHCI_SPEC_300)
1915 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001916 else
Zhangfei Gao03975262010-09-20 15:15:18 -04001917 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05001918
Pierre Ossmand129bce2006-03-24 03:18:17 -08001919 mmc->f_max = host->max_clk;
Andrei Warkentina3c77782011-04-11 16:13:42 -05001920 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001921
Philip Rakity15ec4462010-11-19 16:48:39 -05001922 /*
1923 * A controller may support 8-bit width, but the board itself
1924 * might not have the pins brought out. Boards that support
1925 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
1926 * their platform code before calling sdhci_add_host(), and we
1927 * won't assume 8-bit width for hosts without that CAP.
1928 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001929 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05001930 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001931
Pierre Ossman86a6a872009-02-02 21:13:49 +01001932 if (caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04001933 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001934
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01001935 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1936 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001937 mmc->caps |= MMC_CAP_NEEDS_POLL;
1938
Takashi Iwai8f230f42010-12-08 10:04:30 +01001939 ocr_avail = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001940 if (caps & SDHCI_CAN_VDD_330)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001941 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001942 if (caps & SDHCI_CAN_VDD_300)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001943 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001944 if (caps & SDHCI_CAN_VDD_180)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001945 ocr_avail |= MMC_VDD_165_195;
1946
1947 mmc->ocr_avail = ocr_avail;
1948 mmc->ocr_avail_sdio = ocr_avail;
1949 if (host->ocr_avail_sdio)
1950 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
1951 mmc->ocr_avail_sd = ocr_avail;
1952 if (host->ocr_avail_sd)
1953 mmc->ocr_avail_sd &= host->ocr_avail_sd;
1954 else /* normal SD controllers don't support 1.8V */
1955 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
1956 mmc->ocr_avail_mmc = ocr_avail;
1957 if (host->ocr_avail_mmc)
1958 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07001959
1960 if (mmc->ocr_avail == 0) {
1961 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001962 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001963 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001964 }
1965
Pierre Ossmand129bce2006-03-24 03:18:17 -08001966 spin_lock_init(&host->lock);
1967
1968 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001969 * Maximum number of segments. Depends on if the hardware
1970 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001971 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001972 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001973 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001974 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001975 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001976 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001977 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001978
1979 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001980 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001981 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001982 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001983 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001984
1985 /*
1986 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001987 * of bytes. When doing hardware scatter/gather, each entry cannot
1988 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001989 */
Olof Johansson30652aa2011-01-01 18:37:32 -06001990 if (host->flags & SDHCI_USE_ADMA) {
1991 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
1992 mmc->max_seg_size = 65535;
1993 else
1994 mmc->max_seg_size = 65536;
1995 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02001996 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06001997 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998
1999 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002000 * Maximum block size. This varies from controller to controller and
2001 * is specified in the capabilities register.
2002 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002003 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2004 mmc->max_blk_size = 2;
2005 } else {
2006 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
2007 SDHCI_MAX_BLOCK_SHIFT;
2008 if (mmc->max_blk_size >= 3) {
2009 printk(KERN_WARNING "%s: Invalid maximum block size, "
2010 "assuming 512 bytes\n", mmc_hostname(mmc));
2011 mmc->max_blk_size = 0;
2012 }
2013 }
2014
2015 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002016
2017 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002018 * Maximum block count.
2019 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002020 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002021
2022 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002023 * Init tasklets.
2024 */
2025 tasklet_init(&host->card_tasklet,
2026 sdhci_tasklet_card, (unsigned long)host);
2027 tasklet_init(&host->finish_tasklet,
2028 sdhci_tasklet_finish, (unsigned long)host);
2029
Al Viroe4cad1b2006-10-10 22:47:07 +01002030 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002031
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002032 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002033 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002034 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002035 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002036
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002037 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2038 if (IS_ERR(host->vmmc)) {
2039 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2040 host->vmmc = NULL;
2041 } else {
2042 regulator_enable(host->vmmc);
2043 }
2044
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002045 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002046
2047#ifdef CONFIG_MMC_DEBUG
2048 sdhci_dumpregs(host);
2049#endif
2050
Pierre Ossmanf9134312008-12-21 17:01:48 +01002051#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002052 snprintf(host->led_name, sizeof(host->led_name),
2053 "%s::", mmc_hostname(mmc));
2054 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002055 host->led.brightness = LED_OFF;
2056 host->led.default_trigger = mmc_hostname(mmc);
2057 host->led.brightness_set = sdhci_led_control;
2058
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002059 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002060 if (ret)
2061 goto reset;
2062#endif
2063
Pierre Ossman5f25a662006-10-04 02:15:39 -07002064 mmiowb();
2065
Pierre Ossmand129bce2006-03-24 03:18:17 -08002066 mmc_add_host(mmc);
2067
Richard Röjforsa13abc72009-09-22 16:45:30 -07002068 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002069 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002070 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2071 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002072
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002073 sdhci_enable_card_detection(host);
2074
Pierre Ossmand129bce2006-03-24 03:18:17 -08002075 return 0;
2076
Pierre Ossmanf9134312008-12-21 17:01:48 +01002077#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002078reset:
2079 sdhci_reset(host, SDHCI_RESET_ALL);
2080 free_irq(host->irq, host);
2081#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002082untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083 tasklet_kill(&host->card_tasklet);
2084 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002085
2086 return ret;
2087}
2088
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002089EXPORT_SYMBOL_GPL(sdhci_add_host);
2090
Pierre Ossman1e728592008-04-16 19:13:13 +02002091void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092{
Pierre Ossman1e728592008-04-16 19:13:13 +02002093 unsigned long flags;
2094
2095 if (dead) {
2096 spin_lock_irqsave(&host->lock, flags);
2097
2098 host->flags |= SDHCI_DEVICE_DEAD;
2099
2100 if (host->mrq) {
2101 printk(KERN_ERR "%s: Controller removed during "
2102 " transfer!\n", mmc_hostname(host->mmc));
2103
2104 host->mrq->cmd->error = -ENOMEDIUM;
2105 tasklet_schedule(&host->finish_tasklet);
2106 }
2107
2108 spin_unlock_irqrestore(&host->lock, flags);
2109 }
2110
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002111 sdhci_disable_card_detection(host);
2112
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002113 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002114
Pierre Ossmanf9134312008-12-21 17:01:48 +01002115#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002116 led_classdev_unregister(&host->led);
2117#endif
2118
Pierre Ossman1e728592008-04-16 19:13:13 +02002119 if (!dead)
2120 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002121
2122 free_irq(host->irq, host);
2123
2124 del_timer_sync(&host->timer);
2125
2126 tasklet_kill(&host->card_tasklet);
2127 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002128
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002129 if (host->vmmc) {
2130 regulator_disable(host->vmmc);
2131 regulator_put(host->vmmc);
2132 }
2133
Pierre Ossman2134a922008-06-28 18:28:51 +02002134 kfree(host->adma_desc);
2135 kfree(host->align_buffer);
2136
2137 host->adma_desc = NULL;
2138 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002139}
2140
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002141EXPORT_SYMBOL_GPL(sdhci_remove_host);
2142
2143void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002145 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002146}
2147
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002148EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149
2150/*****************************************************************************\
2151 * *
2152 * Driver init/exit *
2153 * *
2154\*****************************************************************************/
2155
2156static int __init sdhci_drv_init(void)
2157{
2158 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002159 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2161
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002162 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002163}
2164
2165static void __exit sdhci_drv_exit(void)
2166{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167}
2168
2169module_init(sdhci_drv_init);
2170module_exit(sdhci_drv_exit);
2171
Pierre Ossmandf673b22006-06-30 02:22:31 -07002172module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002173
Pierre Ossman32710e82009-04-08 20:14:54 +02002174MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002175MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002176MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002177
Pierre Ossmandf673b22006-06-30 02:22:31 -07002178MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");