blob: fc96f8cb9c0b154db3464a10acdd834d5ec58fa6 [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
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010081 if (host->flags & SDHCI_USE_ADMA)
82 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
83 readl(host->ioaddr + SDHCI_ADMA_ERROR),
84 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
85
Pierre Ossmand129bce2006-03-24 03:18:17 -080086 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
87}
88
89/*****************************************************************************\
90 * *
91 * Low level functions *
92 * *
93\*****************************************************************************/
94
Anton Vorontsov7260cf52009-03-17 00:13:48 +030095static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
96{
97 u32 ier;
98
99 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
100 ier &= ~clear;
101 ier |= set;
102 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
103 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
104}
105
106static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
107{
108 sdhci_clear_set_irqs(host, 0, irqs);
109}
110
111static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
112{
113 sdhci_clear_set_irqs(host, irqs, 0);
114}
115
116static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
117{
118 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
119
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300120 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
121 return;
122
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300123 if (enable)
124 sdhci_unmask_irqs(host, irqs);
125 else
126 sdhci_mask_irqs(host, irqs);
127}
128
129static void sdhci_enable_card_detection(struct sdhci_host *host)
130{
131 sdhci_set_card_detection(host, true);
132}
133
134static void sdhci_disable_card_detection(struct sdhci_host *host)
135{
136 sdhci_set_card_detection(host, false);
137}
138
Pierre Ossmand129bce2006-03-24 03:18:17 -0800139static void sdhci_reset(struct sdhci_host *host, u8 mask)
140{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700141 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300142 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700143
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100144 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300145 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700146 SDHCI_CARD_PRESENT))
147 return;
148 }
149
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300150 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
151 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
152
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300153 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800154
Pierre Ossmane16514d82006-06-30 02:22:24 -0700155 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800156 host->clock = 0;
157
Pierre Ossmane16514d82006-06-30 02:22:24 -0700158 /* Wait max 100 ms */
159 timeout = 100;
160
161 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300162 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700163 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100164 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700165 mmc_hostname(host->mmc), (int)mask);
166 sdhci_dumpregs(host);
167 return;
168 }
169 timeout--;
170 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300172
173 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
174 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175}
176
177static void sdhci_init(struct sdhci_host *host)
178{
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179 sdhci_reset(host, SDHCI_RESET_ALL);
180
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300181 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
182 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700183 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
184 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300185 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300186}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300188static void sdhci_reinit(struct sdhci_host *host)
189{
190 sdhci_init(host);
191 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192}
193
194static void sdhci_activate_led(struct sdhci_host *host)
195{
196 u8 ctrl;
197
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300198 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800199 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300200 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201}
202
203static void sdhci_deactivate_led(struct sdhci_host *host)
204{
205 u8 ctrl;
206
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300207 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300209 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210}
211
Pierre Ossmanf9134312008-12-21 17:01:48 +0100212#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100213static void sdhci_led_control(struct led_classdev *led,
214 enum led_brightness brightness)
215{
216 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
217 unsigned long flags;
218
219 spin_lock_irqsave(&host->lock, flags);
220
221 if (brightness == LED_OFF)
222 sdhci_deactivate_led(host);
223 else
224 sdhci_activate_led(host);
225
226 spin_unlock_irqrestore(&host->lock, flags);
227}
228#endif
229
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230/*****************************************************************************\
231 * *
232 * Core functions *
233 * *
234\*****************************************************************************/
235
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100236static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237{
Pierre Ossman76591502008-07-21 00:32:11 +0200238 unsigned long flags;
239 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700240 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200241 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100243 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100245 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200246 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800247
Pierre Ossman76591502008-07-21 00:32:11 +0200248 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100250 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200251 if (!sg_miter_next(&host->sg_miter))
252 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253
Pierre Ossman76591502008-07-21 00:32:11 +0200254 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255
Pierre Ossman76591502008-07-21 00:32:11 +0200256 blksize -= len;
257 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200258
Pierre Ossman76591502008-07-21 00:32:11 +0200259 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260
Pierre Ossman76591502008-07-21 00:32:11 +0200261 while (len) {
262 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300263 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200264 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265 }
Pierre Ossman76591502008-07-21 00:32:11 +0200266
267 *buf = scratch & 0xFF;
268
269 buf++;
270 scratch >>= 8;
271 chunk--;
272 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273 }
274 }
Pierre Ossman76591502008-07-21 00:32:11 +0200275
276 sg_miter_stop(&host->sg_miter);
277
278 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100279}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100281static void sdhci_write_block_pio(struct sdhci_host *host)
282{
Pierre Ossman76591502008-07-21 00:32:11 +0200283 unsigned long flags;
284 size_t blksize, len, chunk;
285 u32 scratch;
286 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100287
288 DBG("PIO writing\n");
289
290 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200291 chunk = 0;
292 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100293
Pierre Ossman76591502008-07-21 00:32:11 +0200294 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100295
296 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200297 if (!sg_miter_next(&host->sg_miter))
298 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299
Pierre Ossman76591502008-07-21 00:32:11 +0200300 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200301
Pierre Ossman76591502008-07-21 00:32:11 +0200302 blksize -= len;
303 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304
Pierre Ossman76591502008-07-21 00:32:11 +0200305 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306
Pierre Ossman76591502008-07-21 00:32:11 +0200307 while (len) {
308 scratch |= (u32)*buf << (chunk * 8);
309
310 buf++;
311 chunk++;
312 len--;
313
314 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300315 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200316 chunk = 0;
317 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319 }
320 }
Pierre Ossman76591502008-07-21 00:32:11 +0200321
322 sg_miter_stop(&host->sg_miter);
323
324 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100325}
326
327static void sdhci_transfer_pio(struct sdhci_host *host)
328{
329 u32 mask;
330
331 BUG_ON(!host->data);
332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100334 return;
335
336 if (host->data->flags & MMC_DATA_READ)
337 mask = SDHCI_DATA_AVAILABLE;
338 else
339 mask = SDHCI_SPACE_AVAILABLE;
340
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200341 /*
342 * Some controllers (JMicron JMB38x) mess up the buffer bits
343 * for transfers < 4 bytes. As long as it is just one block,
344 * we can ignore the bits.
345 */
346 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
347 (host->data->blocks == 1))
348 mask = ~0;
349
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300350 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300351 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
352 udelay(100);
353
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354 if (host->data->flags & MMC_DATA_READ)
355 sdhci_read_block_pio(host);
356 else
357 sdhci_write_block_pio(host);
358
Pierre Ossman76591502008-07-21 00:32:11 +0200359 host->blocks--;
360 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362 }
363
364 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800365}
366
Pierre Ossman2134a922008-06-28 18:28:51 +0200367static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
368{
369 local_irq_save(*flags);
370 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
371}
372
373static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
374{
375 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
376 local_irq_restore(*flags);
377}
378
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200379static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200380 struct mmc_data *data)
381{
382 int direction;
383
384 u8 *desc;
385 u8 *align;
386 dma_addr_t addr;
387 dma_addr_t align_addr;
388 int len, offset;
389
390 struct scatterlist *sg;
391 int i;
392 char *buffer;
393 unsigned long flags;
394
395 /*
396 * The spec does not specify endianness of descriptor table.
397 * We currently guess that it is LE.
398 */
399
400 if (data->flags & MMC_DATA_READ)
401 direction = DMA_FROM_DEVICE;
402 else
403 direction = DMA_TO_DEVICE;
404
405 /*
406 * The ADMA descriptor table is mapped further down as we
407 * need to fill it with data first.
408 */
409
410 host->align_addr = dma_map_single(mmc_dev(host->mmc),
411 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700412 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200413 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200414 BUG_ON(host->align_addr & 0x3);
415
416 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
417 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200418 if (host->sg_count == 0)
419 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200420
421 desc = host->adma_desc;
422 align = host->align_buffer;
423
424 align_addr = host->align_addr;
425
426 for_each_sg(data->sg, sg, host->sg_count, i) {
427 addr = sg_dma_address(sg);
428 len = sg_dma_len(sg);
429
430 /*
431 * The SDHCI specification states that ADMA
432 * addresses must be 32-bit aligned. If they
433 * aren't, then we use a bounce buffer for
434 * the (up to three) bytes that screw up the
435 * alignment.
436 */
437 offset = (4 - (addr & 0x3)) & 0x3;
438 if (offset) {
439 if (data->flags & MMC_DATA_WRITE) {
440 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200441 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200442 memcpy(align, buffer, offset);
443 sdhci_kunmap_atomic(buffer, &flags);
444 }
445
446 desc[7] = (align_addr >> 24) & 0xff;
447 desc[6] = (align_addr >> 16) & 0xff;
448 desc[5] = (align_addr >> 8) & 0xff;
449 desc[4] = (align_addr >> 0) & 0xff;
450
451 BUG_ON(offset > 65536);
452
453 desc[3] = (offset >> 8) & 0xff;
454 desc[2] = (offset >> 0) & 0xff;
455
456 desc[1] = 0x00;
457 desc[0] = 0x21; /* tran, valid */
458
459 align += 4;
460 align_addr += 4;
461
462 desc += 8;
463
464 addr += offset;
465 len -= offset;
466 }
467
468 desc[7] = (addr >> 24) & 0xff;
469 desc[6] = (addr >> 16) & 0xff;
470 desc[5] = (addr >> 8) & 0xff;
471 desc[4] = (addr >> 0) & 0xff;
472
473 BUG_ON(len > 65536);
474
475 desc[3] = (len >> 8) & 0xff;
476 desc[2] = (len >> 0) & 0xff;
477
478 desc[1] = 0x00;
479 desc[0] = 0x21; /* tran, valid */
480
481 desc += 8;
482
483 /*
484 * If this triggers then we have a calculation bug
485 * somewhere. :/
486 */
487 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
488 }
489
490 /*
491 * Add a terminating entry.
492 */
493 desc[7] = 0;
494 desc[6] = 0;
495 desc[5] = 0;
496 desc[4] = 0;
497
498 desc[3] = 0;
499 desc[2] = 0;
500
501 desc[1] = 0x00;
502 desc[0] = 0x03; /* nop, end, valid */
503
504 /*
505 * Resync align buffer as we might have changed it.
506 */
507 if (data->flags & MMC_DATA_WRITE) {
508 dma_sync_single_for_device(mmc_dev(host->mmc),
509 host->align_addr, 128 * 4, direction);
510 }
511
512 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
513 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200514 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200515 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200516 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200517
518 return 0;
519
520unmap_entries:
521 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
522 data->sg_len, direction);
523unmap_align:
524 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
525 128 * 4, direction);
526fail:
527 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200528}
529
530static void sdhci_adma_table_post(struct sdhci_host *host,
531 struct mmc_data *data)
532{
533 int direction;
534
535 struct scatterlist *sg;
536 int i, size;
537 u8 *align;
538 char *buffer;
539 unsigned long flags;
540
541 if (data->flags & MMC_DATA_READ)
542 direction = DMA_FROM_DEVICE;
543 else
544 direction = DMA_TO_DEVICE;
545
546 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
547 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
548
549 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
550 128 * 4, direction);
551
552 if (data->flags & MMC_DATA_READ) {
553 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
554 data->sg_len, direction);
555
556 align = host->align_buffer;
557
558 for_each_sg(data->sg, sg, host->sg_count, i) {
559 if (sg_dma_address(sg) & 0x3) {
560 size = 4 - (sg_dma_address(sg) & 0x3);
561
562 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200563 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200564 memcpy(buffer, align, size);
565 sdhci_kunmap_atomic(buffer, &flags);
566
567 align += 4;
568 }
569 }
570 }
571
572 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
573 data->sg_len, direction);
574}
575
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200576static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800577{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700578 u8 count;
579 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800580
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200581 /*
582 * If the host controller provides us with an incorrect timeout
583 * value, just skip the check and use 0xE. The hardware may take
584 * longer to time out, but that's much better than having a too-short
585 * timeout value.
586 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200587 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200588 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200589
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700590 /* timeout in us */
591 target_timeout = data->timeout_ns / 1000 +
592 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800593
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700594 /*
595 * Figure out needed cycles.
596 * We do this in steps in order to fit inside a 32 bit int.
597 * The first step is the minimum timeout, which will have a
598 * minimum resolution of 6 bits:
599 * (1) 2^13*1000 > 2^22,
600 * (2) host->timeout_clk < 2^16
601 * =>
602 * (1) / (2) > 2^6
603 */
604 count = 0;
605 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
606 while (current_timeout < target_timeout) {
607 count++;
608 current_timeout <<= 1;
609 if (count >= 0xF)
610 break;
611 }
612
613 if (count >= 0xF) {
614 printk(KERN_WARNING "%s: Too large timeout requested!\n",
615 mmc_hostname(host->mmc));
616 count = 0xE;
617 }
618
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200619 return count;
620}
621
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300622static void sdhci_set_transfer_irqs(struct sdhci_host *host)
623{
624 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
625 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
626
627 if (host->flags & SDHCI_REQ_USE_DMA)
628 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
629 else
630 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
631}
632
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200633static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
634{
635 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200637 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200638
639 WARN_ON(host->data);
640
641 if (data == NULL)
642 return;
643
644 /* Sanity checks */
645 BUG_ON(data->blksz * data->blocks > 524288);
646 BUG_ON(data->blksz > host->mmc->max_blk_size);
647 BUG_ON(data->blocks > 65535);
648
649 host->data = data;
650 host->data_early = 0;
651
652 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300653 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800654
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100655 if (host->flags & SDHCI_USE_DMA)
656 host->flags |= SDHCI_REQ_USE_DMA;
657
Pierre Ossman2134a922008-06-28 18:28:51 +0200658 /*
659 * FIXME: This doesn't account for merging when mapping the
660 * scatterlist.
661 */
662 if (host->flags & SDHCI_REQ_USE_DMA) {
663 int broken, i;
664 struct scatterlist *sg;
665
666 broken = 0;
667 if (host->flags & SDHCI_USE_ADMA) {
668 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
669 broken = 1;
670 } else {
671 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
672 broken = 1;
673 }
674
675 if (unlikely(broken)) {
676 for_each_sg(data->sg, sg, data->sg_len, i) {
677 if (sg->length & 0x3) {
678 DBG("Reverting to PIO because of "
679 "transfer size (%d)\n",
680 sg->length);
681 host->flags &= ~SDHCI_REQ_USE_DMA;
682 break;
683 }
684 }
685 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100686 }
687
688 /*
689 * The assumption here being that alignment is the same after
690 * translation to device address space.
691 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200692 if (host->flags & SDHCI_REQ_USE_DMA) {
693 int broken, i;
694 struct scatterlist *sg;
695
696 broken = 0;
697 if (host->flags & SDHCI_USE_ADMA) {
698 /*
699 * As we use 3 byte chunks to work around
700 * alignment problems, we need to check this
701 * quirk.
702 */
703 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
704 broken = 1;
705 } else {
706 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
707 broken = 1;
708 }
709
710 if (unlikely(broken)) {
711 for_each_sg(data->sg, sg, data->sg_len, i) {
712 if (sg->offset & 0x3) {
713 DBG("Reverting to PIO because of "
714 "bad alignment\n");
715 host->flags &= ~SDHCI_REQ_USE_DMA;
716 break;
717 }
718 }
719 }
720 }
721
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200722 if (host->flags & SDHCI_REQ_USE_DMA) {
723 if (host->flags & SDHCI_USE_ADMA) {
724 ret = sdhci_adma_table_pre(host, data);
725 if (ret) {
726 /*
727 * This only happens when someone fed
728 * us an invalid request.
729 */
730 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200731 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200732 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300733 sdhci_writel(host, host->adma_addr,
734 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200735 }
736 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300737 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200738
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300739 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200740 data->sg, data->sg_len,
741 (data->flags & MMC_DATA_READ) ?
742 DMA_FROM_DEVICE :
743 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300744 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200745 /*
746 * This only happens when someone fed
747 * us an invalid request.
748 */
749 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200750 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200751 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200752 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300753 sdhci_writel(host, sg_dma_address(data->sg),
754 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755 }
756 }
757 }
758
Pierre Ossman2134a922008-06-28 18:28:51 +0200759 /*
760 * Always adjust the DMA selection as some controllers
761 * (e.g. JMicron) can't do PIO properly when the selection
762 * is ADMA.
763 */
764 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300765 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200766 ctrl &= ~SDHCI_CTRL_DMA_MASK;
767 if ((host->flags & SDHCI_REQ_USE_DMA) &&
768 (host->flags & SDHCI_USE_ADMA))
769 ctrl |= SDHCI_CTRL_ADMA32;
770 else
771 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300772 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100773 }
774
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200775 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200776 int flags;
777
778 flags = SG_MITER_ATOMIC;
779 if (host->data->flags & MMC_DATA_READ)
780 flags |= SG_MITER_TO_SG;
781 else
782 flags |= SG_MITER_FROM_SG;
783 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200784 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800785 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700786
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300787 sdhci_set_transfer_irqs(host);
788
Pierre Ossmanbab76962006-07-02 16:51:35 +0100789 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300790 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
791 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700792}
793
794static void sdhci_set_transfer_mode(struct sdhci_host *host,
795 struct mmc_data *data)
796{
797 u16 mode;
798
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700799 if (data == NULL)
800 return;
801
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200802 WARN_ON(!host->data);
803
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700804 mode = SDHCI_TRNS_BLK_CNT_EN;
805 if (data->blocks > 1)
806 mode |= SDHCI_TRNS_MULTI;
807 if (data->flags & MMC_DATA_READ)
808 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100809 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700810 mode |= SDHCI_TRNS_DMA;
811
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300812 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800813}
814
815static void sdhci_finish_data(struct sdhci_host *host)
816{
817 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800818
819 BUG_ON(!host->data);
820
821 data = host->data;
822 host->data = NULL;
823
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100824 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200825 if (host->flags & SDHCI_USE_ADMA)
826 sdhci_adma_table_post(host, data);
827 else {
828 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
829 data->sg_len, (data->flags & MMC_DATA_READ) ?
830 DMA_FROM_DEVICE : DMA_TO_DEVICE);
831 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800832 }
833
834 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200835 * The specification states that the block count register must
836 * be updated, but it does not specify at what point in the
837 * data flow. That makes the register entirely useless to read
838 * back so we have to assume that nothing made it to the card
839 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800840 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200841 if (data->error)
842 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800843 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200844 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800845
Pierre Ossmand129bce2006-03-24 03:18:17 -0800846 if (data->stop) {
847 /*
848 * The controller needs a reset of internal state machines
849 * upon error conditions.
850 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200851 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800852 sdhci_reset(host, SDHCI_RESET_CMD);
853 sdhci_reset(host, SDHCI_RESET_DATA);
854 }
855
856 sdhci_send_command(host, data->stop);
857 } else
858 tasklet_schedule(&host->finish_tasklet);
859}
860
861static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
862{
863 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700864 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700865 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866
867 WARN_ON(host->cmd);
868
Pierre Ossmand129bce2006-03-24 03:18:17 -0800869 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700870 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700871
872 mask = SDHCI_CMD_INHIBIT;
873 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
874 mask |= SDHCI_DATA_INHIBIT;
875
876 /* We shouldn't wait for data inihibit for stop commands, even
877 though they might use busy signaling */
878 if (host->mrq->data && (cmd == host->mrq->data->stop))
879 mask &= ~SDHCI_DATA_INHIBIT;
880
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700882 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800883 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100884 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800885 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200886 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887 tasklet_schedule(&host->finish_tasklet);
888 return;
889 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700890 timeout--;
891 mdelay(1);
892 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893
894 mod_timer(&host->timer, jiffies + 10 * HZ);
895
896 host->cmd = cmd;
897
898 sdhci_prepare_data(host, cmd->data);
899
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300900 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902 sdhci_set_transfer_mode(host, cmd->data);
903
Pierre Ossmand129bce2006-03-24 03:18:17 -0800904 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100905 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800906 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200907 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908 tasklet_schedule(&host->finish_tasklet);
909 return;
910 }
911
912 if (!(cmd->flags & MMC_RSP_PRESENT))
913 flags = SDHCI_CMD_RESP_NONE;
914 else if (cmd->flags & MMC_RSP_136)
915 flags = SDHCI_CMD_RESP_LONG;
916 else if (cmd->flags & MMC_RSP_BUSY)
917 flags = SDHCI_CMD_RESP_SHORT_BUSY;
918 else
919 flags = SDHCI_CMD_RESP_SHORT;
920
921 if (cmd->flags & MMC_RSP_CRC)
922 flags |= SDHCI_CMD_CRC;
923 if (cmd->flags & MMC_RSP_OPCODE)
924 flags |= SDHCI_CMD_INDEX;
925 if (cmd->data)
926 flags |= SDHCI_CMD_DATA;
927
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300928 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929}
930
931static void sdhci_finish_command(struct sdhci_host *host)
932{
933 int i;
934
935 BUG_ON(host->cmd == NULL);
936
937 if (host->cmd->flags & MMC_RSP_PRESENT) {
938 if (host->cmd->flags & MMC_RSP_136) {
939 /* CRC is stripped so we need to do some shifting. */
940 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300941 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 SDHCI_RESPONSE + (3-i)*4) << 8;
943 if (i != 3)
944 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300945 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 SDHCI_RESPONSE + (3-i)*4-1);
947 }
948 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 }
951 }
952
Pierre Ossman17b04292007-07-22 22:18:46 +0200953 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200955 if (host->data && host->data_early)
956 sdhci_finish_data(host);
957
958 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959 tasklet_schedule(&host->finish_tasklet);
960
961 host->cmd = NULL;
962}
963
964static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
965{
966 int div;
967 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700968 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
970 if (clock == host->clock)
971 return;
972
Anton Vorontsov81146342009-03-17 00:13:59 +0300973 if (host->ops->set_clock) {
974 host->ops->set_clock(host, clock);
975 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
976 return;
977 }
978
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300979 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980
981 if (clock == 0)
982 goto out;
983
984 for (div = 1;div < 256;div *= 2) {
985 if ((host->max_clk / div) <= clock)
986 break;
987 }
988 div >>= 1;
989
990 clk = div << SDHCI_DIVIDER_SHIFT;
991 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300992 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993
994 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700995 timeout = 10;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300996 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700997 & SDHCI_CLOCK_INT_STABLE)) {
998 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100999 printk(KERN_ERR "%s: Internal clock never "
1000 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001 sdhci_dumpregs(host);
1002 return;
1003 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001004 timeout--;
1005 mdelay(1);
1006 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007
1008 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001009 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
1011out:
1012 host->clock = clock;
1013}
1014
Pierre Ossman146ad662006-06-30 02:22:23 -07001015static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1016{
1017 u8 pwr;
1018
Pierre Ossmanae628902009-05-03 20:45:03 +02001019 if (power == (unsigned short)-1)
1020 pwr = 0;
1021 else {
1022 switch (1 << power) {
1023 case MMC_VDD_165_195:
1024 pwr = SDHCI_POWER_180;
1025 break;
1026 case MMC_VDD_29_30:
1027 case MMC_VDD_30_31:
1028 pwr = SDHCI_POWER_300;
1029 break;
1030 case MMC_VDD_32_33:
1031 case MMC_VDD_33_34:
1032 pwr = SDHCI_POWER_330;
1033 break;
1034 default:
1035 BUG();
1036 }
1037 }
1038
1039 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001040 return;
1041
Pierre Ossmanae628902009-05-03 20:45:03 +02001042 host->pwr = pwr;
1043
1044 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001045 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001046 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001047 }
1048
1049 /*
1050 * Spec says that we should clear the power reg before setting
1051 * a new value. Some controllers don't seem to like this though.
1052 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001053 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001055
Andres Salomone08c1692008-07-04 10:00:03 -07001056 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001057 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001058 * and set turn on power at the same time, so set the voltage first.
1059 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001060 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001061 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1062
1063 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001064
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001065 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001066
1067 /*
1068 * Some controllers need an extra 10ms delay of 10ms before they
1069 * can apply clock after applying power
1070 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001071 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001072 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001073}
1074
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075/*****************************************************************************\
1076 * *
1077 * MMC callbacks *
1078 * *
1079\*****************************************************************************/
1080
1081static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1082{
1083 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001084 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 unsigned long flags;
1086
1087 host = mmc_priv(mmc);
1088
1089 spin_lock_irqsave(&host->lock, flags);
1090
1091 WARN_ON(host->mrq != NULL);
1092
Pierre Ossmanf9134312008-12-21 17:01:48 +01001093#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001095#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001096
1097 host->mrq = mrq;
1098
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001099 /* If polling, assume that the card is always present. */
1100 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1101 present = true;
1102 else
1103 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1104 SDHCI_CARD_PRESENT;
1105
1106 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001107 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001108 tasklet_schedule(&host->finish_tasklet);
1109 } else
1110 sdhci_send_command(host, mrq->cmd);
1111
Pierre Ossman5f25a662006-10-04 02:15:39 -07001112 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113 spin_unlock_irqrestore(&host->lock, flags);
1114}
1115
1116static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1117{
1118 struct sdhci_host *host;
1119 unsigned long flags;
1120 u8 ctrl;
1121
1122 host = mmc_priv(mmc);
1123
1124 spin_lock_irqsave(&host->lock, flags);
1125
Pierre Ossman1e728592008-04-16 19:13:13 +02001126 if (host->flags & SDHCI_DEVICE_DEAD)
1127 goto out;
1128
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129 /*
1130 * Reset the chip on each power off.
1131 * Should clear out any weird states.
1132 */
1133 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001134 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001135 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136 }
1137
1138 sdhci_set_clock(host, ios->clock);
1139
1140 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001141 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001143 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001145 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001146
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 if (ios->bus_width == MMC_BUS_WIDTH_4)
1148 ctrl |= SDHCI_CTRL_4BITBUS;
1149 else
1150 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001151
1152 if (ios->timing == MMC_TIMING_SD_HS)
1153 ctrl |= SDHCI_CTRL_HISPD;
1154 else
1155 ctrl &= ~SDHCI_CTRL_HISPD;
1156
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001157 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158
Leandro Dorileob8352262007-07-25 23:47:04 +02001159 /*
1160 * Some (ENE) controllers go apeshit on some ios operation,
1161 * signalling timeout and CRC errors even on CMD0. Resetting
1162 * it on each ios seems to solve the problem.
1163 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001164 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001165 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1166
Pierre Ossman1e728592008-04-16 19:13:13 +02001167out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001168 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169 spin_unlock_irqrestore(&host->lock, flags);
1170}
1171
1172static int sdhci_get_ro(struct mmc_host *mmc)
1173{
1174 struct sdhci_host *host;
1175 unsigned long flags;
1176 int present;
1177
1178 host = mmc_priv(mmc);
1179
1180 spin_lock_irqsave(&host->lock, flags);
1181
Pierre Ossman1e728592008-04-16 19:13:13 +02001182 if (host->flags & SDHCI_DEVICE_DEAD)
1183 present = 0;
1184 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001185 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186
1187 spin_unlock_irqrestore(&host->lock, flags);
1188
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001189 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1190 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001191 return !(present & SDHCI_WRITE_PROTECT);
1192}
1193
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001194static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1195{
1196 struct sdhci_host *host;
1197 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001198
1199 host = mmc_priv(mmc);
1200
1201 spin_lock_irqsave(&host->lock, flags);
1202
Pierre Ossman1e728592008-04-16 19:13:13 +02001203 if (host->flags & SDHCI_DEVICE_DEAD)
1204 goto out;
1205
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001206 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001207 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1208 else
1209 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001210out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001211 mmiowb();
1212
1213 spin_unlock_irqrestore(&host->lock, flags);
1214}
1215
David Brownellab7aefd2006-11-12 17:55:30 -08001216static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217 .request = sdhci_request,
1218 .set_ios = sdhci_set_ios,
1219 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001220 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221};
1222
1223/*****************************************************************************\
1224 * *
1225 * Tasklets *
1226 * *
1227\*****************************************************************************/
1228
1229static void sdhci_tasklet_card(unsigned long param)
1230{
1231 struct sdhci_host *host;
1232 unsigned long flags;
1233
1234 host = (struct sdhci_host*)param;
1235
1236 spin_lock_irqsave(&host->lock, flags);
1237
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239 if (host->mrq) {
1240 printk(KERN_ERR "%s: Card removed during transfer!\n",
1241 mmc_hostname(host->mmc));
1242 printk(KERN_ERR "%s: Resetting controller.\n",
1243 mmc_hostname(host->mmc));
1244
1245 sdhci_reset(host, SDHCI_RESET_CMD);
1246 sdhci_reset(host, SDHCI_RESET_DATA);
1247
Pierre Ossman17b04292007-07-22 22:18:46 +02001248 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249 tasklet_schedule(&host->finish_tasklet);
1250 }
1251 }
1252
1253 spin_unlock_irqrestore(&host->lock, flags);
1254
Pierre Ossman04cf5852008-08-18 22:18:14 +02001255 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256}
1257
1258static void sdhci_tasklet_finish(unsigned long param)
1259{
1260 struct sdhci_host *host;
1261 unsigned long flags;
1262 struct mmc_request *mrq;
1263
1264 host = (struct sdhci_host*)param;
1265
1266 spin_lock_irqsave(&host->lock, flags);
1267
1268 del_timer(&host->timer);
1269
1270 mrq = host->mrq;
1271
Pierre Ossmand129bce2006-03-24 03:18:17 -08001272 /*
1273 * The controller needs a reset of internal state machines
1274 * upon error conditions.
1275 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001276 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1277 (mrq->cmd->error ||
1278 (mrq->data && (mrq->data->error ||
1279 (mrq->data->stop && mrq->data->stop->error))) ||
1280 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001281
1282 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001283 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001284 unsigned int clock;
1285
1286 /* This is to force an update */
1287 clock = host->clock;
1288 host->clock = 0;
1289 sdhci_set_clock(host, clock);
1290 }
1291
1292 /* Spec says we should do both at the same time, but Ricoh
1293 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001294 sdhci_reset(host, SDHCI_RESET_CMD);
1295 sdhci_reset(host, SDHCI_RESET_DATA);
1296 }
1297
1298 host->mrq = NULL;
1299 host->cmd = NULL;
1300 host->data = NULL;
1301
Pierre Ossmanf9134312008-12-21 17:01:48 +01001302#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001304#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001305
Pierre Ossman5f25a662006-10-04 02:15:39 -07001306 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307 spin_unlock_irqrestore(&host->lock, flags);
1308
1309 mmc_request_done(host->mmc, mrq);
1310}
1311
1312static void sdhci_timeout_timer(unsigned long data)
1313{
1314 struct sdhci_host *host;
1315 unsigned long flags;
1316
1317 host = (struct sdhci_host*)data;
1318
1319 spin_lock_irqsave(&host->lock, flags);
1320
1321 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001322 printk(KERN_ERR "%s: Timeout waiting for hardware "
1323 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001324 sdhci_dumpregs(host);
1325
1326 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001327 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001328 sdhci_finish_data(host);
1329 } else {
1330 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001331 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001333 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334
1335 tasklet_schedule(&host->finish_tasklet);
1336 }
1337 }
1338
Pierre Ossman5f25a662006-10-04 02:15:39 -07001339 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 spin_unlock_irqrestore(&host->lock, flags);
1341}
1342
1343/*****************************************************************************\
1344 * *
1345 * Interrupt handling *
1346 * *
1347\*****************************************************************************/
1348
1349static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1350{
1351 BUG_ON(intmask == 0);
1352
1353 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001354 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1355 "though no command operation was in progress.\n",
1356 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357 sdhci_dumpregs(host);
1358 return;
1359 }
1360
Pierre Ossman43b58b32007-07-25 23:15:27 +02001361 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001362 host->cmd->error = -ETIMEDOUT;
1363 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1364 SDHCI_INT_INDEX))
1365 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001366
Pierre Ossmane8095172008-07-25 01:09:08 +02001367 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001369 return;
1370 }
1371
1372 /*
1373 * The host can send and interrupt when the busy state has
1374 * ended, allowing us to wait without wasting CPU cycles.
1375 * Unfortunately this is overloaded on the "data complete"
1376 * interrupt, so we need to take some care when handling
1377 * it.
1378 *
1379 * Note: The 1.0 specification is a bit ambiguous about this
1380 * feature so there might be some problems with older
1381 * controllers.
1382 */
1383 if (host->cmd->flags & MMC_RSP_BUSY) {
1384 if (host->cmd->data)
1385 DBG("Cannot wait for busy signal when also "
1386 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001387 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001388 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001389
1390 /* The controller does not support the end-of-busy IRQ,
1391 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001392 }
1393
1394 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001395 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396}
1397
Ben Dooks6882a8c2009-06-14 13:52:38 +01001398#ifdef DEBUG
1399static void sdhci_show_adma_error(struct sdhci_host *host)
1400{
1401 const char *name = mmc_hostname(host->mmc);
1402 u8 *desc = host->adma_desc;
1403 __le32 *dma;
1404 __le16 *len;
1405 u8 attr;
1406
1407 sdhci_dumpregs(host);
1408
1409 while (true) {
1410 dma = (__le32 *)(desc + 4);
1411 len = (__le16 *)(desc + 2);
1412 attr = *desc;
1413
1414 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1415 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1416
1417 desc += 8;
1418
1419 if (attr & 2)
1420 break;
1421 }
1422}
1423#else
1424static void sdhci_show_adma_error(struct sdhci_host *host) { }
1425#endif
1426
Pierre Ossmand129bce2006-03-24 03:18:17 -08001427static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1428{
1429 BUG_ON(intmask == 0);
1430
1431 if (!host->data) {
1432 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001433 * The "data complete" interrupt is also used to
1434 * indicate that a busy state has ended. See comment
1435 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001437 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1438 if (intmask & SDHCI_INT_DATA_END) {
1439 sdhci_finish_command(host);
1440 return;
1441 }
1442 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001444 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1445 "though no data operation was in progress.\n",
1446 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447 sdhci_dumpregs(host);
1448
1449 return;
1450 }
1451
1452 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001453 host->data->error = -ETIMEDOUT;
1454 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1455 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001456 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1457 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1458 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001459 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001460 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461
Pierre Ossman17b04292007-07-22 22:18:46 +02001462 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463 sdhci_finish_data(host);
1464 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001465 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466 sdhci_transfer_pio(host);
1467
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001468 /*
1469 * We currently don't do anything fancy with DMA
1470 * boundaries, but as we can't disable the feature
1471 * we need to at least restart the transfer.
1472 */
1473 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001474 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1475 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001476
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001477 if (intmask & SDHCI_INT_DATA_END) {
1478 if (host->cmd) {
1479 /*
1480 * Data managed to finish before the
1481 * command completed. Make sure we do
1482 * things in the proper order.
1483 */
1484 host->data_early = 1;
1485 } else {
1486 sdhci_finish_data(host);
1487 }
1488 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489 }
1490}
1491
David Howells7d12e782006-10-05 14:55:46 +01001492static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493{
1494 irqreturn_t result;
1495 struct sdhci_host* host = dev_id;
1496 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001497 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498
1499 spin_lock(&host->lock);
1500
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001501 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
Mark Lord62df67a52007-03-06 13:30:13 +01001503 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504 result = IRQ_NONE;
1505 goto out;
1506 }
1507
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001508 DBG("*** %s got interrupt: 0x%08x\n",
1509 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001510
Pierre Ossman3192a282006-06-30 02:22:26 -07001511 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001512 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1513 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001515 }
1516
1517 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518
1519 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001520 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1521 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001522 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523 }
1524
1525 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001526 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1527 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001528 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529 }
1530
1531 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1532
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001533 intmask &= ~SDHCI_INT_ERROR;
1534
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001536 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001538 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001539 }
1540
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001541 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001542
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001543 if (intmask & SDHCI_INT_CARD_INT)
1544 cardint = 1;
1545
1546 intmask &= ~SDHCI_INT_CARD_INT;
1547
Pierre Ossman3192a282006-06-30 02:22:26 -07001548 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001549 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001550 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001551 sdhci_dumpregs(host);
1552
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001553 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001554 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001555
1556 result = IRQ_HANDLED;
1557
Pierre Ossman5f25a662006-10-04 02:15:39 -07001558 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001559out:
1560 spin_unlock(&host->lock);
1561
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001562 /*
1563 * We have to delay this as it calls back into the driver.
1564 */
1565 if (cardint)
1566 mmc_signal_sdio_irq(host->mmc);
1567
Pierre Ossmand129bce2006-03-24 03:18:17 -08001568 return result;
1569}
1570
1571/*****************************************************************************\
1572 * *
1573 * Suspend/resume *
1574 * *
1575\*****************************************************************************/
1576
1577#ifdef CONFIG_PM
1578
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001579int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001580{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001581 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001583 sdhci_disable_card_detection(host);
1584
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001585 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001586 if (ret)
1587 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001588
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001589 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001590
1591 return 0;
1592}
1593
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001594EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001596int sdhci_resume_host(struct sdhci_host *host)
1597{
1598 int ret;
1599
1600 if (host->flags & SDHCI_USE_DMA) {
1601 if (host->ops->enable_dma)
1602 host->ops->enable_dma(host);
1603 }
1604
1605 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1606 mmc_hostname(host->mmc), host);
1607 if (ret)
1608 return ret;
1609
1610 sdhci_init(host);
1611 mmiowb();
1612
1613 ret = mmc_resume_host(host->mmc);
1614 if (ret)
1615 return ret;
1616
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001617 sdhci_enable_card_detection(host);
1618
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001619 return 0;
1620}
1621
1622EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001623
1624#endif /* CONFIG_PM */
1625
1626/*****************************************************************************\
1627 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001628 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629 * *
1630\*****************************************************************************/
1631
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001632struct sdhci_host *sdhci_alloc_host(struct device *dev,
1633 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635 struct mmc_host *mmc;
1636 struct sdhci_host *host;
1637
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001638 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001639
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001640 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001642 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643
1644 host = mmc_priv(mmc);
1645 host->mmc = mmc;
1646
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001647 return host;
1648}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001649
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001650EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652int sdhci_add_host(struct sdhci_host *host)
1653{
1654 struct mmc_host *mmc;
1655 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001656 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001658 WARN_ON(host == NULL);
1659 if (host == NULL)
1660 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001661
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001662 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001663
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001664 if (debug_quirks)
1665 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001666
Pierre Ossmand96649e2006-06-30 02:22:30 -07001667 sdhci_reset(host, SDHCI_RESET_ALL);
1668
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001669 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001670 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1671 >> SDHCI_SPEC_VER_SHIFT;
1672 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001673 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001674 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001675 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001676 }
1677
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001678 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001680 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001681 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001682 else if (!(caps & SDHCI_CAN_DO_DMA))
1683 DBG("Controller doesn't have DMA capability\n");
1684 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001685 host->flags |= SDHCI_USE_DMA;
1686
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001687 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001688 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001689 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001690 host->flags &= ~SDHCI_USE_DMA;
1691 }
1692
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001694 if ((host->version >= SDHCI_SPEC_200) &&
1695 (caps & SDHCI_CAN_DO_ADMA2))
1696 host->flags |= SDHCI_USE_ADMA;
1697 }
1698
1699 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1700 (host->flags & SDHCI_USE_ADMA)) {
1701 DBG("Disabling ADMA as it is marked broken\n");
1702 host->flags &= ~SDHCI_USE_ADMA;
1703 }
1704
1705 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001706 if (host->ops->enable_dma) {
1707 if (host->ops->enable_dma(host)) {
1708 printk(KERN_WARNING "%s: No suitable DMA "
1709 "available. Falling back to PIO.\n",
1710 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001711 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001712 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001713 }
1714 }
1715
Pierre Ossman2134a922008-06-28 18:28:51 +02001716 if (host->flags & SDHCI_USE_ADMA) {
1717 /*
1718 * We need to allocate descriptors for all sg entries
1719 * (128) and potentially one alignment transfer for
1720 * each of those entries.
1721 */
1722 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1723 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1724 if (!host->adma_desc || !host->align_buffer) {
1725 kfree(host->adma_desc);
1726 kfree(host->align_buffer);
1727 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1728 "buffers. Falling back to standard DMA.\n",
1729 mmc_hostname(mmc));
1730 host->flags &= ~SDHCI_USE_ADMA;
1731 }
1732 }
1733
Pierre Ossman76591502008-07-21 00:32:11 +02001734 /*
1735 * If we use DMA, then it's up to the caller to set the DMA
1736 * mask, but PIO does not need the hw shim so we set a new
1737 * mask here in that case.
1738 */
1739 if (!(host->flags & SDHCI_USE_DMA)) {
1740 host->dma_mask = DMA_BIT_MASK(64);
1741 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1742 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001744 host->max_clk =
1745 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001746 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001747 if (host->max_clk == 0) {
1748 if (!host->ops->get_max_clock) {
1749 printk(KERN_ERR
1750 "%s: Hardware doesn't specify base clock "
1751 "frequency.\n", mmc_hostname(mmc));
1752 return -ENODEV;
1753 }
1754 host->max_clk = host->ops->get_max_clock(host);
1755 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001756
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001757 host->timeout_clk =
1758 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1759 if (host->timeout_clk == 0) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001760 if (!host->ops->get_timeout_clock) {
1761 printk(KERN_ERR
1762 "%s: Hardware doesn't specify timeout clock "
1763 "frequency.\n", mmc_hostname(mmc));
1764 return -ENODEV;
1765 }
1766 host->timeout_clk = host->ops->get_timeout_clock(host);
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001767 }
1768 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1769 host->timeout_clk *= 1000;
1770
Pierre Ossmand129bce2006-03-24 03:18:17 -08001771 /*
1772 * Set host parameters.
1773 */
1774 mmc->ops = &sdhci_ops;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001775 if (host->ops->get_min_clock)
1776 mmc->f_min = host->ops->get_min_clock(host);
1777 else
1778 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001779 mmc->f_max = host->max_clk;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001780 mmc->caps = MMC_CAP_SDIO_IRQ;
1781
1782 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1783 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001784
Pierre Ossman86a6a872009-02-02 21:13:49 +01001785 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001786 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1787
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001788 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1789 mmc->caps |= MMC_CAP_NEEDS_POLL;
1790
Pierre Ossman146ad662006-06-30 02:22:23 -07001791 mmc->ocr_avail = 0;
1792 if (caps & SDHCI_CAN_VDD_330)
1793 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001794 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001795 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001796 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001797 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001798
1799 if (mmc->ocr_avail == 0) {
1800 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001801 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001802 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001803 }
1804
Pierre Ossmand129bce2006-03-24 03:18:17 -08001805 spin_lock_init(&host->lock);
1806
1807 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001808 * Maximum number of segments. Depends on if the hardware
1809 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001810 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001811 if (host->flags & SDHCI_USE_ADMA)
1812 mmc->max_hw_segs = 128;
1813 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001814 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001815 else /* PIO */
1816 mmc->max_hw_segs = 128;
1817 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001818
1819 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001820 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001821 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001822 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001823 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001824
1825 /*
1826 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001827 * of bytes. When doing hardware scatter/gather, each entry cannot
1828 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001829 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001830 if (host->flags & SDHCI_USE_ADMA)
1831 mmc->max_seg_size = 65536;
1832 else
1833 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001834
1835 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001836 * Maximum block size. This varies from controller to controller and
1837 * is specified in the capabilities register.
1838 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001839 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1840 mmc->max_blk_size = 2;
1841 } else {
1842 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1843 SDHCI_MAX_BLOCK_SHIFT;
1844 if (mmc->max_blk_size >= 3) {
1845 printk(KERN_WARNING "%s: Invalid maximum block size, "
1846 "assuming 512 bytes\n", mmc_hostname(mmc));
1847 mmc->max_blk_size = 0;
1848 }
1849 }
1850
1851 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001852
1853 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001854 * Maximum block count.
1855 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001856 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001857
1858 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001859 * Init tasklets.
1860 */
1861 tasklet_init(&host->card_tasklet,
1862 sdhci_tasklet_card, (unsigned long)host);
1863 tasklet_init(&host->finish_tasklet,
1864 sdhci_tasklet_finish, (unsigned long)host);
1865
Al Viroe4cad1b2006-10-10 22:47:07 +01001866 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001867
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001868 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001869 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001870 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001871 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001872
1873 sdhci_init(host);
1874
1875#ifdef CONFIG_MMC_DEBUG
1876 sdhci_dumpregs(host);
1877#endif
1878
Pierre Ossmanf9134312008-12-21 17:01:48 +01001879#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001880 snprintf(host->led_name, sizeof(host->led_name),
1881 "%s::", mmc_hostname(mmc));
1882 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001883 host->led.brightness = LED_OFF;
1884 host->led.default_trigger = mmc_hostname(mmc);
1885 host->led.brightness_set = sdhci_led_control;
1886
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001887 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001888 if (ret)
1889 goto reset;
1890#endif
1891
Pierre Ossman5f25a662006-10-04 02:15:39 -07001892 mmiowb();
1893
Pierre Ossmand129bce2006-03-24 03:18:17 -08001894 mmc_add_host(mmc);
1895
Pierre Ossman2134a922008-06-28 18:28:51 +02001896 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001897 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Pierre Ossman2134a922008-06-28 18:28:51 +02001898 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001899 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1900
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001901 sdhci_enable_card_detection(host);
1902
Pierre Ossmand129bce2006-03-24 03:18:17 -08001903 return 0;
1904
Pierre Ossmanf9134312008-12-21 17:01:48 +01001905#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001906reset:
1907 sdhci_reset(host, SDHCI_RESET_ALL);
1908 free_irq(host->irq, host);
1909#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001910untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001911 tasklet_kill(&host->card_tasklet);
1912 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913
1914 return ret;
1915}
1916
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001917EXPORT_SYMBOL_GPL(sdhci_add_host);
1918
Pierre Ossman1e728592008-04-16 19:13:13 +02001919void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001920{
Pierre Ossman1e728592008-04-16 19:13:13 +02001921 unsigned long flags;
1922
1923 if (dead) {
1924 spin_lock_irqsave(&host->lock, flags);
1925
1926 host->flags |= SDHCI_DEVICE_DEAD;
1927
1928 if (host->mrq) {
1929 printk(KERN_ERR "%s: Controller removed during "
1930 " transfer!\n", mmc_hostname(host->mmc));
1931
1932 host->mrq->cmd->error = -ENOMEDIUM;
1933 tasklet_schedule(&host->finish_tasklet);
1934 }
1935
1936 spin_unlock_irqrestore(&host->lock, flags);
1937 }
1938
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001939 sdhci_disable_card_detection(host);
1940
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001941 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001942
Pierre Ossmanf9134312008-12-21 17:01:48 +01001943#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001944 led_classdev_unregister(&host->led);
1945#endif
1946
Pierre Ossman1e728592008-04-16 19:13:13 +02001947 if (!dead)
1948 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001949
1950 free_irq(host->irq, host);
1951
1952 del_timer_sync(&host->timer);
1953
1954 tasklet_kill(&host->card_tasklet);
1955 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001956
1957 kfree(host->adma_desc);
1958 kfree(host->align_buffer);
1959
1960 host->adma_desc = NULL;
1961 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001962}
1963
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001964EXPORT_SYMBOL_GPL(sdhci_remove_host);
1965
1966void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001968 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001969}
1970
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001971EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972
1973/*****************************************************************************\
1974 * *
1975 * Driver init/exit *
1976 * *
1977\*****************************************************************************/
1978
1979static int __init sdhci_drv_init(void)
1980{
1981 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001982 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001983 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1984
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001985 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001986}
1987
1988static void __exit sdhci_drv_exit(void)
1989{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001990}
1991
1992module_init(sdhci_drv_init);
1993module_exit(sdhci_drv_exit);
1994
Pierre Ossmandf673b22006-06-30 02:22:31 -07001995module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001996
Pierre Ossman32710e82009-04-08 20:14:54 +02001997MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001998MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001999MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002000
Pierre Ossmandf673b22006-06-30 02:22:31 -07002001MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");