blob: 836e2ac36a0df682f00550a8c1d445bc8bd4b2e7 [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>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080031#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include "sdhci.h"
34
35#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010038 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmanf9134312008-12-21 17:01:48 +010040#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
41 defined(CONFIG_MMC_SDHCI_MODULE))
42#define SDHCI_USE_LEDS_CLASS
43#endif
44
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Pierre Ossmandf673b22006-06-30 02:22:31 -070047static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030048static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070049
Pierre Ossmand129bce2006-03-24 03:18:17 -080050static void sdhci_finish_data(struct sdhci_host *);
51
52static void sdhci_send_command(struct sdhci_host *, struct mmc_command *);
53static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +053055static void sdhci_tuning_timer(unsigned long data);
Kevin Liu52983382013-01-31 11:31:37 +080056static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030058#ifdef CONFIG_PM_RUNTIME
59static int sdhci_runtime_pm_get(struct sdhci_host *host);
60static int sdhci_runtime_pm_put(struct sdhci_host *host);
61#else
62static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
63{
64 return 0;
65}
66static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
67{
68 return 0;
69}
70#endif
71
Pierre Ossmand129bce2006-03-24 03:18:17 -080072static void sdhci_dumpregs(struct sdhci_host *host)
73{
Girish K Sa3c76eb2011-10-11 11:44:09 +053074 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070075 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080076
Girish K Sa3c76eb2011-10-11 11:44:09 +053077 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030078 sdhci_readl(host, SDHCI_DMA_ADDRESS),
79 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053080 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030081 sdhci_readw(host, SDHCI_BLOCK_SIZE),
82 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053083 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030084 sdhci_readl(host, SDHCI_ARGUMENT),
85 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053086 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030087 sdhci_readl(host, SDHCI_PRESENT_STATE),
88 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053089 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readb(host, SDHCI_POWER_CONTROL),
91 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053092 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030093 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
94 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053095 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030096 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
97 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053098 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030099 sdhci_readl(host, SDHCI_INT_ENABLE),
100 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530101 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300102 sdhci_readw(host, SDHCI_ACMD12_ERR),
103 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530104 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300105 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500106 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530107 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500108 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300109 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530110 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530111 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800112
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100113 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530114 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100115 readl(host->ioaddr + SDHCI_ADMA_ERROR),
116 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
117
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119}
120
121/*****************************************************************************\
122 * *
123 * Low level functions *
124 * *
125\*****************************************************************************/
126
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300127static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
128{
129 u32 ier;
130
131 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
132 ier &= ~clear;
133 ier |= set;
134 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
135 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
136}
137
138static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
139{
140 sdhci_clear_set_irqs(host, 0, irqs);
141}
142
143static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
144{
145 sdhci_clear_set_irqs(host, irqs, 0);
146}
147
148static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
149{
Shawn Guod25928d2011-06-21 22:41:48 +0800150 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300151
Adrian Hunterc79396c2011-12-27 15:48:42 +0200152 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100153 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300154 return;
155
Shawn Guod25928d2011-06-21 22:41:48 +0800156 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
157 SDHCI_CARD_PRESENT;
158 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
159
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300160 if (enable)
161 sdhci_unmask_irqs(host, irqs);
162 else
163 sdhci_mask_irqs(host, irqs);
164}
165
166static void sdhci_enable_card_detection(struct sdhci_host *host)
167{
168 sdhci_set_card_detection(host, true);
169}
170
171static void sdhci_disable_card_detection(struct sdhci_host *host)
172{
173 sdhci_set_card_detection(host, false);
174}
175
Pierre Ossmand129bce2006-03-24 03:18:17 -0800176static void sdhci_reset(struct sdhci_host *host, u8 mask)
177{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700178 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300179 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700180
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100181 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300182 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700183 SDHCI_CARD_PRESENT))
184 return;
185 }
186
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300187 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
188 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
189
Philip Rakity393c1a32011-01-21 11:26:40 -0800190 if (host->ops->platform_reset_enter)
191 host->ops->platform_reset_enter(host, mask);
192
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800194
Pierre Ossmane16514d82006-06-30 02:22:24 -0700195 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800196 host->clock = 0;
197
Pierre Ossmane16514d82006-06-30 02:22:24 -0700198 /* Wait max 100 ms */
199 timeout = 100;
200
201 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300202 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700203 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530204 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700205 mmc_hostname(host->mmc), (int)mask);
206 sdhci_dumpregs(host);
207 return;
208 }
209 timeout--;
210 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800211 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300212
Philip Rakity393c1a32011-01-21 11:26:40 -0800213 if (host->ops->platform_reset_exit)
214 host->ops->platform_reset_exit(host, mask);
215
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300216 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
217 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800218
219 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
220 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
221 host->ops->enable_dma(host);
222 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800223}
224
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800225static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
226
227static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800229 if (soft)
230 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
231 else
232 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800233
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300234 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
235 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700236 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
237 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300238 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800239
240 if (soft) {
241 /* force clock reconfiguration */
242 host->clock = 0;
243 sdhci_set_ios(host->mmc, &host->mmc->ios);
244 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300245}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800246
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247static void sdhci_reinit(struct sdhci_host *host)
248{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800249 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800250 /*
251 * Retuning stuffs are affected by different cards inserted and only
252 * applicable to UHS-I cards. So reset these fields to their initial
253 * value when card is removed.
254 */
Aaron Lu973905f2012-07-04 13:29:09 +0800255 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
256 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
257
Aaron Lub67c6b42012-06-29 16:17:31 +0800258 del_timer_sync(&host->tuning_timer);
259 host->flags &= ~SDHCI_NEEDS_RETUNING;
260 host->mmc->max_blk_count =
261 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
262 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300263 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264}
265
266static void sdhci_activate_led(struct sdhci_host *host)
267{
268 u8 ctrl;
269
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300272 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273}
274
275static void sdhci_deactivate_led(struct sdhci_host *host)
276{
277 u8 ctrl;
278
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300279 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300281 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282}
283
Pierre Ossmanf9134312008-12-21 17:01:48 +0100284#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100285static void sdhci_led_control(struct led_classdev *led,
286 enum led_brightness brightness)
287{
288 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
289 unsigned long flags;
290
291 spin_lock_irqsave(&host->lock, flags);
292
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300293 if (host->runtime_suspended)
294 goto out;
295
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100296 if (brightness == LED_OFF)
297 sdhci_deactivate_led(host);
298 else
299 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300300out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100301 spin_unlock_irqrestore(&host->lock, flags);
302}
303#endif
304
Pierre Ossmand129bce2006-03-24 03:18:17 -0800305/*****************************************************************************\
306 * *
307 * Core functions *
308 * *
309\*****************************************************************************/
310
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100311static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800312{
Pierre Ossman76591502008-07-21 00:32:11 +0200313 unsigned long flags;
314 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700315 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200316 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800317
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100318 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800319
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100320 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200321 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800322
Pierre Ossman76591502008-07-21 00:32:11 +0200323 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800324
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100325 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200326 if (!sg_miter_next(&host->sg_miter))
327 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328
Pierre Ossman76591502008-07-21 00:32:11 +0200329 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800330
Pierre Ossman76591502008-07-21 00:32:11 +0200331 blksize -= len;
332 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200333
Pierre Ossman76591502008-07-21 00:32:11 +0200334 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800335
Pierre Ossman76591502008-07-21 00:32:11 +0200336 while (len) {
337 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300338 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200339 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800340 }
Pierre Ossman76591502008-07-21 00:32:11 +0200341
342 *buf = scratch & 0xFF;
343
344 buf++;
345 scratch >>= 8;
346 chunk--;
347 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800348 }
349 }
Pierre Ossman76591502008-07-21 00:32:11 +0200350
351 sg_miter_stop(&host->sg_miter);
352
353 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100354}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800355
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356static void sdhci_write_block_pio(struct sdhci_host *host)
357{
Pierre Ossman76591502008-07-21 00:32:11 +0200358 unsigned long flags;
359 size_t blksize, len, chunk;
360 u32 scratch;
361 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362
363 DBG("PIO writing\n");
364
365 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200366 chunk = 0;
367 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100370
371 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200372 if (!sg_miter_next(&host->sg_miter))
373 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 blksize -= len;
378 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 while (len) {
383 scratch |= (u32)*buf << (chunk * 8);
384
385 buf++;
386 chunk++;
387 len--;
388
389 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300390 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200391 chunk = 0;
392 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100394 }
395 }
Pierre Ossman76591502008-07-21 00:32:11 +0200396
397 sg_miter_stop(&host->sg_miter);
398
399 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400}
401
402static void sdhci_transfer_pio(struct sdhci_host *host)
403{
404 u32 mask;
405
406 BUG_ON(!host->data);
407
Pierre Ossman76591502008-07-21 00:32:11 +0200408 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409 return;
410
411 if (host->data->flags & MMC_DATA_READ)
412 mask = SDHCI_DATA_AVAILABLE;
413 else
414 mask = SDHCI_SPACE_AVAILABLE;
415
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200416 /*
417 * Some controllers (JMicron JMB38x) mess up the buffer bits
418 * for transfers < 4 bytes. As long as it is just one block,
419 * we can ignore the bits.
420 */
421 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
422 (host->data->blocks == 1))
423 mask = ~0;
424
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300425 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300426 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
427 udelay(100);
428
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100429 if (host->data->flags & MMC_DATA_READ)
430 sdhci_read_block_pio(host);
431 else
432 sdhci_write_block_pio(host);
433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 host->blocks--;
435 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100436 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437 }
438
439 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800440}
441
Pierre Ossman2134a922008-06-28 18:28:51 +0200442static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
443{
444 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800445 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200446}
447
448static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
449{
Cong Wang482fce92011-11-27 13:27:00 +0800450 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200451 local_irq_restore(*flags);
452}
453
Ben Dooks118cd172010-03-05 13:43:26 -0800454static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
455{
Ben Dooks9e506f32010-03-05 13:43:29 -0800456 __le32 *dataddr = (__le32 __force *)(desc + 4);
457 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800458
Ben Dooks9e506f32010-03-05 13:43:29 -0800459 /* SDHCI specification says ADMA descriptors should be 4 byte
460 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800461
Ben Dooks9e506f32010-03-05 13:43:29 -0800462 cmdlen[0] = cpu_to_le16(cmd);
463 cmdlen[1] = cpu_to_le16(len);
464
465 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800466}
467
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200468static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200469 struct mmc_data *data)
470{
471 int direction;
472
473 u8 *desc;
474 u8 *align;
475 dma_addr_t addr;
476 dma_addr_t align_addr;
477 int len, offset;
478
479 struct scatterlist *sg;
480 int i;
481 char *buffer;
482 unsigned long flags;
483
484 /*
485 * The spec does not specify endianness of descriptor table.
486 * We currently guess that it is LE.
487 */
488
489 if (data->flags & MMC_DATA_READ)
490 direction = DMA_FROM_DEVICE;
491 else
492 direction = DMA_TO_DEVICE;
493
494 /*
495 * The ADMA descriptor table is mapped further down as we
496 * need to fill it with data first.
497 */
498
499 host->align_addr = dma_map_single(mmc_dev(host->mmc),
500 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700501 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200502 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200503 BUG_ON(host->align_addr & 0x3);
504
505 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
506 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200507 if (host->sg_count == 0)
508 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200509
510 desc = host->adma_desc;
511 align = host->align_buffer;
512
513 align_addr = host->align_addr;
514
515 for_each_sg(data->sg, sg, host->sg_count, i) {
516 addr = sg_dma_address(sg);
517 len = sg_dma_len(sg);
518
519 /*
520 * The SDHCI specification states that ADMA
521 * addresses must be 32-bit aligned. If they
522 * aren't, then we use a bounce buffer for
523 * the (up to three) bytes that screw up the
524 * alignment.
525 */
526 offset = (4 - (addr & 0x3)) & 0x3;
527 if (offset) {
528 if (data->flags & MMC_DATA_WRITE) {
529 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200530 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200531 memcpy(align, buffer, offset);
532 sdhci_kunmap_atomic(buffer, &flags);
533 }
534
Ben Dooks118cd172010-03-05 13:43:26 -0800535 /* tran, valid */
536 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
538 BUG_ON(offset > 65536);
539
Pierre Ossman2134a922008-06-28 18:28:51 +0200540 align += 4;
541 align_addr += 4;
542
543 desc += 8;
544
545 addr += offset;
546 len -= offset;
547 }
548
Pierre Ossman2134a922008-06-28 18:28:51 +0200549 BUG_ON(len > 65536);
550
Ben Dooks118cd172010-03-05 13:43:26 -0800551 /* tran, valid */
552 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200553 desc += 8;
554
555 /*
556 * If this triggers then we have a calculation bug
557 * somewhere. :/
558 */
559 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
560 }
561
Thomas Abraham70764a92010-05-26 14:42:04 -0700562 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
563 /*
564 * Mark the last descriptor as the terminating descriptor
565 */
566 if (desc != host->adma_desc) {
567 desc -= 8;
568 desc[0] |= 0x2; /* end */
569 }
570 } else {
571 /*
572 * Add a terminating entry.
573 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200574
Thomas Abraham70764a92010-05-26 14:42:04 -0700575 /* nop, end, valid */
576 sdhci_set_adma_desc(desc, 0, 0, 0x3);
577 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 /*
580 * Resync align buffer as we might have changed it.
581 */
582 if (data->flags & MMC_DATA_WRITE) {
583 dma_sync_single_for_device(mmc_dev(host->mmc),
584 host->align_addr, 128 * 4, direction);
585 }
586
587 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
588 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200589 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200590 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200591 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200592
593 return 0;
594
595unmap_entries:
596 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
597 data->sg_len, direction);
598unmap_align:
599 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
600 128 * 4, direction);
601fail:
602 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200603}
604
605static void sdhci_adma_table_post(struct sdhci_host *host,
606 struct mmc_data *data)
607{
608 int direction;
609
610 struct scatterlist *sg;
611 int i, size;
612 u8 *align;
613 char *buffer;
614 unsigned long flags;
615
616 if (data->flags & MMC_DATA_READ)
617 direction = DMA_FROM_DEVICE;
618 else
619 direction = DMA_TO_DEVICE;
620
621 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
622 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
623
624 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
625 128 * 4, direction);
626
627 if (data->flags & MMC_DATA_READ) {
628 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
629 data->sg_len, direction);
630
631 align = host->align_buffer;
632
633 for_each_sg(data->sg, sg, host->sg_count, i) {
634 if (sg_dma_address(sg) & 0x3) {
635 size = 4 - (sg_dma_address(sg) & 0x3);
636
637 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200638 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200639 memcpy(buffer, align, size);
640 sdhci_kunmap_atomic(buffer, &flags);
641
642 align += 4;
643 }
644 }
645 }
646
647 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
648 data->sg_len, direction);
649}
650
Andrei Warkentina3c77782011-04-11 16:13:42 -0500651static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800652{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700653 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700655 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800656
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200657 /*
658 * If the host controller provides us with an incorrect timeout
659 * value, just skip the check and use 0xE. The hardware may take
660 * longer to time out, but that's much better than having a too-short
661 * timeout value.
662 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200663 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200664 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200665
Andrei Warkentina3c77782011-04-11 16:13:42 -0500666 /* Unspecified timeout, assume max */
667 if (!data && !cmd->cmd_timeout_ms)
668 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800669
Andrei Warkentina3c77782011-04-11 16:13:42 -0500670 /* timeout in us */
671 if (!data)
672 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300673 else {
674 target_timeout = data->timeout_ns / 1000;
675 if (host->clock)
676 target_timeout += data->timeout_clks / host->clock;
677 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700678
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700679 /*
680 * Figure out needed cycles.
681 * We do this in steps in order to fit inside a 32 bit int.
682 * The first step is the minimum timeout, which will have a
683 * minimum resolution of 6 bits:
684 * (1) 2^13*1000 > 2^22,
685 * (2) host->timeout_clk < 2^16
686 * =>
687 * (1) / (2) > 2^6
688 */
689 count = 0;
690 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
691 while (current_timeout < target_timeout) {
692 count++;
693 current_timeout <<= 1;
694 if (count >= 0xF)
695 break;
696 }
697
698 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400699 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
700 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700701 count = 0xE;
702 }
703
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200704 return count;
705}
706
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300707static void sdhci_set_transfer_irqs(struct sdhci_host *host)
708{
709 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
710 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
711
712 if (host->flags & SDHCI_REQ_USE_DMA)
713 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
714 else
715 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
716}
717
Andrei Warkentina3c77782011-04-11 16:13:42 -0500718static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200719{
720 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200721 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500722 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200723 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200724
725 WARN_ON(host->data);
726
Andrei Warkentina3c77782011-04-11 16:13:42 -0500727 if (data || (cmd->flags & MMC_RSP_BUSY)) {
728 count = sdhci_calc_timeout(host, cmd);
729 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
730 }
731
732 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200733 return;
734
735 /* Sanity checks */
736 BUG_ON(data->blksz * data->blocks > 524288);
737 BUG_ON(data->blksz > host->mmc->max_blk_size);
738 BUG_ON(data->blocks > 65535);
739
740 host->data = data;
741 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400742 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200743
Richard Röjforsa13abc72009-09-22 16:45:30 -0700744 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100745 host->flags |= SDHCI_REQ_USE_DMA;
746
Pierre Ossman2134a922008-06-28 18:28:51 +0200747 /*
748 * FIXME: This doesn't account for merging when mapping the
749 * scatterlist.
750 */
751 if (host->flags & SDHCI_REQ_USE_DMA) {
752 int broken, i;
753 struct scatterlist *sg;
754
755 broken = 0;
756 if (host->flags & SDHCI_USE_ADMA) {
757 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
758 broken = 1;
759 } else {
760 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
761 broken = 1;
762 }
763
764 if (unlikely(broken)) {
765 for_each_sg(data->sg, sg, data->sg_len, i) {
766 if (sg->length & 0x3) {
767 DBG("Reverting to PIO because of "
768 "transfer size (%d)\n",
769 sg->length);
770 host->flags &= ~SDHCI_REQ_USE_DMA;
771 break;
772 }
773 }
774 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100775 }
776
777 /*
778 * The assumption here being that alignment is the same after
779 * translation to device address space.
780 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200781 if (host->flags & SDHCI_REQ_USE_DMA) {
782 int broken, i;
783 struct scatterlist *sg;
784
785 broken = 0;
786 if (host->flags & SDHCI_USE_ADMA) {
787 /*
788 * As we use 3 byte chunks to work around
789 * alignment problems, we need to check this
790 * quirk.
791 */
792 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
793 broken = 1;
794 } else {
795 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
796 broken = 1;
797 }
798
799 if (unlikely(broken)) {
800 for_each_sg(data->sg, sg, data->sg_len, i) {
801 if (sg->offset & 0x3) {
802 DBG("Reverting to PIO because of "
803 "bad alignment\n");
804 host->flags &= ~SDHCI_REQ_USE_DMA;
805 break;
806 }
807 }
808 }
809 }
810
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200811 if (host->flags & SDHCI_REQ_USE_DMA) {
812 if (host->flags & SDHCI_USE_ADMA) {
813 ret = sdhci_adma_table_pre(host, data);
814 if (ret) {
815 /*
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 {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300822 sdhci_writel(host, host->adma_addr,
823 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200824 }
825 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300826 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200827
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300828 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200829 data->sg, data->sg_len,
830 (data->flags & MMC_DATA_READ) ?
831 DMA_FROM_DEVICE :
832 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300833 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200834 /*
835 * This only happens when someone fed
836 * us an invalid request.
837 */
838 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200839 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200840 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200841 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300842 sdhci_writel(host, sg_dma_address(data->sg),
843 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200844 }
845 }
846 }
847
Pierre Ossman2134a922008-06-28 18:28:51 +0200848 /*
849 * Always adjust the DMA selection as some controllers
850 * (e.g. JMicron) can't do PIO properly when the selection
851 * is ADMA.
852 */
853 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300854 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200855 ctrl &= ~SDHCI_CTRL_DMA_MASK;
856 if ((host->flags & SDHCI_REQ_USE_DMA) &&
857 (host->flags & SDHCI_USE_ADMA))
858 ctrl |= SDHCI_CTRL_ADMA32;
859 else
860 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300861 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100862 }
863
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200864 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200865 int flags;
866
867 flags = SG_MITER_ATOMIC;
868 if (host->data->flags & MMC_DATA_READ)
869 flags |= SG_MITER_TO_SG;
870 else
871 flags |= SG_MITER_FROM_SG;
872 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200873 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800874 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300876 sdhci_set_transfer_irqs(host);
877
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400878 /* Set the DMA boundary value and block size */
879 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
880 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700882}
883
884static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500885 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700886{
887 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890 if (data == NULL)
891 return;
892
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200893 WARN_ON(!host->data);
894
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700895 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500896 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
897 mode |= SDHCI_TRNS_MULTI;
898 /*
899 * If we are sending CMD23, CMD12 never gets sent
900 * on successful completion (so no Auto-CMD12).
901 */
902 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
903 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500904 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
905 mode |= SDHCI_TRNS_AUTO_CMD23;
906 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
907 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700908 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500909
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910 if (data->flags & MMC_DATA_READ)
911 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100912 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700913 mode |= SDHCI_TRNS_DMA;
914
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300915 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800916}
917
918static void sdhci_finish_data(struct sdhci_host *host)
919{
920 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800921
922 BUG_ON(!host->data);
923
924 data = host->data;
925 host->data = NULL;
926
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100927 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200928 if (host->flags & SDHCI_USE_ADMA)
929 sdhci_adma_table_post(host, data);
930 else {
931 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
932 data->sg_len, (data->flags & MMC_DATA_READ) ?
933 DMA_FROM_DEVICE : DMA_TO_DEVICE);
934 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800935 }
936
937 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200938 * The specification states that the block count register must
939 * be updated, but it does not specify at what point in the
940 * data flow. That makes the register entirely useless to read
941 * back so we have to assume that nothing made it to the card
942 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200944 if (data->error)
945 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800946 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200947 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948
Andrei Warkentine89d4562011-05-23 15:06:37 -0500949 /*
950 * Need to send CMD12 if -
951 * a) open-ended multiblock transfer (no CMD23)
952 * b) error in multiblock transfer
953 */
954 if (data->stop &&
955 (data->error ||
956 !host->mrq->sbc)) {
957
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958 /*
959 * The controller needs a reset of internal state machines
960 * upon error conditions.
961 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200962 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963 sdhci_reset(host, SDHCI_RESET_CMD);
964 sdhci_reset(host, SDHCI_RESET_DATA);
965 }
966
967 sdhci_send_command(host, data->stop);
968 } else
969 tasklet_schedule(&host->finish_tasklet);
970}
971
972static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
973{
974 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700975 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700976 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800977
978 WARN_ON(host->cmd);
979
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700981 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700982
983 mask = SDHCI_CMD_INHIBIT;
984 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
985 mask |= SDHCI_DATA_INHIBIT;
986
987 /* We shouldn't wait for data inihibit for stop commands, even
988 though they might use busy signaling */
989 if (host->mrq->data && (cmd == host->mrq->data->stop))
990 mask &= ~SDHCI_DATA_INHIBIT;
991
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300992 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700993 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530994 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100995 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800996 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200997 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 tasklet_schedule(&host->finish_tasklet);
999 return;
1000 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001001 timeout--;
1002 mdelay(1);
1003 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004
1005 mod_timer(&host->timer, jiffies + 10 * HZ);
1006
1007 host->cmd = cmd;
1008
Andrei Warkentina3c77782011-04-11 16:13:42 -05001009 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001011 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Andrei Warkentine89d4562011-05-23 15:06:37 -05001013 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001014
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301016 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001018 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019 tasklet_schedule(&host->finish_tasklet);
1020 return;
1021 }
1022
1023 if (!(cmd->flags & MMC_RSP_PRESENT))
1024 flags = SDHCI_CMD_RESP_NONE;
1025 else if (cmd->flags & MMC_RSP_136)
1026 flags = SDHCI_CMD_RESP_LONG;
1027 else if (cmd->flags & MMC_RSP_BUSY)
1028 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1029 else
1030 flags = SDHCI_CMD_RESP_SHORT;
1031
1032 if (cmd->flags & MMC_RSP_CRC)
1033 flags |= SDHCI_CMD_CRC;
1034 if (cmd->flags & MMC_RSP_OPCODE)
1035 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301036
1037 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301038 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1039 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 flags |= SDHCI_CMD_DATA;
1041
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001042 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043}
1044
1045static void sdhci_finish_command(struct sdhci_host *host)
1046{
1047 int i;
1048
1049 BUG_ON(host->cmd == NULL);
1050
1051 if (host->cmd->flags & MMC_RSP_PRESENT) {
1052 if (host->cmd->flags & MMC_RSP_136) {
1053 /* CRC is stripped so we need to do some shifting. */
1054 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001055 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056 SDHCI_RESPONSE + (3-i)*4) << 8;
1057 if (i != 3)
1058 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001059 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060 SDHCI_RESPONSE + (3-i)*4-1);
1061 }
1062 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001063 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001064 }
1065 }
1066
Pierre Ossman17b04292007-07-22 22:18:46 +02001067 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068
Andrei Warkentine89d4562011-05-23 15:06:37 -05001069 /* Finished CMD23, now send actual command. */
1070 if (host->cmd == host->mrq->sbc) {
1071 host->cmd = NULL;
1072 sdhci_send_command(host, host->mrq->cmd);
1073 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001074
Andrei Warkentine89d4562011-05-23 15:06:37 -05001075 /* Processed actual command. */
1076 if (host->data && host->data_early)
1077 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001078
Andrei Warkentine89d4562011-05-23 15:06:37 -05001079 if (!host->cmd->data)
1080 tasklet_schedule(&host->finish_tasklet);
1081
1082 host->cmd = NULL;
1083 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001084}
1085
Kevin Liu52983382013-01-31 11:31:37 +08001086static u16 sdhci_get_preset_value(struct sdhci_host *host)
1087{
1088 u16 ctrl, preset = 0;
1089
1090 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1091
1092 switch (ctrl & SDHCI_CTRL_UHS_MASK) {
1093 case SDHCI_CTRL_UHS_SDR12:
1094 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1095 break;
1096 case SDHCI_CTRL_UHS_SDR25:
1097 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1098 break;
1099 case SDHCI_CTRL_UHS_SDR50:
1100 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1101 break;
1102 case SDHCI_CTRL_UHS_SDR104:
1103 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1104 break;
1105 case SDHCI_CTRL_UHS_DDR50:
1106 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1107 break;
1108 default:
1109 pr_warn("%s: Invalid UHS-I mode selected\n",
1110 mmc_hostname(host->mmc));
1111 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1112 break;
1113 }
1114 return preset;
1115}
1116
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1118{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301119 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001120 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301121 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001122 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001123
Todd Poynor30832ab2011-12-27 15:48:46 +02001124 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125 return;
1126
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001127 host->mmc->actual_clock = 0;
1128
Anton Vorontsov81146342009-03-17 00:13:59 +03001129 if (host->ops->set_clock) {
1130 host->ops->set_clock(host, clock);
1131 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1132 return;
1133 }
1134
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001135 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
1137 if (clock == 0)
1138 goto out;
1139
Zhangfei Gao85105c52010-08-06 07:10:01 +08001140 if (host->version >= SDHCI_SPEC_300) {
Kevin Liu52983382013-01-31 11:31:37 +08001141 if (sdhci_readw(host, SDHCI_HOST_CONTROL2) &
1142 SDHCI_CTRL_PRESET_VAL_ENABLE) {
1143 u16 pre_val;
1144
1145 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1146 pre_val = sdhci_get_preset_value(host);
1147 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1148 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1149 if (host->clk_mul &&
1150 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1151 clk = SDHCI_PROG_CLOCK_MODE;
1152 real_div = div + 1;
1153 clk_mul = host->clk_mul;
1154 } else {
1155 real_div = max_t(int, 1, div << 1);
1156 }
1157 goto clock_set;
1158 }
1159
Arindam Nathc3ed3872011-05-05 12:19:06 +05301160 /*
1161 * Check if the Host Controller supports Programmable Clock
1162 * Mode.
1163 */
1164 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001165 for (div = 1; div <= 1024; div++) {
1166 if ((host->max_clk * host->clk_mul / div)
1167 <= clock)
1168 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001169 }
Kevin Liu52983382013-01-31 11:31:37 +08001170 /*
1171 * Set Programmable Clock Mode in the Clock
1172 * Control register.
1173 */
1174 clk = SDHCI_PROG_CLOCK_MODE;
1175 real_div = div;
1176 clk_mul = host->clk_mul;
1177 div--;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301178 } else {
1179 /* Version 3.00 divisors must be a multiple of 2. */
1180 if (host->max_clk <= clock)
1181 div = 1;
1182 else {
1183 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1184 div += 2) {
1185 if ((host->max_clk / div) <= clock)
1186 break;
1187 }
1188 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001189 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301190 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001191 }
1192 } else {
1193 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001194 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001195 if ((host->max_clk / div) <= clock)
1196 break;
1197 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001198 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301199 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201
Kevin Liu52983382013-01-31 11:31:37 +08001202clock_set:
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001203 if (real_div)
1204 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1205
Arindam Nathc3ed3872011-05-05 12:19:06 +05301206 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001207 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1208 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001209 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001210 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001211
Chris Ball27f6cb12009-09-22 16:45:31 -07001212 /* Wait max 20 ms */
1213 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001214 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001215 & SDHCI_CLOCK_INT_STABLE)) {
1216 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301217 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001218 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001219 sdhci_dumpregs(host);
1220 return;
1221 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001222 timeout--;
1223 mdelay(1);
1224 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001225
1226 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001227 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228
1229out:
1230 host->clock = clock;
1231}
1232
Andy Shevchenko8213af32013-01-07 16:31:08 +02001233static inline void sdhci_update_clock(struct sdhci_host *host)
1234{
1235 unsigned int clock;
1236
1237 clock = host->clock;
1238 host->clock = 0;
1239 sdhci_set_clock(host, clock);
1240}
1241
Adrian Hunterceb61432011-12-27 15:48:41 +02001242static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001243{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001244 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001245
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001246 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001247 switch (1 << power) {
1248 case MMC_VDD_165_195:
1249 pwr = SDHCI_POWER_180;
1250 break;
1251 case MMC_VDD_29_30:
1252 case MMC_VDD_30_31:
1253 pwr = SDHCI_POWER_300;
1254 break;
1255 case MMC_VDD_32_33:
1256 case MMC_VDD_33_34:
1257 pwr = SDHCI_POWER_330;
1258 break;
1259 default:
1260 BUG();
1261 }
1262 }
1263
1264 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001265 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001266
Pierre Ossmanae628902009-05-03 20:45:03 +02001267 host->pwr = pwr;
1268
1269 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001270 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001271 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001272 }
1273
1274 /*
1275 * Spec says that we should clear the power reg before setting
1276 * a new value. Some controllers don't seem to like this though.
1277 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001278 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001279 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001280
Andres Salomone08c1692008-07-04 10:00:03 -07001281 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001282 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001283 * and set turn on power at the same time, so set the voltage first.
1284 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001285 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001286 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1287
1288 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001289
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001290 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001291
1292 /*
1293 * Some controllers need an extra 10ms delay of 10ms before they
1294 * can apply clock after applying power
1295 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001296 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001297 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001298
1299 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001300}
1301
Pierre Ossmand129bce2006-03-24 03:18:17 -08001302/*****************************************************************************\
1303 * *
1304 * MMC callbacks *
1305 * *
1306\*****************************************************************************/
1307
1308static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1309{
1310 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001311 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001312 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001313 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314
1315 host = mmc_priv(mmc);
1316
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001317 sdhci_runtime_pm_get(host);
1318
Krzysztof Kozlowski34a322c2015-01-05 10:50:15 +01001319 present = mmc_gpio_get_cd(host->mmc);
1320
Pierre Ossmand129bce2006-03-24 03:18:17 -08001321 spin_lock_irqsave(&host->lock, flags);
1322
1323 WARN_ON(host->mrq != NULL);
1324
Pierre Ossmanf9134312008-12-21 17:01:48 +01001325#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001327#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001328
1329 /*
1330 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1331 * requests if Auto-CMD12 is enabled.
1332 */
1333 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001334 if (mrq->stop) {
1335 mrq->data->stop = NULL;
1336 mrq->stop = NULL;
1337 }
1338 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001339
1340 host->mrq = mrq;
1341
Shawn Guo505a8682012-12-11 15:23:42 +08001342 /*
1343 * Firstly check card presence from cd-gpio. The return could
1344 * be one of the following possibilities:
1345 * negative: cd-gpio is not available
1346 * zero: cd-gpio is used, and card is removed
1347 * one: cd-gpio is used, and card is present
1348 */
Shawn Guo505a8682012-12-11 15:23:42 +08001349 if (present < 0) {
1350 /* If polling, assume that the card is always present. */
1351 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1352 present = 1;
1353 else
1354 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1355 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001356 }
1357
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001358 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001359 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001360 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301361 } else {
1362 u32 present_state;
1363
1364 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1365 /*
1366 * Check if the re-tuning timer has already expired and there
1367 * is no on-going data transfer. If so, we need to execute
1368 * tuning procedure before sending command.
1369 */
1370 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1371 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001372 if (mmc->card) {
1373 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1374 tuning_opcode =
1375 mmc->card->type == MMC_TYPE_MMC ?
1376 MMC_SEND_TUNING_BLOCK_HS200 :
1377 MMC_SEND_TUNING_BLOCK;
1378 spin_unlock_irqrestore(&host->lock, flags);
1379 sdhci_execute_tuning(mmc, tuning_opcode);
1380 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301381
Chris Ball14efd952012-11-05 14:29:49 -05001382 /* Restore original mmc_request structure */
1383 host->mrq = mrq;
1384 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301385 }
1386
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001387 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001388 sdhci_send_command(host, mrq->sbc);
1389 else
1390 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301391 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Pierre Ossman5f25a662006-10-04 02:15:39 -07001393 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001394 spin_unlock_irqrestore(&host->lock, flags);
1395}
1396
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001397static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001398{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001399 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001400 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001401 u8 ctrl;
1402
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403 spin_lock_irqsave(&host->lock, flags);
1404
Adrian Hunterceb61432011-12-27 15:48:41 +02001405 if (host->flags & SDHCI_DEVICE_DEAD) {
1406 spin_unlock_irqrestore(&host->lock, flags);
1407 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1408 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1409 return;
1410 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001411
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412 /*
1413 * Reset the chip on each power off.
1414 * Should clear out any weird states.
1415 */
1416 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001417 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001418 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001419 }
1420
Kevin Liu52983382013-01-31 11:31:37 +08001421 if (host->version >= SDHCI_SPEC_300 &&
1422 (ios->power_mode == MMC_POWER_UP))
1423 sdhci_enable_preset_value(host, false);
1424
Pierre Ossmand129bce2006-03-24 03:18:17 -08001425 sdhci_set_clock(host, ios->clock);
1426
1427 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001428 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001429 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001430 vdd_bit = sdhci_set_power(host, ios->vdd);
1431
1432 if (host->vmmc && vdd_bit != -1) {
1433 spin_unlock_irqrestore(&host->lock, flags);
1434 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1435 spin_lock_irqsave(&host->lock, flags);
1436 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001437
Philip Rakity643a81f2010-09-23 08:24:32 -07001438 if (host->ops->platform_send_init_74_clocks)
1439 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1440
Philip Rakity15ec4462010-11-19 16:48:39 -05001441 /*
1442 * If your platform has 8-bit width support but is not a v3 controller,
1443 * or if it requires special setup code, you should implement that in
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001444 * platform_bus_width().
Philip Rakity15ec4462010-11-19 16:48:39 -05001445 */
Sascha Hauer7bc088d2013-01-21 19:02:27 +08001446 if (host->ops->platform_bus_width) {
1447 host->ops->platform_bus_width(host, ios->bus_width);
1448 } else {
Philip Rakity15ec4462010-11-19 16:48:39 -05001449 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1450 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1451 ctrl &= ~SDHCI_CTRL_4BITBUS;
1452 if (host->version >= SDHCI_SPEC_300)
1453 ctrl |= SDHCI_CTRL_8BITBUS;
1454 } else {
1455 if (host->version >= SDHCI_SPEC_300)
1456 ctrl &= ~SDHCI_CTRL_8BITBUS;
1457 if (ios->bus_width == MMC_BUS_WIDTH_4)
1458 ctrl |= SDHCI_CTRL_4BITBUS;
1459 else
1460 ctrl &= ~SDHCI_CTRL_4BITBUS;
1461 }
1462 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1463 }
1464
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001465 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001466
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001467 if ((ios->timing == MMC_TIMING_SD_HS ||
1468 ios->timing == MMC_TIMING_MMC_HS)
1469 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001470 ctrl |= SDHCI_CTRL_HISPD;
1471 else
1472 ctrl &= ~SDHCI_CTRL_HISPD;
1473
Arindam Nathd6d50a12011-05-05 12:18:59 +05301474 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301475 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301476
1477 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301478 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1479 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301480 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1481 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001482 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301483 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301484
1485 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1486 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301487 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301488 /*
1489 * We only need to set Driver Strength if the
1490 * preset value enable is not set.
1491 */
1492 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1493 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1494 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1495 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1496 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1497
1498 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301499 } else {
1500 /*
1501 * According to SDHC Spec v3.00, if the Preset Value
1502 * Enable in the Host Control 2 register is set, we
1503 * need to reset SD Clock Enable before changing High
1504 * Speed Enable to avoid generating clock gliches.
1505 */
Arindam Nath758535c2011-05-05 12:19:00 +05301506
1507 /* Reset SD Clock Enable */
1508 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1509 clk &= ~SDHCI_CLOCK_CARD_EN;
1510 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1511
1512 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1513
1514 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001515 sdhci_update_clock(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301516 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301517
Arindam Nath49c468f2011-05-05 12:19:01 +05301518
1519 /* Reset SD Clock Enable */
1520 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1521 clk &= ~SDHCI_CLOCK_CARD_EN;
1522 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1523
Philip Rakity6322cdd2011-05-13 11:17:15 +05301524 if (host->ops->set_uhs_signaling)
1525 host->ops->set_uhs_signaling(host, ios->timing);
1526 else {
1527 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1528 /* Select Bus Speed Mode for host */
1529 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301530 if (ios->timing == MMC_TIMING_MMC_HS200)
1531 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1532 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301533 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1534 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1535 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1536 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1537 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1538 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1539 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1540 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1541 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1542 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1543 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301544
Kevin Liu52983382013-01-31 11:31:37 +08001545 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1546 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1547 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1548 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1549 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1550 (ios->timing == MMC_TIMING_UHS_DDR50))) {
1551 u16 preset;
1552
1553 sdhci_enable_preset_value(host, true);
1554 preset = sdhci_get_preset_value(host);
1555 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1556 >> SDHCI_PRESET_DRV_SHIFT;
1557 }
1558
Arindam Nath49c468f2011-05-05 12:19:01 +05301559 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001560 sdhci_update_clock(host);
Arindam Nath758535c2011-05-05 12:19:00 +05301561 } else
1562 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301563
Leandro Dorileob8352262007-07-25 23:47:04 +02001564 /*
1565 * Some (ENE) controllers go apeshit on some ios operation,
1566 * signalling timeout and CRC errors even on CMD0. Resetting
1567 * it on each ios seems to solve the problem.
1568 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001569 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001570 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1571
Pierre Ossman5f25a662006-10-04 02:15:39 -07001572 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001573 spin_unlock_irqrestore(&host->lock, flags);
1574}
1575
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001576static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1577{
1578 struct sdhci_host *host = mmc_priv(mmc);
1579
1580 sdhci_runtime_pm_get(host);
1581 sdhci_do_set_ios(host, ios);
1582 sdhci_runtime_pm_put(host);
1583}
1584
Kevin Liu94144a42013-02-28 17:35:53 +08001585static int sdhci_do_get_cd(struct sdhci_host *host)
1586{
1587 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1588
1589 if (host->flags & SDHCI_DEVICE_DEAD)
1590 return 0;
1591
1592 /* If polling/nonremovable, assume that the card is always present. */
1593 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
1594 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
1595 return 1;
1596
1597 /* Try slot gpio detect */
1598 if (!IS_ERR_VALUE(gpio_cd))
1599 return !!gpio_cd;
1600
1601 /* Host native card detect */
1602 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1603}
1604
1605static int sdhci_get_cd(struct mmc_host *mmc)
1606{
1607 struct sdhci_host *host = mmc_priv(mmc);
1608 int ret;
1609
1610 sdhci_runtime_pm_get(host);
1611 ret = sdhci_do_get_cd(host);
1612 sdhci_runtime_pm_put(host);
1613 return ret;
1614}
1615
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001616static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001617{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001618 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001619 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001620
Pierre Ossmand129bce2006-03-24 03:18:17 -08001621 spin_lock_irqsave(&host->lock, flags);
1622
Pierre Ossman1e728592008-04-16 19:13:13 +02001623 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001624 is_readonly = 0;
1625 else if (host->ops->get_ro)
1626 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001627 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001628 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1629 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001630
1631 spin_unlock_irqrestore(&host->lock, flags);
1632
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001633 /* This quirk needs to be replaced by a callback-function later */
1634 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1635 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001636}
1637
Takashi Iwai82b0e232011-04-21 20:26:38 +02001638#define SAMPLE_COUNT 5
1639
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001640static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001641{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001642 int i, ro_count;
1643
Takashi Iwai82b0e232011-04-21 20:26:38 +02001644 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001645 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001646
1647 ro_count = 0;
1648 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001649 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001650 if (++ro_count > SAMPLE_COUNT / 2)
1651 return 1;
1652 }
1653 msleep(30);
1654 }
1655 return 0;
1656}
1657
Adrian Hunter20758b62011-08-29 16:42:12 +03001658static void sdhci_hw_reset(struct mmc_host *mmc)
1659{
1660 struct sdhci_host *host = mmc_priv(mmc);
1661
1662 if (host->ops && host->ops->hw_reset)
1663 host->ops->hw_reset(host);
1664}
1665
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001666static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001667{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001668 struct sdhci_host *host = mmc_priv(mmc);
1669 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001670
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001671 sdhci_runtime_pm_get(host);
1672 ret = sdhci_do_get_ro(host);
1673 sdhci_runtime_pm_put(host);
1674 return ret;
1675}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001676
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001677static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1678{
Pierre Ossman1e728592008-04-16 19:13:13 +02001679 if (host->flags & SDHCI_DEVICE_DEAD)
1680 goto out;
1681
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001682 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001683 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1684 else
1685 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1686
1687 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1688 if (host->runtime_suspended)
1689 goto out;
1690
1691 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001692 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1693 else
1694 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001695out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001696 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001697}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001698
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001699static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1700{
1701 struct sdhci_host *host = mmc_priv(mmc);
1702 unsigned long flags;
1703
1704 spin_lock_irqsave(&host->lock, flags);
1705 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001706 spin_unlock_irqrestore(&host->lock, flags);
1707}
1708
Philip Rakity6231f3d2012-07-23 15:56:23 -07001709static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001710 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001711{
1712 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001713 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001714
1715 /*
1716 * Signal Voltage Switching is only applicable for Host Controllers
1717 * v3.00 and above.
1718 */
1719 if (host->version < SDHCI_SPEC_300)
1720 return 0;
1721
Philip Rakity6231f3d2012-07-23 15:56:23 -07001722 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001723
Fabio Estevam21f59982013-02-14 10:35:03 -02001724 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001725 case MMC_SIGNAL_VOLTAGE_330:
1726 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1727 ctrl &= ~SDHCI_CTRL_VDD_180;
1728 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1729
1730 if (host->vqmmc) {
1731 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
1732 if (ret) {
1733 pr_warning("%s: Switching to 3.3V signalling voltage "
1734 " failed\n", mmc_hostname(host->mmc));
1735 return -EIO;
1736 }
1737 }
1738 /* Wait for 5ms */
1739 usleep_range(5000, 5500);
1740
1741 /* 3.3V regulator output should be stable within 5 ms */
1742 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1743 if (!(ctrl & SDHCI_CTRL_VDD_180))
1744 return 0;
1745
1746 pr_warning("%s: 3.3V regulator output did not became stable\n",
1747 mmc_hostname(host->mmc));
1748
1749 return -EAGAIN;
1750 case MMC_SIGNAL_VOLTAGE_180:
1751 if (host->vqmmc) {
1752 ret = regulator_set_voltage(host->vqmmc,
1753 1700000, 1950000);
1754 if (ret) {
1755 pr_warning("%s: Switching to 1.8V signalling voltage "
1756 " failed\n", mmc_hostname(host->mmc));
1757 return -EIO;
1758 }
1759 }
1760
1761 /*
1762 * Enable 1.8V Signal Enable in the Host Control2
1763 * register
1764 */
1765 ctrl |= SDHCI_CTRL_VDD_180;
1766 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1767
1768 /* Wait for 5ms */
1769 usleep_range(5000, 5500);
1770
1771 /* 1.8V regulator output should be stable within 5 ms */
1772 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1773 if (ctrl & SDHCI_CTRL_VDD_180)
1774 return 0;
1775
1776 pr_warning("%s: 1.8V regulator output did not became stable\n",
1777 mmc_hostname(host->mmc));
1778
1779 return -EAGAIN;
1780 case MMC_SIGNAL_VOLTAGE_120:
1781 if (host->vqmmc) {
1782 ret = regulator_set_voltage(host->vqmmc, 1100000, 1300000);
1783 if (ret) {
1784 pr_warning("%s: Switching to 1.2V signalling voltage "
1785 " failed\n", mmc_hostname(host->mmc));
1786 return -EIO;
1787 }
1788 }
1789 return 0;
1790 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301791 /* No signal voltage switch required */
1792 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001793 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301794}
1795
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001796static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001797 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001798{
1799 struct sdhci_host *host = mmc_priv(mmc);
1800 int err;
1801
1802 if (host->version < SDHCI_SPEC_300)
1803 return 0;
1804 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001805 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001806 sdhci_runtime_pm_put(host);
1807 return err;
1808}
1809
Kevin Liu20b92a32012-12-17 19:29:26 +08001810static int sdhci_card_busy(struct mmc_host *mmc)
1811{
1812 struct sdhci_host *host = mmc_priv(mmc);
1813 u32 present_state;
1814
1815 sdhci_runtime_pm_get(host);
1816 /* Check whether DAT[3:0] is 0000 */
1817 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1818 sdhci_runtime_pm_put(host);
1819
1820 return !(present_state & SDHCI_DATA_LVL_MASK);
1821}
1822
Girish K S069c9f12012-01-06 09:56:39 +05301823static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301824{
1825 struct sdhci_host *host;
1826 u16 ctrl;
1827 u32 ier;
1828 int tuning_loop_counter = MAX_TUNING_LOOP;
1829 unsigned long timeout;
1830 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301831 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301832
1833 host = mmc_priv(mmc);
1834
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001835 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301836 disable_irq(host->irq);
1837 spin_lock(&host->lock);
1838
1839 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1840
1841 /*
Girish K S069c9f12012-01-06 09:56:39 +05301842 * The Host Controller needs tuning only in case of SDR104 mode
1843 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301844 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301845 * If the Host Controller supports the HS200 mode then the
1846 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301847 */
Girish K S069c9f12012-01-06 09:56:39 +05301848 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1849 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1850 host->flags & SDHCI_HS200_NEEDS_TUNING))
1851 requires_tuning_nonuhs = true;
1852
Arindam Nathb513ea22011-05-05 12:19:04 +05301853 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301854 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301855 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1856 else {
1857 spin_unlock(&host->lock);
1858 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001859 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301860 return 0;
1861 }
1862
1863 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1864
1865 /*
1866 * As per the Host Controller spec v3.00, tuning command
1867 * generates Buffer Read Ready interrupt, so enable that.
1868 *
1869 * Note: The spec clearly says that when tuning sequence
1870 * is being performed, the controller does not generate
1871 * interrupts other than Buffer Read Ready interrupt. But
1872 * to make sure we don't hit a controller bug, we _only_
1873 * enable Buffer Read Ready interrupt here.
1874 */
1875 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1876 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1877
1878 /*
1879 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1880 * of loops reaches 40 times or a timeout of 150ms occurs.
1881 */
1882 timeout = 150;
1883 do {
1884 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001885 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301886
1887 if (!tuning_loop_counter && !timeout)
1888 break;
1889
Girish K S069c9f12012-01-06 09:56:39 +05301890 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301891 cmd.arg = 0;
1892 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1893 cmd.retries = 0;
1894 cmd.data = NULL;
1895 cmd.error = 0;
1896
1897 mrq.cmd = &cmd;
1898 host->mrq = &mrq;
1899
1900 /*
1901 * In response to CMD19, the card sends 64 bytes of tuning
1902 * block to the Host Controller. So we set the block size
1903 * to 64 here.
1904 */
Girish K S069c9f12012-01-06 09:56:39 +05301905 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1906 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1907 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1908 SDHCI_BLOCK_SIZE);
1909 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1910 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1911 SDHCI_BLOCK_SIZE);
1912 } else {
1913 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1914 SDHCI_BLOCK_SIZE);
1915 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301916
1917 /*
1918 * The tuning block is sent by the card to the host controller.
1919 * So we set the TRNS_READ bit in the Transfer Mode register.
1920 * This also takes care of setting DMA Enable and Multi Block
1921 * Select in the same register to 0.
1922 */
1923 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1924
1925 sdhci_send_command(host, &cmd);
1926
1927 host->cmd = NULL;
1928 host->mrq = NULL;
1929
1930 spin_unlock(&host->lock);
1931 enable_irq(host->irq);
1932
1933 /* Wait for Buffer Read Ready interrupt */
1934 wait_event_interruptible_timeout(host->buf_ready_int,
1935 (host->tuning_done == 1),
1936 msecs_to_jiffies(50));
1937 disable_irq(host->irq);
1938 spin_lock(&host->lock);
1939
1940 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301941 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301942 "Buffer Read Ready interrupt during tuning "
1943 "procedure, falling back to fixed sampling "
1944 "clock\n");
1945 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1946 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1947 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1948 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1949
1950 err = -EIO;
1951 goto out;
1952 }
1953
1954 host->tuning_done = 0;
1955
1956 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1957 tuning_loop_counter--;
1958 timeout--;
1959 mdelay(1);
1960 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1961
1962 /*
1963 * The Host Driver has exhausted the maximum number of loops allowed,
1964 * so use fixed sampling frequency.
1965 */
1966 if (!tuning_loop_counter || !timeout) {
1967 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1968 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1969 } else {
1970 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301971 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301972 " failed, falling back to fixed sampling"
1973 " clock\n");
1974 err = -EIO;
1975 }
1976 }
1977
1978out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301979 /*
1980 * If this is the very first time we are here, we start the retuning
1981 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1982 * flag won't be set, we check this condition before actually starting
1983 * the timer.
1984 */
1985 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1986 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001987 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301988 mod_timer(&host->tuning_timer, jiffies +
1989 host->tuning_count * HZ);
1990 /* Tuning mode 1 limits the maximum data length to 4MB */
1991 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1992 } else {
1993 host->flags &= ~SDHCI_NEEDS_RETUNING;
1994 /* Reload the new initial value for timer */
1995 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1996 mod_timer(&host->tuning_timer, jiffies +
1997 host->tuning_count * HZ);
1998 }
1999
2000 /*
2001 * In case tuning fails, host controllers which support re-tuning can
2002 * try tuning again at a later time, when the re-tuning timer expires.
2003 * So for these controllers, we return 0. Since there might be other
2004 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08002005 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
2006 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302007 */
Aaron Lu973905f2012-07-04 13:29:09 +08002008 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302009 err = 0;
2010
Arindam Nathb513ea22011-05-05 12:19:04 +05302011 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
2012 spin_unlock(&host->lock);
2013 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002014 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302015
2016 return err;
2017}
2018
Kevin Liu52983382013-01-31 11:31:37 +08002019
2020static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302021{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302022 u16 ctrl;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302023
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302024 /* Host Controller v3.00 defines preset value registers */
2025 if (host->version < SDHCI_SPEC_300)
2026 return;
2027
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302028 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2029
2030 /*
2031 * We only enable or disable Preset Value if they are not already
2032 * enabled or disabled respectively. Otherwise, we bail out.
2033 */
2034 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2035 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2036 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002037 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302038 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
2039 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2040 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002041 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302042 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002043}
2044
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002045static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002046{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002047 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002048 unsigned long flags;
2049
Pierre Ossmand129bce2006-03-24 03:18:17 -08002050 spin_lock_irqsave(&host->lock, flags);
2051
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002052 /* Check host->mrq first in case we are runtime suspended */
2053 if (host->mrq &&
2054 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302055 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002056 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302057 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002058 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002059
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002060 sdhci_reset(host, SDHCI_RESET_CMD);
2061 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002062
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002063 host->mrq->cmd->error = -ENOMEDIUM;
2064 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002065 }
2066
2067 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002068}
2069
2070static const struct mmc_host_ops sdhci_ops = {
2071 .request = sdhci_request,
2072 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002073 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002074 .get_ro = sdhci_get_ro,
2075 .hw_reset = sdhci_hw_reset,
2076 .enable_sdio_irq = sdhci_enable_sdio_irq,
2077 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2078 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002079 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002080 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002081};
2082
2083/*****************************************************************************\
2084 * *
2085 * Tasklets *
2086 * *
2087\*****************************************************************************/
2088
2089static void sdhci_tasklet_card(unsigned long param)
2090{
2091 struct sdhci_host *host = (struct sdhci_host*)param;
2092
2093 sdhci_card_event(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002094
Pierre Ossman04cf5852008-08-18 22:18:14 +02002095 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096}
2097
2098static void sdhci_tasklet_finish(unsigned long param)
2099{
2100 struct sdhci_host *host;
2101 unsigned long flags;
2102 struct mmc_request *mrq;
2103
2104 host = (struct sdhci_host*)param;
2105
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002106 spin_lock_irqsave(&host->lock, flags);
2107
Chris Ball0c9c99a2011-04-27 17:35:31 -04002108 /*
2109 * If this tasklet gets rescheduled while running, it will
2110 * be run again afterwards but without any active request.
2111 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002112 if (!host->mrq) {
2113 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002114 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002115 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002116
2117 del_timer(&host->timer);
2118
2119 mrq = host->mrq;
2120
Pierre Ossmand129bce2006-03-24 03:18:17 -08002121 /*
2122 * The controller needs a reset of internal state machines
2123 * upon error conditions.
2124 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002125 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002126 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002127 (mrq->data && (mrq->data->error ||
2128 (mrq->data->stop && mrq->data->stop->error))) ||
2129 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002130
2131 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002132 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002133 /* This is to force an update */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002134 sdhci_update_clock(host);
Pierre Ossman645289d2006-06-30 02:22:33 -07002135
2136 /* Spec says we should do both at the same time, but Ricoh
2137 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002138 sdhci_reset(host, SDHCI_RESET_CMD);
2139 sdhci_reset(host, SDHCI_RESET_DATA);
2140 }
2141
2142 host->mrq = NULL;
2143 host->cmd = NULL;
2144 host->data = NULL;
2145
Pierre Ossmanf9134312008-12-21 17:01:48 +01002146#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002147 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002148#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149
Pierre Ossman5f25a662006-10-04 02:15:39 -07002150 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002151 spin_unlock_irqrestore(&host->lock, flags);
2152
2153 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002154 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155}
2156
2157static void sdhci_timeout_timer(unsigned long data)
2158{
2159 struct sdhci_host *host;
2160 unsigned long flags;
2161
2162 host = (struct sdhci_host*)data;
2163
2164 spin_lock_irqsave(&host->lock, flags);
2165
2166 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302167 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002168 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002169 sdhci_dumpregs(host);
2170
2171 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002172 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002173 sdhci_finish_data(host);
2174 } else {
2175 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002176 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002177 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002178 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002179
2180 tasklet_schedule(&host->finish_tasklet);
2181 }
2182 }
2183
Pierre Ossman5f25a662006-10-04 02:15:39 -07002184 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185 spin_unlock_irqrestore(&host->lock, flags);
2186}
2187
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302188static void sdhci_tuning_timer(unsigned long data)
2189{
2190 struct sdhci_host *host;
2191 unsigned long flags;
2192
2193 host = (struct sdhci_host *)data;
2194
2195 spin_lock_irqsave(&host->lock, flags);
2196
2197 host->flags |= SDHCI_NEEDS_RETUNING;
2198
2199 spin_unlock_irqrestore(&host->lock, flags);
2200}
2201
Pierre Ossmand129bce2006-03-24 03:18:17 -08002202/*****************************************************************************\
2203 * *
2204 * Interrupt handling *
2205 * *
2206\*****************************************************************************/
2207
2208static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2209{
2210 BUG_ON(intmask == 0);
2211
2212 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302213 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002214 "though no command operation was in progress.\n",
2215 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002216 sdhci_dumpregs(host);
2217 return;
2218 }
2219
Pierre Ossman43b58b32007-07-25 23:15:27 +02002220 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002221 host->cmd->error = -ETIMEDOUT;
2222 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2223 SDHCI_INT_INDEX))
2224 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002225
Pierre Ossmane8095172008-07-25 01:09:08 +02002226 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002228 return;
2229 }
2230
2231 /*
2232 * The host can send and interrupt when the busy state has
2233 * ended, allowing us to wait without wasting CPU cycles.
2234 * Unfortunately this is overloaded on the "data complete"
2235 * interrupt, so we need to take some care when handling
2236 * it.
2237 *
2238 * Note: The 1.0 specification is a bit ambiguous about this
2239 * feature so there might be some problems with older
2240 * controllers.
2241 */
2242 if (host->cmd->flags & MMC_RSP_BUSY) {
2243 if (host->cmd->data)
2244 DBG("Cannot wait for busy signal when also "
2245 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002246 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002247 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002248
2249 /* The controller does not support the end-of-busy IRQ,
2250 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002251 }
2252
2253 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002254 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002255}
2256
George G. Davis0957c332010-02-18 12:32:12 -05002257#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002258static void sdhci_show_adma_error(struct sdhci_host *host)
2259{
2260 const char *name = mmc_hostname(host->mmc);
2261 u8 *desc = host->adma_desc;
2262 __le32 *dma;
2263 __le16 *len;
2264 u8 attr;
2265
2266 sdhci_dumpregs(host);
2267
2268 while (true) {
2269 dma = (__le32 *)(desc + 4);
2270 len = (__le16 *)(desc + 2);
2271 attr = *desc;
2272
2273 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2274 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2275
2276 desc += 8;
2277
2278 if (attr & 2)
2279 break;
2280 }
2281}
2282#else
2283static void sdhci_show_adma_error(struct sdhci_host *host) { }
2284#endif
2285
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2287{
Girish K S069c9f12012-01-06 09:56:39 +05302288 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289 BUG_ON(intmask == 0);
2290
Arindam Nathb513ea22011-05-05 12:19:04 +05302291 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2292 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302293 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2294 if (command == MMC_SEND_TUNING_BLOCK ||
2295 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302296 host->tuning_done = 1;
2297 wake_up(&host->buf_ready_int);
2298 return;
2299 }
2300 }
2301
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302 if (!host->data) {
2303 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002304 * The "data complete" interrupt is also used to
2305 * indicate that a busy state has ended. See comment
2306 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002307 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002308 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2309 if (intmask & SDHCI_INT_DATA_END) {
2310 sdhci_finish_command(host);
2311 return;
2312 }
2313 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314
Girish K Sa3c76eb2011-10-11 11:44:09 +05302315 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002316 "though no data operation was in progress.\n",
2317 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318 sdhci_dumpregs(host);
2319
2320 return;
2321 }
2322
2323 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002324 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002325 else if (intmask & SDHCI_INT_DATA_END_BIT)
2326 host->data->error = -EILSEQ;
2327 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2328 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2329 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002330 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002331 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302332 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002333 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002334 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002335 if (host->ops->adma_workaround)
2336 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002337 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338
Pierre Ossman17b04292007-07-22 22:18:46 +02002339 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340 sdhci_finish_data(host);
2341 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002342 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343 sdhci_transfer_pio(host);
2344
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002345 /*
2346 * We currently don't do anything fancy with DMA
2347 * boundaries, but as we can't disable the feature
2348 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002349 *
2350 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2351 * should return a valid address to continue from, but as
2352 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002353 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002354 if (intmask & SDHCI_INT_DMA_END) {
2355 u32 dmastart, dmanow;
2356 dmastart = sg_dma_address(host->data->sg);
2357 dmanow = dmastart + host->data->bytes_xfered;
2358 /*
2359 * Force update to the next DMA block boundary.
2360 */
2361 dmanow = (dmanow &
2362 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2363 SDHCI_DEFAULT_BOUNDARY_SIZE;
2364 host->data->bytes_xfered = dmanow - dmastart;
2365 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2366 " next 0x%08x\n",
2367 mmc_hostname(host->mmc), dmastart,
2368 host->data->bytes_xfered, dmanow);
2369 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2370 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002371
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002372 if (intmask & SDHCI_INT_DATA_END) {
2373 if (host->cmd) {
2374 /*
2375 * Data managed to finish before the
2376 * command completed. Make sure we do
2377 * things in the proper order.
2378 */
2379 host->data_early = 1;
2380 } else {
2381 sdhci_finish_data(host);
2382 }
2383 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384 }
2385}
2386
David Howells7d12e782006-10-05 14:55:46 +01002387static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388{
2389 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002390 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002391 u32 intmask, unexpected = 0;
2392 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393
2394 spin_lock(&host->lock);
2395
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002396 if (host->runtime_suspended) {
2397 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302398 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002399 mmc_hostname(host->mmc));
2400 return IRQ_HANDLED;
2401 }
2402
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002403 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404
Mark Lord62df67a52007-03-06 13:30:13 +01002405 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406 result = IRQ_NONE;
2407 goto out;
2408 }
2409
Alexander Stein6379b232012-03-14 09:52:10 +01002410again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002411 DBG("*** %s got interrupt: 0x%08x\n",
2412 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413
Pierre Ossman3192a282006-06-30 02:22:26 -07002414 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002415 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2416 SDHCI_CARD_PRESENT;
2417
2418 /*
2419 * There is a observation on i.mx esdhc. INSERT bit will be
2420 * immediately set again when it gets cleared, if a card is
2421 * inserted. We have to mask the irq to prevent interrupt
2422 * storm which will freeze the system. And the REMOVE gets
2423 * the same situation.
2424 *
2425 * More testing are needed here to ensure it works for other
2426 * platforms though.
2427 */
2428 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2429 SDHCI_INT_CARD_REMOVE);
2430 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2431 SDHCI_INT_CARD_INSERT);
2432
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002433 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002434 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2435 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002436 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002437 }
2438
Pierre Ossmand129bce2006-03-24 03:18:17 -08002439 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002440 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2441 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002442 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002443 }
2444
2445 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002446 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2447 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002448 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449 }
2450
2451 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2452
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002453 intmask &= ~SDHCI_INT_ERROR;
2454
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302456 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002458 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 }
2460
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002461 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002462
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002463 if (intmask & SDHCI_INT_CARD_INT)
2464 cardint = 1;
2465
2466 intmask &= ~SDHCI_INT_CARD_INT;
2467
Pierre Ossman3192a282006-06-30 02:22:26 -07002468 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002469 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002470 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002471 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002472
2473 result = IRQ_HANDLED;
2474
Alexander Stein6379b232012-03-14 09:52:10 +01002475 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2476 if (intmask && --max_loops)
2477 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478out:
2479 spin_unlock(&host->lock);
2480
Alexander Stein6379b232012-03-14 09:52:10 +01002481 if (unexpected) {
2482 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2483 mmc_hostname(host->mmc), unexpected);
2484 sdhci_dumpregs(host);
2485 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002486 /*
2487 * We have to delay this as it calls back into the driver.
2488 */
2489 if (cardint)
2490 mmc_signal_sdio_irq(host->mmc);
2491
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492 return result;
2493}
2494
2495/*****************************************************************************\
2496 * *
2497 * Suspend/resume *
2498 * *
2499\*****************************************************************************/
2500
2501#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002502void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2503{
2504 u8 val;
2505 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2506 | SDHCI_WAKE_ON_INT;
2507
2508 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2509 val |= mask ;
2510 /* Avoid fake wake up */
2511 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2512 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2513 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2514}
2515EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2516
2517void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2518{
2519 u8 val;
2520 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2521 | SDHCI_WAKE_ON_INT;
2522
2523 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2524 val &= ~mask;
2525 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2526}
2527EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002528
Manuel Lauss29495aa2011-11-03 11:09:45 +01002529int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002531 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002532
Chris Balla1b13b42012-02-06 00:43:59 -05002533 if (host->ops->platform_suspend)
2534 host->ops->platform_suspend(host);
2535
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002536 sdhci_disable_card_detection(host);
2537
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302538 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002539 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002540 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302541 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302542 }
2543
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002544 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002545 if (ret) {
Aaron Lu973905f2012-07-04 13:29:09 +08002546 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Lu38a60ea2012-01-04 10:07:43 +08002547 host->flags |= SDHCI_NEEDS_RETUNING;
2548 mod_timer(&host->tuning_timer, jiffies +
2549 host->tuning_count * HZ);
2550 }
2551
2552 sdhci_enable_card_detection(host);
2553
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002554 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002555 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002556
Kevin Liuad080d72013-01-05 17:21:33 +08002557 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2558 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2559 free_irq(host->irq, host);
2560 } else {
2561 sdhci_enable_irq_wakeups(host);
2562 enable_irq_wake(host->irq);
2563 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002564 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002565}
2566
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002567EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002568
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002569int sdhci_resume_host(struct sdhci_host *host)
2570{
2571 int ret;
2572
Richard Röjforsa13abc72009-09-22 16:45:30 -07002573 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002574 if (host->ops->enable_dma)
2575 host->ops->enable_dma(host);
2576 }
2577
Kevin Liuad080d72013-01-05 17:21:33 +08002578 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2579 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2580 mmc_hostname(host->mmc), host);
2581 if (ret)
2582 return ret;
2583 } else {
2584 sdhci_disable_irq_wakeups(host);
2585 disable_irq_wake(host->irq);
2586 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002587
Adrian Hunter6308d292012-02-07 14:48:54 +02002588 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2589 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2590 /* Card keeps power but host controller does not */
2591 sdhci_init(host, 0);
2592 host->pwr = 0;
2593 host->clock = 0;
2594 sdhci_do_set_ios(host, &host->mmc->ios);
2595 } else {
2596 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2597 mmiowb();
2598 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002599
2600 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002601 sdhci_enable_card_detection(host);
2602
Chris Balla1b13b42012-02-06 00:43:59 -05002603 if (host->ops->platform_resume)
2604 host->ops->platform_resume(host);
2605
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302606 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002607 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302608 host->flags |= SDHCI_NEEDS_RETUNING;
2609
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002610 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002611}
2612
2613EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002614#endif /* CONFIG_PM */
2615
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002616#ifdef CONFIG_PM_RUNTIME
2617
2618static int sdhci_runtime_pm_get(struct sdhci_host *host)
2619{
2620 return pm_runtime_get_sync(host->mmc->parent);
2621}
2622
2623static int sdhci_runtime_pm_put(struct sdhci_host *host)
2624{
2625 pm_runtime_mark_last_busy(host->mmc->parent);
2626 return pm_runtime_put_autosuspend(host->mmc->parent);
2627}
2628
2629int sdhci_runtime_suspend_host(struct sdhci_host *host)
2630{
2631 unsigned long flags;
2632 int ret = 0;
2633
2634 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002635 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002636 del_timer_sync(&host->tuning_timer);
2637 host->flags &= ~SDHCI_NEEDS_RETUNING;
2638 }
2639
2640 spin_lock_irqsave(&host->lock, flags);
2641 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2642 spin_unlock_irqrestore(&host->lock, flags);
2643
2644 synchronize_irq(host->irq);
2645
2646 spin_lock_irqsave(&host->lock, flags);
2647 host->runtime_suspended = true;
2648 spin_unlock_irqrestore(&host->lock, flags);
2649
2650 return ret;
2651}
2652EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2653
2654int sdhci_runtime_resume_host(struct sdhci_host *host)
2655{
2656 unsigned long flags;
2657 int ret = 0, host_flags = host->flags;
2658
2659 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2660 if (host->ops->enable_dma)
2661 host->ops->enable_dma(host);
2662 }
2663
2664 sdhci_init(host, 0);
2665
2666 /* Force clock and power re-program */
2667 host->pwr = 0;
2668 host->clock = 0;
2669 sdhci_do_set_ios(host, &host->mmc->ios);
2670
2671 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002672 if ((host_flags & SDHCI_PV_ENABLED) &&
2673 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2674 spin_lock_irqsave(&host->lock, flags);
2675 sdhci_enable_preset_value(host, true);
2676 spin_unlock_irqrestore(&host->lock, flags);
2677 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002678
2679 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002680 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002681 host->flags |= SDHCI_NEEDS_RETUNING;
2682
2683 spin_lock_irqsave(&host->lock, flags);
2684
2685 host->runtime_suspended = false;
2686
2687 /* Enable SDIO IRQ */
2688 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2689 sdhci_enable_sdio_irq_nolock(host, true);
2690
2691 /* Enable Card Detection */
2692 sdhci_enable_card_detection(host);
2693
2694 spin_unlock_irqrestore(&host->lock, flags);
2695
2696 return ret;
2697}
2698EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2699
2700#endif
2701
Pierre Ossmand129bce2006-03-24 03:18:17 -08002702/*****************************************************************************\
2703 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002704 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002705 * *
2706\*****************************************************************************/
2707
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002708struct sdhci_host *sdhci_alloc_host(struct device *dev,
2709 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002710{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002711 struct mmc_host *mmc;
2712 struct sdhci_host *host;
2713
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002714 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002715
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002716 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002717 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002718 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002719
2720 host = mmc_priv(mmc);
2721 host->mmc = mmc;
2722
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002723 return host;
2724}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002725
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002726EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002727
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002728int sdhci_add_host(struct sdhci_host *host)
2729{
2730 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002731 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302732 u32 max_current_caps;
2733 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002734 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002735
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002736 WARN_ON(host == NULL);
2737 if (host == NULL)
2738 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002739
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002740 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002741
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002742 if (debug_quirks)
2743 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002744 if (debug_quirks2)
2745 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002746
Pierre Ossmand96649e2006-06-30 02:22:30 -07002747 sdhci_reset(host, SDHCI_RESET_ALL);
2748
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002749 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002750 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2751 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002752 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302753 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002754 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002755 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002756 }
2757
Arindam Nathf2119df2011-05-05 12:18:57 +05302758 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002759 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002760
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002761 if (host->version >= SDHCI_SPEC_300)
2762 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2763 host->caps1 :
2764 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302765
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002766 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002767 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302768 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002769 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002770 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002771 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002772
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002773 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002774 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002775 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002776 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002777 }
2778
Arindam Nathf2119df2011-05-05 12:18:57 +05302779 if ((host->version >= SDHCI_SPEC_200) &&
2780 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002781 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002782
2783 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2784 (host->flags & SDHCI_USE_ADMA)) {
2785 DBG("Disabling ADMA as it is marked broken\n");
2786 host->flags &= ~SDHCI_USE_ADMA;
2787 }
2788
Richard Röjforsa13abc72009-09-22 16:45:30 -07002789 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790 if (host->ops->enable_dma) {
2791 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302792 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002793 "available. Falling back to PIO.\n",
2794 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002795 host->flags &=
2796 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002797 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002798 }
2799 }
2800
Pierre Ossman2134a922008-06-28 18:28:51 +02002801 if (host->flags & SDHCI_USE_ADMA) {
2802 /*
2803 * We need to allocate descriptors for all sg entries
2804 * (128) and potentially one alignment transfer for
2805 * each of those entries.
2806 */
2807 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2808 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2809 if (!host->adma_desc || !host->align_buffer) {
2810 kfree(host->adma_desc);
2811 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302812 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002813 "buffers. Falling back to standard DMA.\n",
2814 mmc_hostname(mmc));
2815 host->flags &= ~SDHCI_USE_ADMA;
2816 }
2817 }
2818
Pierre Ossman76591502008-07-21 00:32:11 +02002819 /*
2820 * If we use DMA, then it's up to the caller to set the DMA
2821 * mask, but PIO does not need the hw shim so we set a new
2822 * mask here in that case.
2823 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002824 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002825 host->dma_mask = DMA_BIT_MASK(64);
2826 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2827 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002829 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302830 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002831 >> SDHCI_CLOCK_BASE_SHIFT;
2832 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302833 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002834 >> SDHCI_CLOCK_BASE_SHIFT;
2835
Pierre Ossmand129bce2006-03-24 03:18:17 -08002836 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002837 if (host->max_clk == 0 || host->quirks &
2838 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002839 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302840 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002841 "frequency.\n", mmc_hostname(mmc));
2842 return -ENODEV;
2843 }
2844 host->max_clk = host->ops->get_max_clock(host);
2845 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002846
2847 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302848 * In case of Host Controller v3.00, find out whether clock
2849 * multiplier is supported.
2850 */
2851 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2852 SDHCI_CLOCK_MUL_SHIFT;
2853
2854 /*
2855 * In case the value in Clock Multiplier is 0, then programmable
2856 * clock mode is not supported, otherwise the actual clock
2857 * multiplier is one more than the value of Clock Multiplier
2858 * in the Capabilities Register.
2859 */
2860 if (host->clk_mul)
2861 host->clk_mul += 1;
2862
2863 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002864 * Set host parameters.
2865 */
2866 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302867 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002868 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002869 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302870 else if (host->version >= SDHCI_SPEC_300) {
2871 if (host->clk_mul) {
2872 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2873 mmc->f_max = host->max_clk * host->clk_mul;
2874 } else
2875 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2876 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002877 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002878
Andy Shevchenko272308c2011-08-03 18:36:00 +03002879 host->timeout_clk =
2880 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2881 if (host->timeout_clk == 0) {
2882 if (host->ops->get_timeout_clock) {
2883 host->timeout_clk = host->ops->get_timeout_clock(host);
2884 } else if (!(host->quirks &
2885 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302886 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002887 "frequency.\n", mmc_hostname(mmc));
2888 return -ENODEV;
2889 }
2890 }
2891 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2892 host->timeout_clk *= 1000;
2893
2894 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002895 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002896
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002897 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002898
Andrei Warkentine89d4562011-05-23 15:06:37 -05002899 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2900
2901 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2902 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002903
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002904 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002905 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002906 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002907 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002908 host->flags |= SDHCI_AUTO_CMD23;
2909 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2910 } else {
2911 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2912 }
2913
Philip Rakity15ec4462010-11-19 16:48:39 -05002914 /*
2915 * A controller may support 8-bit width, but the board itself
2916 * might not have the pins brought out. Boards that support
2917 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2918 * their platform code before calling sdhci_add_host(), and we
2919 * won't assume 8-bit width for hosts without that CAP.
2920 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002921 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002922 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002923
Jerry Huang63ef5d82012-10-25 13:47:19 +08002924 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2925 mmc->caps &= ~MMC_CAP_CMD23;
2926
Arindam Nathf2119df2011-05-05 12:18:57 +05302927 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002928 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002929
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002930 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002931 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002932 mmc->caps |= MMC_CAP_NEEDS_POLL;
2933
Philip Rakity6231f3d2012-07-23 15:56:23 -07002934 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2935 host->vqmmc = regulator_get(mmc_dev(mmc), "vqmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002936 if (IS_ERR_OR_NULL(host->vqmmc)) {
2937 if (PTR_ERR(host->vqmmc) < 0) {
2938 pr_info("%s: no vqmmc regulator found\n",
2939 mmc_hostname(mmc));
2940 host->vqmmc = NULL;
2941 }
Kevin Liu8363c372012-11-17 17:55:51 -05002942 } else {
Chris Balla3361ab2013-03-11 17:51:53 -04002943 ret = regulator_enable(host->vqmmc);
Kevin Liucec2e212012-11-20 08:24:32 -05002944 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2945 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05002946 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2947 SDHCI_SUPPORT_SDR50 |
2948 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04002949 if (ret) {
2950 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
2951 mmc_hostname(mmc), ret);
2952 host->vqmmc = NULL;
2953 }
Kevin Liu8363c372012-11-17 17:55:51 -05002954 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07002955
Daniel Drake6a661802012-11-25 13:01:19 -05002956 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
2957 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2958 SDHCI_SUPPORT_DDR50);
2959
Al Cooper4188bba2012-03-16 15:54:17 -04002960 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2961 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2962 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302963 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2964
2965 /* SDR104 supports also implies SDR50 support */
2966 if (caps[1] & SDHCI_SUPPORT_SDR104)
2967 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2968 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2969 mmc->caps |= MMC_CAP_UHS_SDR50;
2970
2971 if (caps[1] & SDHCI_SUPPORT_DDR50)
2972 mmc->caps |= MMC_CAP_UHS_DDR50;
2973
Girish K S069c9f12012-01-06 09:56:39 +05302974 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302975 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2976 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2977
Girish K S069c9f12012-01-06 09:56:39 +05302978 /* Does the host need tuning for HS200? */
2979 if (mmc->caps2 & MMC_CAP2_HS200)
2980 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2981
Arindam Nathd6d50a12011-05-05 12:18:59 +05302982 /* Driver Type(s) (A, C, D) supported by the host */
2983 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2984 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2985 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2986 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2987 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2988 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2989
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302990 /* Initial value for re-tuning timer count */
2991 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2992 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2993
2994 /*
2995 * In case Re-tuning Timer is not disabled, the actual value of
2996 * re-tuning timer will be 2 ^ (n - 1).
2997 */
2998 if (host->tuning_count)
2999 host->tuning_count = 1 << (host->tuning_count - 1);
3000
3001 /* Re-tuning mode supported by the Host Controller */
3002 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3003 SDHCI_RETUNING_MODE_SHIFT;
3004
Takashi Iwai8f230f42010-12-08 10:04:30 +01003005 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003006
3007 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08003008 if (IS_ERR_OR_NULL(host->vmmc)) {
3009 if (PTR_ERR(host->vmmc) < 0) {
3010 pr_info("%s: no vmmc regulator found\n",
3011 mmc_hostname(mmc));
3012 host->vmmc = NULL;
3013 }
Kevin Liu8363c372012-11-17 17:55:51 -05003014 }
Philip Rakitybad37e12012-05-27 18:36:44 -07003015
Philip Rakity68737042012-06-08 12:26:13 -07003016#ifdef CONFIG_REGULATOR
Marek Szyprowskia4f8f252013-02-12 09:01:36 +01003017 /*
3018 * Voltage range check makes sense only if regulator reports
3019 * any voltage value.
3020 */
3021 if (host->vmmc && regulator_get_voltage(host->vmmc) > 0) {
Kevin Liucec2e212012-11-20 08:24:32 -05003022 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
3023 3600000);
Philip Rakity68737042012-06-08 12:26:13 -07003024 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
3025 caps[0] &= ~SDHCI_CAN_VDD_330;
Philip Rakity68737042012-06-08 12:26:13 -07003026 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
3027 caps[0] &= ~SDHCI_CAN_VDD_300;
Kevin Liucec2e212012-11-20 08:24:32 -05003028 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
3029 1950000);
Philip Rakity68737042012-06-08 12:26:13 -07003030 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
3031 caps[0] &= ~SDHCI_CAN_VDD_180;
3032 }
3033#endif /* CONFIG_REGULATOR */
3034
Arindam Nathf2119df2011-05-05 12:18:57 +05303035 /*
3036 * According to SD Host Controller spec v3.00, if the Host System
3037 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3038 * the value is meaningful only if Voltage Support in the Capabilities
3039 * register is set. The actual current value is 4 times the register
3040 * value.
3041 */
3042 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07003043 if (!max_current_caps && host->vmmc) {
3044 u32 curr = regulator_get_current_limit(host->vmmc);
3045 if (curr > 0) {
3046
3047 /* convert to SDHCI_MAX_CURRENT format */
3048 curr = curr/1000; /* convert to mA */
3049 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3050
3051 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3052 max_current_caps =
3053 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3054 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3055 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3056 }
3057 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303058
3059 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003060 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303061
Aaron Lu55c46652012-07-04 13:31:48 +08003062 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303063 SDHCI_MAX_CURRENT_330_MASK) >>
3064 SDHCI_MAX_CURRENT_330_SHIFT) *
3065 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303066 }
3067 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003068 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303069
Aaron Lu55c46652012-07-04 13:31:48 +08003070 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303071 SDHCI_MAX_CURRENT_300_MASK) >>
3072 SDHCI_MAX_CURRENT_300_SHIFT) *
3073 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303074 }
3075 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003076 ocr_avail |= MMC_VDD_165_195;
3077
Aaron Lu55c46652012-07-04 13:31:48 +08003078 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303079 SDHCI_MAX_CURRENT_180_MASK) >>
3080 SDHCI_MAX_CURRENT_180_SHIFT) *
3081 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303082 }
3083
Takashi Iwai8f230f42010-12-08 10:04:30 +01003084 mmc->ocr_avail = ocr_avail;
3085 mmc->ocr_avail_sdio = ocr_avail;
3086 if (host->ocr_avail_sdio)
3087 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3088 mmc->ocr_avail_sd = ocr_avail;
3089 if (host->ocr_avail_sd)
3090 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3091 else /* normal SD controllers don't support 1.8V */
3092 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3093 mmc->ocr_avail_mmc = ocr_avail;
3094 if (host->ocr_avail_mmc)
3095 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003096
3097 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303098 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003099 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003100 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003101 }
3102
Pierre Ossmand129bce2006-03-24 03:18:17 -08003103 spin_lock_init(&host->lock);
3104
3105 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003106 * Maximum number of segments. Depends on if the hardware
3107 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003108 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003109 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003110 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003111 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003112 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003113 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003114 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003115
3116 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003117 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003118 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003119 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003120 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003121
3122 /*
3123 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003124 * of bytes. When doing hardware scatter/gather, each entry cannot
3125 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003126 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003127 if (host->flags & SDHCI_USE_ADMA) {
3128 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3129 mmc->max_seg_size = 65535;
3130 else
3131 mmc->max_seg_size = 65536;
3132 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003133 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003134 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003135
3136 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003137 * Maximum block size. This varies from controller to controller and
3138 * is specified in the capabilities register.
3139 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003140 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3141 mmc->max_blk_size = 2;
3142 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303143 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003144 SDHCI_MAX_BLOCK_SHIFT;
3145 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303146 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003147 "assuming 512 bytes\n", mmc_hostname(mmc));
3148 mmc->max_blk_size = 0;
3149 }
3150 }
3151
3152 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003153
3154 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003155 * Maximum block count.
3156 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003157 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003158
3159 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003160 * Init tasklets.
3161 */
3162 tasklet_init(&host->card_tasklet,
3163 sdhci_tasklet_card, (unsigned long)host);
3164 tasklet_init(&host->finish_tasklet,
3165 sdhci_tasklet_finish, (unsigned long)host);
3166
Al Viroe4cad1b2006-10-10 22:47:07 +01003167 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003168
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303169 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303170 init_waitqueue_head(&host->buf_ready_int);
3171
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303172 /* Initialize re-tuning timer */
3173 init_timer(&host->tuning_timer);
3174 host->tuning_timer.data = (unsigned long)host;
3175 host->tuning_timer.function = sdhci_tuning_timer;
3176 }
3177
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003178 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003179 mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003180 if (ret) {
3181 pr_err("%s: Failed to request IRQ %d: %d\n",
3182 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003183 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003184 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003185
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003186 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003187
3188#ifdef CONFIG_MMC_DEBUG
3189 sdhci_dumpregs(host);
3190#endif
3191
Pierre Ossmanf9134312008-12-21 17:01:48 +01003192#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003193 snprintf(host->led_name, sizeof(host->led_name),
3194 "%s::", mmc_hostname(mmc));
3195 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003196 host->led.brightness = LED_OFF;
3197 host->led.default_trigger = mmc_hostname(mmc);
3198 host->led.brightness_set = sdhci_led_control;
3199
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003200 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003201 if (ret) {
3202 pr_err("%s: Failed to register LED device: %d\n",
3203 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003204 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003205 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003206#endif
3207
Pierre Ossman5f25a662006-10-04 02:15:39 -07003208 mmiowb();
3209
Pierre Ossmand129bce2006-03-24 03:18:17 -08003210 mmc_add_host(mmc);
3211
Girish K Sa3c76eb2011-10-11 11:44:09 +05303212 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003213 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003214 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3215 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003216
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003217 sdhci_enable_card_detection(host);
3218
Pierre Ossmand129bce2006-03-24 03:18:17 -08003219 return 0;
3220
Pierre Ossmanf9134312008-12-21 17:01:48 +01003221#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003222reset:
3223 sdhci_reset(host, SDHCI_RESET_ALL);
Kevin Liub0a8dec2013-01-05 17:18:28 +08003224 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003225 free_irq(host->irq, host);
3226#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003227untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003228 tasklet_kill(&host->card_tasklet);
3229 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003230
3231 return ret;
3232}
3233
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003234EXPORT_SYMBOL_GPL(sdhci_add_host);
3235
Pierre Ossman1e728592008-04-16 19:13:13 +02003236void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003237{
Pierre Ossman1e728592008-04-16 19:13:13 +02003238 unsigned long flags;
3239
3240 if (dead) {
3241 spin_lock_irqsave(&host->lock, flags);
3242
3243 host->flags |= SDHCI_DEVICE_DEAD;
3244
3245 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303246 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003247 " transfer!\n", mmc_hostname(host->mmc));
3248
3249 host->mrq->cmd->error = -ENOMEDIUM;
3250 tasklet_schedule(&host->finish_tasklet);
3251 }
3252
3253 spin_unlock_irqrestore(&host->lock, flags);
3254 }
3255
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003256 sdhci_disable_card_detection(host);
3257
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003258 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003259
Pierre Ossmanf9134312008-12-21 17:01:48 +01003260#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003261 led_classdev_unregister(&host->led);
3262#endif
3263
Pierre Ossman1e728592008-04-16 19:13:13 +02003264 if (!dead)
3265 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266
Kevin Liub0a8dec2013-01-05 17:18:28 +08003267 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003268 free_irq(host->irq, host);
3269
3270 del_timer_sync(&host->timer);
3271
3272 tasklet_kill(&host->card_tasklet);
3273 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003274
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003275 if (host->vmmc) {
3276 regulator_disable(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003277 regulator_put(host->vmmc);
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003278 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003279
Philip Rakity6231f3d2012-07-23 15:56:23 -07003280 if (host->vqmmc) {
3281 regulator_disable(host->vqmmc);
3282 regulator_put(host->vqmmc);
3283 }
3284
Pierre Ossman2134a922008-06-28 18:28:51 +02003285 kfree(host->adma_desc);
3286 kfree(host->align_buffer);
3287
3288 host->adma_desc = NULL;
3289 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003290}
3291
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003292EXPORT_SYMBOL_GPL(sdhci_remove_host);
3293
3294void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003295{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003296 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003297}
3298
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003299EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003300
3301/*****************************************************************************\
3302 * *
3303 * Driver init/exit *
3304 * *
3305\*****************************************************************************/
3306
3307static int __init sdhci_drv_init(void)
3308{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303309 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003310 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303311 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003313 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003314}
3315
3316static void __exit sdhci_drv_exit(void)
3317{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318}
3319
3320module_init(sdhci_drv_init);
3321module_exit(sdhci_drv_exit);
3322
Pierre Ossmandf673b22006-06-30 02:22:31 -07003323module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003324module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003325
Pierre Ossman32710e82009-04-08 20:14:54 +02003326MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003327MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003328MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003329
Pierre Ossmandf673b22006-06-30 02:22:31 -07003330MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003331MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");