blob: a1ab22415883445d6f3ad6371dfe01470253f941 [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 Ossmane16514d2006-06-30 02:22:24 -0700151 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300152 u32 uninitialized_var(ier);
Pierre Ossmane16514d2006-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 Ossmane16514d2006-06-30 02:22:24 -0700168 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800169 host->clock = 0;
170
Pierre Ossmane16514d2006-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 Ossmane16514d2006-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 Ossmane16514d2006-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{
1016 int div;
1017 u16 clk;
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) {
1035 /* Version 3.00 divisors must be a multiple of 2. */
1036 if (host->max_clk <= clock)
1037 div = 1;
1038 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001039 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001040 if ((host->max_clk / div) <= clock)
1041 break;
1042 }
1043 }
1044 } else {
1045 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001046 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001047 if ((host->max_clk / div) <= clock)
1048 break;
1049 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050 }
1051 div >>= 1;
1052
Zhangfei Gao85105c52010-08-06 07:10:01 +08001053 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1054 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1055 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058
Chris Ball27f6cb12009-09-22 16:45:31 -07001059 /* Wait max 20 ms */
1060 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001061 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001062 & SDHCI_CLOCK_INT_STABLE)) {
1063 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001064 printk(KERN_ERR "%s: Internal clock never "
1065 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066 sdhci_dumpregs(host);
1067 return;
1068 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001069 timeout--;
1070 mdelay(1);
1071 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072
1073 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075
1076out:
1077 host->clock = clock;
1078}
1079
Pierre Ossman146ad662006-06-30 02:22:23 -07001080static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1081{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001082 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001083
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001084 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001085 switch (1 << power) {
1086 case MMC_VDD_165_195:
1087 pwr = SDHCI_POWER_180;
1088 break;
1089 case MMC_VDD_29_30:
1090 case MMC_VDD_30_31:
1091 pwr = SDHCI_POWER_300;
1092 break;
1093 case MMC_VDD_32_33:
1094 case MMC_VDD_33_34:
1095 pwr = SDHCI_POWER_330;
1096 break;
1097 default:
1098 BUG();
1099 }
1100 }
1101
1102 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001103 return;
1104
Pierre Ossmanae628902009-05-03 20:45:03 +02001105 host->pwr = pwr;
1106
1107 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001108 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001109 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001110 }
1111
1112 /*
1113 * Spec says that we should clear the power reg before setting
1114 * a new value. Some controllers don't seem to like this though.
1115 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001116 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001117 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001118
Andres Salomone08c1692008-07-04 10:00:03 -07001119 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001120 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001121 * and set turn on power at the same time, so set the voltage first.
1122 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001123 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001124 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1125
1126 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001127
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001128 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001129
1130 /*
1131 * Some controllers need an extra 10ms delay of 10ms before they
1132 * can apply clock after applying power
1133 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001134 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001135 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001136}
1137
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138/*****************************************************************************\
1139 * *
1140 * MMC callbacks *
1141 * *
1142\*****************************************************************************/
1143
1144static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1145{
1146 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001147 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 unsigned long flags;
1149
1150 host = mmc_priv(mmc);
1151
1152 spin_lock_irqsave(&host->lock, flags);
1153
1154 WARN_ON(host->mrq != NULL);
1155
Pierre Ossmanf9134312008-12-21 17:01:48 +01001156#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001158#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001159 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1160 if (mrq->stop) {
1161 mrq->data->stop = NULL;
1162 mrq->stop = NULL;
1163 }
1164 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165
1166 host->mrq = mrq;
1167
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001168 /* If polling, assume that the card is always present. */
1169 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1170 present = true;
1171 else
1172 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1173 SDHCI_CARD_PRESENT;
1174
1175 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001176 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001177 tasklet_schedule(&host->finish_tasklet);
1178 } else
1179 sdhci_send_command(host, mrq->cmd);
1180
Pierre Ossman5f25a662006-10-04 02:15:39 -07001181 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001182 spin_unlock_irqrestore(&host->lock, flags);
1183}
1184
1185static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1186{
1187 struct sdhci_host *host;
1188 unsigned long flags;
1189 u8 ctrl;
1190
1191 host = mmc_priv(mmc);
1192
1193 spin_lock_irqsave(&host->lock, flags);
1194
Pierre Ossman1e728592008-04-16 19:13:13 +02001195 if (host->flags & SDHCI_DEVICE_DEAD)
1196 goto out;
1197
Pierre Ossmand129bce2006-03-24 03:18:17 -08001198 /*
1199 * Reset the chip on each power off.
1200 * Should clear out any weird states.
1201 */
1202 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001203 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001204 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001205 }
1206
1207 sdhci_set_clock(host, ios->clock);
1208
1209 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001210 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001212 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213
Philip Rakity643a81f2010-09-23 08:24:32 -07001214 if (host->ops->platform_send_init_74_clocks)
1215 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1216
Philip Rakity15ec4462010-11-19 16:48:39 -05001217 /*
1218 * If your platform has 8-bit width support but is not a v3 controller,
1219 * or if it requires special setup code, you should implement that in
1220 * platform_8bit_width().
1221 */
1222 if (host->ops->platform_8bit_width)
1223 host->ops->platform_8bit_width(host, ios->bus_width);
1224 else {
1225 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1226 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1227 ctrl &= ~SDHCI_CTRL_4BITBUS;
1228 if (host->version >= SDHCI_SPEC_300)
1229 ctrl |= SDHCI_CTRL_8BITBUS;
1230 } else {
1231 if (host->version >= SDHCI_SPEC_300)
1232 ctrl &= ~SDHCI_CTRL_8BITBUS;
1233 if (ios->bus_width == MMC_BUS_WIDTH_4)
1234 ctrl |= SDHCI_CTRL_4BITBUS;
1235 else
1236 ctrl &= ~SDHCI_CTRL_4BITBUS;
1237 }
1238 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1239 }
1240
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001241 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001242
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001243 if ((ios->timing == MMC_TIMING_SD_HS ||
1244 ios->timing == MMC_TIMING_MMC_HS)
1245 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001246 ctrl |= SDHCI_CTRL_HISPD;
1247 else
1248 ctrl &= ~SDHCI_CTRL_HISPD;
1249
Arindam Nathd6d50a12011-05-05 12:18:59 +05301250 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301251 u16 clk, ctrl_2;
1252 unsigned int clock;
1253
1254 /* In case of UHS-I modes, set High Speed Enable */
1255 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1256 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1257 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1258 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1259 (ios->timing == MMC_TIMING_UHS_SDR12))
1260 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301261
1262 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1263 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301264 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301265 /*
1266 * We only need to set Driver Strength if the
1267 * preset value enable is not set.
1268 */
1269 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1270 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1271 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1272 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1273 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1274
1275 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301276 } else {
1277 /*
1278 * According to SDHC Spec v3.00, if the Preset Value
1279 * Enable in the Host Control 2 register is set, we
1280 * need to reset SD Clock Enable before changing High
1281 * Speed Enable to avoid generating clock gliches.
1282 */
Arindam Nath758535c2011-05-05 12:19:00 +05301283
1284 /* Reset SD Clock Enable */
1285 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1286 clk &= ~SDHCI_CLOCK_CARD_EN;
1287 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1288
1289 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1290
1291 /* Re-enable SD Clock */
1292 clock = host->clock;
1293 host->clock = 0;
1294 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301295 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301296
1297 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1298
1299 /* Select Bus Speed Mode for host */
1300 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1301 if (ios->timing == MMC_TIMING_UHS_SDR12)
1302 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1303 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1304 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1305 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1306 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1307 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1308 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1309 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1310 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1311
1312 /* Reset SD Clock Enable */
1313 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1314 clk &= ~SDHCI_CLOCK_CARD_EN;
1315 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1316
1317 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1318
1319 /* Re-enable SD Clock */
1320 clock = host->clock;
1321 host->clock = 0;
1322 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301323 } else
1324 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301325
Leandro Dorileob8352262007-07-25 23:47:04 +02001326 /*
1327 * Some (ENE) controllers go apeshit on some ios operation,
1328 * signalling timeout and CRC errors even on CMD0. Resetting
1329 * it on each ios seems to solve the problem.
1330 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001331 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001332 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1333
Pierre Ossman1e728592008-04-16 19:13:13 +02001334out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001335 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 spin_unlock_irqrestore(&host->lock, flags);
1337}
1338
Takashi Iwai82b0e232011-04-21 20:26:38 +02001339static int check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001341 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001342 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 spin_lock_irqsave(&host->lock, flags);
1345
Pierre Ossman1e728592008-04-16 19:13:13 +02001346 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001347 is_readonly = 0;
1348 else if (host->ops->get_ro)
1349 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001350 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001351 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1352 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353
1354 spin_unlock_irqrestore(&host->lock, flags);
1355
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001356 /* This quirk needs to be replaced by a callback-function later */
1357 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1358 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359}
1360
Takashi Iwai82b0e232011-04-21 20:26:38 +02001361#define SAMPLE_COUNT 5
1362
1363static int sdhci_get_ro(struct mmc_host *mmc)
1364{
1365 struct sdhci_host *host;
1366 int i, ro_count;
1367
1368 host = mmc_priv(mmc);
1369
1370 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1371 return check_ro(host);
1372
1373 ro_count = 0;
1374 for (i = 0; i < SAMPLE_COUNT; i++) {
1375 if (check_ro(host)) {
1376 if (++ro_count > SAMPLE_COUNT / 2)
1377 return 1;
1378 }
1379 msleep(30);
1380 }
1381 return 0;
1382}
1383
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001384static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1385{
1386 struct sdhci_host *host;
1387 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001388
1389 host = mmc_priv(mmc);
1390
1391 spin_lock_irqsave(&host->lock, flags);
1392
Pierre Ossman1e728592008-04-16 19:13:13 +02001393 if (host->flags & SDHCI_DEVICE_DEAD)
1394 goto out;
1395
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001396 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001397 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1398 else
1399 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001400out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001401 mmiowb();
1402
1403 spin_unlock_irqrestore(&host->lock, flags);
1404}
1405
Arindam Nathf2119df2011-05-05 12:18:57 +05301406static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1407 struct mmc_ios *ios)
1408{
1409 struct sdhci_host *host;
1410 u8 pwr;
1411 u16 clk, ctrl;
1412 u32 present_state;
1413
1414 host = mmc_priv(mmc);
1415
1416 /*
1417 * Signal Voltage Switching is only applicable for Host Controllers
1418 * v3.00 and above.
1419 */
1420 if (host->version < SDHCI_SPEC_300)
1421 return 0;
1422
1423 /*
1424 * We first check whether the request is to set signalling voltage
1425 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1426 */
1427 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1428 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1429 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1430 ctrl &= ~SDHCI_CTRL_VDD_180;
1431 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1432
1433 /* Wait for 5ms */
1434 usleep_range(5000, 5500);
1435
1436 /* 3.3V regulator output should be stable within 5 ms */
1437 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1438 if (!(ctrl & SDHCI_CTRL_VDD_180))
1439 return 0;
1440 else {
1441 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1442 "signalling voltage failed\n");
1443 return -EIO;
1444 }
1445 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1446 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1447 /* Stop SDCLK */
1448 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1449 clk &= ~SDHCI_CLOCK_CARD_EN;
1450 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1451
1452 /* Check whether DAT[3:0] is 0000 */
1453 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1454 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1455 SDHCI_DATA_LVL_SHIFT)) {
1456 /*
1457 * Enable 1.8V Signal Enable in the Host Control2
1458 * register
1459 */
1460 ctrl |= SDHCI_CTRL_VDD_180;
1461 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1462
1463 /* Wait for 5ms */
1464 usleep_range(5000, 5500);
1465
1466 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1467 if (ctrl & SDHCI_CTRL_VDD_180) {
1468 /* Provide SDCLK again and wait for 1ms*/
1469 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1470 clk |= SDHCI_CLOCK_CARD_EN;
1471 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1472 usleep_range(1000, 1500);
1473
1474 /*
1475 * If DAT[3:0] level is 1111b, then the card
1476 * was successfully switched to 1.8V signaling.
1477 */
1478 present_state = sdhci_readl(host,
1479 SDHCI_PRESENT_STATE);
1480 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1481 SDHCI_DATA_LVL_MASK)
1482 return 0;
1483 }
1484 }
1485
1486 /*
1487 * If we are here, that means the switch to 1.8V signaling
1488 * failed. We power cycle the card, and retry initialization
1489 * sequence by setting S18R to 0.
1490 */
1491 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1492 pwr &= ~SDHCI_POWER_ON;
1493 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1494
1495 /* Wait for 1ms as per the spec */
1496 usleep_range(1000, 1500);
1497 pwr |= SDHCI_POWER_ON;
1498 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1499
1500 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1501 "voltage failed, retrying with S18R set to 0\n");
1502 return -EAGAIN;
1503 } else
1504 /* No signal voltage switch required */
1505 return 0;
1506}
1507
Arindam Nathb513ea22011-05-05 12:19:04 +05301508static int sdhci_execute_tuning(struct mmc_host *mmc)
1509{
1510 struct sdhci_host *host;
1511 u16 ctrl;
1512 u32 ier;
1513 int tuning_loop_counter = MAX_TUNING_LOOP;
1514 unsigned long timeout;
1515 int err = 0;
1516
1517 host = mmc_priv(mmc);
1518
1519 disable_irq(host->irq);
1520 spin_lock(&host->lock);
1521
1522 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1523
1524 /*
1525 * Host Controller needs tuning only in case of SDR104 mode
1526 * and for SDR50 mode when Use Tuning for SDR50 is set in
1527 * Capabilities register.
1528 */
1529 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1530 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1531 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1532 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1533 else {
1534 spin_unlock(&host->lock);
1535 enable_irq(host->irq);
1536 return 0;
1537 }
1538
1539 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1540
1541 /*
1542 * As per the Host Controller spec v3.00, tuning command
1543 * generates Buffer Read Ready interrupt, so enable that.
1544 *
1545 * Note: The spec clearly says that when tuning sequence
1546 * is being performed, the controller does not generate
1547 * interrupts other than Buffer Read Ready interrupt. But
1548 * to make sure we don't hit a controller bug, we _only_
1549 * enable Buffer Read Ready interrupt here.
1550 */
1551 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1552 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1553
1554 /*
1555 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1556 * of loops reaches 40 times or a timeout of 150ms occurs.
1557 */
1558 timeout = 150;
1559 do {
1560 struct mmc_command cmd = {0};
1561 struct mmc_request mrq = {0};
1562
1563 if (!tuning_loop_counter && !timeout)
1564 break;
1565
1566 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1567 cmd.arg = 0;
1568 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1569 cmd.retries = 0;
1570 cmd.data = NULL;
1571 cmd.error = 0;
1572
1573 mrq.cmd = &cmd;
1574 host->mrq = &mrq;
1575
1576 /*
1577 * In response to CMD19, the card sends 64 bytes of tuning
1578 * block to the Host Controller. So we set the block size
1579 * to 64 here.
1580 */
1581 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1582
1583 /*
1584 * The tuning block is sent by the card to the host controller.
1585 * So we set the TRNS_READ bit in the Transfer Mode register.
1586 * This also takes care of setting DMA Enable and Multi Block
1587 * Select in the same register to 0.
1588 */
1589 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1590
1591 sdhci_send_command(host, &cmd);
1592
1593 host->cmd = NULL;
1594 host->mrq = NULL;
1595
1596 spin_unlock(&host->lock);
1597 enable_irq(host->irq);
1598
1599 /* Wait for Buffer Read Ready interrupt */
1600 wait_event_interruptible_timeout(host->buf_ready_int,
1601 (host->tuning_done == 1),
1602 msecs_to_jiffies(50));
1603 disable_irq(host->irq);
1604 spin_lock(&host->lock);
1605
1606 if (!host->tuning_done) {
1607 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1608 "Buffer Read Ready interrupt during tuning "
1609 "procedure, falling back to fixed sampling "
1610 "clock\n");
1611 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1612 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1613 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1614 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1615
1616 err = -EIO;
1617 goto out;
1618 }
1619
1620 host->tuning_done = 0;
1621
1622 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1623 tuning_loop_counter--;
1624 timeout--;
1625 mdelay(1);
1626 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1627
1628 /*
1629 * The Host Driver has exhausted the maximum number of loops allowed,
1630 * so use fixed sampling frequency.
1631 */
1632 if (!tuning_loop_counter || !timeout) {
1633 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1634 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1635 } else {
1636 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1637 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1638 " failed, falling back to fixed sampling"
1639 " clock\n");
1640 err = -EIO;
1641 }
1642 }
1643
1644out:
1645 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1646 spin_unlock(&host->lock);
1647 enable_irq(host->irq);
1648
1649 return err;
1650}
1651
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301652static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1653{
1654 struct sdhci_host *host;
1655 u16 ctrl;
1656 unsigned long flags;
1657
1658 host = mmc_priv(mmc);
1659
1660 /* Host Controller v3.00 defines preset value registers */
1661 if (host->version < SDHCI_SPEC_300)
1662 return;
1663
1664 spin_lock_irqsave(&host->lock, flags);
1665
1666 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1667
1668 /*
1669 * We only enable or disable Preset Value if they are not already
1670 * enabled or disabled respectively. Otherwise, we bail out.
1671 */
1672 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1673 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1674 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1675 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1676 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1677 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1678 }
1679
1680 spin_unlock_irqrestore(&host->lock, flags);
1681}
1682
David Brownellab7aefd2006-11-12 17:55:30 -08001683static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001684 .request = sdhci_request,
1685 .set_ios = sdhci_set_ios,
1686 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001687 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301688 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301689 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301690 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001691};
1692
1693/*****************************************************************************\
1694 * *
1695 * Tasklets *
1696 * *
1697\*****************************************************************************/
1698
1699static void sdhci_tasklet_card(unsigned long param)
1700{
1701 struct sdhci_host *host;
1702 unsigned long flags;
1703
1704 host = (struct sdhci_host*)param;
1705
1706 spin_lock_irqsave(&host->lock, flags);
1707
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001708 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001709 if (host->mrq) {
1710 printk(KERN_ERR "%s: Card removed during transfer!\n",
1711 mmc_hostname(host->mmc));
1712 printk(KERN_ERR "%s: Resetting controller.\n",
1713 mmc_hostname(host->mmc));
1714
1715 sdhci_reset(host, SDHCI_RESET_CMD);
1716 sdhci_reset(host, SDHCI_RESET_DATA);
1717
Pierre Ossman17b04292007-07-22 22:18:46 +02001718 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001719 tasklet_schedule(&host->finish_tasklet);
1720 }
1721 }
1722
1723 spin_unlock_irqrestore(&host->lock, flags);
1724
Pierre Ossman04cf5852008-08-18 22:18:14 +02001725 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001726}
1727
1728static void sdhci_tasklet_finish(unsigned long param)
1729{
1730 struct sdhci_host *host;
1731 unsigned long flags;
1732 struct mmc_request *mrq;
1733
1734 host = (struct sdhci_host*)param;
1735
Chris Ball0c9c99a2011-04-27 17:35:31 -04001736 /*
1737 * If this tasklet gets rescheduled while running, it will
1738 * be run again afterwards but without any active request.
1739 */
1740 if (!host->mrq)
1741 return;
1742
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743 spin_lock_irqsave(&host->lock, flags);
1744
1745 del_timer(&host->timer);
1746
1747 mrq = host->mrq;
1748
Pierre Ossmand129bce2006-03-24 03:18:17 -08001749 /*
1750 * The controller needs a reset of internal state machines
1751 * upon error conditions.
1752 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001753 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001754 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001755 (mrq->data && (mrq->data->error ||
1756 (mrq->data->stop && mrq->data->stop->error))) ||
1757 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001758
1759 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001760 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001761 unsigned int clock;
1762
1763 /* This is to force an update */
1764 clock = host->clock;
1765 host->clock = 0;
1766 sdhci_set_clock(host, clock);
1767 }
1768
1769 /* Spec says we should do both at the same time, but Ricoh
1770 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001771 sdhci_reset(host, SDHCI_RESET_CMD);
1772 sdhci_reset(host, SDHCI_RESET_DATA);
1773 }
1774
1775 host->mrq = NULL;
1776 host->cmd = NULL;
1777 host->data = NULL;
1778
Pierre Ossmanf9134312008-12-21 17:01:48 +01001779#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001780 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001781#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001782
Pierre Ossman5f25a662006-10-04 02:15:39 -07001783 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001784 spin_unlock_irqrestore(&host->lock, flags);
1785
1786 mmc_request_done(host->mmc, mrq);
1787}
1788
1789static void sdhci_timeout_timer(unsigned long data)
1790{
1791 struct sdhci_host *host;
1792 unsigned long flags;
1793
1794 host = (struct sdhci_host*)data;
1795
1796 spin_lock_irqsave(&host->lock, flags);
1797
1798 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001799 printk(KERN_ERR "%s: Timeout waiting for hardware "
1800 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001801 sdhci_dumpregs(host);
1802
1803 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001804 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001805 sdhci_finish_data(host);
1806 } else {
1807 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001808 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001809 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001810 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811
1812 tasklet_schedule(&host->finish_tasklet);
1813 }
1814 }
1815
Pierre Ossman5f25a662006-10-04 02:15:39 -07001816 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001817 spin_unlock_irqrestore(&host->lock, flags);
1818}
1819
1820/*****************************************************************************\
1821 * *
1822 * Interrupt handling *
1823 * *
1824\*****************************************************************************/
1825
1826static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1827{
1828 BUG_ON(intmask == 0);
1829
1830 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001831 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1832 "though no command operation was in progress.\n",
1833 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001834 sdhci_dumpregs(host);
1835 return;
1836 }
1837
Pierre Ossman43b58b32007-07-25 23:15:27 +02001838 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001839 host->cmd->error = -ETIMEDOUT;
1840 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1841 SDHCI_INT_INDEX))
1842 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001843
Pierre Ossmane8095172008-07-25 01:09:08 +02001844 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001845 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001846 return;
1847 }
1848
1849 /*
1850 * The host can send and interrupt when the busy state has
1851 * ended, allowing us to wait without wasting CPU cycles.
1852 * Unfortunately this is overloaded on the "data complete"
1853 * interrupt, so we need to take some care when handling
1854 * it.
1855 *
1856 * Note: The 1.0 specification is a bit ambiguous about this
1857 * feature so there might be some problems with older
1858 * controllers.
1859 */
1860 if (host->cmd->flags & MMC_RSP_BUSY) {
1861 if (host->cmd->data)
1862 DBG("Cannot wait for busy signal when also "
1863 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001864 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001865 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001866
1867 /* The controller does not support the end-of-busy IRQ,
1868 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001869 }
1870
1871 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001872 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001873}
1874
George G. Davis0957c332010-02-18 12:32:12 -05001875#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001876static void sdhci_show_adma_error(struct sdhci_host *host)
1877{
1878 const char *name = mmc_hostname(host->mmc);
1879 u8 *desc = host->adma_desc;
1880 __le32 *dma;
1881 __le16 *len;
1882 u8 attr;
1883
1884 sdhci_dumpregs(host);
1885
1886 while (true) {
1887 dma = (__le32 *)(desc + 4);
1888 len = (__le16 *)(desc + 2);
1889 attr = *desc;
1890
1891 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1892 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1893
1894 desc += 8;
1895
1896 if (attr & 2)
1897 break;
1898 }
1899}
1900#else
1901static void sdhci_show_adma_error(struct sdhci_host *host) { }
1902#endif
1903
Pierre Ossmand129bce2006-03-24 03:18:17 -08001904static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1905{
1906 BUG_ON(intmask == 0);
1907
Arindam Nathb513ea22011-05-05 12:19:04 +05301908 /* CMD19 generates _only_ Buffer Read Ready interrupt */
1909 if (intmask & SDHCI_INT_DATA_AVAIL) {
1910 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
1911 MMC_SEND_TUNING_BLOCK) {
1912 host->tuning_done = 1;
1913 wake_up(&host->buf_ready_int);
1914 return;
1915 }
1916 }
1917
Pierre Ossmand129bce2006-03-24 03:18:17 -08001918 if (!host->data) {
1919 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001920 * The "data complete" interrupt is also used to
1921 * indicate that a busy state has ended. See comment
1922 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001923 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001924 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1925 if (intmask & SDHCI_INT_DATA_END) {
1926 sdhci_finish_command(host);
1927 return;
1928 }
1929 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001930
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001931 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1932 "though no data operation was in progress.\n",
1933 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001934 sdhci_dumpregs(host);
1935
1936 return;
1937 }
1938
1939 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001940 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001941 else if (intmask & SDHCI_INT_DATA_END_BIT)
1942 host->data->error = -EILSEQ;
1943 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1944 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1945 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001946 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001947 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1948 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1949 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001950 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001951 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001952
Pierre Ossman17b04292007-07-22 22:18:46 +02001953 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001954 sdhci_finish_data(host);
1955 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001956 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001957 sdhci_transfer_pio(host);
1958
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001959 /*
1960 * We currently don't do anything fancy with DMA
1961 * boundaries, but as we can't disable the feature
1962 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001963 *
1964 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
1965 * should return a valid address to continue from, but as
1966 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001967 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001968 if (intmask & SDHCI_INT_DMA_END) {
1969 u32 dmastart, dmanow;
1970 dmastart = sg_dma_address(host->data->sg);
1971 dmanow = dmastart + host->data->bytes_xfered;
1972 /*
1973 * Force update to the next DMA block boundary.
1974 */
1975 dmanow = (dmanow &
1976 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
1977 SDHCI_DEFAULT_BOUNDARY_SIZE;
1978 host->data->bytes_xfered = dmanow - dmastart;
1979 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
1980 " next 0x%08x\n",
1981 mmc_hostname(host->mmc), dmastart,
1982 host->data->bytes_xfered, dmanow);
1983 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
1984 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001985
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001986 if (intmask & SDHCI_INT_DATA_END) {
1987 if (host->cmd) {
1988 /*
1989 * Data managed to finish before the
1990 * command completed. Make sure we do
1991 * things in the proper order.
1992 */
1993 host->data_early = 1;
1994 } else {
1995 sdhci_finish_data(host);
1996 }
1997 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998 }
1999}
2000
David Howells7d12e782006-10-05 14:55:46 +01002001static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002002{
2003 irqreturn_t result;
2004 struct sdhci_host* host = dev_id;
2005 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002006 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002007
2008 spin_lock(&host->lock);
2009
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002010 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002011
Mark Lord62df67a52007-03-06 13:30:13 +01002012 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002013 result = IRQ_NONE;
2014 goto out;
2015 }
2016
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002017 DBG("*** %s got interrupt: 0x%08x\n",
2018 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002019
Pierre Ossman3192a282006-06-30 02:22:26 -07002020 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002021 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2022 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002023 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002024 }
2025
2026 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002027
2028 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002029 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2030 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002031 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002032 }
2033
2034 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002035 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2036 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002037 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002038 }
2039
2040 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2041
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002042 intmask &= ~SDHCI_INT_ERROR;
2043
Pierre Ossmand129bce2006-03-24 03:18:17 -08002044 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07002045 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002046 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002047 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002048 }
2049
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002050 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002051
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002052 if (intmask & SDHCI_INT_CARD_INT)
2053 cardint = 1;
2054
2055 intmask &= ~SDHCI_INT_CARD_INT;
2056
Pierre Ossman3192a282006-06-30 02:22:26 -07002057 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002058 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002059 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002060 sdhci_dumpregs(host);
2061
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002062 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002063 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002064
2065 result = IRQ_HANDLED;
2066
Pierre Ossman5f25a662006-10-04 02:15:39 -07002067 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002068out:
2069 spin_unlock(&host->lock);
2070
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002071 /*
2072 * We have to delay this as it calls back into the driver.
2073 */
2074 if (cardint)
2075 mmc_signal_sdio_irq(host->mmc);
2076
Pierre Ossmand129bce2006-03-24 03:18:17 -08002077 return result;
2078}
2079
2080/*****************************************************************************\
2081 * *
2082 * Suspend/resume *
2083 * *
2084\*****************************************************************************/
2085
2086#ifdef CONFIG_PM
2087
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002088int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002090 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002091
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002092 sdhci_disable_card_detection(host);
2093
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002094 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002095 if (ret)
2096 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002098 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002100 if (host->vmmc)
2101 ret = regulator_disable(host->vmmc);
2102
2103 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002104}
2105
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002106EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002107
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002108int sdhci_resume_host(struct sdhci_host *host)
2109{
2110 int ret;
2111
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002112 if (host->vmmc) {
2113 int ret = regulator_enable(host->vmmc);
2114 if (ret)
2115 return ret;
2116 }
2117
2118
Richard Röjforsa13abc72009-09-22 16:45:30 -07002119 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002120 if (host->ops->enable_dma)
2121 host->ops->enable_dma(host);
2122 }
2123
2124 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2125 mmc_hostname(host->mmc), host);
2126 if (ret)
2127 return ret;
2128
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002129 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002130 mmiowb();
2131
2132 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002133 sdhci_enable_card_detection(host);
2134
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002135 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002136}
2137
2138EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002139
Daniel Drake5f619702010-11-04 22:20:39 +00002140void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2141{
2142 u8 val;
2143 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2144 val |= SDHCI_WAKE_ON_INT;
2145 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2146}
2147
2148EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2149
Pierre Ossmand129bce2006-03-24 03:18:17 -08002150#endif /* CONFIG_PM */
2151
2152/*****************************************************************************\
2153 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002154 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155 * *
2156\*****************************************************************************/
2157
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002158struct sdhci_host *sdhci_alloc_host(struct device *dev,
2159 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002161 struct mmc_host *mmc;
2162 struct sdhci_host *host;
2163
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002164 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002165
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002166 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002168 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169
2170 host = mmc_priv(mmc);
2171 host->mmc = mmc;
2172
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002173 return host;
2174}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002175
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002176EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002178int sdhci_add_host(struct sdhci_host *host)
2179{
2180 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302181 u32 caps[2];
2182 u32 max_current_caps;
2183 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002184 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002186 WARN_ON(host == NULL);
2187 if (host == NULL)
2188 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002189
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002190 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002191
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002192 if (debug_quirks)
2193 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194
Pierre Ossmand96649e2006-06-30 02:22:30 -07002195 sdhci_reset(host, SDHCI_RESET_ALL);
2196
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002197 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002198 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2199 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002200 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07002201 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002202 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002203 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002204 }
2205
Arindam Nathf2119df2011-05-05 12:18:57 +05302206 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002207 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208
Arindam Nathf2119df2011-05-05 12:18:57 +05302209 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2210 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2211
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002212 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002213 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302214 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002215 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002216 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002217 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002218
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002219 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002220 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002221 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002222 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002223 }
2224
Arindam Nathf2119df2011-05-05 12:18:57 +05302225 if ((host->version >= SDHCI_SPEC_200) &&
2226 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002227 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002228
2229 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2230 (host->flags & SDHCI_USE_ADMA)) {
2231 DBG("Disabling ADMA as it is marked broken\n");
2232 host->flags &= ~SDHCI_USE_ADMA;
2233 }
2234
Richard Röjforsa13abc72009-09-22 16:45:30 -07002235 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002236 if (host->ops->enable_dma) {
2237 if (host->ops->enable_dma(host)) {
2238 printk(KERN_WARNING "%s: No suitable DMA "
2239 "available. Falling back to PIO.\n",
2240 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002241 host->flags &=
2242 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002243 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244 }
2245 }
2246
Pierre Ossman2134a922008-06-28 18:28:51 +02002247 if (host->flags & SDHCI_USE_ADMA) {
2248 /*
2249 * We need to allocate descriptors for all sg entries
2250 * (128) and potentially one alignment transfer for
2251 * each of those entries.
2252 */
2253 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2254 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2255 if (!host->adma_desc || !host->align_buffer) {
2256 kfree(host->adma_desc);
2257 kfree(host->align_buffer);
2258 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2259 "buffers. Falling back to standard DMA.\n",
2260 mmc_hostname(mmc));
2261 host->flags &= ~SDHCI_USE_ADMA;
2262 }
2263 }
2264
Pierre Ossman76591502008-07-21 00:32:11 +02002265 /*
2266 * If we use DMA, then it's up to the caller to set the DMA
2267 * mask, but PIO does not need the hw shim so we set a new
2268 * mask here in that case.
2269 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002270 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002271 host->dma_mask = DMA_BIT_MASK(64);
2272 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2273 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002275 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302276 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002277 >> SDHCI_CLOCK_BASE_SHIFT;
2278 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302279 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002280 >> SDHCI_CLOCK_BASE_SHIFT;
2281
Pierre Ossmand129bce2006-03-24 03:18:17 -08002282 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002283 if (host->max_clk == 0 || host->quirks &
2284 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002285 if (!host->ops->get_max_clock) {
2286 printk(KERN_ERR
2287 "%s: Hardware doesn't specify base clock "
2288 "frequency.\n", mmc_hostname(mmc));
2289 return -ENODEV;
2290 }
2291 host->max_clk = host->ops->get_max_clock(host);
2292 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002294 host->timeout_clk =
Arindam Nathf2119df2011-05-05 12:18:57 +05302295 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002296 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07002297 if (host->ops->get_timeout_clock) {
2298 host->timeout_clk = host->ops->get_timeout_clock(host);
2299 } else if (!(host->quirks &
2300 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002301 printk(KERN_ERR
2302 "%s: Hardware doesn't specify timeout clock "
2303 "frequency.\n", mmc_hostname(mmc));
2304 return -ENODEV;
2305 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002306 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302307 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002308 host->timeout_clk *= 1000;
2309
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310 /*
2311 * Set host parameters.
2312 */
2313 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002314 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002315 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04002316 else if (host->version >= SDHCI_SPEC_300)
2317 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002318 else
Zhangfei Gao03975262010-09-20 15:15:18 -04002319 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002320
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321 mmc->f_max = host->max_clk;
Andrei Warkentina3c77782011-04-11 16:13:42 -05002322 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002323
Philip Rakity15ec4462010-11-19 16:48:39 -05002324 /*
2325 * A controller may support 8-bit width, but the board itself
2326 * might not have the pins brought out. Boards that support
2327 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2328 * their platform code before calling sdhci_add_host(), and we
2329 * won't assume 8-bit width for hosts without that CAP.
2330 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002331 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002332 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002333
Arindam Nathf2119df2011-05-05 12:18:57 +05302334 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002335 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002336
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002337 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2338 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002339 mmc->caps |= MMC_CAP_NEEDS_POLL;
2340
Arindam Nathf2119df2011-05-05 12:18:57 +05302341 /* UHS-I mode(s) supported by the host controller. */
2342 if (host->version >= SDHCI_SPEC_300)
2343 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2344
2345 /* SDR104 supports also implies SDR50 support */
2346 if (caps[1] & SDHCI_SUPPORT_SDR104)
2347 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2348 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2349 mmc->caps |= MMC_CAP_UHS_SDR50;
2350
2351 if (caps[1] & SDHCI_SUPPORT_DDR50)
2352 mmc->caps |= MMC_CAP_UHS_DDR50;
2353
Arindam Nathb513ea22011-05-05 12:19:04 +05302354 /* Does the host needs tuning for SDR50? */
2355 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2356 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2357
Arindam Nathd6d50a12011-05-05 12:18:59 +05302358 /* Driver Type(s) (A, C, D) supported by the host */
2359 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2360 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2361 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2362 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2363 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2364 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2365
Takashi Iwai8f230f42010-12-08 10:04:30 +01002366 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302367 /*
2368 * According to SD Host Controller spec v3.00, if the Host System
2369 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2370 * the value is meaningful only if Voltage Support in the Capabilities
2371 * register is set. The actual current value is 4 times the register
2372 * value.
2373 */
2374 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2375
2376 if (caps[0] & SDHCI_CAN_VDD_330) {
2377 int max_current_330;
2378
Takashi Iwai8f230f42010-12-08 10:04:30 +01002379 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302380
2381 max_current_330 = ((max_current_caps &
2382 SDHCI_MAX_CURRENT_330_MASK) >>
2383 SDHCI_MAX_CURRENT_330_SHIFT) *
2384 SDHCI_MAX_CURRENT_MULTIPLIER;
2385
2386 if (max_current_330 > 150)
2387 mmc->caps |= MMC_CAP_SET_XPC_330;
2388 }
2389 if (caps[0] & SDHCI_CAN_VDD_300) {
2390 int max_current_300;
2391
Takashi Iwai8f230f42010-12-08 10:04:30 +01002392 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302393
2394 max_current_300 = ((max_current_caps &
2395 SDHCI_MAX_CURRENT_300_MASK) >>
2396 SDHCI_MAX_CURRENT_300_SHIFT) *
2397 SDHCI_MAX_CURRENT_MULTIPLIER;
2398
2399 if (max_current_300 > 150)
2400 mmc->caps |= MMC_CAP_SET_XPC_300;
2401 }
2402 if (caps[0] & SDHCI_CAN_VDD_180) {
2403 int max_current_180;
2404
Takashi Iwai8f230f42010-12-08 10:04:30 +01002405 ocr_avail |= MMC_VDD_165_195;
2406
Arindam Nathf2119df2011-05-05 12:18:57 +05302407 max_current_180 = ((max_current_caps &
2408 SDHCI_MAX_CURRENT_180_MASK) >>
2409 SDHCI_MAX_CURRENT_180_SHIFT) *
2410 SDHCI_MAX_CURRENT_MULTIPLIER;
2411
2412 if (max_current_180 > 150)
2413 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302414
2415 /* Maximum current capabilities of the host at 1.8V */
2416 if (max_current_180 >= 800)
2417 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2418 else if (max_current_180 >= 600)
2419 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2420 else if (max_current_180 >= 400)
2421 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2422 else
2423 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302424 }
2425
Takashi Iwai8f230f42010-12-08 10:04:30 +01002426 mmc->ocr_avail = ocr_avail;
2427 mmc->ocr_avail_sdio = ocr_avail;
2428 if (host->ocr_avail_sdio)
2429 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2430 mmc->ocr_avail_sd = ocr_avail;
2431 if (host->ocr_avail_sd)
2432 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2433 else /* normal SD controllers don't support 1.8V */
2434 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2435 mmc->ocr_avail_mmc = ocr_avail;
2436 if (host->ocr_avail_mmc)
2437 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002438
2439 if (mmc->ocr_avail == 0) {
2440 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002441 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002442 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002443 }
2444
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445 spin_lock_init(&host->lock);
2446
2447 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002448 * Maximum number of segments. Depends on if the hardware
2449 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002451 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002452 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002453 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002454 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002455 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002456 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457
2458 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002459 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002460 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002462 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002463
2464 /*
2465 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002466 * of bytes. When doing hardware scatter/gather, each entry cannot
2467 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002468 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002469 if (host->flags & SDHCI_USE_ADMA) {
2470 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2471 mmc->max_seg_size = 65535;
2472 else
2473 mmc->max_seg_size = 65536;
2474 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002475 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002476 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477
2478 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002479 * Maximum block size. This varies from controller to controller and
2480 * is specified in the capabilities register.
2481 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002482 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2483 mmc->max_blk_size = 2;
2484 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302485 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002486 SDHCI_MAX_BLOCK_SHIFT;
2487 if (mmc->max_blk_size >= 3) {
2488 printk(KERN_WARNING "%s: Invalid maximum block size, "
2489 "assuming 512 bytes\n", mmc_hostname(mmc));
2490 mmc->max_blk_size = 0;
2491 }
2492 }
2493
2494 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002495
2496 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002497 * Maximum block count.
2498 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002499 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002500
2501 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002502 * Init tasklets.
2503 */
2504 tasklet_init(&host->card_tasklet,
2505 sdhci_tasklet_card, (unsigned long)host);
2506 tasklet_init(&host->finish_tasklet,
2507 sdhci_tasklet_finish, (unsigned long)host);
2508
Al Viroe4cad1b2006-10-10 22:47:07 +01002509 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002510
Arindam Nathb513ea22011-05-05 12:19:04 +05302511 if (host->version >= SDHCI_SPEC_300)
2512 init_waitqueue_head(&host->buf_ready_int);
2513
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002514 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002515 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002516 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002517 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002519 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2520 if (IS_ERR(host->vmmc)) {
2521 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2522 host->vmmc = NULL;
2523 } else {
2524 regulator_enable(host->vmmc);
2525 }
2526
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002527 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
2529#ifdef CONFIG_MMC_DEBUG
2530 sdhci_dumpregs(host);
2531#endif
2532
Pierre Ossmanf9134312008-12-21 17:01:48 +01002533#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002534 snprintf(host->led_name, sizeof(host->led_name),
2535 "%s::", mmc_hostname(mmc));
2536 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002537 host->led.brightness = LED_OFF;
2538 host->led.default_trigger = mmc_hostname(mmc);
2539 host->led.brightness_set = sdhci_led_control;
2540
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002541 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002542 if (ret)
2543 goto reset;
2544#endif
2545
Pierre Ossman5f25a662006-10-04 02:15:39 -07002546 mmiowb();
2547
Pierre Ossmand129bce2006-03-24 03:18:17 -08002548 mmc_add_host(mmc);
2549
Richard Röjforsa13abc72009-09-22 16:45:30 -07002550 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002551 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002552 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2553 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002554
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002555 sdhci_enable_card_detection(host);
2556
Pierre Ossmand129bce2006-03-24 03:18:17 -08002557 return 0;
2558
Pierre Ossmanf9134312008-12-21 17:01:48 +01002559#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002560reset:
2561 sdhci_reset(host, SDHCI_RESET_ALL);
2562 free_irq(host->irq, host);
2563#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002564untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002565 tasklet_kill(&host->card_tasklet);
2566 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567
2568 return ret;
2569}
2570
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002571EXPORT_SYMBOL_GPL(sdhci_add_host);
2572
Pierre Ossman1e728592008-04-16 19:13:13 +02002573void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002574{
Pierre Ossman1e728592008-04-16 19:13:13 +02002575 unsigned long flags;
2576
2577 if (dead) {
2578 spin_lock_irqsave(&host->lock, flags);
2579
2580 host->flags |= SDHCI_DEVICE_DEAD;
2581
2582 if (host->mrq) {
2583 printk(KERN_ERR "%s: Controller removed during "
2584 " transfer!\n", mmc_hostname(host->mmc));
2585
2586 host->mrq->cmd->error = -ENOMEDIUM;
2587 tasklet_schedule(&host->finish_tasklet);
2588 }
2589
2590 spin_unlock_irqrestore(&host->lock, flags);
2591 }
2592
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002593 sdhci_disable_card_detection(host);
2594
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002595 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002596
Pierre Ossmanf9134312008-12-21 17:01:48 +01002597#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002598 led_classdev_unregister(&host->led);
2599#endif
2600
Pierre Ossman1e728592008-04-16 19:13:13 +02002601 if (!dead)
2602 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002603
2604 free_irq(host->irq, host);
2605
2606 del_timer_sync(&host->timer);
2607
2608 tasklet_kill(&host->card_tasklet);
2609 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002610
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002611 if (host->vmmc) {
2612 regulator_disable(host->vmmc);
2613 regulator_put(host->vmmc);
2614 }
2615
Pierre Ossman2134a922008-06-28 18:28:51 +02002616 kfree(host->adma_desc);
2617 kfree(host->align_buffer);
2618
2619 host->adma_desc = NULL;
2620 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002621}
2622
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002623EXPORT_SYMBOL_GPL(sdhci_remove_host);
2624
2625void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002627 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002628}
2629
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002630EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631
2632/*****************************************************************************\
2633 * *
2634 * Driver init/exit *
2635 * *
2636\*****************************************************************************/
2637
2638static int __init sdhci_drv_init(void)
2639{
2640 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002641 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002642 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2643
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002644 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002645}
2646
2647static void __exit sdhci_drv_exit(void)
2648{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002649}
2650
2651module_init(sdhci_drv_init);
2652module_exit(sdhci_drv_exit);
2653
Pierre Ossmandf673b22006-06-30 02:22:31 -07002654module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002655
Pierre Ossman32710e82009-04-08 20:14:54 +02002656MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002657MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002658MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002659
Pierre Ossmandf673b22006-06-30 02:22:31 -07002660MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");