blob: cd6dab34ba54d21f046d21874f050092e5ce65fd [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>
Ralf Baechle11763602007-10-23 20:42:11 +020020#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021
Pierre Ossman2f730fe2008-03-17 10:29:38 +010022#include <linux/leds.h>
23
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include "sdhci.h"
27
28#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010031 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmanf9134312008-12-21 17:01:48 +010033#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
34 defined(CONFIG_MMC_SDHCI_MODULE))
35#define SDHCI_USE_LEDS_CLASS
36#endif
37
Pierre Ossmandf673b22006-06-30 02:22:31 -070038static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
41static void sdhci_finish_data(struct sdhci_host *);
42
43static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
44static void sdhci_finish_command(struct sdhci_host *);
45
46static void sdhci_dumpregs(struct sdhci_host *host)
47{
48 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
49
50 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030051 sdhci_readl(host, SDHCI_DMA_ADDRESS),
52 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080053 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readw(host, SDHCI_BLOCK_SIZE),
55 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readl(host, SDHCI_ARGUMENT),
58 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_PRESENT_STATE),
61 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readb(host, SDHCI_POWER_CONTROL),
64 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
67 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
70 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readl(host, SDHCI_INT_ENABLE),
73 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readw(host, SDHCI_ACMD12_ERR),
76 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readl(host, SDHCI_CAPABILITIES),
79 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080080
81 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
82}
83
84/*****************************************************************************\
85 * *
86 * Low level functions *
87 * *
88\*****************************************************************************/
89
Anton Vorontsov7260cf52009-03-17 00:13:48 +030090static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
91{
92 u32 ier;
93
94 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
95 ier &= ~clear;
96 ier |= set;
97 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
98 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
99}
100
101static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
102{
103 sdhci_clear_set_irqs(host, 0, irqs);
104}
105
106static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
107{
108 sdhci_clear_set_irqs(host, irqs, 0);
109}
110
111static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
112{
113 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
114
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300115 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
116 return;
117
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300118 if (enable)
119 sdhci_unmask_irqs(host, irqs);
120 else
121 sdhci_mask_irqs(host, irqs);
122}
123
124static void sdhci_enable_card_detection(struct sdhci_host *host)
125{
126 sdhci_set_card_detection(host, true);
127}
128
129static void sdhci_disable_card_detection(struct sdhci_host *host)
130{
131 sdhci_set_card_detection(host, false);
132}
133
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134static void sdhci_reset(struct sdhci_host *host, u8 mask)
135{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700136 unsigned long timeout;
137
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100138 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300139 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700140 SDHCI_CARD_PRESENT))
141 return;
142 }
143
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300144 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800145
Pierre Ossmane16514d82006-06-30 02:22:24 -0700146 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800147 host->clock = 0;
148
Pierre Ossmane16514d82006-06-30 02:22:24 -0700149 /* Wait max 100 ms */
150 timeout = 100;
151
152 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300153 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700154 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100155 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700156 mmc_hostname(host->mmc), (int)mask);
157 sdhci_dumpregs(host);
158 return;
159 }
160 timeout--;
161 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800162 }
163}
164
165static void sdhci_init(struct sdhci_host *host)
166{
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167 sdhci_reset(host, SDHCI_RESET_ALL);
168
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300169 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
170 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700171 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
172 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300173 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300174}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300176static void sdhci_reinit(struct sdhci_host *host)
177{
178 sdhci_init(host);
179 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180}
181
182static void sdhci_activate_led(struct sdhci_host *host)
183{
184 u8 ctrl;
185
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300186 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300188 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189}
190
191static void sdhci_deactivate_led(struct sdhci_host *host)
192{
193 u8 ctrl;
194
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300195 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800196 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300197 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800198}
199
Pierre Ossmanf9134312008-12-21 17:01:48 +0100200#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100201static void sdhci_led_control(struct led_classdev *led,
202 enum led_brightness brightness)
203{
204 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
205 unsigned long flags;
206
207 spin_lock_irqsave(&host->lock, flags);
208
209 if (brightness == LED_OFF)
210 sdhci_deactivate_led(host);
211 else
212 sdhci_activate_led(host);
213
214 spin_unlock_irqrestore(&host->lock, flags);
215}
216#endif
217
Pierre Ossmand129bce2006-03-24 03:18:17 -0800218/*****************************************************************************\
219 * *
220 * Core functions *
221 * *
222\*****************************************************************************/
223
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100224static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225{
Pierre Ossman76591502008-07-21 00:32:11 +0200226 unsigned long flags;
227 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700228 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200229 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100231 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100233 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200234 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235
Pierre Ossman76591502008-07-21 00:32:11 +0200236 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100238 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200239 if (!sg_miter_next(&host->sg_miter))
240 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241
Pierre Ossman76591502008-07-21 00:32:11 +0200242 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243
Pierre Ossman76591502008-07-21 00:32:11 +0200244 blksize -= len;
245 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200246
Pierre Ossman76591502008-07-21 00:32:11 +0200247 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Pierre Ossman76591502008-07-21 00:32:11 +0200249 while (len) {
250 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300251 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200252 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253 }
Pierre Ossman76591502008-07-21 00:32:11 +0200254
255 *buf = scratch & 0xFF;
256
257 buf++;
258 scratch >>= 8;
259 chunk--;
260 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261 }
262 }
Pierre Ossman76591502008-07-21 00:32:11 +0200263
264 sg_miter_stop(&host->sg_miter);
265
266 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100267}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100269static void sdhci_write_block_pio(struct sdhci_host *host)
270{
Pierre Ossman76591502008-07-21 00:32:11 +0200271 unsigned long flags;
272 size_t blksize, len, chunk;
273 u32 scratch;
274 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100275
276 DBG("PIO writing\n");
277
278 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200279 chunk = 0;
280 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100281
Pierre Ossman76591502008-07-21 00:32:11 +0200282 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100283
284 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200285 if (!sg_miter_next(&host->sg_miter))
286 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100287
Pierre Ossman76591502008-07-21 00:32:11 +0200288 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200289
Pierre Ossman76591502008-07-21 00:32:11 +0200290 blksize -= len;
291 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292
Pierre Ossman76591502008-07-21 00:32:11 +0200293 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100294
Pierre Ossman76591502008-07-21 00:32:11 +0200295 while (len) {
296 scratch |= (u32)*buf << (chunk * 8);
297
298 buf++;
299 chunk++;
300 len--;
301
302 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300303 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200304 chunk = 0;
305 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100307 }
308 }
Pierre Ossman76591502008-07-21 00:32:11 +0200309
310 sg_miter_stop(&host->sg_miter);
311
312 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313}
314
315static void sdhci_transfer_pio(struct sdhci_host *host)
316{
317 u32 mask;
318
319 BUG_ON(!host->data);
320
Pierre Ossman76591502008-07-21 00:32:11 +0200321 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322 return;
323
324 if (host->data->flags & MMC_DATA_READ)
325 mask = SDHCI_DATA_AVAILABLE;
326 else
327 mask = SDHCI_SPACE_AVAILABLE;
328
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200329 /*
330 * Some controllers (JMicron JMB38x) mess up the buffer bits
331 * for transfers < 4 bytes. As long as it is just one block,
332 * we can ignore the bits.
333 */
334 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
335 (host->data->blocks == 1))
336 mask = ~0;
337
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300338 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300339 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
340 udelay(100);
341
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342 if (host->data->flags & MMC_DATA_READ)
343 sdhci_read_block_pio(host);
344 else
345 sdhci_write_block_pio(host);
346
Pierre Ossman76591502008-07-21 00:32:11 +0200347 host->blocks--;
348 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350 }
351
352 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800353}
354
Pierre Ossman2134a922008-06-28 18:28:51 +0200355static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
356{
357 local_irq_save(*flags);
358 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
359}
360
361static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
362{
363 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
364 local_irq_restore(*flags);
365}
366
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200367static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200368 struct mmc_data *data)
369{
370 int direction;
371
372 u8 *desc;
373 u8 *align;
374 dma_addr_t addr;
375 dma_addr_t align_addr;
376 int len, offset;
377
378 struct scatterlist *sg;
379 int i;
380 char *buffer;
381 unsigned long flags;
382
383 /*
384 * The spec does not specify endianness of descriptor table.
385 * We currently guess that it is LE.
386 */
387
388 if (data->flags & MMC_DATA_READ)
389 direction = DMA_FROM_DEVICE;
390 else
391 direction = DMA_TO_DEVICE;
392
393 /*
394 * The ADMA descriptor table is mapped further down as we
395 * need to fill it with data first.
396 */
397
398 host->align_addr = dma_map_single(mmc_dev(host->mmc),
399 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700400 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200401 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200402 BUG_ON(host->align_addr & 0x3);
403
404 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
405 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200406 if (host->sg_count == 0)
407 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200408
409 desc = host->adma_desc;
410 align = host->align_buffer;
411
412 align_addr = host->align_addr;
413
414 for_each_sg(data->sg, sg, host->sg_count, i) {
415 addr = sg_dma_address(sg);
416 len = sg_dma_len(sg);
417
418 /*
419 * The SDHCI specification states that ADMA
420 * addresses must be 32-bit aligned. If they
421 * aren't, then we use a bounce buffer for
422 * the (up to three) bytes that screw up the
423 * alignment.
424 */
425 offset = (4 - (addr & 0x3)) & 0x3;
426 if (offset) {
427 if (data->flags & MMC_DATA_WRITE) {
428 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200429 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200430 memcpy(align, buffer, offset);
431 sdhci_kunmap_atomic(buffer, &flags);
432 }
433
434 desc[7] = (align_addr >> 24) & 0xff;
435 desc[6] = (align_addr >> 16) & 0xff;
436 desc[5] = (align_addr >> 8) & 0xff;
437 desc[4] = (align_addr >> 0) & 0xff;
438
439 BUG_ON(offset > 65536);
440
441 desc[3] = (offset >> 8) & 0xff;
442 desc[2] = (offset >> 0) & 0xff;
443
444 desc[1] = 0x00;
445 desc[0] = 0x21; /* tran, valid */
446
447 align += 4;
448 align_addr += 4;
449
450 desc += 8;
451
452 addr += offset;
453 len -= offset;
454 }
455
456 desc[7] = (addr >> 24) & 0xff;
457 desc[6] = (addr >> 16) & 0xff;
458 desc[5] = (addr >> 8) & 0xff;
459 desc[4] = (addr >> 0) & 0xff;
460
461 BUG_ON(len > 65536);
462
463 desc[3] = (len >> 8) & 0xff;
464 desc[2] = (len >> 0) & 0xff;
465
466 desc[1] = 0x00;
467 desc[0] = 0x21; /* tran, valid */
468
469 desc += 8;
470
471 /*
472 * If this triggers then we have a calculation bug
473 * somewhere. :/
474 */
475 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
476 }
477
478 /*
479 * Add a terminating entry.
480 */
481 desc[7] = 0;
482 desc[6] = 0;
483 desc[5] = 0;
484 desc[4] = 0;
485
486 desc[3] = 0;
487 desc[2] = 0;
488
489 desc[1] = 0x00;
490 desc[0] = 0x03; /* nop, end, valid */
491
492 /*
493 * Resync align buffer as we might have changed it.
494 */
495 if (data->flags & MMC_DATA_WRITE) {
496 dma_sync_single_for_device(mmc_dev(host->mmc),
497 host->align_addr, 128 * 4, direction);
498 }
499
500 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
501 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200502 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200503 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200504 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200505
506 return 0;
507
508unmap_entries:
509 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
510 data->sg_len, direction);
511unmap_align:
512 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
513 128 * 4, direction);
514fail:
515 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516}
517
518static void sdhci_adma_table_post(struct sdhci_host *host,
519 struct mmc_data *data)
520{
521 int direction;
522
523 struct scatterlist *sg;
524 int i, size;
525 u8 *align;
526 char *buffer;
527 unsigned long flags;
528
529 if (data->flags & MMC_DATA_READ)
530 direction = DMA_FROM_DEVICE;
531 else
532 direction = DMA_TO_DEVICE;
533
534 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
535 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
536
537 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
538 128 * 4, direction);
539
540 if (data->flags & MMC_DATA_READ) {
541 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
542 data->sg_len, direction);
543
544 align = host->align_buffer;
545
546 for_each_sg(data->sg, sg, host->sg_count, i) {
547 if (sg_dma_address(sg) & 0x3) {
548 size = 4 - (sg_dma_address(sg) & 0x3);
549
550 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200551 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 memcpy(buffer, align, size);
553 sdhci_kunmap_atomic(buffer, &flags);
554
555 align += 4;
556 }
557 }
558 }
559
560 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
561 data->sg_len, direction);
562}
563
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200564static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800565{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700566 u8 count;
567 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800568
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200569 /*
570 * If the host controller provides us with an incorrect timeout
571 * value, just skip the check and use 0xE. The hardware may take
572 * longer to time out, but that's much better than having a too-short
573 * timeout value.
574 */
575 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
576 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200577
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700578 /* timeout in us */
579 target_timeout = data->timeout_ns / 1000 +
580 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800581
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700582 /*
583 * Figure out needed cycles.
584 * We do this in steps in order to fit inside a 32 bit int.
585 * The first step is the minimum timeout, which will have a
586 * minimum resolution of 6 bits:
587 * (1) 2^13*1000 > 2^22,
588 * (2) host->timeout_clk < 2^16
589 * =>
590 * (1) / (2) > 2^6
591 */
592 count = 0;
593 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
594 while (current_timeout < target_timeout) {
595 count++;
596 current_timeout <<= 1;
597 if (count >= 0xF)
598 break;
599 }
600
601 if (count >= 0xF) {
602 printk(KERN_WARNING "%s: Too large timeout requested!\n",
603 mmc_hostname(host->mmc));
604 count = 0xE;
605 }
606
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200607 return count;
608}
609
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300610static void sdhci_set_transfer_irqs(struct sdhci_host *host)
611{
612 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
613 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
614
615 if (host->flags & SDHCI_REQ_USE_DMA)
616 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
617 else
618 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
619}
620
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200621static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
622{
623 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200625 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200626
627 WARN_ON(host->data);
628
629 if (data == NULL)
630 return;
631
632 /* Sanity checks */
633 BUG_ON(data->blksz * data->blocks > 524288);
634 BUG_ON(data->blksz > host->mmc->max_blk_size);
635 BUG_ON(data->blocks > 65535);
636
637 host->data = data;
638 host->data_early = 0;
639
640 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300641 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800642
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100643 if (host->flags & SDHCI_USE_DMA)
644 host->flags |= SDHCI_REQ_USE_DMA;
645
Pierre Ossman2134a922008-06-28 18:28:51 +0200646 /*
647 * FIXME: This doesn't account for merging when mapping the
648 * scatterlist.
649 */
650 if (host->flags & SDHCI_REQ_USE_DMA) {
651 int broken, i;
652 struct scatterlist *sg;
653
654 broken = 0;
655 if (host->flags & SDHCI_USE_ADMA) {
656 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
657 broken = 1;
658 } else {
659 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
660 broken = 1;
661 }
662
663 if (unlikely(broken)) {
664 for_each_sg(data->sg, sg, data->sg_len, i) {
665 if (sg->length & 0x3) {
666 DBG("Reverting to PIO because of "
667 "transfer size (%d)\n",
668 sg->length);
669 host->flags &= ~SDHCI_REQ_USE_DMA;
670 break;
671 }
672 }
673 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100674 }
675
676 /*
677 * The assumption here being that alignment is the same after
678 * translation to device address space.
679 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200680 if (host->flags & SDHCI_REQ_USE_DMA) {
681 int broken, i;
682 struct scatterlist *sg;
683
684 broken = 0;
685 if (host->flags & SDHCI_USE_ADMA) {
686 /*
687 * As we use 3 byte chunks to work around
688 * alignment problems, we need to check this
689 * quirk.
690 */
691 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
692 broken = 1;
693 } else {
694 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
695 broken = 1;
696 }
697
698 if (unlikely(broken)) {
699 for_each_sg(data->sg, sg, data->sg_len, i) {
700 if (sg->offset & 0x3) {
701 DBG("Reverting to PIO because of "
702 "bad alignment\n");
703 host->flags &= ~SDHCI_REQ_USE_DMA;
704 break;
705 }
706 }
707 }
708 }
709
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200710 if (host->flags & SDHCI_REQ_USE_DMA) {
711 if (host->flags & SDHCI_USE_ADMA) {
712 ret = sdhci_adma_table_pre(host, data);
713 if (ret) {
714 /*
715 * This only happens when someone fed
716 * us an invalid request.
717 */
718 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200719 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200720 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300721 sdhci_writel(host, host->adma_addr,
722 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200723 }
724 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300725 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200726
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300727 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200728 data->sg, data->sg_len,
729 (data->flags & MMC_DATA_READ) ?
730 DMA_FROM_DEVICE :
731 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300732 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200733 /*
734 * This only happens when someone fed
735 * us an invalid request.
736 */
737 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200738 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200739 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200740 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300741 sdhci_writel(host, sg_dma_address(data->sg),
742 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200743 }
744 }
745 }
746
Pierre Ossman2134a922008-06-28 18:28:51 +0200747 /*
748 * Always adjust the DMA selection as some controllers
749 * (e.g. JMicron) can't do PIO properly when the selection
750 * is ADMA.
751 */
752 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300753 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200754 ctrl &= ~SDHCI_CTRL_DMA_MASK;
755 if ((host->flags & SDHCI_REQ_USE_DMA) &&
756 (host->flags & SDHCI_USE_ADMA))
757 ctrl |= SDHCI_CTRL_ADMA32;
758 else
759 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300760 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100761 }
762
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200763 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Pierre Ossman76591502008-07-21 00:32:11 +0200764 sg_miter_start(&host->sg_miter,
765 data->sg, data->sg_len, SG_MITER_ATOMIC);
766 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800767 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700768
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300769 sdhci_set_transfer_irqs(host);
770
Pierre Ossmanbab76962006-07-02 16:51:35 +0100771 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300772 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
773 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700774}
775
776static void sdhci_set_transfer_mode(struct sdhci_host *host,
777 struct mmc_data *data)
778{
779 u16 mode;
780
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700781 if (data == NULL)
782 return;
783
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200784 WARN_ON(!host->data);
785
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700786 mode = SDHCI_TRNS_BLK_CNT_EN;
787 if (data->blocks > 1)
788 mode |= SDHCI_TRNS_MULTI;
789 if (data->flags & MMC_DATA_READ)
790 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100791 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700792 mode |= SDHCI_TRNS_DMA;
793
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300794 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800795}
796
797static void sdhci_finish_data(struct sdhci_host *host)
798{
799 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800800
801 BUG_ON(!host->data);
802
803 data = host->data;
804 host->data = NULL;
805
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100806 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 if (host->flags & SDHCI_USE_ADMA)
808 sdhci_adma_table_post(host, data);
809 else {
810 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
811 data->sg_len, (data->flags & MMC_DATA_READ) ?
812 DMA_FROM_DEVICE : DMA_TO_DEVICE);
813 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800814 }
815
816 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200817 * The specification states that the block count register must
818 * be updated, but it does not specify at what point in the
819 * data flow. That makes the register entirely useless to read
820 * back so we have to assume that nothing made it to the card
821 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800822 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200823 if (data->error)
824 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800825 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200826 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800827
Pierre Ossmand129bce2006-03-24 03:18:17 -0800828 if (data->stop) {
829 /*
830 * The controller needs a reset of internal state machines
831 * upon error conditions.
832 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200833 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800834 sdhci_reset(host, SDHCI_RESET_CMD);
835 sdhci_reset(host, SDHCI_RESET_DATA);
836 }
837
838 sdhci_send_command(host, data->stop);
839 } else
840 tasklet_schedule(&host->finish_tasklet);
841}
842
843static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
844{
845 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700846 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700847 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800848
849 WARN_ON(host->cmd);
850
Pierre Ossmand129bce2006-03-24 03:18:17 -0800851 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700852 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700853
854 mask = SDHCI_CMD_INHIBIT;
855 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
856 mask |= SDHCI_DATA_INHIBIT;
857
858 /* We shouldn't wait for data inihibit for stop commands, even
859 though they might use busy signaling */
860 if (host->mrq->data && (cmd == host->mrq->data->stop))
861 mask &= ~SDHCI_DATA_INHIBIT;
862
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300863 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700864 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800865 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100866 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800867 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200868 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800869 tasklet_schedule(&host->finish_tasklet);
870 return;
871 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700872 timeout--;
873 mdelay(1);
874 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800875
876 mod_timer(&host->timer, jiffies + 10 * HZ);
877
878 host->cmd = cmd;
879
880 sdhci_prepare_data(host, cmd->data);
881
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300882 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884 sdhci_set_transfer_mode(host, cmd->data);
885
Pierre Ossmand129bce2006-03-24 03:18:17 -0800886 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100887 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200889 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890 tasklet_schedule(&host->finish_tasklet);
891 return;
892 }
893
894 if (!(cmd->flags & MMC_RSP_PRESENT))
895 flags = SDHCI_CMD_RESP_NONE;
896 else if (cmd->flags & MMC_RSP_136)
897 flags = SDHCI_CMD_RESP_LONG;
898 else if (cmd->flags & MMC_RSP_BUSY)
899 flags = SDHCI_CMD_RESP_SHORT_BUSY;
900 else
901 flags = SDHCI_CMD_RESP_SHORT;
902
903 if (cmd->flags & MMC_RSP_CRC)
904 flags |= SDHCI_CMD_CRC;
905 if (cmd->flags & MMC_RSP_OPCODE)
906 flags |= SDHCI_CMD_INDEX;
907 if (cmd->data)
908 flags |= SDHCI_CMD_DATA;
909
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300910 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800911}
912
913static void sdhci_finish_command(struct sdhci_host *host)
914{
915 int i;
916
917 BUG_ON(host->cmd == NULL);
918
919 if (host->cmd->flags & MMC_RSP_PRESENT) {
920 if (host->cmd->flags & MMC_RSP_136) {
921 /* CRC is stripped so we need to do some shifting. */
922 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300923 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 SDHCI_RESPONSE + (3-i)*4) << 8;
925 if (i != 3)
926 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300927 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928 SDHCI_RESPONSE + (3-i)*4-1);
929 }
930 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300931 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932 }
933 }
934
Pierre Ossman17b04292007-07-22 22:18:46 +0200935 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800936
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200937 if (host->data && host->data_early)
938 sdhci_finish_data(host);
939
940 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941 tasklet_schedule(&host->finish_tasklet);
942
943 host->cmd = NULL;
944}
945
946static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
947{
948 int div;
949 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700950 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951
952 if (clock == host->clock)
953 return;
954
Anton Vorontsov81146342009-03-17 00:13:59 +0300955 if (host->ops->set_clock) {
956 host->ops->set_clock(host, clock);
957 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
958 return;
959 }
960
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300961 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962
963 if (clock == 0)
964 goto out;
965
966 for (div = 1;div < 256;div *= 2) {
967 if ((host->max_clk / div) <= clock)
968 break;
969 }
970 div >>= 1;
971
972 clk = div << SDHCI_DIVIDER_SHIFT;
973 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300974 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975
976 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700977 timeout = 10;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300978 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700979 & SDHCI_CLOCK_INT_STABLE)) {
980 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100981 printk(KERN_ERR "%s: Internal clock never "
982 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983 sdhci_dumpregs(host);
984 return;
985 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700986 timeout--;
987 mdelay(1);
988 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
990 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992
993out:
994 host->clock = clock;
995}
996
Pierre Ossman146ad662006-06-30 02:22:23 -0700997static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
998{
999 u8 pwr;
1000
1001 if (host->power == power)
1002 return;
1003
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001004 if (power == (unsigned short)-1) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001005 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001006 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001007 }
1008
1009 /*
1010 * Spec says that we should clear the power reg before setting
1011 * a new value. Some controllers don't seem to like this though.
1012 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001013 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001014 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001015
1016 pwr = SDHCI_POWER_ON;
1017
Philip Langdale4be34c92007-03-11 17:15:15 -07001018 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -07001019 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -07001020 pwr |= SDHCI_POWER_180;
1021 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001022 case MMC_VDD_29_30:
1023 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -07001024 pwr |= SDHCI_POWER_300;
1025 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001026 case MMC_VDD_32_33:
1027 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -07001028 pwr |= SDHCI_POWER_330;
1029 break;
1030 default:
1031 BUG();
1032 }
1033
Andres Salomone08c1692008-07-04 10:00:03 -07001034 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001035 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001036 * and set turn on power at the same time, so set the voltage first.
1037 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001038 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001039 sdhci_writeb(host, pwr & ~SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
Andres Salomone08c1692008-07-04 10:00:03 -07001040
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001041 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001042
1043out:
1044 host->power = power;
1045}
1046
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047/*****************************************************************************\
1048 * *
1049 * MMC callbacks *
1050 * *
1051\*****************************************************************************/
1052
1053static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1054{
1055 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001056 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 unsigned long flags;
1058
1059 host = mmc_priv(mmc);
1060
1061 spin_lock_irqsave(&host->lock, flags);
1062
1063 WARN_ON(host->mrq != NULL);
1064
Pierre Ossmanf9134312008-12-21 17:01:48 +01001065#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001067#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068
1069 host->mrq = mrq;
1070
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001071 /* If polling, assume that the card is always present. */
1072 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1073 present = true;
1074 else
1075 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1076 SDHCI_CARD_PRESENT;
1077
1078 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001079 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080 tasklet_schedule(&host->finish_tasklet);
1081 } else
1082 sdhci_send_command(host, mrq->cmd);
1083
Pierre Ossman5f25a662006-10-04 02:15:39 -07001084 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 spin_unlock_irqrestore(&host->lock, flags);
1086}
1087
1088static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1089{
1090 struct sdhci_host *host;
1091 unsigned long flags;
1092 u8 ctrl;
1093
1094 host = mmc_priv(mmc);
1095
1096 spin_lock_irqsave(&host->lock, flags);
1097
Pierre Ossman1e728592008-04-16 19:13:13 +02001098 if (host->flags & SDHCI_DEVICE_DEAD)
1099 goto out;
1100
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 /*
1102 * Reset the chip on each power off.
1103 * Should clear out any weird states.
1104 */
1105 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001106 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001107 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001108 }
1109
1110 sdhci_set_clock(host, ios->clock);
1111
1112 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001113 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001114 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001115 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001117 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001118
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119 if (ios->bus_width == MMC_BUS_WIDTH_4)
1120 ctrl |= SDHCI_CTRL_4BITBUS;
1121 else
1122 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001123
1124 if (ios->timing == MMC_TIMING_SD_HS)
1125 ctrl |= SDHCI_CTRL_HISPD;
1126 else
1127 ctrl &= ~SDHCI_CTRL_HISPD;
1128
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001129 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001130
Leandro Dorileob8352262007-07-25 23:47:04 +02001131 /*
1132 * Some (ENE) controllers go apeshit on some ios operation,
1133 * signalling timeout and CRC errors even on CMD0. Resetting
1134 * it on each ios seems to solve the problem.
1135 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001136 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001137 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1138
Pierre Ossman1e728592008-04-16 19:13:13 +02001139out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001140 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 spin_unlock_irqrestore(&host->lock, flags);
1142}
1143
1144static int sdhci_get_ro(struct mmc_host *mmc)
1145{
1146 struct sdhci_host *host;
1147 unsigned long flags;
1148 int present;
1149
1150 host = mmc_priv(mmc);
1151
1152 spin_lock_irqsave(&host->lock, flags);
1153
Pierre Ossman1e728592008-04-16 19:13:13 +02001154 if (host->flags & SDHCI_DEVICE_DEAD)
1155 present = 0;
1156 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001157 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158
1159 spin_unlock_irqrestore(&host->lock, flags);
1160
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001161 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1162 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 return !(present & SDHCI_WRITE_PROTECT);
1164}
1165
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001166static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1167{
1168 struct sdhci_host *host;
1169 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001170
1171 host = mmc_priv(mmc);
1172
1173 spin_lock_irqsave(&host->lock, flags);
1174
Pierre Ossman1e728592008-04-16 19:13:13 +02001175 if (host->flags & SDHCI_DEVICE_DEAD)
1176 goto out;
1177
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001178 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001179 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1180 else
1181 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001182out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001183 mmiowb();
1184
1185 spin_unlock_irqrestore(&host->lock, flags);
1186}
1187
David Brownellab7aefd2006-11-12 17:55:30 -08001188static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189 .request = sdhci_request,
1190 .set_ios = sdhci_set_ios,
1191 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001192 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193};
1194
1195/*****************************************************************************\
1196 * *
1197 * Tasklets *
1198 * *
1199\*****************************************************************************/
1200
1201static void sdhci_tasklet_card(unsigned long param)
1202{
1203 struct sdhci_host *host;
1204 unsigned long flags;
1205
1206 host = (struct sdhci_host*)param;
1207
1208 spin_lock_irqsave(&host->lock, flags);
1209
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001210 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211 if (host->mrq) {
1212 printk(KERN_ERR "%s: Card removed during transfer!\n",
1213 mmc_hostname(host->mmc));
1214 printk(KERN_ERR "%s: Resetting controller.\n",
1215 mmc_hostname(host->mmc));
1216
1217 sdhci_reset(host, SDHCI_RESET_CMD);
1218 sdhci_reset(host, SDHCI_RESET_DATA);
1219
Pierre Ossman17b04292007-07-22 22:18:46 +02001220 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221 tasklet_schedule(&host->finish_tasklet);
1222 }
1223 }
1224
1225 spin_unlock_irqrestore(&host->lock, flags);
1226
Pierre Ossman04cf5852008-08-18 22:18:14 +02001227 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228}
1229
1230static void sdhci_tasklet_finish(unsigned long param)
1231{
1232 struct sdhci_host *host;
1233 unsigned long flags;
1234 struct mmc_request *mrq;
1235
1236 host = (struct sdhci_host*)param;
1237
1238 spin_lock_irqsave(&host->lock, flags);
1239
1240 del_timer(&host->timer);
1241
1242 mrq = host->mrq;
1243
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 /*
1245 * The controller needs a reset of internal state machines
1246 * upon error conditions.
1247 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001248 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1249 (mrq->cmd->error ||
1250 (mrq->data && (mrq->data->error ||
1251 (mrq->data->stop && mrq->data->stop->error))) ||
1252 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001253
1254 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001255 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001256 unsigned int clock;
1257
1258 /* This is to force an update */
1259 clock = host->clock;
1260 host->clock = 0;
1261 sdhci_set_clock(host, clock);
1262 }
1263
1264 /* Spec says we should do both at the same time, but Ricoh
1265 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266 sdhci_reset(host, SDHCI_RESET_CMD);
1267 sdhci_reset(host, SDHCI_RESET_DATA);
1268 }
1269
1270 host->mrq = NULL;
1271 host->cmd = NULL;
1272 host->data = NULL;
1273
Pierre Ossmanf9134312008-12-21 17:01:48 +01001274#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001275 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001276#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277
Pierre Ossman5f25a662006-10-04 02:15:39 -07001278 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001279 spin_unlock_irqrestore(&host->lock, flags);
1280
1281 mmc_request_done(host->mmc, mrq);
1282}
1283
1284static void sdhci_timeout_timer(unsigned long data)
1285{
1286 struct sdhci_host *host;
1287 unsigned long flags;
1288
1289 host = (struct sdhci_host*)data;
1290
1291 spin_lock_irqsave(&host->lock, flags);
1292
1293 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001294 printk(KERN_ERR "%s: Timeout waiting for hardware "
1295 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296 sdhci_dumpregs(host);
1297
1298 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001299 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001300 sdhci_finish_data(host);
1301 } else {
1302 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001303 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001305 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306
1307 tasklet_schedule(&host->finish_tasklet);
1308 }
1309 }
1310
Pierre Ossman5f25a662006-10-04 02:15:39 -07001311 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 spin_unlock_irqrestore(&host->lock, flags);
1313}
1314
1315/*****************************************************************************\
1316 * *
1317 * Interrupt handling *
1318 * *
1319\*****************************************************************************/
1320
1321static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1322{
1323 BUG_ON(intmask == 0);
1324
1325 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001326 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1327 "though no command operation was in progress.\n",
1328 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 sdhci_dumpregs(host);
1330 return;
1331 }
1332
Pierre Ossman43b58b32007-07-25 23:15:27 +02001333 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001334 host->cmd->error = -ETIMEDOUT;
1335 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1336 SDHCI_INT_INDEX))
1337 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338
Pierre Ossmane8095172008-07-25 01:09:08 +02001339 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001341 return;
1342 }
1343
1344 /*
1345 * The host can send and interrupt when the busy state has
1346 * ended, allowing us to wait without wasting CPU cycles.
1347 * Unfortunately this is overloaded on the "data complete"
1348 * interrupt, so we need to take some care when handling
1349 * it.
1350 *
1351 * Note: The 1.0 specification is a bit ambiguous about this
1352 * feature so there might be some problems with older
1353 * controllers.
1354 */
1355 if (host->cmd->flags & MMC_RSP_BUSY) {
1356 if (host->cmd->data)
1357 DBG("Cannot wait for busy signal when also "
1358 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001359 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001360 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001361
1362 /* The controller does not support the end-of-busy IRQ,
1363 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001364 }
1365
1366 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001367 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368}
1369
1370static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1371{
1372 BUG_ON(intmask == 0);
1373
1374 if (!host->data) {
1375 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001376 * The "data complete" interrupt is also used to
1377 * indicate that a busy state has ended. See comment
1378 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001380 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1381 if (intmask & SDHCI_INT_DATA_END) {
1382 sdhci_finish_command(host);
1383 return;
1384 }
1385 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001386
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001387 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1388 "though no data operation was in progress.\n",
1389 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001390 sdhci_dumpregs(host);
1391
1392 return;
1393 }
1394
1395 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001396 host->data->error = -ETIMEDOUT;
1397 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1398 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001399 else if (intmask & SDHCI_INT_ADMA_ERROR)
1400 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001401
Pierre Ossman17b04292007-07-22 22:18:46 +02001402 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403 sdhci_finish_data(host);
1404 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001405 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406 sdhci_transfer_pio(host);
1407
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001408 /*
1409 * We currently don't do anything fancy with DMA
1410 * boundaries, but as we can't disable the feature
1411 * we need to at least restart the transfer.
1412 */
1413 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001414 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1415 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001416
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001417 if (intmask & SDHCI_INT_DATA_END) {
1418 if (host->cmd) {
1419 /*
1420 * Data managed to finish before the
1421 * command completed. Make sure we do
1422 * things in the proper order.
1423 */
1424 host->data_early = 1;
1425 } else {
1426 sdhci_finish_data(host);
1427 }
1428 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 }
1430}
1431
David Howells7d12e782006-10-05 14:55:46 +01001432static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433{
1434 irqreturn_t result;
1435 struct sdhci_host* host = dev_id;
1436 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001437 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001438
1439 spin_lock(&host->lock);
1440
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001441 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442
Mark Lord62df67a52007-03-06 13:30:13 +01001443 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444 result = IRQ_NONE;
1445 goto out;
1446 }
1447
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001448 DBG("*** %s got interrupt: 0x%08x\n",
1449 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001450
Pierre Ossman3192a282006-06-30 02:22:26 -07001451 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001452 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1453 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001455 }
1456
1457 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458
1459 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001460 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1461 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001462 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463 }
1464
1465 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001466 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1467 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001468 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001469 }
1470
1471 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1472
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001473 intmask &= ~SDHCI_INT_ERROR;
1474
Pierre Ossmand129bce2006-03-24 03:18:17 -08001475 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001476 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001477 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001478 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479 }
1480
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001481 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001482
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001483 if (intmask & SDHCI_INT_CARD_INT)
1484 cardint = 1;
1485
1486 intmask &= ~SDHCI_INT_CARD_INT;
1487
Pierre Ossman3192a282006-06-30 02:22:26 -07001488 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001489 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001490 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 sdhci_dumpregs(host);
1492
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001493 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001494 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495
1496 result = IRQ_HANDLED;
1497
Pierre Ossman5f25a662006-10-04 02:15:39 -07001498 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499out:
1500 spin_unlock(&host->lock);
1501
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001502 /*
1503 * We have to delay this as it calls back into the driver.
1504 */
1505 if (cardint)
1506 mmc_signal_sdio_irq(host->mmc);
1507
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508 return result;
1509}
1510
1511/*****************************************************************************\
1512 * *
1513 * Suspend/resume *
1514 * *
1515\*****************************************************************************/
1516
1517#ifdef CONFIG_PM
1518
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001519int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001520{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001521 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001523 sdhci_disable_card_detection(host);
1524
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001525 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001526 if (ret)
1527 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001529 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530
1531 return 0;
1532}
1533
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001534EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001536int sdhci_resume_host(struct sdhci_host *host)
1537{
1538 int ret;
1539
1540 if (host->flags & SDHCI_USE_DMA) {
1541 if (host->ops->enable_dma)
1542 host->ops->enable_dma(host);
1543 }
1544
1545 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1546 mmc_hostname(host->mmc), host);
1547 if (ret)
1548 return ret;
1549
1550 sdhci_init(host);
1551 mmiowb();
1552
1553 ret = mmc_resume_host(host->mmc);
1554 if (ret)
1555 return ret;
1556
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001557 sdhci_enable_card_detection(host);
1558
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001559 return 0;
1560}
1561
1562EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001563
1564#endif /* CONFIG_PM */
1565
1566/*****************************************************************************\
1567 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001568 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001569 * *
1570\*****************************************************************************/
1571
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001572struct sdhci_host *sdhci_alloc_host(struct device *dev,
1573 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001574{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001575 struct mmc_host *mmc;
1576 struct sdhci_host *host;
1577
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001578 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001579
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001580 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001581 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001582 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583
1584 host = mmc_priv(mmc);
1585 host->mmc = mmc;
1586
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001587 return host;
1588}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001589
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001590EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001591
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001592int sdhci_add_host(struct sdhci_host *host)
1593{
1594 struct mmc_host *mmc;
1595 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001596 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001597
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001598 WARN_ON(host == NULL);
1599 if (host == NULL)
1600 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001602 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001604 if (debug_quirks)
1605 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001606
Pierre Ossmand96649e2006-06-30 02:22:30 -07001607 sdhci_reset(host, SDHCI_RESET_ALL);
1608
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001609 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001610 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1611 >> SDHCI_SPEC_VER_SHIFT;
1612 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001613 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001614 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001615 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001616 }
1617
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001618 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001620 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001621 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001622 else if (!(caps & SDHCI_CAN_DO_DMA))
1623 DBG("Controller doesn't have DMA capability\n");
1624 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625 host->flags |= SDHCI_USE_DMA;
1626
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001627 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001628 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001629 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001630 host->flags &= ~SDHCI_USE_DMA;
1631 }
1632
Pierre Ossmand129bce2006-03-24 03:18:17 -08001633 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001634 if ((host->version >= SDHCI_SPEC_200) &&
1635 (caps & SDHCI_CAN_DO_ADMA2))
1636 host->flags |= SDHCI_USE_ADMA;
1637 }
1638
1639 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1640 (host->flags & SDHCI_USE_ADMA)) {
1641 DBG("Disabling ADMA as it is marked broken\n");
1642 host->flags &= ~SDHCI_USE_ADMA;
1643 }
1644
1645 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001646 if (host->ops->enable_dma) {
1647 if (host->ops->enable_dma(host)) {
1648 printk(KERN_WARNING "%s: No suitable DMA "
1649 "available. Falling back to PIO.\n",
1650 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001651 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653 }
1654 }
1655
Pierre Ossman2134a922008-06-28 18:28:51 +02001656 if (host->flags & SDHCI_USE_ADMA) {
1657 /*
1658 * We need to allocate descriptors for all sg entries
1659 * (128) and potentially one alignment transfer for
1660 * each of those entries.
1661 */
1662 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1663 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1664 if (!host->adma_desc || !host->align_buffer) {
1665 kfree(host->adma_desc);
1666 kfree(host->align_buffer);
1667 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1668 "buffers. Falling back to standard DMA.\n",
1669 mmc_hostname(mmc));
1670 host->flags &= ~SDHCI_USE_ADMA;
1671 }
1672 }
1673
Pierre Ossman76591502008-07-21 00:32:11 +02001674 /*
1675 * If we use DMA, then it's up to the caller to set the DMA
1676 * mask, but PIO does not need the hw shim so we set a new
1677 * mask here in that case.
1678 */
1679 if (!(host->flags & SDHCI_USE_DMA)) {
1680 host->dma_mask = DMA_BIT_MASK(64);
1681 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1682 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001683
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001684 host->max_clk =
1685 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001686 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001687 if (host->max_clk == 0) {
1688 if (!host->ops->get_max_clock) {
1689 printk(KERN_ERR
1690 "%s: Hardware doesn't specify base clock "
1691 "frequency.\n", mmc_hostname(mmc));
1692 return -ENODEV;
1693 }
1694 host->max_clk = host->ops->get_max_clock(host);
1695 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001696
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001697 host->timeout_clk =
1698 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1699 if (host->timeout_clk == 0) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001700 if (!host->ops->get_timeout_clock) {
1701 printk(KERN_ERR
1702 "%s: Hardware doesn't specify timeout clock "
1703 "frequency.\n", mmc_hostname(mmc));
1704 return -ENODEV;
1705 }
1706 host->timeout_clk = host->ops->get_timeout_clock(host);
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001707 }
1708 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1709 host->timeout_clk *= 1000;
1710
Pierre Ossmand129bce2006-03-24 03:18:17 -08001711 /*
1712 * Set host parameters.
1713 */
1714 mmc->ops = &sdhci_ops;
1715 mmc->f_min = host->max_clk / 256;
1716 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001717 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001718
Pierre Ossman86a6a872009-02-02 21:13:49 +01001719 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001720 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1721
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001722 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1723 mmc->caps |= MMC_CAP_NEEDS_POLL;
1724
Pierre Ossman146ad662006-06-30 02:22:23 -07001725 mmc->ocr_avail = 0;
1726 if (caps & SDHCI_CAN_VDD_330)
1727 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001728 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001729 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001730 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001731 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001732
1733 if (mmc->ocr_avail == 0) {
1734 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001735 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001736 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001737 }
1738
Pierre Ossmand129bce2006-03-24 03:18:17 -08001739 spin_lock_init(&host->lock);
1740
1741 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001742 * Maximum number of segments. Depends on if the hardware
1743 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001744 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001745 if (host->flags & SDHCI_USE_ADMA)
1746 mmc->max_hw_segs = 128;
1747 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001748 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001749 else /* PIO */
1750 mmc->max_hw_segs = 128;
1751 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001752
1753 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001754 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001755 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001756 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001757 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001758
1759 /*
1760 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001761 * of bytes. When doing hardware scatter/gather, each entry cannot
1762 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001764 if (host->flags & SDHCI_USE_ADMA)
1765 mmc->max_seg_size = 65536;
1766 else
1767 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768
1769 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001770 * Maximum block size. This varies from controller to controller and
1771 * is specified in the capabilities register.
1772 */
1773 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1774 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001775 printk(KERN_WARNING "%s: Invalid maximum block size, "
1776 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001777 mmc->max_blk_size = 512;
1778 } else
1779 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001780
1781 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001782 * Maximum block count.
1783 */
1784 mmc->max_blk_count = 65535;
1785
1786 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787 * Init tasklets.
1788 */
1789 tasklet_init(&host->card_tasklet,
1790 sdhci_tasklet_card, (unsigned long)host);
1791 tasklet_init(&host->finish_tasklet,
1792 sdhci_tasklet_finish, (unsigned long)host);
1793
Al Viroe4cad1b2006-10-10 22:47:07 +01001794 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001795
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001796 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001797 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001798 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001799 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001800
1801 sdhci_init(host);
1802
1803#ifdef CONFIG_MMC_DEBUG
1804 sdhci_dumpregs(host);
1805#endif
1806
Pierre Ossmanf9134312008-12-21 17:01:48 +01001807#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001808 snprintf(host->led_name, sizeof(host->led_name),
1809 "%s::", mmc_hostname(mmc));
1810 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001811 host->led.brightness = LED_OFF;
1812 host->led.default_trigger = mmc_hostname(mmc);
1813 host->led.brightness_set = sdhci_led_control;
1814
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001815 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001816 if (ret)
1817 goto reset;
1818#endif
1819
Pierre Ossman5f25a662006-10-04 02:15:39 -07001820 mmiowb();
1821
Pierre Ossmand129bce2006-03-24 03:18:17 -08001822 mmc_add_host(mmc);
1823
Pierre Ossman2134a922008-06-28 18:28:51 +02001824 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001825 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Pierre Ossman2134a922008-06-28 18:28:51 +02001826 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001827 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1828
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001829 sdhci_enable_card_detection(host);
1830
Pierre Ossmand129bce2006-03-24 03:18:17 -08001831 return 0;
1832
Pierre Ossmanf9134312008-12-21 17:01:48 +01001833#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001834reset:
1835 sdhci_reset(host, SDHCI_RESET_ALL);
1836 free_irq(host->irq, host);
1837#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001838untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001839 tasklet_kill(&host->card_tasklet);
1840 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001841
1842 return ret;
1843}
1844
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001845EXPORT_SYMBOL_GPL(sdhci_add_host);
1846
Pierre Ossman1e728592008-04-16 19:13:13 +02001847void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001848{
Pierre Ossman1e728592008-04-16 19:13:13 +02001849 unsigned long flags;
1850
1851 if (dead) {
1852 spin_lock_irqsave(&host->lock, flags);
1853
1854 host->flags |= SDHCI_DEVICE_DEAD;
1855
1856 if (host->mrq) {
1857 printk(KERN_ERR "%s: Controller removed during "
1858 " transfer!\n", mmc_hostname(host->mmc));
1859
1860 host->mrq->cmd->error = -ENOMEDIUM;
1861 tasklet_schedule(&host->finish_tasklet);
1862 }
1863
1864 spin_unlock_irqrestore(&host->lock, flags);
1865 }
1866
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001867 sdhci_disable_card_detection(host);
1868
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001869 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001870
Pierre Ossmanf9134312008-12-21 17:01:48 +01001871#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001872 led_classdev_unregister(&host->led);
1873#endif
1874
Pierre Ossman1e728592008-04-16 19:13:13 +02001875 if (!dead)
1876 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001877
1878 free_irq(host->irq, host);
1879
1880 del_timer_sync(&host->timer);
1881
1882 tasklet_kill(&host->card_tasklet);
1883 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001884
1885 kfree(host->adma_desc);
1886 kfree(host->align_buffer);
1887
1888 host->adma_desc = NULL;
1889 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001890}
1891
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001892EXPORT_SYMBOL_GPL(sdhci_remove_host);
1893
1894void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001895{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001896 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001897}
1898
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001899EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001900
1901/*****************************************************************************\
1902 * *
1903 * Driver init/exit *
1904 * *
1905\*****************************************************************************/
1906
1907static int __init sdhci_drv_init(void)
1908{
1909 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001910 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001911 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1912
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001913 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001914}
1915
1916static void __exit sdhci_drv_exit(void)
1917{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001918}
1919
1920module_init(sdhci_drv_init);
1921module_exit(sdhci_drv_exit);
1922
Pierre Ossmandf673b22006-06-30 02:22:31 -07001923module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001924
Pierre Ossmand129bce2006-03-24 03:18:17 -08001925MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001926MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001927MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001928
Pierre Ossmandf673b22006-06-30 02:22:31 -07001929MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");