blob: 5d20661bc357ba387601b6b2089185bceb144aac [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_prepare_data(struct sdhci_host *, struct mmc_data *);
44static void sdhci_finish_data(struct sdhci_host *);
45
46static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
47static void sdhci_finish_command(struct sdhci_host *);
48
49static void sdhci_dumpregs(struct sdhci_host *host)
50{
Philip Rakity412ab652010-09-22 15:25:13 -070051 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
52 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080053
54 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030055 sdhci_readl(host, SDHCI_DMA_ADDRESS),
56 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080057 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030058 sdhci_readw(host, SDHCI_BLOCK_SIZE),
59 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080060 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030061 sdhci_readl(host, SDHCI_ARGUMENT),
62 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080063 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030064 sdhci_readl(host, SDHCI_PRESENT_STATE),
65 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080066 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030067 sdhci_readb(host, SDHCI_POWER_CONTROL),
68 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080069 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030070 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
71 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080072 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030073 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
74 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080075 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030076 sdhci_readl(host, SDHCI_INT_ENABLE),
77 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080078 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030079 sdhci_readw(host, SDHCI_ACMD12_ERR),
80 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050081 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030082 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050083 sdhci_readl(host, SDHCI_CAPABILITIES_1));
84 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
85 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_MAX_CURRENT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080087
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010088 if (host->flags & SDHCI_USE_ADMA)
89 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
90 readl(host->ioaddr + SDHCI_ADMA_ERROR),
91 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
92
Pierre Ossmand129bce2006-03-24 03:18:17 -080093 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
94}
95
96/*****************************************************************************\
97 * *
98 * Low level functions *
99 * *
100\*****************************************************************************/
101
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300102static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
103{
104 u32 ier;
105
106 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
107 ier &= ~clear;
108 ier |= set;
109 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
110 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
111}
112
113static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
114{
115 sdhci_clear_set_irqs(host, 0, irqs);
116}
117
118static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
119{
120 sdhci_clear_set_irqs(host, irqs, 0);
121}
122
123static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
124{
125 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
126
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300127 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
128 return;
129
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300130 if (enable)
131 sdhci_unmask_irqs(host, irqs);
132 else
133 sdhci_mask_irqs(host, irqs);
134}
135
136static void sdhci_enable_card_detection(struct sdhci_host *host)
137{
138 sdhci_set_card_detection(host, true);
139}
140
141static void sdhci_disable_card_detection(struct sdhci_host *host)
142{
143 sdhci_set_card_detection(host, false);
144}
145
Pierre Ossmand129bce2006-03-24 03:18:17 -0800146static void sdhci_reset(struct sdhci_host *host, u8 mask)
147{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700148 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300149 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700150
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100151 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300152 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700153 SDHCI_CARD_PRESENT))
154 return;
155 }
156
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300157 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
158 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
159
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300160 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800161
Pierre Ossmane16514d82006-06-30 02:22:24 -0700162 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800163 host->clock = 0;
164
Pierre Ossmane16514d82006-06-30 02:22:24 -0700165 /* Wait max 100 ms */
166 timeout = 100;
167
168 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300169 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700170 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100171 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700172 mmc_hostname(host->mmc), (int)mask);
173 sdhci_dumpregs(host);
174 return;
175 }
176 timeout--;
177 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300179
180 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
181 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182}
183
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800184static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
185
186static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800188 if (soft)
189 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
190 else
191 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300193 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
194 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700195 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
196 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300197 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800198
199 if (soft) {
200 /* force clock reconfiguration */
201 host->clock = 0;
202 sdhci_set_ios(host->mmc, &host->mmc->ios);
203 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300204}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800205
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300206static void sdhci_reinit(struct sdhci_host *host)
207{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800208 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300209 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210}
211
212static void sdhci_activate_led(struct sdhci_host *host)
213{
214 u8 ctrl;
215
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300216 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300218 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219}
220
221static void sdhci_deactivate_led(struct sdhci_host *host)
222{
223 u8 ctrl;
224
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300225 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300227 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228}
229
Pierre Ossmanf9134312008-12-21 17:01:48 +0100230#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100231static void sdhci_led_control(struct led_classdev *led,
232 enum led_brightness brightness)
233{
234 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
235 unsigned long flags;
236
237 spin_lock_irqsave(&host->lock, flags);
238
239 if (brightness == LED_OFF)
240 sdhci_deactivate_led(host);
241 else
242 sdhci_activate_led(host);
243
244 spin_unlock_irqrestore(&host->lock, flags);
245}
246#endif
247
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248/*****************************************************************************\
249 * *
250 * Core functions *
251 * *
252\*****************************************************************************/
253
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100254static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255{
Pierre Ossman76591502008-07-21 00:32:11 +0200256 unsigned long flags;
257 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700258 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200259 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100261 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100263 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200264 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265
Pierre Ossman76591502008-07-21 00:32:11 +0200266 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100268 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200269 if (!sg_miter_next(&host->sg_miter))
270 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271
Pierre Ossman76591502008-07-21 00:32:11 +0200272 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273
Pierre Ossman76591502008-07-21 00:32:11 +0200274 blksize -= len;
275 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200276
Pierre Ossman76591502008-07-21 00:32:11 +0200277 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278
Pierre Ossman76591502008-07-21 00:32:11 +0200279 while (len) {
280 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200282 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283 }
Pierre Ossman76591502008-07-21 00:32:11 +0200284
285 *buf = scratch & 0xFF;
286
287 buf++;
288 scratch >>= 8;
289 chunk--;
290 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800291 }
292 }
Pierre Ossman76591502008-07-21 00:32:11 +0200293
294 sg_miter_stop(&host->sg_miter);
295
296 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100297}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800298
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299static void sdhci_write_block_pio(struct sdhci_host *host)
300{
Pierre Ossman76591502008-07-21 00:32:11 +0200301 unsigned long flags;
302 size_t blksize, len, chunk;
303 u32 scratch;
304 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100305
306 DBG("PIO writing\n");
307
308 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200309 chunk = 0;
310 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311
Pierre Ossman76591502008-07-21 00:32:11 +0200312 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313
314 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200315 if (!sg_miter_next(&host->sg_miter))
316 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317
Pierre Ossman76591502008-07-21 00:32:11 +0200318 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200319
Pierre Ossman76591502008-07-21 00:32:11 +0200320 blksize -= len;
321 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100322
Pierre Ossman76591502008-07-21 00:32:11 +0200323 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 while (len) {
326 scratch |= (u32)*buf << (chunk * 8);
327
328 buf++;
329 chunk++;
330 len--;
331
332 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300333 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200334 chunk = 0;
335 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100336 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100337 }
338 }
Pierre Ossman76591502008-07-21 00:32:11 +0200339
340 sg_miter_stop(&host->sg_miter);
341
342 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100343}
344
345static void sdhci_transfer_pio(struct sdhci_host *host)
346{
347 u32 mask;
348
349 BUG_ON(!host->data);
350
Pierre Ossman76591502008-07-21 00:32:11 +0200351 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352 return;
353
354 if (host->data->flags & MMC_DATA_READ)
355 mask = SDHCI_DATA_AVAILABLE;
356 else
357 mask = SDHCI_SPACE_AVAILABLE;
358
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200359 /*
360 * Some controllers (JMicron JMB38x) mess up the buffer bits
361 * for transfers < 4 bytes. As long as it is just one block,
362 * we can ignore the bits.
363 */
364 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
365 (host->data->blocks == 1))
366 mask = ~0;
367
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300368 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300369 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
370 udelay(100);
371
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 if (host->data->flags & MMC_DATA_READ)
373 sdhci_read_block_pio(host);
374 else
375 sdhci_write_block_pio(host);
376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 host->blocks--;
378 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380 }
381
382 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800383}
384
Pierre Ossman2134a922008-06-28 18:28:51 +0200385static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
386{
387 local_irq_save(*flags);
388 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
389}
390
391static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
392{
393 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
394 local_irq_restore(*flags);
395}
396
Ben Dooks118cd172010-03-05 13:43:26 -0800397static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
398{
Ben Dooks9e506f32010-03-05 13:43:29 -0800399 __le32 *dataddr = (__le32 __force *)(desc + 4);
400 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800401
Ben Dooks9e506f32010-03-05 13:43:29 -0800402 /* SDHCI specification says ADMA descriptors should be 4 byte
403 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800404
Ben Dooks9e506f32010-03-05 13:43:29 -0800405 cmdlen[0] = cpu_to_le16(cmd);
406 cmdlen[1] = cpu_to_le16(len);
407
408 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800409}
410
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200411static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200412 struct mmc_data *data)
413{
414 int direction;
415
416 u8 *desc;
417 u8 *align;
418 dma_addr_t addr;
419 dma_addr_t align_addr;
420 int len, offset;
421
422 struct scatterlist *sg;
423 int i;
424 char *buffer;
425 unsigned long flags;
426
427 /*
428 * The spec does not specify endianness of descriptor table.
429 * We currently guess that it is LE.
430 */
431
432 if (data->flags & MMC_DATA_READ)
433 direction = DMA_FROM_DEVICE;
434 else
435 direction = DMA_TO_DEVICE;
436
437 /*
438 * The ADMA descriptor table is mapped further down as we
439 * need to fill it with data first.
440 */
441
442 host->align_addr = dma_map_single(mmc_dev(host->mmc),
443 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700444 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200445 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200446 BUG_ON(host->align_addr & 0x3);
447
448 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
449 data->sg, data->sg_len, direction);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200450 if (host->sg_count == 0)
451 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200452
453 desc = host->adma_desc;
454 align = host->align_buffer;
455
456 align_addr = host->align_addr;
457
458 for_each_sg(data->sg, sg, host->sg_count, i) {
459 addr = sg_dma_address(sg);
460 len = sg_dma_len(sg);
461
462 /*
463 * The SDHCI specification states that ADMA
464 * addresses must be 32-bit aligned. If they
465 * aren't, then we use a bounce buffer for
466 * the (up to three) bytes that screw up the
467 * alignment.
468 */
469 offset = (4 - (addr & 0x3)) & 0x3;
470 if (offset) {
471 if (data->flags & MMC_DATA_WRITE) {
472 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200473 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200474 memcpy(align, buffer, offset);
475 sdhci_kunmap_atomic(buffer, &flags);
476 }
477
Ben Dooks118cd172010-03-05 13:43:26 -0800478 /* tran, valid */
479 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200480
481 BUG_ON(offset > 65536);
482
Pierre Ossman2134a922008-06-28 18:28:51 +0200483 align += 4;
484 align_addr += 4;
485
486 desc += 8;
487
488 addr += offset;
489 len -= offset;
490 }
491
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 BUG_ON(len > 65536);
493
Ben Dooks118cd172010-03-05 13:43:26 -0800494 /* tran, valid */
495 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200496 desc += 8;
497
498 /*
499 * If this triggers then we have a calculation bug
500 * somewhere. :/
501 */
502 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
503 }
504
Thomas Abraham70764a92010-05-26 14:42:04 -0700505 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
506 /*
507 * Mark the last descriptor as the terminating descriptor
508 */
509 if (desc != host->adma_desc) {
510 desc -= 8;
511 desc[0] |= 0x2; /* end */
512 }
513 } else {
514 /*
515 * Add a terminating entry.
516 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200517
Thomas Abraham70764a92010-05-26 14:42:04 -0700518 /* nop, end, valid */
519 sdhci_set_adma_desc(desc, 0, 0, 0x3);
520 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200521
522 /*
523 * Resync align buffer as we might have changed it.
524 */
525 if (data->flags & MMC_DATA_WRITE) {
526 dma_sync_single_for_device(mmc_dev(host->mmc),
527 host->align_addr, 128 * 4, direction);
528 }
529
530 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
531 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200532 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200533 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200534 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200535
536 return 0;
537
538unmap_entries:
539 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
540 data->sg_len, direction);
541unmap_align:
542 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
543 128 * 4, direction);
544fail:
545 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200546}
547
548static void sdhci_adma_table_post(struct sdhci_host *host,
549 struct mmc_data *data)
550{
551 int direction;
552
553 struct scatterlist *sg;
554 int i, size;
555 u8 *align;
556 char *buffer;
557 unsigned long flags;
558
559 if (data->flags & MMC_DATA_READ)
560 direction = DMA_FROM_DEVICE;
561 else
562 direction = DMA_TO_DEVICE;
563
564 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
565 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
566
567 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
568 128 * 4, direction);
569
570 if (data->flags & MMC_DATA_READ) {
571 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
572 data->sg_len, direction);
573
574 align = host->align_buffer;
575
576 for_each_sg(data->sg, sg, host->sg_count, i) {
577 if (sg_dma_address(sg) & 0x3) {
578 size = 4 - (sg_dma_address(sg) & 0x3);
579
580 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200581 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200582 memcpy(buffer, align, size);
583 sdhci_kunmap_atomic(buffer, &flags);
584
585 align += 4;
586 }
587 }
588 }
589
590 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
591 data->sg_len, direction);
592}
593
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200594static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_data *data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800595{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700596 u8 count;
597 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800598
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200599 /*
600 * If the host controller provides us with an incorrect timeout
601 * value, just skip the check and use 0xE. The hardware may take
602 * longer to time out, but that's much better than having a too-short
603 * timeout value.
604 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200605 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200606 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200607
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700608 /* timeout in us */
609 target_timeout = data->timeout_ns / 1000 +
610 data->timeout_clks / host->clock;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800611
Anton Vorontsov81b39802009-09-22 16:45:13 -0700612 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
613 host->timeout_clk = host->clock / 1000;
614
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700615 /*
616 * Figure out needed cycles.
617 * We do this in steps in order to fit inside a 32 bit int.
618 * The first step is the minimum timeout, which will have a
619 * minimum resolution of 6 bits:
620 * (1) 2^13*1000 > 2^22,
621 * (2) host->timeout_clk < 2^16
622 * =>
623 * (1) / (2) > 2^6
624 */
625 count = 0;
626 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
627 while (current_timeout < target_timeout) {
628 count++;
629 current_timeout <<= 1;
630 if (count >= 0xF)
631 break;
632 }
633
634 if (count >= 0xF) {
635 printk(KERN_WARNING "%s: Too large timeout requested!\n",
636 mmc_hostname(host->mmc));
637 count = 0xE;
638 }
639
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200640 return count;
641}
642
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300643static void sdhci_set_transfer_irqs(struct sdhci_host *host)
644{
645 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
646 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
647
648 if (host->flags & SDHCI_REQ_USE_DMA)
649 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
650 else
651 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
652}
653
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200654static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_data *data)
655{
656 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200657 u8 ctrl;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200658 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200659
660 WARN_ON(host->data);
661
662 if (data == NULL)
663 return;
664
665 /* Sanity checks */
666 BUG_ON(data->blksz * data->blocks > 524288);
667 BUG_ON(data->blksz > host->mmc->max_blk_size);
668 BUG_ON(data->blocks > 65535);
669
670 host->data = data;
671 host->data_early = 0;
672
673 count = sdhci_calc_timeout(host, data);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300674 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800675
Richard Röjforsa13abc72009-09-22 16:45:30 -0700676 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100677 host->flags |= SDHCI_REQ_USE_DMA;
678
Pierre Ossman2134a922008-06-28 18:28:51 +0200679 /*
680 * FIXME: This doesn't account for merging when mapping the
681 * scatterlist.
682 */
683 if (host->flags & SDHCI_REQ_USE_DMA) {
684 int broken, i;
685 struct scatterlist *sg;
686
687 broken = 0;
688 if (host->flags & SDHCI_USE_ADMA) {
689 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
690 broken = 1;
691 } else {
692 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
693 broken = 1;
694 }
695
696 if (unlikely(broken)) {
697 for_each_sg(data->sg, sg, data->sg_len, i) {
698 if (sg->length & 0x3) {
699 DBG("Reverting to PIO because of "
700 "transfer size (%d)\n",
701 sg->length);
702 host->flags &= ~SDHCI_REQ_USE_DMA;
703 break;
704 }
705 }
706 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100707 }
708
709 /*
710 * The assumption here being that alignment is the same after
711 * translation to device address space.
712 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200713 if (host->flags & SDHCI_REQ_USE_DMA) {
714 int broken, i;
715 struct scatterlist *sg;
716
717 broken = 0;
718 if (host->flags & SDHCI_USE_ADMA) {
719 /*
720 * As we use 3 byte chunks to work around
721 * alignment problems, we need to check this
722 * quirk.
723 */
724 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
725 broken = 1;
726 } else {
727 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
728 broken = 1;
729 }
730
731 if (unlikely(broken)) {
732 for_each_sg(data->sg, sg, data->sg_len, i) {
733 if (sg->offset & 0x3) {
734 DBG("Reverting to PIO because of "
735 "bad alignment\n");
736 host->flags &= ~SDHCI_REQ_USE_DMA;
737 break;
738 }
739 }
740 }
741 }
742
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200743 if (host->flags & SDHCI_REQ_USE_DMA) {
744 if (host->flags & SDHCI_USE_ADMA) {
745 ret = sdhci_adma_table_pre(host, data);
746 if (ret) {
747 /*
748 * This only happens when someone fed
749 * us an invalid request.
750 */
751 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200752 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200753 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300754 sdhci_writel(host, host->adma_addr,
755 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200756 }
757 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300758 int sg_cnt;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200759
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300760 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200761 data->sg, data->sg_len,
762 (data->flags & MMC_DATA_READ) ?
763 DMA_FROM_DEVICE :
764 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300765 if (sg_cnt == 0) {
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200766 /*
767 * This only happens when someone fed
768 * us an invalid request.
769 */
770 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200771 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200772 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200773 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300774 sdhci_writel(host, sg_dma_address(data->sg),
775 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200776 }
777 }
778 }
779
Pierre Ossman2134a922008-06-28 18:28:51 +0200780 /*
781 * Always adjust the DMA selection as some controllers
782 * (e.g. JMicron) can't do PIO properly when the selection
783 * is ADMA.
784 */
785 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300786 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200787 ctrl &= ~SDHCI_CTRL_DMA_MASK;
788 if ((host->flags & SDHCI_REQ_USE_DMA) &&
789 (host->flags & SDHCI_USE_ADMA))
790 ctrl |= SDHCI_CTRL_ADMA32;
791 else
792 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300793 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100794 }
795
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200796 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200797 int flags;
798
799 flags = SG_MITER_ATOMIC;
800 if (host->data->flags & MMC_DATA_READ)
801 flags |= SG_MITER_TO_SG;
802 else
803 flags |= SG_MITER_FROM_SG;
804 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200805 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800806 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700807
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300808 sdhci_set_transfer_irqs(host);
809
Pierre Ossmanbab76962006-07-02 16:51:35 +0100810 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300811 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, data->blksz), SDHCI_BLOCK_SIZE);
812 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700813}
814
815static void sdhci_set_transfer_mode(struct sdhci_host *host,
816 struct mmc_data *data)
817{
818 u16 mode;
819
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700820 if (data == NULL)
821 return;
822
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200823 WARN_ON(!host->data);
824
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700825 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700826 if (data->blocks > 1) {
827 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
828 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
829 else
830 mode |= SDHCI_TRNS_MULTI;
831 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700832 if (data->flags & MMC_DATA_READ)
833 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100834 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700835 mode |= SDHCI_TRNS_DMA;
836
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300837 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800838}
839
840static void sdhci_finish_data(struct sdhci_host *host)
841{
842 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800843
844 BUG_ON(!host->data);
845
846 data = host->data;
847 host->data = NULL;
848
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100849 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200850 if (host->flags & SDHCI_USE_ADMA)
851 sdhci_adma_table_post(host, data);
852 else {
853 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
854 data->sg_len, (data->flags & MMC_DATA_READ) ?
855 DMA_FROM_DEVICE : DMA_TO_DEVICE);
856 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800857 }
858
859 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200860 * The specification states that the block count register must
861 * be updated, but it does not specify at what point in the
862 * data flow. That makes the register entirely useless to read
863 * back so we have to assume that nothing made it to the card
864 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800865 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200866 if (data->error)
867 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800868 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200869 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800870
Pierre Ossmand129bce2006-03-24 03:18:17 -0800871 if (data->stop) {
872 /*
873 * The controller needs a reset of internal state machines
874 * upon error conditions.
875 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200876 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800877 sdhci_reset(host, SDHCI_RESET_CMD);
878 sdhci_reset(host, SDHCI_RESET_DATA);
879 }
880
881 sdhci_send_command(host, data->stop);
882 } else
883 tasklet_schedule(&host->finish_tasklet);
884}
885
886static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
887{
888 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700889 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700890 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800891
892 WARN_ON(host->cmd);
893
Pierre Ossmand129bce2006-03-24 03:18:17 -0800894 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700895 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700896
897 mask = SDHCI_CMD_INHIBIT;
898 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
899 mask |= SDHCI_DATA_INHIBIT;
900
901 /* We shouldn't wait for data inihibit for stop commands, even
902 though they might use busy signaling */
903 if (host->mrq->data && (cmd == host->mrq->data->stop))
904 mask &= ~SDHCI_DATA_INHIBIT;
905
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300906 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700907 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800908 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100909 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800910 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200911 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912 tasklet_schedule(&host->finish_tasklet);
913 return;
914 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700915 timeout--;
916 mdelay(1);
917 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800918
919 mod_timer(&host->timer, jiffies + 10 * HZ);
920
921 host->cmd = cmd;
922
923 sdhci_prepare_data(host, cmd->data);
924
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300925 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800926
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700927 sdhci_set_transfer_mode(host, cmd->data);
928
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100930 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200932 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933 tasklet_schedule(&host->finish_tasklet);
934 return;
935 }
936
937 if (!(cmd->flags & MMC_RSP_PRESENT))
938 flags = SDHCI_CMD_RESP_NONE;
939 else if (cmd->flags & MMC_RSP_136)
940 flags = SDHCI_CMD_RESP_LONG;
941 else if (cmd->flags & MMC_RSP_BUSY)
942 flags = SDHCI_CMD_RESP_SHORT_BUSY;
943 else
944 flags = SDHCI_CMD_RESP_SHORT;
945
946 if (cmd->flags & MMC_RSP_CRC)
947 flags |= SDHCI_CMD_CRC;
948 if (cmd->flags & MMC_RSP_OPCODE)
949 flags |= SDHCI_CMD_INDEX;
950 if (cmd->data)
951 flags |= SDHCI_CMD_DATA;
952
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300953 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954}
955
956static void sdhci_finish_command(struct sdhci_host *host)
957{
958 int i;
959
960 BUG_ON(host->cmd == NULL);
961
962 if (host->cmd->flags & MMC_RSP_PRESENT) {
963 if (host->cmd->flags & MMC_RSP_136) {
964 /* CRC is stripped so we need to do some shifting. */
965 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300966 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 SDHCI_RESPONSE + (3-i)*4) << 8;
968 if (i != 3)
969 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300970 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971 SDHCI_RESPONSE + (3-i)*4-1);
972 }
973 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300974 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975 }
976 }
977
Pierre Ossman17b04292007-07-22 22:18:46 +0200978 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200980 if (host->data && host->data_early)
981 sdhci_finish_data(host);
982
983 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984 tasklet_schedule(&host->finish_tasklet);
985
986 host->cmd = NULL;
987}
988
989static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
990{
991 int div;
992 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700993 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994
995 if (clock == host->clock)
996 return;
997
Anton Vorontsov81146342009-03-17 00:13:59 +0300998 if (host->ops->set_clock) {
999 host->ops->set_clock(host, clock);
1000 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1001 return;
1002 }
1003
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001004 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005
1006 if (clock == 0)
1007 goto out;
1008
Zhangfei Gao85105c52010-08-06 07:10:01 +08001009 if (host->version >= SDHCI_SPEC_300) {
1010 /* Version 3.00 divisors must be a multiple of 2. */
1011 if (host->max_clk <= clock)
1012 div = 1;
1013 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001014 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001015 if ((host->max_clk / div) <= clock)
1016 break;
1017 }
1018 }
1019 } else {
1020 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001021 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001022 if ((host->max_clk / div) <= clock)
1023 break;
1024 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025 }
1026 div >>= 1;
1027
Zhangfei Gao85105c52010-08-06 07:10:01 +08001028 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1029 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1030 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001032 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033
Chris Ball27f6cb12009-09-22 16:45:31 -07001034 /* Wait max 20 ms */
1035 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001036 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001037 & SDHCI_CLOCK_INT_STABLE)) {
1038 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001039 printk(KERN_ERR "%s: Internal clock never "
1040 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001041 sdhci_dumpregs(host);
1042 return;
1043 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001044 timeout--;
1045 mdelay(1);
1046 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001047
1048 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001049 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050
1051out:
1052 host->clock = clock;
1053}
1054
Pierre Ossman146ad662006-06-30 02:22:23 -07001055static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1056{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001057 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001058
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001059 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001060 switch (1 << power) {
1061 case MMC_VDD_165_195:
1062 pwr = SDHCI_POWER_180;
1063 break;
1064 case MMC_VDD_29_30:
1065 case MMC_VDD_30_31:
1066 pwr = SDHCI_POWER_300;
1067 break;
1068 case MMC_VDD_32_33:
1069 case MMC_VDD_33_34:
1070 pwr = SDHCI_POWER_330;
1071 break;
1072 default:
1073 BUG();
1074 }
1075 }
1076
1077 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001078 return;
1079
Pierre Ossmanae628902009-05-03 20:45:03 +02001080 host->pwr = pwr;
1081
1082 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001083 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001084 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001085 }
1086
1087 /*
1088 * Spec says that we should clear the power reg before setting
1089 * a new value. Some controllers don't seem to like this though.
1090 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001091 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001092 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001093
Andres Salomone08c1692008-07-04 10:00:03 -07001094 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001095 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001096 * and set turn on power at the same time, so set the voltage first.
1097 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001098 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001099 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1100
1101 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001102
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001103 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001104
1105 /*
1106 * Some controllers need an extra 10ms delay of 10ms before they
1107 * can apply clock after applying power
1108 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001109 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001110 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001111}
1112
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113/*****************************************************************************\
1114 * *
1115 * MMC callbacks *
1116 * *
1117\*****************************************************************************/
1118
1119static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1120{
1121 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001122 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123 unsigned long flags;
1124
1125 host = mmc_priv(mmc);
1126
1127 spin_lock_irqsave(&host->lock, flags);
1128
1129 WARN_ON(host->mrq != NULL);
1130
Pierre Ossmanf9134312008-12-21 17:01:48 +01001131#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001133#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001134 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1135 if (mrq->stop) {
1136 mrq->data->stop = NULL;
1137 mrq->stop = NULL;
1138 }
1139 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
1141 host->mrq = mrq;
1142
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001143 /* If polling, assume that the card is always present. */
1144 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1145 present = true;
1146 else
1147 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1148 SDHCI_CARD_PRESENT;
1149
1150 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001151 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001152 tasklet_schedule(&host->finish_tasklet);
1153 } else
1154 sdhci_send_command(host, mrq->cmd);
1155
Pierre Ossman5f25a662006-10-04 02:15:39 -07001156 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157 spin_unlock_irqrestore(&host->lock, flags);
1158}
1159
1160static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1161{
1162 struct sdhci_host *host;
1163 unsigned long flags;
1164 u8 ctrl;
1165
1166 host = mmc_priv(mmc);
1167
1168 spin_lock_irqsave(&host->lock, flags);
1169
Pierre Ossman1e728592008-04-16 19:13:13 +02001170 if (host->flags & SDHCI_DEVICE_DEAD)
1171 goto out;
1172
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 /*
1174 * Reset the chip on each power off.
1175 * Should clear out any weird states.
1176 */
1177 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001178 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001179 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180 }
1181
1182 sdhci_set_clock(host, ios->clock);
1183
1184 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001185 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001187 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001188
Philip Rakity643a81f2010-09-23 08:24:32 -07001189 if (host->ops->platform_send_init_74_clocks)
1190 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1191
Philip Rakity15ec4462010-11-19 16:48:39 -05001192 /*
1193 * If your platform has 8-bit width support but is not a v3 controller,
1194 * or if it requires special setup code, you should implement that in
1195 * platform_8bit_width().
1196 */
1197 if (host->ops->platform_8bit_width)
1198 host->ops->platform_8bit_width(host, ios->bus_width);
1199 else {
1200 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1201 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1202 ctrl &= ~SDHCI_CTRL_4BITBUS;
1203 if (host->version >= SDHCI_SPEC_300)
1204 ctrl |= SDHCI_CTRL_8BITBUS;
1205 } else {
1206 if (host->version >= SDHCI_SPEC_300)
1207 ctrl &= ~SDHCI_CTRL_8BITBUS;
1208 if (ios->bus_width == MMC_BUS_WIDTH_4)
1209 ctrl |= SDHCI_CTRL_4BITBUS;
1210 else
1211 ctrl &= ~SDHCI_CTRL_4BITBUS;
1212 }
1213 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1214 }
1215
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001216 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001217
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001218 if ((ios->timing == MMC_TIMING_SD_HS ||
1219 ios->timing == MMC_TIMING_MMC_HS)
1220 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001221 ctrl |= SDHCI_CTRL_HISPD;
1222 else
1223 ctrl &= ~SDHCI_CTRL_HISPD;
1224
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001226
Leandro Dorileob8352262007-07-25 23:47:04 +02001227 /*
1228 * Some (ENE) controllers go apeshit on some ios operation,
1229 * signalling timeout and CRC errors even on CMD0. Resetting
1230 * it on each ios seems to solve the problem.
1231 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001232 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001233 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1234
Pierre Ossman1e728592008-04-16 19:13:13 +02001235out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001236 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237 spin_unlock_irqrestore(&host->lock, flags);
1238}
1239
1240static int sdhci_get_ro(struct mmc_host *mmc)
1241{
1242 struct sdhci_host *host;
1243 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001244 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245
1246 host = mmc_priv(mmc);
1247
1248 spin_lock_irqsave(&host->lock, flags);
1249
Pierre Ossman1e728592008-04-16 19:13:13 +02001250 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001251 is_readonly = 0;
1252 else if (host->ops->get_ro)
1253 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001254 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001255 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1256 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001257
1258 spin_unlock_irqrestore(&host->lock, flags);
1259
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001260 /* This quirk needs to be replaced by a callback-function later */
1261 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1262 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263}
1264
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001265static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1266{
1267 struct sdhci_host *host;
1268 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001269
1270 host = mmc_priv(mmc);
1271
1272 spin_lock_irqsave(&host->lock, flags);
1273
Pierre Ossman1e728592008-04-16 19:13:13 +02001274 if (host->flags & SDHCI_DEVICE_DEAD)
1275 goto out;
1276
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001277 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001278 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1279 else
1280 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001281out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001282 mmiowb();
1283
1284 spin_unlock_irqrestore(&host->lock, flags);
1285}
1286
David Brownellab7aefd2006-11-12 17:55:30 -08001287static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001288 .request = sdhci_request,
1289 .set_ios = sdhci_set_ios,
1290 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001291 .enable_sdio_irq = sdhci_enable_sdio_irq,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001292};
1293
1294/*****************************************************************************\
1295 * *
1296 * Tasklets *
1297 * *
1298\*****************************************************************************/
1299
1300static void sdhci_tasklet_card(unsigned long param)
1301{
1302 struct sdhci_host *host;
1303 unsigned long flags;
1304
1305 host = (struct sdhci_host*)param;
1306
1307 spin_lock_irqsave(&host->lock, flags);
1308
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001309 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310 if (host->mrq) {
1311 printk(KERN_ERR "%s: Card removed during transfer!\n",
1312 mmc_hostname(host->mmc));
1313 printk(KERN_ERR "%s: Resetting controller.\n",
1314 mmc_hostname(host->mmc));
1315
1316 sdhci_reset(host, SDHCI_RESET_CMD);
1317 sdhci_reset(host, SDHCI_RESET_DATA);
1318
Pierre Ossman17b04292007-07-22 22:18:46 +02001319 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 tasklet_schedule(&host->finish_tasklet);
1321 }
1322 }
1323
1324 spin_unlock_irqrestore(&host->lock, flags);
1325
Pierre Ossman04cf5852008-08-18 22:18:14 +02001326 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001327}
1328
1329static void sdhci_tasklet_finish(unsigned long param)
1330{
1331 struct sdhci_host *host;
1332 unsigned long flags;
1333 struct mmc_request *mrq;
1334
1335 host = (struct sdhci_host*)param;
1336
Chris Ball0c9c99a2011-04-27 17:35:31 -04001337 /*
1338 * If this tasklet gets rescheduled while running, it will
1339 * be run again afterwards but without any active request.
1340 */
1341 if (!host->mrq)
1342 return;
1343
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 spin_lock_irqsave(&host->lock, flags);
1345
1346 del_timer(&host->timer);
1347
1348 mrq = host->mrq;
1349
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350 /*
1351 * The controller needs a reset of internal state machines
1352 * upon error conditions.
1353 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001354 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001355 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001356 (mrq->data && (mrq->data->error ||
1357 (mrq->data->stop && mrq->data->stop->error))) ||
1358 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001359
1360 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001361 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001362 unsigned int clock;
1363
1364 /* This is to force an update */
1365 clock = host->clock;
1366 host->clock = 0;
1367 sdhci_set_clock(host, clock);
1368 }
1369
1370 /* Spec says we should do both at the same time, but Ricoh
1371 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001372 sdhci_reset(host, SDHCI_RESET_CMD);
1373 sdhci_reset(host, SDHCI_RESET_DATA);
1374 }
1375
1376 host->mrq = NULL;
1377 host->cmd = NULL;
1378 host->data = NULL;
1379
Pierre Ossmanf9134312008-12-21 17:01:48 +01001380#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001382#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383
Pierre Ossman5f25a662006-10-04 02:15:39 -07001384 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001385 spin_unlock_irqrestore(&host->lock, flags);
1386
1387 mmc_request_done(host->mmc, mrq);
1388}
1389
1390static void sdhci_timeout_timer(unsigned long data)
1391{
1392 struct sdhci_host *host;
1393 unsigned long flags;
1394
1395 host = (struct sdhci_host*)data;
1396
1397 spin_lock_irqsave(&host->lock, flags);
1398
1399 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001400 printk(KERN_ERR "%s: Timeout waiting for hardware "
1401 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402 sdhci_dumpregs(host);
1403
1404 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001405 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406 sdhci_finish_data(host);
1407 } else {
1408 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001409 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001411 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412
1413 tasklet_schedule(&host->finish_tasklet);
1414 }
1415 }
1416
Pierre Ossman5f25a662006-10-04 02:15:39 -07001417 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001418 spin_unlock_irqrestore(&host->lock, flags);
1419}
1420
1421/*****************************************************************************\
1422 * *
1423 * Interrupt handling *
1424 * *
1425\*****************************************************************************/
1426
1427static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1428{
1429 BUG_ON(intmask == 0);
1430
1431 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001432 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1433 "though no command operation was in progress.\n",
1434 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435 sdhci_dumpregs(host);
1436 return;
1437 }
1438
Pierre Ossman43b58b32007-07-25 23:15:27 +02001439 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001440 host->cmd->error = -ETIMEDOUT;
1441 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1442 SDHCI_INT_INDEX))
1443 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444
Pierre Ossmane8095172008-07-25 01:09:08 +02001445 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001446 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001447 return;
1448 }
1449
1450 /*
1451 * The host can send and interrupt when the busy state has
1452 * ended, allowing us to wait without wasting CPU cycles.
1453 * Unfortunately this is overloaded on the "data complete"
1454 * interrupt, so we need to take some care when handling
1455 * it.
1456 *
1457 * Note: The 1.0 specification is a bit ambiguous about this
1458 * feature so there might be some problems with older
1459 * controllers.
1460 */
1461 if (host->cmd->flags & MMC_RSP_BUSY) {
1462 if (host->cmd->data)
1463 DBG("Cannot wait for busy signal when also "
1464 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001465 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001466 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001467
1468 /* The controller does not support the end-of-busy IRQ,
1469 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001470 }
1471
1472 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001473 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001474}
1475
George G. Davis0957c332010-02-18 12:32:12 -05001476#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001477static void sdhci_show_adma_error(struct sdhci_host *host)
1478{
1479 const char *name = mmc_hostname(host->mmc);
1480 u8 *desc = host->adma_desc;
1481 __le32 *dma;
1482 __le16 *len;
1483 u8 attr;
1484
1485 sdhci_dumpregs(host);
1486
1487 while (true) {
1488 dma = (__le32 *)(desc + 4);
1489 len = (__le16 *)(desc + 2);
1490 attr = *desc;
1491
1492 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1493 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1494
1495 desc += 8;
1496
1497 if (attr & 2)
1498 break;
1499 }
1500}
1501#else
1502static void sdhci_show_adma_error(struct sdhci_host *host) { }
1503#endif
1504
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1506{
1507 BUG_ON(intmask == 0);
1508
1509 if (!host->data) {
1510 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001511 * The "data complete" interrupt is also used to
1512 * indicate that a busy state has ended. See comment
1513 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001514 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001515 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1516 if (intmask & SDHCI_INT_DATA_END) {
1517 sdhci_finish_command(host);
1518 return;
1519 }
1520 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001521
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001522 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1523 "though no data operation was in progress.\n",
1524 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001525 sdhci_dumpregs(host);
1526
1527 return;
1528 }
1529
1530 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001531 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001532 else if (intmask & SDHCI_INT_DATA_END_BIT)
1533 host->data->error = -EILSEQ;
1534 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1535 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1536 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001537 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001538 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1539 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1540 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001541 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001542 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001543
Pierre Ossman17b04292007-07-22 22:18:46 +02001544 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001545 sdhci_finish_data(host);
1546 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001547 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001548 sdhci_transfer_pio(host);
1549
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001550 /*
1551 * We currently don't do anything fancy with DMA
1552 * boundaries, but as we can't disable the feature
1553 * we need to at least restart the transfer.
1554 */
1555 if (intmask & SDHCI_INT_DMA_END)
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001556 sdhci_writel(host, sdhci_readl(host, SDHCI_DMA_ADDRESS),
1557 SDHCI_DMA_ADDRESS);
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001558
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001559 if (intmask & SDHCI_INT_DATA_END) {
1560 if (host->cmd) {
1561 /*
1562 * Data managed to finish before the
1563 * command completed. Make sure we do
1564 * things in the proper order.
1565 */
1566 host->data_early = 1;
1567 } else {
1568 sdhci_finish_data(host);
1569 }
1570 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001571 }
1572}
1573
David Howells7d12e782006-10-05 14:55:46 +01001574static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001575{
1576 irqreturn_t result;
1577 struct sdhci_host* host = dev_id;
1578 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001579 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001580
1581 spin_lock(&host->lock);
1582
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001583 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001584
Mark Lord62df67a52007-03-06 13:30:13 +01001585 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001586 result = IRQ_NONE;
1587 goto out;
1588 }
1589
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001590 DBG("*** %s got interrupt: 0x%08x\n",
1591 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001592
Pierre Ossman3192a282006-06-30 02:22:26 -07001593 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001594 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1595 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001596 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001597 }
1598
1599 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600
1601 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001602 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1603 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001604 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001605 }
1606
1607 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001608 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1609 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001610 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611 }
1612
1613 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1614
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001615 intmask &= ~SDHCI_INT_ERROR;
1616
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001618 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001620 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621 }
1622
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001623 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001624
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001625 if (intmask & SDHCI_INT_CARD_INT)
1626 cardint = 1;
1627
1628 intmask &= ~SDHCI_INT_CARD_INT;
1629
Pierre Ossman3192a282006-06-30 02:22:26 -07001630 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001631 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001632 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001633 sdhci_dumpregs(host);
1634
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001635 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001636 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637
1638 result = IRQ_HANDLED;
1639
Pierre Ossman5f25a662006-10-04 02:15:39 -07001640 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641out:
1642 spin_unlock(&host->lock);
1643
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001644 /*
1645 * We have to delay this as it calls back into the driver.
1646 */
1647 if (cardint)
1648 mmc_signal_sdio_irq(host->mmc);
1649
Pierre Ossmand129bce2006-03-24 03:18:17 -08001650 return result;
1651}
1652
1653/*****************************************************************************\
1654 * *
1655 * Suspend/resume *
1656 * *
1657\*****************************************************************************/
1658
1659#ifdef CONFIG_PM
1660
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001661int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001663 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001665 sdhci_disable_card_detection(host);
1666
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001667 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001668 if (ret)
1669 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001671 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001672
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001673 if (host->vmmc)
1674 ret = regulator_disable(host->vmmc);
1675
1676 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001677}
1678
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001679EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001680
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001681int sdhci_resume_host(struct sdhci_host *host)
1682{
1683 int ret;
1684
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001685 if (host->vmmc) {
1686 int ret = regulator_enable(host->vmmc);
1687 if (ret)
1688 return ret;
1689 }
1690
1691
Richard Röjforsa13abc72009-09-22 16:45:30 -07001692 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001693 if (host->ops->enable_dma)
1694 host->ops->enable_dma(host);
1695 }
1696
1697 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1698 mmc_hostname(host->mmc), host);
1699 if (ret)
1700 return ret;
1701
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001702 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001703 mmiowb();
1704
1705 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001706 sdhci_enable_card_detection(host);
1707
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001708 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001709}
1710
1711EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001712
Daniel Drake5f619702010-11-04 22:20:39 +00001713void sdhci_enable_irq_wakeups(struct sdhci_host *host)
1714{
1715 u8 val;
1716 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
1717 val |= SDHCI_WAKE_ON_INT;
1718 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
1719}
1720
1721EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
1722
Pierre Ossmand129bce2006-03-24 03:18:17 -08001723#endif /* CONFIG_PM */
1724
1725/*****************************************************************************\
1726 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001727 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001728 * *
1729\*****************************************************************************/
1730
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001731struct sdhci_host *sdhci_alloc_host(struct device *dev,
1732 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001733{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001734 struct mmc_host *mmc;
1735 struct sdhci_host *host;
1736
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001737 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001738
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001739 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001740 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001741 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001742
1743 host = mmc_priv(mmc);
1744 host->mmc = mmc;
1745
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001746 return host;
1747}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001748
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001749EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001751int sdhci_add_host(struct sdhci_host *host)
1752{
1753 struct mmc_host *mmc;
Takashi Iwai8f230f42010-12-08 10:04:30 +01001754 unsigned int caps, ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001755 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001756
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001757 WARN_ON(host == NULL);
1758 if (host == NULL)
1759 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001760
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001761 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001762
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001763 if (debug_quirks)
1764 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765
Pierre Ossmand96649e2006-06-30 02:22:30 -07001766 sdhci_reset(host, SDHCI_RESET_ALL);
1767
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001768 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02001769 host->version = (host->version & SDHCI_SPEC_VER_MASK)
1770 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001771 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07001772 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001773 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02001774 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07001775 }
1776
Maxim Levitskyccc92c22010-08-10 18:01:42 -07001777 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
1778 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001779
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001780 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07001781 host->flags |= SDHCI_USE_SDMA;
1782 else if (!(caps & SDHCI_CAN_DO_SDMA))
1783 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07001784 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07001785 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001786
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001787 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07001788 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01001789 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07001790 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02001791 }
1792
Richard Röjforsa13abc72009-09-22 16:45:30 -07001793 if ((host->version >= SDHCI_SPEC_200) && (caps & SDHCI_CAN_DO_ADMA2))
1794 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02001795
1796 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
1797 (host->flags & SDHCI_USE_ADMA)) {
1798 DBG("Disabling ADMA as it is marked broken\n");
1799 host->flags &= ~SDHCI_USE_ADMA;
1800 }
1801
Richard Röjforsa13abc72009-09-22 16:45:30 -07001802 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001803 if (host->ops->enable_dma) {
1804 if (host->ops->enable_dma(host)) {
1805 printk(KERN_WARNING "%s: No suitable DMA "
1806 "available. Falling back to PIO.\n",
1807 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07001808 host->flags &=
1809 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001810 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811 }
1812 }
1813
Pierre Ossman2134a922008-06-28 18:28:51 +02001814 if (host->flags & SDHCI_USE_ADMA) {
1815 /*
1816 * We need to allocate descriptors for all sg entries
1817 * (128) and potentially one alignment transfer for
1818 * each of those entries.
1819 */
1820 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
1821 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
1822 if (!host->adma_desc || !host->align_buffer) {
1823 kfree(host->adma_desc);
1824 kfree(host->align_buffer);
1825 printk(KERN_WARNING "%s: Unable to allocate ADMA "
1826 "buffers. Falling back to standard DMA.\n",
1827 mmc_hostname(mmc));
1828 host->flags &= ~SDHCI_USE_ADMA;
1829 }
1830 }
1831
Pierre Ossman76591502008-07-21 00:32:11 +02001832 /*
1833 * If we use DMA, then it's up to the caller to set the DMA
1834 * mask, but PIO does not need the hw shim so we set a new
1835 * mask here in that case.
1836 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07001837 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02001838 host->dma_mask = DMA_BIT_MASK(64);
1839 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
1840 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001841
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04001842 if (host->version >= SDHCI_SPEC_300)
1843 host->max_clk = (caps & SDHCI_CLOCK_V3_BASE_MASK)
1844 >> SDHCI_CLOCK_BASE_SHIFT;
1845 else
1846 host->max_clk = (caps & SDHCI_CLOCK_BASE_MASK)
1847 >> SDHCI_CLOCK_BASE_SHIFT;
1848
Pierre Ossmand129bce2006-03-24 03:18:17 -08001849 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07001850 if (host->max_clk == 0 || host->quirks &
1851 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001852 if (!host->ops->get_max_clock) {
1853 printk(KERN_ERR
1854 "%s: Hardware doesn't specify base clock "
1855 "frequency.\n", mmc_hostname(mmc));
1856 return -ENODEV;
1857 }
1858 host->max_clk = host->ops->get_max_clock(host);
1859 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001860
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001861 host->timeout_clk =
1862 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
1863 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07001864 if (host->ops->get_timeout_clock) {
1865 host->timeout_clk = host->ops->get_timeout_clock(host);
1866 } else if (!(host->quirks &
1867 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03001868 printk(KERN_ERR
1869 "%s: Hardware doesn't specify timeout clock "
1870 "frequency.\n", mmc_hostname(mmc));
1871 return -ENODEV;
1872 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07001873 }
1874 if (caps & SDHCI_TIMEOUT_CLK_UNIT)
1875 host->timeout_clk *= 1000;
1876
Pierre Ossmand129bce2006-03-24 03:18:17 -08001877 /*
1878 * Set host parameters.
1879 */
1880 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07001881 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001882 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04001883 else if (host->version >= SDHCI_SPEC_300)
1884 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07001885 else
Zhangfei Gao03975262010-09-20 15:15:18 -04001886 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05001887
Pierre Ossmand129bce2006-03-24 03:18:17 -08001888 mmc->f_max = host->max_clk;
Kyungmin Parkc1f59772010-08-10 18:01:44 -07001889 mmc->caps |= MMC_CAP_SDIO_IRQ;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001890
Philip Rakity15ec4462010-11-19 16:48:39 -05001891 /*
1892 * A controller may support 8-bit width, but the board itself
1893 * might not have the pins brought out. Boards that support
1894 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
1895 * their platform code before calling sdhci_add_host(), and we
1896 * won't assume 8-bit width for hosts without that CAP.
1897 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04001898 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05001899 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001900
Pierre Ossman86a6a872009-02-02 21:13:49 +01001901 if (caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04001902 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001903
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01001904 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
1905 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001906 mmc->caps |= MMC_CAP_NEEDS_POLL;
1907
Takashi Iwai8f230f42010-12-08 10:04:30 +01001908 ocr_avail = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001909 if (caps & SDHCI_CAN_VDD_330)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001910 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001911 if (caps & SDHCI_CAN_VDD_300)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001912 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Pierre Ossmanc70840e2007-02-02 22:41:41 +01001913 if (caps & SDHCI_CAN_VDD_180)
Takashi Iwai8f230f42010-12-08 10:04:30 +01001914 ocr_avail |= MMC_VDD_165_195;
1915
1916 mmc->ocr_avail = ocr_avail;
1917 mmc->ocr_avail_sdio = ocr_avail;
1918 if (host->ocr_avail_sdio)
1919 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
1920 mmc->ocr_avail_sd = ocr_avail;
1921 if (host->ocr_avail_sd)
1922 mmc->ocr_avail_sd &= host->ocr_avail_sd;
1923 else /* normal SD controllers don't support 1.8V */
1924 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
1925 mmc->ocr_avail_mmc = ocr_avail;
1926 if (host->ocr_avail_mmc)
1927 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07001928
1929 if (mmc->ocr_avail == 0) {
1930 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001931 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001932 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07001933 }
1934
Pierre Ossmand129bce2006-03-24 03:18:17 -08001935 spin_lock_init(&host->lock);
1936
1937 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02001938 * Maximum number of segments. Depends on if the hardware
1939 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001940 */
Pierre Ossman2134a922008-06-28 18:28:51 +02001941 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001942 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07001943 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04001944 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02001945 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04001946 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001947
1948 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01001949 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01001950 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08001951 */
Pierre Ossman55db8902006-11-21 17:55:45 +01001952 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001953
1954 /*
1955 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02001956 * of bytes. When doing hardware scatter/gather, each entry cannot
1957 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001958 */
Olof Johansson30652aa2011-01-01 18:37:32 -06001959 if (host->flags & SDHCI_USE_ADMA) {
1960 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
1961 mmc->max_seg_size = 65535;
1962 else
1963 mmc->max_seg_size = 65536;
1964 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02001965 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06001966 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001967
1968 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001969 * Maximum block size. This varies from controller to controller and
1970 * is specified in the capabilities register.
1971 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03001972 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
1973 mmc->max_blk_size = 2;
1974 } else {
1975 mmc->max_blk_size = (caps & SDHCI_MAX_BLOCK_MASK) >>
1976 SDHCI_MAX_BLOCK_SHIFT;
1977 if (mmc->max_blk_size >= 3) {
1978 printk(KERN_WARNING "%s: Invalid maximum block size, "
1979 "assuming 512 bytes\n", mmc_hostname(mmc));
1980 mmc->max_blk_size = 0;
1981 }
1982 }
1983
1984 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001985
1986 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01001987 * Maximum block count.
1988 */
Ben Dooks1388eef2009-06-14 12:40:53 +01001989 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01001990
1991 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08001992 * Init tasklets.
1993 */
1994 tasklet_init(&host->card_tasklet,
1995 sdhci_tasklet_card, (unsigned long)host);
1996 tasklet_init(&host->finish_tasklet,
1997 sdhci_tasklet_finish, (unsigned long)host);
1998
Al Viroe4cad1b2006-10-10 22:47:07 +01001999 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002000
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002001 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002002 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002003 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002004 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002005
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002006 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2007 if (IS_ERR(host->vmmc)) {
2008 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2009 host->vmmc = NULL;
2010 } else {
2011 regulator_enable(host->vmmc);
2012 }
2013
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002014 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002015
2016#ifdef CONFIG_MMC_DEBUG
2017 sdhci_dumpregs(host);
2018#endif
2019
Pierre Ossmanf9134312008-12-21 17:01:48 +01002020#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002021 snprintf(host->led_name, sizeof(host->led_name),
2022 "%s::", mmc_hostname(mmc));
2023 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002024 host->led.brightness = LED_OFF;
2025 host->led.default_trigger = mmc_hostname(mmc);
2026 host->led.brightness_set = sdhci_led_control;
2027
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002028 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002029 if (ret)
2030 goto reset;
2031#endif
2032
Pierre Ossman5f25a662006-10-04 02:15:39 -07002033 mmiowb();
2034
Pierre Ossmand129bce2006-03-24 03:18:17 -08002035 mmc_add_host(mmc);
2036
Richard Röjforsa13abc72009-09-22 16:45:30 -07002037 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002038 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002039 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2040 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002041
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002042 sdhci_enable_card_detection(host);
2043
Pierre Ossmand129bce2006-03-24 03:18:17 -08002044 return 0;
2045
Pierre Ossmanf9134312008-12-21 17:01:48 +01002046#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002047reset:
2048 sdhci_reset(host, SDHCI_RESET_ALL);
2049 free_irq(host->irq, host);
2050#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002051untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002052 tasklet_kill(&host->card_tasklet);
2053 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054
2055 return ret;
2056}
2057
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002058EXPORT_SYMBOL_GPL(sdhci_add_host);
2059
Pierre Ossman1e728592008-04-16 19:13:13 +02002060void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002061{
Pierre Ossman1e728592008-04-16 19:13:13 +02002062 unsigned long flags;
2063
2064 if (dead) {
2065 spin_lock_irqsave(&host->lock, flags);
2066
2067 host->flags |= SDHCI_DEVICE_DEAD;
2068
2069 if (host->mrq) {
2070 printk(KERN_ERR "%s: Controller removed during "
2071 " transfer!\n", mmc_hostname(host->mmc));
2072
2073 host->mrq->cmd->error = -ENOMEDIUM;
2074 tasklet_schedule(&host->finish_tasklet);
2075 }
2076
2077 spin_unlock_irqrestore(&host->lock, flags);
2078 }
2079
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002080 sdhci_disable_card_detection(host);
2081
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002082 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083
Pierre Ossmanf9134312008-12-21 17:01:48 +01002084#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002085 led_classdev_unregister(&host->led);
2086#endif
2087
Pierre Ossman1e728592008-04-16 19:13:13 +02002088 if (!dead)
2089 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002090
2091 free_irq(host->irq, host);
2092
2093 del_timer_sync(&host->timer);
2094
2095 tasklet_kill(&host->card_tasklet);
2096 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002097
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002098 if (host->vmmc) {
2099 regulator_disable(host->vmmc);
2100 regulator_put(host->vmmc);
2101 }
2102
Pierre Ossman2134a922008-06-28 18:28:51 +02002103 kfree(host->adma_desc);
2104 kfree(host->align_buffer);
2105
2106 host->adma_desc = NULL;
2107 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002108}
2109
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002110EXPORT_SYMBOL_GPL(sdhci_remove_host);
2111
2112void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002114 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002115}
2116
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002117EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118
2119/*****************************************************************************\
2120 * *
2121 * Driver init/exit *
2122 * *
2123\*****************************************************************************/
2124
2125static int __init sdhci_drv_init(void)
2126{
2127 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002128 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002129 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2130
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002131 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002132}
2133
2134static void __exit sdhci_drv_exit(void)
2135{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002136}
2137
2138module_init(sdhci_drv_init);
2139module_exit(sdhci_drv_exit);
2140
Pierre Ossmandf673b22006-06-30 02:22:31 -07002141module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002142
Pierre Ossman32710e82009-04-08 20:14:54 +02002143MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002144MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002145MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002146
Pierre Ossmandf673b22006-06-30 02:22:31 -07002147MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");