blob: 1432a35690dd59729f2626df5b1475381bd83750 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019#include <linux/dma-mapping.h>
Ralf Baechle11763602007-10-23 20:42:11 +020020#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021
Pierre Ossman2f730fe2008-03-17 10:29:38 +010022#include <linux/leds.h>
23
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include "sdhci.h"
27
28#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010031 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmanf9134312008-12-21 17:01:48 +010033#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
34 defined(CONFIG_MMC_SDHCI_MODULE))
35#define SDHCI_USE_LEDS_CLASS
36#endif
37
Pierre Ossmandf673b22006-06-30 02:22:31 -070038static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
41static void sdhci_finish_data(struct sdhci_host *);
42
43static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
44static void sdhci_finish_command(struct sdhci_host *);
45
46static void sdhci_dumpregs(struct sdhci_host *host)
47{
48 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
49
50 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030051 sdhci_readl(host, SDHCI_DMA_ADDRESS),
52 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080053 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readw(host, SDHCI_BLOCK_SIZE),
55 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readl(host, SDHCI_ARGUMENT),
58 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_PRESENT_STATE),
61 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readb(host, SDHCI_POWER_CONTROL),
64 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
67 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
70 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readl(host, SDHCI_INT_ENABLE),
73 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readw(host, SDHCI_ACMD12_ERR),
76 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readl(host, SDHCI_CAPABILITIES),
79 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080080
81 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
82}
83
84/*****************************************************************************\
85 * *
86 * Low level functions *
87 * *
88\*****************************************************************************/
89
Anton Vorontsov7260cf52009-03-17 00:13:48 +030090static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
91{
92 u32 ier;
93
94 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
95 ier &= ~clear;
96 ier |= set;
97 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
98 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
99}
100
101static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
102{
103 sdhci_clear_set_irqs(host, 0, irqs);
104}
105
106static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
107{
108 sdhci_clear_set_irqs(host, irqs, 0);
109}
110
111static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
112{
113 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
114
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300115 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
116 return;
117
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300118 if (enable)
119 sdhci_unmask_irqs(host, irqs);
120 else
121 sdhci_mask_irqs(host, irqs);
122}
123
124static void sdhci_enable_card_detection(struct sdhci_host *host)
125{
126 sdhci_set_card_detection(host, true);
127}
128
129static void sdhci_disable_card_detection(struct sdhci_host *host)
130{
131 sdhci_set_card_detection(host, false);
132}
133
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134static void sdhci_reset(struct sdhci_host *host, u8 mask)
135{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700136 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300137 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700138
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100139 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300140 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700141 SDHCI_CARD_PRESENT))
142 return;
143 }
144
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300145 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
146 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
147
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300148 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800149
Pierre Ossmane16514d82006-06-30 02:22:24 -0700150 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800151 host->clock = 0;
152
Pierre Ossmane16514d82006-06-30 02:22:24 -0700153 /* Wait max 100 ms */
154 timeout = 100;
155
156 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300157 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700158 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100159 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700160 mmc_hostname(host->mmc), (int)mask);
161 sdhci_dumpregs(host);
162 return;
163 }
164 timeout--;
165 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300167
168 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
169 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800170}
171
172static void sdhci_init(struct sdhci_host *host)
173{
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174 sdhci_reset(host, SDHCI_RESET_ALL);
175
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300176 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
177 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700178 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
179 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300180 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300181}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300183static void sdhci_reinit(struct sdhci_host *host)
184{
185 sdhci_init(host);
186 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187}
188
189static void sdhci_activate_led(struct sdhci_host *host)
190{
191 u8 ctrl;
192
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300195 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800196}
197
198static void sdhci_deactivate_led(struct sdhci_host *host)
199{
200 u8 ctrl;
201
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300202 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300204 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205}
206
Pierre Ossmanf9134312008-12-21 17:01:48 +0100207#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100208static void sdhci_led_control(struct led_classdev *led,
209 enum led_brightness brightness)
210{
211 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
212 unsigned long flags;
213
214 spin_lock_irqsave(&host->lock, flags);
215
216 if (brightness == LED_OFF)
217 sdhci_deactivate_led(host);
218 else
219 sdhci_activate_led(host);
220
221 spin_unlock_irqrestore(&host->lock, flags);
222}
223#endif
224
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225/*****************************************************************************\
226 * *
227 * Core functions *
228 * *
229\*****************************************************************************/
230
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100231static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232{
Pierre Ossman76591502008-07-21 00:32:11 +0200233 unsigned long flags;
234 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700235 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200236 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100238 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800239
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100240 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200241 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242
Pierre Ossman76591502008-07-21 00:32:11 +0200243 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100245 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200246 if (!sg_miter_next(&host->sg_miter))
247 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Pierre Ossman76591502008-07-21 00:32:11 +0200249 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800250
Pierre Ossman76591502008-07-21 00:32:11 +0200251 blksize -= len;
252 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200253
Pierre Ossman76591502008-07-21 00:32:11 +0200254 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255
Pierre Ossman76591502008-07-21 00:32:11 +0200256 while (len) {
257 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300258 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200259 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260 }
Pierre Ossman76591502008-07-21 00:32:11 +0200261
262 *buf = scratch & 0xFF;
263
264 buf++;
265 scratch >>= 8;
266 chunk--;
267 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268 }
269 }
Pierre Ossman76591502008-07-21 00:32:11 +0200270
271 sg_miter_stop(&host->sg_miter);
272
273 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100274}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100276static void sdhci_write_block_pio(struct sdhci_host *host)
277{
Pierre Ossman76591502008-07-21 00:32:11 +0200278 unsigned long flags;
279 size_t blksize, len, chunk;
280 u32 scratch;
281 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100282
283 DBG("PIO writing\n");
284
285 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200286 chunk = 0;
287 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100288
Pierre Ossman76591502008-07-21 00:32:11 +0200289 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100290
291 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200292 if (!sg_miter_next(&host->sg_miter))
293 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100294
Pierre Ossman76591502008-07-21 00:32:11 +0200295 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200296
Pierre Ossman76591502008-07-21 00:32:11 +0200297 blksize -= len;
298 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299
Pierre Ossman76591502008-07-21 00:32:11 +0200300 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100301
Pierre Ossman76591502008-07-21 00:32:11 +0200302 while (len) {
303 scratch |= (u32)*buf << (chunk * 8);
304
305 buf++;
306 chunk++;
307 len--;
308
309 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300310 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200311 chunk = 0;
312 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314 }
315 }
Pierre Ossman76591502008-07-21 00:32:11 +0200316
317 sg_miter_stop(&host->sg_miter);
318
319 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320}
321
322static void sdhci_transfer_pio(struct sdhci_host *host)
323{
324 u32 mask;
325
326 BUG_ON(!host->data);
327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329 return;
330
331 if (host->data->flags & MMC_DATA_READ)
332 mask = SDHCI_DATA_AVAILABLE;
333 else
334 mask = SDHCI_SPACE_AVAILABLE;
335
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200336 /*
337 * Some controllers (JMicron JMB38x) mess up the buffer bits
338 * for transfers < 4 bytes. As long as it is just one block,
339 * we can ignore the bits.
340 */
341 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
342 (host->data->blocks == 1))
343 mask = ~0;
344
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300345 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300346 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
347 udelay(100);
348
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349 if (host->data->flags & MMC_DATA_READ)
350 sdhci_read_block_pio(host);
351 else
352 sdhci_write_block_pio(host);
353
Pierre Ossman76591502008-07-21 00:32:11 +0200354 host->blocks--;
355 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357 }
358
359 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800360}
361
Pierre Ossman2134a922008-06-28 18:28:51 +0200362static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
363{
364 local_irq_save(*flags);
365 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
366}
367
368static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
369{
370 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
371 local_irq_restore(*flags);
372}
373
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200374static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200375 struct mmc_data *data)
376{
377 int direction;
378
379 u8 *desc;
380 u8 *align;
381 dma_addr_t addr;
382 dma_addr_t align_addr;
383 int len, offset;
384
385 struct scatterlist *sg;
386 int i;
387 char *buffer;
388 unsigned long flags;
389
390 /*
391 * The spec does not specify endianness of descriptor table.
392 * We currently guess that it is LE.
393 */
394
395 if (data->flags & MMC_DATA_READ)
396 direction = DMA_FROM_DEVICE;
397 else
398 direction = DMA_TO_DEVICE;
399
400 /*
401 * The ADMA descriptor table is mapped further down as we
402 * need to fill it with data first.
403 */
404
405 host->align_addr = dma_map_single(mmc_dev(host->mmc),
406 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700407 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200408 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200409 BUG_ON(host->align_addr & 0x3);
410
411 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
412 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200413 if (host->sg_count == 0)
414 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200415
416 desc = host->adma_desc;
417 align = host->align_buffer;
418
419 align_addr = host->align_addr;
420
421 for_each_sg(data->sg, sg, host->sg_count, i) {
422 addr = sg_dma_address(sg);
423 len = sg_dma_len(sg);
424
425 /*
426 * The SDHCI specification states that ADMA
427 * addresses must be 32-bit aligned. If they
428 * aren't, then we use a bounce buffer for
429 * the (up to three) bytes that screw up the
430 * alignment.
431 */
432 offset = (4 - (addr & 0x3)) & 0x3;
433 if (offset) {
434 if (data->flags & MMC_DATA_WRITE) {
435 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200436 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200437 memcpy(align, buffer, offset);
438 sdhci_kunmap_atomic(buffer, &flags);
439 }
440
441 desc[7] = (align_addr >> 24) & 0xff;
442 desc[6] = (align_addr >> 16) & 0xff;
443 desc[5] = (align_addr >> 8) & 0xff;
444 desc[4] = (align_addr >> 0) & 0xff;
445
446 BUG_ON(offset > 65536);
447
448 desc[3] = (offset >> 8) & 0xff;
449 desc[2] = (offset >> 0) & 0xff;
450
451 desc[1] = 0x00;
452 desc[0] = 0x21; /* tran, valid */
453
454 align += 4;
455 align_addr += 4;
456
457 desc += 8;
458
459 addr += offset;
460 len -= offset;
461 }
462
463 desc[7] = (addr >> 24) & 0xff;
464 desc[6] = (addr >> 16) & 0xff;
465 desc[5] = (addr >> 8) & 0xff;
466 desc[4] = (addr >> 0) & 0xff;
467
468 BUG_ON(len > 65536);
469
470 desc[3] = (len >> 8) & 0xff;
471 desc[2] = (len >> 0) & 0xff;
472
473 desc[1] = 0x00;
474 desc[0] = 0x21; /* tran, valid */
475
476 desc += 8;
477
478 /*
479 * If this triggers then we have a calculation bug
480 * somewhere. :/
481 */
482 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
483 }
484
485 /*
486 * Add a terminating entry.
487 */
488 desc[7] = 0;
489 desc[6] = 0;
490 desc[5] = 0;
491 desc[4] = 0;
492
493 desc[3] = 0;
494 desc[2] = 0;
495
496 desc[1] = 0x00;
497 desc[0] = 0x03; /* nop, end, valid */
498
499 /*
500 * Resync align buffer as we might have changed it.
501 */
502 if (data->flags & MMC_DATA_WRITE) {
503 dma_sync_single_for_device(mmc_dev(host->mmc),
504 host->align_addr, 128 * 4, direction);
505 }
506
507 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
508 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200509 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200510 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200511 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200512
513 return 0;
514
515unmap_entries:
516 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
517 data->sg_len, direction);
518unmap_align:
519 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
520 128 * 4, direction);
521fail:
522 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200523}
524
525static void sdhci_adma_table_post(struct sdhci_host *host,
526 struct mmc_data *data)
527{
528 int direction;
529
530 struct scatterlist *sg;
531 int i, size;
532 u8 *align;
533 char *buffer;
534 unsigned long flags;
535
536 if (data->flags & MMC_DATA_READ)
537 direction = DMA_FROM_DEVICE;
538 else
539 direction = DMA_TO_DEVICE;
540
541 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
542 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
543
544 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
545 128 * 4, direction);
546
547 if (data->flags & MMC_DATA_READ) {
548 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
549 data->sg_len, direction);
550
551 align = host->align_buffer;
552
553 for_each_sg(data->sg, sg, host->sg_count, i) {
554 if (sg_dma_address(sg) & 0x3) {
555 size = 4 - (sg_dma_address(sg) & 0x3);
556
557 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200558 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 memcpy(buffer, align, size);
560 sdhci_kunmap_atomic(buffer, &flags);
561
562 align += 4;
563 }
564 }
565 }
566
567 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
568 data->sg_len, direction);
569}
570
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200571static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800572{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700573 u8 count;
574 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800575
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200576 /*
577 * If the host controller provides us with an incorrect timeout
578 * value, just skip the check and use 0xE. The hardware may take
579 * longer to time out, but that's much better than having a too-short
580 * timeout value.
581 */
582 if ((host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL))
583 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200584
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700585 /* timeout in us */
586 target_timeout = data->timeout_ns / 1000 +
587 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800588
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700589 /*
590 * Figure out needed cycles.
591 * We do this in steps in order to fit inside a 32 bit int.
592 * The first step is the minimum timeout, which will have a
593 * minimum resolution of 6 bits:
594 * (1) 2^13*1000 > 2^22,
595 * (2) host->timeout_clk < 2^16
596 * =>
597 * (1) / (2) > 2^6
598 */
599 count = 0;
600 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
601 while (current_timeout < target_timeout) {
602 count++;
603 current_timeout <<= 1;
604 if (count >= 0xF)
605 break;
606 }
607
608 if (count >= 0xF) {
609 printk(KERN_WARNING "%s: Too large timeout requested!\n",
610 mmc_hostname(host->mmc));
611 count = 0xE;
612 }
613
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200614 return count;
615}
616
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300617static void sdhci_set_transfer_irqs(struct sdhci_host *host)
618{
619 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
620 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
621
622 if (host->flags & SDHCI_REQ_USE_DMA)
623 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
624 else
625 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
626}
627
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200628static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
629{
630 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200631 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200632 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200633
634 WARN_ON(host->data);
635
636 if (data == NULL)
637 return;
638
639 /* Sanity checks */
640 BUG_ON(data->blksz * data->blocks > 524288);
641 BUG_ON(data->blksz > host->mmc->max_blk_size);
642 BUG_ON(data->blocks > 65535);
643
644 host->data = data;
645 host->data_early = 0;
646
647 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300648 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800649
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100650 if (host->flags & SDHCI_USE_DMA)
651 host->flags |= SDHCI_REQ_USE_DMA;
652
Pierre Ossman2134a922008-06-28 18:28:51 +0200653 /*
654 * FIXME: This doesn't account for merging when mapping the
655 * scatterlist.
656 */
657 if (host->flags & SDHCI_REQ_USE_DMA) {
658 int broken, i;
659 struct scatterlist *sg;
660
661 broken = 0;
662 if (host->flags & SDHCI_USE_ADMA) {
663 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
664 broken = 1;
665 } else {
666 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
667 broken = 1;
668 }
669
670 if (unlikely(broken)) {
671 for_each_sg(data->sg, sg, data->sg_len, i) {
672 if (sg->length & 0x3) {
673 DBG("Reverting to PIO because of "
674 "transfer size (%d)\n",
675 sg->length);
676 host->flags &= ~SDHCI_REQ_USE_DMA;
677 break;
678 }
679 }
680 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100681 }
682
683 /*
684 * The assumption here being that alignment is the same after
685 * translation to device address space.
686 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200687 if (host->flags & SDHCI_REQ_USE_DMA) {
688 int broken, i;
689 struct scatterlist *sg;
690
691 broken = 0;
692 if (host->flags & SDHCI_USE_ADMA) {
693 /*
694 * As we use 3 byte chunks to work around
695 * alignment problems, we need to check this
696 * quirk.
697 */
698 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
699 broken = 1;
700 } else {
701 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
702 broken = 1;
703 }
704
705 if (unlikely(broken)) {
706 for_each_sg(data->sg, sg, data->sg_len, i) {
707 if (sg->offset & 0x3) {
708 DBG("Reverting to PIO because of "
709 "bad alignment\n");
710 host->flags &= ~SDHCI_REQ_USE_DMA;
711 break;
712 }
713 }
714 }
715 }
716
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200717 if (host->flags & SDHCI_REQ_USE_DMA) {
718 if (host->flags & SDHCI_USE_ADMA) {
719 ret = sdhci_adma_table_pre(host, data);
720 if (ret) {
721 /*
722 * This only happens when someone fed
723 * us an invalid request.
724 */
725 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200726 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200727 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300728 sdhci_writel(host, host->adma_addr,
729 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200730 }
731 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300732 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200733
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300734 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200735 data->sg, data->sg_len,
736 (data->flags & MMC_DATA_READ) ?
737 DMA_FROM_DEVICE :
738 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300739 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200740 /*
741 * This only happens when someone fed
742 * us an invalid request.
743 */
744 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200745 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200746 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200747 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300748 sdhci_writel(host, sg_dma_address(data->sg),
749 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200750 }
751 }
752 }
753
Pierre Ossman2134a922008-06-28 18:28:51 +0200754 /*
755 * Always adjust the DMA selection as some controllers
756 * (e.g. JMicron) can't do PIO properly when the selection
757 * is ADMA.
758 */
759 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300760 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200761 ctrl &= ~SDHCI_CTRL_DMA_MASK;
762 if ((host->flags & SDHCI_REQ_USE_DMA) &&
763 (host->flags & SDHCI_USE_ADMA))
764 ctrl |= SDHCI_CTRL_ADMA32;
765 else
766 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300767 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100768 }
769
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200770 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Pierre Ossman76591502008-07-21 00:32:11 +0200771 sg_miter_start(&host->sg_miter,
772 data->sg, data->sg_len, SG_MITER_ATOMIC);
773 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800774 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700775
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300776 sdhci_set_transfer_irqs(host);
777
Pierre Ossmanbab76962006-07-02 16:51:35 +0100778 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300779 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
780 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700781}
782
783static void sdhci_set_transfer_mode(struct sdhci_host *host,
784 struct mmc_data *data)
785{
786 u16 mode;
787
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700788 if (data == NULL)
789 return;
790
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200791 WARN_ON(!host->data);
792
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700793 mode = SDHCI_TRNS_BLK_CNT_EN;
794 if (data->blocks > 1)
795 mode |= SDHCI_TRNS_MULTI;
796 if (data->flags & MMC_DATA_READ)
797 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100798 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700799 mode |= SDHCI_TRNS_DMA;
800
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300801 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800802}
803
804static void sdhci_finish_data(struct sdhci_host *host)
805{
806 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800807
808 BUG_ON(!host->data);
809
810 data = host->data;
811 host->data = NULL;
812
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100813 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200814 if (host->flags & SDHCI_USE_ADMA)
815 sdhci_adma_table_post(host, data);
816 else {
817 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
818 data->sg_len, (data->flags & MMC_DATA_READ) ?
819 DMA_FROM_DEVICE : DMA_TO_DEVICE);
820 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800821 }
822
823 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200824 * The specification states that the block count register must
825 * be updated, but it does not specify at what point in the
826 * data flow. That makes the register entirely useless to read
827 * back so we have to assume that nothing made it to the card
828 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800829 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200830 if (data->error)
831 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800832 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200833 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800834
Pierre Ossmand129bce2006-03-24 03:18:17 -0800835 if (data->stop) {
836 /*
837 * The controller needs a reset of internal state machines
838 * upon error conditions.
839 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200840 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800841 sdhci_reset(host, SDHCI_RESET_CMD);
842 sdhci_reset(host, SDHCI_RESET_DATA);
843 }
844
845 sdhci_send_command(host, data->stop);
846 } else
847 tasklet_schedule(&host->finish_tasklet);
848}
849
850static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
851{
852 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700853 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700854 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800855
856 WARN_ON(host->cmd);
857
Pierre Ossmand129bce2006-03-24 03:18:17 -0800858 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700859 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700860
861 mask = SDHCI_CMD_INHIBIT;
862 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
863 mask |= SDHCI_DATA_INHIBIT;
864
865 /* We shouldn't wait for data inihibit for stop commands, even
866 though they might use busy signaling */
867 if (host->mrq->data && (cmd == host->mrq->data->stop))
868 mask &= ~SDHCI_DATA_INHIBIT;
869
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300870 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700871 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100873 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800874 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200875 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800876 tasklet_schedule(&host->finish_tasklet);
877 return;
878 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700879 timeout--;
880 mdelay(1);
881 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800882
883 mod_timer(&host->timer, jiffies + 10 * HZ);
884
885 host->cmd = cmd;
886
887 sdhci_prepare_data(host, cmd->data);
888
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300889 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700891 sdhci_set_transfer_mode(host, cmd->data);
892
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100894 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800895 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200896 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800897 tasklet_schedule(&host->finish_tasklet);
898 return;
899 }
900
901 if (!(cmd->flags & MMC_RSP_PRESENT))
902 flags = SDHCI_CMD_RESP_NONE;
903 else if (cmd->flags & MMC_RSP_136)
904 flags = SDHCI_CMD_RESP_LONG;
905 else if (cmd->flags & MMC_RSP_BUSY)
906 flags = SDHCI_CMD_RESP_SHORT_BUSY;
907 else
908 flags = SDHCI_CMD_RESP_SHORT;
909
910 if (cmd->flags & MMC_RSP_CRC)
911 flags |= SDHCI_CMD_CRC;
912 if (cmd->flags & MMC_RSP_OPCODE)
913 flags |= SDHCI_CMD_INDEX;
914 if (cmd->data)
915 flags |= SDHCI_CMD_DATA;
916
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300917 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918}
919
920static void sdhci_finish_command(struct sdhci_host *host)
921{
922 int i;
923
924 BUG_ON(host->cmd == NULL);
925
926 if (host->cmd->flags & MMC_RSP_PRESENT) {
927 if (host->cmd->flags & MMC_RSP_136) {
928 /* CRC is stripped so we need to do some shifting. */
929 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300930 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 SDHCI_RESPONSE + (3-i)*4) << 8;
932 if (i != 3)
933 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300934 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 SDHCI_RESPONSE + (3-i)*4-1);
936 }
937 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300938 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 }
940 }
941
Pierre Ossman17b04292007-07-22 22:18:46 +0200942 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200944 if (host->data && host->data_early)
945 sdhci_finish_data(host);
946
947 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 tasklet_schedule(&host->finish_tasklet);
949
950 host->cmd = NULL;
951}
952
953static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
954{
955 int div;
956 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700957 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958
959 if (clock == host->clock)
960 return;
961
Anton Vorontsov81146342009-03-17 00:13:59 +0300962 if (host->ops->set_clock) {
963 host->ops->set_clock(host, clock);
964 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
965 return;
966 }
967
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300968 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
970 if (clock == 0)
971 goto out;
972
973 for (div = 1;div < 256;div *= 2) {
974 if ((host->max_clk / div) <= clock)
975 break;
976 }
977 div >>= 1;
978
979 clk = div << SDHCI_DIVIDER_SHIFT;
980 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300981 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982
983 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700984 timeout = 10;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300985 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700986 & SDHCI_CLOCK_INT_STABLE)) {
987 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100988 printk(KERN_ERR "%s: Internal clock never "
989 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 sdhci_dumpregs(host);
991 return;
992 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700993 timeout--;
994 mdelay(1);
995 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996
997 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300998 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800999
1000out:
1001 host->clock = clock;
1002}
1003
Pierre Ossman146ad662006-06-30 02:22:23 -07001004static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1005{
1006 u8 pwr;
1007
Pierre Ossmanae628902009-05-03 20:45:03 +02001008 if (power == (unsigned short)-1)
1009 pwr = 0;
1010 else {
1011 switch (1 << power) {
1012 case MMC_VDD_165_195:
1013 pwr = SDHCI_POWER_180;
1014 break;
1015 case MMC_VDD_29_30:
1016 case MMC_VDD_30_31:
1017 pwr = SDHCI_POWER_300;
1018 break;
1019 case MMC_VDD_32_33:
1020 case MMC_VDD_33_34:
1021 pwr = SDHCI_POWER_330;
1022 break;
1023 default:
1024 BUG();
1025 }
1026 }
1027
1028 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001029 return;
1030
Pierre Ossmanae628902009-05-03 20:45:03 +02001031 host->pwr = pwr;
1032
1033 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001034 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001035 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001036 }
1037
1038 /*
1039 * Spec says that we should clear the power reg before setting
1040 * a new value. Some controllers don't seem to like this though.
1041 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001042 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001044
Andres Salomone08c1692008-07-04 10:00:03 -07001045 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001046 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001047 * and set turn on power at the same time, so set the voltage first.
1048 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001049 if ((host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER))
Pierre Ossmanae628902009-05-03 20:45:03 +02001050 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1051
1052 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001053
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001055}
1056
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057/*****************************************************************************\
1058 * *
1059 * MMC callbacks *
1060 * *
1061\*****************************************************************************/
1062
1063static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1064{
1065 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001066 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067 unsigned long flags;
1068
1069 host = mmc_priv(mmc);
1070
1071 spin_lock_irqsave(&host->lock, flags);
1072
1073 WARN_ON(host->mrq != NULL);
1074
Pierre Ossmanf9134312008-12-21 17:01:48 +01001075#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001076 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001077#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078
1079 host->mrq = mrq;
1080
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001081 /* If polling, assume that the card is always present. */
1082 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1083 present = true;
1084 else
1085 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1086 SDHCI_CARD_PRESENT;
1087
1088 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001089 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001090 tasklet_schedule(&host->finish_tasklet);
1091 } else
1092 sdhci_send_command(host, mrq->cmd);
1093
Pierre Ossman5f25a662006-10-04 02:15:39 -07001094 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 spin_unlock_irqrestore(&host->lock, flags);
1096}
1097
1098static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1099{
1100 struct sdhci_host *host;
1101 unsigned long flags;
1102 u8 ctrl;
1103
1104 host = mmc_priv(mmc);
1105
1106 spin_lock_irqsave(&host->lock, flags);
1107
Pierre Ossman1e728592008-04-16 19:13:13 +02001108 if (host->flags & SDHCI_DEVICE_DEAD)
1109 goto out;
1110
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111 /*
1112 * Reset the chip on each power off.
1113 * Should clear out any weird states.
1114 */
1115 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001116 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001117 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118 }
1119
1120 sdhci_set_clock(host, ios->clock);
1121
1122 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001123 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001125 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001127 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001128
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129 if (ios->bus_width == MMC_BUS_WIDTH_4)
1130 ctrl |= SDHCI_CTRL_4BITBUS;
1131 else
1132 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001133
1134 if (ios->timing == MMC_TIMING_SD_HS)
1135 ctrl |= SDHCI_CTRL_HISPD;
1136 else
1137 ctrl &= ~SDHCI_CTRL_HISPD;
1138
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001139 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Leandro Dorileob8352262007-07-25 23:47:04 +02001141 /*
1142 * Some (ENE) controllers go apeshit on some ios operation,
1143 * signalling timeout and CRC errors even on CMD0. Resetting
1144 * it on each ios seems to solve the problem.
1145 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001146 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001147 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1148
Pierre Ossman1e728592008-04-16 19:13:13 +02001149out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001150 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 spin_unlock_irqrestore(&host->lock, flags);
1152}
1153
1154static int sdhci_get_ro(struct mmc_host *mmc)
1155{
1156 struct sdhci_host *host;
1157 unsigned long flags;
1158 int present;
1159
1160 host = mmc_priv(mmc);
1161
1162 spin_lock_irqsave(&host->lock, flags);
1163
Pierre Ossman1e728592008-04-16 19:13:13 +02001164 if (host->flags & SDHCI_DEVICE_DEAD)
1165 present = 0;
1166 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168
1169 spin_unlock_irqrestore(&host->lock, flags);
1170
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001171 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1172 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 return !(present & SDHCI_WRITE_PROTECT);
1174}
1175
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001176static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1177{
1178 struct sdhci_host *host;
1179 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001180
1181 host = mmc_priv(mmc);
1182
1183 spin_lock_irqsave(&host->lock, flags);
1184
Pierre Ossman1e728592008-04-16 19:13:13 +02001185 if (host->flags & SDHCI_DEVICE_DEAD)
1186 goto out;
1187
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001188 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001189 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1190 else
1191 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001192out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001193 mmiowb();
1194
1195 spin_unlock_irqrestore(&host->lock, flags);
1196}
1197
David Brownellab7aefd2006-11-12 17:55:30 -08001198static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199 .request = sdhci_request,
1200 .set_ios = sdhci_set_ios,
1201 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001202 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203};
1204
1205/*****************************************************************************\
1206 * *
1207 * Tasklets *
1208 * *
1209\*****************************************************************************/
1210
1211static void sdhci_tasklet_card(unsigned long param)
1212{
1213 struct sdhci_host *host;
1214 unsigned long flags;
1215
1216 host = (struct sdhci_host*)param;
1217
1218 spin_lock_irqsave(&host->lock, flags);
1219
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001220 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221 if (host->mrq) {
1222 printk(KERN_ERR "%s: Card removed during transfer!\n",
1223 mmc_hostname(host->mmc));
1224 printk(KERN_ERR "%s: Resetting controller.\n",
1225 mmc_hostname(host->mmc));
1226
1227 sdhci_reset(host, SDHCI_RESET_CMD);
1228 sdhci_reset(host, SDHCI_RESET_DATA);
1229
Pierre Ossman17b04292007-07-22 22:18:46 +02001230 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231 tasklet_schedule(&host->finish_tasklet);
1232 }
1233 }
1234
1235 spin_unlock_irqrestore(&host->lock, flags);
1236
Pierre Ossman04cf5852008-08-18 22:18:14 +02001237 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238}
1239
1240static void sdhci_tasklet_finish(unsigned long param)
1241{
1242 struct sdhci_host *host;
1243 unsigned long flags;
1244 struct mmc_request *mrq;
1245
1246 host = (struct sdhci_host*)param;
1247
1248 spin_lock_irqsave(&host->lock, flags);
1249
1250 del_timer(&host->timer);
1251
1252 mrq = host->mrq;
1253
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 /*
1255 * The controller needs a reset of internal state machines
1256 * upon error conditions.
1257 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001258 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1259 (mrq->cmd->error ||
1260 (mrq->data && (mrq->data->error ||
1261 (mrq->data->stop && mrq->data->stop->error))) ||
1262 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001263
1264 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001265 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001266 unsigned int clock;
1267
1268 /* This is to force an update */
1269 clock = host->clock;
1270 host->clock = 0;
1271 sdhci_set_clock(host, clock);
1272 }
1273
1274 /* Spec says we should do both at the same time, but Ricoh
1275 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 sdhci_reset(host, SDHCI_RESET_CMD);
1277 sdhci_reset(host, SDHCI_RESET_DATA);
1278 }
1279
1280 host->mrq = NULL;
1281 host->cmd = NULL;
1282 host->data = NULL;
1283
Pierre Ossmanf9134312008-12-21 17:01:48 +01001284#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001285 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001286#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287
Pierre Ossman5f25a662006-10-04 02:15:39 -07001288 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001289 spin_unlock_irqrestore(&host->lock, flags);
1290
1291 mmc_request_done(host->mmc, mrq);
1292}
1293
1294static void sdhci_timeout_timer(unsigned long data)
1295{
1296 struct sdhci_host *host;
1297 unsigned long flags;
1298
1299 host = (struct sdhci_host*)data;
1300
1301 spin_lock_irqsave(&host->lock, flags);
1302
1303 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001304 printk(KERN_ERR "%s: Timeout waiting for hardware "
1305 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 sdhci_dumpregs(host);
1307
1308 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001309 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 sdhci_finish_data(host);
1311 } else {
1312 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001313 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001315 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001316
1317 tasklet_schedule(&host->finish_tasklet);
1318 }
1319 }
1320
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
1325/*****************************************************************************\
1326 * *
1327 * Interrupt handling *
1328 * *
1329\*****************************************************************************/
1330
1331static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1332{
1333 BUG_ON(intmask == 0);
1334
1335 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001336 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1337 "though no command operation was in progress.\n",
1338 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339 sdhci_dumpregs(host);
1340 return;
1341 }
1342
Pierre Ossman43b58b32007-07-25 23:15:27 +02001343 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001344 host->cmd->error = -ETIMEDOUT;
1345 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1346 SDHCI_INT_INDEX))
1347 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001348
Pierre Ossmane8095172008-07-25 01:09:08 +02001349 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001351 return;
1352 }
1353
1354 /*
1355 * The host can send and interrupt when the busy state has
1356 * ended, allowing us to wait without wasting CPU cycles.
1357 * Unfortunately this is overloaded on the "data complete"
1358 * interrupt, so we need to take some care when handling
1359 * it.
1360 *
1361 * Note: The 1.0 specification is a bit ambiguous about this
1362 * feature so there might be some problems with older
1363 * controllers.
1364 */
1365 if (host->cmd->flags & MMC_RSP_BUSY) {
1366 if (host->cmd->data)
1367 DBG("Cannot wait for busy signal when also "
1368 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001369 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001370 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001371
1372 /* The controller does not support the end-of-busy IRQ,
1373 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001374 }
1375
1376 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001377 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378}
1379
1380static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1381{
1382 BUG_ON(intmask == 0);
1383
1384 if (!host->data) {
1385 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001386 * The "data complete" interrupt is also used to
1387 * indicate that a busy state has ended. See comment
1388 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001390 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1391 if (intmask & SDHCI_INT_DATA_END) {
1392 sdhci_finish_command(host);
1393 return;
1394 }
1395 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001397 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1398 "though no data operation was in progress.\n",
1399 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400 sdhci_dumpregs(host);
1401
1402 return;
1403 }
1404
1405 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001406 host->data->error = -ETIMEDOUT;
1407 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1408 host->data->error = -EILSEQ;
Pierre Ossman2134a922008-06-28 18:28:51 +02001409 else if (intmask & SDHCI_INT_ADMA_ERROR)
1410 host->data->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001411
Pierre Ossman17b04292007-07-22 22:18:46 +02001412 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001413 sdhci_finish_data(host);
1414 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001415 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001416 sdhci_transfer_pio(host);
1417
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001418 /*
1419 * We currently don't do anything fancy with DMA
1420 * boundaries, but as we can't disable the feature
1421 * we need to at least restart the transfer.
1422 */
1423 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001424 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1425 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001426
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001427 if (intmask & SDHCI_INT_DATA_END) {
1428 if (host->cmd) {
1429 /*
1430 * Data managed to finish before the
1431 * command completed. Make sure we do
1432 * things in the proper order.
1433 */
1434 host->data_early = 1;
1435 } else {
1436 sdhci_finish_data(host);
1437 }
1438 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001439 }
1440}
1441
David Howells7d12e782006-10-05 14:55:46 +01001442static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001443{
1444 irqreturn_t result;
1445 struct sdhci_host* host = dev_id;
1446 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001447 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001448
1449 spin_lock(&host->lock);
1450
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001451 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001452
Mark Lord62df67a52007-03-06 13:30:13 +01001453 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 result = IRQ_NONE;
1455 goto out;
1456 }
1457
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001458 DBG("*** %s got interrupt: 0x%08x\n",
1459 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460
Pierre Ossman3192a282006-06-30 02:22:26 -07001461 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001462 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1463 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001464 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001465 }
1466
1467 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001468
1469 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001470 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1471 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001472 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001473 }
1474
1475 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001476 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1477 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001478 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001479 }
1480
1481 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1482
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001483 intmask &= ~SDHCI_INT_ERROR;
1484
Pierre Ossmand129bce2006-03-24 03:18:17 -08001485 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001486 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001488 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001489 }
1490
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001491 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001492
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001493 if (intmask & SDHCI_INT_CARD_INT)
1494 cardint = 1;
1495
1496 intmask &= ~SDHCI_INT_CARD_INT;
1497
Pierre Ossman3192a282006-06-30 02:22:26 -07001498 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001499 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001500 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501 sdhci_dumpregs(host);
1502
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001503 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001504 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505
1506 result = IRQ_HANDLED;
1507
Pierre Ossman5f25a662006-10-04 02:15:39 -07001508 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001509out:
1510 spin_unlock(&host->lock);
1511
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001512 /*
1513 * We have to delay this as it calls back into the driver.
1514 */
1515 if (cardint)
1516 mmc_signal_sdio_irq(host->mmc);
1517
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518 return result;
1519}
1520
1521/*****************************************************************************\
1522 * *
1523 * Suspend/resume *
1524 * *
1525\*****************************************************************************/
1526
1527#ifdef CONFIG_PM
1528
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001529int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001531 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001532
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001533 sdhci_disable_card_detection(host);
1534
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001535 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001536 if (ret)
1537 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001539 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001540
1541 return 0;
1542}
1543
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001544EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001545
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001546int sdhci_resume_host(struct sdhci_host *host)
1547{
1548 int ret;
1549
1550 if (host->flags & SDHCI_USE_DMA) {
1551 if (host->ops->enable_dma)
1552 host->ops->enable_dma(host);
1553 }
1554
1555 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1556 mmc_hostname(host->mmc), host);
1557 if (ret)
1558 return ret;
1559
1560 sdhci_init(host);
1561 mmiowb();
1562
1563 ret = mmc_resume_host(host->mmc);
1564 if (ret)
1565 return ret;
1566
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001567 sdhci_enable_card_detection(host);
1568
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001569 return 0;
1570}
1571
1572EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001573
1574#endif /* CONFIG_PM */
1575
1576/*****************************************************************************\
1577 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001578 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001579 * *
1580\*****************************************************************************/
1581
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001582struct sdhci_host *sdhci_alloc_host(struct device *dev,
1583 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001584{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001585 struct mmc_host *mmc;
1586 struct sdhci_host *host;
1587
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001588 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001589
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001590 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001591 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001592 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593
1594 host = mmc_priv(mmc);
1595 host->mmc = mmc;
1596
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001597 return host;
1598}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001599
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001600EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001602int sdhci_add_host(struct sdhci_host *host)
1603{
1604 struct mmc_host *mmc;
1605 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001606 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001607
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001608 WARN_ON(host == NULL);
1609 if (host == NULL)
1610 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001612 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001613
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001614 if (debug_quirks)
1615 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001616
Pierre Ossmand96649e2006-06-30 02:22:30 -07001617 sdhci_reset(host, SDHCI_RESET_ALL);
1618
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001619 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001620 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1621 >> SDHCI_SPEC_VER_SHIFT;
1622 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001623 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001624 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001625 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001626 }
1627
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001628 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001630 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Pierre Ossman98608072006-06-30 02:22:34 -07001631 host->flags |= SDHCI_USE_DMA;
Pierre Ossman67435272006-06-30 02:22:31 -07001632 else if (!(caps & SDHCI_CAN_DO_DMA))
1633 DBG("Controller doesn't have DMA capability\n");
1634 else
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635 host->flags |= SDHCI_USE_DMA;
1636
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001637 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Feng Tang7c168e32007-09-30 12:44:18 +02001638 (host->flags & SDHCI_USE_DMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001639 DBG("Disabling DMA as it is marked broken\n");
Feng Tang7c168e32007-09-30 12:44:18 +02001640 host->flags &= ~SDHCI_USE_DMA;
1641 }
1642
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +02001644 if ((host->version >= SDHCI_SPEC_200) &&
1645 (caps & SDHCI_CAN_DO_ADMA2))
1646 host->flags |= SDHCI_USE_ADMA;
1647 }
1648
1649 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1650 (host->flags & SDHCI_USE_ADMA)) {
1651 DBG("Disabling ADMA as it is marked broken\n");
1652 host->flags &= ~SDHCI_USE_ADMA;
1653 }
1654
1655 if (host->flags & SDHCI_USE_DMA) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001656 if (host->ops->enable_dma) {
1657 if (host->ops->enable_dma(host)) {
1658 printk(KERN_WARNING "%s: No suitable DMA "
1659 "available. Falling back to PIO.\n",
1660 mmc_hostname(mmc));
Pierre Ossman2134a922008-06-28 18:28:51 +02001661 host->flags &= ~(SDHCI_USE_DMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001662 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001663 }
1664 }
1665
Pierre Ossman2134a922008-06-28 18:28:51 +02001666 if (host->flags & SDHCI_USE_ADMA) {
1667 /*
1668 * We need to allocate descriptors for all sg entries
1669 * (128) and potentially one alignment transfer for
1670 * each of those entries.
1671 */
1672 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1673 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1674 if (!host->adma_desc || !host->align_buffer) {
1675 kfree(host->adma_desc);
1676 kfree(host->align_buffer);
1677 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1678 "buffers. Falling back to standard DMA.\n",
1679 mmc_hostname(mmc));
1680 host->flags &= ~SDHCI_USE_ADMA;
1681 }
1682 }
1683
Pierre Ossman76591502008-07-21 00:32:11 +02001684 /*
1685 * If we use DMA, then it's up to the caller to set the DMA
1686 * mask, but PIO does not need the hw shim so we set a new
1687 * mask here in that case.
1688 */
1689 if (!(host->flags & SDHCI_USE_DMA)) {
1690 host->dma_mask = DMA_BIT_MASK(64);
1691 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1692 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001693
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001694 host->max_clk =
1695 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001696 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001697 if (host->max_clk == 0) {
1698 if (!host->ops->get_max_clock) {
1699 printk(KERN_ERR
1700 "%s: Hardware doesn't specify base clock "
1701 "frequency.\n", mmc_hostname(mmc));
1702 return -ENODEV;
1703 }
1704 host->max_clk = host->ops->get_max_clock(host);
1705 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001706
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001707 host->timeout_clk =
1708 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1709 if (host->timeout_clk == 0) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001710 if (!host->ops->get_timeout_clock) {
1711 printk(KERN_ERR
1712 "%s: Hardware doesn't specify timeout clock "
1713 "frequency.\n", mmc_hostname(mmc));
1714 return -ENODEV;
1715 }
1716 host->timeout_clk = host->ops->get_timeout_clock(host);
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001717 }
1718 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1719 host->timeout_clk *= 1000;
1720
Pierre Ossmand129bce2006-03-24 03:18:17 -08001721 /*
1722 * Set host parameters.
1723 */
1724 mmc->ops = &sdhci_ops;
1725 mmc->f_min = host->max_clk / 256;
1726 mmc->f_max = host->max_clk;
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001727 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001728
Pierre Ossman86a6a872009-02-02 21:13:49 +01001729 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001730 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1731
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001732 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1733 mmc->caps |= MMC_CAP_NEEDS_POLL;
1734
Pierre Ossman146ad662006-06-30 02:22:23 -07001735 mmc->ocr_avail = 0;
1736 if (caps & SDHCI_CAN_VDD_330)
1737 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001738 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001739 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001740 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001741 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001742
1743 if (mmc->ocr_avail == 0) {
1744 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001745 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001746 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001747 }
1748
Pierre Ossmand129bce2006-03-24 03:18:17 -08001749 spin_lock_init(&host->lock);
1750
1751 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001752 * Maximum number of segments. Depends on if the hardware
1753 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001755 if (host->flags & SDHCI_USE_ADMA)
1756 mmc->max_hw_segs = 128;
1757 else if (host->flags & SDHCI_USE_DMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001758 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001759 else /* PIO */
1760 mmc->max_hw_segs = 128;
1761 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001762
1763 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001764 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001765 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001766 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001767 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768
1769 /*
1770 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001771 * of bytes. When doing hardware scatter/gather, each entry cannot
1772 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001773 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001774 if (host->flags & SDHCI_USE_ADMA)
1775 mmc->max_seg_size = 65536;
1776 else
1777 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001778
1779 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001780 * Maximum block size. This varies from controller to controller and
1781 * is specified in the capabilities register.
1782 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001783 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1784 mmc->max_blk_size = 2;
1785 } else {
1786 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1787 SDHCI_MAX_BLOCK_SHIFT;
1788 if (mmc->max_blk_size >= 3) {
1789 printk(KERN_WARNING "%s: Invalid maximum block size, "
1790 "assuming 512 bytes\n", mmc_hostname(mmc));
1791 mmc->max_blk_size = 0;
1792 }
1793 }
1794
1795 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001796
1797 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001798 * Maximum block count.
1799 */
1800 mmc->max_blk_count = 65535;
1801
1802 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001803 * Init tasklets.
1804 */
1805 tasklet_init(&host->card_tasklet,
1806 sdhci_tasklet_card, (unsigned long)host);
1807 tasklet_init(&host->finish_tasklet,
1808 sdhci_tasklet_finish, (unsigned long)host);
1809
Al Viroe4cad1b2006-10-10 22:47:07 +01001810 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001812 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001813 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001814 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001815 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816
1817 sdhci_init(host);
1818
1819#ifdef CONFIG_MMC_DEBUG
1820 sdhci_dumpregs(host);
1821#endif
1822
Pierre Ossmanf9134312008-12-21 17:01:48 +01001823#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001824 snprintf(host->led_name, sizeof(host->led_name),
1825 "%s::", mmc_hostname(mmc));
1826 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001827 host->led.brightness = LED_OFF;
1828 host->led.default_trigger = mmc_hostname(mmc);
1829 host->led.brightness_set = sdhci_led_control;
1830
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001831 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001832 if (ret)
1833 goto reset;
1834#endif
1835
Pierre Ossman5f25a662006-10-04 02:15:39 -07001836 mmiowb();
1837
Pierre Ossmand129bce2006-03-24 03:18:17 -08001838 mmc_add_host(mmc);
1839
Pierre Ossman2134a922008-06-28 18:28:51 +02001840 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s%s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001841 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Pierre Ossman2134a922008-06-28 18:28:51 +02001842 (host->flags & SDHCI_USE_ADMA)?"A":"",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001843 (host->flags & SDHCI_USE_DMA)?"DMA":"PIO");
1844
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001845 sdhci_enable_card_detection(host);
1846
Pierre Ossmand129bce2006-03-24 03:18:17 -08001847 return 0;
1848
Pierre Ossmanf9134312008-12-21 17:01:48 +01001849#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001850reset:
1851 sdhci_reset(host, SDHCI_RESET_ALL);
1852 free_irq(host->irq, host);
1853#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001854untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001855 tasklet_kill(&host->card_tasklet);
1856 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001857
1858 return ret;
1859}
1860
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001861EXPORT_SYMBOL_GPL(sdhci_add_host);
1862
Pierre Ossman1e728592008-04-16 19:13:13 +02001863void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001864{
Pierre Ossman1e728592008-04-16 19:13:13 +02001865 unsigned long flags;
1866
1867 if (dead) {
1868 spin_lock_irqsave(&host->lock, flags);
1869
1870 host->flags |= SDHCI_DEVICE_DEAD;
1871
1872 if (host->mrq) {
1873 printk(KERN_ERR "%s: Controller removed during "
1874 " transfer!\n", mmc_hostname(host->mmc));
1875
1876 host->mrq->cmd->error = -ENOMEDIUM;
1877 tasklet_schedule(&host->finish_tasklet);
1878 }
1879
1880 spin_unlock_irqrestore(&host->lock, flags);
1881 }
1882
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001883 sdhci_disable_card_detection(host);
1884
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001885 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886
Pierre Ossmanf9134312008-12-21 17:01:48 +01001887#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001888 led_classdev_unregister(&host->led);
1889#endif
1890
Pierre Ossman1e728592008-04-16 19:13:13 +02001891 if (!dead)
1892 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001893
1894 free_irq(host->irq, host);
1895
1896 del_timer_sync(&host->timer);
1897
1898 tasklet_kill(&host->card_tasklet);
1899 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001900
1901 kfree(host->adma_desc);
1902 kfree(host->align_buffer);
1903
1904 host->adma_desc = NULL;
1905 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001906}
1907
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001908EXPORT_SYMBOL_GPL(sdhci_remove_host);
1909
1910void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001911{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001912 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913}
1914
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001915EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001916
1917/*****************************************************************************\
1918 * *
1919 * Driver init/exit *
1920 * *
1921\*****************************************************************************/
1922
1923static int __init sdhci_drv_init(void)
1924{
1925 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001926 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001927 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1928
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001929 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001930}
1931
1932static void __exit sdhci_drv_exit(void)
1933{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001934}
1935
1936module_init(sdhci_drv_init);
1937module_exit(sdhci_drv_exit);
1938
Pierre Ossmandf673b22006-06-30 02:22:31 -07001939module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001940
Pierre Ossman32710e82009-04-08 20:14:54 +02001941MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001942MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001943MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07001944
Pierre Ossmandf673b22006-06-30 02:22:31 -07001945MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");