blob: 6fbbc005dd7f39d515bbe70e06c38917f73575da [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
115 if (enable)
116 sdhci_unmask_irqs(host, irqs);
117 else
118 sdhci_mask_irqs(host, irqs);
119}
120
121static void sdhci_enable_card_detection(struct sdhci_host *host)
122{
123 sdhci_set_card_detection(host, true);
124}
125
126static void sdhci_disable_card_detection(struct sdhci_host *host)
127{
128 sdhci_set_card_detection(host, false);
129}
130
Pierre Ossmand129bce2006-03-24 03:18:17 -0800131static void sdhci_reset(struct sdhci_host *host, u8 mask)
132{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700133 unsigned long timeout;
134
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100135 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300136 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700137 SDHCI_CARD_PRESENT))
138 return;
139 }
140
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300141 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800142
Pierre Ossmane16514d82006-06-30 02:22:24 -0700143 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800144 host->clock = 0;
145
Pierre Ossmane16514d82006-06-30 02:22:24 -0700146 /* Wait max 100 ms */
147 timeout = 100;
148
149 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300150 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700151 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100152 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700153 mmc_hostname(host->mmc), (int)mask);
154 sdhci_dumpregs(host);
155 return;
156 }
157 timeout--;
158 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800159 }
160}
161
162static void sdhci_init(struct sdhci_host *host)
163{
Pierre Ossmand129bce2006-03-24 03:18:17 -0800164 sdhci_reset(host, SDHCI_RESET_ALL);
165
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300166 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
167 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700168 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
169 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300170 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300171}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800172
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300173static void sdhci_reinit(struct sdhci_host *host)
174{
175 sdhci_init(host);
176 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177}
178
179static void sdhci_activate_led(struct sdhci_host *host)
180{
181 u8 ctrl;
182
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300183 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300185 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186}
187
188static void sdhci_deactivate_led(struct sdhci_host *host)
189{
190 u8 ctrl;
191
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300192 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800195}
196
Pierre Ossmanf9134312008-12-21 17:01:48 +0100197#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100198static void sdhci_led_control(struct led_classdev *led,
199 enum led_brightness brightness)
200{
201 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
202 unsigned long flags;
203
204 spin_lock_irqsave(&host->lock, flags);
205
206 if (brightness == LED_OFF)
207 sdhci_deactivate_led(host);
208 else
209 sdhci_activate_led(host);
210
211 spin_unlock_irqrestore(&host->lock, flags);
212}
213#endif
214
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215/*****************************************************************************\
216 * *
217 * Core functions *
218 * *
219\*****************************************************************************/
220
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100221static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222{
Pierre Ossman76591502008-07-21 00:32:11 +0200223 unsigned long flags;
224 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700225 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200226 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100228 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100230 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200231 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Pierre Ossman76591502008-07-21 00:32:11 +0200233 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800234
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100235 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200236 if (!sg_miter_next(&host->sg_miter))
237 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800238
Pierre Ossman76591502008-07-21 00:32:11 +0200239 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800240
Pierre Ossman76591502008-07-21 00:32:11 +0200241 blksize -= len;
242 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200243
Pierre Ossman76591502008-07-21 00:32:11 +0200244 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245
Pierre Ossman76591502008-07-21 00:32:11 +0200246 while (len) {
247 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300248 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200249 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250 }
Pierre Ossman76591502008-07-21 00:32:11 +0200251
252 *buf = scratch & 0xFF;
253
254 buf++;
255 scratch >>= 8;
256 chunk--;
257 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258 }
259 }
Pierre Ossman76591502008-07-21 00:32:11 +0200260
261 sg_miter_stop(&host->sg_miter);
262
263 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100264}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100266static void sdhci_write_block_pio(struct sdhci_host *host)
267{
Pierre Ossman76591502008-07-21 00:32:11 +0200268 unsigned long flags;
269 size_t blksize, len, chunk;
270 u32 scratch;
271 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100272
273 DBG("PIO writing\n");
274
275 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200276 chunk = 0;
277 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100278
Pierre Ossman76591502008-07-21 00:32:11 +0200279 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100280
281 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200282 if (!sg_miter_next(&host->sg_miter))
283 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100284
Pierre Ossman76591502008-07-21 00:32:11 +0200285 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200286
Pierre Ossman76591502008-07-21 00:32:11 +0200287 blksize -= len;
288 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100289
Pierre Ossman76591502008-07-21 00:32:11 +0200290 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100291
Pierre Ossman76591502008-07-21 00:32:11 +0200292 while (len) {
293 scratch |= (u32)*buf << (chunk * 8);
294
295 buf++;
296 chunk++;
297 len--;
298
299 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300300 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200301 chunk = 0;
302 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100303 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304 }
305 }
Pierre Ossman76591502008-07-21 00:32:11 +0200306
307 sg_miter_stop(&host->sg_miter);
308
309 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310}
311
312static void sdhci_transfer_pio(struct sdhci_host *host)
313{
314 u32 mask;
315
316 BUG_ON(!host->data);
317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319 return;
320
321 if (host->data->flags & MMC_DATA_READ)
322 mask = SDHCI_DATA_AVAILABLE;
323 else
324 mask = SDHCI_SPACE_AVAILABLE;
325
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200326 /*
327 * Some controllers (JMicron JMB38x) mess up the buffer bits
328 * for transfers < 4 bytes. As long as it is just one block,
329 * we can ignore the bits.
330 */
331 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
332 (host->data->blocks == 1))
333 mask = ~0;
334
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300335 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336 if (host->data->flags & MMC_DATA_READ)
337 sdhci_read_block_pio(host);
338 else
339 sdhci_write_block_pio(host);
340
Pierre Ossman76591502008-07-21 00:32:11 +0200341 host->blocks--;
342 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100343 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344 }
345
346 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347}
348
Pierre Ossman2134a922008-06-28 18:28:51 +0200349static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
350{
351 local_irq_save(*flags);
352 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
353}
354
355static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
356{
357 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
358 local_irq_restore(*flags);
359}
360
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200361static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200362 struct mmc_data *data)
363{
364 int direction;
365
366 u8 *desc;
367 u8 *align;
368 dma_addr_t addr;
369 dma_addr_t align_addr;
370 int len, offset;
371
372 struct scatterlist *sg;
373 int i;
374 char *buffer;
375 unsigned long flags;
376
377 /*
378 * The spec does not specify endianness of descriptor table.
379 * We currently guess that it is LE.
380 */
381
382 if (data->flags & MMC_DATA_READ)
383 direction = DMA_FROM_DEVICE;
384 else
385 direction = DMA_TO_DEVICE;
386
387 /*
388 * The ADMA descriptor table is mapped further down as we
389 * need to fill it with data first.
390 */
391
392 host->align_addr = dma_map_single(mmc_dev(host->mmc),
393 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700394 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200395 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200396 BUG_ON(host->align_addr & 0x3);
397
398 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
399 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200400 if (host->sg_count == 0)
401 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200402
403 desc = host->adma_desc;
404 align = host->align_buffer;
405
406 align_addr = host->align_addr;
407
408 for_each_sg(data->sg, sg, host->sg_count, i) {
409 addr = sg_dma_address(sg);
410 len = sg_dma_len(sg);
411
412 /*
413 * The SDHCI specification states that ADMA
414 * addresses must be 32-bit aligned. If they
415 * aren't, then we use a bounce buffer for
416 * the (up to three) bytes that screw up the
417 * alignment.
418 */
419 offset = (4 - (addr & 0x3)) & 0x3;
420 if (offset) {
421 if (data->flags & MMC_DATA_WRITE) {
422 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200423 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200424 memcpy(align, buffer, offset);
425 sdhci_kunmap_atomic(buffer, &flags);
426 }
427
428 desc[7] = (align_addr >> 24) & 0xff;
429 desc[6] = (align_addr >> 16) & 0xff;
430 desc[5] = (align_addr >> 8) & 0xff;
431 desc[4] = (align_addr >> 0) & 0xff;
432
433 BUG_ON(offset > 65536);
434
435 desc[3] = (offset >> 8) & 0xff;
436 desc[2] = (offset >> 0) & 0xff;
437
438 desc[1] = 0x00;
439 desc[0] = 0x21; /* tran, valid */
440
441 align += 4;
442 align_addr += 4;
443
444 desc += 8;
445
446 addr += offset;
447 len -= offset;
448 }
449
450 desc[7] = (addr >> 24) & 0xff;
451 desc[6] = (addr >> 16) & 0xff;
452 desc[5] = (addr >> 8) & 0xff;
453 desc[4] = (addr >> 0) & 0xff;
454
455 BUG_ON(len > 65536);
456
457 desc[3] = (len >> 8) & 0xff;
458 desc[2] = (len >> 0) & 0xff;
459
460 desc[1] = 0x00;
461 desc[0] = 0x21; /* tran, valid */
462
463 desc += 8;
464
465 /*
466 * If this triggers then we have a calculation bug
467 * somewhere. :/
468 */
469 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
470 }
471
472 /*
473 * Add a terminating entry.
474 */
475 desc[7] = 0;
476 desc[6] = 0;
477 desc[5] = 0;
478 desc[4] = 0;
479
480 desc[3] = 0;
481 desc[2] = 0;
482
483 desc[1] = 0x00;
484 desc[0] = 0x03; /* nop, end, valid */
485
486 /*
487 * Resync align buffer as we might have changed it.
488 */
489 if (data->flags & MMC_DATA_WRITE) {
490 dma_sync_single_for_device(mmc_dev(host->mmc),
491 host->align_addr, 128 * 4, direction);
492 }
493
494 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
495 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200496 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200497 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200498 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200499
500 return 0;
501
502unmap_entries:
503 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
504 data->sg_len, direction);
505unmap_align:
506 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
507 128 * 4, direction);
508fail:
509 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200510}
511
512static void sdhci_adma_table_post(struct sdhci_host *host,
513 struct mmc_data *data)
514{
515 int direction;
516
517 struct scatterlist *sg;
518 int i, size;
519 u8 *align;
520 char *buffer;
521 unsigned long flags;
522
523 if (data->flags & MMC_DATA_READ)
524 direction = DMA_FROM_DEVICE;
525 else
526 direction = DMA_TO_DEVICE;
527
528 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
529 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
530
531 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
532 128 * 4, direction);
533
534 if (data->flags & MMC_DATA_READ) {
535 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
536 data->sg_len, direction);
537
538 align = host->align_buffer;
539
540 for_each_sg(data->sg, sg, host->sg_count, i) {
541 if (sg_dma_address(sg) & 0x3) {
542 size = 4 - (sg_dma_address(sg) & 0x3);
543
544 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200545 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200546 memcpy(buffer, align, size);
547 sdhci_kunmap_atomic(buffer, &flags);
548
549 align += 4;
550 }
551 }
552 }
553
554 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
555 data->sg_len, direction);
556}
557
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200558static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800559{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700560 u8 count;
561 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800562
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200563 /*
564 * If the host controller provides us with an incorrect timeout
565 * value, just skip the check and use 0xE. The hardware may take
566 * longer to time out, but that's much better than having a too-short
567 * timeout value.
568 */
569 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
570 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200571
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700572 /* timeout in us */
573 target_timeout = data->timeout_ns / 1000 +
574 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800575
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700576 /*
577 * Figure out needed cycles.
578 * We do this in steps in order to fit inside a 32 bit int.
579 * The first step is the minimum timeout, which will have a
580 * minimum resolution of 6 bits:
581 * (1) 2^13*1000 > 2^22,
582 * (2) host->timeout_clk < 2^16
583 * =>
584 * (1) / (2) > 2^6
585 */
586 count = 0;
587 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
588 while (current_timeout < target_timeout) {
589 count++;
590 current_timeout <<= 1;
591 if (count >= 0xF)
592 break;
593 }
594
595 if (count >= 0xF) {
596 printk(KERN_WARNING "%s: Too large timeout requested!\n",
597 mmc_hostname(host->mmc));
598 count = 0xE;
599 }
600
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200601 return count;
602}
603
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300604static void sdhci_set_transfer_irqs(struct sdhci_host *host)
605{
606 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
607 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
608
609 if (host->flags & SDHCI_REQ_USE_DMA)
610 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
611 else
612 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
613}
614
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200615static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
616{
617 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200618 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200619 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200620
621 WARN_ON(host->data);
622
623 if (data == NULL)
624 return;
625
626 /* Sanity checks */
627 BUG_ON(data->blksz * data->blocks > 524288);
628 BUG_ON(data->blksz > host->mmc->max_blk_size);
629 BUG_ON(data->blocks > 65535);
630
631 host->data = data;
632 host->data_early = 0;
633
634 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300635 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800636
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100637 if (host->flags & SDHCI_USE_DMA)
638 host->flags |= SDHCI_REQ_USE_DMA;
639
Pierre Ossman2134a922008-06-28 18:28:51 +0200640 /*
641 * FIXME: This doesn't account for merging when mapping the
642 * scatterlist.
643 */
644 if (host->flags & SDHCI_REQ_USE_DMA) {
645 int broken, i;
646 struct scatterlist *sg;
647
648 broken = 0;
649 if (host->flags & SDHCI_USE_ADMA) {
650 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
651 broken = 1;
652 } else {
653 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
654 broken = 1;
655 }
656
657 if (unlikely(broken)) {
658 for_each_sg(data->sg, sg, data->sg_len, i) {
659 if (sg->length & 0x3) {
660 DBG("Reverting to PIO because of "
661 "transfer size (%d)\n",
662 sg->length);
663 host->flags &= ~SDHCI_REQ_USE_DMA;
664 break;
665 }
666 }
667 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100668 }
669
670 /*
671 * The assumption here being that alignment is the same after
672 * translation to device address space.
673 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200674 if (host->flags & SDHCI_REQ_USE_DMA) {
675 int broken, i;
676 struct scatterlist *sg;
677
678 broken = 0;
679 if (host->flags & SDHCI_USE_ADMA) {
680 /*
681 * As we use 3 byte chunks to work around
682 * alignment problems, we need to check this
683 * quirk.
684 */
685 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
686 broken = 1;
687 } else {
688 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
689 broken = 1;
690 }
691
692 if (unlikely(broken)) {
693 for_each_sg(data->sg, sg, data->sg_len, i) {
694 if (sg->offset & 0x3) {
695 DBG("Reverting to PIO because of "
696 "bad alignment\n");
697 host->flags &= ~SDHCI_REQ_USE_DMA;
698 break;
699 }
700 }
701 }
702 }
703
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200704 if (host->flags & SDHCI_REQ_USE_DMA) {
705 if (host->flags & SDHCI_USE_ADMA) {
706 ret = sdhci_adma_table_pre(host, data);
707 if (ret) {
708 /*
709 * This only happens when someone fed
710 * us an invalid request.
711 */
712 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200713 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200714 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300715 sdhci_writel(host, host->adma_addr,
716 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200717 }
718 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300719 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200720
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300721 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200722 data->sg, data->sg_len,
723 (data->flags & MMC_DATA_READ) ?
724 DMA_FROM_DEVICE :
725 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300726 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200727 /*
728 * This only happens when someone fed
729 * us an invalid request.
730 */
731 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200732 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200733 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200734 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300735 sdhci_writel(host, sg_dma_address(data->sg),
736 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200737 }
738 }
739 }
740
Pierre Ossman2134a922008-06-28 18:28:51 +0200741 /*
742 * Always adjust the DMA selection as some controllers
743 * (e.g. JMicron) can't do PIO properly when the selection
744 * is ADMA.
745 */
746 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300747 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200748 ctrl &= ~SDHCI_CTRL_DMA_MASK;
749 if ((host->flags & SDHCI_REQ_USE_DMA) &&
750 (host->flags & SDHCI_USE_ADMA))
751 ctrl |= SDHCI_CTRL_ADMA32;
752 else
753 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300754 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100755 }
756
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200757 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Pierre Ossman76591502008-07-21 00:32:11 +0200758 sg_miter_start(&host->sg_miter,
759 data->sg, data->sg_len, SG_MITER_ATOMIC);
760 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800761 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700762
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300763 sdhci_set_transfer_irqs(host);
764
Pierre Ossmanbab76962006-07-02 16:51:35 +0100765 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300766 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
767 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700768}
769
770static void sdhci_set_transfer_mode(struct sdhci_host *host,
771 struct mmc_data *data)
772{
773 u16 mode;
774
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700775 if (data == NULL)
776 return;
777
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200778 WARN_ON(!host->data);
779
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700780 mode = SDHCI_TRNS_BLK_CNT_EN;
781 if (data->blocks > 1)
782 mode |= SDHCI_TRNS_MULTI;
783 if (data->flags & MMC_DATA_READ)
784 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100785 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700786 mode |= SDHCI_TRNS_DMA;
787
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300788 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800789}
790
791static void sdhci_finish_data(struct sdhci_host *host)
792{
793 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800794
795 BUG_ON(!host->data);
796
797 data = host->data;
798 host->data = NULL;
799
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100800 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200801 if (host->flags & SDHCI_USE_ADMA)
802 sdhci_adma_table_post(host, data);
803 else {
804 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
805 data->sg_len, (data->flags & MMC_DATA_READ) ?
806 DMA_FROM_DEVICE : DMA_TO_DEVICE);
807 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800808 }
809
810 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200811 * The specification states that the block count register must
812 * be updated, but it does not specify at what point in the
813 * data flow. That makes the register entirely useless to read
814 * back so we have to assume that nothing made it to the card
815 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800816 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200817 if (data->error)
818 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800819 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200820 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800821
Pierre Ossmand129bce2006-03-24 03:18:17 -0800822 if (data->stop) {
823 /*
824 * The controller needs a reset of internal state machines
825 * upon error conditions.
826 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200827 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800828 sdhci_reset(host, SDHCI_RESET_CMD);
829 sdhci_reset(host, SDHCI_RESET_DATA);
830 }
831
832 sdhci_send_command(host, data->stop);
833 } else
834 tasklet_schedule(&host->finish_tasklet);
835}
836
837static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
838{
839 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700840 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700841 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800842
843 WARN_ON(host->cmd);
844
Pierre Ossmand129bce2006-03-24 03:18:17 -0800845 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700846 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700847
848 mask = SDHCI_CMD_INHIBIT;
849 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
850 mask |= SDHCI_DATA_INHIBIT;
851
852 /* We shouldn't wait for data inihibit for stop commands, even
853 though they might use busy signaling */
854 if (host->mrq->data && (cmd == host->mrq->data->stop))
855 mask &= ~SDHCI_DATA_INHIBIT;
856
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300857 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700858 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800859 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100860 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800861 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200862 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800863 tasklet_schedule(&host->finish_tasklet);
864 return;
865 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700866 timeout--;
867 mdelay(1);
868 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800869
870 mod_timer(&host->timer, jiffies + 10 * HZ);
871
872 host->cmd = cmd;
873
874 sdhci_prepare_data(host, cmd->data);
875
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300876 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800877
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700878 sdhci_set_transfer_mode(host, cmd->data);
879
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100881 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800882 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200883 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 tasklet_schedule(&host->finish_tasklet);
885 return;
886 }
887
888 if (!(cmd->flags & MMC_RSP_PRESENT))
889 flags = SDHCI_CMD_RESP_NONE;
890 else if (cmd->flags & MMC_RSP_136)
891 flags = SDHCI_CMD_RESP_LONG;
892 else if (cmd->flags & MMC_RSP_BUSY)
893 flags = SDHCI_CMD_RESP_SHORT_BUSY;
894 else
895 flags = SDHCI_CMD_RESP_SHORT;
896
897 if (cmd->flags & MMC_RSP_CRC)
898 flags |= SDHCI_CMD_CRC;
899 if (cmd->flags & MMC_RSP_OPCODE)
900 flags |= SDHCI_CMD_INDEX;
901 if (cmd->data)
902 flags |= SDHCI_CMD_DATA;
903
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300904 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905}
906
907static void sdhci_finish_command(struct sdhci_host *host)
908{
909 int i;
910
911 BUG_ON(host->cmd == NULL);
912
913 if (host->cmd->flags & MMC_RSP_PRESENT) {
914 if (host->cmd->flags & MMC_RSP_136) {
915 /* CRC is stripped so we need to do some shifting. */
916 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300917 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918 SDHCI_RESPONSE + (3-i)*4) << 8;
919 if (i != 3)
920 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300921 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922 SDHCI_RESPONSE + (3-i)*4-1);
923 }
924 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300925 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800926 }
927 }
928
Pierre Ossman17b04292007-07-22 22:18:46 +0200929 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800930
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200931 if (host->data && host->data_early)
932 sdhci_finish_data(host);
933
934 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 tasklet_schedule(&host->finish_tasklet);
936
937 host->cmd = NULL;
938}
939
940static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
941{
942 int div;
943 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700944 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945
946 if (clock == host->clock)
947 return;
948
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950
951 if (clock == 0)
952 goto out;
953
954 for (div = 1;div < 256;div *= 2) {
955 if ((host->max_clk / div) <= clock)
956 break;
957 }
958 div >>= 1;
959
960 clk = div << SDHCI_DIVIDER_SHIFT;
961 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300962 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963
964 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700965 timeout = 10;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300966 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700967 & SDHCI_CLOCK_INT_STABLE)) {
968 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100969 printk(KERN_ERR "%s: Internal clock never "
970 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 sdhci_dumpregs(host);
972 return;
973 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700974 timeout--;
975 mdelay(1);
976 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977
978 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300979 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980
981out:
982 host->clock = clock;
983}
984
Pierre Ossman146ad662006-06-30 02:22:23 -0700985static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
986{
987 u8 pwr;
988
989 if (host->power == power)
990 return;
991
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100992 if (power == (unsigned short)-1) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300993 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700994 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100995 }
996
997 /*
998 * Spec says that we should clear the power reg before setting
999 * a new value. Some controllers don't seem to like this though.
1000 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001001 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001002 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001003
1004 pwr = SDHCI_POWER_ON;
1005
Philip Langdale4be34c92007-03-11 17:15:15 -07001006 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -07001007 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -07001008 pwr |= SDHCI_POWER_180;
1009 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001010 case MMC_VDD_29_30:
1011 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -07001012 pwr |= SDHCI_POWER_300;
1013 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001014 case MMC_VDD_32_33:
1015 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -07001016 pwr |= SDHCI_POWER_330;
1017 break;
1018 default:
1019 BUG();
1020 }
1021
Andres Salomone08c1692008-07-04 10:00:03 -07001022 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001023 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001024 * and set turn on power at the same time, so set the voltage first.
1025 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001026 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001027 sdhci_writeb(host, pwr & ~SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
Andres Salomone08c1692008-07-04 10:00:03 -07001028
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001030
1031out:
1032 host->power = power;
1033}
1034
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035/*****************************************************************************\
1036 * *
1037 * MMC callbacks *
1038 * *
1039\*****************************************************************************/
1040
1041static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1042{
1043 struct sdhci_host *host;
1044 unsigned long flags;
1045
1046 host = mmc_priv(mmc);
1047
1048 spin_lock_irqsave(&host->lock, flags);
1049
1050 WARN_ON(host->mrq != NULL);
1051
Pierre Ossmanf9134312008-12-21 17:01:48 +01001052#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001053 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001054#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055
1056 host->mrq = mrq;
1057
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)
Pierre Ossman1e728592008-04-16 19:13:13 +02001059 || (host->flags & SDHCI_DEVICE_DEAD)) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001060 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061 tasklet_schedule(&host->finish_tasklet);
1062 } else
1063 sdhci_send_command(host, mrq->cmd);
1064
Pierre Ossman5f25a662006-10-04 02:15:39 -07001065 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066 spin_unlock_irqrestore(&host->lock, flags);
1067}
1068
1069static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1070{
1071 struct sdhci_host *host;
1072 unsigned long flags;
1073 u8 ctrl;
1074
1075 host = mmc_priv(mmc);
1076
1077 spin_lock_irqsave(&host->lock, flags);
1078
Pierre Ossman1e728592008-04-16 19:13:13 +02001079 if (host->flags & SDHCI_DEVICE_DEAD)
1080 goto out;
1081
Pierre Ossmand129bce2006-03-24 03:18:17 -08001082 /*
1083 * Reset the chip on each power off.
1084 * Should clear out any weird states.
1085 */
1086 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001087 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001088 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089 }
1090
1091 sdhci_set_clock(host, ios->clock);
1092
1093 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001094 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001096 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001098 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001099
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100 if (ios->bus_width == MMC_BUS_WIDTH_4)
1101 ctrl |= SDHCI_CTRL_4BITBUS;
1102 else
1103 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001104
1105 if (ios->timing == MMC_TIMING_SD_HS)
1106 ctrl |= SDHCI_CTRL_HISPD;
1107 else
1108 ctrl &= ~SDHCI_CTRL_HISPD;
1109
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001110 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
Leandro Dorileob8352262007-07-25 23:47:04 +02001112 /*
1113 * Some (ENE) controllers go apeshit on some ios operation,
1114 * signalling timeout and CRC errors even on CMD0. Resetting
1115 * it on each ios seems to solve the problem.
1116 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001117 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001118 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1119
Pierre Ossman1e728592008-04-16 19:13:13 +02001120out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001121 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122 spin_unlock_irqrestore(&host->lock, flags);
1123}
1124
1125static int sdhci_get_ro(struct mmc_host *mmc)
1126{
1127 struct sdhci_host *host;
1128 unsigned long flags;
1129 int present;
1130
1131 host = mmc_priv(mmc);
1132
1133 spin_lock_irqsave(&host->lock, flags);
1134
Pierre Ossman1e728592008-04-16 19:13:13 +02001135 if (host->flags & SDHCI_DEVICE_DEAD)
1136 present = 0;
1137 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001138 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139
1140 spin_unlock_irqrestore(&host->lock, flags);
1141
1142 return !(present & SDHCI_WRITE_PROTECT);
1143}
1144
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001145static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1146{
1147 struct sdhci_host *host;
1148 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001149
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 goto out;
1156
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001157 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001158 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1159 else
1160 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001161out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001162 mmiowb();
1163
1164 spin_unlock_irqrestore(&host->lock, flags);
1165}
1166
David Brownellab7aefd2006-11-12 17:55:30 -08001167static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 .request = sdhci_request,
1169 .set_ios = sdhci_set_ios,
1170 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001171 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172};
1173
1174/*****************************************************************************\
1175 * *
1176 * Tasklets *
1177 * *
1178\*****************************************************************************/
1179
1180static void sdhci_tasklet_card(unsigned long param)
1181{
1182 struct sdhci_host *host;
1183 unsigned long flags;
1184
1185 host = (struct sdhci_host*)param;
1186
1187 spin_lock_irqsave(&host->lock, flags);
1188
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001189 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001190 if (host->mrq) {
1191 printk(KERN_ERR "%s: Card removed during transfer!\n",
1192 mmc_hostname(host->mmc));
1193 printk(KERN_ERR "%s: Resetting controller.\n",
1194 mmc_hostname(host->mmc));
1195
1196 sdhci_reset(host, SDHCI_RESET_CMD);
1197 sdhci_reset(host, SDHCI_RESET_DATA);
1198
Pierre Ossman17b04292007-07-22 22:18:46 +02001199 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200 tasklet_schedule(&host->finish_tasklet);
1201 }
1202 }
1203
1204 spin_unlock_irqrestore(&host->lock, flags);
1205
Pierre Ossman04cf5852008-08-18 22:18:14 +02001206 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207}
1208
1209static void sdhci_tasklet_finish(unsigned long param)
1210{
1211 struct sdhci_host *host;
1212 unsigned long flags;
1213 struct mmc_request *mrq;
1214
1215 host = (struct sdhci_host*)param;
1216
1217 spin_lock_irqsave(&host->lock, flags);
1218
1219 del_timer(&host->timer);
1220
1221 mrq = host->mrq;
1222
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223 /*
1224 * The controller needs a reset of internal state machines
1225 * upon error conditions.
1226 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001227 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1228 (mrq->cmd->error ||
1229 (mrq->data && (mrq->data->error ||
1230 (mrq->data->stop && mrq->data->stop->error))) ||
1231 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001232
1233 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001234 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001235 unsigned int clock;
1236
1237 /* This is to force an update */
1238 clock = host->clock;
1239 host->clock = 0;
1240 sdhci_set_clock(host, clock);
1241 }
1242
1243 /* Spec says we should do both at the same time, but Ricoh
1244 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245 sdhci_reset(host, SDHCI_RESET_CMD);
1246 sdhci_reset(host, SDHCI_RESET_DATA);
1247 }
1248
1249 host->mrq = NULL;
1250 host->cmd = NULL;
1251 host->data = NULL;
1252
Pierre Ossmanf9134312008-12-21 17:01:48 +01001253#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001255#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256
Pierre Ossman5f25a662006-10-04 02:15:39 -07001257 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258 spin_unlock_irqrestore(&host->lock, flags);
1259
1260 mmc_request_done(host->mmc, mrq);
1261}
1262
1263static void sdhci_timeout_timer(unsigned long data)
1264{
1265 struct sdhci_host *host;
1266 unsigned long flags;
1267
1268 host = (struct sdhci_host*)data;
1269
1270 spin_lock_irqsave(&host->lock, flags);
1271
1272 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001273 printk(KERN_ERR "%s: Timeout waiting for hardware "
1274 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001275 sdhci_dumpregs(host);
1276
1277 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001278 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001279 sdhci_finish_data(host);
1280 } else {
1281 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001282 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001284 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001285
1286 tasklet_schedule(&host->finish_tasklet);
1287 }
1288 }
1289
Pierre Ossman5f25a662006-10-04 02:15:39 -07001290 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001291 spin_unlock_irqrestore(&host->lock, flags);
1292}
1293
1294/*****************************************************************************\
1295 * *
1296 * Interrupt handling *
1297 * *
1298\*****************************************************************************/
1299
1300static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1301{
1302 BUG_ON(intmask == 0);
1303
1304 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001305 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1306 "though no command operation was in progress.\n",
1307 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308 sdhci_dumpregs(host);
1309 return;
1310 }
1311
Pierre Ossman43b58b32007-07-25 23:15:27 +02001312 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001313 host->cmd->error = -ETIMEDOUT;
1314 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1315 SDHCI_INT_INDEX))
1316 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317
Pierre Ossmane8095172008-07-25 01:09:08 +02001318 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001319 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001320 return;
1321 }
1322
1323 /*
1324 * The host can send and interrupt when the busy state has
1325 * ended, allowing us to wait without wasting CPU cycles.
1326 * Unfortunately this is overloaded on the "data complete"
1327 * interrupt, so we need to take some care when handling
1328 * it.
1329 *
1330 * Note: The 1.0 specification is a bit ambiguous about this
1331 * feature so there might be some problems with older
1332 * controllers.
1333 */
1334 if (host->cmd->flags & MMC_RSP_BUSY) {
1335 if (host->cmd->data)
1336 DBG("Cannot wait for busy signal when also "
1337 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001338 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001339 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001340
1341 /* The controller does not support the end-of-busy IRQ,
1342 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001343 }
1344
1345 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001346 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347}
1348
1349static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1350{
1351 BUG_ON(intmask == 0);
1352
1353 if (!host->data) {
1354 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001355 * The "data complete" interrupt is also used to
1356 * indicate that a busy state has ended. See comment
1357 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001358 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001359 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1360 if (intmask & SDHCI_INT_DATA_END) {
1361 sdhci_finish_command(host);
1362 return;
1363 }
1364 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001366 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1367 "though no data operation was in progress.\n",
1368 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001369 sdhci_dumpregs(host);
1370
1371 return;
1372 }
1373
1374 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001375 host->data->error = -ETIMEDOUT;
1376 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1377 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001378 else if (intmask & SDHCI_INT_ADMA_ERROR)
1379 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001380
Pierre Ossman17b04292007-07-22 22:18:46 +02001381 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001382 sdhci_finish_data(host);
1383 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001384 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 sdhci_transfer_pio(host);
1386
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001387 /*
1388 * We currently don't do anything fancy with DMA
1389 * boundaries, but as we can't disable the feature
1390 * we need to at least restart the transfer.
1391 */
1392 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001393 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1394 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001395
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001396 if (intmask & SDHCI_INT_DATA_END) {
1397 if (host->cmd) {
1398 /*
1399 * Data managed to finish before the
1400 * command completed. Make sure we do
1401 * things in the proper order.
1402 */
1403 host->data_early = 1;
1404 } else {
1405 sdhci_finish_data(host);
1406 }
1407 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001408 }
1409}
1410
David Howells7d12e782006-10-05 14:55:46 +01001411static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412{
1413 irqreturn_t result;
1414 struct sdhci_host* host = dev_id;
1415 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001416 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417
1418 spin_lock(&host->lock);
1419
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001420 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001421
Mark Lord62df67a52007-03-06 13:30:13 +01001422 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001423 result = IRQ_NONE;
1424 goto out;
1425 }
1426
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001427 DBG("*** %s got interrupt: 0x%08x\n",
1428 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429
Pierre Ossman3192a282006-06-30 02:22:26 -07001430 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001431 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1432 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001434 }
1435
1436 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001437
1438 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001439 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1440 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001441 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442 }
1443
1444 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001445 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1446 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001447 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448 }
1449
1450 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1451
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001452 intmask &= ~SDHCI_INT_ERROR;
1453
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001455 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001457 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458 }
1459
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001460 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001461
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001462 if (intmask & SDHCI_INT_CARD_INT)
1463 cardint = 1;
1464
1465 intmask &= ~SDHCI_INT_CARD_INT;
1466
Pierre Ossman3192a282006-06-30 02:22:26 -07001467 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001468 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001469 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470 sdhci_dumpregs(host);
1471
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001472 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001473 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474
1475 result = IRQ_HANDLED;
1476
Pierre Ossman5f25a662006-10-04 02:15:39 -07001477 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478out:
1479 spin_unlock(&host->lock);
1480
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001481 /*
1482 * We have to delay this as it calls back into the driver.
1483 */
1484 if (cardint)
1485 mmc_signal_sdio_irq(host->mmc);
1486
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 return result;
1488}
1489
1490/*****************************************************************************\
1491 * *
1492 * Suspend/resume *
1493 * *
1494\*****************************************************************************/
1495
1496#ifdef CONFIG_PM
1497
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001498int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001500 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001502 sdhci_disable_card_detection(host);
1503
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001504 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001505 if (ret)
1506 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001508 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509
1510 return 0;
1511}
1512
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001513EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001515int sdhci_resume_host(struct sdhci_host *host)
1516{
1517 int ret;
1518
1519 if (host->flags & SDHCI_USE_DMA) {
1520 if (host->ops->enable_dma)
1521 host->ops->enable_dma(host);
1522 }
1523
1524 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1525 mmc_hostname(host->mmc), host);
1526 if (ret)
1527 return ret;
1528
1529 sdhci_init(host);
1530 mmiowb();
1531
1532 ret = mmc_resume_host(host->mmc);
1533 if (ret)
1534 return ret;
1535
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001536 sdhci_enable_card_detection(host);
1537
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001538 return 0;
1539}
1540
1541EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001542
1543#endif /* CONFIG_PM */
1544
1545/*****************************************************************************\
1546 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001547 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001548 * *
1549\*****************************************************************************/
1550
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001551struct sdhci_host *sdhci_alloc_host(struct device *dev,
1552 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001553{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001554 struct mmc_host *mmc;
1555 struct sdhci_host *host;
1556
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001557 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001558
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001559 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001560 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001561 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001562
1563 host = mmc_priv(mmc);
1564 host->mmc = mmc;
1565
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001566 return host;
1567}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001568
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001569EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001571int sdhci_add_host(struct sdhci_host *host)
1572{
1573 struct mmc_host *mmc;
1574 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001575 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001576
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001577 WARN_ON(host == NULL);
1578 if (host == NULL)
1579 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001580
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001581 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001583 if (debug_quirks)
1584 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585
Pierre Ossmand96649e2006-06-30 02:22:30 -07001586 sdhci_reset(host, SDHCI_RESET_ALL);
1587
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001588 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001589 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1590 >> SDHCI_SPEC_VER_SHIFT;
1591 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001592 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001593 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001594 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001595 }
1596
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001597 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001598
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001599 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001600 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001601 else if (!(caps & SDHCI_CAN_DO_DMA))
1602 DBG("Controller doesn't have DMA capability\n");
1603 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001604 host->flags |= SDHCI_USE_DMA;
1605
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001606 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001607 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001608 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001609 host->flags &= ~SDHCI_USE_DMA;
1610 }
1611
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001613 if ((host->version >= SDHCI_SPEC_200) &&
1614 (caps & SDHCI_CAN_DO_ADMA2))
1615 host->flags |= SDHCI_USE_ADMA;
1616 }
1617
1618 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1619 (host->flags & SDHCI_USE_ADMA)) {
1620 DBG("Disabling ADMA as it is marked broken\n");
1621 host->flags &= ~SDHCI_USE_ADMA;
1622 }
1623
1624 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001625 if (host->ops->enable_dma) {
1626 if (host->ops->enable_dma(host)) {
1627 printk(KERN_WARNING "%s: No suitable DMA "
1628 "available. Falling back to PIO.\n",
1629 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001630 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001631 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632 }
1633 }
1634
Pierre Ossman2134a922008-06-28 18:28:51 +02001635 if (host->flags & SDHCI_USE_ADMA) {
1636 /*
1637 * We need to allocate descriptors for all sg entries
1638 * (128) and potentially one alignment transfer for
1639 * each of those entries.
1640 */
1641 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1642 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1643 if (!host->adma_desc || !host->align_buffer) {
1644 kfree(host->adma_desc);
1645 kfree(host->align_buffer);
1646 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1647 "buffers. Falling back to standard DMA.\n",
1648 mmc_hostname(mmc));
1649 host->flags &= ~SDHCI_USE_ADMA;
1650 }
1651 }
1652
Pierre Ossman76591502008-07-21 00:32:11 +02001653 /*
1654 * If we use DMA, then it's up to the caller to set the DMA
1655 * mask, but PIO does not need the hw shim so we set a new
1656 * mask here in that case.
1657 */
1658 if (!(host->flags & SDHCI_USE_DMA)) {
1659 host->dma_mask = DMA_BIT_MASK(64);
1660 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1661 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001663 host->max_clk =
1664 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
1665 if (host->max_clk == 0) {
1666 printk(KERN_ERR "%s: Hardware doesn't specify base clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001667 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001668 return -ENODEV;
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001669 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670 host->max_clk *= 1000000;
1671
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001672 host->timeout_clk =
1673 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1674 if (host->timeout_clk == 0) {
1675 printk(KERN_ERR "%s: Hardware doesn't specify timeout clock "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001676 "frequency.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001677 return -ENODEV;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001678 }
1679 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1680 host->timeout_clk *= 1000;
1681
Pierre Ossmand129bce2006-03-24 03:18:17 -08001682 /*
1683 * Set host parameters.
1684 */
1685 mmc->ops = &sdhci_ops;
1686 mmc->f_min = host->max_clk / 256;
1687 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001688 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001689
Pierre Ossman86a6a872009-02-02 21:13:49 +01001690 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001691 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1692
Pierre Ossman146ad662006-06-30 02:22:23 -07001693 mmc->ocr_avail = 0;
1694 if (caps & SDHCI_CAN_VDD_330)
1695 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001696 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001697 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001698 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001699 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001700
1701 if (mmc->ocr_avail == 0) {
1702 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001703 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001704 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001705 }
1706
Pierre Ossmand129bce2006-03-24 03:18:17 -08001707 spin_lock_init(&host->lock);
1708
1709 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001710 * Maximum number of segments. Depends on if the hardware
1711 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001712 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001713 if (host->flags & SDHCI_USE_ADMA)
1714 mmc->max_hw_segs = 128;
1715 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001716 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001717 else /* PIO */
1718 mmc->max_hw_segs = 128;
1719 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001720
1721 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001722 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001723 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001724 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001725 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001726
1727 /*
1728 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001729 * of bytes. When doing hardware scatter/gather, each entry cannot
1730 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001731 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001732 if (host->flags & SDHCI_USE_ADMA)
1733 mmc->max_seg_size = 65536;
1734 else
1735 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001736
1737 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001738 * Maximum block size. This varies from controller to controller and
1739 * is specified in the capabilities register.
1740 */
1741 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1742 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001743 printk(KERN_WARNING "%s: Invalid maximum block size, "
1744 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001745 mmc->max_blk_size = 512;
1746 } else
1747 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001748
1749 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001750 * Maximum block count.
1751 */
1752 mmc->max_blk_count = 65535;
1753
1754 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001755 * Init tasklets.
1756 */
1757 tasklet_init(&host->card_tasklet,
1758 sdhci_tasklet_card, (unsigned long)host);
1759 tasklet_init(&host->finish_tasklet,
1760 sdhci_tasklet_finish, (unsigned long)host);
1761
Al Viroe4cad1b2006-10-10 22:47:07 +01001762 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001764 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001765 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001766 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001767 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768
1769 sdhci_init(host);
1770
1771#ifdef CONFIG_MMC_DEBUG
1772 sdhci_dumpregs(host);
1773#endif
1774
Pierre Ossmanf9134312008-12-21 17:01:48 +01001775#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001776 snprintf(host->led_name, sizeof(host->led_name),
1777 "%s::", mmc_hostname(mmc));
1778 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001779 host->led.brightness = LED_OFF;
1780 host->led.default_trigger = mmc_hostname(mmc);
1781 host->led.brightness_set = sdhci_led_control;
1782
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001783 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001784 if (ret)
1785 goto reset;
1786#endif
1787
Pierre Ossman5f25a662006-10-04 02:15:39 -07001788 mmiowb();
1789
Pierre Ossmand129bce2006-03-24 03:18:17 -08001790 mmc_add_host(mmc);
1791
Pierre Ossman2134a922008-06-28 18:28:51 +02001792 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001793 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Pierre Ossman2134a922008-06-28 18:28:51 +02001794 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001795 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1796
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001797 sdhci_enable_card_detection(host);
1798
Pierre Ossmand129bce2006-03-24 03:18:17 -08001799 return 0;
1800
Pierre Ossmanf9134312008-12-21 17:01:48 +01001801#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001802reset:
1803 sdhci_reset(host, SDHCI_RESET_ALL);
1804 free_irq(host->irq, host);
1805#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001806untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001807 tasklet_kill(&host->card_tasklet);
1808 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001809
1810 return ret;
1811}
1812
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001813EXPORT_SYMBOL_GPL(sdhci_add_host);
1814
Pierre Ossman1e728592008-04-16 19:13:13 +02001815void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816{
Pierre Ossman1e728592008-04-16 19:13:13 +02001817 unsigned long flags;
1818
1819 if (dead) {
1820 spin_lock_irqsave(&host->lock, flags);
1821
1822 host->flags |= SDHCI_DEVICE_DEAD;
1823
1824 if (host->mrq) {
1825 printk(KERN_ERR "%s: Controller removed during "
1826 " transfer!\n", mmc_hostname(host->mmc));
1827
1828 host->mrq->cmd->error = -ENOMEDIUM;
1829 tasklet_schedule(&host->finish_tasklet);
1830 }
1831
1832 spin_unlock_irqrestore(&host->lock, flags);
1833 }
1834
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001835 sdhci_disable_card_detection(host);
1836
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001837 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001838
Pierre Ossmanf9134312008-12-21 17:01:48 +01001839#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001840 led_classdev_unregister(&host->led);
1841#endif
1842
Pierre Ossman1e728592008-04-16 19:13:13 +02001843 if (!dead)
1844 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001845
1846 free_irq(host->irq, host);
1847
1848 del_timer_sync(&host->timer);
1849
1850 tasklet_kill(&host->card_tasklet);
1851 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001852
1853 kfree(host->adma_desc);
1854 kfree(host->align_buffer);
1855
1856 host->adma_desc = NULL;
1857 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001858}
1859
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001860EXPORT_SYMBOL_GPL(sdhci_remove_host);
1861
1862void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001863{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001864 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001865}
1866
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001867EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001868
1869/*****************************************************************************\
1870 * *
1871 * Driver init/exit *
1872 * *
1873\*****************************************************************************/
1874
1875static int __init sdhci_drv_init(void)
1876{
1877 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001878 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001879 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1880
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001881 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001882}
1883
1884static void __exit sdhci_drv_exit(void)
1885{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886}
1887
1888module_init(sdhci_drv_init);
1889module_exit(sdhci_drv_exit);
1890
Pierre Ossmandf673b22006-06-30 02:22:31 -07001891module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001892
Pierre Ossmand129bce2006-03-24 03:18:17 -08001893MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001894MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001895MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001896
Pierre Ossmandf673b22006-06-30 02:22:31 -07001897MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");