blob: 336ab06aeb2f68ee821520cd308b8ab0304434e5 [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);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030057#ifdef CONFIG_PM_RUNTIME
58static int sdhci_runtime_pm_get(struct sdhci_host *host);
59static int sdhci_runtime_pm_put(struct sdhci_host *host);
60#else
61static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
62{
63 return 0;
64}
65static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
66{
67 return 0;
68}
69#endif
70
Pierre Ossmand129bce2006-03-24 03:18:17 -080071static void sdhci_dumpregs(struct sdhci_host *host)
72{
Girish K Sa3c76eb2011-10-11 11:44:09 +053073 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070074 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080075
Girish K Sa3c76eb2011-10-11 11:44:09 +053076 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readl(host, SDHCI_DMA_ADDRESS),
78 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053079 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readw(host, SDHCI_BLOCK_SIZE),
81 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053082 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readl(host, SDHCI_ARGUMENT),
84 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_PRESENT_STATE),
87 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readb(host, SDHCI_POWER_CONTROL),
90 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
93 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
96 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readl(host, SDHCI_INT_ENABLE),
99 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readw(host, SDHCI_ACMD12_ERR),
102 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500105 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500107 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300108 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530110 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800111
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100112 if (host->flags & SDHCI_USE_ADMA)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530113 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100114 readl(host->ioaddr + SDHCI_ADMA_ERROR),
115 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
116
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800118}
119
120/*****************************************************************************\
121 * *
122 * Low level functions *
123 * *
124\*****************************************************************************/
125
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300126static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
127{
128 u32 ier;
129
130 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
131 ier &= ~clear;
132 ier |= set;
133 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
134 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
135}
136
137static void sdhci_unmask_irqs(struct sdhci_host *host, u32 irqs)
138{
139 sdhci_clear_set_irqs(host, 0, irqs);
140}
141
142static void sdhci_mask_irqs(struct sdhci_host *host, u32 irqs)
143{
144 sdhci_clear_set_irqs(host, irqs, 0);
145}
146
147static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
148{
Shawn Guod25928d2011-06-21 22:41:48 +0800149 u32 present, irqs;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300150
Adrian Hunterc79396c2011-12-27 15:48:42 +0200151 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100152 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300153 return;
154
Shawn Guod25928d2011-06-21 22:41:48 +0800155 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
156 SDHCI_CARD_PRESENT;
157 irqs = present ? SDHCI_INT_CARD_REMOVE : SDHCI_INT_CARD_INSERT;
158
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300159 if (enable)
160 sdhci_unmask_irqs(host, irqs);
161 else
162 sdhci_mask_irqs(host, irqs);
163}
164
165static void sdhci_enable_card_detection(struct sdhci_host *host)
166{
167 sdhci_set_card_detection(host, true);
168}
169
170static void sdhci_disable_card_detection(struct sdhci_host *host)
171{
172 sdhci_set_card_detection(host, false);
173}
174
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175static void sdhci_reset(struct sdhci_host *host, u8 mask)
176{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700177 unsigned long timeout;
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300178 u32 uninitialized_var(ier);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +0100180 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 if (!(sdhci_readl(host, SDHCI_PRESENT_STATE) &
Pierre Ossman8a4da142006-10-04 02:15:40 -0700182 SDHCI_CARD_PRESENT))
183 return;
184 }
185
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300186 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
187 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
188
Philip Rakity393c1a32011-01-21 11:26:40 -0800189 if (host->ops->platform_reset_enter)
190 host->ops->platform_reset_enter(host, mask);
191
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300192 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800193
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 if (mask & SDHCI_RESET_ALL)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800195 host->clock = 0;
196
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 /* Wait max 100 ms */
198 timeout = 100;
199
200 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300201 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700202 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530203 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700204 mmc_hostname(host->mmc), (int)mask);
205 sdhci_dumpregs(host);
206 return;
207 }
208 timeout--;
209 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800210 }
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300211
Philip Rakity393c1a32011-01-21 11:26:40 -0800212 if (host->ops->platform_reset_exit)
213 host->ops->platform_reset_exit(host, mask);
214
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300215 if (host->quirks & SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET)
216 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK, ier);
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800217
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if ((host->ops->enable_dma) && (mask & SDHCI_RESET_ALL))
220 host->ops->enable_dma(host);
221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
225
226static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228 if (soft)
229 sdhci_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
230 else
231 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300233 sdhci_clear_set_irqs(host, SDHCI_INT_ALL_MASK,
234 SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
Pierre Ossman3192a282006-06-30 02:22:26 -0700235 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX |
236 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300237 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800238
239 if (soft) {
240 /* force clock reconfiguration */
241 host->clock = 0;
242 sdhci_set_ios(host->mmc, &host->mmc->ios);
243 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300244}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300246static void sdhci_reinit(struct sdhci_host *host)
247{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800248 sdhci_init(host, 0);
Aaron Lub67c6b42012-06-29 16:17:31 +0800249 /*
250 * Retuning stuffs are affected by different cards inserted and only
251 * applicable to UHS-I cards. So reset these fields to their initial
252 * value when card is removed.
253 */
Aaron Lu973905f2012-07-04 13:29:09 +0800254 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
255 host->flags &= ~SDHCI_USING_RETUNING_TIMER;
256
Aaron Lub67c6b42012-06-29 16:17:31 +0800257 del_timer_sync(&host->tuning_timer);
258 host->flags &= ~SDHCI_NEEDS_RETUNING;
259 host->mmc->max_blk_count =
260 (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
261 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263}
264
265static void sdhci_activate_led(struct sdhci_host *host)
266{
267 u8 ctrl;
268
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300271 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272}
273
274static void sdhci_deactivate_led(struct sdhci_host *host)
275{
276 u8 ctrl;
277
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300278 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300280 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800281}
282
Pierre Ossmanf9134312008-12-21 17:01:48 +0100283#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284static void sdhci_led_control(struct led_classdev *led,
285 enum led_brightness brightness)
286{
287 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
288 unsigned long flags;
289
290 spin_lock_irqsave(&host->lock, flags);
291
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300292 if (host->runtime_suspended)
293 goto out;
294
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100295 if (brightness == LED_OFF)
296 sdhci_deactivate_led(host);
297 else
298 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300299out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100300 spin_unlock_irqrestore(&host->lock, flags);
301}
302#endif
303
Pierre Ossmand129bce2006-03-24 03:18:17 -0800304/*****************************************************************************\
305 * *
306 * Core functions *
307 * *
308\*****************************************************************************/
309
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100310static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311{
Pierre Ossman76591502008-07-21 00:32:11 +0200312 unsigned long flags;
313 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700314 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200315 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100317 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100319 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200320 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100324 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200325 if (!sg_miter_next(&host->sg_miter))
326 BUG();
Pierre Ossmand129bce2006-03-24 03:18:17 -0800327
Pierre Ossman76591502008-07-21 00:32:11 +0200328 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800329
Pierre Ossman76591502008-07-21 00:32:11 +0200330 blksize -= len;
331 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200332
Pierre Ossman76591502008-07-21 00:32:11 +0200333 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800334
Pierre Ossman76591502008-07-21 00:32:11 +0200335 while (len) {
336 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300337 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200338 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800339 }
Pierre Ossman76591502008-07-21 00:32:11 +0200340
341 *buf = scratch & 0xFF;
342
343 buf++;
344 scratch >>= 8;
345 chunk--;
346 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347 }
348 }
Pierre Ossman76591502008-07-21 00:32:11 +0200349
350 sg_miter_stop(&host->sg_miter);
351
352 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355static void sdhci_write_block_pio(struct sdhci_host *host)
356{
Pierre Ossman76591502008-07-21 00:32:11 +0200357 unsigned long flags;
358 size_t blksize, len, chunk;
359 u32 scratch;
360 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
362 DBG("PIO writing\n");
363
364 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200365 chunk = 0;
366 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367
Pierre Ossman76591502008-07-21 00:32:11 +0200368 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100369
370 while (blksize) {
Pierre Ossman76591502008-07-21 00:32:11 +0200371 if (!sg_miter_next(&host->sg_miter))
372 BUG();
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200375
Pierre Ossman76591502008-07-21 00:32:11 +0200376 blksize -= len;
377 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380
Pierre Ossman76591502008-07-21 00:32:11 +0200381 while (len) {
382 scratch |= (u32)*buf << (chunk * 8);
383
384 buf++;
385 chunk++;
386 len--;
387
388 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300389 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200390 chunk = 0;
391 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100393 }
394 }
Pierre Ossman76591502008-07-21 00:32:11 +0200395
396 sg_miter_stop(&host->sg_miter);
397
398 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399}
400
401static void sdhci_transfer_pio(struct sdhci_host *host)
402{
403 u32 mask;
404
405 BUG_ON(!host->data);
406
Pierre Ossman76591502008-07-21 00:32:11 +0200407 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408 return;
409
410 if (host->data->flags & MMC_DATA_READ)
411 mask = SDHCI_DATA_AVAILABLE;
412 else
413 mask = SDHCI_SPACE_AVAILABLE;
414
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200415 /*
416 * Some controllers (JMicron JMB38x) mess up the buffer bits
417 * for transfers < 4 bytes. As long as it is just one block,
418 * we can ignore the bits.
419 */
420 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
421 (host->data->blocks == 1))
422 mask = ~0;
423
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300424 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300425 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
426 udelay(100);
427
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428 if (host->data->flags & MMC_DATA_READ)
429 sdhci_read_block_pio(host);
430 else
431 sdhci_write_block_pio(host);
432
Pierre Ossman76591502008-07-21 00:32:11 +0200433 host->blocks--;
434 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100436 }
437
438 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800439}
440
Pierre Ossman2134a922008-06-28 18:28:51 +0200441static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
442{
443 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800444 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200445}
446
447static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
448{
Cong Wang482fce92011-11-27 13:27:00 +0800449 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200450 local_irq_restore(*flags);
451}
452
Ben Dooks118cd172010-03-05 13:43:26 -0800453static void sdhci_set_adma_desc(u8 *desc, u32 addr, int len, unsigned cmd)
454{
Ben Dooks9e506f32010-03-05 13:43:29 -0800455 __le32 *dataddr = (__le32 __force *)(desc + 4);
456 __le16 *cmdlen = (__le16 __force *)desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800457
Ben Dooks9e506f32010-03-05 13:43:29 -0800458 /* SDHCI specification says ADMA descriptors should be 4 byte
459 * aligned, so using 16 or 32bit operations should be safe. */
Ben Dooks118cd172010-03-05 13:43:26 -0800460
Ben Dooks9e506f32010-03-05 13:43:29 -0800461 cmdlen[0] = cpu_to_le16(cmd);
462 cmdlen[1] = cpu_to_le16(len);
463
464 dataddr[0] = cpu_to_le32(addr);
Ben Dooks118cd172010-03-05 13:43:26 -0800465}
466
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200467static int sdhci_adma_table_pre(struct sdhci_host *host,
Pierre Ossman2134a922008-06-28 18:28:51 +0200468 struct mmc_data *data)
469{
470 int direction;
471
472 u8 *desc;
473 u8 *align;
474 dma_addr_t addr;
475 dma_addr_t align_addr;
476 int len, offset;
477
478 struct scatterlist *sg;
479 int i;
480 char *buffer;
481 unsigned long flags;
482
483 /*
484 * The spec does not specify endianness of descriptor table.
485 * We currently guess that it is LE.
486 */
487
488 if (data->flags & MMC_DATA_READ)
489 direction = DMA_FROM_DEVICE;
490 else
491 direction = DMA_TO_DEVICE;
492
493 /*
494 * The ADMA descriptor table is mapped further down as we
495 * need to fill it with data first.
496 */
497
498 host->align_addr = dma_map_single(mmc_dev(host->mmc),
499 host->align_buffer, 128 * 4, direction);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700500 if (dma_mapping_error(mmc_dev(host->mmc), host->align_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200501 goto fail;
Pierre Ossman2134a922008-06-28 18:28:51 +0200502 BUG_ON(host->align_addr & 0x3);
503
504 host->sg_count = dma_map_sg(mmc_dev(host->mmc),
505 data->sg, data->sg_len, direction);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200506 if (host->sg_count == 0)
507 goto unmap_align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200508
509 desc = host->adma_desc;
510 align = host->align_buffer;
511
512 align_addr = host->align_addr;
513
514 for_each_sg(data->sg, sg, host->sg_count, i) {
515 addr = sg_dma_address(sg);
516 len = sg_dma_len(sg);
517
518 /*
519 * The SDHCI specification states that ADMA
520 * addresses must be 32-bit aligned. If they
521 * aren't, then we use a bounce buffer for
522 * the (up to three) bytes that screw up the
523 * alignment.
524 */
525 offset = (4 - (addr & 0x3)) & 0x3;
526 if (offset) {
527 if (data->flags & MMC_DATA_WRITE) {
528 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200529 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 memcpy(align, buffer, offset);
531 sdhci_kunmap_atomic(buffer, &flags);
532 }
533
Ben Dooks118cd172010-03-05 13:43:26 -0800534 /* tran, valid */
535 sdhci_set_adma_desc(desc, align_addr, offset, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 BUG_ON(offset > 65536);
538
Pierre Ossman2134a922008-06-28 18:28:51 +0200539 align += 4;
540 align_addr += 4;
541
542 desc += 8;
543
544 addr += offset;
545 len -= offset;
546 }
547
Pierre Ossman2134a922008-06-28 18:28:51 +0200548 BUG_ON(len > 65536);
549
Ben Dooks118cd172010-03-05 13:43:26 -0800550 /* tran, valid */
551 sdhci_set_adma_desc(desc, addr, len, 0x21);
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 desc += 8;
553
554 /*
555 * If this triggers then we have a calculation bug
556 * somewhere. :/
557 */
558 WARN_ON((desc - host->adma_desc) > (128 * 2 + 1) * 4);
559 }
560
Thomas Abraham70764a92010-05-26 14:42:04 -0700561 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
562 /*
563 * Mark the last descriptor as the terminating descriptor
564 */
565 if (desc != host->adma_desc) {
566 desc -= 8;
567 desc[0] |= 0x2; /* end */
568 }
569 } else {
570 /*
571 * Add a terminating entry.
572 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
Thomas Abraham70764a92010-05-26 14:42:04 -0700574 /* nop, end, valid */
575 sdhci_set_adma_desc(desc, 0, 0, 0x3);
576 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200577
578 /*
579 * Resync align buffer as we might have changed it.
580 */
581 if (data->flags & MMC_DATA_WRITE) {
582 dma_sync_single_for_device(mmc_dev(host->mmc),
583 host->align_addr, 128 * 4, direction);
584 }
585
586 host->adma_addr = dma_map_single(mmc_dev(host->mmc),
587 host->adma_desc, (128 * 2 + 1) * 4, DMA_TO_DEVICE);
Pierre Ossman980167b2008-07-29 00:53:20 +0200588 if (dma_mapping_error(mmc_dev(host->mmc), host->adma_addr))
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200589 goto unmap_entries;
Pierre Ossman2134a922008-06-28 18:28:51 +0200590 BUG_ON(host->adma_addr & 0x3);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200591
592 return 0;
593
594unmap_entries:
595 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
596 data->sg_len, direction);
597unmap_align:
598 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
599 128 * 4, direction);
600fail:
601 return -EINVAL;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602}
603
604static void sdhci_adma_table_post(struct sdhci_host *host,
605 struct mmc_data *data)
606{
607 int direction;
608
609 struct scatterlist *sg;
610 int i, size;
611 u8 *align;
612 char *buffer;
613 unsigned long flags;
614
615 if (data->flags & MMC_DATA_READ)
616 direction = DMA_FROM_DEVICE;
617 else
618 direction = DMA_TO_DEVICE;
619
620 dma_unmap_single(mmc_dev(host->mmc), host->adma_addr,
621 (128 * 2 + 1) * 4, DMA_TO_DEVICE);
622
623 dma_unmap_single(mmc_dev(host->mmc), host->align_addr,
624 128 * 4, direction);
625
626 if (data->flags & MMC_DATA_READ) {
627 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
628 data->sg_len, direction);
629
630 align = host->align_buffer;
631
632 for_each_sg(data->sg, sg, host->sg_count, i) {
633 if (sg_dma_address(sg) & 0x3) {
634 size = 4 - (sg_dma_address(sg) & 0x3);
635
636 buffer = sdhci_kmap_atomic(sg, &flags);
Pierre Ossman6cefd052008-07-21 00:45:15 +0200637 WARN_ON(((long)buffer & PAGE_MASK) > (PAGE_SIZE - 3));
Pierre Ossman2134a922008-06-28 18:28:51 +0200638 memcpy(buffer, align, size);
639 sdhci_kunmap_atomic(buffer, &flags);
640
641 align += 4;
642 }
643 }
644 }
645
646 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
647 data->sg_len, direction);
648}
649
Andrei Warkentina3c77782011-04-11 16:13:42 -0500650static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800651{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500653 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800655
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200656 /*
657 * If the host controller provides us with an incorrect timeout
658 * value, just skip the check and use 0xE. The hardware may take
659 * longer to time out, but that's much better than having a too-short
660 * timeout value.
661 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200662 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200663 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200664
Andrei Warkentina3c77782011-04-11 16:13:42 -0500665 /* Unspecified timeout, assume max */
666 if (!data && !cmd->cmd_timeout_ms)
667 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 /* timeout in us */
670 if (!data)
671 target_timeout = cmd->cmd_timeout_ms * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300672 else {
673 target_timeout = data->timeout_ns / 1000;
674 if (host->clock)
675 target_timeout += data->timeout_clks / host->clock;
676 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700677
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700678 /*
679 * Figure out needed cycles.
680 * We do this in steps in order to fit inside a 32 bit int.
681 * The first step is the minimum timeout, which will have a
682 * minimum resolution of 6 bits:
683 * (1) 2^13*1000 > 2^22,
684 * (2) host->timeout_clk < 2^16
685 * =>
686 * (1) / (2) > 2^6
687 */
688 count = 0;
689 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
690 while (current_timeout < target_timeout) {
691 count++;
692 current_timeout <<= 1;
693 if (count >= 0xF)
694 break;
695 }
696
697 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400698 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
699 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700700 count = 0xE;
701 }
702
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200703 return count;
704}
705
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300706static void sdhci_set_transfer_irqs(struct sdhci_host *host)
707{
708 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
709 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
710
711 if (host->flags & SDHCI_REQ_USE_DMA)
712 sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
713 else
714 sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
715}
716
Andrei Warkentina3c77782011-04-11 16:13:42 -0500717static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718{
719 u8 count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200720 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500721 struct mmc_data *data = cmd->data;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200722 int ret;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200723
724 WARN_ON(host->data);
725
Andrei Warkentina3c77782011-04-11 16:13:42 -0500726 if (data || (cmd->flags & MMC_RSP_BUSY)) {
727 count = sdhci_calc_timeout(host, cmd);
728 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
729 }
730
731 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200732 return;
733
734 /* Sanity checks */
735 BUG_ON(data->blksz * data->blocks > 524288);
736 BUG_ON(data->blksz > host->mmc->max_blk_size);
737 BUG_ON(data->blocks > 65535);
738
739 host->data = data;
740 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400741 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200742
Richard Röjforsa13abc72009-09-22 16:45:30 -0700743 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100744 host->flags |= SDHCI_REQ_USE_DMA;
745
Pierre Ossman2134a922008-06-28 18:28:51 +0200746 /*
747 * FIXME: This doesn't account for merging when mapping the
748 * scatterlist.
749 */
750 if (host->flags & SDHCI_REQ_USE_DMA) {
751 int broken, i;
752 struct scatterlist *sg;
753
754 broken = 0;
755 if (host->flags & SDHCI_USE_ADMA) {
756 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
757 broken = 1;
758 } else {
759 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
760 broken = 1;
761 }
762
763 if (unlikely(broken)) {
764 for_each_sg(data->sg, sg, data->sg_len, i) {
765 if (sg->length & 0x3) {
766 DBG("Reverting to PIO because of "
767 "transfer size (%d)\n",
768 sg->length);
769 host->flags &= ~SDHCI_REQ_USE_DMA;
770 break;
771 }
772 }
773 }
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100774 }
775
776 /*
777 * The assumption here being that alignment is the same after
778 * translation to device address space.
779 */
Pierre Ossman2134a922008-06-28 18:28:51 +0200780 if (host->flags & SDHCI_REQ_USE_DMA) {
781 int broken, i;
782 struct scatterlist *sg;
783
784 broken = 0;
785 if (host->flags & SDHCI_USE_ADMA) {
786 /*
787 * As we use 3 byte chunks to work around
788 * alignment problems, we need to check this
789 * quirk.
790 */
791 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
792 broken = 1;
793 } else {
794 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
795 broken = 1;
796 }
797
798 if (unlikely(broken)) {
799 for_each_sg(data->sg, sg, data->sg_len, i) {
800 if (sg->offset & 0x3) {
801 DBG("Reverting to PIO because of "
802 "bad alignment\n");
803 host->flags &= ~SDHCI_REQ_USE_DMA;
804 break;
805 }
806 }
807 }
808 }
809
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200810 if (host->flags & SDHCI_REQ_USE_DMA) {
811 if (host->flags & SDHCI_USE_ADMA) {
812 ret = sdhci_adma_table_pre(host, data);
813 if (ret) {
814 /*
815 * This only happens when someone fed
816 * us an invalid request.
817 */
818 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200819 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200820 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300821 sdhci_writel(host, host->adma_addr,
822 SDHCI_ADMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200823 }
824 } else {
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300825 int sg_cnt;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200826
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300827 sg_cnt = dma_map_sg(mmc_dev(host->mmc),
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200828 data->sg, data->sg_len,
829 (data->flags & MMC_DATA_READ) ?
830 DMA_FROM_DEVICE :
831 DMA_TO_DEVICE);
Tomas Winklerc8b3e022008-07-05 19:52:04 +0300832 if (sg_cnt == 0) {
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200833 /*
834 * This only happens when someone fed
835 * us an invalid request.
836 */
837 WARN_ON(1);
Pierre Ossmanebd6d352008-07-29 00:45:51 +0200838 host->flags &= ~SDHCI_REQ_USE_DMA;
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200839 } else {
Pierre Ossman719a61b2008-07-22 13:23:23 +0200840 WARN_ON(sg_cnt != 1);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300841 sdhci_writel(host, sg_dma_address(data->sg),
842 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200843 }
844 }
845 }
846
Pierre Ossman2134a922008-06-28 18:28:51 +0200847 /*
848 * Always adjust the DMA selection as some controllers
849 * (e.g. JMicron) can't do PIO properly when the selection
850 * is ADMA.
851 */
852 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300853 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200854 ctrl &= ~SDHCI_CTRL_DMA_MASK;
855 if ((host->flags & SDHCI_REQ_USE_DMA) &&
856 (host->flags & SDHCI_USE_ADMA))
857 ctrl |= SDHCI_CTRL_ADMA32;
858 else
859 ctrl |= SDHCI_CTRL_SDMA;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300860 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100861 }
862
Pierre Ossman8f1934ce2008-06-30 21:15:49 +0200863 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200864 int flags;
865
866 flags = SG_MITER_ATOMIC;
867 if (host->data->flags & MMC_DATA_READ)
868 flags |= SG_MITER_TO_SG;
869 else
870 flags |= SG_MITER_FROM_SG;
871 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200872 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800873 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700874
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300875 sdhci_set_transfer_irqs(host);
876
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400877 /* Set the DMA boundary value and block size */
878 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
879 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300880 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700881}
882
883static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500884 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700885{
886 u16 mode;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500887 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700888
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889 if (data == NULL)
890 return;
891
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200892 WARN_ON(!host->data);
893
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700894 mode = SDHCI_TRNS_BLK_CNT_EN;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500895 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
896 mode |= SDHCI_TRNS_MULTI;
897 /*
898 * If we are sending CMD23, CMD12 never gets sent
899 * on successful completion (so no Auto-CMD12).
900 */
901 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
902 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500903 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
904 mode |= SDHCI_TRNS_AUTO_CMD23;
905 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
906 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700907 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500908
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700909 if (data->flags & MMC_DATA_READ)
910 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100911 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 mode |= SDHCI_TRNS_DMA;
913
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300914 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800915}
916
917static void sdhci_finish_data(struct sdhci_host *host)
918{
919 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800920
921 BUG_ON(!host->data);
922
923 data = host->data;
924 host->data = NULL;
925
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100926 if (host->flags & SDHCI_REQ_USE_DMA) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200927 if (host->flags & SDHCI_USE_ADMA)
928 sdhci_adma_table_post(host, data);
929 else {
930 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
931 data->sg_len, (data->flags & MMC_DATA_READ) ?
932 DMA_FROM_DEVICE : DMA_TO_DEVICE);
933 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934 }
935
936 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200937 * The specification states that the block count register must
938 * be updated, but it does not specify at what point in the
939 * data flow. That makes the register entirely useless to read
940 * back so we have to assume that nothing made it to the card
941 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800942 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200943 if (data->error)
944 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800945 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200946 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800947
Andrei Warkentine89d4562011-05-23 15:06:37 -0500948 /*
949 * Need to send CMD12 if -
950 * a) open-ended multiblock transfer (no CMD23)
951 * b) error in multiblock transfer
952 */
953 if (data->stop &&
954 (data->error ||
955 !host->mrq->sbc)) {
956
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957 /*
958 * The controller needs a reset of internal state machines
959 * upon error conditions.
960 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200961 if (data->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800962 sdhci_reset(host, SDHCI_RESET_CMD);
963 sdhci_reset(host, SDHCI_RESET_DATA);
964 }
965
966 sdhci_send_command(host, data->stop);
967 } else
968 tasklet_schedule(&host->finish_tasklet);
969}
970
971static void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
972{
973 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700974 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700975 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800976
977 WARN_ON(host->cmd);
978
Pierre Ossmand129bce2006-03-24 03:18:17 -0800979 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700980 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700981
982 mask = SDHCI_CMD_INHIBIT;
983 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
984 mask |= SDHCI_DATA_INHIBIT;
985
986 /* We shouldn't wait for data inihibit for stop commands, even
987 though they might use busy signaling */
988 if (host->mrq->data && (cmd == host->mrq->data->stop))
989 mask &= ~SDHCI_DATA_INHIBIT;
990
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300991 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700992 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530993 pr_err("%s: Controller never released "
Pierre Ossmanacf1da42007-02-09 08:29:19 +0100994 "inhibit bit(s).\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800995 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200996 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 tasklet_schedule(&host->finish_tasklet);
998 return;
999 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001000 timeout--;
1001 mdelay(1);
1002 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003
1004 mod_timer(&host->timer, jiffies + 10 * HZ);
1005
1006 host->cmd = cmd;
1007
Andrei Warkentina3c77782011-04-11 16:13:42 -05001008 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001010 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011
Andrei Warkentine89d4562011-05-23 15:06:37 -05001012 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001013
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301015 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001017 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018 tasklet_schedule(&host->finish_tasklet);
1019 return;
1020 }
1021
1022 if (!(cmd->flags & MMC_RSP_PRESENT))
1023 flags = SDHCI_CMD_RESP_NONE;
1024 else if (cmd->flags & MMC_RSP_136)
1025 flags = SDHCI_CMD_RESP_LONG;
1026 else if (cmd->flags & MMC_RSP_BUSY)
1027 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1028 else
1029 flags = SDHCI_CMD_RESP_SHORT;
1030
1031 if (cmd->flags & MMC_RSP_CRC)
1032 flags |= SDHCI_CMD_CRC;
1033 if (cmd->flags & MMC_RSP_OPCODE)
1034 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301035
1036 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301037 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1038 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001039 flags |= SDHCI_CMD_DATA;
1040
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001041 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042}
1043
1044static void sdhci_finish_command(struct sdhci_host *host)
1045{
1046 int i;
1047
1048 BUG_ON(host->cmd == NULL);
1049
1050 if (host->cmd->flags & MMC_RSP_PRESENT) {
1051 if (host->cmd->flags & MMC_RSP_136) {
1052 /* CRC is stripped so we need to do some shifting. */
1053 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001054 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001055 SDHCI_RESPONSE + (3-i)*4) << 8;
1056 if (i != 3)
1057 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059 SDHCI_RESPONSE + (3-i)*4-1);
1060 }
1061 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001062 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001063 }
1064 }
1065
Pierre Ossman17b04292007-07-22 22:18:46 +02001066 host->cmd->error = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067
Andrei Warkentine89d4562011-05-23 15:06:37 -05001068 /* Finished CMD23, now send actual command. */
1069 if (host->cmd == host->mrq->sbc) {
1070 host->cmd = NULL;
1071 sdhci_send_command(host, host->mrq->cmd);
1072 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001073
Andrei Warkentine89d4562011-05-23 15:06:37 -05001074 /* Processed actual command. */
1075 if (host->data && host->data_early)
1076 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077
Andrei Warkentine89d4562011-05-23 15:06:37 -05001078 if (!host->cmd->data)
1079 tasklet_schedule(&host->finish_tasklet);
1080
1081 host->cmd = NULL;
1082 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001083}
1084
1085static void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1086{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301087 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001088 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301089 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001090 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
Todd Poynor30832ab2011-12-27 15:48:46 +02001092 if (clock && clock == host->clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001093 return;
1094
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001095 host->mmc->actual_clock = 0;
1096
Anton Vorontsov81146342009-03-17 00:13:59 +03001097 if (host->ops->set_clock) {
1098 host->ops->set_clock(host, clock);
1099 if (host->quirks & SDHCI_QUIRK_NONSTANDARD_CLOCK)
1100 return;
1101 }
1102
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001103 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104
1105 if (clock == 0)
1106 goto out;
1107
Zhangfei Gao85105c52010-08-06 07:10:01 +08001108 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301109 /*
1110 * Check if the Host Controller supports Programmable Clock
1111 * Mode.
1112 */
1113 if (host->clk_mul) {
1114 u16 ctrl;
1115
1116 /*
1117 * We need to figure out whether the Host Driver needs
1118 * to select Programmable Clock Mode, or the value can
1119 * be set automatically by the Host Controller based on
1120 * the Preset Value registers.
1121 */
1122 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1123 if (!(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1124 for (div = 1; div <= 1024; div++) {
1125 if (((host->max_clk * host->clk_mul) /
1126 div) <= clock)
1127 break;
1128 }
1129 /*
1130 * Set Programmable Clock Mode in the Clock
1131 * Control register.
1132 */
1133 clk = SDHCI_PROG_CLOCK_MODE;
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001134 real_div = div;
1135 clk_mul = host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 div--;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001137 }
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 } else {
1139 /* Version 3.00 divisors must be a multiple of 2. */
1140 if (host->max_clk <= clock)
1141 div = 1;
1142 else {
1143 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1144 div += 2) {
1145 if ((host->max_clk / div) <= clock)
1146 break;
1147 }
1148 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001149 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301150 div >>= 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001151 }
1152 } else {
1153 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001154 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001155 if ((host->max_clk / div) <= clock)
1156 break;
1157 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001158 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001160 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001161
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001162 if (real_div)
1163 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
1164
Arindam Nathc3ed3872011-05-05 12:19:06 +05301165 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001166 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1167 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001169 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170
Chris Ball27f6cb12009-09-22 16:45:31 -07001171 /* Wait max 20 ms */
1172 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001173 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001174 & SDHCI_CLOCK_INT_STABLE)) {
1175 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301176 pr_err("%s: Internal clock never "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01001177 "stabilised.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 sdhci_dumpregs(host);
1179 return;
1180 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001181 timeout--;
1182 mdelay(1);
1183 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184
1185 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001186 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001187
1188out:
1189 host->clock = clock;
1190}
1191
Andy Shevchenko8213af32013-01-07 16:31:08 +02001192static inline void sdhci_update_clock(struct sdhci_host *host)
1193{
1194 unsigned int clock;
1195
1196 clock = host->clock;
1197 host->clock = 0;
1198 sdhci_set_clock(host, clock);
1199}
1200
Adrian Hunterceb61432011-12-27 15:48:41 +02001201static int sdhci_set_power(struct sdhci_host *host, unsigned short power)
Pierre Ossman146ad662006-06-30 02:22:23 -07001202{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001203 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001204
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001205 if (power != (unsigned short)-1) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001206 switch (1 << power) {
1207 case MMC_VDD_165_195:
1208 pwr = SDHCI_POWER_180;
1209 break;
1210 case MMC_VDD_29_30:
1211 case MMC_VDD_30_31:
1212 pwr = SDHCI_POWER_300;
1213 break;
1214 case MMC_VDD_32_33:
1215 case MMC_VDD_33_34:
1216 pwr = SDHCI_POWER_330;
1217 break;
1218 default:
1219 BUG();
1220 }
1221 }
1222
1223 if (host->pwr == pwr)
Adrian Hunterceb61432011-12-27 15:48:41 +02001224 return -1;
Pierre Ossman146ad662006-06-30 02:22:23 -07001225
Pierre Ossmanae628902009-05-03 20:45:03 +02001226 host->pwr = pwr;
1227
1228 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001229 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterceb61432011-12-27 15:48:41 +02001230 return 0;
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001231 }
1232
1233 /*
1234 * Spec says that we should clear the power reg before setting
1235 * a new value. Some controllers don't seem to like this though.
1236 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001237 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001238 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001239
Andres Salomone08c1692008-07-04 10:00:03 -07001240 /*
Andres Salomonc71f6512008-07-07 17:25:56 -04001241 * At least the Marvell CaFe chip gets confused if we set the voltage
Andres Salomone08c1692008-07-04 10:00:03 -07001242 * and set turn on power at the same time, so set the voltage first.
1243 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001244 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
Pierre Ossmanae628902009-05-03 20:45:03 +02001245 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1246
1247 pwr |= SDHCI_POWER_ON;
Andres Salomone08c1692008-07-04 10:00:03 -07001248
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001249 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Harald Welte557b0692009-06-18 16:53:38 +02001250
1251 /*
1252 * Some controllers need an extra 10ms delay of 10ms before they
1253 * can apply clock after applying power
1254 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +02001255 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
Harald Welte557b0692009-06-18 16:53:38 +02001256 mdelay(10);
Adrian Hunterceb61432011-12-27 15:48:41 +02001257
1258 return power;
Pierre Ossman146ad662006-06-30 02:22:23 -07001259}
1260
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261/*****************************************************************************\
1262 * *
1263 * MMC callbacks *
1264 * *
1265\*****************************************************************************/
1266
1267static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1268{
1269 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001270 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271 unsigned long flags;
Aaron Lu473b0952012-07-03 17:27:49 +08001272 u32 tuning_opcode;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273
1274 host = mmc_priv(mmc);
1275
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001276 sdhci_runtime_pm_get(host);
1277
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278 spin_lock_irqsave(&host->lock, flags);
1279
1280 WARN_ON(host->mrq != NULL);
1281
Pierre Ossmanf9134312008-12-21 17:01:48 +01001282#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001284#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001285
1286 /*
1287 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1288 * requests if Auto-CMD12 is enabled.
1289 */
1290 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001291 if (mrq->stop) {
1292 mrq->data->stop = NULL;
1293 mrq->stop = NULL;
1294 }
1295 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296
1297 host->mrq = mrq;
1298
Shawn Guo505a8682012-12-11 15:23:42 +08001299 /*
1300 * Firstly check card presence from cd-gpio. The return could
1301 * be one of the following possibilities:
1302 * negative: cd-gpio is not available
1303 * zero: cd-gpio is used, and card is removed
1304 * one: cd-gpio is used, and card is present
1305 */
1306 present = mmc_gpio_get_cd(host->mmc);
1307 if (present < 0) {
1308 /* If polling, assume that the card is always present. */
1309 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1310 present = 1;
1311 else
1312 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1313 SDHCI_CARD_PRESENT;
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +08001314 }
1315
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001316 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001317 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001318 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301319 } else {
1320 u32 present_state;
1321
1322 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1323 /*
1324 * Check if the re-tuning timer has already expired and there
1325 * is no on-going data transfer. If so, we need to execute
1326 * tuning procedure before sending command.
1327 */
1328 if ((host->flags & SDHCI_NEEDS_RETUNING) &&
1329 !(present_state & (SDHCI_DOING_WRITE | SDHCI_DOING_READ))) {
Chris Ball14efd952012-11-05 14:29:49 -05001330 if (mmc->card) {
1331 /* eMMC uses cmd21 but sd and sdio use cmd19 */
1332 tuning_opcode =
1333 mmc->card->type == MMC_TYPE_MMC ?
1334 MMC_SEND_TUNING_BLOCK_HS200 :
1335 MMC_SEND_TUNING_BLOCK;
1336 spin_unlock_irqrestore(&host->lock, flags);
1337 sdhci_execute_tuning(mmc, tuning_opcode);
1338 spin_lock_irqsave(&host->lock, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301339
Chris Ball14efd952012-11-05 14:29:49 -05001340 /* Restore original mmc_request structure */
1341 host->mrq = mrq;
1342 }
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301343 }
1344
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001345 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001346 sdhci_send_command(host, mrq->sbc);
1347 else
1348 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301349 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001350
Pierre Ossman5f25a662006-10-04 02:15:39 -07001351 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352 spin_unlock_irqrestore(&host->lock, flags);
1353}
1354
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001355static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357 unsigned long flags;
Adrian Hunterceb61432011-12-27 15:48:41 +02001358 int vdd_bit = -1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359 u8 ctrl;
1360
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 spin_lock_irqsave(&host->lock, flags);
1362
Adrian Hunterceb61432011-12-27 15:48:41 +02001363 if (host->flags & SDHCI_DEVICE_DEAD) {
1364 spin_unlock_irqrestore(&host->lock, flags);
1365 if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
1366 mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
1367 return;
1368 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001369
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 /*
1371 * Reset the chip on each power off.
1372 * Should clear out any weird states.
1373 */
1374 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001375 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001376 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001377 }
1378
1379 sdhci_set_clock(host, ios->clock);
1380
1381 if (ios->power_mode == MMC_POWER_OFF)
Adrian Hunterceb61432011-12-27 15:48:41 +02001382 vdd_bit = sdhci_set_power(host, -1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383 else
Adrian Hunterceb61432011-12-27 15:48:41 +02001384 vdd_bit = sdhci_set_power(host, ios->vdd);
1385
1386 if (host->vmmc && vdd_bit != -1) {
1387 spin_unlock_irqrestore(&host->lock, flags);
1388 mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
1389 spin_lock_irqsave(&host->lock, flags);
1390 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001391
Philip Rakity643a81f2010-09-23 08:24:32 -07001392 if (host->ops->platform_send_init_74_clocks)
1393 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1394
Philip Rakity15ec4462010-11-19 16:48:39 -05001395 /*
1396 * If your platform has 8-bit width support but is not a v3 controller,
1397 * or if it requires special setup code, you should implement that in
1398 * platform_8bit_width().
1399 */
1400 if (host->ops->platform_8bit_width)
1401 host->ops->platform_8bit_width(host, ios->bus_width);
1402 else {
1403 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1404 if (ios->bus_width == MMC_BUS_WIDTH_8) {
1405 ctrl &= ~SDHCI_CTRL_4BITBUS;
1406 if (host->version >= SDHCI_SPEC_300)
1407 ctrl |= SDHCI_CTRL_8BITBUS;
1408 } else {
1409 if (host->version >= SDHCI_SPEC_300)
1410 ctrl &= ~SDHCI_CTRL_8BITBUS;
1411 if (ios->bus_width == MMC_BUS_WIDTH_4)
1412 ctrl |= SDHCI_CTRL_4BITBUS;
1413 else
1414 ctrl &= ~SDHCI_CTRL_4BITBUS;
1415 }
1416 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1417 }
1418
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001419 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001420
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001421 if ((ios->timing == MMC_TIMING_SD_HS ||
1422 ios->timing == MMC_TIMING_MMC_HS)
1423 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001424 ctrl |= SDHCI_CTRL_HISPD;
1425 else
1426 ctrl &= ~SDHCI_CTRL_HISPD;
1427
Arindam Nathd6d50a12011-05-05 12:18:59 +05301428 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301429 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301430
1431 /* In case of UHS-I modes, set High Speed Enable */
Girish K S069c9f12012-01-06 09:56:39 +05301432 if ((ios->timing == MMC_TIMING_MMC_HS200) ||
1433 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301434 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1435 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001436 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301437 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301438
1439 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1440 if (!(ctrl_2 & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
Arindam Nath758535c2011-05-05 12:19:00 +05301441 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301442 /*
1443 * We only need to set Driver Strength if the
1444 * preset value enable is not set.
1445 */
1446 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1447 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1448 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
1449 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1450 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
1451
1452 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301453 } else {
1454 /*
1455 * According to SDHC Spec v3.00, if the Preset Value
1456 * Enable in the Host Control 2 register is set, we
1457 * need to reset SD Clock Enable before changing High
1458 * Speed Enable to avoid generating clock gliches.
1459 */
Arindam Nath758535c2011-05-05 12:19:00 +05301460
1461 /* Reset SD Clock Enable */
1462 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1463 clk &= ~SDHCI_CLOCK_CARD_EN;
1464 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1465
1466 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1467
1468 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001469 sdhci_update_clock(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301470 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301471
Arindam Nath49c468f2011-05-05 12:19:01 +05301472
1473 /* Reset SD Clock Enable */
1474 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1475 clk &= ~SDHCI_CLOCK_CARD_EN;
1476 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1477
Philip Rakity6322cdd2011-05-13 11:17:15 +05301478 if (host->ops->set_uhs_signaling)
1479 host->ops->set_uhs_signaling(host, ios->timing);
1480 else {
1481 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1482 /* Select Bus Speed Mode for host */
1483 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Girish K S069c9f12012-01-06 09:56:39 +05301484 if (ios->timing == MMC_TIMING_MMC_HS200)
1485 ctrl_2 |= SDHCI_CTRL_HS_SDR200;
1486 else if (ios->timing == MMC_TIMING_UHS_SDR12)
Philip Rakity6322cdd2011-05-13 11:17:15 +05301487 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1488 else if (ios->timing == MMC_TIMING_UHS_SDR25)
1489 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1490 else if (ios->timing == MMC_TIMING_UHS_SDR50)
1491 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1492 else if (ios->timing == MMC_TIMING_UHS_SDR104)
1493 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1494 else if (ios->timing == MMC_TIMING_UHS_DDR50)
1495 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
1496 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1497 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301498
1499 /* Re-enable SD Clock */
Andy Shevchenko8213af32013-01-07 16:31:08 +02001500 sdhci_update_clock(host);
Arindam Nath758535c2011-05-05 12:19:00 +05301501 } else
1502 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301503
Leandro Dorileob8352262007-07-25 23:47:04 +02001504 /*
1505 * Some (ENE) controllers go apeshit on some ios operation,
1506 * signalling timeout and CRC errors even on CMD0. Resetting
1507 * it on each ios seems to solve the problem.
1508 */
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01001509 if(host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Leandro Dorileob8352262007-07-25 23:47:04 +02001510 sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1511
Pierre Ossman5f25a662006-10-04 02:15:39 -07001512 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513 spin_unlock_irqrestore(&host->lock, flags);
1514}
1515
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001516static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1517{
1518 struct sdhci_host *host = mmc_priv(mmc);
1519
1520 sdhci_runtime_pm_get(host);
1521 sdhci_do_set_ios(host, ios);
1522 sdhci_runtime_pm_put(host);
1523}
1524
1525static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001527 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001528 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530 spin_lock_irqsave(&host->lock, flags);
1531
Pierre Ossman1e728592008-04-16 19:13:13 +02001532 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001533 is_readonly = 0;
1534 else if (host->ops->get_ro)
1535 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001536 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001537 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1538 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001539
1540 spin_unlock_irqrestore(&host->lock, flags);
1541
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001542 /* This quirk needs to be replaced by a callback-function later */
1543 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1544 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001545}
1546
Takashi Iwai82b0e232011-04-21 20:26:38 +02001547#define SAMPLE_COUNT 5
1548
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001549static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001550{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001551 int i, ro_count;
1552
Takashi Iwai82b0e232011-04-21 20:26:38 +02001553 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001554 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001555
1556 ro_count = 0;
1557 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001558 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001559 if (++ro_count > SAMPLE_COUNT / 2)
1560 return 1;
1561 }
1562 msleep(30);
1563 }
1564 return 0;
1565}
1566
Adrian Hunter20758b62011-08-29 16:42:12 +03001567static void sdhci_hw_reset(struct mmc_host *mmc)
1568{
1569 struct sdhci_host *host = mmc_priv(mmc);
1570
1571 if (host->ops && host->ops->hw_reset)
1572 host->ops->hw_reset(host);
1573}
1574
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001575static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001576{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001577 struct sdhci_host *host = mmc_priv(mmc);
1578 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001579
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001580 sdhci_runtime_pm_get(host);
1581 ret = sdhci_do_get_ro(host);
1582 sdhci_runtime_pm_put(host);
1583 return ret;
1584}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001585
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001586static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1587{
Pierre Ossman1e728592008-04-16 19:13:13 +02001588 if (host->flags & SDHCI_DEVICE_DEAD)
1589 goto out;
1590
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001591 if (enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001592 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1593 else
1594 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1595
1596 /* SDIO IRQ will be enabled as appropriate in runtime resume */
1597 if (host->runtime_suspended)
1598 goto out;
1599
1600 if (enable)
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001601 sdhci_unmask_irqs(host, SDHCI_INT_CARD_INT);
1602 else
1603 sdhci_mask_irqs(host, SDHCI_INT_CARD_INT);
Pierre Ossman1e728592008-04-16 19:13:13 +02001604out:
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001605 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001606}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001607
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001608static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1609{
1610 struct sdhci_host *host = mmc_priv(mmc);
1611 unsigned long flags;
1612
1613 spin_lock_irqsave(&host->lock, flags);
1614 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001615 spin_unlock_irqrestore(&host->lock, flags);
1616}
1617
Philip Rakity6231f3d2012-07-23 15:56:23 -07001618static int sdhci_do_3_3v_signal_voltage_switch(struct sdhci_host *host,
1619 u16 ctrl)
Arindam Nathf2119df2011-05-05 12:18:57 +05301620{
Philip Rakity6231f3d2012-07-23 15:56:23 -07001621 int ret;
Arindam Nathf2119df2011-05-05 12:18:57 +05301622
Philip Rakity6231f3d2012-07-23 15:56:23 -07001623 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1624 ctrl &= ~SDHCI_CTRL_VDD_180;
1625 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Arindam Nathf2119df2011-05-05 12:18:57 +05301626
Philip Rakity6231f3d2012-07-23 15:56:23 -07001627 if (host->vqmmc) {
Kevin Liucec2e212012-11-20 08:24:32 -05001628 ret = regulator_set_voltage(host->vqmmc, 2700000, 3600000);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001629 if (ret) {
1630 pr_warning("%s: Switching to 3.3V signalling voltage "
1631 " failed\n", mmc_hostname(host->mmc));
Arindam Nathf2119df2011-05-05 12:18:57 +05301632 return -EIO;
1633 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07001634 }
1635 /* Wait for 5ms */
1636 usleep_range(5000, 5500);
Arindam Nathf2119df2011-05-05 12:18:57 +05301637
Philip Rakity6231f3d2012-07-23 15:56:23 -07001638 /* 3.3V regulator output should be stable within 5 ms */
1639 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1640 if (!(ctrl & SDHCI_CTRL_VDD_180))
1641 return 0;
1642
1643 pr_warning("%s: 3.3V regulator output did not became stable\n",
1644 mmc_hostname(host->mmc));
1645
1646 return -EIO;
1647}
1648
1649static int sdhci_do_1_8v_signal_voltage_switch(struct sdhci_host *host,
1650 u16 ctrl)
1651{
1652 u8 pwr;
1653 u16 clk;
1654 u32 present_state;
1655 int ret;
1656
1657 /* Stop SDCLK */
1658 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1659 clk &= ~SDHCI_CLOCK_CARD_EN;
1660 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1661
1662 /* Check whether DAT[3:0] is 0000 */
1663 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1664 if (!((present_state & SDHCI_DATA_LVL_MASK) >>
1665 SDHCI_DATA_LVL_SHIFT)) {
1666 /*
1667 * Enable 1.8V Signal Enable in the Host Control2
1668 * register
1669 */
1670 if (host->vqmmc)
1671 ret = regulator_set_voltage(host->vqmmc,
Kevin Liucec2e212012-11-20 08:24:32 -05001672 1700000, 1950000);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001673 else
1674 ret = 0;
1675
1676 if (!ret) {
Arindam Nathf2119df2011-05-05 12:18:57 +05301677 ctrl |= SDHCI_CTRL_VDD_180;
1678 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1679
1680 /* Wait for 5ms */
1681 usleep_range(5000, 5500);
1682
1683 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1684 if (ctrl & SDHCI_CTRL_VDD_180) {
Philip Rakity6231f3d2012-07-23 15:56:23 -07001685 /* Provide SDCLK again and wait for 1ms */
Arindam Nathf2119df2011-05-05 12:18:57 +05301686 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1687 clk |= SDHCI_CLOCK_CARD_EN;
1688 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1689 usleep_range(1000, 1500);
1690
1691 /*
1692 * If DAT[3:0] level is 1111b, then the card
1693 * was successfully switched to 1.8V signaling.
1694 */
1695 present_state = sdhci_readl(host,
1696 SDHCI_PRESENT_STATE);
1697 if ((present_state & SDHCI_DATA_LVL_MASK) ==
1698 SDHCI_DATA_LVL_MASK)
1699 return 0;
1700 }
1701 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07001702 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301703
Philip Rakity6231f3d2012-07-23 15:56:23 -07001704 /*
1705 * If we are here, that means the switch to 1.8V signaling
1706 * failed. We power cycle the card, and retry initialization
1707 * sequence by setting S18R to 0.
1708 */
1709 pwr = sdhci_readb(host, SDHCI_POWER_CONTROL);
1710 pwr &= ~SDHCI_POWER_ON;
1711 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1712 if (host->vmmc)
1713 regulator_disable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301714
Philip Rakity6231f3d2012-07-23 15:56:23 -07001715 /* Wait for 1ms as per the spec */
1716 usleep_range(1000, 1500);
1717 pwr |= SDHCI_POWER_ON;
1718 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1719 if (host->vmmc)
1720 regulator_enable(host->vmmc);
Arindam Nathf2119df2011-05-05 12:18:57 +05301721
Philip Rakity6231f3d2012-07-23 15:56:23 -07001722 pr_warning("%s: Switching to 1.8V signalling voltage failed, "
1723 "retrying with S18R set to 0\n", mmc_hostname(host->mmc));
1724
1725 return -EAGAIN;
1726}
1727
1728static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
1729 struct mmc_ios *ios)
1730{
1731 u16 ctrl;
1732
1733 /*
1734 * Signal Voltage Switching is only applicable for Host Controllers
1735 * v3.00 and above.
1736 */
1737 if (host->version < SDHCI_SPEC_300)
1738 return 0;
1739
1740 /*
1741 * We first check whether the request is to set signalling voltage
1742 * to 3.3V. If so, we change the voltage to 3.3V and return quickly.
1743 */
1744 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1745 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1746 return sdhci_do_3_3v_signal_voltage_switch(host, ctrl);
1747 else if (!(ctrl & SDHCI_CTRL_VDD_180) &&
1748 (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180))
1749 return sdhci_do_1_8v_signal_voltage_switch(host, ctrl);
1750 else
Arindam Nathf2119df2011-05-05 12:18:57 +05301751 /* No signal voltage switch required */
1752 return 0;
1753}
1754
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001755static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1756 struct mmc_ios *ios)
1757{
1758 struct sdhci_host *host = mmc_priv(mmc);
1759 int err;
1760
1761 if (host->version < SDHCI_SPEC_300)
1762 return 0;
1763 sdhci_runtime_pm_get(host);
1764 err = sdhci_do_start_signal_voltage_switch(host, ios);
1765 sdhci_runtime_pm_put(host);
1766 return err;
1767}
1768
Girish K S069c9f12012-01-06 09:56:39 +05301769static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301770{
1771 struct sdhci_host *host;
1772 u16 ctrl;
1773 u32 ier;
1774 int tuning_loop_counter = MAX_TUNING_LOOP;
1775 unsigned long timeout;
1776 int err = 0;
Girish K S069c9f12012-01-06 09:56:39 +05301777 bool requires_tuning_nonuhs = false;
Arindam Nathb513ea22011-05-05 12:19:04 +05301778
1779 host = mmc_priv(mmc);
1780
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001781 sdhci_runtime_pm_get(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301782 disable_irq(host->irq);
1783 spin_lock(&host->lock);
1784
1785 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1786
1787 /*
Girish K S069c9f12012-01-06 09:56:39 +05301788 * The Host Controller needs tuning only in case of SDR104 mode
1789 * and for SDR50 mode when Use Tuning for SDR50 is set in the
Arindam Nathb513ea22011-05-05 12:19:04 +05301790 * Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301791 * If the Host Controller supports the HS200 mode then the
1792 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301793 */
Girish K S069c9f12012-01-06 09:56:39 +05301794 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR50) &&
1795 (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1796 host->flags & SDHCI_HS200_NEEDS_TUNING))
1797 requires_tuning_nonuhs = true;
1798
Arindam Nathb513ea22011-05-05 12:19:04 +05301799 if (((ctrl & SDHCI_CTRL_UHS_MASK) == SDHCI_CTRL_UHS_SDR104) ||
Girish K S069c9f12012-01-06 09:56:39 +05301800 requires_tuning_nonuhs)
Arindam Nathb513ea22011-05-05 12:19:04 +05301801 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1802 else {
1803 spin_unlock(&host->lock);
1804 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001805 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301806 return 0;
1807 }
1808
1809 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1810
1811 /*
1812 * As per the Host Controller spec v3.00, tuning command
1813 * generates Buffer Read Ready interrupt, so enable that.
1814 *
1815 * Note: The spec clearly says that when tuning sequence
1816 * is being performed, the controller does not generate
1817 * interrupts other than Buffer Read Ready interrupt. But
1818 * to make sure we don't hit a controller bug, we _only_
1819 * enable Buffer Read Ready interrupt here.
1820 */
1821 ier = sdhci_readl(host, SDHCI_INT_ENABLE);
1822 sdhci_clear_set_irqs(host, ier, SDHCI_INT_DATA_AVAIL);
1823
1824 /*
1825 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1826 * of loops reaches 40 times or a timeout of 150ms occurs.
1827 */
1828 timeout = 150;
1829 do {
1830 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001831 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301832
1833 if (!tuning_loop_counter && !timeout)
1834 break;
1835
Girish K S069c9f12012-01-06 09:56:39 +05301836 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301837 cmd.arg = 0;
1838 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1839 cmd.retries = 0;
1840 cmd.data = NULL;
1841 cmd.error = 0;
1842
1843 mrq.cmd = &cmd;
1844 host->mrq = &mrq;
1845
1846 /*
1847 * In response to CMD19, the card sends 64 bytes of tuning
1848 * block to the Host Controller. So we set the block size
1849 * to 64 here.
1850 */
Girish K S069c9f12012-01-06 09:56:39 +05301851 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1852 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1853 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1854 SDHCI_BLOCK_SIZE);
1855 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1856 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1857 SDHCI_BLOCK_SIZE);
1858 } else {
1859 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1860 SDHCI_BLOCK_SIZE);
1861 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301862
1863 /*
1864 * The tuning block is sent by the card to the host controller.
1865 * So we set the TRNS_READ bit in the Transfer Mode register.
1866 * This also takes care of setting DMA Enable and Multi Block
1867 * Select in the same register to 0.
1868 */
1869 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1870
1871 sdhci_send_command(host, &cmd);
1872
1873 host->cmd = NULL;
1874 host->mrq = NULL;
1875
1876 spin_unlock(&host->lock);
1877 enable_irq(host->irq);
1878
1879 /* Wait for Buffer Read Ready interrupt */
1880 wait_event_interruptible_timeout(host->buf_ready_int,
1881 (host->tuning_done == 1),
1882 msecs_to_jiffies(50));
1883 disable_irq(host->irq);
1884 spin_lock(&host->lock);
1885
1886 if (!host->tuning_done) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301887 pr_info(DRIVER_NAME ": Timeout waiting for "
Arindam Nathb513ea22011-05-05 12:19:04 +05301888 "Buffer Read Ready interrupt during tuning "
1889 "procedure, falling back to fixed sampling "
1890 "clock\n");
1891 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1892 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1893 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1894 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1895
1896 err = -EIO;
1897 goto out;
1898 }
1899
1900 host->tuning_done = 0;
1901
1902 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1903 tuning_loop_counter--;
1904 timeout--;
1905 mdelay(1);
1906 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1907
1908 /*
1909 * The Host Driver has exhausted the maximum number of loops allowed,
1910 * so use fixed sampling frequency.
1911 */
1912 if (!tuning_loop_counter || !timeout) {
1913 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1914 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1915 } else {
1916 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301917 pr_info(DRIVER_NAME ": Tuning procedure"
Arindam Nathb513ea22011-05-05 12:19:04 +05301918 " failed, falling back to fixed sampling"
1919 " clock\n");
1920 err = -EIO;
1921 }
1922 }
1923
1924out:
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301925 /*
1926 * If this is the very first time we are here, we start the retuning
1927 * timer. Since only during the first time, SDHCI_NEEDS_RETUNING
1928 * flag won't be set, we check this condition before actually starting
1929 * the timer.
1930 */
1931 if (!(host->flags & SDHCI_NEEDS_RETUNING) && host->tuning_count &&
1932 (host->tuning_mode == SDHCI_TUNING_MODE_1)) {
Aaron Lu973905f2012-07-04 13:29:09 +08001933 host->flags |= SDHCI_USING_RETUNING_TIMER;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301934 mod_timer(&host->tuning_timer, jiffies +
1935 host->tuning_count * HZ);
1936 /* Tuning mode 1 limits the maximum data length to 4MB */
1937 mmc->max_blk_count = (4 * 1024 * 1024) / mmc->max_blk_size;
1938 } else {
1939 host->flags &= ~SDHCI_NEEDS_RETUNING;
1940 /* Reload the new initial value for timer */
1941 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1942 mod_timer(&host->tuning_timer, jiffies +
1943 host->tuning_count * HZ);
1944 }
1945
1946 /*
1947 * In case tuning fails, host controllers which support re-tuning can
1948 * try tuning again at a later time, when the re-tuning timer expires.
1949 * So for these controllers, we return 0. Since there might be other
1950 * controllers who do not have this capability, we return error for
Aaron Lu973905f2012-07-04 13:29:09 +08001951 * them. SDHCI_USING_RETUNING_TIMER means the host is currently using
1952 * a retuning timer to do the retuning for the card.
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301953 */
Aaron Lu973905f2012-07-04 13:29:09 +08001954 if (err && (host->flags & SDHCI_USING_RETUNING_TIMER))
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301955 err = 0;
1956
Arindam Nathb513ea22011-05-05 12:19:04 +05301957 sdhci_clear_set_irqs(host, SDHCI_INT_DATA_AVAIL, ier);
1958 spin_unlock(&host->lock);
1959 enable_irq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001960 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05301961
1962 return err;
1963}
1964
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001965static void sdhci_do_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301966{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301967 u16 ctrl;
1968 unsigned long flags;
1969
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301970 /* Host Controller v3.00 defines preset value registers */
1971 if (host->version < SDHCI_SPEC_300)
1972 return;
1973
1974 spin_lock_irqsave(&host->lock, flags);
1975
1976 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1977
1978 /*
1979 * We only enable or disable Preset Value if they are not already
1980 * enabled or disabled respectively. Otherwise, we bail out.
1981 */
1982 if (enable && !(ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1983 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
1984 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001985 host->flags |= SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301986 } else if (!enable && (ctrl & SDHCI_CTRL_PRESET_VAL_ENABLE)) {
1987 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
1988 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001989 host->flags &= ~SDHCI_PV_ENABLED;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301990 }
1991
1992 spin_unlock_irqrestore(&host->lock, flags);
1993}
1994
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001995static void sdhci_enable_preset_value(struct mmc_host *mmc, bool enable)
1996{
1997 struct sdhci_host *host = mmc_priv(mmc);
1998
1999 sdhci_runtime_pm_get(host);
2000 sdhci_do_enable_preset_value(host, enable);
2001 sdhci_runtime_pm_put(host);
2002}
2003
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002004static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002005{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002006 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002007 unsigned long flags;
2008
Pierre Ossmand129bce2006-03-24 03:18:17 -08002009 spin_lock_irqsave(&host->lock, flags);
2010
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002011 /* Check host->mrq first in case we are runtime suspended */
2012 if (host->mrq &&
2013 !(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302014 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002015 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302016 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002017 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002018
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002019 sdhci_reset(host, SDHCI_RESET_CMD);
2020 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002021
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002022 host->mrq->cmd->error = -ENOMEDIUM;
2023 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002024 }
2025
2026 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002027}
2028
2029static const struct mmc_host_ops sdhci_ops = {
2030 .request = sdhci_request,
2031 .set_ios = sdhci_set_ios,
2032 .get_ro = sdhci_get_ro,
2033 .hw_reset = sdhci_hw_reset,
2034 .enable_sdio_irq = sdhci_enable_sdio_irq,
2035 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
2036 .execute_tuning = sdhci_execute_tuning,
2037 .enable_preset_value = sdhci_enable_preset_value,
2038 .card_event = sdhci_card_event,
2039};
2040
2041/*****************************************************************************\
2042 * *
2043 * Tasklets *
2044 * *
2045\*****************************************************************************/
2046
2047static void sdhci_tasklet_card(unsigned long param)
2048{
2049 struct sdhci_host *host = (struct sdhci_host*)param;
2050
2051 sdhci_card_event(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002052
Pierre Ossman04cf5852008-08-18 22:18:14 +02002053 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002054}
2055
2056static void sdhci_tasklet_finish(unsigned long param)
2057{
2058 struct sdhci_host *host;
2059 unsigned long flags;
2060 struct mmc_request *mrq;
2061
2062 host = (struct sdhci_host*)param;
2063
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002064 spin_lock_irqsave(&host->lock, flags);
2065
Chris Ball0c9c99a2011-04-27 17:35:31 -04002066 /*
2067 * If this tasklet gets rescheduled while running, it will
2068 * be run again afterwards but without any active request.
2069 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002070 if (!host->mrq) {
2071 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002072 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002073 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002074
2075 del_timer(&host->timer);
2076
2077 mrq = host->mrq;
2078
Pierre Ossmand129bce2006-03-24 03:18:17 -08002079 /*
2080 * The controller needs a reset of internal state machines
2081 * upon error conditions.
2082 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002083 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002084 ((mrq->cmd && mrq->cmd->error) ||
Pierre Ossman1e728592008-04-16 19:13:13 +02002085 (mrq->data && (mrq->data->error ||
2086 (mrq->data->stop && mrq->data->stop->error))) ||
2087 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002088
2089 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002090 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002091 /* This is to force an update */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002092 sdhci_update_clock(host);
Pierre Ossman645289d2006-06-30 02:22:33 -07002093
2094 /* Spec says we should do both at the same time, but Ricoh
2095 controllers do not like that. */
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096 sdhci_reset(host, SDHCI_RESET_CMD);
2097 sdhci_reset(host, SDHCI_RESET_DATA);
2098 }
2099
2100 host->mrq = NULL;
2101 host->cmd = NULL;
2102 host->data = NULL;
2103
Pierre Ossmanf9134312008-12-21 17:01:48 +01002104#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002105 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002106#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002107
Pierre Ossman5f25a662006-10-04 02:15:39 -07002108 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002109 spin_unlock_irqrestore(&host->lock, flags);
2110
2111 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002112 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113}
2114
2115static void sdhci_timeout_timer(unsigned long data)
2116{
2117 struct sdhci_host *host;
2118 unsigned long flags;
2119
2120 host = (struct sdhci_host*)data;
2121
2122 spin_lock_irqsave(&host->lock, flags);
2123
2124 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302125 pr_err("%s: Timeout waiting for hardware "
Pierre Ossmanacf1da42007-02-09 08:29:19 +01002126 "interrupt.\n", mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002127 sdhci_dumpregs(host);
2128
2129 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002130 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131 sdhci_finish_data(host);
2132 } else {
2133 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002134 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002135 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002136 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002137
2138 tasklet_schedule(&host->finish_tasklet);
2139 }
2140 }
2141
Pierre Ossman5f25a662006-10-04 02:15:39 -07002142 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002143 spin_unlock_irqrestore(&host->lock, flags);
2144}
2145
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302146static void sdhci_tuning_timer(unsigned long data)
2147{
2148 struct sdhci_host *host;
2149 unsigned long flags;
2150
2151 host = (struct sdhci_host *)data;
2152
2153 spin_lock_irqsave(&host->lock, flags);
2154
2155 host->flags |= SDHCI_NEEDS_RETUNING;
2156
2157 spin_unlock_irqrestore(&host->lock, flags);
2158}
2159
Pierre Ossmand129bce2006-03-24 03:18:17 -08002160/*****************************************************************************\
2161 * *
2162 * Interrupt handling *
2163 * *
2164\*****************************************************************************/
2165
2166static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
2167{
2168 BUG_ON(intmask == 0);
2169
2170 if (!host->cmd) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302171 pr_err("%s: Got command interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002172 "though no command operation was in progress.\n",
2173 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002174 sdhci_dumpregs(host);
2175 return;
2176 }
2177
Pierre Ossman43b58b32007-07-25 23:15:27 +02002178 if (intmask & SDHCI_INT_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002179 host->cmd->error = -ETIMEDOUT;
2180 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
2181 SDHCI_INT_INDEX))
2182 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183
Pierre Ossmane8095172008-07-25 01:09:08 +02002184 if (host->cmd->error) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002185 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002186 return;
2187 }
2188
2189 /*
2190 * The host can send and interrupt when the busy state has
2191 * ended, allowing us to wait without wasting CPU cycles.
2192 * Unfortunately this is overloaded on the "data complete"
2193 * interrupt, so we need to take some care when handling
2194 * it.
2195 *
2196 * Note: The 1.0 specification is a bit ambiguous about this
2197 * feature so there might be some problems with older
2198 * controllers.
2199 */
2200 if (host->cmd->flags & MMC_RSP_BUSY) {
2201 if (host->cmd->data)
2202 DBG("Cannot wait for busy signal when also "
2203 "doing a data transfer");
Ben Dooksf9454052009-02-20 20:33:08 +03002204 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ))
Pierre Ossmane8095172008-07-25 01:09:08 +02002205 return;
Ben Dooksf9454052009-02-20 20:33:08 +03002206
2207 /* The controller does not support the end-of-busy IRQ,
2208 * fall through and take the SDHCI_INT_RESPONSE */
Pierre Ossmane8095172008-07-25 01:09:08 +02002209 }
2210
2211 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002212 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002213}
2214
George G. Davis0957c332010-02-18 12:32:12 -05002215#ifdef CONFIG_MMC_DEBUG
Ben Dooks6882a8c2009-06-14 13:52:38 +01002216static void sdhci_show_adma_error(struct sdhci_host *host)
2217{
2218 const char *name = mmc_hostname(host->mmc);
2219 u8 *desc = host->adma_desc;
2220 __le32 *dma;
2221 __le16 *len;
2222 u8 attr;
2223
2224 sdhci_dumpregs(host);
2225
2226 while (true) {
2227 dma = (__le32 *)(desc + 4);
2228 len = (__le16 *)(desc + 2);
2229 attr = *desc;
2230
2231 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2232 name, desc, le32_to_cpu(*dma), le16_to_cpu(*len), attr);
2233
2234 desc += 8;
2235
2236 if (attr & 2)
2237 break;
2238 }
2239}
2240#else
2241static void sdhci_show_adma_error(struct sdhci_host *host) { }
2242#endif
2243
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2245{
Girish K S069c9f12012-01-06 09:56:39 +05302246 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002247 BUG_ON(intmask == 0);
2248
Arindam Nathb513ea22011-05-05 12:19:04 +05302249 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2250 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302251 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2252 if (command == MMC_SEND_TUNING_BLOCK ||
2253 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302254 host->tuning_done = 1;
2255 wake_up(&host->buf_ready_int);
2256 return;
2257 }
2258 }
2259
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 if (!host->data) {
2261 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002262 * The "data complete" interrupt is also used to
2263 * indicate that a busy state has ended. See comment
2264 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002265 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002266 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
2267 if (intmask & SDHCI_INT_DATA_END) {
2268 sdhci_finish_command(host);
2269 return;
2270 }
2271 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272
Girish K Sa3c76eb2011-10-11 11:44:09 +05302273 pr_err("%s: Got data interrupt 0x%08x even "
Pierre Ossmanb67ac3f2007-08-12 17:29:47 +02002274 "though no data operation was in progress.\n",
2275 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276 sdhci_dumpregs(host);
2277
2278 return;
2279 }
2280
2281 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002282 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002283 else if (intmask & SDHCI_INT_DATA_END_BIT)
2284 host->data->error = -EILSEQ;
2285 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2286 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2287 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002288 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002289 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302290 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002291 sdhci_show_adma_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002292 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002293 if (host->ops->adma_workaround)
2294 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002295 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002296
Pierre Ossman17b04292007-07-22 22:18:46 +02002297 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002298 sdhci_finish_data(host);
2299 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002300 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301 sdhci_transfer_pio(host);
2302
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002303 /*
2304 * We currently don't do anything fancy with DMA
2305 * boundaries, but as we can't disable the feature
2306 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002307 *
2308 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2309 * should return a valid address to continue from, but as
2310 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002311 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002312 if (intmask & SDHCI_INT_DMA_END) {
2313 u32 dmastart, dmanow;
2314 dmastart = sg_dma_address(host->data->sg);
2315 dmanow = dmastart + host->data->bytes_xfered;
2316 /*
2317 * Force update to the next DMA block boundary.
2318 */
2319 dmanow = (dmanow &
2320 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2321 SDHCI_DEFAULT_BOUNDARY_SIZE;
2322 host->data->bytes_xfered = dmanow - dmastart;
2323 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2324 " next 0x%08x\n",
2325 mmc_hostname(host->mmc), dmastart,
2326 host->data->bytes_xfered, dmanow);
2327 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2328 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002329
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002330 if (intmask & SDHCI_INT_DATA_END) {
2331 if (host->cmd) {
2332 /*
2333 * Data managed to finish before the
2334 * command completed. Make sure we do
2335 * things in the proper order.
2336 */
2337 host->data_early = 1;
2338 } else {
2339 sdhci_finish_data(host);
2340 }
2341 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342 }
2343}
2344
David Howells7d12e782006-10-05 14:55:46 +01002345static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346{
2347 irqreturn_t result;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002348 struct sdhci_host *host = dev_id;
Alexander Stein6379b232012-03-14 09:52:10 +01002349 u32 intmask, unexpected = 0;
2350 int cardint = 0, max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351
2352 spin_lock(&host->lock);
2353
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002354 if (host->runtime_suspended) {
2355 spin_unlock(&host->lock);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302356 pr_warning("%s: got irq while runtime suspended\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002357 mmc_hostname(host->mmc));
2358 return IRQ_HANDLED;
2359 }
2360
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002361 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362
Mark Lord62df67a52007-03-06 13:30:13 +01002363 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364 result = IRQ_NONE;
2365 goto out;
2366 }
2367
Alexander Stein6379b232012-03-14 09:52:10 +01002368again:
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002369 DBG("*** %s got interrupt: 0x%08x\n",
2370 mmc_hostname(host->mmc), intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002371
Pierre Ossman3192a282006-06-30 02:22:26 -07002372 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Shawn Guod25928d2011-06-21 22:41:48 +08002373 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2374 SDHCI_CARD_PRESENT;
2375
2376 /*
2377 * There is a observation on i.mx esdhc. INSERT bit will be
2378 * immediately set again when it gets cleared, if a card is
2379 * inserted. We have to mask the irq to prevent interrupt
2380 * storm which will freeze the system. And the REMOVE gets
2381 * the same situation.
2382 *
2383 * More testing are needed here to ensure it works for other
2384 * platforms though.
2385 */
2386 sdhci_mask_irqs(host, present ? SDHCI_INT_CARD_INSERT :
2387 SDHCI_INT_CARD_REMOVE);
2388 sdhci_unmask_irqs(host, present ? SDHCI_INT_CARD_REMOVE :
2389 SDHCI_INT_CARD_INSERT);
2390
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002391 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
Shawn Guod25928d2011-06-21 22:41:48 +08002392 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
2393 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002394 tasklet_schedule(&host->card_tasklet);
Pierre Ossman3192a282006-06-30 02:22:26 -07002395 }
2396
Pierre Ossmand129bce2006-03-24 03:18:17 -08002397 if (intmask & SDHCI_INT_CMD_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002398 sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
2399 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002400 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401 }
2402
2403 if (intmask & SDHCI_INT_DATA_MASK) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002404 sdhci_writel(host, intmask & SDHCI_INT_DATA_MASK,
2405 SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002406 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 }
2408
2409 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
2410
Pierre Ossman964f9ce2007-07-20 18:20:36 +02002411 intmask &= ~SDHCI_INT_ERROR;
2412
Pierre Ossmand129bce2006-03-24 03:18:17 -08002413 if (intmask & SDHCI_INT_BUS_POWER) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302414 pr_err("%s: Card is consuming too much power!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08002415 mmc_hostname(host->mmc));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002416 sdhci_writel(host, SDHCI_INT_BUS_POWER, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 }
2418
Rolf Eike Beer9d26a5d2007-06-26 13:31:16 +02002419 intmask &= ~SDHCI_INT_BUS_POWER;
Pierre Ossman3192a282006-06-30 02:22:26 -07002420
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002421 if (intmask & SDHCI_INT_CARD_INT)
2422 cardint = 1;
2423
2424 intmask &= ~SDHCI_INT_CARD_INT;
2425
Pierre Ossman3192a282006-06-30 02:22:26 -07002426 if (intmask) {
Alexander Stein6379b232012-03-14 09:52:10 +01002427 unexpected |= intmask;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002428 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
Pierre Ossman3192a282006-06-30 02:22:26 -07002429 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430
2431 result = IRQ_HANDLED;
2432
Alexander Stein6379b232012-03-14 09:52:10 +01002433 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
2434 if (intmask && --max_loops)
2435 goto again;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002436out:
2437 spin_unlock(&host->lock);
2438
Alexander Stein6379b232012-03-14 09:52:10 +01002439 if (unexpected) {
2440 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2441 mmc_hostname(host->mmc), unexpected);
2442 sdhci_dumpregs(host);
2443 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002444 /*
2445 * We have to delay this as it calls back into the driver.
2446 */
2447 if (cardint)
2448 mmc_signal_sdio_irq(host->mmc);
2449
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450 return result;
2451}
2452
2453/*****************************************************************************\
2454 * *
2455 * Suspend/resume *
2456 * *
2457\*****************************************************************************/
2458
2459#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002460void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2461{
2462 u8 val;
2463 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2464 | SDHCI_WAKE_ON_INT;
2465
2466 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2467 val |= mask ;
2468 /* Avoid fake wake up */
2469 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2470 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2471 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2472}
2473EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2474
2475void sdhci_disable_irq_wakeups(struct sdhci_host *host)
2476{
2477 u8 val;
2478 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2479 | SDHCI_WAKE_ON_INT;
2480
2481 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2482 val &= ~mask;
2483 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2484}
2485EXPORT_SYMBOL_GPL(sdhci_disable_irq_wakeups);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002486
Manuel Lauss29495aa2011-11-03 11:09:45 +01002487int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002488{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002489 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002490
Chris Balla1b13b42012-02-06 00:43:59 -05002491 if (host->ops->platform_suspend)
2492 host->ops->platform_suspend(host);
2493
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002494 sdhci_disable_card_detection(host);
2495
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302496 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002497 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Luc6ced0d2011-12-28 11:11:12 +08002498 del_timer_sync(&host->tuning_timer);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302499 host->flags &= ~SDHCI_NEEDS_RETUNING;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302500 }
2501
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002502 ret = mmc_suspend_host(host->mmc);
Aaron Lu38a60ea2012-01-04 10:07:43 +08002503 if (ret) {
Aaron Lu973905f2012-07-04 13:29:09 +08002504 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Aaron Lu38a60ea2012-01-04 10:07:43 +08002505 host->flags |= SDHCI_NEEDS_RETUNING;
2506 mod_timer(&host->tuning_timer, jiffies +
2507 host->tuning_count * HZ);
2508 }
2509
2510 sdhci_enable_card_detection(host);
2511
Pierre Ossmandf1c4b72007-01-30 07:55:15 +01002512 return ret;
Aaron Lu38a60ea2012-01-04 10:07:43 +08002513 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514
Kevin Liuad080d72013-01-05 17:21:33 +08002515 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2516 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2517 free_irq(host->irq, host);
2518 } else {
2519 sdhci_enable_irq_wakeups(host);
2520 enable_irq_wake(host->irq);
2521 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07002522 return ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002523}
2524
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002525EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002527int sdhci_resume_host(struct sdhci_host *host)
2528{
2529 int ret;
2530
Richard Röjforsa13abc72009-09-22 16:45:30 -07002531 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002532 if (host->ops->enable_dma)
2533 host->ops->enable_dma(host);
2534 }
2535
Kevin Liuad080d72013-01-05 17:21:33 +08002536 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2537 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
2538 mmc_hostname(host->mmc), host);
2539 if (ret)
2540 return ret;
2541 } else {
2542 sdhci_disable_irq_wakeups(host);
2543 disable_irq_wake(host->irq);
2544 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002545
Adrian Hunter6308d292012-02-07 14:48:54 +02002546 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2547 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2548 /* Card keeps power but host controller does not */
2549 sdhci_init(host, 0);
2550 host->pwr = 0;
2551 host->clock = 0;
2552 sdhci_do_set_ios(host, &host->mmc->ios);
2553 } else {
2554 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2555 mmiowb();
2556 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002557
2558 ret = mmc_resume_host(host->mmc);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002559 sdhci_enable_card_detection(host);
2560
Chris Balla1b13b42012-02-06 00:43:59 -05002561 if (host->ops->platform_resume)
2562 host->ops->platform_resume(host);
2563
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302564 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002565 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302566 host->flags |= SDHCI_NEEDS_RETUNING;
2567
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002568 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002569}
2570
2571EXPORT_SYMBOL_GPL(sdhci_resume_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002572#endif /* CONFIG_PM */
2573
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002574#ifdef CONFIG_PM_RUNTIME
2575
2576static int sdhci_runtime_pm_get(struct sdhci_host *host)
2577{
2578 return pm_runtime_get_sync(host->mmc->parent);
2579}
2580
2581static int sdhci_runtime_pm_put(struct sdhci_host *host)
2582{
2583 pm_runtime_mark_last_busy(host->mmc->parent);
2584 return pm_runtime_put_autosuspend(host->mmc->parent);
2585}
2586
2587int sdhci_runtime_suspend_host(struct sdhci_host *host)
2588{
2589 unsigned long flags;
2590 int ret = 0;
2591
2592 /* Disable tuning since we are suspending */
Aaron Lu973905f2012-07-04 13:29:09 +08002593 if (host->flags & SDHCI_USING_RETUNING_TIMER) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002594 del_timer_sync(&host->tuning_timer);
2595 host->flags &= ~SDHCI_NEEDS_RETUNING;
2596 }
2597
2598 spin_lock_irqsave(&host->lock, flags);
2599 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
2600 spin_unlock_irqrestore(&host->lock, flags);
2601
2602 synchronize_irq(host->irq);
2603
2604 spin_lock_irqsave(&host->lock, flags);
2605 host->runtime_suspended = true;
2606 spin_unlock_irqrestore(&host->lock, flags);
2607
2608 return ret;
2609}
2610EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2611
2612int sdhci_runtime_resume_host(struct sdhci_host *host)
2613{
2614 unsigned long flags;
2615 int ret = 0, host_flags = host->flags;
2616
2617 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2618 if (host->ops->enable_dma)
2619 host->ops->enable_dma(host);
2620 }
2621
2622 sdhci_init(host, 0);
2623
2624 /* Force clock and power re-program */
2625 host->pwr = 0;
2626 host->clock = 0;
2627 sdhci_do_set_ios(host, &host->mmc->ios);
2628
2629 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
2630 if (host_flags & SDHCI_PV_ENABLED)
2631 sdhci_do_enable_preset_value(host, true);
2632
2633 /* Set the re-tuning expiration flag */
Aaron Lu973905f2012-07-04 13:29:09 +08002634 if (host->flags & SDHCI_USING_RETUNING_TIMER)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002635 host->flags |= SDHCI_NEEDS_RETUNING;
2636
2637 spin_lock_irqsave(&host->lock, flags);
2638
2639 host->runtime_suspended = false;
2640
2641 /* Enable SDIO IRQ */
2642 if ((host->flags & SDHCI_SDIO_IRQ_ENABLED))
2643 sdhci_enable_sdio_irq_nolock(host, true);
2644
2645 /* Enable Card Detection */
2646 sdhci_enable_card_detection(host);
2647
2648 spin_unlock_irqrestore(&host->lock, flags);
2649
2650 return ret;
2651}
2652EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2653
2654#endif
2655
Pierre Ossmand129bce2006-03-24 03:18:17 -08002656/*****************************************************************************\
2657 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002658 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002659 * *
2660\*****************************************************************************/
2661
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002662struct sdhci_host *sdhci_alloc_host(struct device *dev,
2663 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002664{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665 struct mmc_host *mmc;
2666 struct sdhci_host *host;
2667
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002668 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002669
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002670 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002671 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002672 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673
2674 host = mmc_priv(mmc);
2675 host->mmc = mmc;
2676
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002677 return host;
2678}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002679
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002680EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002681
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002682int sdhci_add_host(struct sdhci_host *host)
2683{
2684 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002685 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302686 u32 max_current_caps;
2687 unsigned int ocr_avail;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002688 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002690 WARN_ON(host == NULL);
2691 if (host == NULL)
2692 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002693
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002694 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002695
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002696 if (debug_quirks)
2697 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002698 if (debug_quirks2)
2699 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002700
Pierre Ossmand96649e2006-06-30 02:22:30 -07002701 sdhci_reset(host, SDHCI_RESET_ALL);
2702
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002703 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002704 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2705 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002706 if (host->version > SDHCI_SPEC_300) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302707 pr_err("%s: Unknown controller version (%d). "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01002708 "You may experience problems.\n", mmc_hostname(mmc),
Pierre Ossman2134a922008-06-28 18:28:51 +02002709 host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002710 }
2711
Arindam Nathf2119df2011-05-05 12:18:57 +05302712 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002713 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002714
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002715 if (host->version >= SDHCI_SPEC_300)
2716 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2717 host->caps1 :
2718 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302719
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002720 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002721 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302722 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002723 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002724 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002725 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002726
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002727 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002728 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002729 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002730 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002731 }
2732
Arindam Nathf2119df2011-05-05 12:18:57 +05302733 if ((host->version >= SDHCI_SPEC_200) &&
2734 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002735 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002736
2737 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2738 (host->flags & SDHCI_USE_ADMA)) {
2739 DBG("Disabling ADMA as it is marked broken\n");
2740 host->flags &= ~SDHCI_USE_ADMA;
2741 }
2742
Richard Röjforsa13abc72009-09-22 16:45:30 -07002743 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002744 if (host->ops->enable_dma) {
2745 if (host->ops->enable_dma(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302746 pr_warning("%s: No suitable DMA "
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002747 "available. Falling back to PIO.\n",
2748 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002749 host->flags &=
2750 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002751 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002752 }
2753 }
2754
Pierre Ossman2134a922008-06-28 18:28:51 +02002755 if (host->flags & SDHCI_USE_ADMA) {
2756 /*
2757 * We need to allocate descriptors for all sg entries
2758 * (128) and potentially one alignment transfer for
2759 * each of those entries.
2760 */
2761 host->adma_desc = kmalloc((128 * 2 + 1) * 4, GFP_KERNEL);
2762 host->align_buffer = kmalloc(128 * 4, GFP_KERNEL);
2763 if (!host->adma_desc || !host->align_buffer) {
2764 kfree(host->adma_desc);
2765 kfree(host->align_buffer);
Girish K Sa3c76eb2011-10-11 11:44:09 +05302766 pr_warning("%s: Unable to allocate ADMA "
Pierre Ossman2134a922008-06-28 18:28:51 +02002767 "buffers. Falling back to standard DMA.\n",
2768 mmc_hostname(mmc));
2769 host->flags &= ~SDHCI_USE_ADMA;
2770 }
2771 }
2772
Pierre Ossman76591502008-07-21 00:32:11 +02002773 /*
2774 * If we use DMA, then it's up to the caller to set the DMA
2775 * mask, but PIO does not need the hw shim so we set a new
2776 * mask here in that case.
2777 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002778 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002779 host->dma_mask = DMA_BIT_MASK(64);
2780 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2781 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002783 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302784 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002785 >> SDHCI_CLOCK_BASE_SHIFT;
2786 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302787 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002788 >> SDHCI_CLOCK_BASE_SHIFT;
2789
Pierre Ossmand129bce2006-03-24 03:18:17 -08002790 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002791 if (host->max_clk == 0 || host->quirks &
2792 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002793 if (!host->ops->get_max_clock) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302794 pr_err("%s: Hardware doesn't specify base clock "
Ben Dooks4240ff02009-03-17 00:13:57 +03002795 "frequency.\n", mmc_hostname(mmc));
2796 return -ENODEV;
2797 }
2798 host->max_clk = host->ops->get_max_clock(host);
2799 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002800
2801 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302802 * In case of Host Controller v3.00, find out whether clock
2803 * multiplier is supported.
2804 */
2805 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2806 SDHCI_CLOCK_MUL_SHIFT;
2807
2808 /*
2809 * In case the value in Clock Multiplier is 0, then programmable
2810 * clock mode is not supported, otherwise the actual clock
2811 * multiplier is one more than the value of Clock Multiplier
2812 * in the Capabilities Register.
2813 */
2814 if (host->clk_mul)
2815 host->clk_mul += 1;
2816
2817 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002818 * Set host parameters.
2819 */
2820 mmc->ops = &sdhci_ops;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302821 mmc->f_max = host->max_clk;
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002822 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002823 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302824 else if (host->version >= SDHCI_SPEC_300) {
2825 if (host->clk_mul) {
2826 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
2827 mmc->f_max = host->max_clk * host->clk_mul;
2828 } else
2829 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2830 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002831 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002832
Andy Shevchenko272308c2011-08-03 18:36:00 +03002833 host->timeout_clk =
2834 (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT;
2835 if (host->timeout_clk == 0) {
2836 if (host->ops->get_timeout_clock) {
2837 host->timeout_clk = host->ops->get_timeout_clock(host);
2838 } else if (!(host->quirks &
2839 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302840 pr_err("%s: Hardware doesn't specify timeout clock "
Andy Shevchenko272308c2011-08-03 18:36:00 +03002841 "frequency.\n", mmc_hostname(mmc));
2842 return -ENODEV;
2843 }
2844 }
2845 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
2846 host->timeout_clk *= 1000;
2847
2848 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002849 host->timeout_clk = mmc->f_max / 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03002850
Andy Shevchenko65be3fe2011-08-03 18:36:01 +03002851 mmc->max_discard_to = (1 << 27) / host->timeout_clk;
Adrian Hunter58d12462011-06-28 17:16:03 +03002852
Andrei Warkentine89d4562011-05-23 15:06:37 -05002853 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
2854
2855 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
2856 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002857
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002858 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002859 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002860 ((host->flags & SDHCI_USE_ADMA) ||
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04002861 !(host->flags & SDHCI_USE_SDMA))) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05002862 host->flags |= SDHCI_AUTO_CMD23;
2863 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
2864 } else {
2865 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
2866 }
2867
Philip Rakity15ec4462010-11-19 16:48:39 -05002868 /*
2869 * A controller may support 8-bit width, but the board itself
2870 * might not have the pins brought out. Boards that support
2871 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
2872 * their platform code before calling sdhci_add_host(), and we
2873 * won't assume 8-bit width for hosts without that CAP.
2874 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04002875 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05002876 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002877
Jerry Huang63ef5d82012-10-25 13:47:19 +08002878 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
2879 mmc->caps &= ~MMC_CAP_CMD23;
2880
Arindam Nathf2119df2011-05-05 12:18:57 +05302881 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04002882 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002883
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01002884 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Daniel Drakeeb6d5ae2012-07-05 22:06:13 +01002885 !(host->mmc->caps & MMC_CAP_NONREMOVABLE))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03002886 mmc->caps |= MMC_CAP_NEEDS_POLL;
2887
Philip Rakity6231f3d2012-07-23 15:56:23 -07002888 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
2889 host->vqmmc = regulator_get(mmc_dev(mmc), "vqmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002890 if (IS_ERR_OR_NULL(host->vqmmc)) {
2891 if (PTR_ERR(host->vqmmc) < 0) {
2892 pr_info("%s: no vqmmc regulator found\n",
2893 mmc_hostname(mmc));
2894 host->vqmmc = NULL;
2895 }
Kevin Liu8363c372012-11-17 17:55:51 -05002896 } else {
Philip Rakity6231f3d2012-07-23 15:56:23 -07002897 regulator_enable(host->vqmmc);
Kevin Liucec2e212012-11-20 08:24:32 -05002898 if (!regulator_is_supported_voltage(host->vqmmc, 1700000,
2899 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05002900 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
2901 SDHCI_SUPPORT_SDR50 |
2902 SDHCI_SUPPORT_DDR50);
2903 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07002904
Daniel Drake6a661802012-11-25 13:01:19 -05002905 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
2906 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2907 SDHCI_SUPPORT_DDR50);
2908
Al Cooper4188bba2012-03-16 15:54:17 -04002909 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
2910 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
2911 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05302912 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
2913
2914 /* SDR104 supports also implies SDR50 support */
2915 if (caps[1] & SDHCI_SUPPORT_SDR104)
2916 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
2917 else if (caps[1] & SDHCI_SUPPORT_SDR50)
2918 mmc->caps |= MMC_CAP_UHS_SDR50;
2919
2920 if (caps[1] & SDHCI_SUPPORT_DDR50)
2921 mmc->caps |= MMC_CAP_UHS_DDR50;
2922
Girish K S069c9f12012-01-06 09:56:39 +05302923 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05302924 if (caps[1] & SDHCI_USE_SDR50_TUNING)
2925 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
2926
Girish K S069c9f12012-01-06 09:56:39 +05302927 /* Does the host need tuning for HS200? */
2928 if (mmc->caps2 & MMC_CAP2_HS200)
2929 host->flags |= SDHCI_HS200_NEEDS_TUNING;
2930
Arindam Nathd6d50a12011-05-05 12:18:59 +05302931 /* Driver Type(s) (A, C, D) supported by the host */
2932 if (caps[1] & SDHCI_DRIVER_TYPE_A)
2933 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
2934 if (caps[1] & SDHCI_DRIVER_TYPE_C)
2935 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
2936 if (caps[1] & SDHCI_DRIVER_TYPE_D)
2937 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
2938
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302939 /* Initial value for re-tuning timer count */
2940 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
2941 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
2942
2943 /*
2944 * In case Re-tuning Timer is not disabled, the actual value of
2945 * re-tuning timer will be 2 ^ (n - 1).
2946 */
2947 if (host->tuning_count)
2948 host->tuning_count = 1 << (host->tuning_count - 1);
2949
2950 /* Re-tuning mode supported by the Host Controller */
2951 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
2952 SDHCI_RETUNING_MODE_SHIFT;
2953
Takashi Iwai8f230f42010-12-08 10:04:30 +01002954 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07002955
2956 host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
Kevin Liu657d5982012-10-17 19:04:44 +08002957 if (IS_ERR_OR_NULL(host->vmmc)) {
2958 if (PTR_ERR(host->vmmc) < 0) {
2959 pr_info("%s: no vmmc regulator found\n",
2960 mmc_hostname(mmc));
2961 host->vmmc = NULL;
2962 }
Kevin Liu8363c372012-11-17 17:55:51 -05002963 }
Philip Rakitybad37e12012-05-27 18:36:44 -07002964
Philip Rakity68737042012-06-08 12:26:13 -07002965#ifdef CONFIG_REGULATOR
2966 if (host->vmmc) {
Kevin Liucec2e212012-11-20 08:24:32 -05002967 ret = regulator_is_supported_voltage(host->vmmc, 2700000,
2968 3600000);
Philip Rakity68737042012-06-08 12:26:13 -07002969 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_330)))
2970 caps[0] &= ~SDHCI_CAN_VDD_330;
Philip Rakity68737042012-06-08 12:26:13 -07002971 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_300)))
2972 caps[0] &= ~SDHCI_CAN_VDD_300;
Kevin Liucec2e212012-11-20 08:24:32 -05002973 ret = regulator_is_supported_voltage(host->vmmc, 1700000,
2974 1950000);
Philip Rakity68737042012-06-08 12:26:13 -07002975 if ((ret <= 0) || (!(caps[0] & SDHCI_CAN_VDD_180)))
2976 caps[0] &= ~SDHCI_CAN_VDD_180;
2977 }
2978#endif /* CONFIG_REGULATOR */
2979
Arindam Nathf2119df2011-05-05 12:18:57 +05302980 /*
2981 * According to SD Host Controller spec v3.00, if the Host System
2982 * can afford more than 150mA, Host Driver should set XPC to 1. Also
2983 * the value is meaningful only if Voltage Support in the Capabilities
2984 * register is set. The actual current value is 4 times the register
2985 * value.
2986 */
2987 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Philip Rakitybad37e12012-05-27 18:36:44 -07002988 if (!max_current_caps && host->vmmc) {
2989 u32 curr = regulator_get_current_limit(host->vmmc);
2990 if (curr > 0) {
2991
2992 /* convert to SDHCI_MAX_CURRENT format */
2993 curr = curr/1000; /* convert to mA */
2994 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
2995
2996 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
2997 max_current_caps =
2998 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
2999 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3000 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3001 }
3002 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303003
3004 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003005 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303006
Aaron Lu55c46652012-07-04 13:31:48 +08003007 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303008 SDHCI_MAX_CURRENT_330_MASK) >>
3009 SDHCI_MAX_CURRENT_330_SHIFT) *
3010 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303011 }
3012 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003013 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303014
Aaron Lu55c46652012-07-04 13:31:48 +08003015 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303016 SDHCI_MAX_CURRENT_300_MASK) >>
3017 SDHCI_MAX_CURRENT_300_SHIFT) *
3018 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303019 }
3020 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003021 ocr_avail |= MMC_VDD_165_195;
3022
Aaron Lu55c46652012-07-04 13:31:48 +08003023 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303024 SDHCI_MAX_CURRENT_180_MASK) >>
3025 SDHCI_MAX_CURRENT_180_SHIFT) *
3026 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303027 }
3028
Takashi Iwai8f230f42010-12-08 10:04:30 +01003029 mmc->ocr_avail = ocr_avail;
3030 mmc->ocr_avail_sdio = ocr_avail;
3031 if (host->ocr_avail_sdio)
3032 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3033 mmc->ocr_avail_sd = ocr_avail;
3034 if (host->ocr_avail_sd)
3035 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3036 else /* normal SD controllers don't support 1.8V */
3037 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3038 mmc->ocr_avail_mmc = ocr_avail;
3039 if (host->ocr_avail_mmc)
3040 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003041
3042 if (mmc->ocr_avail == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303043 pr_err("%s: Hardware doesn't report any "
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003044 "support voltages.\n", mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003045 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003046 }
3047
Pierre Ossmand129bce2006-03-24 03:18:17 -08003048 spin_lock_init(&host->lock);
3049
3050 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003051 * Maximum number of segments. Depends on if the hardware
3052 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003054 if (host->flags & SDHCI_USE_ADMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003055 mmc->max_segs = 128;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003056 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003057 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003058 else /* PIO */
Martin K. Petersena36274e2010-09-10 01:33:59 -04003059 mmc->max_segs = 128;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003060
3061 /*
Pierre Ossmanbab76962006-07-02 16:51:35 +01003062 * Maximum number of sectors in one transfer. Limited by DMA boundary
Pierre Ossman55db8902006-11-21 17:55:45 +01003063 * size (512KiB).
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003065 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003066
3067 /*
3068 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003069 * of bytes. When doing hardware scatter/gather, each entry cannot
3070 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003071 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003072 if (host->flags & SDHCI_USE_ADMA) {
3073 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3074 mmc->max_seg_size = 65535;
3075 else
3076 mmc->max_seg_size = 65536;
3077 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003078 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003079 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003080
3081 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003082 * Maximum block size. This varies from controller to controller and
3083 * is specified in the capabilities register.
3084 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003085 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3086 mmc->max_blk_size = 2;
3087 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303088 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003089 SDHCI_MAX_BLOCK_SHIFT;
3090 if (mmc->max_blk_size >= 3) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303091 pr_warning("%s: Invalid maximum block size, "
Anton Vorontsov0633f652009-03-17 00:14:03 +03003092 "assuming 512 bytes\n", mmc_hostname(mmc));
3093 mmc->max_blk_size = 0;
3094 }
3095 }
3096
3097 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003098
3099 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003100 * Maximum block count.
3101 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003102 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003103
3104 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003105 * Init tasklets.
3106 */
3107 tasklet_init(&host->card_tasklet,
3108 sdhci_tasklet_card, (unsigned long)host);
3109 tasklet_init(&host->finish_tasklet,
3110 sdhci_tasklet_finish, (unsigned long)host);
3111
Al Viroe4cad1b2006-10-10 22:47:07 +01003112 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003113
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303114 if (host->version >= SDHCI_SPEC_300) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303115 init_waitqueue_head(&host->buf_ready_int);
3116
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303117 /* Initialize re-tuning timer */
3118 init_timer(&host->tuning_timer);
3119 host->tuning_timer.data = (unsigned long)host;
3120 host->tuning_timer.function = sdhci_tuning_timer;
3121 }
3122
Thomas Gleixnerdace1452006-07-01 19:29:38 -07003123 ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
Pierre Ossmanb69c9052008-03-08 23:44:25 +01003124 mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003125 if (ret) {
3126 pr_err("%s: Failed to request IRQ %d: %d\n",
3127 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003128 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003129 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003130
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003131 sdhci_init(host, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003132
3133#ifdef CONFIG_MMC_DEBUG
3134 sdhci_dumpregs(host);
3135#endif
3136
Pierre Ossmanf9134312008-12-21 17:01:48 +01003137#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003138 snprintf(host->led_name, sizeof(host->led_name),
3139 "%s::", mmc_hostname(mmc));
3140 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003141 host->led.brightness = LED_OFF;
3142 host->led.default_trigger = mmc_hostname(mmc);
3143 host->led.brightness_set = sdhci_led_control;
3144
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003145 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003146 if (ret) {
3147 pr_err("%s: Failed to register LED device: %d\n",
3148 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003149 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003150 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003151#endif
3152
Pierre Ossman5f25a662006-10-04 02:15:39 -07003153 mmiowb();
3154
Pierre Ossmand129bce2006-03-24 03:18:17 -08003155 mmc_add_host(mmc);
3156
Girish K Sa3c76eb2011-10-11 11:44:09 +05303157 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003158 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Richard Röjforsa13abc72009-09-22 16:45:30 -07003159 (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
3160 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003161
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003162 sdhci_enable_card_detection(host);
3163
Pierre Ossmand129bce2006-03-24 03:18:17 -08003164 return 0;
3165
Pierre Ossmanf9134312008-12-21 17:01:48 +01003166#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003167reset:
3168 sdhci_reset(host, SDHCI_RESET_ALL);
Kevin Liub0a8dec2013-01-05 17:18:28 +08003169 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003170 free_irq(host->irq, host);
3171#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003172untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003173 tasklet_kill(&host->card_tasklet);
3174 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003175
3176 return ret;
3177}
3178
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003179EXPORT_SYMBOL_GPL(sdhci_add_host);
3180
Pierre Ossman1e728592008-04-16 19:13:13 +02003181void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003182{
Pierre Ossman1e728592008-04-16 19:13:13 +02003183 unsigned long flags;
3184
3185 if (dead) {
3186 spin_lock_irqsave(&host->lock, flags);
3187
3188 host->flags |= SDHCI_DEVICE_DEAD;
3189
3190 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303191 pr_err("%s: Controller removed during "
Pierre Ossman1e728592008-04-16 19:13:13 +02003192 " transfer!\n", mmc_hostname(host->mmc));
3193
3194 host->mrq->cmd->error = -ENOMEDIUM;
3195 tasklet_schedule(&host->finish_tasklet);
3196 }
3197
3198 spin_unlock_irqrestore(&host->lock, flags);
3199 }
3200
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003201 sdhci_disable_card_detection(host);
3202
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003203 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003204
Pierre Ossmanf9134312008-12-21 17:01:48 +01003205#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003206 led_classdev_unregister(&host->led);
3207#endif
3208
Pierre Ossman1e728592008-04-16 19:13:13 +02003209 if (!dead)
3210 sdhci_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003211
Kevin Liub0a8dec2013-01-05 17:18:28 +08003212 sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003213 free_irq(host->irq, host);
3214
3215 del_timer_sync(&host->timer);
3216
3217 tasklet_kill(&host->card_tasklet);
3218 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003219
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003220 if (host->vmmc) {
3221 regulator_disable(host->vmmc);
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003222 regulator_put(host->vmmc);
Philip Rakity77dcb3f2012-07-23 17:25:18 -07003223 }
Marek Szyprowski9bea3c82010-08-10 18:01:59 -07003224
Philip Rakity6231f3d2012-07-23 15:56:23 -07003225 if (host->vqmmc) {
3226 regulator_disable(host->vqmmc);
3227 regulator_put(host->vqmmc);
3228 }
3229
Pierre Ossman2134a922008-06-28 18:28:51 +02003230 kfree(host->adma_desc);
3231 kfree(host->align_buffer);
3232
3233 host->adma_desc = NULL;
3234 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003235}
3236
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003237EXPORT_SYMBOL_GPL(sdhci_remove_host);
3238
3239void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003240{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003241 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003242}
3243
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003244EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003245
3246/*****************************************************************************\
3247 * *
3248 * Driver init/exit *
3249 * *
3250\*****************************************************************************/
3251
3252static int __init sdhci_drv_init(void)
3253{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303254 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003255 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303256 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003257
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003258 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003259}
3260
3261static void __exit sdhci_drv_exit(void)
3262{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003263}
3264
3265module_init(sdhci_drv_init);
3266module_exit(sdhci_drv_exit);
3267
Pierre Ossmandf673b22006-06-30 02:22:31 -07003268module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003269module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003270
Pierre Ossman32710e82009-04-08 20:14:54 +02003271MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003272MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003273MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003274
Pierre Ossmandf673b22006-06-30 02:22:31 -07003275MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003276MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");