blob: 34ab77bd12ae15afa76be8fe7a41c7a39cc929ed [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) {
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100339 if (host->data->flags & MMC_DATA_READ)
340 sdhci_read_block_pio(host);
341 else
342 sdhci_write_block_pio(host);
343
Pierre Ossman76591502008-07-21 00:32:11 +0200344 host->blocks--;
345 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347 }
348
349 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350}
351
Pierre Ossman2134a922008-06-28 18:28:51 +0200352static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
353{
354 local_irq_save(*flags);
355 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
356}
357
358static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
359{
360 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
361 local_irq_restore(*flags);
362}
363
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200364static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200365 struct mmc_data *data)
366{
367 int direction;
368
369 u8 *desc;
370 u8 *align;
371 dma_addr_t addr;
372 dma_addr_t align_addr;
373 int len, offset;
374
375 struct scatterlist *sg;
376 int i;
377 char *buffer;
378 unsigned long flags;
379
380 /*
381 * The spec does not specify endianness of descriptor table.
382 * We currently guess that it is LE.
383 */
384
385 if (data->flags & MMC_DATA_READ)
386 direction = DMA_FROM_DEVICE;
387 else
388 direction = DMA_TO_DEVICE;
389
390 /*
391 * The ADMA descriptor table is mapped further down as we
392 * need to fill it with data first.
393 */
394
395 host->align_addr = dma_map_single(mmc_dev(host->mmc),
396 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700397 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200398 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200399 BUG_ON(host->align_addr & 0x3);
400
401 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
402 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200403 if (host->sg_count == 0)
404 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200405
406 desc = host->adma_desc;
407 align = host->align_buffer;
408
409 align_addr = host->align_addr;
410
411 for_each_sg(data->sg, sg, host->sg_count, i) {
412 addr = sg_dma_address(sg);
413 len = sg_dma_len(sg);
414
415 /*
416 * The SDHCI specification states that ADMA
417 * addresses must be 32-bit aligned. If they
418 * aren't, then we use a bounce buffer for
419 * the (up to three) bytes that screw up the
420 * alignment.
421 */
422 offset = (4 - (addr & 0x3)) & 0x3;
423 if (offset) {
424 if (data->flags & MMC_DATA_WRITE) {
425 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200426 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200427 memcpy(align, buffer, offset);
428 sdhci_kunmap_atomic(buffer, &flags);
429 }
430
431 desc[7] = (align_addr >> 24) & 0xff;
432 desc[6] = (align_addr >> 16) & 0xff;
433 desc[5] = (align_addr >> 8) & 0xff;
434 desc[4] = (align_addr >> 0) & 0xff;
435
436 BUG_ON(offset > 65536);
437
438 desc[3] = (offset >> 8) & 0xff;
439 desc[2] = (offset >> 0) & 0xff;
440
441 desc[1] = 0x00;
442 desc[0] = 0x21; /* tran, valid */
443
444 align += 4;
445 align_addr += 4;
446
447 desc += 8;
448
449 addr += offset;
450 len -= offset;
451 }
452
453 desc[7] = (addr >> 24) & 0xff;
454 desc[6] = (addr >> 16) & 0xff;
455 desc[5] = (addr >> 8) & 0xff;
456 desc[4] = (addr >> 0) & 0xff;
457
458 BUG_ON(len > 65536);
459
460 desc[3] = (len >> 8) & 0xff;
461 desc[2] = (len >> 0) & 0xff;
462
463 desc[1] = 0x00;
464 desc[0] = 0x21; /* tran, valid */
465
466 desc += 8;
467
468 /*
469 * If this triggers then we have a calculation bug
470 * somewhere. :/
471 */
472 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
473 }
474
475 /*
476 * Add a terminating entry.
477 */
478 desc[7] = 0;
479 desc[6] = 0;
480 desc[5] = 0;
481 desc[4] = 0;
482
483 desc[3] = 0;
484 desc[2] = 0;
485
486 desc[1] = 0x00;
487 desc[0] = 0x03; /* nop, end, valid */
488
489 /*
490 * Resync align buffer as we might have changed it.
491 */
492 if (data->flags & MMC_DATA_WRITE) {
493 dma_sync_single_for_device(mmc_dev(host->mmc),
494 host->align_addr, 128 * 4, direction);
495 }
496
497 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
498 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200499 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200500 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200502
503 return 0;
504
505unmap_entries:
506 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
507 data->sg_len, direction);
508unmap_align:
509 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
510 128 * 4, direction);
511fail:
512 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200513}
514
515static void sdhci_adma_table_post(struct sdhci_host *host,
516 struct mmc_data *data)
517{
518 int direction;
519
520 struct scatterlist *sg;
521 int i, size;
522 u8 *align;
523 char *buffer;
524 unsigned long flags;
525
526 if (data->flags & MMC_DATA_READ)
527 direction = DMA_FROM_DEVICE;
528 else
529 direction = DMA_TO_DEVICE;
530
531 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
532 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
533
534 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
535 128 * 4, direction);
536
537 if (data->flags & MMC_DATA_READ) {
538 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
539 data->sg_len, direction);
540
541 align = host->align_buffer;
542
543 for_each_sg(data->sg, sg, host->sg_count, i) {
544 if (sg_dma_address(sg) & 0x3) {
545 size = 4 - (sg_dma_address(sg) & 0x3);
546
547 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200548 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200549 memcpy(buffer, align, size);
550 sdhci_kunmap_atomic(buffer, &flags);
551
552 align += 4;
553 }
554 }
555 }
556
557 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
558 data->sg_len, direction);
559}
560
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200561static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800562{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700563 u8 count;
564 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800565
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200566 /*
567 * If the host controller provides us with an incorrect timeout
568 * value, just skip the check and use 0xE. The hardware may take
569 * longer to time out, but that's much better than having a too-short
570 * timeout value.
571 */
572 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
573 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200574
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700575 /* timeout in us */
576 target_timeout = data->timeout_ns / 1000 +
577 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800578
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700579 /*
580 * Figure out needed cycles.
581 * We do this in steps in order to fit inside a 32 bit int.
582 * The first step is the minimum timeout, which will have a
583 * minimum resolution of 6 bits:
584 * (1) 2^13*1000 > 2^22,
585 * (2) host->timeout_clk < 2^16
586 * =>
587 * (1) / (2) > 2^6
588 */
589 count = 0;
590 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
591 while (current_timeout < target_timeout) {
592 count++;
593 current_timeout <<= 1;
594 if (count >= 0xF)
595 break;
596 }
597
598 if (count >= 0xF) {
599 printk(KERN_WARNING "%s: Too large timeout requested!\n",
600 mmc_hostname(host->mmc));
601 count = 0xE;
602 }
603
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200604 return count;
605}
606
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300607static void sdhci_set_transfer_irqs(struct sdhci_host *host)
608{
609 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
610 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
611
612 if (host->flags & SDHCI_REQ_USE_DMA)
613 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
614 else
615 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
616}
617
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200618static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
619{
620 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200622 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200623
624 WARN_ON(host->data);
625
626 if (data == NULL)
627 return;
628
629 /* Sanity checks */
630 BUG_ON(data->blksz * data->blocks > 524288);
631 BUG_ON(data->blksz > host->mmc->max_blk_size);
632 BUG_ON(data->blocks > 65535);
633
634 host->data = data;
635 host->data_early = 0;
636
637 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300638 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800639
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100640 if (host->flags & SDHCI_USE_DMA)
641 host->flags |= SDHCI_REQ_USE_DMA;
642
Pierre Ossman2134a922008-06-28 18:28:51 +0200643 /*
644 * FIXME: This doesn't account for merging when mapping the
645 * scatterlist.
646 */
647 if (host->flags & SDHCI_REQ_USE_DMA) {
648 int broken, i;
649 struct scatterlist *sg;
650
651 broken = 0;
652 if (host->flags & SDHCI_USE_ADMA) {
653 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
654 broken = 1;
655 } else {
656 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
657 broken = 1;
658 }
659
660 if (unlikely(broken)) {
661 for_each_sg(data->sg, sg, data->sg_len, i) {
662 if (sg->length & 0x3) {
663 DBG("Reverting to PIO because of "
664 "transfer size (%d)\n",
665 sg->length);
666 host->flags &= ~SDHCI_REQ_USE_DMA;
667 break;
668 }
669 }
670 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100671 }
672
673 /*
674 * The assumption here being that alignment is the same after
675 * translation to device address space.
676 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200677 if (host->flags & SDHCI_REQ_USE_DMA) {
678 int broken, i;
679 struct scatterlist *sg;
680
681 broken = 0;
682 if (host->flags & SDHCI_USE_ADMA) {
683 /*
684 * As we use 3 byte chunks to work around
685 * alignment problems, we need to check this
686 * quirk.
687 */
688 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
689 broken = 1;
690 } else {
691 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
692 broken = 1;
693 }
694
695 if (unlikely(broken)) {
696 for_each_sg(data->sg, sg, data->sg_len, i) {
697 if (sg->offset & 0x3) {
698 DBG("Reverting to PIO because of "
699 "bad alignment\n");
700 host->flags &= ~SDHCI_REQ_USE_DMA;
701 break;
702 }
703 }
704 }
705 }
706
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200707 if (host->flags & SDHCI_REQ_USE_DMA) {
708 if (host->flags & SDHCI_USE_ADMA) {
709 ret = sdhci_adma_table_pre(host, data);
710 if (ret) {
711 /*
712 * This only happens when someone fed
713 * us an invalid request.
714 */
715 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200716 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200717 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300718 sdhci_writel(host, host->adma_addr,
719 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200720 }
721 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300722 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200723
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300724 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200725 data->sg, data->sg_len,
726 (data->flags & MMC_DATA_READ) ?
727 DMA_FROM_DEVICE :
728 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300729 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200730 /*
731 * This only happens when someone fed
732 * us an invalid request.
733 */
734 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200735 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200736 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200737 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300738 sdhci_writel(host, sg_dma_address(data->sg),
739 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200740 }
741 }
742 }
743
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 /*
745 * Always adjust the DMA selection as some controllers
746 * (e.g. JMicron) can't do PIO properly when the selection
747 * is ADMA.
748 */
749 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300750 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200751 ctrl &= ~SDHCI_CTRL_DMA_MASK;
752 if ((host->flags & SDHCI_REQ_USE_DMA) &&
753 (host->flags & SDHCI_USE_ADMA))
754 ctrl |= SDHCI_CTRL_ADMA32;
755 else
756 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300757 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100758 }
759
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200760 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Pierre Ossman76591502008-07-21 00:32:11 +0200761 sg_miter_start(&host->sg_miter,
762 data->sg, data->sg_len, SG_MITER_ATOMIC);
763 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800764 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700765
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300766 sdhci_set_transfer_irqs(host);
767
Pierre Ossmanbab76962006-07-02 16:51:35 +0100768 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300769 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
770 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700771}
772
773static void sdhci_set_transfer_mode(struct sdhci_host *host,
774 struct mmc_data *data)
775{
776 u16 mode;
777
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700778 if (data == NULL)
779 return;
780
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200781 WARN_ON(!host->data);
782
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700783 mode = SDHCI_TRNS_BLK_CNT_EN;
784 if (data->blocks > 1)
785 mode |= SDHCI_TRNS_MULTI;
786 if (data->flags & MMC_DATA_READ)
787 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100788 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700789 mode |= SDHCI_TRNS_DMA;
790
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300791 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800792}
793
794static void sdhci_finish_data(struct sdhci_host *host)
795{
796 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800797
798 BUG_ON(!host->data);
799
800 data = host->data;
801 host->data = NULL;
802
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100803 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200804 if (host->flags & SDHCI_USE_ADMA)
805 sdhci_adma_table_post(host, data);
806 else {
807 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
808 data->sg_len, (data->flags & MMC_DATA_READ) ?
809 DMA_FROM_DEVICE : DMA_TO_DEVICE);
810 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800811 }
812
813 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200814 * The specification states that the block count register must
815 * be updated, but it does not specify at what point in the
816 * data flow. That makes the register entirely useless to read
817 * back so we have to assume that nothing made it to the card
818 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800819 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200820 if (data->error)
821 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800822 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200823 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800824
Pierre Ossmand129bce2006-03-24 03:18:17 -0800825 if (data->stop) {
826 /*
827 * The controller needs a reset of internal state machines
828 * upon error conditions.
829 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200830 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800831 sdhci_reset(host, SDHCI_RESET_CMD);
832 sdhci_reset(host, SDHCI_RESET_DATA);
833 }
834
835 sdhci_send_command(host, data->stop);
836 } else
837 tasklet_schedule(&host->finish_tasklet);
838}
839
840static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
841{
842 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700843 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700844 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800845
846 WARN_ON(host->cmd);
847
Pierre Ossmand129bce2006-03-24 03:18:17 -0800848 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700849 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700850
851 mask = SDHCI_CMD_INHIBIT;
852 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
853 mask |= SDHCI_DATA_INHIBIT;
854
855 /* We shouldn't wait for data inihibit for stop commands, even
856 though they might use busy signaling */
857 if (host->mrq->data && (cmd == host->mrq->data->stop))
858 mask &= ~SDHCI_DATA_INHIBIT;
859
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700861 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800862 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100863 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800864 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200865 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866 tasklet_schedule(&host->finish_tasklet);
867 return;
868 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700869 timeout--;
870 mdelay(1);
871 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872
873 mod_timer(&host->timer, jiffies + 10 * HZ);
874
875 host->cmd = cmd;
876
877 sdhci_prepare_data(host, cmd->data);
878
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300879 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881 sdhci_set_transfer_mode(host, cmd->data);
882
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100884 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800885 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200886 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887 tasklet_schedule(&host->finish_tasklet);
888 return;
889 }
890
891 if (!(cmd->flags & MMC_RSP_PRESENT))
892 flags = SDHCI_CMD_RESP_NONE;
893 else if (cmd->flags & MMC_RSP_136)
894 flags = SDHCI_CMD_RESP_LONG;
895 else if (cmd->flags & MMC_RSP_BUSY)
896 flags = SDHCI_CMD_RESP_SHORT_BUSY;
897 else
898 flags = SDHCI_CMD_RESP_SHORT;
899
900 if (cmd->flags & MMC_RSP_CRC)
901 flags |= SDHCI_CMD_CRC;
902 if (cmd->flags & MMC_RSP_OPCODE)
903 flags |= SDHCI_CMD_INDEX;
904 if (cmd->data)
905 flags |= SDHCI_CMD_DATA;
906
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300907 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908}
909
910static void sdhci_finish_command(struct sdhci_host *host)
911{
912 int i;
913
914 BUG_ON(host->cmd == NULL);
915
916 if (host->cmd->flags & MMC_RSP_PRESENT) {
917 if (host->cmd->flags & MMC_RSP_136) {
918 /* CRC is stripped so we need to do some shifting. */
919 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300920 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800921 SDHCI_RESPONSE + (3-i)*4) << 8;
922 if (i != 3)
923 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300924 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800925 SDHCI_RESPONSE + (3-i)*4-1);
926 }
927 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300928 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 }
930 }
931
Pierre Ossman17b04292007-07-22 22:18:46 +0200932 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200934 if (host->data && host->data_early)
935 sdhci_finish_data(host);
936
937 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800938 tasklet_schedule(&host->finish_tasklet);
939
940 host->cmd = NULL;
941}
942
943static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
944{
945 int div;
946 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700947 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948
949 if (clock == host->clock)
950 return;
951
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300952 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953
954 if (clock == 0)
955 goto out;
956
957 for (div = 1;div < 256;div *= 2) {
958 if ((host->max_clk / div) <= clock)
959 break;
960 }
961 div >>= 1;
962
963 clk = div << SDHCI_DIVIDER_SHIFT;
964 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300965 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800966
967 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700968 timeout = 10;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300969 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700970 & SDHCI_CLOCK_INT_STABLE)) {
971 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100972 printk(KERN_ERR "%s: Internal clock never "
973 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 sdhci_dumpregs(host);
975 return;
976 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700977 timeout--;
978 mdelay(1);
979 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980
981 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300982 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
984out:
985 host->clock = clock;
986}
987
Pierre Ossman146ad662006-06-30 02:22:23 -0700988static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
989{
990 u8 pwr;
991
992 if (host->power == power)
993 return;
994
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100995 if (power == (unsigned short)-1) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300996 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -0700997 goto out;
Darren Salt9e9dc5f2007-01-27 15:32:31 +0100998 }
999
1000 /*
1001 * Spec says that we should clear the power reg before setting
1002 * a new value. Some controllers don't seem to like this though.
1003 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001004 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001005 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001006
1007 pwr = SDHCI_POWER_ON;
1008
Philip Langdale4be34c92007-03-11 17:15:15 -07001009 switch (1 << power) {
Philip Langdale55556da2007-03-16 19:39:00 -07001010 case MMC_VDD_165_195:
Pierre Ossman146ad662006-06-30 02:22:23 -07001011 pwr |= SDHCI_POWER_180;
1012 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001013 case MMC_VDD_29_30:
1014 case MMC_VDD_30_31:
Pierre Ossman146ad662006-06-30 02:22:23 -07001015 pwr |= SDHCI_POWER_300;
1016 break;
Philip Langdale4be34c92007-03-11 17:15:15 -07001017 case MMC_VDD_32_33:
1018 case MMC_VDD_33_34:
Pierre Ossman146ad662006-06-30 02:22:23 -07001019 pwr |= SDHCI_POWER_330;
1020 break;
1021 default:
1022 BUG();
1023 }
1024
Andres Salomone08c1692008-07-04 10:00:03 -07001025 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001026 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001027 * and set turn on power at the same time, so set the voltage first.
1028 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001029 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001030 sdhci_writeb(host, pwr & ~SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
Andres Salomone08c1692008-07-04 10:00:03 -07001031
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001032 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001033
1034out:
1035 host->power = power;
1036}
1037
Pierre Ossmand129bce2006-03-24 03:18:17 -08001038/*****************************************************************************\
1039 * *
1040 * MMC callbacks *
1041 * *
1042\*****************************************************************************/
1043
1044static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1045{
1046 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001047 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048 unsigned long flags;
1049
1050 host = mmc_priv(mmc);
1051
1052 spin_lock_irqsave(&host->lock, flags);
1053
1054 WARN_ON(host->mrq != NULL);
1055
Pierre Ossmanf9134312008-12-21 17:01:48 +01001056#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001058#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059
1060 host->mrq = mrq;
1061
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001062 /* If polling, assume that the card is always present. */
1063 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1064 present = true;
1065 else
1066 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1067 SDHCI_CARD_PRESENT;
1068
1069 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001070 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071 tasklet_schedule(&host->finish_tasklet);
1072 } else
1073 sdhci_send_command(host, mrq->cmd);
1074
Pierre Ossman5f25a662006-10-04 02:15:39 -07001075 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 spin_unlock_irqrestore(&host->lock, flags);
1077}
1078
1079static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1080{
1081 struct sdhci_host *host;
1082 unsigned long flags;
1083 u8 ctrl;
1084
1085 host = mmc_priv(mmc);
1086
1087 spin_lock_irqsave(&host->lock, flags);
1088
Pierre Ossman1e728592008-04-16 19:13:13 +02001089 if (host->flags & SDHCI_DEVICE_DEAD)
1090 goto out;
1091
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092 /*
1093 * Reset the chip on each power off.
1094 * Should clear out any weird states.
1095 */
1096 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001097 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001098 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099 }
1100
1101 sdhci_set_clock(host, ios->clock);
1102
1103 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001104 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001105 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001106 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001108 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001109
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110 if (ios->bus_width == MMC_BUS_WIDTH_4)
1111 ctrl |= SDHCI_CTRL_4BITBUS;
1112 else
1113 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001114
1115 if (ios->timing == MMC_TIMING_SD_HS)
1116 ctrl |= SDHCI_CTRL_HISPD;
1117 else
1118 ctrl &= ~SDHCI_CTRL_HISPD;
1119
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001120 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121
Leandro Dorileob8352262007-07-25 23:47:04 +02001122 /*
1123 * Some (ENE) controllers go apeshit on some ios operation,
1124 * signalling timeout and CRC errors even on CMD0. Resetting
1125 * it on each ios seems to solve the problem.
1126 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001127 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001128 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1129
Pierre Ossman1e728592008-04-16 19:13:13 +02001130out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001131 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132 spin_unlock_irqrestore(&host->lock, flags);
1133}
1134
1135static int sdhci_get_ro(struct mmc_host *mmc)
1136{
1137 struct sdhci_host *host;
1138 unsigned long flags;
1139 int present;
1140
1141 host = mmc_priv(mmc);
1142
1143 spin_lock_irqsave(&host->lock, flags);
1144
Pierre Ossman1e728592008-04-16 19:13:13 +02001145 if (host->flags & SDHCI_DEVICE_DEAD)
1146 present = 0;
1147 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001148 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149
1150 spin_unlock_irqrestore(&host->lock, flags);
1151
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001152 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1153 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 return !(present & SDHCI_WRITE_PROTECT);
1155}
1156
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001157static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1158{
1159 struct sdhci_host *host;
1160 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001161
1162 host = mmc_priv(mmc);
1163
1164 spin_lock_irqsave(&host->lock, flags);
1165
Pierre Ossman1e728592008-04-16 19:13:13 +02001166 if (host->flags & SDHCI_DEVICE_DEAD)
1167 goto out;
1168
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001169 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001170 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1171 else
1172 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001173out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001174 mmiowb();
1175
1176 spin_unlock_irqrestore(&host->lock, flags);
1177}
1178
David Brownellab7aefd2006-11-12 17:55:30 -08001179static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180 .request = sdhci_request,
1181 .set_ios = sdhci_set_ios,
1182 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001183 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184};
1185
1186/*****************************************************************************\
1187 * *
1188 * Tasklets *
1189 * *
1190\*****************************************************************************/
1191
1192static void sdhci_tasklet_card(unsigned long param)
1193{
1194 struct sdhci_host *host;
1195 unsigned long flags;
1196
1197 host = (struct sdhci_host*)param;
1198
1199 spin_lock_irqsave(&host->lock, flags);
1200
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001201 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001202 if (host->mrq) {
1203 printk(KERN_ERR "%s: Card removed during transfer!\n",
1204 mmc_hostname(host->mmc));
1205 printk(KERN_ERR "%s: Resetting controller.\n",
1206 mmc_hostname(host->mmc));
1207
1208 sdhci_reset(host, SDHCI_RESET_CMD);
1209 sdhci_reset(host, SDHCI_RESET_DATA);
1210
Pierre Ossman17b04292007-07-22 22:18:46 +02001211 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212 tasklet_schedule(&host->finish_tasklet);
1213 }
1214 }
1215
1216 spin_unlock_irqrestore(&host->lock, flags);
1217
Pierre Ossman04cf5852008-08-18 22:18:14 +02001218 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219}
1220
1221static void sdhci_tasklet_finish(unsigned long param)
1222{
1223 struct sdhci_host *host;
1224 unsigned long flags;
1225 struct mmc_request *mrq;
1226
1227 host = (struct sdhci_host*)param;
1228
1229 spin_lock_irqsave(&host->lock, flags);
1230
1231 del_timer(&host->timer);
1232
1233 mrq = host->mrq;
1234
Pierre Ossmand129bce2006-03-24 03:18:17 -08001235 /*
1236 * The controller needs a reset of internal state machines
1237 * upon error conditions.
1238 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001239 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1240 (mrq->cmd->error ||
1241 (mrq->data && (mrq->data->error ||
1242 (mrq->data->stop && mrq->data->stop->error))) ||
1243 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001244
1245 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001246 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001247 unsigned int clock;
1248
1249 /* This is to force an update */
1250 clock = host->clock;
1251 host->clock = 0;
1252 sdhci_set_clock(host, clock);
1253 }
1254
1255 /* Spec says we should do both at the same time, but Ricoh
1256 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001257 sdhci_reset(host, SDHCI_RESET_CMD);
1258 sdhci_reset(host, SDHCI_RESET_DATA);
1259 }
1260
1261 host->mrq = NULL;
1262 host->cmd = NULL;
1263 host->data = NULL;
1264
Pierre Ossmanf9134312008-12-21 17:01:48 +01001265#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001267#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268
Pierre Ossman5f25a662006-10-04 02:15:39 -07001269 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270 spin_unlock_irqrestore(&host->lock, flags);
1271
1272 mmc_request_done(host->mmc, mrq);
1273}
1274
1275static void sdhci_timeout_timer(unsigned long data)
1276{
1277 struct sdhci_host *host;
1278 unsigned long flags;
1279
1280 host = (struct sdhci_host*)data;
1281
1282 spin_lock_irqsave(&host->lock, flags);
1283
1284 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001285 printk(KERN_ERR "%s: Timeout waiting for hardware "
1286 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287 sdhci_dumpregs(host);
1288
1289 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001290 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001291 sdhci_finish_data(host);
1292 } else {
1293 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001294 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001295 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001296 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297
1298 tasklet_schedule(&host->finish_tasklet);
1299 }
1300 }
1301
Pierre Ossman5f25a662006-10-04 02:15:39 -07001302 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303 spin_unlock_irqrestore(&host->lock, flags);
1304}
1305
1306/*****************************************************************************\
1307 * *
1308 * Interrupt handling *
1309 * *
1310\*****************************************************************************/
1311
1312static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1313{
1314 BUG_ON(intmask == 0);
1315
1316 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001317 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1318 "though no command operation was in progress.\n",
1319 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 sdhci_dumpregs(host);
1321 return;
1322 }
1323
Pierre Ossman43b58b32007-07-25 23:15:27 +02001324 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001325 host->cmd->error = -ETIMEDOUT;
1326 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1327 SDHCI_INT_INDEX))
1328 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329
Pierre Ossmane8095172008-07-25 01:09:08 +02001330 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001331 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001332 return;
1333 }
1334
1335 /*
1336 * The host can send and interrupt when the busy state has
1337 * ended, allowing us to wait without wasting CPU cycles.
1338 * Unfortunately this is overloaded on the "data complete"
1339 * interrupt, so we need to take some care when handling
1340 * it.
1341 *
1342 * Note: The 1.0 specification is a bit ambiguous about this
1343 * feature so there might be some problems with older
1344 * controllers.
1345 */
1346 if (host->cmd->flags & MMC_RSP_BUSY) {
1347 if (host->cmd->data)
1348 DBG("Cannot wait for busy signal when also "
1349 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001350 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001351 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001352
1353 /* The controller does not support the end-of-busy IRQ,
1354 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001355 }
1356
1357 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001358 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359}
1360
1361static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1362{
1363 BUG_ON(intmask == 0);
1364
1365 if (!host->data) {
1366 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001367 * The "data complete" interrupt is also used to
1368 * indicate that a busy state has ended. See comment
1369 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001371 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1372 if (intmask & SDHCI_INT_DATA_END) {
1373 sdhci_finish_command(host);
1374 return;
1375 }
1376 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001377
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001378 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1379 "though no data operation was in progress.\n",
1380 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381 sdhci_dumpregs(host);
1382
1383 return;
1384 }
1385
1386 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001387 host->data->error = -ETIMEDOUT;
1388 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1389 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001390 else if (intmask & SDHCI_INT_ADMA_ERROR)
1391 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Pierre Ossman17b04292007-07-22 22:18:46 +02001393 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001394 sdhci_finish_data(host);
1395 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001396 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397 sdhci_transfer_pio(host);
1398
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001399 /*
1400 * We currently don't do anything fancy with DMA
1401 * boundaries, but as we can't disable the feature
1402 * we need to at least restart the transfer.
1403 */
1404 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001405 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1406 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001407
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001408 if (intmask & SDHCI_INT_DATA_END) {
1409 if (host->cmd) {
1410 /*
1411 * Data managed to finish before the
1412 * command completed. Make sure we do
1413 * things in the proper order.
1414 */
1415 host->data_early = 1;
1416 } else {
1417 sdhci_finish_data(host);
1418 }
1419 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001420 }
1421}
1422
David Howells7d12e782006-10-05 14:55:46 +01001423static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001424{
1425 irqreturn_t result;
1426 struct sdhci_host* host = dev_id;
1427 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001428 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429
1430 spin_lock(&host->lock);
1431
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001432 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433
Mark Lord62df67a52007-03-06 13:30:13 +01001434 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435 result = IRQ_NONE;
1436 goto out;
1437 }
1438
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001439 DBG("*** %s got interrupt: 0x%08x\n",
1440 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441
Pierre Ossman3192a282006-06-30 02:22:26 -07001442 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001443 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1444 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001445 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001446 }
1447
1448 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001449
1450 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001451 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1452 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001453 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 }
1455
1456 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001457 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1458 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001459 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460 }
1461
1462 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1463
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001464 intmask &= ~SDHCI_INT_ERROR;
1465
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001467 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001468 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001469 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470 }
1471
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001472 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001473
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001474 if (intmask & SDHCI_INT_CARD_INT)
1475 cardint = 1;
1476
1477 intmask &= ~SDHCI_INT_CARD_INT;
1478
Pierre Ossman3192a282006-06-30 02:22:26 -07001479 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001480 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001481 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482 sdhci_dumpregs(host);
1483
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001484 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001485 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486
1487 result = IRQ_HANDLED;
1488
Pierre Ossman5f25a662006-10-04 02:15:39 -07001489 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490out:
1491 spin_unlock(&host->lock);
1492
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001493 /*
1494 * We have to delay this as it calls back into the driver.
1495 */
1496 if (cardint)
1497 mmc_signal_sdio_irq(host->mmc);
1498
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499 return result;
1500}
1501
1502/*****************************************************************************\
1503 * *
1504 * Suspend/resume *
1505 * *
1506\*****************************************************************************/
1507
1508#ifdef CONFIG_PM
1509
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001510int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001512 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001514 sdhci_disable_card_detection(host);
1515
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001516 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001517 if (ret)
1518 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001520 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001521
1522 return 0;
1523}
1524
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001525EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001527int sdhci_resume_host(struct sdhci_host *host)
1528{
1529 int ret;
1530
1531 if (host->flags & SDHCI_USE_DMA) {
1532 if (host->ops->enable_dma)
1533 host->ops->enable_dma(host);
1534 }
1535
1536 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1537 mmc_hostname(host->mmc), host);
1538 if (ret)
1539 return ret;
1540
1541 sdhci_init(host);
1542 mmiowb();
1543
1544 ret = mmc_resume_host(host->mmc);
1545 if (ret)
1546 return ret;
1547
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001548 sdhci_enable_card_detection(host);
1549
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001550 return 0;
1551}
1552
1553EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001554
1555#endif /* CONFIG_PM */
1556
1557/*****************************************************************************\
1558 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001559 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001560 * *
1561\*****************************************************************************/
1562
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001563struct sdhci_host *sdhci_alloc_host(struct device *dev,
1564 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001565{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566 struct mmc_host *mmc;
1567 struct sdhci_host *host;
1568
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001569 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001571 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001572 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001573 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001574
1575 host = mmc_priv(mmc);
1576 host->mmc = mmc;
1577
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001578 return host;
1579}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001580
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001581EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001583int sdhci_add_host(struct sdhci_host *host)
1584{
1585 struct mmc_host *mmc;
1586 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001587 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001588
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001589 WARN_ON(host == NULL);
1590 if (host == NULL)
1591 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001592
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001593 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001594
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001595 if (debug_quirks)
1596 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001597
Pierre Ossmand96649e2006-06-30 02:22:30 -07001598 sdhci_reset(host, SDHCI_RESET_ALL);
1599
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001600 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001601 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1602 >> SDHCI_SPEC_VER_SHIFT;
1603 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001604 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001605 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001606 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001607 }
1608
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001609 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001610
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001611 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001612 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001613 else if (!(caps & SDHCI_CAN_DO_DMA))
1614 DBG("Controller doesn't have DMA capability\n");
1615 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616 host->flags |= SDHCI_USE_DMA;
1617
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001618 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001619 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001620 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001621 host->flags &= ~SDHCI_USE_DMA;
1622 }
1623
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001625 if ((host->version >= SDHCI_SPEC_200) &&
1626 (caps & SDHCI_CAN_DO_ADMA2))
1627 host->flags |= SDHCI_USE_ADMA;
1628 }
1629
1630 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1631 (host->flags & SDHCI_USE_ADMA)) {
1632 DBG("Disabling ADMA as it is marked broken\n");
1633 host->flags &= ~SDHCI_USE_ADMA;
1634 }
1635
1636 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001637 if (host->ops->enable_dma) {
1638 if (host->ops->enable_dma(host)) {
1639 printk(KERN_WARNING "%s: No suitable DMA "
1640 "available. Falling back to PIO.\n",
1641 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001642 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001643 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644 }
1645 }
1646
Pierre Ossman2134a922008-06-28 18:28:51 +02001647 if (host->flags & SDHCI_USE_ADMA) {
1648 /*
1649 * We need to allocate descriptors for all sg entries
1650 * (128) and potentially one alignment transfer for
1651 * each of those entries.
1652 */
1653 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1654 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1655 if (!host->adma_desc || !host->align_buffer) {
1656 kfree(host->adma_desc);
1657 kfree(host->align_buffer);
1658 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1659 "buffers. Falling back to standard DMA.\n",
1660 mmc_hostname(mmc));
1661 host->flags &= ~SDHCI_USE_ADMA;
1662 }
1663 }
1664
Pierre Ossman76591502008-07-21 00:32:11 +02001665 /*
1666 * If we use DMA, then it's up to the caller to set the DMA
1667 * mask, but PIO does not need the hw shim so we set a new
1668 * mask here in that case.
1669 */
1670 if (!(host->flags & SDHCI_USE_DMA)) {
1671 host->dma_mask = DMA_BIT_MASK(64);
1672 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1673 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001674
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001675 host->max_clk =
1676 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001677 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001678 if (host->max_clk == 0) {
1679 if (!host->ops->get_max_clock) {
1680 printk(KERN_ERR
1681 "%s: Hardware doesn't specify base clock "
1682 "frequency.\n", mmc_hostname(mmc));
1683 return -ENODEV;
1684 }
1685 host->max_clk = host->ops->get_max_clock(host);
1686 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001687
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001688 host->timeout_clk =
1689 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1690 if (host->timeout_clk == 0) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001691 if (!host->ops->get_timeout_clock) {
1692 printk(KERN_ERR
1693 "%s: Hardware doesn't specify timeout clock "
1694 "frequency.\n", mmc_hostname(mmc));
1695 return -ENODEV;
1696 }
1697 host->timeout_clk = host->ops->get_timeout_clock(host);
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001698 }
1699 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1700 host->timeout_clk *= 1000;
1701
Pierre Ossmand129bce2006-03-24 03:18:17 -08001702 /*
1703 * Set host parameters.
1704 */
1705 mmc->ops = &sdhci_ops;
1706 mmc->f_min = host->max_clk / 256;
1707 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001708 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001709
Pierre Ossman86a6a872009-02-02 21:13:49 +01001710 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001711 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1712
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001713 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1714 mmc->caps |= MMC_CAP_NEEDS_POLL;
1715
Pierre Ossman146ad662006-06-30 02:22:23 -07001716 mmc->ocr_avail = 0;
1717 if (caps & SDHCI_CAN_VDD_330)
1718 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001719 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001720 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001721 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001722 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001723
1724 if (mmc->ocr_avail == 0) {
1725 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001726 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001727 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001728 }
1729
Pierre Ossmand129bce2006-03-24 03:18:17 -08001730 spin_lock_init(&host->lock);
1731
1732 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001733 * Maximum number of segments. Depends on if the hardware
1734 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001735 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001736 if (host->flags & SDHCI_USE_ADMA)
1737 mmc->max_hw_segs = 128;
1738 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001739 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001740 else /* PIO */
1741 mmc->max_hw_segs = 128;
1742 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743
1744 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001745 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001746 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001747 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001748 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001749
1750 /*
1751 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001752 * of bytes. When doing hardware scatter/gather, each entry cannot
1753 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001755 if (host->flags & SDHCI_USE_ADMA)
1756 mmc->max_seg_size = 65536;
1757 else
1758 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001759
1760 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001761 * Maximum block size. This varies from controller to controller and
1762 * is specified in the capabilities register.
1763 */
1764 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >> SDHCI_MAX_BLOCK_SHIFT;
1765 if (mmc->max_blk_size >= 3) {
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001766 printk(KERN_WARNING "%s: Invalid maximum block size, "
1767 "assuming 512 bytes\n", mmc_hostname(mmc));
David Vrabel03f85902007-08-10 13:25:03 +01001768 mmc->max_blk_size = 512;
1769 } else
1770 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001771
1772 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001773 * Maximum block count.
1774 */
1775 mmc->max_blk_count = 65535;
1776
1777 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001778 * Init tasklets.
1779 */
1780 tasklet_init(&host->card_tasklet,
1781 sdhci_tasklet_card, (unsigned long)host);
1782 tasklet_init(&host->finish_tasklet,
1783 sdhci_tasklet_finish, (unsigned long)host);
1784
Al Viroe4cad1b2006-10-10 22:47:07 +01001785 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001786
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001787 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001788 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001789 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001790 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001791
1792 sdhci_init(host);
1793
1794#ifdef CONFIG_MMC_DEBUG
1795 sdhci_dumpregs(host);
1796#endif
1797
Pierre Ossmanf9134312008-12-21 17:01:48 +01001798#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001799 snprintf(host->led_name, sizeof(host->led_name),
1800 "%s::", mmc_hostname(mmc));
1801 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001802 host->led.brightness = LED_OFF;
1803 host->led.default_trigger = mmc_hostname(mmc);
1804 host->led.brightness_set = sdhci_led_control;
1805
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001806 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001807 if (ret)
1808 goto reset;
1809#endif
1810
Pierre Ossman5f25a662006-10-04 02:15:39 -07001811 mmiowb();
1812
Pierre Ossmand129bce2006-03-24 03:18:17 -08001813 mmc_add_host(mmc);
1814
Pierre Ossman2134a922008-06-28 18:28:51 +02001815 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001816 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Pierre Ossman2134a922008-06-28 18:28:51 +02001817 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001818 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1819
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001820 sdhci_enable_card_detection(host);
1821
Pierre Ossmand129bce2006-03-24 03:18:17 -08001822 return 0;
1823
Pierre Ossmanf9134312008-12-21 17:01:48 +01001824#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001825reset:
1826 sdhci_reset(host, SDHCI_RESET_ALL);
1827 free_irq(host->irq, host);
1828#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001829untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001830 tasklet_kill(&host->card_tasklet);
1831 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001832
1833 return ret;
1834}
1835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001836EXPORT_SYMBOL_GPL(sdhci_add_host);
1837
Pierre Ossman1e728592008-04-16 19:13:13 +02001838void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001839{
Pierre Ossman1e728592008-04-16 19:13:13 +02001840 unsigned long flags;
1841
1842 if (dead) {
1843 spin_lock_irqsave(&host->lock, flags);
1844
1845 host->flags |= SDHCI_DEVICE_DEAD;
1846
1847 if (host->mrq) {
1848 printk(KERN_ERR "%s: Controller removed during "
1849 " transfer!\n", mmc_hostname(host->mmc));
1850
1851 host->mrq->cmd->error = -ENOMEDIUM;
1852 tasklet_schedule(&host->finish_tasklet);
1853 }
1854
1855 spin_unlock_irqrestore(&host->lock, flags);
1856 }
1857
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001858 sdhci_disable_card_detection(host);
1859
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001860 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001861
Pierre Ossmanf9134312008-12-21 17:01:48 +01001862#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001863 led_classdev_unregister(&host->led);
1864#endif
1865
Pierre Ossman1e728592008-04-16 19:13:13 +02001866 if (!dead)
1867 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001868
1869 free_irq(host->irq, host);
1870
1871 del_timer_sync(&host->timer);
1872
1873 tasklet_kill(&host->card_tasklet);
1874 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001875
1876 kfree(host->adma_desc);
1877 kfree(host->align_buffer);
1878
1879 host->adma_desc = NULL;
1880 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001881}
1882
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001883EXPORT_SYMBOL_GPL(sdhci_remove_host);
1884
1885void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001887 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001888}
1889
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001890EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001891
1892/*****************************************************************************\
1893 * *
1894 * Driver init/exit *
1895 * *
1896\*****************************************************************************/
1897
1898static int __init sdhci_drv_init(void)
1899{
1900 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001901 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001902 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1903
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001904 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001905}
1906
1907static void __exit sdhci_drv_exit(void)
1908{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001909}
1910
1911module_init(sdhci_drv_init);
1912module_exit(sdhci_drv_exit);
1913
Pierre Ossmandf673b22006-06-30 02:22:31 -07001914module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001915
Pierre Ossmand129bce2006-03-24 03:18:17 -08001916MODULE_AUTHOR("Pierre Ossman <drzeus@drzeus.cx>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001917MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001918MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001919
Pierre Ossmandf673b22006-06-30 02:22:31 -07001920MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");