blob: 9d4fdfa685e57521a711c29904c100a66df4cd48 [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
499 /*
500 * Add a terminating entry.
501 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200502
Ben Dooks118cd172010-03-05 13:43:26 -0800503 /* nop, end, valid */
504 sdhci_set_adma_desc(desc, 0, 0, 0x3);
Pierre Ossman2134a922008-06-28 18:28:51 +0200505
506 /*
507 * Resync align buffer as we might have changed it.
508 */
509 if (data->flags & MMC_DATA_WRITE) {
510 dma_sync_single_for_device(mmc_dev(host->mmc),
511 host->align_addr, 128 * 4, direction);
512 }
513
514 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
515 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200516 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200517 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200518 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200519
520 return 0;
521
522unmap_entries:
523 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
524 data->sg_len, direction);
525unmap_align:
526 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
527 128 * 4, direction);
528fail:
529 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200530}
531
532static void sdhci_adma_table_post(struct sdhci_host *host,
533 struct mmc_data *data)
534{
535 int direction;
536
537 struct scatterlist *sg;
538 int i, size;
539 u8 *align;
540 char *buffer;
541 unsigned long flags;
542
543 if (data->flags & MMC_DATA_READ)
544 direction = DMA_FROM_DEVICE;
545 else
546 direction = DMA_TO_DEVICE;
547
548 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
549 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
550
551 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
552 128 * 4, direction);
553
554 if (data->flags & MMC_DATA_READ) {
555 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
556 data->sg_len, direction);
557
558 align = host->align_buffer;
559
560 for_each_sg(data->sg, sg, host->sg_count, i) {
561 if (sg_dma_address(sg) & 0x3) {
562 size = 4 - (sg_dma_address(sg) & 0x3);
563
564 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200565 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200566 memcpy(buffer, align, size);
567 sdhci_kunmap_atomic(buffer, &flags);
568
569 align += 4;
570 }
571 }
572 }
573
574 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
575 data->sg_len, direction);
576}
577
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200578static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800579{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700580 u8 count;
581 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800582
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200583 /*
584 * If the host controller provides us with an incorrect timeout
585 * value, just skip the check and use 0xE. The hardware may take
586 * longer to time out, but that's much better than having a too-short
587 * timeout value.
588 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200589 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200590 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200591
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700592 /* timeout in us */
593 target_timeout = data->timeout_ns / 1000 +
594 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800595
Anton Vorontsov81b39802009-09-22 16:45:13 -0700596 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
597 host->timeout_clk = host->clock / 1000;
598
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700599 /*
600 * Figure out needed cycles.
601 * We do this in steps in order to fit inside a 32 bit int.
602 * The first step is the minimum timeout, which will have a
603 * minimum resolution of 6 bits:
604 * (1) 2^13*1000 > 2^22,
605 * (2) host->timeout_clk < 2^16
606 * =>
607 * (1) / (2) > 2^6
608 */
609 count = 0;
610 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
611 while (current_timeout < target_timeout) {
612 count++;
613 current_timeout <<= 1;
614 if (count >= 0xF)
615 break;
616 }
617
618 if (count >= 0xF) {
619 printk(KERN_WARNING "%s: Too large timeout requested!\n",
620 mmc_hostname(host->mmc));
621 count = 0xE;
622 }
623
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200624 return count;
625}
626
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300627static void sdhci_set_transfer_irqs(struct sdhci_host *host)
628{
629 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
630 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
631
632 if (host->flags & SDHCI_REQ_USE_DMA)
633 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
634 else
635 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
636}
637
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200638static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
639{
640 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200641 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200642 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200643
644 WARN_ON(host->data);
645
646 if (data == NULL)
647 return;
648
649 /* Sanity checks */
650 BUG_ON(data->blksz * data->blocks > 524288);
651 BUG_ON(data->blksz > host->mmc->max_blk_size);
652 BUG_ON(data->blocks > 65535);
653
654 host->data = data;
655 host->data_early = 0;
656
657 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300658 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800659
Richard Röjforsa13abc72009-09-22 16:45:30 -0700660 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100661 host->flags |= SDHCI_REQ_USE_DMA;
662
Pierre Ossman2134a922008-06-28 18:28:51 +0200663 /*
664 * FIXME: This doesn't account for merging when mapping the
665 * scatterlist.
666 */
667 if (host->flags & SDHCI_REQ_USE_DMA) {
668 int broken, i;
669 struct scatterlist *sg;
670
671 broken = 0;
672 if (host->flags & SDHCI_USE_ADMA) {
673 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
674 broken = 1;
675 } else {
676 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
677 broken = 1;
678 }
679
680 if (unlikely(broken)) {
681 for_each_sg(data->sg, sg, data->sg_len, i) {
682 if (sg->length & 0x3) {
683 DBG("Reverting to PIO because of "
684 "transfer size (%d)\n",
685 sg->length);
686 host->flags &= ~SDHCI_REQ_USE_DMA;
687 break;
688 }
689 }
690 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100691 }
692
693 /*
694 * The assumption here being that alignment is the same after
695 * translation to device address space.
696 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200697 if (host->flags & SDHCI_REQ_USE_DMA) {
698 int broken, i;
699 struct scatterlist *sg;
700
701 broken = 0;
702 if (host->flags & SDHCI_USE_ADMA) {
703 /*
704 * As we use 3 byte chunks to work around
705 * alignment problems, we need to check this
706 * quirk.
707 */
708 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
709 broken = 1;
710 } else {
711 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
712 broken = 1;
713 }
714
715 if (unlikely(broken)) {
716 for_each_sg(data->sg, sg, data->sg_len, i) {
717 if (sg->offset & 0x3) {
718 DBG("Reverting to PIO because of "
719 "bad alignment\n");
720 host->flags &= ~SDHCI_REQ_USE_DMA;
721 break;
722 }
723 }
724 }
725 }
726
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200727 if (host->flags & SDHCI_REQ_USE_DMA) {
728 if (host->flags & SDHCI_USE_ADMA) {
729 ret = sdhci_adma_table_pre(host, data);
730 if (ret) {
731 /*
732 * This only happens when someone fed
733 * us an invalid request.
734 */
735 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200736 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200737 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300738 sdhci_writel(host, host->adma_addr,
739 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200740 }
741 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300742 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200743
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300744 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200745 data->sg, data->sg_len,
746 (data->flags & MMC_DATA_READ) ?
747 DMA_FROM_DEVICE :
748 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300749 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200750 /*
751 * This only happens when someone fed
752 * us an invalid request.
753 */
754 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200755 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200756 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200757 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300758 sdhci_writel(host, sg_dma_address(data->sg),
759 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200760 }
761 }
762 }
763
Pierre Ossman2134a922008-06-28 18:28:51 +0200764 /*
765 * Always adjust the DMA selection as some controllers
766 * (e.g. JMicron) can't do PIO properly when the selection
767 * is ADMA.
768 */
769 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300770 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200771 ctrl &= ~SDHCI_CTRL_DMA_MASK;
772 if ((host->flags & SDHCI_REQ_USE_DMA) &&
773 (host->flags & SDHCI_USE_ADMA))
774 ctrl |= SDHCI_CTRL_ADMA32;
775 else
776 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300777 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100778 }
779
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200780 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200781 int flags;
782
783 flags = SG_MITER_ATOMIC;
784 if (host->data->flags & MMC_DATA_READ)
785 flags |= SG_MITER_TO_SG;
786 else
787 flags |= SG_MITER_FROM_SG;
788 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200789 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800790 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700791
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300792 sdhci_set_transfer_irqs(host);
793
Pierre Ossmanbab76962006-07-02 16:51:35 +0100794 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300795 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
796 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700797}
798
799static void sdhci_set_transfer_mode(struct sdhci_host *host,
800 struct mmc_data *data)
801{
802 u16 mode;
803
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700804 if (data == NULL)
805 return;
806
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200807 WARN_ON(!host->data);
808
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700809 mode = SDHCI_TRNS_BLK_CNT_EN;
810 if (data->blocks > 1)
811 mode |= SDHCI_TRNS_MULTI;
812 if (data->flags & MMC_DATA_READ)
813 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100814 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700815 mode |= SDHCI_TRNS_DMA;
816
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300817 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800818}
819
820static void sdhci_finish_data(struct sdhci_host *host)
821{
822 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800823
824 BUG_ON(!host->data);
825
826 data = host->data;
827 host->data = NULL;
828
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100829 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200830 if (host->flags & SDHCI_USE_ADMA)
831 sdhci_adma_table_post(host, data);
832 else {
833 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
834 data->sg_len, (data->flags & MMC_DATA_READ) ?
835 DMA_FROM_DEVICE : DMA_TO_DEVICE);
836 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800837 }
838
839 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200840 * The specification states that the block count register must
841 * be updated, but it does not specify at what point in the
842 * data flow. That makes the register entirely useless to read
843 * back so we have to assume that nothing made it to the card
844 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800845 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200846 if (data->error)
847 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800848 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200849 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800850
Pierre Ossmand129bce2006-03-24 03:18:17 -0800851 if (data->stop) {
852 /*
853 * The controller needs a reset of internal state machines
854 * upon error conditions.
855 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200856 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800857 sdhci_reset(host, SDHCI_RESET_CMD);
858 sdhci_reset(host, SDHCI_RESET_DATA);
859 }
860
861 sdhci_send_command(host, data->stop);
862 } else
863 tasklet_schedule(&host->finish_tasklet);
864}
865
866static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
867{
868 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700869 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700870 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800871
872 WARN_ON(host->cmd);
873
Pierre Ossmand129bce2006-03-24 03:18:17 -0800874 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700875 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700876
877 mask = SDHCI_CMD_INHIBIT;
878 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
879 mask |= SDHCI_DATA_INHIBIT;
880
881 /* We shouldn't wait for data inihibit for stop commands, even
882 though they might use busy signaling */
883 if (host->mrq->data && (cmd == host->mrq->data->stop))
884 mask &= ~SDHCI_DATA_INHIBIT;
885
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300886 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700887 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100889 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200891 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800892 tasklet_schedule(&host->finish_tasklet);
893 return;
894 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700895 timeout--;
896 mdelay(1);
897 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800898
899 mod_timer(&host->timer, jiffies + 10 * HZ);
900
901 host->cmd = cmd;
902
903 sdhci_prepare_data(host, cmd->data);
904
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300905 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800906
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700907 sdhci_set_transfer_mode(host, cmd->data);
908
Pierre Ossmand129bce2006-03-24 03:18:17 -0800909 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100910 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800911 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200912 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913 tasklet_schedule(&host->finish_tasklet);
914 return;
915 }
916
917 if (!(cmd->flags & MMC_RSP_PRESENT))
918 flags = SDHCI_CMD_RESP_NONE;
919 else if (cmd->flags & MMC_RSP_136)
920 flags = SDHCI_CMD_RESP_LONG;
921 else if (cmd->flags & MMC_RSP_BUSY)
922 flags = SDHCI_CMD_RESP_SHORT_BUSY;
923 else
924 flags = SDHCI_CMD_RESP_SHORT;
925
926 if (cmd->flags & MMC_RSP_CRC)
927 flags |= SDHCI_CMD_CRC;
928 if (cmd->flags & MMC_RSP_OPCODE)
929 flags |= SDHCI_CMD_INDEX;
930 if (cmd->data)
931 flags |= SDHCI_CMD_DATA;
932
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300933 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934}
935
936static void sdhci_finish_command(struct sdhci_host *host)
937{
938 int i;
939
940 BUG_ON(host->cmd == NULL);
941
942 if (host->cmd->flags & MMC_RSP_PRESENT) {
943 if (host->cmd->flags & MMC_RSP_136) {
944 /* CRC is stripped so we need to do some shifting. */
945 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300946 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947 SDHCI_RESPONSE + (3-i)*4) << 8;
948 if (i != 3)
949 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300950 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 SDHCI_RESPONSE + (3-i)*4-1);
952 }
953 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300954 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955 }
956 }
957
Pierre Ossman17b04292007-07-22 22:18:46 +0200958 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800959
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200960 if (host->data && host->data_early)
961 sdhci_finish_data(host);
962
963 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800964 tasklet_schedule(&host->finish_tasklet);
965
966 host->cmd = NULL;
967}
968
969static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
970{
971 int div;
972 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700973 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974
975 if (clock == host->clock)
976 return;
977
Anton Vorontsov81146342009-03-17 00:13:59 +0300978 if (host->ops->set_clock) {
979 host->ops->set_clock(host, clock);
980 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
981 return;
982 }
983
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300984 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800985
986 if (clock == 0)
987 goto out;
988
989 for (div = 1;div < 256;div *= 2) {
990 if ((host->max_clk / div) <= clock)
991 break;
992 }
993 div >>= 1;
994
995 clk = div << SDHCI_DIVIDER_SHIFT;
996 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300997 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998
Chris Ball27f6cb12009-09-22 16:45:31 -0700999 /* Wait max 20 ms */
1000 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001001 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001002 & SDHCI_CLOCK_INT_STABLE)) {
1003 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001004 printk(KERN_ERR "%s: Internal clock never "
1005 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 sdhci_dumpregs(host);
1007 return;
1008 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001009 timeout--;
1010 mdelay(1);
1011 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
1013 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001014 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015
1016out:
1017 host->clock = clock;
1018}
1019
Pierre Ossman146ad662006-06-30 02:22:23 -07001020static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1021{
1022 u8 pwr;
1023
Pierre Ossmanae628902009-05-03 20:45:03 +02001024 if (power == (unsigned short)-1)
1025 pwr = 0;
1026 else {
1027 switch (1 << power) {
1028 case MMC_VDD_165_195:
1029 pwr = SDHCI_POWER_180;
1030 break;
1031 case MMC_VDD_29_30:
1032 case MMC_VDD_30_31:
1033 pwr = SDHCI_POWER_300;
1034 break;
1035 case MMC_VDD_32_33:
1036 case MMC_VDD_33_34:
1037 pwr = SDHCI_POWER_330;
1038 break;
1039 default:
1040 BUG();
1041 }
1042 }
1043
1044 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001045 return;
1046
Pierre Ossmanae628902009-05-03 20:45:03 +02001047 host->pwr = pwr;
1048
1049 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001050 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001051 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001052 }
1053
1054 /*
1055 * Spec says that we should clear the power reg before setting
1056 * a new value. Some controllers don't seem to like this though.
1057 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001058 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001059 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001060
Andres Salomone08c1692008-07-04 10:00:03 -07001061 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001062 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001063 * and set turn on power at the same time, so set the voltage first.
1064 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001065 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001066 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1067
1068 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001069
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001070 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001071
1072 /*
1073 * Some controllers need an extra 10ms delay of 10ms before they
1074 * can apply clock after applying power
1075 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001076 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001077 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001078}
1079
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080/*****************************************************************************\
1081 * *
1082 * MMC callbacks *
1083 * *
1084\*****************************************************************************/
1085
1086static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1087{
1088 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001089 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090 unsigned long flags;
1091
1092 host = mmc_priv(mmc);
1093
1094 spin_lock_irqsave(&host->lock, flags);
1095
1096 WARN_ON(host->mrq != NULL);
1097
Pierre Ossmanf9134312008-12-21 17:01:48 +01001098#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001100#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101
1102 host->mrq = mrq;
1103
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001104 /* If polling, assume that the card is always present. */
1105 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1106 present = true;
1107 else
1108 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1109 SDHCI_CARD_PRESENT;
1110
1111 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001112 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113 tasklet_schedule(&host->finish_tasklet);
1114 } else
1115 sdhci_send_command(host, mrq->cmd);
1116
Pierre Ossman5f25a662006-10-04 02:15:39 -07001117 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118 spin_unlock_irqrestore(&host->lock, flags);
1119}
1120
1121static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1122{
1123 struct sdhci_host *host;
1124 unsigned long flags;
1125 u8 ctrl;
1126
1127 host = mmc_priv(mmc);
1128
1129 spin_lock_irqsave(&host->lock, flags);
1130
Pierre Ossman1e728592008-04-16 19:13:13 +02001131 if (host->flags & SDHCI_DEVICE_DEAD)
1132 goto out;
1133
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134 /*
1135 * Reset the chip on each power off.
1136 * Should clear out any weird states.
1137 */
1138 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001139 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001140 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 }
1142
1143 sdhci_set_clock(host, ios->clock);
1144
1145 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001146 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001148 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001150 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001151
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152 if (ios->bus_width == MMC_BUS_WIDTH_4)
1153 ctrl |= SDHCI_CTRL_4BITBUS;
1154 else
1155 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001156
1157 if (ios->timing == MMC_TIMING_SD_HS)
1158 ctrl |= SDHCI_CTRL_HISPD;
1159 else
1160 ctrl &= ~SDHCI_CTRL_HISPD;
1161
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001162 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163
Leandro Dorileob8352262007-07-25 23:47:04 +02001164 /*
1165 * Some (ENE) controllers go apeshit on some ios operation,
1166 * signalling timeout and CRC errors even on CMD0. Resetting
1167 * it on each ios seems to solve the problem.
1168 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001169 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001170 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1171
Pierre Ossman1e728592008-04-16 19:13:13 +02001172out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001173 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001174 spin_unlock_irqrestore(&host->lock, flags);
1175}
1176
1177static int sdhci_get_ro(struct mmc_host *mmc)
1178{
1179 struct sdhci_host *host;
1180 unsigned long flags;
1181 int present;
1182
1183 host = mmc_priv(mmc);
1184
1185 spin_lock_irqsave(&host->lock, flags);
1186
Pierre Ossman1e728592008-04-16 19:13:13 +02001187 if (host->flags & SDHCI_DEVICE_DEAD)
1188 present = 0;
1189 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001190 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001191
1192 spin_unlock_irqrestore(&host->lock, flags);
1193
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001194 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1195 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001196 return !(present & SDHCI_WRITE_PROTECT);
1197}
1198
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001199static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1200{
1201 struct sdhci_host *host;
1202 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001203
1204 host = mmc_priv(mmc);
1205
1206 spin_lock_irqsave(&host->lock, flags);
1207
Pierre Ossman1e728592008-04-16 19:13:13 +02001208 if (host->flags & SDHCI_DEVICE_DEAD)
1209 goto out;
1210
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001211 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001212 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1213 else
1214 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001215out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001216 mmiowb();
1217
1218 spin_unlock_irqrestore(&host->lock, flags);
1219}
1220
David Brownellab7aefd2006-11-12 17:55:30 -08001221static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001222 .request = sdhci_request,
1223 .set_ios = sdhci_set_ios,
1224 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001225 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226};
1227
1228/*****************************************************************************\
1229 * *
1230 * Tasklets *
1231 * *
1232\*****************************************************************************/
1233
1234static void sdhci_tasklet_card(unsigned long param)
1235{
1236 struct sdhci_host *host;
1237 unsigned long flags;
1238
1239 host = (struct sdhci_host*)param;
1240
1241 spin_lock_irqsave(&host->lock, flags);
1242
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001243 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244 if (host->mrq) {
1245 printk(KERN_ERR "%s: Card removed during transfer!\n",
1246 mmc_hostname(host->mmc));
1247 printk(KERN_ERR "%s: Resetting controller.\n",
1248 mmc_hostname(host->mmc));
1249
1250 sdhci_reset(host, SDHCI_RESET_CMD);
1251 sdhci_reset(host, SDHCI_RESET_DATA);
1252
Pierre Ossman17b04292007-07-22 22:18:46 +02001253 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 tasklet_schedule(&host->finish_tasklet);
1255 }
1256 }
1257
1258 spin_unlock_irqrestore(&host->lock, flags);
1259
Pierre Ossman04cf5852008-08-18 22:18:14 +02001260 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261}
1262
1263static void sdhci_tasklet_finish(unsigned long param)
1264{
1265 struct sdhci_host *host;
1266 unsigned long flags;
1267 struct mmc_request *mrq;
1268
1269 host = (struct sdhci_host*)param;
1270
1271 spin_lock_irqsave(&host->lock, flags);
1272
1273 del_timer(&host->timer);
1274
1275 mrq = host->mrq;
1276
Pierre Ossmand129bce2006-03-24 03:18:17 -08001277 /*
1278 * The controller needs a reset of internal state machines
1279 * upon error conditions.
1280 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001281 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1282 (mrq->cmd->error ||
1283 (mrq->data && (mrq->data->error ||
1284 (mrq->data->stop && mrq->data->stop->error))) ||
1285 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001286
1287 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001288 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001289 unsigned int clock;
1290
1291 /* This is to force an update */
1292 clock = host->clock;
1293 host->clock = 0;
1294 sdhci_set_clock(host, clock);
1295 }
1296
1297 /* Spec says we should do both at the same time, but Ricoh
1298 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299 sdhci_reset(host, SDHCI_RESET_CMD);
1300 sdhci_reset(host, SDHCI_RESET_DATA);
1301 }
1302
1303 host->mrq = NULL;
1304 host->cmd = NULL;
1305 host->data = NULL;
1306
Pierre Ossmanf9134312008-12-21 17:01:48 +01001307#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001308 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001309#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310
Pierre Ossman5f25a662006-10-04 02:15:39 -07001311 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 spin_unlock_irqrestore(&host->lock, flags);
1313
1314 mmc_request_done(host->mmc, mrq);
1315}
1316
1317static void sdhci_timeout_timer(unsigned long data)
1318{
1319 struct sdhci_host *host;
1320 unsigned long flags;
1321
1322 host = (struct sdhci_host*)data;
1323
1324 spin_lock_irqsave(&host->lock, flags);
1325
1326 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001327 printk(KERN_ERR "%s: Timeout waiting for hardware "
1328 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 sdhci_dumpregs(host);
1330
1331 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001332 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001333 sdhci_finish_data(host);
1334 } else {
1335 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001336 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001338 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339
1340 tasklet_schedule(&host->finish_tasklet);
1341 }
1342 }
1343
Pierre Ossman5f25a662006-10-04 02:15:39 -07001344 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345 spin_unlock_irqrestore(&host->lock, flags);
1346}
1347
1348/*****************************************************************************\
1349 * *
1350 * Interrupt handling *
1351 * *
1352\*****************************************************************************/
1353
1354static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1355{
1356 BUG_ON(intmask == 0);
1357
1358 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001359 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1360 "though no command operation was in progress.\n",
1361 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001362 sdhci_dumpregs(host);
1363 return;
1364 }
1365
Pierre Ossman43b58b32007-07-25 23:15:27 +02001366 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001367 host->cmd->error = -ETIMEDOUT;
1368 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1369 SDHCI_INT_INDEX))
1370 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371
Pierre Ossmane8095172008-07-25 01:09:08 +02001372 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001373 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001374 return;
1375 }
1376
1377 /*
1378 * The host can send and interrupt when the busy state has
1379 * ended, allowing us to wait without wasting CPU cycles.
1380 * Unfortunately this is overloaded on the "data complete"
1381 * interrupt, so we need to take some care when handling
1382 * it.
1383 *
1384 * Note: The 1.0 specification is a bit ambiguous about this
1385 * feature so there might be some problems with older
1386 * controllers.
1387 */
1388 if (host->cmd->flags & MMC_RSP_BUSY) {
1389 if (host->cmd->data)
1390 DBG("Cannot wait for busy signal when also "
1391 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001392 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001393 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001394
1395 /* The controller does not support the end-of-busy IRQ,
1396 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001397 }
1398
1399 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001400 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001401}
1402
Ben Dooks6882a8c2009-06-14 13:52:38 +01001403#ifdef DEBUG
1404static void sdhci_show_adma_error(struct sdhci_host *host)
1405{
1406 const char *name = mmc_hostname(host->mmc);
1407 u8 *desc = host->adma_desc;
1408 __le32 *dma;
1409 __le16 *len;
1410 u8 attr;
1411
1412 sdhci_dumpregs(host);
1413
1414 while (true) {
1415 dma = (__le32 *)(desc + 4);
1416 len = (__le16 *)(desc + 2);
1417 attr = *desc;
1418
1419 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1420 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1421
1422 desc += 8;
1423
1424 if (attr & 2)
1425 break;
1426 }
1427}
1428#else
1429static void sdhci_show_adma_error(struct sdhci_host *host) { }
1430#endif
1431
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1433{
1434 BUG_ON(intmask == 0);
1435
1436 if (!host->data) {
1437 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001438 * The "data complete" interrupt is also used to
1439 * indicate that a busy state has ended. See comment
1440 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001441 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001442 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1443 if (intmask & SDHCI_INT_DATA_END) {
1444 sdhci_finish_command(host);
1445 return;
1446 }
1447 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001449 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1450 "though no data operation was in progress.\n",
1451 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452 sdhci_dumpregs(host);
1453
1454 return;
1455 }
1456
1457 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001458 host->data->error = -ETIMEDOUT;
1459 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1460 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001461 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1462 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1463 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001464 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001465 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466
Pierre Ossman17b04292007-07-22 22:18:46 +02001467 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001468 sdhci_finish_data(host);
1469 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001470 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001471 sdhci_transfer_pio(host);
1472
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001473 /*
1474 * We currently don't do anything fancy with DMA
1475 * boundaries, but as we can't disable the feature
1476 * we need to at least restart the transfer.
1477 */
1478 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001479 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1480 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001481
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001482 if (intmask & SDHCI_INT_DATA_END) {
1483 if (host->cmd) {
1484 /*
1485 * Data managed to finish before the
1486 * command completed. Make sure we do
1487 * things in the proper order.
1488 */
1489 host->data_early = 1;
1490 } else {
1491 sdhci_finish_data(host);
1492 }
1493 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001494 }
1495}
1496
David Howells7d12e782006-10-05 14:55:46 +01001497static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498{
1499 irqreturn_t result;
1500 struct sdhci_host* host = dev_id;
1501 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001502 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503
1504 spin_lock(&host->lock);
1505
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001506 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507
Mark Lord62df67a52007-03-06 13:30:13 +01001508 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509 result = IRQ_NONE;
1510 goto out;
1511 }
1512
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001513 DBG("*** %s got interrupt: 0x%08x\n",
1514 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515
Pierre Ossman3192a282006-06-30 02:22:26 -07001516 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001517 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1518 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001520 }
1521
1522 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523
1524 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001525 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1526 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001527 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528 }
1529
1530 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001531 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1532 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001533 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001534 }
1535
1536 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1537
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001538 intmask &= ~SDHCI_INT_ERROR;
1539
Pierre Ossmand129bce2006-03-24 03:18:17 -08001540 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001541 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001542 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001543 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001544 }
1545
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001546 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001547
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001548 if (intmask & SDHCI_INT_CARD_INT)
1549 cardint = 1;
1550
1551 intmask &= ~SDHCI_INT_CARD_INT;
1552
Pierre Ossman3192a282006-06-30 02:22:26 -07001553 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001554 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001555 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001556 sdhci_dumpregs(host);
1557
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001558 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001559 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001560
1561 result = IRQ_HANDLED;
1562
Pierre Ossman5f25a662006-10-04 02:15:39 -07001563 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001564out:
1565 spin_unlock(&host->lock);
1566
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001567 /*
1568 * We have to delay this as it calls back into the driver.
1569 */
1570 if (cardint)
1571 mmc_signal_sdio_irq(host->mmc);
1572
Pierre Ossmand129bce2006-03-24 03:18:17 -08001573 return result;
1574}
1575
1576/*****************************************************************************\
1577 * *
1578 * Suspend/resume *
1579 * *
1580\*****************************************************************************/
1581
1582#ifdef CONFIG_PM
1583
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001584int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001586 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001587
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001588 sdhci_disable_card_detection(host);
1589
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001590 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001591 if (ret)
1592 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001594 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595
1596 return 0;
1597}
1598
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001599EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001601int sdhci_resume_host(struct sdhci_host *host)
1602{
1603 int ret;
1604
Richard Röjforsa13abc72009-09-22 16:45:30 -07001605 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001606 if (host->ops->enable_dma)
1607 host->ops->enable_dma(host);
1608 }
1609
1610 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1611 mmc_hostname(host->mmc), host);
1612 if (ret)
1613 return ret;
1614
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001615 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001616 mmiowb();
1617
1618 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001619 sdhci_enable_card_detection(host);
1620
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001621 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001622}
1623
1624EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625
1626#endif /* CONFIG_PM */
1627
1628/*****************************************************************************\
1629 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001630 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001631 * *
1632\*****************************************************************************/
1633
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001634struct sdhci_host *sdhci_alloc_host(struct device *dev,
1635 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637 struct mmc_host *mmc;
1638 struct sdhci_host *host;
1639
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001640 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001642 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001644 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645
1646 host = mmc_priv(mmc);
1647 host->mmc = mmc;
1648
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001649 return host;
1650}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001651
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001652EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001654int sdhci_add_host(struct sdhci_host *host)
1655{
1656 struct mmc_host *mmc;
1657 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001658 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001659
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001660 WARN_ON(host == NULL);
1661 if (host == NULL)
1662 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001663
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001664 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001665
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001666 if (debug_quirks)
1667 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668
Pierre Ossmand96649e2006-06-30 02:22:30 -07001669 sdhci_reset(host, SDHCI_RESET_ALL);
1670
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001671 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001672 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1673 >> SDHCI_SPEC_VER_SHIFT;
1674 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001675 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001676 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001677 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001678 }
1679
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001680 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001681
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001682 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001683 host->flags |= SDHCI_USE_SDMA;
1684 else if (!(caps & SDHCI_CAN_DO_SDMA))
1685 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001686 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001687 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001688
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001689 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001690 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001691 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001692 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001693 }
1694
Richard Röjforsa13abc72009-09-22 16:45:30 -07001695 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1696 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001697
1698 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1699 (host->flags & SDHCI_USE_ADMA)) {
1700 DBG("Disabling ADMA as it is marked broken\n");
1701 host->flags &= ~SDHCI_USE_ADMA;
1702 }
1703
Richard Röjforsa13abc72009-09-22 16:45:30 -07001704 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001705 if (host->ops->enable_dma) {
1706 if (host->ops->enable_dma(host)) {
1707 printk(KERN_WARNING "%s: No suitable DMA "
1708 "available. Falling back to PIO.\n",
1709 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001710 host->flags &=
1711 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001712 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001713 }
1714 }
1715
Pierre Ossman2134a922008-06-28 18:28:51 +02001716 if (host->flags & SDHCI_USE_ADMA) {
1717 /*
1718 * We need to allocate descriptors for all sg entries
1719 * (128) and potentially one alignment transfer for
1720 * each of those entries.
1721 */
1722 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1723 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1724 if (!host->adma_desc || !host->align_buffer) {
1725 kfree(host->adma_desc);
1726 kfree(host->align_buffer);
1727 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1728 "buffers. Falling back to standard DMA.\n",
1729 mmc_hostname(mmc));
1730 host->flags &= ~SDHCI_USE_ADMA;
1731 }
1732 }
1733
Pierre Ossman76591502008-07-21 00:32:11 +02001734 /*
1735 * If we use DMA, then it's up to the caller to set the DMA
1736 * mask, but PIO does not need the hw shim so we set a new
1737 * mask here in that case.
1738 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001739 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001740 host->dma_mask = DMA_BIT_MASK(64);
1741 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1742 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001744 host->max_clk =
1745 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001746 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001747 if (host->max_clk == 0) {
1748 if (!host->ops->get_max_clock) {
1749 printk(KERN_ERR
1750 "%s: Hardware doesn't specify base clock "
1751 "frequency.\n", mmc_hostname(mmc));
1752 return -ENODEV;
1753 }
1754 host->max_clk = host->ops->get_max_clock(host);
1755 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001756
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001757 host->timeout_clk =
1758 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1759 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001760 if (host->ops->get_timeout_clock) {
1761 host->timeout_clk = host->ops->get_timeout_clock(host);
1762 } else if (!(host->quirks &
1763 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001764 printk(KERN_ERR
1765 "%s: Hardware doesn't specify timeout clock "
1766 "frequency.\n", mmc_hostname(mmc));
1767 return -ENODEV;
1768 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001769 }
1770 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1771 host->timeout_clk *= 1000;
1772
Pierre Ossmand129bce2006-03-24 03:18:17 -08001773 /*
1774 * Set host parameters.
1775 */
1776 mmc->ops = &sdhci_ops;
Anton Vorontsove9510172009-09-22 16:45:08 -07001777 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK &&
1778 host->ops->set_clock && host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001779 mmc->f_min = host->ops->get_min_clock(host);
1780 else
1781 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001782 mmc->f_max = host->max_clk;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001783 mmc->caps = MMC_CAP_SDIO_IRQ;
1784
1785 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1786 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787
Pierre Ossman86a6a872009-02-02 21:13:49 +01001788 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001789 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1790
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001791 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1792 mmc->caps |= MMC_CAP_NEEDS_POLL;
1793
Pierre Ossman146ad662006-06-30 02:22:23 -07001794 mmc->ocr_avail = 0;
1795 if (caps & SDHCI_CAN_VDD_330)
1796 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001797 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001798 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001799 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001800 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001801
1802 if (mmc->ocr_avail == 0) {
1803 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001804 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001805 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001806 }
1807
Pierre Ossmand129bce2006-03-24 03:18:17 -08001808 spin_lock_init(&host->lock);
1809
1810 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001811 * Maximum number of segments. Depends on if the hardware
1812 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001813 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001814 if (host->flags & SDHCI_USE_ADMA)
1815 mmc->max_hw_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001816 else if (host->flags & SDHCI_USE_SDMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001817 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001818 else /* PIO */
1819 mmc->max_hw_segs = 128;
1820 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001821
1822 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001823 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001824 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001825 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001826 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001827
1828 /*
1829 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001830 * of bytes. When doing hardware scatter/gather, each entry cannot
1831 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001832 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001833 if (host->flags & SDHCI_USE_ADMA)
1834 mmc->max_seg_size = 65536;
1835 else
1836 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001837
1838 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001839 * Maximum block size. This varies from controller to controller and
1840 * is specified in the capabilities register.
1841 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001842 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1843 mmc->max_blk_size = 2;
1844 } else {
1845 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1846 SDHCI_MAX_BLOCK_SHIFT;
1847 if (mmc->max_blk_size >= 3) {
1848 printk(KERN_WARNING "%s: Invalid maximum block size, "
1849 "assuming 512 bytes\n", mmc_hostname(mmc));
1850 mmc->max_blk_size = 0;
1851 }
1852 }
1853
1854 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001855
1856 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001857 * Maximum block count.
1858 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001859 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001860
1861 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001862 * Init tasklets.
1863 */
1864 tasklet_init(&host->card_tasklet,
1865 sdhci_tasklet_card, (unsigned long)host);
1866 tasklet_init(&host->finish_tasklet,
1867 sdhci_tasklet_finish, (unsigned long)host);
1868
Al Viroe4cad1b2006-10-10 22:47:07 +01001869 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001870
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001871 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001872 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001873 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001874 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001875
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001876 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001877
1878#ifdef CONFIG_MMC_DEBUG
1879 sdhci_dumpregs(host);
1880#endif
1881
Pierre Ossmanf9134312008-12-21 17:01:48 +01001882#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001883 snprintf(host->led_name, sizeof(host->led_name),
1884 "%s::", mmc_hostname(mmc));
1885 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001886 host->led.brightness = LED_OFF;
1887 host->led.default_trigger = mmc_hostname(mmc);
1888 host->led.brightness_set = sdhci_led_control;
1889
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001890 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001891 if (ret)
1892 goto reset;
1893#endif
1894
Pierre Ossman5f25a662006-10-04 02:15:39 -07001895 mmiowb();
1896
Pierre Ossmand129bce2006-03-24 03:18:17 -08001897 mmc_add_host(mmc);
1898
Richard Röjforsa13abc72009-09-22 16:45:30 -07001899 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001900 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001901 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1902 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001903
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001904 sdhci_enable_card_detection(host);
1905
Pierre Ossmand129bce2006-03-24 03:18:17 -08001906 return 0;
1907
Pierre Ossmanf9134312008-12-21 17:01:48 +01001908#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001909reset:
1910 sdhci_reset(host, SDHCI_RESET_ALL);
1911 free_irq(host->irq, host);
1912#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001913untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001914 tasklet_kill(&host->card_tasklet);
1915 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001916
1917 return ret;
1918}
1919
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001920EXPORT_SYMBOL_GPL(sdhci_add_host);
1921
Pierre Ossman1e728592008-04-16 19:13:13 +02001922void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001923{
Pierre Ossman1e728592008-04-16 19:13:13 +02001924 unsigned long flags;
1925
1926 if (dead) {
1927 spin_lock_irqsave(&host->lock, flags);
1928
1929 host->flags |= SDHCI_DEVICE_DEAD;
1930
1931 if (host->mrq) {
1932 printk(KERN_ERR "%s: Controller removed during "
1933 " transfer!\n", mmc_hostname(host->mmc));
1934
1935 host->mrq->cmd->error = -ENOMEDIUM;
1936 tasklet_schedule(&host->finish_tasklet);
1937 }
1938
1939 spin_unlock_irqrestore(&host->lock, flags);
1940 }
1941
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001942 sdhci_disable_card_detection(host);
1943
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001944 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001945
Pierre Ossmanf9134312008-12-21 17:01:48 +01001946#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001947 led_classdev_unregister(&host->led);
1948#endif
1949
Pierre Ossman1e728592008-04-16 19:13:13 +02001950 if (!dead)
1951 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001952
1953 free_irq(host->irq, host);
1954
1955 del_timer_sync(&host->timer);
1956
1957 tasklet_kill(&host->card_tasklet);
1958 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001959
1960 kfree(host->adma_desc);
1961 kfree(host->align_buffer);
1962
1963 host->adma_desc = NULL;
1964 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001965}
1966
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001967EXPORT_SYMBOL_GPL(sdhci_remove_host);
1968
1969void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001970{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001971 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972}
1973
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001974EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001975
1976/*****************************************************************************\
1977 * *
1978 * Driver init/exit *
1979 * *
1980\*****************************************************************************/
1981
1982static int __init sdhci_drv_init(void)
1983{
1984 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001985 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001986 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1987
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001988 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001989}
1990
1991static void __exit sdhci_drv_exit(void)
1992{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001993}
1994
1995module_init(sdhci_drv_init);
1996module_exit(sdhci_drv_exit);
1997
Pierre Ossmandf673b22006-06-30 02:22:31 -07001998module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001999
Pierre Ossman32710e82009-04-08 20:14:54 +02002000MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002001MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002002MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002003
Pierre Ossmandf673b22006-06-30 02:22:31 -07002004MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");