blob: 483b78eee3ac4e01527c8968ce94966b1507c5c9 [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080022
Pierre Ossman2f730fe2008-03-17 10:29:38 +010023#include <linux/leds.h>
24
Pierre Ossmand129bce2006-03-24 03:18:17 -080025#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080026
Pierre Ossmand129bce2006-03-24 03:18:17 -080027#include "sdhci.h"
28
29#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010032 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmanf9134312008-12-21 17:01:48 +010034#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
35 defined(CONFIG_MMC_SDHCI_MODULE))
36#define SDHCI_USE_LEDS_CLASS
37#endif
38
Pierre Ossmandf673b22006-06-30 02:22:31 -070039static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070040
Pierre Ossmand129bce2006-03-24 03:18:17 -080041static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
42static void sdhci_finish_data(struct sdhci_host *);
43
44static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
45static void sdhci_finish_command(struct sdhci_host *);
46
47static void sdhci_dumpregs(struct sdhci_host *host)
48{
49 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
50
51 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030052 sdhci_readl(host, SDHCI_DMA_ADDRESS),
53 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080054 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030055 sdhci_readw(host, SDHCI_BLOCK_SIZE),
56 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080057 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030058 sdhci_readl(host, SDHCI_ARGUMENT),
59 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080060 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030061 sdhci_readl(host, SDHCI_PRESENT_STATE),
62 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080063 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030064 sdhci_readb(host, SDHCI_POWER_CONTROL),
65 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080066 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030067 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
68 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080069 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030070 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
71 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080072 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030073 sdhci_readl(host, SDHCI_INT_ENABLE),
74 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080075 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030076 sdhci_readw(host, SDHCI_ACMD12_ERR),
77 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080078 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030079 sdhci_readl(host, SDHCI_CAPABILITIES),
80 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080081
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010082 if (host->flags & SDHCI_USE_ADMA)
83 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
84 readl(host->ioaddr + SDHCI_ADMA_ERROR),
85 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
86
Pierre Ossmand129bce2006-03-24 03:18:17 -080087 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
88}
89
90/*****************************************************************************\
91 * *
92 * Low level functions *
93 * *
94\*****************************************************************************/
95
Anton Vorontsov7260cf52009-03-17 00:13:48 +030096static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
97{
98 u32 ier;
99
100 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
101 ier &= ~clear;
102 ier |= set;
103 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
104 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
105}
106
107static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
108{
109 sdhci_clear_set_irqs(host, 0, irqs);
110}
111
112static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
113{
114 sdhci_clear_set_irqs(host, irqs, 0);
115}
116
117static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
118{
119 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
120
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300121 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
122 return;
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124 if (enable)
125 sdhci_unmask_irqs(host, irqs);
126 else
127 sdhci_mask_irqs(host, irqs);
128}
129
130static void sdhci_enable_card_detection(struct sdhci_host *host)
131{
132 sdhci_set_card_detection(host, true);
133}
134
135static void sdhci_disable_card_detection(struct sdhci_host *host)
136{
137 sdhci_set_card_detection(host, false);
138}
139
Pierre Ossmand129bce2006-03-24 03:18:17 -0800140static void sdhci_reset(struct sdhci_host *host, u8 mask)
141{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700142 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300143 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700144
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100145 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300146 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700147 SDHCI_CARD_PRESENT))
148 return;
149 }
150
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300151 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
152 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
153
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300154 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800155
Pierre Ossmane16514d82006-06-30 02:22:24 -0700156 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800157 host->clock = 0;
158
Pierre Ossmane16514d82006-06-30 02:22:24 -0700159 /* Wait max 100 ms */
160 timeout = 100;
161
162 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300163 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700164 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100165 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700166 mmc_hostname(host->mmc), (int)mask);
167 sdhci_dumpregs(host);
168 return;
169 }
170 timeout--;
171 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800172 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300173
174 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
175 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800176}
177
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800178static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
179
180static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800182 if (soft)
183 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
184 else
185 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300187 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
188 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700189 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
190 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300191 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800192
193 if (soft) {
194 /* force clock reconfiguration */
195 host->clock = 0;
196 sdhci_set_ios(host->mmc, &host->mmc->ios);
197 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300198}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800199
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300200static void sdhci_reinit(struct sdhci_host *host)
201{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800202 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300203 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800204}
205
206static void sdhci_activate_led(struct sdhci_host *host)
207{
208 u8 ctrl;
209
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300210 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800211 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300212 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800213}
214
215static void sdhci_deactivate_led(struct sdhci_host *host)
216{
217 u8 ctrl;
218
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300219 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800220 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300221 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Pierre Ossmanf9134312008-12-21 17:01:48 +0100224#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100225static void sdhci_led_control(struct led_classdev *led,
226 enum led_brightness brightness)
227{
228 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
229 unsigned long flags;
230
231 spin_lock_irqsave(&host->lock, flags);
232
233 if (brightness == LED_OFF)
234 sdhci_deactivate_led(host);
235 else
236 sdhci_activate_led(host);
237
238 spin_unlock_irqrestore(&host->lock, flags);
239}
240#endif
241
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242/*****************************************************************************\
243 * *
244 * Core functions *
245 * *
246\*****************************************************************************/
247
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100248static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249{
Pierre Ossman76591502008-07-21 00:32:11 +0200250 unsigned long flags;
251 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700252 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200253 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100255 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100257 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200258 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800259
Pierre Ossman76591502008-07-21 00:32:11 +0200260 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100262 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200263 if (!sg_miter_next(&host->sg_miter))
264 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265
Pierre Ossman76591502008-07-21 00:32:11 +0200266 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267
Pierre Ossman76591502008-07-21 00:32:11 +0200268 blksize -= len;
269 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200270
Pierre Ossman76591502008-07-21 00:32:11 +0200271 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272
Pierre Ossman76591502008-07-21 00:32:11 +0200273 while (len) {
274 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200276 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277 }
Pierre Ossman76591502008-07-21 00:32:11 +0200278
279 *buf = scratch & 0xFF;
280
281 buf++;
282 scratch >>= 8;
283 chunk--;
284 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285 }
286 }
Pierre Ossman76591502008-07-21 00:32:11 +0200287
288 sg_miter_stop(&host->sg_miter);
289
290 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100291}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800292
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100293static void sdhci_write_block_pio(struct sdhci_host *host)
294{
Pierre Ossman76591502008-07-21 00:32:11 +0200295 unsigned long flags;
296 size_t blksize, len, chunk;
297 u32 scratch;
298 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299
300 DBG("PIO writing\n");
301
302 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200303 chunk = 0;
304 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100305
Pierre Ossman76591502008-07-21 00:32:11 +0200306 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100307
308 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200309 if (!sg_miter_next(&host->sg_miter))
310 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311
Pierre Ossman76591502008-07-21 00:32:11 +0200312 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200313
Pierre Ossman76591502008-07-21 00:32:11 +0200314 blksize -= len;
315 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 while (len) {
320 scratch |= (u32)*buf << (chunk * 8);
321
322 buf++;
323 chunk++;
324 len--;
325
326 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300327 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200328 chunk = 0;
329 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100330 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100331 }
332 }
Pierre Ossman76591502008-07-21 00:32:11 +0200333
334 sg_miter_stop(&host->sg_miter);
335
336 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100337}
338
339static void sdhci_transfer_pio(struct sdhci_host *host)
340{
341 u32 mask;
342
343 BUG_ON(!host->data);
344
Pierre Ossman76591502008-07-21 00:32:11 +0200345 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346 return;
347
348 if (host->data->flags & MMC_DATA_READ)
349 mask = SDHCI_DATA_AVAILABLE;
350 else
351 mask = SDHCI_SPACE_AVAILABLE;
352
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200353 /*
354 * Some controllers (JMicron JMB38x) mess up the buffer bits
355 * for transfers < 4 bytes. As long as it is just one block,
356 * we can ignore the bits.
357 */
358 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
359 (host->data->blocks == 1))
360 mask = ~0;
361
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300362 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300363 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
364 udelay(100);
365
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366 if (host->data->flags & MMC_DATA_READ)
367 sdhci_read_block_pio(host);
368 else
369 sdhci_write_block_pio(host);
370
Pierre Ossman76591502008-07-21 00:32:11 +0200371 host->blocks--;
372 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 }
375
376 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800377}
378
Pierre Ossman2134a922008-06-28 18:28:51 +0200379static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
380{
381 local_irq_save(*flags);
382 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
383}
384
385static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
386{
387 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
388 local_irq_restore(*flags);
389}
390
Ben Dooks118cd172010-03-05 13:43:26 -0800391static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
392{
Ben Dooks9e506f32010-03-05 13:43:29 -0800393 __le32 *dataddr = (__le32 __force *)(desc + 4);
394 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800395
Ben Dooks9e506f32010-03-05 13:43:29 -0800396 /* SDHCI specification says ADMA descriptors should be 4 byte
397 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800398
Ben Dooks9e506f32010-03-05 13:43:29 -0800399 cmdlen[0] = cpu_to_le16(cmd);
400 cmdlen[1] = cpu_to_le16(len);
401
402 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800403}
404
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200405static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200406 struct mmc_data *data)
407{
408 int direction;
409
410 u8 *desc;
411 u8 *align;
412 dma_addr_t addr;
413 dma_addr_t align_addr;
414 int len, offset;
415
416 struct scatterlist *sg;
417 int i;
418 char *buffer;
419 unsigned long flags;
420
421 /*
422 * The spec does not specify endianness of descriptor table.
423 * We currently guess that it is LE.
424 */
425
426 if (data->flags & MMC_DATA_READ)
427 direction = DMA_FROM_DEVICE;
428 else
429 direction = DMA_TO_DEVICE;
430
431 /*
432 * The ADMA descriptor table is mapped further down as we
433 * need to fill it with data first.
434 */
435
436 host->align_addr = dma_map_single(mmc_dev(host->mmc),
437 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700438 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200439 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200440 BUG_ON(host->align_addr & 0x3);
441
442 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
443 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200444 if (host->sg_count == 0)
445 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200446
447 desc = host->adma_desc;
448 align = host->align_buffer;
449
450 align_addr = host->align_addr;
451
452 for_each_sg(data->sg, sg, host->sg_count, i) {
453 addr = sg_dma_address(sg);
454 len = sg_dma_len(sg);
455
456 /*
457 * The SDHCI specification states that ADMA
458 * addresses must be 32-bit aligned. If they
459 * aren't, then we use a bounce buffer for
460 * the (up to three) bytes that screw up the
461 * alignment.
462 */
463 offset = (4 - (addr & 0x3)) & 0x3;
464 if (offset) {
465 if (data->flags & MMC_DATA_WRITE) {
466 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200467 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200468 memcpy(align, buffer, offset);
469 sdhci_kunmap_atomic(buffer, &flags);
470 }
471
Ben Dooks118cd172010-03-05 13:43:26 -0800472 /* tran, valid */
473 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200474
475 BUG_ON(offset > 65536);
476
Pierre Ossman2134a922008-06-28 18:28:51 +0200477 align += 4;
478 align_addr += 4;
479
480 desc += 8;
481
482 addr += offset;
483 len -= offset;
484 }
485
Pierre Ossman2134a922008-06-28 18:28:51 +0200486 BUG_ON(len > 65536);
487
Ben Dooks118cd172010-03-05 13:43:26 -0800488 /* tran, valid */
489 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200490 desc += 8;
491
492 /*
493 * If this triggers then we have a calculation bug
494 * somewhere. :/
495 */
496 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
497 }
498
Thomas Abraham70764a92010-05-26 14:42:04 -0700499 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
500 /*
501 * Mark the last descriptor as the terminating descriptor
502 */
503 if (desc != host->adma_desc) {
504 desc -= 8;
505 desc[0] |= 0x2; /* end */
506 }
507 } else {
508 /*
509 * Add a terminating entry.
510 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200511
Thomas Abraham70764a92010-05-26 14:42:04 -0700512 /* nop, end, valid */
513 sdhci_set_adma_desc(desc, 0, 0, 0x3);
514 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200515
516 /*
517 * Resync align buffer as we might have changed it.
518 */
519 if (data->flags & MMC_DATA_WRITE) {
520 dma_sync_single_for_device(mmc_dev(host->mmc),
521 host->align_addr, 128 * 4, direction);
522 }
523
524 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
525 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200526 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200527 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200528 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200529
530 return 0;
531
532unmap_entries:
533 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
534 data->sg_len, direction);
535unmap_align:
536 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
537 128 * 4, direction);
538fail:
539 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200540}
541
542static void sdhci_adma_table_post(struct sdhci_host *host,
543 struct mmc_data *data)
544{
545 int direction;
546
547 struct scatterlist *sg;
548 int i, size;
549 u8 *align;
550 char *buffer;
551 unsigned long flags;
552
553 if (data->flags & MMC_DATA_READ)
554 direction = DMA_FROM_DEVICE;
555 else
556 direction = DMA_TO_DEVICE;
557
558 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
559 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
560
561 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
562 128 * 4, direction);
563
564 if (data->flags & MMC_DATA_READ) {
565 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
566 data->sg_len, direction);
567
568 align = host->align_buffer;
569
570 for_each_sg(data->sg, sg, host->sg_count, i) {
571 if (sg_dma_address(sg) & 0x3) {
572 size = 4 - (sg_dma_address(sg) & 0x3);
573
574 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200575 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200576 memcpy(buffer, align, size);
577 sdhci_kunmap_atomic(buffer, &flags);
578
579 align += 4;
580 }
581 }
582 }
583
584 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
585 data->sg_len, direction);
586}
587
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200588static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800589{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700590 u8 count;
591 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800592
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200593 /*
594 * If the host controller provides us with an incorrect timeout
595 * value, just skip the check and use 0xE. The hardware may take
596 * longer to time out, but that's much better than having a too-short
597 * timeout value.
598 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200599 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200600 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200601
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700602 /* timeout in us */
603 target_timeout = data->timeout_ns / 1000 +
604 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800605
Anton Vorontsov81b39802009-09-22 16:45:13 -0700606 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
607 host->timeout_clk = host->clock / 1000;
608
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700609 /*
610 * Figure out needed cycles.
611 * We do this in steps in order to fit inside a 32 bit int.
612 * The first step is the minimum timeout, which will have a
613 * minimum resolution of 6 bits:
614 * (1) 2^13*1000 > 2^22,
615 * (2) host->timeout_clk < 2^16
616 * =>
617 * (1) / (2) > 2^6
618 */
619 count = 0;
620 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
621 while (current_timeout < target_timeout) {
622 count++;
623 current_timeout <<= 1;
624 if (count >= 0xF)
625 break;
626 }
627
628 if (count >= 0xF) {
629 printk(KERN_WARNING "%s: Too large timeout requested!\n",
630 mmc_hostname(host->mmc));
631 count = 0xE;
632 }
633
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200634 return count;
635}
636
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300637static void sdhci_set_transfer_irqs(struct sdhci_host *host)
638{
639 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
640 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
641
642 if (host->flags & SDHCI_REQ_USE_DMA)
643 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
644 else
645 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
646}
647
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200648static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
649{
650 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200651 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200652 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200653
654 WARN_ON(host->data);
655
656 if (data == NULL)
657 return;
658
659 /* Sanity checks */
660 BUG_ON(data->blksz * data->blocks > 524288);
661 BUG_ON(data->blksz > host->mmc->max_blk_size);
662 BUG_ON(data->blocks > 65535);
663
664 host->data = data;
665 host->data_early = 0;
666
667 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300668 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800669
Richard Röjforsa13abc72009-09-22 16:45:30 -0700670 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100671 host->flags |= SDHCI_REQ_USE_DMA;
672
Pierre Ossman2134a922008-06-28 18:28:51 +0200673 /*
674 * FIXME: This doesn't account for merging when mapping the
675 * scatterlist.
676 */
677 if (host->flags & SDHCI_REQ_USE_DMA) {
678 int broken, i;
679 struct scatterlist *sg;
680
681 broken = 0;
682 if (host->flags & SDHCI_USE_ADMA) {
683 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
684 broken = 1;
685 } else {
686 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
687 broken = 1;
688 }
689
690 if (unlikely(broken)) {
691 for_each_sg(data->sg, sg, data->sg_len, i) {
692 if (sg->length & 0x3) {
693 DBG("Reverting to PIO because of "
694 "transfer size (%d)\n",
695 sg->length);
696 host->flags &= ~SDHCI_REQ_USE_DMA;
697 break;
698 }
699 }
700 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100701 }
702
703 /*
704 * The assumption here being that alignment is the same after
705 * translation to device address space.
706 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200707 if (host->flags & SDHCI_REQ_USE_DMA) {
708 int broken, i;
709 struct scatterlist *sg;
710
711 broken = 0;
712 if (host->flags & SDHCI_USE_ADMA) {
713 /*
714 * As we use 3 byte chunks to work around
715 * alignment problems, we need to check this
716 * quirk.
717 */
718 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
719 broken = 1;
720 } else {
721 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
722 broken = 1;
723 }
724
725 if (unlikely(broken)) {
726 for_each_sg(data->sg, sg, data->sg_len, i) {
727 if (sg->offset & 0x3) {
728 DBG("Reverting to PIO because of "
729 "bad alignment\n");
730 host->flags &= ~SDHCI_REQ_USE_DMA;
731 break;
732 }
733 }
734 }
735 }
736
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200737 if (host->flags & SDHCI_REQ_USE_DMA) {
738 if (host->flags & SDHCI_USE_ADMA) {
739 ret = sdhci_adma_table_pre(host, data);
740 if (ret) {
741 /*
742 * This only happens when someone fed
743 * us an invalid request.
744 */
745 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200746 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200747 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300748 sdhci_writel(host, host->adma_addr,
749 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200750 }
751 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300752 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200753
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300754 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755 data->sg, data->sg_len,
756 (data->flags & MMC_DATA_READ) ?
757 DMA_FROM_DEVICE :
758 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300759 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200760 /*
761 * This only happens when someone fed
762 * us an invalid request.
763 */
764 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200765 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200766 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200767 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300768 sdhci_writel(host, sg_dma_address(data->sg),
769 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200770 }
771 }
772 }
773
Pierre Ossman2134a922008-06-28 18:28:51 +0200774 /*
775 * Always adjust the DMA selection as some controllers
776 * (e.g. JMicron) can't do PIO properly when the selection
777 * is ADMA.
778 */
779 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300780 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200781 ctrl &= ~SDHCI_CTRL_DMA_MASK;
782 if ((host->flags & SDHCI_REQ_USE_DMA) &&
783 (host->flags & SDHCI_USE_ADMA))
784 ctrl |= SDHCI_CTRL_ADMA32;
785 else
786 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300787 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100788 }
789
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200790 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200791 int flags;
792
793 flags = SG_MITER_ATOMIC;
794 if (host->data->flags & MMC_DATA_READ)
795 flags |= SG_MITER_TO_SG;
796 else
797 flags |= SG_MITER_FROM_SG;
798 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200799 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800800 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700801
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300802 sdhci_set_transfer_irqs(host);
803
Pierre Ossmanbab76962006-07-02 16:51:35 +0100804 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300805 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
806 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700807}
808
809static void sdhci_set_transfer_mode(struct sdhci_host *host,
810 struct mmc_data *data)
811{
812 u16 mode;
813
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700814 if (data == NULL)
815 return;
816
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200817 WARN_ON(!host->data);
818
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700819 mode = SDHCI_TRNS_BLK_CNT_EN;
820 if (data->blocks > 1)
821 mode |= SDHCI_TRNS_MULTI;
822 if (data->flags & MMC_DATA_READ)
823 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100824 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700825 mode |= SDHCI_TRNS_DMA;
826
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300827 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800828}
829
830static void sdhci_finish_data(struct sdhci_host *host)
831{
832 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800833
834 BUG_ON(!host->data);
835
836 data = host->data;
837 host->data = NULL;
838
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100839 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200840 if (host->flags & SDHCI_USE_ADMA)
841 sdhci_adma_table_post(host, data);
842 else {
843 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
844 data->sg_len, (data->flags & MMC_DATA_READ) ?
845 DMA_FROM_DEVICE : DMA_TO_DEVICE);
846 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800847 }
848
849 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200850 * The specification states that the block count register must
851 * be updated, but it does not specify at what point in the
852 * data flow. That makes the register entirely useless to read
853 * back so we have to assume that nothing made it to the card
854 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800855 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200856 if (data->error)
857 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800858 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200859 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800860
Pierre Ossmand129bce2006-03-24 03:18:17 -0800861 if (data->stop) {
862 /*
863 * The controller needs a reset of internal state machines
864 * upon error conditions.
865 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200866 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800867 sdhci_reset(host, SDHCI_RESET_CMD);
868 sdhci_reset(host, SDHCI_RESET_DATA);
869 }
870
871 sdhci_send_command(host, data->stop);
872 } else
873 tasklet_schedule(&host->finish_tasklet);
874}
875
876static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
877{
878 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700879 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700880 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800881
882 WARN_ON(host->cmd);
883
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700885 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700886
887 mask = SDHCI_CMD_INHIBIT;
888 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
889 mask |= SDHCI_DATA_INHIBIT;
890
891 /* We shouldn't wait for data inihibit for stop commands, even
892 though they might use busy signaling */
893 if (host->mrq->data && (cmd == host->mrq->data->stop))
894 mask &= ~SDHCI_DATA_INHIBIT;
895
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300896 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700897 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800898 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100899 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800900 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200901 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800902 tasklet_schedule(&host->finish_tasklet);
903 return;
904 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700905 timeout--;
906 mdelay(1);
907 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908
909 mod_timer(&host->timer, jiffies + 10 * HZ);
910
911 host->cmd = cmd;
912
913 sdhci_prepare_data(host, cmd->data);
914
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300915 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800916
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700917 sdhci_set_transfer_mode(host, cmd->data);
918
Pierre Ossmand129bce2006-03-24 03:18:17 -0800919 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100920 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800921 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200922 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800923 tasklet_schedule(&host->finish_tasklet);
924 return;
925 }
926
927 if (!(cmd->flags & MMC_RSP_PRESENT))
928 flags = SDHCI_CMD_RESP_NONE;
929 else if (cmd->flags & MMC_RSP_136)
930 flags = SDHCI_CMD_RESP_LONG;
931 else if (cmd->flags & MMC_RSP_BUSY)
932 flags = SDHCI_CMD_RESP_SHORT_BUSY;
933 else
934 flags = SDHCI_CMD_RESP_SHORT;
935
936 if (cmd->flags & MMC_RSP_CRC)
937 flags |= SDHCI_CMD_CRC;
938 if (cmd->flags & MMC_RSP_OPCODE)
939 flags |= SDHCI_CMD_INDEX;
940 if (cmd->data)
941 flags |= SDHCI_CMD_DATA;
942
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300943 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944}
945
946static void sdhci_finish_command(struct sdhci_host *host)
947{
948 int i;
949
950 BUG_ON(host->cmd == NULL);
951
952 if (host->cmd->flags & MMC_RSP_PRESENT) {
953 if (host->cmd->flags & MMC_RSP_136) {
954 /* CRC is stripped so we need to do some shifting. */
955 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300956 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 SDHCI_RESPONSE + (3-i)*4) << 8;
958 if (i != 3)
959 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300960 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 SDHCI_RESPONSE + (3-i)*4-1);
962 }
963 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300964 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 }
966 }
967
Pierre Ossman17b04292007-07-22 22:18:46 +0200968 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200970 if (host->data && host->data_early)
971 sdhci_finish_data(host);
972
973 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 tasklet_schedule(&host->finish_tasklet);
975
976 host->cmd = NULL;
977}
978
979static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
980{
981 int div;
982 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700983 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984
985 if (clock == host->clock)
986 return;
987
Anton Vorontsov81146342009-03-17 00:13:59 +0300988 if (host->ops->set_clock) {
989 host->ops->set_clock(host, clock);
990 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
991 return;
992 }
993
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300994 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995
996 if (clock == 0)
997 goto out;
998
999 for (div = 1;div < 256;div *= 2) {
1000 if ((host->max_clk / div) <= clock)
1001 break;
1002 }
1003 div >>= 1;
1004
1005 clk = div << SDHCI_DIVIDER_SHIFT;
1006 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001007 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008
Chris Ball27f6cb12009-09-22 16:45:31 -07001009 /* Wait max 20 ms */
1010 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001011 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001012 & SDHCI_CLOCK_INT_STABLE)) {
1013 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001014 printk(KERN_ERR "%s: Internal clock never "
1015 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 sdhci_dumpregs(host);
1017 return;
1018 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001019 timeout--;
1020 mdelay(1);
1021 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022
1023 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001024 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025
1026out:
1027 host->clock = clock;
1028}
1029
Pierre Ossman146ad662006-06-30 02:22:23 -07001030static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1031{
1032 u8 pwr;
1033
Pierre Ossmanae628902009-05-03 20:45:03 +02001034 if (power == (unsigned short)-1)
1035 pwr = 0;
1036 else {
1037 switch (1 << power) {
1038 case MMC_VDD_165_195:
1039 pwr = SDHCI_POWER_180;
1040 break;
1041 case MMC_VDD_29_30:
1042 case MMC_VDD_30_31:
1043 pwr = SDHCI_POWER_300;
1044 break;
1045 case MMC_VDD_32_33:
1046 case MMC_VDD_33_34:
1047 pwr = SDHCI_POWER_330;
1048 break;
1049 default:
1050 BUG();
1051 }
1052 }
1053
1054 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001055 return;
1056
Pierre Ossmanae628902009-05-03 20:45:03 +02001057 host->pwr = pwr;
1058
1059 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001060 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001061 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001062 }
1063
1064 /*
1065 * Spec says that we should clear the power reg before setting
1066 * a new value. Some controllers don't seem to like this though.
1067 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001068 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001069 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001070
Andres Salomone08c1692008-07-04 10:00:03 -07001071 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001072 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001073 * and set turn on power at the same time, so set the voltage first.
1074 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001075 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001076 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1077
1078 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001079
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001080 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001081
1082 /*
1083 * Some controllers need an extra 10ms delay of 10ms before they
1084 * can apply clock after applying power
1085 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001086 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001087 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001088}
1089
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090/*****************************************************************************\
1091 * *
1092 * MMC callbacks *
1093 * *
1094\*****************************************************************************/
1095
1096static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1097{
1098 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001099 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100 unsigned long flags;
1101
1102 host = mmc_priv(mmc);
1103
1104 spin_lock_irqsave(&host->lock, flags);
1105
1106 WARN_ON(host->mrq != NULL);
1107
Pierre Ossmanf9134312008-12-21 17:01:48 +01001108#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001109 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001110#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111
1112 host->mrq = mrq;
1113
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001114 /* If polling, assume that the card is always present. */
1115 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1116 present = true;
1117 else
1118 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1119 SDHCI_CARD_PRESENT;
1120
1121 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001122 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123 tasklet_schedule(&host->finish_tasklet);
1124 } else
1125 sdhci_send_command(host, mrq->cmd);
1126
Pierre Ossman5f25a662006-10-04 02:15:39 -07001127 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128 spin_unlock_irqrestore(&host->lock, flags);
1129}
1130
1131static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1132{
1133 struct sdhci_host *host;
1134 unsigned long flags;
1135 u8 ctrl;
1136
1137 host = mmc_priv(mmc);
1138
1139 spin_lock_irqsave(&host->lock, flags);
1140
Pierre Ossman1e728592008-04-16 19:13:13 +02001141 if (host->flags & SDHCI_DEVICE_DEAD)
1142 goto out;
1143
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144 /*
1145 * Reset the chip on each power off.
1146 * Should clear out any weird states.
1147 */
1148 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001149 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001150 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 }
1152
1153 sdhci_set_clock(host, ios->clock);
1154
1155 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001156 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001158 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001160 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001161
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 if (ios->bus_width == MMC_BUS_WIDTH_4)
1163 ctrl |= SDHCI_CTRL_4BITBUS;
1164 else
1165 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001166
1167 if (ios->timing == MMC_TIMING_SD_HS)
1168 ctrl |= SDHCI_CTRL_HISPD;
1169 else
1170 ctrl &= ~SDHCI_CTRL_HISPD;
1171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001172 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173
Leandro Dorileob8352262007-07-25 23:47:04 +02001174 /*
1175 * Some (ENE) controllers go apeshit on some ios operation,
1176 * signalling timeout and CRC errors even on CMD0. Resetting
1177 * it on each ios seems to solve the problem.
1178 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001179 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001180 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1181
Pierre Ossman1e728592008-04-16 19:13:13 +02001182out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001183 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184 spin_unlock_irqrestore(&host->lock, flags);
1185}
1186
1187static int sdhci_get_ro(struct mmc_host *mmc)
1188{
1189 struct sdhci_host *host;
1190 unsigned long flags;
1191 int present;
1192
1193 host = mmc_priv(mmc);
1194
1195 spin_lock_irqsave(&host->lock, flags);
1196
Pierre Ossman1e728592008-04-16 19:13:13 +02001197 if (host->flags & SDHCI_DEVICE_DEAD)
1198 present = 0;
1199 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001200 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201
1202 spin_unlock_irqrestore(&host->lock, flags);
1203
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001204 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1205 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001206 return !(present & SDHCI_WRITE_PROTECT);
1207}
1208
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001209static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1210{
1211 struct sdhci_host *host;
1212 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001213
1214 host = mmc_priv(mmc);
1215
1216 spin_lock_irqsave(&host->lock, flags);
1217
Pierre Ossman1e728592008-04-16 19:13:13 +02001218 if (host->flags & SDHCI_DEVICE_DEAD)
1219 goto out;
1220
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001221 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001222 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1223 else
1224 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001225out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001226 mmiowb();
1227
1228 spin_unlock_irqrestore(&host->lock, flags);
1229}
1230
David Brownellab7aefd2006-11-12 17:55:30 -08001231static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232 .request = sdhci_request,
1233 .set_ios = sdhci_set_ios,
1234 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001235 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001236};
1237
1238/*****************************************************************************\
1239 * *
1240 * Tasklets *
1241 * *
1242\*****************************************************************************/
1243
1244static void sdhci_tasklet_card(unsigned long param)
1245{
1246 struct sdhci_host *host;
1247 unsigned long flags;
1248
1249 host = (struct sdhci_host*)param;
1250
1251 spin_lock_irqsave(&host->lock, flags);
1252
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001253 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 if (host->mrq) {
1255 printk(KERN_ERR "%s: Card removed during transfer!\n",
1256 mmc_hostname(host->mmc));
1257 printk(KERN_ERR "%s: Resetting controller.\n",
1258 mmc_hostname(host->mmc));
1259
1260 sdhci_reset(host, SDHCI_RESET_CMD);
1261 sdhci_reset(host, SDHCI_RESET_DATA);
1262
Pierre Ossman17b04292007-07-22 22:18:46 +02001263 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264 tasklet_schedule(&host->finish_tasklet);
1265 }
1266 }
1267
1268 spin_unlock_irqrestore(&host->lock, flags);
1269
Pierre Ossman04cf5852008-08-18 22:18:14 +02001270 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271}
1272
1273static void sdhci_tasklet_finish(unsigned long param)
1274{
1275 struct sdhci_host *host;
1276 unsigned long flags;
1277 struct mmc_request *mrq;
1278
1279 host = (struct sdhci_host*)param;
1280
1281 spin_lock_irqsave(&host->lock, flags);
1282
1283 del_timer(&host->timer);
1284
1285 mrq = host->mrq;
1286
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287 /*
1288 * The controller needs a reset of internal state machines
1289 * upon error conditions.
1290 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001291 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1292 (mrq->cmd->error ||
1293 (mrq->data && (mrq->data->error ||
1294 (mrq->data->stop && mrq->data->stop->error))) ||
1295 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001296
1297 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001298 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001299 unsigned int clock;
1300
1301 /* This is to force an update */
1302 clock = host->clock;
1303 host->clock = 0;
1304 sdhci_set_clock(host, clock);
1305 }
1306
1307 /* Spec says we should do both at the same time, but Ricoh
1308 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309 sdhci_reset(host, SDHCI_RESET_CMD);
1310 sdhci_reset(host, SDHCI_RESET_DATA);
1311 }
1312
1313 host->mrq = NULL;
1314 host->cmd = NULL;
1315 host->data = NULL;
1316
Pierre Ossmanf9134312008-12-21 17:01:48 +01001317#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001318 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001319#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320
Pierre Ossman5f25a662006-10-04 02:15:39 -07001321 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 spin_unlock_irqrestore(&host->lock, flags);
1323
1324 mmc_request_done(host->mmc, mrq);
1325}
1326
1327static void sdhci_timeout_timer(unsigned long data)
1328{
1329 struct sdhci_host *host;
1330 unsigned long flags;
1331
1332 host = (struct sdhci_host*)data;
1333
1334 spin_lock_irqsave(&host->lock, flags);
1335
1336 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001337 printk(KERN_ERR "%s: Timeout waiting for hardware "
1338 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339 sdhci_dumpregs(host);
1340
1341 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001342 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343 sdhci_finish_data(host);
1344 } else {
1345 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001346 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001347 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001348 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349
1350 tasklet_schedule(&host->finish_tasklet);
1351 }
1352 }
1353
Pierre Ossman5f25a662006-10-04 02:15:39 -07001354 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001355 spin_unlock_irqrestore(&host->lock, flags);
1356}
1357
1358/*****************************************************************************\
1359 * *
1360 * Interrupt handling *
1361 * *
1362\*****************************************************************************/
1363
1364static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1365{
1366 BUG_ON(intmask == 0);
1367
1368 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001369 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1370 "though no command operation was in progress.\n",
1371 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 sdhci_dumpregs(host);
1373 return;
1374 }
1375
Pierre Ossman43b58b32007-07-25 23:15:27 +02001376 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001377 host->cmd->error = -ETIMEDOUT;
1378 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1379 SDHCI_INT_INDEX))
1380 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381
Pierre Ossmane8095172008-07-25 01:09:08 +02001382 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001384 return;
1385 }
1386
1387 /*
1388 * The host can send and interrupt when the busy state has
1389 * ended, allowing us to wait without wasting CPU cycles.
1390 * Unfortunately this is overloaded on the "data complete"
1391 * interrupt, so we need to take some care when handling
1392 * it.
1393 *
1394 * Note: The 1.0 specification is a bit ambiguous about this
1395 * feature so there might be some problems with older
1396 * controllers.
1397 */
1398 if (host->cmd->flags & MMC_RSP_BUSY) {
1399 if (host->cmd->data)
1400 DBG("Cannot wait for busy signal when also "
1401 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001402 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001403 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001404
1405 /* The controller does not support the end-of-busy IRQ,
1406 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001407 }
1408
1409 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001410 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411}
1412
Ben Dooks6882a8c2009-06-14 13:52:38 +01001413#ifdef DEBUG
1414static void sdhci_show_adma_error(struct sdhci_host *host)
1415{
1416 const char *name = mmc_hostname(host->mmc);
1417 u8 *desc = host->adma_desc;
1418 __le32 *dma;
1419 __le16 *len;
1420 u8 attr;
1421
1422 sdhci_dumpregs(host);
1423
1424 while (true) {
1425 dma = (__le32 *)(desc + 4);
1426 len = (__le16 *)(desc + 2);
1427 attr = *desc;
1428
1429 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1430 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1431
1432 desc += 8;
1433
1434 if (attr & 2)
1435 break;
1436 }
1437}
1438#else
1439static void sdhci_show_adma_error(struct sdhci_host *host) { }
1440#endif
1441
Pierre Ossmand129bce2006-03-24 03:18:17 -08001442static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1443{
1444 BUG_ON(intmask == 0);
1445
1446 if (!host->data) {
1447 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001448 * The "data complete" interrupt is also used to
1449 * indicate that a busy state has ended. See comment
1450 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001452 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1453 if (intmask & SDHCI_INT_DATA_END) {
1454 sdhci_finish_command(host);
1455 return;
1456 }
1457 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001458
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001459 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1460 "though no data operation was in progress.\n",
1461 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001462 sdhci_dumpregs(host);
1463
1464 return;
1465 }
1466
1467 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001468 host->data->error = -ETIMEDOUT;
1469 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1470 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001471 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1472 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1473 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001474 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001475 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476
Pierre Ossman17b04292007-07-22 22:18:46 +02001477 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478 sdhci_finish_data(host);
1479 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001480 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481 sdhci_transfer_pio(host);
1482
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001483 /*
1484 * We currently don't do anything fancy with DMA
1485 * boundaries, but as we can't disable the feature
1486 * we need to at least restart the transfer.
1487 */
1488 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001489 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1490 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001491
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001492 if (intmask & SDHCI_INT_DATA_END) {
1493 if (host->cmd) {
1494 /*
1495 * Data managed to finish before the
1496 * command completed. Make sure we do
1497 * things in the proper order.
1498 */
1499 host->data_early = 1;
1500 } else {
1501 sdhci_finish_data(host);
1502 }
1503 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504 }
1505}
1506
David Howells7d12e782006-10-05 14:55:46 +01001507static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508{
1509 irqreturn_t result;
1510 struct sdhci_host* host = dev_id;
1511 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001512 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513
1514 spin_lock(&host->lock);
1515
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001516 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001517
Mark Lord62df67a52007-03-06 13:30:13 +01001518 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519 result = IRQ_NONE;
1520 goto out;
1521 }
1522
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001523 DBG("*** %s got interrupt: 0x%08x\n",
1524 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001525
Pierre Ossman3192a282006-06-30 02:22:26 -07001526 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001527 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1528 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001530 }
1531
1532 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001533
1534 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001535 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1536 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001537 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538 }
1539
1540 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001541 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1542 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001543 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001544 }
1545
1546 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1547
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001548 intmask &= ~SDHCI_INT_ERROR;
1549
Pierre Ossmand129bce2006-03-24 03:18:17 -08001550 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001551 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001552 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001553 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001554 }
1555
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001556 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001557
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001558 if (intmask & SDHCI_INT_CARD_INT)
1559 cardint = 1;
1560
1561 intmask &= ~SDHCI_INT_CARD_INT;
1562
Pierre Ossman3192a282006-06-30 02:22:26 -07001563 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001564 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001565 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001566 sdhci_dumpregs(host);
1567
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001568 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001569 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001570
1571 result = IRQ_HANDLED;
1572
Pierre Ossman5f25a662006-10-04 02:15:39 -07001573 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001574out:
1575 spin_unlock(&host->lock);
1576
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001577 /*
1578 * We have to delay this as it calls back into the driver.
1579 */
1580 if (cardint)
1581 mmc_signal_sdio_irq(host->mmc);
1582
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583 return result;
1584}
1585
1586/*****************************************************************************\
1587 * *
1588 * Suspend/resume *
1589 * *
1590\*****************************************************************************/
1591
1592#ifdef CONFIG_PM
1593
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001594int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001596 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001597
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001598 sdhci_disable_card_detection(host);
1599
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001600 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001601 if (ret)
1602 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001604 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001605
1606 return 0;
1607}
1608
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001609EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001610
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001611int sdhci_resume_host(struct sdhci_host *host)
1612{
1613 int ret;
1614
Richard Röjforsa13abc72009-09-22 16:45:30 -07001615 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001616 if (host->ops->enable_dma)
1617 host->ops->enable_dma(host);
1618 }
1619
1620 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1621 mmc_hostname(host->mmc), host);
1622 if (ret)
1623 return ret;
1624
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001625 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001626 mmiowb();
1627
1628 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001629 sdhci_enable_card_detection(host);
1630
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001631 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001632}
1633
1634EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635
1636#endif /* CONFIG_PM */
1637
1638/*****************************************************************************\
1639 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001640 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641 * *
1642\*****************************************************************************/
1643
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001644struct sdhci_host *sdhci_alloc_host(struct device *dev,
1645 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001646{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001647 struct mmc_host *mmc;
1648 struct sdhci_host *host;
1649
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001650 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001651
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001654 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001655
1656 host = mmc_priv(mmc);
1657 host->mmc = mmc;
1658
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001659 return host;
1660}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001661
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001662EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001663
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001664int sdhci_add_host(struct sdhci_host *host)
1665{
1666 struct mmc_host *mmc;
1667 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001668 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001669
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001670 WARN_ON(host == NULL);
1671 if (host == NULL)
1672 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001674 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001675
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001676 if (debug_quirks)
1677 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678
Pierre Ossmand96649e2006-06-30 02:22:30 -07001679 sdhci_reset(host, SDHCI_RESET_ALL);
1680
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001681 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001682 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1683 >> SDHCI_SPEC_VER_SHIFT;
1684 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001685 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001686 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001687 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001688 }
1689
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001690 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1691 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001692
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001693 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001694 host->flags |= SDHCI_USE_SDMA;
1695 else if (!(caps & SDHCI_CAN_DO_SDMA))
1696 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001697 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001698 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001699
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001700 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001701 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001702 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001703 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001704 }
1705
Richard Röjforsa13abc72009-09-22 16:45:30 -07001706 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1707 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001708
1709 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1710 (host->flags & SDHCI_USE_ADMA)) {
1711 DBG("Disabling ADMA as it is marked broken\n");
1712 host->flags &= ~SDHCI_USE_ADMA;
1713 }
1714
Richard Röjforsa13abc72009-09-22 16:45:30 -07001715 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001716 if (host->ops->enable_dma) {
1717 if (host->ops->enable_dma(host)) {
1718 printk(KERN_WARNING "%s: No suitable DMA "
1719 "available. Falling back to PIO.\n",
1720 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001721 host->flags &=
1722 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001723 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001724 }
1725 }
1726
Pierre Ossman2134a922008-06-28 18:28:51 +02001727 if (host->flags & SDHCI_USE_ADMA) {
1728 /*
1729 * We need to allocate descriptors for all sg entries
1730 * (128) and potentially one alignment transfer for
1731 * each of those entries.
1732 */
1733 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1734 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1735 if (!host->adma_desc || !host->align_buffer) {
1736 kfree(host->adma_desc);
1737 kfree(host->align_buffer);
1738 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1739 "buffers. Falling back to standard DMA.\n",
1740 mmc_hostname(mmc));
1741 host->flags &= ~SDHCI_USE_ADMA;
1742 }
1743 }
1744
Pierre Ossman76591502008-07-21 00:32:11 +02001745 /*
1746 * If we use DMA, then it's up to the caller to set the DMA
1747 * mask, but PIO does not need the hw shim so we set a new
1748 * mask here in that case.
1749 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001750 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001751 host->dma_mask = DMA_BIT_MASK(64);
1752 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1753 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001755 host->max_clk =
1756 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001757 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001758 if (host->max_clk == 0 || host->quirks &
1759 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001760 if (!host->ops->get_max_clock) {
1761 printk(KERN_ERR
1762 "%s: Hardware doesn't specify base clock "
1763 "frequency.\n", mmc_hostname(mmc));
1764 return -ENODEV;
1765 }
1766 host->max_clk = host->ops->get_max_clock(host);
1767 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001769 host->timeout_clk =
1770 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1771 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001772 if (host->ops->get_timeout_clock) {
1773 host->timeout_clk = host->ops->get_timeout_clock(host);
1774 } else if (!(host->quirks &
1775 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001776 printk(KERN_ERR
1777 "%s: Hardware doesn't specify timeout clock "
1778 "frequency.\n", mmc_hostname(mmc));
1779 return -ENODEV;
1780 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001781 }
1782 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1783 host->timeout_clk *= 1000;
1784
Pierre Ossmand129bce2006-03-24 03:18:17 -08001785 /*
1786 * Set host parameters.
1787 */
1788 mmc->ops = &sdhci_ops;
Anton Vorontsove9510172009-09-22 16:45:08 -07001789 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK &&
1790 host->ops->set_clock && host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001791 mmc->f_min = host->ops->get_min_clock(host);
1792 else
1793 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001794 mmc->f_max = host->max_clk;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001795 mmc->caps = MMC_CAP_SDIO_IRQ;
1796
1797 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1798 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001799
Pierre Ossman86a6a872009-02-02 21:13:49 +01001800 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001801 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1802
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001803 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1804 mmc->caps |= MMC_CAP_NEEDS_POLL;
1805
Pierre Ossman146ad662006-06-30 02:22:23 -07001806 mmc->ocr_avail = 0;
1807 if (caps & SDHCI_CAN_VDD_330)
1808 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001809 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001810 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001811 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001812 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001813
1814 if (mmc->ocr_avail == 0) {
1815 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001816 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001817 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001818 }
1819
Pierre Ossmand129bce2006-03-24 03:18:17 -08001820 spin_lock_init(&host->lock);
1821
1822 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001823 * Maximum number of segments. Depends on if the hardware
1824 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001825 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001826 if (host->flags & SDHCI_USE_ADMA)
1827 mmc->max_hw_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001828 else if (host->flags & SDHCI_USE_SDMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001829 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001830 else /* PIO */
1831 mmc->max_hw_segs = 128;
1832 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001833
1834 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001835 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001836 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001837 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001838 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001839
1840 /*
1841 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001842 * of bytes. When doing hardware scatter/gather, each entry cannot
1843 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001844 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001845 if (host->flags & SDHCI_USE_ADMA)
1846 mmc->max_seg_size = 65536;
1847 else
1848 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001849
1850 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001851 * Maximum block size. This varies from controller to controller and
1852 * is specified in the capabilities register.
1853 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001854 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1855 mmc->max_blk_size = 2;
1856 } else {
1857 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1858 SDHCI_MAX_BLOCK_SHIFT;
1859 if (mmc->max_blk_size >= 3) {
1860 printk(KERN_WARNING "%s: Invalid maximum block size, "
1861 "assuming 512 bytes\n", mmc_hostname(mmc));
1862 mmc->max_blk_size = 0;
1863 }
1864 }
1865
1866 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001867
1868 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001869 * Maximum block count.
1870 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001871 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001872
1873 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874 * Init tasklets.
1875 */
1876 tasklet_init(&host->card_tasklet,
1877 sdhci_tasklet_card, (unsigned long)host);
1878 tasklet_init(&host->finish_tasklet,
1879 sdhci_tasklet_finish, (unsigned long)host);
1880
Al Viroe4cad1b2006-10-10 22:47:07 +01001881 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001882
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001883 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001884 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001885 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001886 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001887
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001888 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001889
1890#ifdef CONFIG_MMC_DEBUG
1891 sdhci_dumpregs(host);
1892#endif
1893
Pierre Ossmanf9134312008-12-21 17:01:48 +01001894#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001895 snprintf(host->led_name, sizeof(host->led_name),
1896 "%s::", mmc_hostname(mmc));
1897 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001898 host->led.brightness = LED_OFF;
1899 host->led.default_trigger = mmc_hostname(mmc);
1900 host->led.brightness_set = sdhci_led_control;
1901
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001902 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001903 if (ret)
1904 goto reset;
1905#endif
1906
Pierre Ossman5f25a662006-10-04 02:15:39 -07001907 mmiowb();
1908
Pierre Ossmand129bce2006-03-24 03:18:17 -08001909 mmc_add_host(mmc);
1910
Richard Röjforsa13abc72009-09-22 16:45:30 -07001911 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001912 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001913 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1914 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001915
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001916 sdhci_enable_card_detection(host);
1917
Pierre Ossmand129bce2006-03-24 03:18:17 -08001918 return 0;
1919
Pierre Ossmanf9134312008-12-21 17:01:48 +01001920#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001921reset:
1922 sdhci_reset(host, SDHCI_RESET_ALL);
1923 free_irq(host->irq, host);
1924#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001925untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001926 tasklet_kill(&host->card_tasklet);
1927 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001928
1929 return ret;
1930}
1931
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001932EXPORT_SYMBOL_GPL(sdhci_add_host);
1933
Pierre Ossman1e728592008-04-16 19:13:13 +02001934void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001935{
Pierre Ossman1e728592008-04-16 19:13:13 +02001936 unsigned long flags;
1937
1938 if (dead) {
1939 spin_lock_irqsave(&host->lock, flags);
1940
1941 host->flags |= SDHCI_DEVICE_DEAD;
1942
1943 if (host->mrq) {
1944 printk(KERN_ERR "%s: Controller removed during "
1945 " transfer!\n", mmc_hostname(host->mmc));
1946
1947 host->mrq->cmd->error = -ENOMEDIUM;
1948 tasklet_schedule(&host->finish_tasklet);
1949 }
1950
1951 spin_unlock_irqrestore(&host->lock, flags);
1952 }
1953
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001954 sdhci_disable_card_detection(host);
1955
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001956 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001957
Pierre Ossmanf9134312008-12-21 17:01:48 +01001958#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001959 led_classdev_unregister(&host->led);
1960#endif
1961
Pierre Ossman1e728592008-04-16 19:13:13 +02001962 if (!dead)
1963 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001964
1965 free_irq(host->irq, host);
1966
1967 del_timer_sync(&host->timer);
1968
1969 tasklet_kill(&host->card_tasklet);
1970 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001971
1972 kfree(host->adma_desc);
1973 kfree(host->align_buffer);
1974
1975 host->adma_desc = NULL;
1976 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001977}
1978
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001979EXPORT_SYMBOL_GPL(sdhci_remove_host);
1980
1981void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001982{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001983 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001984}
1985
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001986EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001987
1988/*****************************************************************************\
1989 * *
1990 * Driver init/exit *
1991 * *
1992\*****************************************************************************/
1993
1994static int __init sdhci_drv_init(void)
1995{
1996 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001997 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1999
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002000 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002001}
2002
2003static void __exit sdhci_drv_exit(void)
2004{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002005}
2006
2007module_init(sdhci_drv_init);
2008module_exit(sdhci_drv_exit);
2009
Pierre Ossmandf673b22006-06-30 02:22:31 -07002010module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002011
Pierre Ossman32710e82009-04-08 20:14:54 +02002012MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002013MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002014MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002015
Pierre Ossmandf673b22006-06-30 02:22:31 -07002016MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");