blob: d6ab62d539fb2aa03f084eee2a305fcee3d5bd84 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019#include <linux/dma-mapping.h>
Ralf Baechle11763602007-10-23 20:42:11 +020020#include <linux/scatterlist.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021
Pierre Ossman2f730fe2008-03-17 10:29:38 +010022#include <linux/leds.h>
23
Pierre Ossmand129bce2006-03-24 03:18:17 -080024#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossmand129bce2006-03-24 03:18:17 -080026#include "sdhci.h"
27
28#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010031 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmanf9134312008-12-21 17:01:48 +010033#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
34 defined(CONFIG_MMC_SDHCI_MODULE))
35#define SDHCI_USE_LEDS_CLASS
36#endif
37
Pierre Ossmandf673b22006-06-30 02:22:31 -070038static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040static void sdhci_prepare_data(struct sdhci_host *, struct mmc_data *);
41static void sdhci_finish_data(struct sdhci_host *);
42
43static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
44static void sdhci_finish_command(struct sdhci_host *);
45
46static void sdhci_dumpregs(struct sdhci_host *host)
47{
48 printk(KERN_DEBUG DRIVER_NAME ": ============== REGISTER DUMP ==============\n");
49
50 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030051 sdhci_readl(host, SDHCI_DMA_ADDRESS),
52 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080053 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readw(host, SDHCI_BLOCK_SIZE),
55 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readl(host, SDHCI_ARGUMENT),
58 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_PRESENT_STATE),
61 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readb(host, SDHCI_POWER_CONTROL),
64 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
67 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
70 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readl(host, SDHCI_INT_ENABLE),
73 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readw(host, SDHCI_ACMD12_ERR),
76 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Max curr: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readl(host, SDHCI_CAPABILITIES),
79 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080080
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010081 if (host->flags & SDHCI_USE_ADMA)
82 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
83 readl(host->ioaddr + SDHCI_ADMA_ERROR),
84 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
85
Pierre Ossmand129bce2006-03-24 03:18:17 -080086 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
87}
88
89/*****************************************************************************\
90 * *
91 * Low level functions *
92 * *
93\*****************************************************************************/
94
Anton Vorontsov7260cf52009-03-17 00:13:48 +030095static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
96{
97 u32 ier;
98
99 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
100 ier &= ~clear;
101 ier |= set;
102 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
103 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
104}
105
106static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
107{
108 sdhci_clear_set_irqs(host, 0, irqs);
109}
110
111static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
112{
113 sdhci_clear_set_irqs(host, irqs, 0);
114}
115
116static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
117{
118 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
119
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300120 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
121 return;
122
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300123 if (enable)
124 sdhci_unmask_irqs(host, irqs);
125 else
126 sdhci_mask_irqs(host, irqs);
127}
128
129static void sdhci_enable_card_detection(struct sdhci_host *host)
130{
131 sdhci_set_card_detection(host, true);
132}
133
134static void sdhci_disable_card_detection(struct sdhci_host *host)
135{
136 sdhci_set_card_detection(host, false);
137}
138
Pierre Ossmand129bce2006-03-24 03:18:17 -0800139static void sdhci_reset(struct sdhci_host *host, u8 mask)
140{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700141 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300142 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700143
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100144 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300145 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700146 SDHCI_CARD_PRESENT))
147 return;
148 }
149
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300150 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
151 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
152
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300153 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800154
Pierre Ossmane16514d82006-06-30 02:22:24 -0700155 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800156 host->clock = 0;
157
Pierre Ossmane16514d82006-06-30 02:22:24 -0700158 /* Wait max 100 ms */
159 timeout = 100;
160
161 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300162 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700163 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100164 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700165 mmc_hostname(host->mmc), (int)mask);
166 sdhci_dumpregs(host);
167 return;
168 }
169 timeout--;
170 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300172
173 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
174 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175}
176
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800177static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
178
179static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800181 if (soft)
182 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
183 else
184 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800185
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300186 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
187 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700188 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
189 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300190 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800191
192 if (soft) {
193 /* force clock reconfiguration */
194 host->clock = 0;
195 sdhci_set_ios(host->mmc, &host->mmc->ios);
196 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300197}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800198
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300199static void sdhci_reinit(struct sdhci_host *host)
200{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800201 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300202 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203}
204
205static void sdhci_activate_led(struct sdhci_host *host)
206{
207 u8 ctrl;
208
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300209 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300211 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212}
213
214static void sdhci_deactivate_led(struct sdhci_host *host)
215{
216 u8 ctrl;
217
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300218 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300220 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221}
222
Pierre Ossmanf9134312008-12-21 17:01:48 +0100223#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100224static void sdhci_led_control(struct led_classdev *led,
225 enum led_brightness brightness)
226{
227 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
228 unsigned long flags;
229
230 spin_lock_irqsave(&host->lock, flags);
231
232 if (brightness == LED_OFF)
233 sdhci_deactivate_led(host);
234 else
235 sdhci_activate_led(host);
236
237 spin_unlock_irqrestore(&host->lock, flags);
238}
239#endif
240
Pierre Ossmand129bce2006-03-24 03:18:17 -0800241/*****************************************************************************\
242 * *
243 * Core functions *
244 * *
245\*****************************************************************************/
246
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100247static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248{
Pierre Ossman76591502008-07-21 00:32:11 +0200249 unsigned long flags;
250 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700251 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200252 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100254 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100256 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200257 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258
Pierre Ossman76591502008-07-21 00:32:11 +0200259 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100261 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200262 if (!sg_miter_next(&host->sg_miter))
263 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264
Pierre Ossman76591502008-07-21 00:32:11 +0200265 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800266
Pierre Ossman76591502008-07-21 00:32:11 +0200267 blksize -= len;
268 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200269
Pierre Ossman76591502008-07-21 00:32:11 +0200270 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271
Pierre Ossman76591502008-07-21 00:32:11 +0200272 while (len) {
273 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200275 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276 }
Pierre Ossman76591502008-07-21 00:32:11 +0200277
278 *buf = scratch & 0xFF;
279
280 buf++;
281 scratch >>= 8;
282 chunk--;
283 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284 }
285 }
Pierre Ossman76591502008-07-21 00:32:11 +0200286
287 sg_miter_stop(&host->sg_miter);
288
289 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100290}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292static void sdhci_write_block_pio(struct sdhci_host *host)
293{
Pierre Ossman76591502008-07-21 00:32:11 +0200294 unsigned long flags;
295 size_t blksize, len, chunk;
296 u32 scratch;
297 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100298
299 DBG("PIO writing\n");
300
301 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200302 chunk = 0;
303 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304
Pierre Ossman76591502008-07-21 00:32:11 +0200305 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306
307 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200308 if (!sg_miter_next(&host->sg_miter))
309 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310
Pierre Ossman76591502008-07-21 00:32:11 +0200311 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200312
Pierre Ossman76591502008-07-21 00:32:11 +0200313 blksize -= len;
314 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315
Pierre Ossman76591502008-07-21 00:32:11 +0200316 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 while (len) {
319 scratch |= (u32)*buf << (chunk * 8);
320
321 buf++;
322 chunk++;
323 len--;
324
325 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300326 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200327 chunk = 0;
328 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100330 }
331 }
Pierre Ossman76591502008-07-21 00:32:11 +0200332
333 sg_miter_stop(&host->sg_miter);
334
335 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336}
337
338static void sdhci_transfer_pio(struct sdhci_host *host)
339{
340 u32 mask;
341
342 BUG_ON(!host->data);
343
Pierre Ossman76591502008-07-21 00:32:11 +0200344 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100345 return;
346
347 if (host->data->flags & MMC_DATA_READ)
348 mask = SDHCI_DATA_AVAILABLE;
349 else
350 mask = SDHCI_SPACE_AVAILABLE;
351
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200352 /*
353 * Some controllers (JMicron JMB38x) mess up the buffer bits
354 * for transfers < 4 bytes. As long as it is just one block,
355 * we can ignore the bits.
356 */
357 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
358 (host->data->blocks == 1))
359 mask = ~0;
360
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300361 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300362 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
363 udelay(100);
364
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365 if (host->data->flags & MMC_DATA_READ)
366 sdhci_read_block_pio(host);
367 else
368 sdhci_write_block_pio(host);
369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 host->blocks--;
371 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373 }
374
375 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800376}
377
Pierre Ossman2134a922008-06-28 18:28:51 +0200378static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
379{
380 local_irq_save(*flags);
381 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
382}
383
384static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
385{
386 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
387 local_irq_restore(*flags);
388}
389
Ben Dooks118cd172010-03-05 13:43:26 -0800390static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
391{
Ben Dooks9e506f32010-03-05 13:43:29 -0800392 __le32 *dataddr = (__le32 __force *)(desc + 4);
393 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800394
Ben Dooks9e506f32010-03-05 13:43:29 -0800395 /* SDHCI specification says ADMA descriptors should be 4 byte
396 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800397
Ben Dooks9e506f32010-03-05 13:43:29 -0800398 cmdlen[0] = cpu_to_le16(cmd);
399 cmdlen[1] = cpu_to_le16(len);
400
401 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800402}
403
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200404static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200405 struct mmc_data *data)
406{
407 int direction;
408
409 u8 *desc;
410 u8 *align;
411 dma_addr_t addr;
412 dma_addr_t align_addr;
413 int len, offset;
414
415 struct scatterlist *sg;
416 int i;
417 char *buffer;
418 unsigned long flags;
419
420 /*
421 * The spec does not specify endianness of descriptor table.
422 * We currently guess that it is LE.
423 */
424
425 if (data->flags & MMC_DATA_READ)
426 direction = DMA_FROM_DEVICE;
427 else
428 direction = DMA_TO_DEVICE;
429
430 /*
431 * The ADMA descriptor table is mapped further down as we
432 * need to fill it with data first.
433 */
434
435 host->align_addr = dma_map_single(mmc_dev(host->mmc),
436 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700437 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200438 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200439 BUG_ON(host->align_addr & 0x3);
440
441 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
442 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200443 if (host->sg_count == 0)
444 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200445
446 desc = host->adma_desc;
447 align = host->align_buffer;
448
449 align_addr = host->align_addr;
450
451 for_each_sg(data->sg, sg, host->sg_count, i) {
452 addr = sg_dma_address(sg);
453 len = sg_dma_len(sg);
454
455 /*
456 * The SDHCI specification states that ADMA
457 * addresses must be 32-bit aligned. If they
458 * aren't, then we use a bounce buffer for
459 * the (up to three) bytes that screw up the
460 * alignment.
461 */
462 offset = (4 - (addr & 0x3)) & 0x3;
463 if (offset) {
464 if (data->flags & MMC_DATA_WRITE) {
465 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200466 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200467 memcpy(align, buffer, offset);
468 sdhci_kunmap_atomic(buffer, &flags);
469 }
470
Ben Dooks118cd172010-03-05 13:43:26 -0800471 /* tran, valid */
472 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200473
474 BUG_ON(offset > 65536);
475
Pierre Ossman2134a922008-06-28 18:28:51 +0200476 align += 4;
477 align_addr += 4;
478
479 desc += 8;
480
481 addr += offset;
482 len -= offset;
483 }
484
Pierre Ossman2134a922008-06-28 18:28:51 +0200485 BUG_ON(len > 65536);
486
Ben Dooks118cd172010-03-05 13:43:26 -0800487 /* tran, valid */
488 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200489 desc += 8;
490
491 /*
492 * If this triggers then we have a calculation bug
493 * somewhere. :/
494 */
495 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
496 }
497
498 /*
499 * Add a terminating entry.
500 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200501
Ben Dooks118cd172010-03-05 13:43:26 -0800502 /* nop, end, valid */
503 sdhci_set_adma_desc(desc, 0, 0, 0x3);
Pierre Ossman2134a922008-06-28 18:28:51 +0200504
505 /*
506 * Resync align buffer as we might have changed it.
507 */
508 if (data->flags & MMC_DATA_WRITE) {
509 dma_sync_single_for_device(mmc_dev(host->mmc),
510 host->align_addr, 128 * 4, direction);
511 }
512
513 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
514 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200515 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200516 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200517 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200518
519 return 0;
520
521unmap_entries:
522 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
523 data->sg_len, direction);
524unmap_align:
525 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
526 128 * 4, direction);
527fail:
528 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200529}
530
531static void sdhci_adma_table_post(struct sdhci_host *host,
532 struct mmc_data *data)
533{
534 int direction;
535
536 struct scatterlist *sg;
537 int i, size;
538 u8 *align;
539 char *buffer;
540 unsigned long flags;
541
542 if (data->flags & MMC_DATA_READ)
543 direction = DMA_FROM_DEVICE;
544 else
545 direction = DMA_TO_DEVICE;
546
547 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
548 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
549
550 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
551 128 * 4, direction);
552
553 if (data->flags & MMC_DATA_READ) {
554 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
555 data->sg_len, direction);
556
557 align = host->align_buffer;
558
559 for_each_sg(data->sg, sg, host->sg_count, i) {
560 if (sg_dma_address(sg) & 0x3) {
561 size = 4 - (sg_dma_address(sg) & 0x3);
562
563 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200564 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200565 memcpy(buffer, align, size);
566 sdhci_kunmap_atomic(buffer, &flags);
567
568 align += 4;
569 }
570 }
571 }
572
573 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
574 data->sg_len, direction);
575}
576
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200577static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800578{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700579 u8 count;
580 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800581
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200582 /*
583 * If the host controller provides us with an incorrect timeout
584 * value, just skip the check and use 0xE. The hardware may take
585 * longer to time out, but that's much better than having a too-short
586 * timeout value.
587 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200588 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200589 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200590
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700591 /* timeout in us */
592 target_timeout = data->timeout_ns / 1000 +
593 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800594
Anton Vorontsov81b39802009-09-22 16:45:13 -0700595 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
596 host->timeout_clk = host->clock / 1000;
597
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700598 /*
599 * Figure out needed cycles.
600 * We do this in steps in order to fit inside a 32 bit int.
601 * The first step is the minimum timeout, which will have a
602 * minimum resolution of 6 bits:
603 * (1) 2^13*1000 > 2^22,
604 * (2) host->timeout_clk < 2^16
605 * =>
606 * (1) / (2) > 2^6
607 */
608 count = 0;
609 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
610 while (current_timeout < target_timeout) {
611 count++;
612 current_timeout <<= 1;
613 if (count >= 0xF)
614 break;
615 }
616
617 if (count >= 0xF) {
618 printk(KERN_WARNING "%s: Too large timeout requested!\n",
619 mmc_hostname(host->mmc));
620 count = 0xE;
621 }
622
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200623 return count;
624}
625
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300626static void sdhci_set_transfer_irqs(struct sdhci_host *host)
627{
628 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
629 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
630
631 if (host->flags & SDHCI_REQ_USE_DMA)
632 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
633 else
634 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
635}
636
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200637static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
638{
639 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200640 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200641 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200642
643 WARN_ON(host->data);
644
645 if (data == NULL)
646 return;
647
648 /* Sanity checks */
649 BUG_ON(data->blksz * data->blocks > 524288);
650 BUG_ON(data->blksz > host->mmc->max_blk_size);
651 BUG_ON(data->blocks > 65535);
652
653 host->data = data;
654 host->data_early = 0;
655
656 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300657 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800658
Richard Röjforsa13abc72009-09-22 16:45:30 -0700659 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100660 host->flags |= SDHCI_REQ_USE_DMA;
661
Pierre Ossman2134a922008-06-28 18:28:51 +0200662 /*
663 * FIXME: This doesn't account for merging when mapping the
664 * scatterlist.
665 */
666 if (host->flags & SDHCI_REQ_USE_DMA) {
667 int broken, i;
668 struct scatterlist *sg;
669
670 broken = 0;
671 if (host->flags & SDHCI_USE_ADMA) {
672 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
673 broken = 1;
674 } else {
675 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
676 broken = 1;
677 }
678
679 if (unlikely(broken)) {
680 for_each_sg(data->sg, sg, data->sg_len, i) {
681 if (sg->length & 0x3) {
682 DBG("Reverting to PIO because of "
683 "transfer size (%d)\n",
684 sg->length);
685 host->flags &= ~SDHCI_REQ_USE_DMA;
686 break;
687 }
688 }
689 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100690 }
691
692 /*
693 * The assumption here being that alignment is the same after
694 * translation to device address space.
695 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200696 if (host->flags & SDHCI_REQ_USE_DMA) {
697 int broken, i;
698 struct scatterlist *sg;
699
700 broken = 0;
701 if (host->flags & SDHCI_USE_ADMA) {
702 /*
703 * As we use 3 byte chunks to work around
704 * alignment problems, we need to check this
705 * quirk.
706 */
707 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
708 broken = 1;
709 } else {
710 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
711 broken = 1;
712 }
713
714 if (unlikely(broken)) {
715 for_each_sg(data->sg, sg, data->sg_len, i) {
716 if (sg->offset & 0x3) {
717 DBG("Reverting to PIO because of "
718 "bad alignment\n");
719 host->flags &= ~SDHCI_REQ_USE_DMA;
720 break;
721 }
722 }
723 }
724 }
725
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200726 if (host->flags & SDHCI_REQ_USE_DMA) {
727 if (host->flags & SDHCI_USE_ADMA) {
728 ret = sdhci_adma_table_pre(host, data);
729 if (ret) {
730 /*
731 * This only happens when someone fed
732 * us an invalid request.
733 */
734 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200735 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200736 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300737 sdhci_writel(host, host->adma_addr,
738 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200739 }
740 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300741 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200742
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300743 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200744 data->sg, data->sg_len,
745 (data->flags & MMC_DATA_READ) ?
746 DMA_FROM_DEVICE :
747 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300748 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200749 /*
750 * This only happens when someone fed
751 * us an invalid request.
752 */
753 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200754 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200755 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200756 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300757 sdhci_writel(host, sg_dma_address(data->sg),
758 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200759 }
760 }
761 }
762
Pierre Ossman2134a922008-06-28 18:28:51 +0200763 /*
764 * Always adjust the DMA selection as some controllers
765 * (e.g. JMicron) can't do PIO properly when the selection
766 * is ADMA.
767 */
768 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300769 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200770 ctrl &= ~SDHCI_CTRL_DMA_MASK;
771 if ((host->flags & SDHCI_REQ_USE_DMA) &&
772 (host->flags & SDHCI_USE_ADMA))
773 ctrl |= SDHCI_CTRL_ADMA32;
774 else
775 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300776 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100777 }
778
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200779 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200780 int flags;
781
782 flags = SG_MITER_ATOMIC;
783 if (host->data->flags & MMC_DATA_READ)
784 flags |= SG_MITER_TO_SG;
785 else
786 flags |= SG_MITER_FROM_SG;
787 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200788 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800789 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700790
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300791 sdhci_set_transfer_irqs(host);
792
Pierre Ossmanbab76962006-07-02 16:51:35 +0100793 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300794 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
795 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700796}
797
798static void sdhci_set_transfer_mode(struct sdhci_host *host,
799 struct mmc_data *data)
800{
801 u16 mode;
802
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700803 if (data == NULL)
804 return;
805
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200806 WARN_ON(!host->data);
807
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700808 mode = SDHCI_TRNS_BLK_CNT_EN;
809 if (data->blocks > 1)
810 mode |= SDHCI_TRNS_MULTI;
811 if (data->flags & MMC_DATA_READ)
812 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100813 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700814 mode |= SDHCI_TRNS_DMA;
815
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300816 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800817}
818
819static void sdhci_finish_data(struct sdhci_host *host)
820{
821 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800822
823 BUG_ON(!host->data);
824
825 data = host->data;
826 host->data = NULL;
827
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100828 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 if (host->flags & SDHCI_USE_ADMA)
830 sdhci_adma_table_post(host, data);
831 else {
832 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
833 data->sg_len, (data->flags & MMC_DATA_READ) ?
834 DMA_FROM_DEVICE : DMA_TO_DEVICE);
835 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800836 }
837
838 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200839 * The specification states that the block count register must
840 * be updated, but it does not specify at what point in the
841 * data flow. That makes the register entirely useless to read
842 * back so we have to assume that nothing made it to the card
843 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800844 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200845 if (data->error)
846 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800847 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200848 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800849
Pierre Ossmand129bce2006-03-24 03:18:17 -0800850 if (data->stop) {
851 /*
852 * The controller needs a reset of internal state machines
853 * upon error conditions.
854 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200855 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800856 sdhci_reset(host, SDHCI_RESET_CMD);
857 sdhci_reset(host, SDHCI_RESET_DATA);
858 }
859
860 sdhci_send_command(host, data->stop);
861 } else
862 tasklet_schedule(&host->finish_tasklet);
863}
864
865static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
866{
867 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700868 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700869 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800870
871 WARN_ON(host->cmd);
872
Pierre Ossmand129bce2006-03-24 03:18:17 -0800873 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700874 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700875
876 mask = SDHCI_CMD_INHIBIT;
877 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
878 mask |= SDHCI_DATA_INHIBIT;
879
880 /* We shouldn't wait for data inihibit for stop commands, even
881 though they might use busy signaling */
882 if (host->mrq->data && (cmd == host->mrq->data->stop))
883 mask &= ~SDHCI_DATA_INHIBIT;
884
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300885 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700886 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100888 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800889 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200890 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800891 tasklet_schedule(&host->finish_tasklet);
892 return;
893 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700894 timeout--;
895 mdelay(1);
896 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800897
898 mod_timer(&host->timer, jiffies + 10 * HZ);
899
900 host->cmd = cmd;
901
902 sdhci_prepare_data(host, cmd->data);
903
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300904 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906 sdhci_set_transfer_mode(host, cmd->data);
907
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100909 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800910 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200911 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912 tasklet_schedule(&host->finish_tasklet);
913 return;
914 }
915
916 if (!(cmd->flags & MMC_RSP_PRESENT))
917 flags = SDHCI_CMD_RESP_NONE;
918 else if (cmd->flags & MMC_RSP_136)
919 flags = SDHCI_CMD_RESP_LONG;
920 else if (cmd->flags & MMC_RSP_BUSY)
921 flags = SDHCI_CMD_RESP_SHORT_BUSY;
922 else
923 flags = SDHCI_CMD_RESP_SHORT;
924
925 if (cmd->flags & MMC_RSP_CRC)
926 flags |= SDHCI_CMD_CRC;
927 if (cmd->flags & MMC_RSP_OPCODE)
928 flags |= SDHCI_CMD_INDEX;
929 if (cmd->data)
930 flags |= SDHCI_CMD_DATA;
931
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300932 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933}
934
935static void sdhci_finish_command(struct sdhci_host *host)
936{
937 int i;
938
939 BUG_ON(host->cmd == NULL);
940
941 if (host->cmd->flags & MMC_RSP_PRESENT) {
942 if (host->cmd->flags & MMC_RSP_136) {
943 /* CRC is stripped so we need to do some shifting. */
944 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300945 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 SDHCI_RESPONSE + (3-i)*4) << 8;
947 if (i != 3)
948 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 SDHCI_RESPONSE + (3-i)*4-1);
951 }
952 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300953 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954 }
955 }
956
Pierre Ossman17b04292007-07-22 22:18:46 +0200957 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200959 if (host->data && host->data_early)
960 sdhci_finish_data(host);
961
962 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963 tasklet_schedule(&host->finish_tasklet);
964
965 host->cmd = NULL;
966}
967
968static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
969{
970 int div;
971 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700972 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973
974 if (clock == host->clock)
975 return;
976
Anton Vorontsov81146342009-03-17 00:13:59 +0300977 if (host->ops->set_clock) {
978 host->ops->set_clock(host, clock);
979 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
980 return;
981 }
982
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300983 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984
985 if (clock == 0)
986 goto out;
987
988 for (div = 1;div < 256;div *= 2) {
989 if ((host->max_clk / div) <= clock)
990 break;
991 }
992 div >>= 1;
993
994 clk = div << SDHCI_DIVIDER_SHIFT;
995 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300996 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
Chris Ball27f6cb12009-09-22 16:45:31 -0700998 /* Wait max 20 ms */
999 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001000 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001001 & SDHCI_CLOCK_INT_STABLE)) {
1002 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001003 printk(KERN_ERR "%s: Internal clock never "
1004 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 sdhci_dumpregs(host);
1006 return;
1007 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001008 timeout--;
1009 mdelay(1);
1010 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011
1012 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001013 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015out:
1016 host->clock = clock;
1017}
1018
Pierre Ossman146ad662006-06-30 02:22:23 -07001019static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1020{
1021 u8 pwr;
1022
Pierre Ossmanae628902009-05-03 20:45:03 +02001023 if (power == (unsigned short)-1)
1024 pwr = 0;
1025 else {
1026 switch (1 << power) {
1027 case MMC_VDD_165_195:
1028 pwr = SDHCI_POWER_180;
1029 break;
1030 case MMC_VDD_29_30:
1031 case MMC_VDD_30_31:
1032 pwr = SDHCI_POWER_300;
1033 break;
1034 case MMC_VDD_32_33:
1035 case MMC_VDD_33_34:
1036 pwr = SDHCI_POWER_330;
1037 break;
1038 default:
1039 BUG();
1040 }
1041 }
1042
1043 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001044 return;
1045
Pierre Ossmanae628902009-05-03 20:45:03 +02001046 host->pwr = pwr;
1047
1048 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001049 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001050 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001051 }
1052
1053 /*
1054 * Spec says that we should clear the power reg before setting
1055 * a new value. Some controllers don't seem to like this though.
1056 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001057 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001059
Andres Salomone08c1692008-07-04 10:00:03 -07001060 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001061 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001062 * and set turn on power at the same time, so set the voltage first.
1063 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001064 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001065 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1066
1067 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001068
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001069 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001070
1071 /*
1072 * Some controllers need an extra 10ms delay of 10ms before they
1073 * can apply clock after applying power
1074 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001075 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001076 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001077}
1078
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079/*****************************************************************************\
1080 * *
1081 * MMC callbacks *
1082 * *
1083\*****************************************************************************/
1084
1085static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1086{
1087 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001088 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001089 unsigned long flags;
1090
1091 host = mmc_priv(mmc);
1092
1093 spin_lock_irqsave(&host->lock, flags);
1094
1095 WARN_ON(host->mrq != NULL);
1096
Pierre Ossmanf9134312008-12-21 17:01:48 +01001097#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001098 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001099#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001100
1101 host->mrq = mrq;
1102
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001103 /* If polling, assume that the card is always present. */
1104 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1105 present = true;
1106 else
1107 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1108 SDHCI_CARD_PRESENT;
1109
1110 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001111 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112 tasklet_schedule(&host->finish_tasklet);
1113 } else
1114 sdhci_send_command(host, mrq->cmd);
1115
Pierre Ossman5f25a662006-10-04 02:15:39 -07001116 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 spin_unlock_irqrestore(&host->lock, flags);
1118}
1119
1120static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1121{
1122 struct sdhci_host *host;
1123 unsigned long flags;
1124 u8 ctrl;
1125
1126 host = mmc_priv(mmc);
1127
1128 spin_lock_irqsave(&host->lock, flags);
1129
Pierre Ossman1e728592008-04-16 19:13:13 +02001130 if (host->flags & SDHCI_DEVICE_DEAD)
1131 goto out;
1132
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133 /*
1134 * Reset the chip on each power off.
1135 * Should clear out any weird states.
1136 */
1137 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001138 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001139 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 }
1141
1142 sdhci_set_clock(host, ios->clock);
1143
1144 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001145 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001147 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001149 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001150
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 if (ios->bus_width == MMC_BUS_WIDTH_4)
1152 ctrl |= SDHCI_CTRL_4BITBUS;
1153 else
1154 ctrl &= ~SDHCI_CTRL_4BITBUS;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001155
1156 if (ios->timing == MMC_TIMING_SD_HS)
1157 ctrl |= SDHCI_CTRL_HISPD;
1158 else
1159 ctrl &= ~SDHCI_CTRL_HISPD;
1160
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001161 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162
Leandro Dorileob8352262007-07-25 23:47:04 +02001163 /*
1164 * Some (ENE) controllers go apeshit on some ios operation,
1165 * signalling timeout and CRC errors even on CMD0. Resetting
1166 * it on each ios seems to solve the problem.
1167 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001168 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001169 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1170
Pierre Ossman1e728592008-04-16 19:13:13 +02001171out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001172 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 spin_unlock_irqrestore(&host->lock, flags);
1174}
1175
1176static int sdhci_get_ro(struct mmc_host *mmc)
1177{
1178 struct sdhci_host *host;
1179 unsigned long flags;
1180 int present;
1181
1182 host = mmc_priv(mmc);
1183
1184 spin_lock_irqsave(&host->lock, flags);
1185
Pierre Ossman1e728592008-04-16 19:13:13 +02001186 if (host->flags & SDHCI_DEVICE_DEAD)
1187 present = 0;
1188 else
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001189 present = sdhci_readl(host, SDHCI_PRESENT_STATE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001190
1191 spin_unlock_irqrestore(&host->lock, flags);
1192
Anton Vorontsovc5075a12009-03-17 00:13:54 +03001193 if (host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT)
1194 return !!(present & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001195 return !(present & SDHCI_WRITE_PROTECT);
1196}
1197
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001198static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1199{
1200 struct sdhci_host *host;
1201 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001202
1203 host = mmc_priv(mmc);
1204
1205 spin_lock_irqsave(&host->lock, flags);
1206
Pierre Ossman1e728592008-04-16 19:13:13 +02001207 if (host->flags & SDHCI_DEVICE_DEAD)
1208 goto out;
1209
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001210 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001211 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1212 else
1213 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001214out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001215 mmiowb();
1216
1217 spin_unlock_irqrestore(&host->lock, flags);
1218}
1219
David Brownellab7aefd2006-11-12 17:55:30 -08001220static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001221 .request = sdhci_request,
1222 .set_ios = sdhci_set_ios,
1223 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001224 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225};
1226
1227/*****************************************************************************\
1228 * *
1229 * Tasklets *
1230 * *
1231\*****************************************************************************/
1232
1233static void sdhci_tasklet_card(unsigned long param)
1234{
1235 struct sdhci_host *host;
1236 unsigned long flags;
1237
1238 host = (struct sdhci_host*)param;
1239
1240 spin_lock_irqsave(&host->lock, flags);
1241
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001242 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243 if (host->mrq) {
1244 printk(KERN_ERR "%s: Card removed during transfer!\n",
1245 mmc_hostname(host->mmc));
1246 printk(KERN_ERR "%s: Resetting controller.\n",
1247 mmc_hostname(host->mmc));
1248
1249 sdhci_reset(host, SDHCI_RESET_CMD);
1250 sdhci_reset(host, SDHCI_RESET_DATA);
1251
Pierre Ossman17b04292007-07-22 22:18:46 +02001252 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 tasklet_schedule(&host->finish_tasklet);
1254 }
1255 }
1256
1257 spin_unlock_irqrestore(&host->lock, flags);
1258
Pierre Ossman04cf5852008-08-18 22:18:14 +02001259 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001260}
1261
1262static void sdhci_tasklet_finish(unsigned long param)
1263{
1264 struct sdhci_host *host;
1265 unsigned long flags;
1266 struct mmc_request *mrq;
1267
1268 host = (struct sdhci_host*)param;
1269
1270 spin_lock_irqsave(&host->lock, flags);
1271
1272 del_timer(&host->timer);
1273
1274 mrq = host->mrq;
1275
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 /*
1277 * The controller needs a reset of internal state machines
1278 * upon error conditions.
1279 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001280 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
1281 (mrq->cmd->error ||
1282 (mrq->data && (mrq->data->error ||
1283 (mrq->data->stop && mrq->data->stop->error))) ||
1284 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001285
1286 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001287 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001288 unsigned int clock;
1289
1290 /* This is to force an update */
1291 clock = host->clock;
1292 host->clock = 0;
1293 sdhci_set_clock(host, clock);
1294 }
1295
1296 /* Spec says we should do both at the same time, but Ricoh
1297 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001298 sdhci_reset(host, SDHCI_RESET_CMD);
1299 sdhci_reset(host, SDHCI_RESET_DATA);
1300 }
1301
1302 host->mrq = NULL;
1303 host->cmd = NULL;
1304 host->data = NULL;
1305
Pierre Ossmanf9134312008-12-21 17:01:48 +01001306#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001308#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309
Pierre Ossman5f25a662006-10-04 02:15:39 -07001310 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 spin_unlock_irqrestore(&host->lock, flags);
1312
1313 mmc_request_done(host->mmc, mrq);
1314}
1315
1316static void sdhci_timeout_timer(unsigned long data)
1317{
1318 struct sdhci_host *host;
1319 unsigned long flags;
1320
1321 host = (struct sdhci_host*)data;
1322
1323 spin_lock_irqsave(&host->lock, flags);
1324
1325 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001326 printk(KERN_ERR "%s: Timeout waiting for hardware "
1327 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001328 sdhci_dumpregs(host);
1329
1330 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001331 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 sdhci_finish_data(host);
1333 } else {
1334 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001335 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001337 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338
1339 tasklet_schedule(&host->finish_tasklet);
1340 }
1341 }
1342
Pierre Ossman5f25a662006-10-04 02:15:39 -07001343 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 spin_unlock_irqrestore(&host->lock, flags);
1345}
1346
1347/*****************************************************************************\
1348 * *
1349 * Interrupt handling *
1350 * *
1351\*****************************************************************************/
1352
1353static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1354{
1355 BUG_ON(intmask == 0);
1356
1357 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001358 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1359 "though no command operation was in progress.\n",
1360 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 sdhci_dumpregs(host);
1362 return;
1363 }
1364
Pierre Ossman43b58b32007-07-25 23:15:27 +02001365 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001366 host->cmd->error = -ETIMEDOUT;
1367 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1368 SDHCI_INT_INDEX))
1369 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370
Pierre Ossmane8095172008-07-25 01:09:08 +02001371 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001373 return;
1374 }
1375
1376 /*
1377 * The host can send and interrupt when the busy state has
1378 * ended, allowing us to wait without wasting CPU cycles.
1379 * Unfortunately this is overloaded on the "data complete"
1380 * interrupt, so we need to take some care when handling
1381 * it.
1382 *
1383 * Note: The 1.0 specification is a bit ambiguous about this
1384 * feature so there might be some problems with older
1385 * controllers.
1386 */
1387 if (host->cmd->flags & MMC_RSP_BUSY) {
1388 if (host->cmd->data)
1389 DBG("Cannot wait for busy signal when also "
1390 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001391 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001392 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001393
1394 /* The controller does not support the end-of-busy IRQ,
1395 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001396 }
1397
1398 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001399 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400}
1401
Ben Dooks6882a8c2009-06-14 13:52:38 +01001402#ifdef DEBUG
1403static void sdhci_show_adma_error(struct sdhci_host *host)
1404{
1405 const char *name = mmc_hostname(host->mmc);
1406 u8 *desc = host->adma_desc;
1407 __le32 *dma;
1408 __le16 *len;
1409 u8 attr;
1410
1411 sdhci_dumpregs(host);
1412
1413 while (true) {
1414 dma = (__le32 *)(desc + 4);
1415 len = (__le16 *)(desc + 2);
1416 attr = *desc;
1417
1418 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1419 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1420
1421 desc += 8;
1422
1423 if (attr & 2)
1424 break;
1425 }
1426}
1427#else
1428static void sdhci_show_adma_error(struct sdhci_host *host) { }
1429#endif
1430
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1432{
1433 BUG_ON(intmask == 0);
1434
1435 if (!host->data) {
1436 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001437 * The "data complete" interrupt is also used to
1438 * indicate that a busy state has ended. See comment
1439 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001440 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001441 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1442 if (intmask & SDHCI_INT_DATA_END) {
1443 sdhci_finish_command(host);
1444 return;
1445 }
1446 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001447
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001448 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1449 "though no data operation was in progress.\n",
1450 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001451 sdhci_dumpregs(host);
1452
1453 return;
1454 }
1455
1456 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001457 host->data->error = -ETIMEDOUT;
1458 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT))
1459 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001460 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1461 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1462 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001463 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001464 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465
Pierre Ossman17b04292007-07-22 22:18:46 +02001466 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001467 sdhci_finish_data(host);
1468 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001469 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470 sdhci_transfer_pio(host);
1471
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001472 /*
1473 * We currently don't do anything fancy with DMA
1474 * boundaries, but as we can't disable the feature
1475 * we need to at least restart the transfer.
1476 */
1477 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001478 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1479 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001480
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001481 if (intmask & SDHCI_INT_DATA_END) {
1482 if (host->cmd) {
1483 /*
1484 * Data managed to finish before the
1485 * command completed. Make sure we do
1486 * things in the proper order.
1487 */
1488 host->data_early = 1;
1489 } else {
1490 sdhci_finish_data(host);
1491 }
1492 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493 }
1494}
1495
David Howells7d12e782006-10-05 14:55:46 +01001496static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497{
1498 irqreturn_t result;
1499 struct sdhci_host* host = dev_id;
1500 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001501 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502
1503 spin_lock(&host->lock);
1504
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001505 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506
Mark Lord62df67a52007-03-06 13:30:13 +01001507 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508 result = IRQ_NONE;
1509 goto out;
1510 }
1511
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001512 DBG("*** %s got interrupt: 0x%08x\n",
1513 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514
Pierre Ossman3192a282006-06-30 02:22:26 -07001515 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001516 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1517 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001519 }
1520
1521 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522
1523 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001524 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1525 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001526 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001527 }
1528
1529 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001530 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1531 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001532 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001533 }
1534
1535 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1536
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001537 intmask &= ~SDHCI_INT_ERROR;
1538
Pierre Ossmand129bce2006-03-24 03:18:17 -08001539 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001540 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001542 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001543 }
1544
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001545 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001546
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001547 if (intmask & SDHCI_INT_CARD_INT)
1548 cardint = 1;
1549
1550 intmask &= ~SDHCI_INT_CARD_INT;
1551
Pierre Ossman3192a282006-06-30 02:22:26 -07001552 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001553 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001554 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001555 sdhci_dumpregs(host);
1556
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001557 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001558 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001559
1560 result = IRQ_HANDLED;
1561
Pierre Ossman5f25a662006-10-04 02:15:39 -07001562 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001563out:
1564 spin_unlock(&host->lock);
1565
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001566 /*
1567 * We have to delay this as it calls back into the driver.
1568 */
1569 if (cardint)
1570 mmc_signal_sdio_irq(host->mmc);
1571
Pierre Ossmand129bce2006-03-24 03:18:17 -08001572 return result;
1573}
1574
1575/*****************************************************************************\
1576 * *
1577 * Suspend/resume *
1578 * *
1579\*****************************************************************************/
1580
1581#ifdef CONFIG_PM
1582
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001583int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001584{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001585 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001586
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001587 sdhci_disable_card_detection(host);
1588
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001589 ret = mmc_suspend_host(host->mmc, state);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001590 if (ret)
1591 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001592
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001593 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001594
1595 return 0;
1596}
1597
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001598EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001600int sdhci_resume_host(struct sdhci_host *host)
1601{
1602 int ret;
1603
Richard Röjforsa13abc72009-09-22 16:45:30 -07001604 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001605 if (host->ops->enable_dma)
1606 host->ops->enable_dma(host);
1607 }
1608
1609 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1610 mmc_hostname(host->mmc), host);
1611 if (ret)
1612 return ret;
1613
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001614 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001615 mmiowb();
1616
1617 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001618 sdhci_enable_card_detection(host);
1619
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001620 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001621}
1622
1623EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
1625#endif /* CONFIG_PM */
1626
1627/*****************************************************************************\
1628 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001629 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001630 * *
1631\*****************************************************************************/
1632
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001633struct sdhci_host *sdhci_alloc_host(struct device *dev,
1634 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001635{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636 struct mmc_host *mmc;
1637 struct sdhci_host *host;
1638
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001639 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001641 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001643 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644
1645 host = mmc_priv(mmc);
1646 host->mmc = mmc;
1647
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001648 return host;
1649}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001650
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001651EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001653int sdhci_add_host(struct sdhci_host *host)
1654{
1655 struct mmc_host *mmc;
1656 unsigned int caps;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001657 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001659 WARN_ON(host == NULL);
1660 if (host == NULL)
1661 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001663 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001665 if (debug_quirks)
1666 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001667
Pierre Ossmand96649e2006-06-30 02:22:30 -07001668 sdhci_reset(host, SDHCI_RESET_ALL);
1669
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001670 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001671 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1672 >> SDHCI_SPEC_VER_SHIFT;
1673 if (host->version > SDHCI_SPEC_200) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001674 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001675 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001676 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001677 }
1678
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001679 caps = sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001680
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001681 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001682 host->flags |= SDHCI_USE_SDMA;
1683 else if (!(caps & SDHCI_CAN_DO_SDMA))
1684 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001685 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001686 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001687
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001688 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001689 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001690 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001691 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001692 }
1693
Richard Röjforsa13abc72009-09-22 16:45:30 -07001694 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1695 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001696
1697 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1698 (host->flags & SDHCI_USE_ADMA)) {
1699 DBG("Disabling ADMA as it is marked broken\n");
1700 host->flags &= ~SDHCI_USE_ADMA;
1701 }
1702
Richard Röjforsa13abc72009-09-22 16:45:30 -07001703 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001704 if (host->ops->enable_dma) {
1705 if (host->ops->enable_dma(host)) {
1706 printk(KERN_WARNING "%s: No suitable DMA "
1707 "available. Falling back to PIO.\n",
1708 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001709 host->flags &=
1710 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001711 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001712 }
1713 }
1714
Pierre Ossman2134a922008-06-28 18:28:51 +02001715 if (host->flags & SDHCI_USE_ADMA) {
1716 /*
1717 * We need to allocate descriptors for all sg entries
1718 * (128) and potentially one alignment transfer for
1719 * each of those entries.
1720 */
1721 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1722 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1723 if (!host->adma_desc || !host->align_buffer) {
1724 kfree(host->adma_desc);
1725 kfree(host->align_buffer);
1726 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1727 "buffers. Falling back to standard DMA.\n",
1728 mmc_hostname(mmc));
1729 host->flags &= ~SDHCI_USE_ADMA;
1730 }
1731 }
1732
Pierre Ossman76591502008-07-21 00:32:11 +02001733 /*
1734 * If we use DMA, then it's up to the caller to set the DMA
1735 * mask, but PIO does not need the hw shim so we set a new
1736 * mask here in that case.
1737 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001738 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001739 host->dma_mask = DMA_BIT_MASK(64);
1740 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1741 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001742
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001743 host->max_clk =
1744 (caps & SDHCI_CLOCK_BASE_MASK) >> SDHCI_CLOCK_BASE_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001745 host->max_clk *= 1000000;
Ben Dooks4240ff02009-03-17 00:13:57 +03001746 if (host->max_clk == 0) {
1747 if (!host->ops->get_max_clock) {
1748 printk(KERN_ERR
1749 "%s: Hardware doesn't specify base clock "
1750 "frequency.\n", mmc_hostname(mmc));
1751 return -ENODEV;
1752 }
1753 host->max_clk = host->ops->get_max_clock(host);
1754 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001755
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001756 host->timeout_clk =
1757 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1758 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001759 if (host->ops->get_timeout_clock) {
1760 host->timeout_clk = host->ops->get_timeout_clock(host);
1761 } else if (!(host->quirks &
1762 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001763 printk(KERN_ERR
1764 "%s: Hardware doesn't specify timeout clock "
1765 "frequency.\n", mmc_hostname(mmc));
1766 return -ENODEV;
1767 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001768 }
1769 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1770 host->timeout_clk *= 1000;
1771
Pierre Ossmand129bce2006-03-24 03:18:17 -08001772 /*
1773 * Set host parameters.
1774 */
1775 mmc->ops = &sdhci_ops;
Anton Vorontsove9510172009-09-22 16:45:08 -07001776 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK &&
1777 host->ops->set_clock && host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001778 mmc->f_min = host->ops->get_min_clock(host);
1779 else
1780 mmc->f_min = host->max_clk / 256;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001781 mmc->f_max = host->max_clk;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001782 mmc->caps = MMC_CAP_SDIO_IRQ;
1783
1784 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
1785 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001786
Pierre Ossman86a6a872009-02-02 21:13:49 +01001787 if (caps & SDHCI_CAN_DO_HISPD)
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001788 mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1789
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001790 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1791 mmc->caps |= MMC_CAP_NEEDS_POLL;
1792
Pierre Ossman146ad662006-06-30 02:22:23 -07001793 mmc->ocr_avail = 0;
1794 if (caps & SDHCI_CAN_VDD_330)
1795 mmc->ocr_avail |= MMC_VDD_32_33|MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001796 if (caps & SDHCI_CAN_VDD_300)
Pierre Ossman146ad662006-06-30 02:22:23 -07001797 mmc->ocr_avail |= MMC_VDD_29_30|MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001798 if (caps & SDHCI_CAN_VDD_180)
Philip Langdale55556da2007-03-16 19:39:00 -07001799 mmc->ocr_avail |= MMC_VDD_165_195;
Pierre Ossman146ad662006-06-30 02:22:23 -07001800
1801 if (mmc->ocr_avail == 0) {
1802 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001803 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001804 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001805 }
1806
Pierre Ossmand129bce2006-03-24 03:18:17 -08001807 spin_lock_init(&host->lock);
1808
1809 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001810 * Maximum number of segments. Depends on if the hardware
1811 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001812 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001813 if (host->flags & SDHCI_USE_ADMA)
1814 mmc->max_hw_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001815 else if (host->flags & SDHCI_USE_SDMA)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816 mmc->max_hw_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001817 else /* PIO */
1818 mmc->max_hw_segs = 128;
1819 mmc->max_phys_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001820
1821 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001822 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001823 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001824 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001825 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001826
1827 /*
1828 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001829 * of bytes. When doing hardware scatter/gather, each entry cannot
1830 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001831 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001832 if (host->flags & SDHCI_USE_ADMA)
1833 mmc->max_seg_size = 65536;
1834 else
1835 mmc->max_seg_size = mmc->max_req_size;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001836
1837 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001838 * Maximum block size. This varies from controller to controller and
1839 * is specified in the capabilities register.
1840 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001841 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1842 mmc->max_blk_size = 2;
1843 } else {
1844 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1845 SDHCI_MAX_BLOCK_SHIFT;
1846 if (mmc->max_blk_size >= 3) {
1847 printk(KERN_WARNING "%s: Invalid maximum block size, "
1848 "assuming 512 bytes\n", mmc_hostname(mmc));
1849 mmc->max_blk_size = 0;
1850 }
1851 }
1852
1853 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001854
1855 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001856 * Maximum block count.
1857 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001858 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001859
1860 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001861 * Init tasklets.
1862 */
1863 tasklet_init(&host->card_tasklet,
1864 sdhci_tasklet_card, (unsigned long)host);
1865 tasklet_init(&host->finish_tasklet,
1866 sdhci_tasklet_finish, (unsigned long)host);
1867
Al Viroe4cad1b2006-10-10 22:47:07 +01001868 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001869
Thomas Gleixnerdace1452006-07-01 19:29:38 -07001870 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001871 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001872 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001873 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001875 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001876
1877#ifdef CONFIG_MMC_DEBUG
1878 sdhci_dumpregs(host);
1879#endif
1880
Pierre Ossmanf9134312008-12-21 17:01:48 +01001881#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01001882 snprintf(host->led_name, sizeof(host->led_name),
1883 "%s::", mmc_hostname(mmc));
1884 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001885 host->led.brightness = LED_OFF;
1886 host->led.default_trigger = mmc_hostname(mmc);
1887 host->led.brightness_set = sdhci_led_control;
1888
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001889 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001890 if (ret)
1891 goto reset;
1892#endif
1893
Pierre Ossman5f25a662006-10-04 02:15:39 -07001894 mmiowb();
1895
Pierre Ossmand129bce2006-03-24 03:18:17 -08001896 mmc_add_host(mmc);
1897
Richard Röjforsa13abc72009-09-22 16:45:30 -07001898 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01001899 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07001900 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
1901 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001902
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001903 sdhci_enable_card_detection(host);
1904
Pierre Ossmand129bce2006-03-24 03:18:17 -08001905 return 0;
1906
Pierre Ossmanf9134312008-12-21 17:01:48 +01001907#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001908reset:
1909 sdhci_reset(host, SDHCI_RESET_ALL);
1910 free_irq(host->irq, host);
1911#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07001912untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913 tasklet_kill(&host->card_tasklet);
1914 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001915
1916 return ret;
1917}
1918
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001919EXPORT_SYMBOL_GPL(sdhci_add_host);
1920
Pierre Ossman1e728592008-04-16 19:13:13 +02001921void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001922{
Pierre Ossman1e728592008-04-16 19:13:13 +02001923 unsigned long flags;
1924
1925 if (dead) {
1926 spin_lock_irqsave(&host->lock, flags);
1927
1928 host->flags |= SDHCI_DEVICE_DEAD;
1929
1930 if (host->mrq) {
1931 printk(KERN_ERR "%s: Controller removed during "
1932 " transfer!\n", mmc_hostname(host->mmc));
1933
1934 host->mrq->cmd->error = -ENOMEDIUM;
1935 tasklet_schedule(&host->finish_tasklet);
1936 }
1937
1938 spin_unlock_irqrestore(&host->lock, flags);
1939 }
1940
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001941 sdhci_disable_card_detection(host);
1942
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001943 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001944
Pierre Ossmanf9134312008-12-21 17:01:48 +01001945#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001946 led_classdev_unregister(&host->led);
1947#endif
1948
Pierre Ossman1e728592008-04-16 19:13:13 +02001949 if (!dead)
1950 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001951
1952 free_irq(host->irq, host);
1953
1954 del_timer_sync(&host->timer);
1955
1956 tasklet_kill(&host->card_tasklet);
1957 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02001958
1959 kfree(host->adma_desc);
1960 kfree(host->align_buffer);
1961
1962 host->adma_desc = NULL;
1963 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001964}
1965
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001966EXPORT_SYMBOL_GPL(sdhci_remove_host);
1967
1968void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001969{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001970 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001971}
1972
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001973EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001974
1975/*****************************************************************************\
1976 * *
1977 * Driver init/exit *
1978 * *
1979\*****************************************************************************/
1980
1981static int __init sdhci_drv_init(void)
1982{
1983 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01001984 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08001985 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
1986
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001987 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001988}
1989
1990static void __exit sdhci_drv_exit(void)
1991{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992}
1993
1994module_init(sdhci_drv_init);
1995module_exit(sdhci_drv_exit);
1996
Pierre Ossmandf673b22006-06-30 02:22:31 -07001997module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07001998
Pierre Ossman32710e82009-04-08 20:14:54 +02001999MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002000MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002001MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002002
Pierre Ossmandf673b22006-06-30 02:22:31 -07002003MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");