blob: 07cca557c4b5346690d7f3d6294f7fd995d5fe86 [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
Arindam Nathb513ea22011-05-05 12:19:04 +053041#define MAX_TUNING_LOOP 40
42
Pierre Ossmandf673b22006-06-30 02:22:31 -070043static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070044
Pierre Ossmand129bce2006-03-24 03:18:17 -080045static void sdhci_finish_data(struct sdhci_host *);
46
47static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
48static void sdhci_finish_command(struct sdhci_host *);
49
50static void sdhci_dumpregs(struct sdhci_host *host)
51{
Philip Rakity412ab652010-09-22 15:25:13 -070052 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
53 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
55 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030056 sdhci_readl(host, SDHCI_DMA_ADDRESS),
57 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080058 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030059 sdhci_readw(host, SDHCI_BLOCK_SIZE),
60 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080061 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030062 sdhci_readl(host, SDHCI_ARGUMENT),
63 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080064 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030065 sdhci_readl(host, SDHCI_PRESENT_STATE),
66 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080067 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030068 sdhci_readb(host, SDHCI_POWER_CONTROL),
69 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080070 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030071 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
72 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030074 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
75 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080076 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readl(host, SDHCI_INT_ENABLE),
78 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080079 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readw(host, SDHCI_ACMD12_ERR),
81 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050082 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050084 sdhci_readl(host, SDHCI_CAPABILITIES_1));
85 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
86 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readl(host, SDHCI_MAX_CURRENT));
Arindam Nathf2119df2011-05-05 12:18:57 +053088 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
89 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080090
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010091 if (host->flags & SDHCI_USE_ADMA)
92 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
93 readl(host->ioaddr + SDHCI_ADMA_ERROR),
94 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
95
Pierre Ossmand129bce2006-03-24 03:18:17 -080096 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
97}
98
99/*****************************************************************************\
100 * *
101 * Low level functions *
102 * *
103\*****************************************************************************/
104
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300105static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
106{
107 u32 ier;
108
109 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
110 ier &= ~clear;
111 ier |= set;
112 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
113 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
114}
115
116static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
117{
118 sdhci_clear_set_irqs(host, 0, irqs);
119}
120
121static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
122{
123 sdhci_clear_set_irqs(host, irqs, 0);
124}
125
126static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
127{
128 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
129
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300130 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
131 return;
132
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300133 if (enable)
134 sdhci_unmask_irqs(host, irqs);
135 else
136 sdhci_mask_irqs(host, irqs);
137}
138
139static void sdhci_enable_card_detection(struct sdhci_host *host)
140{
141 sdhci_set_card_detection(host, true);
142}
143
144static void sdhci_disable_card_detection(struct sdhci_host *host)
145{
146 sdhci_set_card_detection(host, false);
147}
148
Pierre Ossmand129bce2006-03-24 03:18:17 -0800149static void sdhci_reset(struct sdhci_host *host, u8 mask)
150{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700151 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300152 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700153
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100154 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300155 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700156 SDHCI_CARD_PRESENT))
157 return;
158 }
159
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300160 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
161 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
162
Philip Rakity393c1a32011-01-21 11:26:40 -0800163 if (host->ops->platform_reset_enter)
164 host->ops->platform_reset_enter(host, mask);
165
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300166 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800169 host->clock = 0;
170
Pierre Ossmane16514d82006-06-30 02:22:24 -0700171 /* Wait max 100 ms */
172 timeout = 100;
173
174 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300175 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100177 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 mmc_hostname(host->mmc), (int)mask);
179 sdhci_dumpregs(host);
180 return;
181 }
182 timeout--;
183 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300185
Philip Rakity393c1a32011-01-21 11:26:40 -0800186 if (host->ops->platform_reset_exit)
187 host->ops->platform_reset_exit(host, mask);
188
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300189 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
190 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191}
192
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800193static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
194
195static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800196{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800197 if (soft)
198 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
199 else
200 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300202 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
203 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700204 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
205 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300206 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800207
208 if (soft) {
209 /* force clock reconfiguration */
210 host->clock = 0;
211 sdhci_set_ios(host->mmc, &host->mmc->ios);
212 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300213}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800214
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300215static void sdhci_reinit(struct sdhci_host *host)
216{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300218 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219}
220
221static void sdhci_activate_led(struct sdhci_host *host)
222{
223 u8 ctrl;
224
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300225 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300227 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228}
229
230static void sdhci_deactivate_led(struct sdhci_host *host)
231{
232 u8 ctrl;
233
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300234 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300236 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237}
238
Pierre Ossmanf9134312008-12-21 17:01:48 +0100239#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100240static void sdhci_led_control(struct led_classdev *led,
241 enum led_brightness brightness)
242{
243 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
244 unsigned long flags;
245
246 spin_lock_irqsave(&host->lock, flags);
247
248 if (brightness == LED_OFF)
249 sdhci_deactivate_led(host);
250 else
251 sdhci_activate_led(host);
252
253 spin_unlock_irqrestore(&host->lock, flags);
254}
255#endif
256
Pierre Ossmand129bce2006-03-24 03:18:17 -0800257/*****************************************************************************\
258 * *
259 * Core functions *
260 * *
261\*****************************************************************************/
262
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100263static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264{
Pierre Ossman76591502008-07-21 00:32:11 +0200265 unsigned long flags;
266 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700267 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200268 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100270 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100272 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200273 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274
Pierre Ossman76591502008-07-21 00:32:11 +0200275 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100277 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200278 if (!sg_miter_next(&host->sg_miter))
279 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280
Pierre Ossman76591502008-07-21 00:32:11 +0200281 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282
Pierre Ossman76591502008-07-21 00:32:11 +0200283 blksize -= len;
284 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200285
Pierre Ossman76591502008-07-21 00:32:11 +0200286 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800287
Pierre Ossman76591502008-07-21 00:32:11 +0200288 while (len) {
289 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300290 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200291 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800292 }
Pierre Ossman76591502008-07-21 00:32:11 +0200293
294 *buf = scratch & 0xFF;
295
296 buf++;
297 scratch >>= 8;
298 chunk--;
299 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300 }
301 }
Pierre Ossman76591502008-07-21 00:32:11 +0200302
303 sg_miter_stop(&host->sg_miter);
304
305 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308static void sdhci_write_block_pio(struct sdhci_host *host)
309{
Pierre Ossman76591502008-07-21 00:32:11 +0200310 unsigned long flags;
311 size_t blksize, len, chunk;
312 u32 scratch;
313 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314
315 DBG("PIO writing\n");
316
317 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200318 chunk = 0;
319 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322
323 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200324 if (!sg_miter_next(&host->sg_miter))
325 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100326
Pierre Ossman76591502008-07-21 00:32:11 +0200327 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 blksize -= len;
330 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 while (len) {
335 scratch |= (u32)*buf << (chunk * 8);
336
337 buf++;
338 chunk++;
339 len--;
340
341 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300342 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200343 chunk = 0;
344 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100345 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346 }
347 }
Pierre Ossman76591502008-07-21 00:32:11 +0200348
349 sg_miter_stop(&host->sg_miter);
350
351 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352}
353
354static void sdhci_transfer_pio(struct sdhci_host *host)
355{
356 u32 mask;
357
358 BUG_ON(!host->data);
359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361 return;
362
363 if (host->data->flags & MMC_DATA_READ)
364 mask = SDHCI_DATA_AVAILABLE;
365 else
366 mask = SDHCI_SPACE_AVAILABLE;
367
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200368 /*
369 * Some controllers (JMicron JMB38x) mess up the buffer bits
370 * for transfers < 4 bytes. As long as it is just one block,
371 * we can ignore the bits.
372 */
373 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
374 (host->data->blocks == 1))
375 mask = ~0;
376
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300377 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300378 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
379 udelay(100);
380
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 if (host->data->flags & MMC_DATA_READ)
382 sdhci_read_block_pio(host);
383 else
384 sdhci_write_block_pio(host);
385
Pierre Ossman76591502008-07-21 00:32:11 +0200386 host->blocks--;
387 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389 }
390
391 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800392}
393
Pierre Ossman2134a922008-06-28 18:28:51 +0200394static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
395{
396 local_irq_save(*flags);
397 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
398}
399
400static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
401{
402 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
403 local_irq_restore(*flags);
404}
405
Ben Dooks118cd172010-03-05 13:43:26 -0800406static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
407{
Ben Dooks9e506f32010-03-05 13:43:29 -0800408 __le32 *dataddr = (__le32 __force *)(desc + 4);
409 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800410
Ben Dooks9e506f32010-03-05 13:43:29 -0800411 /* SDHCI specification says ADMA descriptors should be 4 byte
412 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800413
Ben Dooks9e506f32010-03-05 13:43:29 -0800414 cmdlen[0] = cpu_to_le16(cmd);
415 cmdlen[1] = cpu_to_le16(len);
416
417 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800418}
419
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200420static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200421 struct mmc_data *data)
422{
423 int direction;
424
425 u8 *desc;
426 u8 *align;
427 dma_addr_t addr;
428 dma_addr_t align_addr;
429 int len, offset;
430
431 struct scatterlist *sg;
432 int i;
433 char *buffer;
434 unsigned long flags;
435
436 /*
437 * The spec does not specify endianness of descriptor table.
438 * We currently guess that it is LE.
439 */
440
441 if (data->flags & MMC_DATA_READ)
442 direction = DMA_FROM_DEVICE;
443 else
444 direction = DMA_TO_DEVICE;
445
446 /*
447 * The ADMA descriptor table is mapped further down as we
448 * need to fill it with data first.
449 */
450
451 host->align_addr = dma_map_single(mmc_dev(host->mmc),
452 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700453 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200454 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200455 BUG_ON(host->align_addr & 0x3);
456
457 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
458 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200459 if (host->sg_count == 0)
460 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200461
462 desc = host->adma_desc;
463 align = host->align_buffer;
464
465 align_addr = host->align_addr;
466
467 for_each_sg(data->sg, sg, host->sg_count, i) {
468 addr = sg_dma_address(sg);
469 len = sg_dma_len(sg);
470
471 /*
472 * The SDHCI specification states that ADMA
473 * addresses must be 32-bit aligned. If they
474 * aren't, then we use a bounce buffer for
475 * the (up to three) bytes that screw up the
476 * alignment.
477 */
478 offset = (4 - (addr & 0x3)) & 0x3;
479 if (offset) {
480 if (data->flags & MMC_DATA_WRITE) {
481 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200482 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200483 memcpy(align, buffer, offset);
484 sdhci_kunmap_atomic(buffer, &flags);
485 }
486
Ben Dooks118cd172010-03-05 13:43:26 -0800487 /* tran, valid */
488 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200489
490 BUG_ON(offset > 65536);
491
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 align += 4;
493 align_addr += 4;
494
495 desc += 8;
496
497 addr += offset;
498 len -= offset;
499 }
500
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 BUG_ON(len > 65536);
502
Ben Dooks118cd172010-03-05 13:43:26 -0800503 /* tran, valid */
504 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200505 desc += 8;
506
507 /*
508 * If this triggers then we have a calculation bug
509 * somewhere. :/
510 */
511 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
512 }
513
Thomas Abraham70764a92010-05-26 14:42:04 -0700514 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
515 /*
516 * Mark the last descriptor as the terminating descriptor
517 */
518 if (desc != host->adma_desc) {
519 desc -= 8;
520 desc[0] |= 0x2; /* end */
521 }
522 } else {
523 /*
524 * Add a terminating entry.
525 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200526
Thomas Abraham70764a92010-05-26 14:42:04 -0700527 /* nop, end, valid */
528 sdhci_set_adma_desc(desc, 0, 0, 0x3);
529 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200530
531 /*
532 * Resync align buffer as we might have changed it.
533 */
534 if (data->flags & MMC_DATA_WRITE) {
535 dma_sync_single_for_device(mmc_dev(host->mmc),
536 host->align_addr, 128 * 4, direction);
537 }
538
539 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
540 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200541 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200542 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200543 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200544
545 return 0;
546
547unmap_entries:
548 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
549 data->sg_len, direction);
550unmap_align:
551 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
552 128 * 4, direction);
553fail:
554 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200555}
556
557static void sdhci_adma_table_post(struct sdhci_host *host,
558 struct mmc_data *data)
559{
560 int direction;
561
562 struct scatterlist *sg;
563 int i, size;
564 u8 *align;
565 char *buffer;
566 unsigned long flags;
567
568 if (data->flags & MMC_DATA_READ)
569 direction = DMA_FROM_DEVICE;
570 else
571 direction = DMA_TO_DEVICE;
572
573 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
574 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
575
576 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
577 128 * 4, direction);
578
579 if (data->flags & MMC_DATA_READ) {
580 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
581 data->sg_len, direction);
582
583 align = host->align_buffer;
584
585 for_each_sg(data->sg, sg, host->sg_count, i) {
586 if (sg_dma_address(sg) & 0x3) {
587 size = 4 - (sg_dma_address(sg) & 0x3);
588
589 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200590 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200591 memcpy(buffer, align, size);
592 sdhci_kunmap_atomic(buffer, &flags);
593
594 align += 4;
595 }
596 }
597 }
598
599 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
600 data->sg_len, direction);
601}
602
Andrei Warkentina3c77782011-04-11 16:13:42 -0500603static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800604{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700605 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500606 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700607 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800608
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200609 /*
610 * If the host controller provides us with an incorrect timeout
611 * value, just skip the check and use 0xE. The hardware may take
612 * longer to time out, but that's much better than having a too-short
613 * timeout value.
614 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200615 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200616 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200617
Andrei Warkentina3c77782011-04-11 16:13:42 -0500618 /* Unspecified timeout, assume max */
619 if (!data && !cmd->cmd_timeout_ms)
620 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800621
Andrei Warkentina3c77782011-04-11 16:13:42 -0500622 /* timeout in us */
623 if (!data)
624 target_timeout = cmd->cmd_timeout_ms * 1000;
625 else
626 target_timeout = data->timeout_ns / 1000 +
627 data->timeout_clks / host->clock;
Anton Vorontsov81b39802009-09-22 16:45:13 -0700628
Mark Brown4b016812011-04-19 18:44:17 +0100629 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
630 host->timeout_clk = host->clock / 1000;
631
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700632 /*
633 * Figure out needed cycles.
634 * We do this in steps in order to fit inside a 32 bit int.
635 * The first step is the minimum timeout, which will have a
636 * minimum resolution of 6 bits:
637 * (1) 2^13*1000 > 2^22,
638 * (2) host->timeout_clk < 2^16
639 * =>
640 * (1) / (2) > 2^6
641 */
Mark Brown4b016812011-04-19 18:44:17 +0100642 BUG_ON(!host->timeout_clk);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700643 count = 0;
644 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
645 while (current_timeout < target_timeout) {
646 count++;
647 current_timeout <<= 1;
648 if (count >= 0xF)
649 break;
650 }
651
652 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500653 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
654 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700655 count = 0xE;
656 }
657
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 return count;
659}
660
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300661static void sdhci_set_transfer_irqs(struct sdhci_host *host)
662{
663 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
664 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
665
666 if (host->flags & SDHCI_REQ_USE_DMA)
667 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
668 else
669 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
670}
671
Andrei Warkentina3c77782011-04-11 16:13:42 -0500672static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200673{
674 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200675 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500676 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200677 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200678
679 WARN_ON(host->data);
680
Andrei Warkentina3c77782011-04-11 16:13:42 -0500681 if (data || (cmd->flags & MMC_RSP_BUSY)) {
682 count = sdhci_calc_timeout(host, cmd);
683 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
684 }
685
686 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200687 return;
688
689 /* Sanity checks */
690 BUG_ON(data->blksz * data->blocks > 524288);
691 BUG_ON(data->blksz > host->mmc->max_blk_size);
692 BUG_ON(data->blocks > 65535);
693
694 host->data = data;
695 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400696 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200697
Richard Röjforsa13abc72009-09-22 16:45:30 -0700698 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100699 host->flags |= SDHCI_REQ_USE_DMA;
700
Pierre Ossman2134a922008-06-28 18:28:51 +0200701 /*
702 * FIXME: This doesn't account for merging when mapping the
703 * scatterlist.
704 */
705 if (host->flags & SDHCI_REQ_USE_DMA) {
706 int broken, i;
707 struct scatterlist *sg;
708
709 broken = 0;
710 if (host->flags & SDHCI_USE_ADMA) {
711 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
712 broken = 1;
713 } else {
714 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
715 broken = 1;
716 }
717
718 if (unlikely(broken)) {
719 for_each_sg(data->sg, sg, data->sg_len, i) {
720 if (sg->length & 0x3) {
721 DBG("Reverting to PIO because of "
722 "transfer size (%d)\n",
723 sg->length);
724 host->flags &= ~SDHCI_REQ_USE_DMA;
725 break;
726 }
727 }
728 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100729 }
730
731 /*
732 * The assumption here being that alignment is the same after
733 * translation to device address space.
734 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200735 if (host->flags & SDHCI_REQ_USE_DMA) {
736 int broken, i;
737 struct scatterlist *sg;
738
739 broken = 0;
740 if (host->flags & SDHCI_USE_ADMA) {
741 /*
742 * As we use 3 byte chunks to work around
743 * alignment problems, we need to check this
744 * quirk.
745 */
746 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
747 broken = 1;
748 } else {
749 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
750 broken = 1;
751 }
752
753 if (unlikely(broken)) {
754 for_each_sg(data->sg, sg, data->sg_len, i) {
755 if (sg->offset & 0x3) {
756 DBG("Reverting to PIO because of "
757 "bad alignment\n");
758 host->flags &= ~SDHCI_REQ_USE_DMA;
759 break;
760 }
761 }
762 }
763 }
764
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200765 if (host->flags & SDHCI_REQ_USE_DMA) {
766 if (host->flags & SDHCI_USE_ADMA) {
767 ret = sdhci_adma_table_pre(host, data);
768 if (ret) {
769 /*
770 * This only happens when someone fed
771 * us an invalid request.
772 */
773 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200774 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200775 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300776 sdhci_writel(host, host->adma_addr,
777 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200778 }
779 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300780 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200781
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300782 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200783 data->sg, data->sg_len,
784 (data->flags & MMC_DATA_READ) ?
785 DMA_FROM_DEVICE :
786 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300787 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200788 /*
789 * This only happens when someone fed
790 * us an invalid request.
791 */
792 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200793 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200794 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200795 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300796 sdhci_writel(host, sg_dma_address(data->sg),
797 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200798 }
799 }
800 }
801
Pierre Ossman2134a922008-06-28 18:28:51 +0200802 /*
803 * Always adjust the DMA selection as some controllers
804 * (e.g. JMicron) can't do PIO properly when the selection
805 * is ADMA.
806 */
807 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300808 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200809 ctrl &= ~SDHCI_CTRL_DMA_MASK;
810 if ((host->flags & SDHCI_REQ_USE_DMA) &&
811 (host->flags & SDHCI_USE_ADMA))
812 ctrl |= SDHCI_CTRL_ADMA32;
813 else
814 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300815 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100816 }
817
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200818 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200819 int flags;
820
821 flags = SG_MITER_ATOMIC;
822 if (host->data->flags & MMC_DATA_READ)
823 flags |= SG_MITER_TO_SG;
824 else
825 flags |= SG_MITER_FROM_SG;
826 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200827 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800828 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700829
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300830 sdhci_set_transfer_irqs(host);
831
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400832 /* Set the DMA boundary value and block size */
833 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
834 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300835 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700836}
837
838static void sdhci_set_transfer_mode(struct sdhci_host *host,
839 struct mmc_data *data)
840{
841 u16 mode;
842
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700843 if (data == NULL)
844 return;
845
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200846 WARN_ON(!host->data);
847
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700848 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700849 if (data->blocks > 1) {
850 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
851 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
852 else
853 mode |= SDHCI_TRNS_MULTI;
854 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700855 if (data->flags & MMC_DATA_READ)
856 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100857 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700858 mode |= SDHCI_TRNS_DMA;
859
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800861}
862
863static void sdhci_finish_data(struct sdhci_host *host)
864{
865 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866
867 BUG_ON(!host->data);
868
869 data = host->data;
870 host->data = NULL;
871
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100872 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200873 if (host->flags & SDHCI_USE_ADMA)
874 sdhci_adma_table_post(host, data);
875 else {
876 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
877 data->sg_len, (data->flags & MMC_DATA_READ) ?
878 DMA_FROM_DEVICE : DMA_TO_DEVICE);
879 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880 }
881
882 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200883 * The specification states that the block count register must
884 * be updated, but it does not specify at what point in the
885 * data flow. That makes the register entirely useless to read
886 * back so we have to assume that nothing made it to the card
887 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200889 if (data->error)
890 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800891 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200892 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893
Pierre Ossmand129bce2006-03-24 03:18:17 -0800894 if (data->stop) {
895 /*
896 * The controller needs a reset of internal state machines
897 * upon error conditions.
898 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200899 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900 sdhci_reset(host, SDHCI_RESET_CMD);
901 sdhci_reset(host, SDHCI_RESET_DATA);
902 }
903
904 sdhci_send_command(host, data->stop);
905 } else
906 tasklet_schedule(&host->finish_tasklet);
907}
908
909static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
910{
911 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700912 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700913 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914
915 WARN_ON(host->cmd);
916
Pierre Ossmand129bce2006-03-24 03:18:17 -0800917 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700918 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700919
920 mask = SDHCI_CMD_INHIBIT;
921 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
922 mask |= SDHCI_DATA_INHIBIT;
923
924 /* We shouldn't wait for data inihibit for stop commands, even
925 though they might use busy signaling */
926 if (host->mrq->data && (cmd == host->mrq->data->stop))
927 mask &= ~SDHCI_DATA_INHIBIT;
928
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300929 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700930 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100932 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200934 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 tasklet_schedule(&host->finish_tasklet);
936 return;
937 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700938 timeout--;
939 mdelay(1);
940 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941
942 mod_timer(&host->timer, jiffies + 10 * HZ);
943
944 host->cmd = cmd;
945
Andrei Warkentina3c77782011-04-11 16:13:42 -0500946 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300948 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700950 sdhci_set_transfer_mode(host, cmd->data);
951
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100953 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200955 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800956 tasklet_schedule(&host->finish_tasklet);
957 return;
958 }
959
960 if (!(cmd->flags & MMC_RSP_PRESENT))
961 flags = SDHCI_CMD_RESP_NONE;
962 else if (cmd->flags & MMC_RSP_136)
963 flags = SDHCI_CMD_RESP_LONG;
964 else if (cmd->flags & MMC_RSP_BUSY)
965 flags = SDHCI_CMD_RESP_SHORT_BUSY;
966 else
967 flags = SDHCI_CMD_RESP_SHORT;
968
969 if (cmd->flags & MMC_RSP_CRC)
970 flags |= SDHCI_CMD_CRC;
971 if (cmd->flags & MMC_RSP_OPCODE)
972 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +0530973
974 /* CMD19 is special in that the Data Present Select should be set */
975 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 flags |= SDHCI_CMD_DATA;
977
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300978 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979}
980
981static void sdhci_finish_command(struct sdhci_host *host)
982{
983 int i;
984
985 BUG_ON(host->cmd == NULL);
986
987 if (host->cmd->flags & MMC_RSP_PRESENT) {
988 if (host->cmd->flags & MMC_RSP_136) {
989 /* CRC is stripped so we need to do some shifting. */
990 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 SDHCI_RESPONSE + (3-i)*4) << 8;
993 if (i != 3)
994 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300995 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 SDHCI_RESPONSE + (3-i)*4-1);
997 }
998 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300999 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 }
1001 }
1002
Pierre Ossman17b04292007-07-22 22:18:46 +02001003 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001005 if (host->data && host->data_early)
1006 sdhci_finish_data(host);
1007
1008 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009 tasklet_schedule(&host->finish_tasklet);
1010
1011 host->cmd = NULL;
1012}
1013
1014static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1015{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301016 int div = 0; /* Initialized for compiler warning */
1017 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001018 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
1020 if (clock == host->clock)
1021 return;
1022
Anton Vorontsov81146342009-03-17 00:13:59 +03001023 if (host->ops->set_clock) {
1024 host->ops->set_clock(host, clock);
1025 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1026 return;
1027 }
1028
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
1031 if (clock == 0)
1032 goto out;
1033
Zhangfei Gao85105c52010-08-06 07:10:01 +08001034 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301035 /*
1036 * Check if the Host Controller supports Programmable Clock
1037 * Mode.
1038 */
1039 if (host->clk_mul) {
1040 u16 ctrl;
1041
1042 /*
1043 * We need to figure out whether the Host Driver needs
1044 * to select Programmable Clock Mode, or the value can
1045 * be set automatically by the Host Controller based on
1046 * the Preset Value registers.
1047 */
1048 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1049 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1050 for (div = 1; div <= 1024; div++) {
1051 if (((host->max_clk * host->clk_mul) /
1052 div) <= clock)
1053 break;
1054 }
1055 /*
1056 * Set Programmable Clock Mode in the Clock
1057 * Control register.
1058 */
1059 clk = SDHCI_PROG_CLOCK_MODE;
1060 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001061 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301062 } else {
1063 /* Version 3.00 divisors must be a multiple of 2. */
1064 if (host->max_clk <= clock)
1065 div = 1;
1066 else {
1067 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1068 div += 2) {
1069 if ((host->max_clk / div) <= clock)
1070 break;
1071 }
1072 }
1073 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001074 }
1075 } else {
1076 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001077 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001078 if ((host->max_clk / div) <= clock)
1079 break;
1080 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301081 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083
Arindam Nathc3ed3872011-05-05 12:19:06 +05301084 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001085 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1086 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001088 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089
Chris Ball27f6cb12009-09-22 16:45:31 -07001090 /* Wait max 20 ms */
1091 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001092 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001093 & SDHCI_CLOCK_INT_STABLE)) {
1094 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001095 printk(KERN_ERR "%s: Internal clock never "
1096 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097 sdhci_dumpregs(host);
1098 return;
1099 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001100 timeout--;
1101 mdelay(1);
1102 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001103
1104 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001105 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106
1107out:
1108 host->clock = clock;
1109}
1110
Pierre Ossman146ad662006-06-30 02:22:23 -07001111static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1112{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001113 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001114
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001115 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001116 switch (1 << power) {
1117 case MMC_VDD_165_195:
1118 pwr = SDHCI_POWER_180;
1119 break;
1120 case MMC_VDD_29_30:
1121 case MMC_VDD_30_31:
1122 pwr = SDHCI_POWER_300;
1123 break;
1124 case MMC_VDD_32_33:
1125 case MMC_VDD_33_34:
1126 pwr = SDHCI_POWER_330;
1127 break;
1128 default:
1129 BUG();
1130 }
1131 }
1132
1133 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001134 return;
1135
Pierre Ossmanae628902009-05-03 20:45:03 +02001136 host->pwr = pwr;
1137
1138 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001139 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001140 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001141 }
1142
1143 /*
1144 * Spec says that we should clear the power reg before setting
1145 * a new value. Some controllers don't seem to like this though.
1146 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001147 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001148 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001149
Andres Salomone08c1692008-07-04 10:00:03 -07001150 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001151 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001152 * and set turn on power at the same time, so set the voltage first.
1153 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001154 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001155 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1156
1157 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001158
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001159 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001160
1161 /*
1162 * Some controllers need an extra 10ms delay of 10ms before they
1163 * can apply clock after applying power
1164 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001165 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001166 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001167}
1168
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169/*****************************************************************************\
1170 * *
1171 * MMC callbacks *
1172 * *
1173\*****************************************************************************/
1174
1175static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1176{
1177 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001178 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001179 unsigned long flags;
1180
1181 host = mmc_priv(mmc);
1182
1183 spin_lock_irqsave(&host->lock, flags);
1184
1185 WARN_ON(host->mrq != NULL);
1186
Pierre Ossmanf9134312008-12-21 17:01:48 +01001187#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001189#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001190 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1191 if (mrq->stop) {
1192 mrq->data->stop = NULL;
1193 mrq->stop = NULL;
1194 }
1195 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001196
1197 host->mrq = mrq;
1198
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001199 /* If polling, assume that the card is always present. */
1200 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1201 present = true;
1202 else
1203 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1204 SDHCI_CARD_PRESENT;
1205
1206 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001207 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208 tasklet_schedule(&host->finish_tasklet);
1209 } else
1210 sdhci_send_command(host, mrq->cmd);
1211
Pierre Ossman5f25a662006-10-04 02:15:39 -07001212 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213 spin_unlock_irqrestore(&host->lock, flags);
1214}
1215
1216static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1217{
1218 struct sdhci_host *host;
1219 unsigned long flags;
1220 u8 ctrl;
1221
1222 host = mmc_priv(mmc);
1223
1224 spin_lock_irqsave(&host->lock, flags);
1225
Pierre Ossman1e728592008-04-16 19:13:13 +02001226 if (host->flags & SDHCI_DEVICE_DEAD)
1227 goto out;
1228
Pierre Ossmand129bce2006-03-24 03:18:17 -08001229 /*
1230 * Reset the chip on each power off.
1231 * Should clear out any weird states.
1232 */
1233 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001234 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001235 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236 }
1237
1238 sdhci_set_clock(host, ios->clock);
1239
1240 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001241 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001243 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244
Philip Rakity643a81f2010-09-23 08:24:32 -07001245 if (host->ops->platform_send_init_74_clocks)
1246 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1247
Philip Rakity15ec4462010-11-19 16:48:39 -05001248 /*
1249 * If your platform has 8-bit width support but is not a v3 controller,
1250 * or if it requires special setup code, you should implement that in
1251 * platform_8bit_width().
1252 */
1253 if (host->ops->platform_8bit_width)
1254 host->ops->platform_8bit_width(host, ios->bus_width);
1255 else {
1256 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1257 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1258 ctrl &= ~SDHCI_CTRL_4BITBUS;
1259 if (host->version >= SDHCI_SPEC_300)
1260 ctrl |= SDHCI_CTRL_8BITBUS;
1261 } else {
1262 if (host->version >= SDHCI_SPEC_300)
1263 ctrl &= ~SDHCI_CTRL_8BITBUS;
1264 if (ios->bus_width == MMC_BUS_WIDTH_4)
1265 ctrl |= SDHCI_CTRL_4BITBUS;
1266 else
1267 ctrl &= ~SDHCI_CTRL_4BITBUS;
1268 }
1269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1270 }
1271
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001272 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001273
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001274 if ((ios->timing == MMC_TIMING_SD_HS ||
1275 ios->timing == MMC_TIMING_MMC_HS)
1276 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001277 ctrl |= SDHCI_CTRL_HISPD;
1278 else
1279 ctrl &= ~SDHCI_CTRL_HISPD;
1280
Arindam Nathd6d50a12011-05-05 12:18:59 +05301281 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301282 u16 clk, ctrl_2;
1283 unsigned int clock;
1284
1285 /* In case of UHS-I modes, set High Speed Enable */
1286 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1287 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1288 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1289 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1290 (ios->timing == MMC_TIMING_UHS_SDR12))
1291 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301292
1293 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1294 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301295 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301296 /*
1297 * We only need to set Driver Strength if the
1298 * preset value enable is not set.
1299 */
1300 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1301 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1302 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1303 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1304 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1305
1306 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301307 } else {
1308 /*
1309 * According to SDHC Spec v3.00, if the Preset Value
1310 * Enable in the Host Control 2 register is set, we
1311 * need to reset SD Clock Enable before changing High
1312 * Speed Enable to avoid generating clock gliches.
1313 */
Arindam Nath758535c2011-05-05 12:19:00 +05301314
1315 /* Reset SD Clock Enable */
1316 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1317 clk &= ~SDHCI_CLOCK_CARD_EN;
1318 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1319
1320 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1321
1322 /* Re-enable SD Clock */
1323 clock = host->clock;
1324 host->clock = 0;
1325 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301326 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301327
1328 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1329
1330 /* Select Bus Speed Mode for host */
1331 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1332 if (ios->timing == MMC_TIMING_UHS_SDR12)
1333 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1334 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1335 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1336 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1337 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1338 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1339 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1340 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1341 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1342
1343 /* Reset SD Clock Enable */
1344 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1345 clk &= ~SDHCI_CLOCK_CARD_EN;
1346 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1347
1348 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1349
1350 /* Re-enable SD Clock */
1351 clock = host->clock;
1352 host->clock = 0;
1353 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301354 } else
1355 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301356
Leandro Dorileob8352262007-07-25 23:47:04 +02001357 /*
1358 * Some (ENE) controllers go apeshit on some ios operation,
1359 * signalling timeout and CRC errors even on CMD0. Resetting
1360 * it on each ios seems to solve the problem.
1361 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001362 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001363 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1364
Pierre Ossman1e728592008-04-16 19:13:13 +02001365out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001366 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 spin_unlock_irqrestore(&host->lock, flags);
1368}
1369
Takashi Iwai82b0e232011-04-21 20:26:38 +02001370static int check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001373 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374
Pierre Ossmand129bce2006-03-24 03:18:17 -08001375 spin_lock_irqsave(&host->lock, flags);
1376
Pierre Ossman1e728592008-04-16 19:13:13 +02001377 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001378 is_readonly = 0;
1379 else if (host->ops->get_ro)
1380 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001381 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001382 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1383 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001384
1385 spin_unlock_irqrestore(&host->lock, flags);
1386
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001387 /* This quirk needs to be replaced by a callback-function later */
1388 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1389 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001390}
1391
Takashi Iwai82b0e232011-04-21 20:26:38 +02001392#define SAMPLE_COUNT 5
1393
1394static int sdhci_get_ro(struct mmc_host *mmc)
1395{
1396 struct sdhci_host *host;
1397 int i, ro_count;
1398
1399 host = mmc_priv(mmc);
1400
1401 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1402 return check_ro(host);
1403
1404 ro_count = 0;
1405 for (i = 0; i < SAMPLE_COUNT; i++) {
1406 if (check_ro(host)) {
1407 if (++ro_count > SAMPLE_COUNT / 2)
1408 return 1;
1409 }
1410 msleep(30);
1411 }
1412 return 0;
1413}
1414
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001415static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1416{
1417 struct sdhci_host *host;
1418 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001419
1420 host = mmc_priv(mmc);
1421
1422 spin_lock_irqsave(&host->lock, flags);
1423
Pierre Ossman1e728592008-04-16 19:13:13 +02001424 if (host->flags & SDHCI_DEVICE_DEAD)
1425 goto out;
1426
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001427 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001428 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1429 else
1430 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001431out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001432 mmiowb();
1433
1434 spin_unlock_irqrestore(&host->lock, flags);
1435}
1436
Arindam Nathf2119df2011-05-05 12:18:57 +05301437static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1438 struct mmc_ios *ios)
1439{
1440 struct sdhci_host *host;
1441 u8 pwr;
1442 u16 clk, ctrl;
1443 u32 present_state;
1444
1445 host = mmc_priv(mmc);
1446
1447 /*
1448 * Signal Voltage Switching is only applicable for Host Controllers
1449 * v3.00 and above.
1450 */
1451 if (host->version < SDHCI_SPEC_300)
1452 return 0;
1453
1454 /*
1455 * We first check whether the request is to set signalling voltage
1456 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1457 */
1458 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1459 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1460 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1461 ctrl &= ~SDHCI_CTRL_VDD_180;
1462 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1463
1464 /* Wait for 5ms */
1465 usleep_range(5000, 5500);
1466
1467 /* 3.3V regulator output should be stable within 5 ms */
1468 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1469 if (!(ctrl & SDHCI_CTRL_VDD_180))
1470 return 0;
1471 else {
1472 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1473 "signalling voltage failed\n");
1474 return -EIO;
1475 }
1476 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1477 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1478 /* Stop SDCLK */
1479 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1480 clk &= ~SDHCI_CLOCK_CARD_EN;
1481 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1482
1483 /* Check whether DAT[3:0] is 0000 */
1484 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1485 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1486 SDHCI_DATA_LVL_SHIFT)) {
1487 /*
1488 * Enable 1.8V Signal Enable in the Host Control2
1489 * register
1490 */
1491 ctrl |= SDHCI_CTRL_VDD_180;
1492 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1493
1494 /* Wait for 5ms */
1495 usleep_range(5000, 5500);
1496
1497 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1498 if (ctrl & SDHCI_CTRL_VDD_180) {
1499 /* Provide SDCLK again and wait for 1ms*/
1500 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1501 clk |= SDHCI_CLOCK_CARD_EN;
1502 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1503 usleep_range(1000, 1500);
1504
1505 /*
1506 * If DAT[3:0] level is 1111b, then the card
1507 * was successfully switched to 1.8V signaling.
1508 */
1509 present_state = sdhci_readl(host,
1510 SDHCI_PRESENT_STATE);
1511 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1512 SDHCI_DATA_LVL_MASK)
1513 return 0;
1514 }
1515 }
1516
1517 /*
1518 * If we are here, that means the switch to 1.8V signaling
1519 * failed. We power cycle the card, and retry initialization
1520 * sequence by setting S18R to 0.
1521 */
1522 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1523 pwr &= ~SDHCI_POWER_ON;
1524 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1525
1526 /* Wait for 1ms as per the spec */
1527 usleep_range(1000, 1500);
1528 pwr |= SDHCI_POWER_ON;
1529 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1530
1531 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1532 "voltage failed, retrying with S18R set to 0\n");
1533 return -EAGAIN;
1534 } else
1535 /* No signal voltage switch required */
1536 return 0;
1537}
1538
Arindam Nathb513ea22011-05-05 12:19:04 +05301539static int sdhci_execute_tuning(struct mmc_host *mmc)
1540{
1541 struct sdhci_host *host;
1542 u16 ctrl;
1543 u32 ier;
1544 int tuning_loop_counter = MAX_TUNING_LOOP;
1545 unsigned long timeout;
1546 int err = 0;
1547
1548 host = mmc_priv(mmc);
1549
1550 disable_irq(host->irq);
1551 spin_lock(&host->lock);
1552
1553 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1554
1555 /*
1556 * Host Controller needs tuning only in case of SDR104 mode
1557 * and for SDR50 mode when Use Tuning for SDR50 is set in
1558 * Capabilities register.
1559 */
1560 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1561 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1562 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1563 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1564 else {
1565 spin_unlock(&host->lock);
1566 enable_irq(host->irq);
1567 return 0;
1568 }
1569
1570 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1571
1572 /*
1573 * As per the Host Controller spec v3.00, tuning command
1574 * generates Buffer Read Ready interrupt, so enable that.
1575 *
1576 * Note: The spec clearly says that when tuning sequence
1577 * is being performed, the controller does not generate
1578 * interrupts other than Buffer Read Ready interrupt. But
1579 * to make sure we don't hit a controller bug, we _only_
1580 * enable Buffer Read Ready interrupt here.
1581 */
1582 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1583 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1584
1585 /*
1586 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1587 * of loops reaches 40 times or a timeout of 150ms occurs.
1588 */
1589 timeout = 150;
1590 do {
1591 struct mmc_command cmd = {0};
1592 struct mmc_request mrq = {0};
1593
1594 if (!tuning_loop_counter && !timeout)
1595 break;
1596
1597 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1598 cmd.arg = 0;
1599 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1600 cmd.retries = 0;
1601 cmd.data = NULL;
1602 cmd.error = 0;
1603
1604 mrq.cmd = &cmd;
1605 host->mrq = &mrq;
1606
1607 /*
1608 * In response to CMD19, the card sends 64 bytes of tuning
1609 * block to the Host Controller. So we set the block size
1610 * to 64 here.
1611 */
1612 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1613
1614 /*
1615 * The tuning block is sent by the card to the host controller.
1616 * So we set the TRNS_READ bit in the Transfer Mode register.
1617 * This also takes care of setting DMA Enable and Multi Block
1618 * Select in the same register to 0.
1619 */
1620 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1621
1622 sdhci_send_command(host, &cmd);
1623
1624 host->cmd = NULL;
1625 host->mrq = NULL;
1626
1627 spin_unlock(&host->lock);
1628 enable_irq(host->irq);
1629
1630 /* Wait for Buffer Read Ready interrupt */
1631 wait_event_interruptible_timeout(host->buf_ready_int,
1632 (host->tuning_done == 1),
1633 msecs_to_jiffies(50));
1634 disable_irq(host->irq);
1635 spin_lock(&host->lock);
1636
1637 if (!host->tuning_done) {
1638 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1639 "Buffer Read Ready interrupt during tuning "
1640 "procedure, falling back to fixed sampling "
1641 "clock\n");
1642 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1643 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1644 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1645 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1646
1647 err = -EIO;
1648 goto out;
1649 }
1650
1651 host->tuning_done = 0;
1652
1653 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1654 tuning_loop_counter--;
1655 timeout--;
1656 mdelay(1);
1657 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1658
1659 /*
1660 * The Host Driver has exhausted the maximum number of loops allowed,
1661 * so use fixed sampling frequency.
1662 */
1663 if (!tuning_loop_counter || !timeout) {
1664 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1665 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1666 } else {
1667 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1668 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1669 " failed, falling back to fixed sampling"
1670 " clock\n");
1671 err = -EIO;
1672 }
1673 }
1674
1675out:
1676 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1677 spin_unlock(&host->lock);
1678 enable_irq(host->irq);
1679
1680 return err;
1681}
1682
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301683static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1684{
1685 struct sdhci_host *host;
1686 u16 ctrl;
1687 unsigned long flags;
1688
1689 host = mmc_priv(mmc);
1690
1691 /* Host Controller v3.00 defines preset value registers */
1692 if (host->version < SDHCI_SPEC_300)
1693 return;
1694
1695 spin_lock_irqsave(&host->lock, flags);
1696
1697 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1698
1699 /*
1700 * We only enable or disable Preset Value if they are not already
1701 * enabled or disabled respectively. Otherwise, we bail out.
1702 */
1703 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1704 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1705 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1706 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1707 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1708 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1709 }
1710
1711 spin_unlock_irqrestore(&host->lock, flags);
1712}
1713
David Brownellab7aefd2006-11-12 17:55:30 -08001714static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001715 .request = sdhci_request,
1716 .set_ios = sdhci_set_ios,
1717 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001718 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301719 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301720 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301721 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001722};
1723
1724/*****************************************************************************\
1725 * *
1726 * Tasklets *
1727 * *
1728\*****************************************************************************/
1729
1730static void sdhci_tasklet_card(unsigned long param)
1731{
1732 struct sdhci_host *host;
1733 unsigned long flags;
1734
1735 host = (struct sdhci_host*)param;
1736
1737 spin_lock_irqsave(&host->lock, flags);
1738
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001739 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001740 if (host->mrq) {
1741 printk(KERN_ERR "%s: Card removed during transfer!\n",
1742 mmc_hostname(host->mmc));
1743 printk(KERN_ERR "%s: Resetting controller.\n",
1744 mmc_hostname(host->mmc));
1745
1746 sdhci_reset(host, SDHCI_RESET_CMD);
1747 sdhci_reset(host, SDHCI_RESET_DATA);
1748
Pierre Ossman17b04292007-07-22 22:18:46 +02001749 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750 tasklet_schedule(&host->finish_tasklet);
1751 }
1752 }
1753
1754 spin_unlock_irqrestore(&host->lock, flags);
1755
Pierre Ossman04cf5852008-08-18 22:18:14 +02001756 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001757}
1758
1759static void sdhci_tasklet_finish(unsigned long param)
1760{
1761 struct sdhci_host *host;
1762 unsigned long flags;
1763 struct mmc_request *mrq;
1764
1765 host = (struct sdhci_host*)param;
1766
Chris Ball0c9c99a2011-04-27 17:35:31 -04001767 /*
1768 * If this tasklet gets rescheduled while running, it will
1769 * be run again afterwards but without any active request.
1770 */
1771 if (!host->mrq)
1772 return;
1773
Pierre Ossmand129bce2006-03-24 03:18:17 -08001774 spin_lock_irqsave(&host->lock, flags);
1775
1776 del_timer(&host->timer);
1777
1778 mrq = host->mrq;
1779
Pierre Ossmand129bce2006-03-24 03:18:17 -08001780 /*
1781 * The controller needs a reset of internal state machines
1782 * upon error conditions.
1783 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001784 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001785 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001786 (mrq->data && (mrq->data->error ||
1787 (mrq->data->stop && mrq->data->stop->error))) ||
1788 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001789
1790 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001791 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001792 unsigned int clock;
1793
1794 /* This is to force an update */
1795 clock = host->clock;
1796 host->clock = 0;
1797 sdhci_set_clock(host, clock);
1798 }
1799
1800 /* Spec says we should do both at the same time, but Ricoh
1801 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001802 sdhci_reset(host, SDHCI_RESET_CMD);
1803 sdhci_reset(host, SDHCI_RESET_DATA);
1804 }
1805
1806 host->mrq = NULL;
1807 host->cmd = NULL;
1808 host->data = NULL;
1809
Pierre Ossmanf9134312008-12-21 17:01:48 +01001810#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001812#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001813
Pierre Ossman5f25a662006-10-04 02:15:39 -07001814 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001815 spin_unlock_irqrestore(&host->lock, flags);
1816
1817 mmc_request_done(host->mmc, mrq);
1818}
1819
1820static void sdhci_timeout_timer(unsigned long data)
1821{
1822 struct sdhci_host *host;
1823 unsigned long flags;
1824
1825 host = (struct sdhci_host*)data;
1826
1827 spin_lock_irqsave(&host->lock, flags);
1828
1829 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001830 printk(KERN_ERR "%s: Timeout waiting for hardware "
1831 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001832 sdhci_dumpregs(host);
1833
1834 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001835 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001836 sdhci_finish_data(host);
1837 } else {
1838 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001839 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001840 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001841 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001842
1843 tasklet_schedule(&host->finish_tasklet);
1844 }
1845 }
1846
Pierre Ossman5f25a662006-10-04 02:15:39 -07001847 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001848 spin_unlock_irqrestore(&host->lock, flags);
1849}
1850
1851/*****************************************************************************\
1852 * *
1853 * Interrupt handling *
1854 * *
1855\*****************************************************************************/
1856
1857static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1858{
1859 BUG_ON(intmask == 0);
1860
1861 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001862 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1863 "though no command operation was in progress.\n",
1864 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001865 sdhci_dumpregs(host);
1866 return;
1867 }
1868
Pierre Ossman43b58b32007-07-25 23:15:27 +02001869 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001870 host->cmd->error = -ETIMEDOUT;
1871 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1872 SDHCI_INT_INDEX))
1873 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874
Pierre Ossmane8095172008-07-25 01:09:08 +02001875 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001876 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001877 return;
1878 }
1879
1880 /*
1881 * The host can send and interrupt when the busy state has
1882 * ended, allowing us to wait without wasting CPU cycles.
1883 * Unfortunately this is overloaded on the "data complete"
1884 * interrupt, so we need to take some care when handling
1885 * it.
1886 *
1887 * Note: The 1.0 specification is a bit ambiguous about this
1888 * feature so there might be some problems with older
1889 * controllers.
1890 */
1891 if (host->cmd->flags & MMC_RSP_BUSY) {
1892 if (host->cmd->data)
1893 DBG("Cannot wait for busy signal when also "
1894 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001895 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001896 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001897
1898 /* The controller does not support the end-of-busy IRQ,
1899 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001900 }
1901
1902 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001903 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001904}
1905
George G. Davis0957c332010-02-18 12:32:12 -05001906#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001907static void sdhci_show_adma_error(struct sdhci_host *host)
1908{
1909 const char *name = mmc_hostname(host->mmc);
1910 u8 *desc = host->adma_desc;
1911 __le32 *dma;
1912 __le16 *len;
1913 u8 attr;
1914
1915 sdhci_dumpregs(host);
1916
1917 while (true) {
1918 dma = (__le32 *)(desc + 4);
1919 len = (__le16 *)(desc + 2);
1920 attr = *desc;
1921
1922 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1923 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1924
1925 desc += 8;
1926
1927 if (attr & 2)
1928 break;
1929 }
1930}
1931#else
1932static void sdhci_show_adma_error(struct sdhci_host *host) { }
1933#endif
1934
Pierre Ossmand129bce2006-03-24 03:18:17 -08001935static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1936{
1937 BUG_ON(intmask == 0);
1938
Arindam Nathb513ea22011-05-05 12:19:04 +05301939 /* CMD19 generates _only_ Buffer Read Ready interrupt */
1940 if (intmask & SDHCI_INT_DATA_AVAIL) {
1941 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
1942 MMC_SEND_TUNING_BLOCK) {
1943 host->tuning_done = 1;
1944 wake_up(&host->buf_ready_int);
1945 return;
1946 }
1947 }
1948
Pierre Ossmand129bce2006-03-24 03:18:17 -08001949 if (!host->data) {
1950 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001951 * The "data complete" interrupt is also used to
1952 * indicate that a busy state has ended. See comment
1953 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001954 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001955 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1956 if (intmask & SDHCI_INT_DATA_END) {
1957 sdhci_finish_command(host);
1958 return;
1959 }
1960 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001961
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001962 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1963 "though no data operation was in progress.\n",
1964 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001965 sdhci_dumpregs(host);
1966
1967 return;
1968 }
1969
1970 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001971 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001972 else if (intmask & SDHCI_INT_DATA_END_BIT)
1973 host->data->error = -EILSEQ;
1974 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1975 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1976 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001977 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001978 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1979 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1980 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001981 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001982 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001983
Pierre Ossman17b04292007-07-22 22:18:46 +02001984 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001985 sdhci_finish_data(host);
1986 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001987 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001988 sdhci_transfer_pio(host);
1989
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001990 /*
1991 * We currently don't do anything fancy with DMA
1992 * boundaries, but as we can't disable the feature
1993 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001994 *
1995 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
1996 * should return a valid address to continue from, but as
1997 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001998 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001999 if (intmask & SDHCI_INT_DMA_END) {
2000 u32 dmastart, dmanow;
2001 dmastart = sg_dma_address(host->data->sg);
2002 dmanow = dmastart + host->data->bytes_xfered;
2003 /*
2004 * Force update to the next DMA block boundary.
2005 */
2006 dmanow = (dmanow &
2007 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2008 SDHCI_DEFAULT_BOUNDARY_SIZE;
2009 host->data->bytes_xfered = dmanow - dmastart;
2010 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2011 " next 0x%08x\n",
2012 mmc_hostname(host->mmc), dmastart,
2013 host->data->bytes_xfered, dmanow);
2014 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2015 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002016
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002017 if (intmask & SDHCI_INT_DATA_END) {
2018 if (host->cmd) {
2019 /*
2020 * Data managed to finish before the
2021 * command completed. Make sure we do
2022 * things in the proper order.
2023 */
2024 host->data_early = 1;
2025 } else {
2026 sdhci_finish_data(host);
2027 }
2028 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002029 }
2030}
2031
David Howells7d12e782006-10-05 14:55:46 +01002032static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002033{
2034 irqreturn_t result;
2035 struct sdhci_host* host = dev_id;
2036 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002037 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002038
2039 spin_lock(&host->lock);
2040
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002041 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002042
Mark Lord62df67a52007-03-06 13:30:13 +01002043 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002044 result = IRQ_NONE;
2045 goto out;
2046 }
2047
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002048 DBG("*** %s got interrupt: 0x%08x\n",
2049 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002050
Pierre Ossman3192a282006-06-30 02:22:26 -07002051 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002052 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2053 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002055 }
2056
2057 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002058
2059 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002060 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2061 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002062 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002063 }
2064
2065 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002066 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2067 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002068 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002069 }
2070
2071 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2072
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002073 intmask &= ~SDHCI_INT_ERROR;
2074
Pierre Ossmand129bce2006-03-24 03:18:17 -08002075 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07002076 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002077 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002078 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002079 }
2080
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002081 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002082
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002083 if (intmask & SDHCI_INT_CARD_INT)
2084 cardint = 1;
2085
2086 intmask &= ~SDHCI_INT_CARD_INT;
2087
Pierre Ossman3192a282006-06-30 02:22:26 -07002088 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002089 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002090 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002091 sdhci_dumpregs(host);
2092
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002093 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002094 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002095
2096 result = IRQ_HANDLED;
2097
Pierre Ossman5f25a662006-10-04 02:15:39 -07002098 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099out:
2100 spin_unlock(&host->lock);
2101
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002102 /*
2103 * We have to delay this as it calls back into the driver.
2104 */
2105 if (cardint)
2106 mmc_signal_sdio_irq(host->mmc);
2107
Pierre Ossmand129bce2006-03-24 03:18:17 -08002108 return result;
2109}
2110
2111/*****************************************************************************\
2112 * *
2113 * Suspend/resume *
2114 * *
2115\*****************************************************************************/
2116
2117#ifdef CONFIG_PM
2118
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002119int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002120{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002121 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002122
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002123 sdhci_disable_card_detection(host);
2124
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002125 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002126 if (ret)
2127 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002128
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002129 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002130
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002131 if (host->vmmc)
2132 ret = regulator_disable(host->vmmc);
2133
2134 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135}
2136
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002137EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002138
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002139int sdhci_resume_host(struct sdhci_host *host)
2140{
2141 int ret;
2142
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002143 if (host->vmmc) {
2144 int ret = regulator_enable(host->vmmc);
2145 if (ret)
2146 return ret;
2147 }
2148
2149
Richard Röjforsa13abc72009-09-22 16:45:30 -07002150 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002151 if (host->ops->enable_dma)
2152 host->ops->enable_dma(host);
2153 }
2154
2155 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2156 mmc_hostname(host->mmc), host);
2157 if (ret)
2158 return ret;
2159
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002160 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002161 mmiowb();
2162
2163 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002164 sdhci_enable_card_detection(host);
2165
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002166 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002167}
2168
2169EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170
Daniel Drake5f619702010-11-04 22:20:39 +00002171void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2172{
2173 u8 val;
2174 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2175 val |= SDHCI_WAKE_ON_INT;
2176 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2177}
2178
2179EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2180
Pierre Ossmand129bce2006-03-24 03:18:17 -08002181#endif /* CONFIG_PM */
2182
2183/*****************************************************************************\
2184 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002185 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002186 * *
2187\*****************************************************************************/
2188
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002189struct sdhci_host *sdhci_alloc_host(struct device *dev,
2190 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002192 struct mmc_host *mmc;
2193 struct sdhci_host *host;
2194
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002195 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002196
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002197 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002199 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002200
2201 host = mmc_priv(mmc);
2202 host->mmc = mmc;
2203
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002204 return host;
2205}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002206
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002207EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002209int sdhci_add_host(struct sdhci_host *host)
2210{
2211 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302212 u32 caps[2];
2213 u32 max_current_caps;
2214 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002215 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002217 WARN_ON(host == NULL);
2218 if (host == NULL)
2219 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002220
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002221 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002223 if (debug_quirks)
2224 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002225
Pierre Ossmand96649e2006-06-30 02:22:30 -07002226 sdhci_reset(host, SDHCI_RESET_ALL);
2227
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002228 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002229 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2230 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002231 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07002232 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002233 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002234 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002235 }
2236
Arindam Nathf2119df2011-05-05 12:18:57 +05302237 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002238 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239
Arindam Nathf2119df2011-05-05 12:18:57 +05302240 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2241 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2242
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002243 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002244 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302245 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002246 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002247 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002248 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002249
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002250 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002251 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002252 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002253 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002254 }
2255
Arindam Nathf2119df2011-05-05 12:18:57 +05302256 if ((host->version >= SDHCI_SPEC_200) &&
2257 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002258 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002259
2260 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2261 (host->flags & SDHCI_USE_ADMA)) {
2262 DBG("Disabling ADMA as it is marked broken\n");
2263 host->flags &= ~SDHCI_USE_ADMA;
2264 }
2265
Richard Röjforsa13abc72009-09-22 16:45:30 -07002266 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002267 if (host->ops->enable_dma) {
2268 if (host->ops->enable_dma(host)) {
2269 printk(KERN_WARNING "%s: No suitable DMA "
2270 "available. Falling back to PIO.\n",
2271 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002272 host->flags &=
2273 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002274 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 }
2276 }
2277
Pierre Ossman2134a922008-06-28 18:28:51 +02002278 if (host->flags & SDHCI_USE_ADMA) {
2279 /*
2280 * We need to allocate descriptors for all sg entries
2281 * (128) and potentially one alignment transfer for
2282 * each of those entries.
2283 */
2284 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2285 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2286 if (!host->adma_desc || !host->align_buffer) {
2287 kfree(host->adma_desc);
2288 kfree(host->align_buffer);
2289 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2290 "buffers. Falling back to standard DMA.\n",
2291 mmc_hostname(mmc));
2292 host->flags &= ~SDHCI_USE_ADMA;
2293 }
2294 }
2295
Pierre Ossman76591502008-07-21 00:32:11 +02002296 /*
2297 * If we use DMA, then it's up to the caller to set the DMA
2298 * mask, but PIO does not need the hw shim so we set a new
2299 * mask here in that case.
2300 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002301 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002302 host->dma_mask = DMA_BIT_MASK(64);
2303 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2304 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002306 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302307 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002308 >> SDHCI_CLOCK_BASE_SHIFT;
2309 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302310 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002311 >> SDHCI_CLOCK_BASE_SHIFT;
2312
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002314 if (host->max_clk == 0 || host->quirks &
2315 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002316 if (!host->ops->get_max_clock) {
2317 printk(KERN_ERR
2318 "%s: Hardware doesn't specify base clock "
2319 "frequency.\n", mmc_hostname(mmc));
2320 return -ENODEV;
2321 }
2322 host->max_clk = host->ops->get_max_clock(host);
2323 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002325 host->timeout_clk =
Arindam Nathf2119df2011-05-05 12:18:57 +05302326 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002327 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07002328 if (host->ops->get_timeout_clock) {
2329 host->timeout_clk = host->ops->get_timeout_clock(host);
2330 } else if (!(host->quirks &
2331 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002332 printk(KERN_ERR
2333 "%s: Hardware doesn't specify timeout clock "
2334 "frequency.\n", mmc_hostname(mmc));
2335 return -ENODEV;
2336 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002337 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302338 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002339 host->timeout_clk *= 1000;
2340
Pierre Ossmand129bce2006-03-24 03:18:17 -08002341 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302342 * In case of Host Controller v3.00, find out whether clock
2343 * multiplier is supported.
2344 */
2345 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2346 SDHCI_CLOCK_MUL_SHIFT;
2347
2348 /*
2349 * In case the value in Clock Multiplier is 0, then programmable
2350 * clock mode is not supported, otherwise the actual clock
2351 * multiplier is one more than the value of Clock Multiplier
2352 * in the Capabilities Register.
2353 */
2354 if (host->clk_mul)
2355 host->clk_mul += 1;
2356
2357 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002358 * Set host parameters.
2359 */
2360 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302361 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002362 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002363 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302364 else if (host->version >= SDHCI_SPEC_300) {
2365 if (host->clk_mul) {
2366 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2367 mmc->f_max = host->max_clk * host->clk_mul;
2368 } else
2369 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2370 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002371 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002372
Andrei Warkentina3c77782011-04-11 16:13:42 -05002373 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002374
Philip Rakity15ec4462010-11-19 16:48:39 -05002375 /*
2376 * A controller may support 8-bit width, but the board itself
2377 * might not have the pins brought out. Boards that support
2378 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2379 * their platform code before calling sdhci_add_host(), and we
2380 * won't assume 8-bit width for hosts without that CAP.
2381 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002382 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002383 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384
Arindam Nathf2119df2011-05-05 12:18:57 +05302385 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002386 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002387
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002388 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2389 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002390 mmc->caps |= MMC_CAP_NEEDS_POLL;
2391
Arindam Nathf2119df2011-05-05 12:18:57 +05302392 /* UHS-I mode(s) supported by the host controller. */
2393 if (host->version >= SDHCI_SPEC_300)
2394 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2395
2396 /* SDR104 supports also implies SDR50 support */
2397 if (caps[1] & SDHCI_SUPPORT_SDR104)
2398 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2399 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2400 mmc->caps |= MMC_CAP_UHS_SDR50;
2401
2402 if (caps[1] & SDHCI_SUPPORT_DDR50)
2403 mmc->caps |= MMC_CAP_UHS_DDR50;
2404
Arindam Nathb513ea22011-05-05 12:19:04 +05302405 /* Does the host needs tuning for SDR50? */
2406 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2407 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2408
Arindam Nathd6d50a12011-05-05 12:18:59 +05302409 /* Driver Type(s) (A, C, D) supported by the host */
2410 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2411 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2412 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2413 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2414 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2415 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2416
Takashi Iwai8f230f42010-12-08 10:04:30 +01002417 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302418 /*
2419 * According to SD Host Controller spec v3.00, if the Host System
2420 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2421 * the value is meaningful only if Voltage Support in the Capabilities
2422 * register is set. The actual current value is 4 times the register
2423 * value.
2424 */
2425 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2426
2427 if (caps[0] & SDHCI_CAN_VDD_330) {
2428 int max_current_330;
2429
Takashi Iwai8f230f42010-12-08 10:04:30 +01002430 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302431
2432 max_current_330 = ((max_current_caps &
2433 SDHCI_MAX_CURRENT_330_MASK) >>
2434 SDHCI_MAX_CURRENT_330_SHIFT) *
2435 SDHCI_MAX_CURRENT_MULTIPLIER;
2436
2437 if (max_current_330 > 150)
2438 mmc->caps |= MMC_CAP_SET_XPC_330;
2439 }
2440 if (caps[0] & SDHCI_CAN_VDD_300) {
2441 int max_current_300;
2442
Takashi Iwai8f230f42010-12-08 10:04:30 +01002443 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302444
2445 max_current_300 = ((max_current_caps &
2446 SDHCI_MAX_CURRENT_300_MASK) >>
2447 SDHCI_MAX_CURRENT_300_SHIFT) *
2448 SDHCI_MAX_CURRENT_MULTIPLIER;
2449
2450 if (max_current_300 > 150)
2451 mmc->caps |= MMC_CAP_SET_XPC_300;
2452 }
2453 if (caps[0] & SDHCI_CAN_VDD_180) {
2454 int max_current_180;
2455
Takashi Iwai8f230f42010-12-08 10:04:30 +01002456 ocr_avail |= MMC_VDD_165_195;
2457
Arindam Nathf2119df2011-05-05 12:18:57 +05302458 max_current_180 = ((max_current_caps &
2459 SDHCI_MAX_CURRENT_180_MASK) >>
2460 SDHCI_MAX_CURRENT_180_SHIFT) *
2461 SDHCI_MAX_CURRENT_MULTIPLIER;
2462
2463 if (max_current_180 > 150)
2464 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302465
2466 /* Maximum current capabilities of the host at 1.8V */
2467 if (max_current_180 >= 800)
2468 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2469 else if (max_current_180 >= 600)
2470 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2471 else if (max_current_180 >= 400)
2472 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2473 else
2474 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302475 }
2476
Takashi Iwai8f230f42010-12-08 10:04:30 +01002477 mmc->ocr_avail = ocr_avail;
2478 mmc->ocr_avail_sdio = ocr_avail;
2479 if (host->ocr_avail_sdio)
2480 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2481 mmc->ocr_avail_sd = ocr_avail;
2482 if (host->ocr_avail_sd)
2483 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2484 else /* normal SD controllers don't support 1.8V */
2485 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2486 mmc->ocr_avail_mmc = ocr_avail;
2487 if (host->ocr_avail_mmc)
2488 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002489
2490 if (mmc->ocr_avail == 0) {
2491 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002492 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002493 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002494 }
2495
Pierre Ossmand129bce2006-03-24 03:18:17 -08002496 spin_lock_init(&host->lock);
2497
2498 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002499 * Maximum number of segments. Depends on if the hardware
2500 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002501 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002502 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002503 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002504 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002505 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002506 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002507 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508
2509 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002510 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002511 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002513 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514
2515 /*
2516 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002517 * of bytes. When doing hardware scatter/gather, each entry cannot
2518 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002519 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002520 if (host->flags & SDHCI_USE_ADMA) {
2521 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2522 mmc->max_seg_size = 65535;
2523 else
2524 mmc->max_seg_size = 65536;
2525 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002526 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002527 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
2529 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002530 * Maximum block size. This varies from controller to controller and
2531 * is specified in the capabilities register.
2532 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002533 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2534 mmc->max_blk_size = 2;
2535 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302536 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002537 SDHCI_MAX_BLOCK_SHIFT;
2538 if (mmc->max_blk_size >= 3) {
2539 printk(KERN_WARNING "%s: Invalid maximum block size, "
2540 "assuming 512 bytes\n", mmc_hostname(mmc));
2541 mmc->max_blk_size = 0;
2542 }
2543 }
2544
2545 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002546
2547 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002548 * Maximum block count.
2549 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002550 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002551
2552 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002553 * Init tasklets.
2554 */
2555 tasklet_init(&host->card_tasklet,
2556 sdhci_tasklet_card, (unsigned long)host);
2557 tasklet_init(&host->finish_tasklet,
2558 sdhci_tasklet_finish, (unsigned long)host);
2559
Al Viroe4cad1b2006-10-10 22:47:07 +01002560 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561
Arindam Nathb513ea22011-05-05 12:19:04 +05302562 if (host->version >= SDHCI_SPEC_300)
2563 init_waitqueue_head(&host->buf_ready_int);
2564
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002565 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002566 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002568 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002569
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002570 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2571 if (IS_ERR(host->vmmc)) {
2572 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2573 host->vmmc = NULL;
2574 } else {
2575 regulator_enable(host->vmmc);
2576 }
2577
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002578 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002579
2580#ifdef CONFIG_MMC_DEBUG
2581 sdhci_dumpregs(host);
2582#endif
2583
Pierre Ossmanf9134312008-12-21 17:01:48 +01002584#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002585 snprintf(host->led_name, sizeof(host->led_name),
2586 "%s::", mmc_hostname(mmc));
2587 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002588 host->led.brightness = LED_OFF;
2589 host->led.default_trigger = mmc_hostname(mmc);
2590 host->led.brightness_set = sdhci_led_control;
2591
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002592 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002593 if (ret)
2594 goto reset;
2595#endif
2596
Pierre Ossman5f25a662006-10-04 02:15:39 -07002597 mmiowb();
2598
Pierre Ossmand129bce2006-03-24 03:18:17 -08002599 mmc_add_host(mmc);
2600
Richard Röjforsa13abc72009-09-22 16:45:30 -07002601 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002602 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002603 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2604 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002605
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002606 sdhci_enable_card_detection(host);
2607
Pierre Ossmand129bce2006-03-24 03:18:17 -08002608 return 0;
2609
Pierre Ossmanf9134312008-12-21 17:01:48 +01002610#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002611reset:
2612 sdhci_reset(host, SDHCI_RESET_ALL);
2613 free_irq(host->irq, host);
2614#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002615untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002616 tasklet_kill(&host->card_tasklet);
2617 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002618
2619 return ret;
2620}
2621
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002622EXPORT_SYMBOL_GPL(sdhci_add_host);
2623
Pierre Ossman1e728592008-04-16 19:13:13 +02002624void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625{
Pierre Ossman1e728592008-04-16 19:13:13 +02002626 unsigned long flags;
2627
2628 if (dead) {
2629 spin_lock_irqsave(&host->lock, flags);
2630
2631 host->flags |= SDHCI_DEVICE_DEAD;
2632
2633 if (host->mrq) {
2634 printk(KERN_ERR "%s: Controller removed during "
2635 " transfer!\n", mmc_hostname(host->mmc));
2636
2637 host->mrq->cmd->error = -ENOMEDIUM;
2638 tasklet_schedule(&host->finish_tasklet);
2639 }
2640
2641 spin_unlock_irqrestore(&host->lock, flags);
2642 }
2643
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002644 sdhci_disable_card_detection(host);
2645
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002646 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002647
Pierre Ossmanf9134312008-12-21 17:01:48 +01002648#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002649 led_classdev_unregister(&host->led);
2650#endif
2651
Pierre Ossman1e728592008-04-16 19:13:13 +02002652 if (!dead)
2653 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
2655 free_irq(host->irq, host);
2656
2657 del_timer_sync(&host->timer);
2658
2659 tasklet_kill(&host->card_tasklet);
2660 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002661
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002662 if (host->vmmc) {
2663 regulator_disable(host->vmmc);
2664 regulator_put(host->vmmc);
2665 }
2666
Pierre Ossman2134a922008-06-28 18:28:51 +02002667 kfree(host->adma_desc);
2668 kfree(host->align_buffer);
2669
2670 host->adma_desc = NULL;
2671 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002672}
2673
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002674EXPORT_SYMBOL_GPL(sdhci_remove_host);
2675
2676void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002677{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002678 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679}
2680
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002681EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002682
2683/*****************************************************************************\
2684 * *
2685 * Driver init/exit *
2686 * *
2687\*****************************************************************************/
2688
2689static int __init sdhci_drv_init(void)
2690{
2691 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002692 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002693 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2694
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002695 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002696}
2697
2698static void __exit sdhci_drv_exit(void)
2699{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002700}
2701
2702module_init(sdhci_drv_init);
2703module_exit(sdhci_drv_exit);
2704
Pierre Ossmandf673b22006-06-30 02:22:31 -07002705module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002706
Pierre Ossman32710e82009-04-08 20:14:54 +02002707MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002708MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002709MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002710
Pierre Ossmandf673b22006-06-30 02:22:31 -07002711MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");