blob: fcdd0078503374226b09a09b393f4f7c8930c96b [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
Ben Dooks118cd172010-03-05 13:43:26 -0800379static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
380{
381 desc[7] = (addr >> 24) & 0xff;
382 desc[6] = (addr >> 16) & 0xff;
383 desc[5] = (addr >> 8) & 0xff;
384 desc[4] = (addr >> 0) & 0xff;
385
386 desc[3] = (len >> 8) & 0xff;
387 desc[2] = (len >> 0) & 0xff;
388
389 desc[0] = cmd & 0xff;
390 desc[1] = cmd >> 8;
391}
392
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200393static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200394 struct mmc_data *data)
395{
396 int direction;
397
398 u8 *desc;
399 u8 *align;
400 dma_addr_t addr;
401 dma_addr_t align_addr;
402 int len, offset;
403
404 struct scatterlist *sg;
405 int i;
406 char *buffer;
407 unsigned long flags;
408
409 /*
410 * The spec does not specify endianness of descriptor table.
411 * We currently guess that it is LE.
412 */
413
414 if (data->flags & MMC_DATA_READ)
415 direction = DMA_FROM_DEVICE;
416 else
417 direction = DMA_TO_DEVICE;
418
419 /*
420 * The ADMA descriptor table is mapped further down as we
421 * need to fill it with data first.
422 */
423
424 host->align_addr = dma_map_single(mmc_dev(host->mmc),
425 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700426 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200427 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200428 BUG_ON(host->align_addr & 0x3);
429
430 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
431 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200432 if (host->sg_count == 0)
433 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200434
435 desc = host->adma_desc;
436 align = host->align_buffer;
437
438 align_addr = host->align_addr;
439
440 for_each_sg(data->sg, sg, host->sg_count, i) {
441 addr = sg_dma_address(sg);
442 len = sg_dma_len(sg);
443
444 /*
445 * The SDHCI specification states that ADMA
446 * addresses must be 32-bit aligned. If they
447 * aren't, then we use a bounce buffer for
448 * the (up to three) bytes that screw up the
449 * alignment.
450 */
451 offset = (4 - (addr & 0x3)) & 0x3;
452 if (offset) {
453 if (data->flags & MMC_DATA_WRITE) {
454 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200455 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200456 memcpy(align, buffer, offset);
457 sdhci_kunmap_atomic(buffer, &flags);
458 }
459
Ben Dooks118cd172010-03-05 13:43:26 -0800460 /* tran, valid */
461 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200462
463 BUG_ON(offset > 65536);
464
Pierre Ossman2134a922008-06-28 18:28:51 +0200465 align += 4;
466 align_addr += 4;
467
468 desc += 8;
469
470 addr += offset;
471 len -= offset;
472 }
473
Pierre Ossman2134a922008-06-28 18:28:51 +0200474 BUG_ON(len > 65536);
475
Ben Dooks118cd172010-03-05 13:43:26 -0800476 /* tran, valid */
477 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200478 desc += 8;
479
480 /*
481 * If this triggers then we have a calculation bug
482 * somewhere. :/
483 */
484 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
485 }
486
487 /*
488 * Add a terminating entry.
489 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200490
Ben Dooks118cd172010-03-05 13:43:26 -0800491 /* nop, end, valid */
492 sdhci_set_adma_desc(desc, 0, 0, 0x3);
Pierre Ossman2134a922008-06-28 18:28:51 +0200493
494 /*
495 * Resync align buffer as we might have changed it.
496 */
497 if (data->flags & MMC_DATA_WRITE) {
498 dma_sync_single_for_device(mmc_dev(host->mmc),
499 host->align_addr, 128 * 4, direction);
500 }
501
502 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
503 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200504 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200505 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200507
508 return 0;
509
510unmap_entries:
511 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
512 data->sg_len, direction);
513unmap_align:
514 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
515 128 * 4, direction);
516fail:
517 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200518}
519
520static void sdhci_adma_table_post(struct sdhci_host *host,
521 struct mmc_data *data)
522{
523 int direction;
524
525 struct scatterlist *sg;
526 int i, size;
527 u8 *align;
528 char *buffer;
529 unsigned long flags;
530
531 if (data->flags & MMC_DATA_READ)
532 direction = DMA_FROM_DEVICE;
533 else
534 direction = DMA_TO_DEVICE;
535
536 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
537 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
538
539 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
540 128 * 4, direction);
541
542 if (data->flags & MMC_DATA_READ) {
543 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
544 data->sg_len, direction);
545
546 align = host->align_buffer;
547
548 for_each_sg(data->sg, sg, host->sg_count, i) {
549 if (sg_dma_address(sg) & 0x3) {
550 size = 4 - (sg_dma_address(sg) & 0x3);
551
552 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200553 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 memcpy(buffer, align, size);
555 sdhci_kunmap_atomic(buffer, &flags);
556
557 align += 4;
558 }
559 }
560 }
561
562 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
563 data->sg_len, direction);
564}
565
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200566static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800567{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700568 u8 count;
569 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800570
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200571 /*
572 * If the host controller provides us with an incorrect timeout
573 * value, just skip the check and use 0xE. The hardware may take
574 * longer to time out, but that's much better than having a too-short
575 * timeout value.
576 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200577 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200578 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200579
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700580 /* timeout in us */
581 target_timeout = data->timeout_ns / 1000 +
582 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800583
Anton Vorontsov81b39802009-09-22 16:45:13 -0700584 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
585 host->timeout_clk = host->clock / 1000;
586
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700587 /*
588 * Figure out needed cycles.
589 * We do this in steps in order to fit inside a 32 bit int.
590 * The first step is the minimum timeout, which will have a
591 * minimum resolution of 6 bits:
592 * (1) 2^13*1000 > 2^22,
593 * (2) host->timeout_clk < 2^16
594 * =>
595 * (1) / (2) > 2^6
596 */
597 count = 0;
598 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
599 while (current_timeout < target_timeout) {
600 count++;
601 current_timeout <<= 1;
602 if (count >= 0xF)
603 break;
604 }
605
606 if (count >= 0xF) {
607 printk(KERN_WARNING "%s: Too large timeout requested!\n",
608 mmc_hostname(host->mmc));
609 count = 0xE;
610 }
611
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200612 return count;
613}
614
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300615static void sdhci_set_transfer_irqs(struct sdhci_host *host)
616{
617 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
618 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
619
620 if (host->flags & SDHCI_REQ_USE_DMA)
621 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
622 else
623 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
624}
625
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200626static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
627{
628 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200629 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200630 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200631
632 WARN_ON(host->data);
633
634 if (data == NULL)
635 return;
636
637 /* Sanity checks */
638 BUG_ON(data->blksz * data->blocks > 524288);
639 BUG_ON(data->blksz > host->mmc->max_blk_size);
640 BUG_ON(data->blocks > 65535);
641
642 host->data = data;
643 host->data_early = 0;
644
645 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300646 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800647
Richard Röjforsa13abc72009-09-22 16:45:30 -0700648 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100649 host->flags |= SDHCI_REQ_USE_DMA;
650
Pierre Ossman2134a922008-06-28 18:28:51 +0200651 /*
652 * FIXME: This doesn't account for merging when mapping the
653 * scatterlist.
654 */
655 if (host->flags & SDHCI_REQ_USE_DMA) {
656 int broken, i;
657 struct scatterlist *sg;
658
659 broken = 0;
660 if (host->flags & SDHCI_USE_ADMA) {
661 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
662 broken = 1;
663 } else {
664 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
665 broken = 1;
666 }
667
668 if (unlikely(broken)) {
669 for_each_sg(data->sg, sg, data->sg_len, i) {
670 if (sg->length & 0x3) {
671 DBG("Reverting to PIO because of "
672 "transfer size (%d)\n",
673 sg->length);
674 host->flags &= ~SDHCI_REQ_USE_DMA;
675 break;
676 }
677 }
678 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100679 }
680
681 /*
682 * The assumption here being that alignment is the same after
683 * translation to device address space.
684 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200685 if (host->flags & SDHCI_REQ_USE_DMA) {
686 int broken, i;
687 struct scatterlist *sg;
688
689 broken = 0;
690 if (host->flags & SDHCI_USE_ADMA) {
691 /*
692 * As we use 3 byte chunks to work around
693 * alignment problems, we need to check this
694 * quirk.
695 */
696 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
697 broken = 1;
698 } else {
699 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
700 broken = 1;
701 }
702
703 if (unlikely(broken)) {
704 for_each_sg(data->sg, sg, data->sg_len, i) {
705 if (sg->offset & 0x3) {
706 DBG("Reverting to PIO because of "
707 "bad alignment\n");
708 host->flags &= ~SDHCI_REQ_USE_DMA;
709 break;
710 }
711 }
712 }
713 }
714
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200715 if (host->flags & SDHCI_REQ_USE_DMA) {
716 if (host->flags & SDHCI_USE_ADMA) {
717 ret = sdhci_adma_table_pre(host, data);
718 if (ret) {
719 /*
720 * This only happens when someone fed
721 * us an invalid request.
722 */
723 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200724 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200725 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300726 sdhci_writel(host, host->adma_addr,
727 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200728 }
729 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300730 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200731
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300732 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200733 data->sg, data->sg_len,
734 (data->flags & MMC_DATA_READ) ?
735 DMA_FROM_DEVICE :
736 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300737 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200738 /*
739 * This only happens when someone fed
740 * us an invalid request.
741 */
742 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200743 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200744 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200745 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300746 sdhci_writel(host, sg_dma_address(data->sg),
747 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200748 }
749 }
750 }
751
Pierre Ossman2134a922008-06-28 18:28:51 +0200752 /*
753 * Always adjust the DMA selection as some controllers
754 * (e.g. JMicron) can't do PIO properly when the selection
755 * is ADMA.
756 */
757 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300758 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200759 ctrl &= ~SDHCI_CTRL_DMA_MASK;
760 if ((host->flags & SDHCI_REQ_USE_DMA) &&
761 (host->flags & SDHCI_USE_ADMA))
762 ctrl |= SDHCI_CTRL_ADMA32;
763 else
764 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300765 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100766 }
767
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200768 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200769 int flags;
770
771 flags = SG_MITER_ATOMIC;
772 if (host->data->flags & MMC_DATA_READ)
773 flags |= SG_MITER_TO_SG;
774 else
775 flags |= SG_MITER_FROM_SG;
776 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200777 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800778 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700779
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300780 sdhci_set_transfer_irqs(host);
781
Pierre Ossmanbab76962006-07-02 16:51:35 +0100782 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300783 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
784 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700785}
786
787static void sdhci_set_transfer_mode(struct sdhci_host *host,
788 struct mmc_data *data)
789{
790 u16 mode;
791
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700792 if (data == NULL)
793 return;
794
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200795 WARN_ON(!host->data);
796
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700797 mode = SDHCI_TRNS_BLK_CNT_EN;
798 if (data->blocks > 1)
799 mode |= SDHCI_TRNS_MULTI;
800 if (data->flags & MMC_DATA_READ)
801 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100802 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700803 mode |= SDHCI_TRNS_DMA;
804
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300805 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800806}
807
808static void sdhci_finish_data(struct sdhci_host *host)
809{
810 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800811
812 BUG_ON(!host->data);
813
814 data = host->data;
815 host->data = NULL;
816
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100817 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200818 if (host->flags & SDHCI_USE_ADMA)
819 sdhci_adma_table_post(host, data);
820 else {
821 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
822 data->sg_len, (data->flags & MMC_DATA_READ) ?
823 DMA_FROM_DEVICE : DMA_TO_DEVICE);
824 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800825 }
826
827 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200828 * The specification states that the block count register must
829 * be updated, but it does not specify at what point in the
830 * data flow. That makes the register entirely useless to read
831 * back so we have to assume that nothing made it to the card
832 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800833 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200834 if (data->error)
835 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800836 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200837 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800838
Pierre Ossmand129bce2006-03-24 03:18:17 -0800839 if (data->stop) {
840 /*
841 * The controller needs a reset of internal state machines
842 * upon error conditions.
843 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200844 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800845 sdhci_reset(host, SDHCI_RESET_CMD);
846 sdhci_reset(host, SDHCI_RESET_DATA);
847 }
848
849 sdhci_send_command(host, data->stop);
850 } else
851 tasklet_schedule(&host->finish_tasklet);
852}
853
854static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
855{
856 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700857 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700858 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800859
860 WARN_ON(host->cmd);
861
Pierre Ossmand129bce2006-03-24 03:18:17 -0800862 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700863 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700864
865 mask = SDHCI_CMD_INHIBIT;
866 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
867 mask |= SDHCI_DATA_INHIBIT;
868
869 /* We shouldn't wait for data inihibit for stop commands, even
870 though they might use busy signaling */
871 if (host->mrq->data && (cmd == host->mrq->data->stop))
872 mask &= ~SDHCI_DATA_INHIBIT;
873
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300874 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700875 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800876 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100877 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800878 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200879 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880 tasklet_schedule(&host->finish_tasklet);
881 return;
882 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700883 timeout--;
884 mdelay(1);
885 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800886
887 mod_timer(&host->timer, jiffies + 10 * HZ);
888
889 host->cmd = cmd;
890
891 sdhci_prepare_data(host, cmd->data);
892
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300893 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800894
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895 sdhci_set_transfer_mode(host, cmd->data);
896
Pierre Ossmand129bce2006-03-24 03:18:17 -0800897 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100898 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800899 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200900 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800901 tasklet_schedule(&host->finish_tasklet);
902 return;
903 }
904
905 if (!(cmd->flags & MMC_RSP_PRESENT))
906 flags = SDHCI_CMD_RESP_NONE;
907 else if (cmd->flags & MMC_RSP_136)
908 flags = SDHCI_CMD_RESP_LONG;
909 else if (cmd->flags & MMC_RSP_BUSY)
910 flags = SDHCI_CMD_RESP_SHORT_BUSY;
911 else
912 flags = SDHCI_CMD_RESP_SHORT;
913
914 if (cmd->flags & MMC_RSP_CRC)
915 flags |= SDHCI_CMD_CRC;
916 if (cmd->flags & MMC_RSP_OPCODE)
917 flags |= SDHCI_CMD_INDEX;
918 if (cmd->data)
919 flags |= SDHCI_CMD_DATA;
920
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300921 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922}
923
924static void sdhci_finish_command(struct sdhci_host *host)
925{
926 int i;
927
928 BUG_ON(host->cmd == NULL);
929
930 if (host->cmd->flags & MMC_RSP_PRESENT) {
931 if (host->cmd->flags & MMC_RSP_136) {
932 /* CRC is stripped so we need to do some shifting. */
933 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300934 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 SDHCI_RESPONSE + (3-i)*4) << 8;
936 if (i != 3)
937 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300938 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 SDHCI_RESPONSE + (3-i)*4-1);
940 }
941 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300942 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 }
944 }
945
Pierre Ossman17b04292007-07-22 22:18:46 +0200946 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200948 if (host->data && host->data_early)
949 sdhci_finish_data(host);
950
951 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 tasklet_schedule(&host->finish_tasklet);
953
954 host->cmd = NULL;
955}
956
957static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
958{
959 int div;
960 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700961 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962
963 if (clock == host->clock)
964 return;
965
Anton Vorontsov81146342009-03-17 00:13:59 +0300966 if (host->ops->set_clock) {
967 host->ops->set_clock(host, clock);
968 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
969 return;
970 }
971
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300972 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973
974 if (clock == 0)
975 goto out;
976
977 for (div = 1;div < 256;div *= 2) {
978 if ((host->max_clk / div) <= clock)
979 break;
980 }
981 div >>= 1;
982
983 clk = div << SDHCI_DIVIDER_SHIFT;
984 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300985 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986
Chris Ball27f6cb12009-09-22 16:45:31 -0700987 /* Wait max 20 ms */
988 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300989 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700990 & SDHCI_CLOCK_INT_STABLE)) {
991 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100992 printk(KERN_ERR "%s: Internal clock never "
993 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 sdhci_dumpregs(host);
995 return;
996 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700997 timeout--;
998 mdelay(1);
999 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
1001 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001002 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
1004out:
1005 host->clock = clock;
1006}
1007
Pierre Ossman146ad662006-06-30 02:22:23 -07001008static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1009{
1010 u8 pwr;
1011
Pierre Ossmanae628902009-05-03 20:45:03 +02001012 if (power == (unsigned short)-1)
1013 pwr = 0;
1014 else {
1015 switch (1 << power) {
1016 case MMC_VDD_165_195:
1017 pwr = SDHCI_POWER_180;
1018 break;
1019 case MMC_VDD_29_30:
1020 case MMC_VDD_30_31:
1021 pwr = SDHCI_POWER_300;
1022 break;
1023 case MMC_VDD_32_33:
1024 case MMC_VDD_33_34:
1025 pwr = SDHCI_POWER_330;
1026 break;
1027 default:
1028 BUG();
1029 }
1030 }
1031
1032 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001033 return;
1034
Pierre Ossmanae628902009-05-03 20:45:03 +02001035 host->pwr = pwr;
1036
1037 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001038 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001039 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001040 }
1041
1042 /*
1043 * Spec says that we should clear the power reg before setting
1044 * a new value. Some controllers don't seem to like this though.
1045 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001046 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001047 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001048
Andres Salomone08c1692008-07-04 10:00:03 -07001049 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001050 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001051 * and set turn on power at the same time, so set the voltage first.
1052 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001053 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001054 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1055
1056 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001057
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001059
1060 /*
1061 * Some controllers need an extra 10ms delay of 10ms before they
1062 * can apply clock after applying power
1063 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001064 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001065 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001066}
1067
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068/*****************************************************************************\
1069 * *
1070 * MMC callbacks *
1071 * *
1072\*****************************************************************************/
1073
1074static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1075{
1076 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001077 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078 unsigned long flags;
1079
1080 host = mmc_priv(mmc);
1081
1082 spin_lock_irqsave(&host->lock, flags);
1083
1084 WARN_ON(host->mrq != NULL);
1085
Pierre Ossmanf9134312008-12-21 17:01:48 +01001086#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001088#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089
1090 host->mrq = mrq;
1091
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001092 /* If polling, assume that the card is always present. */
1093 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1094 present = true;
1095 else
1096 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1097 SDHCI_CARD_PRESENT;
1098
1099 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001100 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 tasklet_schedule(&host->finish_tasklet);
1102 } else
1103 sdhci_send_command(host, mrq->cmd);
1104
Pierre Ossman5f25a662006-10-04 02:15:39 -07001105 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001106 spin_unlock_irqrestore(&host->lock, flags);
1107}
1108
1109static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1110{
1111 struct sdhci_host *host;
1112 unsigned long flags;
1113 u8 ctrl;
1114
1115 host = mmc_priv(mmc);
1116
1117 spin_lock_irqsave(&host->lock, flags);
1118
Pierre Ossman1e728592008-04-16 19:13:13 +02001119 if (host->flags & SDHCI_DEVICE_DEAD)
1120 goto out;
1121
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122 /*
1123 * Reset the chip on each power off.
1124 * Should clear out any weird states.
1125 */
1126 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001127 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001128 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129 }
1130
1131 sdhci_set_clock(host, ios->clock);
1132
1133 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001134 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001136 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001138 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001139
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 if (ios->bus_width == MMC_BUS_WIDTH_4)
1141 ctrl |= SDHCI_CTRL_4BITBUS;
1142 else
1143 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001144
1145 if (ios->timing == MMC_TIMING_SD_HS)
1146 ctrl |= SDHCI_CTRL_HISPD;
1147 else
1148 ctrl &= ~SDHCI_CTRL_HISPD;
1149
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001150 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151
Leandro Dorileob8352262007-07-25 23:47:04 +02001152 /*
1153 * Some (ENE) controllers go apeshit on some ios operation,
1154 * signalling timeout and CRC errors even on CMD0. Resetting
1155 * it on each ios seems to solve the problem.
1156 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001157 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001158 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1159
Pierre Ossman1e728592008-04-16 19:13:13 +02001160out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001161 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 spin_unlock_irqrestore(&host->lock, flags);
1163}
1164
1165static int sdhci_get_ro(struct mmc_host *mmc)
1166{
1167 struct sdhci_host *host;
1168 unsigned long flags;
1169 int present;
1170
1171 host = mmc_priv(mmc);
1172
1173 spin_lock_irqsave(&host->lock, flags);
1174
Pierre Ossman1e728592008-04-16 19:13:13 +02001175 if (host->flags & SDHCI_DEVICE_DEAD)
1176 present = 0;
1177 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001178 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001179
1180 spin_unlock_irqrestore(&host->lock, flags);
1181
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001182 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1183 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184 return !(present & SDHCI_WRITE_PROTECT);
1185}
1186
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001187static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1188{
1189 struct sdhci_host *host;
1190 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001191
1192 host = mmc_priv(mmc);
1193
1194 spin_lock_irqsave(&host->lock, flags);
1195
Pierre Ossman1e728592008-04-16 19:13:13 +02001196 if (host->flags & SDHCI_DEVICE_DEAD)
1197 goto out;
1198
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001199 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001200 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1201 else
1202 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001203out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001204 mmiowb();
1205
1206 spin_unlock_irqrestore(&host->lock, flags);
1207}
1208
David Brownellab7aefd2006-11-12 17:55:30 -08001209static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001210 .request = sdhci_request,
1211 .set_ios = sdhci_set_ios,
1212 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001213 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214};
1215
1216/*****************************************************************************\
1217 * *
1218 * Tasklets *
1219 * *
1220\*****************************************************************************/
1221
1222static void sdhci_tasklet_card(unsigned long param)
1223{
1224 struct sdhci_host *host;
1225 unsigned long flags;
1226
1227 host = (struct sdhci_host*)param;
1228
1229 spin_lock_irqsave(&host->lock, flags);
1230
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001231 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232 if (host->mrq) {
1233 printk(KERN_ERR "%s: Card removed during transfer!\n",
1234 mmc_hostname(host->mmc));
1235 printk(KERN_ERR "%s: Resetting controller.\n",
1236 mmc_hostname(host->mmc));
1237
1238 sdhci_reset(host, SDHCI_RESET_CMD);
1239 sdhci_reset(host, SDHCI_RESET_DATA);
1240
Pierre Ossman17b04292007-07-22 22:18:46 +02001241 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 tasklet_schedule(&host->finish_tasklet);
1243 }
1244 }
1245
1246 spin_unlock_irqrestore(&host->lock, flags);
1247
Pierre Ossman04cf5852008-08-18 22:18:14 +02001248 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249}
1250
1251static void sdhci_tasklet_finish(unsigned long param)
1252{
1253 struct sdhci_host *host;
1254 unsigned long flags;
1255 struct mmc_request *mrq;
1256
1257 host = (struct sdhci_host*)param;
1258
1259 spin_lock_irqsave(&host->lock, flags);
1260
1261 del_timer(&host->timer);
1262
1263 mrq = host->mrq;
1264
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265 /*
1266 * The controller needs a reset of internal state machines
1267 * upon error conditions.
1268 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001269 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1270 (mrq->cmd->error ||
1271 (mrq->data && (mrq->data->error ||
1272 (mrq->data->stop && mrq->data->stop->error))) ||
1273 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001274
1275 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001276 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001277 unsigned int clock;
1278
1279 /* This is to force an update */
1280 clock = host->clock;
1281 host->clock = 0;
1282 sdhci_set_clock(host, clock);
1283 }
1284
1285 /* Spec says we should do both at the same time, but Ricoh
1286 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287 sdhci_reset(host, SDHCI_RESET_CMD);
1288 sdhci_reset(host, SDHCI_RESET_DATA);
1289 }
1290
1291 host->mrq = NULL;
1292 host->cmd = NULL;
1293 host->data = NULL;
1294
Pierre Ossmanf9134312008-12-21 17:01:48 +01001295#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001297#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001298
Pierre Ossman5f25a662006-10-04 02:15:39 -07001299 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001300 spin_unlock_irqrestore(&host->lock, flags);
1301
1302 mmc_request_done(host->mmc, mrq);
1303}
1304
1305static void sdhci_timeout_timer(unsigned long data)
1306{
1307 struct sdhci_host *host;
1308 unsigned long flags;
1309
1310 host = (struct sdhci_host*)data;
1311
1312 spin_lock_irqsave(&host->lock, flags);
1313
1314 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001315 printk(KERN_ERR "%s: Timeout waiting for hardware "
1316 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001317 sdhci_dumpregs(host);
1318
1319 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001320 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001321 sdhci_finish_data(host);
1322 } else {
1323 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001324 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001326 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327
1328 tasklet_schedule(&host->finish_tasklet);
1329 }
1330 }
1331
Pierre Ossman5f25a662006-10-04 02:15:39 -07001332 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333 spin_unlock_irqrestore(&host->lock, flags);
1334}
1335
1336/*****************************************************************************\
1337 * *
1338 * Interrupt handling *
1339 * *
1340\*****************************************************************************/
1341
1342static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1343{
1344 BUG_ON(intmask == 0);
1345
1346 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001347 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1348 "though no command operation was in progress.\n",
1349 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350 sdhci_dumpregs(host);
1351 return;
1352 }
1353
Pierre Ossman43b58b32007-07-25 23:15:27 +02001354 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001355 host->cmd->error = -ETIMEDOUT;
1356 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1357 SDHCI_INT_INDEX))
1358 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359
Pierre Ossmane8095172008-07-25 01:09:08 +02001360 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001362 return;
1363 }
1364
1365 /*
1366 * The host can send and interrupt when the busy state has
1367 * ended, allowing us to wait without wasting CPU cycles.
1368 * Unfortunately this is overloaded on the "data complete"
1369 * interrupt, so we need to take some care when handling
1370 * it.
1371 *
1372 * Note: The 1.0 specification is a bit ambiguous about this
1373 * feature so there might be some problems with older
1374 * controllers.
1375 */
1376 if (host->cmd->flags & MMC_RSP_BUSY) {
1377 if (host->cmd->data)
1378 DBG("Cannot wait for busy signal when also "
1379 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001380 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001381 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001382
1383 /* The controller does not support the end-of-busy IRQ,
1384 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001385 }
1386
1387 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001388 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389}
1390
Ben Dooks6882a8c2009-06-14 13:52:38 +01001391#ifdef DEBUG
1392static void sdhci_show_adma_error(struct sdhci_host *host)
1393{
1394 const char *name = mmc_hostname(host->mmc);
1395 u8 *desc = host->adma_desc;
1396 __le32 *dma;
1397 __le16 *len;
1398 u8 attr;
1399
1400 sdhci_dumpregs(host);
1401
1402 while (true) {
1403 dma = (__le32 *)(desc + 4);
1404 len = (__le16 *)(desc + 2);
1405 attr = *desc;
1406
1407 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1408 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1409
1410 desc += 8;
1411
1412 if (attr & 2)
1413 break;
1414 }
1415}
1416#else
1417static void sdhci_show_adma_error(struct sdhci_host *host) { }
1418#endif
1419
Pierre Ossmand129bce2006-03-24 03:18:17 -08001420static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1421{
1422 BUG_ON(intmask == 0);
1423
1424 if (!host->data) {
1425 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001426 * The "data complete" interrupt is also used to
1427 * indicate that a busy state has ended. See comment
1428 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001430 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1431 if (intmask & SDHCI_INT_DATA_END) {
1432 sdhci_finish_command(host);
1433 return;
1434 }
1435 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001436
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001437 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1438 "though no data operation was in progress.\n",
1439 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440 sdhci_dumpregs(host);
1441
1442 return;
1443 }
1444
1445 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001446 host->data->error = -ETIMEDOUT;
1447 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1448 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001449 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1450 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1451 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001452 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001453 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454
Pierre Ossman17b04292007-07-22 22:18:46 +02001455 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456 sdhci_finish_data(host);
1457 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001458 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001459 sdhci_transfer_pio(host);
1460
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001461 /*
1462 * We currently don't do anything fancy with DMA
1463 * boundaries, but as we can't disable the feature
1464 * we need to at least restart the transfer.
1465 */
1466 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001467 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1468 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001469
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001470 if (intmask & SDHCI_INT_DATA_END) {
1471 if (host->cmd) {
1472 /*
1473 * Data managed to finish before the
1474 * command completed. Make sure we do
1475 * things in the proper order.
1476 */
1477 host->data_early = 1;
1478 } else {
1479 sdhci_finish_data(host);
1480 }
1481 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482 }
1483}
1484
David Howells7d12e782006-10-05 14:55:46 +01001485static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486{
1487 irqreturn_t result;
1488 struct sdhci_host* host = dev_id;
1489 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001490 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491
1492 spin_lock(&host->lock);
1493
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001494 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495
Mark Lord62df67a52007-03-06 13:30:13 +01001496 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497 result = IRQ_NONE;
1498 goto out;
1499 }
1500
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001501 DBG("*** %s got interrupt: 0x%08x\n",
1502 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503
Pierre Ossman3192a282006-06-30 02:22:26 -07001504 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001505 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1506 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001508 }
1509
1510 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511
1512 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001513 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1514 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001515 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001516 }
1517
1518 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001519 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1520 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001521 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522 }
1523
1524 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1525
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001526 intmask &= ~SDHCI_INT_ERROR;
1527
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001529 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001531 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001532 }
1533
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001534 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001535
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001536 if (intmask & SDHCI_INT_CARD_INT)
1537 cardint = 1;
1538
1539 intmask &= ~SDHCI_INT_CARD_INT;
1540
Pierre Ossman3192a282006-06-30 02:22:26 -07001541 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001542 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001543 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001544 sdhci_dumpregs(host);
1545
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001546 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001547 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001548
1549 result = IRQ_HANDLED;
1550
Pierre Ossman5f25a662006-10-04 02:15:39 -07001551 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001552out:
1553 spin_unlock(&host->lock);
1554
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001555 /*
1556 * We have to delay this as it calls back into the driver.
1557 */
1558 if (cardint)
1559 mmc_signal_sdio_irq(host->mmc);
1560
Pierre Ossmand129bce2006-03-24 03:18:17 -08001561 return result;
1562}
1563
1564/*****************************************************************************\
1565 * *
1566 * Suspend/resume *
1567 * *
1568\*****************************************************************************/
1569
1570#ifdef CONFIG_PM
1571
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001572int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001573{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001574 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001575
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001576 sdhci_disable_card_detection(host);
1577
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001578 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001579 if (ret)
1580 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001581
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001582 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583
1584 return 0;
1585}
1586
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001587EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001588
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001589int sdhci_resume_host(struct sdhci_host *host)
1590{
1591 int ret;
1592
Richard Röjforsa13abc72009-09-22 16:45:30 -07001593 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001594 if (host->ops->enable_dma)
1595 host->ops->enable_dma(host);
1596 }
1597
1598 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1599 mmc_hostname(host->mmc), host);
1600 if (ret)
1601 return ret;
1602
1603 sdhci_init(host);
1604 mmiowb();
1605
1606 ret = mmc_resume_host(host->mmc);
1607 if (ret)
1608 return ret;
1609
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001610 sdhci_enable_card_detection(host);
1611
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001612 return 0;
1613}
1614
1615EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616
1617#endif /* CONFIG_PM */
1618
1619/*****************************************************************************\
1620 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001621 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001622 * *
1623\*****************************************************************************/
1624
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001625struct sdhci_host *sdhci_alloc_host(struct device *dev,
1626 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628 struct mmc_host *mmc;
1629 struct sdhci_host *host;
1630
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001631 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001633 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001635 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636
1637 host = mmc_priv(mmc);
1638 host->mmc = mmc;
1639
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001640 return host;
1641}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001642
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001643EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001645int sdhci_add_host(struct sdhci_host *host)
1646{
1647 struct mmc_host *mmc;
1648 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001649 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001651 WARN_ON(host == NULL);
1652 if (host == NULL)
1653 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001654
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001655 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001656
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001657 if (debug_quirks)
1658 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001659
Pierre Ossmand96649e2006-06-30 02:22:30 -07001660 sdhci_reset(host, SDHCI_RESET_ALL);
1661
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001662 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001663 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1664 >> SDHCI_SPEC_VER_SHIFT;
1665 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001666 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001667 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001668 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001669 }
1670
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001671 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001673 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001674 host->flags |= SDHCI_USE_SDMA;
1675 else if (!(caps & SDHCI_CAN_DO_SDMA))
1676 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001677 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001678 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001680 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001681 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001682 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001683 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001684 }
1685
Richard Röjforsa13abc72009-09-22 16:45:30 -07001686 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1687 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001688
1689 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1690 (host->flags & SDHCI_USE_ADMA)) {
1691 DBG("Disabling ADMA as it is marked broken\n");
1692 host->flags &= ~SDHCI_USE_ADMA;
1693 }
1694
Richard Röjforsa13abc72009-09-22 16:45:30 -07001695 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001696 if (host->ops->enable_dma) {
1697 if (host->ops->enable_dma(host)) {
1698 printk(KERN_WARNING "%s: No suitable DMA "
1699 "available. Falling back to PIO.\n",
1700 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001701 host->flags &=
1702 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001703 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001704 }
1705 }
1706
Pierre Ossman2134a922008-06-28 18:28:51 +02001707 if (host->flags & SDHCI_USE_ADMA) {
1708 /*
1709 * We need to allocate descriptors for all sg entries
1710 * (128) and potentially one alignment transfer for
1711 * each of those entries.
1712 */
1713 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1714 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1715 if (!host->adma_desc || !host->align_buffer) {
1716 kfree(host->adma_desc);
1717 kfree(host->align_buffer);
1718 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1719 "buffers. Falling back to standard DMA.\n",
1720 mmc_hostname(mmc));
1721 host->flags &= ~SDHCI_USE_ADMA;
1722 }
1723 }
1724
Pierre Ossman76591502008-07-21 00:32:11 +02001725 /*
1726 * If we use DMA, then it's up to the caller to set the DMA
1727 * mask, but PIO does not need the hw shim so we set a new
1728 * mask here in that case.
1729 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001730 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001731 host->dma_mask = DMA_BIT_MASK(64);
1732 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1733 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001734
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001735 host->max_clk =
1736 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001737 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001738 if (host->max_clk == 0) {
1739 if (!host->ops->get_max_clock) {
1740 printk(KERN_ERR
1741 "%s: Hardware doesn't specify base clock "
1742 "frequency.\n", mmc_hostname(mmc));
1743 return -ENODEV;
1744 }
1745 host->max_clk = host->ops->get_max_clock(host);
1746 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001747
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001748 host->timeout_clk =
1749 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1750 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001751 if (host->ops->get_timeout_clock) {
1752 host->timeout_clk = host->ops->get_timeout_clock(host);
1753 } else if (!(host->quirks &
1754 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001755 printk(KERN_ERR
1756 "%s: Hardware doesn't specify timeout clock "
1757 "frequency.\n", mmc_hostname(mmc));
1758 return -ENODEV;
1759 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001760 }
1761 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1762 host->timeout_clk *= 1000;
1763
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764 /*
1765 * Set host parameters.
1766 */
1767 mmc->ops = &sdhci_ops;
Anton Vorontsove9510172009-09-22 16:45:08 -07001768 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK &&
1769 host->ops->set_clock && host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001770 mmc->f_min = host->ops->get_min_clock(host);
1771 else
1772 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001773 mmc->f_max = host->max_clk;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001774 mmc->caps = MMC_CAP_SDIO_IRQ;
1775
1776 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1777 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001778
Pierre Ossman86a6a872009-02-02 21:13:49 +01001779 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001780 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1781
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001782 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1783 mmc->caps |= MMC_CAP_NEEDS_POLL;
1784
Pierre Ossman146ad662006-06-30 02:22:23 -07001785 mmc->ocr_avail = 0;
1786 if (caps & SDHCI_CAN_VDD_330)
1787 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001788 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001789 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001790 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001791 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001792
1793 if (mmc->ocr_avail == 0) {
1794 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001795 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001796 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001797 }
1798
Pierre Ossmand129bce2006-03-24 03:18:17 -08001799 spin_lock_init(&host->lock);
1800
1801 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001802 * Maximum number of segments. Depends on if the hardware
1803 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001804 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001805 if (host->flags & SDHCI_USE_ADMA)
1806 mmc->max_hw_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001807 else if (host->flags & SDHCI_USE_SDMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001808 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001809 else /* PIO */
1810 mmc->max_hw_segs = 128;
1811 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001812
1813 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001814 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001815 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001817 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001818
1819 /*
1820 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001821 * of bytes. When doing hardware scatter/gather, each entry cannot
1822 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001823 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001824 if (host->flags & SDHCI_USE_ADMA)
1825 mmc->max_seg_size = 65536;
1826 else
1827 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001828
1829 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001830 * Maximum block size. This varies from controller to controller and
1831 * is specified in the capabilities register.
1832 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001833 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1834 mmc->max_blk_size = 2;
1835 } else {
1836 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1837 SDHCI_MAX_BLOCK_SHIFT;
1838 if (mmc->max_blk_size >= 3) {
1839 printk(KERN_WARNING "%s: Invalid maximum block size, "
1840 "assuming 512 bytes\n", mmc_hostname(mmc));
1841 mmc->max_blk_size = 0;
1842 }
1843 }
1844
1845 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001846
1847 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001848 * Maximum block count.
1849 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001850 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001851
1852 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001853 * Init tasklets.
1854 */
1855 tasklet_init(&host->card_tasklet,
1856 sdhci_tasklet_card, (unsigned long)host);
1857 tasklet_init(&host->finish_tasklet,
1858 sdhci_tasklet_finish, (unsigned long)host);
1859
Al Viroe4cad1b2006-10-10 22:47:07 +01001860 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001861
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001862 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001863 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001864 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001865 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001866
1867 sdhci_init(host);
1868
1869#ifdef CONFIG_MMC_DEBUG
1870 sdhci_dumpregs(host);
1871#endif
1872
Pierre Ossmanf9134312008-12-21 17:01:48 +01001873#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001874 snprintf(host->led_name, sizeof(host->led_name),
1875 "%s::", mmc_hostname(mmc));
1876 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001877 host->led.brightness = LED_OFF;
1878 host->led.default_trigger = mmc_hostname(mmc);
1879 host->led.brightness_set = sdhci_led_control;
1880
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001881 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001882 if (ret)
1883 goto reset;
1884#endif
1885
Pierre Ossman5f25a662006-10-04 02:15:39 -07001886 mmiowb();
1887
Pierre Ossmand129bce2006-03-24 03:18:17 -08001888 mmc_add_host(mmc);
1889
Richard Röjforsa13abc72009-09-22 16:45:30 -07001890 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001891 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001892 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1893 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001894
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001895 sdhci_enable_card_detection(host);
1896
Pierre Ossmand129bce2006-03-24 03:18:17 -08001897 return 0;
1898
Pierre Ossmanf9134312008-12-21 17:01:48 +01001899#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001900reset:
1901 sdhci_reset(host, SDHCI_RESET_ALL);
1902 free_irq(host->irq, host);
1903#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001904untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001905 tasklet_kill(&host->card_tasklet);
1906 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001907
1908 return ret;
1909}
1910
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001911EXPORT_SYMBOL_GPL(sdhci_add_host);
1912
Pierre Ossman1e728592008-04-16 19:13:13 +02001913void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001914{
Pierre Ossman1e728592008-04-16 19:13:13 +02001915 unsigned long flags;
1916
1917 if (dead) {
1918 spin_lock_irqsave(&host->lock, flags);
1919
1920 host->flags |= SDHCI_DEVICE_DEAD;
1921
1922 if (host->mrq) {
1923 printk(KERN_ERR "%s: Controller removed during "
1924 " transfer!\n", mmc_hostname(host->mmc));
1925
1926 host->mrq->cmd->error = -ENOMEDIUM;
1927 tasklet_schedule(&host->finish_tasklet);
1928 }
1929
1930 spin_unlock_irqrestore(&host->lock, flags);
1931 }
1932
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001933 sdhci_disable_card_detection(host);
1934
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001935 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001936
Pierre Ossmanf9134312008-12-21 17:01:48 +01001937#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001938 led_classdev_unregister(&host->led);
1939#endif
1940
Pierre Ossman1e728592008-04-16 19:13:13 +02001941 if (!dead)
1942 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001943
1944 free_irq(host->irq, host);
1945
1946 del_timer_sync(&host->timer);
1947
1948 tasklet_kill(&host->card_tasklet);
1949 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001950
1951 kfree(host->adma_desc);
1952 kfree(host->align_buffer);
1953
1954 host->adma_desc = NULL;
1955 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001956}
1957
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001958EXPORT_SYMBOL_GPL(sdhci_remove_host);
1959
1960void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001961{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001962 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001963}
1964
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001965EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001966
1967/*****************************************************************************\
1968 * *
1969 * Driver init/exit *
1970 * *
1971\*****************************************************************************/
1972
1973static int __init sdhci_drv_init(void)
1974{
1975 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001976 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001977 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1978
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001979 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001980}
1981
1982static void __exit sdhci_drv_exit(void)
1983{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001984}
1985
1986module_init(sdhci_drv_init);
1987module_exit(sdhci_drv_exit);
1988
Pierre Ossmandf673b22006-06-30 02:22:31 -07001989module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001990
Pierre Ossman32710e82009-04-08 20:14:54 +02001991MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001992MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001993MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001994
Pierre Ossmandf673b22006-06-30 02:22:31 -07001995MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");