blob: 96c7f6010d0d83c0a438ad04b7ee3a1c04a8a92b [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
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossmand129bce2006-03-24 03:18:17 -080028#include "sdhci.h"
29
30#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080031
Pierre Ossmand129bce2006-03-24 03:18:17 -080032#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010033 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmanf9134312008-12-21 17:01:48 +010035#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
36 defined(CONFIG_MMC_SDHCI_MODULE))
37#define SDHCI_USE_LEDS_CLASS
38#endif
39
Pierre Ossmandf673b22006-06-30 02:22:31 -070040static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070041
Pierre Ossmand129bce2006-03-24 03:18:17 -080042static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
43static void sdhci_finish_data(struct sdhci_host *);
44
45static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46static void sdhci_finish_command(struct sdhci_host *);
47
48static void sdhci_dumpregs(struct sdhci_host *host)
49{
Philip Rakity412ab652010-09-22 15:25:13 -070050 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
51 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readl(host, SDHCI_DMA_ADDRESS),
55 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readw(host, SDHCI_BLOCK_SIZE),
58 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_ARGUMENT),
61 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readl(host, SDHCI_PRESENT_STATE),
64 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_POWER_CONTROL),
67 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
70 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
73 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_INT_ENABLE),
76 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_ACMD12_ERR),
79 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080080 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_CAPABILITIES),
82 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080083
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010084 if (host->flags & SDHCI_USE_ADMA)
85 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
86 readl(host->ioaddr + SDHCI_ADMA_ERROR),
87 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
88
Pierre Ossmand129bce2006-03-24 03:18:17 -080089 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
90}
91
92/*****************************************************************************\
93 * *
94 * Low level functions *
95 * *
96\*****************************************************************************/
97
Anton Vorontsov7260cf52009-03-17 00:13:48 +030098static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
99{
100 u32 ier;
101
102 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
103 ier &= ~clear;
104 ier |= set;
105 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
106 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
107}
108
109static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
110{
111 sdhci_clear_set_irqs(host, 0, irqs);
112}
113
114static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
115{
116 sdhci_clear_set_irqs(host, irqs, 0);
117}
118
119static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
120{
121 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
122
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300123 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
124 return;
125
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300126 if (enable)
127 sdhci_unmask_irqs(host, irqs);
128 else
129 sdhci_mask_irqs(host, irqs);
130}
131
132static void sdhci_enable_card_detection(struct sdhci_host *host)
133{
134 sdhci_set_card_detection(host, true);
135}
136
137static void sdhci_disable_card_detection(struct sdhci_host *host)
138{
139 sdhci_set_card_detection(host, false);
140}
141
Pierre Ossmand129bce2006-03-24 03:18:17 -0800142static void sdhci_reset(struct sdhci_host *host, u8 mask)
143{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700144 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300145 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700146
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100147 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300148 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700149 SDHCI_CARD_PRESENT))
150 return;
151 }
152
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300153 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
154 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
155
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300156 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800157
Pierre Ossmane16514d82006-06-30 02:22:24 -0700158 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800159 host->clock = 0;
160
Pierre Ossmane16514d82006-06-30 02:22:24 -0700161 /* Wait max 100 ms */
162 timeout = 100;
163
164 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300165 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700166 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100167 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 mmc_hostname(host->mmc), (int)mask);
169 sdhci_dumpregs(host);
170 return;
171 }
172 timeout--;
173 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300175
176 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
177 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178}
179
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800180static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
181
182static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800184 if (soft)
185 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
186 else
187 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300189 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
190 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700191 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
192 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300193 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800194
195 if (soft) {
196 /* force clock reconfiguration */
197 host->clock = 0;
198 sdhci_set_ios(host->mmc, &host->mmc->ios);
199 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300200}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300202static void sdhci_reinit(struct sdhci_host *host)
203{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800204 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300205 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800206}
207
208static void sdhci_activate_led(struct sdhci_host *host)
209{
210 u8 ctrl;
211
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300212 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800213 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300214 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
217static void sdhci_deactivate_led(struct sdhci_host *host)
218{
219 u8 ctrl;
220
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300221 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300223 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
Pierre Ossmanf9134312008-12-21 17:01:48 +0100226#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100227static void sdhci_led_control(struct led_classdev *led,
228 enum led_brightness brightness)
229{
230 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
231 unsigned long flags;
232
233 spin_lock_irqsave(&host->lock, flags);
234
235 if (brightness == LED_OFF)
236 sdhci_deactivate_led(host);
237 else
238 sdhci_activate_led(host);
239
240 spin_unlock_irqrestore(&host->lock, flags);
241}
242#endif
243
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244/*****************************************************************************\
245 * *
246 * Core functions *
247 * *
248\*****************************************************************************/
249
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100250static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251{
Pierre Ossman76591502008-07-21 00:32:11 +0200252 unsigned long flags;
253 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700254 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200255 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100257 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100259 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200260 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Pierre Ossman76591502008-07-21 00:32:11 +0200262 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100264 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200265 if (!sg_miter_next(&host->sg_miter))
266 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267
Pierre Ossman76591502008-07-21 00:32:11 +0200268 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269
Pierre Ossman76591502008-07-21 00:32:11 +0200270 blksize -= len;
271 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200272
Pierre Ossman76591502008-07-21 00:32:11 +0200273 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274
Pierre Ossman76591502008-07-21 00:32:11 +0200275 while (len) {
276 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300277 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200278 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279 }
Pierre Ossman76591502008-07-21 00:32:11 +0200280
281 *buf = scratch & 0xFF;
282
283 buf++;
284 scratch >>= 8;
285 chunk--;
286 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800287 }
288 }
Pierre Ossman76591502008-07-21 00:32:11 +0200289
290 sg_miter_stop(&host->sg_miter);
291
292 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100293}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800294
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100295static void sdhci_write_block_pio(struct sdhci_host *host)
296{
Pierre Ossman76591502008-07-21 00:32:11 +0200297 unsigned long flags;
298 size_t blksize, len, chunk;
299 u32 scratch;
300 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100301
302 DBG("PIO writing\n");
303
304 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200305 chunk = 0;
306 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100307
Pierre Ossman76591502008-07-21 00:32:11 +0200308 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309
310 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200311 if (!sg_miter_next(&host->sg_miter))
312 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 blksize -= len;
317 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 while (len) {
322 scratch |= (u32)*buf << (chunk * 8);
323
324 buf++;
325 chunk++;
326 len--;
327
328 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300329 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200330 chunk = 0;
331 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100332 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100333 }
334 }
Pierre Ossman76591502008-07-21 00:32:11 +0200335
336 sg_miter_stop(&host->sg_miter);
337
338 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100339}
340
341static void sdhci_transfer_pio(struct sdhci_host *host)
342{
343 u32 mask;
344
345 BUG_ON(!host->data);
346
Pierre Ossman76591502008-07-21 00:32:11 +0200347 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348 return;
349
350 if (host->data->flags & MMC_DATA_READ)
351 mask = SDHCI_DATA_AVAILABLE;
352 else
353 mask = SDHCI_SPACE_AVAILABLE;
354
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200355 /*
356 * Some controllers (JMicron JMB38x) mess up the buffer bits
357 * for transfers < 4 bytes. As long as it is just one block,
358 * we can ignore the bits.
359 */
360 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
361 (host->data->blocks == 1))
362 mask = ~0;
363
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300364 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300365 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
366 udelay(100);
367
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368 if (host->data->flags & MMC_DATA_READ)
369 sdhci_read_block_pio(host);
370 else
371 sdhci_write_block_pio(host);
372
Pierre Ossman76591502008-07-21 00:32:11 +0200373 host->blocks--;
374 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376 }
377
378 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800379}
380
Pierre Ossman2134a922008-06-28 18:28:51 +0200381static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
382{
383 local_irq_save(*flags);
384 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
385}
386
387static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
388{
389 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
390 local_irq_restore(*flags);
391}
392
Ben Dooks118cd172010-03-05 13:43:26 -0800393static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
394{
Ben Dooks9e506f32010-03-05 13:43:29 -0800395 __le32 *dataddr = (__le32 __force *)(desc + 4);
396 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800397
Ben Dooks9e506f32010-03-05 13:43:29 -0800398 /* SDHCI specification says ADMA descriptors should be 4 byte
399 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800400
Ben Dooks9e506f32010-03-05 13:43:29 -0800401 cmdlen[0] = cpu_to_le16(cmd);
402 cmdlen[1] = cpu_to_le16(len);
403
404 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800405}
406
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200407static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200408 struct mmc_data *data)
409{
410 int direction;
411
412 u8 *desc;
413 u8 *align;
414 dma_addr_t addr;
415 dma_addr_t align_addr;
416 int len, offset;
417
418 struct scatterlist *sg;
419 int i;
420 char *buffer;
421 unsigned long flags;
422
423 /*
424 * The spec does not specify endianness of descriptor table.
425 * We currently guess that it is LE.
426 */
427
428 if (data->flags & MMC_DATA_READ)
429 direction = DMA_FROM_DEVICE;
430 else
431 direction = DMA_TO_DEVICE;
432
433 /*
434 * The ADMA descriptor table is mapped further down as we
435 * need to fill it with data first.
436 */
437
438 host->align_addr = dma_map_single(mmc_dev(host->mmc),
439 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700440 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200441 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200442 BUG_ON(host->align_addr & 0x3);
443
444 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
445 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200446 if (host->sg_count == 0)
447 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200448
449 desc = host->adma_desc;
450 align = host->align_buffer;
451
452 align_addr = host->align_addr;
453
454 for_each_sg(data->sg, sg, host->sg_count, i) {
455 addr = sg_dma_address(sg);
456 len = sg_dma_len(sg);
457
458 /*
459 * The SDHCI specification states that ADMA
460 * addresses must be 32-bit aligned. If they
461 * aren't, then we use a bounce buffer for
462 * the (up to three) bytes that screw up the
463 * alignment.
464 */
465 offset = (4 - (addr & 0x3)) & 0x3;
466 if (offset) {
467 if (data->flags & MMC_DATA_WRITE) {
468 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200469 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200470 memcpy(align, buffer, offset);
471 sdhci_kunmap_atomic(buffer, &flags);
472 }
473
Ben Dooks118cd172010-03-05 13:43:26 -0800474 /* tran, valid */
475 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200476
477 BUG_ON(offset > 65536);
478
Pierre Ossman2134a922008-06-28 18:28:51 +0200479 align += 4;
480 align_addr += 4;
481
482 desc += 8;
483
484 addr += offset;
485 len -= offset;
486 }
487
Pierre Ossman2134a922008-06-28 18:28:51 +0200488 BUG_ON(len > 65536);
489
Ben Dooks118cd172010-03-05 13:43:26 -0800490 /* tran, valid */
491 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 desc += 8;
493
494 /*
495 * If this triggers then we have a calculation bug
496 * somewhere. :/
497 */
498 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
499 }
500
Thomas Abraham70764a92010-05-26 14:42:04 -0700501 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
502 /*
503 * Mark the last descriptor as the terminating descriptor
504 */
505 if (desc != host->adma_desc) {
506 desc -= 8;
507 desc[0] |= 0x2; /* end */
508 }
509 } else {
510 /*
511 * Add a terminating entry.
512 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200513
Thomas Abraham70764a92010-05-26 14:42:04 -0700514 /* nop, end, valid */
515 sdhci_set_adma_desc(desc, 0, 0, 0x3);
516 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200517
518 /*
519 * Resync align buffer as we might have changed it.
520 */
521 if (data->flags & MMC_DATA_WRITE) {
522 dma_sync_single_for_device(mmc_dev(host->mmc),
523 host->align_addr, 128 * 4, direction);
524 }
525
526 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
527 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200528 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200529 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200531
532 return 0;
533
534unmap_entries:
535 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
536 data->sg_len, direction);
537unmap_align:
538 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
539 128 * 4, direction);
540fail:
541 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200542}
543
544static void sdhci_adma_table_post(struct sdhci_host *host,
545 struct mmc_data *data)
546{
547 int direction;
548
549 struct scatterlist *sg;
550 int i, size;
551 u8 *align;
552 char *buffer;
553 unsigned long flags;
554
555 if (data->flags & MMC_DATA_READ)
556 direction = DMA_FROM_DEVICE;
557 else
558 direction = DMA_TO_DEVICE;
559
560 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
561 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
562
563 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
564 128 * 4, direction);
565
566 if (data->flags & MMC_DATA_READ) {
567 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
568 data->sg_len, direction);
569
570 align = host->align_buffer;
571
572 for_each_sg(data->sg, sg, host->sg_count, i) {
573 if (sg_dma_address(sg) & 0x3) {
574 size = 4 - (sg_dma_address(sg) & 0x3);
575
576 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200577 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200578 memcpy(buffer, align, size);
579 sdhci_kunmap_atomic(buffer, &flags);
580
581 align += 4;
582 }
583 }
584 }
585
586 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
587 data->sg_len, direction);
588}
589
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200590static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800591{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700592 u8 count;
593 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800594
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200595 /*
596 * If the host controller provides us with an incorrect timeout
597 * value, just skip the check and use 0xE. The hardware may take
598 * longer to time out, but that's much better than having a too-short
599 * timeout value.
600 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200601 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200602 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200603
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700604 /* timeout in us */
605 target_timeout = data->timeout_ns / 1000 +
606 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800607
Anton Vorontsov81b39802009-09-22 16:45:13 -0700608 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
609 host->timeout_clk = host->clock / 1000;
610
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700611 /*
612 * Figure out needed cycles.
613 * We do this in steps in order to fit inside a 32 bit int.
614 * The first step is the minimum timeout, which will have a
615 * minimum resolution of 6 bits:
616 * (1) 2^13*1000 > 2^22,
617 * (2) host->timeout_clk < 2^16
618 * =>
619 * (1) / (2) > 2^6
620 */
621 count = 0;
622 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
623 while (current_timeout < target_timeout) {
624 count++;
625 current_timeout <<= 1;
626 if (count >= 0xF)
627 break;
628 }
629
630 if (count >= 0xF) {
631 printk(KERN_WARNING "%s: Too large timeout requested!\n",
632 mmc_hostname(host->mmc));
633 count = 0xE;
634 }
635
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200636 return count;
637}
638
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300639static void sdhci_set_transfer_irqs(struct sdhci_host *host)
640{
641 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
642 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
643
644 if (host->flags & SDHCI_REQ_USE_DMA)
645 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
646 else
647 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
648}
649
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200650static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
651{
652 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200653 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200654 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200655
656 WARN_ON(host->data);
657
658 if (data == NULL)
659 return;
660
661 /* Sanity checks */
662 BUG_ON(data->blksz * data->blocks > 524288);
663 BUG_ON(data->blksz > host->mmc->max_blk_size);
664 BUG_ON(data->blocks > 65535);
665
666 host->data = data;
667 host->data_early = 0;
668
669 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300670 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800671
Richard Röjforsa13abc72009-09-22 16:45:30 -0700672 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100673 host->flags |= SDHCI_REQ_USE_DMA;
674
Pierre Ossman2134a922008-06-28 18:28:51 +0200675 /*
676 * FIXME: This doesn't account for merging when mapping the
677 * scatterlist.
678 */
679 if (host->flags & SDHCI_REQ_USE_DMA) {
680 int broken, i;
681 struct scatterlist *sg;
682
683 broken = 0;
684 if (host->flags & SDHCI_USE_ADMA) {
685 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
686 broken = 1;
687 } else {
688 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
689 broken = 1;
690 }
691
692 if (unlikely(broken)) {
693 for_each_sg(data->sg, sg, data->sg_len, i) {
694 if (sg->length & 0x3) {
695 DBG("Reverting to PIO because of "
696 "transfer size (%d)\n",
697 sg->length);
698 host->flags &= ~SDHCI_REQ_USE_DMA;
699 break;
700 }
701 }
702 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100703 }
704
705 /*
706 * The assumption here being that alignment is the same after
707 * translation to device address space.
708 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200709 if (host->flags & SDHCI_REQ_USE_DMA) {
710 int broken, i;
711 struct scatterlist *sg;
712
713 broken = 0;
714 if (host->flags & SDHCI_USE_ADMA) {
715 /*
716 * As we use 3 byte chunks to work around
717 * alignment problems, we need to check this
718 * quirk.
719 */
720 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
721 broken = 1;
722 } else {
723 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
724 broken = 1;
725 }
726
727 if (unlikely(broken)) {
728 for_each_sg(data->sg, sg, data->sg_len, i) {
729 if (sg->offset & 0x3) {
730 DBG("Reverting to PIO because of "
731 "bad alignment\n");
732 host->flags &= ~SDHCI_REQ_USE_DMA;
733 break;
734 }
735 }
736 }
737 }
738
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200739 if (host->flags & SDHCI_REQ_USE_DMA) {
740 if (host->flags & SDHCI_USE_ADMA) {
741 ret = sdhci_adma_table_pre(host, data);
742 if (ret) {
743 /*
744 * This only happens when someone fed
745 * us an invalid request.
746 */
747 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200748 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200749 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300750 sdhci_writel(host, host->adma_addr,
751 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200752 }
753 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300754 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300756 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200757 data->sg, data->sg_len,
758 (data->flags & MMC_DATA_READ) ?
759 DMA_FROM_DEVICE :
760 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300761 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200762 /*
763 * This only happens when someone fed
764 * us an invalid request.
765 */
766 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200767 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200768 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200769 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300770 sdhci_writel(host, sg_dma_address(data->sg),
771 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200772 }
773 }
774 }
775
Pierre Ossman2134a922008-06-28 18:28:51 +0200776 /*
777 * Always adjust the DMA selection as some controllers
778 * (e.g. JMicron) can't do PIO properly when the selection
779 * is ADMA.
780 */
781 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300782 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200783 ctrl &= ~SDHCI_CTRL_DMA_MASK;
784 if ((host->flags & SDHCI_REQ_USE_DMA) &&
785 (host->flags & SDHCI_USE_ADMA))
786 ctrl |= SDHCI_CTRL_ADMA32;
787 else
788 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300789 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100790 }
791
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200792 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200793 int flags;
794
795 flags = SG_MITER_ATOMIC;
796 if (host->data->flags & MMC_DATA_READ)
797 flags |= SG_MITER_TO_SG;
798 else
799 flags |= SG_MITER_FROM_SG;
800 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200801 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800802 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700803
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300804 sdhci_set_transfer_irqs(host);
805
Pierre Ossmanbab76962006-07-02 16:51:35 +0100806 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300807 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
808 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700809}
810
811static void sdhci_set_transfer_mode(struct sdhci_host *host,
812 struct mmc_data *data)
813{
814 u16 mode;
815
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700816 if (data == NULL)
817 return;
818
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200819 WARN_ON(!host->data);
820
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700821 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700822 if (data->blocks > 1) {
823 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
824 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
825 else
826 mode |= SDHCI_TRNS_MULTI;
827 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700828 if (data->flags & MMC_DATA_READ)
829 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100830 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700831 mode |= SDHCI_TRNS_DMA;
832
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300833 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800834}
835
836static void sdhci_finish_data(struct sdhci_host *host)
837{
838 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800839
840 BUG_ON(!host->data);
841
842 data = host->data;
843 host->data = NULL;
844
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100845 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200846 if (host->flags & SDHCI_USE_ADMA)
847 sdhci_adma_table_post(host, data);
848 else {
849 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
850 data->sg_len, (data->flags & MMC_DATA_READ) ?
851 DMA_FROM_DEVICE : DMA_TO_DEVICE);
852 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800853 }
854
855 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200856 * The specification states that the block count register must
857 * be updated, but it does not specify at what point in the
858 * data flow. That makes the register entirely useless to read
859 * back so we have to assume that nothing made it to the card
860 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800861 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200862 if (data->error)
863 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800864 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200865 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866
Pierre Ossmand129bce2006-03-24 03:18:17 -0800867 if (data->stop) {
868 /*
869 * The controller needs a reset of internal state machines
870 * upon error conditions.
871 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200872 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800873 sdhci_reset(host, SDHCI_RESET_CMD);
874 sdhci_reset(host, SDHCI_RESET_DATA);
875 }
876
877 sdhci_send_command(host, data->stop);
878 } else
879 tasklet_schedule(&host->finish_tasklet);
880}
881
882static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
883{
884 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700885 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700886 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887
888 WARN_ON(host->cmd);
889
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700891 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700892
893 mask = SDHCI_CMD_INHIBIT;
894 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
895 mask |= SDHCI_DATA_INHIBIT;
896
897 /* We shouldn't wait for data inihibit for stop commands, even
898 though they might use busy signaling */
899 if (host->mrq->data && (cmd == host->mrq->data->stop))
900 mask &= ~SDHCI_DATA_INHIBIT;
901
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300902 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700903 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800904 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100905 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800906 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200907 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908 tasklet_schedule(&host->finish_tasklet);
909 return;
910 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700911 timeout--;
912 mdelay(1);
913 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914
915 mod_timer(&host->timer, jiffies + 10 * HZ);
916
917 host->cmd = cmd;
918
919 sdhci_prepare_data(host, cmd->data);
920
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300921 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923 sdhci_set_transfer_mode(host, cmd->data);
924
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100926 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200928 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 tasklet_schedule(&host->finish_tasklet);
930 return;
931 }
932
933 if (!(cmd->flags & MMC_RSP_PRESENT))
934 flags = SDHCI_CMD_RESP_NONE;
935 else if (cmd->flags & MMC_RSP_136)
936 flags = SDHCI_CMD_RESP_LONG;
937 else if (cmd->flags & MMC_RSP_BUSY)
938 flags = SDHCI_CMD_RESP_SHORT_BUSY;
939 else
940 flags = SDHCI_CMD_RESP_SHORT;
941
942 if (cmd->flags & MMC_RSP_CRC)
943 flags |= SDHCI_CMD_CRC;
944 if (cmd->flags & MMC_RSP_OPCODE)
945 flags |= SDHCI_CMD_INDEX;
946 if (cmd->data)
947 flags |= SDHCI_CMD_DATA;
948
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950}
951
952static void sdhci_finish_command(struct sdhci_host *host)
953{
954 int i;
955
956 BUG_ON(host->cmd == NULL);
957
958 if (host->cmd->flags & MMC_RSP_PRESENT) {
959 if (host->cmd->flags & MMC_RSP_136) {
960 /* CRC is stripped so we need to do some shifting. */
961 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300962 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963 SDHCI_RESPONSE + (3-i)*4) << 8;
964 if (i != 3)
965 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300966 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 SDHCI_RESPONSE + (3-i)*4-1);
968 }
969 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300970 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 }
972 }
973
Pierre Ossman17b04292007-07-22 22:18:46 +0200974 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200976 if (host->data && host->data_early)
977 sdhci_finish_data(host);
978
979 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 tasklet_schedule(&host->finish_tasklet);
981
982 host->cmd = NULL;
983}
984
985static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
986{
987 int div;
988 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700989 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990
991 if (clock == host->clock)
992 return;
993
Anton Vorontsov81146342009-03-17 00:13:59 +0300994 if (host->ops->set_clock) {
995 host->ops->set_clock(host, clock);
996 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
997 return;
998 }
999
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001000 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001
1002 if (clock == 0)
1003 goto out;
1004
Zhangfei Gao85105c52010-08-06 07:10:01 +08001005 if (host->version >= SDHCI_SPEC_300) {
1006 /* Version 3.00 divisors must be a multiple of 2. */
1007 if (host->max_clk <= clock)
1008 div = 1;
1009 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001010 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001011 if ((host->max_clk / div) <= clock)
1012 break;
1013 }
1014 }
1015 } else {
1016 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001017 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001018 if ((host->max_clk / div) <= clock)
1019 break;
1020 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 }
1022 div >>= 1;
1023
Zhangfei Gao85105c52010-08-06 07:10:01 +08001024 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1025 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1026 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001028 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029
Chris Ball27f6cb12009-09-22 16:45:31 -07001030 /* Wait max 20 ms */
1031 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001032 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001033 & SDHCI_CLOCK_INT_STABLE)) {
1034 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001035 printk(KERN_ERR "%s: Internal clock never "
1036 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001037 sdhci_dumpregs(host);
1038 return;
1039 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001040 timeout--;
1041 mdelay(1);
1042 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043
1044 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001045 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046
1047out:
1048 host->clock = clock;
1049}
1050
Pierre Ossman146ad662006-06-30 02:22:23 -07001051static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1052{
1053 u8 pwr;
1054
Pierre Ossmanae628902009-05-03 20:45:03 +02001055 if (power == (unsigned short)-1)
1056 pwr = 0;
1057 else {
1058 switch (1 << power) {
1059 case MMC_VDD_165_195:
1060 pwr = SDHCI_POWER_180;
1061 break;
1062 case MMC_VDD_29_30:
1063 case MMC_VDD_30_31:
1064 pwr = SDHCI_POWER_300;
1065 break;
1066 case MMC_VDD_32_33:
1067 case MMC_VDD_33_34:
1068 pwr = SDHCI_POWER_330;
1069 break;
1070 default:
1071 BUG();
1072 }
1073 }
1074
1075 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001076 return;
1077
Pierre Ossmanae628902009-05-03 20:45:03 +02001078 host->pwr = pwr;
1079
1080 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001081 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001082 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001083 }
1084
1085 /*
1086 * Spec says that we should clear the power reg before setting
1087 * a new value. Some controllers don't seem to like this though.
1088 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001089 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001090 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001091
Andres Salomone08c1692008-07-04 10:00:03 -07001092 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001093 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001094 * and set turn on power at the same time, so set the voltage first.
1095 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001096 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001097 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1098
1099 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001100
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001101 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001102
1103 /*
1104 * Some controllers need an extra 10ms delay of 10ms before they
1105 * can apply clock after applying power
1106 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001107 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001108 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001109}
1110
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111/*****************************************************************************\
1112 * *
1113 * MMC callbacks *
1114 * *
1115\*****************************************************************************/
1116
1117static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1118{
1119 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001120 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121 unsigned long flags;
1122
1123 host = mmc_priv(mmc);
1124
1125 spin_lock_irqsave(&host->lock, flags);
1126
1127 WARN_ON(host->mrq != NULL);
1128
Pierre Ossmanf9134312008-12-21 17:01:48 +01001129#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001130 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001131#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001132 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1133 if (mrq->stop) {
1134 mrq->data->stop = NULL;
1135 mrq->stop = NULL;
1136 }
1137 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138
1139 host->mrq = mrq;
1140
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001141 /* If polling, assume that the card is always present. */
1142 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1143 present = true;
1144 else
1145 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1146 SDHCI_CARD_PRESENT;
1147
1148 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001149 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150 tasklet_schedule(&host->finish_tasklet);
1151 } else
1152 sdhci_send_command(host, mrq->cmd);
1153
Pierre Ossman5f25a662006-10-04 02:15:39 -07001154 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155 spin_unlock_irqrestore(&host->lock, flags);
1156}
1157
1158static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1159{
1160 struct sdhci_host *host;
1161 unsigned long flags;
1162 u8 ctrl;
1163
1164 host = mmc_priv(mmc);
1165
1166 spin_lock_irqsave(&host->lock, flags);
1167
Pierre Ossman1e728592008-04-16 19:13:13 +02001168 if (host->flags & SDHCI_DEVICE_DEAD)
1169 goto out;
1170
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171 /*
1172 * Reset the chip on each power off.
1173 * Should clear out any weird states.
1174 */
1175 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001176 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001177 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 }
1179
1180 sdhci_set_clock(host, ios->clock);
1181
1182 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001183 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001185 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001187 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001188
Kyungmin Parkae6d6c92010-08-10 18:01:43 -07001189 if (ios->bus_width == MMC_BUS_WIDTH_8)
1190 ctrl |= SDHCI_CTRL_8BITBUS;
1191 else
1192 ctrl &= ~SDHCI_CTRL_8BITBUS;
1193
Pierre Ossmand129bce2006-03-24 03:18:17 -08001194 if (ios->bus_width == MMC_BUS_WIDTH_4)
1195 ctrl |= SDHCI_CTRL_4BITBUS;
1196 else
1197 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001198
Kyungmin Park51932502010-08-19 14:13:35 -07001199 if (ios->timing == MMC_TIMING_SD_HS &&
1200 !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001201 ctrl |= SDHCI_CTRL_HISPD;
1202 else
1203 ctrl &= ~SDHCI_CTRL_HISPD;
1204
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001205 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001206
Leandro Dorileob8352262007-07-25 23:47:04 +02001207 /*
1208 * Some (ENE) controllers go apeshit on some ios operation,
1209 * signalling timeout and CRC errors even on CMD0. Resetting
1210 * it on each ios seems to solve the problem.
1211 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001212 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001213 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1214
Pierre Ossman1e728592008-04-16 19:13:13 +02001215out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001216 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217 spin_unlock_irqrestore(&host->lock, flags);
1218}
1219
1220static int sdhci_get_ro(struct mmc_host *mmc)
1221{
1222 struct sdhci_host *host;
1223 unsigned long flags;
1224 int present;
1225
1226 host = mmc_priv(mmc);
1227
1228 spin_lock_irqsave(&host->lock, flags);
1229
Pierre Ossman1e728592008-04-16 19:13:13 +02001230 if (host->flags & SDHCI_DEVICE_DEAD)
1231 present = 0;
1232 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001233 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001234
1235 spin_unlock_irqrestore(&host->lock, flags);
1236
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001237 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1238 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239 return !(present & SDHCI_WRITE_PROTECT);
1240}
1241
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001242static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1243{
1244 struct sdhci_host *host;
1245 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001246
1247 host = mmc_priv(mmc);
1248
1249 spin_lock_irqsave(&host->lock, flags);
1250
Pierre Ossman1e728592008-04-16 19:13:13 +02001251 if (host->flags & SDHCI_DEVICE_DEAD)
1252 goto out;
1253
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001254 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001255 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1256 else
1257 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001258out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001259 mmiowb();
1260
1261 spin_unlock_irqrestore(&host->lock, flags);
1262}
1263
David Brownellab7aefd2006-11-12 17:55:30 -08001264static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265 .request = sdhci_request,
1266 .set_ios = sdhci_set_ios,
1267 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001268 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001269};
1270
1271/*****************************************************************************\
1272 * *
1273 * Tasklets *
1274 * *
1275\*****************************************************************************/
1276
1277static void sdhci_tasklet_card(unsigned long param)
1278{
1279 struct sdhci_host *host;
1280 unsigned long flags;
1281
1282 host = (struct sdhci_host*)param;
1283
1284 spin_lock_irqsave(&host->lock, flags);
1285
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001286 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287 if (host->mrq) {
1288 printk(KERN_ERR "%s: Card removed during transfer!\n",
1289 mmc_hostname(host->mmc));
1290 printk(KERN_ERR "%s: Resetting controller.\n",
1291 mmc_hostname(host->mmc));
1292
1293 sdhci_reset(host, SDHCI_RESET_CMD);
1294 sdhci_reset(host, SDHCI_RESET_DATA);
1295
Pierre Ossman17b04292007-07-22 22:18:46 +02001296 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297 tasklet_schedule(&host->finish_tasklet);
1298 }
1299 }
1300
1301 spin_unlock_irqrestore(&host->lock, flags);
1302
Pierre Ossman04cf5852008-08-18 22:18:14 +02001303 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304}
1305
1306static void sdhci_tasklet_finish(unsigned long param)
1307{
1308 struct sdhci_host *host;
1309 unsigned long flags;
1310 struct mmc_request *mrq;
1311
1312 host = (struct sdhci_host*)param;
1313
1314 spin_lock_irqsave(&host->lock, flags);
1315
1316 del_timer(&host->timer);
1317
1318 mrq = host->mrq;
1319
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 /*
1321 * The controller needs a reset of internal state machines
1322 * upon error conditions.
1323 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001324 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1325 (mrq->cmd->error ||
1326 (mrq->data && (mrq->data->error ||
1327 (mrq->data->stop && mrq->data->stop->error))) ||
1328 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001329
1330 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001331 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001332 unsigned int clock;
1333
1334 /* This is to force an update */
1335 clock = host->clock;
1336 host->clock = 0;
1337 sdhci_set_clock(host, clock);
1338 }
1339
1340 /* Spec says we should do both at the same time, but Ricoh
1341 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001342 sdhci_reset(host, SDHCI_RESET_CMD);
1343 sdhci_reset(host, SDHCI_RESET_DATA);
1344 }
1345
1346 host->mrq = NULL;
1347 host->cmd = NULL;
1348 host->data = NULL;
1349
Pierre Ossmanf9134312008-12-21 17:01:48 +01001350#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001352#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353
Pierre Ossman5f25a662006-10-04 02:15:39 -07001354 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001355 spin_unlock_irqrestore(&host->lock, flags);
1356
1357 mmc_request_done(host->mmc, mrq);
1358}
1359
1360static void sdhci_timeout_timer(unsigned long data)
1361{
1362 struct sdhci_host *host;
1363 unsigned long flags;
1364
1365 host = (struct sdhci_host*)data;
1366
1367 spin_lock_irqsave(&host->lock, flags);
1368
1369 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001370 printk(KERN_ERR "%s: Timeout waiting for hardware "
1371 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 sdhci_dumpregs(host);
1373
1374 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001375 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376 sdhci_finish_data(host);
1377 } else {
1378 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001379 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001380 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001381 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001382
1383 tasklet_schedule(&host->finish_tasklet);
1384 }
1385 }
1386
Pierre Ossman5f25a662006-10-04 02:15:39 -07001387 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388 spin_unlock_irqrestore(&host->lock, flags);
1389}
1390
1391/*****************************************************************************\
1392 * *
1393 * Interrupt handling *
1394 * *
1395\*****************************************************************************/
1396
1397static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1398{
1399 BUG_ON(intmask == 0);
1400
1401 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001402 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1403 "though no command operation was in progress.\n",
1404 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001405 sdhci_dumpregs(host);
1406 return;
1407 }
1408
Pierre Ossman43b58b32007-07-25 23:15:27 +02001409 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001410 host->cmd->error = -ETIMEDOUT;
1411 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1412 SDHCI_INT_INDEX))
1413 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001414
Pierre Ossmane8095172008-07-25 01:09:08 +02001415 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001416 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001417 return;
1418 }
1419
1420 /*
1421 * The host can send and interrupt when the busy state has
1422 * ended, allowing us to wait without wasting CPU cycles.
1423 * Unfortunately this is overloaded on the "data complete"
1424 * interrupt, so we need to take some care when handling
1425 * it.
1426 *
1427 * Note: The 1.0 specification is a bit ambiguous about this
1428 * feature so there might be some problems with older
1429 * controllers.
1430 */
1431 if (host->cmd->flags & MMC_RSP_BUSY) {
1432 if (host->cmd->data)
1433 DBG("Cannot wait for busy signal when also "
1434 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001435 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001436 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001437
1438 /* The controller does not support the end-of-busy IRQ,
1439 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001440 }
1441
1442 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001443 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444}
1445
George G. Davis0957c332010-02-18 12:32:12 -05001446#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001447static void sdhci_show_adma_error(struct sdhci_host *host)
1448{
1449 const char *name = mmc_hostname(host->mmc);
1450 u8 *desc = host->adma_desc;
1451 __le32 *dma;
1452 __le16 *len;
1453 u8 attr;
1454
1455 sdhci_dumpregs(host);
1456
1457 while (true) {
1458 dma = (__le32 *)(desc + 4);
1459 len = (__le16 *)(desc + 2);
1460 attr = *desc;
1461
1462 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1463 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1464
1465 desc += 8;
1466
1467 if (attr & 2)
1468 break;
1469 }
1470}
1471#else
1472static void sdhci_show_adma_error(struct sdhci_host *host) { }
1473#endif
1474
Pierre Ossmand129bce2006-03-24 03:18:17 -08001475static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1476{
1477 BUG_ON(intmask == 0);
1478
1479 if (!host->data) {
1480 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001481 * The "data complete" interrupt is also used to
1482 * indicate that a busy state has ended. See comment
1483 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001485 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1486 if (intmask & SDHCI_INT_DATA_END) {
1487 sdhci_finish_command(host);
1488 return;
1489 }
1490 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001492 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1493 "though no data operation was in progress.\n",
1494 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495 sdhci_dumpregs(host);
1496
1497 return;
1498 }
1499
1500 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001501 host->data->error = -ETIMEDOUT;
1502 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1503 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001504 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1505 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1506 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001507 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001508 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509
Pierre Ossman17b04292007-07-22 22:18:46 +02001510 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511 sdhci_finish_data(host);
1512 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001513 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514 sdhci_transfer_pio(host);
1515
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001516 /*
1517 * We currently don't do anything fancy with DMA
1518 * boundaries, but as we can't disable the feature
1519 * we need to at least restart the transfer.
1520 */
1521 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001522 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1523 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001524
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001525 if (intmask & SDHCI_INT_DATA_END) {
1526 if (host->cmd) {
1527 /*
1528 * Data managed to finish before the
1529 * command completed. Make sure we do
1530 * things in the proper order.
1531 */
1532 host->data_early = 1;
1533 } else {
1534 sdhci_finish_data(host);
1535 }
1536 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537 }
1538}
1539
David Howells7d12e782006-10-05 14:55:46 +01001540static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541{
1542 irqreturn_t result;
1543 struct sdhci_host* host = dev_id;
1544 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001545 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001546
1547 spin_lock(&host->lock);
1548
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001549 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001550
Mark Lord62df67a52007-03-06 13:30:13 +01001551 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001552 result = IRQ_NONE;
1553 goto out;
1554 }
1555
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001556 DBG("*** %s got interrupt: 0x%08x\n",
1557 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001558
Pierre Ossman3192a282006-06-30 02:22:26 -07001559 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001560 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1561 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001562 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001563 }
1564
1565 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566
1567 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001568 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1569 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001570 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 }
1572
1573 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001574 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1575 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001576 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001577 }
1578
1579 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1580
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001581 intmask &= ~SDHCI_INT_ERROR;
1582
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001584 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001586 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001587 }
1588
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001589 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001590
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001591 if (intmask & SDHCI_INT_CARD_INT)
1592 cardint = 1;
1593
1594 intmask &= ~SDHCI_INT_CARD_INT;
1595
Pierre Ossman3192a282006-06-30 02:22:26 -07001596 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001597 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001598 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599 sdhci_dumpregs(host);
1600
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001601 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001602 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603
1604 result = IRQ_HANDLED;
1605
Pierre Ossman5f25a662006-10-04 02:15:39 -07001606 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001607out:
1608 spin_unlock(&host->lock);
1609
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001610 /*
1611 * We have to delay this as it calls back into the driver.
1612 */
1613 if (cardint)
1614 mmc_signal_sdio_irq(host->mmc);
1615
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616 return result;
1617}
1618
1619/*****************************************************************************\
1620 * *
1621 * Suspend/resume *
1622 * *
1623\*****************************************************************************/
1624
1625#ifdef CONFIG_PM
1626
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001627int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001629 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001630
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001631 sdhci_disable_card_detection(host);
1632
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001633 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001634 if (ret)
1635 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001637 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001638
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001639 if (host->vmmc)
1640 ret = regulator_disable(host->vmmc);
1641
1642 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643}
1644
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001645EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001646
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001647int sdhci_resume_host(struct sdhci_host *host)
1648{
1649 int ret;
1650
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001651 if (host->vmmc) {
1652 int ret = regulator_enable(host->vmmc);
1653 if (ret)
1654 return ret;
1655 }
1656
1657
Richard Röjforsa13abc72009-09-22 16:45:30 -07001658 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001659 if (host->ops->enable_dma)
1660 host->ops->enable_dma(host);
1661 }
1662
1663 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1664 mmc_hostname(host->mmc), host);
1665 if (ret)
1666 return ret;
1667
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001668 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001669 mmiowb();
1670
1671 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001672 sdhci_enable_card_detection(host);
1673
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001674 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001675}
1676
1677EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678
1679#endif /* CONFIG_PM */
1680
1681/*****************************************************************************\
1682 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001683 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001684 * *
1685\*****************************************************************************/
1686
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001687struct sdhci_host *sdhci_alloc_host(struct device *dev,
1688 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001689{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001690 struct mmc_host *mmc;
1691 struct sdhci_host *host;
1692
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001693 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001694
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001695 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001696 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001697 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001698
1699 host = mmc_priv(mmc);
1700 host->mmc = mmc;
1701
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001702 return host;
1703}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001704
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001705EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001706
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001707int sdhci_add_host(struct sdhci_host *host)
1708{
1709 struct mmc_host *mmc;
1710 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001711 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001712
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001713 WARN_ON(host == NULL);
1714 if (host == NULL)
1715 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001716
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001717 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001719 if (debug_quirks)
1720 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001721
Pierre Ossmand96649e2006-06-30 02:22:30 -07001722 sdhci_reset(host, SDHCI_RESET_ALL);
1723
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001724 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001725 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1726 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001727 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001728 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001729 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001730 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001731 }
1732
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001733 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1734 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001735
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001736 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001737 host->flags |= SDHCI_USE_SDMA;
1738 else if (!(caps & SDHCI_CAN_DO_SDMA))
1739 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001740 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001741 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001742
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001743 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001744 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001745 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001746 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001747 }
1748
Richard Röjforsa13abc72009-09-22 16:45:30 -07001749 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1750 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001751
1752 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1753 (host->flags & SDHCI_USE_ADMA)) {
1754 DBG("Disabling ADMA as it is marked broken\n");
1755 host->flags &= ~SDHCI_USE_ADMA;
1756 }
1757
Richard Röjforsa13abc72009-09-22 16:45:30 -07001758 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001759 if (host->ops->enable_dma) {
1760 if (host->ops->enable_dma(host)) {
1761 printk(KERN_WARNING "%s: No suitable DMA "
1762 "available. Falling back to PIO.\n",
1763 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001764 host->flags &=
1765 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001766 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001767 }
1768 }
1769
Pierre Ossman2134a922008-06-28 18:28:51 +02001770 if (host->flags & SDHCI_USE_ADMA) {
1771 /*
1772 * We need to allocate descriptors for all sg entries
1773 * (128) and potentially one alignment transfer for
1774 * each of those entries.
1775 */
1776 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1777 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1778 if (!host->adma_desc || !host->align_buffer) {
1779 kfree(host->adma_desc);
1780 kfree(host->align_buffer);
1781 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1782 "buffers. Falling back to standard DMA.\n",
1783 mmc_hostname(mmc));
1784 host->flags &= ~SDHCI_USE_ADMA;
1785 }
1786 }
1787
Pierre Ossman76591502008-07-21 00:32:11 +02001788 /*
1789 * If we use DMA, then it's up to the caller to set the DMA
1790 * mask, but PIO does not need the hw shim so we set a new
1791 * mask here in that case.
1792 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001793 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001794 host->dma_mask = DMA_BIT_MASK(64);
1795 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1796 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001797
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04001798 if (host->version >= SDHCI_SPEC_300)
1799 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
1800 >> SDHCI_CLOCK_BASE_SHIFT;
1801 else
1802 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
1803 >> SDHCI_CLOCK_BASE_SHIFT;
1804
Pierre Ossmand129bce2006-03-24 03:18:17 -08001805 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001806 if (host->max_clk == 0 || host->quirks &
1807 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001808 if (!host->ops->get_max_clock) {
1809 printk(KERN_ERR
1810 "%s: Hardware doesn't specify base clock "
1811 "frequency.\n", mmc_hostname(mmc));
1812 return -ENODEV;
1813 }
1814 host->max_clk = host->ops->get_max_clock(host);
1815 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001817 host->timeout_clk =
1818 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1819 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001820 if (host->ops->get_timeout_clock) {
1821 host->timeout_clk = host->ops->get_timeout_clock(host);
1822 } else if (!(host->quirks &
1823 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001824 printk(KERN_ERR
1825 "%s: Hardware doesn't specify timeout clock "
1826 "frequency.\n", mmc_hostname(mmc));
1827 return -ENODEV;
1828 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001829 }
1830 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1831 host->timeout_clk *= 1000;
1832
Pierre Ossmand129bce2006-03-24 03:18:17 -08001833 /*
1834 * Set host parameters.
1835 */
1836 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07001837 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001838 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04001839 else if (host->version >= SDHCI_SPEC_300)
1840 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001841 else
Zhangfei Gao03975262010-09-20 15:15:18 -04001842 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001843 mmc->f_max = host->max_clk;
Kyungmin Parkc1f59772010-08-10 18:01:44 -07001844 mmc->caps |= MMC_CAP_SDIO_IRQ;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001845
1846 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Giuseppe Cavallarob08caed2010-09-20 21:22:13 -04001847 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001848
Pierre Ossman86a6a872009-02-02 21:13:49 +01001849 if (caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04001850 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001851
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01001852 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1853 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001854 mmc->caps |= MMC_CAP_NEEDS_POLL;
1855
Pierre Ossman146ad662006-06-30 02:22:23 -07001856 mmc->ocr_avail = 0;
1857 if (caps & SDHCI_CAN_VDD_330)
1858 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001859 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001860 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001861 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001862 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001863
1864 if (mmc->ocr_avail == 0) {
1865 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001866 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001867 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001868 }
1869
Pierre Ossmand129bce2006-03-24 03:18:17 -08001870 spin_lock_init(&host->lock);
1871
1872 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001873 * Maximum number of segments. Depends on if the hardware
1874 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001875 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001876 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001877 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001878 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001879 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001880 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001881 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001882
1883 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001884 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001885 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001887 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001888
1889 /*
1890 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001891 * of bytes. When doing hardware scatter/gather, each entry cannot
1892 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001893 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001894 if (host->flags & SDHCI_USE_ADMA)
1895 mmc->max_seg_size = 65536;
1896 else
1897 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001898
1899 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001900 * Maximum block size. This varies from controller to controller and
1901 * is specified in the capabilities register.
1902 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001903 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1904 mmc->max_blk_size = 2;
1905 } else {
1906 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1907 SDHCI_MAX_BLOCK_SHIFT;
1908 if (mmc->max_blk_size >= 3) {
1909 printk(KERN_WARNING "%s: Invalid maximum block size, "
1910 "assuming 512 bytes\n", mmc_hostname(mmc));
1911 mmc->max_blk_size = 0;
1912 }
1913 }
1914
1915 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001916
1917 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001918 * Maximum block count.
1919 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001920 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001921
1922 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001923 * Init tasklets.
1924 */
1925 tasklet_init(&host->card_tasklet,
1926 sdhci_tasklet_card, (unsigned long)host);
1927 tasklet_init(&host->finish_tasklet,
1928 sdhci_tasklet_finish, (unsigned long)host);
1929
Al Viroe4cad1b2006-10-10 22:47:07 +01001930 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001931
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001932 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001933 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001934 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001935 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001936
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001937 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1938 if (IS_ERR(host->vmmc)) {
1939 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
1940 host->vmmc = NULL;
1941 } else {
1942 regulator_enable(host->vmmc);
1943 }
1944
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001945 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001946
1947#ifdef CONFIG_MMC_DEBUG
1948 sdhci_dumpregs(host);
1949#endif
1950
Pierre Ossmanf9134312008-12-21 17:01:48 +01001951#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001952 snprintf(host->led_name, sizeof(host->led_name),
1953 "%s::", mmc_hostname(mmc));
1954 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001955 host->led.brightness = LED_OFF;
1956 host->led.default_trigger = mmc_hostname(mmc);
1957 host->led.brightness_set = sdhci_led_control;
1958
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001959 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001960 if (ret)
1961 goto reset;
1962#endif
1963
Pierre Ossman5f25a662006-10-04 02:15:39 -07001964 mmiowb();
1965
Pierre Ossmand129bce2006-03-24 03:18:17 -08001966 mmc_add_host(mmc);
1967
Richard Röjforsa13abc72009-09-22 16:45:30 -07001968 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001969 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001970 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1971 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001973 sdhci_enable_card_detection(host);
1974
Pierre Ossmand129bce2006-03-24 03:18:17 -08001975 return 0;
1976
Pierre Ossmanf9134312008-12-21 17:01:48 +01001977#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001978reset:
1979 sdhci_reset(host, SDHCI_RESET_ALL);
1980 free_irq(host->irq, host);
1981#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001982untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001983 tasklet_kill(&host->card_tasklet);
1984 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001985
1986 return ret;
1987}
1988
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001989EXPORT_SYMBOL_GPL(sdhci_add_host);
1990
Pierre Ossman1e728592008-04-16 19:13:13 +02001991void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992{
Pierre Ossman1e728592008-04-16 19:13:13 +02001993 unsigned long flags;
1994
1995 if (dead) {
1996 spin_lock_irqsave(&host->lock, flags);
1997
1998 host->flags |= SDHCI_DEVICE_DEAD;
1999
2000 if (host->mrq) {
2001 printk(KERN_ERR "%s: Controller removed during "
2002 " transfer!\n", mmc_hostname(host->mmc));
2003
2004 host->mrq->cmd->error = -ENOMEDIUM;
2005 tasklet_schedule(&host->finish_tasklet);
2006 }
2007
2008 spin_unlock_irqrestore(&host->lock, flags);
2009 }
2010
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002011 sdhci_disable_card_detection(host);
2012
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002013 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002014
Pierre Ossmanf9134312008-12-21 17:01:48 +01002015#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002016 led_classdev_unregister(&host->led);
2017#endif
2018
Pierre Ossman1e728592008-04-16 19:13:13 +02002019 if (!dead)
2020 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002021
2022 free_irq(host->irq, host);
2023
2024 del_timer_sync(&host->timer);
2025
2026 tasklet_kill(&host->card_tasklet);
2027 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002028
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002029 if (host->vmmc) {
2030 regulator_disable(host->vmmc);
2031 regulator_put(host->vmmc);
2032 }
2033
Pierre Ossman2134a922008-06-28 18:28:51 +02002034 kfree(host->adma_desc);
2035 kfree(host->align_buffer);
2036
2037 host->adma_desc = NULL;
2038 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002039}
2040
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002041EXPORT_SYMBOL_GPL(sdhci_remove_host);
2042
2043void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002044{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002045 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002046}
2047
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002048EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002049
2050/*****************************************************************************\
2051 * *
2052 * Driver init/exit *
2053 * *
2054\*****************************************************************************/
2055
2056static int __init sdhci_drv_init(void)
2057{
2058 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002059 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002060 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2061
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002062 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002063}
2064
2065static void __exit sdhci_drv_exit(void)
2066{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002067}
2068
2069module_init(sdhci_drv_init);
2070module_exit(sdhci_drv_exit);
2071
Pierre Ossmandf673b22006-06-30 02:22:31 -07002072module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002073
Pierre Ossman32710e82009-04-08 20:14:54 +02002074MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002075MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002076MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002077
Pierre Ossmandf673b22006-06-30 02:22:31 -07002078MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");