blob: 9394860602fe8f380b86ee506396c27716c4b322 [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>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080024
Pierre Ossman2f730fe2008-03-17 10:29:38 +010025#include <linux/leds.h>
26
Aries Lee22113ef2010-12-15 08:14:24 +010027#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080028#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#include "sdhci.h"
31
32#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010035 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmanf9134312008-12-21 17:01:48 +010037#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
38 defined(CONFIG_MMC_SDHCI_MODULE))
39#define SDHCI_USE_LEDS_CLASS
40#endif
41
Arindam Nathb513ea22011-05-05 12:19:04 +053042#define MAX_TUNING_LOOP 40
43
Pierre Ossmandf673b22006-06-30 02:22:31 -070044static unsigned int debug_quirks = 0;
Pierre Ossman67435272006-06-30 02:22:31 -070045
Pierre Ossmand129bce2006-03-24 03:18:17 -080046static void sdhci_finish_data(struct sdhci_host *);
47
48static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
49static void sdhci_finish_command(struct sdhci_host *);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053050static int sdhci_execute_tuning(struct mmc_host *mmc);
51static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53static void sdhci_dumpregs(struct sdhci_host *host)
54{
Philip Rakity412ab652010-09-22 15:25:13 -070055 printk(KERN_DEBUG DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
56 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
58 printk(KERN_DEBUG DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030059 sdhci_readl(host, SDHCI_DMA_ADDRESS),
60 sdhci_readw(host, SDHCI_HOST_VERSION));
Pierre Ossmand129bce2006-03-24 03:18:17 -080061 printk(KERN_DEBUG DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030062 sdhci_readw(host, SDHCI_BLOCK_SIZE),
63 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmand129bce2006-03-24 03:18:17 -080064 printk(KERN_DEBUG DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030065 sdhci_readl(host, SDHCI_ARGUMENT),
66 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080067 printk(KERN_DEBUG DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030068 sdhci_readl(host, SDHCI_PRESENT_STATE),
69 sdhci_readb(host, SDHCI_HOST_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080070 printk(KERN_DEBUG DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030071 sdhci_readb(host, SDHCI_POWER_CONTROL),
72 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073 printk(KERN_DEBUG DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030074 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
75 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Pierre Ossmand129bce2006-03-24 03:18:17 -080076 printk(KERN_DEBUG DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
78 sdhci_readl(host, SDHCI_INT_STATUS));
Pierre Ossmand129bce2006-03-24 03:18:17 -080079 printk(KERN_DEBUG DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readl(host, SDHCI_INT_ENABLE),
81 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Pierre Ossmand129bce2006-03-24 03:18:17 -080082 printk(KERN_DEBUG DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readw(host, SDHCI_ACMD12_ERR),
84 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Philip Rakitye8120ad2010-11-30 00:55:23 -050085 printk(KERN_DEBUG DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050087 sdhci_readl(host, SDHCI_CAPABILITIES_1));
88 printk(KERN_DEBUG DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
89 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readl(host, SDHCI_MAX_CURRENT));
Arindam Nathf2119df2011-05-05 12:18:57 +053091 printk(KERN_DEBUG DRIVER_NAME ": Host ctl2: 0x%08x\n",
92 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080093
Ben Dooksbe3f4ae2009-06-08 23:33:52 +010094 if (host->flags & SDHCI_USE_ADMA)
95 printk(KERN_DEBUG DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
96 readl(host->ioaddr + SDHCI_ADMA_ERROR),
97 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
98
Pierre Ossmand129bce2006-03-24 03:18:17 -080099 printk(KERN_DEBUG DRIVER_NAME ": ===========================================\n");
100}
101
102/*****************************************************************************\
103 * *
104 * Low level functions *
105 * *
106\*****************************************************************************/
107
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300108static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
109{
110 u32 ier;
111
112 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
113 ier &= ~clear;
114 ier |= set;
115 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
116 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
117}
118
119static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
120{
121 sdhci_clear_set_irqs(host, 0, irqs);
122}
123
124static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
125{
126 sdhci_clear_set_irqs(host, irqs, 0);
127}
128
129static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
130{
Shawn Guod25928d2011-06-21 22:41:48 +0800131 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300132
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300133 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
134 return;
135
Shawn Guod25928d2011-06-21 22:41:48 +0800136 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
137 SDHCI_CARD_PRESENT;
138 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
139
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300140 if (enable)
141 sdhci_unmask_irqs(host, irqs);
142 else
143 sdhci_mask_irqs(host, irqs);
144}
145
146static void sdhci_enable_card_detection(struct sdhci_host *host)
147{
148 sdhci_set_card_detection(host, true);
149}
150
151static void sdhci_disable_card_detection(struct sdhci_host *host)
152{
153 sdhci_set_card_detection(host, false);
154}
155
Pierre Ossmand129bce2006-03-24 03:18:17 -0800156static void sdhci_reset(struct sdhci_host *host, u8 mask)
157{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700158 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300159 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700160
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100161 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300162 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700163 SDHCI_CARD_PRESENT))
164 return;
165 }
166
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300167 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
168 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
169
Philip Rakity393c1a32011-01-21 11:26:40 -0800170 if (host->ops->platform_reset_enter)
171 host->ops->platform_reset_enter(host, mask);
172
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300173 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174
Pierre Ossmane16514d82006-06-30 02:22:24 -0700175 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800176 host->clock = 0;
177
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 /* Wait max 100 ms */
179 timeout = 100;
180
181 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300182 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700183 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100184 printk(KERN_ERR "%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700185 mmc_hostname(host->mmc), (int)mask);
186 sdhci_dumpregs(host);
187 return;
188 }
189 timeout--;
190 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300192
Philip Rakity393c1a32011-01-21 11:26:40 -0800193 if (host->ops->platform_reset_exit)
194 host->ops->platform_reset_exit(host, mask);
195
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300196 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
197 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800198}
199
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800200static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
201
202static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800204 if (soft)
205 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
206 else
207 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800208
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300209 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
210 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700211 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
212 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300213 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800214
215 if (soft) {
216 /* force clock reconfiguration */
217 host->clock = 0;
218 sdhci_set_ios(host->mmc, &host->mmc->ios);
219 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300220}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300222static void sdhci_reinit(struct sdhci_host *host)
223{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300225 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
228static void sdhci_activate_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
237static void sdhci_deactivate_led(struct sdhci_host *host)
238{
239 u8 ctrl;
240
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300241 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300243 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800244}
245
Pierre Ossmanf9134312008-12-21 17:01:48 +0100246#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100247static void sdhci_led_control(struct led_classdev *led,
248 enum led_brightness brightness)
249{
250 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
251 unsigned long flags;
252
253 spin_lock_irqsave(&host->lock, flags);
254
255 if (brightness == LED_OFF)
256 sdhci_deactivate_led(host);
257 else
258 sdhci_activate_led(host);
259
260 spin_unlock_irqrestore(&host->lock, flags);
261}
262#endif
263
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264/*****************************************************************************\
265 * *
266 * Core functions *
267 * *
268\*****************************************************************************/
269
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100270static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271{
Pierre Ossman76591502008-07-21 00:32:11 +0200272 unsigned long flags;
273 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700274 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200275 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100277 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100279 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200280 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800281
Pierre Ossman76591502008-07-21 00:32:11 +0200282 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800283
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100284 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200285 if (!sg_miter_next(&host->sg_miter))
286 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800287
Pierre Ossman76591502008-07-21 00:32:11 +0200288 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800289
Pierre Ossman76591502008-07-21 00:32:11 +0200290 blksize -= len;
291 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200292
Pierre Ossman76591502008-07-21 00:32:11 +0200293 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800294
Pierre Ossman76591502008-07-21 00:32:11 +0200295 while (len) {
296 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300297 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200298 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800299 }
Pierre Ossman76591502008-07-21 00:32:11 +0200300
301 *buf = scratch & 0xFF;
302
303 buf++;
304 scratch >>= 8;
305 chunk--;
306 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800307 }
308 }
Pierre Ossman76591502008-07-21 00:32:11 +0200309
310 sg_miter_stop(&host->sg_miter);
311
312 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100313}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800314
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100315static void sdhci_write_block_pio(struct sdhci_host *host)
316{
Pierre Ossman76591502008-07-21 00:32:11 +0200317 unsigned long flags;
318 size_t blksize, len, chunk;
319 u32 scratch;
320 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100321
322 DBG("PIO writing\n");
323
324 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200325 chunk = 0;
326 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100329
330 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200331 if (!sg_miter_next(&host->sg_miter))
332 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 blksize -= len;
337 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100338
Pierre Ossman76591502008-07-21 00:32:11 +0200339 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100340
Pierre Ossman76591502008-07-21 00:32:11 +0200341 while (len) {
342 scratch |= (u32)*buf << (chunk * 8);
343
344 buf++;
345 chunk++;
346 len--;
347
348 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300349 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200350 chunk = 0;
351 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100352 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353 }
354 }
Pierre Ossman76591502008-07-21 00:32:11 +0200355
356 sg_miter_stop(&host->sg_miter);
357
358 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100359}
360
361static void sdhci_transfer_pio(struct sdhci_host *host)
362{
363 u32 mask;
364
365 BUG_ON(!host->data);
366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368 return;
369
370 if (host->data->flags & MMC_DATA_READ)
371 mask = SDHCI_DATA_AVAILABLE;
372 else
373 mask = SDHCI_SPACE_AVAILABLE;
374
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200375 /*
376 * Some controllers (JMicron JMB38x) mess up the buffer bits
377 * for transfers < 4 bytes. As long as it is just one block,
378 * we can ignore the bits.
379 */
380 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
381 (host->data->blocks == 1))
382 mask = ~0;
383
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300384 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300385 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
386 udelay(100);
387
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100388 if (host->data->flags & MMC_DATA_READ)
389 sdhci_read_block_pio(host);
390 else
391 sdhci_write_block_pio(host);
392
Pierre Ossman76591502008-07-21 00:32:11 +0200393 host->blocks--;
394 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 }
397
398 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800399}
400
Pierre Ossman2134a922008-06-28 18:28:51 +0200401static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
402{
403 local_irq_save(*flags);
404 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
405}
406
407static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
408{
409 kunmap_atomic(buffer, KM_BIO_SRC_IRQ);
410 local_irq_restore(*flags);
411}
412
Ben Dooks118cd172010-03-05 13:43:26 -0800413static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
414{
Ben Dooks9e506f32010-03-05 13:43:29 -0800415 __le32 *dataddr = (__le32 __force *)(desc + 4);
416 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800417
Ben Dooks9e506f32010-03-05 13:43:29 -0800418 /* SDHCI specification says ADMA descriptors should be 4 byte
419 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800420
Ben Dooks9e506f32010-03-05 13:43:29 -0800421 cmdlen[0] = cpu_to_le16(cmd);
422 cmdlen[1] = cpu_to_le16(len);
423
424 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800425}
426
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200427static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200428 struct mmc_data *data)
429{
430 int direction;
431
432 u8 *desc;
433 u8 *align;
434 dma_addr_t addr;
435 dma_addr_t align_addr;
436 int len, offset;
437
438 struct scatterlist *sg;
439 int i;
440 char *buffer;
441 unsigned long flags;
442
443 /*
444 * The spec does not specify endianness of descriptor table.
445 * We currently guess that it is LE.
446 */
447
448 if (data->flags & MMC_DATA_READ)
449 direction = DMA_FROM_DEVICE;
450 else
451 direction = DMA_TO_DEVICE;
452
453 /*
454 * The ADMA descriptor table is mapped further down as we
455 * need to fill it with data first.
456 */
457
458 host->align_addr = dma_map_single(mmc_dev(host->mmc),
459 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700460 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200461 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200462 BUG_ON(host->align_addr & 0x3);
463
464 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
465 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200466 if (host->sg_count == 0)
467 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200468
469 desc = host->adma_desc;
470 align = host->align_buffer;
471
472 align_addr = host->align_addr;
473
474 for_each_sg(data->sg, sg, host->sg_count, i) {
475 addr = sg_dma_address(sg);
476 len = sg_dma_len(sg);
477
478 /*
479 * The SDHCI specification states that ADMA
480 * addresses must be 32-bit aligned. If they
481 * aren't, then we use a bounce buffer for
482 * the (up to three) bytes that screw up the
483 * alignment.
484 */
485 offset = (4 - (addr & 0x3)) & 0x3;
486 if (offset) {
487 if (data->flags & MMC_DATA_WRITE) {
488 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200489 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200490 memcpy(align, buffer, offset);
491 sdhci_kunmap_atomic(buffer, &flags);
492 }
493
Ben Dooks118cd172010-03-05 13:43:26 -0800494 /* tran, valid */
495 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200496
497 BUG_ON(offset > 65536);
498
Pierre Ossman2134a922008-06-28 18:28:51 +0200499 align += 4;
500 align_addr += 4;
501
502 desc += 8;
503
504 addr += offset;
505 len -= offset;
506 }
507
Pierre Ossman2134a922008-06-28 18:28:51 +0200508 BUG_ON(len > 65536);
509
Ben Dooks118cd172010-03-05 13:43:26 -0800510 /* tran, valid */
511 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200512 desc += 8;
513
514 /*
515 * If this triggers then we have a calculation bug
516 * somewhere. :/
517 */
518 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
519 }
520
Thomas Abraham70764a92010-05-26 14:42:04 -0700521 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
522 /*
523 * Mark the last descriptor as the terminating descriptor
524 */
525 if (desc != host->adma_desc) {
526 desc -= 8;
527 desc[0] |= 0x2; /* end */
528 }
529 } else {
530 /*
531 * Add a terminating entry.
532 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200533
Thomas Abraham70764a92010-05-26 14:42:04 -0700534 /* nop, end, valid */
535 sdhci_set_adma_desc(desc, 0, 0, 0x3);
536 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
538 /*
539 * Resync align buffer as we might have changed it.
540 */
541 if (data->flags & MMC_DATA_WRITE) {
542 dma_sync_single_for_device(mmc_dev(host->mmc),
543 host->align_addr, 128 * 4, direction);
544 }
545
546 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
547 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200548 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200549 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200551
552 return 0;
553
554unmap_entries:
555 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
556 data->sg_len, direction);
557unmap_align:
558 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
559 128 * 4, direction);
560fail:
561 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200562}
563
564static void sdhci_adma_table_post(struct sdhci_host *host,
565 struct mmc_data *data)
566{
567 int direction;
568
569 struct scatterlist *sg;
570 int i, size;
571 u8 *align;
572 char *buffer;
573 unsigned long flags;
574
575 if (data->flags & MMC_DATA_READ)
576 direction = DMA_FROM_DEVICE;
577 else
578 direction = DMA_TO_DEVICE;
579
580 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
581 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
582
583 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
584 128 * 4, direction);
585
586 if (data->flags & MMC_DATA_READ) {
587 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
588 data->sg_len, direction);
589
590 align = host->align_buffer;
591
592 for_each_sg(data->sg, sg, host->sg_count, i) {
593 if (sg_dma_address(sg) & 0x3) {
594 size = 4 - (sg_dma_address(sg) & 0x3);
595
596 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200597 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200598 memcpy(buffer, align, size);
599 sdhci_kunmap_atomic(buffer, &flags);
600
601 align += 4;
602 }
603 }
604 }
605
606 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
607 data->sg_len, direction);
608}
609
Andrei Warkentina3c77782011-04-11 16:13:42 -0500610static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800611{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700612 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500613 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700614 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800615
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200616 /*
617 * If the host controller provides us with an incorrect timeout
618 * value, just skip the check and use 0xE. The hardware may take
619 * longer to time out, but that's much better than having a too-short
620 * timeout value.
621 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200622 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200623 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200624
Andrei Warkentina3c77782011-04-11 16:13:42 -0500625 /* Unspecified timeout, assume max */
626 if (!data && !cmd->cmd_timeout_ms)
627 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800628
Andrei Warkentina3c77782011-04-11 16:13:42 -0500629 /* timeout in us */
630 if (!data)
631 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300632 else {
633 target_timeout = data->timeout_ns / 1000;
634 if (host->clock)
635 target_timeout += data->timeout_clks / host->clock;
636 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700637
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700638 /*
639 * Figure out needed cycles.
640 * We do this in steps in order to fit inside a 32 bit int.
641 * The first step is the minimum timeout, which will have a
642 * minimum resolution of 6 bits:
643 * (1) 2^13*1000 > 2^22,
644 * (2) host->timeout_clk < 2^16
645 * =>
646 * (1) / (2) > 2^6
647 */
648 count = 0;
649 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
650 while (current_timeout < target_timeout) {
651 count++;
652 current_timeout <<= 1;
653 if (count >= 0xF)
654 break;
655 }
656
657 if (count >= 0xF) {
Andrei Warkentina3c77782011-04-11 16:13:42 -0500658 printk(KERN_WARNING "%s: Too large timeout requested for CMD%d!\n",
659 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700660 count = 0xE;
661 }
662
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200663 return count;
664}
665
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300666static void sdhci_set_transfer_irqs(struct sdhci_host *host)
667{
668 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
669 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
670
671 if (host->flags & SDHCI_REQ_USE_DMA)
672 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
673 else
674 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
675}
676
Andrei Warkentina3c77782011-04-11 16:13:42 -0500677static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200678{
679 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200680 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500681 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200682 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200683
684 WARN_ON(host->data);
685
Andrei Warkentina3c77782011-04-11 16:13:42 -0500686 if (data || (cmd->flags & MMC_RSP_BUSY)) {
687 count = sdhci_calc_timeout(host, cmd);
688 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
689 }
690
691 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200692 return;
693
694 /* Sanity checks */
695 BUG_ON(data->blksz * data->blocks > 524288);
696 BUG_ON(data->blksz > host->mmc->max_blk_size);
697 BUG_ON(data->blocks > 65535);
698
699 host->data = data;
700 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400701 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200702
Richard Röjforsa13abc72009-09-22 16:45:30 -0700703 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100704 host->flags |= SDHCI_REQ_USE_DMA;
705
Pierre Ossman2134a922008-06-28 18:28:51 +0200706 /*
707 * FIXME: This doesn't account for merging when mapping the
708 * scatterlist.
709 */
710 if (host->flags & SDHCI_REQ_USE_DMA) {
711 int broken, i;
712 struct scatterlist *sg;
713
714 broken = 0;
715 if (host->flags & SDHCI_USE_ADMA) {
716 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
717 broken = 1;
718 } else {
719 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
720 broken = 1;
721 }
722
723 if (unlikely(broken)) {
724 for_each_sg(data->sg, sg, data->sg_len, i) {
725 if (sg->length & 0x3) {
726 DBG("Reverting to PIO because of "
727 "transfer size (%d)\n",
728 sg->length);
729 host->flags &= ~SDHCI_REQ_USE_DMA;
730 break;
731 }
732 }
733 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100734 }
735
736 /*
737 * The assumption here being that alignment is the same after
738 * translation to device address space.
739 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200740 if (host->flags & SDHCI_REQ_USE_DMA) {
741 int broken, i;
742 struct scatterlist *sg;
743
744 broken = 0;
745 if (host->flags & SDHCI_USE_ADMA) {
746 /*
747 * As we use 3 byte chunks to work around
748 * alignment problems, we need to check this
749 * quirk.
750 */
751 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
752 broken = 1;
753 } else {
754 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
755 broken = 1;
756 }
757
758 if (unlikely(broken)) {
759 for_each_sg(data->sg, sg, data->sg_len, i) {
760 if (sg->offset & 0x3) {
761 DBG("Reverting to PIO because of "
762 "bad alignment\n");
763 host->flags &= ~SDHCI_REQ_USE_DMA;
764 break;
765 }
766 }
767 }
768 }
769
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200770 if (host->flags & SDHCI_REQ_USE_DMA) {
771 if (host->flags & SDHCI_USE_ADMA) {
772 ret = sdhci_adma_table_pre(host, data);
773 if (ret) {
774 /*
775 * This only happens when someone fed
776 * us an invalid request.
777 */
778 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200779 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200780 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300781 sdhci_writel(host, host->adma_addr,
782 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200783 }
784 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300785 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200786
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300787 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200788 data->sg, data->sg_len,
789 (data->flags & MMC_DATA_READ) ?
790 DMA_FROM_DEVICE :
791 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300792 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200793 /*
794 * This only happens when someone fed
795 * us an invalid request.
796 */
797 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200798 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200799 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200800 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300801 sdhci_writel(host, sg_dma_address(data->sg),
802 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200803 }
804 }
805 }
806
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 /*
808 * Always adjust the DMA selection as some controllers
809 * (e.g. JMicron) can't do PIO properly when the selection
810 * is ADMA.
811 */
812 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300813 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200814 ctrl &= ~SDHCI_CTRL_DMA_MASK;
815 if ((host->flags & SDHCI_REQ_USE_DMA) &&
816 (host->flags & SDHCI_USE_ADMA))
817 ctrl |= SDHCI_CTRL_ADMA32;
818 else
819 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300820 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100821 }
822
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200823 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200824 int flags;
825
826 flags = SG_MITER_ATOMIC;
827 if (host->data->flags & MMC_DATA_READ)
828 flags |= SG_MITER_TO_SG;
829 else
830 flags |= SG_MITER_FROM_SG;
831 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200832 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800833 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700834
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300835 sdhci_set_transfer_irqs(host);
836
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400837 /* Set the DMA boundary value and block size */
838 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
839 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300840 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700841}
842
843static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500844 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700845{
846 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500847 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700848
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700849 if (data == NULL)
850 return;
851
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200852 WARN_ON(!host->data);
853
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700854 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500855 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
856 mode |= SDHCI_TRNS_MULTI;
857 /*
858 * If we are sending CMD23, CMD12 never gets sent
859 * on successful completion (so no Auto-CMD12).
860 */
861 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
862 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500863 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
864 mode |= SDHCI_TRNS_AUTO_CMD23;
865 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
866 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700867 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500868
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700869 if (data->flags & MMC_DATA_READ)
870 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100871 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700872 mode |= SDHCI_TRNS_DMA;
873
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300874 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800875}
876
877static void sdhci_finish_data(struct sdhci_host *host)
878{
879 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800880
881 BUG_ON(!host->data);
882
883 data = host->data;
884 host->data = NULL;
885
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100886 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200887 if (host->flags & SDHCI_USE_ADMA)
888 sdhci_adma_table_post(host, data);
889 else {
890 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
891 data->sg_len, (data->flags & MMC_DATA_READ) ?
892 DMA_FROM_DEVICE : DMA_TO_DEVICE);
893 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800894 }
895
896 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200897 * The specification states that the block count register must
898 * be updated, but it does not specify at what point in the
899 * data flow. That makes the register entirely useless to read
900 * back so we have to assume that nothing made it to the card
901 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800902 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200903 if (data->error)
904 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800905 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200906 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800907
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 /*
909 * Need to send CMD12 if -
910 * a) open-ended multiblock transfer (no CMD23)
911 * b) error in multiblock transfer
912 */
913 if (data->stop &&
914 (data->error ||
915 !host->mrq->sbc)) {
916
Pierre Ossmand129bce2006-03-24 03:18:17 -0800917 /*
918 * The controller needs a reset of internal state machines
919 * upon error conditions.
920 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200921 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800922 sdhci_reset(host, SDHCI_RESET_CMD);
923 sdhci_reset(host, SDHCI_RESET_DATA);
924 }
925
926 sdhci_send_command(host, data->stop);
927 } else
928 tasklet_schedule(&host->finish_tasklet);
929}
930
931static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
932{
933 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700934 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700935 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800936
937 WARN_ON(host->cmd);
938
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700940 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700941
942 mask = SDHCI_CMD_INHIBIT;
943 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
944 mask |= SDHCI_DATA_INHIBIT;
945
946 /* We shouldn't wait for data inihibit for stop commands, even
947 though they might use busy signaling */
948 if (host->mrq->data && (cmd == host->mrq->data->stop))
949 mask &= ~SDHCI_DATA_INHIBIT;
950
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300951 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700952 if (timeout == 0) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953 printk(KERN_ERR "%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100954 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200956 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 tasklet_schedule(&host->finish_tasklet);
958 return;
959 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700960 timeout--;
961 mdelay(1);
962 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963
964 mod_timer(&host->timer, jiffies + 10 * HZ);
965
966 host->cmd = cmd;
967
Andrei Warkentina3c77782011-04-11 16:13:42 -0500968 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800969
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300970 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800971
Andrei Warkentine89d4562011-05-23 15:06:37 -0500972 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700973
Pierre Ossmand129bce2006-03-24 03:18:17 -0800974 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100975 printk(KERN_ERR "%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200977 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978 tasklet_schedule(&host->finish_tasklet);
979 return;
980 }
981
982 if (!(cmd->flags & MMC_RSP_PRESENT))
983 flags = SDHCI_CMD_RESP_NONE;
984 else if (cmd->flags & MMC_RSP_136)
985 flags = SDHCI_CMD_RESP_LONG;
986 else if (cmd->flags & MMC_RSP_BUSY)
987 flags = SDHCI_CMD_RESP_SHORT_BUSY;
988 else
989 flags = SDHCI_CMD_RESP_SHORT;
990
991 if (cmd->flags & MMC_RSP_CRC)
992 flags |= SDHCI_CMD_CRC;
993 if (cmd->flags & MMC_RSP_OPCODE)
994 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +0530995
996 /* CMD19 is special in that the Data Present Select should be set */
997 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 flags |= SDHCI_CMD_DATA;
999
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001000 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001}
1002
1003static void sdhci_finish_command(struct sdhci_host *host)
1004{
1005 int i;
1006
1007 BUG_ON(host->cmd == NULL);
1008
1009 if (host->cmd->flags & MMC_RSP_PRESENT) {
1010 if (host->cmd->flags & MMC_RSP_136) {
1011 /* CRC is stripped so we need to do some shifting. */
1012 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001013 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 SDHCI_RESPONSE + (3-i)*4) << 8;
1015 if (i != 3)
1016 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001017 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 SDHCI_RESPONSE + (3-i)*4-1);
1019 }
1020 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001021 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022 }
1023 }
1024
Pierre Ossman17b04292007-07-22 22:18:46 +02001025 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Andrei Warkentine89d4562011-05-23 15:06:37 -05001027 /* Finished CMD23, now send actual command. */
1028 if (host->cmd == host->mrq->sbc) {
1029 host->cmd = NULL;
1030 sdhci_send_command(host, host->mrq->cmd);
1031 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001032
Andrei Warkentine89d4562011-05-23 15:06:37 -05001033 /* Processed actual command. */
1034 if (host->data && host->data_early)
1035 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036
Andrei Warkentine89d4562011-05-23 15:06:37 -05001037 if (!host->cmd->data)
1038 tasklet_schedule(&host->finish_tasklet);
1039
1040 host->cmd = NULL;
1041 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042}
1043
1044static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1045{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301046 int div = 0; /* Initialized for compiler warning */
1047 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001048 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001049
1050 if (clock == host->clock)
1051 return;
1052
Anton Vorontsov81146342009-03-17 00:13:59 +03001053 if (host->ops->set_clock) {
1054 host->ops->set_clock(host, clock);
1055 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1056 return;
1057 }
1058
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001059 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060
1061 if (clock == 0)
1062 goto out;
1063
Zhangfei Gao85105c52010-08-06 07:10:01 +08001064 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301065 /*
1066 * Check if the Host Controller supports Programmable Clock
1067 * Mode.
1068 */
1069 if (host->clk_mul) {
1070 u16 ctrl;
1071
1072 /*
1073 * We need to figure out whether the Host Driver needs
1074 * to select Programmable Clock Mode, or the value can
1075 * be set automatically by the Host Controller based on
1076 * the Preset Value registers.
1077 */
1078 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1079 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1080 for (div = 1; div <= 1024; div++) {
1081 if (((host->max_clk * host->clk_mul) /
1082 div) <= clock)
1083 break;
1084 }
1085 /*
1086 * Set Programmable Clock Mode in the Clock
1087 * Control register.
1088 */
1089 clk = SDHCI_PROG_CLOCK_MODE;
1090 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001091 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301092 } else {
1093 /* Version 3.00 divisors must be a multiple of 2. */
1094 if (host->max_clk <= clock)
1095 div = 1;
1096 else {
1097 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1098 div += 2) {
1099 if ((host->max_clk / div) <= clock)
1100 break;
1101 }
1102 }
1103 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001104 }
1105 } else {
1106 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001107 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001108 if ((host->max_clk / div) <= clock)
1109 break;
1110 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301111 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001112 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113
Arindam Nathc3ed3872011-05-05 12:19:06 +05301114 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001115 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1116 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001118 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119
Chris Ball27f6cb12009-09-22 16:45:31 -07001120 /* Wait max 20 ms */
1121 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001122 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001123 & SDHCI_CLOCK_INT_STABLE)) {
1124 if (timeout == 0) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001125 printk(KERN_ERR "%s: Internal clock never "
1126 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127 sdhci_dumpregs(host);
1128 return;
1129 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001130 timeout--;
1131 mdelay(1);
1132 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
1134 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001135 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
1137out:
1138 host->clock = clock;
1139}
1140
Pierre Ossman146ad662006-06-30 02:22:23 -07001141static void sdhci_set_power(struct sdhci_host *host, unsigned short power)
1142{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001143 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001144
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001145 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001146 switch (1 << power) {
1147 case MMC_VDD_165_195:
1148 pwr = SDHCI_POWER_180;
1149 break;
1150 case MMC_VDD_29_30:
1151 case MMC_VDD_30_31:
1152 pwr = SDHCI_POWER_300;
1153 break;
1154 case MMC_VDD_32_33:
1155 case MMC_VDD_33_34:
1156 pwr = SDHCI_POWER_330;
1157 break;
1158 default:
1159 BUG();
1160 }
1161 }
1162
1163 if (host->pwr == pwr)
Pierre Ossman146ad662006-06-30 02:22:23 -07001164 return;
1165
Pierre Ossmanae628902009-05-03 20:45:03 +02001166 host->pwr = pwr;
1167
1168 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001169 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossmanae628902009-05-03 20:45:03 +02001170 return;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001171 }
1172
1173 /*
1174 * Spec says that we should clear the power reg before setting
1175 * a new value. Some controllers don't seem to like this though.
1176 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001177 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001178 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001179
Andres Salomone08c1692008-07-04 10:00:03 -07001180 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001181 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001182 * and set turn on power at the same time, so set the voltage first.
1183 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001184 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001185 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1186
1187 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001188
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001189 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001190
1191 /*
1192 * Some controllers need an extra 10ms delay of 10ms before they
1193 * can apply clock after applying power
1194 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001195 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001196 mdelay(10);
Pierre Ossman146ad662006-06-30 02:22:23 -07001197}
1198
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199/*****************************************************************************\
1200 * *
1201 * MMC callbacks *
1202 * *
1203\*****************************************************************************/
1204
1205static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1206{
1207 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001208 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209 unsigned long flags;
1210
1211 host = mmc_priv(mmc);
1212
1213 spin_lock_irqsave(&host->lock, flags);
1214
1215 WARN_ON(host->mrq != NULL);
1216
Pierre Ossmanf9134312008-12-21 17:01:48 +01001217#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001218 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001219#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001220
1221 /*
1222 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1223 * requests if Auto-CMD12 is enabled.
1224 */
1225 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001226 if (mrq->stop) {
1227 mrq->data->stop = NULL;
1228 mrq->stop = NULL;
1229 }
1230 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231
1232 host->mrq = mrq;
1233
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001234 /* If polling, assume that the card is always present. */
1235 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1236 present = true;
1237 else
1238 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1239 SDHCI_CARD_PRESENT;
1240
1241 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001242 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301244 } else {
1245 u32 present_state;
1246
1247 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1248 /*
1249 * Check if the re-tuning timer has already expired and there
1250 * is no on-going data transfer. If so, we need to execute
1251 * tuning procedure before sending command.
1252 */
1253 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1254 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1255 spin_unlock_irqrestore(&host->lock, flags);
1256 sdhci_execute_tuning(mmc);
1257 spin_lock_irqsave(&host->lock, flags);
1258
1259 /* Restore original mmc_request structure */
1260 host->mrq = mrq;
1261 }
1262
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001263 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001264 sdhci_send_command(host, mrq->sbc);
1265 else
1266 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301267 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268
Pierre Ossman5f25a662006-10-04 02:15:39 -07001269 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270 spin_unlock_irqrestore(&host->lock, flags);
1271}
1272
1273static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1274{
1275 struct sdhci_host *host;
1276 unsigned long flags;
1277 u8 ctrl;
1278
1279 host = mmc_priv(mmc);
1280
1281 spin_lock_irqsave(&host->lock, flags);
1282
Pierre Ossman1e728592008-04-16 19:13:13 +02001283 if (host->flags & SDHCI_DEVICE_DEAD)
1284 goto out;
1285
Pierre Ossmand129bce2006-03-24 03:18:17 -08001286 /*
1287 * Reset the chip on each power off.
1288 * Should clear out any weird states.
1289 */
1290 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001291 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001292 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001293 }
1294
1295 sdhci_set_clock(host, ios->clock);
1296
1297 if (ios->power_mode == MMC_POWER_OFF)
Pierre Ossman146ad662006-06-30 02:22:23 -07001298 sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001299 else
Pierre Ossman146ad662006-06-30 02:22:23 -07001300 sdhci_set_power(host, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001301
Philip Rakity643a81f2010-09-23 08:24:32 -07001302 if (host->ops->platform_send_init_74_clocks)
1303 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1304
Philip Rakity15ec4462010-11-19 16:48:39 -05001305 /*
1306 * If your platform has 8-bit width support but is not a v3 controller,
1307 * or if it requires special setup code, you should implement that in
1308 * platform_8bit_width().
1309 */
1310 if (host->ops->platform_8bit_width)
1311 host->ops->platform_8bit_width(host, ios->bus_width);
1312 else {
1313 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1314 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1315 ctrl &= ~SDHCI_CTRL_4BITBUS;
1316 if (host->version >= SDHCI_SPEC_300)
1317 ctrl |= SDHCI_CTRL_8BITBUS;
1318 } else {
1319 if (host->version >= SDHCI_SPEC_300)
1320 ctrl &= ~SDHCI_CTRL_8BITBUS;
1321 if (ios->bus_width == MMC_BUS_WIDTH_4)
1322 ctrl |= SDHCI_CTRL_4BITBUS;
1323 else
1324 ctrl &= ~SDHCI_CTRL_4BITBUS;
1325 }
1326 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1327 }
1328
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001329 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001330
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001331 if ((ios->timing == MMC_TIMING_SD_HS ||
1332 ios->timing == MMC_TIMING_MMC_HS)
1333 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001334 ctrl |= SDHCI_CTRL_HISPD;
1335 else
1336 ctrl &= ~SDHCI_CTRL_HISPD;
1337
Arindam Nathd6d50a12011-05-05 12:18:59 +05301338 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301339 u16 clk, ctrl_2;
1340 unsigned int clock;
1341
1342 /* In case of UHS-I modes, set High Speed Enable */
1343 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1344 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1345 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1346 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1347 (ios->timing == MMC_TIMING_UHS_SDR12))
1348 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301349
1350 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1351 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301352 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301353 /*
1354 * We only need to set Driver Strength if the
1355 * preset value enable is not set.
1356 */
1357 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1358 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1359 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1360 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1361 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1362
1363 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301364 } else {
1365 /*
1366 * According to SDHC Spec v3.00, if the Preset Value
1367 * Enable in the Host Control 2 register is set, we
1368 * need to reset SD Clock Enable before changing High
1369 * Speed Enable to avoid generating clock gliches.
1370 */
Arindam Nath758535c2011-05-05 12:19:00 +05301371
1372 /* Reset SD Clock Enable */
1373 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1374 clk &= ~SDHCI_CLOCK_CARD_EN;
1375 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1376
1377 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1378
1379 /* Re-enable SD Clock */
1380 clock = host->clock;
1381 host->clock = 0;
1382 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301383 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301384
Arindam Nath49c468f2011-05-05 12:19:01 +05301385
1386 /* Reset SD Clock Enable */
1387 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1388 clk &= ~SDHCI_CLOCK_CARD_EN;
1389 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1390
Philip Rakity6322cdd2011-05-13 11:17:15 +05301391 if (host->ops->set_uhs_signaling)
1392 host->ops->set_uhs_signaling(host, ios->timing);
1393 else {
1394 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1395 /* Select Bus Speed Mode for host */
1396 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1397 if (ios->timing == MMC_TIMING_UHS_SDR12)
1398 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1399 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1400 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1401 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1402 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1403 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1404 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1405 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1406 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1407 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1408 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301409
1410 /* Re-enable SD Clock */
1411 clock = host->clock;
1412 host->clock = 0;
1413 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301414 } else
1415 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301416
Leandro Dorileob8352262007-07-25 23:47:04 +02001417 /*
1418 * Some (ENE) controllers go apeshit on some ios operation,
1419 * signalling timeout and CRC errors even on CMD0. Resetting
1420 * it on each ios seems to solve the problem.
1421 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001422 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001423 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1424
Pierre Ossman1e728592008-04-16 19:13:13 +02001425out:
Pierre Ossman5f25a662006-10-04 02:15:39 -07001426 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001427 spin_unlock_irqrestore(&host->lock, flags);
1428}
1429
Takashi Iwai82b0e232011-04-21 20:26:38 +02001430static int check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001431{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001432 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001433 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001434
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435 spin_lock_irqsave(&host->lock, flags);
1436
Pierre Ossman1e728592008-04-16 19:13:13 +02001437 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001438 is_readonly = 0;
1439 else if (host->ops->get_ro)
1440 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001441 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001442 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1443 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001444
1445 spin_unlock_irqrestore(&host->lock, flags);
1446
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001447 /* This quirk needs to be replaced by a callback-function later */
1448 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1449 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001450}
1451
Takashi Iwai82b0e232011-04-21 20:26:38 +02001452#define SAMPLE_COUNT 5
1453
1454static int sdhci_get_ro(struct mmc_host *mmc)
1455{
1456 struct sdhci_host *host;
1457 int i, ro_count;
1458
1459 host = mmc_priv(mmc);
1460
1461 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
1462 return check_ro(host);
1463
1464 ro_count = 0;
1465 for (i = 0; i < SAMPLE_COUNT; i++) {
1466 if (check_ro(host)) {
1467 if (++ro_count > SAMPLE_COUNT / 2)
1468 return 1;
1469 }
1470 msleep(30);
1471 }
1472 return 0;
1473}
1474
Adrian Hunter20758b62011-08-29 16:42:12 +03001475static void sdhci_hw_reset(struct mmc_host *mmc)
1476{
1477 struct sdhci_host *host = mmc_priv(mmc);
1478
1479 if (host->ops && host->ops->hw_reset)
1480 host->ops->hw_reset(host);
1481}
1482
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001483static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1484{
1485 struct sdhci_host *host;
1486 unsigned long flags;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001487
1488 host = mmc_priv(mmc);
1489
1490 spin_lock_irqsave(&host->lock, flags);
1491
Pierre Ossman1e728592008-04-16 19:13:13 +02001492 if (host->flags & SDHCI_DEVICE_DEAD)
1493 goto out;
1494
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001495 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001496 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1497 else
1498 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001499out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001500 mmiowb();
1501
1502 spin_unlock_irqrestore(&host->lock, flags);
1503}
1504
Arindam Nathf2119df2011-05-05 12:18:57 +05301505static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1506 struct mmc_ios *ios)
1507{
1508 struct sdhci_host *host;
1509 u8 pwr;
1510 u16 clk, ctrl;
1511 u32 present_state;
1512
1513 host = mmc_priv(mmc);
1514
1515 /*
1516 * Signal Voltage Switching is only applicable for Host Controllers
1517 * v3.00 and above.
1518 */
1519 if (host->version < SDHCI_SPEC_300)
1520 return 0;
1521
1522 /*
1523 * We first check whether the request is to set signalling voltage
1524 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1525 */
1526 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1527 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1528 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1529 ctrl &= ~SDHCI_CTRL_VDD_180;
1530 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1531
1532 /* Wait for 5ms */
1533 usleep_range(5000, 5500);
1534
1535 /* 3.3V regulator output should be stable within 5 ms */
1536 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1537 if (!(ctrl & SDHCI_CTRL_VDD_180))
1538 return 0;
1539 else {
1540 printk(KERN_INFO DRIVER_NAME ": Switching to 3.3V "
1541 "signalling voltage failed\n");
1542 return -EIO;
1543 }
1544 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1545 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1546 /* Stop SDCLK */
1547 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1548 clk &= ~SDHCI_CLOCK_CARD_EN;
1549 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1550
1551 /* Check whether DAT[3:0] is 0000 */
1552 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1553 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1554 SDHCI_DATA_LVL_SHIFT)) {
1555 /*
1556 * Enable 1.8V Signal Enable in the Host Control2
1557 * register
1558 */
1559 ctrl |= SDHCI_CTRL_VDD_180;
1560 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1561
1562 /* Wait for 5ms */
1563 usleep_range(5000, 5500);
1564
1565 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1566 if (ctrl & SDHCI_CTRL_VDD_180) {
1567 /* Provide SDCLK again and wait for 1ms*/
1568 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1569 clk |= SDHCI_CLOCK_CARD_EN;
1570 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1571 usleep_range(1000, 1500);
1572
1573 /*
1574 * If DAT[3:0] level is 1111b, then the card
1575 * was successfully switched to 1.8V signaling.
1576 */
1577 present_state = sdhci_readl(host,
1578 SDHCI_PRESENT_STATE);
1579 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1580 SDHCI_DATA_LVL_MASK)
1581 return 0;
1582 }
1583 }
1584
1585 /*
1586 * If we are here, that means the switch to 1.8V signaling
1587 * failed. We power cycle the card, and retry initialization
1588 * sequence by setting S18R to 0.
1589 */
1590 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1591 pwr &= ~SDHCI_POWER_ON;
1592 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1593
1594 /* Wait for 1ms as per the spec */
1595 usleep_range(1000, 1500);
1596 pwr |= SDHCI_POWER_ON;
1597 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1598
1599 printk(KERN_INFO DRIVER_NAME ": Switching to 1.8V signalling "
1600 "voltage failed, retrying with S18R set to 0\n");
1601 return -EAGAIN;
1602 } else
1603 /* No signal voltage switch required */
1604 return 0;
1605}
1606
Arindam Nathb513ea22011-05-05 12:19:04 +05301607static int sdhci_execute_tuning(struct mmc_host *mmc)
1608{
1609 struct sdhci_host *host;
1610 u16 ctrl;
1611 u32 ier;
1612 int tuning_loop_counter = MAX_TUNING_LOOP;
1613 unsigned long timeout;
1614 int err = 0;
1615
1616 host = mmc_priv(mmc);
1617
1618 disable_irq(host->irq);
1619 spin_lock(&host->lock);
1620
1621 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1622
1623 /*
1624 * Host Controller needs tuning only in case of SDR104 mode
1625 * and for SDR50 mode when Use Tuning for SDR50 is set in
1626 * Capabilities register.
1627 */
1628 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1629 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1630 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1631 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1632 else {
1633 spin_unlock(&host->lock);
1634 enable_irq(host->irq);
1635 return 0;
1636 }
1637
1638 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1639
1640 /*
1641 * As per the Host Controller spec v3.00, tuning command
1642 * generates Buffer Read Ready interrupt, so enable that.
1643 *
1644 * Note: The spec clearly says that when tuning sequence
1645 * is being performed, the controller does not generate
1646 * interrupts other than Buffer Read Ready interrupt. But
1647 * to make sure we don't hit a controller bug, we _only_
1648 * enable Buffer Read Ready interrupt here.
1649 */
1650 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1651 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1652
1653 /*
1654 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1655 * of loops reaches 40 times or a timeout of 150ms occurs.
1656 */
1657 timeout = 150;
1658 do {
1659 struct mmc_command cmd = {0};
1660 struct mmc_request mrq = {0};
1661
1662 if (!tuning_loop_counter && !timeout)
1663 break;
1664
1665 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1666 cmd.arg = 0;
1667 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1668 cmd.retries = 0;
1669 cmd.data = NULL;
1670 cmd.error = 0;
1671
1672 mrq.cmd = &cmd;
1673 host->mrq = &mrq;
1674
1675 /*
1676 * In response to CMD19, the card sends 64 bytes of tuning
1677 * block to the Host Controller. So we set the block size
1678 * to 64 here.
1679 */
1680 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1681
1682 /*
1683 * The tuning block is sent by the card to the host controller.
1684 * So we set the TRNS_READ bit in the Transfer Mode register.
1685 * This also takes care of setting DMA Enable and Multi Block
1686 * Select in the same register to 0.
1687 */
1688 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1689
1690 sdhci_send_command(host, &cmd);
1691
1692 host->cmd = NULL;
1693 host->mrq = NULL;
1694
1695 spin_unlock(&host->lock);
1696 enable_irq(host->irq);
1697
1698 /* Wait for Buffer Read Ready interrupt */
1699 wait_event_interruptible_timeout(host->buf_ready_int,
1700 (host->tuning_done == 1),
1701 msecs_to_jiffies(50));
1702 disable_irq(host->irq);
1703 spin_lock(&host->lock);
1704
1705 if (!host->tuning_done) {
1706 printk(KERN_INFO DRIVER_NAME ": Timeout waiting for "
1707 "Buffer Read Ready interrupt during tuning "
1708 "procedure, falling back to fixed sampling "
1709 "clock\n");
1710 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1711 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1712 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1713 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1714
1715 err = -EIO;
1716 goto out;
1717 }
1718
1719 host->tuning_done = 0;
1720
1721 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1722 tuning_loop_counter--;
1723 timeout--;
1724 mdelay(1);
1725 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1726
1727 /*
1728 * The Host Driver has exhausted the maximum number of loops allowed,
1729 * so use fixed sampling frequency.
1730 */
1731 if (!tuning_loop_counter || !timeout) {
1732 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1733 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1734 } else {
1735 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
1736 printk(KERN_INFO DRIVER_NAME ": Tuning procedure"
1737 " failed, falling back to fixed sampling"
1738 " clock\n");
1739 err = -EIO;
1740 }
1741 }
1742
1743out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301744 /*
1745 * If this is the very first time we are here, we start the retuning
1746 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1747 * flag won't be set, we check this condition before actually starting
1748 * the timer.
1749 */
1750 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1751 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1752 mod_timer(&host->tuning_timer, jiffies +
1753 host->tuning_count * HZ);
1754 /* Tuning mode 1 limits the maximum data length to 4MB */
1755 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1756 } else {
1757 host->flags &= ~SDHCI_NEEDS_RETUNING;
1758 /* Reload the new initial value for timer */
1759 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1760 mod_timer(&host->tuning_timer, jiffies +
1761 host->tuning_count * HZ);
1762 }
1763
1764 /*
1765 * In case tuning fails, host controllers which support re-tuning can
1766 * try tuning again at a later time, when the re-tuning timer expires.
1767 * So for these controllers, we return 0. Since there might be other
1768 * controllers who do not have this capability, we return error for
1769 * them.
1770 */
1771 if (err && host->tuning_count &&
1772 host->tuning_mode == SDHCI_TUNING_MODE_1)
1773 err = 0;
1774
Arindam Nathb513ea22011-05-05 12:19:04 +05301775 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1776 spin_unlock(&host->lock);
1777 enable_irq(host->irq);
1778
1779 return err;
1780}
1781
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301782static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1783{
1784 struct sdhci_host *host;
1785 u16 ctrl;
1786 unsigned long flags;
1787
1788 host = mmc_priv(mmc);
1789
1790 /* Host Controller v3.00 defines preset value registers */
1791 if (host->version < SDHCI_SPEC_300)
1792 return;
1793
1794 spin_lock_irqsave(&host->lock, flags);
1795
1796 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1797
1798 /*
1799 * We only enable or disable Preset Value if they are not already
1800 * enabled or disabled respectively. Otherwise, we bail out.
1801 */
1802 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1803 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1804 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1805 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1806 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1807 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1808 }
1809
1810 spin_unlock_irqrestore(&host->lock, flags);
1811}
1812
David Brownellab7aefd2006-11-12 17:55:30 -08001813static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001814 .request = sdhci_request,
1815 .set_ios = sdhci_set_ios,
1816 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001817 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001818 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301819 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301820 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301821 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001822};
1823
1824/*****************************************************************************\
1825 * *
1826 * Tasklets *
1827 * *
1828\*****************************************************************************/
1829
1830static void sdhci_tasklet_card(unsigned long param)
1831{
1832 struct sdhci_host *host;
1833 unsigned long flags;
1834
1835 host = (struct sdhci_host*)param;
1836
1837 spin_lock_irqsave(&host->lock, flags);
1838
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001839 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001840 if (host->mrq) {
1841 printk(KERN_ERR "%s: Card removed during transfer!\n",
1842 mmc_hostname(host->mmc));
1843 printk(KERN_ERR "%s: Resetting controller.\n",
1844 mmc_hostname(host->mmc));
1845
1846 sdhci_reset(host, SDHCI_RESET_CMD);
1847 sdhci_reset(host, SDHCI_RESET_DATA);
1848
Pierre Ossman17b04292007-07-22 22:18:46 +02001849 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001850 tasklet_schedule(&host->finish_tasklet);
1851 }
1852 }
1853
1854 spin_unlock_irqrestore(&host->lock, flags);
1855
Pierre Ossman04cf5852008-08-18 22:18:14 +02001856 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001857}
1858
1859static void sdhci_tasklet_finish(unsigned long param)
1860{
1861 struct sdhci_host *host;
1862 unsigned long flags;
1863 struct mmc_request *mrq;
1864
1865 host = (struct sdhci_host*)param;
1866
Chris Ball0c9c99a2011-04-27 17:35:31 -04001867 /*
1868 * If this tasklet gets rescheduled while running, it will
1869 * be run again afterwards but without any active request.
1870 */
1871 if (!host->mrq)
1872 return;
1873
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874 spin_lock_irqsave(&host->lock, flags);
1875
1876 del_timer(&host->timer);
1877
1878 mrq = host->mrq;
1879
Pierre Ossmand129bce2006-03-24 03:18:17 -08001880 /*
1881 * The controller needs a reset of internal state machines
1882 * upon error conditions.
1883 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001884 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001885 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001886 (mrq->data && (mrq->data->error ||
1887 (mrq->data->stop && mrq->data->stop->error))) ||
1888 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001889
1890 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001891 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001892 unsigned int clock;
1893
1894 /* This is to force an update */
1895 clock = host->clock;
1896 host->clock = 0;
1897 sdhci_set_clock(host, clock);
1898 }
1899
1900 /* Spec says we should do both at the same time, but Ricoh
1901 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001902 sdhci_reset(host, SDHCI_RESET_CMD);
1903 sdhci_reset(host, SDHCI_RESET_DATA);
1904 }
1905
1906 host->mrq = NULL;
1907 host->cmd = NULL;
1908 host->data = NULL;
1909
Pierre Ossmanf9134312008-12-21 17:01:48 +01001910#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001911 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001912#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08001913
Pierre Ossman5f25a662006-10-04 02:15:39 -07001914 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001915 spin_unlock_irqrestore(&host->lock, flags);
1916
1917 mmc_request_done(host->mmc, mrq);
1918}
1919
1920static void sdhci_timeout_timer(unsigned long data)
1921{
1922 struct sdhci_host *host;
1923 unsigned long flags;
1924
1925 host = (struct sdhci_host*)data;
1926
1927 spin_lock_irqsave(&host->lock, flags);
1928
1929 if (host->mrq) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001930 printk(KERN_ERR "%s: Timeout waiting for hardware "
1931 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001932 sdhci_dumpregs(host);
1933
1934 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001935 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001936 sdhci_finish_data(host);
1937 } else {
1938 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02001939 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001940 else
Pierre Ossman17b04292007-07-22 22:18:46 +02001941 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001942
1943 tasklet_schedule(&host->finish_tasklet);
1944 }
1945 }
1946
Pierre Ossman5f25a662006-10-04 02:15:39 -07001947 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001948 spin_unlock_irqrestore(&host->lock, flags);
1949}
1950
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301951static void sdhci_tuning_timer(unsigned long data)
1952{
1953 struct sdhci_host *host;
1954 unsigned long flags;
1955
1956 host = (struct sdhci_host *)data;
1957
1958 spin_lock_irqsave(&host->lock, flags);
1959
1960 host->flags |= SDHCI_NEEDS_RETUNING;
1961
1962 spin_unlock_irqrestore(&host->lock, flags);
1963}
1964
Pierre Ossmand129bce2006-03-24 03:18:17 -08001965/*****************************************************************************\
1966 * *
1967 * Interrupt handling *
1968 * *
1969\*****************************************************************************/
1970
1971static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
1972{
1973 BUG_ON(intmask == 0);
1974
1975 if (!host->cmd) {
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02001976 printk(KERN_ERR "%s: Got command interrupt 0x%08x even "
1977 "though no command operation was in progress.\n",
1978 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001979 sdhci_dumpregs(host);
1980 return;
1981 }
1982
Pierre Ossman43b58b32007-07-25 23:15:27 +02001983 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02001984 host->cmd->error = -ETIMEDOUT;
1985 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
1986 SDHCI_INT_INDEX))
1987 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001988
Pierre Ossmane8095172008-07-25 01:09:08 +02001989 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001990 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02001991 return;
1992 }
1993
1994 /*
1995 * The host can send and interrupt when the busy state has
1996 * ended, allowing us to wait without wasting CPU cycles.
1997 * Unfortunately this is overloaded on the "data complete"
1998 * interrupt, so we need to take some care when handling
1999 * it.
2000 *
2001 * Note: The 1.0 specification is a bit ambiguous about this
2002 * feature so there might be some problems with older
2003 * controllers.
2004 */
2005 if (host->cmd->flags & MMC_RSP_BUSY) {
2006 if (host->cmd->data)
2007 DBG("Cannot wait for busy signal when also "
2008 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002009 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002010 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002011
2012 /* The controller does not support the end-of-busy IRQ,
2013 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002014 }
2015
2016 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002017 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002018}
2019
George G. Davis0957c332010-02-18 12:32:12 -05002020#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002021static void sdhci_show_adma_error(struct sdhci_host *host)
2022{
2023 const char *name = mmc_hostname(host->mmc);
2024 u8 *desc = host->adma_desc;
2025 __le32 *dma;
2026 __le16 *len;
2027 u8 attr;
2028
2029 sdhci_dumpregs(host);
2030
2031 while (true) {
2032 dma = (__le32 *)(desc + 4);
2033 len = (__le16 *)(desc + 2);
2034 attr = *desc;
2035
2036 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2037 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2038
2039 desc += 8;
2040
2041 if (attr & 2)
2042 break;
2043 }
2044}
2045#else
2046static void sdhci_show_adma_error(struct sdhci_host *host) { }
2047#endif
2048
Pierre Ossmand129bce2006-03-24 03:18:17 -08002049static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2050{
2051 BUG_ON(intmask == 0);
2052
Arindam Nathb513ea22011-05-05 12:19:04 +05302053 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2054 if (intmask & SDHCI_INT_DATA_AVAIL) {
2055 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2056 MMC_SEND_TUNING_BLOCK) {
2057 host->tuning_done = 1;
2058 wake_up(&host->buf_ready_int);
2059 return;
2060 }
2061 }
2062
Pierre Ossmand129bce2006-03-24 03:18:17 -08002063 if (!host->data) {
2064 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002065 * The "data complete" interrupt is also used to
2066 * indicate that a busy state has ended. See comment
2067 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002068 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002069 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2070 if (intmask & SDHCI_INT_DATA_END) {
2071 sdhci_finish_command(host);
2072 return;
2073 }
2074 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002075
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002076 printk(KERN_ERR "%s: Got data interrupt 0x%08x even "
2077 "though no data operation was in progress.\n",
2078 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002079 sdhci_dumpregs(host);
2080
2081 return;
2082 }
2083
2084 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002085 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002086 else if (intmask & SDHCI_INT_DATA_END_BIT)
2087 host->data->error = -EILSEQ;
2088 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2089 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2090 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002091 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002092 else if (intmask & SDHCI_INT_ADMA_ERROR) {
2093 printk(KERN_ERR "%s: ADMA error\n", mmc_hostname(host->mmc));
2094 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002095 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002096 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Pierre Ossman17b04292007-07-22 22:18:46 +02002098 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002099 sdhci_finish_data(host);
2100 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002101 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002102 sdhci_transfer_pio(host);
2103
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002104 /*
2105 * We currently don't do anything fancy with DMA
2106 * boundaries, but as we can't disable the feature
2107 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002108 *
2109 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2110 * should return a valid address to continue from, but as
2111 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002112 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002113 if (intmask & SDHCI_INT_DMA_END) {
2114 u32 dmastart, dmanow;
2115 dmastart = sg_dma_address(host->data->sg);
2116 dmanow = dmastart + host->data->bytes_xfered;
2117 /*
2118 * Force update to the next DMA block boundary.
2119 */
2120 dmanow = (dmanow &
2121 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2122 SDHCI_DEFAULT_BOUNDARY_SIZE;
2123 host->data->bytes_xfered = dmanow - dmastart;
2124 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2125 " next 0x%08x\n",
2126 mmc_hostname(host->mmc), dmastart,
2127 host->data->bytes_xfered, dmanow);
2128 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2129 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002130
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002131 if (intmask & SDHCI_INT_DATA_END) {
2132 if (host->cmd) {
2133 /*
2134 * Data managed to finish before the
2135 * command completed. Make sure we do
2136 * things in the proper order.
2137 */
2138 host->data_early = 1;
2139 } else {
2140 sdhci_finish_data(host);
2141 }
2142 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002143 }
2144}
2145
David Howells7d12e782006-10-05 14:55:46 +01002146static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002147{
2148 irqreturn_t result;
2149 struct sdhci_host* host = dev_id;
2150 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002151 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002152
2153 spin_lock(&host->lock);
2154
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002155 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002156
Mark Lord62df67a52007-03-06 13:30:13 +01002157 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002158 result = IRQ_NONE;
2159 goto out;
2160 }
2161
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002162 DBG("*** %s got interrupt: 0x%08x\n",
2163 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002164
Pierre Ossman3192a282006-06-30 02:22:26 -07002165 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002166 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2167 SDHCI_CARD_PRESENT;
2168
2169 /*
2170 * There is a observation on i.mx esdhc. INSERT bit will be
2171 * immediately set again when it gets cleared, if a card is
2172 * inserted. We have to mask the irq to prevent interrupt
2173 * storm which will freeze the system. And the REMOVE gets
2174 * the same situation.
2175 *
2176 * More testing are needed here to ensure it works for other
2177 * platforms though.
2178 */
2179 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2180 SDHCI_INT_CARD_REMOVE);
2181 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2182 SDHCI_INT_CARD_INSERT);
2183
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002184 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002185 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2186 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002187 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002188 }
2189
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002191 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2192 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002193 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 }
2195
2196 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002197 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2198 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002199 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002200 }
2201
2202 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2203
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002204 intmask &= ~SDHCI_INT_ERROR;
2205
Pierre Ossmand129bce2006-03-24 03:18:17 -08002206 if (intmask & SDHCI_INT_BUS_POWER) {
Pierre Ossman3192a282006-06-30 02:22:26 -07002207 printk(KERN_ERR "%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002209 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002210 }
2211
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002212 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002213
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002214 if (intmask & SDHCI_INT_CARD_INT)
2215 cardint = 1;
2216
2217 intmask &= ~SDHCI_INT_CARD_INT;
2218
Pierre Ossman3192a282006-06-30 02:22:26 -07002219 if (intmask) {
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002220 printk(KERN_ERR "%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002221 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002222 sdhci_dumpregs(host);
2223
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002224 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002226
2227 result = IRQ_HANDLED;
2228
Pierre Ossman5f25a662006-10-04 02:15:39 -07002229 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230out:
2231 spin_unlock(&host->lock);
2232
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002233 /*
2234 * We have to delay this as it calls back into the driver.
2235 */
2236 if (cardint)
2237 mmc_signal_sdio_irq(host->mmc);
2238
Pierre Ossmand129bce2006-03-24 03:18:17 -08002239 return result;
2240}
2241
2242/*****************************************************************************\
2243 * *
2244 * Suspend/resume *
2245 * *
2246\*****************************************************************************/
2247
2248#ifdef CONFIG_PM
2249
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002250int sdhci_suspend_host(struct sdhci_host *host, pm_message_t state)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002252 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002254 sdhci_disable_card_detection(host);
2255
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302256 /* Disable tuning since we are suspending */
2257 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2258 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2259 host->flags &= ~SDHCI_NEEDS_RETUNING;
2260 mod_timer(&host->tuning_timer, jiffies +
2261 host->tuning_count * HZ);
2262 }
2263
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002264 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002265 if (ret)
2266 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002267
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002268 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002269
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002270 if (host->vmmc)
2271 ret = regulator_disable(host->vmmc);
2272
2273 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274}
2275
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002276EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002278int sdhci_resume_host(struct sdhci_host *host)
2279{
2280 int ret;
2281
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002282 if (host->vmmc) {
2283 int ret = regulator_enable(host->vmmc);
2284 if (ret)
2285 return ret;
2286 }
2287
2288
Richard Röjforsa13abc72009-09-22 16:45:30 -07002289 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002290 if (host->ops->enable_dma)
2291 host->ops->enable_dma(host);
2292 }
2293
2294 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2295 mmc_hostname(host->mmc), host);
2296 if (ret)
2297 return ret;
2298
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002299 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002300 mmiowb();
2301
2302 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002303 sdhci_enable_card_detection(host);
2304
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302305 /* Set the re-tuning expiration flag */
2306 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2307 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2308 host->flags |= SDHCI_NEEDS_RETUNING;
2309
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002310 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002311}
2312
2313EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314
Daniel Drake5f619702010-11-04 22:20:39 +00002315void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2316{
2317 u8 val;
2318 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2319 val |= SDHCI_WAKE_ON_INT;
2320 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2321}
2322
2323EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2324
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325#endif /* CONFIG_PM */
2326
2327/*****************************************************************************\
2328 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002329 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330 * *
2331\*****************************************************************************/
2332
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002333struct sdhci_host *sdhci_alloc_host(struct device *dev,
2334 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002336 struct mmc_host *mmc;
2337 struct sdhci_host *host;
2338
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002339 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002341 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002343 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002344
2345 host = mmc_priv(mmc);
2346 host->mmc = mmc;
2347
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002348 return host;
2349}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002350
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002351EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002352
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002353int sdhci_add_host(struct sdhci_host *host)
2354{
2355 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302356 u32 caps[2];
2357 u32 max_current_caps;
2358 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002359 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002360
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002361 WARN_ON(host == NULL);
2362 if (host == NULL)
2363 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002365 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002367 if (debug_quirks)
2368 host->quirks = debug_quirks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369
Pierre Ossmand96649e2006-06-30 02:22:30 -07002370 sdhci_reset(host, SDHCI_RESET_ALL);
2371
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002372 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002373 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2374 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002375 if (host->version > SDHCI_SPEC_300) {
Pierre Ossman4a965502006-06-30 02:22:29 -07002376 printk(KERN_ERR "%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002377 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002378 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002379 }
2380
Arindam Nathf2119df2011-05-05 12:18:57 +05302381 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002382 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002383
Arindam Nathf2119df2011-05-05 12:18:57 +05302384 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2385 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2386
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002387 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002388 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302389 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002390 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002391 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002392 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002394 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002395 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002396 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002397 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002398 }
2399
Arindam Nathf2119df2011-05-05 12:18:57 +05302400 if ((host->version >= SDHCI_SPEC_200) &&
2401 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002402 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002403
2404 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2405 (host->flags & SDHCI_USE_ADMA)) {
2406 DBG("Disabling ADMA as it is marked broken\n");
2407 host->flags &= ~SDHCI_USE_ADMA;
2408 }
2409
Richard Röjforsa13abc72009-09-22 16:45:30 -07002410 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002411 if (host->ops->enable_dma) {
2412 if (host->ops->enable_dma(host)) {
2413 printk(KERN_WARNING "%s: No suitable DMA "
2414 "available. Falling back to PIO.\n",
2415 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002416 host->flags &=
2417 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002418 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419 }
2420 }
2421
Pierre Ossman2134a922008-06-28 18:28:51 +02002422 if (host->flags & SDHCI_USE_ADMA) {
2423 /*
2424 * We need to allocate descriptors for all sg entries
2425 * (128) and potentially one alignment transfer for
2426 * each of those entries.
2427 */
2428 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2429 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2430 if (!host->adma_desc || !host->align_buffer) {
2431 kfree(host->adma_desc);
2432 kfree(host->align_buffer);
2433 printk(KERN_WARNING "%s: Unable to allocate ADMA "
2434 "buffers. Falling back to standard DMA.\n",
2435 mmc_hostname(mmc));
2436 host->flags &= ~SDHCI_USE_ADMA;
2437 }
2438 }
2439
Pierre Ossman76591502008-07-21 00:32:11 +02002440 /*
2441 * If we use DMA, then it's up to the caller to set the DMA
2442 * mask, but PIO does not need the hw shim so we set a new
2443 * mask here in that case.
2444 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002445 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002446 host->dma_mask = DMA_BIT_MASK(64);
2447 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2448 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002450 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302451 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002452 >> SDHCI_CLOCK_BASE_SHIFT;
2453 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302454 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002455 >> SDHCI_CLOCK_BASE_SHIFT;
2456
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002458 if (host->max_clk == 0 || host->quirks &
2459 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002460 if (!host->ops->get_max_clock) {
2461 printk(KERN_ERR
2462 "%s: Hardware doesn't specify base clock "
2463 "frequency.\n", mmc_hostname(mmc));
2464 return -ENODEV;
2465 }
2466 host->max_clk = host->ops->get_max_clock(host);
2467 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002468
2469 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302470 * In case of Host Controller v3.00, find out whether clock
2471 * multiplier is supported.
2472 */
2473 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2474 SDHCI_CLOCK_MUL_SHIFT;
2475
2476 /*
2477 * In case the value in Clock Multiplier is 0, then programmable
2478 * clock mode is not supported, otherwise the actual clock
2479 * multiplier is one more than the value of Clock Multiplier
2480 * in the Capabilities Register.
2481 */
2482 if (host->clk_mul)
2483 host->clk_mul += 1;
2484
2485 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002486 * Set host parameters.
2487 */
2488 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302489 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002490 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002491 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302492 else if (host->version >= SDHCI_SPEC_300) {
2493 if (host->clk_mul) {
2494 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2495 mmc->f_max = host->max_clk * host->clk_mul;
2496 } else
2497 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2498 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002499 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002500
Andy Shevchenko272308c2011-08-03 18:36:00 +03002501 host->timeout_clk =
2502 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2503 if (host->timeout_clk == 0) {
2504 if (host->ops->get_timeout_clock) {
2505 host->timeout_clk = host->ops->get_timeout_clock(host);
2506 } else if (!(host->quirks &
2507 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
2508 printk(KERN_ERR
2509 "%s: Hardware doesn't specify timeout clock "
2510 "frequency.\n", mmc_hostname(mmc));
2511 return -ENODEV;
2512 }
2513 }
2514 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2515 host->timeout_clk *= 1000;
2516
2517 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002518 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002519
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002520 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002521
Andrei Warkentine89d4562011-05-23 15:06:37 -05002522 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2523
2524 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2525 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002526
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002527 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002528 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002529 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002530 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002531 host->flags |= SDHCI_AUTO_CMD23;
2532 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2533 } else {
2534 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2535 }
2536
Philip Rakity15ec4462010-11-19 16:48:39 -05002537 /*
2538 * A controller may support 8-bit width, but the board itself
2539 * might not have the pins brought out. Boards that support
2540 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2541 * their platform code before calling sdhci_add_host(), and we
2542 * won't assume 8-bit width for hosts without that CAP.
2543 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002544 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002545 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002546
Arindam Nathf2119df2011-05-05 12:18:57 +05302547 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002548 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002549
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002550 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2551 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002552 mmc->caps |= MMC_CAP_NEEDS_POLL;
2553
Arindam Nathf2119df2011-05-05 12:18:57 +05302554 /* UHS-I mode(s) supported by the host controller. */
2555 if (host->version >= SDHCI_SPEC_300)
2556 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2557
2558 /* SDR104 supports also implies SDR50 support */
2559 if (caps[1] & SDHCI_SUPPORT_SDR104)
2560 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2561 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2562 mmc->caps |= MMC_CAP_UHS_SDR50;
2563
2564 if (caps[1] & SDHCI_SUPPORT_DDR50)
2565 mmc->caps |= MMC_CAP_UHS_DDR50;
2566
Arindam Nathb513ea22011-05-05 12:19:04 +05302567 /* Does the host needs tuning for SDR50? */
2568 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2569 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2570
Arindam Nathd6d50a12011-05-05 12:18:59 +05302571 /* Driver Type(s) (A, C, D) supported by the host */
2572 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2573 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2574 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2575 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2576 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2577 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2578
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302579 /* Initial value for re-tuning timer count */
2580 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2581 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2582
2583 /*
2584 * In case Re-tuning Timer is not disabled, the actual value of
2585 * re-tuning timer will be 2 ^ (n - 1).
2586 */
2587 if (host->tuning_count)
2588 host->tuning_count = 1 << (host->tuning_count - 1);
2589
2590 /* Re-tuning mode supported by the Host Controller */
2591 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2592 SDHCI_RETUNING_MODE_SHIFT;
2593
Takashi Iwai8f230f42010-12-08 10:04:30 +01002594 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302595 /*
2596 * According to SD Host Controller spec v3.00, if the Host System
2597 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2598 * the value is meaningful only if Voltage Support in the Capabilities
2599 * register is set. The actual current value is 4 times the register
2600 * value.
2601 */
2602 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2603
2604 if (caps[0] & SDHCI_CAN_VDD_330) {
2605 int max_current_330;
2606
Takashi Iwai8f230f42010-12-08 10:04:30 +01002607 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302608
2609 max_current_330 = ((max_current_caps &
2610 SDHCI_MAX_CURRENT_330_MASK) >>
2611 SDHCI_MAX_CURRENT_330_SHIFT) *
2612 SDHCI_MAX_CURRENT_MULTIPLIER;
2613
2614 if (max_current_330 > 150)
2615 mmc->caps |= MMC_CAP_SET_XPC_330;
2616 }
2617 if (caps[0] & SDHCI_CAN_VDD_300) {
2618 int max_current_300;
2619
Takashi Iwai8f230f42010-12-08 10:04:30 +01002620 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302621
2622 max_current_300 = ((max_current_caps &
2623 SDHCI_MAX_CURRENT_300_MASK) >>
2624 SDHCI_MAX_CURRENT_300_SHIFT) *
2625 SDHCI_MAX_CURRENT_MULTIPLIER;
2626
2627 if (max_current_300 > 150)
2628 mmc->caps |= MMC_CAP_SET_XPC_300;
2629 }
2630 if (caps[0] & SDHCI_CAN_VDD_180) {
2631 int max_current_180;
2632
Takashi Iwai8f230f42010-12-08 10:04:30 +01002633 ocr_avail |= MMC_VDD_165_195;
2634
Arindam Nathf2119df2011-05-05 12:18:57 +05302635 max_current_180 = ((max_current_caps &
2636 SDHCI_MAX_CURRENT_180_MASK) >>
2637 SDHCI_MAX_CURRENT_180_SHIFT) *
2638 SDHCI_MAX_CURRENT_MULTIPLIER;
2639
2640 if (max_current_180 > 150)
2641 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302642
2643 /* Maximum current capabilities of the host at 1.8V */
2644 if (max_current_180 >= 800)
2645 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2646 else if (max_current_180 >= 600)
2647 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2648 else if (max_current_180 >= 400)
2649 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2650 else
2651 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302652 }
2653
Takashi Iwai8f230f42010-12-08 10:04:30 +01002654 mmc->ocr_avail = ocr_avail;
2655 mmc->ocr_avail_sdio = ocr_avail;
2656 if (host->ocr_avail_sdio)
2657 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2658 mmc->ocr_avail_sd = ocr_avail;
2659 if (host->ocr_avail_sd)
2660 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2661 else /* normal SD controllers don't support 1.8V */
2662 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2663 mmc->ocr_avail_mmc = ocr_avail;
2664 if (host->ocr_avail_mmc)
2665 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002666
2667 if (mmc->ocr_avail == 0) {
2668 printk(KERN_ERR "%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002669 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002670 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002671 }
2672
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673 spin_lock_init(&host->lock);
2674
2675 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002676 * Maximum number of segments. Depends on if the hardware
2677 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002678 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002679 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002680 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002681 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002682 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002683 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002684 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002685
2686 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002687 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002688 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002690 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002691
2692 /*
2693 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002694 * of bytes. When doing hardware scatter/gather, each entry cannot
2695 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002696 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002697 if (host->flags & SDHCI_USE_ADMA) {
2698 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2699 mmc->max_seg_size = 65535;
2700 else
2701 mmc->max_seg_size = 65536;
2702 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002703 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002704 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002705
2706 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002707 * Maximum block size. This varies from controller to controller and
2708 * is specified in the capabilities register.
2709 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002710 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2711 mmc->max_blk_size = 2;
2712 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302713 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002714 SDHCI_MAX_BLOCK_SHIFT;
2715 if (mmc->max_blk_size >= 3) {
2716 printk(KERN_WARNING "%s: Invalid maximum block size, "
2717 "assuming 512 bytes\n", mmc_hostname(mmc));
2718 mmc->max_blk_size = 0;
2719 }
2720 }
2721
2722 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002723
2724 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002725 * Maximum block count.
2726 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002727 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002728
2729 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002730 * Init tasklets.
2731 */
2732 tasklet_init(&host->card_tasklet,
2733 sdhci_tasklet_card, (unsigned long)host);
2734 tasklet_init(&host->finish_tasklet,
2735 sdhci_tasklet_finish, (unsigned long)host);
2736
Al Viroe4cad1b2006-10-10 22:47:07 +01002737 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002738
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302739 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302740 init_waitqueue_head(&host->buf_ready_int);
2741
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302742 /* Initialize re-tuning timer */
2743 init_timer(&host->tuning_timer);
2744 host->tuning_timer.data = (unsigned long)host;
2745 host->tuning_timer.function = sdhci_tuning_timer;
2746 }
2747
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002748 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002749 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002750 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002751 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002752
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002753 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2754 if (IS_ERR(host->vmmc)) {
2755 printk(KERN_INFO "%s: no vmmc regulator found\n", mmc_hostname(mmc));
2756 host->vmmc = NULL;
2757 } else {
2758 regulator_enable(host->vmmc);
2759 }
2760
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002761 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002762
2763#ifdef CONFIG_MMC_DEBUG
2764 sdhci_dumpregs(host);
2765#endif
2766
Pierre Ossmanf9134312008-12-21 17:01:48 +01002767#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002768 snprintf(host->led_name, sizeof(host->led_name),
2769 "%s::", mmc_hostname(mmc));
2770 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002771 host->led.brightness = LED_OFF;
2772 host->led.default_trigger = mmc_hostname(mmc);
2773 host->led.brightness_set = sdhci_led_control;
2774
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002775 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002776 if (ret)
2777 goto reset;
2778#endif
2779
Pierre Ossman5f25a662006-10-04 02:15:39 -07002780 mmiowb();
2781
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782 mmc_add_host(mmc);
2783
Richard Röjforsa13abc72009-09-22 16:45:30 -07002784 printk(KERN_INFO "%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002785 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002786 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2787 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002789 sdhci_enable_card_detection(host);
2790
Pierre Ossmand129bce2006-03-24 03:18:17 -08002791 return 0;
2792
Pierre Ossmanf9134312008-12-21 17:01:48 +01002793#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002794reset:
2795 sdhci_reset(host, SDHCI_RESET_ALL);
2796 free_irq(host->irq, host);
2797#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002798untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799 tasklet_kill(&host->card_tasklet);
2800 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002801
2802 return ret;
2803}
2804
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002805EXPORT_SYMBOL_GPL(sdhci_add_host);
2806
Pierre Ossman1e728592008-04-16 19:13:13 +02002807void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002808{
Pierre Ossman1e728592008-04-16 19:13:13 +02002809 unsigned long flags;
2810
2811 if (dead) {
2812 spin_lock_irqsave(&host->lock, flags);
2813
2814 host->flags |= SDHCI_DEVICE_DEAD;
2815
2816 if (host->mrq) {
2817 printk(KERN_ERR "%s: Controller removed during "
2818 " transfer!\n", mmc_hostname(host->mmc));
2819
2820 host->mrq->cmd->error = -ENOMEDIUM;
2821 tasklet_schedule(&host->finish_tasklet);
2822 }
2823
2824 spin_unlock_irqrestore(&host->lock, flags);
2825 }
2826
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002827 sdhci_disable_card_detection(host);
2828
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002829 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002830
Pierre Ossmanf9134312008-12-21 17:01:48 +01002831#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002832 led_classdev_unregister(&host->led);
2833#endif
2834
Pierre Ossman1e728592008-04-16 19:13:13 +02002835 if (!dead)
2836 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002837
2838 free_irq(host->irq, host);
2839
2840 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302841 if (host->version >= SDHCI_SPEC_300)
2842 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002843
2844 tasklet_kill(&host->card_tasklet);
2845 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02002846
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002847 if (host->vmmc) {
2848 regulator_disable(host->vmmc);
2849 regulator_put(host->vmmc);
2850 }
2851
Pierre Ossman2134a922008-06-28 18:28:51 +02002852 kfree(host->adma_desc);
2853 kfree(host->align_buffer);
2854
2855 host->adma_desc = NULL;
2856 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857}
2858
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002859EXPORT_SYMBOL_GPL(sdhci_remove_host);
2860
2861void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002864}
2865
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002866EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002867
2868/*****************************************************************************\
2869 * *
2870 * Driver init/exit *
2871 * *
2872\*****************************************************************************/
2873
2874static int __init sdhci_drv_init(void)
2875{
2876 printk(KERN_INFO DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01002877 ": Secure Digital Host Controller Interface driver\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002878 printk(KERN_INFO DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
2879
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002880 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002881}
2882
2883static void __exit sdhci_drv_exit(void)
2884{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002885}
2886
2887module_init(sdhci_drv_init);
2888module_exit(sdhci_drv_exit);
2889
Pierre Ossmandf673b22006-06-30 02:22:31 -07002890module_param(debug_quirks, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07002891
Pierre Ossman32710e82009-04-08 20:14:54 +02002892MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002893MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002894MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07002895
Pierre Ossmandf673b22006-06-30 02:22:31 -07002896MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");