blob: 785512133b508a593149576d7a37a9988c8d2338 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020021#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070022#include <linux/regulator/consumer.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080023
Pierre Ossman2f730fe2008-03-17 10:29:38 +010024#include <linux/leds.h>
25
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossmand129bce2006-03-24 03:18:17 -080028#include "sdhci.h"
29
30#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080031
Pierre Ossmand129bce2006-03-24 03:18:17 -080032#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010033 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmanf9134312008-12-21 17:01:48 +010035#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
36 defined(CONFIG_MMC_SDHCI_MODULE))
37#define SDHCI_USE_LEDS_CLASS
38#endif
39
Pierre Ossmandf673b22006-06-30 02:22:31 -070040static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070041
Pierre Ossmand129bce2006-03-24 03:18:17 -080042static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
43static void sdhci_finish_data(struct sdhci_host *);
44
45static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46static void sdhci_finish_command(struct sdhci_host *);
47
48static void sdhci_dumpregs(struct sdhci_host *host)
49{
50 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
51
52 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030053 sdhci_readl(host, SDHCI_DMA_ADDRESS),
54 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080055 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030056 sdhci_readw(host, SDHCI_BLOCK_SIZE),
57 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080058 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030059 sdhci_readl(host, SDHCI_ARGUMENT),
60 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080061 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030062 sdhci_readl(host, SDHCI_PRESENT_STATE),
63 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080064 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030065 sdhci_readb(host, SDHCI_POWER_CONTROL),
66 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080067 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030068 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
69 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080070 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030071 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
72 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030074 sdhci_readl(host, SDHCI_INT_ENABLE),
75 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080076 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readw(host, SDHCI_ACMD12_ERR),
78 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080079 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readl(host, SDHCI_CAPABILITIES),
81 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080082
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010083 if (host->flags & SDHCI_USE_ADMA)
84 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
85 readl(host->ioaddr + SDHCI_ADMA_ERROR),
86 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
87
Pierre Ossmand129bce2006-03-24 03:18:17 -080088 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
89}
90
91/*****************************************************************************\
92 * *
93 * Low level functions *
94 * *
95\*****************************************************************************/
96
Anton Vorontsov7260cf52009-03-17 00:13:48 +030097static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
98{
99 u32 ier;
100
101 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
102 ier &= ~clear;
103 ier |= set;
104 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
105 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
106}
107
108static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
109{
110 sdhci_clear_set_irqs(host, 0, irqs);
111}
112
113static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
114{
115 sdhci_clear_set_irqs(host, irqs, 0);
116}
117
118static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
119{
120 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
121
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300122 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
123 return;
124
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300125 if (enable)
126 sdhci_unmask_irqs(host, irqs);
127 else
128 sdhci_mask_irqs(host, irqs);
129}
130
131static void sdhci_enable_card_detection(struct sdhci_host *host)
132{
133 sdhci_set_card_detection(host, true);
134}
135
136static void sdhci_disable_card_detection(struct sdhci_host *host)
137{
138 sdhci_set_card_detection(host, false);
139}
140
Pierre Ossmand129bce2006-03-24 03:18:17 -0800141static void sdhci_reset(struct sdhci_host *host, u8 mask)
142{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700143 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300144 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700145
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100146 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300147 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700148 SDHCI_CARD_PRESENT))
149 return;
150 }
151
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300152 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
153 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
154
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300155 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800156
Pierre Ossmane16514d82006-06-30 02:22:24 -0700157 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800158 host->clock = 0;
159
Pierre Ossmane16514d82006-06-30 02:22:24 -0700160 /* Wait max 100 ms */
161 timeout = 100;
162
163 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300164 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700165 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100166 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700167 mmc_hostname(host->mmc), (int)mask);
168 sdhci_dumpregs(host);
169 return;
170 }
171 timeout--;
172 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300174
175 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
176 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177}
178
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800179static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
180
181static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800183 if (soft)
184 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
185 else
186 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300188 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
189 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700190 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
191 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300192 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800193
194 if (soft) {
195 /* force clock reconfiguration */
196 host->clock = 0;
197 sdhci_set_ios(host->mmc, &host->mmc->ios);
198 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300199}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800200
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300201static void sdhci_reinit(struct sdhci_host *host)
202{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800203 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300204 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205}
206
207static void sdhci_activate_led(struct sdhci_host *host)
208{
209 u8 ctrl;
210
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300211 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300213 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800214}
215
216static void sdhci_deactivate_led(struct sdhci_host *host)
217{
218 u8 ctrl;
219
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300220 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300222 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800223}
224
Pierre Ossmanf9134312008-12-21 17:01:48 +0100225#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100226static void sdhci_led_control(struct led_classdev *led,
227 enum led_brightness brightness)
228{
229 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
230 unsigned long flags;
231
232 spin_lock_irqsave(&host->lock, flags);
233
234 if (brightness == LED_OFF)
235 sdhci_deactivate_led(host);
236 else
237 sdhci_activate_led(host);
238
239 spin_unlock_irqrestore(&host->lock, flags);
240}
241#endif
242
Pierre Ossmand129bce2006-03-24 03:18:17 -0800243/*****************************************************************************\
244 * *
245 * Core functions *
246 * *
247\*****************************************************************************/
248
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100249static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250{
Pierre Ossman76591502008-07-21 00:32:11 +0200251 unsigned long flags;
252 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700253 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200254 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100256 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800257
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100258 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200259 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260
Pierre Ossman76591502008-07-21 00:32:11 +0200261 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100263 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200264 if (!sg_miter_next(&host->sg_miter))
265 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266
Pierre Ossman76591502008-07-21 00:32:11 +0200267 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268
Pierre Ossman76591502008-07-21 00:32:11 +0200269 blksize -= len;
270 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200271
Pierre Ossman76591502008-07-21 00:32:11 +0200272 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273
Pierre Ossman76591502008-07-21 00:32:11 +0200274 while (len) {
275 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200277 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278 }
Pierre Ossman76591502008-07-21 00:32:11 +0200279
280 *buf = scratch & 0xFF;
281
282 buf++;
283 scratch >>= 8;
284 chunk--;
285 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286 }
287 }
Pierre Ossman76591502008-07-21 00:32:11 +0200288
289 sg_miter_stop(&host->sg_miter);
290
291 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800293
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100294static void sdhci_write_block_pio(struct sdhci_host *host)
295{
Pierre Ossman76591502008-07-21 00:32:11 +0200296 unsigned long flags;
297 size_t blksize, len, chunk;
298 u32 scratch;
299 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100300
301 DBG("PIO writing\n");
302
303 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200304 chunk = 0;
305 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306
Pierre Ossman76591502008-07-21 00:32:11 +0200307 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100308
309 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200310 if (!sg_miter_next(&host->sg_miter))
311 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312
Pierre Ossman76591502008-07-21 00:32:11 +0200313 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200314
Pierre Ossman76591502008-07-21 00:32:11 +0200315 blksize -= len;
316 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319
Pierre Ossman76591502008-07-21 00:32:11 +0200320 while (len) {
321 scratch |= (u32)*buf << (chunk * 8);
322
323 buf++;
324 chunk++;
325 len--;
326
327 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300328 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200329 chunk = 0;
330 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100331 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100332 }
333 }
Pierre Ossman76591502008-07-21 00:32:11 +0200334
335 sg_miter_stop(&host->sg_miter);
336
337 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338}
339
340static void sdhci_transfer_pio(struct sdhci_host *host)
341{
342 u32 mask;
343
344 BUG_ON(!host->data);
345
Pierre Ossman76591502008-07-21 00:32:11 +0200346 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100347 return;
348
349 if (host->data->flags & MMC_DATA_READ)
350 mask = SDHCI_DATA_AVAILABLE;
351 else
352 mask = SDHCI_SPACE_AVAILABLE;
353
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200354 /*
355 * Some controllers (JMicron JMB38x) mess up the buffer bits
356 * for transfers < 4 bytes. As long as it is just one block,
357 * we can ignore the bits.
358 */
359 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
360 (host->data->blocks == 1))
361 mask = ~0;
362
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300363 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300364 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
365 udelay(100);
366
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367 if (host->data->flags & MMC_DATA_READ)
368 sdhci_read_block_pio(host);
369 else
370 sdhci_write_block_pio(host);
371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 host->blocks--;
373 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375 }
376
377 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800378}
379
Pierre Ossman2134a922008-06-28 18:28:51 +0200380static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
381{
382 local_irq_save(*flags);
383 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
384}
385
386static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
387{
388 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
389 local_irq_restore(*flags);
390}
391
Ben Dooks118cd172010-03-05 13:43:26 -0800392static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
393{
Ben Dooks9e506f32010-03-05 13:43:29 -0800394 __le32 *dataddr = (__le32 __force *)(desc + 4);
395 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800396
Ben Dooks9e506f32010-03-05 13:43:29 -0800397 /* SDHCI specification says ADMA descriptors should be 4 byte
398 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800399
Ben Dooks9e506f32010-03-05 13:43:29 -0800400 cmdlen[0] = cpu_to_le16(cmd);
401 cmdlen[1] = cpu_to_le16(len);
402
403 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800404}
405
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200406static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200407 struct mmc_data *data)
408{
409 int direction;
410
411 u8 *desc;
412 u8 *align;
413 dma_addr_t addr;
414 dma_addr_t align_addr;
415 int len, offset;
416
417 struct scatterlist *sg;
418 int i;
419 char *buffer;
420 unsigned long flags;
421
422 /*
423 * The spec does not specify endianness of descriptor table.
424 * We currently guess that it is LE.
425 */
426
427 if (data->flags & MMC_DATA_READ)
428 direction = DMA_FROM_DEVICE;
429 else
430 direction = DMA_TO_DEVICE;
431
432 /*
433 * The ADMA descriptor table is mapped further down as we
434 * need to fill it with data first.
435 */
436
437 host->align_addr = dma_map_single(mmc_dev(host->mmc),
438 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700439 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200440 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200441 BUG_ON(host->align_addr & 0x3);
442
443 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
444 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200445 if (host->sg_count == 0)
446 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200447
448 desc = host->adma_desc;
449 align = host->align_buffer;
450
451 align_addr = host->align_addr;
452
453 for_each_sg(data->sg, sg, host->sg_count, i) {
454 addr = sg_dma_address(sg);
455 len = sg_dma_len(sg);
456
457 /*
458 * The SDHCI specification states that ADMA
459 * addresses must be 32-bit aligned. If they
460 * aren't, then we use a bounce buffer for
461 * the (up to three) bytes that screw up the
462 * alignment.
463 */
464 offset = (4 - (addr & 0x3)) & 0x3;
465 if (offset) {
466 if (data->flags & MMC_DATA_WRITE) {
467 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200468 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200469 memcpy(align, buffer, offset);
470 sdhci_kunmap_atomic(buffer, &flags);
471 }
472
Ben Dooks118cd172010-03-05 13:43:26 -0800473 /* tran, valid */
474 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200475
476 BUG_ON(offset > 65536);
477
Pierre Ossman2134a922008-06-28 18:28:51 +0200478 align += 4;
479 align_addr += 4;
480
481 desc += 8;
482
483 addr += offset;
484 len -= offset;
485 }
486
Pierre Ossman2134a922008-06-28 18:28:51 +0200487 BUG_ON(len > 65536);
488
Ben Dooks118cd172010-03-05 13:43:26 -0800489 /* tran, valid */
490 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 desc += 8;
492
493 /*
494 * If this triggers then we have a calculation bug
495 * somewhere. :/
496 */
497 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
498 }
499
Thomas Abraham70764a92010-05-26 14:42:04 -0700500 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
501 /*
502 * Mark the last descriptor as the terminating descriptor
503 */
504 if (desc != host->adma_desc) {
505 desc -= 8;
506 desc[0] |= 0x2; /* end */
507 }
508 } else {
509 /*
510 * Add a terminating entry.
511 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200512
Thomas Abraham70764a92010-05-26 14:42:04 -0700513 /* nop, end, valid */
514 sdhci_set_adma_desc(desc, 0, 0, 0x3);
515 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200516
517 /*
518 * Resync align buffer as we might have changed it.
519 */
520 if (data->flags & MMC_DATA_WRITE) {
521 dma_sync_single_for_device(mmc_dev(host->mmc),
522 host->align_addr, 128 * 4, direction);
523 }
524
525 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
526 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200527 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200528 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200529 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200530
531 return 0;
532
533unmap_entries:
534 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
535 data->sg_len, direction);
536unmap_align:
537 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
538 128 * 4, direction);
539fail:
540 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200541}
542
543static void sdhci_adma_table_post(struct sdhci_host *host,
544 struct mmc_data *data)
545{
546 int direction;
547
548 struct scatterlist *sg;
549 int i, size;
550 u8 *align;
551 char *buffer;
552 unsigned long flags;
553
554 if (data->flags & MMC_DATA_READ)
555 direction = DMA_FROM_DEVICE;
556 else
557 direction = DMA_TO_DEVICE;
558
559 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
560 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
561
562 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
563 128 * 4, direction);
564
565 if (data->flags & MMC_DATA_READ) {
566 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
567 data->sg_len, direction);
568
569 align = host->align_buffer;
570
571 for_each_sg(data->sg, sg, host->sg_count, i) {
572 if (sg_dma_address(sg) & 0x3) {
573 size = 4 - (sg_dma_address(sg) & 0x3);
574
575 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200576 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200577 memcpy(buffer, align, size);
578 sdhci_kunmap_atomic(buffer, &flags);
579
580 align += 4;
581 }
582 }
583 }
584
585 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
586 data->sg_len, direction);
587}
588
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200589static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800590{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700591 u8 count;
592 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800593
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200594 /*
595 * If the host controller provides us with an incorrect timeout
596 * value, just skip the check and use 0xE. The hardware may take
597 * longer to time out, but that's much better than having a too-short
598 * timeout value.
599 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200600 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200601 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200602
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700603 /* timeout in us */
604 target_timeout = data->timeout_ns / 1000 +
605 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800606
Anton Vorontsov81b39802009-09-22 16:45:13 -0700607 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
608 host->timeout_clk = host->clock / 1000;
609
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700610 /*
611 * Figure out needed cycles.
612 * We do this in steps in order to fit inside a 32 bit int.
613 * The first step is the minimum timeout, which will have a
614 * minimum resolution of 6 bits:
615 * (1) 2^13*1000 > 2^22,
616 * (2) host->timeout_clk < 2^16
617 * =>
618 * (1) / (2) > 2^6
619 */
620 count = 0;
621 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
622 while (current_timeout < target_timeout) {
623 count++;
624 current_timeout <<= 1;
625 if (count >= 0xF)
626 break;
627 }
628
629 if (count >= 0xF) {
630 printk(KERN_WARNING "%s: Too large timeout requested!\n",
631 mmc_hostname(host->mmc));
632 count = 0xE;
633 }
634
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200635 return count;
636}
637
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300638static void sdhci_set_transfer_irqs(struct sdhci_host *host)
639{
640 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
641 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
642
643 if (host->flags & SDHCI_REQ_USE_DMA)
644 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
645 else
646 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
647}
648
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200649static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
650{
651 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200652 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200653 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200654
655 WARN_ON(host->data);
656
657 if (data == NULL)
658 return;
659
660 /* Sanity checks */
661 BUG_ON(data->blksz * data->blocks > 524288);
662 BUG_ON(data->blksz > host->mmc->max_blk_size);
663 BUG_ON(data->blocks > 65535);
664
665 host->data = data;
666 host->data_early = 0;
667
668 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300669 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800670
Richard Röjforsa13abc72009-09-22 16:45:30 -0700671 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100672 host->flags |= SDHCI_REQ_USE_DMA;
673
Pierre Ossman2134a922008-06-28 18:28:51 +0200674 /*
675 * FIXME: This doesn't account for merging when mapping the
676 * scatterlist.
677 */
678 if (host->flags & SDHCI_REQ_USE_DMA) {
679 int broken, i;
680 struct scatterlist *sg;
681
682 broken = 0;
683 if (host->flags & SDHCI_USE_ADMA) {
684 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
685 broken = 1;
686 } else {
687 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
688 broken = 1;
689 }
690
691 if (unlikely(broken)) {
692 for_each_sg(data->sg, sg, data->sg_len, i) {
693 if (sg->length & 0x3) {
694 DBG("Reverting to PIO because of "
695 "transfer size (%d)\n",
696 sg->length);
697 host->flags &= ~SDHCI_REQ_USE_DMA;
698 break;
699 }
700 }
701 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100702 }
703
704 /*
705 * The assumption here being that alignment is the same after
706 * translation to device address space.
707 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200708 if (host->flags & SDHCI_REQ_USE_DMA) {
709 int broken, i;
710 struct scatterlist *sg;
711
712 broken = 0;
713 if (host->flags & SDHCI_USE_ADMA) {
714 /*
715 * As we use 3 byte chunks to work around
716 * alignment problems, we need to check this
717 * quirk.
718 */
719 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
720 broken = 1;
721 } else {
722 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
723 broken = 1;
724 }
725
726 if (unlikely(broken)) {
727 for_each_sg(data->sg, sg, data->sg_len, i) {
728 if (sg->offset & 0x3) {
729 DBG("Reverting to PIO because of "
730 "bad alignment\n");
731 host->flags &= ~SDHCI_REQ_USE_DMA;
732 break;
733 }
734 }
735 }
736 }
737
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200738 if (host->flags & SDHCI_REQ_USE_DMA) {
739 if (host->flags & SDHCI_USE_ADMA) {
740 ret = sdhci_adma_table_pre(host, data);
741 if (ret) {
742 /*
743 * This only happens when someone fed
744 * us an invalid request.
745 */
746 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200747 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200748 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300749 sdhci_writel(host, host->adma_addr,
750 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200751 }
752 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300753 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200754
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300755 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200756 data->sg, data->sg_len,
757 (data->flags & MMC_DATA_READ) ?
758 DMA_FROM_DEVICE :
759 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300760 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200761 /*
762 * This only happens when someone fed
763 * us an invalid request.
764 */
765 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200766 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200767 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200768 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300769 sdhci_writel(host, sg_dma_address(data->sg),
770 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200771 }
772 }
773 }
774
Pierre Ossman2134a922008-06-28 18:28:51 +0200775 /*
776 * Always adjust the DMA selection as some controllers
777 * (e.g. JMicron) can't do PIO properly when the selection
778 * is ADMA.
779 */
780 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300781 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200782 ctrl &= ~SDHCI_CTRL_DMA_MASK;
783 if ((host->flags & SDHCI_REQ_USE_DMA) &&
784 (host->flags & SDHCI_USE_ADMA))
785 ctrl |= SDHCI_CTRL_ADMA32;
786 else
787 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300788 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100789 }
790
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200791 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200792 int flags;
793
794 flags = SG_MITER_ATOMIC;
795 if (host->data->flags & MMC_DATA_READ)
796 flags |= SG_MITER_TO_SG;
797 else
798 flags |= SG_MITER_FROM_SG;
799 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200800 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800801 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700802
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300803 sdhci_set_transfer_irqs(host);
804
Pierre Ossmanbab76962006-07-02 16:51:35 +0100805 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300806 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
807 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700808}
809
810static void sdhci_set_transfer_mode(struct sdhci_host *host,
811 struct mmc_data *data)
812{
813 u16 mode;
814
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700815 if (data == NULL)
816 return;
817
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200818 WARN_ON(!host->data);
819
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700820 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700821 if (data->blocks > 1) {
822 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
823 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
824 else
825 mode |= SDHCI_TRNS_MULTI;
826 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700827 if (data->flags & MMC_DATA_READ)
828 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100829 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700830 mode |= SDHCI_TRNS_DMA;
831
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300832 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800833}
834
835static void sdhci_finish_data(struct sdhci_host *host)
836{
837 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800838
839 BUG_ON(!host->data);
840
841 data = host->data;
842 host->data = NULL;
843
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100844 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200845 if (host->flags & SDHCI_USE_ADMA)
846 sdhci_adma_table_post(host, data);
847 else {
848 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
849 data->sg_len, (data->flags & MMC_DATA_READ) ?
850 DMA_FROM_DEVICE : DMA_TO_DEVICE);
851 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800852 }
853
854 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200855 * The specification states that the block count register must
856 * be updated, but it does not specify at what point in the
857 * data flow. That makes the register entirely useless to read
858 * back so we have to assume that nothing made it to the card
859 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800860 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200861 if (data->error)
862 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800863 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200864 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800865
Pierre Ossmand129bce2006-03-24 03:18:17 -0800866 if (data->stop) {
867 /*
868 * The controller needs a reset of internal state machines
869 * upon error conditions.
870 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200871 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872 sdhci_reset(host, SDHCI_RESET_CMD);
873 sdhci_reset(host, SDHCI_RESET_DATA);
874 }
875
876 sdhci_send_command(host, data->stop);
877 } else
878 tasklet_schedule(&host->finish_tasklet);
879}
880
881static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
882{
883 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700884 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700885 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800886
887 WARN_ON(host->cmd);
888
Pierre Ossmand129bce2006-03-24 03:18:17 -0800889 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700890 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700891
892 mask = SDHCI_CMD_INHIBIT;
893 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
894 mask |= SDHCI_DATA_INHIBIT;
895
896 /* We shouldn't wait for data inihibit for stop commands, even
897 though they might use busy signaling */
898 if (host->mrq->data && (cmd == host->mrq->data->stop))
899 mask &= ~SDHCI_DATA_INHIBIT;
900
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300901 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700902 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800903 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100904 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200906 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800907 tasklet_schedule(&host->finish_tasklet);
908 return;
909 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700910 timeout--;
911 mdelay(1);
912 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913
914 mod_timer(&host->timer, jiffies + 10 * HZ);
915
916 host->cmd = cmd;
917
918 sdhci_prepare_data(host, cmd->data);
919
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300920 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800921
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700922 sdhci_set_transfer_mode(host, cmd->data);
923
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100925 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800926 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200927 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800928 tasklet_schedule(&host->finish_tasklet);
929 return;
930 }
931
932 if (!(cmd->flags & MMC_RSP_PRESENT))
933 flags = SDHCI_CMD_RESP_NONE;
934 else if (cmd->flags & MMC_RSP_136)
935 flags = SDHCI_CMD_RESP_LONG;
936 else if (cmd->flags & MMC_RSP_BUSY)
937 flags = SDHCI_CMD_RESP_SHORT_BUSY;
938 else
939 flags = SDHCI_CMD_RESP_SHORT;
940
941 if (cmd->flags & MMC_RSP_CRC)
942 flags |= SDHCI_CMD_CRC;
943 if (cmd->flags & MMC_RSP_OPCODE)
944 flags |= SDHCI_CMD_INDEX;
945 if (cmd->data)
946 flags |= SDHCI_CMD_DATA;
947
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300948 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949}
950
951static void sdhci_finish_command(struct sdhci_host *host)
952{
953 int i;
954
955 BUG_ON(host->cmd == NULL);
956
957 if (host->cmd->flags & MMC_RSP_PRESENT) {
958 if (host->cmd->flags & MMC_RSP_136) {
959 /* CRC is stripped so we need to do some shifting. */
960 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300961 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 SDHCI_RESPONSE + (3-i)*4) << 8;
963 if (i != 3)
964 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300965 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800966 SDHCI_RESPONSE + (3-i)*4-1);
967 }
968 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300969 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970 }
971 }
972
Pierre Ossman17b04292007-07-22 22:18:46 +0200973 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200975 if (host->data && host->data_early)
976 sdhci_finish_data(host);
977
978 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 tasklet_schedule(&host->finish_tasklet);
980
981 host->cmd = NULL;
982}
983
984static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
985{
986 int div;
987 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700988 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800989
990 if (clock == host->clock)
991 return;
992
Anton Vorontsov81146342009-03-17 00:13:59 +0300993 if (host->ops->set_clock) {
994 host->ops->set_clock(host, clock);
995 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
996 return;
997 }
998
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300999 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
1001 if (clock == 0)
1002 goto out;
1003
1004 for (div = 1;div < 256;div *= 2) {
1005 if ((host->max_clk / div) <= clock)
1006 break;
1007 }
1008 div >>= 1;
1009
1010 clk = div << SDHCI_DIVIDER_SHIFT;
1011 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001012 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
Chris Ball27f6cb12009-09-22 16:45:31 -07001014 /* Wait max 20 ms */
1015 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001016 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001017 & SDHCI_CLOCK_INT_STABLE)) {
1018 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001019 printk(KERN_ERR "%s: Internal clock never "
1020 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 sdhci_dumpregs(host);
1022 return;
1023 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001024 timeout--;
1025 mdelay(1);
1026 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027
1028 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001029 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030
1031out:
1032 host->clock = clock;
1033}
1034
Pierre Ossman146ad662006-06-30 02:22:23 -07001035static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1036{
1037 u8 pwr;
1038
Pierre Ossmanae628902009-05-03 20:45:03 +02001039 if (power == (unsigned short)-1)
1040 pwr = 0;
1041 else {
1042 switch (1 << power) {
1043 case MMC_VDD_165_195:
1044 pwr = SDHCI_POWER_180;
1045 break;
1046 case MMC_VDD_29_30:
1047 case MMC_VDD_30_31:
1048 pwr = SDHCI_POWER_300;
1049 break;
1050 case MMC_VDD_32_33:
1051 case MMC_VDD_33_34:
1052 pwr = SDHCI_POWER_330;
1053 break;
1054 default:
1055 BUG();
1056 }
1057 }
1058
1059 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001060 return;
1061
Pierre Ossmanae628902009-05-03 20:45:03 +02001062 host->pwr = pwr;
1063
1064 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001065 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001066 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001067 }
1068
1069 /*
1070 * Spec says that we should clear the power reg before setting
1071 * a new value. Some controllers don't seem to like this though.
1072 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001073 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001075
Andres Salomone08c1692008-07-04 10:00:03 -07001076 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001077 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001078 * and set turn on power at the same time, so set the voltage first.
1079 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001080 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001081 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1082
1083 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001084
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001085 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001086
1087 /*
1088 * Some controllers need an extra 10ms delay of 10ms before they
1089 * can apply clock after applying power
1090 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001091 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001092 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001093}
1094
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095/*****************************************************************************\
1096 * *
1097 * MMC callbacks *
1098 * *
1099\*****************************************************************************/
1100
1101static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1102{
1103 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001104 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001105 unsigned long flags;
1106
1107 host = mmc_priv(mmc);
1108
1109 spin_lock_irqsave(&host->lock, flags);
1110
1111 WARN_ON(host->mrq != NULL);
1112
Pierre Ossmanf9134312008-12-21 17:01:48 +01001113#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001114 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001115#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001116 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1117 if (mrq->stop) {
1118 mrq->data->stop = NULL;
1119 mrq->stop = NULL;
1120 }
1121 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122
1123 host->mrq = mrq;
1124
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001125 /* If polling, assume that the card is always present. */
1126 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1127 present = true;
1128 else
1129 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1130 SDHCI_CARD_PRESENT;
1131
1132 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001133 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134 tasklet_schedule(&host->finish_tasklet);
1135 } else
1136 sdhci_send_command(host, mrq->cmd);
1137
Pierre Ossman5f25a662006-10-04 02:15:39 -07001138 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139 spin_unlock_irqrestore(&host->lock, flags);
1140}
1141
1142static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1143{
1144 struct sdhci_host *host;
1145 unsigned long flags;
1146 u8 ctrl;
1147
1148 host = mmc_priv(mmc);
1149
1150 spin_lock_irqsave(&host->lock, flags);
1151
Pierre Ossman1e728592008-04-16 19:13:13 +02001152 if (host->flags & SDHCI_DEVICE_DEAD)
1153 goto out;
1154
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155 /*
1156 * Reset the chip on each power off.
1157 * Should clear out any weird states.
1158 */
1159 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001160 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001161 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 }
1163
1164 sdhci_set_clock(host, ios->clock);
1165
1166 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001167 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001169 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001171 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001172
Kyungmin Parkae6d6c92010-08-10 18:01:43 -07001173 if (ios->bus_width == MMC_BUS_WIDTH_8)
1174 ctrl |= SDHCI_CTRL_8BITBUS;
1175 else
1176 ctrl &= ~SDHCI_CTRL_8BITBUS;
1177
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 if (ios->bus_width == MMC_BUS_WIDTH_4)
1179 ctrl |= SDHCI_CTRL_4BITBUS;
1180 else
1181 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001182
1183 if (ios->timing == MMC_TIMING_SD_HS)
1184 ctrl |= SDHCI_CTRL_HISPD;
1185 else
1186 ctrl &= ~SDHCI_CTRL_HISPD;
1187
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001188 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189
Leandro Dorileob8352262007-07-25 23:47:04 +02001190 /*
1191 * Some (ENE) controllers go apeshit on some ios operation,
1192 * signalling timeout and CRC errors even on CMD0. Resetting
1193 * it on each ios seems to solve the problem.
1194 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001195 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001196 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1197
Pierre Ossman1e728592008-04-16 19:13:13 +02001198out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001199 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200 spin_unlock_irqrestore(&host->lock, flags);
1201}
1202
1203static int sdhci_get_ro(struct mmc_host *mmc)
1204{
1205 struct sdhci_host *host;
1206 unsigned long flags;
1207 int present;
1208
1209 host = mmc_priv(mmc);
1210
1211 spin_lock_irqsave(&host->lock, flags);
1212
Pierre Ossman1e728592008-04-16 19:13:13 +02001213 if (host->flags & SDHCI_DEVICE_DEAD)
1214 present = 0;
1215 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001216 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217
1218 spin_unlock_irqrestore(&host->lock, flags);
1219
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001220 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1221 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222 return !(present & SDHCI_WRITE_PROTECT);
1223}
1224
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001225static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1226{
1227 struct sdhci_host *host;
1228 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001229
1230 host = mmc_priv(mmc);
1231
1232 spin_lock_irqsave(&host->lock, flags);
1233
Pierre Ossman1e728592008-04-16 19:13:13 +02001234 if (host->flags & SDHCI_DEVICE_DEAD)
1235 goto out;
1236
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001237 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001238 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1239 else
1240 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001241out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001242 mmiowb();
1243
1244 spin_unlock_irqrestore(&host->lock, flags);
1245}
1246
David Brownellab7aefd2006-11-12 17:55:30 -08001247static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248 .request = sdhci_request,
1249 .set_ios = sdhci_set_ios,
1250 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001251 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252};
1253
1254/*****************************************************************************\
1255 * *
1256 * Tasklets *
1257 * *
1258\*****************************************************************************/
1259
1260static void sdhci_tasklet_card(unsigned long param)
1261{
1262 struct sdhci_host *host;
1263 unsigned long flags;
1264
1265 host = (struct sdhci_host*)param;
1266
1267 spin_lock_irqsave(&host->lock, flags);
1268
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001269 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270 if (host->mrq) {
1271 printk(KERN_ERR "%s: Card removed during transfer!\n",
1272 mmc_hostname(host->mmc));
1273 printk(KERN_ERR "%s: Resetting controller.\n",
1274 mmc_hostname(host->mmc));
1275
1276 sdhci_reset(host, SDHCI_RESET_CMD);
1277 sdhci_reset(host, SDHCI_RESET_DATA);
1278
Pierre Ossman17b04292007-07-22 22:18:46 +02001279 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001280 tasklet_schedule(&host->finish_tasklet);
1281 }
1282 }
1283
1284 spin_unlock_irqrestore(&host->lock, flags);
1285
Pierre Ossman04cf5852008-08-18 22:18:14 +02001286 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287}
1288
1289static void sdhci_tasklet_finish(unsigned long param)
1290{
1291 struct sdhci_host *host;
1292 unsigned long flags;
1293 struct mmc_request *mrq;
1294
1295 host = (struct sdhci_host*)param;
1296
1297 spin_lock_irqsave(&host->lock, flags);
1298
1299 del_timer(&host->timer);
1300
1301 mrq = host->mrq;
1302
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303 /*
1304 * The controller needs a reset of internal state machines
1305 * upon error conditions.
1306 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001307 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1308 (mrq->cmd->error ||
1309 (mrq->data && (mrq->data->error ||
1310 (mrq->data->stop && mrq->data->stop->error))) ||
1311 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001312
1313 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001314 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001315 unsigned int clock;
1316
1317 /* This is to force an update */
1318 clock = host->clock;
1319 host->clock = 0;
1320 sdhci_set_clock(host, clock);
1321 }
1322
1323 /* Spec says we should do both at the same time, but Ricoh
1324 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001325 sdhci_reset(host, SDHCI_RESET_CMD);
1326 sdhci_reset(host, SDHCI_RESET_DATA);
1327 }
1328
1329 host->mrq = NULL;
1330 host->cmd = NULL;
1331 host->data = NULL;
1332
Pierre Ossmanf9134312008-12-21 17:01:48 +01001333#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001335#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336
Pierre Ossman5f25a662006-10-04 02:15:39 -07001337 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338 spin_unlock_irqrestore(&host->lock, flags);
1339
1340 mmc_request_done(host->mmc, mrq);
1341}
1342
1343static void sdhci_timeout_timer(unsigned long data)
1344{
1345 struct sdhci_host *host;
1346 unsigned long flags;
1347
1348 host = (struct sdhci_host*)data;
1349
1350 spin_lock_irqsave(&host->lock, flags);
1351
1352 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001353 printk(KERN_ERR "%s: Timeout waiting for hardware "
1354 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001355 sdhci_dumpregs(host);
1356
1357 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001358 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359 sdhci_finish_data(host);
1360 } else {
1361 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001362 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001364 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001365
1366 tasklet_schedule(&host->finish_tasklet);
1367 }
1368 }
1369
Pierre Ossman5f25a662006-10-04 02:15:39 -07001370 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371 spin_unlock_irqrestore(&host->lock, flags);
1372}
1373
1374/*****************************************************************************\
1375 * *
1376 * Interrupt handling *
1377 * *
1378\*****************************************************************************/
1379
1380static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1381{
1382 BUG_ON(intmask == 0);
1383
1384 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001385 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1386 "though no command operation was in progress.\n",
1387 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388 sdhci_dumpregs(host);
1389 return;
1390 }
1391
Pierre Ossman43b58b32007-07-25 23:15:27 +02001392 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001393 host->cmd->error = -ETIMEDOUT;
1394 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1395 SDHCI_INT_INDEX))
1396 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397
Pierre Ossmane8095172008-07-25 01:09:08 +02001398 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001399 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001400 return;
1401 }
1402
1403 /*
1404 * The host can send and interrupt when the busy state has
1405 * ended, allowing us to wait without wasting CPU cycles.
1406 * Unfortunately this is overloaded on the "data complete"
1407 * interrupt, so we need to take some care when handling
1408 * it.
1409 *
1410 * Note: The 1.0 specification is a bit ambiguous about this
1411 * feature so there might be some problems with older
1412 * controllers.
1413 */
1414 if (host->cmd->flags & MMC_RSP_BUSY) {
1415 if (host->cmd->data)
1416 DBG("Cannot wait for busy signal when also "
1417 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001418 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001419 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001420
1421 /* The controller does not support the end-of-busy IRQ,
1422 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001423 }
1424
1425 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001426 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001427}
1428
Ben Dooks6882a8c2009-06-14 13:52:38 +01001429#ifdef DEBUG
1430static void sdhci_show_adma_error(struct sdhci_host *host)
1431{
1432 const char *name = mmc_hostname(host->mmc);
1433 u8 *desc = host->adma_desc;
1434 __le32 *dma;
1435 __le16 *len;
1436 u8 attr;
1437
1438 sdhci_dumpregs(host);
1439
1440 while (true) {
1441 dma = (__le32 *)(desc + 4);
1442 len = (__le16 *)(desc + 2);
1443 attr = *desc;
1444
1445 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1446 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1447
1448 desc += 8;
1449
1450 if (attr & 2)
1451 break;
1452 }
1453}
1454#else
1455static void sdhci_show_adma_error(struct sdhci_host *host) { }
1456#endif
1457
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1459{
1460 BUG_ON(intmask == 0);
1461
1462 if (!host->data) {
1463 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001464 * The "data complete" interrupt is also used to
1465 * indicate that a busy state has ended. See comment
1466 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001467 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001468 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1469 if (intmask & SDHCI_INT_DATA_END) {
1470 sdhci_finish_command(host);
1471 return;
1472 }
1473 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001475 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1476 "though no data operation was in progress.\n",
1477 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478 sdhci_dumpregs(host);
1479
1480 return;
1481 }
1482
1483 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001484 host->data->error = -ETIMEDOUT;
1485 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1486 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001487 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1488 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1489 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001490 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001491 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492
Pierre Ossman17b04292007-07-22 22:18:46 +02001493 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494 sdhci_finish_data(host);
1495 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001496 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497 sdhci_transfer_pio(host);
1498
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001499 /*
1500 * We currently don't do anything fancy with DMA
1501 * boundaries, but as we can't disable the feature
1502 * we need to at least restart the transfer.
1503 */
1504 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001505 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1506 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001507
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001508 if (intmask & SDHCI_INT_DATA_END) {
1509 if (host->cmd) {
1510 /*
1511 * Data managed to finish before the
1512 * command completed. Make sure we do
1513 * things in the proper order.
1514 */
1515 host->data_early = 1;
1516 } else {
1517 sdhci_finish_data(host);
1518 }
1519 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001520 }
1521}
1522
David Howells7d12e782006-10-05 14:55:46 +01001523static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001524{
1525 irqreturn_t result;
1526 struct sdhci_host* host = dev_id;
1527 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001528 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529
1530 spin_lock(&host->lock);
1531
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001532 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001533
Mark Lord62df67a52007-03-06 13:30:13 +01001534 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001535 result = IRQ_NONE;
1536 goto out;
1537 }
1538
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001539 DBG("*** %s got interrupt: 0x%08x\n",
1540 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541
Pierre Ossman3192a282006-06-30 02:22:26 -07001542 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001543 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1544 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001545 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001546 }
1547
1548 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001549
1550 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001551 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1552 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001553 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001554 }
1555
1556 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001557 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1558 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001559 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001560 }
1561
1562 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1563
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001564 intmask &= ~SDHCI_INT_ERROR;
1565
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001567 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001568 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001569 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570 }
1571
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001572 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001573
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001574 if (intmask & SDHCI_INT_CARD_INT)
1575 cardint = 1;
1576
1577 intmask &= ~SDHCI_INT_CARD_INT;
1578
Pierre Ossman3192a282006-06-30 02:22:26 -07001579 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001580 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001581 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582 sdhci_dumpregs(host);
1583
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001584 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001585 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001586
1587 result = IRQ_HANDLED;
1588
Pierre Ossman5f25a662006-10-04 02:15:39 -07001589 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001590out:
1591 spin_unlock(&host->lock);
1592
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001593 /*
1594 * We have to delay this as it calls back into the driver.
1595 */
1596 if (cardint)
1597 mmc_signal_sdio_irq(host->mmc);
1598
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599 return result;
1600}
1601
1602/*****************************************************************************\
1603 * *
1604 * Suspend/resume *
1605 * *
1606\*****************************************************************************/
1607
1608#ifdef CONFIG_PM
1609
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001610int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001612 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001613
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001614 sdhci_disable_card_detection(host);
1615
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001616 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001617 if (ret)
1618 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001620 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001622 if (host->vmmc)
1623 ret = regulator_disable(host->vmmc);
1624
1625 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001626}
1627
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001628EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001630int sdhci_resume_host(struct sdhci_host *host)
1631{
1632 int ret;
1633
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001634 if (host->vmmc) {
1635 int ret = regulator_enable(host->vmmc);
1636 if (ret)
1637 return ret;
1638 }
1639
1640
Richard Röjforsa13abc72009-09-22 16:45:30 -07001641 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001642 if (host->ops->enable_dma)
1643 host->ops->enable_dma(host);
1644 }
1645
1646 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1647 mmc_hostname(host->mmc), host);
1648 if (ret)
1649 return ret;
1650
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001651 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652 mmiowb();
1653
1654 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001655 sdhci_enable_card_detection(host);
1656
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001657 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001658}
1659
1660EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001661
1662#endif /* CONFIG_PM */
1663
1664/*****************************************************************************\
1665 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001666 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001667 * *
1668\*****************************************************************************/
1669
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001670struct sdhci_host *sdhci_alloc_host(struct device *dev,
1671 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673 struct mmc_host *mmc;
1674 struct sdhci_host *host;
1675
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001676 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001677
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001678 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001679 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001680 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681
1682 host = mmc_priv(mmc);
1683 host->mmc = mmc;
1684
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001685 return host;
1686}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001687
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001688EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001689
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001690int sdhci_add_host(struct sdhci_host *host)
1691{
1692 struct mmc_host *mmc;
1693 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001694 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001695
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001696 WARN_ON(host == NULL);
1697 if (host == NULL)
1698 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001699
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001700 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001701
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001702 if (debug_quirks)
1703 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001704
Pierre Ossmand96649e2006-06-30 02:22:30 -07001705 sdhci_reset(host, SDHCI_RESET_ALL);
1706
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001707 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001708 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1709 >> SDHCI_SPEC_VER_SHIFT;
1710 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001711 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001712 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001713 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001714 }
1715
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001716 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1717 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001719 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001720 host->flags |= SDHCI_USE_SDMA;
1721 else if (!(caps & SDHCI_CAN_DO_SDMA))
1722 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001723 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001724 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001725
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001726 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001727 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001728 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001729 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001730 }
1731
Richard Röjforsa13abc72009-09-22 16:45:30 -07001732 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1733 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001734
1735 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1736 (host->flags & SDHCI_USE_ADMA)) {
1737 DBG("Disabling ADMA as it is marked broken\n");
1738 host->flags &= ~SDHCI_USE_ADMA;
1739 }
1740
Richard Röjforsa13abc72009-09-22 16:45:30 -07001741 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001742 if (host->ops->enable_dma) {
1743 if (host->ops->enable_dma(host)) {
1744 printk(KERN_WARNING "%s: No suitable DMA "
1745 "available. Falling back to PIO.\n",
1746 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001747 host->flags &=
1748 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001749 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750 }
1751 }
1752
Pierre Ossman2134a922008-06-28 18:28:51 +02001753 if (host->flags & SDHCI_USE_ADMA) {
1754 /*
1755 * We need to allocate descriptors for all sg entries
1756 * (128) and potentially one alignment transfer for
1757 * each of those entries.
1758 */
1759 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1760 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1761 if (!host->adma_desc || !host->align_buffer) {
1762 kfree(host->adma_desc);
1763 kfree(host->align_buffer);
1764 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1765 "buffers. Falling back to standard DMA.\n",
1766 mmc_hostname(mmc));
1767 host->flags &= ~SDHCI_USE_ADMA;
1768 }
1769 }
1770
Pierre Ossman76591502008-07-21 00:32:11 +02001771 /*
1772 * If we use DMA, then it's up to the caller to set the DMA
1773 * mask, but PIO does not need the hw shim so we set a new
1774 * mask here in that case.
1775 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001776 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001777 host->dma_mask = DMA_BIT_MASK(64);
1778 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1779 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001780
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001781 host->max_clk =
1782 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001783 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001784 if (host->max_clk == 0 || host->quirks &
1785 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001786 if (!host->ops->get_max_clock) {
1787 printk(KERN_ERR
1788 "%s: Hardware doesn't specify base clock "
1789 "frequency.\n", mmc_hostname(mmc));
1790 return -ENODEV;
1791 }
1792 host->max_clk = host->ops->get_max_clock(host);
1793 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001794
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001795 host->timeout_clk =
1796 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1797 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001798 if (host->ops->get_timeout_clock) {
1799 host->timeout_clk = host->ops->get_timeout_clock(host);
1800 } else if (!(host->quirks &
1801 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001802 printk(KERN_ERR
1803 "%s: Hardware doesn't specify timeout clock "
1804 "frequency.\n", mmc_hostname(mmc));
1805 return -ENODEV;
1806 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001807 }
1808 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1809 host->timeout_clk *= 1000;
1810
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811 /*
1812 * Set host parameters.
1813 */
1814 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07001815 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001816 mmc->f_min = host->ops->get_min_clock(host);
1817 else
1818 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001819 mmc->f_max = host->max_clk;
Kyungmin Parkc1f59772010-08-10 18:01:44 -07001820 mmc->caps |= MMC_CAP_SDIO_IRQ;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001821
1822 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1823 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001824
Pierre Ossman86a6a872009-02-02 21:13:49 +01001825 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001826 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1827
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001828 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1829 mmc->caps |= MMC_CAP_NEEDS_POLL;
1830
Pierre Ossman146ad662006-06-30 02:22:23 -07001831 mmc->ocr_avail = 0;
1832 if (caps & SDHCI_CAN_VDD_330)
1833 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001834 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001835 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001836 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001837 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001838
1839 if (mmc->ocr_avail == 0) {
1840 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001841 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001842 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001843 }
1844
Pierre Ossmand129bce2006-03-24 03:18:17 -08001845 spin_lock_init(&host->lock);
1846
1847 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001848 * Maximum number of segments. Depends on if the hardware
1849 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001850 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001851 if (host->flags & SDHCI_USE_ADMA)
1852 mmc->max_hw_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001853 else if (host->flags & SDHCI_USE_SDMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001854 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001855 else /* PIO */
1856 mmc->max_hw_segs = 128;
1857 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001858
1859 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001860 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001861 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001862 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001863 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001864
1865 /*
1866 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001867 * of bytes. When doing hardware scatter/gather, each entry cannot
1868 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001869 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001870 if (host->flags & SDHCI_USE_ADMA)
1871 mmc->max_seg_size = 65536;
1872 else
1873 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874
1875 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001876 * Maximum block size. This varies from controller to controller and
1877 * is specified in the capabilities register.
1878 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001879 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1880 mmc->max_blk_size = 2;
1881 } else {
1882 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1883 SDHCI_MAX_BLOCK_SHIFT;
1884 if (mmc->max_blk_size >= 3) {
1885 printk(KERN_WARNING "%s: Invalid maximum block size, "
1886 "assuming 512 bytes\n", mmc_hostname(mmc));
1887 mmc->max_blk_size = 0;
1888 }
1889 }
1890
1891 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001892
1893 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001894 * Maximum block count.
1895 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001896 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001897
1898 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001899 * Init tasklets.
1900 */
1901 tasklet_init(&host->card_tasklet,
1902 sdhci_tasklet_card, (unsigned long)host);
1903 tasklet_init(&host->finish_tasklet,
1904 sdhci_tasklet_finish, (unsigned long)host);
1905
Al Viroe4cad1b2006-10-10 22:47:07 +01001906 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001907
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001908 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001909 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001910 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001911 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001912
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001913 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
1914 if (IS_ERR(host->vmmc)) {
1915 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
1916 host->vmmc = NULL;
1917 } else {
1918 regulator_enable(host->vmmc);
1919 }
1920
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001921 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001922
1923#ifdef CONFIG_MMC_DEBUG
1924 sdhci_dumpregs(host);
1925#endif
1926
Pierre Ossmanf9134312008-12-21 17:01:48 +01001927#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001928 snprintf(host->led_name, sizeof(host->led_name),
1929 "%s::", mmc_hostname(mmc));
1930 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001931 host->led.brightness = LED_OFF;
1932 host->led.default_trigger = mmc_hostname(mmc);
1933 host->led.brightness_set = sdhci_led_control;
1934
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001935 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001936 if (ret)
1937 goto reset;
1938#endif
1939
Pierre Ossman5f25a662006-10-04 02:15:39 -07001940 mmiowb();
1941
Pierre Ossmand129bce2006-03-24 03:18:17 -08001942 mmc_add_host(mmc);
1943
Richard Röjforsa13abc72009-09-22 16:45:30 -07001944 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001945 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001946 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1947 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001948
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001949 sdhci_enable_card_detection(host);
1950
Pierre Ossmand129bce2006-03-24 03:18:17 -08001951 return 0;
1952
Pierre Ossmanf9134312008-12-21 17:01:48 +01001953#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001954reset:
1955 sdhci_reset(host, SDHCI_RESET_ALL);
1956 free_irq(host->irq, host);
1957#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001958untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001959 tasklet_kill(&host->card_tasklet);
1960 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001961
1962 return ret;
1963}
1964
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001965EXPORT_SYMBOL_GPL(sdhci_add_host);
1966
Pierre Ossman1e728592008-04-16 19:13:13 +02001967void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001968{
Pierre Ossman1e728592008-04-16 19:13:13 +02001969 unsigned long flags;
1970
1971 if (dead) {
1972 spin_lock_irqsave(&host->lock, flags);
1973
1974 host->flags |= SDHCI_DEVICE_DEAD;
1975
1976 if (host->mrq) {
1977 printk(KERN_ERR "%s: Controller removed during "
1978 " transfer!\n", mmc_hostname(host->mmc));
1979
1980 host->mrq->cmd->error = -ENOMEDIUM;
1981 tasklet_schedule(&host->finish_tasklet);
1982 }
1983
1984 spin_unlock_irqrestore(&host->lock, flags);
1985 }
1986
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001987 sdhci_disable_card_detection(host);
1988
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001989 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001990
Pierre Ossmanf9134312008-12-21 17:01:48 +01001991#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001992 led_classdev_unregister(&host->led);
1993#endif
1994
Pierre Ossman1e728592008-04-16 19:13:13 +02001995 if (!dead)
1996 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001997
1998 free_irq(host->irq, host);
1999
2000 del_timer_sync(&host->timer);
2001
2002 tasklet_kill(&host->card_tasklet);
2003 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002004
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002005 if (host->vmmc) {
2006 regulator_disable(host->vmmc);
2007 regulator_put(host->vmmc);
2008 }
2009
Pierre Ossman2134a922008-06-28 18:28:51 +02002010 kfree(host->adma_desc);
2011 kfree(host->align_buffer);
2012
2013 host->adma_desc = NULL;
2014 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002015}
2016
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002017EXPORT_SYMBOL_GPL(sdhci_remove_host);
2018
2019void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002020{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002021 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002022}
2023
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002024EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002025
2026/*****************************************************************************\
2027 * *
2028 * Driver init/exit *
2029 * *
2030\*****************************************************************************/
2031
2032static int __init sdhci_drv_init(void)
2033{
2034 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002035 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002036 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2037
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002038 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002039}
2040
2041static void __exit sdhci_drv_exit(void)
2042{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002043}
2044
2045module_init(sdhci_drv_init);
2046module_exit(sdhci_drv_exit);
2047
Pierre Ossmandf673b22006-06-30 02:22:31 -07002048module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002049
Pierre Ossman32710e82009-04-08 20:14:54 +02002050MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002051MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002052MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002053
Pierre Ossmandf673b22006-06-30 02:22:31 -07002054MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");