blob: 8994493dd940e6abad61136a41b40cff93c703ef [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));
Arindam Nathf2119df2011-05-05 12:18:57 +053086 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
87 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080088
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010089 if (host->flags & SDHCI_USE_ADMA)
90 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
91 readl(host->ioaddr + SDHCI_ADMA_ERROR),
92 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
93
Pierre Ossmand129bce2006-03-24 03:18:17 -080094 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
95}
96
97/*****************************************************************************\
98 * *
99 * Low level functions *
100 * *
101\*****************************************************************************/
102
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300103static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
104{
105 u32 ier;
106
107 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
108 ier &= ~clear;
109 ier |= set;
110 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
111 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
112}
113
114static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
115{
116 sdhci_clear_set_irqs(host, 0, irqs);
117}
118
119static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
120{
121 sdhci_clear_set_irqs(host, irqs, 0);
122}
123
124static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
125{
126 u32 irqs = SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT;
127
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300128 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
129 return;
130
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300131 if (enable)
132 sdhci_unmask_irqs(host, irqs);
133 else
134 sdhci_mask_irqs(host, irqs);
135}
136
137static void sdhci_enable_card_detection(struct sdhci_host *host)
138{
139 sdhci_set_card_detection(host, true);
140}
141
142static void sdhci_disable_card_detection(struct sdhci_host *host)
143{
144 sdhci_set_card_detection(host, false);
145}
146
Pierre Ossmand129bce2006-03-24 03:18:17 -0800147static void sdhci_reset(struct sdhci_host *host, u8 mask)
148{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700149 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300150 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700151
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100152 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300153 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700154 SDHCI_CARD_PRESENT))
155 return;
156 }
157
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300158 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
159 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
160
Philip Rakity393c1a32011-01-21 11:26:40 -0800161 if (host->ops->platform_reset_enter)
162 host->ops->platform_reset_enter(host, mask);
163
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300164 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800165
Pierre Ossmane16514d82006-06-30 02:22:24 -0700166 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167 host->clock = 0;
168
Pierre Ossmane16514d82006-06-30 02:22:24 -0700169 /* Wait max 100 ms */
170 timeout = 100;
171
172 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300173 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700174 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100175 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 mmc_hostname(host->mmc), (int)mask);
177 sdhci_dumpregs(host);
178 return;
179 }
180 timeout--;
181 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300183
Philip Rakity393c1a32011-01-21 11:26:40 -0800184 if (host->ops->platform_reset_exit)
185 host->ops->platform_reset_exit(host, mask);
186
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300187 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
188 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189}
190
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800191static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
192
193static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800195 if (soft)
196 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
197 else
198 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800199
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300200 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
201 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700202 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
203 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300204 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800205
206 if (soft) {
207 /* force clock reconfiguration */
208 host->clock = 0;
209 sdhci_set_ios(host->mmc, &host->mmc->ios);
210 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300211}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800212
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300213static void sdhci_reinit(struct sdhci_host *host)
214{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800215 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300216 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217}
218
219static void sdhci_activate_led(struct sdhci_host *host)
220{
221 u8 ctrl;
222
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300223 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300225 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
228static void sdhci_deactivate_led(struct sdhci_host *host)
229{
230 u8 ctrl;
231
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300232 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800233 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300234 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235}
236
Pierre Ossmanf9134312008-12-21 17:01:48 +0100237#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100238static void sdhci_led_control(struct led_classdev *led,
239 enum led_brightness brightness)
240{
241 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
242 unsigned long flags;
243
244 spin_lock_irqsave(&host->lock, flags);
245
246 if (brightness == LED_OFF)
247 sdhci_deactivate_led(host);
248 else
249 sdhci_activate_led(host);
250
251 spin_unlock_irqrestore(&host->lock, flags);
252}
253#endif
254
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255/*****************************************************************************\
256 * *
257 * Core functions *
258 * *
259\*****************************************************************************/
260
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100261static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262{
Pierre Ossman76591502008-07-21 00:32:11 +0200263 unsigned long flags;
264 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700265 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200266 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100268 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100270 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200271 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272
Pierre Ossman76591502008-07-21 00:32:11 +0200273 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800274
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100275 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200276 if (!sg_miter_next(&host->sg_miter))
277 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278
Pierre Ossman76591502008-07-21 00:32:11 +0200279 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280
Pierre Ossman76591502008-07-21 00:32:11 +0200281 blksize -= len;
282 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200283
Pierre Ossman76591502008-07-21 00:32:11 +0200284 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285
Pierre Ossman76591502008-07-21 00:32:11 +0200286 while (len) {
287 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300288 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200289 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800290 }
Pierre Ossman76591502008-07-21 00:32:11 +0200291
292 *buf = scratch & 0xFF;
293
294 buf++;
295 scratch >>= 8;
296 chunk--;
297 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800298 }
299 }
Pierre Ossman76591502008-07-21 00:32:11 +0200300
301 sg_miter_stop(&host->sg_miter);
302
303 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100304}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306static void sdhci_write_block_pio(struct sdhci_host *host)
307{
Pierre Ossman76591502008-07-21 00:32:11 +0200308 unsigned long flags;
309 size_t blksize, len, chunk;
310 u32 scratch;
311 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100312
313 DBG("PIO writing\n");
314
315 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200316 chunk = 0;
317 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320
321 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200322 if (!sg_miter_next(&host->sg_miter))
323 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324
Pierre Ossman76591502008-07-21 00:32:11 +0200325 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200326
Pierre Ossman76591502008-07-21 00:32:11 +0200327 blksize -= len;
328 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329
Pierre Ossman76591502008-07-21 00:32:11 +0200330 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100331
Pierre Ossman76591502008-07-21 00:32:11 +0200332 while (len) {
333 scratch |= (u32)*buf << (chunk * 8);
334
335 buf++;
336 chunk++;
337 len--;
338
339 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300340 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200341 chunk = 0;
342 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100343 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344 }
345 }
Pierre Ossman76591502008-07-21 00:32:11 +0200346
347 sg_miter_stop(&host->sg_miter);
348
349 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350}
351
352static void sdhci_transfer_pio(struct sdhci_host *host)
353{
354 u32 mask;
355
356 BUG_ON(!host->data);
357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359 return;
360
361 if (host->data->flags & MMC_DATA_READ)
362 mask = SDHCI_DATA_AVAILABLE;
363 else
364 mask = SDHCI_SPACE_AVAILABLE;
365
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200366 /*
367 * Some controllers (JMicron JMB38x) mess up the buffer bits
368 * for transfers < 4 bytes. As long as it is just one block,
369 * we can ignore the bits.
370 */
371 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
372 (host->data->blocks == 1))
373 mask = ~0;
374
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300375 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300376 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
377 udelay(100);
378
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 if (host->data->flags & MMC_DATA_READ)
380 sdhci_read_block_pio(host);
381 else
382 sdhci_write_block_pio(host);
383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 host->blocks--;
385 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100386 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387 }
388
389 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800390}
391
Pierre Ossman2134a922008-06-28 18:28:51 +0200392static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
393{
394 local_irq_save(*flags);
395 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
396}
397
398static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
399{
400 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
401 local_irq_restore(*flags);
402}
403
Ben Dooks118cd172010-03-05 13:43:26 -0800404static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
405{
Ben Dooks9e506f32010-03-05 13:43:29 -0800406 __le32 *dataddr = (__le32 __force *)(desc + 4);
407 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800408
Ben Dooks9e506f32010-03-05 13:43:29 -0800409 /* SDHCI specification says ADMA descriptors should be 4 byte
410 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800411
Ben Dooks9e506f32010-03-05 13:43:29 -0800412 cmdlen[0] = cpu_to_le16(cmd);
413 cmdlen[1] = cpu_to_le16(len);
414
415 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800416}
417
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200418static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200419 struct mmc_data *data)
420{
421 int direction;
422
423 u8 *desc;
424 u8 *align;
425 dma_addr_t addr;
426 dma_addr_t align_addr;
427 int len, offset;
428
429 struct scatterlist *sg;
430 int i;
431 char *buffer;
432 unsigned long flags;
433
434 /*
435 * The spec does not specify endianness of descriptor table.
436 * We currently guess that it is LE.
437 */
438
439 if (data->flags & MMC_DATA_READ)
440 direction = DMA_FROM_DEVICE;
441 else
442 direction = DMA_TO_DEVICE;
443
444 /*
445 * The ADMA descriptor table is mapped further down as we
446 * need to fill it with data first.
447 */
448
449 host->align_addr = dma_map_single(mmc_dev(host->mmc),
450 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700451 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200452 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200453 BUG_ON(host->align_addr & 0x3);
454
455 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
456 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200457 if (host->sg_count == 0)
458 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200459
460 desc = host->adma_desc;
461 align = host->align_buffer;
462
463 align_addr = host->align_addr;
464
465 for_each_sg(data->sg, sg, host->sg_count, i) {
466 addr = sg_dma_address(sg);
467 len = sg_dma_len(sg);
468
469 /*
470 * The SDHCI specification states that ADMA
471 * addresses must be 32-bit aligned. If they
472 * aren't, then we use a bounce buffer for
473 * the (up to three) bytes that screw up the
474 * alignment.
475 */
476 offset = (4 - (addr & 0x3)) & 0x3;
477 if (offset) {
478 if (data->flags & MMC_DATA_WRITE) {
479 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200480 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200481 memcpy(align, buffer, offset);
482 sdhci_kunmap_atomic(buffer, &flags);
483 }
484
Ben Dooks118cd172010-03-05 13:43:26 -0800485 /* tran, valid */
486 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200487
488 BUG_ON(offset > 65536);
489
Pierre Ossman2134a922008-06-28 18:28:51 +0200490 align += 4;
491 align_addr += 4;
492
493 desc += 8;
494
495 addr += offset;
496 len -= offset;
497 }
498
Pierre Ossman2134a922008-06-28 18:28:51 +0200499 BUG_ON(len > 65536);
500
Ben Dooks118cd172010-03-05 13:43:26 -0800501 /* tran, valid */
502 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200503 desc += 8;
504
505 /*
506 * If this triggers then we have a calculation bug
507 * somewhere. :/
508 */
509 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
510 }
511
Thomas Abraham70764a92010-05-26 14:42:04 -0700512 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
513 /*
514 * Mark the last descriptor as the terminating descriptor
515 */
516 if (desc != host->adma_desc) {
517 desc -= 8;
518 desc[0] |= 0x2; /* end */
519 }
520 } else {
521 /*
522 * Add a terminating entry.
523 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200524
Thomas Abraham70764a92010-05-26 14:42:04 -0700525 /* nop, end, valid */
526 sdhci_set_adma_desc(desc, 0, 0, 0x3);
527 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200528
529 /*
530 * Resync align buffer as we might have changed it.
531 */
532 if (data->flags & MMC_DATA_WRITE) {
533 dma_sync_single_for_device(mmc_dev(host->mmc),
534 host->align_addr, 128 * 4, direction);
535 }
536
537 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
538 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200539 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200540 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200541 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200542
543 return 0;
544
545unmap_entries:
546 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
547 data->sg_len, direction);
548unmap_align:
549 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
550 128 * 4, direction);
551fail:
552 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200553}
554
555static void sdhci_adma_table_post(struct sdhci_host *host,
556 struct mmc_data *data)
557{
558 int direction;
559
560 struct scatterlist *sg;
561 int i, size;
562 u8 *align;
563 char *buffer;
564 unsigned long flags;
565
566 if (data->flags & MMC_DATA_READ)
567 direction = DMA_FROM_DEVICE;
568 else
569 direction = DMA_TO_DEVICE;
570
571 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
572 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
573
574 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
575 128 * 4, direction);
576
577 if (data->flags & MMC_DATA_READ) {
578 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
579 data->sg_len, direction);
580
581 align = host->align_buffer;
582
583 for_each_sg(data->sg, sg, host->sg_count, i) {
584 if (sg_dma_address(sg) & 0x3) {
585 size = 4 - (sg_dma_address(sg) & 0x3);
586
587 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200588 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200589 memcpy(buffer, align, size);
590 sdhci_kunmap_atomic(buffer, &flags);
591
592 align += 4;
593 }
594 }
595 }
596
597 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
598 data->sg_len, direction);
599}
600
Andrei Warkentina3c77782011-04-11 16:13:42 -0500601static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800602{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700603 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500604 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700605 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800606
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200607 /*
608 * If the host controller provides us with an incorrect timeout
609 * value, just skip the check and use 0xE. The hardware may take
610 * longer to time out, but that's much better than having a too-short
611 * timeout value.
612 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200613 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200614 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200615
Andrei Warkentina3c77782011-04-11 16:13:42 -0500616 /* Unspecified timeout, assume max */
617 if (!data && !cmd->cmd_timeout_ms)
618 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800619
Andrei Warkentina3c77782011-04-11 16:13:42 -0500620 /* timeout in us */
621 if (!data)
622 target_timeout = cmd->cmd_timeout_ms * 1000;
623 else
624 target_timeout = data->timeout_ns / 1000 +
625 data->timeout_clks / host->clock;
Anton Vorontsov81b39802009-09-22 16:45:13 -0700626
Mark Brown4b016812011-04-19 18:44:17 +0100627 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
628 host->timeout_clk = host->clock / 1000;
629
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700630 /*
631 * Figure out needed cycles.
632 * We do this in steps in order to fit inside a 32 bit int.
633 * The first step is the minimum timeout, which will have a
634 * minimum resolution of 6 bits:
635 * (1) 2^13*1000 > 2^22,
636 * (2) host->timeout_clk < 2^16
637 * =>
638 * (1) / (2) > 2^6
639 */
Mark Brown4b016812011-04-19 18:44:17 +0100640 BUG_ON(!host->timeout_clk);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700641 count = 0;
642 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
643 while (current_timeout < target_timeout) {
644 count++;
645 current_timeout <<= 1;
646 if (count >= 0xF)
647 break;
648 }
649
650 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500651 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
652 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700653 count = 0xE;
654 }
655
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200656 return count;
657}
658
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300659static void sdhci_set_transfer_irqs(struct sdhci_host *host)
660{
661 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
662 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
663
664 if (host->flags & SDHCI_REQ_USE_DMA)
665 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
666 else
667 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
668}
669
Andrei Warkentina3c77782011-04-11 16:13:42 -0500670static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200671{
672 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200673 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500674 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200675 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200676
677 WARN_ON(host->data);
678
Andrei Warkentina3c77782011-04-11 16:13:42 -0500679 if (data || (cmd->flags & MMC_RSP_BUSY)) {
680 count = sdhci_calc_timeout(host, cmd);
681 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
682 }
683
684 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200685 return;
686
687 /* Sanity checks */
688 BUG_ON(data->blksz * data->blocks > 524288);
689 BUG_ON(data->blksz > host->mmc->max_blk_size);
690 BUG_ON(data->blocks > 65535);
691
692 host->data = data;
693 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400694 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200695
Richard Röjforsa13abc72009-09-22 16:45:30 -0700696 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100697 host->flags |= SDHCI_REQ_USE_DMA;
698
Pierre Ossman2134a922008-06-28 18:28:51 +0200699 /*
700 * FIXME: This doesn't account for merging when mapping the
701 * scatterlist.
702 */
703 if (host->flags & SDHCI_REQ_USE_DMA) {
704 int broken, i;
705 struct scatterlist *sg;
706
707 broken = 0;
708 if (host->flags & SDHCI_USE_ADMA) {
709 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
710 broken = 1;
711 } else {
712 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
713 broken = 1;
714 }
715
716 if (unlikely(broken)) {
717 for_each_sg(data->sg, sg, data->sg_len, i) {
718 if (sg->length & 0x3) {
719 DBG("Reverting to PIO because of "
720 "transfer size (%d)\n",
721 sg->length);
722 host->flags &= ~SDHCI_REQ_USE_DMA;
723 break;
724 }
725 }
726 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100727 }
728
729 /*
730 * The assumption here being that alignment is the same after
731 * translation to device address space.
732 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200733 if (host->flags & SDHCI_REQ_USE_DMA) {
734 int broken, i;
735 struct scatterlist *sg;
736
737 broken = 0;
738 if (host->flags & SDHCI_USE_ADMA) {
739 /*
740 * As we use 3 byte chunks to work around
741 * alignment problems, we need to check this
742 * quirk.
743 */
744 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
745 broken = 1;
746 } else {
747 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
748 broken = 1;
749 }
750
751 if (unlikely(broken)) {
752 for_each_sg(data->sg, sg, data->sg_len, i) {
753 if (sg->offset & 0x3) {
754 DBG("Reverting to PIO because of "
755 "bad alignment\n");
756 host->flags &= ~SDHCI_REQ_USE_DMA;
757 break;
758 }
759 }
760 }
761 }
762
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200763 if (host->flags & SDHCI_REQ_USE_DMA) {
764 if (host->flags & SDHCI_USE_ADMA) {
765 ret = sdhci_adma_table_pre(host, data);
766 if (ret) {
767 /*
768 * This only happens when someone fed
769 * us an invalid request.
770 */
771 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200772 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200773 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300774 sdhci_writel(host, host->adma_addr,
775 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200776 }
777 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300778 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200779
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300780 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200781 data->sg, data->sg_len,
782 (data->flags & MMC_DATA_READ) ?
783 DMA_FROM_DEVICE :
784 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300785 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200786 /*
787 * This only happens when someone fed
788 * us an invalid request.
789 */
790 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200791 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200792 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200793 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300794 sdhci_writel(host, sg_dma_address(data->sg),
795 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200796 }
797 }
798 }
799
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 /*
801 * Always adjust the DMA selection as some controllers
802 * (e.g. JMicron) can't do PIO properly when the selection
803 * is ADMA.
804 */
805 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300806 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 ctrl &= ~SDHCI_CTRL_DMA_MASK;
808 if ((host->flags & SDHCI_REQ_USE_DMA) &&
809 (host->flags & SDHCI_USE_ADMA))
810 ctrl |= SDHCI_CTRL_ADMA32;
811 else
812 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300813 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100814 }
815
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200816 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200817 int flags;
818
819 flags = SG_MITER_ATOMIC;
820 if (host->data->flags & MMC_DATA_READ)
821 flags |= SG_MITER_TO_SG;
822 else
823 flags |= SG_MITER_FROM_SG;
824 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200825 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800826 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700827
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300828 sdhci_set_transfer_irqs(host);
829
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400830 /* Set the DMA boundary value and block size */
831 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
832 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300833 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700834}
835
836static void sdhci_set_transfer_mode(struct sdhci_host *host,
837 struct mmc_data *data)
838{
839 u16 mode;
840
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700841 if (data == NULL)
842 return;
843
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200844 WARN_ON(!host->data);
845
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700846 mode = SDHCI_TRNS_BLK_CNT_EN;
Jerry Huangc4512f72010-08-10 18:01:59 -0700847 if (data->blocks > 1) {
848 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
849 mode |= SDHCI_TRNS_MULTI | SDHCI_TRNS_ACMD12;
850 else
851 mode |= SDHCI_TRNS_MULTI;
852 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700853 if (data->flags & MMC_DATA_READ)
854 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100855 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700856 mode |= SDHCI_TRNS_DMA;
857
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300858 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800859}
860
861static void sdhci_finish_data(struct sdhci_host *host)
862{
863 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800864
865 BUG_ON(!host->data);
866
867 data = host->data;
868 host->data = NULL;
869
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100870 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200871 if (host->flags & SDHCI_USE_ADMA)
872 sdhci_adma_table_post(host, data);
873 else {
874 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
875 data->sg_len, (data->flags & MMC_DATA_READ) ?
876 DMA_FROM_DEVICE : DMA_TO_DEVICE);
877 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800878 }
879
880 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200881 * The specification states that the block count register must
882 * be updated, but it does not specify at what point in the
883 * data flow. That makes the register entirely useless to read
884 * back so we have to assume that nothing made it to the card
885 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800886 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200887 if (data->error)
888 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800889 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200890 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800891
Pierre Ossmand129bce2006-03-24 03:18:17 -0800892 if (data->stop) {
893 /*
894 * The controller needs a reset of internal state machines
895 * upon error conditions.
896 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200897 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800898 sdhci_reset(host, SDHCI_RESET_CMD);
899 sdhci_reset(host, SDHCI_RESET_DATA);
900 }
901
902 sdhci_send_command(host, data->stop);
903 } else
904 tasklet_schedule(&host->finish_tasklet);
905}
906
907static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
908{
909 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700910 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700911 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912
913 WARN_ON(host->cmd);
914
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700916 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700917
918 mask = SDHCI_CMD_INHIBIT;
919 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
920 mask |= SDHCI_DATA_INHIBIT;
921
922 /* We shouldn't wait for data inihibit for stop commands, even
923 though they might use busy signaling */
924 if (host->mrq->data && (cmd == host->mrq->data->stop))
925 mask &= ~SDHCI_DATA_INHIBIT;
926
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300927 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700928 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100930 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200932 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800933 tasklet_schedule(&host->finish_tasklet);
934 return;
935 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700936 timeout--;
937 mdelay(1);
938 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939
940 mod_timer(&host->timer, jiffies + 10 * HZ);
941
942 host->cmd = cmd;
943
Andrei Warkentina3c77782011-04-11 16:13:42 -0500944 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300946 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700948 sdhci_set_transfer_mode(host, cmd->data);
949
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100951 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800952 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200953 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800954 tasklet_schedule(&host->finish_tasklet);
955 return;
956 }
957
958 if (!(cmd->flags & MMC_RSP_PRESENT))
959 flags = SDHCI_CMD_RESP_NONE;
960 else if (cmd->flags & MMC_RSP_136)
961 flags = SDHCI_CMD_RESP_LONG;
962 else if (cmd->flags & MMC_RSP_BUSY)
963 flags = SDHCI_CMD_RESP_SHORT_BUSY;
964 else
965 flags = SDHCI_CMD_RESP_SHORT;
966
967 if (cmd->flags & MMC_RSP_CRC)
968 flags |= SDHCI_CMD_CRC;
969 if (cmd->flags & MMC_RSP_OPCODE)
970 flags |= SDHCI_CMD_INDEX;
971 if (cmd->data)
972 flags |= SDHCI_CMD_DATA;
973
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300974 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800975}
976
977static void sdhci_finish_command(struct sdhci_host *host)
978{
979 int i;
980
981 BUG_ON(host->cmd == NULL);
982
983 if (host->cmd->flags & MMC_RSP_PRESENT) {
984 if (host->cmd->flags & MMC_RSP_136) {
985 /* CRC is stripped so we need to do some shifting. */
986 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300987 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 SDHCI_RESPONSE + (3-i)*4) << 8;
989 if (i != 3)
990 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 SDHCI_RESPONSE + (3-i)*4-1);
993 }
994 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300995 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 }
997 }
998
Pierre Ossman17b04292007-07-22 22:18:46 +0200999 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001001 if (host->data && host->data_early)
1002 sdhci_finish_data(host);
1003
1004 if (!host->cmd->data)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001005 tasklet_schedule(&host->finish_tasklet);
1006
1007 host->cmd = NULL;
1008}
1009
1010static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1011{
1012 int div;
1013 u16 clk;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001014 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015
1016 if (clock == host->clock)
1017 return;
1018
Anton Vorontsov81146342009-03-17 00:13:59 +03001019 if (host->ops->set_clock) {
1020 host->ops->set_clock(host, clock);
1021 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1022 return;
1023 }
1024
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001025 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
1027 if (clock == 0)
1028 goto out;
1029
Zhangfei Gao85105c52010-08-06 07:10:01 +08001030 if (host->version >= SDHCI_SPEC_300) {
1031 /* Version 3.00 divisors must be a multiple of 2. */
1032 if (host->max_clk <= clock)
1033 div = 1;
1034 else {
Zhangfei Gao03975262010-09-20 15:15:18 -04001035 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300; div += 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001036 if ((host->max_clk / div) <= clock)
1037 break;
1038 }
1039 }
1040 } else {
1041 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001042 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001043 if ((host->max_clk / div) <= clock)
1044 break;
1045 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001046 }
1047 div >>= 1;
1048
Zhangfei Gao85105c52010-08-06 07:10:01 +08001049 clk = (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
1050 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1051 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001053 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054
Chris Ball27f6cb12009-09-22 16:45:31 -07001055 /* Wait max 20 ms */
1056 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001057 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001058 & SDHCI_CLOCK_INT_STABLE)) {
1059 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001060 printk(KERN_ERR "%s: Internal clock never "
1061 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001062 sdhci_dumpregs(host);
1063 return;
1064 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001065 timeout--;
1066 mdelay(1);
1067 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068
1069 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001070 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071
1072out:
1073 host->clock = clock;
1074}
1075
Pierre Ossman146ad662006-06-30 02:22:23 -07001076static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1077{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001078 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001079
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001080 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001081 switch (1 << power) {
1082 case MMC_VDD_165_195:
1083 pwr = SDHCI_POWER_180;
1084 break;
1085 case MMC_VDD_29_30:
1086 case MMC_VDD_30_31:
1087 pwr = SDHCI_POWER_300;
1088 break;
1089 case MMC_VDD_32_33:
1090 case MMC_VDD_33_34:
1091 pwr = SDHCI_POWER_330;
1092 break;
1093 default:
1094 BUG();
1095 }
1096 }
1097
1098 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001099 return;
1100
Pierre Ossmanae628902009-05-03 20:45:03 +02001101 host->pwr = pwr;
1102
1103 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001104 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001105 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001106 }
1107
1108 /*
1109 * Spec says that we should clear the power reg before setting
1110 * a new value. Some controllers don't seem to like this though.
1111 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001112 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001113 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001114
Andres Salomone08c1692008-07-04 10:00:03 -07001115 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001116 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001117 * and set turn on power at the same time, so set the voltage first.
1118 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001119 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001120 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1121
1122 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001123
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001124 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001125
1126 /*
1127 * Some controllers need an extra 10ms delay of 10ms before they
1128 * can apply clock after applying power
1129 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001130 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001131 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001132}
1133
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134/*****************************************************************************\
1135 * *
1136 * MMC callbacks *
1137 * *
1138\*****************************************************************************/
1139
1140static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1141{
1142 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001143 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144 unsigned long flags;
1145
1146 host = mmc_priv(mmc);
1147
1148 spin_lock_irqsave(&host->lock, flags);
1149
1150 WARN_ON(host->mrq != NULL);
1151
Pierre Ossmanf9134312008-12-21 17:01:48 +01001152#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001154#endif
Jerry Huangc4512f72010-08-10 18:01:59 -07001155 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12) {
1156 if (mrq->stop) {
1157 mrq->data->stop = NULL;
1158 mrq->stop = NULL;
1159 }
1160 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001161
1162 host->mrq = mrq;
1163
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001164 /* If polling, assume that the card is always present. */
1165 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1166 present = true;
1167 else
1168 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1169 SDHCI_CARD_PRESENT;
1170
1171 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001172 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 tasklet_schedule(&host->finish_tasklet);
1174 } else
1175 sdhci_send_command(host, mrq->cmd);
1176
Pierre Ossman5f25a662006-10-04 02:15:39 -07001177 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 spin_unlock_irqrestore(&host->lock, flags);
1179}
1180
1181static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1182{
1183 struct sdhci_host *host;
1184 unsigned long flags;
1185 u8 ctrl;
1186
1187 host = mmc_priv(mmc);
1188
1189 spin_lock_irqsave(&host->lock, flags);
1190
Pierre Ossman1e728592008-04-16 19:13:13 +02001191 if (host->flags & SDHCI_DEVICE_DEAD)
1192 goto out;
1193
Pierre Ossmand129bce2006-03-24 03:18:17 -08001194 /*
1195 * Reset the chip on each power off.
1196 * Should clear out any weird states.
1197 */
1198 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001199 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001200 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201 }
1202
1203 sdhci_set_clock(host, ios->clock);
1204
1205 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001206 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001208 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209
Philip Rakity643a81f2010-09-23 08:24:32 -07001210 if (host->ops->platform_send_init_74_clocks)
1211 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1212
Philip Rakity15ec4462010-11-19 16:48:39 -05001213 /*
1214 * If your platform has 8-bit width support but is not a v3 controller,
1215 * or if it requires special setup code, you should implement that in
1216 * platform_8bit_width().
1217 */
1218 if (host->ops->platform_8bit_width)
1219 host->ops->platform_8bit_width(host, ios->bus_width);
1220 else {
1221 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1222 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1223 ctrl &= ~SDHCI_CTRL_4BITBUS;
1224 if (host->version >= SDHCI_SPEC_300)
1225 ctrl |= SDHCI_CTRL_8BITBUS;
1226 } else {
1227 if (host->version >= SDHCI_SPEC_300)
1228 ctrl &= ~SDHCI_CTRL_8BITBUS;
1229 if (ios->bus_width == MMC_BUS_WIDTH_4)
1230 ctrl |= SDHCI_CTRL_4BITBUS;
1231 else
1232 ctrl &= ~SDHCI_CTRL_4BITBUS;
1233 }
1234 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1235 }
1236
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001237 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001238
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001239 if ((ios->timing == MMC_TIMING_SD_HS ||
1240 ios->timing == MMC_TIMING_MMC_HS)
1241 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001242 ctrl |= SDHCI_CTRL_HISPD;
1243 else
1244 ctrl &= ~SDHCI_CTRL_HISPD;
1245
Arindam Nathd6d50a12011-05-05 12:18:59 +05301246 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301247 u16 clk, ctrl_2;
1248 unsigned int clock;
1249
1250 /* In case of UHS-I modes, set High Speed Enable */
1251 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1252 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1253 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1254 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1255 (ios->timing == MMC_TIMING_UHS_SDR12))
1256 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301257
1258 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1259 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301260 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301261 /*
1262 * We only need to set Driver Strength if the
1263 * preset value enable is not set.
1264 */
1265 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1266 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1267 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1268 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1269 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1270
1271 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301272 } else {
1273 /*
1274 * According to SDHC Spec v3.00, if the Preset Value
1275 * Enable in the Host Control 2 register is set, we
1276 * need to reset SD Clock Enable before changing High
1277 * Speed Enable to avoid generating clock gliches.
1278 */
Arindam Nath758535c2011-05-05 12:19:00 +05301279
1280 /* Reset SD Clock Enable */
1281 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1282 clk &= ~SDHCI_CLOCK_CARD_EN;
1283 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1284
1285 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1286
1287 /* Re-enable SD Clock */
1288 clock = host->clock;
1289 host->clock = 0;
1290 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301291 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301292
1293 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1294
1295 /* Select Bus Speed Mode for host */
1296 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1297 if (ios->timing == MMC_TIMING_UHS_SDR12)
1298 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1299 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1300 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1301 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1302 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1303 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1304 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1305 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1306 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1307
1308 /* Reset SD Clock Enable */
1309 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1310 clk &= ~SDHCI_CLOCK_CARD_EN;
1311 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1312
1313 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1314
1315 /* Re-enable SD Clock */
1316 clock = host->clock;
1317 host->clock = 0;
1318 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301319 } else
1320 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301321
Leandro Dorileob8352262007-07-25 23:47:04 +02001322 /*
1323 * Some (ENE) controllers go apeshit on some ios operation,
1324 * signalling timeout and CRC errors even on CMD0. Resetting
1325 * it on each ios seems to solve the problem.
1326 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001327 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001328 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1329
Pierre Ossman1e728592008-04-16 19:13:13 +02001330out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001331 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 spin_unlock_irqrestore(&host->lock, flags);
1333}
1334
Takashi Iwai82b0e232011-04-21 20:26:38 +02001335static int check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001338 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339
Pierre Ossmand129bce2006-03-24 03:18:17 -08001340 spin_lock_irqsave(&host->lock, flags);
1341
Pierre Ossman1e728592008-04-16 19:13:13 +02001342 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001343 is_readonly = 0;
1344 else if (host->ops->get_ro)
1345 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001346 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001347 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1348 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349
1350 spin_unlock_irqrestore(&host->lock, flags);
1351
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001352 /* This quirk needs to be replaced by a callback-function later */
1353 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1354 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001355}
1356
Takashi Iwai82b0e232011-04-21 20:26:38 +02001357#define SAMPLE_COUNT 5
1358
1359static int sdhci_get_ro(struct mmc_host *mmc)
1360{
1361 struct sdhci_host *host;
1362 int i, ro_count;
1363
1364 host = mmc_priv(mmc);
1365
1366 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1367 return check_ro(host);
1368
1369 ro_count = 0;
1370 for (i = 0; i < SAMPLE_COUNT; i++) {
1371 if (check_ro(host)) {
1372 if (++ro_count > SAMPLE_COUNT / 2)
1373 return 1;
1374 }
1375 msleep(30);
1376 }
1377 return 0;
1378}
1379
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001380static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1381{
1382 struct sdhci_host *host;
1383 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001384
1385 host = mmc_priv(mmc);
1386
1387 spin_lock_irqsave(&host->lock, flags);
1388
Pierre Ossman1e728592008-04-16 19:13:13 +02001389 if (host->flags & SDHCI_DEVICE_DEAD)
1390 goto out;
1391
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001392 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001393 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1394 else
1395 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001396out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001397 mmiowb();
1398
1399 spin_unlock_irqrestore(&host->lock, flags);
1400}
1401
Arindam Nathf2119df2011-05-05 12:18:57 +05301402static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1403 struct mmc_ios *ios)
1404{
1405 struct sdhci_host *host;
1406 u8 pwr;
1407 u16 clk, ctrl;
1408 u32 present_state;
1409
1410 host = mmc_priv(mmc);
1411
1412 /*
1413 * Signal Voltage Switching is only applicable for Host Controllers
1414 * v3.00 and above.
1415 */
1416 if (host->version < SDHCI_SPEC_300)
1417 return 0;
1418
1419 /*
1420 * We first check whether the request is to set signalling voltage
1421 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1422 */
1423 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1424 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1425 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1426 ctrl &= ~SDHCI_CTRL_VDD_180;
1427 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1428
1429 /* Wait for 5ms */
1430 usleep_range(5000, 5500);
1431
1432 /* 3.3V regulator output should be stable within 5 ms */
1433 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1434 if (!(ctrl & SDHCI_CTRL_VDD_180))
1435 return 0;
1436 else {
1437 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1438 "signalling voltage failed\n");
1439 return -EIO;
1440 }
1441 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1442 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1443 /* Stop SDCLK */
1444 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1445 clk &= ~SDHCI_CLOCK_CARD_EN;
1446 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1447
1448 /* Check whether DAT[3:0] is 0000 */
1449 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1450 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1451 SDHCI_DATA_LVL_SHIFT)) {
1452 /*
1453 * Enable 1.8V Signal Enable in the Host Control2
1454 * register
1455 */
1456 ctrl |= SDHCI_CTRL_VDD_180;
1457 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1458
1459 /* Wait for 5ms */
1460 usleep_range(5000, 5500);
1461
1462 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1463 if (ctrl & SDHCI_CTRL_VDD_180) {
1464 /* Provide SDCLK again and wait for 1ms*/
1465 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1466 clk |= SDHCI_CLOCK_CARD_EN;
1467 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1468 usleep_range(1000, 1500);
1469
1470 /*
1471 * If DAT[3:0] level is 1111b, then the card
1472 * was successfully switched to 1.8V signaling.
1473 */
1474 present_state = sdhci_readl(host,
1475 SDHCI_PRESENT_STATE);
1476 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1477 SDHCI_DATA_LVL_MASK)
1478 return 0;
1479 }
1480 }
1481
1482 /*
1483 * If we are here, that means the switch to 1.8V signaling
1484 * failed. We power cycle the card, and retry initialization
1485 * sequence by setting S18R to 0.
1486 */
1487 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1488 pwr &= ~SDHCI_POWER_ON;
1489 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1490
1491 /* Wait for 1ms as per the spec */
1492 usleep_range(1000, 1500);
1493 pwr |= SDHCI_POWER_ON;
1494 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1495
1496 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1497 "voltage failed, retrying with S18R set to 0\n");
1498 return -EAGAIN;
1499 } else
1500 /* No signal voltage switch required */
1501 return 0;
1502}
1503
David Brownellab7aefd2006-11-12 17:55:30 -08001504static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 .request = sdhci_request,
1506 .set_ios = sdhci_set_ios,
1507 .get_ro = sdhci_get_ro,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001508 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301509 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001510};
1511
1512/*****************************************************************************\
1513 * *
1514 * Tasklets *
1515 * *
1516\*****************************************************************************/
1517
1518static void sdhci_tasklet_card(unsigned long param)
1519{
1520 struct sdhci_host *host;
1521 unsigned long flags;
1522
1523 host = (struct sdhci_host*)param;
1524
1525 spin_lock_irqsave(&host->lock, flags);
1526
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001527 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528 if (host->mrq) {
1529 printk(KERN_ERR "%s: Card removed during transfer!\n",
1530 mmc_hostname(host->mmc));
1531 printk(KERN_ERR "%s: Resetting controller.\n",
1532 mmc_hostname(host->mmc));
1533
1534 sdhci_reset(host, SDHCI_RESET_CMD);
1535 sdhci_reset(host, SDHCI_RESET_DATA);
1536
Pierre Ossman17b04292007-07-22 22:18:46 +02001537 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538 tasklet_schedule(&host->finish_tasklet);
1539 }
1540 }
1541
1542 spin_unlock_irqrestore(&host->lock, flags);
1543
Pierre Ossman04cf5852008-08-18 22:18:14 +02001544 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001545}
1546
1547static void sdhci_tasklet_finish(unsigned long param)
1548{
1549 struct sdhci_host *host;
1550 unsigned long flags;
1551 struct mmc_request *mrq;
1552
1553 host = (struct sdhci_host*)param;
1554
Chris Ball0c9c99a2011-04-27 17:35:31 -04001555 /*
1556 * If this tasklet gets rescheduled while running, it will
1557 * be run again afterwards but without any active request.
1558 */
1559 if (!host->mrq)
1560 return;
1561
Pierre Ossmand129bce2006-03-24 03:18:17 -08001562 spin_lock_irqsave(&host->lock, flags);
1563
1564 del_timer(&host->timer);
1565
1566 mrq = host->mrq;
1567
Pierre Ossmand129bce2006-03-24 03:18:17 -08001568 /*
1569 * The controller needs a reset of internal state machines
1570 * upon error conditions.
1571 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001572 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001573 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001574 (mrq->data && (mrq->data->error ||
1575 (mrq->data->stop && mrq->data->stop->error))) ||
1576 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001577
1578 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001579 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001580 unsigned int clock;
1581
1582 /* This is to force an update */
1583 clock = host->clock;
1584 host->clock = 0;
1585 sdhci_set_clock(host, clock);
1586 }
1587
1588 /* Spec says we should do both at the same time, but Ricoh
1589 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001590 sdhci_reset(host, SDHCI_RESET_CMD);
1591 sdhci_reset(host, SDHCI_RESET_DATA);
1592 }
1593
1594 host->mrq = NULL;
1595 host->cmd = NULL;
1596 host->data = NULL;
1597
Pierre Ossmanf9134312008-12-21 17:01:48 +01001598#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001600#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601
Pierre Ossman5f25a662006-10-04 02:15:39 -07001602 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001603 spin_unlock_irqrestore(&host->lock, flags);
1604
1605 mmc_request_done(host->mmc, mrq);
1606}
1607
1608static void sdhci_timeout_timer(unsigned long data)
1609{
1610 struct sdhci_host *host;
1611 unsigned long flags;
1612
1613 host = (struct sdhci_host*)data;
1614
1615 spin_lock_irqsave(&host->lock, flags);
1616
1617 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001618 printk(KERN_ERR "%s: Timeout waiting for hardware "
1619 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001620 sdhci_dumpregs(host);
1621
1622 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001623 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624 sdhci_finish_data(host);
1625 } else {
1626 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001627 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001629 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001630
1631 tasklet_schedule(&host->finish_tasklet);
1632 }
1633 }
1634
Pierre Ossman5f25a662006-10-04 02:15:39 -07001635 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636 spin_unlock_irqrestore(&host->lock, flags);
1637}
1638
1639/*****************************************************************************\
1640 * *
1641 * Interrupt handling *
1642 * *
1643\*****************************************************************************/
1644
1645static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1646{
1647 BUG_ON(intmask == 0);
1648
1649 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001650 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1651 "though no command operation was in progress.\n",
1652 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001653 sdhci_dumpregs(host);
1654 return;
1655 }
1656
Pierre Ossman43b58b32007-07-25 23:15:27 +02001657 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001658 host->cmd->error = -ETIMEDOUT;
1659 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1660 SDHCI_INT_INDEX))
1661 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001662
Pierre Ossmane8095172008-07-25 01:09:08 +02001663 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001664 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001665 return;
1666 }
1667
1668 /*
1669 * The host can send and interrupt when the busy state has
1670 * ended, allowing us to wait without wasting CPU cycles.
1671 * Unfortunately this is overloaded on the "data complete"
1672 * interrupt, so we need to take some care when handling
1673 * it.
1674 *
1675 * Note: The 1.0 specification is a bit ambiguous about this
1676 * feature so there might be some problems with older
1677 * controllers.
1678 */
1679 if (host->cmd->flags & MMC_RSP_BUSY) {
1680 if (host->cmd->data)
1681 DBG("Cannot wait for busy signal when also "
1682 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03001683 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02001684 return;
Ben Dooksf9454052009-02-20 20:33:08 +03001685
1686 /* The controller does not support the end-of-busy IRQ,
1687 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02001688 }
1689
1690 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02001691 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001692}
1693
George G. Davis0957c332010-02-18 12:32:12 -05001694#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01001695static void sdhci_show_adma_error(struct sdhci_host *host)
1696{
1697 const char *name = mmc_hostname(host->mmc);
1698 u8 *desc = host->adma_desc;
1699 __le32 *dma;
1700 __le16 *len;
1701 u8 attr;
1702
1703 sdhci_dumpregs(host);
1704
1705 while (true) {
1706 dma = (__le32 *)(desc + 4);
1707 len = (__le16 *)(desc + 2);
1708 attr = *desc;
1709
1710 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
1711 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
1712
1713 desc += 8;
1714
1715 if (attr & 2)
1716 break;
1717 }
1718}
1719#else
1720static void sdhci_show_adma_error(struct sdhci_host *host) { }
1721#endif
1722
Pierre Ossmand129bce2006-03-24 03:18:17 -08001723static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
1724{
1725 BUG_ON(intmask == 0);
1726
1727 if (!host->data) {
1728 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02001729 * The "data complete" interrupt is also used to
1730 * indicate that a busy state has ended. See comment
1731 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08001732 */
Pierre Ossmane8095172008-07-25 01:09:08 +02001733 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
1734 if (intmask & SDHCI_INT_DATA_END) {
1735 sdhci_finish_command(host);
1736 return;
1737 }
1738 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001739
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001740 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
1741 "though no data operation was in progress.\n",
1742 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001743 sdhci_dumpregs(host);
1744
1745 return;
1746 }
1747
1748 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001749 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01001750 else if (intmask & SDHCI_INT_DATA_END_BIT)
1751 host->data->error = -EILSEQ;
1752 else if ((intmask & SDHCI_INT_DATA_CRC) &&
1753 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
1754 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02001755 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001756 else if (intmask & SDHCI_INT_ADMA_ERROR) {
1757 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
1758 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02001759 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01001760 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001761
Pierre Ossman17b04292007-07-22 22:18:46 +02001762 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763 sdhci_finish_data(host);
1764 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01001765 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001766 sdhci_transfer_pio(host);
1767
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001768 /*
1769 * We currently don't do anything fancy with DMA
1770 * boundaries, but as we can't disable the feature
1771 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001772 *
1773 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
1774 * should return a valid address to continue from, but as
1775 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001776 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001777 if (intmask & SDHCI_INT_DMA_END) {
1778 u32 dmastart, dmanow;
1779 dmastart = sg_dma_address(host->data->sg);
1780 dmanow = dmastart + host->data->bytes_xfered;
1781 /*
1782 * Force update to the next DMA block boundary.
1783 */
1784 dmanow = (dmanow &
1785 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
1786 SDHCI_DEFAULT_BOUNDARY_SIZE;
1787 host->data->bytes_xfered = dmanow - dmastart;
1788 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
1789 " next 0x%08x\n",
1790 mmc_hostname(host->mmc), dmastart,
1791 host->data->bytes_xfered, dmanow);
1792 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
1793 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02001794
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001795 if (intmask & SDHCI_INT_DATA_END) {
1796 if (host->cmd) {
1797 /*
1798 * Data managed to finish before the
1799 * command completed. Make sure we do
1800 * things in the proper order.
1801 */
1802 host->data_early = 1;
1803 } else {
1804 sdhci_finish_data(host);
1805 }
1806 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001807 }
1808}
1809
David Howells7d12e782006-10-05 14:55:46 +01001810static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811{
1812 irqreturn_t result;
1813 struct sdhci_host* host = dev_id;
1814 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001815 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001816
1817 spin_lock(&host->lock);
1818
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001819 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001820
Mark Lord62df67a52007-03-06 13:30:13 +01001821 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001822 result = IRQ_NONE;
1823 goto out;
1824 }
1825
Pierre Ossmanb69c9052008-03-08 23:44:25 +01001826 DBG("*** %s got interrupt: 0x%08x\n",
1827 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001828
Pierre Ossman3192a282006-06-30 02:22:26 -07001829 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001830 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
1831 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001832 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07001833 }
1834
1835 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001836
1837 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001838 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
1839 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001840 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001841 }
1842
1843 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001844 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
1845 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001846 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001847 }
1848
1849 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
1850
Pierre Ossman964f9ce2007-07-20 18:20:36 +02001851 intmask &= ~SDHCI_INT_ERROR;
1852
Pierre Ossmand129bce2006-03-24 03:18:17 -08001853 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07001854 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001855 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001856 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001857 }
1858
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02001859 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07001860
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001861 if (intmask & SDHCI_INT_CARD_INT)
1862 cardint = 1;
1863
1864 intmask &= ~SDHCI_INT_CARD_INT;
1865
Pierre Ossman3192a282006-06-30 02:22:26 -07001866 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001867 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07001868 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001869 sdhci_dumpregs(host);
1870
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001871 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07001872 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001873
1874 result = IRQ_HANDLED;
1875
Pierre Ossman5f25a662006-10-04 02:15:39 -07001876 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001877out:
1878 spin_unlock(&host->lock);
1879
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001880 /*
1881 * We have to delay this as it calls back into the driver.
1882 */
1883 if (cardint)
1884 mmc_signal_sdio_irq(host->mmc);
1885
Pierre Ossmand129bce2006-03-24 03:18:17 -08001886 return result;
1887}
1888
1889/*****************************************************************************\
1890 * *
1891 * Suspend/resume *
1892 * *
1893\*****************************************************************************/
1894
1895#ifdef CONFIG_PM
1896
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001897int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001898{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001899 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001900
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001901 sdhci_disable_card_detection(host);
1902
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001903 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01001904 if (ret)
1905 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001906
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001907 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001908
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001909 if (host->vmmc)
1910 ret = regulator_disable(host->vmmc);
1911
1912 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913}
1914
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001915EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001916
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001917int sdhci_resume_host(struct sdhci_host *host)
1918{
1919 int ret;
1920
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07001921 if (host->vmmc) {
1922 int ret = regulator_enable(host->vmmc);
1923 if (ret)
1924 return ret;
1925 }
1926
1927
Richard Röjforsa13abc72009-09-22 16:45:30 -07001928 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001929 if (host->ops->enable_dma)
1930 host->ops->enable_dma(host);
1931 }
1932
1933 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
1934 mmc_hostname(host->mmc), host);
1935 if (ret)
1936 return ret;
1937
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001938 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001939 mmiowb();
1940
1941 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001942 sdhci_enable_card_detection(host);
1943
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08001944 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001945}
1946
1947EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001948
Daniel Drake5f619702010-11-04 22:20:39 +00001949void sdhci_enable_irq_wakeups(struct sdhci_host *host)
1950{
1951 u8 val;
1952 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
1953 val |= SDHCI_WAKE_ON_INT;
1954 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
1955}
1956
1957EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
1958
Pierre Ossmand129bce2006-03-24 03:18:17 -08001959#endif /* CONFIG_PM */
1960
1961/*****************************************************************************\
1962 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001963 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08001964 * *
1965\*****************************************************************************/
1966
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001967struct sdhci_host *sdhci_alloc_host(struct device *dev,
1968 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001969{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001970 struct mmc_host *mmc;
1971 struct sdhci_host *host;
1972
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001973 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001974
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001975 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001976 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001977 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001978
1979 host = mmc_priv(mmc);
1980 host->mmc = mmc;
1981
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001982 return host;
1983}
Pierre Ossman8a4da142006-10-04 02:15:40 -07001984
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001985EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001986
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001987int sdhci_add_host(struct sdhci_host *host)
1988{
1989 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05301990 u32 caps[2];
1991 u32 max_current_caps;
1992 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001993 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001994
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001995 WARN_ON(host == NULL);
1996 if (host == NULL)
1997 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001998
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001999 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002000
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002001 if (debug_quirks)
2002 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002003
Pierre Ossmand96649e2006-06-30 02:22:30 -07002004 sdhci_reset(host, SDHCI_RESET_ALL);
2005
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002006 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002007 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2008 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002009 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07002010 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002011 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002012 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002013 }
2014
Arindam Nathf2119df2011-05-05 12:18:57 +05302015 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002016 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002017
Arindam Nathf2119df2011-05-05 12:18:57 +05302018 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2019 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2020
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002021 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002022 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302023 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002024 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002025 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002026 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002027
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002028 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002029 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002030 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002031 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002032 }
2033
Arindam Nathf2119df2011-05-05 12:18:57 +05302034 if ((host->version >= SDHCI_SPEC_200) &&
2035 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002036 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002037
2038 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2039 (host->flags & SDHCI_USE_ADMA)) {
2040 DBG("Disabling ADMA as it is marked broken\n");
2041 host->flags &= ~SDHCI_USE_ADMA;
2042 }
2043
Richard Röjforsa13abc72009-09-22 16:45:30 -07002044 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002045 if (host->ops->enable_dma) {
2046 if (host->ops->enable_dma(host)) {
2047 printk(KERN_WARNING "%s: No suitable DMA "
2048 "available. Falling back to PIO.\n",
2049 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002050 host->flags &=
2051 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002052 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002053 }
2054 }
2055
Pierre Ossman2134a922008-06-28 18:28:51 +02002056 if (host->flags & SDHCI_USE_ADMA) {
2057 /*
2058 * We need to allocate descriptors for all sg entries
2059 * (128) and potentially one alignment transfer for
2060 * each of those entries.
2061 */
2062 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2063 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2064 if (!host->adma_desc || !host->align_buffer) {
2065 kfree(host->adma_desc);
2066 kfree(host->align_buffer);
2067 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2068 "buffers. Falling back to standard DMA.\n",
2069 mmc_hostname(mmc));
2070 host->flags &= ~SDHCI_USE_ADMA;
2071 }
2072 }
2073
Pierre Ossman76591502008-07-21 00:32:11 +02002074 /*
2075 * If we use DMA, then it's up to the caller to set the DMA
2076 * mask, but PIO does not need the hw shim so we set a new
2077 * mask here in that case.
2078 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002079 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002080 host->dma_mask = DMA_BIT_MASK(64);
2081 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2082 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002084 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302085 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002086 >> SDHCI_CLOCK_BASE_SHIFT;
2087 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302088 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002089 >> SDHCI_CLOCK_BASE_SHIFT;
2090
Pierre Ossmand129bce2006-03-24 03:18:17 -08002091 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002092 if (host->max_clk == 0 || host->quirks &
2093 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002094 if (!host->ops->get_max_clock) {
2095 printk(KERN_ERR
2096 "%s: Hardware doesn't specify base clock "
2097 "frequency.\n", mmc_hostname(mmc));
2098 return -ENODEV;
2099 }
2100 host->max_clk = host->ops->get_max_clock(host);
2101 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002102
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002103 host->timeout_clk =
Arindam Nathf2119df2011-05-05 12:18:57 +05302104 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002105 if (host->timeout_clk == 0) {
Anton Vorontsov81b39802009-09-22 16:45:13 -07002106 if (host->ops->get_timeout_clock) {
2107 host->timeout_clk = host->ops->get_timeout_clock(host);
2108 } else if (!(host->quirks &
2109 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002110 printk(KERN_ERR
2111 "%s: Hardware doesn't specify timeout clock "
2112 "frequency.\n", mmc_hostname(mmc));
2113 return -ENODEV;
2114 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002115 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302116 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
Pierre Ossman1c8cde92006-06-30 02:22:25 -07002117 host->timeout_clk *= 1000;
2118
Pierre Ossmand129bce2006-03-24 03:18:17 -08002119 /*
2120 * Set host parameters.
2121 */
2122 mmc->ops = &sdhci_ops;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002123 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002124 mmc->f_min = host->ops->get_min_clock(host);
Zhangfei Gao03975262010-09-20 15:15:18 -04002125 else if (host->version >= SDHCI_SPEC_300)
2126 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002127 else
Zhangfei Gao03975262010-09-20 15:15:18 -04002128 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002129
Pierre Ossmand129bce2006-03-24 03:18:17 -08002130 mmc->f_max = host->max_clk;
Andrei Warkentina3c77782011-04-11 16:13:42 -05002131 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002132
Philip Rakity15ec4462010-11-19 16:48:39 -05002133 /*
2134 * A controller may support 8-bit width, but the board itself
2135 * might not have the pins brought out. Boards that support
2136 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2137 * their platform code before calling sdhci_add_host(), and we
2138 * won't assume 8-bit width for hosts without that CAP.
2139 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002140 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002141 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002142
Arindam Nathf2119df2011-05-05 12:18:57 +05302143 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002144 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002145
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002146 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2147 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002148 mmc->caps |= MMC_CAP_NEEDS_POLL;
2149
Arindam Nathf2119df2011-05-05 12:18:57 +05302150 /* UHS-I mode(s) supported by the host controller. */
2151 if (host->version >= SDHCI_SPEC_300)
2152 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2153
2154 /* SDR104 supports also implies SDR50 support */
2155 if (caps[1] & SDHCI_SUPPORT_SDR104)
2156 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2157 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2158 mmc->caps |= MMC_CAP_UHS_SDR50;
2159
2160 if (caps[1] & SDHCI_SUPPORT_DDR50)
2161 mmc->caps |= MMC_CAP_UHS_DDR50;
2162
Arindam Nathd6d50a12011-05-05 12:18:59 +05302163 /* Driver Type(s) (A, C, D) supported by the host */
2164 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2165 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2166 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2167 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2168 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2169 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2170
Takashi Iwai8f230f42010-12-08 10:04:30 +01002171 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302172 /*
2173 * According to SD Host Controller spec v3.00, if the Host System
2174 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2175 * the value is meaningful only if Voltage Support in the Capabilities
2176 * register is set. The actual current value is 4 times the register
2177 * value.
2178 */
2179 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2180
2181 if (caps[0] & SDHCI_CAN_VDD_330) {
2182 int max_current_330;
2183
Takashi Iwai8f230f42010-12-08 10:04:30 +01002184 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302185
2186 max_current_330 = ((max_current_caps &
2187 SDHCI_MAX_CURRENT_330_MASK) >>
2188 SDHCI_MAX_CURRENT_330_SHIFT) *
2189 SDHCI_MAX_CURRENT_MULTIPLIER;
2190
2191 if (max_current_330 > 150)
2192 mmc->caps |= MMC_CAP_SET_XPC_330;
2193 }
2194 if (caps[0] & SDHCI_CAN_VDD_300) {
2195 int max_current_300;
2196
Takashi Iwai8f230f42010-12-08 10:04:30 +01002197 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302198
2199 max_current_300 = ((max_current_caps &
2200 SDHCI_MAX_CURRENT_300_MASK) >>
2201 SDHCI_MAX_CURRENT_300_SHIFT) *
2202 SDHCI_MAX_CURRENT_MULTIPLIER;
2203
2204 if (max_current_300 > 150)
2205 mmc->caps |= MMC_CAP_SET_XPC_300;
2206 }
2207 if (caps[0] & SDHCI_CAN_VDD_180) {
2208 int max_current_180;
2209
Takashi Iwai8f230f42010-12-08 10:04:30 +01002210 ocr_avail |= MMC_VDD_165_195;
2211
Arindam Nathf2119df2011-05-05 12:18:57 +05302212 max_current_180 = ((max_current_caps &
2213 SDHCI_MAX_CURRENT_180_MASK) >>
2214 SDHCI_MAX_CURRENT_180_SHIFT) *
2215 SDHCI_MAX_CURRENT_MULTIPLIER;
2216
2217 if (max_current_180 > 150)
2218 mmc->caps |= MMC_CAP_SET_XPC_180;
2219 }
2220
Takashi Iwai8f230f42010-12-08 10:04:30 +01002221 mmc->ocr_avail = ocr_avail;
2222 mmc->ocr_avail_sdio = ocr_avail;
2223 if (host->ocr_avail_sdio)
2224 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2225 mmc->ocr_avail_sd = ocr_avail;
2226 if (host->ocr_avail_sd)
2227 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2228 else /* normal SD controllers don't support 1.8V */
2229 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2230 mmc->ocr_avail_mmc = ocr_avail;
2231 if (host->ocr_avail_mmc)
2232 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002233
2234 if (mmc->ocr_avail == 0) {
2235 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002236 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002237 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002238 }
2239
Pierre Ossmand129bce2006-03-24 03:18:17 -08002240 spin_lock_init(&host->lock);
2241
2242 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002243 * Maximum number of segments. Depends on if the hardware
2244 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002246 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002247 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002248 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002249 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002250 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002251 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252
2253 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002254 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002255 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002256 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002257 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258
2259 /*
2260 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002261 * of bytes. When doing hardware scatter/gather, each entry cannot
2262 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002263 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002264 if (host->flags & SDHCI_USE_ADMA) {
2265 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2266 mmc->max_seg_size = 65535;
2267 else
2268 mmc->max_seg_size = 65536;
2269 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002270 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002271 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272
2273 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002274 * Maximum block size. This varies from controller to controller and
2275 * is specified in the capabilities register.
2276 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002277 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2278 mmc->max_blk_size = 2;
2279 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302280 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002281 SDHCI_MAX_BLOCK_SHIFT;
2282 if (mmc->max_blk_size >= 3) {
2283 printk(KERN_WARNING "%s: Invalid maximum block size, "
2284 "assuming 512 bytes\n", mmc_hostname(mmc));
2285 mmc->max_blk_size = 0;
2286 }
2287 }
2288
2289 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002290
2291 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002292 * Maximum block count.
2293 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002294 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002295
2296 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002297 * Init tasklets.
2298 */
2299 tasklet_init(&host->card_tasklet,
2300 sdhci_tasklet_card, (unsigned long)host);
2301 tasklet_init(&host->finish_tasklet,
2302 sdhci_tasklet_finish, (unsigned long)host);
2303
Al Viroe4cad1b2006-10-10 22:47:07 +01002304 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002306 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002307 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002308 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002309 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002311 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2312 if (IS_ERR(host->vmmc)) {
2313 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2314 host->vmmc = NULL;
2315 } else {
2316 regulator_enable(host->vmmc);
2317 }
2318
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002319 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320
2321#ifdef CONFIG_MMC_DEBUG
2322 sdhci_dumpregs(host);
2323#endif
2324
Pierre Ossmanf9134312008-12-21 17:01:48 +01002325#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002326 snprintf(host->led_name, sizeof(host->led_name),
2327 "%s::", mmc_hostname(mmc));
2328 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002329 host->led.brightness = LED_OFF;
2330 host->led.default_trigger = mmc_hostname(mmc);
2331 host->led.brightness_set = sdhci_led_control;
2332
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002333 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002334 if (ret)
2335 goto reset;
2336#endif
2337
Pierre Ossman5f25a662006-10-04 02:15:39 -07002338 mmiowb();
2339
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340 mmc_add_host(mmc);
2341
Richard Röjforsa13abc72009-09-22 16:45:30 -07002342 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002343 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002344 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2345 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002347 sdhci_enable_card_detection(host);
2348
Pierre Ossmand129bce2006-03-24 03:18:17 -08002349 return 0;
2350
Pierre Ossmanf9134312008-12-21 17:01:48 +01002351#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002352reset:
2353 sdhci_reset(host, SDHCI_RESET_ALL);
2354 free_irq(host->irq, host);
2355#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002356untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357 tasklet_kill(&host->card_tasklet);
2358 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002359
2360 return ret;
2361}
2362
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002363EXPORT_SYMBOL_GPL(sdhci_add_host);
2364
Pierre Ossman1e728592008-04-16 19:13:13 +02002365void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366{
Pierre Ossman1e728592008-04-16 19:13:13 +02002367 unsigned long flags;
2368
2369 if (dead) {
2370 spin_lock_irqsave(&host->lock, flags);
2371
2372 host->flags |= SDHCI_DEVICE_DEAD;
2373
2374 if (host->mrq) {
2375 printk(KERN_ERR "%s: Controller removed during "
2376 " transfer!\n", mmc_hostname(host->mmc));
2377
2378 host->mrq->cmd->error = -ENOMEDIUM;
2379 tasklet_schedule(&host->finish_tasklet);
2380 }
2381
2382 spin_unlock_irqrestore(&host->lock, flags);
2383 }
2384
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002385 sdhci_disable_card_detection(host);
2386
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002387 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388
Pierre Ossmanf9134312008-12-21 17:01:48 +01002389#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002390 led_classdev_unregister(&host->led);
2391#endif
2392
Pierre Ossman1e728592008-04-16 19:13:13 +02002393 if (!dead)
2394 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395
2396 free_irq(host->irq, host);
2397
2398 del_timer_sync(&host->timer);
2399
2400 tasklet_kill(&host->card_tasklet);
2401 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002402
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002403 if (host->vmmc) {
2404 regulator_disable(host->vmmc);
2405 regulator_put(host->vmmc);
2406 }
2407
Pierre Ossman2134a922008-06-28 18:28:51 +02002408 kfree(host->adma_desc);
2409 kfree(host->align_buffer);
2410
2411 host->adma_desc = NULL;
2412 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413}
2414
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002415EXPORT_SYMBOL_GPL(sdhci_remove_host);
2416
2417void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002418{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002419 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002420}
2421
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002422EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423
2424/*****************************************************************************\
2425 * *
2426 * Driver init/exit *
2427 * *
2428\*****************************************************************************/
2429
2430static int __init sdhci_drv_init(void)
2431{
2432 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002433 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2435
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002436 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002437}
2438
2439static void __exit sdhci_drv_exit(void)
2440{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002441}
2442
2443module_init(sdhci_drv_init);
2444module_exit(sdhci_drv_exit);
2445
Pierre Ossmandf673b22006-06-30 02:22:31 -07002446module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002447
Pierre Ossman32710e82009-04-08 20:14:54 +02002448MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002449MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002451
Pierre Ossmandf673b22006-06-30 02:22:31 -07002452MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");