blob: a70a3d1ef35a9c5da6ccf8f6667dbd99e2d0e460 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080019#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020021#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070022#include <linux/regulator/consumer.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080023
Pierre Ossman2f730fe2008-03-17 10:29:38 +010024#include <linux/leds.h>
25
Aries Lee22113ef2010-12-15 08:14:24 +010026#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080028
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include "sdhci.h"
30
31#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010034 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmanf9134312008-12-21 17:01:48 +010036#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
37 defined(CONFIG_MMC_SDHCI_MODULE))
38#define SDHCI_USE_LEDS_CLASS
39#endif
40
Pierre Ossmandf673b22006-06-30 02:22:31 -070041static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070042
Pierre Ossmand129bce2006-03-24 03:18:17 -080043static void sdhci_finish_data(struct sdhci_host *);
44
45static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
46static void sdhci_finish_command(struct sdhci_host *);
47
48static void sdhci_dumpregs(struct sdhci_host *host)
49{
Philip Rakity412ab652010-09-22 15:25:13 -070050 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
51 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030054 sdhci_readl(host, SDHCI_DMA_ADDRESS),
55 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030057 sdhci_readw(host, SDHCI_BLOCK_SIZE),
58 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080059 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030060 sdhci_readl(host, SDHCI_ARGUMENT),
61 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080062 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030063 sdhci_readl(host, SDHCI_PRESENT_STATE),
64 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080065 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030066 sdhci_readb(host, SDHCI_POWER_CONTROL),
67 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080068 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030069 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
70 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080071 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030072 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
73 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080074 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_INT_ENABLE),
76 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080077 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_ACMD12_ERR),
79 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050080 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050082 sdhci_readl(host, SDHCI_CAPABILITIES_1));
83 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
84 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030085 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080086
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010087 if (host->flags & SDHCI_USE_ADMA)
88 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
89 readl(host->ioaddr + SDHCI_ADMA_ERROR),
90 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
91
Pierre Ossmand129bce2006-03-24 03:18:17 -080092 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
93}
94
95/*****************************************************************************\
96 * *
97 * Low level functions *
98 * *
99\*****************************************************************************/
100
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300101static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
102{
103 u32 ier;
104
105 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
106 ier &= ~clear;
107 ier |= set;
108 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
109 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
110}
111
112static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
113{
114 sdhci_clear_set_irqs(host, 0, irqs);
115}
116
117static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
118{
119 sdhci_clear_set_irqs(host, irqs, 0);
120}
121
122static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
123{
124 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
125
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300126 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
127 return;
128
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300129 if (enable)
130 sdhci_unmask_irqs(host, irqs);
131 else
132 sdhci_mask_irqs(host, irqs);
133}
134
135static void sdhci_enable_card_detection(struct sdhci_host *host)
136{
137 sdhci_set_card_detection(host, true);
138}
139
140static void sdhci_disable_card_detection(struct sdhci_host *host)
141{
142 sdhci_set_card_detection(host, false);
143}
144
Pierre Ossmand129bce2006-03-24 03:18:17 -0800145static void sdhci_reset(struct sdhci_host *host, u8 mask)
146{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700147 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300148 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700149
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100150 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300151 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700152 SDHCI_CARD_PRESENT))
153 return;
154 }
155
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300156 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
157 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
158
Philip Rakity393c1a32011-01-21 11:26:40 -0800159 if (host->ops->platform_reset_enter)
160 host->ops->platform_reset_enter(host, mask);
161
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300162 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800163
Pierre Ossmane16514d82006-06-30 02:22:24 -0700164 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800165 host->clock = 0;
166
Pierre Ossmane16514d82006-06-30 02:22:24 -0700167 /* Wait max 100 ms */
168 timeout = 100;
169
170 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300171 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700172 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100173 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700174 mmc_hostname(host->mmc), (int)mask);
175 sdhci_dumpregs(host);
176 return;
177 }
178 timeout--;
179 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300181
Philip Rakity393c1a32011-01-21 11:26:40 -0800182 if (host->ops->platform_reset_exit)
183 host->ops->platform_reset_exit(host, mask);
184
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300185 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
186 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187}
188
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800189static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
190
191static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800193 if (soft)
194 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
195 else
196 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800197
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300198 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
199 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700200 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
201 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300202 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800203
204 if (soft) {
205 /* force clock reconfiguration */
206 host->clock = 0;
207 sdhci_set_ios(host->mmc, &host->mmc->ios);
208 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300209}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300211static void sdhci_reinit(struct sdhci_host *host)
212{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800213 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300214 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800215}
216
217static void sdhci_activate_led(struct sdhci_host *host)
218{
219 u8 ctrl;
220
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300221 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300223 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224}
225
226static void sdhci_deactivate_led(struct sdhci_host *host)
227{
228 u8 ctrl;
229
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300230 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800231 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300232 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800233}
234
Pierre Ossmanf9134312008-12-21 17:01:48 +0100235#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100236static void sdhci_led_control(struct led_classdev *led,
237 enum led_brightness brightness)
238{
239 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
240 unsigned long flags;
241
242 spin_lock_irqsave(&host->lock, flags);
243
244 if (brightness == LED_OFF)
245 sdhci_deactivate_led(host);
246 else
247 sdhci_activate_led(host);
248
249 spin_unlock_irqrestore(&host->lock, flags);
250}
251#endif
252
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253/*****************************************************************************\
254 * *
255 * Core functions *
256 * *
257\*****************************************************************************/
258
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100259static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260{
Pierre Ossman76591502008-07-21 00:32:11 +0200261 unsigned long flags;
262 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700263 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200264 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100266 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100268 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200269 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270
Pierre Ossman76591502008-07-21 00:32:11 +0200271 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100273 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200274 if (!sg_miter_next(&host->sg_miter))
275 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276
Pierre Ossman76591502008-07-21 00:32:11 +0200277 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278
Pierre Ossman76591502008-07-21 00:32:11 +0200279 blksize -= len;
280 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200281
Pierre Ossman76591502008-07-21 00:32:11 +0200282 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283
Pierre Ossman76591502008-07-21 00:32:11 +0200284 while (len) {
285 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300286 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200287 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800288 }
Pierre Ossman76591502008-07-21 00:32:11 +0200289
290 *buf = scratch & 0xFF;
291
292 buf++;
293 scratch >>= 8;
294 chunk--;
295 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800296 }
297 }
Pierre Ossman76591502008-07-21 00:32:11 +0200298
299 sg_miter_stop(&host->sg_miter);
300
301 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100302}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304static void sdhci_write_block_pio(struct sdhci_host *host)
305{
Pierre Ossman76591502008-07-21 00:32:11 +0200306 unsigned long flags;
307 size_t blksize, len, chunk;
308 u32 scratch;
309 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310
311 DBG("PIO writing\n");
312
313 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200314 chunk = 0;
315 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318
319 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200320 if (!sg_miter_next(&host->sg_miter))
321 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322
Pierre Ossman76591502008-07-21 00:32:11 +0200323 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 blksize -= len;
326 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329
Pierre Ossman76591502008-07-21 00:32:11 +0200330 while (len) {
331 scratch |= (u32)*buf << (chunk * 8);
332
333 buf++;
334 chunk++;
335 len--;
336
337 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300338 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200339 chunk = 0;
340 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100341 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342 }
343 }
Pierre Ossman76591502008-07-21 00:32:11 +0200344
345 sg_miter_stop(&host->sg_miter);
346
347 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348}
349
350static void sdhci_transfer_pio(struct sdhci_host *host)
351{
352 u32 mask;
353
354 BUG_ON(!host->data);
355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357 return;
358
359 if (host->data->flags & MMC_DATA_READ)
360 mask = SDHCI_DATA_AVAILABLE;
361 else
362 mask = SDHCI_SPACE_AVAILABLE;
363
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200364 /*
365 * Some controllers (JMicron JMB38x) mess up the buffer bits
366 * for transfers < 4 bytes. As long as it is just one block,
367 * we can ignore the bits.
368 */
369 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
370 (host->data->blocks == 1))
371 mask = ~0;
372
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300373 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300374 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
375 udelay(100);
376
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100377 if (host->data->flags & MMC_DATA_READ)
378 sdhci_read_block_pio(host);
379 else
380 sdhci_write_block_pio(host);
381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 host->blocks--;
383 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100384 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385 }
386
387 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800388}
389
Pierre Ossman2134a922008-06-28 18:28:51 +0200390static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
391{
392 local_irq_save(*flags);
393 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
394}
395
396static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
397{
398 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
399 local_irq_restore(*flags);
400}
401
Ben Dooks118cd172010-03-05 13:43:26 -0800402static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
403{
Ben Dooks9e506f32010-03-05 13:43:29 -0800404 __le32 *dataddr = (__le32 __force *)(desc + 4);
405 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800406
Ben Dooks9e506f32010-03-05 13:43:29 -0800407 /* SDHCI specification says ADMA descriptors should be 4 byte
408 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800409
Ben Dooks9e506f32010-03-05 13:43:29 -0800410 cmdlen[0] = cpu_to_le16(cmd);
411 cmdlen[1] = cpu_to_le16(len);
412
413 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800414}
415
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200416static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200417 struct mmc_data *data)
418{
419 int direction;
420
421 u8 *desc;
422 u8 *align;
423 dma_addr_t addr;
424 dma_addr_t align_addr;
425 int len, offset;
426
427 struct scatterlist *sg;
428 int i;
429 char *buffer;
430 unsigned long flags;
431
432 /*
433 * The spec does not specify endianness of descriptor table.
434 * We currently guess that it is LE.
435 */
436
437 if (data->flags & MMC_DATA_READ)
438 direction = DMA_FROM_DEVICE;
439 else
440 direction = DMA_TO_DEVICE;
441
442 /*
443 * The ADMA descriptor table is mapped further down as we
444 * need to fill it with data first.
445 */
446
447 host->align_addr = dma_map_single(mmc_dev(host->mmc),
448 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700449 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200450 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200451 BUG_ON(host->align_addr & 0x3);
452
453 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
454 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200455 if (host->sg_count == 0)
456 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200457
458 desc = host->adma_desc;
459 align = host->align_buffer;
460
461 align_addr = host->align_addr;
462
463 for_each_sg(data->sg, sg, host->sg_count, i) {
464 addr = sg_dma_address(sg);
465 len = sg_dma_len(sg);
466
467 /*
468 * The SDHCI specification states that ADMA
469 * addresses must be 32-bit aligned. If they
470 * aren't, then we use a bounce buffer for
471 * the (up to three) bytes that screw up the
472 * alignment.
473 */
474 offset = (4 - (addr & 0x3)) & 0x3;
475 if (offset) {
476 if (data->flags & MMC_DATA_WRITE) {
477 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200478 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200479 memcpy(align, buffer, offset);
480 sdhci_kunmap_atomic(buffer, &flags);
481 }
482
Ben Dooks118cd172010-03-05 13:43:26 -0800483 /* tran, valid */
484 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200485
486 BUG_ON(offset > 65536);
487
Pierre Ossman2134a922008-06-28 18:28:51 +0200488 align += 4;
489 align_addr += 4;
490
491 desc += 8;
492
493 addr += offset;
494 len -= offset;
495 }
496
Pierre Ossman2134a922008-06-28 18:28:51 +0200497 BUG_ON(len > 65536);
498
Ben Dooks118cd172010-03-05 13:43:26 -0800499 /* tran, valid */
500 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200501 desc += 8;
502
503 /*
504 * If this triggers then we have a calculation bug
505 * somewhere. :/
506 */
507 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
508 }
509
Thomas Abraham70764a92010-05-26 14:42:04 -0700510 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
511 /*
512 * Mark the last descriptor as the terminating descriptor
513 */
514 if (desc != host->adma_desc) {
515 desc -= 8;
516 desc[0] |= 0x2; /* end */
517 }
518 } else {
519 /*
520 * Add a terminating entry.
521 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200522
Thomas Abraham70764a92010-05-26 14:42:04 -0700523 /* nop, end, valid */
524 sdhci_set_adma_desc(desc, 0, 0, 0x3);
525 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200526
527 /*
528 * Resync align buffer as we might have changed it.
529 */
530 if (data->flags & MMC_DATA_WRITE) {
531 dma_sync_single_for_device(mmc_dev(host->mmc),
532 host->align_addr, 128 * 4, direction);
533 }
534
535 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
536 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200537 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200538 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200539 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200540
541 return 0;
542
543unmap_entries:
544 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
545 data->sg_len, direction);
546unmap_align:
547 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
548 128 * 4, direction);
549fail:
550 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200551}
552
553static void sdhci_adma_table_post(struct sdhci_host *host,
554 struct mmc_data *data)
555{
556 int direction;
557
558 struct scatterlist *sg;
559 int i, size;
560 u8 *align;
561 char *buffer;
562 unsigned long flags;
563
564 if (data->flags & MMC_DATA_READ)
565 direction = DMA_FROM_DEVICE;
566 else
567 direction = DMA_TO_DEVICE;
568
569 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
570 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
571
572 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
573 128 * 4, direction);
574
575 if (data->flags & MMC_DATA_READ) {
576 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
577 data->sg_len, direction);
578
579 align = host->align_buffer;
580
581 for_each_sg(data->sg, sg, host->sg_count, i) {
582 if (sg_dma_address(sg) & 0x3) {
583 size = 4 - (sg_dma_address(sg) & 0x3);
584
585 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200586 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200587 memcpy(buffer, align, size);
588 sdhci_kunmap_atomic(buffer, &flags);
589
590 align += 4;
591 }
592 }
593 }
594
595 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
596 data->sg_len, direction);
597}
598
Andrei Warkentina3c77782011-04-11 16:13:42 -0500599static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800600{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700601 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500602 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700603 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800604
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200605 /*
606 * If the host controller provides us with an incorrect timeout
607 * value, just skip the check and use 0xE. The hardware may take
608 * longer to time out, but that's much better than having a too-short
609 * timeout value.
610 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200611 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200612 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200613
Andrei Warkentina3c77782011-04-11 16:13:42 -0500614 /* Unspecified timeout, assume max */
615 if (!data && !cmd->cmd_timeout_ms)
616 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800617
Andrei Warkentina3c77782011-04-11 16:13:42 -0500618 /* timeout in us */
619 if (!data)
620 target_timeout = cmd->cmd_timeout_ms * 1000;
621 else
622 target_timeout = data->timeout_ns / 1000 +
623 data->timeout_clks / host->clock;
Anton Vorontsov81b39802009-09-22 16:45:13 -0700624
Mark Brown4b016812011-04-19 18:44:17 +0100625 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
626 host->timeout_clk = host->clock / 1000;
627
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700628 /*
629 * Figure out needed cycles.
630 * We do this in steps in order to fit inside a 32 bit int.
631 * The first step is the minimum timeout, which will have a
632 * minimum resolution of 6 bits:
633 * (1) 2^13*1000 > 2^22,
634 * (2) host->timeout_clk < 2^16
635 * =>
636 * (1) / (2) > 2^6
637 */
Mark Brown4b016812011-04-19 18:44:17 +0100638 BUG_ON(!host->timeout_clk);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700639 count = 0;
640 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
641 while (current_timeout < target_timeout) {
642 count++;
643 current_timeout <<= 1;
644 if (count >= 0xF)
645 break;
646 }
647
648 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500649 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
650 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700651 count = 0xE;
652 }
653
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200654 return count;
655}
656
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300657static void sdhci_set_transfer_irqs(struct sdhci_host *host)
658{
659 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
660 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
661
662 if (host->flags & SDHCI_REQ_USE_DMA)
663 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
664 else
665 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
666}
667
Andrei Warkentina3c77782011-04-11 16:13:42 -0500668static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200669{
670 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200671 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500672 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200673 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200674
675 WARN_ON(host->data);
676
Andrei Warkentina3c77782011-04-11 16:13:42 -0500677 if (data || (cmd->flags & MMC_RSP_BUSY)) {
678 count = sdhci_calc_timeout(host, cmd);
679 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
680 }
681
682 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200683 return;
684
685 /* Sanity checks */
686 BUG_ON(data->blksz * data->blocks > 524288);
687 BUG_ON(data->blksz > host->mmc->max_blk_size);
688 BUG_ON(data->blocks > 65535);
689
690 host->data = data;
691 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400692 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200693
Richard Röjforsa13abc72009-09-22 16:45:30 -0700694 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100695 host->flags |= SDHCI_REQ_USE_DMA;
696
Pierre Ossman2134a922008-06-28 18:28:51 +0200697 /*
698 * FIXME: This doesn't account for merging when mapping the
699 * scatterlist.
700 */
701 if (host->flags & SDHCI_REQ_USE_DMA) {
702 int broken, i;
703 struct scatterlist *sg;
704
705 broken = 0;
706 if (host->flags & SDHCI_USE_ADMA) {
707 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
708 broken = 1;
709 } else {
710 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
711 broken = 1;
712 }
713
714 if (unlikely(broken)) {
715 for_each_sg(data->sg, sg, data->sg_len, i) {
716 if (sg->length & 0x3) {
717 DBG("Reverting to PIO because of "
718 "transfer size (%d)\n",
719 sg->length);
720 host->flags &= ~SDHCI_REQ_USE_DMA;
721 break;
722 }
723 }
724 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100725 }
726
727 /*
728 * The assumption here being that alignment is the same after
729 * translation to device address space.
730 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200731 if (host->flags & SDHCI_REQ_USE_DMA) {
732 int broken, i;
733 struct scatterlist *sg;
734
735 broken = 0;
736 if (host->flags & SDHCI_USE_ADMA) {
737 /*
738 * As we use 3 byte chunks to work around
739 * alignment problems, we need to check this
740 * quirk.
741 */
742 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
743 broken = 1;
744 } else {
745 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
746 broken = 1;
747 }
748
749 if (unlikely(broken)) {
750 for_each_sg(data->sg, sg, data->sg_len, i) {
751 if (sg->offset & 0x3) {
752 DBG("Reverting to PIO because of "
753 "bad alignment\n");
754 host->flags &= ~SDHCI_REQ_USE_DMA;
755 break;
756 }
757 }
758 }
759 }
760
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200761 if (host->flags & SDHCI_REQ_USE_DMA) {
762 if (host->flags & SDHCI_USE_ADMA) {
763 ret = sdhci_adma_table_pre(host, data);
764 if (ret) {
765 /*
766 * This only happens when someone fed
767 * us an invalid request.
768 */
769 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200770 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200771 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300772 sdhci_writel(host, host->adma_addr,
773 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200774 }
775 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300776 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200777
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300778 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200779 data->sg, data->sg_len,
780 (data->flags & MMC_DATA_READ) ?
781 DMA_FROM_DEVICE :
782 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300783 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200784 /*
785 * This only happens when someone fed
786 * us an invalid request.
787 */
788 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200789 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200790 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200791 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300792 sdhci_writel(host, sg_dma_address(data->sg),
793 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200794 }
795 }
796 }
797
Pierre Ossman2134a922008-06-28 18:28:51 +0200798 /*
799 * Always adjust the DMA selection as some controllers
800 * (e.g. JMicron) can't do PIO properly when the selection
801 * is ADMA.
802 */
803 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300804 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200805 ctrl &= ~SDHCI_CTRL_DMA_MASK;
806 if ((host->flags & SDHCI_REQ_USE_DMA) &&
807 (host->flags & SDHCI_USE_ADMA))
808 ctrl |= SDHCI_CTRL_ADMA32;
809 else
810 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300811 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100812 }
813
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200814 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200815 int flags;
816
817 flags = SG_MITER_ATOMIC;
818 if (host->data->flags & MMC_DATA_READ)
819 flags |= SG_MITER_TO_SG;
820 else
821 flags |= SG_MITER_FROM_SG;
822 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200823 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800824 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700825
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300826 sdhci_set_transfer_irqs(host);
827
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400828 /* Set the DMA boundary value and block size */
829 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
830 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300831 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700832}
833
834static void sdhci_set_transfer_mode(struct sdhci_host *host,
835 struct mmc_data *data)
836{
837 u16 mode;
838
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700839 if (data == NULL)
840 return;
841
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200842 WARN_ON(!host->data);
843
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700844 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700845 if (data->blocks > 1) {
846 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
847 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
848 else
849 mode |= SDHCI_TRNS_MULTI;
850 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700851 if (data->flags & MMC_DATA_READ)
852 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100853 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700854 mode |= SDHCI_TRNS_DMA;
855
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300856 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800857}
858
859static void sdhci_finish_data(struct sdhci_host *host)
860{
861 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800862
863 BUG_ON(!host->data);
864
865 data = host->data;
866 host->data = NULL;
867
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100868 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200869 if (host->flags & SDHCI_USE_ADMA)
870 sdhci_adma_table_post(host, data);
871 else {
872 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
873 data->sg_len, (data->flags & MMC_DATA_READ) ?
874 DMA_FROM_DEVICE : DMA_TO_DEVICE);
875 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800876 }
877
878 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200879 * The specification states that the block count register must
880 * be updated, but it does not specify at what point in the
881 * data flow. That makes the register entirely useless to read
882 * back so we have to assume that nothing made it to the card
883 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800884 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200885 if (data->error)
886 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800887 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200888 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800889
Pierre Ossmand129bce2006-03-24 03:18:17 -0800890 if (data->stop) {
891 /*
892 * The controller needs a reset of internal state machines
893 * upon error conditions.
894 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200895 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896 sdhci_reset(host, SDHCI_RESET_CMD);
897 sdhci_reset(host, SDHCI_RESET_DATA);
898 }
899
900 sdhci_send_command(host, data->stop);
901 } else
902 tasklet_schedule(&host->finish_tasklet);
903}
904
905static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
906{
907 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700908 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700909 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800910
911 WARN_ON(host->cmd);
912
Pierre Ossmand129bce2006-03-24 03:18:17 -0800913 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700914 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700915
916 mask = SDHCI_CMD_INHIBIT;
917 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
918 mask |= SDHCI_DATA_INHIBIT;
919
920 /* We shouldn't wait for data inihibit for stop commands, even
921 though they might use busy signaling */
922 if (host->mrq->data && (cmd == host->mrq->data->stop))
923 mask &= ~SDHCI_DATA_INHIBIT;
924
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300925 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700926 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100928 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200930 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 tasklet_schedule(&host->finish_tasklet);
932 return;
933 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700934 timeout--;
935 mdelay(1);
936 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800937
938 mod_timer(&host->timer, jiffies + 10 * HZ);
939
940 host->cmd = cmd;
941
Andrei Warkentina3c77782011-04-11 16:13:42 -0500942 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300944 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700946 sdhci_set_transfer_mode(host, cmd->data);
947
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100949 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200951 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 tasklet_schedule(&host->finish_tasklet);
953 return;
954 }
955
956 if (!(cmd->flags & MMC_RSP_PRESENT))
957 flags = SDHCI_CMD_RESP_NONE;
958 else if (cmd->flags & MMC_RSP_136)
959 flags = SDHCI_CMD_RESP_LONG;
960 else if (cmd->flags & MMC_RSP_BUSY)
961 flags = SDHCI_CMD_RESP_SHORT_BUSY;
962 else
963 flags = SDHCI_CMD_RESP_SHORT;
964
965 if (cmd->flags & MMC_RSP_CRC)
966 flags |= SDHCI_CMD_CRC;
967 if (cmd->flags & MMC_RSP_OPCODE)
968 flags |= SDHCI_CMD_INDEX;
969 if (cmd->data)
970 flags |= SDHCI_CMD_DATA;
971
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300972 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973}
974
975static void sdhci_finish_command(struct sdhci_host *host)
976{
977 int i;
978
979 BUG_ON(host->cmd == NULL);
980
981 if (host->cmd->flags & MMC_RSP_PRESENT) {
982 if (host->cmd->flags & MMC_RSP_136) {
983 /* CRC is stripped so we need to do some shifting. */
984 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300985 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800986 SDHCI_RESPONSE + (3-i)*4) << 8;
987 if (i != 3)
988 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300989 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 SDHCI_RESPONSE + (3-i)*4-1);
991 }
992 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300993 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 }
995 }
996
Pierre Ossman17b04292007-07-22 22:18:46 +0200997 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200999 if (host->data && host->data_early)
1000 sdhci_finish_data(host);
1001
1002 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003 tasklet_schedule(&host->finish_tasklet);
1004
1005 host->cmd = NULL;
1006}
1007
1008static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1009{
1010 int div;
1011 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001012 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
1014 if (clock == host->clock)
1015 return;
1016
Anton Vorontsov81146342009-03-17 00:13:59 +03001017 if (host->ops->set_clock) {
1018 host->ops->set_clock(host, clock);
1019 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1020 return;
1021 }
1022
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001023 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024
1025 if (clock == 0)
1026 goto out;
1027
Zhangfei Gao85105c52010-08-06 07:10:01 +08001028 if (host->version >= SDHCI_SPEC_300) {
1029 /* Version 3.00 divisors must be a multiple of 2. */
1030 if (host->max_clk <= clock)
1031 div = 1;
1032 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001033 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001034 if ((host->max_clk / div) <= clock)
1035 break;
1036 }
1037 }
1038 } else {
1039 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001040 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001041 if ((host->max_clk / div) <= clock)
1042 break;
1043 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 }
1045 div >>= 1;
1046
Zhangfei Gao85105c52010-08-06 07:10:01 +08001047 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1048 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1049 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001051 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052
Chris Ball27f6cb12009-09-22 16:45:31 -07001053 /* Wait max 20 ms */
1054 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001055 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001056 & SDHCI_CLOCK_INT_STABLE)) {
1057 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001058 printk(KERN_ERR "%s: Internal clock never "
1059 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 sdhci_dumpregs(host);
1061 return;
1062 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001063 timeout--;
1064 mdelay(1);
1065 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001066
1067 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069
1070out:
1071 host->clock = clock;
1072}
1073
Pierre Ossman146ad662006-06-30 02:22:23 -07001074static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1075{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001076 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001077
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001078 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001079 switch (1 << power) {
1080 case MMC_VDD_165_195:
1081 pwr = SDHCI_POWER_180;
1082 break;
1083 case MMC_VDD_29_30:
1084 case MMC_VDD_30_31:
1085 pwr = SDHCI_POWER_300;
1086 break;
1087 case MMC_VDD_32_33:
1088 case MMC_VDD_33_34:
1089 pwr = SDHCI_POWER_330;
1090 break;
1091 default:
1092 BUG();
1093 }
1094 }
1095
1096 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001097 return;
1098
Pierre Ossmanae628902009-05-03 20:45:03 +02001099 host->pwr = pwr;
1100
1101 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001102 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001103 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001104 }
1105
1106 /*
1107 * Spec says that we should clear the power reg before setting
1108 * a new value. Some controllers don't seem to like this though.
1109 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001110 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001111 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001112
Andres Salomone08c1692008-07-04 10:00:03 -07001113 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001114 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001115 * and set turn on power at the same time, so set the voltage first.
1116 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001117 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001118 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1119
1120 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001121
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001122 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001123
1124 /*
1125 * Some controllers need an extra 10ms delay of 10ms before they
1126 * can apply clock after applying power
1127 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001128 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001129 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001130}
1131
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132/*****************************************************************************\
1133 * *
1134 * MMC callbacks *
1135 * *
1136\*****************************************************************************/
1137
1138static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1139{
1140 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001141 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142 unsigned long flags;
1143
1144 host = mmc_priv(mmc);
1145
1146 spin_lock_irqsave(&host->lock, flags);
1147
1148 WARN_ON(host->mrq != NULL);
1149
Pierre Ossmanf9134312008-12-21 17:01:48 +01001150#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001152#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001153 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1154 if (mrq->stop) {
1155 mrq->data->stop = NULL;
1156 mrq->stop = NULL;
1157 }
1158 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159
1160 host->mrq = mrq;
1161
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001162 /* If polling, assume that the card is always present. */
1163 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1164 present = true;
1165 else
1166 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1167 SDHCI_CARD_PRESENT;
1168
1169 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001170 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171 tasklet_schedule(&host->finish_tasklet);
1172 } else
1173 sdhci_send_command(host, mrq->cmd);
1174
Pierre Ossman5f25a662006-10-04 02:15:39 -07001175 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176 spin_unlock_irqrestore(&host->lock, flags);
1177}
1178
1179static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1180{
1181 struct sdhci_host *host;
1182 unsigned long flags;
1183 u8 ctrl;
1184
1185 host = mmc_priv(mmc);
1186
1187 spin_lock_irqsave(&host->lock, flags);
1188
Pierre Ossman1e728592008-04-16 19:13:13 +02001189 if (host->flags & SDHCI_DEVICE_DEAD)
1190 goto out;
1191
Pierre Ossmand129bce2006-03-24 03:18:17 -08001192 /*
1193 * Reset the chip on each power off.
1194 * Should clear out any weird states.
1195 */
1196 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001197 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001198 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199 }
1200
1201 sdhci_set_clock(host, ios->clock);
1202
1203 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001204 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001205 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001206 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207
Philip Rakity643a81f2010-09-23 08:24:32 -07001208 if (host->ops->platform_send_init_74_clocks)
1209 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1210
Philip Rakity15ec4462010-11-19 16:48:39 -05001211 /*
1212 * If your platform has 8-bit width support but is not a v3 controller,
1213 * or if it requires special setup code, you should implement that in
1214 * platform_8bit_width().
1215 */
1216 if (host->ops->platform_8bit_width)
1217 host->ops->platform_8bit_width(host, ios->bus_width);
1218 else {
1219 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1220 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1221 ctrl &= ~SDHCI_CTRL_4BITBUS;
1222 if (host->version >= SDHCI_SPEC_300)
1223 ctrl |= SDHCI_CTRL_8BITBUS;
1224 } else {
1225 if (host->version >= SDHCI_SPEC_300)
1226 ctrl &= ~SDHCI_CTRL_8BITBUS;
1227 if (ios->bus_width == MMC_BUS_WIDTH_4)
1228 ctrl |= SDHCI_CTRL_4BITBUS;
1229 else
1230 ctrl &= ~SDHCI_CTRL_4BITBUS;
1231 }
1232 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1233 }
1234
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001235 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001236
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001237 if ((ios->timing == MMC_TIMING_SD_HS ||
1238 ios->timing == MMC_TIMING_MMC_HS)
1239 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001240 ctrl |= SDHCI_CTRL_HISPD;
1241 else
1242 ctrl &= ~SDHCI_CTRL_HISPD;
1243
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001244 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245
Leandro Dorileob8352262007-07-25 23:47:04 +02001246 /*
1247 * Some (ENE) controllers go apeshit on some ios operation,
1248 * signalling timeout and CRC errors even on CMD0. Resetting
1249 * it on each ios seems to solve the problem.
1250 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001251 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001252 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1253
Pierre Ossman1e728592008-04-16 19:13:13 +02001254out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001255 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256 spin_unlock_irqrestore(&host->lock, flags);
1257}
1258
1259static int sdhci_get_ro(struct mmc_host *mmc)
1260{
1261 struct sdhci_host *host;
1262 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001263 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
1265 host = mmc_priv(mmc);
1266
1267 spin_lock_irqsave(&host->lock, flags);
1268
Pierre Ossman1e728592008-04-16 19:13:13 +02001269 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001270 is_readonly = 0;
1271 else if (host->ops->get_ro)
1272 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001273 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001274 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1275 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276
1277 spin_unlock_irqrestore(&host->lock, flags);
1278
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001279 /* This quirk needs to be replaced by a callback-function later */
1280 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1281 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001282}
1283
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001284static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1285{
1286 struct sdhci_host *host;
1287 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001288
1289 host = mmc_priv(mmc);
1290
1291 spin_lock_irqsave(&host->lock, flags);
1292
Pierre Ossman1e728592008-04-16 19:13:13 +02001293 if (host->flags & SDHCI_DEVICE_DEAD)
1294 goto out;
1295
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001296 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001297 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1298 else
1299 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001300out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001301 mmiowb();
1302
1303 spin_unlock_irqrestore(&host->lock, flags);
1304}
1305
David Brownellab7aefd2006-11-12 17:55:30 -08001306static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307 .request = sdhci_request,
1308 .set_ios = sdhci_set_ios,
1309 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001310 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311};
1312
1313/*****************************************************************************\
1314 * *
1315 * Tasklets *
1316 * *
1317\*****************************************************************************/
1318
1319static void sdhci_tasklet_card(unsigned long param)
1320{
1321 struct sdhci_host *host;
1322 unsigned long flags;
1323
1324 host = (struct sdhci_host*)param;
1325
1326 spin_lock_irqsave(&host->lock, flags);
1327
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001328 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 if (host->mrq) {
1330 printk(KERN_ERR "%s: Card removed during transfer!\n",
1331 mmc_hostname(host->mmc));
1332 printk(KERN_ERR "%s: Resetting controller.\n",
1333 mmc_hostname(host->mmc));
1334
1335 sdhci_reset(host, SDHCI_RESET_CMD);
1336 sdhci_reset(host, SDHCI_RESET_DATA);
1337
Pierre Ossman17b04292007-07-22 22:18:46 +02001338 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339 tasklet_schedule(&host->finish_tasklet);
1340 }
1341 }
1342
1343 spin_unlock_irqrestore(&host->lock, flags);
1344
Pierre Ossman04cf5852008-08-18 22:18:14 +02001345 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001346}
1347
1348static void sdhci_tasklet_finish(unsigned long param)
1349{
1350 struct sdhci_host *host;
1351 unsigned long flags;
1352 struct mmc_request *mrq;
1353
1354 host = (struct sdhci_host*)param;
1355
Chris Ball0c9c99a2011-04-27 17:35:31 -04001356 /*
1357 * If this tasklet gets rescheduled while running, it will
1358 * be run again afterwards but without any active request.
1359 */
1360 if (!host->mrq)
1361 return;
1362
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363 spin_lock_irqsave(&host->lock, flags);
1364
1365 del_timer(&host->timer);
1366
1367 mrq = host->mrq;
1368
Pierre Ossmand129bce2006-03-24 03:18:17 -08001369 /*
1370 * The controller needs a reset of internal state machines
1371 * upon error conditions.
1372 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001373 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001374 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001375 (mrq->data && (mrq->data->error ||
1376 (mrq->data->stop && mrq->data->stop->error))) ||
1377 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001378
1379 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001380 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001381 unsigned int clock;
1382
1383 /* This is to force an update */
1384 clock = host->clock;
1385 host->clock = 0;
1386 sdhci_set_clock(host, clock);
1387 }
1388
1389 /* Spec says we should do both at the same time, but Ricoh
1390 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001391 sdhci_reset(host, SDHCI_RESET_CMD);
1392 sdhci_reset(host, SDHCI_RESET_DATA);
1393 }
1394
1395 host->mrq = NULL;
1396 host->cmd = NULL;
1397 host->data = NULL;
1398
Pierre Ossmanf9134312008-12-21 17:01:48 +01001399#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001400 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001401#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402
Pierre Ossman5f25a662006-10-04 02:15:39 -07001403 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404 spin_unlock_irqrestore(&host->lock, flags);
1405
1406 mmc_request_done(host->mmc, mrq);
1407}
1408
1409static void sdhci_timeout_timer(unsigned long data)
1410{
1411 struct sdhci_host *host;
1412 unsigned long flags;
1413
1414 host = (struct sdhci_host*)data;
1415
1416 spin_lock_irqsave(&host->lock, flags);
1417
1418 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001419 printk(KERN_ERR "%s: Timeout waiting for hardware "
1420 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001421 sdhci_dumpregs(host);
1422
1423 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001424 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425 sdhci_finish_data(host);
1426 } else {
1427 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001428 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001430 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431
1432 tasklet_schedule(&host->finish_tasklet);
1433 }
1434 }
1435
Pierre Ossman5f25a662006-10-04 02:15:39 -07001436 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001437 spin_unlock_irqrestore(&host->lock, flags);
1438}
1439
1440/*****************************************************************************\
1441 * *
1442 * Interrupt handling *
1443 * *
1444\*****************************************************************************/
1445
1446static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1447{
1448 BUG_ON(intmask == 0);
1449
1450 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001451 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1452 "though no command operation was in progress.\n",
1453 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454 sdhci_dumpregs(host);
1455 return;
1456 }
1457
Pierre Ossman43b58b32007-07-25 23:15:27 +02001458 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001459 host->cmd->error = -ETIMEDOUT;
1460 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1461 SDHCI_INT_INDEX))
1462 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001463
Pierre Ossmane8095172008-07-25 01:09:08 +02001464 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001466 return;
1467 }
1468
1469 /*
1470 * The host can send and interrupt when the busy state has
1471 * ended, allowing us to wait without wasting CPU cycles.
1472 * Unfortunately this is overloaded on the "data complete"
1473 * interrupt, so we need to take some care when handling
1474 * it.
1475 *
1476 * Note: The 1.0 specification is a bit ambiguous about this
1477 * feature so there might be some problems with older
1478 * controllers.
1479 */
1480 if (host->cmd->flags & MMC_RSP_BUSY) {
1481 if (host->cmd->data)
1482 DBG("Cannot wait for busy signal when also "
1483 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001484 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001485 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001486
1487 /* The controller does not support the end-of-busy IRQ,
1488 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001489 }
1490
1491 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001492 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493}
1494
George G. Davis0957c332010-02-18 12:32:12 -05001495#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001496static void sdhci_show_adma_error(struct sdhci_host *host)
1497{
1498 const char *name = mmc_hostname(host->mmc);
1499 u8 *desc = host->adma_desc;
1500 __le32 *dma;
1501 __le16 *len;
1502 u8 attr;
1503
1504 sdhci_dumpregs(host);
1505
1506 while (true) {
1507 dma = (__le32 *)(desc + 4);
1508 len = (__le16 *)(desc + 2);
1509 attr = *desc;
1510
1511 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1512 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1513
1514 desc += 8;
1515
1516 if (attr & 2)
1517 break;
1518 }
1519}
1520#else
1521static void sdhci_show_adma_error(struct sdhci_host *host) { }
1522#endif
1523
Pierre Ossmand129bce2006-03-24 03:18:17 -08001524static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1525{
1526 BUG_ON(intmask == 0);
1527
1528 if (!host->data) {
1529 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001530 * The "data complete" interrupt is also used to
1531 * indicate that a busy state has ended. See comment
1532 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001533 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001534 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1535 if (intmask & SDHCI_INT_DATA_END) {
1536 sdhci_finish_command(host);
1537 return;
1538 }
1539 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001540
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001541 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1542 "though no data operation was in progress.\n",
1543 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001544 sdhci_dumpregs(host);
1545
1546 return;
1547 }
1548
1549 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001550 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001551 else if (intmask & SDHCI_INT_DATA_END_BIT)
1552 host->data->error = -EILSEQ;
1553 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1554 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1555 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001556 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001557 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1558 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1559 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001560 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001561 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001562
Pierre Ossman17b04292007-07-22 22:18:46 +02001563 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001564 sdhci_finish_data(host);
1565 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001566 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001567 sdhci_transfer_pio(host);
1568
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001569 /*
1570 * We currently don't do anything fancy with DMA
1571 * boundaries, but as we can't disable the feature
1572 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001573 *
1574 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
1575 * should return a valid address to continue from, but as
1576 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001577 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001578 if (intmask & SDHCI_INT_DMA_END) {
1579 u32 dmastart, dmanow;
1580 dmastart = sg_dma_address(host->data->sg);
1581 dmanow = dmastart + host->data->bytes_xfered;
1582 /*
1583 * Force update to the next DMA block boundary.
1584 */
1585 dmanow = (dmanow &
1586 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
1587 SDHCI_DEFAULT_BOUNDARY_SIZE;
1588 host->data->bytes_xfered = dmanow - dmastart;
1589 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
1590 " next 0x%08x\n",
1591 mmc_hostname(host->mmc), dmastart,
1592 host->data->bytes_xfered, dmanow);
1593 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
1594 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001595
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001596 if (intmask & SDHCI_INT_DATA_END) {
1597 if (host->cmd) {
1598 /*
1599 * Data managed to finish before the
1600 * command completed. Make sure we do
1601 * things in the proper order.
1602 */
1603 host->data_early = 1;
1604 } else {
1605 sdhci_finish_data(host);
1606 }
1607 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608 }
1609}
1610
David Howells7d12e782006-10-05 14:55:46 +01001611static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612{
1613 irqreturn_t result;
1614 struct sdhci_host* host = dev_id;
1615 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001616 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617
1618 spin_lock(&host->lock);
1619
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001620 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621
Mark Lord62df67a52007-03-06 13:30:13 +01001622 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001623 result = IRQ_NONE;
1624 goto out;
1625 }
1626
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001627 DBG("*** %s got interrupt: 0x%08x\n",
1628 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629
Pierre Ossman3192a282006-06-30 02:22:26 -07001630 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001631 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1632 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001633 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001634 }
1635
1636 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637
1638 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001639 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1640 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001641 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642 }
1643
1644 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001645 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1646 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001647 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001648 }
1649
1650 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1651
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001652 intmask &= ~SDHCI_INT_ERROR;
1653
Pierre Ossmand129bce2006-03-24 03:18:17 -08001654 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001655 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001656 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001657 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658 }
1659
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001660 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001661
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001662 if (intmask & SDHCI_INT_CARD_INT)
1663 cardint = 1;
1664
1665 intmask &= ~SDHCI_INT_CARD_INT;
1666
Pierre Ossman3192a282006-06-30 02:22:26 -07001667 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001668 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001669 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670 sdhci_dumpregs(host);
1671
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001672 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001673 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001674
1675 result = IRQ_HANDLED;
1676
Pierre Ossman5f25a662006-10-04 02:15:39 -07001677 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001678out:
1679 spin_unlock(&host->lock);
1680
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001681 /*
1682 * We have to delay this as it calls back into the driver.
1683 */
1684 if (cardint)
1685 mmc_signal_sdio_irq(host->mmc);
1686
Pierre Ossmand129bce2006-03-24 03:18:17 -08001687 return result;
1688}
1689
1690/*****************************************************************************\
1691 * *
1692 * Suspend/resume *
1693 * *
1694\*****************************************************************************/
1695
1696#ifdef CONFIG_PM
1697
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001698int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001699{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001700 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001701
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001702 sdhci_disable_card_detection(host);
1703
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001704 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001705 if (ret)
1706 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001707
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001708 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001709
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001710 if (host->vmmc)
1711 ret = regulator_disable(host->vmmc);
1712
1713 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001714}
1715
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001716EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001717
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001718int sdhci_resume_host(struct sdhci_host *host)
1719{
1720 int ret;
1721
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001722 if (host->vmmc) {
1723 int ret = regulator_enable(host->vmmc);
1724 if (ret)
1725 return ret;
1726 }
1727
1728
Richard Röjforsa13abc72009-09-22 16:45:30 -07001729 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001730 if (host->ops->enable_dma)
1731 host->ops->enable_dma(host);
1732 }
1733
1734 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1735 mmc_hostname(host->mmc), host);
1736 if (ret)
1737 return ret;
1738
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001739 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001740 mmiowb();
1741
1742 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001743 sdhci_enable_card_detection(host);
1744
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001745 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001746}
1747
1748EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001749
Daniel Drake5f619702010-11-04 22:20:39 +00001750void sdhci_enable_irq_wakeups(struct sdhci_host *host)
1751{
1752 u8 val;
1753 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
1754 val |= SDHCI_WAKE_ON_INT;
1755 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
1756}
1757
1758EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
1759
Pierre Ossmand129bce2006-03-24 03:18:17 -08001760#endif /* CONFIG_PM */
1761
1762/*****************************************************************************\
1763 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001764 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765 * *
1766\*****************************************************************************/
1767
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001768struct sdhci_host *sdhci_alloc_host(struct device *dev,
1769 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001770{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001771 struct mmc_host *mmc;
1772 struct sdhci_host *host;
1773
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001774 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001775
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001776 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001777 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001778 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001779
1780 host = mmc_priv(mmc);
1781 host->mmc = mmc;
1782
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001783 return host;
1784}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001785
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001786EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001788int sdhci_add_host(struct sdhci_host *host)
1789{
1790 struct mmc_host *mmc;
Takashi Iwai8f230f42010-12-08 10:04:30 +01001791 unsigned int caps, ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001792 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001793
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001794 WARN_ON(host == NULL);
1795 if (host == NULL)
1796 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001797
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001798 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001799
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001800 if (debug_quirks)
1801 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001802
Pierre Ossmand96649e2006-06-30 02:22:30 -07001803 sdhci_reset(host, SDHCI_RESET_ALL);
1804
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001805 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001806 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1807 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001808 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001809 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001810 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001811 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001812 }
1813
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001814 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1815 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001817 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001818 host->flags |= SDHCI_USE_SDMA;
1819 else if (!(caps & SDHCI_CAN_DO_SDMA))
1820 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001821 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001822 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001823
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001824 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001825 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001826 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001827 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001828 }
1829
Richard Röjforsa13abc72009-09-22 16:45:30 -07001830 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1831 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001832
1833 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1834 (host->flags & SDHCI_USE_ADMA)) {
1835 DBG("Disabling ADMA as it is marked broken\n");
1836 host->flags &= ~SDHCI_USE_ADMA;
1837 }
1838
Richard Röjforsa13abc72009-09-22 16:45:30 -07001839 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001840 if (host->ops->enable_dma) {
1841 if (host->ops->enable_dma(host)) {
1842 printk(KERN_WARNING "%s: No suitable DMA "
1843 "available. Falling back to PIO.\n",
1844 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001845 host->flags &=
1846 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001847 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001848 }
1849 }
1850
Pierre Ossman2134a922008-06-28 18:28:51 +02001851 if (host->flags & SDHCI_USE_ADMA) {
1852 /*
1853 * We need to allocate descriptors for all sg entries
1854 * (128) and potentially one alignment transfer for
1855 * each of those entries.
1856 */
1857 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1858 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1859 if (!host->adma_desc || !host->align_buffer) {
1860 kfree(host->adma_desc);
1861 kfree(host->align_buffer);
1862 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1863 "buffers. Falling back to standard DMA.\n",
1864 mmc_hostname(mmc));
1865 host->flags &= ~SDHCI_USE_ADMA;
1866 }
1867 }
1868
Pierre Ossman76591502008-07-21 00:32:11 +02001869 /*
1870 * If we use DMA, then it's up to the caller to set the DMA
1871 * mask, but PIO does not need the hw shim so we set a new
1872 * mask here in that case.
1873 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001874 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001875 host->dma_mask = DMA_BIT_MASK(64);
1876 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1877 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001878
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04001879 if (host->version >= SDHCI_SPEC_300)
1880 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
1881 >> SDHCI_CLOCK_BASE_SHIFT;
1882 else
1883 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
1884 >> SDHCI_CLOCK_BASE_SHIFT;
1885
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001887 if (host->max_clk == 0 || host->quirks &
1888 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001889 if (!host->ops->get_max_clock) {
1890 printk(KERN_ERR
1891 "%s: Hardware doesn't specify base clock "
1892 "frequency.\n", mmc_hostname(mmc));
1893 return -ENODEV;
1894 }
1895 host->max_clk = host->ops->get_max_clock(host);
1896 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001897
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001898 host->timeout_clk =
1899 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1900 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001901 if (host->ops->get_timeout_clock) {
1902 host->timeout_clk = host->ops->get_timeout_clock(host);
1903 } else if (!(host->quirks &
1904 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001905 printk(KERN_ERR
1906 "%s: Hardware doesn't specify timeout clock "
1907 "frequency.\n", mmc_hostname(mmc));
1908 return -ENODEV;
1909 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001910 }
1911 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1912 host->timeout_clk *= 1000;
1913
Pierre Ossmand129bce2006-03-24 03:18:17 -08001914 /*
1915 * Set host parameters.
1916 */
1917 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07001918 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001919 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04001920 else if (host->version >= SDHCI_SPEC_300)
1921 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001922 else
Zhangfei Gao03975262010-09-20 15:15:18 -04001923 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05001924
Pierre Ossmand129bce2006-03-24 03:18:17 -08001925 mmc->f_max = host->max_clk;
Andrei Warkentina3c77782011-04-11 16:13:42 -05001926 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001927
Philip Rakity15ec4462010-11-19 16:48:39 -05001928 /*
1929 * A controller may support 8-bit width, but the board itself
1930 * might not have the pins brought out. Boards that support
1931 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
1932 * their platform code before calling sdhci_add_host(), and we
1933 * won't assume 8-bit width for hosts without that CAP.
1934 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001935 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05001936 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001937
Pierre Ossman86a6a872009-02-02 21:13:49 +01001938 if (caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04001939 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001940
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01001941 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1942 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001943 mmc->caps |= MMC_CAP_NEEDS_POLL;
1944
Takashi Iwai8f230f42010-12-08 10:04:30 +01001945 ocr_avail = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001946 if (caps & SDHCI_CAN_VDD_330)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001947 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001948 if (caps & SDHCI_CAN_VDD_300)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001949 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001950 if (caps & SDHCI_CAN_VDD_180)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001951 ocr_avail |= MMC_VDD_165_195;
1952
1953 mmc->ocr_avail = ocr_avail;
1954 mmc->ocr_avail_sdio = ocr_avail;
1955 if (host->ocr_avail_sdio)
1956 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
1957 mmc->ocr_avail_sd = ocr_avail;
1958 if (host->ocr_avail_sd)
1959 mmc->ocr_avail_sd &= host->ocr_avail_sd;
1960 else /* normal SD controllers don't support 1.8V */
1961 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
1962 mmc->ocr_avail_mmc = ocr_avail;
1963 if (host->ocr_avail_mmc)
1964 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07001965
1966 if (mmc->ocr_avail == 0) {
1967 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001968 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001969 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001970 }
1971
Pierre Ossmand129bce2006-03-24 03:18:17 -08001972 spin_lock_init(&host->lock);
1973
1974 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001975 * Maximum number of segments. Depends on if the hardware
1976 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001977 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001978 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001979 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001980 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001981 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001982 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001983 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001984
1985 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001986 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001987 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001988 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001989 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001990
1991 /*
1992 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001993 * of bytes. When doing hardware scatter/gather, each entry cannot
1994 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001995 */
Olof Johansson30652aa2011-01-01 18:37:32 -06001996 if (host->flags & SDHCI_USE_ADMA) {
1997 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
1998 mmc->max_seg_size = 65535;
1999 else
2000 mmc->max_seg_size = 65536;
2001 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002002 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002003 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002004
2005 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002006 * Maximum block size. This varies from controller to controller and
2007 * is specified in the capabilities register.
2008 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002009 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2010 mmc->max_blk_size = 2;
2011 } else {
2012 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
2013 SDHCI_MAX_BLOCK_SHIFT;
2014 if (mmc->max_blk_size >= 3) {
2015 printk(KERN_WARNING "%s: Invalid maximum block size, "
2016 "assuming 512 bytes\n", mmc_hostname(mmc));
2017 mmc->max_blk_size = 0;
2018 }
2019 }
2020
2021 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002022
2023 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002024 * Maximum block count.
2025 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002026 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002027
2028 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002029 * Init tasklets.
2030 */
2031 tasklet_init(&host->card_tasklet,
2032 sdhci_tasklet_card, (unsigned long)host);
2033 tasklet_init(&host->finish_tasklet,
2034 sdhci_tasklet_finish, (unsigned long)host);
2035
Al Viroe4cad1b2006-10-10 22:47:07 +01002036 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002037
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002038 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002039 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002040 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002041 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002042
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002043 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2044 if (IS_ERR(host->vmmc)) {
2045 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2046 host->vmmc = NULL;
2047 } else {
2048 regulator_enable(host->vmmc);
2049 }
2050
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002051 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002052
2053#ifdef CONFIG_MMC_DEBUG
2054 sdhci_dumpregs(host);
2055#endif
2056
Pierre Ossmanf9134312008-12-21 17:01:48 +01002057#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002058 snprintf(host->led_name, sizeof(host->led_name),
2059 "%s::", mmc_hostname(mmc));
2060 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002061 host->led.brightness = LED_OFF;
2062 host->led.default_trigger = mmc_hostname(mmc);
2063 host->led.brightness_set = sdhci_led_control;
2064
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002065 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002066 if (ret)
2067 goto reset;
2068#endif
2069
Pierre Ossman5f25a662006-10-04 02:15:39 -07002070 mmiowb();
2071
Pierre Ossmand129bce2006-03-24 03:18:17 -08002072 mmc_add_host(mmc);
2073
Richard Röjforsa13abc72009-09-22 16:45:30 -07002074 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002075 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002076 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2077 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002079 sdhci_enable_card_detection(host);
2080
Pierre Ossmand129bce2006-03-24 03:18:17 -08002081 return 0;
2082
Pierre Ossmanf9134312008-12-21 17:01:48 +01002083#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002084reset:
2085 sdhci_reset(host, SDHCI_RESET_ALL);
2086 free_irq(host->irq, host);
2087#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002088untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 tasklet_kill(&host->card_tasklet);
2090 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002091
2092 return ret;
2093}
2094
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002095EXPORT_SYMBOL_GPL(sdhci_add_host);
2096
Pierre Ossman1e728592008-04-16 19:13:13 +02002097void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002098{
Pierre Ossman1e728592008-04-16 19:13:13 +02002099 unsigned long flags;
2100
2101 if (dead) {
2102 spin_lock_irqsave(&host->lock, flags);
2103
2104 host->flags |= SDHCI_DEVICE_DEAD;
2105
2106 if (host->mrq) {
2107 printk(KERN_ERR "%s: Controller removed during "
2108 " transfer!\n", mmc_hostname(host->mmc));
2109
2110 host->mrq->cmd->error = -ENOMEDIUM;
2111 tasklet_schedule(&host->finish_tasklet);
2112 }
2113
2114 spin_unlock_irqrestore(&host->lock, flags);
2115 }
2116
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002117 sdhci_disable_card_detection(host);
2118
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002119 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002120
Pierre Ossmanf9134312008-12-21 17:01:48 +01002121#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002122 led_classdev_unregister(&host->led);
2123#endif
2124
Pierre Ossman1e728592008-04-16 19:13:13 +02002125 if (!dead)
2126 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002127
2128 free_irq(host->irq, host);
2129
2130 del_timer_sync(&host->timer);
2131
2132 tasklet_kill(&host->card_tasklet);
2133 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002134
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002135 if (host->vmmc) {
2136 regulator_disable(host->vmmc);
2137 regulator_put(host->vmmc);
2138 }
2139
Pierre Ossman2134a922008-06-28 18:28:51 +02002140 kfree(host->adma_desc);
2141 kfree(host->align_buffer);
2142
2143 host->adma_desc = NULL;
2144 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145}
2146
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002147EXPORT_SYMBOL_GPL(sdhci_remove_host);
2148
2149void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002150{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002151 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002152}
2153
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002154EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155
2156/*****************************************************************************\
2157 * *
2158 * Driver init/exit *
2159 * *
2160\*****************************************************************************/
2161
2162static int __init sdhci_drv_init(void)
2163{
2164 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002165 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2167
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002168 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169}
2170
2171static void __exit sdhci_drv_exit(void)
2172{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173}
2174
2175module_init(sdhci_drv_init);
2176module_exit(sdhci_drv_exit);
2177
Pierre Ossmandf673b22006-06-30 02:22:31 -07002178module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002179
Pierre Ossman32710e82009-04-08 20:14:54 +02002180MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002181MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002182MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002183
Pierre Ossmandf673b22006-06-30 02:22:31 -07002184MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");