blob: 6a35d03681178d5fab96357efc8922f1d10df56e [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>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include "sdhci.h"
32
33#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010036 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmanf9134312008-12-21 17:01:48 +010038#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
39 defined(CONFIG_MMC_SDHCI_MODULE))
40#define SDHCI_USE_LEDS_CLASS
41#endif
42
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
50static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
51static void sdhci_finish_command(struct sdhci_host *);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053052static int sdhci_execute_tuning(struct mmc_host *mmc);
53static void sdhci_tuning_timer(unsigned long data);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030055#ifdef CONFIG_PM_RUNTIME
56static int sdhci_runtime_pm_get(struct sdhci_host *host);
57static int sdhci_runtime_pm_put(struct sdhci_host *host);
58#else
59static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
60{
61 return 0;
62}
63static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
64{
65 return 0;
66}
67#endif
68
Pierre Ossmand129bce2006-03-24 03:18:17 -080069static void sdhci_dumpregs(struct sdhci_host *host)
70{
Girish K Sa3c76eb2011-10-11 11:44:09 +053071 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070072 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080073
Girish K Sa3c76eb2011-10-11 11:44:09 +053074 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030075 sdhci_readl(host, SDHCI_DMA_ADDRESS),
76 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053077 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readw(host, SDHCI_BLOCK_SIZE),
79 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053080 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readl(host, SDHCI_ARGUMENT),
82 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_PRESENT_STATE),
85 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readb(host, SDHCI_POWER_CONTROL),
88 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
91 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
94 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readl(host, SDHCI_INT_ENABLE),
97 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readw(host, SDHCI_ACMD12_ERR),
100 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500103 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300106 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530108 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800109
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100110 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530111 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 readl(host->ioaddr + SDHCI_ADMA_ERROR),
113 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
114
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300124static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
125{
126 u32 ier;
127
128 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
129 ier &= ~clear;
130 ier |= set;
131 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
133}
134
135static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
136{
137 sdhci_clear_set_irqs(host, 0, irqs);
138}
139
140static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
141{
142 sdhci_clear_set_irqs(host, irqs, 0);
143}
144
145static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
146{
Shawn Guod25928d2011-06-21 22:41:48 +0800147 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148
Anton Vorontsov68d1fb72009-03-17 00:13:52 +0300149 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
150 return;
151
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300152 if (host->quirks2 & SDHCI_QUIRK2_OWN_CARD_DETECTION)
153 return;
154
Shawn Guod25928d2011-06-21 22:41:48 +0800155 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
156 SDHCI_CARD_PRESENT;
157 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
158
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300159 if (enable)
160 sdhci_unmask_irqs(host, irqs);
161 else
162 sdhci_mask_irqs(host, irqs);
163}
164
165static void sdhci_enable_card_detection(struct sdhci_host *host)
166{
167 sdhci_set_card_detection(host, true);
168}
169
170static void sdhci_disable_card_detection(struct sdhci_host *host)
171{
172 sdhci_set_card_detection(host, false);
173}
174
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175static void sdhci_reset(struct sdhci_host *host, u8 mask)
176{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700177 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300178 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100180 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700182 SDHCI_CARD_PRESENT))
183 return;
184 }
185
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300186 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
187 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
188
Philip Rakity393c1a32011-01-21 11:26:40 -0800189 if (host->ops->platform_reset_enter)
190 host->ops->platform_reset_enter(host, mask);
191
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300192 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800195 host->clock = 0;
196
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 /* Wait max 100 ms */
198 timeout = 100;
199
200 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300201 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700202 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530203 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700204 mmc_hostname(host->mmc), (int)mask);
205 sdhci_dumpregs(host);
206 return;
207 }
208 timeout--;
209 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300211
Philip Rakity393c1a32011-01-21 11:26:40 -0800212 if (host->ops->platform_reset_exit)
213 host->ops->platform_reset_exit(host, mask);
214
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300215 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
216 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217}
218
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800219static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
220
221static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800223 if (soft)
224 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
225 else
226 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300228 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
229 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700230 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
231 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300232 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800233
234 if (soft) {
235 /* force clock reconfiguration */
236 host->clock = 0;
237 sdhci_set_ios(host->mmc, &host->mmc->ios);
238 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300239}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800240
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300241static void sdhci_reinit(struct sdhci_host *host)
242{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800243 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300244 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245}
246
247static void sdhci_activate_led(struct sdhci_host *host)
248{
249 u8 ctrl;
250
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300251 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800252 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300253 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254}
255
256static void sdhci_deactivate_led(struct sdhci_host *host)
257{
258 u8 ctrl;
259
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300260 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300262 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263}
264
Pierre Ossmanf9134312008-12-21 17:01:48 +0100265#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100266static void sdhci_led_control(struct led_classdev *led,
267 enum led_brightness brightness)
268{
269 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
270 unsigned long flags;
271
272 spin_lock_irqsave(&host->lock, flags);
273
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300274 if (host->runtime_suspended)
275 goto out;
276
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100277 if (brightness == LED_OFF)
278 sdhci_deactivate_led(host);
279 else
280 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300281out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100282 spin_unlock_irqrestore(&host->lock, flags);
283}
284#endif
285
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286/*****************************************************************************\
287 * *
288 * Core functions *
289 * *
290\*****************************************************************************/
291
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100292static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800293{
Pierre Ossman76591502008-07-21 00:32:11 +0200294 unsigned long flags;
295 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700296 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200297 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800298
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100299 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800300
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100301 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200302 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800303
Pierre Ossman76591502008-07-21 00:32:11 +0200304 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100306 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200307 if (!sg_miter_next(&host->sg_miter))
308 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309
Pierre Ossman76591502008-07-21 00:32:11 +0200310 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossman76591502008-07-21 00:32:11 +0200312 blksize -= len;
313 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200314
Pierre Ossman76591502008-07-21 00:32:11 +0200315 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 while (len) {
318 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300319 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200320 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321 }
Pierre Ossman76591502008-07-21 00:32:11 +0200322
323 *buf = scratch & 0xFF;
324
325 buf++;
326 scratch >>= 8;
327 chunk--;
328 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800329 }
330 }
Pierre Ossman76591502008-07-21 00:32:11 +0200331
332 sg_miter_stop(&host->sg_miter);
333
334 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100335}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100337static void sdhci_write_block_pio(struct sdhci_host *host)
338{
Pierre Ossman76591502008-07-21 00:32:11 +0200339 unsigned long flags;
340 size_t blksize, len, chunk;
341 u32 scratch;
342 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100343
344 DBG("PIO writing\n");
345
346 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200347 chunk = 0;
348 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100349
Pierre Ossman76591502008-07-21 00:32:11 +0200350 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100351
352 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200353 if (!sg_miter_next(&host->sg_miter))
354 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 blksize -= len;
359 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 while (len) {
364 scratch |= (u32)*buf << (chunk * 8);
365
366 buf++;
367 chunk++;
368 len--;
369
370 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300371 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200372 chunk = 0;
373 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375 }
376 }
Pierre Ossman76591502008-07-21 00:32:11 +0200377
378 sg_miter_stop(&host->sg_miter);
379
380 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381}
382
383static void sdhci_transfer_pio(struct sdhci_host *host)
384{
385 u32 mask;
386
387 BUG_ON(!host->data);
388
Pierre Ossman76591502008-07-21 00:32:11 +0200389 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100390 return;
391
392 if (host->data->flags & MMC_DATA_READ)
393 mask = SDHCI_DATA_AVAILABLE;
394 else
395 mask = SDHCI_SPACE_AVAILABLE;
396
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200397 /*
398 * Some controllers (JMicron JMB38x) mess up the buffer bits
399 * for transfers < 4 bytes. As long as it is just one block,
400 * we can ignore the bits.
401 */
402 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
403 (host->data->blocks == 1))
404 mask = ~0;
405
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300406 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300407 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
408 udelay(100);
409
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100410 if (host->data->flags & MMC_DATA_READ)
411 sdhci_read_block_pio(host);
412 else
413 sdhci_write_block_pio(host);
414
Pierre Ossman76591502008-07-21 00:32:11 +0200415 host->blocks--;
416 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418 }
419
420 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800421}
422
Pierre Ossman2134a922008-06-28 18:28:51 +0200423static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
424{
425 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800426 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200427}
428
429static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
430{
Cong Wang482fce92011-11-27 13:27:00 +0800431 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200432 local_irq_restore(*flags);
433}
434
Ben Dooks118cd172010-03-05 13:43:26 -0800435static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
436{
Ben Dooks9e506f32010-03-05 13:43:29 -0800437 __le32 *dataddr = (__le32 __force *)(desc + 4);
438 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800439
Ben Dooks9e506f32010-03-05 13:43:29 -0800440 /* SDHCI specification says ADMA descriptors should be 4 byte
441 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800442
Ben Dooks9e506f32010-03-05 13:43:29 -0800443 cmdlen[0] = cpu_to_le16(cmd);
444 cmdlen[1] = cpu_to_le16(len);
445
446 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800447}
448
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200449static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200450 struct mmc_data *data)
451{
452 int direction;
453
454 u8 *desc;
455 u8 *align;
456 dma_addr_t addr;
457 dma_addr_t align_addr;
458 int len, offset;
459
460 struct scatterlist *sg;
461 int i;
462 char *buffer;
463 unsigned long flags;
464
465 /*
466 * The spec does not specify endianness of descriptor table.
467 * We currently guess that it is LE.
468 */
469
470 if (data->flags & MMC_DATA_READ)
471 direction = DMA_FROM_DEVICE;
472 else
473 direction = DMA_TO_DEVICE;
474
475 /*
476 * The ADMA descriptor table is mapped further down as we
477 * need to fill it with data first.
478 */
479
480 host->align_addr = dma_map_single(mmc_dev(host->mmc),
481 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700482 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200483 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200484 BUG_ON(host->align_addr & 0x3);
485
486 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
487 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200488 if (host->sg_count == 0)
489 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200490
491 desc = host->adma_desc;
492 align = host->align_buffer;
493
494 align_addr = host->align_addr;
495
496 for_each_sg(data->sg, sg, host->sg_count, i) {
497 addr = sg_dma_address(sg);
498 len = sg_dma_len(sg);
499
500 /*
501 * The SDHCI specification states that ADMA
502 * addresses must be 32-bit aligned. If they
503 * aren't, then we use a bounce buffer for
504 * the (up to three) bytes that screw up the
505 * alignment.
506 */
507 offset = (4 - (addr & 0x3)) & 0x3;
508 if (offset) {
509 if (data->flags & MMC_DATA_WRITE) {
510 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200511 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200512 memcpy(align, buffer, offset);
513 sdhci_kunmap_atomic(buffer, &flags);
514 }
515
Ben Dooks118cd172010-03-05 13:43:26 -0800516 /* tran, valid */
517 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200518
519 BUG_ON(offset > 65536);
520
Pierre Ossman2134a922008-06-28 18:28:51 +0200521 align += 4;
522 align_addr += 4;
523
524 desc += 8;
525
526 addr += offset;
527 len -= offset;
528 }
529
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 BUG_ON(len > 65536);
531
Ben Dooks118cd172010-03-05 13:43:26 -0800532 /* tran, valid */
533 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200534 desc += 8;
535
536 /*
537 * If this triggers then we have a calculation bug
538 * somewhere. :/
539 */
540 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
541 }
542
Thomas Abraham70764a92010-05-26 14:42:04 -0700543 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
544 /*
545 * Mark the last descriptor as the terminating descriptor
546 */
547 if (desc != host->adma_desc) {
548 desc -= 8;
549 desc[0] |= 0x2; /* end */
550 }
551 } else {
552 /*
553 * Add a terminating entry.
554 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200555
Thomas Abraham70764a92010-05-26 14:42:04 -0700556 /* nop, end, valid */
557 sdhci_set_adma_desc(desc, 0, 0, 0x3);
558 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200559
560 /*
561 * Resync align buffer as we might have changed it.
562 */
563 if (data->flags & MMC_DATA_WRITE) {
564 dma_sync_single_for_device(mmc_dev(host->mmc),
565 host->align_addr, 128 * 4, direction);
566 }
567
568 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
569 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200570 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200571 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200572 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200573
574 return 0;
575
576unmap_entries:
577 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
578 data->sg_len, direction);
579unmap_align:
580 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
581 128 * 4, direction);
582fail:
583 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200584}
585
586static void sdhci_adma_table_post(struct sdhci_host *host,
587 struct mmc_data *data)
588{
589 int direction;
590
591 struct scatterlist *sg;
592 int i, size;
593 u8 *align;
594 char *buffer;
595 unsigned long flags;
596
597 if (data->flags & MMC_DATA_READ)
598 direction = DMA_FROM_DEVICE;
599 else
600 direction = DMA_TO_DEVICE;
601
602 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
603 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
604
605 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
606 128 * 4, direction);
607
608 if (data->flags & MMC_DATA_READ) {
609 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
610 data->sg_len, direction);
611
612 align = host->align_buffer;
613
614 for_each_sg(data->sg, sg, host->sg_count, i) {
615 if (sg_dma_address(sg) & 0x3) {
616 size = 4 - (sg_dma_address(sg) & 0x3);
617
618 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200619 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200620 memcpy(buffer, align, size);
621 sdhci_kunmap_atomic(buffer, &flags);
622
623 align += 4;
624 }
625 }
626 }
627
628 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
629 data->sg_len, direction);
630}
631
Andrei Warkentina3c77782011-04-11 16:13:42 -0500632static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800633{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700634 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500635 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700636 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800637
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200638 /*
639 * If the host controller provides us with an incorrect timeout
640 * value, just skip the check and use 0xE. The hardware may take
641 * longer to time out, but that's much better than having a too-short
642 * timeout value.
643 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200644 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200645 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200646
Andrei Warkentina3c77782011-04-11 16:13:42 -0500647 /* Unspecified timeout, assume max */
648 if (!data && !cmd->cmd_timeout_ms)
649 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800650
Andrei Warkentina3c77782011-04-11 16:13:42 -0500651 /* timeout in us */
652 if (!data)
653 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300654 else {
655 target_timeout = data->timeout_ns / 1000;
656 if (host->clock)
657 target_timeout += data->timeout_clks / host->clock;
658 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700659
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700660 /*
661 * Figure out needed cycles.
662 * We do this in steps in order to fit inside a 32 bit int.
663 * The first step is the minimum timeout, which will have a
664 * minimum resolution of 6 bits:
665 * (1) 2^13*1000 > 2^22,
666 * (2) host->timeout_clk < 2^16
667 * =>
668 * (1) / (2) > 2^6
669 */
670 count = 0;
671 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
672 while (current_timeout < target_timeout) {
673 count++;
674 current_timeout <<= 1;
675 if (count >= 0xF)
676 break;
677 }
678
679 if (count >= 0xF) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530680 pr_warning("%s: Too large timeout requested for CMD%d!\n",
Andrei Warkentina3c77782011-04-11 16:13:42 -0500681 mmc_hostname(host->mmc), cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700682 count = 0xE;
683 }
684
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200685 return count;
686}
687
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300688static void sdhci_set_transfer_irqs(struct sdhci_host *host)
689{
690 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
691 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
692
693 if (host->flags & SDHCI_REQ_USE_DMA)
694 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
695 else
696 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
697}
698
Andrei Warkentina3c77782011-04-11 16:13:42 -0500699static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200700{
701 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200702 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500703 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200704 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200705
706 WARN_ON(host->data);
707
Andrei Warkentina3c77782011-04-11 16:13:42 -0500708 if (data || (cmd->flags & MMC_RSP_BUSY)) {
709 count = sdhci_calc_timeout(host, cmd);
710 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
711 }
712
713 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200714 return;
715
716 /* Sanity checks */
717 BUG_ON(data->blksz * data->blocks > 524288);
718 BUG_ON(data->blksz > host->mmc->max_blk_size);
719 BUG_ON(data->blocks > 65535);
720
721 host->data = data;
722 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400723 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200724
Richard Röjforsa13abc72009-09-22 16:45:30 -0700725 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100726 host->flags |= SDHCI_REQ_USE_DMA;
727
Pierre Ossman2134a922008-06-28 18:28:51 +0200728 /*
729 * FIXME: This doesn't account for merging when mapping the
730 * scatterlist.
731 */
732 if (host->flags & SDHCI_REQ_USE_DMA) {
733 int broken, i;
734 struct scatterlist *sg;
735
736 broken = 0;
737 if (host->flags & SDHCI_USE_ADMA) {
738 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
739 broken = 1;
740 } else {
741 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
742 broken = 1;
743 }
744
745 if (unlikely(broken)) {
746 for_each_sg(data->sg, sg, data->sg_len, i) {
747 if (sg->length & 0x3) {
748 DBG("Reverting to PIO because of "
749 "transfer size (%d)\n",
750 sg->length);
751 host->flags &= ~SDHCI_REQ_USE_DMA;
752 break;
753 }
754 }
755 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100756 }
757
758 /*
759 * The assumption here being that alignment is the same after
760 * translation to device address space.
761 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200762 if (host->flags & SDHCI_REQ_USE_DMA) {
763 int broken, i;
764 struct scatterlist *sg;
765
766 broken = 0;
767 if (host->flags & SDHCI_USE_ADMA) {
768 /*
769 * As we use 3 byte chunks to work around
770 * alignment problems, we need to check this
771 * quirk.
772 */
773 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
774 broken = 1;
775 } else {
776 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
777 broken = 1;
778 }
779
780 if (unlikely(broken)) {
781 for_each_sg(data->sg, sg, data->sg_len, i) {
782 if (sg->offset & 0x3) {
783 DBG("Reverting to PIO because of "
784 "bad alignment\n");
785 host->flags &= ~SDHCI_REQ_USE_DMA;
786 break;
787 }
788 }
789 }
790 }
791
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200792 if (host->flags & SDHCI_REQ_USE_DMA) {
793 if (host->flags & SDHCI_USE_ADMA) {
794 ret = sdhci_adma_table_pre(host, data);
795 if (ret) {
796 /*
797 * This only happens when someone fed
798 * us an invalid request.
799 */
800 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200801 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200802 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300803 sdhci_writel(host, host->adma_addr,
804 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200805 }
806 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300807 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200808
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300809 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200810 data->sg, data->sg_len,
811 (data->flags & MMC_DATA_READ) ?
812 DMA_FROM_DEVICE :
813 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300814 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200815 /*
816 * This only happens when someone fed
817 * us an invalid request.
818 */
819 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200820 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200821 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200822 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300823 sdhci_writel(host, sg_dma_address(data->sg),
824 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200825 }
826 }
827 }
828
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 /*
830 * Always adjust the DMA selection as some controllers
831 * (e.g. JMicron) can't do PIO properly when the selection
832 * is ADMA.
833 */
834 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300835 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200836 ctrl &= ~SDHCI_CTRL_DMA_MASK;
837 if ((host->flags & SDHCI_REQ_USE_DMA) &&
838 (host->flags & SDHCI_USE_ADMA))
839 ctrl |= SDHCI_CTRL_ADMA32;
840 else
841 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300842 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100843 }
844
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200845 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200846 int flags;
847
848 flags = SG_MITER_ATOMIC;
849 if (host->data->flags & MMC_DATA_READ)
850 flags |= SG_MITER_TO_SG;
851 else
852 flags |= SG_MITER_FROM_SG;
853 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200854 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800855 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700856
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300857 sdhci_set_transfer_irqs(host);
858
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400859 /* Set the DMA boundary value and block size */
860 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
861 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300862 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700863}
864
865static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500866 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700867{
868 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500869 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700870
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700871 if (data == NULL)
872 return;
873
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200874 WARN_ON(!host->data);
875
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700876 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500877 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
878 mode |= SDHCI_TRNS_MULTI;
879 /*
880 * If we are sending CMD23, CMD12 never gets sent
881 * on successful completion (so no Auto-CMD12).
882 */
883 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
884 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500885 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
886 mode |= SDHCI_TRNS_AUTO_CMD23;
887 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
888 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700889 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500890
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700891 if (data->flags & MMC_DATA_READ)
892 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100893 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700894 mode |= SDHCI_TRNS_DMA;
895
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300896 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800897}
898
899static void sdhci_finish_data(struct sdhci_host *host)
900{
901 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800902
903 BUG_ON(!host->data);
904
905 data = host->data;
906 host->data = NULL;
907
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100908 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200909 if (host->flags & SDHCI_USE_ADMA)
910 sdhci_adma_table_post(host, data);
911 else {
912 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
913 data->sg_len, (data->flags & MMC_DATA_READ) ?
914 DMA_FROM_DEVICE : DMA_TO_DEVICE);
915 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800916 }
917
918 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200919 * The specification states that the block count register must
920 * be updated, but it does not specify at what point in the
921 * data flow. That makes the register entirely useless to read
922 * back so we have to assume that nothing made it to the card
923 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200925 if (data->error)
926 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200928 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 /*
931 * Need to send CMD12 if -
932 * a) open-ended multiblock transfer (no CMD23)
933 * b) error in multiblock transfer
934 */
935 if (data->stop &&
936 (data->error ||
937 !host->mrq->sbc)) {
938
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939 /*
940 * The controller needs a reset of internal state machines
941 * upon error conditions.
942 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200943 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800944 sdhci_reset(host, SDHCI_RESET_CMD);
945 sdhci_reset(host, SDHCI_RESET_DATA);
946 }
947
948 sdhci_send_command(host, data->stop);
949 } else
950 tasklet_schedule(&host->finish_tasklet);
951}
952
953static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
954{
955 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700956 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700957 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958
959 WARN_ON(host->cmd);
960
Pierre Ossmand129bce2006-03-24 03:18:17 -0800961 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700962 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700963
964 mask = SDHCI_CMD_INHIBIT;
965 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
966 mask |= SDHCI_DATA_INHIBIT;
967
968 /* We shouldn't wait for data inihibit for stop commands, even
969 though they might use busy signaling */
970 if (host->mrq->data && (cmd == host->mrq->data->stop))
971 mask &= ~SDHCI_DATA_INHIBIT;
972
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300973 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700974 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530975 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100976 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200978 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 tasklet_schedule(&host->finish_tasklet);
980 return;
981 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700982 timeout--;
983 mdelay(1);
984 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800985
986 mod_timer(&host->timer, jiffies + 10 * HZ);
987
988 host->cmd = cmd;
989
Andrei Warkentina3c77782011-04-11 16:13:42 -0500990 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800991
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300992 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993
Andrei Warkentine89d4562011-05-23 15:06:37 -0500994 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700995
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530997 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200999 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000 tasklet_schedule(&host->finish_tasklet);
1001 return;
1002 }
1003
1004 if (!(cmd->flags & MMC_RSP_PRESENT))
1005 flags = SDHCI_CMD_RESP_NONE;
1006 else if (cmd->flags & MMC_RSP_136)
1007 flags = SDHCI_CMD_RESP_LONG;
1008 else if (cmd->flags & MMC_RSP_BUSY)
1009 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1010 else
1011 flags = SDHCI_CMD_RESP_SHORT;
1012
1013 if (cmd->flags & MMC_RSP_CRC)
1014 flags |= SDHCI_CMD_CRC;
1015 if (cmd->flags & MMC_RSP_OPCODE)
1016 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301017
1018 /* CMD19 is special in that the Data Present Select should be set */
1019 if (cmd->data || (cmd->opcode == MMC_SEND_TUNING_BLOCK))
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 flags |= SDHCI_CMD_DATA;
1021
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001022 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023}
1024
1025static void sdhci_finish_command(struct sdhci_host *host)
1026{
1027 int i;
1028
1029 BUG_ON(host->cmd == NULL);
1030
1031 if (host->cmd->flags & MMC_RSP_PRESENT) {
1032 if (host->cmd->flags & MMC_RSP_136) {
1033 /* CRC is stripped so we need to do some shifting. */
1034 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001035 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 SDHCI_RESPONSE + (3-i)*4) << 8;
1037 if (i != 3)
1038 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001039 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 SDHCI_RESPONSE + (3-i)*4-1);
1041 }
1042 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001043 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 }
1045 }
1046
Pierre Ossman17b04292007-07-22 22:18:46 +02001047 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001048
Andrei Warkentine89d4562011-05-23 15:06:37 -05001049 /* Finished CMD23, now send actual command. */
1050 if (host->cmd == host->mrq->sbc) {
1051 host->cmd = NULL;
1052 sdhci_send_command(host, host->mrq->cmd);
1053 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001054
Andrei Warkentine89d4562011-05-23 15:06:37 -05001055 /* Processed actual command. */
1056 if (host->data && host->data_early)
1057 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058
Andrei Warkentine89d4562011-05-23 15:06:37 -05001059 if (!host->cmd->data)
1060 tasklet_schedule(&host->finish_tasklet);
1061
1062 host->cmd = NULL;
1063 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064}
1065
1066static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1067{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301068 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001069 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301070 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001071 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001072
1073 if (clock == host->clock)
1074 return;
1075
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001076 host->mmc->actual_clock = 0;
1077
Anton Vorontsov81146342009-03-17 00:13:59 +03001078 if (host->ops->set_clock) {
1079 host->ops->set_clock(host, clock);
1080 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1081 return;
1082 }
1083
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001084 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085
1086 if (clock == 0)
1087 goto out;
1088
Zhangfei Gao85105c52010-08-06 07:10:01 +08001089 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301090 /*
1091 * Check if the Host Controller supports Programmable Clock
1092 * Mode.
1093 */
1094 if (host->clk_mul) {
1095 u16 ctrl;
1096
1097 /*
1098 * We need to figure out whether the Host Driver needs
1099 * to select Programmable Clock Mode, or the value can
1100 * be set automatically by the Host Controller based on
1101 * the Preset Value registers.
1102 */
1103 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1104 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1105 for (div = 1; div <= 1024; div++) {
1106 if (((host->max_clk * host->clk_mul) /
1107 div) <= clock)
1108 break;
1109 }
1110 /*
1111 * Set Programmable Clock Mode in the Clock
1112 * Control register.
1113 */
1114 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001115 real_div = div;
1116 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301117 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001118 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301119 } else {
1120 /* Version 3.00 divisors must be a multiple of 2. */
1121 if (host->max_clk <= clock)
1122 div = 1;
1123 else {
1124 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1125 div += 2) {
1126 if ((host->max_clk / div) <= clock)
1127 break;
1128 }
1129 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001130 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301131 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001132 }
1133 } else {
1134 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001135 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001136 if ((host->max_clk / div) <= clock)
1137 break;
1138 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001139 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301140 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001143 if (real_div)
1144 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1145
Arindam Nathc3ed3872011-05-05 12:19:06 +05301146 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001147 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1148 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001149 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001150 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151
Chris Ball27f6cb12009-09-22 16:45:31 -07001152 /* Wait max 20 ms */
1153 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001154 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001155 & SDHCI_CLOCK_INT_STABLE)) {
1156 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301157 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001158 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159 sdhci_dumpregs(host);
1160 return;
1161 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001162 timeout--;
1163 mdelay(1);
1164 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001165
1166 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001167 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168
1169out:
1170 host->clock = clock;
1171}
1172
Adrian Hunterceb61432011-12-27 15:48:41 +02001173static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001174{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001175 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001176
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001177 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001178 switch (1 << power) {
1179 case MMC_VDD_165_195:
1180 pwr = SDHCI_POWER_180;
1181 break;
1182 case MMC_VDD_29_30:
1183 case MMC_VDD_30_31:
1184 pwr = SDHCI_POWER_300;
1185 break;
1186 case MMC_VDD_32_33:
1187 case MMC_VDD_33_34:
1188 pwr = SDHCI_POWER_330;
1189 break;
1190 default:
1191 BUG();
1192 }
1193 }
1194
1195 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001196 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001197
Pierre Ossmanae628902009-05-03 20:45:03 +02001198 host->pwr = pwr;
1199
1200 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001201 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001202 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001203 }
1204
1205 /*
1206 * Spec says that we should clear the power reg before setting
1207 * a new value. Some controllers don't seem to like this though.
1208 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001209 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001210 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001211
Andres Salomone08c1692008-07-04 10:00:03 -07001212 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001213 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001214 * and set turn on power at the same time, so set the voltage first.
1215 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001216 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001217 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1218
1219 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001220
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001221 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001222
1223 /*
1224 * Some controllers need an extra 10ms delay of 10ms before they
1225 * can apply clock after applying power
1226 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001227 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001228 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001229
1230 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001231}
1232
Pierre Ossmand129bce2006-03-24 03:18:17 -08001233/*****************************************************************************\
1234 * *
1235 * MMC callbacks *
1236 * *
1237\*****************************************************************************/
1238
1239static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1240{
1241 struct sdhci_host *host;
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001242 bool present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243 unsigned long flags;
1244
1245 host = mmc_priv(mmc);
1246
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001247 sdhci_runtime_pm_get(host);
1248
Pierre Ossmand129bce2006-03-24 03:18:17 -08001249 spin_lock_irqsave(&host->lock, flags);
1250
1251 WARN_ON(host->mrq != NULL);
1252
Pierre Ossmanf9134312008-12-21 17:01:48 +01001253#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001255#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001256
1257 /*
1258 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1259 * requests if Auto-CMD12 is enabled.
1260 */
1261 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001262 if (mrq->stop) {
1263 mrq->data->stop = NULL;
1264 mrq->stop = NULL;
1265 }
1266 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001267
1268 host->mrq = mrq;
1269
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001270 /* If polling, assume that the card is always present. */
1271 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1272 present = true;
1273 else
1274 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1275 SDHCI_CARD_PRESENT;
1276
1277 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001278 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001279 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301280 } else {
1281 u32 present_state;
1282
1283 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1284 /*
1285 * Check if the re-tuning timer has already expired and there
1286 * is no on-going data transfer. If so, we need to execute
1287 * tuning procedure before sending command.
1288 */
1289 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1290 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
1291 spin_unlock_irqrestore(&host->lock, flags);
1292 sdhci_execute_tuning(mmc);
1293 spin_lock_irqsave(&host->lock, flags);
1294
1295 /* Restore original mmc_request structure */
1296 host->mrq = mrq;
1297 }
1298
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001299 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001300 sdhci_send_command(host, mrq->sbc);
1301 else
1302 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301303 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001304
Pierre Ossman5f25a662006-10-04 02:15:39 -07001305 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 spin_unlock_irqrestore(&host->lock, flags);
1307}
1308
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001309static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001310{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001312 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 u8 ctrl;
1314
Pierre Ossmand129bce2006-03-24 03:18:17 -08001315 spin_lock_irqsave(&host->lock, flags);
1316
Adrian Hunterceb61432011-12-27 15:48:41 +02001317 if (host->flags & SDHCI_DEVICE_DEAD) {
1318 spin_unlock_irqrestore(&host->lock, flags);
1319 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1320 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1321 return;
1322 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001323
Pierre Ossmand129bce2006-03-24 03:18:17 -08001324 /*
1325 * Reset the chip on each power off.
1326 * Should clear out any weird states.
1327 */
1328 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001329 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001330 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001331 }
1332
1333 sdhci_set_clock(host, ios->clock);
1334
1335 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001336 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001337 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001338 vdd_bit = sdhci_set_power(host, ios->vdd);
1339
1340 if (host->vmmc && vdd_bit != -1) {
1341 spin_unlock_irqrestore(&host->lock, flags);
1342 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1343 spin_lock_irqsave(&host->lock, flags);
1344 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345
Philip Rakity643a81f2010-09-23 08:24:32 -07001346 if (host->ops->platform_send_init_74_clocks)
1347 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1348
Philip Rakity15ec4462010-11-19 16:48:39 -05001349 /*
1350 * If your platform has 8-bit width support but is not a v3 controller,
1351 * or if it requires special setup code, you should implement that in
1352 * platform_8bit_width().
1353 */
1354 if (host->ops->platform_8bit_width)
1355 host->ops->platform_8bit_width(host, ios->bus_width);
1356 else {
1357 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1358 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1359 ctrl &= ~SDHCI_CTRL_4BITBUS;
1360 if (host->version >= SDHCI_SPEC_300)
1361 ctrl |= SDHCI_CTRL_8BITBUS;
1362 } else {
1363 if (host->version >= SDHCI_SPEC_300)
1364 ctrl &= ~SDHCI_CTRL_8BITBUS;
1365 if (ios->bus_width == MMC_BUS_WIDTH_4)
1366 ctrl |= SDHCI_CTRL_4BITBUS;
1367 else
1368 ctrl &= ~SDHCI_CTRL_4BITBUS;
1369 }
1370 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1371 }
1372
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001373 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001374
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001375 if ((ios->timing == MMC_TIMING_SD_HS ||
1376 ios->timing == MMC_TIMING_MMC_HS)
1377 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001378 ctrl |= SDHCI_CTRL_HISPD;
1379 else
1380 ctrl &= ~SDHCI_CTRL_HISPD;
1381
Arindam Nathd6d50a12011-05-05 12:18:59 +05301382 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301383 u16 clk, ctrl_2;
1384 unsigned int clock;
1385
1386 /* In case of UHS-I modes, set High Speed Enable */
1387 if ((ios->timing == MMC_TIMING_UHS_SDR50) ||
1388 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1389 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1390 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1391 (ios->timing == MMC_TIMING_UHS_SDR12))
1392 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301393
1394 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1395 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301396 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301397 /*
1398 * We only need to set Driver Strength if the
1399 * preset value enable is not set.
1400 */
1401 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1402 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1403 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1404 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1405 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1406
1407 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301408 } else {
1409 /*
1410 * According to SDHC Spec v3.00, if the Preset Value
1411 * Enable in the Host Control 2 register is set, we
1412 * need to reset SD Clock Enable before changing High
1413 * Speed Enable to avoid generating clock gliches.
1414 */
Arindam Nath758535c2011-05-05 12:19:00 +05301415
1416 /* Reset SD Clock Enable */
1417 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1418 clk &= ~SDHCI_CLOCK_CARD_EN;
1419 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1420
1421 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1422
1423 /* Re-enable SD Clock */
1424 clock = host->clock;
1425 host->clock = 0;
1426 sdhci_set_clock(host, clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301427 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301428
Arindam Nath49c468f2011-05-05 12:19:01 +05301429
1430 /* Reset SD Clock Enable */
1431 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1432 clk &= ~SDHCI_CLOCK_CARD_EN;
1433 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1434
Philip Rakity6322cdd2011-05-13 11:17:15 +05301435 if (host->ops->set_uhs_signaling)
1436 host->ops->set_uhs_signaling(host, ios->timing);
1437 else {
1438 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1439 /* Select Bus Speed Mode for host */
1440 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1441 if (ios->timing == MMC_TIMING_UHS_SDR12)
1442 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1443 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1444 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1445 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1446 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1447 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1448 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1449 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1450 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1451 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1452 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301453
1454 /* Re-enable SD Clock */
1455 clock = host->clock;
1456 host->clock = 0;
1457 sdhci_set_clock(host, clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301458 } else
1459 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301460
Leandro Dorileob8352262007-07-25 23:47:04 +02001461 /*
1462 * Some (ENE) controllers go apeshit on some ios operation,
1463 * signalling timeout and CRC errors even on CMD0. Resetting
1464 * it on each ios seems to solve the problem.
1465 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001466 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001467 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1468
Pierre Ossman5f25a662006-10-04 02:15:39 -07001469 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001470 spin_unlock_irqrestore(&host->lock, flags);
1471}
1472
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001473static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1474{
1475 struct sdhci_host *host = mmc_priv(mmc);
1476
1477 sdhci_runtime_pm_get(host);
1478 sdhci_do_set_ios(host, ios);
1479 sdhci_runtime_pm_put(host);
1480}
1481
1482static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001483{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001484 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001485 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001486
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487 spin_lock_irqsave(&host->lock, flags);
1488
Pierre Ossman1e728592008-04-16 19:13:13 +02001489 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001490 is_readonly = 0;
1491 else if (host->ops->get_ro)
1492 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001493 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001494 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1495 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001496
1497 spin_unlock_irqrestore(&host->lock, flags);
1498
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001499 /* This quirk needs to be replaced by a callback-function later */
1500 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1501 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001502}
1503
Takashi Iwai82b0e232011-04-21 20:26:38 +02001504#define SAMPLE_COUNT 5
1505
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001506static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001507{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001508 int i, ro_count;
1509
Takashi Iwai82b0e232011-04-21 20:26:38 +02001510 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001511 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001512
1513 ro_count = 0;
1514 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001515 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001516 if (++ro_count > SAMPLE_COUNT / 2)
1517 return 1;
1518 }
1519 msleep(30);
1520 }
1521 return 0;
1522}
1523
Adrian Hunter20758b62011-08-29 16:42:12 +03001524static void sdhci_hw_reset(struct mmc_host *mmc)
1525{
1526 struct sdhci_host *host = mmc_priv(mmc);
1527
1528 if (host->ops && host->ops->hw_reset)
1529 host->ops->hw_reset(host);
1530}
1531
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001532static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001533{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001534 struct sdhci_host *host = mmc_priv(mmc);
1535 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001536
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001537 sdhci_runtime_pm_get(host);
1538 ret = sdhci_do_get_ro(host);
1539 sdhci_runtime_pm_put(host);
1540 return ret;
1541}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001542
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001543static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1544{
Pierre Ossman1e728592008-04-16 19:13:13 +02001545 if (host->flags & SDHCI_DEVICE_DEAD)
1546 goto out;
1547
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001548 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001549 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1550 else
1551 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1552
1553 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1554 if (host->runtime_suspended)
1555 goto out;
1556
1557 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001558 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1559 else
1560 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001561out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001562 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001563}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001564
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001565static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1566{
1567 struct sdhci_host *host = mmc_priv(mmc);
1568 unsigned long flags;
1569
1570 spin_lock_irqsave(&host->lock, flags);
1571 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001572 spin_unlock_irqrestore(&host->lock, flags);
1573}
1574
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001575static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1576 struct mmc_ios *ios)
Arindam Nathf2119df2011-05-05 12:18:57 +05301577{
Arindam Nathf2119df2011-05-05 12:18:57 +05301578 u8 pwr;
1579 u16 clk, ctrl;
1580 u32 present_state;
1581
Arindam Nathf2119df2011-05-05 12:18:57 +05301582 /*
1583 * Signal Voltage Switching is only applicable for Host Controllers
1584 * v3.00 and above.
1585 */
1586 if (host->version < SDHCI_SPEC_300)
1587 return 0;
1588
1589 /*
1590 * We first check whether the request is to set signalling voltage
1591 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1592 */
1593 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1594 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1595 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1596 ctrl &= ~SDHCI_CTRL_VDD_180;
1597 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1598
1599 /* Wait for 5ms */
1600 usleep_range(5000, 5500);
1601
1602 /* 3.3V regulator output should be stable within 5 ms */
1603 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1604 if (!(ctrl & SDHCI_CTRL_VDD_180))
1605 return 0;
1606 else {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301607 pr_info(DRIVER_NAME ": Switching to 3.3V "
Arindam Nathf2119df2011-05-05 12:18:57 +05301608 "signalling voltage failed\n");
1609 return -EIO;
1610 }
1611 } else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1612 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)) {
1613 /* Stop SDCLK */
1614 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1615 clk &= ~SDHCI_CLOCK_CARD_EN;
1616 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1617
1618 /* Check whether DAT[3:0] is 0000 */
1619 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1620 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1621 SDHCI_DATA_LVL_SHIFT)) {
1622 /*
1623 * Enable 1.8V Signal Enable in the Host Control2
1624 * register
1625 */
1626 ctrl |= SDHCI_CTRL_VDD_180;
1627 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1628
1629 /* Wait for 5ms */
1630 usleep_range(5000, 5500);
1631
1632 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1633 if (ctrl & SDHCI_CTRL_VDD_180) {
1634 /* Provide SDCLK again and wait for 1ms*/
1635 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1636 clk |= SDHCI_CLOCK_CARD_EN;
1637 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1638 usleep_range(1000, 1500);
1639
1640 /*
1641 * If DAT[3:0] level is 1111b, then the card
1642 * was successfully switched to 1.8V signaling.
1643 */
1644 present_state = sdhci_readl(host,
1645 SDHCI_PRESENT_STATE);
1646 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1647 SDHCI_DATA_LVL_MASK)
1648 return 0;
1649 }
1650 }
1651
1652 /*
1653 * If we are here, that means the switch to 1.8V signaling
1654 * failed. We power cycle the card, and retry initialization
1655 * sequence by setting S18R to 0.
1656 */
1657 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1658 pwr &= ~SDHCI_POWER_ON;
1659 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1660
1661 /* Wait for 1ms as per the spec */
1662 usleep_range(1000, 1500);
1663 pwr |= SDHCI_POWER_ON;
1664 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1665
Girish K Sa3c76eb2011-10-11 11:44:09 +05301666 pr_info(DRIVER_NAME ": Switching to 1.8V signalling "
Arindam Nathf2119df2011-05-05 12:18:57 +05301667 "voltage failed, retrying with S18R set to 0\n");
1668 return -EAGAIN;
1669 } else
1670 /* No signal voltage switch required */
1671 return 0;
1672}
1673
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001674static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1675 struct mmc_ios *ios)
1676{
1677 struct sdhci_host *host = mmc_priv(mmc);
1678 int err;
1679
1680 if (host->version < SDHCI_SPEC_300)
1681 return 0;
1682 sdhci_runtime_pm_get(host);
1683 err = sdhci_do_start_signal_voltage_switch(host, ios);
1684 sdhci_runtime_pm_put(host);
1685 return err;
1686}
1687
Arindam Nathb513ea22011-05-05 12:19:04 +05301688static int sdhci_execute_tuning(struct mmc_host *mmc)
1689{
1690 struct sdhci_host *host;
1691 u16 ctrl;
1692 u32 ier;
1693 int tuning_loop_counter = MAX_TUNING_LOOP;
1694 unsigned long timeout;
1695 int err = 0;
1696
1697 host = mmc_priv(mmc);
1698
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001699 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301700 disable_irq(host->irq);
1701 spin_lock(&host->lock);
1702
1703 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1704
1705 /*
1706 * Host Controller needs tuning only in case of SDR104 mode
1707 * and for SDR50 mode when Use Tuning for SDR50 is set in
1708 * Capabilities register.
1709 */
1710 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
1711 (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1712 (host->flags & SDHCI_SDR50_NEEDS_TUNING)))
1713 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1714 else {
1715 spin_unlock(&host->lock);
1716 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001717 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301718 return 0;
1719 }
1720
1721 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1722
1723 /*
1724 * As per the Host Controller spec v3.00, tuning command
1725 * generates Buffer Read Ready interrupt, so enable that.
1726 *
1727 * Note: The spec clearly says that when tuning sequence
1728 * is being performed, the controller does not generate
1729 * interrupts other than Buffer Read Ready interrupt. But
1730 * to make sure we don't hit a controller bug, we _only_
1731 * enable Buffer Read Ready interrupt here.
1732 */
1733 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1734 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1735
1736 /*
1737 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1738 * of loops reaches 40 times or a timeout of 150ms occurs.
1739 */
1740 timeout = 150;
1741 do {
1742 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001743 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301744
1745 if (!tuning_loop_counter && !timeout)
1746 break;
1747
1748 cmd.opcode = MMC_SEND_TUNING_BLOCK;
1749 cmd.arg = 0;
1750 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1751 cmd.retries = 0;
1752 cmd.data = NULL;
1753 cmd.error = 0;
1754
1755 mrq.cmd = &cmd;
1756 host->mrq = &mrq;
1757
1758 /*
1759 * In response to CMD19, the card sends 64 bytes of tuning
1760 * block to the Host Controller. So we set the block size
1761 * to 64 here.
1762 */
1763 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
1764
1765 /*
1766 * The tuning block is sent by the card to the host controller.
1767 * So we set the TRNS_READ bit in the Transfer Mode register.
1768 * This also takes care of setting DMA Enable and Multi Block
1769 * Select in the same register to 0.
1770 */
1771 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1772
1773 sdhci_send_command(host, &cmd);
1774
1775 host->cmd = NULL;
1776 host->mrq = NULL;
1777
1778 spin_unlock(&host->lock);
1779 enable_irq(host->irq);
1780
1781 /* Wait for Buffer Read Ready interrupt */
1782 wait_event_interruptible_timeout(host->buf_ready_int,
1783 (host->tuning_done == 1),
1784 msecs_to_jiffies(50));
1785 disable_irq(host->irq);
1786 spin_lock(&host->lock);
1787
1788 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301789 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301790 "Buffer Read Ready interrupt during tuning "
1791 "procedure, falling back to fixed sampling "
1792 "clock\n");
1793 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1794 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1795 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1796 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1797
1798 err = -EIO;
1799 goto out;
1800 }
1801
1802 host->tuning_done = 0;
1803
1804 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1805 tuning_loop_counter--;
1806 timeout--;
1807 mdelay(1);
1808 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1809
1810 /*
1811 * The Host Driver has exhausted the maximum number of loops allowed,
1812 * so use fixed sampling frequency.
1813 */
1814 if (!tuning_loop_counter || !timeout) {
1815 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1816 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1817 } else {
1818 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301819 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301820 " failed, falling back to fixed sampling"
1821 " clock\n");
1822 err = -EIO;
1823 }
1824 }
1825
1826out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301827 /*
1828 * If this is the very first time we are here, we start the retuning
1829 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1830 * flag won't be set, we check this condition before actually starting
1831 * the timer.
1832 */
1833 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1834 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
1835 mod_timer(&host->tuning_timer, jiffies +
1836 host->tuning_count * HZ);
1837 /* Tuning mode 1 limits the maximum data length to 4MB */
1838 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1839 } else {
1840 host->flags &= ~SDHCI_NEEDS_RETUNING;
1841 /* Reload the new initial value for timer */
1842 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1843 mod_timer(&host->tuning_timer, jiffies +
1844 host->tuning_count * HZ);
1845 }
1846
1847 /*
1848 * In case tuning fails, host controllers which support re-tuning can
1849 * try tuning again at a later time, when the re-tuning timer expires.
1850 * So for these controllers, we return 0. Since there might be other
1851 * controllers who do not have this capability, we return error for
1852 * them.
1853 */
1854 if (err && host->tuning_count &&
1855 host->tuning_mode == SDHCI_TUNING_MODE_1)
1856 err = 0;
1857
Arindam Nathb513ea22011-05-05 12:19:04 +05301858 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1859 spin_unlock(&host->lock);
1860 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001861 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301862
1863 return err;
1864}
1865
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001866static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301867{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301868 u16 ctrl;
1869 unsigned long flags;
1870
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301871 /* Host Controller v3.00 defines preset value registers */
1872 if (host->version < SDHCI_SPEC_300)
1873 return;
1874
1875 spin_lock_irqsave(&host->lock, flags);
1876
1877 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1878
1879 /*
1880 * We only enable or disable Preset Value if they are not already
1881 * enabled or disabled respectively. Otherwise, we bail out.
1882 */
1883 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1884 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1885 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001886 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301887 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1888 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1889 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001890 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301891 }
1892
1893 spin_unlock_irqrestore(&host->lock, flags);
1894}
1895
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001896static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1897{
1898 struct sdhci_host *host = mmc_priv(mmc);
1899
1900 sdhci_runtime_pm_get(host);
1901 sdhci_do_enable_preset_value(host, enable);
1902 sdhci_runtime_pm_put(host);
1903}
1904
David Brownellab7aefd2006-11-12 17:55:30 -08001905static const struct mmc_host_ops sdhci_ops = {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001906 .request = sdhci_request,
1907 .set_ios = sdhci_set_ios,
1908 .get_ro = sdhci_get_ro,
Adrian Hunter20758b62011-08-29 16:42:12 +03001909 .hw_reset = sdhci_hw_reset,
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001910 .enable_sdio_irq = sdhci_enable_sdio_irq,
Arindam Nathf2119df2011-05-05 12:18:57 +05301911 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Arindam Nathb513ea22011-05-05 12:19:04 +05301912 .execute_tuning = sdhci_execute_tuning,
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301913 .enable_preset_value = sdhci_enable_preset_value,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001914};
1915
1916/*****************************************************************************\
1917 * *
1918 * Tasklets *
1919 * *
1920\*****************************************************************************/
1921
1922static void sdhci_tasklet_card(unsigned long param)
1923{
1924 struct sdhci_host *host;
1925 unsigned long flags;
1926
1927 host = (struct sdhci_host*)param;
1928
1929 spin_lock_irqsave(&host->lock, flags);
1930
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001931 /* Check host->mrq first in case we are runtime suspended */
1932 if (host->mrq &&
1933 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301934 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001935 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05301936 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001937 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001938
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001939 sdhci_reset(host, SDHCI_RESET_CMD);
1940 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001941
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001942 host->mrq->cmd->error = -ENOMEDIUM;
1943 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001944 }
1945
1946 spin_unlock_irqrestore(&host->lock, flags);
1947
Pierre Ossman04cf5852008-08-18 22:18:14 +02001948 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001949}
1950
1951static void sdhci_tasklet_finish(unsigned long param)
1952{
1953 struct sdhci_host *host;
1954 unsigned long flags;
1955 struct mmc_request *mrq;
1956
1957 host = (struct sdhci_host*)param;
1958
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001959 spin_lock_irqsave(&host->lock, flags);
1960
Chris Ball0c9c99a2011-04-27 17:35:31 -04001961 /*
1962 * If this tasklet gets rescheduled while running, it will
1963 * be run again afterwards but without any active request.
1964 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001965 if (!host->mrq) {
1966 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04001967 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001968 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001969
1970 del_timer(&host->timer);
1971
1972 mrq = host->mrq;
1973
Pierre Ossmand129bce2006-03-24 03:18:17 -08001974 /*
1975 * The controller needs a reset of internal state machines
1976 * upon error conditions.
1977 */
Pierre Ossman1e728592008-04-16 19:13:13 +02001978 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01001979 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02001980 (mrq->data && (mrq->data->error ||
1981 (mrq->data->stop && mrq->data->stop->error))) ||
1982 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001983
1984 /* Some controllers need this kick or reset won't work here */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001985 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET) {
Pierre Ossman645289d2006-06-30 02:22:33 -07001986 unsigned int clock;
1987
1988 /* This is to force an update */
1989 clock = host->clock;
1990 host->clock = 0;
1991 sdhci_set_clock(host, clock);
1992 }
1993
1994 /* Spec says we should do both at the same time, but Ricoh
1995 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08001996 sdhci_reset(host, SDHCI_RESET_CMD);
1997 sdhci_reset(host, SDHCI_RESET_DATA);
1998 }
1999
2000 host->mrq = NULL;
2001 host->cmd = NULL;
2002 host->data = NULL;
2003
Pierre Ossmanf9134312008-12-21 17:01:48 +01002004#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002005 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002006#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002007
Pierre Ossman5f25a662006-10-04 02:15:39 -07002008 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002009 spin_unlock_irqrestore(&host->lock, flags);
2010
2011 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002012 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002013}
2014
2015static void sdhci_timeout_timer(unsigned long data)
2016{
2017 struct sdhci_host *host;
2018 unsigned long flags;
2019
2020 host = (struct sdhci_host*)data;
2021
2022 spin_lock_irqsave(&host->lock, flags);
2023
2024 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302025 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002026 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002027 sdhci_dumpregs(host);
2028
2029 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002030 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002031 sdhci_finish_data(host);
2032 } else {
2033 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002034 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002035 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002036 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002037
2038 tasklet_schedule(&host->finish_tasklet);
2039 }
2040 }
2041
Pierre Ossman5f25a662006-10-04 02:15:39 -07002042 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002043 spin_unlock_irqrestore(&host->lock, flags);
2044}
2045
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302046static void sdhci_tuning_timer(unsigned long data)
2047{
2048 struct sdhci_host *host;
2049 unsigned long flags;
2050
2051 host = (struct sdhci_host *)data;
2052
2053 spin_lock_irqsave(&host->lock, flags);
2054
2055 host->flags |= SDHCI_NEEDS_RETUNING;
2056
2057 spin_unlock_irqrestore(&host->lock, flags);
2058}
2059
Pierre Ossmand129bce2006-03-24 03:18:17 -08002060/*****************************************************************************\
2061 * *
2062 * Interrupt handling *
2063 * *
2064\*****************************************************************************/
2065
2066static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2067{
2068 BUG_ON(intmask == 0);
2069
2070 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302071 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002072 "though no command operation was in progress.\n",
2073 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002074 sdhci_dumpregs(host);
2075 return;
2076 }
2077
Pierre Ossman43b58b32007-07-25 23:15:27 +02002078 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002079 host->cmd->error = -ETIMEDOUT;
2080 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2081 SDHCI_INT_INDEX))
2082 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002083
Pierre Ossmane8095172008-07-25 01:09:08 +02002084 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002085 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002086 return;
2087 }
2088
2089 /*
2090 * The host can send and interrupt when the busy state has
2091 * ended, allowing us to wait without wasting CPU cycles.
2092 * Unfortunately this is overloaded on the "data complete"
2093 * interrupt, so we need to take some care when handling
2094 * it.
2095 *
2096 * Note: The 1.0 specification is a bit ambiguous about this
2097 * feature so there might be some problems with older
2098 * controllers.
2099 */
2100 if (host->cmd->flags & MMC_RSP_BUSY) {
2101 if (host->cmd->data)
2102 DBG("Cannot wait for busy signal when also "
2103 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002104 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002105 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002106
2107 /* The controller does not support the end-of-busy IRQ,
2108 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002109 }
2110
2111 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002112 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113}
2114
George G. Davis0957c332010-02-18 12:32:12 -05002115#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002116static void sdhci_show_adma_error(struct sdhci_host *host)
2117{
2118 const char *name = mmc_hostname(host->mmc);
2119 u8 *desc = host->adma_desc;
2120 __le32 *dma;
2121 __le16 *len;
2122 u8 attr;
2123
2124 sdhci_dumpregs(host);
2125
2126 while (true) {
2127 dma = (__le32 *)(desc + 4);
2128 len = (__le16 *)(desc + 2);
2129 attr = *desc;
2130
2131 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2132 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2133
2134 desc += 8;
2135
2136 if (attr & 2)
2137 break;
2138 }
2139}
2140#else
2141static void sdhci_show_adma_error(struct sdhci_host *host) { }
2142#endif
2143
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2145{
2146 BUG_ON(intmask == 0);
2147
Arindam Nathb513ea22011-05-05 12:19:04 +05302148 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2149 if (intmask & SDHCI_INT_DATA_AVAIL) {
2150 if (SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND)) ==
2151 MMC_SEND_TUNING_BLOCK) {
2152 host->tuning_done = 1;
2153 wake_up(&host->buf_ready_int);
2154 return;
2155 }
2156 }
2157
Pierre Ossmand129bce2006-03-24 03:18:17 -08002158 if (!host->data) {
2159 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002160 * The "data complete" interrupt is also used to
2161 * indicate that a busy state has ended. See comment
2162 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002163 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002164 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2165 if (intmask & SDHCI_INT_DATA_END) {
2166 sdhci_finish_command(host);
2167 return;
2168 }
2169 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002170
Girish K Sa3c76eb2011-10-11 11:44:09 +05302171 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002172 "though no data operation was in progress.\n",
2173 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002174 sdhci_dumpregs(host);
2175
2176 return;
2177 }
2178
2179 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002180 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002181 else if (intmask & SDHCI_INT_DATA_END_BIT)
2182 host->data->error = -EILSEQ;
2183 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2184 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2185 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002186 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002187 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302188 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002189 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002190 host->data->error = -EIO;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002191 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002192
Pierre Ossman17b04292007-07-22 22:18:46 +02002193 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002194 sdhci_finish_data(host);
2195 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002196 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197 sdhci_transfer_pio(host);
2198
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002199 /*
2200 * We currently don't do anything fancy with DMA
2201 * boundaries, but as we can't disable the feature
2202 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002203 *
2204 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2205 * should return a valid address to continue from, but as
2206 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002207 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002208 if (intmask & SDHCI_INT_DMA_END) {
2209 u32 dmastart, dmanow;
2210 dmastart = sg_dma_address(host->data->sg);
2211 dmanow = dmastart + host->data->bytes_xfered;
2212 /*
2213 * Force update to the next DMA block boundary.
2214 */
2215 dmanow = (dmanow &
2216 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2217 SDHCI_DEFAULT_BOUNDARY_SIZE;
2218 host->data->bytes_xfered = dmanow - dmastart;
2219 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2220 " next 0x%08x\n",
2221 mmc_hostname(host->mmc), dmastart,
2222 host->data->bytes_xfered, dmanow);
2223 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2224 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002225
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002226 if (intmask & SDHCI_INT_DATA_END) {
2227 if (host->cmd) {
2228 /*
2229 * Data managed to finish before the
2230 * command completed. Make sure we do
2231 * things in the proper order.
2232 */
2233 host->data_early = 1;
2234 } else {
2235 sdhci_finish_data(host);
2236 }
2237 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238 }
2239}
2240
David Howells7d12e782006-10-05 14:55:46 +01002241static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242{
2243 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002244 struct sdhci_host *host = dev_id;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245 u32 intmask;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002246 int cardint = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002247
2248 spin_lock(&host->lock);
2249
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002250 if (host->runtime_suspended) {
2251 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302252 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002253 mmc_hostname(host->mmc));
2254 return IRQ_HANDLED;
2255 }
2256
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002257 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002258
Mark Lord62df67a52007-03-06 13:30:13 +01002259 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 result = IRQ_NONE;
2261 goto out;
2262 }
2263
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002264 DBG("*** %s got interrupt: 0x%08x\n",
2265 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002266
Pierre Ossman3192a282006-06-30 02:22:26 -07002267 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002268 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2269 SDHCI_CARD_PRESENT;
2270
2271 /*
2272 * There is a observation on i.mx esdhc. INSERT bit will be
2273 * immediately set again when it gets cleared, if a card is
2274 * inserted. We have to mask the irq to prevent interrupt
2275 * storm which will freeze the system. And the REMOVE gets
2276 * the same situation.
2277 *
2278 * More testing are needed here to ensure it works for other
2279 * platforms though.
2280 */
2281 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2282 SDHCI_INT_CARD_REMOVE);
2283 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2284 SDHCI_INT_CARD_INSERT);
2285
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002286 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002287 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2288 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002290 }
2291
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002293 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2294 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002295 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002296 }
2297
2298 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002299 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2300 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002301 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302 }
2303
2304 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2305
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002306 intmask &= ~SDHCI_INT_ERROR;
2307
Pierre Ossmand129bce2006-03-24 03:18:17 -08002308 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302309 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002311 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002312 }
2313
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002314 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002315
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002316 if (intmask & SDHCI_INT_CARD_INT)
2317 cardint = 1;
2318
2319 intmask &= ~SDHCI_INT_CARD_INT;
2320
Pierre Ossman3192a282006-06-30 02:22:26 -07002321 if (intmask) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302322 pr_err("%s: Unexpected interrupt 0x%08x.\n",
Pierre Ossman3192a282006-06-30 02:22:26 -07002323 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324 sdhci_dumpregs(host);
2325
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002326 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002327 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328
2329 result = IRQ_HANDLED;
2330
Pierre Ossman5f25a662006-10-04 02:15:39 -07002331 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002332out:
2333 spin_unlock(&host->lock);
2334
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002335 /*
2336 * We have to delay this as it calls back into the driver.
2337 */
2338 if (cardint)
2339 mmc_signal_sdio_irq(host->mmc);
2340
Pierre Ossmand129bce2006-03-24 03:18:17 -08002341 return result;
2342}
2343
2344/*****************************************************************************\
2345 * *
2346 * Suspend/resume *
2347 * *
2348\*****************************************************************************/
2349
2350#ifdef CONFIG_PM
2351
Manuel Lauss29495aa2011-11-03 11:09:45 +01002352int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002353{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002354 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002355
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002356 sdhci_disable_card_detection(host);
2357
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302358 /* Disable tuning since we are suspending */
2359 if (host->version >= SDHCI_SPEC_300 && host->tuning_count &&
2360 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2361 host->flags &= ~SDHCI_NEEDS_RETUNING;
2362 mod_timer(&host->tuning_timer, jiffies +
2363 host->tuning_count * HZ);
2364 }
2365
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002366 ret = mmc_suspend_host(host->mmc);
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002367 if (ret)
2368 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002370 free_irq(host->irq, host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002371
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002372 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002373}
2374
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002375EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002377int sdhci_resume_host(struct sdhci_host *host)
2378{
2379 int ret;
2380
Richard Röjforsa13abc72009-09-22 16:45:30 -07002381 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002382 if (host->ops->enable_dma)
2383 host->ops->enable_dma(host);
2384 }
2385
2386 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2387 mmc_hostname(host->mmc), host);
2388 if (ret)
2389 return ret;
2390
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002391 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002392 mmiowb();
2393
2394 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002395 sdhci_enable_card_detection(host);
2396
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302397 /* Set the re-tuning expiration flag */
2398 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2399 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2400 host->flags |= SDHCI_NEEDS_RETUNING;
2401
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002402 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002403}
2404
2405EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406
Daniel Drake5f619702010-11-04 22:20:39 +00002407void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2408{
2409 u8 val;
2410 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2411 val |= SDHCI_WAKE_ON_INT;
2412 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2413}
2414
2415EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2416
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417#endif /* CONFIG_PM */
2418
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002419#ifdef CONFIG_PM_RUNTIME
2420
2421static int sdhci_runtime_pm_get(struct sdhci_host *host)
2422{
2423 return pm_runtime_get_sync(host->mmc->parent);
2424}
2425
2426static int sdhci_runtime_pm_put(struct sdhci_host *host)
2427{
2428 pm_runtime_mark_last_busy(host->mmc->parent);
2429 return pm_runtime_put_autosuspend(host->mmc->parent);
2430}
2431
2432int sdhci_runtime_suspend_host(struct sdhci_host *host)
2433{
2434 unsigned long flags;
2435 int ret = 0;
2436
2437 /* Disable tuning since we are suspending */
2438 if (host->version >= SDHCI_SPEC_300 &&
2439 host->tuning_mode == SDHCI_TUNING_MODE_1) {
2440 del_timer_sync(&host->tuning_timer);
2441 host->flags &= ~SDHCI_NEEDS_RETUNING;
2442 }
2443
2444 spin_lock_irqsave(&host->lock, flags);
2445 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2446 spin_unlock_irqrestore(&host->lock, flags);
2447
2448 synchronize_irq(host->irq);
2449
2450 spin_lock_irqsave(&host->lock, flags);
2451 host->runtime_suspended = true;
2452 spin_unlock_irqrestore(&host->lock, flags);
2453
2454 return ret;
2455}
2456EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2457
2458int sdhci_runtime_resume_host(struct sdhci_host *host)
2459{
2460 unsigned long flags;
2461 int ret = 0, host_flags = host->flags;
2462
2463 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2464 if (host->ops->enable_dma)
2465 host->ops->enable_dma(host);
2466 }
2467
2468 sdhci_init(host, 0);
2469
2470 /* Force clock and power re-program */
2471 host->pwr = 0;
2472 host->clock = 0;
2473 sdhci_do_set_ios(host, &host->mmc->ios);
2474
2475 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2476 if (host_flags & SDHCI_PV_ENABLED)
2477 sdhci_do_enable_preset_value(host, true);
2478
2479 /* Set the re-tuning expiration flag */
2480 if ((host->version >= SDHCI_SPEC_300) && host->tuning_count &&
2481 (host->tuning_mode == SDHCI_TUNING_MODE_1))
2482 host->flags |= SDHCI_NEEDS_RETUNING;
2483
2484 spin_lock_irqsave(&host->lock, flags);
2485
2486 host->runtime_suspended = false;
2487
2488 /* Enable SDIO IRQ */
2489 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2490 sdhci_enable_sdio_irq_nolock(host, true);
2491
2492 /* Enable Card Detection */
2493 sdhci_enable_card_detection(host);
2494
2495 spin_unlock_irqrestore(&host->lock, flags);
2496
2497 return ret;
2498}
2499EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2500
2501#endif
2502
Pierre Ossmand129bce2006-03-24 03:18:17 -08002503/*****************************************************************************\
2504 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002505 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002506 * *
2507\*****************************************************************************/
2508
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002509struct sdhci_host *sdhci_alloc_host(struct device *dev,
2510 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002511{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512 struct mmc_host *mmc;
2513 struct sdhci_host *host;
2514
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002515 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002516
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002517 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002519 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002520
2521 host = mmc_priv(mmc);
2522 host->mmc = mmc;
2523
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002524 return host;
2525}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002527EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002529int sdhci_add_host(struct sdhci_host *host)
2530{
2531 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302532 u32 caps[2];
2533 u32 max_current_caps;
2534 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002535 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002536
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002537 WARN_ON(host == NULL);
2538 if (host == NULL)
2539 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002540
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002541 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002542
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002543 if (debug_quirks)
2544 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002545 if (debug_quirks2)
2546 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002547
Pierre Ossmand96649e2006-06-30 02:22:30 -07002548 sdhci_reset(host, SDHCI_RESET_ALL);
2549
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002550 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002551 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2552 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002553 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302554 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002555 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002556 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002557 }
2558
Arindam Nathf2119df2011-05-05 12:18:57 +05302559 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002560 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561
Arindam Nathf2119df2011-05-05 12:18:57 +05302562 caps[1] = (host->version >= SDHCI_SPEC_300) ?
2563 sdhci_readl(host, SDHCI_CAPABILITIES_1) : 0;
2564
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002565 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002566 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302567 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002568 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002569 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002570 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002572 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002573 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002574 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002575 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002576 }
2577
Arindam Nathf2119df2011-05-05 12:18:57 +05302578 if ((host->version >= SDHCI_SPEC_200) &&
2579 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002580 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002581
2582 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2583 (host->flags & SDHCI_USE_ADMA)) {
2584 DBG("Disabling ADMA as it is marked broken\n");
2585 host->flags &= ~SDHCI_USE_ADMA;
2586 }
2587
Richard Röjforsa13abc72009-09-22 16:45:30 -07002588 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002589 if (host->ops->enable_dma) {
2590 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302591 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002592 "available. Falling back to PIO.\n",
2593 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002594 host->flags &=
2595 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002596 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002597 }
2598 }
2599
Pierre Ossman2134a922008-06-28 18:28:51 +02002600 if (host->flags & SDHCI_USE_ADMA) {
2601 /*
2602 * We need to allocate descriptors for all sg entries
2603 * (128) and potentially one alignment transfer for
2604 * each of those entries.
2605 */
2606 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2607 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2608 if (!host->adma_desc || !host->align_buffer) {
2609 kfree(host->adma_desc);
2610 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302611 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002612 "buffers. Falling back to standard DMA.\n",
2613 mmc_hostname(mmc));
2614 host->flags &= ~SDHCI_USE_ADMA;
2615 }
2616 }
2617
Pierre Ossman76591502008-07-21 00:32:11 +02002618 /*
2619 * If we use DMA, then it's up to the caller to set the DMA
2620 * mask, but PIO does not need the hw shim so we set a new
2621 * mask here in that case.
2622 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002623 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002624 host->dma_mask = DMA_BIT_MASK(64);
2625 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2626 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002627
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002628 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302629 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002630 >> SDHCI_CLOCK_BASE_SHIFT;
2631 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302632 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002633 >> SDHCI_CLOCK_BASE_SHIFT;
2634
Pierre Ossmand129bce2006-03-24 03:18:17 -08002635 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002636 if (host->max_clk == 0 || host->quirks &
2637 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002638 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302639 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002640 "frequency.\n", mmc_hostname(mmc));
2641 return -ENODEV;
2642 }
2643 host->max_clk = host->ops->get_max_clock(host);
2644 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002645
2646 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302647 * In case of Host Controller v3.00, find out whether clock
2648 * multiplier is supported.
2649 */
2650 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2651 SDHCI_CLOCK_MUL_SHIFT;
2652
2653 /*
2654 * In case the value in Clock Multiplier is 0, then programmable
2655 * clock mode is not supported, otherwise the actual clock
2656 * multiplier is one more than the value of Clock Multiplier
2657 * in the Capabilities Register.
2658 */
2659 if (host->clk_mul)
2660 host->clk_mul += 1;
2661
2662 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002663 * Set host parameters.
2664 */
2665 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302666 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002667 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002668 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302669 else if (host->version >= SDHCI_SPEC_300) {
2670 if (host->clk_mul) {
2671 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2672 mmc->f_max = host->max_clk * host->clk_mul;
2673 } else
2674 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2675 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002676 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002677
Andy Shevchenko272308c2011-08-03 18:36:00 +03002678 host->timeout_clk =
2679 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2680 if (host->timeout_clk == 0) {
2681 if (host->ops->get_timeout_clock) {
2682 host->timeout_clk = host->ops->get_timeout_clock(host);
2683 } else if (!(host->quirks &
2684 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302685 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002686 "frequency.\n", mmc_hostname(mmc));
2687 return -ENODEV;
2688 }
2689 }
2690 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2691 host->timeout_clk *= 1000;
2692
2693 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002694 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002695
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002696 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002697
Andrei Warkentine89d4562011-05-23 15:06:37 -05002698 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2699
2700 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2701 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002702
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002703 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002704 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002705 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002706 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002707 host->flags |= SDHCI_AUTO_CMD23;
2708 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2709 } else {
2710 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2711 }
2712
Philip Rakity15ec4462010-11-19 16:48:39 -05002713 /*
2714 * A controller may support 8-bit width, but the board itself
2715 * might not have the pins brought out. Boards that support
2716 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2717 * their platform code before calling sdhci_add_host(), and we
2718 * won't assume 8-bit width for hosts without that CAP.
2719 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002720 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002721 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002722
Arindam Nathf2119df2011-05-05 12:18:57 +05302723 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002724 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002725
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002726 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2727 mmc_card_is_removable(mmc))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002728 mmc->caps |= MMC_CAP_NEEDS_POLL;
2729
Arindam Nathf2119df2011-05-05 12:18:57 +05302730 /* UHS-I mode(s) supported by the host controller. */
2731 if (host->version >= SDHCI_SPEC_300)
2732 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2733
2734 /* SDR104 supports also implies SDR50 support */
2735 if (caps[1] & SDHCI_SUPPORT_SDR104)
2736 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2737 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2738 mmc->caps |= MMC_CAP_UHS_SDR50;
2739
2740 if (caps[1] & SDHCI_SUPPORT_DDR50)
2741 mmc->caps |= MMC_CAP_UHS_DDR50;
2742
Arindam Nathb513ea22011-05-05 12:19:04 +05302743 /* Does the host needs tuning for SDR50? */
2744 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2745 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2746
Arindam Nathd6d50a12011-05-05 12:18:59 +05302747 /* Driver Type(s) (A, C, D) supported by the host */
2748 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2749 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2750 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2751 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2752 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2753 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2754
Girish K Sbec87262011-10-13 12:04:16 +05302755 /*
2756 * If Power Off Notify capability is enabled by the host,
2757 * set notify to short power off notify timeout value.
2758 */
2759 if (mmc->caps2 & MMC_CAP2_POWEROFF_NOTIFY)
2760 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2761 else
2762 mmc->power_notify_type = MMC_HOST_PW_NOTIFY_NONE;
2763
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302764 /* Initial value for re-tuning timer count */
2765 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2766 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2767
2768 /*
2769 * In case Re-tuning Timer is not disabled, the actual value of
2770 * re-tuning timer will be 2 ^ (n - 1).
2771 */
2772 if (host->tuning_count)
2773 host->tuning_count = 1 << (host->tuning_count - 1);
2774
2775 /* Re-tuning mode supported by the Host Controller */
2776 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2777 SDHCI_RETUNING_MODE_SHIFT;
2778
Takashi Iwai8f230f42010-12-08 10:04:30 +01002779 ocr_avail = 0;
Arindam Nathf2119df2011-05-05 12:18:57 +05302780 /*
2781 * According to SD Host Controller spec v3.00, if the Host System
2782 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2783 * the value is meaningful only if Voltage Support in the Capabilities
2784 * register is set. The actual current value is 4 times the register
2785 * value.
2786 */
2787 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
2788
2789 if (caps[0] & SDHCI_CAN_VDD_330) {
2790 int max_current_330;
2791
Takashi Iwai8f230f42010-12-08 10:04:30 +01002792 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05302793
2794 max_current_330 = ((max_current_caps &
2795 SDHCI_MAX_CURRENT_330_MASK) >>
2796 SDHCI_MAX_CURRENT_330_SHIFT) *
2797 SDHCI_MAX_CURRENT_MULTIPLIER;
2798
2799 if (max_current_330 > 150)
2800 mmc->caps |= MMC_CAP_SET_XPC_330;
2801 }
2802 if (caps[0] & SDHCI_CAN_VDD_300) {
2803 int max_current_300;
2804
Takashi Iwai8f230f42010-12-08 10:04:30 +01002805 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05302806
2807 max_current_300 = ((max_current_caps &
2808 SDHCI_MAX_CURRENT_300_MASK) >>
2809 SDHCI_MAX_CURRENT_300_SHIFT) *
2810 SDHCI_MAX_CURRENT_MULTIPLIER;
2811
2812 if (max_current_300 > 150)
2813 mmc->caps |= MMC_CAP_SET_XPC_300;
2814 }
2815 if (caps[0] & SDHCI_CAN_VDD_180) {
2816 int max_current_180;
2817
Takashi Iwai8f230f42010-12-08 10:04:30 +01002818 ocr_avail |= MMC_VDD_165_195;
2819
Arindam Nathf2119df2011-05-05 12:18:57 +05302820 max_current_180 = ((max_current_caps &
2821 SDHCI_MAX_CURRENT_180_MASK) >>
2822 SDHCI_MAX_CURRENT_180_SHIFT) *
2823 SDHCI_MAX_CURRENT_MULTIPLIER;
2824
2825 if (max_current_180 > 150)
2826 mmc->caps |= MMC_CAP_SET_XPC_180;
Arindam Nath5371c922011-05-05 12:19:02 +05302827
2828 /* Maximum current capabilities of the host at 1.8V */
2829 if (max_current_180 >= 800)
2830 mmc->caps |= MMC_CAP_MAX_CURRENT_800;
2831 else if (max_current_180 >= 600)
2832 mmc->caps |= MMC_CAP_MAX_CURRENT_600;
2833 else if (max_current_180 >= 400)
2834 mmc->caps |= MMC_CAP_MAX_CURRENT_400;
2835 else
2836 mmc->caps |= MMC_CAP_MAX_CURRENT_200;
Arindam Nathf2119df2011-05-05 12:18:57 +05302837 }
2838
Takashi Iwai8f230f42010-12-08 10:04:30 +01002839 mmc->ocr_avail = ocr_avail;
2840 mmc->ocr_avail_sdio = ocr_avail;
2841 if (host->ocr_avail_sdio)
2842 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
2843 mmc->ocr_avail_sd = ocr_avail;
2844 if (host->ocr_avail_sd)
2845 mmc->ocr_avail_sd &= host->ocr_avail_sd;
2846 else /* normal SD controllers don't support 1.8V */
2847 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
2848 mmc->ocr_avail_mmc = ocr_avail;
2849 if (host->ocr_avail_mmc)
2850 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07002851
2852 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302853 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002854 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07002856 }
2857
Pierre Ossmand129bce2006-03-24 03:18:17 -08002858 spin_lock_init(&host->lock);
2859
2860 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02002861 * Maximum number of segments. Depends on if the hardware
2862 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002863 */
Pierre Ossman2134a922008-06-28 18:28:51 +02002864 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002865 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07002866 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04002867 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02002868 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002869 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870
2871 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01002872 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01002873 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874 */
Pierre Ossman55db8902006-11-21 17:55:45 +01002875 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002876
2877 /*
2878 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02002879 * of bytes. When doing hardware scatter/gather, each entry cannot
2880 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08002881 */
Olof Johansson30652aa2011-01-01 18:37:32 -06002882 if (host->flags & SDHCI_USE_ADMA) {
2883 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
2884 mmc->max_seg_size = 65535;
2885 else
2886 mmc->max_seg_size = 65536;
2887 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02002888 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06002889 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890
2891 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002892 * Maximum block size. This varies from controller to controller and
2893 * is specified in the capabilities register.
2894 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03002895 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
2896 mmc->max_blk_size = 2;
2897 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05302898 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03002899 SDHCI_MAX_BLOCK_SHIFT;
2900 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302901 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03002902 "assuming 512 bytes\n", mmc_hostname(mmc));
2903 mmc->max_blk_size = 0;
2904 }
2905 }
2906
2907 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01002908
2909 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01002910 * Maximum block count.
2911 */
Ben Dooks1388eef2009-06-14 12:40:53 +01002912 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01002913
2914 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002915 * Init tasklets.
2916 */
2917 tasklet_init(&host->card_tasklet,
2918 sdhci_tasklet_card, (unsigned long)host);
2919 tasklet_init(&host->finish_tasklet,
2920 sdhci_tasklet_finish, (unsigned long)host);
2921
Al Viroe4cad1b2006-10-10 22:47:07 +01002922 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002923
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302924 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302925 init_waitqueue_head(&host->buf_ready_int);
2926
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302927 /* Initialize re-tuning timer */
2928 init_timer(&host->tuning_timer);
2929 host->tuning_timer.data = (unsigned long)host;
2930 host->tuning_timer.function = sdhci_tuning_timer;
2931 }
2932
Thomas Gleixnerdace1452006-07-01 19:29:38 -07002933 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002934 mmc_hostname(mmc), host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002935 if (ret)
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002936 goto untasklet;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002938 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
2939 if (IS_ERR(host->vmmc)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302940 pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002941 host->vmmc = NULL;
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002942 }
2943
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002944 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945
2946#ifdef CONFIG_MMC_DEBUG
2947 sdhci_dumpregs(host);
2948#endif
2949
Pierre Ossmanf9134312008-12-21 17:01:48 +01002950#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01002951 snprintf(host->led_name, sizeof(host->led_name),
2952 "%s::", mmc_hostname(mmc));
2953 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002954 host->led.brightness = LED_OFF;
2955 host->led.default_trigger = mmc_hostname(mmc);
2956 host->led.brightness_set = sdhci_led_control;
2957
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002958 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002959 if (ret)
2960 goto reset;
2961#endif
2962
Pierre Ossman5f25a662006-10-04 02:15:39 -07002963 mmiowb();
2964
Pierre Ossmand129bce2006-03-24 03:18:17 -08002965 mmc_add_host(mmc);
2966
Girish K Sa3c76eb2011-10-11 11:44:09 +05302967 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01002968 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07002969 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
2970 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002972 sdhci_enable_card_detection(host);
2973
Pierre Ossmand129bce2006-03-24 03:18:17 -08002974 return 0;
2975
Pierre Ossmanf9134312008-12-21 17:01:48 +01002976#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002977reset:
2978 sdhci_reset(host, SDHCI_RESET_ALL);
2979 free_irq(host->irq, host);
2980#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07002981untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08002982 tasklet_kill(&host->card_tasklet);
2983 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984
2985 return ret;
2986}
2987
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002988EXPORT_SYMBOL_GPL(sdhci_add_host);
2989
Pierre Ossman1e728592008-04-16 19:13:13 +02002990void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002991{
Pierre Ossman1e728592008-04-16 19:13:13 +02002992 unsigned long flags;
2993
2994 if (dead) {
2995 spin_lock_irqsave(&host->lock, flags);
2996
2997 host->flags |= SDHCI_DEVICE_DEAD;
2998
2999 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303000 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003001 " transfer!\n", mmc_hostname(host->mmc));
3002
3003 host->mrq->cmd->error = -ENOMEDIUM;
3004 tasklet_schedule(&host->finish_tasklet);
3005 }
3006
3007 spin_unlock_irqrestore(&host->lock, flags);
3008 }
3009
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003010 sdhci_disable_card_detection(host);
3011
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003012 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003013
Pierre Ossmanf9134312008-12-21 17:01:48 +01003014#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003015 led_classdev_unregister(&host->led);
3016#endif
3017
Pierre Ossman1e728592008-04-16 19:13:13 +02003018 if (!dead)
3019 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003020
3021 free_irq(host->irq, host);
3022
3023 del_timer_sync(&host->timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303024 if (host->version >= SDHCI_SPEC_300)
3025 del_timer_sync(&host->tuning_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003026
3027 tasklet_kill(&host->card_tasklet);
3028 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003029
Adrian Hunterceb61432011-12-27 15:48:41 +02003030 if (host->vmmc)
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003031 regulator_put(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003032
Pierre Ossman2134a922008-06-28 18:28:51 +02003033 kfree(host->adma_desc);
3034 kfree(host->align_buffer);
3035
3036 host->adma_desc = NULL;
3037 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003038}
3039
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003040EXPORT_SYMBOL_GPL(sdhci_remove_host);
3041
3042void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003043{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003044 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003045}
3046
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003047EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003048
3049/*****************************************************************************\
3050 * *
3051 * Driver init/exit *
3052 * *
3053\*****************************************************************************/
3054
3055static int __init sdhci_drv_init(void)
3056{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303057 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003058 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303059 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003061 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003062}
3063
3064static void __exit sdhci_drv_exit(void)
3065{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003066}
3067
3068module_init(sdhci_drv_init);
3069module_exit(sdhci_drv_exit);
3070
Pierre Ossmandf673b22006-06-30 02:22:31 -07003071module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003072module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003073
Pierre Ossman32710e82009-04-08 20:14:54 +02003074MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003075MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003076MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003077
Pierre Ossmandf673b22006-06-30 02:22:31 -07003078MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003079MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");